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