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::Region;
   8use aws_config::stalled_stream_protection::StalledStreamProtectionConfig;
   9use aws_credential_types::Credentials;
  10use aws_http_client::AwsHttpClient;
  11use bedrock::bedrock_client::types::{
  12    ContentBlockDelta, ContentBlockStart, ContentBlockStartEvent, ConverseStreamOutput,
  13};
  14use bedrock::bedrock_client::{self, Config};
  15use bedrock::{
  16    BedrockError, BedrockInnerContent, BedrockMessage, BedrockSpecificTool,
  17    BedrockStreamingResponse, BedrockTool, BedrockToolChoice, BedrockToolInputSchema, Model,
  18    value_to_aws_document,
  19};
  20use collections::{BTreeMap, HashMap};
  21use credentials_provider::CredentialsProvider;
  22use editor::{Editor, EditorElement, EditorStyle};
  23use futures::{FutureExt, Stream, StreamExt, future::BoxFuture, stream::BoxStream};
  24use gpui::{
  25    AnyView, App, AsyncApp, Context, Entity, FontStyle, Subscription, Task, TextStyle, WhiteSpace,
  26};
  27use gpui_tokio::Tokio;
  28use http_client::HttpClient;
  29use language_model::{
  30    AuthenticateError, LanguageModel, LanguageModelCacheConfiguration,
  31    LanguageModelCompletionEvent, LanguageModelId, LanguageModelName, LanguageModelProvider,
  32    LanguageModelProviderId, LanguageModelProviderName, LanguageModelProviderState,
  33    LanguageModelRequest, LanguageModelToolUse, MessageContent, RateLimiter, Role,
  34};
  35use schemars::JsonSchema;
  36use serde::{Deserialize, Serialize};
  37use serde_json::Value;
  38use settings::{Settings, SettingsStore};
  39use strum::IntoEnumIterator;
  40use theme::ThemeSettings;
  41use tokio::runtime::Handle;
  42use ui::{Icon, IconName, List, Tooltip, prelude::*};
  43use util::{ResultExt, maybe};
  44
  45use crate::AllLanguageModelSettings;
  46
  47const PROVIDER_ID: &str = "amazon-bedrock";
  48const PROVIDER_NAME: &str = "Amazon Bedrock";
  49
  50#[derive(Default, Clone, Deserialize, Serialize, PartialEq, Debug)]
  51pub struct BedrockCredentials {
  52    pub region: String,
  53    pub access_key_id: String,
  54    pub secret_access_key: String,
  55}
  56
  57#[derive(Default, Clone, Debug, PartialEq)]
  58pub struct AmazonBedrockSettings {
  59    pub session_token: Option<String>,
  60    pub available_models: Vec<AvailableModel>,
  61}
  62
  63#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
  64pub struct AvailableModel {
  65    pub name: String,
  66    pub display_name: Option<String>,
  67    pub max_tokens: usize,
  68    pub cache_configuration: Option<LanguageModelCacheConfiguration>,
  69    pub max_output_tokens: Option<u32>,
  70    pub default_temperature: Option<f32>,
  71}
  72
  73/// The URL of the base AWS service.
  74///
  75/// Right now we're just using this as the key to store the AWS credentials
  76/// under in the keychain.
  77const AMAZON_AWS_URL: &str = "https://amazonaws.com";
  78
  79// These environment variables all use a `ZED_` prefix because we don't want to overwrite the user's AWS credentials.
  80const ZED_BEDROCK_ACCESS_KEY_ID_VAR: &str = "ZED_ACCESS_KEY_ID";
  81const ZED_BEDROCK_SECRET_ACCESS_KEY_VAR: &str = "ZED_SECRET_ACCESS_KEY";
  82const ZED_BEDROCK_REGION_VAR: &str = "ZED_AWS_REGION";
  83const ZED_AWS_CREDENTIALS_VAR: &str = "ZED_AWS_CREDENTIALS";
  84
  85pub struct State {
  86    credentials: Option<BedrockCredentials>,
  87    credentials_from_env: bool,
  88    _subscription: Subscription,
  89}
  90
  91impl State {
  92    fn reset_credentials(&self, cx: &mut Context<Self>) -> Task<Result<()>> {
  93        let credentials_provider = <dyn CredentialsProvider>::global(cx);
  94        cx.spawn(async move |this, cx| {
  95            credentials_provider
  96                .delete_credentials(AMAZON_AWS_URL, &cx)
  97                .await
  98                .log_err();
  99            this.update(cx, |this, cx| {
 100                this.credentials = None;
 101                this.credentials_from_env = false;
 102                cx.notify();
 103            })
 104        })
 105    }
 106
 107    fn set_credentials(
 108        &mut self,
 109        credentials: BedrockCredentials,
 110        cx: &mut Context<Self>,
 111    ) -> Task<Result<()>> {
 112        let credentials_provider = <dyn CredentialsProvider>::global(cx);
 113        cx.spawn(async move |this, cx| {
 114            credentials_provider
 115                .write_credentials(
 116                    AMAZON_AWS_URL,
 117                    "Bearer",
 118                    &serde_json::to_vec(&credentials)?,
 119                    &cx,
 120                )
 121                .await?;
 122            this.update(cx, |this, cx| {
 123                this.credentials = Some(credentials);
 124                cx.notify();
 125            })
 126        })
 127    }
 128
 129    fn is_authenticated(&self) -> bool {
 130        self.credentials.is_some()
 131    }
 132
 133    fn authenticate(&self, cx: &mut Context<Self>) -> Task<Result<(), AuthenticateError>> {
 134        if self.is_authenticated() {
 135            return Task::ready(Ok(()));
 136        }
 137
 138        let credentials_provider = <dyn CredentialsProvider>::global(cx);
 139        cx.spawn(async move |this, cx| {
 140            let (credentials, from_env) =
 141                if let Ok(credentials) = std::env::var(ZED_AWS_CREDENTIALS_VAR) {
 142                    (credentials, true)
 143                } else {
 144                    let (_, credentials) = credentials_provider
 145                        .read_credentials(AMAZON_AWS_URL, &cx)
 146                        .await?
 147                        .ok_or_else(|| AuthenticateError::CredentialsNotFound)?;
 148                    (
 149                        String::from_utf8(credentials)
 150                            .context("invalid {PROVIDER_NAME} credentials")?,
 151                        false,
 152                    )
 153                };
 154
 155            let credentials: BedrockCredentials =
 156                serde_json::from_str(&credentials).context("failed to parse credentials")?;
 157
 158            this.update(cx, |this, cx| {
 159                this.credentials = Some(credentials);
 160                this.credentials_from_env = from_env;
 161                cx.notify();
 162            })?;
 163
 164            Ok(())
 165        })
 166    }
 167}
 168
 169pub struct BedrockLanguageModelProvider {
 170    http_client: AwsHttpClient,
 171    handler: tokio::runtime::Handle,
 172    state: gpui::Entity<State>,
 173}
 174
 175impl BedrockLanguageModelProvider {
 176    pub fn new(http_client: Arc<dyn HttpClient>, cx: &mut App) -> Self {
 177        let state = cx.new(|cx| State {
 178            credentials: None,
 179            credentials_from_env: false,
 180            _subscription: cx.observe_global::<SettingsStore>(|_, cx| {
 181                cx.notify();
 182            }),
 183        });
 184
 185        let tokio_handle = Tokio::handle(cx);
 186
 187        let coerced_client = AwsHttpClient::new(http_client.clone(), tokio_handle.clone());
 188
 189        Self {
 190            http_client: coerced_client,
 191            handler: tokio_handle.clone(),
 192            state,
 193        }
 194    }
 195}
 196
 197impl LanguageModelProvider for BedrockLanguageModelProvider {
 198    fn id(&self) -> LanguageModelProviderId {
 199        LanguageModelProviderId(PROVIDER_ID.into())
 200    }
 201
 202    fn name(&self) -> LanguageModelProviderName {
 203        LanguageModelProviderName(PROVIDER_NAME.into())
 204    }
 205
 206    fn icon(&self) -> IconName {
 207        IconName::AiBedrock
 208    }
 209
 210    fn default_model(&self, _cx: &App) -> Option<Arc<dyn LanguageModel>> {
 211        let model = bedrock::Model::default();
 212        Some(Arc::new(BedrockModel {
 213            id: LanguageModelId::from(model.id().to_string()),
 214            model,
 215            http_client: self.http_client.clone(),
 216            handler: self.handler.clone(),
 217            state: self.state.clone(),
 218            request_limiter: RateLimiter::new(4),
 219        }))
 220    }
 221
 222    fn provided_models(&self, cx: &App) -> Vec<Arc<dyn LanguageModel>> {
 223        let mut models = BTreeMap::default();
 224
 225        for model in bedrock::Model::iter() {
 226            if !matches!(model, bedrock::Model::Custom { .. }) {
 227                models.insert(model.id().to_string(), model);
 228            }
 229        }
 230
 231        // Override with available models from settings
 232        for model in AllLanguageModelSettings::get_global(cx)
 233            .bedrock
 234            .available_models
 235            .iter()
 236        {
 237            models.insert(
 238                model.name.clone(),
 239                bedrock::Model::Custom {
 240                    name: model.name.clone(),
 241                    display_name: model.display_name.clone(),
 242                    max_tokens: model.max_tokens,
 243                    max_output_tokens: model.max_output_tokens,
 244                    default_temperature: model.default_temperature,
 245                },
 246            );
 247        }
 248
 249        models
 250            .into_values()
 251            .map(|model| {
 252                Arc::new(BedrockModel {
 253                    id: LanguageModelId::from(model.id().to_string()),
 254                    model,
 255                    http_client: self.http_client.clone(),
 256                    handler: self.handler.clone(),
 257                    state: self.state.clone(),
 258                    request_limiter: RateLimiter::new(4),
 259                }) as Arc<dyn LanguageModel>
 260            })
 261            .collect()
 262    }
 263
 264    fn is_authenticated(&self, cx: &App) -> bool {
 265        self.state.read(cx).is_authenticated()
 266    }
 267
 268    fn authenticate(&self, cx: &mut App) -> Task<Result<(), AuthenticateError>> {
 269        self.state.update(cx, |state, cx| state.authenticate(cx))
 270    }
 271
 272    fn configuration_view(&self, window: &mut Window, cx: &mut App) -> AnyView {
 273        cx.new(|cx| ConfigurationView::new(self.state.clone(), window, cx))
 274            .into()
 275    }
 276
 277    fn reset_credentials(&self, cx: &mut App) -> Task<Result<()>> {
 278        self.state
 279            .update(cx, |state, cx| state.reset_credentials(cx))
 280    }
 281}
 282
 283impl LanguageModelProviderState for BedrockLanguageModelProvider {
 284    type ObservableEntity = State;
 285
 286    fn observable_entity(&self) -> Option<gpui::Entity<Self::ObservableEntity>> {
 287        Some(self.state.clone())
 288    }
 289}
 290
 291struct BedrockModel {
 292    id: LanguageModelId,
 293    model: Model,
 294    http_client: AwsHttpClient,
 295    handler: tokio::runtime::Handle,
 296    state: gpui::Entity<State>,
 297    request_limiter: RateLimiter,
 298}
 299
 300impl BedrockModel {
 301    fn stream_completion(
 302        &self,
 303        request: bedrock::Request,
 304        cx: &AsyncApp,
 305    ) -> Result<
 306        BoxFuture<'static, BoxStream<'static, Result<BedrockStreamingResponse, BedrockError>>>,
 307    > {
 308        let Ok(Ok((access_key_id, secret_access_key, region))) =
 309            cx.read_entity(&self.state, |state, _cx| {
 310                if let Some(credentials) = &state.credentials {
 311                    Ok((
 312                        credentials.access_key_id.clone(),
 313                        credentials.secret_access_key.clone(),
 314                        credentials.region.clone(),
 315                    ))
 316                } else {
 317                    return Err(anyhow!("Failed to read credentials"));
 318                }
 319            })
 320        else {
 321            return Err(anyhow!("App state dropped"));
 322        };
 323
 324        let runtime_client = bedrock_client::Client::from_conf(
 325            Config::builder()
 326                .stalled_stream_protection(StalledStreamProtectionConfig::disabled())
 327                .credentials_provider(Credentials::new(
 328                    access_key_id,
 329                    secret_access_key,
 330                    None,
 331                    None,
 332                    "Keychain",
 333                ))
 334                .region(Region::new(region))
 335                .http_client(self.http_client.clone())
 336                .build(),
 337        );
 338
 339        let owned_handle = self.handler.clone();
 340
 341        Ok(async move {
 342            let request = bedrock::stream_completion(runtime_client, request, owned_handle);
 343            request.await.unwrap_or_else(|e| {
 344                futures::stream::once(async move { Err(BedrockError::ClientError(e)) }).boxed()
 345            })
 346        }
 347        .boxed())
 348    }
 349}
 350
 351impl LanguageModel for BedrockModel {
 352    fn id(&self) -> LanguageModelId {
 353        self.id.clone()
 354    }
 355
 356    fn name(&self) -> LanguageModelName {
 357        LanguageModelName::from(self.model.display_name().to_string())
 358    }
 359
 360    fn provider_id(&self) -> LanguageModelProviderId {
 361        LanguageModelProviderId(PROVIDER_ID.into())
 362    }
 363
 364    fn provider_name(&self) -> LanguageModelProviderName {
 365        LanguageModelProviderName(PROVIDER_NAME.into())
 366    }
 367
 368    fn supports_tools(&self) -> bool {
 369        true
 370    }
 371
 372    fn telemetry_id(&self) -> String {
 373        format!("bedrock/{}", self.model.id())
 374    }
 375
 376    fn max_token_count(&self) -> usize {
 377        self.model.max_token_count()
 378    }
 379
 380    fn max_output_tokens(&self) -> Option<u32> {
 381        Some(self.model.max_output_tokens())
 382    }
 383
 384    fn count_tokens(
 385        &self,
 386        request: LanguageModelRequest,
 387        cx: &App,
 388    ) -> BoxFuture<'static, Result<usize>> {
 389        get_bedrock_tokens(request, cx)
 390    }
 391
 392    fn stream_completion(
 393        &self,
 394        request: LanguageModelRequest,
 395        cx: &AsyncApp,
 396    ) -> BoxFuture<'static, Result<BoxStream<'static, Result<LanguageModelCompletionEvent>>>> {
 397        let request = into_bedrock(
 398            request,
 399            self.model.id().into(),
 400            self.model.default_temperature(),
 401            self.model.max_output_tokens(),
 402        );
 403
 404        let owned_handle = self.handler.clone();
 405
 406        let request = self.stream_completion(request, cx);
 407        let future = self.request_limiter.stream(async move {
 408            let response = request.map_err(|err| anyhow!(err))?.await;
 409            Ok(map_to_language_model_completion_events(
 410                response,
 411                owned_handle,
 412            ))
 413        });
 414        async move { Ok(future.await?.boxed()) }.boxed()
 415    }
 416
 417    fn use_any_tool(
 418        &self,
 419        request: LanguageModelRequest,
 420        name: String,
 421        description: String,
 422        schema: Value,
 423        _cx: &AsyncApp,
 424    ) -> BoxFuture<'static, Result<BoxStream<'static, Result<String>>>> {
 425        let mut request = into_bedrock(
 426            request,
 427            self.model.id().into(),
 428            self.model.default_temperature(),
 429            self.model.max_output_tokens(),
 430        );
 431
 432        request.tool_choice = BedrockSpecificTool::builder()
 433            .name(name.clone())
 434            .build()
 435            .log_err()
 436            .map(BedrockToolChoice::Tool);
 437
 438        if let Some(tool) = BedrockTool::builder()
 439            .name(name.clone())
 440            .description(description.clone())
 441            .input_schema(BedrockToolInputSchema::Json(value_to_aws_document(&schema)))
 442            .build()
 443            .log_err()
 444        {
 445            request.tools.push(tool);
 446        }
 447
 448        let handle = self.handler.clone();
 449
 450        let request = self.stream_completion(request, _cx);
 451        self.request_limiter
 452            .run(async move {
 453                let response = request.map_err(|err| anyhow!(err))?.await;
 454                Ok(extract_tool_args_from_events(name, response, handle)
 455                    .await?
 456                    .boxed())
 457            })
 458            .boxed()
 459    }
 460
 461    fn cache_configuration(&self) -> Option<LanguageModelCacheConfiguration> {
 462        None
 463    }
 464}
 465
 466pub fn into_bedrock(
 467    request: LanguageModelRequest,
 468    model: String,
 469    default_temperature: f32,
 470    max_output_tokens: u32,
 471) -> bedrock::Request {
 472    let mut new_messages: Vec<BedrockMessage> = Vec::new();
 473    let mut system_message = String::new();
 474
 475    for message in request.messages {
 476        if message.contents_empty() {
 477            continue;
 478        }
 479
 480        match message.role {
 481            Role::User | Role::Assistant => {
 482                let bedrock_message_content: Vec<BedrockInnerContent> = message
 483                    .content
 484                    .into_iter()
 485                    .filter_map(|content| match content {
 486                        MessageContent::Text(text) => {
 487                            if !text.is_empty() {
 488                                Some(BedrockInnerContent::Text(text))
 489                            } else {
 490                                None
 491                            }
 492                        }
 493                        _ => None,
 494                    })
 495                    .collect();
 496                let bedrock_role = match message.role {
 497                    Role::User => bedrock::BedrockRole::User,
 498                    Role::Assistant => bedrock::BedrockRole::Assistant,
 499                    Role::System => unreachable!("System role should never occur here"),
 500                };
 501                if let Some(last_message) = new_messages.last_mut() {
 502                    if last_message.role == bedrock_role {
 503                        last_message.content.extend(bedrock_message_content);
 504                        continue;
 505                    }
 506                }
 507                new_messages.push(
 508                    BedrockMessage::builder()
 509                        .role(bedrock_role)
 510                        .set_content(Some(bedrock_message_content))
 511                        .build()
 512                        .expect("failed to build Bedrock message"),
 513                );
 514            }
 515            Role::System => {
 516                if !system_message.is_empty() {
 517                    system_message.push_str("\n\n");
 518                }
 519                system_message.push_str(&message.string_contents());
 520            }
 521        }
 522    }
 523
 524    bedrock::Request {
 525        model,
 526        messages: new_messages,
 527        max_tokens: max_output_tokens,
 528        system: Some(system_message),
 529        tools: vec![],
 530        tool_choice: None,
 531        metadata: None,
 532        stop_sequences: Vec::new(),
 533        temperature: request.temperature.or(Some(default_temperature)),
 534        top_k: None,
 535        top_p: None,
 536    }
 537}
 538
 539// TODO: just call the ConverseOutput.usage() method:
 540// https://docs.rs/aws-sdk-bedrockruntime/latest/aws_sdk_bedrockruntime/operation/converse/struct.ConverseOutput.html#method.output
 541pub fn get_bedrock_tokens(
 542    request: LanguageModelRequest,
 543    cx: &App,
 544) -> BoxFuture<'static, Result<usize>> {
 545    cx.background_executor()
 546        .spawn(async move {
 547            let messages = request.messages;
 548            let mut tokens_from_images = 0;
 549            let mut string_messages = Vec::with_capacity(messages.len());
 550
 551            for message in messages {
 552                use language_model::MessageContent;
 553
 554                let mut string_contents = String::new();
 555
 556                for content in message.content {
 557                    match content {
 558                        MessageContent::Text(text) => {
 559                            string_contents.push_str(&text);
 560                        }
 561                        MessageContent::Image(image) => {
 562                            tokens_from_images += image.estimate_tokens();
 563                        }
 564                        MessageContent::ToolUse(_tool_use) => {
 565                            // TODO: Estimate token usage from tool uses.
 566                        }
 567                        MessageContent::ToolResult(tool_result) => {
 568                            string_contents.push_str(&tool_result.content);
 569                        }
 570                    }
 571                }
 572
 573                if !string_contents.is_empty() {
 574                    string_messages.push(tiktoken_rs::ChatCompletionRequestMessage {
 575                        role: match message.role {
 576                            Role::User => "user".into(),
 577                            Role::Assistant => "assistant".into(),
 578                            Role::System => "system".into(),
 579                        },
 580                        content: Some(string_contents),
 581                        name: None,
 582                        function_call: None,
 583                    });
 584                }
 585            }
 586
 587            // Tiktoken doesn't yet support these models, so we manually use the
 588            // same tokenizer as GPT-4.
 589            tiktoken_rs::num_tokens_from_messages("gpt-4", &string_messages)
 590                .map(|tokens| tokens + tokens_from_images)
 591        })
 592        .boxed()
 593}
 594
 595pub async fn extract_tool_args_from_events(
 596    name: String,
 597    mut events: Pin<Box<dyn Send + Stream<Item = Result<BedrockStreamingResponse, BedrockError>>>>,
 598    handle: Handle,
 599) -> Result<impl Send + Stream<Item = Result<String>>> {
 600    handle
 601        .spawn(async move {
 602            let mut tool_use_index = None;
 603            while let Some(event) = events.next().await {
 604                if let BedrockStreamingResponse::ContentBlockStart(ContentBlockStartEvent {
 605                    content_block_index,
 606                    start,
 607                    ..
 608                }) = event?
 609                {
 610                    match start {
 611                        None => {
 612                            continue;
 613                        }
 614                        Some(start) => match start.as_tool_use() {
 615                            Ok(tool_use) => {
 616                                if name == tool_use.name {
 617                                    tool_use_index = Some(content_block_index);
 618                                    break;
 619                                }
 620                            }
 621                            Err(err) => {
 622                                return Err(anyhow!("Failed to parse tool use event: {:?}", err));
 623                            }
 624                        },
 625                    }
 626                }
 627            }
 628
 629            let Some(tool_use_index) = tool_use_index else {
 630                return Err(anyhow!("Tool is not used"));
 631            };
 632
 633            Ok(events.filter_map(move |event| {
 634                let result = match event {
 635                    Err(_err) => None,
 636                    Ok(output) => match output.clone() {
 637                        BedrockStreamingResponse::ContentBlockDelta(inner) => {
 638                            match inner.clone().delta {
 639                                Some(ContentBlockDelta::ToolUse(tool_use)) => {
 640                                    if inner.content_block_index == tool_use_index {
 641                                        Some(Ok(tool_use.input))
 642                                    } else {
 643                                        None
 644                                    }
 645                                }
 646                                _ => None,
 647                            }
 648                        }
 649                        _ => None,
 650                    },
 651                };
 652
 653                async move { result }
 654            }))
 655        })
 656        .await?
 657}
 658
 659pub fn map_to_language_model_completion_events(
 660    events: Pin<Box<dyn Send + Stream<Item = Result<BedrockStreamingResponse, BedrockError>>>>,
 661    handle: Handle,
 662) -> impl Stream<Item = Result<LanguageModelCompletionEvent>> {
 663    struct RawToolUse {
 664        id: String,
 665        name: String,
 666        input_json: String,
 667    }
 668
 669    struct State {
 670        events: Pin<Box<dyn Send + Stream<Item = Result<BedrockStreamingResponse, BedrockError>>>>,
 671        tool_uses_by_index: HashMap<i32, RawToolUse>,
 672    }
 673
 674    futures::stream::unfold(
 675        State {
 676            events,
 677            tool_uses_by_index: HashMap::default(),
 678        },
 679        move |mut state: State| {
 680            let inner_handle = handle.clone();
 681            async move {
 682                inner_handle
 683                    .spawn(async {
 684                        while let Some(event) = state.events.next().await {
 685                            match event {
 686                                Ok(event) => match event {
 687                                    ConverseStreamOutput::ContentBlockDelta(cb_delta) => {
 688                                        if let Some(ContentBlockDelta::Text(text_out)) =
 689                                            cb_delta.delta
 690                                        {
 691                                            return Some((
 692                                                Some(Ok(LanguageModelCompletionEvent::Text(
 693                                                    text_out,
 694                                                ))),
 695                                                state,
 696                                            ));
 697                                        } else if let Some(ContentBlockDelta::ToolUse(text_out)) =
 698                                            cb_delta.delta
 699                                        {
 700                                            if let Some(tool_use) = state
 701                                                .tool_uses_by_index
 702                                                .get_mut(&cb_delta.content_block_index)
 703                                            {
 704                                                tool_use.input_json.push_str(text_out.input());
 705                                                return Some((None, state));
 706                                            };
 707
 708                                            return Some((None, state));
 709                                        } else if cb_delta.delta.is_none() {
 710                                            return Some((None, state));
 711                                        }
 712                                    }
 713                                    ConverseStreamOutput::ContentBlockStart(cb_start) => {
 714                                        if let Some(start) = cb_start.start {
 715                                            match start {
 716                                                ContentBlockStart::ToolUse(text_out) => {
 717                                                    let tool_use = RawToolUse {
 718                                                        id: text_out.tool_use_id,
 719                                                        name: text_out.name,
 720                                                        input_json: String::new(),
 721                                                    };
 722
 723                                                    state.tool_uses_by_index.insert(
 724                                                        cb_start.content_block_index,
 725                                                        tool_use,
 726                                                    );
 727                                                }
 728                                                _ => {}
 729                                            }
 730                                        }
 731                                    }
 732                                    ConverseStreamOutput::ContentBlockStop(cb_stop) => {
 733                                        if let Some(tool_use) = state
 734                                            .tool_uses_by_index
 735                                            .remove(&cb_stop.content_block_index)
 736                                        {
 737                                            return Some((
 738                                                Some(maybe!({
 739                                                    Ok(LanguageModelCompletionEvent::ToolUse(
 740                                                        LanguageModelToolUse {
 741                                                            id: tool_use.id.into(),
 742                                                            name: tool_use.name.into(),
 743                                                            input: if tool_use.input_json.is_empty()
 744                                                            {
 745                                                                Value::Null
 746                                                            } else {
 747                                                                serde_json::Value::from_str(
 748                                                                    &tool_use.input_json,
 749                                                                )
 750                                                                .map_err(|err| anyhow!(err))?
 751                                                            },
 752                                                        },
 753                                                    ))
 754                                                })),
 755                                                state,
 756                                            ));
 757                                        }
 758                                    }
 759                                    _ => {}
 760                                },
 761                                Err(err) => return Some((Some(Err(anyhow!(err))), state)),
 762                            }
 763                        }
 764                        None
 765                    })
 766                    .await
 767                    .log_err()
 768                    .flatten()
 769            }
 770        },
 771    )
 772    .filter_map(|event| async move { event })
 773}
 774
 775struct ConfigurationView {
 776    access_key_id_editor: Entity<Editor>,
 777    secret_access_key_editor: Entity<Editor>,
 778    region_editor: Entity<Editor>,
 779    state: gpui::Entity<State>,
 780    load_credentials_task: Option<Task<()>>,
 781}
 782
 783impl ConfigurationView {
 784    const PLACEHOLDER_ACCESS_KEY_ID_TEXT: &'static str = "XXXXXXXXXXXXXXXX";
 785    const PLACEHOLDER_SECRET_ACCESS_KEY_TEXT: &'static str =
 786        "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
 787    const PLACEHOLDER_REGION: &'static str = "us-east-1";
 788
 789    fn new(state: gpui::Entity<State>, window: &mut Window, cx: &mut Context<Self>) -> Self {
 790        cx.observe(&state, |_, _, cx| {
 791            cx.notify();
 792        })
 793        .detach();
 794
 795        let load_credentials_task = Some(cx.spawn({
 796            let state = state.clone();
 797            async move |this, cx| {
 798                if let Some(task) = state
 799                    .update(cx, |state, cx| state.authenticate(cx))
 800                    .log_err()
 801                {
 802                    // We don't log an error, because "not signed in" is also an error.
 803                    let _ = task.await;
 804                }
 805                this.update(cx, |this, cx| {
 806                    this.load_credentials_task = None;
 807                    cx.notify();
 808                })
 809                .log_err();
 810            }
 811        }));
 812
 813        Self {
 814            access_key_id_editor: cx.new(|cx| {
 815                let mut editor = Editor::single_line(window, cx);
 816                editor.set_placeholder_text(Self::PLACEHOLDER_ACCESS_KEY_ID_TEXT, cx);
 817                editor
 818            }),
 819            secret_access_key_editor: cx.new(|cx| {
 820                let mut editor = Editor::single_line(window, cx);
 821                editor.set_placeholder_text(Self::PLACEHOLDER_SECRET_ACCESS_KEY_TEXT, cx);
 822                editor
 823            }),
 824            region_editor: cx.new(|cx| {
 825                let mut editor = Editor::single_line(window, cx);
 826                editor.set_placeholder_text(Self::PLACEHOLDER_REGION, cx);
 827                editor
 828            }),
 829            state,
 830            load_credentials_task,
 831        }
 832    }
 833
 834    fn save_credentials(
 835        &mut self,
 836        _: &menu::Confirm,
 837        _window: &mut Window,
 838        cx: &mut Context<Self>,
 839    ) {
 840        let access_key_id = self
 841            .access_key_id_editor
 842            .read(cx)
 843            .text(cx)
 844            .to_string()
 845            .trim()
 846            .to_string();
 847        let secret_access_key = self
 848            .secret_access_key_editor
 849            .read(cx)
 850            .text(cx)
 851            .to_string()
 852            .trim()
 853            .to_string();
 854        let region = self
 855            .region_editor
 856            .read(cx)
 857            .text(cx)
 858            .to_string()
 859            .trim()
 860            .to_string();
 861
 862        let state = self.state.clone();
 863        cx.spawn(async move |_, cx| {
 864            state
 865                .update(cx, |state, cx| {
 866                    let credentials: BedrockCredentials = BedrockCredentials {
 867                        access_key_id: access_key_id.clone(),
 868                        secret_access_key: secret_access_key.clone(),
 869                        region: region.clone(),
 870                    };
 871
 872                    state.set_credentials(credentials, cx)
 873                })?
 874                .await
 875        })
 876        .detach_and_log_err(cx);
 877    }
 878
 879    fn reset_credentials(&mut self, window: &mut Window, cx: &mut Context<Self>) {
 880        self.access_key_id_editor
 881            .update(cx, |editor, cx| editor.set_text("", window, cx));
 882        self.secret_access_key_editor
 883            .update(cx, |editor, cx| editor.set_text("", window, cx));
 884        self.region_editor
 885            .update(cx, |editor, cx| editor.set_text("", window, cx));
 886
 887        let state = self.state.clone();
 888        cx.spawn(async move |_, cx| {
 889            state
 890                .update(cx, |state, cx| state.reset_credentials(cx))?
 891                .await
 892        })
 893        .detach_and_log_err(cx);
 894    }
 895
 896    fn make_text_style(&self, cx: &Context<Self>) -> TextStyle {
 897        let settings = ThemeSettings::get_global(cx);
 898        TextStyle {
 899            color: cx.theme().colors().text,
 900            font_family: settings.ui_font.family.clone(),
 901            font_features: settings.ui_font.features.clone(),
 902            font_fallbacks: settings.ui_font.fallbacks.clone(),
 903            font_size: rems(0.875).into(),
 904            font_weight: settings.ui_font.weight,
 905            font_style: FontStyle::Normal,
 906            line_height: relative(1.3),
 907            background_color: None,
 908            underline: None,
 909            strikethrough: None,
 910            white_space: WhiteSpace::Normal,
 911            text_overflow: None,
 912            text_align: Default::default(),
 913            line_clamp: None,
 914        }
 915    }
 916
 917    fn render_aa_id_editor(&self, cx: &mut Context<Self>) -> impl IntoElement {
 918        let text_style = self.make_text_style(cx);
 919
 920        EditorElement::new(
 921            &self.access_key_id_editor,
 922            EditorStyle {
 923                background: cx.theme().colors().editor_background,
 924                local_player: cx.theme().players().local(),
 925                text: text_style,
 926                ..Default::default()
 927            },
 928        )
 929    }
 930
 931    fn render_sk_editor(&self, cx: &mut Context<Self>) -> impl IntoElement {
 932        let text_style = self.make_text_style(cx);
 933
 934        EditorElement::new(
 935            &self.secret_access_key_editor,
 936            EditorStyle {
 937                background: cx.theme().colors().editor_background,
 938                local_player: cx.theme().players().local(),
 939                text: text_style,
 940                ..Default::default()
 941            },
 942        )
 943    }
 944
 945    fn render_region_editor(&self, cx: &mut Context<Self>) -> impl IntoElement {
 946        let text_style = self.make_text_style(cx);
 947
 948        EditorElement::new(
 949            &self.region_editor,
 950            EditorStyle {
 951                background: cx.theme().colors().editor_background,
 952                local_player: cx.theme().players().local(),
 953                text: text_style,
 954                ..Default::default()
 955            },
 956        )
 957    }
 958
 959    fn should_render_editor(&self, cx: &mut Context<Self>) -> bool {
 960        !self.state.read(cx).is_authenticated()
 961    }
 962}
 963
 964impl Render for ConfigurationView {
 965    fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
 966        let env_var_set = self.state.read(cx).credentials_from_env;
 967        let bg_color = cx.theme().colors().editor_background;
 968        let border_color = cx.theme().colors().border_variant;
 969        let input_base_styles = || {
 970            h_flex()
 971                .w_full()
 972                .px_2()
 973                .py_1()
 974                .bg(bg_color)
 975                .border_1()
 976                .border_color(border_color)
 977                .rounded_sm()
 978        };
 979
 980        if self.load_credentials_task.is_some() {
 981            div().child(Label::new("Loading credentials...")).into_any()
 982        } else if self.should_render_editor(cx) {
 983            v_flex()
 984                .size_full()
 985                .on_action(cx.listener(ConfigurationView::save_credentials))
 986                .child(Label::new("To use Zed's assistant with Bedrock, you need to add the Access Key ID, Secret Access Key and AWS Region. Follow these steps:"))
 987                .child(
 988                    List::new()
 989                        .child(
 990                            InstructionListItem::new(
 991                                "Start by",
 992                                Some("creating a user and security credentials"),
 993                                Some("https://us-east-1.console.aws.amazon.com/iam/home")
 994                            )
 995                        )
 996                        .child(
 997                            InstructionListItem::new(
 998                                "Grant that user permissions according to this documentation:",
 999                                Some("Prerequisites"),
1000                                Some("https://docs.aws.amazon.com/bedrock/latest/userguide/inference-prereq.html")
1001                            )
1002                        )
1003                        .child(
1004                            InstructionListItem::new(
1005                                "Select the models you would like access to:",
1006                                Some("Bedrock Model Catalog"),
1007                                Some("https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/modelaccess")
1008                            )
1009                        )
1010                        .child(
1011                            InstructionListItem::text_only("Fill the fields below and hit enter to start using the assistant")
1012                        )
1013                )
1014                .child(
1015                    v_flex()
1016                        .my_2()
1017                        .gap_1p5()
1018                        .child(
1019                            v_flex()
1020                                .gap_0p5()
1021                                .child(Label::new("Access Key ID").size(LabelSize::Small))
1022                                .child(
1023                                    input_base_styles().child(self.render_aa_id_editor(cx))
1024                                )
1025                        )
1026                        .child(
1027                            v_flex()
1028                                .gap_0p5()
1029                                .child(Label::new("Secret Access Key").size(LabelSize::Small))
1030                                .child(
1031                                    input_base_styles().child(self.render_sk_editor(cx))
1032                                )
1033                        )
1034                        .child(
1035                            v_flex()
1036                                .gap_0p5()
1037                                .child(Label::new("Region").size(LabelSize::Small))
1038                                .child(
1039                                    input_base_styles().child(self.render_region_editor(cx))
1040                                )
1041                            )
1042                )
1043                .child(
1044                    Label::new(
1045                        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."),
1046                    )
1047                        .size(LabelSize::Small)
1048                        .color(Color::Muted),
1049                )
1050                .into_any()
1051        } else {
1052            h_flex()
1053                .size_full()
1054                .justify_between()
1055                .child(
1056                    h_flex()
1057                        .gap_1()
1058                        .child(Icon::new(IconName::Check).color(Color::Success))
1059                        .child(Label::new(if env_var_set {
1060                            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.")
1061                        } else {
1062                            "Credentials configured.".to_string()
1063                        })),
1064                )
1065                .child(
1066                    Button::new("reset-key", "Reset key")
1067                        .icon(Some(IconName::Trash))
1068                        .icon_size(IconSize::Small)
1069                        .icon_position(IconPosition::Start)
1070                        .disabled(env_var_set)
1071                        .when(env_var_set, |this| {
1072                            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.")))
1073                        })
1074                        .on_click(cx.listener(|this, _, window, cx| this.reset_credentials(window, cx))),
1075                )
1076                .into_any()
1077        }
1078    }
1079}