1use anyhow::{Result, anyhow};
2use collections::BTreeMap;
3use futures::{FutureExt, StreamExt, future, future::BoxFuture};
4use gpui::{AnyView, App, AsyncApp, Context, Entity, Task, Window};
5use http_client::HttpClient;
6use language_model::{
7 AuthenticateError, LanguageModel, LanguageModelCompletionError, LanguageModelCompletionEvent,
8 LanguageModelId, LanguageModelName, LanguageModelProvider, LanguageModelProviderId,
9 LanguageModelProviderName, LanguageModelProviderState, LanguageModelRequest,
10 LanguageModelToolChoice, LanguageModelToolSchemaFormat, RateLimiter, Role,
11};
12use open_ai::ResponseStreamEvent;
13pub use settings::XaiAvailableModel as AvailableModel;
14use settings::{Settings, SettingsStore};
15use std::sync::{Arc, LazyLock};
16use strum::IntoEnumIterator;
17use ui::{ElevationIndex, List, Tooltip, prelude::*};
18use ui_input::SingleLineInput;
19use util::{ResultExt, truncate_and_trailoff};
20use x_ai::{Model, XAI_API_URL};
21use zed_env_vars::{EnvVar, env_var};
22
23use crate::{api_key::ApiKeyState, ui::InstructionListItem};
24
25const PROVIDER_ID: LanguageModelProviderId = LanguageModelProviderId::new("x_ai");
26const PROVIDER_NAME: LanguageModelProviderName = LanguageModelProviderName::new("xAI");
27
28const API_KEY_ENV_VAR_NAME: &str = "XAI_API_KEY";
29static API_KEY_ENV_VAR: LazyLock<EnvVar> = env_var!(API_KEY_ENV_VAR_NAME);
30
31#[derive(Default, Clone, Debug, PartialEq)]
32pub struct XAiSettings {
33 pub api_url: String,
34 pub available_models: Vec<AvailableModel>,
35}
36
37pub struct XAiLanguageModelProvider {
38 http_client: Arc<dyn HttpClient>,
39 state: Entity<State>,
40}
41
42pub struct State {
43 api_key_state: ApiKeyState,
44}
45
46impl State {
47 const fn is_authenticated(&self) -> bool {
48 self.api_key_state.has_key()
49 }
50
51 fn set_api_key(&mut self, api_key: Option<String>, cx: &mut Context<Self>) -> Task<Result<()>> {
52 let api_url = XAiLanguageModelProvider::api_url(cx);
53 self.api_key_state
54 .store(api_url, api_key, |this| &mut this.api_key_state, cx)
55 }
56
57 fn authenticate(&mut self, cx: &mut Context<Self>) -> Task<Result<(), AuthenticateError>> {
58 let api_url = XAiLanguageModelProvider::api_url(cx);
59 self.api_key_state.load_if_needed(
60 api_url,
61 &API_KEY_ENV_VAR,
62 |this| &mut this.api_key_state,
63 cx,
64 )
65 }
66}
67
68impl XAiLanguageModelProvider {
69 pub fn new(http_client: Arc<dyn HttpClient>, cx: &mut App) -> Self {
70 let state = cx.new(|cx| {
71 cx.observe_global::<SettingsStore>(|this: &mut State, cx| {
72 let api_url = Self::api_url(cx);
73 this.api_key_state.handle_url_change(
74 api_url,
75 &API_KEY_ENV_VAR,
76 |this| &mut this.api_key_state,
77 cx,
78 );
79 cx.notify();
80 })
81 .detach();
82 State {
83 api_key_state: ApiKeyState::new(Self::api_url(cx)),
84 }
85 });
86
87 Self { http_client, state }
88 }
89
90 fn create_language_model(&self, model: x_ai::Model) -> Arc<dyn LanguageModel> {
91 Arc::new(XAiLanguageModel {
92 id: LanguageModelId::from(model.id().to_string()),
93 model,
94 state: self.state.clone(),
95 http_client: self.http_client.clone(),
96 request_limiter: RateLimiter::new(4),
97 })
98 }
99
100 fn settings(cx: &App) -> &XAiSettings {
101 &crate::AllLanguageModelSettings::get_global(cx).x_ai
102 }
103
104 fn api_url(cx: &App) -> SharedString {
105 let api_url = &Self::settings(cx).api_url;
106 if api_url.is_empty() {
107 XAI_API_URL.into()
108 } else {
109 SharedString::new(api_url.as_str())
110 }
111 }
112}
113
114impl LanguageModelProviderState for XAiLanguageModelProvider {
115 type ObservableEntity = State;
116
117 fn observable_entity(&self) -> Option<Entity<Self::ObservableEntity>> {
118 Some(self.state.clone())
119 }
120}
121
122impl LanguageModelProvider for XAiLanguageModelProvider {
123 fn id(&self) -> LanguageModelProviderId {
124 PROVIDER_ID
125 }
126
127 fn name(&self) -> LanguageModelProviderName {
128 PROVIDER_NAME
129 }
130
131 fn icon(&self) -> IconName {
132 IconName::AiXAi
133 }
134
135 fn default_model(&self, _cx: &App) -> Option<Arc<dyn LanguageModel>> {
136 Some(self.create_language_model(x_ai::Model::default()))
137 }
138
139 fn default_fast_model(&self, _cx: &App) -> Option<Arc<dyn LanguageModel>> {
140 Some(self.create_language_model(x_ai::Model::default_fast()))
141 }
142
143 fn provided_models(&self, cx: &App) -> Vec<Arc<dyn LanguageModel>> {
144 let mut models = BTreeMap::default();
145
146 for model in x_ai::Model::iter() {
147 if !matches!(model, x_ai::Model::Custom { .. }) {
148 models.insert(model.id().to_string(), model);
149 }
150 }
151
152 for model in &Self::settings(cx).available_models {
153 models.insert(
154 model.name.clone(),
155 x_ai::Model::Custom {
156 name: model.name.clone(),
157 display_name: model.display_name.clone(),
158 max_tokens: model.max_tokens,
159 max_output_tokens: model.max_output_tokens,
160 max_completion_tokens: model.max_completion_tokens,
161 supports_images: model.supports_images,
162 supports_tools: model.supports_tools,
163 parallel_tool_calls: model.parallel_tool_calls,
164 },
165 );
166 }
167
168 models
169 .into_values()
170 .map(|model| self.create_language_model(model))
171 .collect()
172 }
173
174 fn is_authenticated(&self, cx: &App) -> bool {
175 self.state.read(cx).is_authenticated()
176 }
177
178 fn authenticate(&self, cx: &mut App) -> Task<Result<(), AuthenticateError>> {
179 self.state.update(cx, |state, cx| state.authenticate(cx))
180 }
181
182 fn configuration_view(
183 &self,
184 _target_agent: language_model::ConfigurationViewTargetAgent,
185 window: &mut Window,
186 cx: &mut App,
187 ) -> AnyView {
188 cx.new(|cx| ConfigurationView::new(self.state.clone(), window, cx))
189 .into()
190 }
191
192 fn reset_credentials(&self, cx: &mut App) -> Task<Result<()>> {
193 self.state
194 .update(cx, |state, cx| state.set_api_key(None, cx))
195 }
196}
197
198pub struct XAiLanguageModel {
199 id: LanguageModelId,
200 model: x_ai::Model,
201 state: Entity<State>,
202 http_client: Arc<dyn HttpClient>,
203 request_limiter: RateLimiter,
204}
205
206impl XAiLanguageModel {
207 fn stream_completion(
208 &self,
209 request: open_ai::Request,
210 cx: &AsyncApp,
211 ) -> BoxFuture<'static, Result<futures::stream::BoxStream<'static, Result<ResponseStreamEvent>>>>
212 {
213 let http_client = self.http_client.clone();
214
215 let Ok((api_key, api_url)) = self.state.read_with(cx, |state, cx| {
216 let api_url = XAiLanguageModelProvider::api_url(cx);
217 (state.api_key_state.key(&api_url), api_url)
218 }) else {
219 return future::ready(Err(anyhow!("App state dropped"))).boxed();
220 };
221
222 let future = self.request_limiter.stream(async move {
223 let Some(api_key) = api_key else {
224 return Err(LanguageModelCompletionError::NoApiKey {
225 provider: PROVIDER_NAME,
226 });
227 };
228 let request =
229 open_ai::stream_completion(http_client.as_ref(), &api_url, &api_key, request);
230 let response = request.await?;
231 Ok(response)
232 });
233
234 async move { Ok(future.await?.boxed()) }.boxed()
235 }
236}
237
238impl LanguageModel for XAiLanguageModel {
239 fn id(&self) -> LanguageModelId {
240 self.id.clone()
241 }
242
243 fn name(&self) -> LanguageModelName {
244 LanguageModelName::from(self.model.display_name().to_string())
245 }
246
247 fn provider_id(&self) -> LanguageModelProviderId {
248 PROVIDER_ID
249 }
250
251 fn provider_name(&self) -> LanguageModelProviderName {
252 PROVIDER_NAME
253 }
254
255 fn supports_tools(&self) -> bool {
256 self.model.supports_tool()
257 }
258
259 fn supports_images(&self) -> bool {
260 self.model.supports_images()
261 }
262
263 fn supports_tool_choice(&self, choice: LanguageModelToolChoice) -> bool {
264 match choice {
265 LanguageModelToolChoice::Auto
266 | LanguageModelToolChoice::Any
267 | LanguageModelToolChoice::None => true,
268 }
269 }
270 fn tool_input_format(&self) -> LanguageModelToolSchemaFormat {
271 let model_id = self.model.id().trim().to_lowercase();
272 if model_id.eq(x_ai::Model::Grok4.id()) || model_id.eq(x_ai::Model::GrokCodeFast1.id()) {
273 LanguageModelToolSchemaFormat::JsonSchemaSubset
274 } else {
275 LanguageModelToolSchemaFormat::JsonSchema
276 }
277 }
278
279 fn telemetry_id(&self) -> String {
280 format!("x_ai/{}", self.model.id())
281 }
282
283 fn max_token_count(&self) -> u64 {
284 self.model.max_token_count()
285 }
286
287 fn max_output_tokens(&self) -> Option<u64> {
288 self.model.max_output_tokens()
289 }
290
291 fn count_tokens(
292 &self,
293 request: LanguageModelRequest,
294 cx: &App,
295 ) -> BoxFuture<'static, Result<u64>> {
296 count_xai_tokens(request, self.model.clone(), cx)
297 }
298
299 fn stream_completion(
300 &self,
301 request: LanguageModelRequest,
302 cx: &AsyncApp,
303 ) -> BoxFuture<
304 'static,
305 Result<
306 futures::stream::BoxStream<
307 'static,
308 Result<LanguageModelCompletionEvent, LanguageModelCompletionError>,
309 >,
310 LanguageModelCompletionError,
311 >,
312 > {
313 let request = crate::provider::open_ai::into_open_ai(
314 request,
315 self.model.id(),
316 self.model.supports_parallel_tool_calls(),
317 self.model.supports_prompt_cache_key(),
318 self.max_output_tokens(),
319 None,
320 );
321 let completions = self.stream_completion(request, cx);
322 async move {
323 let mapper = crate::provider::open_ai::OpenAiEventMapper::new();
324 Ok(mapper.map_stream(completions.await?).boxed())
325 }
326 .boxed()
327 }
328}
329
330pub fn count_xai_tokens(
331 request: LanguageModelRequest,
332 model: Model,
333 cx: &App,
334) -> BoxFuture<'static, Result<u64>> {
335 cx.background_spawn(async move {
336 let messages = request
337 .messages
338 .into_iter()
339 .map(|message| tiktoken_rs::ChatCompletionRequestMessage {
340 role: match message.role {
341 Role::User => "user".into(),
342 Role::Assistant => "assistant".into(),
343 Role::System => "system".into(),
344 },
345 content: Some(message.string_contents()),
346 name: None,
347 function_call: None,
348 })
349 .collect::<Vec<_>>();
350
351 let model_name = if model.max_token_count() >= 100_000 {
352 "gpt-4o"
353 } else {
354 "gpt-4"
355 };
356 tiktoken_rs::num_tokens_from_messages(model_name, &messages).map(|tokens| tokens as u64)
357 })
358 .boxed()
359}
360
361struct ConfigurationView {
362 api_key_editor: Entity<SingleLineInput>,
363 state: Entity<State>,
364 load_credentials_task: Option<Task<()>>,
365}
366
367impl ConfigurationView {
368 fn new(state: Entity<State>, window: &mut Window, cx: &mut Context<Self>) -> Self {
369 let api_key_editor = cx.new(|cx| {
370 SingleLineInput::new(
371 window,
372 cx,
373 "xai-0000000000000000000000000000000000000000000000000",
374 )
375 .label("API key")
376 });
377
378 cx.observe(&state, |_, _, cx| {
379 cx.notify();
380 })
381 .detach();
382
383 let load_credentials_task = Some(cx.spawn_in(window, {
384 let state = state.clone();
385 async move |this, cx| {
386 if let Some(task) = state
387 .update(cx, |state, cx| state.authenticate(cx))
388 .log_err()
389 {
390 // We don't log an error, because "not signed in" is also an error.
391 let _ = task.await;
392 }
393 this.update(cx, |this, cx| {
394 this.load_credentials_task = None;
395 cx.notify();
396 })
397 .log_err();
398 }
399 }));
400
401 Self {
402 api_key_editor,
403 state,
404 load_credentials_task,
405 }
406 }
407
408 fn save_api_key(&mut self, _: &menu::Confirm, window: &mut Window, cx: &mut Context<Self>) {
409 let api_key = self.api_key_editor.read(cx).text(cx).trim().to_string();
410 if api_key.is_empty() {
411 return;
412 }
413
414 // url changes can cause the editor to be displayed again
415 self.api_key_editor
416 .update(cx, |editor, cx| editor.set_text("", window, cx));
417
418 let state = self.state.clone();
419 cx.spawn_in(window, async move |_, cx| {
420 state
421 .update(cx, |state, cx| state.set_api_key(Some(api_key), cx))?
422 .await
423 })
424 .detach_and_log_err(cx);
425 }
426
427 fn reset_api_key(&mut self, window: &mut Window, cx: &mut Context<Self>) {
428 self.api_key_editor
429 .update(cx, |input, cx| input.set_text("", window, cx));
430
431 let state = self.state.clone();
432 cx.spawn_in(window, async move |_, cx| {
433 state
434 .update(cx, |state, cx| state.set_api_key(None, cx))?
435 .await
436 })
437 .detach_and_log_err(cx);
438 }
439
440 fn should_render_editor(&self, cx: &mut Context<Self>) -> bool {
441 !self.state.read(cx).is_authenticated()
442 }
443}
444
445impl Render for ConfigurationView {
446 fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
447 let env_var_set = self.state.read(cx).api_key_state.is_from_env_var();
448
449 let api_key_section = if self.should_render_editor(cx) {
450 v_flex()
451 .on_action(cx.listener(Self::save_api_key))
452 .child(Label::new("To use Zed's agent with xAI, you need to add an API key. Follow these steps:"))
453 .child(
454 List::new()
455 .child(InstructionListItem::new(
456 "Create one by visiting",
457 Some("xAI console"),
458 Some("https://console.x.ai/team/default/api-keys"),
459 ))
460 .child(InstructionListItem::text_only(
461 "Paste your API key below and hit enter to start using the agent",
462 )),
463 )
464 .child(self.api_key_editor.clone())
465 .child(
466 Label::new(format!(
467 "You can also assign the {API_KEY_ENV_VAR_NAME} environment variable and restart Zed."
468 ))
469 .size(LabelSize::Small)
470 .color(Color::Muted),
471 )
472 .child(
473 Label::new("Note that xAI is a custom OpenAI-compatible provider.")
474 .size(LabelSize::Small)
475 .color(Color::Muted),
476 )
477 .into_any()
478 } else {
479 h_flex()
480 .mt_1()
481 .p_1()
482 .justify_between()
483 .rounded_md()
484 .border_1()
485 .border_color(cx.theme().colors().border)
486 .bg(cx.theme().colors().background)
487 .child(
488 h_flex()
489 .gap_1()
490 .child(Icon::new(IconName::Check).color(Color::Success))
491 .child(Label::new(if env_var_set {
492 format!("API key set in {API_KEY_ENV_VAR_NAME} environment variable")
493 } else {
494 let api_url = XAiLanguageModelProvider::api_url(cx);
495 if api_url == XAI_API_URL {
496 "API key configured".to_string()
497 } else {
498 format!("API key configured for {}", truncate_and_trailoff(&api_url, 32))
499 }
500 })),
501 )
502 .child(
503 Button::new("reset-api-key", "Reset API Key")
504 .label_size(LabelSize::Small)
505 .icon(IconName::Undo)
506 .icon_size(IconSize::Small)
507 .icon_position(IconPosition::Start)
508 .layer(ElevationIndex::ModalSurface)
509 .when(env_var_set, |this| {
510 this.tooltip(Tooltip::text(format!("To reset your API key, unset the {API_KEY_ENV_VAR_NAME} environment variable.")))
511 })
512 .on_click(cx.listener(|this, _, window, cx| this.reset_api_key(window, cx))),
513 )
514 .into_any()
515 };
516
517 if self.load_credentials_task.is_some() {
518 div().child(Label::new("Loading credentials…")).into_any()
519 } else {
520 v_flex().size_full().child(api_key_section).into_any()
521 }
522 }
523}