open_router.rs

   1use anyhow::Result;
   2use collections::HashMap;
   3use credentials_provider::CredentialsProvider;
   4use futures::{FutureExt, Stream, StreamExt, future::BoxFuture};
   5use gpui::{AnyView, App, AsyncApp, Context, Entity, SharedString, Task};
   6use http_client::HttpClient;
   7use language_model::{
   8    ApiKeyState, AuthenticateError, EnvVar, IconOrSvg, LanguageModel, LanguageModelCompletionError,
   9    LanguageModelCompletionEvent, LanguageModelId, LanguageModelName, LanguageModelProvider,
  10    LanguageModelProviderId, LanguageModelProviderName, LanguageModelProviderState,
  11    LanguageModelRequest, LanguageModelToolChoice, LanguageModelToolResultContent,
  12    LanguageModelToolSchemaFormat, LanguageModelToolUse, MessageContent, RateLimiter, Role,
  13    StopReason, TokenUsage, env_var,
  14};
  15use open_router::{
  16    Model, ModelMode as OpenRouterModelMode, OPEN_ROUTER_API_URL, ResponseStreamEvent, list_models,
  17};
  18use settings::{OpenRouterAvailableModel as AvailableModel, Settings, SettingsStore};
  19use std::pin::Pin;
  20use std::sync::{Arc, LazyLock};
  21use ui::{ButtonLink, ConfiguredApiCard, List, ListBulletItem, prelude::*};
  22use ui_input::InputField;
  23use util::ResultExt;
  24
  25use language_model::util::{fix_streamed_json, parse_tool_arguments};
  26
  27const PROVIDER_ID: LanguageModelProviderId = LanguageModelProviderId::new("openrouter");
  28const PROVIDER_NAME: LanguageModelProviderName = LanguageModelProviderName::new("OpenRouter");
  29
  30const API_KEY_ENV_VAR_NAME: &str = "OPENROUTER_API_KEY";
  31static API_KEY_ENV_VAR: LazyLock<EnvVar> = env_var!(API_KEY_ENV_VAR_NAME);
  32
  33#[derive(Default, Clone, Debug, PartialEq)]
  34pub struct OpenRouterSettings {
  35    pub api_url: String,
  36    pub available_models: Vec<AvailableModel>,
  37}
  38
  39pub struct OpenRouterLanguageModelProvider {
  40    http_client: Arc<dyn HttpClient>,
  41    state: Entity<State>,
  42}
  43
  44pub struct State {
  45    api_key_state: ApiKeyState,
  46    credentials_provider: Arc<dyn CredentialsProvider>,
  47    http_client: Arc<dyn HttpClient>,
  48    available_models: Vec<open_router::Model>,
  49    fetch_models_task: Option<Task<Result<(), LanguageModelCompletionError>>>,
  50}
  51
  52impl State {
  53    fn is_authenticated(&self) -> bool {
  54        self.api_key_state.has_key()
  55    }
  56
  57    fn set_api_key(&mut self, api_key: Option<String>, cx: &mut Context<Self>) -> Task<Result<()>> {
  58        let credentials_provider = self.credentials_provider.clone();
  59        let api_url = OpenRouterLanguageModelProvider::api_url(cx);
  60        self.api_key_state.store(
  61            api_url,
  62            api_key,
  63            |this| &mut this.api_key_state,
  64            credentials_provider,
  65            cx,
  66        )
  67    }
  68
  69    fn authenticate(&mut self, cx: &mut Context<Self>) -> Task<Result<(), AuthenticateError>> {
  70        let credentials_provider = self.credentials_provider.clone();
  71        let api_url = OpenRouterLanguageModelProvider::api_url(cx);
  72        let task = self.api_key_state.load_if_needed(
  73            api_url,
  74            |this| &mut this.api_key_state,
  75            credentials_provider,
  76            cx,
  77        );
  78
  79        cx.spawn(async move |this, cx| {
  80            let result = task.await;
  81            this.update(cx, |this, cx| this.restart_fetch_models_task(cx))
  82                .ok();
  83            result
  84        })
  85    }
  86
  87    fn fetch_models(
  88        &mut self,
  89        cx: &mut Context<Self>,
  90    ) -> Task<Result<(), LanguageModelCompletionError>> {
  91        let http_client = self.http_client.clone();
  92        let api_url = OpenRouterLanguageModelProvider::api_url(cx);
  93        let Some(api_key) = self.api_key_state.key(&api_url) else {
  94            return Task::ready(Err(LanguageModelCompletionError::NoApiKey {
  95                provider: PROVIDER_NAME,
  96            }));
  97        };
  98        cx.spawn(async move |this, cx| {
  99            let models = list_models(http_client.as_ref(), &api_url, &api_key)
 100                .await
 101                .map_err(|e| {
 102                    LanguageModelCompletionError::Other(anyhow::anyhow!(
 103                        "OpenRouter error: {:?}",
 104                        e
 105                    ))
 106                })?;
 107
 108            this.update(cx, |this, cx| {
 109                this.available_models = models;
 110                cx.notify();
 111            })
 112            .map_err(|e| LanguageModelCompletionError::Other(e))?;
 113
 114            Ok(())
 115        })
 116    }
 117
 118    fn restart_fetch_models_task(&mut self, cx: &mut Context<Self>) {
 119        if self.is_authenticated() {
 120            let task = self.fetch_models(cx);
 121            self.fetch_models_task.replace(task);
 122        } else {
 123            self.available_models = Vec::new();
 124        }
 125    }
 126}
 127
 128impl OpenRouterLanguageModelProvider {
 129    pub fn new(
 130        http_client: Arc<dyn HttpClient>,
 131        credentials_provider: Arc<dyn CredentialsProvider>,
 132        cx: &mut App,
 133    ) -> Self {
 134        let state = cx.new(|cx| {
 135            cx.observe_global::<SettingsStore>({
 136                let mut last_settings = OpenRouterLanguageModelProvider::settings(cx).clone();
 137                move |this: &mut State, cx| {
 138                    let current_settings = OpenRouterLanguageModelProvider::settings(cx);
 139                    let settings_changed = current_settings != &last_settings;
 140                    if settings_changed {
 141                        last_settings = current_settings.clone();
 142                        this.authenticate(cx).detach();
 143                        cx.notify();
 144                    }
 145                }
 146            })
 147            .detach();
 148            State {
 149                api_key_state: ApiKeyState::new(Self::api_url(cx), (*API_KEY_ENV_VAR).clone()),
 150                credentials_provider,
 151                http_client: http_client.clone(),
 152                available_models: Vec::new(),
 153                fetch_models_task: None,
 154            }
 155        });
 156
 157        Self { http_client, state }
 158    }
 159
 160    fn settings(cx: &App) -> &OpenRouterSettings {
 161        &crate::AllLanguageModelSettings::get_global(cx).open_router
 162    }
 163
 164    fn api_url(cx: &App) -> SharedString {
 165        let api_url = &Self::settings(cx).api_url;
 166        if api_url.is_empty() {
 167            OPEN_ROUTER_API_URL.into()
 168        } else {
 169            SharedString::new(api_url.as_str())
 170        }
 171    }
 172
 173    fn create_language_model(&self, model: open_router::Model) -> Arc<dyn LanguageModel> {
 174        Arc::new(OpenRouterLanguageModel {
 175            id: LanguageModelId::from(model.id().to_string()),
 176            model,
 177            state: self.state.clone(),
 178            http_client: self.http_client.clone(),
 179            request_limiter: RateLimiter::new(4),
 180        })
 181    }
 182}
 183
 184impl LanguageModelProviderState for OpenRouterLanguageModelProvider {
 185    type ObservableEntity = State;
 186
 187    fn observable_entity(&self) -> Option<Entity<Self::ObservableEntity>> {
 188        Some(self.state.clone())
 189    }
 190}
 191
 192impl LanguageModelProvider for OpenRouterLanguageModelProvider {
 193    fn id(&self) -> LanguageModelProviderId {
 194        PROVIDER_ID
 195    }
 196
 197    fn name(&self) -> LanguageModelProviderName {
 198        PROVIDER_NAME
 199    }
 200
 201    fn icon(&self) -> IconOrSvg {
 202        IconOrSvg::Icon(IconName::AiOpenRouter)
 203    }
 204
 205    fn default_model(&self, _cx: &App) -> Option<Arc<dyn LanguageModel>> {
 206        Some(self.create_language_model(open_router::Model::default()))
 207    }
 208
 209    fn default_fast_model(&self, _cx: &App) -> Option<Arc<dyn LanguageModel>> {
 210        None
 211    }
 212
 213    fn provided_models(&self, cx: &App) -> Vec<Arc<dyn LanguageModel>> {
 214        let mut models_from_api = self.state.read(cx).available_models.clone();
 215        let mut settings_models = Vec::new();
 216
 217        for model in &Self::settings(cx).available_models {
 218            settings_models.push(open_router::Model {
 219                name: model.name.clone(),
 220                display_name: model.display_name.clone(),
 221                max_tokens: model.max_tokens,
 222                supports_tools: model.supports_tools,
 223                supports_images: model.supports_images,
 224                mode: model.mode.unwrap_or_default(),
 225                provider: model.provider.clone(),
 226            });
 227        }
 228
 229        for settings_model in &settings_models {
 230            if let Some(pos) = models_from_api
 231                .iter()
 232                .position(|m| m.name == settings_model.name)
 233            {
 234                models_from_api[pos] = settings_model.clone();
 235            } else {
 236                models_from_api.push(settings_model.clone());
 237            }
 238        }
 239
 240        models_from_api
 241            .into_iter()
 242            .map(|model| self.create_language_model(model))
 243            .collect()
 244    }
 245
 246    fn is_authenticated(&self, cx: &App) -> bool {
 247        self.state.read(cx).is_authenticated()
 248    }
 249
 250    fn authenticate(&self, cx: &mut App) -> Task<Result<(), AuthenticateError>> {
 251        self.state.update(cx, |state, cx| state.authenticate(cx))
 252    }
 253
 254    fn configuration_view(
 255        &self,
 256        _target_agent: language_model::ConfigurationViewTargetAgent,
 257        window: &mut Window,
 258        cx: &mut App,
 259    ) -> AnyView {
 260        cx.new(|cx| ConfigurationView::new(self.state.clone(), window, cx))
 261            .into()
 262    }
 263
 264    fn reset_credentials(&self, cx: &mut App) -> Task<Result<()>> {
 265        self.state
 266            .update(cx, |state, cx| state.set_api_key(None, cx))
 267    }
 268}
 269
 270pub struct OpenRouterLanguageModel {
 271    id: LanguageModelId,
 272    model: open_router::Model,
 273    state: Entity<State>,
 274    http_client: Arc<dyn HttpClient>,
 275    request_limiter: RateLimiter,
 276}
 277
 278impl OpenRouterLanguageModel {
 279    fn stream_completion(
 280        &self,
 281        request: open_router::Request,
 282        cx: &AsyncApp,
 283    ) -> BoxFuture<
 284        'static,
 285        Result<
 286            futures::stream::BoxStream<
 287                'static,
 288                Result<ResponseStreamEvent, open_router::OpenRouterError>,
 289            >,
 290            LanguageModelCompletionError,
 291        >,
 292    > {
 293        let http_client = self.http_client.clone();
 294        let (api_key, api_url) = self.state.read_with(cx, |state, cx| {
 295            let api_url = OpenRouterLanguageModelProvider::api_url(cx);
 296            (state.api_key_state.key(&api_url), api_url)
 297        });
 298
 299        async move {
 300            let Some(api_key) = api_key else {
 301                return Err(LanguageModelCompletionError::NoApiKey {
 302                    provider: PROVIDER_NAME,
 303                });
 304            };
 305            let request =
 306                open_router::stream_completion(http_client.as_ref(), &api_url, &api_key, request);
 307            request.await.map_err(Into::into)
 308        }
 309        .boxed()
 310    }
 311}
 312
 313impl LanguageModel for OpenRouterLanguageModel {
 314    fn id(&self) -> LanguageModelId {
 315        self.id.clone()
 316    }
 317
 318    fn name(&self) -> LanguageModelName {
 319        LanguageModelName::from(self.model.display_name().to_string())
 320    }
 321
 322    fn provider_id(&self) -> LanguageModelProviderId {
 323        PROVIDER_ID
 324    }
 325
 326    fn provider_name(&self) -> LanguageModelProviderName {
 327        PROVIDER_NAME
 328    }
 329
 330    fn supports_tools(&self) -> bool {
 331        self.model.supports_tool_calls()
 332    }
 333
 334    fn supports_streaming_tools(&self) -> bool {
 335        true
 336    }
 337
 338    fn supports_thinking(&self) -> bool {
 339        matches!(self.model.mode, OpenRouterModelMode::Thinking { .. })
 340    }
 341
 342    fn tool_input_format(&self) -> LanguageModelToolSchemaFormat {
 343        let model_id = self.model.id().trim().to_lowercase();
 344        if model_id.contains("gemini") || model_id.contains("grok") {
 345            LanguageModelToolSchemaFormat::JsonSchemaSubset
 346        } else {
 347            LanguageModelToolSchemaFormat::JsonSchema
 348        }
 349    }
 350
 351    fn telemetry_id(&self) -> String {
 352        format!("openrouter/{}", self.model.id())
 353    }
 354
 355    fn max_token_count(&self) -> u64 {
 356        self.model.max_token_count()
 357    }
 358
 359    fn max_output_tokens(&self) -> Option<u64> {
 360        self.model.max_output_tokens()
 361    }
 362
 363    fn supports_tool_choice(&self, choice: LanguageModelToolChoice) -> bool {
 364        match choice {
 365            LanguageModelToolChoice::Auto => true,
 366            LanguageModelToolChoice::Any => true,
 367            LanguageModelToolChoice::None => true,
 368        }
 369    }
 370
 371    fn supports_images(&self) -> bool {
 372        self.model.supports_images.unwrap_or(false)
 373    }
 374
 375    fn stream_completion(
 376        &self,
 377        request: LanguageModelRequest,
 378        cx: &AsyncApp,
 379    ) -> BoxFuture<
 380        'static,
 381        Result<
 382            futures::stream::BoxStream<
 383                'static,
 384                Result<LanguageModelCompletionEvent, LanguageModelCompletionError>,
 385            >,
 386            LanguageModelCompletionError,
 387        >,
 388    > {
 389        let openrouter_request = into_open_router(request, &self.model, self.max_output_tokens());
 390        let request = self.stream_completion(openrouter_request, cx);
 391        let future = self.request_limiter.stream(async move {
 392            let response = request.await?;
 393            Ok(OpenRouterEventMapper::new().map_stream(response))
 394        });
 395        async move { Ok(future.await?.boxed()) }.boxed()
 396    }
 397}
 398
 399pub fn into_open_router(
 400    request: LanguageModelRequest,
 401    model: &Model,
 402    max_output_tokens: Option<u64>,
 403) -> open_router::Request {
 404    // Anthropic models via OpenRouter don't accept reasoning_details being echoed back
 405    // in requests - it's an output-only field for them. However, Gemini models require
 406    // the thought signatures to be echoed back for proper reasoning chain continuity.
 407    // Note: OpenRouter's model API provides an `architecture.tokenizer` field (e.g. "Claude",
 408    // "Gemini") which could replace this ID prefix check, but since this is the only place
 409    // we need this distinction, we're just using this less invasive check instead.
 410    // If we ever have a more formal distionction between the models in the future,
 411    // we should revise this to use that instead.
 412    let is_anthropic_model = model.id().starts_with("anthropic/");
 413
 414    let mut messages = Vec::new();
 415    for message in request.messages {
 416        let reasoning_details_for_message = if is_anthropic_model {
 417            None
 418        } else {
 419            message.reasoning_details.clone()
 420        };
 421
 422        for content in message.content {
 423            match content {
 424                MessageContent::Text(text) => add_message_content_part(
 425                    open_router::MessagePart::Text { text },
 426                    message.role,
 427                    &mut messages,
 428                    reasoning_details_for_message.clone(),
 429                ),
 430                MessageContent::Thinking { .. } => {}
 431                MessageContent::RedactedThinking(_) => {}
 432                MessageContent::Image(image) => {
 433                    add_message_content_part(
 434                        open_router::MessagePart::Image {
 435                            image_url: image.to_base64_url(),
 436                        },
 437                        message.role,
 438                        &mut messages,
 439                        reasoning_details_for_message.clone(),
 440                    );
 441                }
 442                MessageContent::ToolUse(tool_use) => {
 443                    let tool_call = open_router::ToolCall {
 444                        id: tool_use.id.to_string(),
 445                        content: open_router::ToolCallContent::Function {
 446                            function: open_router::FunctionContent {
 447                                name: tool_use.name.to_string(),
 448                                arguments: serde_json::to_string(&tool_use.input)
 449                                    .unwrap_or_default(),
 450                                thought_signature: tool_use.thought_signature.clone(),
 451                            },
 452                        },
 453                    };
 454
 455                    if let Some(open_router::RequestMessage::Assistant { tool_calls, .. }) =
 456                        messages.last_mut()
 457                    {
 458                        tool_calls.push(tool_call);
 459                    } else {
 460                        messages.push(open_router::RequestMessage::Assistant {
 461                            content: None,
 462                            tool_calls: vec![tool_call],
 463                            reasoning_details: reasoning_details_for_message.clone(),
 464                        });
 465                    }
 466                }
 467                MessageContent::ToolResult(tool_result) => {
 468                    let content = match &tool_result.content {
 469                        LanguageModelToolResultContent::Text(text) => {
 470                            vec![open_router::MessagePart::Text {
 471                                text: text.to_string(),
 472                            }]
 473                        }
 474                        LanguageModelToolResultContent::Image(image) => {
 475                            vec![open_router::MessagePart::Image {
 476                                image_url: image.to_base64_url(),
 477                            }]
 478                        }
 479                    };
 480
 481                    messages.push(open_router::RequestMessage::Tool {
 482                        content: content.into(),
 483                        tool_call_id: tool_result.tool_use_id.to_string(),
 484                    });
 485                }
 486            }
 487        }
 488    }
 489
 490    open_router::Request {
 491        model: model.id().into(),
 492        messages,
 493        stream: true,
 494        stop: request.stop,
 495        temperature: request.temperature.unwrap_or(0.4),
 496        max_tokens: max_output_tokens,
 497        parallel_tool_calls: if model.supports_parallel_tool_calls() && !request.tools.is_empty() {
 498            Some(false)
 499        } else {
 500            None
 501        },
 502        usage: open_router::RequestUsage { include: true },
 503        reasoning: if request.thinking_allowed
 504            && let OpenRouterModelMode::Thinking { budget_tokens } = model.mode
 505        {
 506            Some(open_router::Reasoning {
 507                effort: None,
 508                max_tokens: budget_tokens,
 509                exclude: Some(false),
 510                enabled: Some(true),
 511            })
 512        } else {
 513            None
 514        },
 515        tools: request
 516            .tools
 517            .into_iter()
 518            .map(|tool| open_router::ToolDefinition::Function {
 519                function: open_router::FunctionDefinition {
 520                    name: tool.name,
 521                    description: Some(tool.description),
 522                    parameters: Some(tool.input_schema),
 523                },
 524            })
 525            .collect(),
 526        tool_choice: request.tool_choice.map(|choice| match choice {
 527            LanguageModelToolChoice::Auto => open_router::ToolChoice::Auto,
 528            LanguageModelToolChoice::Any => open_router::ToolChoice::Required,
 529            LanguageModelToolChoice::None => open_router::ToolChoice::None,
 530        }),
 531        provider: model.provider.clone(),
 532    }
 533}
 534
 535fn add_message_content_part(
 536    new_part: open_router::MessagePart,
 537    role: Role,
 538    messages: &mut Vec<open_router::RequestMessage>,
 539    reasoning_details: Option<serde_json::Value>,
 540) {
 541    match (role, messages.last_mut()) {
 542        (Role::User, Some(open_router::RequestMessage::User { content }))
 543        | (Role::System, Some(open_router::RequestMessage::System { content })) => {
 544            content.push_part(new_part);
 545        }
 546        (
 547            Role::Assistant,
 548            Some(open_router::RequestMessage::Assistant {
 549                content: Some(content),
 550                ..
 551            }),
 552        ) => {
 553            content.push_part(new_part);
 554        }
 555        _ => {
 556            messages.push(match role {
 557                Role::User => open_router::RequestMessage::User {
 558                    content: open_router::MessageContent::from(vec![new_part]),
 559                },
 560                Role::Assistant => open_router::RequestMessage::Assistant {
 561                    content: Some(open_router::MessageContent::from(vec![new_part])),
 562                    tool_calls: Vec::new(),
 563                    reasoning_details,
 564                },
 565                Role::System => open_router::RequestMessage::System {
 566                    content: open_router::MessageContent::from(vec![new_part]),
 567                },
 568            });
 569        }
 570    }
 571}
 572
 573pub struct OpenRouterEventMapper {
 574    tool_calls_by_index: HashMap<usize, RawToolCall>,
 575    reasoning_details: Option<serde_json::Value>,
 576}
 577
 578impl OpenRouterEventMapper {
 579    pub fn new() -> Self {
 580        Self {
 581            tool_calls_by_index: HashMap::default(),
 582            reasoning_details: None,
 583        }
 584    }
 585
 586    pub fn map_stream(
 587        mut self,
 588        events: Pin<
 589            Box<
 590                dyn Send + Stream<Item = Result<ResponseStreamEvent, open_router::OpenRouterError>>,
 591            >,
 592        >,
 593    ) -> impl Stream<Item = Result<LanguageModelCompletionEvent, LanguageModelCompletionError>>
 594    {
 595        events.flat_map(move |event| {
 596            futures::stream::iter(match event {
 597                Ok(event) => self.map_event(event),
 598                Err(error) => vec![Err(error.into())],
 599            })
 600        })
 601    }
 602
 603    pub fn map_event(
 604        &mut self,
 605        event: ResponseStreamEvent,
 606    ) -> Vec<Result<LanguageModelCompletionEvent, LanguageModelCompletionError>> {
 607        let mut events = Vec::new();
 608
 609        if let Some(usage) = event.usage {
 610            events.push(Ok(LanguageModelCompletionEvent::UsageUpdate(TokenUsage {
 611                input_tokens: usage.prompt_tokens,
 612                output_tokens: usage.completion_tokens,
 613                cache_creation_input_tokens: 0,
 614                cache_read_input_tokens: 0,
 615            })));
 616        }
 617
 618        let Some(choice) = event.choices.first() else {
 619            return events;
 620        };
 621
 622        if let Some(details) = choice.delta.reasoning_details.clone() {
 623            // Emit reasoning_details immediately
 624            events.push(Ok(LanguageModelCompletionEvent::ReasoningDetails(
 625                details.clone(),
 626            )));
 627            self.reasoning_details = Some(details);
 628        }
 629
 630        if let Some(reasoning) = choice.delta.reasoning.clone() {
 631            events.push(Ok(LanguageModelCompletionEvent::Thinking {
 632                text: reasoning,
 633                signature: None,
 634            }));
 635        }
 636
 637        if let Some(content) = choice.delta.content.clone() {
 638            // OpenRouter send empty content string with the reasoning content
 639            // This is a workaround for the OpenRouter API bug
 640            if !content.is_empty() {
 641                events.push(Ok(LanguageModelCompletionEvent::Text(content)));
 642            }
 643        }
 644
 645        if let Some(tool_calls) = choice.delta.tool_calls.as_ref() {
 646            for tool_call in tool_calls {
 647                let entry = self.tool_calls_by_index.entry(tool_call.index).or_default();
 648
 649                if let Some(tool_id) = tool_call.id.clone() {
 650                    entry.id = tool_id;
 651                }
 652
 653                if let Some(function) = tool_call.function.as_ref() {
 654                    if let Some(name) = function.name.clone() {
 655                        entry.name = name;
 656                    }
 657
 658                    if let Some(arguments) = function.arguments.clone() {
 659                        entry.arguments.push_str(&arguments);
 660                    }
 661
 662                    if let Some(signature) = function.thought_signature.clone() {
 663                        entry.thought_signature = Some(signature);
 664                    }
 665                }
 666
 667                if !entry.id.is_empty() && !entry.name.is_empty() {
 668                    if let Ok(input) = serde_json::from_str::<serde_json::Value>(
 669                        &fix_streamed_json(&entry.arguments),
 670                    ) {
 671                        events.push(Ok(LanguageModelCompletionEvent::ToolUse(
 672                            LanguageModelToolUse {
 673                                id: entry.id.clone().into(),
 674                                name: entry.name.as_str().into(),
 675                                is_input_complete: false,
 676                                input,
 677                                raw_input: entry.arguments.clone(),
 678                                thought_signature: entry.thought_signature.clone(),
 679                            },
 680                        )));
 681                    }
 682                }
 683            }
 684        }
 685
 686        match choice.finish_reason.as_deref() {
 687            Some("stop") => {
 688                // Don't emit reasoning_details here - already emitted immediately when captured
 689                events.push(Ok(LanguageModelCompletionEvent::Stop(StopReason::EndTurn)));
 690            }
 691            Some("tool_calls") => {
 692                events.extend(self.tool_calls_by_index.drain().map(|(_, tool_call)| {
 693                    match parse_tool_arguments(&tool_call.arguments) {
 694                        Ok(input) => Ok(LanguageModelCompletionEvent::ToolUse(
 695                            LanguageModelToolUse {
 696                                id: tool_call.id.clone().into(),
 697                                name: tool_call.name.as_str().into(),
 698                                is_input_complete: true,
 699                                input,
 700                                raw_input: tool_call.arguments.clone(),
 701                                thought_signature: tool_call.thought_signature.clone(),
 702                            },
 703                        )),
 704                        Err(error) => Ok(LanguageModelCompletionEvent::ToolUseJsonParseError {
 705                            id: tool_call.id.clone().into(),
 706                            tool_name: tool_call.name.as_str().into(),
 707                            raw_input: tool_call.arguments.clone().into(),
 708                            json_parse_error: error.to_string(),
 709                        }),
 710                    }
 711                }));
 712
 713                // Don't emit reasoning_details here - already emitted immediately when captured
 714                events.push(Ok(LanguageModelCompletionEvent::Stop(StopReason::ToolUse)));
 715            }
 716            Some(stop_reason) => {
 717                log::error!("Unexpected OpenRouter stop_reason: {stop_reason:?}",);
 718                // Don't emit reasoning_details here - already emitted immediately when captured
 719                events.push(Ok(LanguageModelCompletionEvent::Stop(StopReason::EndTurn)));
 720            }
 721            None => {}
 722        }
 723
 724        events
 725    }
 726}
 727
 728#[derive(Default)]
 729struct RawToolCall {
 730    id: String,
 731    name: String,
 732    arguments: String,
 733    thought_signature: Option<String>,
 734}
 735
 736struct ConfigurationView {
 737    api_key_editor: Entity<InputField>,
 738    state: Entity<State>,
 739    load_credentials_task: Option<Task<()>>,
 740}
 741
 742impl ConfigurationView {
 743    fn new(state: Entity<State>, window: &mut Window, cx: &mut Context<Self>) -> Self {
 744        let api_key_editor = cx.new(|cx| {
 745            InputField::new(
 746                window,
 747                cx,
 748                "sk_or_000000000000000000000000000000000000000000000000",
 749            )
 750        });
 751
 752        cx.observe(&state, |_, _, cx| {
 753            cx.notify();
 754        })
 755        .detach();
 756
 757        let load_credentials_task = Some(cx.spawn_in(window, {
 758            let state = state.clone();
 759            async move |this, cx| {
 760                if let Some(task) = Some(state.update(cx, |state, cx| state.authenticate(cx))) {
 761                    let _ = task.await;
 762                }
 763
 764                this.update(cx, |this, cx| {
 765                    this.load_credentials_task = None;
 766                    cx.notify();
 767                })
 768                .log_err();
 769            }
 770        }));
 771
 772        Self {
 773            api_key_editor,
 774            state,
 775            load_credentials_task,
 776        }
 777    }
 778
 779    fn save_api_key(&mut self, _: &menu::Confirm, window: &mut Window, cx: &mut Context<Self>) {
 780        let api_key = self.api_key_editor.read(cx).text(cx).trim().to_string();
 781        if api_key.is_empty() {
 782            return;
 783        }
 784
 785        // url changes can cause the editor to be displayed again
 786        self.api_key_editor
 787            .update(cx, |editor, cx| editor.set_text("", window, cx));
 788
 789        let state = self.state.clone();
 790        cx.spawn_in(window, async move |_, cx| {
 791            state
 792                .update(cx, |state, cx| state.set_api_key(Some(api_key), cx))
 793                .await
 794        })
 795        .detach_and_log_err(cx);
 796    }
 797
 798    fn reset_api_key(&mut self, window: &mut Window, cx: &mut Context<Self>) {
 799        self.api_key_editor
 800            .update(cx, |editor, cx| editor.set_text("", window, cx));
 801
 802        let state = self.state.clone();
 803        cx.spawn_in(window, async move |_, cx| {
 804            state
 805                .update(cx, |state, cx| state.set_api_key(None, cx))
 806                .await
 807        })
 808        .detach_and_log_err(cx);
 809    }
 810
 811    fn should_render_editor(&self, cx: &mut Context<Self>) -> bool {
 812        !self.state.read(cx).is_authenticated()
 813    }
 814}
 815
 816impl Render for ConfigurationView {
 817    fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
 818        let env_var_set = self.state.read(cx).api_key_state.is_from_env_var();
 819        let configured_card_label = if env_var_set {
 820            format!("API key set in {API_KEY_ENV_VAR_NAME} environment variable")
 821        } else {
 822            let api_url = OpenRouterLanguageModelProvider::api_url(cx);
 823            if api_url == OPEN_ROUTER_API_URL {
 824                "API key configured".to_string()
 825            } else {
 826                format!("API key configured for {}", api_url)
 827            }
 828        };
 829
 830        if self.load_credentials_task.is_some() {
 831            div()
 832                .child(Label::new("Loading credentials..."))
 833                .into_any_element()
 834        } else if self.should_render_editor(cx) {
 835            v_flex()
 836                .size_full()
 837                .on_action(cx.listener(Self::save_api_key))
 838                .child(Label::new("To use Zed's agent with OpenRouter, you need to add an API key. Follow these steps:"))
 839                .child(
 840                    List::new()
 841                        .child(
 842                            ListBulletItem::new("")
 843                                .child(Label::new("Create an API key by visiting"))
 844                                .child(ButtonLink::new("OpenRouter's console", "https://openrouter.ai/keys"))
 845                        )
 846                        .child(ListBulletItem::new("Ensure your OpenRouter account has credits")
 847                        )
 848                        .child(ListBulletItem::new("Paste your API key below and hit enter to start using the assistant")
 849                        ),
 850                )
 851                .child(self.api_key_editor.clone())
 852                .child(
 853                    Label::new(
 854                        format!("You can also set the {API_KEY_ENV_VAR_NAME} environment variable and restart Zed."),
 855                    )
 856                    .size(LabelSize::Small).color(Color::Muted),
 857                )
 858                .into_any_element()
 859        } else {
 860            ConfiguredApiCard::new(configured_card_label)
 861                .disabled(env_var_set)
 862                .on_click(cx.listener(|this, _, window, cx| this.reset_api_key(window, cx)))
 863                .when(env_var_set, |this| {
 864                    this.tooltip_label(format!("To reset your API key, unset the {API_KEY_ENV_VAR_NAME} environment variable."))
 865                })
 866                .into_any_element()
 867        }
 868    }
 869}
 870
 871#[cfg(test)]
 872mod tests {
 873    use super::*;
 874
 875    use open_router::{ChoiceDelta, FunctionChunk, ResponseMessageDelta, ToolCallChunk};
 876
 877    #[gpui::test]
 878    async fn test_reasoning_details_preservation_with_tool_calls() {
 879        // This test verifies that reasoning_details are properly captured and preserved
 880        // when a model uses tool calling with reasoning/thinking tokens.
 881        //
 882        // The key regression this prevents:
 883        // - OpenRouter sends multiple reasoning_details updates during streaming
 884        // - First with actual content (encrypted reasoning data)
 885        // - Then with empty array on completion
 886        // - We must NOT overwrite the real data with the empty array
 887
 888        let mut mapper = OpenRouterEventMapper::new();
 889
 890        // Simulate the streaming events as they come from OpenRouter/Gemini
 891        let events = vec![
 892            // Event 1: Initial reasoning details with text
 893            ResponseStreamEvent {
 894                id: Some("response_123".into()),
 895                created: 1234567890,
 896                model: "google/gemini-3.1-pro-preview".into(),
 897                choices: vec![ChoiceDelta {
 898                    index: 0,
 899                    delta: ResponseMessageDelta {
 900                        role: None,
 901                        content: None,
 902                        reasoning: None,
 903                        tool_calls: None,
 904                        reasoning_details: Some(serde_json::json!([
 905                            {
 906                                "type": "reasoning.text",
 907                                "text": "Let me analyze this request...",
 908                                "format": "google-gemini-v1",
 909                                "index": 0
 910                            }
 911                        ])),
 912                    },
 913                    finish_reason: None,
 914                }],
 915                usage: None,
 916            },
 917            // Event 2: More reasoning details
 918            ResponseStreamEvent {
 919                id: Some("response_123".into()),
 920                created: 1234567890,
 921                model: "google/gemini-3.1-pro-preview".into(),
 922                choices: vec![ChoiceDelta {
 923                    index: 0,
 924                    delta: ResponseMessageDelta {
 925                        role: None,
 926                        content: None,
 927                        reasoning: None,
 928                        tool_calls: None,
 929                        reasoning_details: Some(serde_json::json!([
 930                            {
 931                                "type": "reasoning.encrypted",
 932                                "data": "EtgDCtUDAdHtim9OF5jm4aeZSBAtl/randomized123",
 933                                "format": "google-gemini-v1",
 934                                "index": 0,
 935                                "id": "tool_call_abc123"
 936                            }
 937                        ])),
 938                    },
 939                    finish_reason: None,
 940                }],
 941                usage: None,
 942            },
 943            // Event 3: Tool call starts
 944            ResponseStreamEvent {
 945                id: Some("response_123".into()),
 946                created: 1234567890,
 947                model: "google/gemini-3.1-pro-preview".into(),
 948                choices: vec![ChoiceDelta {
 949                    index: 0,
 950                    delta: ResponseMessageDelta {
 951                        role: None,
 952                        content: None,
 953                        reasoning: None,
 954                        tool_calls: Some(vec![ToolCallChunk {
 955                            index: 0,
 956                            id: Some("tool_call_abc123".into()),
 957                            function: Some(FunctionChunk {
 958                                name: Some("list_directory".into()),
 959                                arguments: Some("{\"path\":\"test\"}".into()),
 960                                thought_signature: Some("sha256:test_signature_xyz789".into()),
 961                            }),
 962                        }]),
 963                        reasoning_details: None,
 964                    },
 965                    finish_reason: None,
 966                }],
 967                usage: None,
 968            },
 969            // Event 4: Empty reasoning_details on tool_calls finish
 970            // This is the critical event - we must not overwrite with this empty array!
 971            ResponseStreamEvent {
 972                id: Some("response_123".into()),
 973                created: 1234567890,
 974                model: "google/gemini-3.1-pro-preview".into(),
 975                choices: vec![ChoiceDelta {
 976                    index: 0,
 977                    delta: ResponseMessageDelta {
 978                        role: None,
 979                        content: None,
 980                        reasoning: None,
 981                        tool_calls: None,
 982                        reasoning_details: Some(serde_json::json!([])),
 983                    },
 984                    finish_reason: Some("tool_calls".into()),
 985                }],
 986                usage: None,
 987            },
 988        ];
 989
 990        // Process all events
 991        let mut collected_events = Vec::new();
 992        for event in events {
 993            let mapped = mapper.map_event(event);
 994            collected_events.extend(mapped);
 995        }
 996
 997        // Verify we got the expected events
 998        let mut has_tool_use = false;
 999        let mut reasoning_details_events = Vec::new();
1000        let mut thought_signature_value = None;
1001
1002        for event_result in collected_events {
1003            match event_result {
1004                Ok(LanguageModelCompletionEvent::ToolUse(tool_use)) => {
1005                    has_tool_use = true;
1006                    assert_eq!(tool_use.id.to_string(), "tool_call_abc123");
1007                    assert_eq!(tool_use.name.as_ref(), "list_directory");
1008                    thought_signature_value = tool_use.thought_signature.clone();
1009                }
1010                Ok(LanguageModelCompletionEvent::ReasoningDetails(details)) => {
1011                    reasoning_details_events.push(details);
1012                }
1013                _ => {}
1014            }
1015        }
1016
1017        // Assertions
1018        assert!(has_tool_use, "Should have emitted ToolUse event");
1019        assert!(
1020            !reasoning_details_events.is_empty(),
1021            "Should have emitted ReasoningDetails events"
1022        );
1023
1024        // We should have received multiple reasoning_details events (text, encrypted, empty)
1025        // The agent layer is responsible for keeping only the first non-empty one
1026        assert!(
1027            reasoning_details_events.len() >= 2,
1028            "Should have multiple reasoning_details events from streaming"
1029        );
1030
1031        // Verify at least one contains the encrypted data
1032        let has_encrypted = reasoning_details_events.iter().any(|details| {
1033            if let serde_json::Value::Array(arr) = details {
1034                arr.iter().any(|item| {
1035                    item["type"] == "reasoning.encrypted"
1036                        && item["data"]
1037                            .as_str()
1038                            .map_or(false, |s| s.contains("EtgDCtUDAdHtim9OF5jm4aeZSBAtl"))
1039                })
1040            } else {
1041                false
1042            }
1043        });
1044        assert!(
1045            has_encrypted,
1046            "Should have at least one reasoning_details with encrypted data"
1047        );
1048
1049        // Verify thought_signature was captured
1050        assert!(
1051            thought_signature_value.is_some(),
1052            "Tool use should have thought_signature"
1053        );
1054        assert_eq!(
1055            thought_signature_value.unwrap(),
1056            "sha256:test_signature_xyz789"
1057        );
1058    }
1059
1060    #[gpui::test]
1061    async fn test_usage_only_chunk_with_empty_choices_does_not_error() {
1062        let mut mapper = OpenRouterEventMapper::new();
1063
1064        let events = mapper.map_event(ResponseStreamEvent {
1065            id: Some("response_123".into()),
1066            created: 1234567890,
1067            model: "google/gemini-3-flash-preview".into(),
1068            choices: Vec::new(),
1069            usage: Some(open_router::Usage {
1070                prompt_tokens: 12,
1071                completion_tokens: 7,
1072                total_tokens: 19,
1073            }),
1074        });
1075
1076        assert_eq!(events.len(), 1);
1077        match events.into_iter().next().unwrap() {
1078            Ok(LanguageModelCompletionEvent::UsageUpdate(usage)) => {
1079                assert_eq!(usage.input_tokens, 12);
1080                assert_eq!(usage.output_tokens, 7);
1081            }
1082            other => panic!("Expected usage update event, got: {other:?}"),
1083        }
1084    }
1085
1086    #[gpui::test]
1087    async fn test_agent_prevents_empty_reasoning_details_overwrite() {
1088        // This test verifies that the agent layer prevents empty reasoning_details
1089        // from overwriting non-empty ones, even though the mapper emits all events.
1090
1091        // Simulate what the agent does when it receives multiple ReasoningDetails events
1092        let mut agent_reasoning_details: Option<serde_json::Value> = None;
1093
1094        let events = vec![
1095            // First event: non-empty reasoning_details
1096            serde_json::json!([
1097                {
1098                    "type": "reasoning.encrypted",
1099                    "data": "real_data_here",
1100                    "format": "google-gemini-v1"
1101                }
1102            ]),
1103            // Second event: empty array (should not overwrite)
1104            serde_json::json!([]),
1105        ];
1106
1107        for details in events {
1108            // This mimics the agent's logic: only store if we don't already have it
1109            if agent_reasoning_details.is_none() {
1110                agent_reasoning_details = Some(details);
1111            }
1112        }
1113
1114        // Verify the agent kept the first non-empty reasoning_details
1115        assert!(agent_reasoning_details.is_some());
1116        let final_details = agent_reasoning_details.unwrap();
1117        if let serde_json::Value::Array(arr) = &final_details {
1118            assert!(
1119                !arr.is_empty(),
1120                "Agent should have kept the non-empty reasoning_details"
1121            );
1122            assert_eq!(arr[0]["data"], "real_data_here");
1123        } else {
1124            panic!("Expected array");
1125        }
1126    }
1127}