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::{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
  86#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
  87pub struct AvailableModel {
  88    pub name: String,
  89    pub display_name: Option<String>,
  90    pub max_tokens: u64,
  91    pub cache_configuration: Option<LanguageModelCacheConfiguration>,
  92    pub max_output_tokens: Option<u64>,
  93    pub default_temperature: Option<f32>,
  94    pub mode: Option<ModelMode>,
  95}
  96
  97#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize, JsonSchema)]
  98#[serde(tag = "type", rename_all = "lowercase")]
  99pub enum ModelMode {
 100    #[default]
 101    Default,
 102    Thinking {
 103        /// The maximum number of tokens to use for reasoning. Must be lower than the model's `max_output_tokens`.
 104        budget_tokens: Option<u64>,
 105    },
 106}
 107
 108impl From<ModelMode> for BedrockModelMode {
 109    fn from(value: ModelMode) -> Self {
 110        match value {
 111            ModelMode::Default => BedrockModelMode::Default,
 112            ModelMode::Thinking { budget_tokens } => BedrockModelMode::Thinking { budget_tokens },
 113        }
 114    }
 115}
 116
 117impl From<BedrockModelMode> for ModelMode {
 118    fn from(value: BedrockModelMode) -> Self {
 119        match value {
 120            BedrockModelMode::Default => ModelMode::Default,
 121            BedrockModelMode::Thinking { budget_tokens } => ModelMode::Thinking { budget_tokens },
 122        }
 123    }
 124}
 125
 126/// The URL of the base AWS service.
 127///
 128/// Right now we're just using this as the key to store the AWS credentials
 129/// under in the keychain.
 130const AMAZON_AWS_URL: &str = "https://amazonaws.com";
 131
 132// These environment variables all use a `ZED_` prefix because we don't want to overwrite the user's AWS credentials.
 133const ZED_BEDROCK_ACCESS_KEY_ID_VAR: &str = "ZED_ACCESS_KEY_ID";
 134const ZED_BEDROCK_SECRET_ACCESS_KEY_VAR: &str = "ZED_SECRET_ACCESS_KEY";
 135const ZED_BEDROCK_SESSION_TOKEN_VAR: &str = "ZED_SESSION_TOKEN";
 136const ZED_AWS_PROFILE_VAR: &str = "ZED_AWS_PROFILE";
 137const ZED_BEDROCK_REGION_VAR: &str = "ZED_AWS_REGION";
 138const ZED_AWS_CREDENTIALS_VAR: &str = "ZED_AWS_CREDENTIALS";
 139const ZED_AWS_ENDPOINT_VAR: &str = "ZED_AWS_ENDPOINT";
 140
 141pub struct State {
 142    credentials: Option<BedrockCredentials>,
 143    settings: Option<AmazonBedrockSettings>,
 144    credentials_from_env: bool,
 145    _subscription: Subscription,
 146}
 147
 148impl State {
 149    fn reset_credentials(&self, cx: &mut Context<Self>) -> Task<Result<()>> {
 150        let credentials_provider = <dyn CredentialsProvider>::global(cx);
 151        cx.spawn(async move |this, cx| {
 152            credentials_provider
 153                .delete_credentials(AMAZON_AWS_URL, &cx)
 154                .await
 155                .log_err();
 156            this.update(cx, |this, cx| {
 157                this.credentials = None;
 158                this.credentials_from_env = false;
 159                this.settings = None;
 160                cx.notify();
 161            })
 162        })
 163    }
 164
 165    fn set_credentials(
 166        &mut self,
 167        credentials: BedrockCredentials,
 168        cx: &mut Context<Self>,
 169    ) -> Task<Result<()>> {
 170        let credentials_provider = <dyn CredentialsProvider>::global(cx);
 171        cx.spawn(async move |this, cx| {
 172            credentials_provider
 173                .write_credentials(
 174                    AMAZON_AWS_URL,
 175                    "Bearer",
 176                    &serde_json::to_vec(&credentials)?,
 177                    &cx,
 178                )
 179                .await?;
 180            this.update(cx, |this, cx| {
 181                this.credentials = Some(credentials);
 182                cx.notify();
 183            })
 184        })
 185    }
 186
 187    fn is_authenticated(&self) -> bool {
 188        let derived = self
 189            .settings
 190            .as_ref()
 191            .and_then(|s| s.authentication_method.as_ref());
 192        let creds = self.credentials.as_ref();
 193
 194        derived.is_some() || creds.is_some()
 195    }
 196
 197    fn authenticate(&self, cx: &mut Context<Self>) -> Task<Result<(), AuthenticateError>> {
 198        if self.is_authenticated() {
 199            return Task::ready(Ok(()));
 200        }
 201
 202        let credentials_provider = <dyn CredentialsProvider>::global(cx);
 203        cx.spawn(async move |this, cx| {
 204            let (credentials, from_env) =
 205                if let Ok(credentials) = std::env::var(ZED_AWS_CREDENTIALS_VAR) {
 206                    (credentials, true)
 207                } else {
 208                    let (_, credentials) = credentials_provider
 209                        .read_credentials(AMAZON_AWS_URL, &cx)
 210                        .await?
 211                        .ok_or_else(|| AuthenticateError::CredentialsNotFound)?;
 212                    (
 213                        String::from_utf8(credentials)
 214                            .context("invalid {PROVIDER_NAME} credentials")?,
 215                        false,
 216                    )
 217                };
 218
 219            let credentials: BedrockCredentials =
 220                serde_json::from_str(&credentials).context("failed to parse credentials")?;
 221
 222            this.update(cx, |this, cx| {
 223                this.credentials = Some(credentials);
 224                this.credentials_from_env = from_env;
 225                cx.notify();
 226            })?;
 227
 228            Ok(())
 229        })
 230    }
 231
 232    fn get_region(&self) -> String {
 233        // Get region - from credentials or directly from settings
 234        let credentials_region = self.credentials.as_ref().map(|s| s.region.clone());
 235        let settings_region = self.settings.as_ref().and_then(|s| s.region.clone());
 236
 237        // Use credentials region if available, otherwise use settings region, finally fall back to default
 238        credentials_region
 239            .or(settings_region)
 240            .unwrap_or(String::from("us-east-1"))
 241    }
 242}
 243
 244pub struct BedrockLanguageModelProvider {
 245    http_client: AwsHttpClient,
 246    handler: tokio::runtime::Handle,
 247    state: gpui::Entity<State>,
 248}
 249
 250impl BedrockLanguageModelProvider {
 251    pub fn new(http_client: Arc<dyn HttpClient>, cx: &mut App) -> Self {
 252        let state = cx.new(|cx| State {
 253            credentials: None,
 254            settings: Some(AllLanguageModelSettings::get_global(cx).bedrock.clone()),
 255            credentials_from_env: false,
 256            _subscription: cx.observe_global::<SettingsStore>(|_, cx| {
 257                cx.notify();
 258            }),
 259        });
 260
 261        let tokio_handle = Tokio::handle(cx);
 262
 263        let coerced_client = AwsHttpClient::new(http_client.clone(), tokio_handle.clone());
 264
 265        Self {
 266            http_client: coerced_client,
 267            handler: tokio_handle.clone(),
 268            state,
 269        }
 270    }
 271
 272    fn create_language_model(&self, model: bedrock::Model) -> Arc<dyn LanguageModel> {
 273        Arc::new(BedrockModel {
 274            id: LanguageModelId::from(model.id().to_string()),
 275            model,
 276            http_client: self.http_client.clone(),
 277            handler: self.handler.clone(),
 278            state: self.state.clone(),
 279            client: OnceCell::new(),
 280            request_limiter: RateLimiter::new(4),
 281        })
 282    }
 283}
 284
 285impl LanguageModelProvider for BedrockLanguageModelProvider {
 286    fn id(&self) -> LanguageModelProviderId {
 287        PROVIDER_ID
 288    }
 289
 290    fn name(&self) -> LanguageModelProviderName {
 291        PROVIDER_NAME
 292    }
 293
 294    fn icon(&self) -> IconName {
 295        IconName::AiBedrock
 296    }
 297
 298    fn default_model(&self, _cx: &App) -> Option<Arc<dyn LanguageModel>> {
 299        Some(self.create_language_model(bedrock::Model::default()))
 300    }
 301
 302    fn default_fast_model(&self, cx: &App) -> Option<Arc<dyn LanguageModel>> {
 303        let region = self.state.read(cx).get_region();
 304        Some(self.create_language_model(bedrock::Model::default_fast(region.as_str())))
 305    }
 306
 307    fn provided_models(&self, cx: &App) -> Vec<Arc<dyn LanguageModel>> {
 308        let mut models = BTreeMap::default();
 309
 310        for model in bedrock::Model::iter() {
 311            if !matches!(model, bedrock::Model::Custom { .. }) {
 312                // TODO: Sonnet 3.7 vs. 3.7 Thinking bug is here.
 313                models.insert(model.id().to_string(), model);
 314            }
 315        }
 316
 317        // Override with available models from settings
 318        for model in AllLanguageModelSettings::get_global(cx)
 319            .bedrock
 320            .available_models
 321            .iter()
 322        {
 323            models.insert(
 324                model.name.clone(),
 325                bedrock::Model::Custom {
 326                    name: model.name.clone(),
 327                    display_name: model.display_name.clone(),
 328                    max_tokens: model.max_tokens,
 329                    max_output_tokens: model.max_output_tokens,
 330                    default_temperature: model.default_temperature,
 331                    cache_configuration: model.cache_configuration.as_ref().map(|config| {
 332                        bedrock::BedrockModelCacheConfiguration {
 333                            max_cache_anchors: config.max_cache_anchors,
 334                            min_total_token: config.min_total_token,
 335                        }
 336                    }),
 337                },
 338            );
 339        }
 340
 341        models
 342            .into_values()
 343            .map(|model| self.create_language_model(model))
 344            .collect()
 345    }
 346
 347    fn is_authenticated(&self, cx: &App) -> bool {
 348        self.state.read(cx).is_authenticated()
 349    }
 350
 351    fn authenticate(&self, cx: &mut App) -> Task<Result<(), AuthenticateError>> {
 352        self.state.update(cx, |state, cx| state.authenticate(cx))
 353    }
 354
 355    fn configuration_view(&self, window: &mut Window, cx: &mut App) -> 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    handler: 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                    if !endpoint_url.is_empty() {
 416                        config_builder = config_builder.endpoint_url(endpoint_url);
 417                    }
 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.handler.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                    if last_message.role == bedrock_role {
 732                        last_message.content.extend(bedrock_message_content);
 733                        continue;
 734                    }
 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.clone(),
 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.clone(),
 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, 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, 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, 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, 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            .to_string()
1089            .trim()
1090            .to_string();
1091        let secret_access_key = self
1092            .secret_access_key_editor
1093            .read(cx)
1094            .text(cx)
1095            .to_string()
1096            .trim()
1097            .to_string();
1098        let session_token = self
1099            .session_token_editor
1100            .read(cx)
1101            .text(cx)
1102            .to_string()
1103            .trim()
1104            .to_string();
1105        let session_token = if session_token.is_empty() {
1106            None
1107        } else {
1108            Some(session_token)
1109        };
1110        let region = self
1111            .region_editor
1112            .read(cx)
1113            .text(cx)
1114            .to_string()
1115            .trim()
1116            .to_string();
1117        let region = if region.is_empty() {
1118            "us-east-1".to_string()
1119        } else {
1120            region
1121        };
1122
1123        let state = self.state.clone();
1124        cx.spawn(async move |_, cx| {
1125            state
1126                .update(cx, |state, cx| {
1127                    let credentials: BedrockCredentials = BedrockCredentials {
1128                        region: region.clone(),
1129                        access_key_id: access_key_id.clone(),
1130                        secret_access_key: secret_access_key.clone(),
1131                        session_token: session_token.clone(),
1132                    };
1133
1134                    state.set_credentials(credentials, cx)
1135                })?
1136                .await
1137        })
1138        .detach_and_log_err(cx);
1139    }
1140
1141    fn reset_credentials(&mut self, window: &mut Window, cx: &mut Context<Self>) {
1142        self.access_key_id_editor
1143            .update(cx, |editor, cx| editor.set_text("", window, cx));
1144        self.secret_access_key_editor
1145            .update(cx, |editor, cx| editor.set_text("", window, cx));
1146        self.session_token_editor
1147            .update(cx, |editor, cx| editor.set_text("", window, cx));
1148        self.region_editor
1149            .update(cx, |editor, cx| editor.set_text("", window, cx));
1150
1151        let state = self.state.clone();
1152        cx.spawn(async move |_, cx| {
1153            state
1154                .update(cx, |state, cx| state.reset_credentials(cx))?
1155                .await
1156        })
1157        .detach_and_log_err(cx);
1158    }
1159
1160    fn make_text_style(&self, cx: &Context<Self>) -> TextStyle {
1161        let settings = ThemeSettings::get_global(cx);
1162        TextStyle {
1163            color: cx.theme().colors().text,
1164            font_family: settings.ui_font.family.clone(),
1165            font_features: settings.ui_font.features.clone(),
1166            font_fallbacks: settings.ui_font.fallbacks.clone(),
1167            font_size: rems(0.875).into(),
1168            font_weight: settings.ui_font.weight,
1169            font_style: FontStyle::Normal,
1170            line_height: relative(1.3),
1171            background_color: None,
1172            underline: None,
1173            strikethrough: None,
1174            white_space: WhiteSpace::Normal,
1175            text_overflow: None,
1176            text_align: Default::default(),
1177            line_clamp: None,
1178        }
1179    }
1180
1181    fn make_input_styles(&self, cx: &Context<Self>) -> Div {
1182        let bg_color = cx.theme().colors().editor_background;
1183        let border_color = cx.theme().colors().border;
1184
1185        h_flex()
1186            .w_full()
1187            .px_2()
1188            .py_1()
1189            .bg(bg_color)
1190            .border_1()
1191            .border_color(border_color)
1192            .rounded_sm()
1193    }
1194
1195    fn should_render_editor(&self, cx: &Context<Self>) -> bool {
1196        self.state.read(cx).is_authenticated()
1197    }
1198}
1199
1200impl Render for ConfigurationView {
1201    fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
1202        let env_var_set = self.state.read(cx).credentials_from_env;
1203        let bedrock_settings = self.state.read(cx).settings.as_ref();
1204        let bedrock_method = bedrock_settings
1205            .as_ref()
1206            .and_then(|s| s.authentication_method.clone());
1207
1208        if self.load_credentials_task.is_some() {
1209            return div().child(Label::new("Loading credentials...")).into_any();
1210        }
1211
1212        if self.should_render_editor(cx) {
1213            return h_flex()
1214                .mt_1()
1215                .p_1()
1216                .justify_between()
1217                .rounded_md()
1218                .border_1()
1219                .border_color(cx.theme().colors().border)
1220                .bg(cx.theme().colors().background)
1221                .child(
1222                    h_flex()
1223                        .gap_1()
1224                        .child(Icon::new(IconName::Check).color(Color::Success))
1225                        .child(Label::new(if env_var_set {
1226                            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.")
1227                        } else {
1228                            match bedrock_method {
1229                                Some(BedrockAuthMethod::Automatic) => "You are using automatic credentials".into(),
1230                                Some(BedrockAuthMethod::NamedProfile) => {
1231                                    "You are using named profile".into()
1232                                },
1233                                Some(BedrockAuthMethod::SingleSignOn) => "You are using a single sign on profile".into(),
1234                                None => "You are using static credentials".into(),
1235                            }
1236                        })),
1237                )
1238                .child(
1239                    Button::new("reset-key", "Reset Key")
1240                        .icon(Some(IconName::Trash))
1241                        .icon_size(IconSize::Small)
1242                        .icon_position(IconPosition::Start)
1243                        .disabled(env_var_set || bedrock_method.is_some())
1244                        .when(env_var_set, |this| {
1245                            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.")))
1246                        })
1247                        .when(bedrock_method.is_some(), |this| {
1248                            this.tooltip(Tooltip::text("You cannot reset credentials as they're being derived, check Zed settings to understand how"))
1249                        })
1250                        .on_click(cx.listener(|this, _, window, cx| this.reset_credentials(window, cx))),
1251                )
1252                .into_any();
1253        }
1254
1255        v_flex()
1256            .size_full()
1257            .on_action(cx.listener(ConfigurationView::save_credentials))
1258            .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."))
1259            .child(Label::new("But, to access models on AWS, you need to:").mt_1())
1260            .child(
1261                List::new()
1262                    .child(
1263                        InstructionListItem::new(
1264                            "Grant permissions to the strategy you'll use according to the:",
1265                            Some("Prerequisites"),
1266                            Some("https://docs.aws.amazon.com/bedrock/latest/userguide/inference-prereq.html"),
1267                        )
1268                    )
1269                    .child(
1270                        InstructionListItem::new(
1271                            "Select the models you would like access to:",
1272                            Some("Bedrock Model Catalog"),
1273                            Some("https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/modelaccess"),
1274                        )
1275                    )
1276            )
1277            .child(self.render_static_credentials_ui(cx))
1278            .child(self.render_common_fields(cx))
1279            .child(
1280                Label::new(
1281                    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."),
1282                )
1283                    .size(LabelSize::Small)
1284                    .color(Color::Muted)
1285                    .my_1(),
1286            )
1287            .child(
1288                Label::new(
1289                    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}."),
1290                )
1291                    .size(LabelSize::Small)
1292                    .color(Color::Muted),
1293            )
1294            .into_any()
1295    }
1296}
1297
1298impl ConfigurationView {
1299    fn render_access_key_id_editor(&self, cx: &mut Context<Self>) -> impl IntoElement {
1300        let text_style = self.make_text_style(cx);
1301
1302        EditorElement::new(
1303            &self.access_key_id_editor,
1304            EditorStyle {
1305                background: cx.theme().colors().editor_background,
1306                local_player: cx.theme().players().local(),
1307                text: text_style,
1308                ..Default::default()
1309            },
1310        )
1311    }
1312
1313    fn render_secret_key_editor(&self, cx: &mut Context<Self>) -> impl IntoElement {
1314        let text_style = self.make_text_style(cx);
1315
1316        EditorElement::new(
1317            &self.secret_access_key_editor,
1318            EditorStyle {
1319                background: cx.theme().colors().editor_background,
1320                local_player: cx.theme().players().local(),
1321                text: text_style,
1322                ..Default::default()
1323            },
1324        )
1325    }
1326
1327    fn render_session_token_editor(&self, cx: &mut Context<Self>) -> impl IntoElement {
1328        let text_style = self.make_text_style(cx);
1329
1330        EditorElement::new(
1331            &self.session_token_editor,
1332            EditorStyle {
1333                background: cx.theme().colors().editor_background,
1334                local_player: cx.theme().players().local(),
1335                text: text_style,
1336                ..Default::default()
1337            },
1338        )
1339    }
1340
1341    fn render_region_editor(&self, cx: &mut Context<Self>) -> impl IntoElement {
1342        let text_style = self.make_text_style(cx);
1343
1344        EditorElement::new(
1345            &self.region_editor,
1346            EditorStyle {
1347                background: cx.theme().colors().editor_background,
1348                local_player: cx.theme().players().local(),
1349                text: text_style,
1350                ..Default::default()
1351            },
1352        )
1353    }
1354
1355    fn render_static_credentials_ui(&self, cx: &mut Context<Self>) -> AnyElement {
1356        v_flex()
1357            .my_2()
1358            .gap_1p5()
1359            .child(
1360                Label::new("Static Keys")
1361                    .size(LabelSize::Default)
1362                    .weight(FontWeight::BOLD),
1363            )
1364            .child(
1365                Label::new(
1366                    "This method uses your AWS access key ID and secret access key directly.",
1367                )
1368            )
1369            .child(
1370                List::new()
1371                    .child(InstructionListItem::new(
1372                        "Create an IAM user in the AWS console with programmatic access",
1373                        Some("IAM Console"),
1374                        Some("https://us-east-1.console.aws.amazon.com/iam/home?region=us-east-1#/users"),
1375                    ))
1376                    .child(InstructionListItem::new(
1377                        "Attach the necessary Bedrock permissions to this ",
1378                        Some("user"),
1379                        Some("https://docs.aws.amazon.com/bedrock/latest/userguide/inference-prereq.html"),
1380                    ))
1381                    .child(InstructionListItem::text_only(
1382                        "Copy the access key ID and secret access key when provided",
1383                    ))
1384                    .child(InstructionListItem::text_only(
1385                        "Enter these credentials below",
1386                    )),
1387            )
1388            .child(
1389                v_flex()
1390                    .gap_0p5()
1391                    .child(Label::new("Access Key ID").size(LabelSize::Small))
1392                    .child(
1393                        self.make_input_styles(cx)
1394                            .child(self.render_access_key_id_editor(cx)),
1395                    ),
1396            )
1397            .child(
1398                v_flex()
1399                    .gap_0p5()
1400                    .child(Label::new("Secret Access Key").size(LabelSize::Small))
1401                    .child(self.make_input_styles(cx).child(self.render_secret_key_editor(cx))),
1402            )
1403            .child(
1404                v_flex()
1405                    .gap_0p5()
1406                    .child(Label::new("Session Token (Optional)").size(LabelSize::Small))
1407                    .child(
1408                        self.make_input_styles(cx)
1409                            .child(self.render_session_token_editor(cx)),
1410                    ),
1411            )
1412            .into_any_element()
1413    }
1414
1415    fn render_common_fields(&self, cx: &mut Context<Self>) -> AnyElement {
1416        v_flex()
1417            .gap_0p5()
1418            .child(Label::new("Region").size(LabelSize::Small))
1419            .child(
1420                self.make_input_styles(cx)
1421                    .child(self.render_region_editor(cx)),
1422            )
1423            .into_any_element()
1424    }
1425}