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 tools: request
477 .tools
478 .into_iter()
479 .map(|tool| open_ai::ToolDefinition::Function {
480 function: open_ai::FunctionDefinition {
481 name: tool.name,
482 description: Some(tool.description),
483 parameters: Some(tool.input_schema),
484 },
485 })
486 .collect(),
487 tool_choice: request.tool_choice.map(|choice| match choice {
488 LanguageModelToolChoice::Auto => open_ai::ToolChoice::Auto,
489 LanguageModelToolChoice::Any => open_ai::ToolChoice::Required,
490 LanguageModelToolChoice::None => open_ai::ToolChoice::None,
491 }),
492 }
493}
494
495fn add_message_content_part(
496 new_part: open_ai::MessagePart,
497 role: Role,
498 messages: &mut Vec<open_ai::RequestMessage>,
499) {
500 match (role, messages.last_mut()) {
501 (Role::User, Some(open_ai::RequestMessage::User { content }))
502 | (
503 Role::Assistant,
504 Some(open_ai::RequestMessage::Assistant {
505 content: Some(content),
506 ..
507 }),
508 )
509 | (Role::System, Some(open_ai::RequestMessage::System { content, .. })) => {
510 content.push_part(new_part);
511 }
512 _ => {
513 messages.push(match role {
514 Role::User => open_ai::RequestMessage::User {
515 content: open_ai::MessageContent::from(vec![new_part]),
516 },
517 Role::Assistant => open_ai::RequestMessage::Assistant {
518 content: Some(open_ai::MessageContent::from(vec![new_part])),
519 tool_calls: Vec::new(),
520 },
521 Role::System => open_ai::RequestMessage::System {
522 content: open_ai::MessageContent::from(vec![new_part]),
523 },
524 });
525 }
526 }
527}
528
529pub struct OpenAiEventMapper {
530 tool_calls_by_index: HashMap<usize, RawToolCall>,
531}
532
533impl OpenAiEventMapper {
534 pub fn new() -> Self {
535 Self {
536 tool_calls_by_index: HashMap::default(),
537 }
538 }
539
540 pub fn map_stream(
541 mut self,
542 events: Pin<Box<dyn Send + Stream<Item = Result<ResponseStreamEvent>>>>,
543 ) -> impl Stream<Item = Result<LanguageModelCompletionEvent, LanguageModelCompletionError>>
544 {
545 events.flat_map(move |event| {
546 futures::stream::iter(match event {
547 Ok(event) => self.map_event(event),
548 Err(error) => vec![Err(LanguageModelCompletionError::from(anyhow!(error)))],
549 })
550 })
551 }
552
553 pub fn map_event(
554 &mut self,
555 event: ResponseStreamEvent,
556 ) -> Vec<Result<LanguageModelCompletionEvent, LanguageModelCompletionError>> {
557 let mut events = Vec::new();
558 if let Some(usage) = event.usage {
559 events.push(Ok(LanguageModelCompletionEvent::UsageUpdate(TokenUsage {
560 input_tokens: usage.prompt_tokens,
561 output_tokens: usage.completion_tokens,
562 cache_creation_input_tokens: 0,
563 cache_read_input_tokens: 0,
564 })));
565 }
566
567 let Some(choice) = event.choices.first() else {
568 return events;
569 };
570
571 if let Some(content) = choice.delta.content.clone() {
572 events.push(Ok(LanguageModelCompletionEvent::Text(content)));
573 }
574
575 if let Some(tool_calls) = choice.delta.tool_calls.as_ref() {
576 for tool_call in tool_calls {
577 let entry = self.tool_calls_by_index.entry(tool_call.index).or_default();
578
579 if let Some(tool_id) = tool_call.id.clone() {
580 entry.id = tool_id;
581 }
582
583 if let Some(function) = tool_call.function.as_ref() {
584 if let Some(name) = function.name.clone() {
585 entry.name = name;
586 }
587
588 if let Some(arguments) = function.arguments.clone() {
589 entry.arguments.push_str(&arguments);
590 }
591 }
592 }
593 }
594
595 match choice.finish_reason.as_deref() {
596 Some("stop") => {
597 events.push(Ok(LanguageModelCompletionEvent::Stop(StopReason::EndTurn)));
598 }
599 Some("tool_calls") => {
600 events.extend(self.tool_calls_by_index.drain().map(|(_, tool_call)| {
601 match serde_json::Value::from_str(&tool_call.arguments) {
602 Ok(input) => Ok(LanguageModelCompletionEvent::ToolUse(
603 LanguageModelToolUse {
604 id: tool_call.id.clone().into(),
605 name: tool_call.name.as_str().into(),
606 is_input_complete: true,
607 input,
608 raw_input: tool_call.arguments.clone(),
609 },
610 )),
611 Err(error) => Ok(LanguageModelCompletionEvent::ToolUseJsonParseError {
612 id: tool_call.id.into(),
613 tool_name: tool_call.name.into(),
614 raw_input: tool_call.arguments.clone().into(),
615 json_parse_error: error.to_string(),
616 }),
617 }
618 }));
619
620 events.push(Ok(LanguageModelCompletionEvent::Stop(StopReason::ToolUse)));
621 }
622 Some(stop_reason) => {
623 log::error!("Unexpected OpenAI stop_reason: {stop_reason:?}",);
624 events.push(Ok(LanguageModelCompletionEvent::Stop(StopReason::EndTurn)));
625 }
626 None => {}
627 }
628
629 events
630 }
631}
632
633#[derive(Default)]
634struct RawToolCall {
635 id: String,
636 name: String,
637 arguments: String,
638}
639
640pub(crate) fn collect_tiktoken_messages(
641 request: LanguageModelRequest,
642) -> Vec<tiktoken_rs::ChatCompletionRequestMessage> {
643 request
644 .messages
645 .into_iter()
646 .map(|message| tiktoken_rs::ChatCompletionRequestMessage {
647 role: match message.role {
648 Role::User => "user".into(),
649 Role::Assistant => "assistant".into(),
650 Role::System => "system".into(),
651 },
652 content: Some(message.string_contents()),
653 name: None,
654 function_call: None,
655 })
656 .collect::<Vec<_>>()
657}
658
659pub fn count_open_ai_tokens(
660 request: LanguageModelRequest,
661 model: Model,
662 cx: &App,
663) -> BoxFuture<'static, Result<u64>> {
664 cx.background_spawn(async move {
665 let messages = collect_tiktoken_messages(request);
666
667 match model {
668 Model::Custom { max_tokens, .. } => {
669 let model = if max_tokens >= 100_000 {
670 // If the max tokens is 100k or more, it is likely the o200k_base tokenizer from gpt4o
671 "gpt-4o"
672 } else {
673 // Otherwise fallback to gpt-4, since only cl100k_base and o200k_base are
674 // supported with this tiktoken method
675 "gpt-4"
676 };
677 tiktoken_rs::num_tokens_from_messages(model, &messages)
678 }
679 // Currently supported by tiktoken_rs
680 // Sometimes tiktoken-rs is behind on model support. If that is the case, make a new branch
681 // arm with an override. We enumerate all supported models here so that we can check if new
682 // models are supported yet or not.
683 Model::ThreePointFiveTurbo
684 | Model::Four
685 | Model::FourTurbo
686 | Model::FourOmni
687 | Model::FourOmniMini
688 | Model::FourPointOne
689 | Model::FourPointOneMini
690 | Model::FourPointOneNano
691 | Model::O1
692 | Model::O3
693 | Model::O3Mini
694 | Model::O4Mini => tiktoken_rs::num_tokens_from_messages(model.id(), &messages),
695 // GPT-5 models don't have tiktoken support yet; fall back on gpt-4o tokenizer
696 Model::Five | Model::FiveMini | Model::FiveNano => {
697 tiktoken_rs::num_tokens_from_messages("gpt-4o", &messages)
698 }
699 }
700 .map(|tokens| tokens as u64)
701 })
702 .boxed()
703}
704
705struct ConfigurationView {
706 api_key_editor: Entity<SingleLineInput>,
707 state: gpui::Entity<State>,
708 load_credentials_task: Option<Task<()>>,
709}
710
711impl ConfigurationView {
712 fn new(state: gpui::Entity<State>, window: &mut Window, cx: &mut Context<Self>) -> Self {
713 let api_key_editor = cx.new(|cx| {
714 SingleLineInput::new(
715 window,
716 cx,
717 "sk-000000000000000000000000000000000000000000000000",
718 )
719 });
720
721 cx.observe(&state, |_, _, cx| {
722 cx.notify();
723 })
724 .detach();
725
726 let load_credentials_task = Some(cx.spawn_in(window, {
727 let state = state.clone();
728 async move |this, cx| {
729 if let Some(task) = state
730 .update(cx, |state, cx| state.authenticate(cx))
731 .log_err()
732 {
733 // We don't log an error, because "not signed in" is also an error.
734 let _ = task.await;
735 }
736 this.update(cx, |this, cx| {
737 this.load_credentials_task = None;
738 cx.notify();
739 })
740 .log_err();
741 }
742 }));
743
744 Self {
745 api_key_editor,
746 state,
747 load_credentials_task,
748 }
749 }
750
751 fn save_api_key(&mut self, _: &menu::Confirm, window: &mut Window, cx: &mut Context<Self>) {
752 let api_key = self
753 .api_key_editor
754 .read(cx)
755 .editor()
756 .read(cx)
757 .text(cx)
758 .trim()
759 .to_string();
760
761 // Don't proceed if no API key is provided and we're not authenticated
762 if api_key.is_empty() && !self.state.read(cx).is_authenticated() {
763 return;
764 }
765
766 let state = self.state.clone();
767 cx.spawn_in(window, async move |_, cx| {
768 state
769 .update(cx, |state, cx| state.set_api_key(api_key, cx))?
770 .await
771 })
772 .detach_and_log_err(cx);
773
774 cx.notify();
775 }
776
777 fn reset_api_key(&mut self, window: &mut Window, cx: &mut Context<Self>) {
778 self.api_key_editor.update(cx, |input, cx| {
779 input.editor.update(cx, |editor, cx| {
780 editor.set_text("", window, cx);
781 });
782 });
783
784 let state = self.state.clone();
785 cx.spawn_in(window, async move |_, cx| {
786 state.update(cx, |state, cx| state.reset_api_key(cx))?.await
787 })
788 .detach_and_log_err(cx);
789
790 cx.notify();
791 }
792
793 fn should_render_editor(&self, cx: &mut Context<Self>) -> bool {
794 !self.state.read(cx).is_authenticated()
795 }
796}
797
798impl Render for ConfigurationView {
799 fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
800 let env_var_set = self.state.read(cx).api_key_from_env;
801
802 let api_key_section = if self.should_render_editor(cx) {
803 v_flex()
804 .on_action(cx.listener(Self::save_api_key))
805 .child(Label::new("To use Zed's agent with OpenAI, you need to add an API key. Follow these steps:"))
806 .child(
807 List::new()
808 .child(InstructionListItem::new(
809 "Create one by visiting",
810 Some("OpenAI's console"),
811 Some("https://platform.openai.com/api-keys"),
812 ))
813 .child(InstructionListItem::text_only(
814 "Ensure your OpenAI account has credits",
815 ))
816 .child(InstructionListItem::text_only(
817 "Paste your API key below and hit enter to start using the assistant",
818 )),
819 )
820 .child(self.api_key_editor.clone())
821 .child(
822 Label::new(
823 format!("You can also assign the {OPENAI_API_KEY_VAR} environment variable and restart Zed."),
824 )
825 .size(LabelSize::Small).color(Color::Muted),
826 )
827 .child(
828 Label::new(
829 "Note that having a subscription for another service like GitHub Copilot won't work.",
830 )
831 .size(LabelSize::Small).color(Color::Muted),
832 )
833 .into_any()
834 } else {
835 h_flex()
836 .mt_1()
837 .p_1()
838 .justify_between()
839 .rounded_md()
840 .border_1()
841 .border_color(cx.theme().colors().border)
842 .bg(cx.theme().colors().background)
843 .child(
844 h_flex()
845 .gap_1()
846 .child(Icon::new(IconName::Check).color(Color::Success))
847 .child(Label::new(if env_var_set {
848 format!("API key set in {OPENAI_API_KEY_VAR} environment variable.")
849 } else {
850 "API key configured.".to_string()
851 })),
852 )
853 .child(
854 Button::new("reset-api-key", "Reset API Key")
855 .label_size(LabelSize::Small)
856 .icon(IconName::Undo)
857 .icon_size(IconSize::Small)
858 .icon_position(IconPosition::Start)
859 .layer(ElevationIndex::ModalSurface)
860 .when(env_var_set, |this| {
861 this.tooltip(Tooltip::text(format!("To reset your API key, unset the {OPENAI_API_KEY_VAR} environment variable.")))
862 })
863 .on_click(cx.listener(|this, _, window, cx| this.reset_api_key(window, cx))),
864 )
865 .into_any()
866 };
867
868 let compatible_api_section = h_flex()
869 .mt_1p5()
870 .gap_0p5()
871 .flex_wrap()
872 .when(self.should_render_editor(cx), |this| {
873 this.pt_1p5()
874 .border_t_1()
875 .border_color(cx.theme().colors().border_variant)
876 })
877 .child(
878 h_flex()
879 .gap_2()
880 .child(
881 Icon::new(IconName::Info)
882 .size(IconSize::XSmall)
883 .color(Color::Muted),
884 )
885 .child(Label::new("Zed also supports OpenAI-compatible models.")),
886 )
887 .child(
888 Button::new("docs", "Learn More")
889 .icon(IconName::ArrowUpRight)
890 .icon_size(IconSize::Small)
891 .icon_color(Color::Muted)
892 .on_click(move |_, _window, cx| {
893 cx.open_url("https://zed.dev/docs/ai/llm-providers#openai-api-compatible")
894 }),
895 );
896
897 if self.load_credentials_task.is_some() {
898 div().child(Label::new("Loading credentials…")).into_any()
899 } else {
900 v_flex()
901 .size_full()
902 .child(api_key_section)
903 .child(compatible_api_section)
904 .into_any()
905 }
906 }
907}
908
909#[cfg(test)]
910mod tests {
911 use gpui::TestAppContext;
912 use language_model::LanguageModelRequestMessage;
913
914 use super::*;
915
916 #[gpui::test]
917 fn tiktoken_rs_support(cx: &TestAppContext) {
918 let request = LanguageModelRequest {
919 thread_id: None,
920 prompt_id: None,
921 intent: None,
922 mode: None,
923 messages: vec![LanguageModelRequestMessage {
924 role: Role::User,
925 content: vec![MessageContent::Text("message".into())],
926 cache: false,
927 }],
928 tools: vec![],
929 tool_choice: None,
930 stop: vec![],
931 temperature: None,
932 thinking_allowed: true,
933 };
934
935 // Validate that all models are supported by tiktoken-rs
936 for model in Model::iter() {
937 let count = cx
938 .executor()
939 .block(count_open_ai_tokens(
940 request.clone(),
941 model,
942 &cx.app.borrow(),
943 ))
944 .unwrap();
945 assert!(count > 0);
946 }
947 }
948}