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