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