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