1use crate::{
2 agent_profile::AgentProfile,
3 context::{AgentContext, AgentContextHandle, ContextLoadResult, LoadedContext},
4 thread_store::{
5 SerializedCrease, SerializedLanguageModel, SerializedMessage, SerializedMessageSegment,
6 SerializedThread, SerializedToolResult, SerializedToolUse, SharedProjectContext,
7 ThreadStore,
8 },
9 tool_use::{PendingToolUse, ToolUse, ToolUseMetadata, ToolUseState},
10};
11use agent_settings::{AgentProfileId, AgentSettings, CompletionMode};
12use anyhow::{Result, anyhow};
13use assistant_tool::{ActionLog, AnyToolCard, Tool, ToolWorkingSet};
14use chrono::{DateTime, Utc};
15use client::{ModelRequestUsage, RequestUsage};
16use collections::{HashMap, HashSet};
17use feature_flags::{self, FeatureFlagAppExt};
18use futures::{FutureExt, StreamExt as _, future::Shared};
19use git::repository::DiffType;
20use gpui::{
21 AnyWindowHandle, App, AppContext, AsyncApp, Context, Entity, EventEmitter, SharedString, Task,
22 WeakEntity, Window,
23};
24use language_model::{
25 ConfiguredModel, LanguageModel, LanguageModelCompletionError, LanguageModelCompletionEvent,
26 LanguageModelId, LanguageModelKnownError, LanguageModelRegistry, LanguageModelRequest,
27 LanguageModelRequestMessage, LanguageModelRequestTool, LanguageModelToolResult,
28 LanguageModelToolResultContent, LanguageModelToolUseId, MessageContent,
29 ModelRequestLimitReachedError, PaymentRequiredError, Role, SelectedModel, StopReason,
30 TokenUsage,
31};
32use postage::stream::Stream as _;
33use project::{
34 Project,
35 git_store::{GitStore, GitStoreCheckpoint, RepositoryState},
36};
37use prompt_store::{ModelContext, PromptBuilder};
38use proto::Plan;
39use schemars::JsonSchema;
40use serde::{Deserialize, Serialize};
41use settings::Settings;
42use std::{io::Write, ops::Range, sync::Arc, time::Instant};
43use thiserror::Error;
44use util::{ResultExt as _, post_inc};
45use uuid::Uuid;
46use zed_llm_client::{CompletionIntent, CompletionRequestStatus, UsageLimit};
47
48#[derive(
49 Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Serialize, Deserialize, JsonSchema,
50)]
51pub struct ThreadId(Arc<str>);
52
53impl ThreadId {
54 pub fn new() -> Self {
55 Self(Uuid::new_v4().to_string().into())
56 }
57}
58
59impl std::fmt::Display for ThreadId {
60 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
61 write!(f, "{}", self.0)
62 }
63}
64
65impl From<&str> for ThreadId {
66 fn from(value: &str) -> Self {
67 Self(value.into())
68 }
69}
70
71/// The ID of the user prompt that initiated a request.
72///
73/// This equates to the user physically submitting a message to the model (e.g., by pressing the Enter key).
74#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Serialize, Deserialize)]
75pub struct PromptId(Arc<str>);
76
77impl PromptId {
78 pub fn new() -> Self {
79 Self(Uuid::new_v4().to_string().into())
80 }
81}
82
83impl std::fmt::Display for PromptId {
84 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
85 write!(f, "{}", self.0)
86 }
87}
88
89#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, Serialize, Deserialize)]
90pub struct MessageId(pub(crate) usize);
91
92impl MessageId {
93 fn post_inc(&mut self) -> Self {
94 Self(post_inc(&mut self.0))
95 }
96
97 pub fn as_usize(&self) -> usize {
98 self.0
99 }
100}
101
102/// Stored information that can be used to resurrect a context crease when creating an editor for a past message.
103#[derive(Clone, Debug)]
104pub struct MessageCrease {
105 pub range: Range<usize>,
106 pub icon_path: SharedString,
107 pub label: SharedString,
108 /// None for a deserialized message, Some otherwise.
109 pub context: Option<AgentContextHandle>,
110}
111
112/// A message in a [`Thread`].
113#[derive(Debug, Clone)]
114pub struct Message {
115 pub id: MessageId,
116 pub role: Role,
117 pub segments: Vec<MessageSegment>,
118 pub loaded_context: LoadedContext,
119 pub creases: Vec<MessageCrease>,
120 pub is_hidden: bool,
121}
122
123impl Message {
124 /// Returns whether the message contains any meaningful text that should be displayed
125 /// The model sometimes runs tool without producing any text or just a marker ([`USING_TOOL_MARKER`])
126 pub fn should_display_content(&self) -> bool {
127 self.segments.iter().all(|segment| segment.should_display())
128 }
129
130 pub fn push_thinking(&mut self, text: &str, signature: Option<String>) {
131 if let Some(MessageSegment::Thinking {
132 text: segment,
133 signature: current_signature,
134 }) = self.segments.last_mut()
135 {
136 if let Some(signature) = signature {
137 *current_signature = Some(signature);
138 }
139 segment.push_str(text);
140 } else {
141 self.segments.push(MessageSegment::Thinking {
142 text: text.to_string(),
143 signature,
144 });
145 }
146 }
147
148 pub fn push_redacted_thinking(&mut self, data: String) {
149 self.segments.push(MessageSegment::RedactedThinking(data));
150 }
151
152 pub fn push_text(&mut self, text: &str) {
153 if let Some(MessageSegment::Text(segment)) = self.segments.last_mut() {
154 segment.push_str(text);
155 } else {
156 self.segments.push(MessageSegment::Text(text.to_string()));
157 }
158 }
159
160 pub fn to_string(&self) -> String {
161 let mut result = String::new();
162
163 if !self.loaded_context.text.is_empty() {
164 result.push_str(&self.loaded_context.text);
165 }
166
167 for segment in &self.segments {
168 match segment {
169 MessageSegment::Text(text) => result.push_str(text),
170 MessageSegment::Thinking { text, .. } => {
171 result.push_str("<think>\n");
172 result.push_str(text);
173 result.push_str("\n</think>");
174 }
175 MessageSegment::RedactedThinking(_) => {}
176 }
177 }
178
179 result
180 }
181}
182
183#[derive(Debug, Clone, PartialEq, Eq)]
184pub enum MessageSegment {
185 Text(String),
186 Thinking {
187 text: String,
188 signature: Option<String>,
189 },
190 RedactedThinking(String),
191}
192
193impl MessageSegment {
194 pub fn should_display(&self) -> bool {
195 match self {
196 Self::Text(text) => text.is_empty(),
197 Self::Thinking { text, .. } => text.is_empty(),
198 Self::RedactedThinking(_) => false,
199 }
200 }
201
202 pub fn text(&self) -> Option<&str> {
203 match self {
204 MessageSegment::Text(text) => Some(text),
205 _ => None,
206 }
207 }
208}
209
210#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
211pub struct ProjectSnapshot {
212 pub worktree_snapshots: Vec<WorktreeSnapshot>,
213 pub unsaved_buffer_paths: Vec<String>,
214 pub timestamp: DateTime<Utc>,
215}
216
217#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
218pub struct WorktreeSnapshot {
219 pub worktree_path: String,
220 pub git_state: Option<GitState>,
221}
222
223#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
224pub struct GitState {
225 pub remote_url: Option<String>,
226 pub head_sha: Option<String>,
227 pub current_branch: Option<String>,
228 pub diff: Option<String>,
229}
230
231#[derive(Clone, Debug)]
232pub struct ThreadCheckpoint {
233 message_id: MessageId,
234 git_checkpoint: GitStoreCheckpoint,
235}
236
237#[derive(Copy, Clone, Debug, PartialEq, Eq)]
238pub enum ThreadFeedback {
239 Positive,
240 Negative,
241}
242
243pub enum LastRestoreCheckpoint {
244 Pending {
245 message_id: MessageId,
246 },
247 Error {
248 message_id: MessageId,
249 error: String,
250 },
251}
252
253impl LastRestoreCheckpoint {
254 pub fn message_id(&self) -> MessageId {
255 match self {
256 LastRestoreCheckpoint::Pending { message_id } => *message_id,
257 LastRestoreCheckpoint::Error { message_id, .. } => *message_id,
258 }
259 }
260}
261
262#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
263pub enum DetailedSummaryState {
264 #[default]
265 NotGenerated,
266 Generating {
267 message_id: MessageId,
268 },
269 Generated {
270 text: SharedString,
271 message_id: MessageId,
272 },
273}
274
275impl DetailedSummaryState {
276 fn text(&self) -> Option<SharedString> {
277 if let Self::Generated { text, .. } = self {
278 Some(text.clone())
279 } else {
280 None
281 }
282 }
283}
284
285#[derive(Default, Debug)]
286pub struct TotalTokenUsage {
287 pub total: u64,
288 pub max: u64,
289}
290
291impl TotalTokenUsage {
292 pub fn ratio(&self) -> TokenUsageRatio {
293 #[cfg(debug_assertions)]
294 let warning_threshold: f32 = std::env::var("ZED_THREAD_WARNING_THRESHOLD")
295 .unwrap_or("0.8".to_string())
296 .parse()
297 .unwrap();
298 #[cfg(not(debug_assertions))]
299 let warning_threshold: f32 = 0.8;
300
301 // When the maximum is unknown because there is no selected model,
302 // avoid showing the token limit warning.
303 if self.max == 0 {
304 TokenUsageRatio::Normal
305 } else if self.total >= self.max {
306 TokenUsageRatio::Exceeded
307 } else if self.total as f32 / self.max as f32 >= warning_threshold {
308 TokenUsageRatio::Warning
309 } else {
310 TokenUsageRatio::Normal
311 }
312 }
313
314 pub fn add(&self, tokens: u64) -> TotalTokenUsage {
315 TotalTokenUsage {
316 total: self.total + tokens,
317 max: self.max,
318 }
319 }
320}
321
322#[derive(Debug, Default, PartialEq, Eq)]
323pub enum TokenUsageRatio {
324 #[default]
325 Normal,
326 Warning,
327 Exceeded,
328}
329
330#[derive(Debug, Clone, Copy)]
331pub enum QueueState {
332 Sending,
333 Queued { position: usize },
334 Started,
335}
336
337/// A thread of conversation with the LLM.
338pub struct Thread {
339 id: ThreadId,
340 updated_at: DateTime<Utc>,
341 summary: ThreadSummary,
342 pending_summary: Task<Option<()>>,
343 detailed_summary_task: Task<Option<()>>,
344 detailed_summary_tx: postage::watch::Sender<DetailedSummaryState>,
345 detailed_summary_rx: postage::watch::Receiver<DetailedSummaryState>,
346 completion_mode: agent_settings::CompletionMode,
347 messages: Vec<Message>,
348 next_message_id: MessageId,
349 last_prompt_id: PromptId,
350 project_context: SharedProjectContext,
351 checkpoints_by_message: HashMap<MessageId, ThreadCheckpoint>,
352 completion_count: usize,
353 pending_completions: Vec<PendingCompletion>,
354 project: Entity<Project>,
355 prompt_builder: Arc<PromptBuilder>,
356 tools: Entity<ToolWorkingSet>,
357 tool_use: ToolUseState,
358 action_log: Entity<ActionLog>,
359 last_restore_checkpoint: Option<LastRestoreCheckpoint>,
360 pending_checkpoint: Option<ThreadCheckpoint>,
361 initial_project_snapshot: Shared<Task<Option<Arc<ProjectSnapshot>>>>,
362 request_token_usage: Vec<TokenUsage>,
363 cumulative_token_usage: TokenUsage,
364 exceeded_window_error: Option<ExceededWindowError>,
365 tool_use_limit_reached: bool,
366 feedback: Option<ThreadFeedback>,
367 message_feedback: HashMap<MessageId, ThreadFeedback>,
368 last_auto_capture_at: Option<Instant>,
369 last_received_chunk_at: Option<Instant>,
370 request_callback: Option<
371 Box<dyn FnMut(&LanguageModelRequest, &[Result<LanguageModelCompletionEvent, String>])>,
372 >,
373 remaining_turns: u32,
374 configured_model: Option<ConfiguredModel>,
375 profile: AgentProfile,
376}
377
378#[derive(Clone, Debug, PartialEq, Eq)]
379pub enum ThreadSummary {
380 Pending,
381 Generating,
382 Ready(SharedString),
383 Error,
384}
385
386impl ThreadSummary {
387 pub const DEFAULT: SharedString = SharedString::new_static("New Thread");
388
389 pub fn or_default(&self) -> SharedString {
390 self.unwrap_or(Self::DEFAULT)
391 }
392
393 pub fn unwrap_or(&self, message: impl Into<SharedString>) -> SharedString {
394 self.ready().unwrap_or_else(|| message.into())
395 }
396
397 pub fn ready(&self) -> Option<SharedString> {
398 match self {
399 ThreadSummary::Ready(summary) => Some(summary.clone()),
400 ThreadSummary::Pending | ThreadSummary::Generating | ThreadSummary::Error => None,
401 }
402 }
403}
404
405#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
406pub struct ExceededWindowError {
407 /// Model used when last message exceeded context window
408 model_id: LanguageModelId,
409 /// Token count including last message
410 token_count: u64,
411}
412
413impl Thread {
414 pub fn new(
415 project: Entity<Project>,
416 tools: Entity<ToolWorkingSet>,
417 prompt_builder: Arc<PromptBuilder>,
418 system_prompt: SharedProjectContext,
419 cx: &mut Context<Self>,
420 ) -> Self {
421 let (detailed_summary_tx, detailed_summary_rx) = postage::watch::channel();
422 let configured_model = LanguageModelRegistry::read_global(cx).default_model();
423 let profile_id = AgentSettings::get_global(cx).default_profile.clone();
424
425 Self {
426 id: ThreadId::new(),
427 updated_at: Utc::now(),
428 summary: ThreadSummary::Pending,
429 pending_summary: Task::ready(None),
430 detailed_summary_task: Task::ready(None),
431 detailed_summary_tx,
432 detailed_summary_rx,
433 completion_mode: AgentSettings::get_global(cx).preferred_completion_mode,
434 messages: Vec::new(),
435 next_message_id: MessageId(0),
436 last_prompt_id: PromptId::new(),
437 project_context: system_prompt,
438 checkpoints_by_message: HashMap::default(),
439 completion_count: 0,
440 pending_completions: Vec::new(),
441 project: project.clone(),
442 prompt_builder,
443 tools: tools.clone(),
444 last_restore_checkpoint: None,
445 pending_checkpoint: None,
446 tool_use: ToolUseState::new(tools.clone()),
447 action_log: cx.new(|_| ActionLog::new(project.clone())),
448 initial_project_snapshot: {
449 let project_snapshot = Self::project_snapshot(project, cx);
450 cx.foreground_executor()
451 .spawn(async move { Some(project_snapshot.await) })
452 .shared()
453 },
454 request_token_usage: Vec::new(),
455 cumulative_token_usage: TokenUsage::default(),
456 exceeded_window_error: None,
457 tool_use_limit_reached: false,
458 feedback: None,
459 message_feedback: HashMap::default(),
460 last_auto_capture_at: None,
461 last_received_chunk_at: None,
462 request_callback: None,
463 remaining_turns: u32::MAX,
464 configured_model,
465 profile: AgentProfile::new(profile_id, tools),
466 }
467 }
468
469 pub fn deserialize(
470 id: ThreadId,
471 serialized: SerializedThread,
472 project: Entity<Project>,
473 tools: Entity<ToolWorkingSet>,
474 prompt_builder: Arc<PromptBuilder>,
475 project_context: SharedProjectContext,
476 window: Option<&mut Window>, // None in headless mode
477 cx: &mut Context<Self>,
478 ) -> Self {
479 let next_message_id = MessageId(
480 serialized
481 .messages
482 .last()
483 .map(|message| message.id.0 + 1)
484 .unwrap_or(0),
485 );
486 let tool_use = ToolUseState::from_serialized_messages(
487 tools.clone(),
488 &serialized.messages,
489 project.clone(),
490 window,
491 cx,
492 );
493 let (detailed_summary_tx, detailed_summary_rx) =
494 postage::watch::channel_with(serialized.detailed_summary_state);
495
496 let configured_model = LanguageModelRegistry::global(cx).update(cx, |registry, cx| {
497 serialized
498 .model
499 .and_then(|model| {
500 let model = SelectedModel {
501 provider: model.provider.clone().into(),
502 model: model.model.clone().into(),
503 };
504 registry.select_model(&model, cx)
505 })
506 .or_else(|| registry.default_model())
507 });
508
509 let completion_mode = serialized
510 .completion_mode
511 .unwrap_or_else(|| AgentSettings::get_global(cx).preferred_completion_mode);
512 let profile_id = serialized
513 .profile
514 .unwrap_or_else(|| AgentSettings::get_global(cx).default_profile.clone());
515
516 Self {
517 id,
518 updated_at: serialized.updated_at,
519 summary: ThreadSummary::Ready(serialized.summary),
520 pending_summary: Task::ready(None),
521 detailed_summary_task: Task::ready(None),
522 detailed_summary_tx,
523 detailed_summary_rx,
524 completion_mode,
525 messages: serialized
526 .messages
527 .into_iter()
528 .map(|message| Message {
529 id: message.id,
530 role: message.role,
531 segments: message
532 .segments
533 .into_iter()
534 .map(|segment| match segment {
535 SerializedMessageSegment::Text { text } => MessageSegment::Text(text),
536 SerializedMessageSegment::Thinking { text, signature } => {
537 MessageSegment::Thinking { text, signature }
538 }
539 SerializedMessageSegment::RedactedThinking { data } => {
540 MessageSegment::RedactedThinking(data)
541 }
542 })
543 .collect(),
544 loaded_context: LoadedContext {
545 contexts: Vec::new(),
546 text: message.context,
547 images: Vec::new(),
548 },
549 creases: message
550 .creases
551 .into_iter()
552 .map(|crease| MessageCrease {
553 range: crease.start..crease.end,
554 icon_path: crease.icon_path,
555 label: crease.label,
556 context: None,
557 })
558 .collect(),
559 is_hidden: message.is_hidden,
560 })
561 .collect(),
562 next_message_id,
563 last_prompt_id: PromptId::new(),
564 project_context,
565 checkpoints_by_message: HashMap::default(),
566 completion_count: 0,
567 pending_completions: Vec::new(),
568 last_restore_checkpoint: None,
569 pending_checkpoint: None,
570 project: project.clone(),
571 prompt_builder,
572 tools: tools.clone(),
573 tool_use,
574 action_log: cx.new(|_| ActionLog::new(project)),
575 initial_project_snapshot: Task::ready(serialized.initial_project_snapshot).shared(),
576 request_token_usage: serialized.request_token_usage,
577 cumulative_token_usage: serialized.cumulative_token_usage,
578 exceeded_window_error: None,
579 tool_use_limit_reached: serialized.tool_use_limit_reached,
580 feedback: None,
581 message_feedback: HashMap::default(),
582 last_auto_capture_at: None,
583 last_received_chunk_at: None,
584 request_callback: None,
585 remaining_turns: u32::MAX,
586 configured_model,
587 profile: AgentProfile::new(profile_id, tools),
588 }
589 }
590
591 pub fn set_request_callback(
592 &mut self,
593 callback: impl 'static
594 + FnMut(&LanguageModelRequest, &[Result<LanguageModelCompletionEvent, String>]),
595 ) {
596 self.request_callback = Some(Box::new(callback));
597 }
598
599 pub fn id(&self) -> &ThreadId {
600 &self.id
601 }
602
603 pub fn profile(&self) -> &AgentProfile {
604 &self.profile
605 }
606
607 pub fn set_profile(&mut self, id: AgentProfileId, cx: &mut Context<Self>) {
608 if &id != self.profile.id() {
609 self.profile = AgentProfile::new(id, self.tools.clone());
610 cx.emit(ThreadEvent::ProfileChanged);
611 }
612 }
613
614 pub fn is_empty(&self) -> bool {
615 self.messages.is_empty()
616 }
617
618 pub fn updated_at(&self) -> DateTime<Utc> {
619 self.updated_at
620 }
621
622 pub fn touch_updated_at(&mut self) {
623 self.updated_at = Utc::now();
624 }
625
626 pub fn advance_prompt_id(&mut self) {
627 self.last_prompt_id = PromptId::new();
628 }
629
630 pub fn project_context(&self) -> SharedProjectContext {
631 self.project_context.clone()
632 }
633
634 pub fn get_or_init_configured_model(&mut self, cx: &App) -> Option<ConfiguredModel> {
635 if self.configured_model.is_none() {
636 self.configured_model = LanguageModelRegistry::read_global(cx).default_model();
637 }
638 self.configured_model.clone()
639 }
640
641 pub fn configured_model(&self) -> Option<ConfiguredModel> {
642 self.configured_model.clone()
643 }
644
645 pub fn set_configured_model(&mut self, model: Option<ConfiguredModel>, cx: &mut Context<Self>) {
646 self.configured_model = model;
647 cx.notify();
648 }
649
650 pub fn summary(&self) -> &ThreadSummary {
651 &self.summary
652 }
653
654 pub fn set_summary(&mut self, new_summary: impl Into<SharedString>, cx: &mut Context<Self>) {
655 let current_summary = match &self.summary {
656 ThreadSummary::Pending | ThreadSummary::Generating => return,
657 ThreadSummary::Ready(summary) => summary,
658 ThreadSummary::Error => &ThreadSummary::DEFAULT,
659 };
660
661 let mut new_summary = new_summary.into();
662
663 if new_summary.is_empty() {
664 new_summary = ThreadSummary::DEFAULT;
665 }
666
667 if current_summary != &new_summary {
668 self.summary = ThreadSummary::Ready(new_summary);
669 cx.emit(ThreadEvent::SummaryChanged);
670 }
671 }
672
673 pub fn completion_mode(&self) -> CompletionMode {
674 self.completion_mode
675 }
676
677 pub fn set_completion_mode(&mut self, mode: CompletionMode) {
678 self.completion_mode = mode;
679 }
680
681 pub fn message(&self, id: MessageId) -> Option<&Message> {
682 let index = self
683 .messages
684 .binary_search_by(|message| message.id.cmp(&id))
685 .ok()?;
686
687 self.messages.get(index)
688 }
689
690 pub fn messages(&self) -> impl ExactSizeIterator<Item = &Message> {
691 self.messages.iter()
692 }
693
694 pub fn is_generating(&self) -> bool {
695 !self.pending_completions.is_empty() || !self.all_tools_finished()
696 }
697
698 /// Indicates whether streaming of language model events is stale.
699 /// When `is_generating()` is false, this method returns `None`.
700 pub fn is_generation_stale(&self) -> Option<bool> {
701 const STALE_THRESHOLD: u128 = 250;
702
703 self.last_received_chunk_at
704 .map(|instant| instant.elapsed().as_millis() > STALE_THRESHOLD)
705 }
706
707 fn received_chunk(&mut self) {
708 self.last_received_chunk_at = Some(Instant::now());
709 }
710
711 pub fn queue_state(&self) -> Option<QueueState> {
712 self.pending_completions
713 .first()
714 .map(|pending_completion| pending_completion.queue_state)
715 }
716
717 pub fn tools(&self) -> &Entity<ToolWorkingSet> {
718 &self.tools
719 }
720
721 pub fn pending_tool(&self, id: &LanguageModelToolUseId) -> Option<&PendingToolUse> {
722 self.tool_use
723 .pending_tool_uses()
724 .into_iter()
725 .find(|tool_use| &tool_use.id == id)
726 }
727
728 pub fn tools_needing_confirmation(&self) -> impl Iterator<Item = &PendingToolUse> {
729 self.tool_use
730 .pending_tool_uses()
731 .into_iter()
732 .filter(|tool_use| tool_use.status.needs_confirmation())
733 }
734
735 pub fn has_pending_tool_uses(&self) -> bool {
736 !self.tool_use.pending_tool_uses().is_empty()
737 }
738
739 pub fn checkpoint_for_message(&self, id: MessageId) -> Option<ThreadCheckpoint> {
740 self.checkpoints_by_message.get(&id).cloned()
741 }
742
743 pub fn restore_checkpoint(
744 &mut self,
745 checkpoint: ThreadCheckpoint,
746 cx: &mut Context<Self>,
747 ) -> Task<Result<()>> {
748 self.last_restore_checkpoint = Some(LastRestoreCheckpoint::Pending {
749 message_id: checkpoint.message_id,
750 });
751 cx.emit(ThreadEvent::CheckpointChanged);
752 cx.notify();
753
754 let git_store = self.project().read(cx).git_store().clone();
755 let restore = git_store.update(cx, |git_store, cx| {
756 git_store.restore_checkpoint(checkpoint.git_checkpoint.clone(), cx)
757 });
758
759 cx.spawn(async move |this, cx| {
760 let result = restore.await;
761 this.update(cx, |this, cx| {
762 if let Err(err) = result.as_ref() {
763 this.last_restore_checkpoint = Some(LastRestoreCheckpoint::Error {
764 message_id: checkpoint.message_id,
765 error: err.to_string(),
766 });
767 } else {
768 this.truncate(checkpoint.message_id, cx);
769 this.last_restore_checkpoint = None;
770 }
771 this.pending_checkpoint = None;
772 cx.emit(ThreadEvent::CheckpointChanged);
773 cx.notify();
774 })?;
775 result
776 })
777 }
778
779 fn finalize_pending_checkpoint(&mut self, cx: &mut Context<Self>) {
780 let pending_checkpoint = if self.is_generating() {
781 return;
782 } else if let Some(checkpoint) = self.pending_checkpoint.take() {
783 checkpoint
784 } else {
785 return;
786 };
787
788 self.finalize_checkpoint(pending_checkpoint, cx);
789 }
790
791 fn finalize_checkpoint(
792 &mut self,
793 pending_checkpoint: ThreadCheckpoint,
794 cx: &mut Context<Self>,
795 ) {
796 let git_store = self.project.read(cx).git_store().clone();
797 let final_checkpoint = git_store.update(cx, |git_store, cx| git_store.checkpoint(cx));
798 cx.spawn(async move |this, cx| match final_checkpoint.await {
799 Ok(final_checkpoint) => {
800 let equal = git_store
801 .update(cx, |store, cx| {
802 store.compare_checkpoints(
803 pending_checkpoint.git_checkpoint.clone(),
804 final_checkpoint.clone(),
805 cx,
806 )
807 })?
808 .await
809 .unwrap_or(false);
810
811 if !equal {
812 this.update(cx, |this, cx| {
813 this.insert_checkpoint(pending_checkpoint, cx)
814 })?;
815 }
816
817 Ok(())
818 }
819 Err(_) => this.update(cx, |this, cx| {
820 this.insert_checkpoint(pending_checkpoint, cx)
821 }),
822 })
823 .detach();
824 }
825
826 fn insert_checkpoint(&mut self, checkpoint: ThreadCheckpoint, cx: &mut Context<Self>) {
827 self.checkpoints_by_message
828 .insert(checkpoint.message_id, checkpoint);
829 cx.emit(ThreadEvent::CheckpointChanged);
830 cx.notify();
831 }
832
833 pub fn last_restore_checkpoint(&self) -> Option<&LastRestoreCheckpoint> {
834 self.last_restore_checkpoint.as_ref()
835 }
836
837 pub fn truncate(&mut self, message_id: MessageId, cx: &mut Context<Self>) {
838 let Some(message_ix) = self
839 .messages
840 .iter()
841 .rposition(|message| message.id == message_id)
842 else {
843 return;
844 };
845 for deleted_message in self.messages.drain(message_ix..) {
846 self.checkpoints_by_message.remove(&deleted_message.id);
847 }
848 cx.notify();
849 }
850
851 pub fn context_for_message(&self, id: MessageId) -> impl Iterator<Item = &AgentContext> {
852 self.messages
853 .iter()
854 .find(|message| message.id == id)
855 .into_iter()
856 .flat_map(|message| message.loaded_context.contexts.iter())
857 }
858
859 pub fn is_turn_end(&self, ix: usize) -> bool {
860 if self.messages.is_empty() {
861 return false;
862 }
863
864 if !self.is_generating() && ix == self.messages.len() - 1 {
865 return true;
866 }
867
868 let Some(message) = self.messages.get(ix) else {
869 return false;
870 };
871
872 if message.role != Role::Assistant {
873 return false;
874 }
875
876 self.messages
877 .get(ix + 1)
878 .and_then(|message| {
879 self.message(message.id)
880 .map(|next_message| next_message.role == Role::User && !next_message.is_hidden)
881 })
882 .unwrap_or(false)
883 }
884
885 pub fn tool_use_limit_reached(&self) -> bool {
886 self.tool_use_limit_reached
887 }
888
889 /// Returns whether all of the tool uses have finished running.
890 pub fn all_tools_finished(&self) -> bool {
891 // If the only pending tool uses left are the ones with errors, then
892 // that means that we've finished running all of the pending tools.
893 self.tool_use
894 .pending_tool_uses()
895 .iter()
896 .all(|pending_tool_use| pending_tool_use.status.is_error())
897 }
898
899 /// Returns whether any pending tool uses may perform edits
900 pub fn has_pending_edit_tool_uses(&self) -> bool {
901 self.tool_use
902 .pending_tool_uses()
903 .iter()
904 .filter(|pending_tool_use| !pending_tool_use.status.is_error())
905 .any(|pending_tool_use| pending_tool_use.may_perform_edits)
906 }
907
908 pub fn tool_uses_for_message(&self, id: MessageId, cx: &App) -> Vec<ToolUse> {
909 self.tool_use.tool_uses_for_message(id, cx)
910 }
911
912 pub fn tool_results_for_message(
913 &self,
914 assistant_message_id: MessageId,
915 ) -> Vec<&LanguageModelToolResult> {
916 self.tool_use.tool_results_for_message(assistant_message_id)
917 }
918
919 pub fn tool_result(&self, id: &LanguageModelToolUseId) -> Option<&LanguageModelToolResult> {
920 self.tool_use.tool_result(id)
921 }
922
923 pub fn output_for_tool(&self, id: &LanguageModelToolUseId) -> Option<&Arc<str>> {
924 match &self.tool_use.tool_result(id)?.content {
925 LanguageModelToolResultContent::Text(text) => Some(text),
926 LanguageModelToolResultContent::Image(_) => {
927 // TODO: We should display image
928 None
929 }
930 }
931 }
932
933 pub fn card_for_tool(&self, id: &LanguageModelToolUseId) -> Option<AnyToolCard> {
934 self.tool_use.tool_result_card(id).cloned()
935 }
936
937 /// Return tools that are both enabled and supported by the model
938 pub fn available_tools(
939 &self,
940 cx: &App,
941 model: Arc<dyn LanguageModel>,
942 ) -> Vec<LanguageModelRequestTool> {
943 if model.supports_tools() {
944 resolve_tool_name_conflicts(self.profile.enabled_tools(cx).as_slice())
945 .into_iter()
946 .filter_map(|(name, tool)| {
947 // Skip tools that cannot be supported
948 let input_schema = tool.input_schema(model.tool_input_format()).ok()?;
949 Some(LanguageModelRequestTool {
950 name,
951 description: tool.description(),
952 input_schema,
953 })
954 })
955 .collect()
956 } else {
957 Vec::default()
958 }
959 }
960
961 pub fn insert_user_message(
962 &mut self,
963 text: impl Into<String>,
964 loaded_context: ContextLoadResult,
965 git_checkpoint: Option<GitStoreCheckpoint>,
966 creases: Vec<MessageCrease>,
967 cx: &mut Context<Self>,
968 ) -> MessageId {
969 if !loaded_context.referenced_buffers.is_empty() {
970 self.action_log.update(cx, |log, cx| {
971 for buffer in loaded_context.referenced_buffers {
972 log.buffer_read(buffer, cx);
973 }
974 });
975 }
976
977 let message_id = self.insert_message(
978 Role::User,
979 vec![MessageSegment::Text(text.into())],
980 loaded_context.loaded_context,
981 creases,
982 false,
983 cx,
984 );
985
986 if let Some(git_checkpoint) = git_checkpoint {
987 self.pending_checkpoint = Some(ThreadCheckpoint {
988 message_id,
989 git_checkpoint,
990 });
991 }
992
993 self.auto_capture_telemetry(cx);
994
995 message_id
996 }
997
998 pub fn insert_invisible_continue_message(&mut self, cx: &mut Context<Self>) -> MessageId {
999 let id = self.insert_message(
1000 Role::User,
1001 vec![MessageSegment::Text("Continue where you left off".into())],
1002 LoadedContext::default(),
1003 vec![],
1004 true,
1005 cx,
1006 );
1007 self.pending_checkpoint = None;
1008
1009 id
1010 }
1011
1012 pub fn insert_assistant_message(
1013 &mut self,
1014 segments: Vec<MessageSegment>,
1015 cx: &mut Context<Self>,
1016 ) -> MessageId {
1017 self.insert_message(
1018 Role::Assistant,
1019 segments,
1020 LoadedContext::default(),
1021 Vec::new(),
1022 false,
1023 cx,
1024 )
1025 }
1026
1027 pub fn insert_message(
1028 &mut self,
1029 role: Role,
1030 segments: Vec<MessageSegment>,
1031 loaded_context: LoadedContext,
1032 creases: Vec<MessageCrease>,
1033 is_hidden: bool,
1034 cx: &mut Context<Self>,
1035 ) -> MessageId {
1036 let id = self.next_message_id.post_inc();
1037 self.messages.push(Message {
1038 id,
1039 role,
1040 segments,
1041 loaded_context,
1042 creases,
1043 is_hidden,
1044 });
1045 self.touch_updated_at();
1046 cx.emit(ThreadEvent::MessageAdded(id));
1047 id
1048 }
1049
1050 pub fn edit_message(
1051 &mut self,
1052 id: MessageId,
1053 new_role: Role,
1054 new_segments: Vec<MessageSegment>,
1055 creases: Vec<MessageCrease>,
1056 loaded_context: Option<LoadedContext>,
1057 checkpoint: Option<GitStoreCheckpoint>,
1058 cx: &mut Context<Self>,
1059 ) -> bool {
1060 let Some(message) = self.messages.iter_mut().find(|message| message.id == id) else {
1061 return false;
1062 };
1063 message.role = new_role;
1064 message.segments = new_segments;
1065 message.creases = creases;
1066 if let Some(context) = loaded_context {
1067 message.loaded_context = context;
1068 }
1069 if let Some(git_checkpoint) = checkpoint {
1070 self.checkpoints_by_message.insert(
1071 id,
1072 ThreadCheckpoint {
1073 message_id: id,
1074 git_checkpoint,
1075 },
1076 );
1077 }
1078 self.touch_updated_at();
1079 cx.emit(ThreadEvent::MessageEdited(id));
1080 true
1081 }
1082
1083 pub fn delete_message(&mut self, id: MessageId, cx: &mut Context<Self>) -> bool {
1084 let Some(index) = self.messages.iter().position(|message| message.id == id) else {
1085 return false;
1086 };
1087 self.messages.remove(index);
1088 self.touch_updated_at();
1089 cx.emit(ThreadEvent::MessageDeleted(id));
1090 true
1091 }
1092
1093 /// Returns the representation of this [`Thread`] in a textual form.
1094 ///
1095 /// This is the representation we use when attaching a thread as context to another thread.
1096 pub fn text(&self) -> String {
1097 let mut text = String::new();
1098
1099 for message in &self.messages {
1100 text.push_str(match message.role {
1101 language_model::Role::User => "User:",
1102 language_model::Role::Assistant => "Agent:",
1103 language_model::Role::System => "System:",
1104 });
1105 text.push('\n');
1106
1107 for segment in &message.segments {
1108 match segment {
1109 MessageSegment::Text(content) => text.push_str(content),
1110 MessageSegment::Thinking { text: content, .. } => {
1111 text.push_str(&format!("<think>{}</think>", content))
1112 }
1113 MessageSegment::RedactedThinking(_) => {}
1114 }
1115 }
1116 text.push('\n');
1117 }
1118
1119 text
1120 }
1121
1122 /// Serializes this thread into a format for storage or telemetry.
1123 pub fn serialize(&self, cx: &mut Context<Self>) -> Task<Result<SerializedThread>> {
1124 let initial_project_snapshot = self.initial_project_snapshot.clone();
1125 cx.spawn(async move |this, cx| {
1126 let initial_project_snapshot = initial_project_snapshot.await;
1127 this.read_with(cx, |this, cx| SerializedThread {
1128 version: SerializedThread::VERSION.to_string(),
1129 summary: this.summary().or_default(),
1130 updated_at: this.updated_at(),
1131 messages: this
1132 .messages()
1133 .map(|message| SerializedMessage {
1134 id: message.id,
1135 role: message.role,
1136 segments: message
1137 .segments
1138 .iter()
1139 .map(|segment| match segment {
1140 MessageSegment::Text(text) => {
1141 SerializedMessageSegment::Text { text: text.clone() }
1142 }
1143 MessageSegment::Thinking { text, signature } => {
1144 SerializedMessageSegment::Thinking {
1145 text: text.clone(),
1146 signature: signature.clone(),
1147 }
1148 }
1149 MessageSegment::RedactedThinking(data) => {
1150 SerializedMessageSegment::RedactedThinking {
1151 data: data.clone(),
1152 }
1153 }
1154 })
1155 .collect(),
1156 tool_uses: this
1157 .tool_uses_for_message(message.id, cx)
1158 .into_iter()
1159 .map(|tool_use| SerializedToolUse {
1160 id: tool_use.id,
1161 name: tool_use.name,
1162 input: tool_use.input,
1163 })
1164 .collect(),
1165 tool_results: this
1166 .tool_results_for_message(message.id)
1167 .into_iter()
1168 .map(|tool_result| SerializedToolResult {
1169 tool_use_id: tool_result.tool_use_id.clone(),
1170 is_error: tool_result.is_error,
1171 content: tool_result.content.clone(),
1172 output: tool_result.output.clone(),
1173 })
1174 .collect(),
1175 context: message.loaded_context.text.clone(),
1176 creases: message
1177 .creases
1178 .iter()
1179 .map(|crease| SerializedCrease {
1180 start: crease.range.start,
1181 end: crease.range.end,
1182 icon_path: crease.icon_path.clone(),
1183 label: crease.label.clone(),
1184 })
1185 .collect(),
1186 is_hidden: message.is_hidden,
1187 })
1188 .collect(),
1189 initial_project_snapshot,
1190 cumulative_token_usage: this.cumulative_token_usage,
1191 request_token_usage: this.request_token_usage.clone(),
1192 detailed_summary_state: this.detailed_summary_rx.borrow().clone(),
1193 exceeded_window_error: this.exceeded_window_error.clone(),
1194 model: this
1195 .configured_model
1196 .as_ref()
1197 .map(|model| SerializedLanguageModel {
1198 provider: model.provider.id().0.to_string(),
1199 model: model.model.id().0.to_string(),
1200 }),
1201 completion_mode: Some(this.completion_mode),
1202 tool_use_limit_reached: this.tool_use_limit_reached,
1203 profile: Some(this.profile.id().clone()),
1204 })
1205 })
1206 }
1207
1208 pub fn remaining_turns(&self) -> u32 {
1209 self.remaining_turns
1210 }
1211
1212 pub fn set_remaining_turns(&mut self, remaining_turns: u32) {
1213 self.remaining_turns = remaining_turns;
1214 }
1215
1216 pub fn send_to_model(
1217 &mut self,
1218 model: Arc<dyn LanguageModel>,
1219 intent: CompletionIntent,
1220 window: Option<AnyWindowHandle>,
1221 cx: &mut Context<Self>,
1222 ) {
1223 if self.remaining_turns == 0 {
1224 return;
1225 }
1226
1227 self.remaining_turns -= 1;
1228
1229 let request = self.to_completion_request(model.clone(), intent, cx);
1230
1231 self.stream_completion(request, model, window, cx);
1232 }
1233
1234 pub fn used_tools_since_last_user_message(&self) -> bool {
1235 for message in self.messages.iter().rev() {
1236 if self.tool_use.message_has_tool_results(message.id) {
1237 return true;
1238 } else if message.role == Role::User {
1239 return false;
1240 }
1241 }
1242
1243 false
1244 }
1245
1246 pub fn to_completion_request(
1247 &self,
1248 model: Arc<dyn LanguageModel>,
1249 intent: CompletionIntent,
1250 cx: &mut Context<Self>,
1251 ) -> LanguageModelRequest {
1252 let mut request = LanguageModelRequest {
1253 thread_id: Some(self.id.to_string()),
1254 prompt_id: Some(self.last_prompt_id.to_string()),
1255 intent: Some(intent),
1256 mode: None,
1257 messages: vec![],
1258 tools: Vec::new(),
1259 tool_choice: None,
1260 stop: Vec::new(),
1261 temperature: AgentSettings::temperature_for_model(&model, cx),
1262 };
1263
1264 let available_tools = self.available_tools(cx, model.clone());
1265 let available_tool_names = available_tools
1266 .iter()
1267 .map(|tool| tool.name.clone())
1268 .collect();
1269
1270 let model_context = &ModelContext {
1271 available_tools: available_tool_names,
1272 };
1273
1274 if let Some(project_context) = self.project_context.borrow().as_ref() {
1275 match self
1276 .prompt_builder
1277 .generate_assistant_system_prompt(project_context, model_context)
1278 {
1279 Err(err) => {
1280 let message = format!("{err:?}").into();
1281 log::error!("{message}");
1282 cx.emit(ThreadEvent::ShowError(ThreadError::Message {
1283 header: "Error generating system prompt".into(),
1284 message,
1285 }));
1286 }
1287 Ok(system_prompt) => {
1288 request.messages.push(LanguageModelRequestMessage {
1289 role: Role::System,
1290 content: vec![MessageContent::Text(system_prompt)],
1291 cache: true,
1292 });
1293 }
1294 }
1295 } else {
1296 let message = "Context for system prompt unexpectedly not ready.".into();
1297 log::error!("{message}");
1298 cx.emit(ThreadEvent::ShowError(ThreadError::Message {
1299 header: "Error generating system prompt".into(),
1300 message,
1301 }));
1302 }
1303
1304 let mut message_ix_to_cache = None;
1305 for message in &self.messages {
1306 let mut request_message = LanguageModelRequestMessage {
1307 role: message.role,
1308 content: Vec::new(),
1309 cache: false,
1310 };
1311
1312 message
1313 .loaded_context
1314 .add_to_request_message(&mut request_message);
1315
1316 for segment in &message.segments {
1317 match segment {
1318 MessageSegment::Text(text) => {
1319 if !text.is_empty() {
1320 request_message
1321 .content
1322 .push(MessageContent::Text(text.into()));
1323 }
1324 }
1325 MessageSegment::Thinking { text, signature } => {
1326 if !text.is_empty() {
1327 request_message.content.push(MessageContent::Thinking {
1328 text: text.into(),
1329 signature: signature.clone(),
1330 });
1331 }
1332 }
1333 MessageSegment::RedactedThinking(data) => {
1334 request_message
1335 .content
1336 .push(MessageContent::RedactedThinking(data.clone()));
1337 }
1338 };
1339 }
1340
1341 let mut cache_message = true;
1342 let mut tool_results_message = LanguageModelRequestMessage {
1343 role: Role::User,
1344 content: Vec::new(),
1345 cache: false,
1346 };
1347 for (tool_use, tool_result) in self.tool_use.tool_results(message.id) {
1348 if let Some(tool_result) = tool_result {
1349 request_message
1350 .content
1351 .push(MessageContent::ToolUse(tool_use.clone()));
1352 tool_results_message
1353 .content
1354 .push(MessageContent::ToolResult(LanguageModelToolResult {
1355 tool_use_id: tool_use.id.clone(),
1356 tool_name: tool_result.tool_name.clone(),
1357 is_error: tool_result.is_error,
1358 content: if tool_result.content.is_empty() {
1359 // Surprisingly, the API fails if we return an empty string here.
1360 // It thinks we are sending a tool use without a tool result.
1361 "<Tool returned an empty string>".into()
1362 } else {
1363 tool_result.content.clone()
1364 },
1365 output: None,
1366 }));
1367 } else {
1368 cache_message = false;
1369 log::debug!(
1370 "skipped tool use {:?} because it is still pending",
1371 tool_use
1372 );
1373 }
1374 }
1375
1376 if cache_message {
1377 message_ix_to_cache = Some(request.messages.len());
1378 }
1379 request.messages.push(request_message);
1380
1381 if !tool_results_message.content.is_empty() {
1382 if cache_message {
1383 message_ix_to_cache = Some(request.messages.len());
1384 }
1385 request.messages.push(tool_results_message);
1386 }
1387 }
1388
1389 // https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching
1390 if let Some(message_ix_to_cache) = message_ix_to_cache {
1391 request.messages[message_ix_to_cache].cache = true;
1392 }
1393
1394 request.tools = available_tools;
1395 request.mode = if model.supports_max_mode() {
1396 Some(self.completion_mode.into())
1397 } else {
1398 Some(CompletionMode::Normal.into())
1399 };
1400
1401 request
1402 }
1403
1404 fn to_summarize_request(
1405 &self,
1406 model: &Arc<dyn LanguageModel>,
1407 intent: CompletionIntent,
1408 added_user_message: String,
1409 cx: &App,
1410 ) -> LanguageModelRequest {
1411 let mut request = LanguageModelRequest {
1412 thread_id: None,
1413 prompt_id: None,
1414 intent: Some(intent),
1415 mode: None,
1416 messages: vec![],
1417 tools: Vec::new(),
1418 tool_choice: None,
1419 stop: Vec::new(),
1420 temperature: AgentSettings::temperature_for_model(model, cx),
1421 };
1422
1423 for message in &self.messages {
1424 let mut request_message = LanguageModelRequestMessage {
1425 role: message.role,
1426 content: Vec::new(),
1427 cache: false,
1428 };
1429
1430 for segment in &message.segments {
1431 match segment {
1432 MessageSegment::Text(text) => request_message
1433 .content
1434 .push(MessageContent::Text(text.clone())),
1435 MessageSegment::Thinking { .. } => {}
1436 MessageSegment::RedactedThinking(_) => {}
1437 }
1438 }
1439
1440 if request_message.content.is_empty() {
1441 continue;
1442 }
1443
1444 request.messages.push(request_message);
1445 }
1446
1447 request.messages.push(LanguageModelRequestMessage {
1448 role: Role::User,
1449 content: vec![MessageContent::Text(added_user_message)],
1450 cache: false,
1451 });
1452
1453 request
1454 }
1455
1456 pub fn stream_completion(
1457 &mut self,
1458 request: LanguageModelRequest,
1459 model: Arc<dyn LanguageModel>,
1460 window: Option<AnyWindowHandle>,
1461 cx: &mut Context<Self>,
1462 ) {
1463 self.tool_use_limit_reached = false;
1464
1465 let pending_completion_id = post_inc(&mut self.completion_count);
1466 let mut request_callback_parameters = if self.request_callback.is_some() {
1467 Some((request.clone(), Vec::new()))
1468 } else {
1469 None
1470 };
1471 let prompt_id = self.last_prompt_id.clone();
1472 let tool_use_metadata = ToolUseMetadata {
1473 model: model.clone(),
1474 thread_id: self.id.clone(),
1475 prompt_id: prompt_id.clone(),
1476 };
1477
1478 self.last_received_chunk_at = Some(Instant::now());
1479
1480 let task = cx.spawn(async move |thread, cx| {
1481 let stream_completion_future = model.stream_completion(request, &cx);
1482 let initial_token_usage =
1483 thread.read_with(cx, |thread, _cx| thread.cumulative_token_usage);
1484 let stream_completion = async {
1485 let mut events = stream_completion_future.await?;
1486
1487 let mut stop_reason = StopReason::EndTurn;
1488 let mut current_token_usage = TokenUsage::default();
1489
1490 thread
1491 .update(cx, |_thread, cx| {
1492 cx.emit(ThreadEvent::NewRequest);
1493 })
1494 .ok();
1495
1496 let mut request_assistant_message_id = None;
1497
1498 while let Some(event) = events.next().await {
1499 if let Some((_, response_events)) = request_callback_parameters.as_mut() {
1500 response_events
1501 .push(event.as_ref().map_err(|error| error.to_string()).cloned());
1502 }
1503
1504 thread.update(cx, |thread, cx| {
1505 let event = match event {
1506 Ok(event) => event,
1507 Err(error) => {
1508 match error {
1509 LanguageModelCompletionError::RateLimitExceeded { retry_after } => {
1510 anyhow::bail!(LanguageModelKnownError::RateLimitExceeded { retry_after });
1511 }
1512 LanguageModelCompletionError::Overloaded => {
1513 anyhow::bail!(LanguageModelKnownError::Overloaded);
1514 }
1515 LanguageModelCompletionError::ApiInternalServerError =>{
1516 anyhow::bail!(LanguageModelKnownError::ApiInternalServerError);
1517 }
1518 LanguageModelCompletionError::PromptTooLarge { tokens } => {
1519 let tokens = tokens.unwrap_or_else(|| {
1520 // We didn't get an exact token count from the API, so fall back on our estimate.
1521 thread.total_token_usage()
1522 .map(|usage| usage.total)
1523 .unwrap_or(0)
1524 // We know the context window was exceeded in practice, so if our estimate was
1525 // lower than max tokens, the estimate was wrong; return that we exceeded by 1.
1526 .max(model.max_token_count().saturating_add(1))
1527 });
1528
1529 anyhow::bail!(LanguageModelKnownError::ContextWindowLimitExceeded { tokens })
1530 }
1531 LanguageModelCompletionError::ApiReadResponseError(io_error) => {
1532 anyhow::bail!(LanguageModelKnownError::ReadResponseError(io_error));
1533 }
1534 LanguageModelCompletionError::UnknownResponseFormat(error) => {
1535 anyhow::bail!(LanguageModelKnownError::UnknownResponseFormat(error));
1536 }
1537 LanguageModelCompletionError::HttpResponseError { status, ref body } => {
1538 if let Some(known_error) = LanguageModelKnownError::from_http_response(status, body) {
1539 anyhow::bail!(known_error);
1540 } else {
1541 return Err(error.into());
1542 }
1543 }
1544 LanguageModelCompletionError::DeserializeResponse(error) => {
1545 anyhow::bail!(LanguageModelKnownError::DeserializeResponse(error));
1546 }
1547 LanguageModelCompletionError::BadInputJson {
1548 id,
1549 tool_name,
1550 raw_input: invalid_input_json,
1551 json_parse_error,
1552 } => {
1553 thread.receive_invalid_tool_json(
1554 id,
1555 tool_name,
1556 invalid_input_json,
1557 json_parse_error,
1558 window,
1559 cx,
1560 );
1561 return Ok(());
1562 }
1563 // These are all errors we can't automatically attempt to recover from (e.g. by retrying)
1564 err @ LanguageModelCompletionError::BadRequestFormat |
1565 err @ LanguageModelCompletionError::AuthenticationError |
1566 err @ LanguageModelCompletionError::PermissionError |
1567 err @ LanguageModelCompletionError::ApiEndpointNotFound |
1568 err @ LanguageModelCompletionError::SerializeRequest(_) |
1569 err @ LanguageModelCompletionError::BuildRequestBody(_) |
1570 err @ LanguageModelCompletionError::HttpSend(_) => {
1571 anyhow::bail!(err);
1572 }
1573 LanguageModelCompletionError::Other(error) => {
1574 return Err(error);
1575 }
1576 }
1577 }
1578 };
1579
1580 match event {
1581 LanguageModelCompletionEvent::StartMessage { .. } => {
1582 request_assistant_message_id =
1583 Some(thread.insert_assistant_message(
1584 vec![MessageSegment::Text(String::new())],
1585 cx,
1586 ));
1587 }
1588 LanguageModelCompletionEvent::Stop(reason) => {
1589 stop_reason = reason;
1590 }
1591 LanguageModelCompletionEvent::UsageUpdate(token_usage) => {
1592 thread.update_token_usage_at_last_message(token_usage);
1593 thread.cumulative_token_usage = thread.cumulative_token_usage
1594 + token_usage
1595 - current_token_usage;
1596 current_token_usage = token_usage;
1597 }
1598 LanguageModelCompletionEvent::Text(chunk) => {
1599 thread.received_chunk();
1600
1601 cx.emit(ThreadEvent::ReceivedTextChunk);
1602 if let Some(last_message) = thread.messages.last_mut() {
1603 if last_message.role == Role::Assistant
1604 && !thread.tool_use.has_tool_results(last_message.id)
1605 {
1606 last_message.push_text(&chunk);
1607 cx.emit(ThreadEvent::StreamedAssistantText(
1608 last_message.id,
1609 chunk,
1610 ));
1611 } else {
1612 // If we won't have an Assistant message yet, assume this chunk marks the beginning
1613 // of a new Assistant response.
1614 //
1615 // Importantly: We do *not* want to emit a `StreamedAssistantText` event here, as it
1616 // will result in duplicating the text of the chunk in the rendered Markdown.
1617 request_assistant_message_id =
1618 Some(thread.insert_assistant_message(
1619 vec![MessageSegment::Text(chunk.to_string())],
1620 cx,
1621 ));
1622 };
1623 }
1624 }
1625 LanguageModelCompletionEvent::Thinking {
1626 text: chunk,
1627 signature,
1628 } => {
1629 thread.received_chunk();
1630
1631 if let Some(last_message) = thread.messages.last_mut() {
1632 if last_message.role == Role::Assistant
1633 && !thread.tool_use.has_tool_results(last_message.id)
1634 {
1635 last_message.push_thinking(&chunk, signature);
1636 cx.emit(ThreadEvent::StreamedAssistantThinking(
1637 last_message.id,
1638 chunk,
1639 ));
1640 } else {
1641 // If we won't have an Assistant message yet, assume this chunk marks the beginning
1642 // of a new Assistant response.
1643 //
1644 // Importantly: We do *not* want to emit a `StreamedAssistantText` event here, as it
1645 // will result in duplicating the text of the chunk in the rendered Markdown.
1646 request_assistant_message_id =
1647 Some(thread.insert_assistant_message(
1648 vec![MessageSegment::Thinking {
1649 text: chunk.to_string(),
1650 signature,
1651 }],
1652 cx,
1653 ));
1654 };
1655 }
1656 }
1657 LanguageModelCompletionEvent::RedactedThinking {
1658 data
1659 } => {
1660 thread.received_chunk();
1661
1662 if let Some(last_message) = thread.messages.last_mut() {
1663 if last_message.role == Role::Assistant
1664 && !thread.tool_use.has_tool_results(last_message.id)
1665 {
1666 last_message.push_redacted_thinking(data);
1667 } else {
1668 request_assistant_message_id =
1669 Some(thread.insert_assistant_message(
1670 vec![MessageSegment::RedactedThinking(data)],
1671 cx,
1672 ));
1673 };
1674 }
1675 }
1676 LanguageModelCompletionEvent::ToolUse(tool_use) => {
1677 let last_assistant_message_id = request_assistant_message_id
1678 .unwrap_or_else(|| {
1679 let new_assistant_message_id =
1680 thread.insert_assistant_message(vec![], cx);
1681 request_assistant_message_id =
1682 Some(new_assistant_message_id);
1683 new_assistant_message_id
1684 });
1685
1686 let tool_use_id = tool_use.id.clone();
1687 let streamed_input = if tool_use.is_input_complete {
1688 None
1689 } else {
1690 Some((&tool_use.input).clone())
1691 };
1692
1693 let ui_text = thread.tool_use.request_tool_use(
1694 last_assistant_message_id,
1695 tool_use,
1696 tool_use_metadata.clone(),
1697 cx,
1698 );
1699
1700 if let Some(input) = streamed_input {
1701 cx.emit(ThreadEvent::StreamedToolUse {
1702 tool_use_id,
1703 ui_text,
1704 input,
1705 });
1706 }
1707 }
1708 LanguageModelCompletionEvent::StatusUpdate(status_update) => {
1709 if let Some(completion) = thread
1710 .pending_completions
1711 .iter_mut()
1712 .find(|completion| completion.id == pending_completion_id)
1713 {
1714 match status_update {
1715 CompletionRequestStatus::Queued {
1716 position,
1717 } => {
1718 completion.queue_state = QueueState::Queued { position };
1719 }
1720 CompletionRequestStatus::Started => {
1721 completion.queue_state = QueueState::Started;
1722 }
1723 CompletionRequestStatus::Failed {
1724 code, message, request_id
1725 } => {
1726 anyhow::bail!("completion request failed. request_id: {request_id}, code: {code}, message: {message}");
1727 }
1728 CompletionRequestStatus::UsageUpdated {
1729 amount, limit
1730 } => {
1731 thread.update_model_request_usage(amount as u32, limit, cx);
1732 }
1733 CompletionRequestStatus::ToolUseLimitReached => {
1734 thread.tool_use_limit_reached = true;
1735 cx.emit(ThreadEvent::ToolUseLimitReached);
1736 }
1737 }
1738 }
1739 }
1740 }
1741
1742 thread.touch_updated_at();
1743 cx.emit(ThreadEvent::StreamedCompletion);
1744 cx.notify();
1745
1746 thread.auto_capture_telemetry(cx);
1747 Ok(())
1748 })??;
1749
1750 smol::future::yield_now().await;
1751 }
1752
1753 thread.update(cx, |thread, cx| {
1754 thread.last_received_chunk_at = None;
1755 thread
1756 .pending_completions
1757 .retain(|completion| completion.id != pending_completion_id);
1758
1759 // If there is a response without tool use, summarize the message. Otherwise,
1760 // allow two tool uses before summarizing.
1761 if matches!(thread.summary, ThreadSummary::Pending)
1762 && thread.messages.len() >= 2
1763 && (!thread.has_pending_tool_uses() || thread.messages.len() >= 6)
1764 {
1765 thread.summarize(cx);
1766 }
1767 })?;
1768
1769 anyhow::Ok(stop_reason)
1770 };
1771
1772 let result = stream_completion.await;
1773
1774 thread
1775 .update(cx, |thread, cx| {
1776 thread.finalize_pending_checkpoint(cx);
1777 match result.as_ref() {
1778 Ok(stop_reason) => match stop_reason {
1779 StopReason::ToolUse => {
1780 let tool_uses = thread.use_pending_tools(window, model.clone(), cx);
1781 cx.emit(ThreadEvent::UsePendingTools { tool_uses });
1782 }
1783 StopReason::EndTurn | StopReason::MaxTokens => {
1784 thread.project.update(cx, |project, cx| {
1785 project.set_agent_location(None, cx);
1786 });
1787 }
1788 StopReason::Refusal => {
1789 thread.project.update(cx, |project, cx| {
1790 project.set_agent_location(None, cx);
1791 });
1792
1793 // Remove the turn that was refused.
1794 //
1795 // https://docs.anthropic.com/en/docs/test-and-evaluate/strengthen-guardrails/handle-streaming-refusals#reset-context-after-refusal
1796 {
1797 let mut messages_to_remove = Vec::new();
1798
1799 for (ix, message) in thread.messages.iter().enumerate().rev() {
1800 messages_to_remove.push(message.id);
1801
1802 if message.role == Role::User {
1803 if ix == 0 {
1804 break;
1805 }
1806
1807 if let Some(prev_message) = thread.messages.get(ix - 1) {
1808 if prev_message.role == Role::Assistant {
1809 break;
1810 }
1811 }
1812 }
1813 }
1814
1815 for message_id in messages_to_remove {
1816 thread.delete_message(message_id, cx);
1817 }
1818 }
1819
1820 cx.emit(ThreadEvent::ShowError(ThreadError::Message {
1821 header: "Language model refusal".into(),
1822 message: "Model refused to generate content for safety reasons.".into(),
1823 }));
1824 }
1825 },
1826 Err(error) => {
1827 thread.project.update(cx, |project, cx| {
1828 project.set_agent_location(None, cx);
1829 });
1830
1831 fn emit_generic_error(error: &anyhow::Error, cx: &mut Context<Thread>) {
1832 let error_message = error
1833 .chain()
1834 .map(|err| err.to_string())
1835 .collect::<Vec<_>>()
1836 .join("\n");
1837 cx.emit(ThreadEvent::ShowError(ThreadError::Message {
1838 header: "Error interacting with language model".into(),
1839 message: SharedString::from(error_message.clone()),
1840 }));
1841 }
1842
1843 if error.is::<PaymentRequiredError>() {
1844 cx.emit(ThreadEvent::ShowError(ThreadError::PaymentRequired));
1845 } else if let Some(error) =
1846 error.downcast_ref::<ModelRequestLimitReachedError>()
1847 {
1848 cx.emit(ThreadEvent::ShowError(
1849 ThreadError::ModelRequestLimitReached { plan: error.plan },
1850 ));
1851 } else if let Some(known_error) =
1852 error.downcast_ref::<LanguageModelKnownError>()
1853 {
1854 match known_error {
1855 LanguageModelKnownError::ContextWindowLimitExceeded { tokens } => {
1856 thread.exceeded_window_error = Some(ExceededWindowError {
1857 model_id: model.id(),
1858 token_count: *tokens,
1859 });
1860 cx.notify();
1861 }
1862 LanguageModelKnownError::RateLimitExceeded { .. } => {
1863 // In the future we will report the error to the user, wait retry_after, and then retry.
1864 emit_generic_error(error, cx);
1865 }
1866 LanguageModelKnownError::Overloaded => {
1867 // In the future we will wait and then retry, up to N times.
1868 emit_generic_error(error, cx);
1869 }
1870 LanguageModelKnownError::ApiInternalServerError => {
1871 // In the future we will retry the request, but only once.
1872 emit_generic_error(error, cx);
1873 }
1874 LanguageModelKnownError::ReadResponseError(_) |
1875 LanguageModelKnownError::DeserializeResponse(_) |
1876 LanguageModelKnownError::UnknownResponseFormat(_) => {
1877 // In the future we will attempt to re-roll response, but only once
1878 emit_generic_error(error, cx);
1879 }
1880 }
1881 } else {
1882 emit_generic_error(error, cx);
1883 }
1884
1885 thread.cancel_last_completion(window, cx);
1886 }
1887 }
1888
1889 cx.emit(ThreadEvent::Stopped(result.map_err(Arc::new)));
1890
1891 if let Some((request_callback, (request, response_events))) = thread
1892 .request_callback
1893 .as_mut()
1894 .zip(request_callback_parameters.as_ref())
1895 {
1896 request_callback(request, response_events);
1897 }
1898
1899 thread.auto_capture_telemetry(cx);
1900
1901 if let Ok(initial_usage) = initial_token_usage {
1902 let usage = thread.cumulative_token_usage - initial_usage;
1903
1904 telemetry::event!(
1905 "Assistant Thread Completion",
1906 thread_id = thread.id().to_string(),
1907 prompt_id = prompt_id,
1908 model = model.telemetry_id(),
1909 model_provider = model.provider_id().to_string(),
1910 input_tokens = usage.input_tokens,
1911 output_tokens = usage.output_tokens,
1912 cache_creation_input_tokens = usage.cache_creation_input_tokens,
1913 cache_read_input_tokens = usage.cache_read_input_tokens,
1914 );
1915 }
1916 })
1917 .ok();
1918 });
1919
1920 self.pending_completions.push(PendingCompletion {
1921 id: pending_completion_id,
1922 queue_state: QueueState::Sending,
1923 _task: task,
1924 });
1925 }
1926
1927 pub fn summarize(&mut self, cx: &mut Context<Self>) {
1928 let Some(model) = LanguageModelRegistry::read_global(cx).thread_summary_model() else {
1929 println!("No thread summary model");
1930 return;
1931 };
1932
1933 if !model.provider.is_authenticated(cx) {
1934 return;
1935 }
1936
1937 let added_user_message = include_str!("./prompts/summarize_thread_prompt.txt");
1938
1939 let request = self.to_summarize_request(
1940 &model.model,
1941 CompletionIntent::ThreadSummarization,
1942 added_user_message.into(),
1943 cx,
1944 );
1945
1946 self.summary = ThreadSummary::Generating;
1947
1948 self.pending_summary = cx.spawn(async move |this, cx| {
1949 let result = async {
1950 let mut messages = model.model.stream_completion(request, &cx).await?;
1951
1952 let mut new_summary = String::new();
1953 while let Some(event) = messages.next().await {
1954 let Ok(event) = event else {
1955 continue;
1956 };
1957 let text = match event {
1958 LanguageModelCompletionEvent::Text(text) => text,
1959 LanguageModelCompletionEvent::StatusUpdate(
1960 CompletionRequestStatus::UsageUpdated { amount, limit },
1961 ) => {
1962 this.update(cx, |thread, cx| {
1963 thread.update_model_request_usage(amount as u32, limit, cx);
1964 })?;
1965 continue;
1966 }
1967 _ => continue,
1968 };
1969
1970 let mut lines = text.lines();
1971 new_summary.extend(lines.next());
1972
1973 // Stop if the LLM generated multiple lines.
1974 if lines.next().is_some() {
1975 break;
1976 }
1977 }
1978
1979 anyhow::Ok(new_summary)
1980 }
1981 .await;
1982
1983 this.update(cx, |this, cx| {
1984 match result {
1985 Ok(new_summary) => {
1986 if new_summary.is_empty() {
1987 this.summary = ThreadSummary::Error;
1988 } else {
1989 this.summary = ThreadSummary::Ready(new_summary.into());
1990 }
1991 }
1992 Err(err) => {
1993 this.summary = ThreadSummary::Error;
1994 log::error!("Failed to generate thread summary: {}", err);
1995 }
1996 }
1997 cx.emit(ThreadEvent::SummaryGenerated);
1998 })
1999 .log_err()?;
2000
2001 Some(())
2002 });
2003 }
2004
2005 pub fn start_generating_detailed_summary_if_needed(
2006 &mut self,
2007 thread_store: WeakEntity<ThreadStore>,
2008 cx: &mut Context<Self>,
2009 ) {
2010 let Some(last_message_id) = self.messages.last().map(|message| message.id) else {
2011 return;
2012 };
2013
2014 match &*self.detailed_summary_rx.borrow() {
2015 DetailedSummaryState::Generating { message_id, .. }
2016 | DetailedSummaryState::Generated { message_id, .. }
2017 if *message_id == last_message_id =>
2018 {
2019 // Already up-to-date
2020 return;
2021 }
2022 _ => {}
2023 }
2024
2025 let Some(ConfiguredModel { model, provider }) =
2026 LanguageModelRegistry::read_global(cx).thread_summary_model()
2027 else {
2028 return;
2029 };
2030
2031 if !provider.is_authenticated(cx) {
2032 return;
2033 }
2034
2035 let added_user_message = include_str!("./prompts/summarize_thread_detailed_prompt.txt");
2036
2037 let request = self.to_summarize_request(
2038 &model,
2039 CompletionIntent::ThreadContextSummarization,
2040 added_user_message.into(),
2041 cx,
2042 );
2043
2044 *self.detailed_summary_tx.borrow_mut() = DetailedSummaryState::Generating {
2045 message_id: last_message_id,
2046 };
2047
2048 // Replace the detailed summarization task if there is one, cancelling it. It would probably
2049 // be better to allow the old task to complete, but this would require logic for choosing
2050 // which result to prefer (the old task could complete after the new one, resulting in a
2051 // stale summary).
2052 self.detailed_summary_task = cx.spawn(async move |thread, cx| {
2053 let stream = model.stream_completion_text(request, &cx);
2054 let Some(mut messages) = stream.await.log_err() else {
2055 thread
2056 .update(cx, |thread, _cx| {
2057 *thread.detailed_summary_tx.borrow_mut() =
2058 DetailedSummaryState::NotGenerated;
2059 })
2060 .ok()?;
2061 return None;
2062 };
2063
2064 let mut new_detailed_summary = String::new();
2065
2066 while let Some(chunk) = messages.stream.next().await {
2067 if let Some(chunk) = chunk.log_err() {
2068 new_detailed_summary.push_str(&chunk);
2069 }
2070 }
2071
2072 thread
2073 .update(cx, |thread, _cx| {
2074 *thread.detailed_summary_tx.borrow_mut() = DetailedSummaryState::Generated {
2075 text: new_detailed_summary.into(),
2076 message_id: last_message_id,
2077 };
2078 })
2079 .ok()?;
2080
2081 // Save thread so its summary can be reused later
2082 if let Some(thread) = thread.upgrade() {
2083 if let Ok(Ok(save_task)) = cx.update(|cx| {
2084 thread_store
2085 .update(cx, |thread_store, cx| thread_store.save_thread(&thread, cx))
2086 }) {
2087 save_task.await.log_err();
2088 }
2089 }
2090
2091 Some(())
2092 });
2093 }
2094
2095 pub async fn wait_for_detailed_summary_or_text(
2096 this: &Entity<Self>,
2097 cx: &mut AsyncApp,
2098 ) -> Option<SharedString> {
2099 let mut detailed_summary_rx = this
2100 .read_with(cx, |this, _cx| this.detailed_summary_rx.clone())
2101 .ok()?;
2102 loop {
2103 match detailed_summary_rx.recv().await? {
2104 DetailedSummaryState::Generating { .. } => {}
2105 DetailedSummaryState::NotGenerated => {
2106 return this.read_with(cx, |this, _cx| this.text().into()).ok();
2107 }
2108 DetailedSummaryState::Generated { text, .. } => return Some(text),
2109 }
2110 }
2111 }
2112
2113 pub fn latest_detailed_summary_or_text(&self) -> SharedString {
2114 self.detailed_summary_rx
2115 .borrow()
2116 .text()
2117 .unwrap_or_else(|| self.text().into())
2118 }
2119
2120 pub fn is_generating_detailed_summary(&self) -> bool {
2121 matches!(
2122 &*self.detailed_summary_rx.borrow(),
2123 DetailedSummaryState::Generating { .. }
2124 )
2125 }
2126
2127 pub fn use_pending_tools(
2128 &mut self,
2129 window: Option<AnyWindowHandle>,
2130 model: Arc<dyn LanguageModel>,
2131 cx: &mut Context<Self>,
2132 ) -> Vec<PendingToolUse> {
2133 self.auto_capture_telemetry(cx);
2134 let request =
2135 Arc::new(self.to_completion_request(model.clone(), CompletionIntent::ToolResults, cx));
2136 let pending_tool_uses = self
2137 .tool_use
2138 .pending_tool_uses()
2139 .into_iter()
2140 .filter(|tool_use| tool_use.status.is_idle())
2141 .cloned()
2142 .collect::<Vec<_>>();
2143
2144 for tool_use in pending_tool_uses.iter() {
2145 self.use_pending_tool(tool_use.clone(), request.clone(), model.clone(), window, cx);
2146 }
2147
2148 pending_tool_uses
2149 }
2150
2151 fn use_pending_tool(
2152 &mut self,
2153 tool_use: PendingToolUse,
2154 request: Arc<LanguageModelRequest>,
2155 model: Arc<dyn LanguageModel>,
2156 window: Option<AnyWindowHandle>,
2157 cx: &mut Context<Self>,
2158 ) {
2159 let Some(tool) = self.tools.read(cx).tool(&tool_use.name, cx) else {
2160 return self.handle_hallucinated_tool_use(tool_use.id, tool_use.name, window, cx);
2161 };
2162
2163 if !self.profile.is_tool_enabled(tool.source(), tool.name(), cx) {
2164 return self.handle_hallucinated_tool_use(tool_use.id, tool_use.name, window, cx);
2165 }
2166
2167 if tool.needs_confirmation(&tool_use.input, cx)
2168 && !AgentSettings::get_global(cx).always_allow_tool_actions
2169 {
2170 self.tool_use.confirm_tool_use(
2171 tool_use.id,
2172 tool_use.ui_text,
2173 tool_use.input,
2174 request,
2175 tool,
2176 );
2177 cx.emit(ThreadEvent::ToolConfirmationNeeded);
2178 } else {
2179 self.run_tool(
2180 tool_use.id,
2181 tool_use.ui_text,
2182 tool_use.input,
2183 request,
2184 tool,
2185 model,
2186 window,
2187 cx,
2188 );
2189 }
2190 }
2191
2192 pub fn handle_hallucinated_tool_use(
2193 &mut self,
2194 tool_use_id: LanguageModelToolUseId,
2195 hallucinated_tool_name: Arc<str>,
2196 window: Option<AnyWindowHandle>,
2197 cx: &mut Context<Thread>,
2198 ) {
2199 let available_tools = self.profile.enabled_tools(cx);
2200
2201 let tool_list = available_tools
2202 .iter()
2203 .map(|tool| format!("- {}: {}", tool.name(), tool.description()))
2204 .collect::<Vec<_>>()
2205 .join("\n");
2206
2207 let error_message = format!(
2208 "The tool '{}' doesn't exist or is not enabled. Available tools:\n{}",
2209 hallucinated_tool_name, tool_list
2210 );
2211
2212 let pending_tool_use = self.tool_use.insert_tool_output(
2213 tool_use_id.clone(),
2214 hallucinated_tool_name,
2215 Err(anyhow!("Missing tool call: {error_message}")),
2216 self.configured_model.as_ref(),
2217 );
2218
2219 cx.emit(ThreadEvent::MissingToolUse {
2220 tool_use_id: tool_use_id.clone(),
2221 ui_text: error_message.into(),
2222 });
2223
2224 self.tool_finished(tool_use_id, pending_tool_use, false, window, cx);
2225 }
2226
2227 pub fn receive_invalid_tool_json(
2228 &mut self,
2229 tool_use_id: LanguageModelToolUseId,
2230 tool_name: Arc<str>,
2231 invalid_json: Arc<str>,
2232 error: String,
2233 window: Option<AnyWindowHandle>,
2234 cx: &mut Context<Thread>,
2235 ) {
2236 log::error!("The model returned invalid input JSON: {invalid_json}");
2237
2238 let pending_tool_use = self.tool_use.insert_tool_output(
2239 tool_use_id.clone(),
2240 tool_name,
2241 Err(anyhow!("Error parsing input JSON: {error}")),
2242 self.configured_model.as_ref(),
2243 );
2244 let ui_text = if let Some(pending_tool_use) = &pending_tool_use {
2245 pending_tool_use.ui_text.clone()
2246 } else {
2247 log::error!(
2248 "There was no pending tool use for tool use {tool_use_id}, even though it finished (with invalid input JSON)."
2249 );
2250 format!("Unknown tool {}", tool_use_id).into()
2251 };
2252
2253 cx.emit(ThreadEvent::InvalidToolInput {
2254 tool_use_id: tool_use_id.clone(),
2255 ui_text,
2256 invalid_input_json: invalid_json,
2257 });
2258
2259 self.tool_finished(tool_use_id, pending_tool_use, false, window, cx);
2260 }
2261
2262 pub fn run_tool(
2263 &mut self,
2264 tool_use_id: LanguageModelToolUseId,
2265 ui_text: impl Into<SharedString>,
2266 input: serde_json::Value,
2267 request: Arc<LanguageModelRequest>,
2268 tool: Arc<dyn Tool>,
2269 model: Arc<dyn LanguageModel>,
2270 window: Option<AnyWindowHandle>,
2271 cx: &mut Context<Thread>,
2272 ) {
2273 let task =
2274 self.spawn_tool_use(tool_use_id.clone(), request, input, tool, model, window, cx);
2275 self.tool_use
2276 .run_pending_tool(tool_use_id, ui_text.into(), task);
2277 }
2278
2279 fn spawn_tool_use(
2280 &mut self,
2281 tool_use_id: LanguageModelToolUseId,
2282 request: Arc<LanguageModelRequest>,
2283 input: serde_json::Value,
2284 tool: Arc<dyn Tool>,
2285 model: Arc<dyn LanguageModel>,
2286 window: Option<AnyWindowHandle>,
2287 cx: &mut Context<Thread>,
2288 ) -> Task<()> {
2289 let tool_name: Arc<str> = tool.name().into();
2290
2291 let tool_result = tool.run(
2292 input,
2293 request,
2294 self.project.clone(),
2295 self.action_log.clone(),
2296 model,
2297 window,
2298 cx,
2299 );
2300
2301 // Store the card separately if it exists
2302 if let Some(card) = tool_result.card.clone() {
2303 self.tool_use
2304 .insert_tool_result_card(tool_use_id.clone(), card);
2305 }
2306
2307 cx.spawn({
2308 async move |thread: WeakEntity<Thread>, cx| {
2309 let output = tool_result.output.await;
2310
2311 thread
2312 .update(cx, |thread, cx| {
2313 let pending_tool_use = thread.tool_use.insert_tool_output(
2314 tool_use_id.clone(),
2315 tool_name,
2316 output,
2317 thread.configured_model.as_ref(),
2318 );
2319 thread.tool_finished(tool_use_id, pending_tool_use, false, window, cx);
2320 })
2321 .ok();
2322 }
2323 })
2324 }
2325
2326 fn tool_finished(
2327 &mut self,
2328 tool_use_id: LanguageModelToolUseId,
2329 pending_tool_use: Option<PendingToolUse>,
2330 canceled: bool,
2331 window: Option<AnyWindowHandle>,
2332 cx: &mut Context<Self>,
2333 ) {
2334 if self.all_tools_finished() {
2335 if let Some(ConfiguredModel { model, .. }) = self.configured_model.as_ref() {
2336 if !canceled {
2337 self.send_to_model(model.clone(), CompletionIntent::ToolResults, window, cx);
2338 }
2339 self.auto_capture_telemetry(cx);
2340 }
2341 }
2342
2343 cx.emit(ThreadEvent::ToolFinished {
2344 tool_use_id,
2345 pending_tool_use,
2346 });
2347 }
2348
2349 /// Cancels the last pending completion, if there are any pending.
2350 ///
2351 /// Returns whether a completion was canceled.
2352 pub fn cancel_last_completion(
2353 &mut self,
2354 window: Option<AnyWindowHandle>,
2355 cx: &mut Context<Self>,
2356 ) -> bool {
2357 let mut canceled = self.pending_completions.pop().is_some();
2358
2359 for pending_tool_use in self.tool_use.cancel_pending() {
2360 canceled = true;
2361 self.tool_finished(
2362 pending_tool_use.id.clone(),
2363 Some(pending_tool_use),
2364 true,
2365 window,
2366 cx,
2367 );
2368 }
2369
2370 if canceled {
2371 cx.emit(ThreadEvent::CompletionCanceled);
2372
2373 // When canceled, we always want to insert the checkpoint.
2374 // (We skip over finalize_pending_checkpoint, because it
2375 // would conclude we didn't have anything to insert here.)
2376 if let Some(checkpoint) = self.pending_checkpoint.take() {
2377 self.insert_checkpoint(checkpoint, cx);
2378 }
2379 } else {
2380 self.finalize_pending_checkpoint(cx);
2381 }
2382
2383 canceled
2384 }
2385
2386 /// Signals that any in-progress editing should be canceled.
2387 ///
2388 /// This method is used to notify listeners (like ActiveThread) that
2389 /// they should cancel any editing operations.
2390 pub fn cancel_editing(&mut self, cx: &mut Context<Self>) {
2391 cx.emit(ThreadEvent::CancelEditing);
2392 }
2393
2394 pub fn feedback(&self) -> Option<ThreadFeedback> {
2395 self.feedback
2396 }
2397
2398 pub fn message_feedback(&self, message_id: MessageId) -> Option<ThreadFeedback> {
2399 self.message_feedback.get(&message_id).copied()
2400 }
2401
2402 pub fn report_message_feedback(
2403 &mut self,
2404 message_id: MessageId,
2405 feedback: ThreadFeedback,
2406 cx: &mut Context<Self>,
2407 ) -> Task<Result<()>> {
2408 if self.message_feedback.get(&message_id) == Some(&feedback) {
2409 return Task::ready(Ok(()));
2410 }
2411
2412 let final_project_snapshot = Self::project_snapshot(self.project.clone(), cx);
2413 let serialized_thread = self.serialize(cx);
2414 let thread_id = self.id().clone();
2415 let client = self.project.read(cx).client();
2416
2417 let enabled_tool_names: Vec<String> = self
2418 .profile
2419 .enabled_tools(cx)
2420 .iter()
2421 .map(|tool| tool.name())
2422 .collect();
2423
2424 self.message_feedback.insert(message_id, feedback);
2425
2426 cx.notify();
2427
2428 let message_content = self
2429 .message(message_id)
2430 .map(|msg| msg.to_string())
2431 .unwrap_or_default();
2432
2433 cx.background_spawn(async move {
2434 let final_project_snapshot = final_project_snapshot.await;
2435 let serialized_thread = serialized_thread.await?;
2436 let thread_data =
2437 serde_json::to_value(serialized_thread).unwrap_or_else(|_| serde_json::Value::Null);
2438
2439 let rating = match feedback {
2440 ThreadFeedback::Positive => "positive",
2441 ThreadFeedback::Negative => "negative",
2442 };
2443 telemetry::event!(
2444 "Assistant Thread Rated",
2445 rating,
2446 thread_id,
2447 enabled_tool_names,
2448 message_id = message_id.0,
2449 message_content,
2450 thread_data,
2451 final_project_snapshot
2452 );
2453 client.telemetry().flush_events().await;
2454
2455 Ok(())
2456 })
2457 }
2458
2459 pub fn report_feedback(
2460 &mut self,
2461 feedback: ThreadFeedback,
2462 cx: &mut Context<Self>,
2463 ) -> Task<Result<()>> {
2464 let last_assistant_message_id = self
2465 .messages
2466 .iter()
2467 .rev()
2468 .find(|msg| msg.role == Role::Assistant)
2469 .map(|msg| msg.id);
2470
2471 if let Some(message_id) = last_assistant_message_id {
2472 self.report_message_feedback(message_id, feedback, cx)
2473 } else {
2474 let final_project_snapshot = Self::project_snapshot(self.project.clone(), cx);
2475 let serialized_thread = self.serialize(cx);
2476 let thread_id = self.id().clone();
2477 let client = self.project.read(cx).client();
2478 self.feedback = Some(feedback);
2479 cx.notify();
2480
2481 cx.background_spawn(async move {
2482 let final_project_snapshot = final_project_snapshot.await;
2483 let serialized_thread = serialized_thread.await?;
2484 let thread_data = serde_json::to_value(serialized_thread)
2485 .unwrap_or_else(|_| serde_json::Value::Null);
2486
2487 let rating = match feedback {
2488 ThreadFeedback::Positive => "positive",
2489 ThreadFeedback::Negative => "negative",
2490 };
2491 telemetry::event!(
2492 "Assistant Thread Rated",
2493 rating,
2494 thread_id,
2495 thread_data,
2496 final_project_snapshot
2497 );
2498 client.telemetry().flush_events().await;
2499
2500 Ok(())
2501 })
2502 }
2503 }
2504
2505 /// Create a snapshot of the current project state including git information and unsaved buffers.
2506 fn project_snapshot(
2507 project: Entity<Project>,
2508 cx: &mut Context<Self>,
2509 ) -> Task<Arc<ProjectSnapshot>> {
2510 let git_store = project.read(cx).git_store().clone();
2511 let worktree_snapshots: Vec<_> = project
2512 .read(cx)
2513 .visible_worktrees(cx)
2514 .map(|worktree| Self::worktree_snapshot(worktree, git_store.clone(), cx))
2515 .collect();
2516
2517 cx.spawn(async move |_, cx| {
2518 let worktree_snapshots = futures::future::join_all(worktree_snapshots).await;
2519
2520 let mut unsaved_buffers = Vec::new();
2521 cx.update(|app_cx| {
2522 let buffer_store = project.read(app_cx).buffer_store();
2523 for buffer_handle in buffer_store.read(app_cx).buffers() {
2524 let buffer = buffer_handle.read(app_cx);
2525 if buffer.is_dirty() {
2526 if let Some(file) = buffer.file() {
2527 let path = file.path().to_string_lossy().to_string();
2528 unsaved_buffers.push(path);
2529 }
2530 }
2531 }
2532 })
2533 .ok();
2534
2535 Arc::new(ProjectSnapshot {
2536 worktree_snapshots,
2537 unsaved_buffer_paths: unsaved_buffers,
2538 timestamp: Utc::now(),
2539 })
2540 })
2541 }
2542
2543 fn worktree_snapshot(
2544 worktree: Entity<project::Worktree>,
2545 git_store: Entity<GitStore>,
2546 cx: &App,
2547 ) -> Task<WorktreeSnapshot> {
2548 cx.spawn(async move |cx| {
2549 // Get worktree path and snapshot
2550 let worktree_info = cx.update(|app_cx| {
2551 let worktree = worktree.read(app_cx);
2552 let path = worktree.abs_path().to_string_lossy().to_string();
2553 let snapshot = worktree.snapshot();
2554 (path, snapshot)
2555 });
2556
2557 let Ok((worktree_path, _snapshot)) = worktree_info else {
2558 return WorktreeSnapshot {
2559 worktree_path: String::new(),
2560 git_state: None,
2561 };
2562 };
2563
2564 let git_state = git_store
2565 .update(cx, |git_store, cx| {
2566 git_store
2567 .repositories()
2568 .values()
2569 .find(|repo| {
2570 repo.read(cx)
2571 .abs_path_to_repo_path(&worktree.read(cx).abs_path())
2572 .is_some()
2573 })
2574 .cloned()
2575 })
2576 .ok()
2577 .flatten()
2578 .map(|repo| {
2579 repo.update(cx, |repo, _| {
2580 let current_branch =
2581 repo.branch.as_ref().map(|branch| branch.name().to_owned());
2582 repo.send_job(None, |state, _| async move {
2583 let RepositoryState::Local { backend, .. } = state else {
2584 return GitState {
2585 remote_url: None,
2586 head_sha: None,
2587 current_branch,
2588 diff: None,
2589 };
2590 };
2591
2592 let remote_url = backend.remote_url("origin");
2593 let head_sha = backend.head_sha().await;
2594 let diff = backend.diff(DiffType::HeadToWorktree).await.ok();
2595
2596 GitState {
2597 remote_url,
2598 head_sha,
2599 current_branch,
2600 diff,
2601 }
2602 })
2603 })
2604 });
2605
2606 let git_state = match git_state {
2607 Some(git_state) => match git_state.ok() {
2608 Some(git_state) => git_state.await.ok(),
2609 None => None,
2610 },
2611 None => None,
2612 };
2613
2614 WorktreeSnapshot {
2615 worktree_path,
2616 git_state,
2617 }
2618 })
2619 }
2620
2621 pub fn to_markdown(&self, cx: &App) -> Result<String> {
2622 let mut markdown = Vec::new();
2623
2624 let summary = self.summary().or_default();
2625 writeln!(markdown, "# {summary}\n")?;
2626
2627 for message in self.messages() {
2628 writeln!(
2629 markdown,
2630 "## {role}\n",
2631 role = match message.role {
2632 Role::User => "User",
2633 Role::Assistant => "Agent",
2634 Role::System => "System",
2635 }
2636 )?;
2637
2638 if !message.loaded_context.text.is_empty() {
2639 writeln!(markdown, "{}", message.loaded_context.text)?;
2640 }
2641
2642 if !message.loaded_context.images.is_empty() {
2643 writeln!(
2644 markdown,
2645 "\n{} images attached as context.\n",
2646 message.loaded_context.images.len()
2647 )?;
2648 }
2649
2650 for segment in &message.segments {
2651 match segment {
2652 MessageSegment::Text(text) => writeln!(markdown, "{}\n", text)?,
2653 MessageSegment::Thinking { text, .. } => {
2654 writeln!(markdown, "<think>\n{}\n</think>\n", text)?
2655 }
2656 MessageSegment::RedactedThinking(_) => {}
2657 }
2658 }
2659
2660 for tool_use in self.tool_uses_for_message(message.id, cx) {
2661 writeln!(
2662 markdown,
2663 "**Use Tool: {} ({})**",
2664 tool_use.name, tool_use.id
2665 )?;
2666 writeln!(markdown, "```json")?;
2667 writeln!(
2668 markdown,
2669 "{}",
2670 serde_json::to_string_pretty(&tool_use.input)?
2671 )?;
2672 writeln!(markdown, "```")?;
2673 }
2674
2675 for tool_result in self.tool_results_for_message(message.id) {
2676 write!(markdown, "\n**Tool Results: {}", tool_result.tool_use_id)?;
2677 if tool_result.is_error {
2678 write!(markdown, " (Error)")?;
2679 }
2680
2681 writeln!(markdown, "**\n")?;
2682 match &tool_result.content {
2683 LanguageModelToolResultContent::Text(text) => {
2684 writeln!(markdown, "{text}")?;
2685 }
2686 LanguageModelToolResultContent::Image(image) => {
2687 writeln!(markdown, "", image.source)?;
2688 }
2689 }
2690
2691 if let Some(output) = tool_result.output.as_ref() {
2692 writeln!(
2693 markdown,
2694 "\n\nDebug Output:\n\n```json\n{}\n```\n",
2695 serde_json::to_string_pretty(output)?
2696 )?;
2697 }
2698 }
2699 }
2700
2701 Ok(String::from_utf8_lossy(&markdown).to_string())
2702 }
2703
2704 pub fn keep_edits_in_range(
2705 &mut self,
2706 buffer: Entity<language::Buffer>,
2707 buffer_range: Range<language::Anchor>,
2708 cx: &mut Context<Self>,
2709 ) {
2710 self.action_log.update(cx, |action_log, cx| {
2711 action_log.keep_edits_in_range(buffer, buffer_range, cx)
2712 });
2713 }
2714
2715 pub fn keep_all_edits(&mut self, cx: &mut Context<Self>) {
2716 self.action_log
2717 .update(cx, |action_log, cx| action_log.keep_all_edits(cx));
2718 }
2719
2720 pub fn reject_edits_in_ranges(
2721 &mut self,
2722 buffer: Entity<language::Buffer>,
2723 buffer_ranges: Vec<Range<language::Anchor>>,
2724 cx: &mut Context<Self>,
2725 ) -> Task<Result<()>> {
2726 self.action_log.update(cx, |action_log, cx| {
2727 action_log.reject_edits_in_ranges(buffer, buffer_ranges, cx)
2728 })
2729 }
2730
2731 pub fn action_log(&self) -> &Entity<ActionLog> {
2732 &self.action_log
2733 }
2734
2735 pub fn project(&self) -> &Entity<Project> {
2736 &self.project
2737 }
2738
2739 pub fn auto_capture_telemetry(&mut self, cx: &mut Context<Self>) {
2740 if !cx.has_flag::<feature_flags::ThreadAutoCaptureFeatureFlag>() {
2741 return;
2742 }
2743
2744 let now = Instant::now();
2745 if let Some(last) = self.last_auto_capture_at {
2746 if now.duration_since(last).as_secs() < 10 {
2747 return;
2748 }
2749 }
2750
2751 self.last_auto_capture_at = Some(now);
2752
2753 let thread_id = self.id().clone();
2754 let github_login = self
2755 .project
2756 .read(cx)
2757 .user_store()
2758 .read(cx)
2759 .current_user()
2760 .map(|user| user.github_login.clone());
2761 let client = self.project.read(cx).client();
2762 let serialize_task = self.serialize(cx);
2763
2764 cx.background_executor()
2765 .spawn(async move {
2766 if let Ok(serialized_thread) = serialize_task.await {
2767 if let Ok(thread_data) = serde_json::to_value(serialized_thread) {
2768 telemetry::event!(
2769 "Agent Thread Auto-Captured",
2770 thread_id = thread_id.to_string(),
2771 thread_data = thread_data,
2772 auto_capture_reason = "tracked_user",
2773 github_login = github_login
2774 );
2775
2776 client.telemetry().flush_events().await;
2777 }
2778 }
2779 })
2780 .detach();
2781 }
2782
2783 pub fn cumulative_token_usage(&self) -> TokenUsage {
2784 self.cumulative_token_usage
2785 }
2786
2787 pub fn token_usage_up_to_message(&self, message_id: MessageId) -> TotalTokenUsage {
2788 let Some(model) = self.configured_model.as_ref() else {
2789 return TotalTokenUsage::default();
2790 };
2791
2792 let max = model.model.max_token_count();
2793
2794 let index = self
2795 .messages
2796 .iter()
2797 .position(|msg| msg.id == message_id)
2798 .unwrap_or(0);
2799
2800 if index == 0 {
2801 return TotalTokenUsage { total: 0, max };
2802 }
2803
2804 let token_usage = &self
2805 .request_token_usage
2806 .get(index - 1)
2807 .cloned()
2808 .unwrap_or_default();
2809
2810 TotalTokenUsage {
2811 total: token_usage.total_tokens(),
2812 max,
2813 }
2814 }
2815
2816 pub fn total_token_usage(&self) -> Option<TotalTokenUsage> {
2817 let model = self.configured_model.as_ref()?;
2818
2819 let max = model.model.max_token_count();
2820
2821 if let Some(exceeded_error) = &self.exceeded_window_error {
2822 if model.model.id() == exceeded_error.model_id {
2823 return Some(TotalTokenUsage {
2824 total: exceeded_error.token_count,
2825 max,
2826 });
2827 }
2828 }
2829
2830 let total = self
2831 .token_usage_at_last_message()
2832 .unwrap_or_default()
2833 .total_tokens();
2834
2835 Some(TotalTokenUsage { total, max })
2836 }
2837
2838 fn token_usage_at_last_message(&self) -> Option<TokenUsage> {
2839 self.request_token_usage
2840 .get(self.messages.len().saturating_sub(1))
2841 .or_else(|| self.request_token_usage.last())
2842 .cloned()
2843 }
2844
2845 fn update_token_usage_at_last_message(&mut self, token_usage: TokenUsage) {
2846 let placeholder = self.token_usage_at_last_message().unwrap_or_default();
2847 self.request_token_usage
2848 .resize(self.messages.len(), placeholder);
2849
2850 if let Some(last) = self.request_token_usage.last_mut() {
2851 *last = token_usage;
2852 }
2853 }
2854
2855 fn update_model_request_usage(&self, amount: u32, limit: UsageLimit, cx: &mut Context<Self>) {
2856 self.project.update(cx, |project, cx| {
2857 project.user_store().update(cx, |user_store, cx| {
2858 user_store.update_model_request_usage(
2859 ModelRequestUsage(RequestUsage {
2860 amount: amount as i32,
2861 limit,
2862 }),
2863 cx,
2864 )
2865 })
2866 });
2867 }
2868
2869 pub fn deny_tool_use(
2870 &mut self,
2871 tool_use_id: LanguageModelToolUseId,
2872 tool_name: Arc<str>,
2873 window: Option<AnyWindowHandle>,
2874 cx: &mut Context<Self>,
2875 ) {
2876 let err = Err(anyhow::anyhow!(
2877 "Permission to run tool action denied by user"
2878 ));
2879
2880 self.tool_use.insert_tool_output(
2881 tool_use_id.clone(),
2882 tool_name,
2883 err,
2884 self.configured_model.as_ref(),
2885 );
2886 self.tool_finished(tool_use_id.clone(), None, true, window, cx);
2887 }
2888}
2889
2890#[derive(Debug, Clone, Error)]
2891pub enum ThreadError {
2892 #[error("Payment required")]
2893 PaymentRequired,
2894 #[error("Model request limit reached")]
2895 ModelRequestLimitReached { plan: Plan },
2896 #[error("Message {header}: {message}")]
2897 Message {
2898 header: SharedString,
2899 message: SharedString,
2900 },
2901}
2902
2903#[derive(Debug, Clone)]
2904pub enum ThreadEvent {
2905 ShowError(ThreadError),
2906 StreamedCompletion,
2907 ReceivedTextChunk,
2908 NewRequest,
2909 StreamedAssistantText(MessageId, String),
2910 StreamedAssistantThinking(MessageId, String),
2911 StreamedToolUse {
2912 tool_use_id: LanguageModelToolUseId,
2913 ui_text: Arc<str>,
2914 input: serde_json::Value,
2915 },
2916 MissingToolUse {
2917 tool_use_id: LanguageModelToolUseId,
2918 ui_text: Arc<str>,
2919 },
2920 InvalidToolInput {
2921 tool_use_id: LanguageModelToolUseId,
2922 ui_text: Arc<str>,
2923 invalid_input_json: Arc<str>,
2924 },
2925 Stopped(Result<StopReason, Arc<anyhow::Error>>),
2926 MessageAdded(MessageId),
2927 MessageEdited(MessageId),
2928 MessageDeleted(MessageId),
2929 SummaryGenerated,
2930 SummaryChanged,
2931 UsePendingTools {
2932 tool_uses: Vec<PendingToolUse>,
2933 },
2934 ToolFinished {
2935 #[allow(unused)]
2936 tool_use_id: LanguageModelToolUseId,
2937 /// The pending tool use that corresponds to this tool.
2938 pending_tool_use: Option<PendingToolUse>,
2939 },
2940 CheckpointChanged,
2941 ToolConfirmationNeeded,
2942 ToolUseLimitReached,
2943 CancelEditing,
2944 CompletionCanceled,
2945 ProfileChanged,
2946}
2947
2948impl EventEmitter<ThreadEvent> for Thread {}
2949
2950struct PendingCompletion {
2951 id: usize,
2952 queue_state: QueueState,
2953 _task: Task<()>,
2954}
2955
2956/// Resolves tool name conflicts by ensuring all tool names are unique.
2957///
2958/// When multiple tools have the same name, this function applies the following rules:
2959/// 1. Native tools always keep their original name
2960/// 2. Context server tools get prefixed with their server ID and an underscore
2961/// 3. All tool names are truncated to MAX_TOOL_NAME_LENGTH (64 characters)
2962/// 4. If conflicts still exist after prefixing, the conflicting tools are filtered out
2963///
2964/// Note: This function assumes that built-in tools occur before MCP tools in the tools list.
2965fn resolve_tool_name_conflicts(tools: &[Arc<dyn Tool>]) -> Vec<(String, Arc<dyn Tool>)> {
2966 fn resolve_tool_name(tool: &Arc<dyn Tool>) -> String {
2967 let mut tool_name = tool.name();
2968 tool_name.truncate(MAX_TOOL_NAME_LENGTH);
2969 tool_name
2970 }
2971
2972 const MAX_TOOL_NAME_LENGTH: usize = 64;
2973
2974 let mut duplicated_tool_names = HashSet::default();
2975 let mut seen_tool_names = HashSet::default();
2976 for tool in tools {
2977 let tool_name = resolve_tool_name(tool);
2978 if seen_tool_names.contains(&tool_name) {
2979 debug_assert!(
2980 tool.source() != assistant_tool::ToolSource::Native,
2981 "There are two built-in tools with the same name: {}",
2982 tool_name
2983 );
2984 duplicated_tool_names.insert(tool_name);
2985 } else {
2986 seen_tool_names.insert(tool_name);
2987 }
2988 }
2989
2990 if duplicated_tool_names.is_empty() {
2991 return tools
2992 .into_iter()
2993 .map(|tool| (resolve_tool_name(tool), tool.clone()))
2994 .collect();
2995 }
2996
2997 tools
2998 .into_iter()
2999 .filter_map(|tool| {
3000 let mut tool_name = resolve_tool_name(tool);
3001 if !duplicated_tool_names.contains(&tool_name) {
3002 return Some((tool_name, tool.clone()));
3003 }
3004 match tool.source() {
3005 assistant_tool::ToolSource::Native => {
3006 // Built-in tools always keep their original name
3007 Some((tool_name, tool.clone()))
3008 }
3009 assistant_tool::ToolSource::ContextServer { id } => {
3010 // Context server tools are prefixed with the context server ID, and truncated if necessary
3011 tool_name.insert(0, '_');
3012 if tool_name.len() + id.len() > MAX_TOOL_NAME_LENGTH {
3013 let len = MAX_TOOL_NAME_LENGTH - tool_name.len();
3014 let mut id = id.to_string();
3015 id.truncate(len);
3016 tool_name.insert_str(0, &id);
3017 } else {
3018 tool_name.insert_str(0, &id);
3019 }
3020
3021 tool_name.truncate(MAX_TOOL_NAME_LENGTH);
3022
3023 if seen_tool_names.contains(&tool_name) {
3024 log::error!("Cannot resolve tool name conflict for tool {}", tool.name());
3025 None
3026 } else {
3027 Some((tool_name, tool.clone()))
3028 }
3029 }
3030 }
3031 })
3032 .collect()
3033}
3034
3035#[cfg(test)]
3036mod tests {
3037 use super::*;
3038 use crate::{
3039 context::load_context, context_store::ContextStore, thread_store, thread_store::ThreadStore,
3040 };
3041 use agent_settings::{AgentProfileId, AgentSettings, LanguageModelParameters};
3042 use assistant_tool::ToolRegistry;
3043 use gpui::TestAppContext;
3044 use icons::IconName;
3045 use language_model::fake_provider::{FakeLanguageModel, FakeLanguageModelProvider};
3046 use project::{FakeFs, Project};
3047 use prompt_store::PromptBuilder;
3048 use serde_json::json;
3049 use settings::{Settings, SettingsStore};
3050 use std::sync::Arc;
3051 use theme::ThemeSettings;
3052 use util::path;
3053 use workspace::Workspace;
3054
3055 #[gpui::test]
3056 async fn test_message_with_context(cx: &mut TestAppContext) {
3057 init_test_settings(cx);
3058
3059 let project = create_test_project(
3060 cx,
3061 json!({"code.rs": "fn main() {\n println!(\"Hello, world!\");\n}"}),
3062 )
3063 .await;
3064
3065 let (_workspace, _thread_store, thread, context_store, model) =
3066 setup_test_environment(cx, project.clone()).await;
3067
3068 add_file_to_context(&project, &context_store, "test/code.rs", cx)
3069 .await
3070 .unwrap();
3071
3072 let context =
3073 context_store.read_with(cx, |store, _| store.context().next().cloned().unwrap());
3074 let loaded_context = cx
3075 .update(|cx| load_context(vec![context], &project, &None, cx))
3076 .await;
3077
3078 // Insert user message with context
3079 let message_id = thread.update(cx, |thread, cx| {
3080 thread.insert_user_message(
3081 "Please explain this code",
3082 loaded_context,
3083 None,
3084 Vec::new(),
3085 cx,
3086 )
3087 });
3088
3089 // Check content and context in message object
3090 let message = thread.read_with(cx, |thread, _| thread.message(message_id).unwrap().clone());
3091
3092 // Use different path format strings based on platform for the test
3093 #[cfg(windows)]
3094 let path_part = r"test\code.rs";
3095 #[cfg(not(windows))]
3096 let path_part = "test/code.rs";
3097
3098 let expected_context = format!(
3099 r#"
3100<context>
3101The following items were attached by the user. They are up-to-date and don't need to be re-read.
3102
3103<files>
3104```rs {path_part}
3105fn main() {{
3106 println!("Hello, world!");
3107}}
3108```
3109</files>
3110</context>
3111"#
3112 );
3113
3114 assert_eq!(message.role, Role::User);
3115 assert_eq!(message.segments.len(), 1);
3116 assert_eq!(
3117 message.segments[0],
3118 MessageSegment::Text("Please explain this code".to_string())
3119 );
3120 assert_eq!(message.loaded_context.text, expected_context);
3121
3122 // Check message in request
3123 let request = thread.update(cx, |thread, cx| {
3124 thread.to_completion_request(model.clone(), CompletionIntent::UserPrompt, cx)
3125 });
3126
3127 assert_eq!(request.messages.len(), 2);
3128 let expected_full_message = format!("{}Please explain this code", expected_context);
3129 assert_eq!(request.messages[1].string_contents(), expected_full_message);
3130 }
3131
3132 #[gpui::test]
3133 async fn test_only_include_new_contexts(cx: &mut TestAppContext) {
3134 init_test_settings(cx);
3135
3136 let project = create_test_project(
3137 cx,
3138 json!({
3139 "file1.rs": "fn function1() {}\n",
3140 "file2.rs": "fn function2() {}\n",
3141 "file3.rs": "fn function3() {}\n",
3142 "file4.rs": "fn function4() {}\n",
3143 }),
3144 )
3145 .await;
3146
3147 let (_, _thread_store, thread, context_store, model) =
3148 setup_test_environment(cx, project.clone()).await;
3149
3150 // First message with context 1
3151 add_file_to_context(&project, &context_store, "test/file1.rs", cx)
3152 .await
3153 .unwrap();
3154 let new_contexts = context_store.update(cx, |store, cx| {
3155 store.new_context_for_thread(thread.read(cx), None)
3156 });
3157 assert_eq!(new_contexts.len(), 1);
3158 let loaded_context = cx
3159 .update(|cx| load_context(new_contexts, &project, &None, cx))
3160 .await;
3161 let message1_id = thread.update(cx, |thread, cx| {
3162 thread.insert_user_message("Message 1", loaded_context, None, Vec::new(), cx)
3163 });
3164
3165 // Second message with contexts 1 and 2 (context 1 should be skipped as it's already included)
3166 add_file_to_context(&project, &context_store, "test/file2.rs", cx)
3167 .await
3168 .unwrap();
3169 let new_contexts = context_store.update(cx, |store, cx| {
3170 store.new_context_for_thread(thread.read(cx), None)
3171 });
3172 assert_eq!(new_contexts.len(), 1);
3173 let loaded_context = cx
3174 .update(|cx| load_context(new_contexts, &project, &None, cx))
3175 .await;
3176 let message2_id = thread.update(cx, |thread, cx| {
3177 thread.insert_user_message("Message 2", loaded_context, None, Vec::new(), cx)
3178 });
3179
3180 // Third message with all three contexts (contexts 1 and 2 should be skipped)
3181 //
3182 add_file_to_context(&project, &context_store, "test/file3.rs", cx)
3183 .await
3184 .unwrap();
3185 let new_contexts = context_store.update(cx, |store, cx| {
3186 store.new_context_for_thread(thread.read(cx), None)
3187 });
3188 assert_eq!(new_contexts.len(), 1);
3189 let loaded_context = cx
3190 .update(|cx| load_context(new_contexts, &project, &None, cx))
3191 .await;
3192 let message3_id = thread.update(cx, |thread, cx| {
3193 thread.insert_user_message("Message 3", loaded_context, None, Vec::new(), cx)
3194 });
3195
3196 // Check what contexts are included in each message
3197 let (message1, message2, message3) = thread.read_with(cx, |thread, _| {
3198 (
3199 thread.message(message1_id).unwrap().clone(),
3200 thread.message(message2_id).unwrap().clone(),
3201 thread.message(message3_id).unwrap().clone(),
3202 )
3203 });
3204
3205 // First message should include context 1
3206 assert!(message1.loaded_context.text.contains("file1.rs"));
3207
3208 // Second message should include only context 2 (not 1)
3209 assert!(!message2.loaded_context.text.contains("file1.rs"));
3210 assert!(message2.loaded_context.text.contains("file2.rs"));
3211
3212 // Third message should include only context 3 (not 1 or 2)
3213 assert!(!message3.loaded_context.text.contains("file1.rs"));
3214 assert!(!message3.loaded_context.text.contains("file2.rs"));
3215 assert!(message3.loaded_context.text.contains("file3.rs"));
3216
3217 // Check entire request to make sure all contexts are properly included
3218 let request = thread.update(cx, |thread, cx| {
3219 thread.to_completion_request(model.clone(), CompletionIntent::UserPrompt, cx)
3220 });
3221
3222 // The request should contain all 3 messages
3223 assert_eq!(request.messages.len(), 4);
3224
3225 // Check that the contexts are properly formatted in each message
3226 assert!(request.messages[1].string_contents().contains("file1.rs"));
3227 assert!(!request.messages[1].string_contents().contains("file2.rs"));
3228 assert!(!request.messages[1].string_contents().contains("file3.rs"));
3229
3230 assert!(!request.messages[2].string_contents().contains("file1.rs"));
3231 assert!(request.messages[2].string_contents().contains("file2.rs"));
3232 assert!(!request.messages[2].string_contents().contains("file3.rs"));
3233
3234 assert!(!request.messages[3].string_contents().contains("file1.rs"));
3235 assert!(!request.messages[3].string_contents().contains("file2.rs"));
3236 assert!(request.messages[3].string_contents().contains("file3.rs"));
3237
3238 add_file_to_context(&project, &context_store, "test/file4.rs", cx)
3239 .await
3240 .unwrap();
3241 let new_contexts = context_store.update(cx, |store, cx| {
3242 store.new_context_for_thread(thread.read(cx), Some(message2_id))
3243 });
3244 assert_eq!(new_contexts.len(), 3);
3245 let loaded_context = cx
3246 .update(|cx| load_context(new_contexts, &project, &None, cx))
3247 .await
3248 .loaded_context;
3249
3250 assert!(!loaded_context.text.contains("file1.rs"));
3251 assert!(loaded_context.text.contains("file2.rs"));
3252 assert!(loaded_context.text.contains("file3.rs"));
3253 assert!(loaded_context.text.contains("file4.rs"));
3254
3255 let new_contexts = context_store.update(cx, |store, cx| {
3256 // Remove file4.rs
3257 store.remove_context(&loaded_context.contexts[2].handle(), cx);
3258 store.new_context_for_thread(thread.read(cx), Some(message2_id))
3259 });
3260 assert_eq!(new_contexts.len(), 2);
3261 let loaded_context = cx
3262 .update(|cx| load_context(new_contexts, &project, &None, cx))
3263 .await
3264 .loaded_context;
3265
3266 assert!(!loaded_context.text.contains("file1.rs"));
3267 assert!(loaded_context.text.contains("file2.rs"));
3268 assert!(loaded_context.text.contains("file3.rs"));
3269 assert!(!loaded_context.text.contains("file4.rs"));
3270
3271 let new_contexts = context_store.update(cx, |store, cx| {
3272 // Remove file3.rs
3273 store.remove_context(&loaded_context.contexts[1].handle(), cx);
3274 store.new_context_for_thread(thread.read(cx), Some(message2_id))
3275 });
3276 assert_eq!(new_contexts.len(), 1);
3277 let loaded_context = cx
3278 .update(|cx| load_context(new_contexts, &project, &None, cx))
3279 .await
3280 .loaded_context;
3281
3282 assert!(!loaded_context.text.contains("file1.rs"));
3283 assert!(loaded_context.text.contains("file2.rs"));
3284 assert!(!loaded_context.text.contains("file3.rs"));
3285 assert!(!loaded_context.text.contains("file4.rs"));
3286 }
3287
3288 #[gpui::test]
3289 async fn test_message_without_files(cx: &mut TestAppContext) {
3290 init_test_settings(cx);
3291
3292 let project = create_test_project(
3293 cx,
3294 json!({"code.rs": "fn main() {\n println!(\"Hello, world!\");\n}"}),
3295 )
3296 .await;
3297
3298 let (_, _thread_store, thread, _context_store, model) =
3299 setup_test_environment(cx, project.clone()).await;
3300
3301 // Insert user message without any context (empty context vector)
3302 let message_id = thread.update(cx, |thread, cx| {
3303 thread.insert_user_message(
3304 "What is the best way to learn Rust?",
3305 ContextLoadResult::default(),
3306 None,
3307 Vec::new(),
3308 cx,
3309 )
3310 });
3311
3312 // Check content and context in message object
3313 let message = thread.read_with(cx, |thread, _| thread.message(message_id).unwrap().clone());
3314
3315 // Context should be empty when no files are included
3316 assert_eq!(message.role, Role::User);
3317 assert_eq!(message.segments.len(), 1);
3318 assert_eq!(
3319 message.segments[0],
3320 MessageSegment::Text("What is the best way to learn Rust?".to_string())
3321 );
3322 assert_eq!(message.loaded_context.text, "");
3323
3324 // Check message in request
3325 let request = thread.update(cx, |thread, cx| {
3326 thread.to_completion_request(model.clone(), CompletionIntent::UserPrompt, cx)
3327 });
3328
3329 assert_eq!(request.messages.len(), 2);
3330 assert_eq!(
3331 request.messages[1].string_contents(),
3332 "What is the best way to learn Rust?"
3333 );
3334
3335 // Add second message, also without context
3336 let message2_id = thread.update(cx, |thread, cx| {
3337 thread.insert_user_message(
3338 "Are there any good books?",
3339 ContextLoadResult::default(),
3340 None,
3341 Vec::new(),
3342 cx,
3343 )
3344 });
3345
3346 let message2 =
3347 thread.read_with(cx, |thread, _| thread.message(message2_id).unwrap().clone());
3348 assert_eq!(message2.loaded_context.text, "");
3349
3350 // Check that both messages appear in the request
3351 let request = thread.update(cx, |thread, cx| {
3352 thread.to_completion_request(model.clone(), CompletionIntent::UserPrompt, cx)
3353 });
3354
3355 assert_eq!(request.messages.len(), 3);
3356 assert_eq!(
3357 request.messages[1].string_contents(),
3358 "What is the best way to learn Rust?"
3359 );
3360 assert_eq!(
3361 request.messages[2].string_contents(),
3362 "Are there any good books?"
3363 );
3364 }
3365
3366 #[gpui::test]
3367 async fn test_storing_profile_setting_per_thread(cx: &mut TestAppContext) {
3368 init_test_settings(cx);
3369
3370 let project = create_test_project(
3371 cx,
3372 json!({"code.rs": "fn main() {\n println!(\"Hello, world!\");\n}"}),
3373 )
3374 .await;
3375
3376 let (_workspace, thread_store, thread, _context_store, _model) =
3377 setup_test_environment(cx, project.clone()).await;
3378
3379 // Check that we are starting with the default profile
3380 let profile = cx.read(|cx| thread.read(cx).profile.clone());
3381 let tool_set = cx.read(|cx| thread_store.read(cx).tools());
3382 assert_eq!(
3383 profile,
3384 AgentProfile::new(AgentProfileId::default(), tool_set)
3385 );
3386 }
3387
3388 #[gpui::test]
3389 async fn test_serializing_thread_profile(cx: &mut TestAppContext) {
3390 init_test_settings(cx);
3391
3392 let project = create_test_project(
3393 cx,
3394 json!({"code.rs": "fn main() {\n println!(\"Hello, world!\");\n}"}),
3395 )
3396 .await;
3397
3398 let (_workspace, thread_store, thread, _context_store, _model) =
3399 setup_test_environment(cx, project.clone()).await;
3400
3401 // Profile gets serialized with default values
3402 let serialized = thread
3403 .update(cx, |thread, cx| thread.serialize(cx))
3404 .await
3405 .unwrap();
3406
3407 assert_eq!(serialized.profile, Some(AgentProfileId::default()));
3408
3409 let deserialized = cx.update(|cx| {
3410 thread.update(cx, |thread, cx| {
3411 Thread::deserialize(
3412 thread.id.clone(),
3413 serialized,
3414 thread.project.clone(),
3415 thread.tools.clone(),
3416 thread.prompt_builder.clone(),
3417 thread.project_context.clone(),
3418 None,
3419 cx,
3420 )
3421 })
3422 });
3423 let tool_set = cx.read(|cx| thread_store.read(cx).tools());
3424
3425 assert_eq!(
3426 deserialized.profile,
3427 AgentProfile::new(AgentProfileId::default(), tool_set)
3428 );
3429 }
3430
3431 #[gpui::test]
3432 async fn test_temperature_setting(cx: &mut TestAppContext) {
3433 init_test_settings(cx);
3434
3435 let project = create_test_project(
3436 cx,
3437 json!({"code.rs": "fn main() {\n println!(\"Hello, world!\");\n}"}),
3438 )
3439 .await;
3440
3441 let (_workspace, _thread_store, thread, _context_store, model) =
3442 setup_test_environment(cx, project.clone()).await;
3443
3444 // Both model and provider
3445 cx.update(|cx| {
3446 AgentSettings::override_global(
3447 AgentSettings {
3448 model_parameters: vec![LanguageModelParameters {
3449 provider: Some(model.provider_id().0.to_string().into()),
3450 model: Some(model.id().0.clone()),
3451 temperature: Some(0.66),
3452 }],
3453 ..AgentSettings::get_global(cx).clone()
3454 },
3455 cx,
3456 );
3457 });
3458
3459 let request = thread.update(cx, |thread, cx| {
3460 thread.to_completion_request(model.clone(), CompletionIntent::UserPrompt, cx)
3461 });
3462 assert_eq!(request.temperature, Some(0.66));
3463
3464 // Only model
3465 cx.update(|cx| {
3466 AgentSettings::override_global(
3467 AgentSettings {
3468 model_parameters: vec![LanguageModelParameters {
3469 provider: None,
3470 model: Some(model.id().0.clone()),
3471 temperature: Some(0.66),
3472 }],
3473 ..AgentSettings::get_global(cx).clone()
3474 },
3475 cx,
3476 );
3477 });
3478
3479 let request = thread.update(cx, |thread, cx| {
3480 thread.to_completion_request(model.clone(), CompletionIntent::UserPrompt, cx)
3481 });
3482 assert_eq!(request.temperature, Some(0.66));
3483
3484 // Only provider
3485 cx.update(|cx| {
3486 AgentSettings::override_global(
3487 AgentSettings {
3488 model_parameters: vec![LanguageModelParameters {
3489 provider: Some(model.provider_id().0.to_string().into()),
3490 model: None,
3491 temperature: Some(0.66),
3492 }],
3493 ..AgentSettings::get_global(cx).clone()
3494 },
3495 cx,
3496 );
3497 });
3498
3499 let request = thread.update(cx, |thread, cx| {
3500 thread.to_completion_request(model.clone(), CompletionIntent::UserPrompt, cx)
3501 });
3502 assert_eq!(request.temperature, Some(0.66));
3503
3504 // Same model name, different provider
3505 cx.update(|cx| {
3506 AgentSettings::override_global(
3507 AgentSettings {
3508 model_parameters: vec![LanguageModelParameters {
3509 provider: Some("anthropic".into()),
3510 model: Some(model.id().0.clone()),
3511 temperature: Some(0.66),
3512 }],
3513 ..AgentSettings::get_global(cx).clone()
3514 },
3515 cx,
3516 );
3517 });
3518
3519 let request = thread.update(cx, |thread, cx| {
3520 thread.to_completion_request(model.clone(), CompletionIntent::UserPrompt, cx)
3521 });
3522 assert_eq!(request.temperature, None);
3523 }
3524
3525 #[gpui::test]
3526 async fn test_thread_summary(cx: &mut TestAppContext) {
3527 init_test_settings(cx);
3528
3529 let project = create_test_project(cx, json!({})).await;
3530
3531 let (_, _thread_store, thread, _context_store, model) =
3532 setup_test_environment(cx, project.clone()).await;
3533
3534 // Initial state should be pending
3535 thread.read_with(cx, |thread, _| {
3536 assert!(matches!(thread.summary(), ThreadSummary::Pending));
3537 assert_eq!(thread.summary().or_default(), ThreadSummary::DEFAULT);
3538 });
3539
3540 // Manually setting the summary should not be allowed in this state
3541 thread.update(cx, |thread, cx| {
3542 thread.set_summary("This should not work", cx);
3543 });
3544
3545 thread.read_with(cx, |thread, _| {
3546 assert!(matches!(thread.summary(), ThreadSummary::Pending));
3547 });
3548
3549 // Send a message
3550 thread.update(cx, |thread, cx| {
3551 thread.insert_user_message("Hi!", ContextLoadResult::default(), None, vec![], cx);
3552 thread.send_to_model(
3553 model.clone(),
3554 CompletionIntent::ThreadSummarization,
3555 None,
3556 cx,
3557 );
3558 });
3559
3560 let fake_model = model.as_fake();
3561 simulate_successful_response(&fake_model, cx);
3562
3563 // Should start generating summary when there are >= 2 messages
3564 thread.read_with(cx, |thread, _| {
3565 assert_eq!(*thread.summary(), ThreadSummary::Generating);
3566 });
3567
3568 // Should not be able to set the summary while generating
3569 thread.update(cx, |thread, cx| {
3570 thread.set_summary("This should not work either", cx);
3571 });
3572
3573 thread.read_with(cx, |thread, _| {
3574 assert!(matches!(thread.summary(), ThreadSummary::Generating));
3575 assert_eq!(thread.summary().or_default(), ThreadSummary::DEFAULT);
3576 });
3577
3578 cx.run_until_parked();
3579 fake_model.stream_last_completion_response("Brief");
3580 fake_model.stream_last_completion_response(" Introduction");
3581 fake_model.end_last_completion_stream();
3582 cx.run_until_parked();
3583
3584 // Summary should be set
3585 thread.read_with(cx, |thread, _| {
3586 assert!(matches!(thread.summary(), ThreadSummary::Ready(_)));
3587 assert_eq!(thread.summary().or_default(), "Brief Introduction");
3588 });
3589
3590 // Now we should be able to set a summary
3591 thread.update(cx, |thread, cx| {
3592 thread.set_summary("Brief Intro", cx);
3593 });
3594
3595 thread.read_with(cx, |thread, _| {
3596 assert_eq!(thread.summary().or_default(), "Brief Intro");
3597 });
3598
3599 // Test setting an empty summary (should default to DEFAULT)
3600 thread.update(cx, |thread, cx| {
3601 thread.set_summary("", cx);
3602 });
3603
3604 thread.read_with(cx, |thread, _| {
3605 assert!(matches!(thread.summary(), ThreadSummary::Ready(_)));
3606 assert_eq!(thread.summary().or_default(), ThreadSummary::DEFAULT);
3607 });
3608 }
3609
3610 #[gpui::test]
3611 async fn test_thread_summary_error_set_manually(cx: &mut TestAppContext) {
3612 init_test_settings(cx);
3613
3614 let project = create_test_project(cx, json!({})).await;
3615
3616 let (_, _thread_store, thread, _context_store, model) =
3617 setup_test_environment(cx, project.clone()).await;
3618
3619 test_summarize_error(&model, &thread, cx);
3620
3621 // Now we should be able to set a summary
3622 thread.update(cx, |thread, cx| {
3623 thread.set_summary("Brief Intro", cx);
3624 });
3625
3626 thread.read_with(cx, |thread, _| {
3627 assert!(matches!(thread.summary(), ThreadSummary::Ready(_)));
3628 assert_eq!(thread.summary().or_default(), "Brief Intro");
3629 });
3630 }
3631
3632 #[gpui::test]
3633 async fn test_thread_summary_error_retry(cx: &mut TestAppContext) {
3634 init_test_settings(cx);
3635
3636 let project = create_test_project(cx, json!({})).await;
3637
3638 let (_, _thread_store, thread, _context_store, model) =
3639 setup_test_environment(cx, project.clone()).await;
3640
3641 test_summarize_error(&model, &thread, cx);
3642
3643 // Sending another message should not trigger another summarize request
3644 thread.update(cx, |thread, cx| {
3645 thread.insert_user_message(
3646 "How are you?",
3647 ContextLoadResult::default(),
3648 None,
3649 vec![],
3650 cx,
3651 );
3652 thread.send_to_model(model.clone(), CompletionIntent::UserPrompt, None, cx);
3653 });
3654
3655 let fake_model = model.as_fake();
3656 simulate_successful_response(&fake_model, cx);
3657
3658 thread.read_with(cx, |thread, _| {
3659 // State is still Error, not Generating
3660 assert!(matches!(thread.summary(), ThreadSummary::Error));
3661 });
3662
3663 // But the summarize request can be invoked manually
3664 thread.update(cx, |thread, cx| {
3665 thread.summarize(cx);
3666 });
3667
3668 thread.read_with(cx, |thread, _| {
3669 assert!(matches!(thread.summary(), ThreadSummary::Generating));
3670 });
3671
3672 cx.run_until_parked();
3673 fake_model.stream_last_completion_response("A successful summary");
3674 fake_model.end_last_completion_stream();
3675 cx.run_until_parked();
3676
3677 thread.read_with(cx, |thread, _| {
3678 assert!(matches!(thread.summary(), ThreadSummary::Ready(_)));
3679 assert_eq!(thread.summary().or_default(), "A successful summary");
3680 });
3681 }
3682
3683 #[gpui::test]
3684 fn test_resolve_tool_name_conflicts() {
3685 use assistant_tool::{Tool, ToolSource};
3686
3687 assert_resolve_tool_name_conflicts(
3688 vec![
3689 TestTool::new("tool1", ToolSource::Native),
3690 TestTool::new("tool2", ToolSource::Native),
3691 TestTool::new("tool3", ToolSource::ContextServer { id: "mcp-1".into() }),
3692 ],
3693 vec!["tool1", "tool2", "tool3"],
3694 );
3695
3696 assert_resolve_tool_name_conflicts(
3697 vec![
3698 TestTool::new("tool1", ToolSource::Native),
3699 TestTool::new("tool2", ToolSource::Native),
3700 TestTool::new("tool3", ToolSource::ContextServer { id: "mcp-1".into() }),
3701 TestTool::new("tool3", ToolSource::ContextServer { id: "mcp-2".into() }),
3702 ],
3703 vec!["tool1", "tool2", "mcp-1_tool3", "mcp-2_tool3"],
3704 );
3705
3706 assert_resolve_tool_name_conflicts(
3707 vec![
3708 TestTool::new("tool1", ToolSource::Native),
3709 TestTool::new("tool2", ToolSource::Native),
3710 TestTool::new("tool3", ToolSource::Native),
3711 TestTool::new("tool3", ToolSource::ContextServer { id: "mcp-1".into() }),
3712 TestTool::new("tool3", ToolSource::ContextServer { id: "mcp-2".into() }),
3713 ],
3714 vec!["tool1", "tool2", "tool3", "mcp-1_tool3", "mcp-2_tool3"],
3715 );
3716
3717 // Test that tool with very long name is always truncated
3718 assert_resolve_tool_name_conflicts(
3719 vec![TestTool::new(
3720 "tool-with-more-then-64-characters-blah-blah-blah-blah-blah-blah-blah-blah",
3721 ToolSource::Native,
3722 )],
3723 vec!["tool-with-more-then-64-characters-blah-blah-blah-blah-blah-blah-"],
3724 );
3725
3726 // Test deduplication of tools with very long names, in this case the mcp server name should be truncated
3727 assert_resolve_tool_name_conflicts(
3728 vec![
3729 TestTool::new("tool-with-very-very-very-long-name", ToolSource::Native),
3730 TestTool::new(
3731 "tool-with-very-very-very-long-name",
3732 ToolSource::ContextServer {
3733 id: "mcp-with-very-very-very-long-name".into(),
3734 },
3735 ),
3736 ],
3737 vec![
3738 "tool-with-very-very-very-long-name",
3739 "mcp-with-very-very-very-long-_tool-with-very-very-very-long-name",
3740 ],
3741 );
3742
3743 fn assert_resolve_tool_name_conflicts(
3744 tools: Vec<TestTool>,
3745 expected: Vec<impl Into<String>>,
3746 ) {
3747 let tools: Vec<Arc<dyn Tool>> = tools
3748 .into_iter()
3749 .map(|t| Arc::new(t) as Arc<dyn Tool>)
3750 .collect();
3751 let tools = resolve_tool_name_conflicts(&tools);
3752 assert_eq!(tools.len(), expected.len());
3753 for (i, expected_name) in expected.into_iter().enumerate() {
3754 let expected_name = expected_name.into();
3755 let actual_name = &tools[i].0;
3756 assert_eq!(
3757 actual_name, &expected_name,
3758 "Expected '{}' got '{}' at index {}",
3759 expected_name, actual_name, i
3760 );
3761 }
3762 }
3763
3764 struct TestTool {
3765 name: String,
3766 source: ToolSource,
3767 }
3768
3769 impl TestTool {
3770 fn new(name: impl Into<String>, source: ToolSource) -> Self {
3771 Self {
3772 name: name.into(),
3773 source,
3774 }
3775 }
3776 }
3777
3778 impl Tool for TestTool {
3779 fn name(&self) -> String {
3780 self.name.clone()
3781 }
3782
3783 fn icon(&self) -> IconName {
3784 IconName::Ai
3785 }
3786
3787 fn may_perform_edits(&self) -> bool {
3788 false
3789 }
3790
3791 fn needs_confirmation(&self, _input: &serde_json::Value, _cx: &App) -> bool {
3792 true
3793 }
3794
3795 fn source(&self) -> ToolSource {
3796 self.source.clone()
3797 }
3798
3799 fn description(&self) -> String {
3800 "Test tool".to_string()
3801 }
3802
3803 fn ui_text(&self, _input: &serde_json::Value) -> String {
3804 "Test tool".to_string()
3805 }
3806
3807 fn run(
3808 self: Arc<Self>,
3809 _input: serde_json::Value,
3810 _request: Arc<LanguageModelRequest>,
3811 _project: Entity<Project>,
3812 _action_log: Entity<ActionLog>,
3813 _model: Arc<dyn LanguageModel>,
3814 _window: Option<AnyWindowHandle>,
3815 _cx: &mut App,
3816 ) -> assistant_tool::ToolResult {
3817 assistant_tool::ToolResult {
3818 output: Task::ready(Err(anyhow::anyhow!("No content"))),
3819 card: None,
3820 }
3821 }
3822 }
3823 }
3824
3825 fn test_summarize_error(
3826 model: &Arc<dyn LanguageModel>,
3827 thread: &Entity<Thread>,
3828 cx: &mut TestAppContext,
3829 ) {
3830 thread.update(cx, |thread, cx| {
3831 thread.insert_user_message("Hi!", ContextLoadResult::default(), None, vec![], cx);
3832 thread.send_to_model(
3833 model.clone(),
3834 CompletionIntent::ThreadSummarization,
3835 None,
3836 cx,
3837 );
3838 });
3839
3840 let fake_model = model.as_fake();
3841 simulate_successful_response(&fake_model, cx);
3842
3843 thread.read_with(cx, |thread, _| {
3844 assert!(matches!(thread.summary(), ThreadSummary::Generating));
3845 assert_eq!(thread.summary().or_default(), ThreadSummary::DEFAULT);
3846 });
3847
3848 // Simulate summary request ending
3849 cx.run_until_parked();
3850 fake_model.end_last_completion_stream();
3851 cx.run_until_parked();
3852
3853 // State is set to Error and default message
3854 thread.read_with(cx, |thread, _| {
3855 assert!(matches!(thread.summary(), ThreadSummary::Error));
3856 assert_eq!(thread.summary().or_default(), ThreadSummary::DEFAULT);
3857 });
3858 }
3859
3860 fn simulate_successful_response(fake_model: &FakeLanguageModel, cx: &mut TestAppContext) {
3861 cx.run_until_parked();
3862 fake_model.stream_last_completion_response("Assistant response");
3863 fake_model.end_last_completion_stream();
3864 cx.run_until_parked();
3865 }
3866
3867 fn init_test_settings(cx: &mut TestAppContext) {
3868 cx.update(|cx| {
3869 let settings_store = SettingsStore::test(cx);
3870 cx.set_global(settings_store);
3871 language::init(cx);
3872 Project::init_settings(cx);
3873 AgentSettings::register(cx);
3874 prompt_store::init(cx);
3875 thread_store::init(cx);
3876 workspace::init_settings(cx);
3877 language_model::init_settings(cx);
3878 ThemeSettings::register(cx);
3879 ToolRegistry::default_global(cx);
3880 });
3881 }
3882
3883 // Helper to create a test project with test files
3884 async fn create_test_project(
3885 cx: &mut TestAppContext,
3886 files: serde_json::Value,
3887 ) -> Entity<Project> {
3888 let fs = FakeFs::new(cx.executor());
3889 fs.insert_tree(path!("/test"), files).await;
3890 Project::test(fs, [path!("/test").as_ref()], cx).await
3891 }
3892
3893 async fn setup_test_environment(
3894 cx: &mut TestAppContext,
3895 project: Entity<Project>,
3896 ) -> (
3897 Entity<Workspace>,
3898 Entity<ThreadStore>,
3899 Entity<Thread>,
3900 Entity<ContextStore>,
3901 Arc<dyn LanguageModel>,
3902 ) {
3903 let (workspace, cx) =
3904 cx.add_window_view(|window, cx| Workspace::test_new(project.clone(), window, cx));
3905
3906 let thread_store = cx
3907 .update(|_, cx| {
3908 ThreadStore::load(
3909 project.clone(),
3910 cx.new(|_| ToolWorkingSet::default()),
3911 None,
3912 Arc::new(PromptBuilder::new(None).unwrap()),
3913 cx,
3914 )
3915 })
3916 .await
3917 .unwrap();
3918
3919 let thread = thread_store.update(cx, |store, cx| store.create_thread(cx));
3920 let context_store = cx.new(|_cx| ContextStore::new(project.downgrade(), None));
3921
3922 let provider = Arc::new(FakeLanguageModelProvider);
3923 let model = provider.test_model();
3924 let model: Arc<dyn LanguageModel> = Arc::new(model);
3925
3926 cx.update(|_, cx| {
3927 LanguageModelRegistry::global(cx).update(cx, |registry, cx| {
3928 registry.set_default_model(
3929 Some(ConfiguredModel {
3930 provider: provider.clone(),
3931 model: model.clone(),
3932 }),
3933 cx,
3934 );
3935 registry.set_thread_summary_model(
3936 Some(ConfiguredModel {
3937 provider,
3938 model: model.clone(),
3939 }),
3940 cx,
3941 );
3942 })
3943 });
3944
3945 (workspace, thread_store, thread, context_store, model)
3946 }
3947
3948 async fn add_file_to_context(
3949 project: &Entity<Project>,
3950 context_store: &Entity<ContextStore>,
3951 path: &str,
3952 cx: &mut TestAppContext,
3953 ) -> Result<Entity<language::Buffer>> {
3954 let buffer_path = project
3955 .read_with(cx, |project, cx| project.find_project_path(path, cx))
3956 .unwrap();
3957
3958 let buffer = project
3959 .update(cx, |project, cx| {
3960 project.open_buffer(buffer_path.clone(), cx)
3961 })
3962 .await
3963 .unwrap();
3964
3965 context_store.update(cx, |context_store, cx| {
3966 context_store.add_file_from_buffer(&buffer_path, buffer.clone(), false, cx);
3967 });
3968
3969 Ok(buffer)
3970 }
3971}