1use crate::{
2 DebugEvent, EditPredictionFinishedDebugEvent, EditPredictionId, EditPredictionModelInput,
3 EditPredictionStartedDebugEvent, open_ai_response::text_from_response,
4 prediction::EditPredictionResult, zeta1::compute_edits,
5};
6use anyhow::{Context as _, Result};
7use cloud_llm_client::EditPredictionRejectReason;
8use futures::AsyncReadExt as _;
9use gpui::{
10 App, AppContext as _, Entity, Global, SharedString, Task,
11 http_client::{self, AsyncBody, HttpClient, Method},
12};
13use language::{OffsetRangeExt as _, ToOffset, ToPoint as _};
14use language_model::{ApiKeyState, EnvVar, env_var};
15use release_channel::AppVersion;
16use serde::Serialize;
17use std::{mem, ops::Range, path::Path, sync::Arc, time::Instant};
18
19use zeta_prompt::ZetaPromptInput;
20
21const MERCURY_API_URL: &str = "https://api.inceptionlabs.ai/v1/edit/completions";
22const MAX_REWRITE_TOKENS: usize = 150;
23const MAX_CONTEXT_TOKENS: usize = 350;
24
25pub struct Mercury {
26 pub api_token: Entity<ApiKeyState>,
27}
28
29impl Mercury {
30 pub fn new(cx: &mut App) -> Self {
31 Mercury {
32 api_token: mercury_api_token(cx),
33 }
34 }
35
36 pub(crate) fn request_prediction(
37 &self,
38 EditPredictionModelInput {
39 buffer,
40 snapshot,
41 position,
42 events,
43 related_files,
44 debug_tx,
45 ..
46 }: EditPredictionModelInput,
47 cx: &mut App,
48 ) -> Task<Result<Option<EditPredictionResult>>> {
49 self.api_token.update(cx, |key_state, cx| {
50 _ = key_state.load_if_needed(MERCURY_CREDENTIALS_URL, |s| s, cx);
51 });
52 let Some(api_token) = self.api_token.read(cx).key(&MERCURY_CREDENTIALS_URL) else {
53 return Task::ready(Ok(None));
54 };
55 let full_path: Arc<Path> = snapshot
56 .file()
57 .map(|file| file.full_path(cx))
58 .unwrap_or_else(|| "untitled".into())
59 .into();
60
61 let http_client = cx.http_client();
62 let cursor_point = position.to_point(&snapshot);
63 let buffer_snapshotted_at = Instant::now();
64 let active_buffer = buffer.clone();
65
66 let result = cx.background_spawn(async move {
67 let (editable_range, context_range) =
68 crate::cursor_excerpt::editable_and_context_ranges_for_cursor_position(
69 cursor_point,
70 &snapshot,
71 MAX_CONTEXT_TOKENS,
72 MAX_REWRITE_TOKENS,
73 );
74
75 let related_files = crate::filter_redundant_excerpts(
76 related_files,
77 full_path.as_ref(),
78 context_range.start.row..context_range.end.row,
79 );
80
81 let context_offset_range = context_range.to_offset(&snapshot);
82 let context_start_row = context_range.start.row;
83
84 let editable_offset_range = editable_range.to_offset(&snapshot);
85
86 let inputs = zeta_prompt::ZetaPromptInput {
87 events,
88 related_files,
89 cursor_offset_in_excerpt: cursor_point.to_offset(&snapshot)
90 - context_offset_range.start,
91 cursor_path: full_path.clone(),
92 cursor_excerpt: snapshot
93 .text_for_range(context_range)
94 .collect::<String>()
95 .into(),
96 editable_range_in_excerpt: (editable_offset_range.start
97 - context_offset_range.start)
98 ..(editable_offset_range.end - context_offset_range.start),
99 excerpt_start_row: Some(context_start_row),
100 excerpt_ranges: None,
101 preferred_model: None,
102 in_open_source_repo: false,
103 can_collect_data: false,
104 };
105
106 let prompt = build_prompt(&inputs);
107
108 if let Some(debug_tx) = &debug_tx {
109 debug_tx
110 .unbounded_send(DebugEvent::EditPredictionStarted(
111 EditPredictionStartedDebugEvent {
112 buffer: active_buffer.downgrade(),
113 prompt: Some(prompt.clone()),
114 position,
115 },
116 ))
117 .ok();
118 }
119
120 let request_body = open_ai::Request {
121 model: "mercury-coder".into(),
122 messages: vec![open_ai::RequestMessage::User {
123 content: open_ai::MessageContent::Plain(prompt),
124 }],
125 stream: false,
126 max_completion_tokens: None,
127 stop: vec![],
128 temperature: None,
129 tool_choice: None,
130 parallel_tool_calls: None,
131 tools: vec![],
132 prompt_cache_key: None,
133 reasoning_effort: None,
134 };
135
136 let buf = serde_json::to_vec(&request_body)?;
137 let body: AsyncBody = buf.into();
138
139 let request = http_client::Request::builder()
140 .uri(MERCURY_API_URL)
141 .header("Content-Type", "application/json")
142 .header("Authorization", format!("Bearer {}", api_token))
143 .header("Connection", "keep-alive")
144 .method(Method::POST)
145 .body(body)
146 .context("Failed to create request")?;
147
148 let mut response = http_client
149 .send(request)
150 .await
151 .context("Failed to send request")?;
152
153 let mut body: Vec<u8> = Vec::new();
154 response
155 .body_mut()
156 .read_to_end(&mut body)
157 .await
158 .context("Failed to read response body")?;
159
160 let response_received_at = Instant::now();
161 if !response.status().is_success() {
162 anyhow::bail!(
163 "Request failed with status: {:?}\nBody: {}",
164 response.status(),
165 String::from_utf8_lossy(&body),
166 );
167 };
168
169 let mut response: open_ai::Response =
170 serde_json::from_slice(&body).context("Failed to parse response")?;
171
172 let id = mem::take(&mut response.id);
173 let response_str = text_from_response(response).unwrap_or_default();
174
175 if let Some(debug_tx) = &debug_tx {
176 debug_tx
177 .unbounded_send(DebugEvent::EditPredictionFinished(
178 EditPredictionFinishedDebugEvent {
179 buffer: active_buffer.downgrade(),
180 model_output: Some(response_str.clone()),
181 position,
182 },
183 ))
184 .ok();
185 }
186
187 let response_str = response_str.strip_prefix("```\n").unwrap_or(&response_str);
188 let response_str = response_str.strip_suffix("\n```").unwrap_or(&response_str);
189
190 let mut edits = Vec::new();
191 const NO_PREDICTION_OUTPUT: &str = "None";
192
193 if response_str != NO_PREDICTION_OUTPUT {
194 let old_text = snapshot
195 .text_for_range(editable_offset_range.clone())
196 .collect::<String>();
197 edits = compute_edits(
198 old_text,
199 &response_str,
200 editable_offset_range.start,
201 &snapshot,
202 );
203 }
204
205 anyhow::Ok((id, edits, snapshot, response_received_at, inputs))
206 });
207
208 cx.spawn(async move |cx| {
209 let (id, edits, old_snapshot, response_received_at, inputs) =
210 result.await.context("Mercury edit prediction failed")?;
211 anyhow::Ok(Some(
212 EditPredictionResult::new(
213 EditPredictionId(id.into()),
214 &buffer,
215 &old_snapshot,
216 edits.into(),
217 None,
218 buffer_snapshotted_at,
219 response_received_at,
220 inputs,
221 cx,
222 )
223 .await,
224 ))
225 })
226 }
227}
228
229fn build_prompt(inputs: &ZetaPromptInput) -> String {
230 const RECENTLY_VIEWED_SNIPPETS_START: &str = "<|recently_viewed_code_snippets|>\n";
231 const RECENTLY_VIEWED_SNIPPETS_END: &str = "<|/recently_viewed_code_snippets|>\n";
232 const RECENTLY_VIEWED_SNIPPET_START: &str = "<|recently_viewed_code_snippet|>\n";
233 const RECENTLY_VIEWED_SNIPPET_END: &str = "<|/recently_viewed_code_snippet|>\n";
234 const CURRENT_FILE_CONTENT_START: &str = "<|current_file_content|>\n";
235 const CURRENT_FILE_CONTENT_END: &str = "<|/current_file_content|>\n";
236 const CODE_TO_EDIT_START: &str = "<|code_to_edit|>\n";
237 const CODE_TO_EDIT_END: &str = "<|/code_to_edit|>\n";
238 const EDIT_DIFF_HISTORY_START: &str = "<|edit_diff_history|>\n";
239 const EDIT_DIFF_HISTORY_END: &str = "<|/edit_diff_history|>\n";
240 const CURSOR_TAG: &str = "<|cursor|>";
241 const CODE_SNIPPET_FILE_PATH_PREFIX: &str = "code_snippet_file_path: ";
242 const CURRENT_FILE_PATH_PREFIX: &str = "current_file_path: ";
243
244 let mut prompt = String::new();
245
246 push_delimited(
247 &mut prompt,
248 RECENTLY_VIEWED_SNIPPETS_START..RECENTLY_VIEWED_SNIPPETS_END,
249 |prompt| {
250 for related_file in inputs.related_files.iter() {
251 for related_excerpt in &related_file.excerpts {
252 push_delimited(
253 prompt,
254 RECENTLY_VIEWED_SNIPPET_START..RECENTLY_VIEWED_SNIPPET_END,
255 |prompt| {
256 prompt.push_str(CODE_SNIPPET_FILE_PATH_PREFIX);
257 prompt.push_str(related_file.path.to_string_lossy().as_ref());
258 prompt.push('\n');
259 prompt.push_str(related_excerpt.text.as_ref());
260 },
261 );
262 }
263 }
264 },
265 );
266
267 push_delimited(
268 &mut prompt,
269 CURRENT_FILE_CONTENT_START..CURRENT_FILE_CONTENT_END,
270 |prompt| {
271 prompt.push_str(CURRENT_FILE_PATH_PREFIX);
272 prompt.push_str(inputs.cursor_path.as_os_str().to_string_lossy().as_ref());
273 prompt.push('\n');
274
275 prompt.push_str(&inputs.cursor_excerpt[0..inputs.editable_range_in_excerpt.start]);
276 push_delimited(prompt, CODE_TO_EDIT_START..CODE_TO_EDIT_END, |prompt| {
277 prompt.push_str(
278 &inputs.cursor_excerpt
279 [inputs.editable_range_in_excerpt.start..inputs.cursor_offset_in_excerpt],
280 );
281 prompt.push_str(CURSOR_TAG);
282 prompt.push_str(
283 &inputs.cursor_excerpt
284 [inputs.cursor_offset_in_excerpt..inputs.editable_range_in_excerpt.end],
285 );
286 });
287 prompt.push_str(&inputs.cursor_excerpt[inputs.editable_range_in_excerpt.end..]);
288 },
289 );
290
291 push_delimited(
292 &mut prompt,
293 EDIT_DIFF_HISTORY_START..EDIT_DIFF_HISTORY_END,
294 |prompt| {
295 for event in inputs.events.iter() {
296 zeta_prompt::write_event(prompt, &event);
297 }
298 },
299 );
300
301 prompt
302}
303
304fn push_delimited(prompt: &mut String, delimiters: Range<&str>, cb: impl FnOnce(&mut String)) {
305 prompt.push_str(delimiters.start);
306 cb(prompt);
307 prompt.push('\n');
308 prompt.push_str(delimiters.end);
309}
310
311pub const MERCURY_CREDENTIALS_URL: SharedString =
312 SharedString::new_static("https://api.inceptionlabs.ai/v1/edit/completions");
313pub const MERCURY_CREDENTIALS_USERNAME: &str = "mercury-api-token";
314pub static MERCURY_TOKEN_ENV_VAR: std::sync::LazyLock<EnvVar> = env_var!("MERCURY_AI_TOKEN");
315
316struct GlobalMercuryApiKey(Entity<ApiKeyState>);
317
318impl Global for GlobalMercuryApiKey {}
319
320pub fn mercury_api_token(cx: &mut App) -> Entity<ApiKeyState> {
321 if let Some(global) = cx.try_global::<GlobalMercuryApiKey>() {
322 return global.0.clone();
323 }
324 let entity =
325 cx.new(|_| ApiKeyState::new(MERCURY_CREDENTIALS_URL, MERCURY_TOKEN_ENV_VAR.clone()));
326 cx.set_global(GlobalMercuryApiKey(entity.clone()));
327 entity
328}
329
330pub fn load_mercury_api_token(cx: &mut App) -> Task<Result<(), language_model::AuthenticateError>> {
331 mercury_api_token(cx).update(cx, |key_state, cx| {
332 key_state.load_if_needed(MERCURY_CREDENTIALS_URL, |s| s, cx)
333 })
334}
335
336const FEEDBACK_API_URL: &str = "https://api-feedback.inceptionlabs.ai/feedback";
337
338#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
339#[serde(rename_all = "snake_case")]
340enum MercuryUserAction {
341 Accept,
342 Reject,
343 Ignore,
344}
345
346#[derive(Serialize)]
347struct FeedbackRequest {
348 request_id: SharedString,
349 provider_name: &'static str,
350 user_action: MercuryUserAction,
351 provider_version: String,
352}
353
354pub(crate) fn edit_prediction_accepted(
355 prediction_id: EditPredictionId,
356 http_client: Arc<dyn HttpClient>,
357 cx: &App,
358) {
359 send_feedback(prediction_id, MercuryUserAction::Accept, http_client, cx);
360}
361
362pub(crate) fn edit_prediction_rejected(
363 prediction_id: EditPredictionId,
364 was_shown: bool,
365 reason: EditPredictionRejectReason,
366 http_client: Arc<dyn HttpClient>,
367 cx: &App,
368) {
369 if !was_shown {
370 return;
371 }
372 let action = match reason {
373 EditPredictionRejectReason::Rejected => MercuryUserAction::Reject,
374 EditPredictionRejectReason::Discarded => MercuryUserAction::Ignore,
375 _ => return,
376 };
377 send_feedback(prediction_id, action, http_client, cx);
378}
379
380fn send_feedback(
381 prediction_id: EditPredictionId,
382 action: MercuryUserAction,
383 http_client: Arc<dyn HttpClient>,
384 cx: &App,
385) {
386 let request_id = prediction_id.0;
387 let app_version = AppVersion::global(cx);
388 cx.background_spawn(async move {
389 let body = FeedbackRequest {
390 request_id,
391 provider_name: "zed",
392 user_action: action,
393 provider_version: app_version.to_string(),
394 };
395
396 let request = http_client::Request::builder()
397 .uri(FEEDBACK_API_URL)
398 .method(Method::POST)
399 .header("Content-Type", "application/json")
400 .body(AsyncBody::from(serde_json::to_vec(&body)?))?;
401
402 let response = http_client.send(request).await?;
403 if !response.status().is_success() {
404 anyhow::bail!("Feedback API returned status: {}", response.status());
405 }
406
407 log::debug!(
408 "Mercury feedback sent: request_id={}, action={:?}",
409 body.request_id,
410 body.user_action
411 );
412
413 anyhow::Ok(())
414 })
415 .detach_and_log_err(cx);
416}