1use crate::{
2 ContextServerRegistry, CopyPathTool, CreateDirectoryTool, DbLanguageModel, DbThread,
3 DeletePathTool, DiagnosticsTool, EditFileTool, FetchTool, FindPathTool, GrepTool,
4 ListDirectoryTool, MovePathTool, NowTool, OpenTool, ReadFileTool, SystemPromptTemplate,
5 Template, Templates, TerminalTool, ThinkingTool, WebSearchTool,
6};
7use acp_thread::{MentionUri, UserMessageId};
8use action_log::ActionLog;
9use agent::thread::{GitState, ProjectSnapshot, WorktreeSnapshot};
10use agent_client_protocol as acp;
11use agent_settings::{
12 AgentProfileId, AgentProfileSettings, AgentSettings, CompletionMode,
13 SUMMARIZE_THREAD_DETAILED_PROMPT, SUMMARIZE_THREAD_PROMPT,
14};
15use anyhow::{Context as _, Result, anyhow};
16use assistant_tool::adapt_schema_to_format;
17use chrono::{DateTime, Utc};
18use client::{ModelRequestUsage, RequestUsage};
19use cloud_llm_client::{CompletionIntent, CompletionRequestStatus, UsageLimit};
20use collections::{HashMap, HashSet, IndexMap};
21use fs::Fs;
22use futures::{
23 FutureExt,
24 channel::{mpsc, oneshot},
25 future::Shared,
26 stream::FuturesUnordered,
27};
28use git::repository::DiffType;
29use gpui::{
30 App, AppContext, AsyncApp, Context, Entity, EventEmitter, SharedString, Task, WeakEntity,
31};
32use language_model::{
33 LanguageModel, LanguageModelCompletionError, LanguageModelCompletionEvent, LanguageModelExt,
34 LanguageModelImage, LanguageModelProviderId, LanguageModelRegistry, LanguageModelRequest,
35 LanguageModelRequestMessage, LanguageModelRequestTool, LanguageModelToolResult,
36 LanguageModelToolResultContent, LanguageModelToolSchemaFormat, LanguageModelToolUse,
37 LanguageModelToolUseId, Role, SelectedModel, StopReason, TokenUsage,
38};
39use project::{
40 Project,
41 git_store::{GitStore, RepositoryState},
42};
43use prompt_store::ProjectContext;
44use schemars::{JsonSchema, Schema};
45use serde::{Deserialize, Serialize};
46use settings::{Settings, update_settings_file};
47use smol::stream::StreamExt;
48use std::fmt::Write;
49use std::{
50 collections::BTreeMap,
51 ops::RangeInclusive,
52 path::Path,
53 sync::Arc,
54 time::{Duration, Instant},
55};
56use util::{ResultExt, debug_panic, markdown::MarkdownCodeBlock};
57use uuid::Uuid;
58
59const TOOL_CANCELED_MESSAGE: &str = "Tool canceled by user";
60pub const MAX_TOOL_NAME_LENGTH: usize = 64;
61
62/// The ID of the user prompt that initiated a request.
63///
64/// This equates to the user physically submitting a message to the model (e.g., by pressing the Enter key).
65#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Serialize, Deserialize)]
66pub struct PromptId(Arc<str>);
67
68impl PromptId {
69 pub fn new() -> Self {
70 Self(Uuid::new_v4().to_string().into())
71 }
72}
73
74impl std::fmt::Display for PromptId {
75 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
76 write!(f, "{}", self.0)
77 }
78}
79
80pub(crate) const MAX_RETRY_ATTEMPTS: u8 = 4;
81pub(crate) const BASE_RETRY_DELAY: Duration = Duration::from_secs(5);
82
83#[derive(Debug, Clone)]
84enum RetryStrategy {
85 ExponentialBackoff {
86 initial_delay: Duration,
87 max_attempts: u8,
88 },
89 Fixed {
90 delay: Duration,
91 max_attempts: u8,
92 },
93}
94
95#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
96pub enum Message {
97 User(UserMessage),
98 Agent(AgentMessage),
99 Resume,
100}
101
102impl Message {
103 pub fn as_agent_message(&self) -> Option<&AgentMessage> {
104 match self {
105 Message::Agent(agent_message) => Some(agent_message),
106 _ => None,
107 }
108 }
109
110 pub fn to_request(&self) -> Vec<LanguageModelRequestMessage> {
111 match self {
112 Message::User(message) => vec![message.to_request()],
113 Message::Agent(message) => message.to_request(),
114 Message::Resume => vec![LanguageModelRequestMessage {
115 role: Role::User,
116 content: vec!["Continue where you left off".into()],
117 cache: false,
118 }],
119 }
120 }
121
122 pub fn to_markdown(&self) -> String {
123 match self {
124 Message::User(message) => message.to_markdown(),
125 Message::Agent(message) => message.to_markdown(),
126 Message::Resume => "[resume]\n".into(),
127 }
128 }
129
130 pub fn role(&self) -> Role {
131 match self {
132 Message::User(_) | Message::Resume => Role::User,
133 Message::Agent(_) => Role::Assistant,
134 }
135 }
136}
137
138#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
139pub struct UserMessage {
140 pub id: UserMessageId,
141 pub content: Vec<UserMessageContent>,
142}
143
144#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
145pub enum UserMessageContent {
146 Text(String),
147 Mention { uri: MentionUri, content: String },
148 Image(LanguageModelImage),
149}
150
151impl UserMessage {
152 pub fn to_markdown(&self) -> String {
153 let mut markdown = String::from("## User\n\n");
154
155 for content in &self.content {
156 match content {
157 UserMessageContent::Text(text) => {
158 markdown.push_str(text);
159 markdown.push('\n');
160 }
161 UserMessageContent::Image(_) => {
162 markdown.push_str("<image />\n");
163 }
164 UserMessageContent::Mention { uri, content } => {
165 if !content.is_empty() {
166 let _ = writeln!(&mut markdown, "{}\n\n{}", uri.as_link(), content);
167 } else {
168 let _ = writeln!(&mut markdown, "{}", uri.as_link());
169 }
170 }
171 }
172 }
173
174 markdown
175 }
176
177 fn to_request(&self) -> LanguageModelRequestMessage {
178 let mut message = LanguageModelRequestMessage {
179 role: Role::User,
180 content: Vec::with_capacity(self.content.len()),
181 cache: false,
182 };
183
184 const OPEN_CONTEXT: &str = "<context>\n\
185 The following items were attached by the user. \
186 They are up-to-date and don't need to be re-read.\n\n";
187
188 const OPEN_FILES_TAG: &str = "<files>";
189 const OPEN_DIRECTORIES_TAG: &str = "<directories>";
190 const OPEN_SYMBOLS_TAG: &str = "<symbols>";
191 const OPEN_SELECTIONS_TAG: &str = "<selections>";
192 const OPEN_THREADS_TAG: &str = "<threads>";
193 const OPEN_FETCH_TAG: &str = "<fetched_urls>";
194 const OPEN_RULES_TAG: &str =
195 "<rules>\nThe user has specified the following rules that should be applied:\n";
196
197 let mut file_context = OPEN_FILES_TAG.to_string();
198 let mut directory_context = OPEN_DIRECTORIES_TAG.to_string();
199 let mut symbol_context = OPEN_SYMBOLS_TAG.to_string();
200 let mut selection_context = OPEN_SELECTIONS_TAG.to_string();
201 let mut thread_context = OPEN_THREADS_TAG.to_string();
202 let mut fetch_context = OPEN_FETCH_TAG.to_string();
203 let mut rules_context = OPEN_RULES_TAG.to_string();
204
205 for chunk in &self.content {
206 let chunk = match chunk {
207 UserMessageContent::Text(text) => {
208 language_model::MessageContent::Text(text.clone())
209 }
210 UserMessageContent::Image(value) => {
211 language_model::MessageContent::Image(value.clone())
212 }
213 UserMessageContent::Mention { uri, content } => {
214 match uri {
215 MentionUri::File { abs_path } => {
216 write!(
217 &mut file_context,
218 "\n{}",
219 MarkdownCodeBlock {
220 tag: &codeblock_tag(abs_path, None),
221 text: &content.to_string(),
222 }
223 )
224 .ok();
225 }
226 MentionUri::PastedImage => {
227 debug_panic!("pasted image URI should not be used in mention content")
228 }
229 MentionUri::Directory { .. } => {
230 write!(&mut directory_context, "\n{}\n", content).ok();
231 }
232 MentionUri::Symbol {
233 abs_path: path,
234 line_range,
235 ..
236 } => {
237 write!(
238 &mut symbol_context,
239 "\n{}",
240 MarkdownCodeBlock {
241 tag: &codeblock_tag(path, Some(line_range)),
242 text: content
243 }
244 )
245 .ok();
246 }
247 MentionUri::Selection {
248 abs_path: path,
249 line_range,
250 ..
251 } => {
252 write!(
253 &mut selection_context,
254 "\n{}",
255 MarkdownCodeBlock {
256 tag: &codeblock_tag(
257 path.as_deref().unwrap_or("Untitled".as_ref()),
258 Some(line_range)
259 ),
260 text: content
261 }
262 )
263 .ok();
264 }
265 MentionUri::Thread { .. } => {
266 write!(&mut thread_context, "\n{}\n", content).ok();
267 }
268 MentionUri::TextThread { .. } => {
269 write!(&mut thread_context, "\n{}\n", content).ok();
270 }
271 MentionUri::Rule { .. } => {
272 write!(
273 &mut rules_context,
274 "\n{}",
275 MarkdownCodeBlock {
276 tag: "",
277 text: content
278 }
279 )
280 .ok();
281 }
282 MentionUri::Fetch { url } => {
283 write!(&mut fetch_context, "\nFetch: {}\n\n{}", url, content).ok();
284 }
285 }
286
287 language_model::MessageContent::Text(uri.as_link().to_string())
288 }
289 };
290
291 message.content.push(chunk);
292 }
293
294 let len_before_context = message.content.len();
295
296 if file_context.len() > OPEN_FILES_TAG.len() {
297 file_context.push_str("</files>\n");
298 message
299 .content
300 .push(language_model::MessageContent::Text(file_context));
301 }
302
303 if directory_context.len() > OPEN_DIRECTORIES_TAG.len() {
304 directory_context.push_str("</directories>\n");
305 message
306 .content
307 .push(language_model::MessageContent::Text(directory_context));
308 }
309
310 if symbol_context.len() > OPEN_SYMBOLS_TAG.len() {
311 symbol_context.push_str("</symbols>\n");
312 message
313 .content
314 .push(language_model::MessageContent::Text(symbol_context));
315 }
316
317 if selection_context.len() > OPEN_SELECTIONS_TAG.len() {
318 selection_context.push_str("</selections>\n");
319 message
320 .content
321 .push(language_model::MessageContent::Text(selection_context));
322 }
323
324 if thread_context.len() > OPEN_THREADS_TAG.len() {
325 thread_context.push_str("</threads>\n");
326 message
327 .content
328 .push(language_model::MessageContent::Text(thread_context));
329 }
330
331 if fetch_context.len() > OPEN_FETCH_TAG.len() {
332 fetch_context.push_str("</fetched_urls>\n");
333 message
334 .content
335 .push(language_model::MessageContent::Text(fetch_context));
336 }
337
338 if rules_context.len() > OPEN_RULES_TAG.len() {
339 rules_context.push_str("</user_rules>\n");
340 message
341 .content
342 .push(language_model::MessageContent::Text(rules_context));
343 }
344
345 if message.content.len() > len_before_context {
346 message.content.insert(
347 len_before_context,
348 language_model::MessageContent::Text(OPEN_CONTEXT.into()),
349 );
350 message
351 .content
352 .push(language_model::MessageContent::Text("</context>".into()));
353 }
354
355 message
356 }
357}
358
359fn codeblock_tag(full_path: &Path, line_range: Option<&RangeInclusive<u32>>) -> String {
360 let mut result = String::new();
361
362 if let Some(extension) = full_path.extension().and_then(|ext| ext.to_str()) {
363 let _ = write!(result, "{} ", extension);
364 }
365
366 let _ = write!(result, "{}", full_path.display());
367
368 if let Some(range) = line_range {
369 if range.start() == range.end() {
370 let _ = write!(result, ":{}", range.start() + 1);
371 } else {
372 let _ = write!(result, ":{}-{}", range.start() + 1, range.end() + 1);
373 }
374 }
375
376 result
377}
378
379impl AgentMessage {
380 pub fn to_markdown(&self) -> String {
381 let mut markdown = String::from("## Assistant\n\n");
382
383 for content in &self.content {
384 match content {
385 AgentMessageContent::Text(text) => {
386 markdown.push_str(text);
387 markdown.push('\n');
388 }
389 AgentMessageContent::Thinking { text, .. } => {
390 markdown.push_str("<think>");
391 markdown.push_str(text);
392 markdown.push_str("</think>\n");
393 }
394 AgentMessageContent::RedactedThinking(_) => {
395 markdown.push_str("<redacted_thinking />\n")
396 }
397 AgentMessageContent::ToolUse(tool_use) => {
398 markdown.push_str(&format!(
399 "**Tool Use**: {} (ID: {})\n",
400 tool_use.name, tool_use.id
401 ));
402 markdown.push_str(&format!(
403 "{}\n",
404 MarkdownCodeBlock {
405 tag: "json",
406 text: &format!("{:#}", tool_use.input)
407 }
408 ));
409 }
410 }
411 }
412
413 for tool_result in self.tool_results.values() {
414 markdown.push_str(&format!(
415 "**Tool Result**: {} (ID: {})\n\n",
416 tool_result.tool_name, tool_result.tool_use_id
417 ));
418 if tool_result.is_error {
419 markdown.push_str("**ERROR:**\n");
420 }
421
422 match &tool_result.content {
423 LanguageModelToolResultContent::Text(text) => {
424 writeln!(markdown, "{text}\n").ok();
425 }
426 LanguageModelToolResultContent::Image(_) => {
427 writeln!(markdown, "<image />\n").ok();
428 }
429 }
430
431 if let Some(output) = tool_result.output.as_ref() {
432 writeln!(
433 markdown,
434 "**Debug Output**:\n\n```json\n{}\n```\n",
435 serde_json::to_string_pretty(output).unwrap()
436 )
437 .unwrap();
438 }
439 }
440
441 markdown
442 }
443
444 pub fn to_request(&self) -> Vec<LanguageModelRequestMessage> {
445 let mut assistant_message = LanguageModelRequestMessage {
446 role: Role::Assistant,
447 content: Vec::with_capacity(self.content.len()),
448 cache: false,
449 };
450 for chunk in &self.content {
451 match chunk {
452 AgentMessageContent::Text(text) => {
453 assistant_message
454 .content
455 .push(language_model::MessageContent::Text(text.clone()));
456 }
457 AgentMessageContent::Thinking { text, signature } => {
458 assistant_message
459 .content
460 .push(language_model::MessageContent::Thinking {
461 text: text.clone(),
462 signature: signature.clone(),
463 });
464 }
465 AgentMessageContent::RedactedThinking(value) => {
466 assistant_message.content.push(
467 language_model::MessageContent::RedactedThinking(value.clone()),
468 );
469 }
470 AgentMessageContent::ToolUse(tool_use) => {
471 if self.tool_results.contains_key(&tool_use.id) {
472 assistant_message
473 .content
474 .push(language_model::MessageContent::ToolUse(tool_use.clone()));
475 }
476 }
477 };
478 }
479
480 let mut user_message = LanguageModelRequestMessage {
481 role: Role::User,
482 content: Vec::new(),
483 cache: false,
484 };
485
486 for tool_result in self.tool_results.values() {
487 let mut tool_result = tool_result.clone();
488 // Surprisingly, the API fails if we return an empty string here.
489 // It thinks we are sending a tool use without a tool result.
490 if tool_result.content.is_empty() {
491 tool_result.content = "<Tool returned an empty string>".into();
492 }
493 user_message
494 .content
495 .push(language_model::MessageContent::ToolResult(tool_result));
496 }
497
498 let mut messages = Vec::new();
499 if !assistant_message.content.is_empty() {
500 messages.push(assistant_message);
501 }
502 if !user_message.content.is_empty() {
503 messages.push(user_message);
504 }
505 messages
506 }
507}
508
509#[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
510pub struct AgentMessage {
511 pub content: Vec<AgentMessageContent>,
512 pub tool_results: IndexMap<LanguageModelToolUseId, LanguageModelToolResult>,
513}
514
515#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
516pub enum AgentMessageContent {
517 Text(String),
518 Thinking {
519 text: String,
520 signature: Option<String>,
521 },
522 RedactedThinking(String),
523 ToolUse(LanguageModelToolUse),
524}
525
526#[derive(Debug)]
527pub enum ThreadEvent {
528 UserMessage(UserMessage),
529 AgentText(String),
530 AgentThinking(String),
531 ToolCall(acp::ToolCall),
532 ToolCallUpdate(acp_thread::ToolCallUpdate),
533 ToolCallAuthorization(ToolCallAuthorization),
534 Retry(acp_thread::RetryStatus),
535 Stop(acp::StopReason),
536}
537
538#[derive(Debug)]
539pub struct ToolCallAuthorization {
540 pub tool_call: acp::ToolCallUpdate,
541 pub options: Vec<acp::PermissionOption>,
542 pub response: oneshot::Sender<acp::PermissionOptionId>,
543}
544
545#[derive(Debug, thiserror::Error)]
546enum CompletionError {
547 #[error("max tokens")]
548 MaxTokens,
549 #[error("refusal")]
550 Refusal,
551 #[error(transparent)]
552 Other(#[from] anyhow::Error),
553}
554
555pub struct Thread {
556 id: acp::SessionId,
557 prompt_id: PromptId,
558 updated_at: DateTime<Utc>,
559 title: Option<SharedString>,
560 pending_title_generation: Option<Task<()>>,
561 summary: Option<SharedString>,
562 messages: Vec<Message>,
563 completion_mode: CompletionMode,
564 /// Holds the task that handles agent interaction until the end of the turn.
565 /// Survives across multiple requests as the model performs tool calls and
566 /// we run tools, report their results.
567 running_turn: Option<RunningTurn>,
568 pending_message: Option<AgentMessage>,
569 tools: BTreeMap<SharedString, Arc<dyn AnyAgentTool>>,
570 tool_use_limit_reached: bool,
571 request_token_usage: HashMap<UserMessageId, language_model::TokenUsage>,
572 #[allow(unused)]
573 cumulative_token_usage: TokenUsage,
574 #[allow(unused)]
575 initial_project_snapshot: Shared<Task<Option<Arc<ProjectSnapshot>>>>,
576 context_server_registry: Entity<ContextServerRegistry>,
577 profile_id: AgentProfileId,
578 project_context: Entity<ProjectContext>,
579 templates: Arc<Templates>,
580 model: Option<Arc<dyn LanguageModel>>,
581 summarization_model: Option<Arc<dyn LanguageModel>>,
582 prompt_capabilities_tx: watch::Sender<acp::PromptCapabilities>,
583 pub(crate) prompt_capabilities_rx: watch::Receiver<acp::PromptCapabilities>,
584 pub(crate) project: Entity<Project>,
585 pub(crate) action_log: Entity<ActionLog>,
586}
587
588impl Thread {
589 fn prompt_capabilities(model: Option<&dyn LanguageModel>) -> acp::PromptCapabilities {
590 let image = model.map_or(true, |model| model.supports_images());
591 acp::PromptCapabilities {
592 image,
593 audio: false,
594 embedded_context: true,
595 }
596 }
597
598 pub fn new(
599 project: Entity<Project>,
600 project_context: Entity<ProjectContext>,
601 context_server_registry: Entity<ContextServerRegistry>,
602 templates: Arc<Templates>,
603 model: Option<Arc<dyn LanguageModel>>,
604 cx: &mut Context<Self>,
605 ) -> Self {
606 let profile_id = AgentSettings::get_global(cx).default_profile.clone();
607 let action_log = cx.new(|_cx| ActionLog::new(project.clone()));
608 let (prompt_capabilities_tx, prompt_capabilities_rx) =
609 watch::channel(Self::prompt_capabilities(model.as_deref()));
610 Self {
611 id: acp::SessionId(uuid::Uuid::new_v4().to_string().into()),
612 prompt_id: PromptId::new(),
613 updated_at: Utc::now(),
614 title: None,
615 pending_title_generation: None,
616 summary: None,
617 messages: Vec::new(),
618 completion_mode: AgentSettings::get_global(cx).preferred_completion_mode,
619 running_turn: None,
620 pending_message: None,
621 tools: BTreeMap::default(),
622 tool_use_limit_reached: false,
623 request_token_usage: HashMap::default(),
624 cumulative_token_usage: TokenUsage::default(),
625 initial_project_snapshot: {
626 let project_snapshot = Self::project_snapshot(project.clone(), cx);
627 cx.foreground_executor()
628 .spawn(async move { Some(project_snapshot.await) })
629 .shared()
630 },
631 context_server_registry,
632 profile_id,
633 project_context,
634 templates,
635 model,
636 summarization_model: None,
637 prompt_capabilities_tx,
638 prompt_capabilities_rx,
639 project,
640 action_log,
641 }
642 }
643
644 pub fn id(&self) -> &acp::SessionId {
645 &self.id
646 }
647
648 pub fn replay(
649 &mut self,
650 cx: &mut Context<Self>,
651 ) -> mpsc::UnboundedReceiver<Result<ThreadEvent>> {
652 let (tx, rx) = mpsc::unbounded();
653 let stream = ThreadEventStream(tx);
654 for message in &self.messages {
655 match message {
656 Message::User(user_message) => stream.send_user_message(user_message),
657 Message::Agent(assistant_message) => {
658 for content in &assistant_message.content {
659 match content {
660 AgentMessageContent::Text(text) => stream.send_text(text),
661 AgentMessageContent::Thinking { text, .. } => {
662 stream.send_thinking(text)
663 }
664 AgentMessageContent::RedactedThinking(_) => {}
665 AgentMessageContent::ToolUse(tool_use) => {
666 self.replay_tool_call(
667 tool_use,
668 assistant_message.tool_results.get(&tool_use.id),
669 &stream,
670 cx,
671 );
672 }
673 }
674 }
675 }
676 Message::Resume => {}
677 }
678 }
679 rx
680 }
681
682 fn replay_tool_call(
683 &self,
684 tool_use: &LanguageModelToolUse,
685 tool_result: Option<&LanguageModelToolResult>,
686 stream: &ThreadEventStream,
687 cx: &mut Context<Self>,
688 ) {
689 let tool = self.tools.get(tool_use.name.as_ref()).cloned().or_else(|| {
690 self.context_server_registry
691 .read(cx)
692 .servers()
693 .find_map(|(_, tools)| {
694 if let Some(tool) = tools.get(tool_use.name.as_ref()) {
695 Some(tool.clone())
696 } else {
697 None
698 }
699 })
700 });
701
702 let Some(tool) = tool else {
703 stream
704 .0
705 .unbounded_send(Ok(ThreadEvent::ToolCall(acp::ToolCall {
706 id: acp::ToolCallId(tool_use.id.to_string().into()),
707 title: tool_use.name.to_string(),
708 kind: acp::ToolKind::Other,
709 status: acp::ToolCallStatus::Failed,
710 content: Vec::new(),
711 locations: Vec::new(),
712 raw_input: Some(tool_use.input.clone()),
713 raw_output: None,
714 })))
715 .ok();
716 return;
717 };
718
719 let title = tool.initial_title(tool_use.input.clone());
720 let kind = tool.kind();
721 stream.send_tool_call(&tool_use.id, title, kind, tool_use.input.clone());
722
723 let output = tool_result
724 .as_ref()
725 .and_then(|result| result.output.clone());
726 if let Some(output) = output.clone() {
727 let tool_event_stream = ToolCallEventStream::new(
728 tool_use.id.clone(),
729 stream.clone(),
730 Some(self.project.read(cx).fs().clone()),
731 );
732 tool.replay(tool_use.input.clone(), output, tool_event_stream, cx)
733 .log_err();
734 }
735
736 stream.update_tool_call_fields(
737 &tool_use.id,
738 acp::ToolCallUpdateFields {
739 status: Some(
740 tool_result
741 .as_ref()
742 .map_or(acp::ToolCallStatus::Failed, |result| {
743 if result.is_error {
744 acp::ToolCallStatus::Failed
745 } else {
746 acp::ToolCallStatus::Completed
747 }
748 }),
749 ),
750 raw_output: output,
751 ..Default::default()
752 },
753 );
754 }
755
756 pub fn from_db(
757 id: acp::SessionId,
758 db_thread: DbThread,
759 project: Entity<Project>,
760 project_context: Entity<ProjectContext>,
761 context_server_registry: Entity<ContextServerRegistry>,
762 action_log: Entity<ActionLog>,
763 templates: Arc<Templates>,
764 cx: &mut Context<Self>,
765 ) -> Self {
766 let profile_id = db_thread
767 .profile
768 .unwrap_or_else(|| AgentSettings::get_global(cx).default_profile.clone());
769 let model = LanguageModelRegistry::global(cx).update(cx, |registry, cx| {
770 db_thread
771 .model
772 .and_then(|model| {
773 let model = SelectedModel {
774 provider: model.provider.clone().into(),
775 model: model.model.into(),
776 };
777 registry.select_model(&model, cx)
778 })
779 .or_else(|| registry.default_model())
780 .map(|model| model.model)
781 });
782 let (prompt_capabilities_tx, prompt_capabilities_rx) =
783 watch::channel(Self::prompt_capabilities(model.as_deref()));
784
785 Self {
786 id,
787 prompt_id: PromptId::new(),
788 title: if db_thread.title.is_empty() {
789 None
790 } else {
791 Some(db_thread.title.clone())
792 },
793 pending_title_generation: None,
794 summary: db_thread.detailed_summary,
795 messages: db_thread.messages,
796 completion_mode: db_thread.completion_mode.unwrap_or_default(),
797 running_turn: None,
798 pending_message: None,
799 tools: BTreeMap::default(),
800 tool_use_limit_reached: false,
801 request_token_usage: db_thread.request_token_usage.clone(),
802 cumulative_token_usage: db_thread.cumulative_token_usage,
803 initial_project_snapshot: Task::ready(db_thread.initial_project_snapshot).shared(),
804 context_server_registry,
805 profile_id,
806 project_context,
807 templates,
808 model,
809 summarization_model: None,
810 project,
811 action_log,
812 updated_at: db_thread.updated_at,
813 prompt_capabilities_tx,
814 prompt_capabilities_rx,
815 }
816 }
817
818 pub fn to_db(&self, cx: &App) -> Task<DbThread> {
819 let initial_project_snapshot = self.initial_project_snapshot.clone();
820 let mut thread = DbThread {
821 title: self.title(),
822 messages: self.messages.clone(),
823 updated_at: self.updated_at,
824 detailed_summary: self.summary.clone(),
825 initial_project_snapshot: None,
826 cumulative_token_usage: self.cumulative_token_usage,
827 request_token_usage: self.request_token_usage.clone(),
828 model: self.model.as_ref().map(|model| DbLanguageModel {
829 provider: model.provider_id().to_string(),
830 model: model.name().0.to_string(),
831 }),
832 completion_mode: Some(self.completion_mode),
833 profile: Some(self.profile_id.clone()),
834 };
835
836 cx.background_spawn(async move {
837 let initial_project_snapshot = initial_project_snapshot.await;
838 thread.initial_project_snapshot = initial_project_snapshot;
839 thread
840 })
841 }
842
843 /// Create a snapshot of the current project state including git information and unsaved buffers.
844 fn project_snapshot(
845 project: Entity<Project>,
846 cx: &mut Context<Self>,
847 ) -> Task<Arc<agent::thread::ProjectSnapshot>> {
848 let git_store = project.read(cx).git_store().clone();
849 let worktree_snapshots: Vec<_> = project
850 .read(cx)
851 .visible_worktrees(cx)
852 .map(|worktree| Self::worktree_snapshot(worktree, git_store.clone(), cx))
853 .collect();
854
855 cx.spawn(async move |_, cx| {
856 let worktree_snapshots = futures::future::join_all(worktree_snapshots).await;
857
858 let mut unsaved_buffers = Vec::new();
859 cx.update(|app_cx| {
860 let buffer_store = project.read(app_cx).buffer_store();
861 for buffer_handle in buffer_store.read(app_cx).buffers() {
862 let buffer = buffer_handle.read(app_cx);
863 if buffer.is_dirty()
864 && let Some(file) = buffer.file()
865 {
866 let path = file.path().to_string_lossy().to_string();
867 unsaved_buffers.push(path);
868 }
869 }
870 })
871 .ok();
872
873 Arc::new(ProjectSnapshot {
874 worktree_snapshots,
875 unsaved_buffer_paths: unsaved_buffers,
876 timestamp: Utc::now(),
877 })
878 })
879 }
880
881 fn worktree_snapshot(
882 worktree: Entity<project::Worktree>,
883 git_store: Entity<GitStore>,
884 cx: &App,
885 ) -> Task<agent::thread::WorktreeSnapshot> {
886 cx.spawn(async move |cx| {
887 // Get worktree path and snapshot
888 let worktree_info = cx.update(|app_cx| {
889 let worktree = worktree.read(app_cx);
890 let path = worktree.abs_path().to_string_lossy().to_string();
891 let snapshot = worktree.snapshot();
892 (path, snapshot)
893 });
894
895 let Ok((worktree_path, _snapshot)) = worktree_info else {
896 return WorktreeSnapshot {
897 worktree_path: String::new(),
898 git_state: None,
899 };
900 };
901
902 let git_state = git_store
903 .update(cx, |git_store, cx| {
904 git_store
905 .repositories()
906 .values()
907 .find(|repo| {
908 repo.read(cx)
909 .abs_path_to_repo_path(&worktree.read(cx).abs_path())
910 .is_some()
911 })
912 .cloned()
913 })
914 .ok()
915 .flatten()
916 .map(|repo| {
917 repo.update(cx, |repo, _| {
918 let current_branch =
919 repo.branch.as_ref().map(|branch| branch.name().to_owned());
920 repo.send_job(None, |state, _| async move {
921 let RepositoryState::Local { backend, .. } = state else {
922 return GitState {
923 remote_url: None,
924 head_sha: None,
925 current_branch,
926 diff: None,
927 };
928 };
929
930 let remote_url = backend.remote_url("origin");
931 let head_sha = backend.head_sha().await;
932 let diff = backend.diff(DiffType::HeadToWorktree).await.ok();
933
934 GitState {
935 remote_url,
936 head_sha,
937 current_branch,
938 diff,
939 }
940 })
941 })
942 });
943
944 let git_state = match git_state {
945 Some(git_state) => match git_state.ok() {
946 Some(git_state) => git_state.await.ok(),
947 None => None,
948 },
949 None => None,
950 };
951
952 WorktreeSnapshot {
953 worktree_path,
954 git_state,
955 }
956 })
957 }
958
959 pub fn project_context(&self) -> &Entity<ProjectContext> {
960 &self.project_context
961 }
962
963 pub fn project(&self) -> &Entity<Project> {
964 &self.project
965 }
966
967 pub fn action_log(&self) -> &Entity<ActionLog> {
968 &self.action_log
969 }
970
971 pub fn is_empty(&self) -> bool {
972 self.messages.is_empty() && self.title.is_none()
973 }
974
975 pub fn model(&self) -> Option<&Arc<dyn LanguageModel>> {
976 self.model.as_ref()
977 }
978
979 pub fn set_model(&mut self, model: Arc<dyn LanguageModel>, cx: &mut Context<Self>) {
980 let old_usage = self.latest_token_usage();
981 self.model = Some(model);
982 let new_caps = Self::prompt_capabilities(self.model.as_deref());
983 let new_usage = self.latest_token_usage();
984 if old_usage != new_usage {
985 cx.emit(TokenUsageUpdated(new_usage));
986 }
987 self.prompt_capabilities_tx.send(new_caps).log_err();
988 cx.notify()
989 }
990
991 pub fn summarization_model(&self) -> Option<&Arc<dyn LanguageModel>> {
992 self.summarization_model.as_ref()
993 }
994
995 pub fn set_summarization_model(
996 &mut self,
997 model: Option<Arc<dyn LanguageModel>>,
998 cx: &mut Context<Self>,
999 ) {
1000 self.summarization_model = model;
1001 cx.notify()
1002 }
1003
1004 pub fn completion_mode(&self) -> CompletionMode {
1005 self.completion_mode
1006 }
1007
1008 pub fn set_completion_mode(&mut self, mode: CompletionMode, cx: &mut Context<Self>) {
1009 let old_usage = self.latest_token_usage();
1010 self.completion_mode = mode;
1011 let new_usage = self.latest_token_usage();
1012 if old_usage != new_usage {
1013 cx.emit(TokenUsageUpdated(new_usage));
1014 }
1015 cx.notify()
1016 }
1017
1018 #[cfg(any(test, feature = "test-support"))]
1019 pub fn last_message(&self) -> Option<Message> {
1020 if let Some(message) = self.pending_message.clone() {
1021 Some(Message::Agent(message))
1022 } else {
1023 self.messages.last().cloned()
1024 }
1025 }
1026
1027 pub fn add_default_tools(&mut self, cx: &mut Context<Self>) {
1028 let language_registry = self.project.read(cx).languages().clone();
1029 self.add_tool(CopyPathTool::new(self.project.clone()));
1030 self.add_tool(CreateDirectoryTool::new(self.project.clone()));
1031 self.add_tool(DeletePathTool::new(
1032 self.project.clone(),
1033 self.action_log.clone(),
1034 ));
1035 self.add_tool(DiagnosticsTool::new(self.project.clone()));
1036 self.add_tool(EditFileTool::new(cx.weak_entity(), language_registry));
1037 self.add_tool(FetchTool::new(self.project.read(cx).client().http_client()));
1038 self.add_tool(FindPathTool::new(self.project.clone()));
1039 self.add_tool(GrepTool::new(self.project.clone()));
1040 self.add_tool(ListDirectoryTool::new(self.project.clone()));
1041 self.add_tool(MovePathTool::new(self.project.clone()));
1042 self.add_tool(NowTool);
1043 self.add_tool(OpenTool::new(self.project.clone()));
1044 self.add_tool(ReadFileTool::new(
1045 self.project.clone(),
1046 self.action_log.clone(),
1047 ));
1048 self.add_tool(TerminalTool::new(self.project.clone(), cx));
1049 self.add_tool(ThinkingTool);
1050 self.add_tool(WebSearchTool);
1051 }
1052
1053 pub fn add_tool<T: AgentTool>(&mut self, tool: T) {
1054 self.tools.insert(T::name().into(), tool.erase());
1055 }
1056
1057 pub fn remove_tool(&mut self, name: &str) -> bool {
1058 self.tools.remove(name).is_some()
1059 }
1060
1061 pub fn profile(&self) -> &AgentProfileId {
1062 &self.profile_id
1063 }
1064
1065 pub fn set_profile(&mut self, profile_id: AgentProfileId) {
1066 self.profile_id = profile_id;
1067 }
1068
1069 pub fn cancel(&mut self, cx: &mut Context<Self>) {
1070 if let Some(running_turn) = self.running_turn.take() {
1071 running_turn.cancel();
1072 }
1073 self.flush_pending_message(cx);
1074 }
1075
1076 fn update_token_usage(&mut self, update: language_model::TokenUsage, cx: &mut Context<Self>) {
1077 let Some(last_user_message) = self.last_user_message() else {
1078 return;
1079 };
1080
1081 self.request_token_usage
1082 .insert(last_user_message.id.clone(), update);
1083 cx.emit(TokenUsageUpdated(self.latest_token_usage()));
1084 cx.notify();
1085 }
1086
1087 pub fn truncate(&mut self, message_id: UserMessageId, cx: &mut Context<Self>) -> Result<()> {
1088 self.cancel(cx);
1089 let Some(position) = self.messages.iter().position(
1090 |msg| matches!(msg, Message::User(UserMessage { id, .. }) if id == &message_id),
1091 ) else {
1092 return Err(anyhow!("Message not found"));
1093 };
1094
1095 for message in self.messages.drain(position..) {
1096 match message {
1097 Message::User(message) => {
1098 self.request_token_usage.remove(&message.id);
1099 }
1100 Message::Agent(_) | Message::Resume => {}
1101 }
1102 }
1103 self.summary = None;
1104 cx.notify();
1105 Ok(())
1106 }
1107
1108 pub fn latest_token_usage(&self) -> Option<acp_thread::TokenUsage> {
1109 let last_user_message = self.last_user_message()?;
1110 let tokens = self.request_token_usage.get(&last_user_message.id)?;
1111 let model = self.model.clone()?;
1112
1113 Some(acp_thread::TokenUsage {
1114 max_tokens: model.max_token_count_for_mode(self.completion_mode.into()),
1115 used_tokens: tokens.total_tokens(),
1116 })
1117 }
1118
1119 pub fn resume(
1120 &mut self,
1121 cx: &mut Context<Self>,
1122 ) -> Result<mpsc::UnboundedReceiver<Result<ThreadEvent>>> {
1123 self.messages.push(Message::Resume);
1124 cx.notify();
1125
1126 log::debug!("Total messages in thread: {}", self.messages.len());
1127 self.run_turn(cx)
1128 }
1129
1130 /// Sending a message results in the model streaming a response, which could include tool calls.
1131 /// After calling tools, the model will stops and waits for any outstanding tool calls to be completed and their results sent.
1132 /// The returned channel will report all the occurrences in which the model stops before erroring or ending its turn.
1133 pub fn send<T>(
1134 &mut self,
1135 id: UserMessageId,
1136 content: impl IntoIterator<Item = T>,
1137 cx: &mut Context<Self>,
1138 ) -> Result<mpsc::UnboundedReceiver<Result<ThreadEvent>>>
1139 where
1140 T: Into<UserMessageContent>,
1141 {
1142 let model = self.model().context("No language model configured")?;
1143
1144 log::info!("Thread::send called with model: {}", model.name().0);
1145 self.advance_prompt_id();
1146
1147 let content = content.into_iter().map(Into::into).collect::<Vec<_>>();
1148 log::debug!("Thread::send content: {:?}", content);
1149
1150 self.messages
1151 .push(Message::User(UserMessage { id, content }));
1152 cx.notify();
1153
1154 log::debug!("Total messages in thread: {}", self.messages.len());
1155 self.run_turn(cx)
1156 }
1157
1158 fn run_turn(
1159 &mut self,
1160 cx: &mut Context<Self>,
1161 ) -> Result<mpsc::UnboundedReceiver<Result<ThreadEvent>>> {
1162 self.cancel(cx);
1163
1164 let model = self.model.clone().context("No language model configured")?;
1165 let profile = AgentSettings::get_global(cx)
1166 .profiles
1167 .get(&self.profile_id)
1168 .context("Profile not found")?;
1169 let (events_tx, events_rx) = mpsc::unbounded::<Result<ThreadEvent>>();
1170 let event_stream = ThreadEventStream(events_tx);
1171 let message_ix = self.messages.len().saturating_sub(1);
1172 self.tool_use_limit_reached = false;
1173 self.summary = None;
1174 self.running_turn = Some(RunningTurn {
1175 event_stream: event_stream.clone(),
1176 tools: self.enabled_tools(profile, &model, cx),
1177 _task: cx.spawn(async move |this, cx| {
1178 log::debug!("Starting agent turn execution");
1179
1180 let turn_result = Self::run_turn_internal(&this, model, &event_stream, cx).await;
1181 _ = this.update(cx, |this, cx| this.flush_pending_message(cx));
1182
1183 match turn_result {
1184 Ok(()) => {
1185 log::debug!("Turn execution completed");
1186 event_stream.send_stop(acp::StopReason::EndTurn);
1187 }
1188 Err(error) => {
1189 log::error!("Turn execution failed: {:?}", error);
1190 match error.downcast::<CompletionError>() {
1191 Ok(CompletionError::Refusal) => {
1192 event_stream.send_stop(acp::StopReason::Refusal);
1193 _ = this.update(cx, |this, _| this.messages.truncate(message_ix));
1194 }
1195 Ok(CompletionError::MaxTokens) => {
1196 event_stream.send_stop(acp::StopReason::MaxTokens);
1197 }
1198 Ok(CompletionError::Other(error)) | Err(error) => {
1199 event_stream.send_error(error);
1200 }
1201 }
1202 }
1203 }
1204
1205 _ = this.update(cx, |this, _| this.running_turn.take());
1206 }),
1207 });
1208 Ok(events_rx)
1209 }
1210
1211 async fn run_turn_internal(
1212 this: &WeakEntity<Self>,
1213 model: Arc<dyn LanguageModel>,
1214 event_stream: &ThreadEventStream,
1215 cx: &mut AsyncApp,
1216 ) -> Result<()> {
1217 let mut attempt = 0;
1218 let mut intent = CompletionIntent::UserPrompt;
1219 loop {
1220 let request =
1221 this.update(cx, |this, cx| this.build_completion_request(intent, cx))??;
1222
1223 telemetry::event!(
1224 "Agent Thread Completion",
1225 thread_id = this.read_with(cx, |this, _| this.id.to_string())?,
1226 prompt_id = this.read_with(cx, |this, _| this.prompt_id.to_string())?,
1227 model = model.telemetry_id(),
1228 model_provider = model.provider_id().to_string(),
1229 attempt
1230 );
1231
1232 log::debug!("Calling model.stream_completion, attempt {}", attempt);
1233 let mut events = model
1234 .stream_completion(request, cx)
1235 .await
1236 .map_err(|error| anyhow!(error))?;
1237 let mut tool_results = FuturesUnordered::new();
1238 let mut error = None;
1239 while let Some(event) = events.next().await {
1240 log::trace!("Received completion event: {:?}", event);
1241 match event {
1242 Ok(event) => {
1243 tool_results.extend(this.update(cx, |this, cx| {
1244 this.handle_completion_event(event, event_stream, cx)
1245 })??);
1246 }
1247 Err(err) => {
1248 error = Some(err);
1249 break;
1250 }
1251 }
1252 }
1253
1254 let end_turn = tool_results.is_empty();
1255 while let Some(tool_result) = tool_results.next().await {
1256 log::debug!("Tool finished {:?}", tool_result);
1257
1258 event_stream.update_tool_call_fields(
1259 &tool_result.tool_use_id,
1260 acp::ToolCallUpdateFields {
1261 status: Some(if tool_result.is_error {
1262 acp::ToolCallStatus::Failed
1263 } else {
1264 acp::ToolCallStatus::Completed
1265 }),
1266 raw_output: tool_result.output.clone(),
1267 ..Default::default()
1268 },
1269 );
1270 this.update(cx, |this, _cx| {
1271 this.pending_message()
1272 .tool_results
1273 .insert(tool_result.tool_use_id.clone(), tool_result);
1274 })?;
1275 }
1276
1277 this.update(cx, |this, cx| {
1278 this.flush_pending_message(cx);
1279 if this.title.is_none() && this.pending_title_generation.is_none() {
1280 this.generate_title(cx);
1281 }
1282 })?;
1283
1284 if let Some(error) = error {
1285 attempt += 1;
1286 let retry =
1287 this.update(cx, |this, _| this.handle_completion_error(error, attempt))??;
1288 let timer = cx.background_executor().timer(retry.duration);
1289 event_stream.send_retry(retry);
1290 timer.await;
1291 this.update(cx, |this, _cx| {
1292 if let Some(Message::Agent(message)) = this.messages.last() {
1293 if message.tool_results.is_empty() {
1294 intent = CompletionIntent::UserPrompt;
1295 this.messages.push(Message::Resume);
1296 }
1297 }
1298 })?;
1299 } else if this.read_with(cx, |this, _| this.tool_use_limit_reached)? {
1300 return Err(language_model::ToolUseLimitReachedError.into());
1301 } else if end_turn {
1302 return Ok(());
1303 } else {
1304 intent = CompletionIntent::ToolResults;
1305 attempt = 0;
1306 }
1307 }
1308 }
1309
1310 fn handle_completion_error(
1311 &mut self,
1312 error: LanguageModelCompletionError,
1313 attempt: u8,
1314 ) -> Result<acp_thread::RetryStatus> {
1315 if self.completion_mode == CompletionMode::Normal {
1316 return Err(anyhow!(error));
1317 }
1318
1319 let Some(strategy) = Self::retry_strategy_for(&error) else {
1320 return Err(anyhow!(error));
1321 };
1322
1323 let max_attempts = match &strategy {
1324 RetryStrategy::ExponentialBackoff { max_attempts, .. } => *max_attempts,
1325 RetryStrategy::Fixed { max_attempts, .. } => *max_attempts,
1326 };
1327
1328 if attempt > max_attempts {
1329 return Err(anyhow!(error));
1330 }
1331
1332 let delay = match &strategy {
1333 RetryStrategy::ExponentialBackoff { initial_delay, .. } => {
1334 let delay_secs = initial_delay.as_secs() * 2u64.pow((attempt - 1) as u32);
1335 Duration::from_secs(delay_secs)
1336 }
1337 RetryStrategy::Fixed { delay, .. } => *delay,
1338 };
1339 log::debug!("Retry attempt {attempt} with delay {delay:?}");
1340
1341 Ok(acp_thread::RetryStatus {
1342 last_error: error.to_string().into(),
1343 attempt: attempt as usize,
1344 max_attempts: max_attempts as usize,
1345 started_at: Instant::now(),
1346 duration: delay,
1347 })
1348 }
1349
1350 /// A helper method that's called on every streamed completion event.
1351 /// Returns an optional tool result task, which the main agentic loop will
1352 /// send back to the model when it resolves.
1353 fn handle_completion_event(
1354 &mut self,
1355 event: LanguageModelCompletionEvent,
1356 event_stream: &ThreadEventStream,
1357 cx: &mut Context<Self>,
1358 ) -> Result<Option<Task<LanguageModelToolResult>>> {
1359 log::trace!("Handling streamed completion event: {:?}", event);
1360 use LanguageModelCompletionEvent::*;
1361
1362 match event {
1363 StartMessage { .. } => {
1364 self.flush_pending_message(cx);
1365 self.pending_message = Some(AgentMessage::default());
1366 }
1367 Text(new_text) => self.handle_text_event(new_text, event_stream, cx),
1368 Thinking { text, signature } => {
1369 self.handle_thinking_event(text, signature, event_stream, cx)
1370 }
1371 RedactedThinking { data } => self.handle_redacted_thinking_event(data, cx),
1372 ToolUse(tool_use) => {
1373 return Ok(self.handle_tool_use_event(tool_use, event_stream, cx));
1374 }
1375 ToolUseJsonParseError {
1376 id,
1377 tool_name,
1378 raw_input,
1379 json_parse_error,
1380 } => {
1381 return Ok(Some(Task::ready(
1382 self.handle_tool_use_json_parse_error_event(
1383 id,
1384 tool_name,
1385 raw_input,
1386 json_parse_error,
1387 ),
1388 )));
1389 }
1390 UsageUpdate(usage) => {
1391 telemetry::event!(
1392 "Agent Thread Completion Usage Updated",
1393 thread_id = self.id.to_string(),
1394 prompt_id = self.prompt_id.to_string(),
1395 model = self.model.as_ref().map(|m| m.telemetry_id()),
1396 model_provider = self.model.as_ref().map(|m| m.provider_id().to_string()),
1397 input_tokens = usage.input_tokens,
1398 output_tokens = usage.output_tokens,
1399 cache_creation_input_tokens = usage.cache_creation_input_tokens,
1400 cache_read_input_tokens = usage.cache_read_input_tokens,
1401 );
1402 self.update_token_usage(usage, cx);
1403 }
1404 StatusUpdate(CompletionRequestStatus::UsageUpdated { amount, limit }) => {
1405 self.update_model_request_usage(amount, limit, cx);
1406 }
1407 StatusUpdate(
1408 CompletionRequestStatus::Started
1409 | CompletionRequestStatus::Queued { .. }
1410 | CompletionRequestStatus::Failed { .. },
1411 ) => {}
1412 StatusUpdate(CompletionRequestStatus::ToolUseLimitReached) => {
1413 self.tool_use_limit_reached = true;
1414 }
1415 Stop(StopReason::Refusal) => return Err(CompletionError::Refusal.into()),
1416 Stop(StopReason::MaxTokens) => return Err(CompletionError::MaxTokens.into()),
1417 Stop(StopReason::ToolUse | StopReason::EndTurn) => {}
1418 }
1419
1420 Ok(None)
1421 }
1422
1423 fn handle_text_event(
1424 &mut self,
1425 new_text: String,
1426 event_stream: &ThreadEventStream,
1427 cx: &mut Context<Self>,
1428 ) {
1429 event_stream.send_text(&new_text);
1430
1431 let last_message = self.pending_message();
1432 if let Some(AgentMessageContent::Text(text)) = last_message.content.last_mut() {
1433 text.push_str(&new_text);
1434 } else {
1435 last_message
1436 .content
1437 .push(AgentMessageContent::Text(new_text));
1438 }
1439
1440 cx.notify();
1441 }
1442
1443 fn handle_thinking_event(
1444 &mut self,
1445 new_text: String,
1446 new_signature: Option<String>,
1447 event_stream: &ThreadEventStream,
1448 cx: &mut Context<Self>,
1449 ) {
1450 event_stream.send_thinking(&new_text);
1451
1452 let last_message = self.pending_message();
1453 if let Some(AgentMessageContent::Thinking { text, signature }) =
1454 last_message.content.last_mut()
1455 {
1456 text.push_str(&new_text);
1457 *signature = new_signature.or(signature.take());
1458 } else {
1459 last_message.content.push(AgentMessageContent::Thinking {
1460 text: new_text,
1461 signature: new_signature,
1462 });
1463 }
1464
1465 cx.notify();
1466 }
1467
1468 fn handle_redacted_thinking_event(&mut self, data: String, cx: &mut Context<Self>) {
1469 let last_message = self.pending_message();
1470 last_message
1471 .content
1472 .push(AgentMessageContent::RedactedThinking(data));
1473 cx.notify();
1474 }
1475
1476 fn handle_tool_use_event(
1477 &mut self,
1478 tool_use: LanguageModelToolUse,
1479 event_stream: &ThreadEventStream,
1480 cx: &mut Context<Self>,
1481 ) -> Option<Task<LanguageModelToolResult>> {
1482 cx.notify();
1483
1484 let tool = self.tool(tool_use.name.as_ref());
1485 let mut title = SharedString::from(&tool_use.name);
1486 let mut kind = acp::ToolKind::Other;
1487 if let Some(tool) = tool.as_ref() {
1488 title = tool.initial_title(tool_use.input.clone());
1489 kind = tool.kind();
1490 }
1491
1492 // Ensure the last message ends in the current tool use
1493 let last_message = self.pending_message();
1494 let push_new_tool_use = last_message.content.last_mut().is_none_or(|content| {
1495 if let AgentMessageContent::ToolUse(last_tool_use) = content {
1496 if last_tool_use.id == tool_use.id {
1497 *last_tool_use = tool_use.clone();
1498 false
1499 } else {
1500 true
1501 }
1502 } else {
1503 true
1504 }
1505 });
1506
1507 if push_new_tool_use {
1508 event_stream.send_tool_call(&tool_use.id, title, kind, tool_use.input.clone());
1509 last_message
1510 .content
1511 .push(AgentMessageContent::ToolUse(tool_use.clone()));
1512 } else {
1513 event_stream.update_tool_call_fields(
1514 &tool_use.id,
1515 acp::ToolCallUpdateFields {
1516 title: Some(title.into()),
1517 kind: Some(kind),
1518 raw_input: Some(tool_use.input.clone()),
1519 ..Default::default()
1520 },
1521 );
1522 }
1523
1524 if !tool_use.is_input_complete {
1525 return None;
1526 }
1527
1528 let Some(tool) = tool else {
1529 let content = format!("No tool named {} exists", tool_use.name);
1530 return Some(Task::ready(LanguageModelToolResult {
1531 content: LanguageModelToolResultContent::Text(Arc::from(content)),
1532 tool_use_id: tool_use.id,
1533 tool_name: tool_use.name,
1534 is_error: true,
1535 output: None,
1536 }));
1537 };
1538
1539 let fs = self.project.read(cx).fs().clone();
1540 let tool_event_stream =
1541 ToolCallEventStream::new(tool_use.id.clone(), event_stream.clone(), Some(fs));
1542 tool_event_stream.update_fields(acp::ToolCallUpdateFields {
1543 status: Some(acp::ToolCallStatus::InProgress),
1544 ..Default::default()
1545 });
1546 let supports_images = self.model().is_some_and(|model| model.supports_images());
1547 let tool_result = tool.run(tool_use.input, tool_event_stream, cx);
1548 log::debug!("Running tool {}", tool_use.name);
1549 Some(cx.foreground_executor().spawn(async move {
1550 let tool_result = tool_result.await.and_then(|output| {
1551 if let LanguageModelToolResultContent::Image(_) = &output.llm_output
1552 && !supports_images
1553 {
1554 return Err(anyhow!(
1555 "Attempted to read an image, but this model doesn't support it.",
1556 ));
1557 }
1558 Ok(output)
1559 });
1560
1561 match tool_result {
1562 Ok(output) => LanguageModelToolResult {
1563 tool_use_id: tool_use.id,
1564 tool_name: tool_use.name,
1565 is_error: false,
1566 content: output.llm_output,
1567 output: Some(output.raw_output),
1568 },
1569 Err(error) => LanguageModelToolResult {
1570 tool_use_id: tool_use.id,
1571 tool_name: tool_use.name,
1572 is_error: true,
1573 content: LanguageModelToolResultContent::Text(Arc::from(error.to_string())),
1574 output: Some(error.to_string().into()),
1575 },
1576 }
1577 }))
1578 }
1579
1580 fn handle_tool_use_json_parse_error_event(
1581 &mut self,
1582 tool_use_id: LanguageModelToolUseId,
1583 tool_name: Arc<str>,
1584 raw_input: Arc<str>,
1585 json_parse_error: String,
1586 ) -> LanguageModelToolResult {
1587 let tool_output = format!("Error parsing input JSON: {json_parse_error}");
1588 LanguageModelToolResult {
1589 tool_use_id,
1590 tool_name,
1591 is_error: true,
1592 content: LanguageModelToolResultContent::Text(tool_output.into()),
1593 output: Some(serde_json::Value::String(raw_input.to_string())),
1594 }
1595 }
1596
1597 fn update_model_request_usage(&self, amount: usize, limit: UsageLimit, cx: &mut Context<Self>) {
1598 self.project
1599 .read(cx)
1600 .user_store()
1601 .update(cx, |user_store, cx| {
1602 user_store.update_model_request_usage(
1603 ModelRequestUsage(RequestUsage {
1604 amount: amount as i32,
1605 limit,
1606 }),
1607 cx,
1608 )
1609 });
1610 }
1611
1612 pub fn title(&self) -> SharedString {
1613 self.title.clone().unwrap_or("New Thread".into())
1614 }
1615
1616 pub fn summary(&mut self, cx: &mut Context<Self>) -> Task<Result<SharedString>> {
1617 if let Some(summary) = self.summary.as_ref() {
1618 return Task::ready(Ok(summary.clone()));
1619 }
1620 let Some(model) = self.summarization_model.clone() else {
1621 return Task::ready(Err(anyhow!("No summarization model available")));
1622 };
1623 let mut request = LanguageModelRequest {
1624 intent: Some(CompletionIntent::ThreadContextSummarization),
1625 temperature: AgentSettings::temperature_for_model(&model, cx),
1626 ..Default::default()
1627 };
1628
1629 for message in &self.messages {
1630 request.messages.extend(message.to_request());
1631 }
1632
1633 request.messages.push(LanguageModelRequestMessage {
1634 role: Role::User,
1635 content: vec![SUMMARIZE_THREAD_DETAILED_PROMPT.into()],
1636 cache: false,
1637 });
1638 cx.spawn(async move |this, cx| {
1639 let mut summary = String::new();
1640 let mut messages = model.stream_completion(request, cx).await?;
1641 while let Some(event) = messages.next().await {
1642 let event = event?;
1643 let text = match event {
1644 LanguageModelCompletionEvent::Text(text) => text,
1645 LanguageModelCompletionEvent::StatusUpdate(
1646 CompletionRequestStatus::UsageUpdated { amount, limit },
1647 ) => {
1648 this.update(cx, |thread, cx| {
1649 thread.update_model_request_usage(amount, limit, cx);
1650 })?;
1651 continue;
1652 }
1653 _ => continue,
1654 };
1655
1656 let mut lines = text.lines();
1657 summary.extend(lines.next());
1658 }
1659
1660 log::debug!("Setting summary: {}", summary);
1661 let summary = SharedString::from(summary);
1662
1663 this.update(cx, |this, cx| {
1664 this.summary = Some(summary.clone());
1665 cx.notify()
1666 })?;
1667
1668 Ok(summary)
1669 })
1670 }
1671
1672 fn generate_title(&mut self, cx: &mut Context<Self>) {
1673 let Some(model) = self.summarization_model.clone() else {
1674 return;
1675 };
1676
1677 log::debug!(
1678 "Generating title with model: {:?}",
1679 self.summarization_model.as_ref().map(|model| model.name())
1680 );
1681 let mut request = LanguageModelRequest {
1682 intent: Some(CompletionIntent::ThreadSummarization),
1683 temperature: AgentSettings::temperature_for_model(&model, cx),
1684 ..Default::default()
1685 };
1686
1687 for message in &self.messages {
1688 request.messages.extend(message.to_request());
1689 }
1690
1691 request.messages.push(LanguageModelRequestMessage {
1692 role: Role::User,
1693 content: vec![SUMMARIZE_THREAD_PROMPT.into()],
1694 cache: false,
1695 });
1696 self.pending_title_generation = Some(cx.spawn(async move |this, cx| {
1697 let mut title = String::new();
1698
1699 let generate = async {
1700 let mut messages = model.stream_completion(request, cx).await?;
1701 while let Some(event) = messages.next().await {
1702 let event = event?;
1703 let text = match event {
1704 LanguageModelCompletionEvent::Text(text) => text,
1705 LanguageModelCompletionEvent::StatusUpdate(
1706 CompletionRequestStatus::UsageUpdated { amount, limit },
1707 ) => {
1708 this.update(cx, |thread, cx| {
1709 thread.update_model_request_usage(amount, limit, cx);
1710 })?;
1711 continue;
1712 }
1713 _ => continue,
1714 };
1715
1716 let mut lines = text.lines();
1717 title.extend(lines.next());
1718
1719 // Stop if the LLM generated multiple lines.
1720 if lines.next().is_some() {
1721 break;
1722 }
1723 }
1724 anyhow::Ok(())
1725 };
1726
1727 if generate.await.context("failed to generate title").is_ok() {
1728 _ = this.update(cx, |this, cx| this.set_title(title.into(), cx));
1729 }
1730 _ = this.update(cx, |this, _| this.pending_title_generation = None);
1731 }));
1732 }
1733
1734 pub fn set_title(&mut self, title: SharedString, cx: &mut Context<Self>) {
1735 self.pending_title_generation = None;
1736 if Some(&title) != self.title.as_ref() {
1737 self.title = Some(title);
1738 cx.emit(TitleUpdated);
1739 cx.notify();
1740 }
1741 }
1742
1743 fn last_user_message(&self) -> Option<&UserMessage> {
1744 self.messages
1745 .iter()
1746 .rev()
1747 .find_map(|message| match message {
1748 Message::User(user_message) => Some(user_message),
1749 Message::Agent(_) => None,
1750 Message::Resume => None,
1751 })
1752 }
1753
1754 fn pending_message(&mut self) -> &mut AgentMessage {
1755 self.pending_message.get_or_insert_default()
1756 }
1757
1758 fn flush_pending_message(&mut self, cx: &mut Context<Self>) {
1759 let Some(mut message) = self.pending_message.take() else {
1760 return;
1761 };
1762
1763 if message.content.is_empty() {
1764 return;
1765 }
1766
1767 for content in &message.content {
1768 let AgentMessageContent::ToolUse(tool_use) = content else {
1769 continue;
1770 };
1771
1772 if !message.tool_results.contains_key(&tool_use.id) {
1773 message.tool_results.insert(
1774 tool_use.id.clone(),
1775 LanguageModelToolResult {
1776 tool_use_id: tool_use.id.clone(),
1777 tool_name: tool_use.name.clone(),
1778 is_error: true,
1779 content: LanguageModelToolResultContent::Text(TOOL_CANCELED_MESSAGE.into()),
1780 output: None,
1781 },
1782 );
1783 }
1784 }
1785
1786 self.messages.push(Message::Agent(message));
1787 self.updated_at = Utc::now();
1788 self.summary = None;
1789 cx.notify()
1790 }
1791
1792 pub(crate) fn build_completion_request(
1793 &self,
1794 completion_intent: CompletionIntent,
1795 cx: &App,
1796 ) -> Result<LanguageModelRequest> {
1797 let model = self.model().context("No language model configured")?;
1798 let tools = if let Some(turn) = self.running_turn.as_ref() {
1799 turn.tools
1800 .iter()
1801 .filter_map(|(tool_name, tool)| {
1802 log::trace!("Including tool: {}", tool_name);
1803 Some(LanguageModelRequestTool {
1804 name: tool_name.to_string(),
1805 description: tool.description().to_string(),
1806 input_schema: tool.input_schema(model.tool_input_format()).log_err()?,
1807 })
1808 })
1809 .collect::<Vec<_>>()
1810 } else {
1811 Vec::new()
1812 };
1813
1814 log::debug!("Building completion request");
1815 log::debug!("Completion intent: {:?}", completion_intent);
1816 log::debug!("Completion mode: {:?}", self.completion_mode);
1817
1818 let messages = self.build_request_messages(cx);
1819 log::debug!("Request will include {} messages", messages.len());
1820 log::debug!("Request includes {} tools", tools.len());
1821
1822 let request = LanguageModelRequest {
1823 thread_id: Some(self.id.to_string()),
1824 prompt_id: Some(self.prompt_id.to_string()),
1825 intent: Some(completion_intent),
1826 mode: Some(self.completion_mode.into()),
1827 messages,
1828 tools,
1829 tool_choice: None,
1830 stop: Vec::new(),
1831 temperature: AgentSettings::temperature_for_model(model, cx),
1832 thinking_allowed: true,
1833 };
1834
1835 log::debug!("Completion request built successfully");
1836 Ok(request)
1837 }
1838
1839 fn enabled_tools(
1840 &self,
1841 profile: &AgentProfileSettings,
1842 model: &Arc<dyn LanguageModel>,
1843 cx: &App,
1844 ) -> BTreeMap<SharedString, Arc<dyn AnyAgentTool>> {
1845 fn truncate(tool_name: &SharedString) -> SharedString {
1846 if tool_name.len() > MAX_TOOL_NAME_LENGTH {
1847 let mut truncated = tool_name.to_string();
1848 truncated.truncate(MAX_TOOL_NAME_LENGTH);
1849 truncated.into()
1850 } else {
1851 tool_name.clone()
1852 }
1853 }
1854
1855 let mut tools = self
1856 .tools
1857 .iter()
1858 .filter_map(|(tool_name, tool)| {
1859 if tool.supported_provider(&model.provider_id())
1860 && profile.is_tool_enabled(tool_name)
1861 {
1862 Some((truncate(tool_name), tool.clone()))
1863 } else {
1864 None
1865 }
1866 })
1867 .collect::<BTreeMap<_, _>>();
1868
1869 let mut context_server_tools = Vec::new();
1870 let mut seen_tools = tools.keys().cloned().collect::<HashSet<_>>();
1871 let mut duplicate_tool_names = HashSet::default();
1872 for (server_id, server_tools) in self.context_server_registry.read(cx).servers() {
1873 for (tool_name, tool) in server_tools {
1874 if profile.is_context_server_tool_enabled(&server_id.0, &tool_name) {
1875 let tool_name = truncate(tool_name);
1876 if !seen_tools.insert(tool_name.clone()) {
1877 duplicate_tool_names.insert(tool_name.clone());
1878 }
1879 context_server_tools.push((server_id.clone(), tool_name, tool.clone()));
1880 }
1881 }
1882 }
1883
1884 // When there are duplicate tool names, disambiguate by prefixing them
1885 // with the server ID. In the rare case there isn't enough space for the
1886 // disambiguated tool name, keep only the last tool with this name.
1887 for (server_id, tool_name, tool) in context_server_tools {
1888 if duplicate_tool_names.contains(&tool_name) {
1889 let available = MAX_TOOL_NAME_LENGTH.saturating_sub(tool_name.len());
1890 if available >= 2 {
1891 let mut disambiguated = server_id.0.to_string();
1892 disambiguated.truncate(available - 1);
1893 disambiguated.push('_');
1894 disambiguated.push_str(&tool_name);
1895 tools.insert(disambiguated.into(), tool.clone());
1896 } else {
1897 tools.insert(tool_name, tool.clone());
1898 }
1899 } else {
1900 tools.insert(tool_name, tool.clone());
1901 }
1902 }
1903
1904 tools
1905 }
1906
1907 fn tool(&self, name: &str) -> Option<Arc<dyn AnyAgentTool>> {
1908 self.running_turn.as_ref()?.tools.get(name).cloned()
1909 }
1910
1911 fn build_request_messages(&self, cx: &App) -> Vec<LanguageModelRequestMessage> {
1912 log::trace!(
1913 "Building request messages from {} thread messages",
1914 self.messages.len()
1915 );
1916
1917 let system_prompt = SystemPromptTemplate {
1918 project: self.project_context.read(cx),
1919 available_tools: self.tools.keys().cloned().collect(),
1920 }
1921 .render(&self.templates)
1922 .context("failed to build system prompt")
1923 .expect("Invalid template");
1924 let mut messages = vec![LanguageModelRequestMessage {
1925 role: Role::System,
1926 content: vec![system_prompt.into()],
1927 cache: false,
1928 }];
1929 for message in &self.messages {
1930 messages.extend(message.to_request());
1931 }
1932
1933 if let Some(last_message) = messages.last_mut() {
1934 last_message.cache = true;
1935 }
1936
1937 if let Some(message) = self.pending_message.as_ref() {
1938 messages.extend(message.to_request());
1939 }
1940
1941 messages
1942 }
1943
1944 pub fn to_markdown(&self) -> String {
1945 let mut markdown = String::new();
1946 for (ix, message) in self.messages.iter().enumerate() {
1947 if ix > 0 {
1948 markdown.push('\n');
1949 }
1950 markdown.push_str(&message.to_markdown());
1951 }
1952
1953 if let Some(message) = self.pending_message.as_ref() {
1954 markdown.push('\n');
1955 markdown.push_str(&message.to_markdown());
1956 }
1957
1958 markdown
1959 }
1960
1961 fn advance_prompt_id(&mut self) {
1962 self.prompt_id = PromptId::new();
1963 }
1964
1965 fn retry_strategy_for(error: &LanguageModelCompletionError) -> Option<RetryStrategy> {
1966 use LanguageModelCompletionError::*;
1967 use http_client::StatusCode;
1968
1969 // General strategy here:
1970 // - If retrying won't help (e.g. invalid API key or payload too large), return None so we don't retry at all.
1971 // - If it's a time-based issue (e.g. server overloaded, rate limit exceeded), retry up to 4 times with exponential backoff.
1972 // - If it's an issue that *might* be fixed by retrying (e.g. internal server error), retry up to 3 times.
1973 match error {
1974 HttpResponseError {
1975 status_code: StatusCode::TOO_MANY_REQUESTS,
1976 ..
1977 } => Some(RetryStrategy::ExponentialBackoff {
1978 initial_delay: BASE_RETRY_DELAY,
1979 max_attempts: MAX_RETRY_ATTEMPTS,
1980 }),
1981 ServerOverloaded { retry_after, .. } | RateLimitExceeded { retry_after, .. } => {
1982 Some(RetryStrategy::Fixed {
1983 delay: retry_after.unwrap_or(BASE_RETRY_DELAY),
1984 max_attempts: MAX_RETRY_ATTEMPTS,
1985 })
1986 }
1987 UpstreamProviderError {
1988 status,
1989 retry_after,
1990 ..
1991 } => match *status {
1992 StatusCode::TOO_MANY_REQUESTS | StatusCode::SERVICE_UNAVAILABLE => {
1993 Some(RetryStrategy::Fixed {
1994 delay: retry_after.unwrap_or(BASE_RETRY_DELAY),
1995 max_attempts: MAX_RETRY_ATTEMPTS,
1996 })
1997 }
1998 StatusCode::INTERNAL_SERVER_ERROR => Some(RetryStrategy::Fixed {
1999 delay: retry_after.unwrap_or(BASE_RETRY_DELAY),
2000 // Internal Server Error could be anything, retry up to 3 times.
2001 max_attempts: 3,
2002 }),
2003 status => {
2004 // There is no StatusCode variant for the unofficial HTTP 529 ("The service is overloaded"),
2005 // but we frequently get them in practice. See https://http.dev/529
2006 if status.as_u16() == 529 {
2007 Some(RetryStrategy::Fixed {
2008 delay: retry_after.unwrap_or(BASE_RETRY_DELAY),
2009 max_attempts: MAX_RETRY_ATTEMPTS,
2010 })
2011 } else {
2012 Some(RetryStrategy::Fixed {
2013 delay: retry_after.unwrap_or(BASE_RETRY_DELAY),
2014 max_attempts: 2,
2015 })
2016 }
2017 }
2018 },
2019 ApiInternalServerError { .. } => Some(RetryStrategy::Fixed {
2020 delay: BASE_RETRY_DELAY,
2021 max_attempts: 3,
2022 }),
2023 ApiReadResponseError { .. }
2024 | HttpSend { .. }
2025 | DeserializeResponse { .. }
2026 | BadRequestFormat { .. } => Some(RetryStrategy::Fixed {
2027 delay: BASE_RETRY_DELAY,
2028 max_attempts: 3,
2029 }),
2030 // Retrying these errors definitely shouldn't help.
2031 HttpResponseError {
2032 status_code:
2033 StatusCode::PAYLOAD_TOO_LARGE | StatusCode::FORBIDDEN | StatusCode::UNAUTHORIZED,
2034 ..
2035 }
2036 | AuthenticationError { .. }
2037 | PermissionError { .. }
2038 | NoApiKey { .. }
2039 | ApiEndpointNotFound { .. }
2040 | PromptTooLarge { .. } => None,
2041 // These errors might be transient, so retry them
2042 SerializeRequest { .. } | BuildRequestBody { .. } => Some(RetryStrategy::Fixed {
2043 delay: BASE_RETRY_DELAY,
2044 max_attempts: 1,
2045 }),
2046 // Retry all other 4xx and 5xx errors once.
2047 HttpResponseError { status_code, .. }
2048 if status_code.is_client_error() || status_code.is_server_error() =>
2049 {
2050 Some(RetryStrategy::Fixed {
2051 delay: BASE_RETRY_DELAY,
2052 max_attempts: 3,
2053 })
2054 }
2055 Other(err)
2056 if err.is::<language_model::PaymentRequiredError>()
2057 || err.is::<language_model::ModelRequestLimitReachedError>() =>
2058 {
2059 // Retrying won't help for Payment Required or Model Request Limit errors (where
2060 // the user must upgrade to usage-based billing to get more requests, or else wait
2061 // for a significant amount of time for the request limit to reset).
2062 None
2063 }
2064 // Conservatively assume that any other errors are non-retryable
2065 HttpResponseError { .. } | Other(..) => Some(RetryStrategy::Fixed {
2066 delay: BASE_RETRY_DELAY,
2067 max_attempts: 2,
2068 }),
2069 }
2070 }
2071}
2072
2073struct RunningTurn {
2074 /// Holds the task that handles agent interaction until the end of the turn.
2075 /// Survives across multiple requests as the model performs tool calls and
2076 /// we run tools, report their results.
2077 _task: Task<()>,
2078 /// The current event stream for the running turn. Used to report a final
2079 /// cancellation event if we cancel the turn.
2080 event_stream: ThreadEventStream,
2081 /// The tools that were enabled for this turn.
2082 tools: BTreeMap<SharedString, Arc<dyn AnyAgentTool>>,
2083}
2084
2085impl RunningTurn {
2086 fn cancel(self) {
2087 log::debug!("Cancelling in progress turn");
2088 self.event_stream.send_canceled();
2089 }
2090}
2091
2092pub struct TokenUsageUpdated(pub Option<acp_thread::TokenUsage>);
2093
2094impl EventEmitter<TokenUsageUpdated> for Thread {}
2095
2096pub struct TitleUpdated;
2097
2098impl EventEmitter<TitleUpdated> for Thread {}
2099
2100pub trait AgentTool
2101where
2102 Self: 'static + Sized,
2103{
2104 type Input: for<'de> Deserialize<'de> + Serialize + JsonSchema;
2105 type Output: for<'de> Deserialize<'de> + Serialize + Into<LanguageModelToolResultContent>;
2106
2107 fn name() -> &'static str;
2108
2109 fn description(&self) -> SharedString {
2110 let schema = schemars::schema_for!(Self::Input);
2111 SharedString::new(
2112 schema
2113 .get("description")
2114 .and_then(|description| description.as_str())
2115 .unwrap_or_default(),
2116 )
2117 }
2118
2119 fn kind() -> acp::ToolKind;
2120
2121 /// The initial tool title to display. Can be updated during the tool run.
2122 fn initial_title(&self, input: Result<Self::Input, serde_json::Value>) -> SharedString;
2123
2124 /// Returns the JSON schema that describes the tool's input.
2125 fn input_schema(&self, format: LanguageModelToolSchemaFormat) -> Schema {
2126 crate::tool_schema::root_schema_for::<Self::Input>(format)
2127 }
2128
2129 /// Some tools rely on a provider for the underlying billing or other reasons.
2130 /// Allow the tool to check if they are compatible, or should be filtered out.
2131 fn supported_provider(&self, _provider: &LanguageModelProviderId) -> bool {
2132 true
2133 }
2134
2135 /// Runs the tool with the provided input.
2136 fn run(
2137 self: Arc<Self>,
2138 input: Self::Input,
2139 event_stream: ToolCallEventStream,
2140 cx: &mut App,
2141 ) -> Task<Result<Self::Output>>;
2142
2143 /// Emits events for a previous execution of the tool.
2144 fn replay(
2145 &self,
2146 _input: Self::Input,
2147 _output: Self::Output,
2148 _event_stream: ToolCallEventStream,
2149 _cx: &mut App,
2150 ) -> Result<()> {
2151 Ok(())
2152 }
2153
2154 fn erase(self) -> Arc<dyn AnyAgentTool> {
2155 Arc::new(Erased(Arc::new(self)))
2156 }
2157}
2158
2159pub struct Erased<T>(T);
2160
2161pub struct AgentToolOutput {
2162 pub llm_output: LanguageModelToolResultContent,
2163 pub raw_output: serde_json::Value,
2164}
2165
2166pub trait AnyAgentTool {
2167 fn name(&self) -> SharedString;
2168 fn description(&self) -> SharedString;
2169 fn kind(&self) -> acp::ToolKind;
2170 fn initial_title(&self, input: serde_json::Value) -> SharedString;
2171 fn input_schema(&self, format: LanguageModelToolSchemaFormat) -> Result<serde_json::Value>;
2172 fn supported_provider(&self, _provider: &LanguageModelProviderId) -> bool {
2173 true
2174 }
2175 fn run(
2176 self: Arc<Self>,
2177 input: serde_json::Value,
2178 event_stream: ToolCallEventStream,
2179 cx: &mut App,
2180 ) -> Task<Result<AgentToolOutput>>;
2181 fn replay(
2182 &self,
2183 input: serde_json::Value,
2184 output: serde_json::Value,
2185 event_stream: ToolCallEventStream,
2186 cx: &mut App,
2187 ) -> Result<()>;
2188}
2189
2190impl<T> AnyAgentTool for Erased<Arc<T>>
2191where
2192 T: AgentTool,
2193{
2194 fn name(&self) -> SharedString {
2195 T::name().into()
2196 }
2197
2198 fn description(&self) -> SharedString {
2199 self.0.description()
2200 }
2201
2202 fn kind(&self) -> agent_client_protocol::ToolKind {
2203 T::kind()
2204 }
2205
2206 fn initial_title(&self, input: serde_json::Value) -> SharedString {
2207 let parsed_input = serde_json::from_value(input.clone()).map_err(|_| input);
2208 self.0.initial_title(parsed_input)
2209 }
2210
2211 fn input_schema(&self, format: LanguageModelToolSchemaFormat) -> Result<serde_json::Value> {
2212 let mut json = serde_json::to_value(self.0.input_schema(format))?;
2213 adapt_schema_to_format(&mut json, format)?;
2214 Ok(json)
2215 }
2216
2217 fn supported_provider(&self, provider: &LanguageModelProviderId) -> bool {
2218 self.0.supported_provider(provider)
2219 }
2220
2221 fn run(
2222 self: Arc<Self>,
2223 input: serde_json::Value,
2224 event_stream: ToolCallEventStream,
2225 cx: &mut App,
2226 ) -> Task<Result<AgentToolOutput>> {
2227 cx.spawn(async move |cx| {
2228 let input = serde_json::from_value(input)?;
2229 let output = cx
2230 .update(|cx| self.0.clone().run(input, event_stream, cx))?
2231 .await?;
2232 let raw_output = serde_json::to_value(&output)?;
2233 Ok(AgentToolOutput {
2234 llm_output: output.into(),
2235 raw_output,
2236 })
2237 })
2238 }
2239
2240 fn replay(
2241 &self,
2242 input: serde_json::Value,
2243 output: serde_json::Value,
2244 event_stream: ToolCallEventStream,
2245 cx: &mut App,
2246 ) -> Result<()> {
2247 let input = serde_json::from_value(input)?;
2248 let output = serde_json::from_value(output)?;
2249 self.0.replay(input, output, event_stream, cx)
2250 }
2251}
2252
2253#[derive(Clone)]
2254struct ThreadEventStream(mpsc::UnboundedSender<Result<ThreadEvent>>);
2255
2256impl ThreadEventStream {
2257 fn send_user_message(&self, message: &UserMessage) {
2258 self.0
2259 .unbounded_send(Ok(ThreadEvent::UserMessage(message.clone())))
2260 .ok();
2261 }
2262
2263 fn send_text(&self, text: &str) {
2264 self.0
2265 .unbounded_send(Ok(ThreadEvent::AgentText(text.to_string())))
2266 .ok();
2267 }
2268
2269 fn send_thinking(&self, text: &str) {
2270 self.0
2271 .unbounded_send(Ok(ThreadEvent::AgentThinking(text.to_string())))
2272 .ok();
2273 }
2274
2275 fn send_tool_call(
2276 &self,
2277 id: &LanguageModelToolUseId,
2278 title: SharedString,
2279 kind: acp::ToolKind,
2280 input: serde_json::Value,
2281 ) {
2282 self.0
2283 .unbounded_send(Ok(ThreadEvent::ToolCall(Self::initial_tool_call(
2284 id,
2285 title.to_string(),
2286 kind,
2287 input,
2288 ))))
2289 .ok();
2290 }
2291
2292 fn initial_tool_call(
2293 id: &LanguageModelToolUseId,
2294 title: String,
2295 kind: acp::ToolKind,
2296 input: serde_json::Value,
2297 ) -> acp::ToolCall {
2298 acp::ToolCall {
2299 id: acp::ToolCallId(id.to_string().into()),
2300 title,
2301 kind,
2302 status: acp::ToolCallStatus::Pending,
2303 content: vec![],
2304 locations: vec![],
2305 raw_input: Some(input),
2306 raw_output: None,
2307 }
2308 }
2309
2310 fn update_tool_call_fields(
2311 &self,
2312 tool_use_id: &LanguageModelToolUseId,
2313 fields: acp::ToolCallUpdateFields,
2314 ) {
2315 self.0
2316 .unbounded_send(Ok(ThreadEvent::ToolCallUpdate(
2317 acp::ToolCallUpdate {
2318 id: acp::ToolCallId(tool_use_id.to_string().into()),
2319 fields,
2320 }
2321 .into(),
2322 )))
2323 .ok();
2324 }
2325
2326 fn send_retry(&self, status: acp_thread::RetryStatus) {
2327 self.0.unbounded_send(Ok(ThreadEvent::Retry(status))).ok();
2328 }
2329
2330 fn send_stop(&self, reason: acp::StopReason) {
2331 self.0.unbounded_send(Ok(ThreadEvent::Stop(reason))).ok();
2332 }
2333
2334 fn send_canceled(&self) {
2335 self.0
2336 .unbounded_send(Ok(ThreadEvent::Stop(acp::StopReason::Cancelled)))
2337 .ok();
2338 }
2339
2340 fn send_error(&self, error: impl Into<anyhow::Error>) {
2341 self.0.unbounded_send(Err(error.into())).ok();
2342 }
2343}
2344
2345#[derive(Clone)]
2346pub struct ToolCallEventStream {
2347 tool_use_id: LanguageModelToolUseId,
2348 stream: ThreadEventStream,
2349 fs: Option<Arc<dyn Fs>>,
2350}
2351
2352impl ToolCallEventStream {
2353 #[cfg(test)]
2354 pub fn test() -> (Self, ToolCallEventStreamReceiver) {
2355 let (events_tx, events_rx) = mpsc::unbounded::<Result<ThreadEvent>>();
2356
2357 let stream = ToolCallEventStream::new("test_id".into(), ThreadEventStream(events_tx), None);
2358
2359 (stream, ToolCallEventStreamReceiver(events_rx))
2360 }
2361
2362 fn new(
2363 tool_use_id: LanguageModelToolUseId,
2364 stream: ThreadEventStream,
2365 fs: Option<Arc<dyn Fs>>,
2366 ) -> Self {
2367 Self {
2368 tool_use_id,
2369 stream,
2370 fs,
2371 }
2372 }
2373
2374 pub fn update_fields(&self, fields: acp::ToolCallUpdateFields) {
2375 self.stream
2376 .update_tool_call_fields(&self.tool_use_id, fields);
2377 }
2378
2379 pub fn update_diff(&self, diff: Entity<acp_thread::Diff>) {
2380 self.stream
2381 .0
2382 .unbounded_send(Ok(ThreadEvent::ToolCallUpdate(
2383 acp_thread::ToolCallUpdateDiff {
2384 id: acp::ToolCallId(self.tool_use_id.to_string().into()),
2385 diff,
2386 }
2387 .into(),
2388 )))
2389 .ok();
2390 }
2391
2392 pub fn update_terminal(&self, terminal: Entity<acp_thread::Terminal>) {
2393 self.stream
2394 .0
2395 .unbounded_send(Ok(ThreadEvent::ToolCallUpdate(
2396 acp_thread::ToolCallUpdateTerminal {
2397 id: acp::ToolCallId(self.tool_use_id.to_string().into()),
2398 terminal,
2399 }
2400 .into(),
2401 )))
2402 .ok();
2403 }
2404
2405 pub fn authorize(&self, title: impl Into<String>, cx: &mut App) -> Task<Result<()>> {
2406 if agent_settings::AgentSettings::get_global(cx).always_allow_tool_actions {
2407 return Task::ready(Ok(()));
2408 }
2409
2410 let (response_tx, response_rx) = oneshot::channel();
2411 self.stream
2412 .0
2413 .unbounded_send(Ok(ThreadEvent::ToolCallAuthorization(
2414 ToolCallAuthorization {
2415 tool_call: acp::ToolCallUpdate {
2416 id: acp::ToolCallId(self.tool_use_id.to_string().into()),
2417 fields: acp::ToolCallUpdateFields {
2418 title: Some(title.into()),
2419 ..Default::default()
2420 },
2421 },
2422 options: vec![
2423 acp::PermissionOption {
2424 id: acp::PermissionOptionId("always_allow".into()),
2425 name: "Always Allow".into(),
2426 kind: acp::PermissionOptionKind::AllowAlways,
2427 },
2428 acp::PermissionOption {
2429 id: acp::PermissionOptionId("allow".into()),
2430 name: "Allow".into(),
2431 kind: acp::PermissionOptionKind::AllowOnce,
2432 },
2433 acp::PermissionOption {
2434 id: acp::PermissionOptionId("deny".into()),
2435 name: "Deny".into(),
2436 kind: acp::PermissionOptionKind::RejectOnce,
2437 },
2438 ],
2439 response: response_tx,
2440 },
2441 )))
2442 .ok();
2443 let fs = self.fs.clone();
2444 cx.spawn(async move |cx| match response_rx.await?.0.as_ref() {
2445 "always_allow" => {
2446 if let Some(fs) = fs.clone() {
2447 cx.update(|cx| {
2448 update_settings_file::<AgentSettings>(fs, cx, |settings, _| {
2449 settings.set_always_allow_tool_actions(true);
2450 });
2451 })?;
2452 }
2453
2454 Ok(())
2455 }
2456 "allow" => Ok(()),
2457 _ => Err(anyhow!("Permission to run tool denied by user")),
2458 })
2459 }
2460}
2461
2462#[cfg(test)]
2463pub struct ToolCallEventStreamReceiver(mpsc::UnboundedReceiver<Result<ThreadEvent>>);
2464
2465#[cfg(test)]
2466impl ToolCallEventStreamReceiver {
2467 pub async fn expect_authorization(&mut self) -> ToolCallAuthorization {
2468 let event = self.0.next().await;
2469 if let Some(Ok(ThreadEvent::ToolCallAuthorization(auth))) = event {
2470 auth
2471 } else {
2472 panic!("Expected ToolCallAuthorization but got: {:?}", event);
2473 }
2474 }
2475
2476 pub async fn expect_update_fields(&mut self) -> acp::ToolCallUpdateFields {
2477 let event = self.0.next().await;
2478 if let Some(Ok(ThreadEvent::ToolCallUpdate(acp_thread::ToolCallUpdate::UpdateFields(
2479 update,
2480 )))) = event
2481 {
2482 update.fields
2483 } else {
2484 panic!("Expected update fields but got: {:?}", event);
2485 }
2486 }
2487
2488 pub async fn expect_diff(&mut self) -> Entity<acp_thread::Diff> {
2489 let event = self.0.next().await;
2490 if let Some(Ok(ThreadEvent::ToolCallUpdate(acp_thread::ToolCallUpdate::UpdateDiff(
2491 update,
2492 )))) = event
2493 {
2494 update.diff
2495 } else {
2496 panic!("Expected diff but got: {:?}", event);
2497 }
2498 }
2499
2500 pub async fn expect_terminal(&mut self) -> Entity<acp_thread::Terminal> {
2501 let event = self.0.next().await;
2502 if let Some(Ok(ThreadEvent::ToolCallUpdate(acp_thread::ToolCallUpdate::UpdateTerminal(
2503 update,
2504 )))) = event
2505 {
2506 update.terminal
2507 } else {
2508 panic!("Expected terminal but got: {:?}", event);
2509 }
2510 }
2511}
2512
2513#[cfg(test)]
2514impl std::ops::Deref for ToolCallEventStreamReceiver {
2515 type Target = mpsc::UnboundedReceiver<Result<ThreadEvent>>;
2516
2517 fn deref(&self) -> &Self::Target {
2518 &self.0
2519 }
2520}
2521
2522#[cfg(test)]
2523impl std::ops::DerefMut for ToolCallEventStreamReceiver {
2524 fn deref_mut(&mut self) -> &mut Self::Target {
2525 &mut self.0
2526 }
2527}
2528
2529impl From<&str> for UserMessageContent {
2530 fn from(text: &str) -> Self {
2531 Self::Text(text.into())
2532 }
2533}
2534
2535impl From<acp::ContentBlock> for UserMessageContent {
2536 fn from(value: acp::ContentBlock) -> Self {
2537 match value {
2538 acp::ContentBlock::Text(text_content) => Self::Text(text_content.text),
2539 acp::ContentBlock::Image(image_content) => Self::Image(convert_image(image_content)),
2540 acp::ContentBlock::Audio(_) => {
2541 // TODO
2542 Self::Text("[audio]".to_string())
2543 }
2544 acp::ContentBlock::ResourceLink(resource_link) => {
2545 match MentionUri::parse(&resource_link.uri) {
2546 Ok(uri) => Self::Mention {
2547 uri,
2548 content: String::new(),
2549 },
2550 Err(err) => {
2551 log::error!("Failed to parse mention link: {}", err);
2552 Self::Text(format!("[{}]({})", resource_link.name, resource_link.uri))
2553 }
2554 }
2555 }
2556 acp::ContentBlock::Resource(resource) => match resource.resource {
2557 acp::EmbeddedResourceResource::TextResourceContents(resource) => {
2558 match MentionUri::parse(&resource.uri) {
2559 Ok(uri) => Self::Mention {
2560 uri,
2561 content: resource.text,
2562 },
2563 Err(err) => {
2564 log::error!("Failed to parse mention link: {}", err);
2565 Self::Text(
2566 MarkdownCodeBlock {
2567 tag: &resource.uri,
2568 text: &resource.text,
2569 }
2570 .to_string(),
2571 )
2572 }
2573 }
2574 }
2575 acp::EmbeddedResourceResource::BlobResourceContents(_) => {
2576 // TODO
2577 Self::Text("[blob]".to_string())
2578 }
2579 },
2580 }
2581 }
2582}
2583
2584impl From<UserMessageContent> for acp::ContentBlock {
2585 fn from(content: UserMessageContent) -> Self {
2586 match content {
2587 UserMessageContent::Text(text) => acp::ContentBlock::Text(acp::TextContent {
2588 text,
2589 annotations: None,
2590 }),
2591 UserMessageContent::Image(image) => acp::ContentBlock::Image(acp::ImageContent {
2592 data: image.source.to_string(),
2593 mime_type: "image/png".to_string(),
2594 annotations: None,
2595 uri: None,
2596 }),
2597 UserMessageContent::Mention { uri, content } => {
2598 acp::ContentBlock::Resource(acp::EmbeddedResource {
2599 resource: acp::EmbeddedResourceResource::TextResourceContents(
2600 acp::TextResourceContents {
2601 mime_type: None,
2602 text: content,
2603 uri: uri.to_uri().to_string(),
2604 },
2605 ),
2606 annotations: None,
2607 })
2608 }
2609 }
2610 }
2611}
2612
2613fn convert_image(image_content: acp::ImageContent) -> LanguageModelImage {
2614 LanguageModelImage {
2615 source: image_content.data.into(),
2616 // TODO: make this optional?
2617 size: gpui::Size::new(0.into(), 0.into()),
2618 }
2619}