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