1use anyhow::{anyhow, Result};
2use collections::BTreeMap;
3use editor::{Editor, EditorElement, EditorStyle};
4use futures::{future::BoxFuture, FutureExt, StreamExt};
5use google_ai::stream_generate_content;
6use gpui::{
7 AnyView, AppContext, AsyncAppContext, FontStyle, Subscription, Task, TextStyle, View,
8 WhiteSpace,
9};
10use http_client::HttpClient;
11use settings::{Settings, SettingsStore};
12use std::{sync::Arc, time::Duration};
13use strum::IntoEnumIterator;
14use theme::ThemeSettings;
15use ui::prelude::*;
16use util::ResultExt;
17
18use crate::{
19 settings::AllLanguageModelSettings, LanguageModel, LanguageModelId, LanguageModelName,
20 LanguageModelProvider, LanguageModelProviderId, LanguageModelProviderName,
21 LanguageModelProviderState, LanguageModelRequest,
22};
23
24const PROVIDER_ID: &str = "google";
25const PROVIDER_NAME: &str = "Google AI";
26
27#[derive(Default, Clone, Debug, PartialEq)]
28pub struct GoogleSettings {
29 pub api_url: String,
30 pub low_speed_timeout: Option<Duration>,
31 pub available_models: Vec<google_ai::Model>,
32}
33
34pub struct GoogleLanguageModelProvider {
35 http_client: Arc<dyn HttpClient>,
36 state: gpui::Model<State>,
37}
38
39struct State {
40 api_key: Option<String>,
41 _subscription: Subscription,
42}
43
44impl GoogleLanguageModelProvider {
45 pub fn new(http_client: Arc<dyn HttpClient>, cx: &mut AppContext) -> Self {
46 let state = cx.new_model(|cx| State {
47 api_key: None,
48 _subscription: cx.observe_global::<SettingsStore>(|_, cx| {
49 cx.notify();
50 }),
51 });
52
53 Self { http_client, state }
54 }
55}
56
57impl LanguageModelProviderState for GoogleLanguageModelProvider {
58 fn subscribe<T: 'static>(&self, cx: &mut gpui::ModelContext<T>) -> Option<gpui::Subscription> {
59 Some(cx.observe(&self.state, |_, _, cx| {
60 cx.notify();
61 }))
62 }
63}
64
65impl LanguageModelProvider for GoogleLanguageModelProvider {
66 fn id(&self) -> LanguageModelProviderId {
67 LanguageModelProviderId(PROVIDER_ID.into())
68 }
69
70 fn name(&self) -> LanguageModelProviderName {
71 LanguageModelProviderName(PROVIDER_NAME.into())
72 }
73
74 fn provided_models(&self, cx: &AppContext) -> Vec<Arc<dyn LanguageModel>> {
75 let mut models = BTreeMap::default();
76
77 // Add base models from google_ai::Model::iter()
78 for model in google_ai::Model::iter() {
79 if !matches!(model, google_ai::Model::Custom { .. }) {
80 models.insert(model.id().to_string(), model);
81 }
82 }
83
84 // Override with available models from settings
85 for model in &AllLanguageModelSettings::get_global(cx)
86 .google
87 .available_models
88 {
89 models.insert(model.id().to_string(), model.clone());
90 }
91
92 models
93 .into_values()
94 .map(|model| {
95 Arc::new(GoogleLanguageModel {
96 id: LanguageModelId::from(model.id().to_string()),
97 model,
98 state: self.state.clone(),
99 http_client: self.http_client.clone(),
100 }) as Arc<dyn LanguageModel>
101 })
102 .collect()
103 }
104
105 fn is_authenticated(&self, cx: &AppContext) -> bool {
106 self.state.read(cx).api_key.is_some()
107 }
108
109 fn authenticate(&self, cx: &AppContext) -> Task<Result<()>> {
110 if self.is_authenticated(cx) {
111 Task::ready(Ok(()))
112 } else {
113 let api_url = AllLanguageModelSettings::get_global(cx)
114 .google
115 .api_url
116 .clone();
117 let state = self.state.clone();
118 cx.spawn(|mut cx| async move {
119 let api_key = if let Ok(api_key) = std::env::var("GOOGLE_AI_API_KEY") {
120 api_key
121 } else {
122 let (_, api_key) = cx
123 .update(|cx| cx.read_credentials(&api_url))?
124 .await?
125 .ok_or_else(|| anyhow!("credentials not found"))?;
126 String::from_utf8(api_key)?
127 };
128
129 state.update(&mut cx, |this, cx| {
130 this.api_key = Some(api_key);
131 cx.notify();
132 })
133 })
134 }
135 }
136
137 fn authentication_prompt(&self, cx: &mut WindowContext) -> AnyView {
138 cx.new_view(|cx| AuthenticationPrompt::new(self.state.clone(), cx))
139 .into()
140 }
141
142 fn reset_credentials(&self, cx: &AppContext) -> Task<Result<()>> {
143 let state = self.state.clone();
144 let delete_credentials =
145 cx.delete_credentials(&AllLanguageModelSettings::get_global(cx).google.api_url);
146 cx.spawn(|mut cx| async move {
147 delete_credentials.await.log_err();
148 state.update(&mut cx, |this, cx| {
149 this.api_key = None;
150 cx.notify();
151 })
152 })
153 }
154}
155
156pub struct GoogleLanguageModel {
157 id: LanguageModelId,
158 model: google_ai::Model,
159 state: gpui::Model<State>,
160 http_client: Arc<dyn HttpClient>,
161}
162
163impl LanguageModel for GoogleLanguageModel {
164 fn id(&self) -> LanguageModelId {
165 self.id.clone()
166 }
167
168 fn name(&self) -> LanguageModelName {
169 LanguageModelName::from(self.model.display_name().to_string())
170 }
171
172 fn provider_id(&self) -> LanguageModelProviderId {
173 LanguageModelProviderId(PROVIDER_ID.into())
174 }
175
176 fn provider_name(&self) -> LanguageModelProviderName {
177 LanguageModelProviderName(PROVIDER_NAME.into())
178 }
179
180 fn telemetry_id(&self) -> String {
181 format!("google/{}", self.model.id())
182 }
183
184 fn max_token_count(&self) -> usize {
185 self.model.max_token_count()
186 }
187
188 fn count_tokens(
189 &self,
190 request: LanguageModelRequest,
191 cx: &AppContext,
192 ) -> BoxFuture<'static, Result<usize>> {
193 let request = request.into_google(self.model.id().to_string());
194 let http_client = self.http_client.clone();
195 let api_key = self.state.read(cx).api_key.clone();
196 let api_url = AllLanguageModelSettings::get_global(cx)
197 .google
198 .api_url
199 .clone();
200
201 async move {
202 let api_key = api_key.ok_or_else(|| anyhow!("missing api key"))?;
203 let response = google_ai::count_tokens(
204 http_client.as_ref(),
205 &api_url,
206 &api_key,
207 google_ai::CountTokensRequest {
208 contents: request.contents,
209 },
210 )
211 .await?;
212 Ok(response.total_tokens)
213 }
214 .boxed()
215 }
216
217 fn stream_completion(
218 &self,
219 request: LanguageModelRequest,
220 cx: &AsyncAppContext,
221 ) -> BoxFuture<'static, Result<futures::stream::BoxStream<'static, Result<String>>>> {
222 let request = request.into_google(self.model.id().to_string());
223
224 let http_client = self.http_client.clone();
225 let Ok((api_key, api_url)) = cx.read_model(&self.state, |state, cx| {
226 let settings = &AllLanguageModelSettings::get_global(cx).google;
227 (state.api_key.clone(), settings.api_url.clone())
228 }) else {
229 return futures::future::ready(Err(anyhow!("App state dropped"))).boxed();
230 };
231
232 async move {
233 let api_key = api_key.ok_or_else(|| anyhow!("missing api key"))?;
234 let response =
235 stream_generate_content(http_client.as_ref(), &api_url, &api_key, request);
236 let events = response.await?;
237 Ok(google_ai::extract_text_from_events(events).boxed())
238 }
239 .boxed()
240 }
241}
242
243struct AuthenticationPrompt {
244 api_key: View<Editor>,
245 state: gpui::Model<State>,
246}
247
248impl AuthenticationPrompt {
249 fn new(state: gpui::Model<State>, cx: &mut WindowContext) -> Self {
250 Self {
251 api_key: cx.new_view(|cx| {
252 let mut editor = Editor::single_line(cx);
253 editor.set_placeholder_text("AIzaSy...", cx);
254 editor
255 }),
256 state,
257 }
258 }
259
260 fn save_api_key(&mut self, _: &menu::Confirm, cx: &mut ViewContext<Self>) {
261 let api_key = self.api_key.read(cx).text(cx);
262 if api_key.is_empty() {
263 return;
264 }
265
266 let settings = &AllLanguageModelSettings::get_global(cx).google;
267 let write_credentials =
268 cx.write_credentials(&settings.api_url, "Bearer", api_key.as_bytes());
269 let state = self.state.clone();
270 cx.spawn(|_, mut cx| async move {
271 write_credentials.await?;
272 state.update(&mut cx, |this, cx| {
273 this.api_key = Some(api_key);
274 cx.notify();
275 })
276 })
277 .detach_and_log_err(cx);
278 }
279
280 fn render_api_key_editor(&self, cx: &mut ViewContext<Self>) -> impl IntoElement {
281 let settings = ThemeSettings::get_global(cx);
282 let text_style = TextStyle {
283 color: cx.theme().colors().text,
284 font_family: settings.ui_font.family.clone(),
285 font_features: settings.ui_font.features.clone(),
286 font_fallbacks: settings.ui_font.fallbacks.clone(),
287 font_size: rems(0.875).into(),
288 font_weight: settings.ui_font.weight,
289 font_style: FontStyle::Normal,
290 line_height: relative(1.3),
291 background_color: None,
292 underline: None,
293 strikethrough: None,
294 white_space: WhiteSpace::Normal,
295 };
296 EditorElement::new(
297 &self.api_key,
298 EditorStyle {
299 background: cx.theme().colors().editor_background,
300 local_player: cx.theme().players().local(),
301 text: text_style,
302 ..Default::default()
303 },
304 )
305 }
306}
307
308impl Render for AuthenticationPrompt {
309 fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
310 const INSTRUCTIONS: [&str; 4] = [
311 "To use the Google AI assistant, you need to add your Google AI API key.",
312 "You can create an API key at: https://makersuite.google.com/app/apikey",
313 "",
314 "Paste your Google AI API key below and hit enter to use the assistant:",
315 ];
316
317 v_flex()
318 .p_4()
319 .size_full()
320 .on_action(cx.listener(Self::save_api_key))
321 .children(
322 INSTRUCTIONS.map(|instruction| Label::new(instruction).size(LabelSize::Small)),
323 )
324 .child(
325 h_flex()
326 .w_full()
327 .my_2()
328 .px_2()
329 .py_1()
330 .bg(cx.theme().colors().editor_background)
331 .rounded_md()
332 .child(self.render_api_key_editor(cx)),
333 )
334 .child(
335 Label::new(
336 "You can also assign the GOOGLE_AI_API_KEY environment variable and restart Zed.",
337 )
338 .size(LabelSize::Small),
339 )
340 .child(
341 h_flex()
342 .gap_2()
343 .child(Label::new("Click on").size(LabelSize::Small))
344 .child(Icon::new(IconName::ZedAssistant).size(IconSize::XSmall))
345 .child(
346 Label::new("in the status bar to close this panel.").size(LabelSize::Small),
347 ),
348 )
349 .into_any()
350 }
351}