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                            let thinking = BedrockThinkingTextBlock::builder()
 635                                .text(text)
 636                                .set_signature(signature)
 637                                .build()
 638                                .context("failed to build reasoning block")
 639                                .log_err()?;
 640
 641                            Some(BedrockInnerContent::ReasoningContent(
 642                                BedrockThinkingBlock::ReasoningText(thinking),
 643                            ))
 644                        }
 645                        MessageContent::RedactedThinking(blob) => {
 646                            let redacted =
 647                                BedrockThinkingBlock::RedactedContent(BedrockBlob::new(blob));
 648
 649                            Some(BedrockInnerContent::ReasoningContent(redacted))
 650                        }
 651                        MessageContent::ToolUse(tool_use) => {
 652                            let input = if tool_use.input.is_null() {
 653                                // Bedrock API requires valid JsonValue, not null, for tool use input
 654                                value_to_aws_document(&serde_json::json!({}))
 655                            } else {
 656                                value_to_aws_document(&tool_use.input)
 657                            };
 658                            BedrockToolUseBlock::builder()
 659                                .name(tool_use.name.to_string())
 660                                .tool_use_id(tool_use.id.to_string())
 661                                .input(input)
 662                                .build()
 663                                .context("failed to build Bedrock tool use block")
 664                                .log_err()
 665                                .map(BedrockInnerContent::ToolUse)
 666                        },
 667                        MessageContent::ToolResult(tool_result) => {
 668                            BedrockToolResultBlock::builder()
 669                                .tool_use_id(tool_result.tool_use_id.to_string())
 670                                .content(match tool_result.content {
 671                                    LanguageModelToolResultContent::Text(text) => {
 672                                        BedrockToolResultContentBlock::Text(text.to_string())
 673                                    }
 674                                    LanguageModelToolResultContent::Image(_) => {
 675                                        BedrockToolResultContentBlock::Text(
 676                                            // TODO: Bedrock image support
 677                                            "[Tool responded with an image, but Zed doesn't support these in Bedrock models yet]".to_string()
 678                                        )
 679                                    }
 680                                })
 681                                .status({
 682                                    if tool_result.is_error {
 683                                        BedrockToolResultStatus::Error
 684                                    } else {
 685                                        BedrockToolResultStatus::Success
 686                                    }
 687                                })
 688                                .build()
 689                                .context("failed to build Bedrock tool result block")
 690                                .log_err()
 691                                .map(BedrockInnerContent::ToolResult)
 692                        }
 693                        _ => None,
 694                    })
 695                    .collect();
 696                let bedrock_role = match message.role {
 697                    Role::User => bedrock::BedrockRole::User,
 698                    Role::Assistant => bedrock::BedrockRole::Assistant,
 699                    Role::System => unreachable!("System role should never occur here"),
 700                };
 701                if let Some(last_message) = new_messages.last_mut() {
 702                    if last_message.role == bedrock_role {
 703                        last_message.content.extend(bedrock_message_content);
 704                        continue;
 705                    }
 706                }
 707                new_messages.push(
 708                    BedrockMessage::builder()
 709                        .role(bedrock_role)
 710                        .set_content(Some(bedrock_message_content))
 711                        .build()
 712                        .context("failed to build Bedrock message")?,
 713                );
 714            }
 715            Role::System => {
 716                if !system_message.is_empty() {
 717                    system_message.push_str("\n\n");
 718                }
 719                system_message.push_str(&message.string_contents());
 720            }
 721        }
 722    }
 723
 724    let tool_spec: Vec<BedrockTool> = request
 725        .tools
 726        .iter()
 727        .filter_map(|tool| {
 728            Some(BedrockTool::ToolSpec(
 729                BedrockToolSpec::builder()
 730                    .name(tool.name.clone())
 731                    .description(tool.description.clone())
 732                    .input_schema(BedrockToolInputSchema::Json(value_to_aws_document(
 733                        &tool.input_schema,
 734                    )))
 735                    .build()
 736                    .log_err()?,
 737            ))
 738        })
 739        .collect();
 740
 741    let tool_choice = match request.tool_choice {
 742        Some(LanguageModelToolChoice::Auto) | None => {
 743            BedrockToolChoice::Auto(BedrockAutoToolChoice::builder().build())
 744        }
 745        Some(LanguageModelToolChoice::Any) => {
 746            BedrockToolChoice::Any(BedrockAnyToolChoice::builder().build())
 747        }
 748        Some(LanguageModelToolChoice::None) => {
 749            // For None, we still use Auto but will filter out tool calls in the response
 750            BedrockToolChoice::Auto(BedrockAutoToolChoice::builder().build())
 751        }
 752    };
 753    let tool_config: BedrockToolConfig = BedrockToolConfig::builder()
 754        .set_tools(Some(tool_spec))
 755        .tool_choice(tool_choice)
 756        .build()?;
 757
 758    Ok(bedrock::Request {
 759        model,
 760        messages: new_messages,
 761        max_tokens: max_output_tokens,
 762        system: Some(system_message),
 763        tools: Some(tool_config),
 764        thinking: if let BedrockModelMode::Thinking { budget_tokens } = mode {
 765            Some(bedrock::Thinking::Enabled { budget_tokens })
 766        } else {
 767            None
 768        },
 769        metadata: None,
 770        stop_sequences: Vec::new(),
 771        temperature: request.temperature.or(Some(default_temperature)),
 772        top_k: None,
 773        top_p: None,
 774    })
 775}
 776
 777// TODO: just call the ConverseOutput.usage() method:
 778// https://docs.rs/aws-sdk-bedrockruntime/latest/aws_sdk_bedrockruntime/operation/converse/struct.ConverseOutput.html#method.output
 779pub fn get_bedrock_tokens(
 780    request: LanguageModelRequest,
 781    cx: &App,
 782) -> BoxFuture<'static, Result<u64>> {
 783    cx.background_executor()
 784        .spawn(async move {
 785            let messages = request.messages;
 786            let mut tokens_from_images = 0;
 787            let mut string_messages = Vec::with_capacity(messages.len());
 788
 789            for message in messages {
 790                use language_model::MessageContent;
 791
 792                let mut string_contents = String::new();
 793
 794                for content in message.content {
 795                    match content {
 796                        MessageContent::Text(text) | MessageContent::Thinking { text, .. } => {
 797                            string_contents.push_str(&text);
 798                        }
 799                        MessageContent::RedactedThinking(_) => {}
 800                        MessageContent::Image(image) => {
 801                            tokens_from_images += image.estimate_tokens();
 802                        }
 803                        MessageContent::ToolUse(_tool_use) => {
 804                            // TODO: Estimate token usage from tool uses.
 805                        }
 806                        MessageContent::ToolResult(tool_result) => match tool_result.content {
 807                            LanguageModelToolResultContent::Text(text) => {
 808                                string_contents.push_str(&text);
 809                            }
 810                            LanguageModelToolResultContent::Image(image) => {
 811                                tokens_from_images += image.estimate_tokens();
 812                            }
 813                        },
 814                    }
 815                }
 816
 817                if !string_contents.is_empty() {
 818                    string_messages.push(tiktoken_rs::ChatCompletionRequestMessage {
 819                        role: match message.role {
 820                            Role::User => "user".into(),
 821                            Role::Assistant => "assistant".into(),
 822                            Role::System => "system".into(),
 823                        },
 824                        content: Some(string_contents),
 825                        name: None,
 826                        function_call: None,
 827                    });
 828                }
 829            }
 830
 831            // Tiktoken doesn't yet support these models, so we manually use the
 832            // same tokenizer as GPT-4.
 833            tiktoken_rs::num_tokens_from_messages("gpt-4", &string_messages)
 834                .map(|tokens| (tokens + tokens_from_images) as u64)
 835        })
 836        .boxed()
 837}
 838
 839pub fn map_to_language_model_completion_events(
 840    events: Pin<Box<dyn Send + Stream<Item = Result<BedrockStreamingResponse, BedrockError>>>>,
 841    handle: Handle,
 842) -> impl Stream<Item = Result<LanguageModelCompletionEvent, LanguageModelCompletionError>> {
 843    struct RawToolUse {
 844        id: String,
 845        name: String,
 846        input_json: String,
 847    }
 848
 849    struct State {
 850        events: Pin<Box<dyn Send + Stream<Item = Result<BedrockStreamingResponse, BedrockError>>>>,
 851        tool_uses_by_index: HashMap<i32, RawToolUse>,
 852    }
 853
 854    futures::stream::unfold(
 855        State {
 856            events,
 857            tool_uses_by_index: HashMap::default(),
 858        },
 859        move |mut state: State| {
 860            let inner_handle = handle.clone();
 861            async move {
 862                inner_handle
 863                    .spawn(async {
 864                        while let Some(event) = state.events.next().await {
 865                            match event {
 866                                Ok(event) => match event {
 867                                    ConverseStreamOutput::ContentBlockDelta(cb_delta) => {
 868                                        match cb_delta.delta {
 869                                            Some(ContentBlockDelta::Text(text_out)) => {
 870                                                let completion_event =
 871                                                    LanguageModelCompletionEvent::Text(text_out);
 872                                                return Some((Some(Ok(completion_event)), state));
 873                                            }
 874
 875                                            Some(ContentBlockDelta::ToolUse(text_out)) => {
 876                                                if let Some(tool_use) = state
 877                                                    .tool_uses_by_index
 878                                                    .get_mut(&cb_delta.content_block_index)
 879                                                {
 880                                                    tool_use.input_json.push_str(text_out.input());
 881                                                }
 882                                            }
 883
 884                                            Some(ContentBlockDelta::ReasoningContent(thinking)) => {
 885                                                match thinking {
 886                                                    ReasoningContentBlockDelta::RedactedContent(
 887                                                        redacted,
 888                                                    ) => {
 889                                                        let thinking_event =
 890                                                            LanguageModelCompletionEvent::Thinking {
 891                                                                text: String::from_utf8(
 892                                                                    redacted.into_inner(),
 893                                                                )
 894                                                                .unwrap_or("REDACTED".to_string()),
 895                                                                signature: None,
 896                                                            };
 897
 898                                                        return Some((
 899                                                            Some(Ok(thinking_event)),
 900                                                            state,
 901                                                        ));
 902                                                    }
 903                                                    ReasoningContentBlockDelta::Signature(
 904                                                        signature,
 905                                                    ) => {
 906                                                        return Some((
 907                                                            Some(Ok(LanguageModelCompletionEvent::Thinking {
 908                                                                text: "".to_string(),
 909                                                                signature: Some(signature)
 910                                                            })),
 911                                                            state,
 912                                                        ));
 913                                                    }
 914                                                    ReasoningContentBlockDelta::Text(thoughts) => {
 915                                                        let thinking_event =
 916                                                            LanguageModelCompletionEvent::Thinking {
 917                                                                text: thoughts.to_string(),
 918                                                                signature: None
 919                                                            };
 920
 921                                                        return Some((
 922                                                            Some(Ok(thinking_event)),
 923                                                            state,
 924                                                        ));
 925                                                    }
 926                                                    _ => {}
 927                                                }
 928                                            }
 929                                            _ => {}
 930                                        }
 931                                    }
 932                                    ConverseStreamOutput::ContentBlockStart(cb_start) => {
 933                                        if let Some(ContentBlockStart::ToolUse(text_out)) =
 934                                            cb_start.start
 935                                        {
 936                                            let tool_use = RawToolUse {
 937                                                id: text_out.tool_use_id,
 938                                                name: text_out.name,
 939                                                input_json: String::new(),
 940                                            };
 941
 942                                            state
 943                                                .tool_uses_by_index
 944                                                .insert(cb_start.content_block_index, tool_use);
 945                                        }
 946                                    }
 947                                    ConverseStreamOutput::ContentBlockStop(cb_stop) => {
 948                                        if let Some(tool_use) = state
 949                                            .tool_uses_by_index
 950                                            .remove(&cb_stop.content_block_index)
 951                                        {
 952                                            let tool_use_event = LanguageModelToolUse {
 953                                                id: tool_use.id.into(),
 954                                                name: tool_use.name.into(),
 955                                                is_input_complete: true,
 956                                                raw_input: tool_use.input_json.clone(),
 957                                                input: if tool_use.input_json.is_empty() {
 958                                                    Value::Null
 959                                                } else {
 960                                                    serde_json::Value::from_str(
 961                                                        &tool_use.input_json,
 962                                                    )
 963                                                    .map_err(|err| anyhow!(err))
 964                                                    .unwrap()
 965                                                },
 966                                            };
 967
 968                                            return Some((
 969                                                Some(Ok(LanguageModelCompletionEvent::ToolUse(
 970                                                    tool_use_event,
 971                                                ))),
 972                                                state,
 973                                            ));
 974                                        }
 975                                    }
 976
 977                                    ConverseStreamOutput::Metadata(cb_meta) => {
 978                                        if let Some(metadata) = cb_meta.usage {
 979                                            let completion_event =
 980                                                LanguageModelCompletionEvent::UsageUpdate(
 981                                                    TokenUsage {
 982                                                        input_tokens: metadata.input_tokens as u64,
 983                                                        output_tokens: metadata.output_tokens
 984                                                            as u64,
 985                                                        cache_creation_input_tokens: default(),
 986                                                        cache_read_input_tokens: default(),
 987                                                    },
 988                                                );
 989                                            return Some((Some(Ok(completion_event)), state));
 990                                        }
 991                                    }
 992                                    ConverseStreamOutput::MessageStop(message_stop) => {
 993                                        let reason = match message_stop.stop_reason {
 994                                            StopReason::ContentFiltered => {
 995                                                LanguageModelCompletionEvent::Stop(
 996                                                    language_model::StopReason::EndTurn,
 997                                                )
 998                                            }
 999                                            StopReason::EndTurn => {
1000                                                LanguageModelCompletionEvent::Stop(
1001                                                    language_model::StopReason::EndTurn,
1002                                                )
1003                                            }
1004                                            StopReason::GuardrailIntervened => {
1005                                                LanguageModelCompletionEvent::Stop(
1006                                                    language_model::StopReason::EndTurn,
1007                                                )
1008                                            }
1009                                            StopReason::MaxTokens => {
1010                                                LanguageModelCompletionEvent::Stop(
1011                                                    language_model::StopReason::EndTurn,
1012                                                )
1013                                            }
1014                                            StopReason::StopSequence => {
1015                                                LanguageModelCompletionEvent::Stop(
1016                                                    language_model::StopReason::EndTurn,
1017                                                )
1018                                            }
1019                                            StopReason::ToolUse => {
1020                                                LanguageModelCompletionEvent::Stop(
1021                                                    language_model::StopReason::ToolUse,
1022                                                )
1023                                            }
1024                                            _ => LanguageModelCompletionEvent::Stop(
1025                                                language_model::StopReason::EndTurn,
1026                                            ),
1027                                        };
1028                                        return Some((Some(Ok(reason)), state));
1029                                    }
1030                                    _ => {}
1031                                },
1032
1033                                Err(err) => return Some((Some(Err(anyhow!(err).into())), state)),
1034                            }
1035                        }
1036                        None
1037                    })
1038                    .await
1039                    .log_err()
1040                    .flatten()
1041            }
1042        },
1043    )
1044    .filter_map(|event| async move { event })
1045}
1046
1047struct ConfigurationView {
1048    access_key_id_editor: Entity<Editor>,
1049    secret_access_key_editor: Entity<Editor>,
1050    session_token_editor: Entity<Editor>,
1051    region_editor: Entity<Editor>,
1052    state: gpui::Entity<State>,
1053    load_credentials_task: Option<Task<()>>,
1054}
1055
1056impl ConfigurationView {
1057    const PLACEHOLDER_ACCESS_KEY_ID_TEXT: &'static str = "XXXXXXXXXXXXXXXX";
1058    const PLACEHOLDER_SECRET_ACCESS_KEY_TEXT: &'static str =
1059        "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
1060    const PLACEHOLDER_SESSION_TOKEN_TEXT: &'static str = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
1061    const PLACEHOLDER_REGION: &'static str = "us-east-1";
1062
1063    fn new(state: gpui::Entity<State>, window: &mut Window, cx: &mut Context<Self>) -> Self {
1064        cx.observe(&state, |_, _, cx| {
1065            cx.notify();
1066        })
1067        .detach();
1068
1069        let load_credentials_task = Some(cx.spawn({
1070            let state = state.clone();
1071            async move |this, cx| {
1072                if let Some(task) = state
1073                    .update(cx, |state, cx| state.authenticate(cx))
1074                    .log_err()
1075                {
1076                    // We don't log an error, because "not signed in" is also an error.
1077                    let _ = task.await;
1078                }
1079                this.update(cx, |this, cx| {
1080                    this.load_credentials_task = None;
1081                    cx.notify();
1082                })
1083                .log_err();
1084            }
1085        }));
1086
1087        Self {
1088            access_key_id_editor: cx.new(|cx| {
1089                let mut editor = Editor::single_line(window, cx);
1090                editor.set_placeholder_text(Self::PLACEHOLDER_ACCESS_KEY_ID_TEXT, cx);
1091                editor
1092            }),
1093            secret_access_key_editor: cx.new(|cx| {
1094                let mut editor = Editor::single_line(window, cx);
1095                editor.set_placeholder_text(Self::PLACEHOLDER_SECRET_ACCESS_KEY_TEXT, cx);
1096                editor
1097            }),
1098            session_token_editor: cx.new(|cx| {
1099                let mut editor = Editor::single_line(window, cx);
1100                editor.set_placeholder_text(Self::PLACEHOLDER_SESSION_TOKEN_TEXT, cx);
1101                editor
1102            }),
1103            region_editor: cx.new(|cx| {
1104                let mut editor = Editor::single_line(window, cx);
1105                editor.set_placeholder_text(Self::PLACEHOLDER_REGION, cx);
1106                editor
1107            }),
1108            state,
1109            load_credentials_task,
1110        }
1111    }
1112
1113    fn save_credentials(
1114        &mut self,
1115        _: &menu::Confirm,
1116        _window: &mut Window,
1117        cx: &mut Context<Self>,
1118    ) {
1119        let access_key_id = self
1120            .access_key_id_editor
1121            .read(cx)
1122            .text(cx)
1123            .to_string()
1124            .trim()
1125            .to_string();
1126        let secret_access_key = self
1127            .secret_access_key_editor
1128            .read(cx)
1129            .text(cx)
1130            .to_string()
1131            .trim()
1132            .to_string();
1133        let session_token = self
1134            .session_token_editor
1135            .read(cx)
1136            .text(cx)
1137            .to_string()
1138            .trim()
1139            .to_string();
1140        let session_token = if session_token.is_empty() {
1141            None
1142        } else {
1143            Some(session_token)
1144        };
1145        let region = self
1146            .region_editor
1147            .read(cx)
1148            .text(cx)
1149            .to_string()
1150            .trim()
1151            .to_string();
1152        let region = if region.is_empty() {
1153            "us-east-1".to_string()
1154        } else {
1155            region
1156        };
1157
1158        let state = self.state.clone();
1159        cx.spawn(async move |_, cx| {
1160            state
1161                .update(cx, |state, cx| {
1162                    let credentials: BedrockCredentials = BedrockCredentials {
1163                        region: region.clone(),
1164                        access_key_id: access_key_id.clone(),
1165                        secret_access_key: secret_access_key.clone(),
1166                        session_token: session_token.clone(),
1167                    };
1168
1169                    state.set_credentials(credentials, cx)
1170                })?
1171                .await
1172        })
1173        .detach_and_log_err(cx);
1174    }
1175
1176    fn reset_credentials(&mut self, window: &mut Window, cx: &mut Context<Self>) {
1177        self.access_key_id_editor
1178            .update(cx, |editor, cx| editor.set_text("", window, cx));
1179        self.secret_access_key_editor
1180            .update(cx, |editor, cx| editor.set_text("", window, cx));
1181        self.session_token_editor
1182            .update(cx, |editor, cx| editor.set_text("", window, cx));
1183        self.region_editor
1184            .update(cx, |editor, cx| editor.set_text("", window, cx));
1185
1186        let state = self.state.clone();
1187        cx.spawn(async move |_, cx| {
1188            state
1189                .update(cx, |state, cx| state.reset_credentials(cx))?
1190                .await
1191        })
1192        .detach_and_log_err(cx);
1193    }
1194
1195    fn make_text_style(&self, cx: &Context<Self>) -> TextStyle {
1196        let settings = ThemeSettings::get_global(cx);
1197        TextStyle {
1198            color: cx.theme().colors().text,
1199            font_family: settings.ui_font.family.clone(),
1200            font_features: settings.ui_font.features.clone(),
1201            font_fallbacks: settings.ui_font.fallbacks.clone(),
1202            font_size: rems(0.875).into(),
1203            font_weight: settings.ui_font.weight,
1204            font_style: FontStyle::Normal,
1205            line_height: relative(1.3),
1206            background_color: None,
1207            underline: None,
1208            strikethrough: None,
1209            white_space: WhiteSpace::Normal,
1210            text_overflow: None,
1211            text_align: Default::default(),
1212            line_clamp: None,
1213        }
1214    }
1215
1216    fn make_input_styles(&self, cx: &Context<Self>) -> Div {
1217        let bg_color = cx.theme().colors().editor_background;
1218        let border_color = cx.theme().colors().border;
1219
1220        h_flex()
1221            .w_full()
1222            .px_2()
1223            .py_1()
1224            .bg(bg_color)
1225            .border_1()
1226            .border_color(border_color)
1227            .rounded_sm()
1228    }
1229
1230    fn should_render_editor(&self, cx: &Context<Self>) -> bool {
1231        self.state.read(cx).is_authenticated()
1232    }
1233}
1234
1235impl Render for ConfigurationView {
1236    fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
1237        let env_var_set = self.state.read(cx).credentials_from_env;
1238        let bedrock_settings = self.state.read(cx).settings.as_ref();
1239        let bedrock_method = bedrock_settings
1240            .as_ref()
1241            .and_then(|s| s.authentication_method.clone());
1242
1243        if self.load_credentials_task.is_some() {
1244            return div().child(Label::new("Loading credentials...")).into_any();
1245        }
1246
1247        if self.should_render_editor(cx) {
1248            return h_flex()
1249                .mt_1()
1250                .p_1()
1251                .justify_between()
1252                .rounded_md()
1253                .border_1()
1254                .border_color(cx.theme().colors().border)
1255                .bg(cx.theme().colors().background)
1256                .child(
1257                    h_flex()
1258                        .gap_1()
1259                        .child(Icon::new(IconName::Check).color(Color::Success))
1260                        .child(Label::new(if env_var_set {
1261                            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.")
1262                        } else {
1263                            match bedrock_method {
1264                                Some(BedrockAuthMethod::Automatic) => "You are using automatic credentials".into(),
1265                                Some(BedrockAuthMethod::NamedProfile) => {
1266                                    "You are using named profile".into()
1267                                },
1268                                Some(BedrockAuthMethod::SingleSignOn) => "You are using a single sign on profile".into(),
1269                                None => "You are using static credentials".into(),
1270                            }
1271                        })),
1272                )
1273                .child(
1274                    Button::new("reset-key", "Reset Key")
1275                        .icon(Some(IconName::Trash))
1276                        .icon_size(IconSize::Small)
1277                        .icon_position(IconPosition::Start)
1278                        .disabled(env_var_set || bedrock_method.is_some())
1279                        .when(env_var_set, |this| {
1280                            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.")))
1281                        })
1282                        .when(bedrock_method.is_some(), |this| {
1283                            this.tooltip(Tooltip::text("You cannot reset credentials as they're being derived, check Zed settings to understand how"))
1284                        })
1285                        .on_click(cx.listener(|this, _, window, cx| this.reset_credentials(window, cx))),
1286                )
1287                .into_any();
1288        }
1289
1290        v_flex()
1291            .size_full()
1292            .on_action(cx.listener(ConfigurationView::save_credentials))
1293            .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."))
1294            .child(Label::new("But, to access models on AWS, you need to:").mt_1())
1295            .child(
1296                List::new()
1297                    .child(
1298                        InstructionListItem::new(
1299                            "Grant permissions to the strategy you'll use according to the:",
1300                            Some("Prerequisites"),
1301                            Some("https://docs.aws.amazon.com/bedrock/latest/userguide/inference-prereq.html"),
1302                        )
1303                    )
1304                    .child(
1305                        InstructionListItem::new(
1306                            "Select the models you would like access to:",
1307                            Some("Bedrock Model Catalog"),
1308                            Some("https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/modelaccess"),
1309                        )
1310                    )
1311            )
1312            .child(self.render_static_credentials_ui(cx))
1313            .child(self.render_common_fields(cx))
1314            .child(
1315                Label::new(
1316                    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."),
1317                )
1318                    .size(LabelSize::Small)
1319                    .color(Color::Muted)
1320                    .my_1(),
1321            )
1322            .child(
1323                Label::new(
1324                    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}."),
1325                )
1326                    .size(LabelSize::Small)
1327                    .color(Color::Muted),
1328            )
1329            .into_any()
1330    }
1331}
1332
1333impl ConfigurationView {
1334    fn render_access_key_id_editor(&self, cx: &mut Context<Self>) -> impl IntoElement {
1335        let text_style = self.make_text_style(cx);
1336
1337        EditorElement::new(
1338            &self.access_key_id_editor,
1339            EditorStyle {
1340                background: cx.theme().colors().editor_background,
1341                local_player: cx.theme().players().local(),
1342                text: text_style,
1343                ..Default::default()
1344            },
1345        )
1346    }
1347
1348    fn render_secret_key_editor(&self, cx: &mut Context<Self>) -> impl IntoElement {
1349        let text_style = self.make_text_style(cx);
1350
1351        EditorElement::new(
1352            &self.secret_access_key_editor,
1353            EditorStyle {
1354                background: cx.theme().colors().editor_background,
1355                local_player: cx.theme().players().local(),
1356                text: text_style,
1357                ..Default::default()
1358            },
1359        )
1360    }
1361
1362    fn render_session_token_editor(&self, cx: &mut Context<Self>) -> impl IntoElement {
1363        let text_style = self.make_text_style(cx);
1364
1365        EditorElement::new(
1366            &self.session_token_editor,
1367            EditorStyle {
1368                background: cx.theme().colors().editor_background,
1369                local_player: cx.theme().players().local(),
1370                text: text_style,
1371                ..Default::default()
1372            },
1373        )
1374    }
1375
1376    fn render_region_editor(&self, cx: &mut Context<Self>) -> impl IntoElement {
1377        let text_style = self.make_text_style(cx);
1378
1379        EditorElement::new(
1380            &self.region_editor,
1381            EditorStyle {
1382                background: cx.theme().colors().editor_background,
1383                local_player: cx.theme().players().local(),
1384                text: text_style,
1385                ..Default::default()
1386            },
1387        )
1388    }
1389
1390    fn render_static_credentials_ui(&self, cx: &mut Context<Self>) -> AnyElement {
1391        v_flex()
1392            .my_2()
1393            .gap_1p5()
1394            .child(
1395                Label::new("Static Keys")
1396                    .size(LabelSize::Default)
1397                    .weight(FontWeight::BOLD),
1398            )
1399            .child(
1400                Label::new(
1401                    "This method uses your AWS access key ID and secret access key directly.",
1402                )
1403            )
1404            .child(
1405                List::new()
1406                    .child(InstructionListItem::new(
1407                        "Create an IAM user in the AWS console with programmatic access",
1408                        Some("IAM Console"),
1409                        Some("https://us-east-1.console.aws.amazon.com/iam/home?region=us-east-1#/users"),
1410                    ))
1411                    .child(InstructionListItem::new(
1412                        "Attach the necessary Bedrock permissions to this ",
1413                        Some("user"),
1414                        Some("https://docs.aws.amazon.com/bedrock/latest/userguide/inference-prereq.html"),
1415                    ))
1416                    .child(InstructionListItem::text_only(
1417                        "Copy the access key ID and secret access key when provided",
1418                    ))
1419                    .child(InstructionListItem::text_only(
1420                        "Enter these credentials below",
1421                    )),
1422            )
1423            .child(
1424                v_flex()
1425                    .gap_0p5()
1426                    .child(Label::new("Access Key ID").size(LabelSize::Small))
1427                    .child(
1428                        self.make_input_styles(cx)
1429                            .child(self.render_access_key_id_editor(cx)),
1430                    ),
1431            )
1432            .child(
1433                v_flex()
1434                    .gap_0p5()
1435                    .child(Label::new("Secret Access Key").size(LabelSize::Small))
1436                    .child(self.make_input_styles(cx).child(self.render_secret_key_editor(cx))),
1437            )
1438            .child(
1439                v_flex()
1440                    .gap_0p5()
1441                    .child(Label::new("Session Token (Optional)").size(LabelSize::Small))
1442                    .child(
1443                        self.make_input_styles(cx)
1444                            .child(self.render_session_token_editor(cx)),
1445                    ),
1446            )
1447            .into_any_element()
1448    }
1449
1450    fn render_common_fields(&self, cx: &mut Context<Self>) -> AnyElement {
1451        v_flex()
1452            .gap_0p5()
1453            .child(Label::new("Region").size(LabelSize::Small))
1454            .child(
1455                self.make_input_styles(cx)
1456                    .child(self.render_region_editor(cx)),
1457            )
1458            .into_any_element()
1459    }
1460}