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