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