946 lines · 33.0 KB · Rust
RawDownload
use anyhow::Result;use collections::BTreeMap;use credentials_provider::CredentialsProvider;use fs::Fs;use futures::{FutureExt, StreamExt, future::BoxFuture};use gpui::{AnyView, App, AsyncApp, Context, Entity, SharedString, Task, TaskExt, Window};use http_client::{AsyncBody, HttpClient, http};use language_model::{    ApiKeyState, AuthenticateError, EnvVar, IconOrSvg, LanguageModel, LanguageModelCompletionError,    LanguageModelCompletionEvent, LanguageModelEffortLevel, LanguageModelId, LanguageModelName,    LanguageModelProvider, LanguageModelProviderId, LanguageModelProviderName,    LanguageModelProviderState, LanguageModelRequest, LanguageModelToolChoice, RateLimiter,    ReasoningEffort, env_var,};use opencode::{ApiProtocol, OPENCODE_API_URL, OpenCodeSubscription};pub use settings::OpenCodeAvailableModel as AvailableModel;use settings::{Settings, SettingsStore, update_settings_file};use std::sync::{Arc, LazyLock};use strum::IntoEnumIterator;use ui::{    Banner, ButtonLink, ConfiguredApiCard, List, ListBulletItem, Severity, Switch,    SwitchLabelPosition, ToggleState, prelude::*,};use ui_input::InputField;use util::ResultExt; use crate::provider::anthropic::{AnthropicEventMapper, into_anthropic};use crate::provider::google::{GoogleEventMapper, into_google};use crate::provider::open_ai::{    OpenAiEventMapper, OpenAiResponseEventMapper, into_open_ai, into_open_ai_response,}; fn normalize_reasoning_effort(effort: &str) -> Option<ReasoningEffort> {    match effort.trim().to_ascii_lowercase().as_str() {        "none" => Some(ReasoningEffort::None),        "minimal" => Some(ReasoningEffort::Minimal),        "low" => Some(ReasoningEffort::Low),        "medium" => Some(ReasoningEffort::Medium),        "high" => Some(ReasoningEffort::High),        "max" | "xhigh" => Some(ReasoningEffort::XHigh),        _ => None,    }} fn reasoning_effort_display(effort: ReasoningEffort) -> (&'static str, &'static str) {    match effort {        ReasoningEffort::None => ("None", "none"),        ReasoningEffort::Minimal => ("Minimal", "minimal"),        ReasoningEffort::Low => ("Low", "low"),        ReasoningEffort::Medium => ("Medium", "medium"),        ReasoningEffort::High => ("High", "high"),        ReasoningEffort::XHigh => ("XHigh", "xhigh"),    }} const PROVIDER_ID: LanguageModelProviderId = LanguageModelProviderId::new("opencode");const PROVIDER_NAME: LanguageModelProviderName = LanguageModelProviderName::new("OpenCode"); const API_KEY_ENV_VAR_NAME: &str = "OPENCODE_API_KEY";static API_KEY_ENV_VAR: LazyLock<EnvVar> = env_var!(API_KEY_ENV_VAR_NAME); #[derive(Default, Clone, Debug, PartialEq)]pub struct OpenCodeSettings {    pub api_url: String,    pub available_models: Vec<AvailableModel>,    pub show_zen_models: bool,    pub show_go_models: bool,    pub show_free_models: bool,} pub struct OpenCodeLanguageModelProvider {    http_client: Arc<dyn HttpClient>,    state: Entity<State>,} pub struct State {    api_key_state: ApiKeyState,    credentials_provider: Arc<dyn CredentialsProvider>,} impl State {    fn is_authenticated(&self) -> bool {        self.api_key_state.has_key()    }     fn set_api_key(&mut self, api_key: Option<String>, cx: &mut Context<Self>) -> Task<Result<()>> {        let credentials_provider = self.credentials_provider.clone();        let api_url = OpenCodeLanguageModelProvider::api_url(cx);        self.api_key_state.store(            api_url,            api_key,            |this| &mut this.api_key_state,            credentials_provider,            cx,        )    }     fn authenticate(&mut self, cx: &mut Context<Self>) -> Task<Result<(), AuthenticateError>> {        let credentials_provider = self.credentials_provider.clone();        let api_url = OpenCodeLanguageModelProvider::api_url(cx);        self.api_key_state.load_if_needed(            api_url,            |this| &mut this.api_key_state,            credentials_provider,            cx,        )    }} impl OpenCodeLanguageModelProvider {    pub fn new(        http_client: Arc<dyn HttpClient>,        credentials_provider: Arc<dyn CredentialsProvider>,        cx: &mut App,    ) -> Self {        let state = cx.new(|cx| {            cx.observe_global::<SettingsStore>(|this: &mut State, cx| {                let credentials_provider = this.credentials_provider.clone();                let api_url = Self::api_url(cx);                this.api_key_state.handle_url_change(                    api_url,                    |this| &mut this.api_key_state,                    credentials_provider,                    cx,                );                cx.notify();            })            .detach();            State {                api_key_state: ApiKeyState::new(Self::api_url(cx), (*API_KEY_ENV_VAR).clone()),                credentials_provider,            }        });         Self { http_client, state }    }     fn create_language_model(        &self,        model: opencode::Model,        subscription: OpenCodeSubscription,    ) -> Arc<dyn LanguageModel> {        let id_str = format!("{}/{}", subscription.id_prefix(), model.id());        Arc::new(OpenCodeLanguageModel {            id: LanguageModelId::from(id_str),            model,            subscription,            state: self.state.clone(),            http_client: self.http_client.clone(),            request_limiter: RateLimiter::new(4),        })    }     pub fn settings(cx: &App) -> &OpenCodeSettings {        &crate::AllLanguageModelSettings::get_global(cx).opencode    }     fn subscription_enabled(subscription: OpenCodeSubscription, cx: &App) -> bool {        let settings = Self::settings(cx);        match subscription {            OpenCodeSubscription::Zen => settings.show_zen_models,            OpenCodeSubscription::Go => settings.show_go_models,            OpenCodeSubscription::Free => settings.show_free_models,        }    }     fn api_url(cx: &App) -> SharedString {        let api_url = &Self::settings(cx).api_url;        if api_url.is_empty() {            OPENCODE_API_URL.into()        } else {            SharedString::new(api_url.as_str())        }    }} impl LanguageModelProviderState for OpenCodeLanguageModelProvider {    type ObservableEntity = State;     fn observable_entity(&self) -> Option<Entity<Self::ObservableEntity>> {        Some(self.state.clone())    }} impl LanguageModelProvider for OpenCodeLanguageModelProvider {    fn id(&self) -> LanguageModelProviderId {        PROVIDER_ID    }     fn name(&self) -> LanguageModelProviderName {        PROVIDER_NAME    }     fn icon(&self) -> IconOrSvg {        IconOrSvg::Icon(IconName::AiOpenCode)    }     fn default_model(&self, cx: &App) -> Option<Arc<dyn LanguageModel>> {        if Self::subscription_enabled(OpenCodeSubscription::Go, cx) {            // If both Go and Zen are enabled, prefer Go since it's not pay-as-you-go            Some(                self.create_language_model(opencode::Model::default_go(), OpenCodeSubscription::Go),            )        } else if Self::subscription_enabled(OpenCodeSubscription::Zen, cx) {            Some(self.create_language_model(opencode::Model::default(), OpenCodeSubscription::Zen))        } else if Self::subscription_enabled(OpenCodeSubscription::Free, cx) {            Some(                self.create_language_model(                    opencode::Model::default_free(),                    OpenCodeSubscription::Free,                ),            )        } else {            None        }    }     fn default_fast_model(&self, cx: &App) -> Option<Arc<dyn LanguageModel>> {        if Self::subscription_enabled(OpenCodeSubscription::Go, cx) {            // If both Go and Zen are enabled, prefer Go since it's not pay-as-you-go            Some(self.create_language_model(                opencode::Model::default_go_fast(),                OpenCodeSubscription::Go,            ))        } else if Self::subscription_enabled(OpenCodeSubscription::Zen, cx) {            Some(                self.create_language_model(                    opencode::Model::default_fast(),                    OpenCodeSubscription::Zen,                ),            )        } else if Self::subscription_enabled(OpenCodeSubscription::Free, cx) {            Some(self.create_language_model(                opencode::Model::default_free_fast(),                OpenCodeSubscription::Free,            ))        } else {            None        }    }     fn provided_models(&self, cx: &App) -> Vec<Arc<dyn LanguageModel>> {        let mut models: BTreeMap<String, (opencode::Model, OpenCodeSubscription)> =            BTreeMap::default();        let settings = Self::settings(cx);         for model in opencode::Model::iter() {            if matches!(model, opencode::Model::Custom { .. }) {                continue;            }            for &subscription in model.available_subscriptions() {                if Self::subscription_enabled(subscription, cx) {                    let key = format!("{}/{}", subscription.id_prefix(), model.id());                    models.insert(key, (model.clone(), subscription));                }            }        }         for model in &settings.available_models {            let protocol = match model.protocol.as_str() {                "anthropic" => ApiProtocol::Anthropic,                "openai_responses" => ApiProtocol::OpenAiResponses,                "openai_chat" => ApiProtocol::OpenAiChat,                "google" => ApiProtocol::Google,                _ => ApiProtocol::OpenAiChat, // default fallback            };            let subscription = match model.subscription {                Some(settings::OpenCodeModelSubscription::Go) => OpenCodeSubscription::Go,                Some(settings::OpenCodeModelSubscription::Free) => OpenCodeSubscription::Free,                Some(settings::OpenCodeModelSubscription::Zen) | None => OpenCodeSubscription::Zen,            };            if !Self::subscription_enabled(subscription, cx) {                continue;            }            let custom_model = opencode::Model::Custom {                name: model.name.clone(),                display_name: model.display_name.clone(),                max_tokens: model.max_tokens,                max_output_tokens: model.max_output_tokens,                protocol,                reasoning_effort_levels: model.reasoning_effort_levels.clone(),                custom_model_api_url: model.custom_model_api_url.clone(),                interleaved_reasoning: model.interleaved_reasoning,            };            let key = format!("{}/{}", subscription.id_prefix(), model.name);            models.insert(key, (custom_model, subscription));        }         models            .into_values()            .map(|(model, subscription)| self.create_language_model(model, subscription))            .collect()    }     fn is_authenticated(&self, cx: &App) -> bool {        self.state.read(cx).is_authenticated()    }     fn authenticate(&self, cx: &mut App) -> Task<Result<(), AuthenticateError>> {        self.state.update(cx, |state, cx| state.authenticate(cx))    }     fn configuration_view(        &self,        _target_agent: language_model::ConfigurationViewTargetAgent,        window: &mut Window,        cx: &mut App,    ) -> AnyView {        cx.new(|cx| ConfigurationView::new(self.state.clone(), window, cx))            .into()    }     fn reset_credentials(&self, cx: &mut App) -> Task<Result<()>> {        self.state            .update(cx, |state, cx| state.set_api_key(None, cx))    }} pub struct OpenCodeLanguageModel {    id: LanguageModelId,    model: opencode::Model,    subscription: OpenCodeSubscription,    state: Entity<State>,    http_client: Arc<dyn HttpClient>,    request_limiter: RateLimiter,} struct InjectHeaderClient {    inner: Arc<dyn HttpClient>,    name: http::HeaderName,    value: http::HeaderValue,} impl HttpClient for InjectHeaderClient {    fn user_agent(&self) -> Option<&http::HeaderValue> {        self.inner.user_agent()    }    fn proxy(&self) -> Option<&http_client::Url> {        self.inner.proxy()    }    fn send(        &self,        mut req: http::Request<AsyncBody>,    ) -> futures::future::BoxFuture<'static, anyhow::Result<http::Response<AsyncBody>>> {        req.headers_mut()            .insert(self.name.clone(), self.value.clone());        self.inner.send(req)    }} impl OpenCodeLanguageModel {    fn base_api_url(&self, cx: &AsyncApp) -> SharedString {        // Custom models can override the API URL        if let opencode::Model::Custom {            custom_model_api_url: Some(url),            ..        } = &self.model        {            if !url.is_empty() {                return url.clone().into();            }        }         // Combine base URL with subscription path suffix        let base = self            .state            .read_with(cx, |_, cx| OpenCodeLanguageModelProvider::api_url(cx));         let suffix = self.subscription.api_path_suffix();        let base_str = base.as_ref().trim_end_matches('/');        format!("{}{}", base_str, suffix).into()    }     fn api_key(&self, cx: &AsyncApp) -> Option<Arc<str>> {        self.state.read_with(cx, |state, cx| {            let api_url = OpenCodeLanguageModelProvider::api_url(cx);            state.api_key_state.key(&api_url)        })    }     fn stream_anthropic(        &self,        request: anthropic::Request,        http_client: Arc<dyn HttpClient>,        cx: &AsyncApp,    ) -> BoxFuture<        'static,        Result<            futures::stream::BoxStream<                'static,                Result<anthropic::Event, anthropic::AnthropicError>,            >,            LanguageModelCompletionError,        >,    > {        // Anthropic crate appends /v1/messages to api_url        let api_url = self.base_api_url(cx);        let api_key = self.api_key(cx);         let future = self.request_limiter.stream(async move {            let Some(api_key) = api_key else {                return Err(LanguageModelCompletionError::NoApiKey {                    provider: PROVIDER_NAME,                });            };            let request = anthropic::stream_completion(                http_client.as_ref(),                &api_url,                &api_key,                request,                None,            );            let response = request.await?;            Ok(response)        });         async move { Ok(future.await?.boxed()) }.boxed()    }     fn stream_openai_chat(        &self,        request: open_ai::Request,        http_client: Arc<dyn HttpClient>,        cx: &AsyncApp,    ) -> BoxFuture<        'static,        Result<futures::stream::BoxStream<'static, Result<open_ai::ResponseStreamEvent>>>,    > {        // OpenAI crate appends /chat/completions to api_url, so we pass base + "/v1"        let base_url = self.base_api_url(cx);        let api_url: SharedString = format!("{base_url}/v1").into();        let api_key = self.api_key(cx);        let provider_name = PROVIDER_NAME.0.to_string();         let future = self.request_limiter.stream(async move {            let Some(api_key) = api_key else {                return Err(LanguageModelCompletionError::NoApiKey {                    provider: PROVIDER_NAME,                });            };            let request = open_ai::stream_completion(                http_client.as_ref(),                &provider_name,                &api_url,                &api_key,                request,            );            let response = request.await?;            Ok(response)        });         async move { Ok(future.await?.boxed()) }.boxed()    }     fn stream_openai_response(        &self,        request: open_ai::responses::Request,        http_client: Arc<dyn HttpClient>,        cx: &AsyncApp,    ) -> BoxFuture<        'static,        Result<futures::stream::BoxStream<'static, Result<open_ai::responses::StreamEvent>>>,    > {        // Responses crate appends /responses to api_url, so we pass base + "/v1"        let base_url = self.base_api_url(cx);        let api_url: SharedString = format!("{base_url}/v1").into();        let api_key = self.api_key(cx);        let provider_name = PROVIDER_NAME.0.to_string();         let future = self.request_limiter.stream(async move {            let Some(api_key) = api_key else {                return Err(LanguageModelCompletionError::NoApiKey {                    provider: PROVIDER_NAME,                });            };            let request = open_ai::responses::stream_response(                http_client.as_ref(),                &provider_name,                &api_url,                &api_key,                request,                vec![],            );            let response = request.await?;            Ok(response)        });         async move { Ok(future.await?.boxed()) }.boxed()    }     fn stream_google(        &self,        request: google_ai::GenerateContentRequest,        http_client: Arc<dyn HttpClient>,        cx: &AsyncApp,    ) -> BoxFuture<        'static,        Result<futures::stream::BoxStream<'static, Result<google_ai::GenerateContentResponse>>>,    > {        let api_url = self.base_api_url(cx);

Lines 1–500 of 946.