open_ai.rs

  1use anyhow::{Context as _, Result, anyhow};
  2use collections::{BTreeMap, HashMap};
  3use credentials_provider::CredentialsProvider;
  4
  5use futures::Stream;
  6use futures::{FutureExt, StreamExt, future::BoxFuture};
  7use gpui::{AnyView, App, AsyncApp, Context, Entity, Subscription, Task, Window};
  8use http_client::HttpClient;
  9use language_model::{
 10    AuthenticateError, LanguageModel, LanguageModelCompletionError, LanguageModelCompletionEvent,
 11    LanguageModelId, LanguageModelName, LanguageModelProvider, LanguageModelProviderId,
 12    LanguageModelProviderName, LanguageModelProviderState, LanguageModelRequest,
 13    LanguageModelToolChoice, LanguageModelToolResultContent, LanguageModelToolUse, MessageContent,
 14    RateLimiter, Role, StopReason, TokenUsage,
 15};
 16use menu;
 17use open_ai::{ImageUrl, Model, ResponseStreamEvent, stream_completion};
 18use schemars::JsonSchema;
 19use serde::{Deserialize, Serialize};
 20use settings::{Settings, SettingsStore};
 21use std::pin::Pin;
 22use std::str::FromStr as _;
 23use std::sync::Arc;
 24use strum::IntoEnumIterator;
 25
 26use ui::{ElevationIndex, List, Tooltip, prelude::*};
 27use ui_input::SingleLineInput;
 28use util::ResultExt;
 29
 30use crate::{AllLanguageModelSettings, ui::InstructionListItem};
 31
 32const PROVIDER_ID: LanguageModelProviderId = language_model::OPEN_AI_PROVIDER_ID;
 33const PROVIDER_NAME: LanguageModelProviderName = language_model::OPEN_AI_PROVIDER_NAME;
 34
 35#[derive(Default, Clone, Debug, PartialEq)]
 36pub struct OpenAiSettings {
 37    pub api_url: String,
 38    pub available_models: Vec<AvailableModel>,
 39}
 40
 41#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
 42pub struct AvailableModel {
 43    pub name: String,
 44    pub display_name: Option<String>,
 45    pub max_tokens: u64,
 46    pub max_output_tokens: Option<u64>,
 47    pub max_completion_tokens: Option<u64>,
 48}
 49
 50pub struct OpenAiLanguageModelProvider {
 51    http_client: Arc<dyn HttpClient>,
 52    state: gpui::Entity<State>,
 53}
 54
 55pub struct State {
 56    api_key: Option<String>,
 57    api_key_from_env: bool,
 58    _subscription: Subscription,
 59}
 60
 61const OPENAI_API_KEY_VAR: &str = "OPENAI_API_KEY";
 62
 63impl State {
 64    //
 65    fn is_authenticated(&self) -> bool {
 66        self.api_key.is_some()
 67    }
 68
 69    fn reset_api_key(&self, cx: &mut Context<Self>) -> Task<Result<()>> {
 70        let credentials_provider = <dyn CredentialsProvider>::global(cx);
 71        let api_url = AllLanguageModelSettings::get_global(cx)
 72            .openai
 73            .api_url
 74            .clone();
 75        cx.spawn(async move |this, cx| {
 76            credentials_provider
 77                .delete_credentials(&api_url, &cx)
 78                .await
 79                .log_err();
 80            this.update(cx, |this, cx| {
 81                this.api_key = None;
 82                this.api_key_from_env = false;
 83                cx.notify();
 84            })
 85        })
 86    }
 87
 88    fn set_api_key(&mut self, api_key: String, cx: &mut Context<Self>) -> Task<Result<()>> {
 89        let credentials_provider = <dyn CredentialsProvider>::global(cx);
 90        let api_url = AllLanguageModelSettings::get_global(cx)
 91            .openai
 92            .api_url
 93            .clone();
 94        cx.spawn(async move |this, cx| {
 95            credentials_provider
 96                .write_credentials(&api_url, "Bearer", api_key.as_bytes(), &cx)
 97                .await
 98                .log_err();
 99            this.update(cx, |this, cx| {
100                this.api_key = Some(api_key);
101                cx.notify();
102            })
103        })
104    }
105
106    fn authenticate(&self, cx: &mut Context<Self>) -> Task<Result<(), AuthenticateError>> {
107        if self.is_authenticated() {
108            return Task::ready(Ok(()));
109        }
110
111        let credentials_provider = <dyn CredentialsProvider>::global(cx);
112        let api_url = AllLanguageModelSettings::get_global(cx)
113            .openai
114            .api_url
115            .clone();
116        cx.spawn(async move |this, cx| {
117            let (api_key, from_env) = if let Ok(api_key) = std::env::var(OPENAI_API_KEY_VAR) {
118                (api_key, true)
119            } else {
120                let (_, api_key) = credentials_provider
121                    .read_credentials(&api_url, &cx)
122                    .await?
123                    .ok_or(AuthenticateError::CredentialsNotFound)?;
124                (
125                    String::from_utf8(api_key).context("invalid {PROVIDER_NAME} API key")?,
126                    false,
127                )
128            };
129            this.update(cx, |this, cx| {
130                this.api_key = Some(api_key);
131                this.api_key_from_env = from_env;
132                cx.notify();
133            })?;
134
135            Ok(())
136        })
137    }
138}
139
140impl OpenAiLanguageModelProvider {
141    pub fn new(http_client: Arc<dyn HttpClient>, cx: &mut App) -> Self {
142        let state = cx.new(|cx| State {
143            api_key: None,
144            api_key_from_env: false,
145            _subscription: cx.observe_global::<SettingsStore>(|_this: &mut State, cx| {
146                cx.notify();
147            }),
148        });
149
150        Self { http_client, state }
151    }
152
153    fn create_language_model(&self, model: open_ai::Model) -> Arc<dyn LanguageModel> {
154        Arc::new(OpenAiLanguageModel {
155            id: LanguageModelId::from(model.id().to_string()),
156            model,
157            state: self.state.clone(),
158            http_client: self.http_client.clone(),
159            request_limiter: RateLimiter::new(4),
160        })
161    }
162}
163
164impl LanguageModelProviderState for OpenAiLanguageModelProvider {
165    type ObservableEntity = State;
166
167    fn observable_entity(&self) -> Option<gpui::Entity<Self::ObservableEntity>> {
168        Some(self.state.clone())
169    }
170}
171
172impl LanguageModelProvider for OpenAiLanguageModelProvider {
173    fn id(&self) -> LanguageModelProviderId {
174        PROVIDER_ID
175    }
176
177    fn name(&self) -> LanguageModelProviderName {
178        PROVIDER_NAME
179    }
180
181    fn icon(&self) -> IconName {
182        IconName::AiOpenAi
183    }
184
185    fn default_model(&self, _cx: &App) -> Option<Arc<dyn LanguageModel>> {
186        Some(self.create_language_model(open_ai::Model::default()))
187    }
188
189    fn default_fast_model(&self, _cx: &App) -> Option<Arc<dyn LanguageModel>> {
190        Some(self.create_language_model(open_ai::Model::default_fast()))
191    }
192
193    fn provided_models(&self, cx: &App) -> Vec<Arc<dyn LanguageModel>> {
194        let mut models = BTreeMap::default();
195
196        // Add base models from open_ai::Model::iter()
197        for model in open_ai::Model::iter() {
198            if !matches!(model, open_ai::Model::Custom { .. }) {
199                models.insert(model.id().to_string(), model);
200            }
201        }
202
203        // Override with available models from settings
204        for model in &AllLanguageModelSettings::get_global(cx)
205            .openai
206            .available_models
207        {
208            models.insert(
209                model.name.clone(),
210                open_ai::Model::Custom {
211                    name: model.name.clone(),
212                    display_name: model.display_name.clone(),
213                    max_tokens: model.max_tokens,
214                    max_output_tokens: model.max_output_tokens,
215                    max_completion_tokens: model.max_completion_tokens,
216                },
217            );
218        }
219
220        models
221            .into_values()
222            .map(|model| self.create_language_model(model))
223            .collect()
224    }
225
226    fn is_authenticated(&self, cx: &App) -> bool {
227        self.state.read(cx).is_authenticated()
228    }
229
230    fn authenticate(&self, cx: &mut App) -> Task<Result<(), AuthenticateError>> {
231        self.state.update(cx, |state, cx| state.authenticate(cx))
232    }
233
234    fn configuration_view(&self, window: &mut Window, cx: &mut App) -> AnyView {
235        cx.new(|cx| ConfigurationView::new(self.state.clone(), window, cx))
236            .into()
237    }
238
239    fn reset_credentials(&self, cx: &mut App) -> Task<Result<()>> {
240        self.state.update(cx, |state, cx| state.reset_api_key(cx))
241    }
242}
243
244pub struct OpenAiLanguageModel {
245    id: LanguageModelId,
246    model: open_ai::Model,
247    state: gpui::Entity<State>,
248    http_client: Arc<dyn HttpClient>,
249    request_limiter: RateLimiter,
250}
251
252impl OpenAiLanguageModel {
253    fn stream_completion(
254        &self,
255        request: open_ai::Request,
256        cx: &AsyncApp,
257    ) -> BoxFuture<'static, Result<futures::stream::BoxStream<'static, Result<ResponseStreamEvent>>>>
258    {
259        let http_client = self.http_client.clone();
260        let Ok((api_key, api_url)) = cx.read_entity(&self.state, |state, cx| {
261            let settings = &AllLanguageModelSettings::get_global(cx).openai;
262            (state.api_key.clone(), settings.api_url.clone())
263        }) else {
264            return futures::future::ready(Err(anyhow!("App state dropped"))).boxed();
265        };
266
267        let future = self.request_limiter.stream(async move {
268            let Some(api_key) = api_key else {
269                return Err(LanguageModelCompletionError::NoApiKey {
270                    provider: PROVIDER_NAME,
271                });
272            };
273            let request = stream_completion(http_client.as_ref(), &api_url, &api_key, request);
274            let response = request.await?;
275            Ok(response)
276        });
277
278        async move { Ok(future.await?.boxed()) }.boxed()
279    }
280}
281
282impl LanguageModel for OpenAiLanguageModel {
283    fn id(&self) -> LanguageModelId {
284        self.id.clone()
285    }
286
287    fn name(&self) -> LanguageModelName {
288        LanguageModelName::from(self.model.display_name().to_string())
289    }
290
291    fn provider_id(&self) -> LanguageModelProviderId {
292        PROVIDER_ID
293    }
294
295    fn provider_name(&self) -> LanguageModelProviderName {
296        PROVIDER_NAME
297    }
298
299    fn supports_tools(&self) -> bool {
300        true
301    }
302
303    fn supports_images(&self) -> bool {
304        use open_ai::Model;
305        match &self.model {
306            Model::FourOmni
307            | Model::FourOmniMini
308            | Model::FourPointOne
309            | Model::FourPointOneMini
310            | Model::FourPointOneNano
311            | Model::Five
312            | Model::FiveMini
313            | Model::FiveNano
314            | Model::O1
315            | Model::O3
316            | Model::O4Mini => true,
317            Model::ThreePointFiveTurbo
318            | Model::Four
319            | Model::FourTurbo
320            | Model::O3Mini
321            | Model::Custom { .. } => false,
322        }
323    }
324
325    fn supports_tool_choice(&self, choice: LanguageModelToolChoice) -> bool {
326        match choice {
327            LanguageModelToolChoice::Auto => true,
328            LanguageModelToolChoice::Any => true,
329            LanguageModelToolChoice::None => true,
330        }
331    }
332
333    fn telemetry_id(&self) -> String {
334        format!("openai/{}", self.model.id())
335    }
336
337    fn max_token_count(&self) -> u64 {
338        self.model.max_token_count()
339    }
340
341    fn max_output_tokens(&self) -> Option<u64> {
342        self.model.max_output_tokens()
343    }
344
345    fn count_tokens(
346        &self,
347        request: LanguageModelRequest,
348        cx: &App,
349    ) -> BoxFuture<'static, Result<u64>> {
350        count_open_ai_tokens(request, self.model.clone(), cx)
351    }
352
353    fn stream_completion(
354        &self,
355        request: LanguageModelRequest,
356        cx: &AsyncApp,
357    ) -> BoxFuture<
358        'static,
359        Result<
360            futures::stream::BoxStream<
361                'static,
362                Result<LanguageModelCompletionEvent, LanguageModelCompletionError>,
363            >,
364            LanguageModelCompletionError,
365        >,
366    > {
367        let request = into_open_ai(
368            request,
369            self.model.id(),
370            self.model.supports_parallel_tool_calls(),
371            self.max_output_tokens(),
372        );
373        let completions = self.stream_completion(request, cx);
374        async move {
375            let mapper = OpenAiEventMapper::new();
376            Ok(mapper.map_stream(completions.await?).boxed())
377        }
378        .boxed()
379    }
380}
381
382pub fn into_open_ai(
383    request: LanguageModelRequest,
384    model_id: &str,
385    supports_parallel_tool_calls: bool,
386    max_output_tokens: Option<u64>,
387) -> open_ai::Request {
388    let stream = !model_id.starts_with("o1-");
389
390    let mut messages = Vec::new();
391    for message in request.messages {
392        for content in message.content {
393            match content {
394                MessageContent::Text(text) | MessageContent::Thinking { text, .. } => {
395                    add_message_content_part(
396                        open_ai::MessagePart::Text { text: text },
397                        message.role,
398                        &mut messages,
399                    )
400                }
401                MessageContent::RedactedThinking(_) => {}
402                MessageContent::Image(image) => {
403                    add_message_content_part(
404                        open_ai::MessagePart::Image {
405                            image_url: ImageUrl {
406                                url: image.to_base64_url(),
407                                detail: None,
408                            },
409                        },
410                        message.role,
411                        &mut messages,
412                    );
413                }
414                MessageContent::ToolUse(tool_use) => {
415                    let tool_call = open_ai::ToolCall {
416                        id: tool_use.id.to_string(),
417                        content: open_ai::ToolCallContent::Function {
418                            function: open_ai::FunctionContent {
419                                name: tool_use.name.to_string(),
420                                arguments: serde_json::to_string(&tool_use.input)
421                                    .unwrap_or_default(),
422                            },
423                        },
424                    };
425
426                    if let Some(open_ai::RequestMessage::Assistant { tool_calls, .. }) =
427                        messages.last_mut()
428                    {
429                        tool_calls.push(tool_call);
430                    } else {
431                        messages.push(open_ai::RequestMessage::Assistant {
432                            content: None,
433                            tool_calls: vec![tool_call],
434                        });
435                    }
436                }
437                MessageContent::ToolResult(tool_result) => {
438                    let content = match &tool_result.content {
439                        LanguageModelToolResultContent::Text(text) => {
440                            vec![open_ai::MessagePart::Text {
441                                text: text.to_string(),
442                            }]
443                        }
444                        LanguageModelToolResultContent::Image(image) => {
445                            vec![open_ai::MessagePart::Image {
446                                image_url: ImageUrl {
447                                    url: image.to_base64_url(),
448                                    detail: None,
449                                },
450                            }]
451                        }
452                    };
453
454                    messages.push(open_ai::RequestMessage::Tool {
455                        content: content.into(),
456                        tool_call_id: tool_result.tool_use_id.to_string(),
457                    });
458                }
459            }
460        }
461    }
462
463    open_ai::Request {
464        model: model_id.into(),
465        messages,
466        stream,
467        stop: request.stop,
468        temperature: request.temperature.unwrap_or(1.0),
469        max_completion_tokens: max_output_tokens,
470        parallel_tool_calls: if supports_parallel_tool_calls && !request.tools.is_empty() {
471            // Disable parallel tool calls, as the Agent currently expects a maximum of one per turn.
472            Some(false)
473        } else {
474            None
475        },
476        prompt_cache_key: request.thread_id,
477        tools: request
478            .tools
479            .into_iter()
480            .map(|tool| open_ai::ToolDefinition::Function {
481                function: open_ai::FunctionDefinition {
482                    name: tool.name,
483                    description: Some(tool.description),
484                    parameters: Some(tool.input_schema),
485                },
486            })
487            .collect(),
488        tool_choice: request.tool_choice.map(|choice| match choice {
489            LanguageModelToolChoice::Auto => open_ai::ToolChoice::Auto,
490            LanguageModelToolChoice::Any => open_ai::ToolChoice::Required,
491            LanguageModelToolChoice::None => open_ai::ToolChoice::None,
492        }),
493    }
494}
495
496fn add_message_content_part(
497    new_part: open_ai::MessagePart,
498    role: Role,
499    messages: &mut Vec<open_ai::RequestMessage>,
500) {
501    match (role, messages.last_mut()) {
502        (Role::User, Some(open_ai::RequestMessage::User { content }))
503        | (
504            Role::Assistant,
505            Some(open_ai::RequestMessage::Assistant {
506                content: Some(content),
507                ..
508            }),
509        )
510        | (Role::System, Some(open_ai::RequestMessage::System { content, .. })) => {
511            content.push_part(new_part);
512        }
513        _ => {
514            messages.push(match role {
515                Role::User => open_ai::RequestMessage::User {
516                    content: open_ai::MessageContent::from(vec![new_part]),
517                },
518                Role::Assistant => open_ai::RequestMessage::Assistant {
519                    content: Some(open_ai::MessageContent::from(vec![new_part])),
520                    tool_calls: Vec::new(),
521                },
522                Role::System => open_ai::RequestMessage::System {
523                    content: open_ai::MessageContent::from(vec![new_part]),
524                },
525            });
526        }
527    }
528}
529
530pub struct OpenAiEventMapper {
531    tool_calls_by_index: HashMap<usize, RawToolCall>,
532}
533
534impl OpenAiEventMapper {
535    pub fn new() -> Self {
536        Self {
537            tool_calls_by_index: HashMap::default(),
538        }
539    }
540
541    pub fn map_stream(
542        mut self,
543        events: Pin<Box<dyn Send + Stream<Item = Result<ResponseStreamEvent>>>>,
544    ) -> impl Stream<Item = Result<LanguageModelCompletionEvent, LanguageModelCompletionError>>
545    {
546        events.flat_map(move |event| {
547            futures::stream::iter(match event {
548                Ok(event) => self.map_event(event),
549                Err(error) => vec![Err(LanguageModelCompletionError::from(anyhow!(error)))],
550            })
551        })
552    }
553
554    pub fn map_event(
555        &mut self,
556        event: ResponseStreamEvent,
557    ) -> Vec<Result<LanguageModelCompletionEvent, LanguageModelCompletionError>> {
558        let mut events = Vec::new();
559        if let Some(usage) = event.usage {
560            events.push(Ok(LanguageModelCompletionEvent::UsageUpdate(TokenUsage {
561                input_tokens: usage.prompt_tokens,
562                output_tokens: usage.completion_tokens,
563                cache_creation_input_tokens: 0,
564                cache_read_input_tokens: 0,
565            })));
566        }
567
568        let Some(choice) = event.choices.first() else {
569            return events;
570        };
571
572        if let Some(content) = choice.delta.content.clone() {
573            events.push(Ok(LanguageModelCompletionEvent::Text(content)));
574        }
575
576        if let Some(tool_calls) = choice.delta.tool_calls.as_ref() {
577            for tool_call in tool_calls {
578                let entry = self.tool_calls_by_index.entry(tool_call.index).or_default();
579
580                if let Some(tool_id) = tool_call.id.clone() {
581                    entry.id = tool_id;
582                }
583
584                if let Some(function) = tool_call.function.as_ref() {
585                    if let Some(name) = function.name.clone() {
586                        entry.name = name;
587                    }
588
589                    if let Some(arguments) = function.arguments.clone() {
590                        entry.arguments.push_str(&arguments);
591                    }
592                }
593            }
594        }
595
596        match choice.finish_reason.as_deref() {
597            Some("stop") => {
598                events.push(Ok(LanguageModelCompletionEvent::Stop(StopReason::EndTurn)));
599            }
600            Some("tool_calls") => {
601                events.extend(self.tool_calls_by_index.drain().map(|(_, tool_call)| {
602                    match serde_json::Value::from_str(&tool_call.arguments) {
603                        Ok(input) => Ok(LanguageModelCompletionEvent::ToolUse(
604                            LanguageModelToolUse {
605                                id: tool_call.id.clone().into(),
606                                name: tool_call.name.as_str().into(),
607                                is_input_complete: true,
608                                input,
609                                raw_input: tool_call.arguments.clone(),
610                            },
611                        )),
612                        Err(error) => Ok(LanguageModelCompletionEvent::ToolUseJsonParseError {
613                            id: tool_call.id.into(),
614                            tool_name: tool_call.name.into(),
615                            raw_input: tool_call.arguments.clone().into(),
616                            json_parse_error: error.to_string(),
617                        }),
618                    }
619                }));
620
621                events.push(Ok(LanguageModelCompletionEvent::Stop(StopReason::ToolUse)));
622            }
623            Some(stop_reason) => {
624                log::error!("Unexpected OpenAI stop_reason: {stop_reason:?}",);
625                events.push(Ok(LanguageModelCompletionEvent::Stop(StopReason::EndTurn)));
626            }
627            None => {}
628        }
629
630        events
631    }
632}
633
634#[derive(Default)]
635struct RawToolCall {
636    id: String,
637    name: String,
638    arguments: String,
639}
640
641pub(crate) fn collect_tiktoken_messages(
642    request: LanguageModelRequest,
643) -> Vec<tiktoken_rs::ChatCompletionRequestMessage> {
644    request
645        .messages
646        .into_iter()
647        .map(|message| tiktoken_rs::ChatCompletionRequestMessage {
648            role: match message.role {
649                Role::User => "user".into(),
650                Role::Assistant => "assistant".into(),
651                Role::System => "system".into(),
652            },
653            content: Some(message.string_contents()),
654            name: None,
655            function_call: None,
656        })
657        .collect::<Vec<_>>()
658}
659
660pub fn count_open_ai_tokens(
661    request: LanguageModelRequest,
662    model: Model,
663    cx: &App,
664) -> BoxFuture<'static, Result<u64>> {
665    cx.background_spawn(async move {
666        let messages = collect_tiktoken_messages(request);
667
668        match model {
669            Model::Custom { max_tokens, .. } => {
670                let model = if max_tokens >= 100_000 {
671                    // If the max tokens is 100k or more, it is likely the o200k_base tokenizer from gpt4o
672                    "gpt-4o"
673                } else {
674                    // Otherwise fallback to gpt-4, since only cl100k_base and o200k_base are
675                    // supported with this tiktoken method
676                    "gpt-4"
677                };
678                tiktoken_rs::num_tokens_from_messages(model, &messages)
679            }
680            // Currently supported by tiktoken_rs
681            // Sometimes tiktoken-rs is behind on model support. If that is the case, make a new branch
682            // arm with an override. We enumerate all supported models here so that we can check if new
683            // models are supported yet or not.
684            Model::ThreePointFiveTurbo
685            | Model::Four
686            | Model::FourTurbo
687            | Model::FourOmni
688            | Model::FourOmniMini
689            | Model::FourPointOne
690            | Model::FourPointOneMini
691            | Model::FourPointOneNano
692            | Model::O1
693            | Model::O3
694            | Model::O3Mini
695            | Model::O4Mini => tiktoken_rs::num_tokens_from_messages(model.id(), &messages),
696            // GPT-5 models don't have tiktoken support yet; fall back on gpt-4o tokenizer
697            Model::Five | Model::FiveMini | Model::FiveNano => {
698                tiktoken_rs::num_tokens_from_messages("gpt-4o", &messages)
699            }
700        }
701        .map(|tokens| tokens as u64)
702    })
703    .boxed()
704}
705
706struct ConfigurationView {
707    api_key_editor: Entity<SingleLineInput>,
708    state: gpui::Entity<State>,
709    load_credentials_task: Option<Task<()>>,
710}
711
712impl ConfigurationView {
713    fn new(state: gpui::Entity<State>, window: &mut Window, cx: &mut Context<Self>) -> Self {
714        let api_key_editor = cx.new(|cx| {
715            SingleLineInput::new(
716                window,
717                cx,
718                "sk-000000000000000000000000000000000000000000000000",
719            )
720        });
721
722        cx.observe(&state, |_, _, cx| {
723            cx.notify();
724        })
725        .detach();
726
727        let load_credentials_task = Some(cx.spawn_in(window, {
728            let state = state.clone();
729            async move |this, cx| {
730                if let Some(task) = state
731                    .update(cx, |state, cx| state.authenticate(cx))
732                    .log_err()
733                {
734                    // We don't log an error, because "not signed in" is also an error.
735                    let _ = task.await;
736                }
737                this.update(cx, |this, cx| {
738                    this.load_credentials_task = None;
739                    cx.notify();
740                })
741                .log_err();
742            }
743        }));
744
745        Self {
746            api_key_editor,
747            state,
748            load_credentials_task,
749        }
750    }
751
752    fn save_api_key(&mut self, _: &menu::Confirm, window: &mut Window, cx: &mut Context<Self>) {
753        let api_key = self
754            .api_key_editor
755            .read(cx)
756            .editor()
757            .read(cx)
758            .text(cx)
759            .trim()
760            .to_string();
761
762        // Don't proceed if no API key is provided and we're not authenticated
763        if api_key.is_empty() && !self.state.read(cx).is_authenticated() {
764            return;
765        }
766
767        let state = self.state.clone();
768        cx.spawn_in(window, async move |_, cx| {
769            state
770                .update(cx, |state, cx| state.set_api_key(api_key, cx))?
771                .await
772        })
773        .detach_and_log_err(cx);
774
775        cx.notify();
776    }
777
778    fn reset_api_key(&mut self, window: &mut Window, cx: &mut Context<Self>) {
779        self.api_key_editor.update(cx, |input, cx| {
780            input.editor.update(cx, |editor, cx| {
781                editor.set_text("", window, cx);
782            });
783        });
784
785        let state = self.state.clone();
786        cx.spawn_in(window, async move |_, cx| {
787            state.update(cx, |state, cx| state.reset_api_key(cx))?.await
788        })
789        .detach_and_log_err(cx);
790
791        cx.notify();
792    }
793
794    fn should_render_editor(&self, cx: &mut Context<Self>) -> bool {
795        !self.state.read(cx).is_authenticated()
796    }
797}
798
799impl Render for ConfigurationView {
800    fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
801        let env_var_set = self.state.read(cx).api_key_from_env;
802
803        let api_key_section = if self.should_render_editor(cx) {
804            v_flex()
805                .on_action(cx.listener(Self::save_api_key))
806                .child(Label::new("To use Zed's agent with OpenAI, you need to add an API key. Follow these steps:"))
807                .child(
808                    List::new()
809                        .child(InstructionListItem::new(
810                            "Create one by visiting",
811                            Some("OpenAI's console"),
812                            Some("https://platform.openai.com/api-keys"),
813                        ))
814                        .child(InstructionListItem::text_only(
815                            "Ensure your OpenAI account has credits",
816                        ))
817                        .child(InstructionListItem::text_only(
818                            "Paste your API key below and hit enter to start using the assistant",
819                        )),
820                )
821                .child(self.api_key_editor.clone())
822                .child(
823                    Label::new(
824                        format!("You can also assign the {OPENAI_API_KEY_VAR} environment variable and restart Zed."),
825                    )
826                    .size(LabelSize::Small).color(Color::Muted),
827                )
828                .child(
829                    Label::new(
830                        "Note that having a subscription for another service like GitHub Copilot won't work.",
831                    )
832                    .size(LabelSize::Small).color(Color::Muted),
833                )
834                .into_any()
835        } else {
836            h_flex()
837                .mt_1()
838                .p_1()
839                .justify_between()
840                .rounded_md()
841                .border_1()
842                .border_color(cx.theme().colors().border)
843                .bg(cx.theme().colors().background)
844                .child(
845                    h_flex()
846                        .gap_1()
847                        .child(Icon::new(IconName::Check).color(Color::Success))
848                        .child(Label::new(if env_var_set {
849                            format!("API key set in {OPENAI_API_KEY_VAR} environment variable.")
850                        } else {
851                            "API key configured.".to_string()
852                        })),
853                )
854                .child(
855                    Button::new("reset-api-key", "Reset API Key")
856                        .label_size(LabelSize::Small)
857                        .icon(IconName::Undo)
858                        .icon_size(IconSize::Small)
859                        .icon_position(IconPosition::Start)
860                        .layer(ElevationIndex::ModalSurface)
861                        .when(env_var_set, |this| {
862                            this.tooltip(Tooltip::text(format!("To reset your API key, unset the {OPENAI_API_KEY_VAR} environment variable.")))
863                        })
864                        .on_click(cx.listener(|this, _, window, cx| this.reset_api_key(window, cx))),
865                )
866                .into_any()
867        };
868
869        let compatible_api_section = h_flex()
870            .mt_1p5()
871            .gap_0p5()
872            .flex_wrap()
873            .when(self.should_render_editor(cx), |this| {
874                this.pt_1p5()
875                    .border_t_1()
876                    .border_color(cx.theme().colors().border_variant)
877            })
878            .child(
879                h_flex()
880                    .gap_2()
881                    .child(
882                        Icon::new(IconName::Info)
883                            .size(IconSize::XSmall)
884                            .color(Color::Muted),
885                    )
886                    .child(Label::new("Zed also supports OpenAI-compatible models.")),
887            )
888            .child(
889                Button::new("docs", "Learn More")
890                    .icon(IconName::ArrowUpRight)
891                    .icon_size(IconSize::Small)
892                    .icon_color(Color::Muted)
893                    .on_click(move |_, _window, cx| {
894                        cx.open_url("https://zed.dev/docs/ai/llm-providers#openai-api-compatible")
895                    }),
896            );
897
898        if self.load_credentials_task.is_some() {
899            div().child(Label::new("Loading credentials…")).into_any()
900        } else {
901            v_flex()
902                .size_full()
903                .child(api_key_section)
904                .child(compatible_api_section)
905                .into_any()
906        }
907    }
908}
909
910#[cfg(test)]
911mod tests {
912    use gpui::TestAppContext;
913    use language_model::LanguageModelRequestMessage;
914
915    use super::*;
916
917    #[gpui::test]
918    fn tiktoken_rs_support(cx: &TestAppContext) {
919        let request = LanguageModelRequest {
920            thread_id: None,
921            prompt_id: None,
922            intent: None,
923            mode: None,
924            messages: vec![LanguageModelRequestMessage {
925                role: Role::User,
926                content: vec![MessageContent::Text("message".into())],
927                cache: false,
928            }],
929            tools: vec![],
930            tool_choice: None,
931            stop: vec![],
932            temperature: None,
933            thinking_allowed: true,
934        };
935
936        // Validate that all models are supported by tiktoken-rs
937        for model in Model::iter() {
938            let count = cx
939                .executor()
940                .block(count_open_ai_tokens(
941                    request.clone(),
942                    model,
943                    &cx.app.borrow(),
944                ))
945                .unwrap();
946            assert!(count > 0);
947        }
948    }
949}