bedrock.rs

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