vercel.rs

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