bedrock.rs

   1use std::pin::Pin;
   2use std::str::FromStr;
   3use std::sync::Arc;
   4
   5use crate::ui::InstructionListItem;
   6use anyhow::{Context as _, Result, anyhow};
   7use aws_config::stalled_stream_protection::StalledStreamProtectionConfig;
   8use aws_config::{BehaviorVersion, Region};
   9use aws_credential_types::Credentials;
  10use aws_http_client::AwsHttpClient;
  11use bedrock::bedrock_client::Client as BedrockClient;
  12use bedrock::bedrock_client::config::timeout::TimeoutConfig;
  13use bedrock::bedrock_client::types::{
  14    ContentBlockDelta, ContentBlockStart, ConverseStreamOutput, ReasoningContentBlockDelta,
  15    StopReason,
  16};
  17use bedrock::{
  18    BedrockAnyToolChoice, BedrockAutoToolChoice, BedrockBlob, BedrockError, BedrockInnerContent,
  19    BedrockMessage, BedrockModelMode, BedrockStreamingResponse, BedrockThinkingBlock,
  20    BedrockThinkingTextBlock, BedrockTool, BedrockToolChoice, BedrockToolConfig,
  21    BedrockToolInputSchema, BedrockToolResultBlock, BedrockToolResultContentBlock,
  22    BedrockToolResultStatus, BedrockToolSpec, BedrockToolUseBlock, Model, value_to_aws_document,
  23};
  24use collections::{BTreeMap, HashMap};
  25use credentials_provider::CredentialsProvider;
  26use editor::{Editor, EditorElement, EditorStyle};
  27use futures::{FutureExt, Stream, StreamExt, future::BoxFuture, stream::BoxStream};
  28use gpui::{
  29    AnyView, App, AsyncApp, Context, Entity, FontStyle, FontWeight, Subscription, Task, TextStyle,
  30    WhiteSpace,
  31};
  32use gpui_tokio::Tokio;
  33use http_client::HttpClient;
  34use language_model::{
  35    AuthenticateError, LanguageModel, LanguageModelCacheConfiguration,
  36    LanguageModelCompletionError, LanguageModelCompletionEvent, LanguageModelId, LanguageModelName,
  37    LanguageModelProvider, LanguageModelProviderId, LanguageModelProviderName,
  38    LanguageModelProviderState, LanguageModelRequest, LanguageModelToolChoice,
  39    LanguageModelToolResultContent, LanguageModelToolUse, MessageContent, RateLimiter, Role,
  40    TokenUsage,
  41};
  42use schemars::JsonSchema;
  43use serde::{Deserialize, Serialize};
  44use serde_json::Value;
  45use settings::{Settings, SettingsStore};
  46use smol::lock::OnceCell;
  47use strum::{EnumIter, IntoEnumIterator, IntoStaticStr};
  48use theme::ThemeSettings;
  49use tokio::runtime::Handle;
  50use ui::{Icon, IconName, List, Tooltip, prelude::*};
  51use util::{ResultExt, default};
  52
  53use crate::AllLanguageModelSettings;
  54
  55const PROVIDER_ID: &str = "amazon-bedrock";
  56const PROVIDER_NAME: &str = "Amazon Bedrock";
  57
  58#[derive(Default, Clone, Deserialize, Serialize, PartialEq, Debug)]
  59pub struct BedrockCredentials {
  60    pub access_key_id: String,
  61    pub secret_access_key: String,
  62    pub session_token: Option<String>,
  63    pub region: String,
  64}
  65
  66#[derive(Default, Clone, Debug, PartialEq)]
  67pub struct AmazonBedrockSettings {
  68    pub available_models: Vec<AvailableModel>,
  69    pub region: Option<String>,
  70    pub endpoint: Option<String>,
  71    pub profile_name: Option<String>,
  72    pub role_arn: Option<String>,
  73    pub authentication_method: Option<BedrockAuthMethod>,
  74}
  75
  76#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, EnumIter, IntoStaticStr, JsonSchema)]
  77pub enum BedrockAuthMethod {
  78    #[serde(rename = "named_profile")]
  79    NamedProfile,
  80    #[serde(rename = "sso")]
  81    SingleSignOn,
  82    /// IMDSv2, PodIdentity, env vars, etc.
  83    #[serde(rename = "default")]
  84    Automatic,
  85}
  86
  87#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
  88pub struct AvailableModel {
  89    pub name: String,
  90    pub display_name: Option<String>,
  91    pub max_tokens: u64,
  92    pub cache_configuration: Option<LanguageModelCacheConfiguration>,
  93    pub max_output_tokens: Option<u64>,
  94    pub default_temperature: Option<f32>,
  95    pub mode: Option<ModelMode>,
  96}
  97
  98#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize, JsonSchema)]
  99#[serde(tag = "type", rename_all = "lowercase")]
 100pub enum ModelMode {
 101    #[default]
 102    Default,
 103    Thinking {
 104        /// The maximum number of tokens to use for reasoning. Must be lower than the model's `max_output_tokens`.
 105        budget_tokens: Option<u64>,
 106    },
 107}
 108
 109impl From<ModelMode> for BedrockModelMode {
 110    fn from(value: ModelMode) -> Self {
 111        match value {
 112            ModelMode::Default => BedrockModelMode::Default,
 113            ModelMode::Thinking { budget_tokens } => BedrockModelMode::Thinking { budget_tokens },
 114        }
 115    }
 116}
 117
 118impl From<BedrockModelMode> for ModelMode {
 119    fn from(value: BedrockModelMode) -> Self {
 120        match value {
 121            BedrockModelMode::Default => ModelMode::Default,
 122            BedrockModelMode::Thinking { budget_tokens } => ModelMode::Thinking { budget_tokens },
 123        }
 124    }
 125}
 126
 127/// The URL of the base AWS service.
 128///
 129/// Right now we're just using this as the key to store the AWS credentials
 130/// under in the keychain.
 131const AMAZON_AWS_URL: &str = "https://amazonaws.com";
 132
 133// These environment variables all use a `ZED_` prefix because we don't want to overwrite the user's AWS credentials.
 134const ZED_BEDROCK_ACCESS_KEY_ID_VAR: &str = "ZED_ACCESS_KEY_ID";
 135const ZED_BEDROCK_SECRET_ACCESS_KEY_VAR: &str = "ZED_SECRET_ACCESS_KEY";
 136const ZED_BEDROCK_SESSION_TOKEN_VAR: &str = "ZED_SESSION_TOKEN";
 137const ZED_AWS_PROFILE_VAR: &str = "ZED_AWS_PROFILE";
 138const ZED_BEDROCK_REGION_VAR: &str = "ZED_AWS_REGION";
 139const ZED_AWS_CREDENTIALS_VAR: &str = "ZED_AWS_CREDENTIALS";
 140const ZED_AWS_ENDPOINT_VAR: &str = "ZED_AWS_ENDPOINT";
 141
 142pub struct State {
 143    credentials: Option<BedrockCredentials>,
 144    settings: Option<AmazonBedrockSettings>,
 145    credentials_from_env: bool,
 146    _subscription: Subscription,
 147}
 148
 149impl State {
 150    fn reset_credentials(&self, cx: &mut Context<Self>) -> Task<Result<()>> {
 151        let credentials_provider = <dyn CredentialsProvider>::global(cx);
 152        cx.spawn(async move |this, cx| {
 153            credentials_provider
 154                .delete_credentials(AMAZON_AWS_URL, &cx)
 155                .await
 156                .log_err();
 157            this.update(cx, |this, cx| {
 158                this.credentials = None;
 159                this.credentials_from_env = false;
 160                this.settings = None;
 161                cx.notify();
 162            })
 163        })
 164    }
 165
 166    fn set_credentials(
 167        &mut self,
 168        credentials: BedrockCredentials,
 169        cx: &mut Context<Self>,
 170    ) -> Task<Result<()>> {
 171        let credentials_provider = <dyn CredentialsProvider>::global(cx);
 172        cx.spawn(async move |this, cx| {
 173            credentials_provider
 174                .write_credentials(
 175                    AMAZON_AWS_URL,
 176                    "Bearer",
 177                    &serde_json::to_vec(&credentials)?,
 178                    &cx,
 179                )
 180                .await?;
 181            this.update(cx, |this, cx| {
 182                this.credentials = Some(credentials);
 183                cx.notify();
 184            })
 185        })
 186    }
 187
 188    fn is_authenticated(&self) -> bool {
 189        let derived = self
 190            .settings
 191            .as_ref()
 192            .and_then(|s| s.authentication_method.as_ref());
 193        let creds = self.credentials.as_ref();
 194
 195        derived.is_some() || creds.is_some()
 196    }
 197
 198    fn authenticate(&self, cx: &mut Context<Self>) -> Task<Result<(), AuthenticateError>> {
 199        if self.is_authenticated() {
 200            return Task::ready(Ok(()));
 201        }
 202
 203        let credentials_provider = <dyn CredentialsProvider>::global(cx);
 204        cx.spawn(async move |this, cx| {
 205            let (credentials, from_env) =
 206                if let Ok(credentials) = std::env::var(ZED_AWS_CREDENTIALS_VAR) {
 207                    (credentials, true)
 208                } else {
 209                    let (_, credentials) = credentials_provider
 210                        .read_credentials(AMAZON_AWS_URL, &cx)
 211                        .await?
 212                        .ok_or_else(|| AuthenticateError::CredentialsNotFound)?;
 213                    (
 214                        String::from_utf8(credentials)
 215                            .context("invalid {PROVIDER_NAME} credentials")?,
 216                        false,
 217                    )
 218                };
 219
 220            let credentials: BedrockCredentials =
 221                serde_json::from_str(&credentials).context("failed to parse credentials")?;
 222
 223            this.update(cx, |this, cx| {
 224                this.credentials = Some(credentials);
 225                this.credentials_from_env = from_env;
 226                cx.notify();
 227            })?;
 228
 229            Ok(())
 230        })
 231    }
 232
 233    fn get_region(&self) -> String {
 234        // Get region - from credentials or directly from settings
 235        let credentials_region = self.credentials.as_ref().map(|s| s.region.clone());
 236        let settings_region = self.settings.as_ref().and_then(|s| s.region.clone());
 237
 238        // Use credentials region if available, otherwise use settings region, finally fall back to default
 239        credentials_region
 240            .or(settings_region)
 241            .unwrap_or(String::from("us-east-1"))
 242    }
 243}
 244
 245pub struct BedrockLanguageModelProvider {
 246    http_client: AwsHttpClient,
 247    handler: tokio::runtime::Handle,
 248    state: gpui::Entity<State>,
 249}
 250
 251impl BedrockLanguageModelProvider {
 252    pub fn new(http_client: Arc<dyn HttpClient>, cx: &mut App) -> Self {
 253        let state = cx.new(|cx| State {
 254            credentials: None,
 255            settings: Some(AllLanguageModelSettings::get_global(cx).bedrock.clone()),
 256            credentials_from_env: false,
 257            _subscription: cx.observe_global::<SettingsStore>(|_, cx| {
 258                cx.notify();
 259            }),
 260        });
 261
 262        let tokio_handle = Tokio::handle(cx);
 263
 264        let coerced_client = AwsHttpClient::new(http_client.clone(), tokio_handle.clone());
 265
 266        Self {
 267            http_client: coerced_client,
 268            handler: tokio_handle.clone(),
 269            state,
 270        }
 271    }
 272
 273    fn create_language_model(&self, model: bedrock::Model) -> Arc<dyn LanguageModel> {
 274        Arc::new(BedrockModel {
 275            id: LanguageModelId::from(model.id().to_string()),
 276            model,
 277            http_client: self.http_client.clone(),
 278            handler: self.handler.clone(),
 279            state: self.state.clone(),
 280            client: OnceCell::new(),
 281            request_limiter: RateLimiter::new(4),
 282        })
 283    }
 284}
 285
 286impl LanguageModelProvider for BedrockLanguageModelProvider {
 287    fn id(&self) -> LanguageModelProviderId {
 288        LanguageModelProviderId(PROVIDER_ID.into())
 289    }
 290
 291    fn name(&self) -> LanguageModelProviderName {
 292        LanguageModelProviderName(PROVIDER_NAME.into())
 293    }
 294
 295    fn icon(&self) -> IconName {
 296        IconName::AiBedrock
 297    }
 298
 299    fn default_model(&self, _cx: &App) -> Option<Arc<dyn LanguageModel>> {
 300        Some(self.create_language_model(bedrock::Model::default()))
 301    }
 302
 303    fn default_fast_model(&self, cx: &App) -> Option<Arc<dyn LanguageModel>> {
 304        let region = self.state.read(cx).get_region();
 305        Some(self.create_language_model(bedrock::Model::default_fast(region.as_str())))
 306    }
 307
 308    fn provided_models(&self, cx: &App) -> Vec<Arc<dyn LanguageModel>> {
 309        let mut models = BTreeMap::default();
 310
 311        for model in bedrock::Model::iter() {
 312            if !matches!(model, bedrock::Model::Custom { .. }) {
 313                // TODO: Sonnet 3.7 vs. 3.7 Thinking bug is here.
 314                models.insert(model.id().to_string(), model);
 315            }
 316        }
 317
 318        // Override with available models from settings
 319        for model in AllLanguageModelSettings::get_global(cx)
 320            .bedrock
 321            .available_models
 322            .iter()
 323        {
 324            models.insert(
 325                model.name.clone(),
 326                bedrock::Model::Custom {
 327                    name: model.name.clone(),
 328                    display_name: model.display_name.clone(),
 329                    max_tokens: model.max_tokens,
 330                    max_output_tokens: model.max_output_tokens,
 331                    default_temperature: model.default_temperature,
 332                },
 333            );
 334        }
 335
 336        models
 337            .into_values()
 338            .map(|model| self.create_language_model(model))
 339            .collect()
 340    }
 341
 342    fn is_authenticated(&self, cx: &App) -> bool {
 343        self.state.read(cx).is_authenticated()
 344    }
 345
 346    fn authenticate(&self, cx: &mut App) -> Task<Result<(), AuthenticateError>> {
 347        self.state.update(cx, |state, cx| state.authenticate(cx))
 348    }
 349
 350    fn configuration_view(&self, window: &mut Window, cx: &mut App) -> AnyView {
 351        cx.new(|cx| ConfigurationView::new(self.state.clone(), window, cx))
 352            .into()
 353    }
 354
 355    fn reset_credentials(&self, cx: &mut App) -> Task<Result<()>> {
 356        self.state
 357            .update(cx, |state, cx| state.reset_credentials(cx))
 358    }
 359}
 360
 361impl LanguageModelProviderState for BedrockLanguageModelProvider {
 362    type ObservableEntity = State;
 363
 364    fn observable_entity(&self) -> Option<gpui::Entity<Self::ObservableEntity>> {
 365        Some(self.state.clone())
 366    }
 367}
 368
 369struct BedrockModel {
 370    id: LanguageModelId,
 371    model: Model,
 372    http_client: AwsHttpClient,
 373    handler: tokio::runtime::Handle,
 374    client: OnceCell<BedrockClient>,
 375    state: gpui::Entity<State>,
 376    request_limiter: RateLimiter,
 377}
 378
 379impl BedrockModel {
 380    fn get_or_init_client(&self, cx: &AsyncApp) -> anyhow::Result<&BedrockClient> {
 381        self.client
 382            .get_or_try_init_blocking(|| {
 383                let (auth_method, credentials, endpoint, region, settings) =
 384                    cx.read_entity(&self.state, |state, _cx| {
 385                        let auth_method = state
 386                            .settings
 387                            .as_ref()
 388                            .and_then(|s| s.authentication_method.clone());
 389
 390                        let endpoint = state.settings.as_ref().and_then(|s| s.endpoint.clone());
 391
 392                        let region = state.get_region();
 393
 394                        (
 395                            auth_method,
 396                            state.credentials.clone(),
 397                            endpoint,
 398                            region,
 399                            state.settings.clone(),
 400                        )
 401                    })?;
 402
 403                let mut config_builder = aws_config::defaults(BehaviorVersion::latest())
 404                    .stalled_stream_protection(StalledStreamProtectionConfig::disabled())
 405                    .http_client(self.http_client.clone())
 406                    .region(Region::new(region))
 407                    .timeout_config(TimeoutConfig::disabled());
 408
 409                if let Some(endpoint_url) = endpoint {
 410                    if !endpoint_url.is_empty() {
 411                        config_builder = config_builder.endpoint_url(endpoint_url);
 412                    }
 413                }
 414
 415                match auth_method {
 416                    None => {
 417                        if let Some(creds) = credentials {
 418                            let aws_creds = Credentials::new(
 419                                creds.access_key_id,
 420                                creds.secret_access_key,
 421                                creds.session_token,
 422                                None,
 423                                "zed-bedrock-provider",
 424                            );
 425                            config_builder = config_builder.credentials_provider(aws_creds);
 426                        }
 427                    }
 428                    Some(BedrockAuthMethod::NamedProfile)
 429                    | Some(BedrockAuthMethod::SingleSignOn) => {
 430                        // Currently NamedProfile and SSO behave the same way but only the instructions change
 431                        // Until we support BearerAuth through SSO, this will not change.
 432                        let profile_name = settings
 433                            .and_then(|s| s.profile_name)
 434                            .unwrap_or_else(|| "default".to_string());
 435
 436                        if !profile_name.is_empty() {
 437                            config_builder = config_builder.profile_name(profile_name);
 438                        }
 439                    }
 440                    Some(BedrockAuthMethod::Automatic) => {
 441                        // Use default credential provider chain
 442                    }
 443                }
 444
 445                let config = self.handler.block_on(config_builder.load());
 446                anyhow::Ok(BedrockClient::new(&config))
 447            })
 448            .context("initializing Bedrock client")?;
 449
 450        self.client.get().context("Bedrock client not initialized")
 451    }
 452
 453    fn stream_completion(
 454        &self,
 455        request: bedrock::Request,
 456        cx: &AsyncApp,
 457    ) -> Result<
 458        BoxFuture<'static, BoxStream<'static, Result<BedrockStreamingResponse, BedrockError>>>,
 459    > {
 460        let runtime_client = self
 461            .get_or_init_client(cx)
 462            .cloned()
 463            .context("Bedrock client not initialized")?;
 464        let owned_handle = self.handler.clone();
 465
 466        Ok(async move {
 467            let request = bedrock::stream_completion(runtime_client, request, owned_handle);
 468            request.await.unwrap_or_else(|e| {
 469                futures::stream::once(async move { Err(BedrockError::ClientError(e)) }).boxed()
 470            })
 471        }
 472        .boxed())
 473    }
 474}
 475
 476impl LanguageModel for BedrockModel {
 477    fn id(&self) -> LanguageModelId {
 478        self.id.clone()
 479    }
 480
 481    fn name(&self) -> LanguageModelName {
 482        LanguageModelName::from(self.model.display_name().to_string())
 483    }
 484
 485    fn provider_id(&self) -> LanguageModelProviderId {
 486        LanguageModelProviderId(PROVIDER_ID.into())
 487    }
 488
 489    fn provider_name(&self) -> LanguageModelProviderName {
 490        LanguageModelProviderName(PROVIDER_NAME.into())
 491    }
 492
 493    fn supports_tools(&self) -> bool {
 494        self.model.supports_tool_use()
 495    }
 496
 497    fn supports_images(&self) -> bool {
 498        false
 499    }
 500
 501    fn supports_tool_choice(&self, choice: LanguageModelToolChoice) -> bool {
 502        match choice {
 503            LanguageModelToolChoice::Auto | LanguageModelToolChoice::Any => {
 504                self.model.supports_tool_use()
 505            }
 506            // Add support for None - we'll filter tool calls at response
 507            LanguageModelToolChoice::None => self.model.supports_tool_use(),
 508        }
 509    }
 510
 511    fn telemetry_id(&self) -> String {
 512        format!("bedrock/{}", self.model.id())
 513    }
 514
 515    fn max_token_count(&self) -> u64 {
 516        self.model.max_token_count()
 517    }
 518
 519    fn max_output_tokens(&self) -> Option<u64> {
 520        Some(self.model.max_output_tokens())
 521    }
 522
 523    fn count_tokens(
 524        &self,
 525        request: LanguageModelRequest,
 526        cx: &App,
 527    ) -> BoxFuture<'static, Result<u64>> {
 528        get_bedrock_tokens(request, cx)
 529    }
 530
 531    fn stream_completion(
 532        &self,
 533        request: LanguageModelRequest,
 534        cx: &AsyncApp,
 535    ) -> BoxFuture<
 536        'static,
 537        Result<
 538            BoxStream<'static, Result<LanguageModelCompletionEvent, LanguageModelCompletionError>>,
 539            LanguageModelCompletionError,
 540        >,
 541    > {
 542        let Ok(region) = cx.read_entity(&self.state, |state, _cx| state.get_region()) else {
 543            return async move { Err(anyhow::anyhow!("App State Dropped").into()) }.boxed();
 544        };
 545
 546        let model_id = match self.model.cross_region_inference_id(&region) {
 547            Ok(s) => s,
 548            Err(e) => {
 549                return async move { Err(e.into()) }.boxed();
 550            }
 551        };
 552
 553        let deny_tool_calls = request.tool_choice == Some(LanguageModelToolChoice::None);
 554
 555        let request = match into_bedrock(
 556            request,
 557            model_id,
 558            self.model.default_temperature(),
 559            self.model.max_output_tokens(),
 560            self.model.mode(),
 561        ) {
 562            Ok(request) => request,
 563            Err(err) => return futures::future::ready(Err(err.into())).boxed(),
 564        };
 565
 566        let owned_handle = self.handler.clone();
 567
 568        let request = self.stream_completion(request, cx);
 569        let future = self.request_limiter.stream(async move {
 570            let response = request.map_err(|err| anyhow!(err))?.await;
 571            let events = map_to_language_model_completion_events(response, owned_handle);
 572
 573            if deny_tool_calls {
 574                Ok(deny_tool_use_events(events).boxed())
 575            } else {
 576                Ok(events.boxed())
 577            }
 578        });
 579
 580        async move { Ok(future.await?.boxed()) }.boxed()
 581    }
 582
 583    fn cache_configuration(&self) -> Option<LanguageModelCacheConfiguration> {
 584        None
 585    }
 586}
 587
 588fn deny_tool_use_events(
 589    events: impl Stream<Item = Result<LanguageModelCompletionEvent, LanguageModelCompletionError>>,
 590) -> impl Stream<Item = Result<LanguageModelCompletionEvent, LanguageModelCompletionError>> {
 591    events.map(|event| {
 592        match event {
 593            Ok(LanguageModelCompletionEvent::ToolUse(tool_use)) => {
 594                // Convert tool use to an error message if model decided to call it
 595                Ok(LanguageModelCompletionEvent::Text(format!(
 596                    "\n\n[Error: Tool calls are disabled in this context. Attempted to call '{}']",
 597                    tool_use.name
 598                )))
 599            }
 600            other => other,
 601        }
 602    })
 603}
 604
 605pub fn into_bedrock(
 606    request: LanguageModelRequest,
 607    model: String,
 608    default_temperature: f32,
 609    max_output_tokens: u64,
 610    mode: BedrockModelMode,
 611) -> Result<bedrock::Request> {
 612    let mut new_messages: Vec<BedrockMessage> = Vec::new();
 613    let mut system_message = String::new();
 614
 615    for message in request.messages {
 616        if message.contents_empty() {
 617            continue;
 618        }
 619
 620        match message.role {
 621            Role::User | Role::Assistant => {
 622                let bedrock_message_content: Vec<BedrockInnerContent> = message
 623                    .content
 624                    .into_iter()
 625                    .filter_map(|content| match content {
 626                        MessageContent::Text(text) => {
 627                            if !text.is_empty() {
 628                                Some(BedrockInnerContent::Text(text))
 629                            } else {
 630                                None
 631                            }
 632                        }
 633                        MessageContent::Thinking { text, signature } => {
 634                            if model.contains(Model::DeepSeekR1.request_id()) {
 635                                // DeepSeekR1 doesn't support thinking blocks
 636                                // And the AWS API demands that you strip them
 637                                return None;
 638                            }
 639                            let thinking = BedrockThinkingTextBlock::builder()
 640                                .text(text)
 641                                .set_signature(signature)
 642                                .build()
 643                                .context("failed to build reasoning block")
 644                                .log_err()?;
 645
 646                            Some(BedrockInnerContent::ReasoningContent(
 647                                BedrockThinkingBlock::ReasoningText(thinking),
 648                            ))
 649                        }
 650                        MessageContent::RedactedThinking(blob) => {
 651                            if model.contains(Model::DeepSeekR1.request_id()) {
 652                                // DeepSeekR1 doesn't support thinking blocks
 653                                // And the AWS API demands that you strip them
 654                                return None;
 655                            }
 656                            let redacted =
 657                                BedrockThinkingBlock::RedactedContent(BedrockBlob::new(blob));
 658
 659                            Some(BedrockInnerContent::ReasoningContent(redacted))
 660                        }
 661                        MessageContent::ToolUse(tool_use) => {
 662                            let input = if tool_use.input.is_null() {
 663                                // Bedrock API requires valid JsonValue, not null, for tool use input
 664                                value_to_aws_document(&serde_json::json!({}))
 665                            } else {
 666                                value_to_aws_document(&tool_use.input)
 667                            };
 668                            BedrockToolUseBlock::builder()
 669                                .name(tool_use.name.to_string())
 670                                .tool_use_id(tool_use.id.to_string())
 671                                .input(input)
 672                                .build()
 673                                .context("failed to build Bedrock tool use block")
 674                                .log_err()
 675                                .map(BedrockInnerContent::ToolUse)
 676                        },
 677                        MessageContent::ToolResult(tool_result) => {
 678                            BedrockToolResultBlock::builder()
 679                                .tool_use_id(tool_result.tool_use_id.to_string())
 680                                .content(match tool_result.content {
 681                                    LanguageModelToolResultContent::Text(text) => {
 682                                        BedrockToolResultContentBlock::Text(text.to_string())
 683                                    }
 684                                    LanguageModelToolResultContent::Image(_) => {
 685                                        BedrockToolResultContentBlock::Text(
 686                                            // TODO: Bedrock image support
 687                                            "[Tool responded with an image, but Zed doesn't support these in Bedrock models yet]".to_string()
 688                                        )
 689                                    }
 690                                })
 691                                .status({
 692                                    if tool_result.is_error {
 693                                        BedrockToolResultStatus::Error
 694                                    } else {
 695                                        BedrockToolResultStatus::Success
 696                                    }
 697                                })
 698                                .build()
 699                                .context("failed to build Bedrock tool result block")
 700                                .log_err()
 701                                .map(BedrockInnerContent::ToolResult)
 702                        }
 703                        _ => None,
 704                    })
 705                    .collect();
 706                let bedrock_role = match message.role {
 707                    Role::User => bedrock::BedrockRole::User,
 708                    Role::Assistant => bedrock::BedrockRole::Assistant,
 709                    Role::System => unreachable!("System role should never occur here"),
 710                };
 711                if let Some(last_message) = new_messages.last_mut() {
 712                    if last_message.role == bedrock_role {
 713                        last_message.content.extend(bedrock_message_content);
 714                        continue;
 715                    }
 716                }
 717                new_messages.push(
 718                    BedrockMessage::builder()
 719                        .role(bedrock_role)
 720                        .set_content(Some(bedrock_message_content))
 721                        .build()
 722                        .context("failed to build Bedrock message")?,
 723                );
 724            }
 725            Role::System => {
 726                if !system_message.is_empty() {
 727                    system_message.push_str("\n\n");
 728                }
 729                system_message.push_str(&message.string_contents());
 730            }
 731        }
 732    }
 733
 734    let tool_spec: Vec<BedrockTool> = request
 735        .tools
 736        .iter()
 737        .filter_map(|tool| {
 738            Some(BedrockTool::ToolSpec(
 739                BedrockToolSpec::builder()
 740                    .name(tool.name.clone())
 741                    .description(tool.description.clone())
 742                    .input_schema(BedrockToolInputSchema::Json(value_to_aws_document(
 743                        &tool.input_schema,
 744                    )))
 745                    .build()
 746                    .log_err()?,
 747            ))
 748        })
 749        .collect();
 750
 751    let tool_choice = match request.tool_choice {
 752        Some(LanguageModelToolChoice::Auto) | None => {
 753            BedrockToolChoice::Auto(BedrockAutoToolChoice::builder().build())
 754        }
 755        Some(LanguageModelToolChoice::Any) => {
 756            BedrockToolChoice::Any(BedrockAnyToolChoice::builder().build())
 757        }
 758        Some(LanguageModelToolChoice::None) => {
 759            // For None, we still use Auto but will filter out tool calls in the response
 760            BedrockToolChoice::Auto(BedrockAutoToolChoice::builder().build())
 761        }
 762    };
 763    let tool_config: BedrockToolConfig = BedrockToolConfig::builder()
 764        .set_tools(Some(tool_spec))
 765        .tool_choice(tool_choice)
 766        .build()?;
 767
 768    Ok(bedrock::Request {
 769        model,
 770        messages: new_messages,
 771        max_tokens: max_output_tokens,
 772        system: Some(system_message),
 773        tools: Some(tool_config),
 774        thinking: if let BedrockModelMode::Thinking { budget_tokens } = mode {
 775            Some(bedrock::Thinking::Enabled { budget_tokens })
 776        } else {
 777            None
 778        },
 779        metadata: None,
 780        stop_sequences: Vec::new(),
 781        temperature: request.temperature.or(Some(default_temperature)),
 782        top_k: None,
 783        top_p: None,
 784    })
 785}
 786
 787// TODO: just call the ConverseOutput.usage() method:
 788// https://docs.rs/aws-sdk-bedrockruntime/latest/aws_sdk_bedrockruntime/operation/converse/struct.ConverseOutput.html#method.output
 789pub fn get_bedrock_tokens(
 790    request: LanguageModelRequest,
 791    cx: &App,
 792) -> BoxFuture<'static, Result<u64>> {
 793    cx.background_executor()
 794        .spawn(async move {
 795            let messages = request.messages;
 796            let mut tokens_from_images = 0;
 797            let mut string_messages = Vec::with_capacity(messages.len());
 798
 799            for message in messages {
 800                use language_model::MessageContent;
 801
 802                let mut string_contents = String::new();
 803
 804                for content in message.content {
 805                    match content {
 806                        MessageContent::Text(text) | MessageContent::Thinking { text, .. } => {
 807                            string_contents.push_str(&text);
 808                        }
 809                        MessageContent::RedactedThinking(_) => {}
 810                        MessageContent::Image(image) => {
 811                            tokens_from_images += image.estimate_tokens();
 812                        }
 813                        MessageContent::ToolUse(_tool_use) => {
 814                            // TODO: Estimate token usage from tool uses.
 815                        }
 816                        MessageContent::ToolResult(tool_result) => match tool_result.content {
 817                            LanguageModelToolResultContent::Text(text) => {
 818                                string_contents.push_str(&text);
 819                            }
 820                            LanguageModelToolResultContent::Image(image) => {
 821                                tokens_from_images += image.estimate_tokens();
 822                            }
 823                        },
 824                    }
 825                }
 826
 827                if !string_contents.is_empty() {
 828                    string_messages.push(tiktoken_rs::ChatCompletionRequestMessage {
 829                        role: match message.role {
 830                            Role::User => "user".into(),
 831                            Role::Assistant => "assistant".into(),
 832                            Role::System => "system".into(),
 833                        },
 834                        content: Some(string_contents),
 835                        name: None,
 836                        function_call: None,
 837                    });
 838                }
 839            }
 840
 841            // Tiktoken doesn't yet support these models, so we manually use the
 842            // same tokenizer as GPT-4.
 843            tiktoken_rs::num_tokens_from_messages("gpt-4", &string_messages)
 844                .map(|tokens| (tokens + tokens_from_images) as u64)
 845        })
 846        .boxed()
 847}
 848
 849pub fn map_to_language_model_completion_events(
 850    events: Pin<Box<dyn Send + Stream<Item = Result<BedrockStreamingResponse, BedrockError>>>>,
 851    handle: Handle,
 852) -> impl Stream<Item = Result<LanguageModelCompletionEvent, LanguageModelCompletionError>> {
 853    struct RawToolUse {
 854        id: String,
 855        name: String,
 856        input_json: String,
 857    }
 858
 859    struct State {
 860        events: Pin<Box<dyn Send + Stream<Item = Result<BedrockStreamingResponse, BedrockError>>>>,
 861        tool_uses_by_index: HashMap<i32, RawToolUse>,
 862    }
 863
 864    futures::stream::unfold(
 865        State {
 866            events,
 867            tool_uses_by_index: HashMap::default(),
 868        },
 869        move |mut state: State| {
 870            let inner_handle = handle.clone();
 871            async move {
 872                inner_handle
 873                    .spawn(async {
 874                        while let Some(event) = state.events.next().await {
 875                            match event {
 876                                Ok(event) => match event {
 877                                    ConverseStreamOutput::ContentBlockDelta(cb_delta) => {
 878                                        match cb_delta.delta {
 879                                            Some(ContentBlockDelta::Text(text_out)) => {
 880                                                let completion_event =
 881                                                    LanguageModelCompletionEvent::Text(text_out);
 882                                                return Some((Some(Ok(completion_event)), state));
 883                                            }
 884
 885                                            Some(ContentBlockDelta::ToolUse(text_out)) => {
 886                                                if let Some(tool_use) = state
 887                                                    .tool_uses_by_index
 888                                                    .get_mut(&cb_delta.content_block_index)
 889                                                {
 890                                                    tool_use.input_json.push_str(text_out.input());
 891                                                }
 892                                            }
 893
 894                                            Some(ContentBlockDelta::ReasoningContent(thinking)) => {
 895                                                match thinking {
 896                                                    ReasoningContentBlockDelta::RedactedContent(
 897                                                        redacted,
 898                                                    ) => {
 899                                                        let thinking_event =
 900                                                            LanguageModelCompletionEvent::Thinking {
 901                                                                text: String::from_utf8(
 902                                                                    redacted.into_inner(),
 903                                                                )
 904                                                                .unwrap_or("REDACTED".to_string()),
 905                                                                signature: None,
 906                                                            };
 907
 908                                                        return Some((
 909                                                            Some(Ok(thinking_event)),
 910                                                            state,
 911                                                        ));
 912                                                    }
 913                                                    ReasoningContentBlockDelta::Signature(
 914                                                        signature,
 915                                                    ) => {
 916                                                        return Some((
 917                                                            Some(Ok(LanguageModelCompletionEvent::Thinking {
 918                                                                text: "".to_string(),
 919                                                                signature: Some(signature)
 920                                                            })),
 921                                                            state,
 922                                                        ));
 923                                                    }
 924                                                    ReasoningContentBlockDelta::Text(thoughts) => {
 925                                                        let thinking_event =
 926                                                            LanguageModelCompletionEvent::Thinking {
 927                                                                text: thoughts.to_string(),
 928                                                                signature: None
 929                                                            };
 930
 931                                                        return Some((
 932                                                            Some(Ok(thinking_event)),
 933                                                            state,
 934                                                        ));
 935                                                    }
 936                                                    _ => {}
 937                                                }
 938                                            }
 939                                            _ => {}
 940                                        }
 941                                    }
 942                                    ConverseStreamOutput::ContentBlockStart(cb_start) => {
 943                                        if let Some(ContentBlockStart::ToolUse(text_out)) =
 944                                            cb_start.start
 945                                        {
 946                                            let tool_use = RawToolUse {
 947                                                id: text_out.tool_use_id,
 948                                                name: text_out.name,
 949                                                input_json: String::new(),
 950                                            };
 951
 952                                            state
 953                                                .tool_uses_by_index
 954                                                .insert(cb_start.content_block_index, tool_use);
 955                                        }
 956                                    }
 957                                    ConverseStreamOutput::ContentBlockStop(cb_stop) => {
 958                                        if let Some(tool_use) = state
 959                                            .tool_uses_by_index
 960                                            .remove(&cb_stop.content_block_index)
 961                                        {
 962                                            let tool_use_event = LanguageModelToolUse {
 963                                                id: tool_use.id.into(),
 964                                                name: tool_use.name.into(),
 965                                                is_input_complete: true,
 966                                                raw_input: tool_use.input_json.clone(),
 967                                                input: if tool_use.input_json.is_empty() {
 968                                                    Value::Null
 969                                                } else {
 970                                                    serde_json::Value::from_str(
 971                                                        &tool_use.input_json,
 972                                                    )
 973                                                    .map_err(|err| anyhow!(err))
 974                                                    .unwrap()
 975                                                },
 976                                            };
 977
 978                                            return Some((
 979                                                Some(Ok(LanguageModelCompletionEvent::ToolUse(
 980                                                    tool_use_event,
 981                                                ))),
 982                                                state,
 983                                            ));
 984                                        }
 985                                    }
 986
 987                                    ConverseStreamOutput::Metadata(cb_meta) => {
 988                                        if let Some(metadata) = cb_meta.usage {
 989                                            let completion_event =
 990                                                LanguageModelCompletionEvent::UsageUpdate(
 991                                                    TokenUsage {
 992                                                        input_tokens: metadata.input_tokens as u64,
 993                                                        output_tokens: metadata.output_tokens
 994                                                            as u64,
 995                                                        cache_creation_input_tokens: default(),
 996                                                        cache_read_input_tokens: default(),
 997                                                    },
 998                                                );
 999                                            return Some((Some(Ok(completion_event)), state));
1000                                        }
1001                                    }
1002                                    ConverseStreamOutput::MessageStop(message_stop) => {
1003                                        let reason = match message_stop.stop_reason {
1004                                            StopReason::ContentFiltered => {
1005                                                LanguageModelCompletionEvent::Stop(
1006                                                    language_model::StopReason::EndTurn,
1007                                                )
1008                                            }
1009                                            StopReason::EndTurn => {
1010                                                LanguageModelCompletionEvent::Stop(
1011                                                    language_model::StopReason::EndTurn,
1012                                                )
1013                                            }
1014                                            StopReason::GuardrailIntervened => {
1015                                                LanguageModelCompletionEvent::Stop(
1016                                                    language_model::StopReason::EndTurn,
1017                                                )
1018                                            }
1019                                            StopReason::MaxTokens => {
1020                                                LanguageModelCompletionEvent::Stop(
1021                                                    language_model::StopReason::EndTurn,
1022                                                )
1023                                            }
1024                                            StopReason::StopSequence => {
1025                                                LanguageModelCompletionEvent::Stop(
1026                                                    language_model::StopReason::EndTurn,
1027                                                )
1028                                            }
1029                                            StopReason::ToolUse => {
1030                                                LanguageModelCompletionEvent::Stop(
1031                                                    language_model::StopReason::ToolUse,
1032                                                )
1033                                            }
1034                                            _ => LanguageModelCompletionEvent::Stop(
1035                                                language_model::StopReason::EndTurn,
1036                                            ),
1037                                        };
1038                                        return Some((Some(Ok(reason)), state));
1039                                    }
1040                                    _ => {}
1041                                },
1042
1043                                Err(err) => return Some((Some(Err(anyhow!(err).into())), state)),
1044                            }
1045                        }
1046                        None
1047                    })
1048                    .await
1049                    .log_err()
1050                    .flatten()
1051            }
1052        },
1053    )
1054    .filter_map(|event| async move { event })
1055}
1056
1057struct ConfigurationView {
1058    access_key_id_editor: Entity<Editor>,
1059    secret_access_key_editor: Entity<Editor>,
1060    session_token_editor: Entity<Editor>,
1061    region_editor: Entity<Editor>,
1062    state: gpui::Entity<State>,
1063    load_credentials_task: Option<Task<()>>,
1064}
1065
1066impl ConfigurationView {
1067    const PLACEHOLDER_ACCESS_KEY_ID_TEXT: &'static str = "XXXXXXXXXXXXXXXX";
1068    const PLACEHOLDER_SECRET_ACCESS_KEY_TEXT: &'static str =
1069        "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
1070    const PLACEHOLDER_SESSION_TOKEN_TEXT: &'static str = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
1071    const PLACEHOLDER_REGION: &'static str = "us-east-1";
1072
1073    fn new(state: gpui::Entity<State>, window: &mut Window, cx: &mut Context<Self>) -> Self {
1074        cx.observe(&state, |_, _, cx| {
1075            cx.notify();
1076        })
1077        .detach();
1078
1079        let load_credentials_task = Some(cx.spawn({
1080            let state = state.clone();
1081            async move |this, cx| {
1082                if let Some(task) = state
1083                    .update(cx, |state, cx| state.authenticate(cx))
1084                    .log_err()
1085                {
1086                    // We don't log an error, because "not signed in" is also an error.
1087                    let _ = task.await;
1088                }
1089                this.update(cx, |this, cx| {
1090                    this.load_credentials_task = None;
1091                    cx.notify();
1092                })
1093                .log_err();
1094            }
1095        }));
1096
1097        Self {
1098            access_key_id_editor: cx.new(|cx| {
1099                let mut editor = Editor::single_line(window, cx);
1100                editor.set_placeholder_text(Self::PLACEHOLDER_ACCESS_KEY_ID_TEXT, cx);
1101                editor
1102            }),
1103            secret_access_key_editor: cx.new(|cx| {
1104                let mut editor = Editor::single_line(window, cx);
1105                editor.set_placeholder_text(Self::PLACEHOLDER_SECRET_ACCESS_KEY_TEXT, cx);
1106                editor
1107            }),
1108            session_token_editor: cx.new(|cx| {
1109                let mut editor = Editor::single_line(window, cx);
1110                editor.set_placeholder_text(Self::PLACEHOLDER_SESSION_TOKEN_TEXT, cx);
1111                editor
1112            }),
1113            region_editor: cx.new(|cx| {
1114                let mut editor = Editor::single_line(window, cx);
1115                editor.set_placeholder_text(Self::PLACEHOLDER_REGION, cx);
1116                editor
1117            }),
1118            state,
1119            load_credentials_task,
1120        }
1121    }
1122
1123    fn save_credentials(
1124        &mut self,
1125        _: &menu::Confirm,
1126        _window: &mut Window,
1127        cx: &mut Context<Self>,
1128    ) {
1129        let access_key_id = self
1130            .access_key_id_editor
1131            .read(cx)
1132            .text(cx)
1133            .to_string()
1134            .trim()
1135            .to_string();
1136        let secret_access_key = self
1137            .secret_access_key_editor
1138            .read(cx)
1139            .text(cx)
1140            .to_string()
1141            .trim()
1142            .to_string();
1143        let session_token = self
1144            .session_token_editor
1145            .read(cx)
1146            .text(cx)
1147            .to_string()
1148            .trim()
1149            .to_string();
1150        let session_token = if session_token.is_empty() {
1151            None
1152        } else {
1153            Some(session_token)
1154        };
1155        let region = self
1156            .region_editor
1157            .read(cx)
1158            .text(cx)
1159            .to_string()
1160            .trim()
1161            .to_string();
1162        let region = if region.is_empty() {
1163            "us-east-1".to_string()
1164        } else {
1165            region
1166        };
1167
1168        let state = self.state.clone();
1169        cx.spawn(async move |_, cx| {
1170            state
1171                .update(cx, |state, cx| {
1172                    let credentials: BedrockCredentials = BedrockCredentials {
1173                        region: region.clone(),
1174                        access_key_id: access_key_id.clone(),
1175                        secret_access_key: secret_access_key.clone(),
1176                        session_token: session_token.clone(),
1177                    };
1178
1179                    state.set_credentials(credentials, cx)
1180                })?
1181                .await
1182        })
1183        .detach_and_log_err(cx);
1184    }
1185
1186    fn reset_credentials(&mut self, window: &mut Window, cx: &mut Context<Self>) {
1187        self.access_key_id_editor
1188            .update(cx, |editor, cx| editor.set_text("", window, cx));
1189        self.secret_access_key_editor
1190            .update(cx, |editor, cx| editor.set_text("", window, cx));
1191        self.session_token_editor
1192            .update(cx, |editor, cx| editor.set_text("", window, cx));
1193        self.region_editor
1194            .update(cx, |editor, cx| editor.set_text("", window, cx));
1195
1196        let state = self.state.clone();
1197        cx.spawn(async move |_, cx| {
1198            state
1199                .update(cx, |state, cx| state.reset_credentials(cx))?
1200                .await
1201        })
1202        .detach_and_log_err(cx);
1203    }
1204
1205    fn make_text_style(&self, cx: &Context<Self>) -> TextStyle {
1206        let settings = ThemeSettings::get_global(cx);
1207        TextStyle {
1208            color: cx.theme().colors().text,
1209            font_family: settings.ui_font.family.clone(),
1210            font_features: settings.ui_font.features.clone(),
1211            font_fallbacks: settings.ui_font.fallbacks.clone(),
1212            font_size: rems(0.875).into(),
1213            font_weight: settings.ui_font.weight,
1214            font_style: FontStyle::Normal,
1215            line_height: relative(1.3),
1216            background_color: None,
1217            underline: None,
1218            strikethrough: None,
1219            white_space: WhiteSpace::Normal,
1220            text_overflow: None,
1221            text_align: Default::default(),
1222            line_clamp: None,
1223        }
1224    }
1225
1226    fn make_input_styles(&self, cx: &Context<Self>) -> Div {
1227        let bg_color = cx.theme().colors().editor_background;
1228        let border_color = cx.theme().colors().border;
1229
1230        h_flex()
1231            .w_full()
1232            .px_2()
1233            .py_1()
1234            .bg(bg_color)
1235            .border_1()
1236            .border_color(border_color)
1237            .rounded_sm()
1238    }
1239
1240    fn should_render_editor(&self, cx: &Context<Self>) -> bool {
1241        self.state.read(cx).is_authenticated()
1242    }
1243}
1244
1245impl Render for ConfigurationView {
1246    fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
1247        let env_var_set = self.state.read(cx).credentials_from_env;
1248        let bedrock_settings = self.state.read(cx).settings.as_ref();
1249        let bedrock_method = bedrock_settings
1250            .as_ref()
1251            .and_then(|s| s.authentication_method.clone());
1252
1253        if self.load_credentials_task.is_some() {
1254            return div().child(Label::new("Loading credentials...")).into_any();
1255        }
1256
1257        if self.should_render_editor(cx) {
1258            return h_flex()
1259                .mt_1()
1260                .p_1()
1261                .justify_between()
1262                .rounded_md()
1263                .border_1()
1264                .border_color(cx.theme().colors().border)
1265                .bg(cx.theme().colors().background)
1266                .child(
1267                    h_flex()
1268                        .gap_1()
1269                        .child(Icon::new(IconName::Check).color(Color::Success))
1270                        .child(Label::new(if env_var_set {
1271                            format!("Access Key ID is set in {ZED_BEDROCK_ACCESS_KEY_ID_VAR}, Secret Key is set in {ZED_BEDROCK_SECRET_ACCESS_KEY_VAR}, Region is set in {ZED_BEDROCK_REGION_VAR} environment variables.")
1272                        } else {
1273                            match bedrock_method {
1274                                Some(BedrockAuthMethod::Automatic) => "You are using automatic credentials".into(),
1275                                Some(BedrockAuthMethod::NamedProfile) => {
1276                                    "You are using named profile".into()
1277                                },
1278                                Some(BedrockAuthMethod::SingleSignOn) => "You are using a single sign on profile".into(),
1279                                None => "You are using static credentials".into(),
1280                            }
1281                        })),
1282                )
1283                .child(
1284                    Button::new("reset-key", "Reset Key")
1285                        .icon(Some(IconName::Trash))
1286                        .icon_size(IconSize::Small)
1287                        .icon_position(IconPosition::Start)
1288                        .disabled(env_var_set || bedrock_method.is_some())
1289                        .when(env_var_set, |this| {
1290                            this.tooltip(Tooltip::text(format!("To reset your credentials, unset the {ZED_BEDROCK_ACCESS_KEY_ID_VAR}, {ZED_BEDROCK_SECRET_ACCESS_KEY_VAR}, and {ZED_BEDROCK_REGION_VAR} environment variables.")))
1291                        })
1292                        .when(bedrock_method.is_some(), |this| {
1293                            this.tooltip(Tooltip::text("You cannot reset credentials as they're being derived, check Zed settings to understand how"))
1294                        })
1295                        .on_click(cx.listener(|this, _, window, cx| this.reset_credentials(window, cx))),
1296                )
1297                .into_any();
1298        }
1299
1300        v_flex()
1301            .size_full()
1302            .on_action(cx.listener(ConfigurationView::save_credentials))
1303            .child(Label::new("To use Zed's assistant with Bedrock, you can set a custom authentication strategy through the settings.json, or use static credentials."))
1304            .child(Label::new("But, to access models on AWS, you need to:").mt_1())
1305            .child(
1306                List::new()
1307                    .child(
1308                        InstructionListItem::new(
1309                            "Grant permissions to the strategy you'll use according to the:",
1310                            Some("Prerequisites"),
1311                            Some("https://docs.aws.amazon.com/bedrock/latest/userguide/inference-prereq.html"),
1312                        )
1313                    )
1314                    .child(
1315                        InstructionListItem::new(
1316                            "Select the models you would like access to:",
1317                            Some("Bedrock Model Catalog"),
1318                            Some("https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/modelaccess"),
1319                        )
1320                    )
1321            )
1322            .child(self.render_static_credentials_ui(cx))
1323            .child(self.render_common_fields(cx))
1324            .child(
1325                Label::new(
1326                    format!("You can also assign the {ZED_BEDROCK_ACCESS_KEY_ID_VAR}, {ZED_BEDROCK_SECRET_ACCESS_KEY_VAR} AND {ZED_BEDROCK_REGION_VAR} environment variables and restart Zed."),
1327                )
1328                    .size(LabelSize::Small)
1329                    .color(Color::Muted)
1330                    .my_1(),
1331            )
1332            .child(
1333                Label::new(
1334                    format!("Optionally, if your environment uses AWS CLI profiles, you can set {ZED_AWS_PROFILE_VAR}; if it requires a custom endpoint, you can set {ZED_AWS_ENDPOINT_VAR}; and if it requires a Session Token, you can set {ZED_BEDROCK_SESSION_TOKEN_VAR}."),
1335                )
1336                    .size(LabelSize::Small)
1337                    .color(Color::Muted),
1338            )
1339            .into_any()
1340    }
1341}
1342
1343impl ConfigurationView {
1344    fn render_access_key_id_editor(&self, cx: &mut Context<Self>) -> impl IntoElement {
1345        let text_style = self.make_text_style(cx);
1346
1347        EditorElement::new(
1348            &self.access_key_id_editor,
1349            EditorStyle {
1350                background: cx.theme().colors().editor_background,
1351                local_player: cx.theme().players().local(),
1352                text: text_style,
1353                ..Default::default()
1354            },
1355        )
1356    }
1357
1358    fn render_secret_key_editor(&self, cx: &mut Context<Self>) -> impl IntoElement {
1359        let text_style = self.make_text_style(cx);
1360
1361        EditorElement::new(
1362            &self.secret_access_key_editor,
1363            EditorStyle {
1364                background: cx.theme().colors().editor_background,
1365                local_player: cx.theme().players().local(),
1366                text: text_style,
1367                ..Default::default()
1368            },
1369        )
1370    }
1371
1372    fn render_session_token_editor(&self, cx: &mut Context<Self>) -> impl IntoElement {
1373        let text_style = self.make_text_style(cx);
1374
1375        EditorElement::new(
1376            &self.session_token_editor,
1377            EditorStyle {
1378                background: cx.theme().colors().editor_background,
1379                local_player: cx.theme().players().local(),
1380                text: text_style,
1381                ..Default::default()
1382            },
1383        )
1384    }
1385
1386    fn render_region_editor(&self, cx: &mut Context<Self>) -> impl IntoElement {
1387        let text_style = self.make_text_style(cx);
1388
1389        EditorElement::new(
1390            &self.region_editor,
1391            EditorStyle {
1392                background: cx.theme().colors().editor_background,
1393                local_player: cx.theme().players().local(),
1394                text: text_style,
1395                ..Default::default()
1396            },
1397        )
1398    }
1399
1400    fn render_static_credentials_ui(&self, cx: &mut Context<Self>) -> AnyElement {
1401        v_flex()
1402            .my_2()
1403            .gap_1p5()
1404            .child(
1405                Label::new("Static Keys")
1406                    .size(LabelSize::Default)
1407                    .weight(FontWeight::BOLD),
1408            )
1409            .child(
1410                Label::new(
1411                    "This method uses your AWS access key ID and secret access key directly.",
1412                )
1413            )
1414            .child(
1415                List::new()
1416                    .child(InstructionListItem::new(
1417                        "Create an IAM user in the AWS console with programmatic access",
1418                        Some("IAM Console"),
1419                        Some("https://us-east-1.console.aws.amazon.com/iam/home?region=us-east-1#/users"),
1420                    ))
1421                    .child(InstructionListItem::new(
1422                        "Attach the necessary Bedrock permissions to this ",
1423                        Some("user"),
1424                        Some("https://docs.aws.amazon.com/bedrock/latest/userguide/inference-prereq.html"),
1425                    ))
1426                    .child(InstructionListItem::text_only(
1427                        "Copy the access key ID and secret access key when provided",
1428                    ))
1429                    .child(InstructionListItem::text_only(
1430                        "Enter these credentials below",
1431                    )),
1432            )
1433            .child(
1434                v_flex()
1435                    .gap_0p5()
1436                    .child(Label::new("Access Key ID").size(LabelSize::Small))
1437                    .child(
1438                        self.make_input_styles(cx)
1439                            .child(self.render_access_key_id_editor(cx)),
1440                    ),
1441            )
1442            .child(
1443                v_flex()
1444                    .gap_0p5()
1445                    .child(Label::new("Secret Access Key").size(LabelSize::Small))
1446                    .child(self.make_input_styles(cx).child(self.render_secret_key_editor(cx))),
1447            )
1448            .child(
1449                v_flex()
1450                    .gap_0p5()
1451                    .child(Label::new("Session Token (Optional)").size(LabelSize::Small))
1452                    .child(
1453                        self.make_input_styles(cx)
1454                            .child(self.render_session_token_editor(cx)),
1455                    ),
1456            )
1457            .into_any_element()
1458    }
1459
1460    fn render_common_fields(&self, cx: &mut Context<Self>) -> AnyElement {
1461        v_flex()
1462            .gap_0p5()
1463            .child(Label::new("Region").size(LabelSize::Small))
1464            .child(
1465                self.make_input_styles(cx)
1466                    .child(self.render_region_editor(cx)),
1467            )
1468            .into_any_element()
1469    }
1470}