Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

46.7 KB
Raw Blame
use std::{ops::Range, sync::Arc};

use acp_thread::{AcpThread, AgentThreadEntry, AssistantMessageChunk, ToolCall};
use agent::ThreadStore;
use agent_client_protocol::v2::schema as acp_v2;
use agent_settings::AgentSettings;
use collections::{HashMap, HashSet};
use editor::{
    Editor, EditorEvent, EditorMode, HiddenUnstagedDiffHunkRenderer, MinimapVisibility,
    SizingBehavior,
};
use gpui::{
    AnyEntity, App, AppContext as _, Corners, Entity, EntityId, EventEmitter, FocusHandle,
    Focusable, ScrollHandle, TextStyleRefinement, WeakEntity, Window,
};
use language::language_settings::SoftWrap;
use multi_buffer::MultiBuffer;
use project::{AgentId, Project, project_settings::DiagnosticSeverity};
use rope::Point;
use settings::{Settings as _, ThinkingBlockDisplay};
use terminal_view::TerminalView;
use theme_settings::ThemeSettings;
use ui::{Context, TextSize};
use workspace::Workspace;

use crate::message_editor::{MessageEditor, MessageEditorEvent, SharedSessionCapabilities};

/// Top padding, in rems, inside agent terminals. The tool card adds it to the
/// scrollable terminal's height so the number of visible rows doesn't change.
pub(crate) const TERMINAL_CONTENT_TOP_PADDING: f32 = 0.5;

/// Maps an entry index through the removal of `removed` (a contiguous range of
/// entries), returning `None` if the index referred to a removed entry.
fn reindex_after_removal(index: usize, removed: &Range<usize>) -> Option<usize> {
    if index < removed.start {
        Some(index)
    } else if index < removed.end {
        None
    } else {
        Some(index - removed.len())
    }
}

/// How much of a tool call's content to materialize while syncing an entry.
#[derive(Clone, Copy)]
enum ToolCallSync {
    /// Live updates: materialize visible content and run terminal lifecycle.
    Live,
    /// Thread restore: skip content that is not visible.
    Restore,
    /// First view after expansion: materialize without re-running terminal
    /// lifecycle, which would collapse the card again.
    Materialize,
}

pub struct EntryViewState {
    workspace: WeakEntity<Workspace>,
    project: WeakEntity<Project>,
    thread_store: Option<Entity<ThreadStore>>,
    entries: Vec<Entry>,
    session_capabilities: SharedSessionCapabilities,
    agent_id: AgentId,
    expanded_thinking_blocks: HashSet<(usize, usize)>,
    auto_expanded_thinking_block: Option<(usize, usize)>,
    user_toggled_thinking_blocks: HashSet<(usize, usize)>,
    expanded_compactions: HashSet<usize>,
    expanded_tool_calls: HashSet<acp_v2::ToolCallId>,
    /// Subagent cards shown for an ACP parent's outgoing session messages.
    expanded_session_message_cards: HashSet<acp_v2::MessageId>,
}

impl EntryViewState {
    pub fn new(
        workspace: WeakEntity<Workspace>,
        project: WeakEntity<Project>,
        thread_store: Option<Entity<ThreadStore>>,
        session_capabilities: SharedSessionCapabilities,
        agent_id: AgentId,
    ) -> Self {
        Self {
            workspace,
            project,
            thread_store,
            entries: Vec::new(),
            session_capabilities,
            agent_id,
            expanded_thinking_blocks: HashSet::default(),
            auto_expanded_thinking_block: None,
            user_toggled_thinking_blocks: HashSet::default(),
            expanded_compactions: HashSet::default(),
            expanded_tool_calls: HashSet::default(),
            expanded_session_message_cards: HashSet::default(),
        }
    }

    pub(crate) fn is_tool_call_expanded(&self, tool_call_id: &acp_v2::ToolCallId) -> bool {
        self.expanded_tool_calls.contains(tool_call_id)
    }

    pub(crate) fn is_tool_call_content_visible(&self, tool_call: &ToolCall) -> bool {
        self.is_tool_call_expanded(&tool_call.id) || tool_call.authorization_id().is_some()
    }

    pub(crate) fn expand_tool_call(&mut self, tool_call_id: acp_v2::ToolCallId) {
        self.expanded_tool_calls.insert(tool_call_id);
    }

    pub(crate) fn collapse_tool_call(&mut self, tool_call_id: &acp_v2::ToolCallId) {
        self.expanded_tool_calls.remove(tool_call_id);
    }

    pub(crate) fn toggle_tool_call_expansion(&mut self, tool_call_id: &acp_v2::ToolCallId) {
        if !self.expanded_tool_calls.remove(tool_call_id) {
            self.expanded_tool_calls.insert(tool_call_id.clone());
        }
    }

    pub(crate) fn is_session_message_card_expanded(&self, message_id: &acp_v2::MessageId) -> bool {
        self.expanded_session_message_cards.contains(message_id)
    }

    pub(crate) fn toggle_session_message_card_expansion(&mut self, message_id: &acp_v2::MessageId) {
        if !self.expanded_session_message_cards.remove(message_id) {
            self.expanded_session_message_cards
                .insert(message_id.clone());
        }
    }

    pub(crate) fn is_compaction_expanded(&self, entry_ix: usize) -> bool {
        self.expanded_compactions.contains(&entry_ix)
    }

    pub(crate) fn collapse_compaction(&mut self, entry_ix: usize) {
        self.expanded_compactions.remove(&entry_ix);
    }

    pub(crate) fn toggle_compaction_expansion(&mut self, entry_ix: usize) {
        if !self.expanded_compactions.remove(&entry_ix) {
            self.expanded_compactions.insert(entry_ix);
        }
    }

    pub(crate) fn clear_auto_expand_tracking(&mut self) {
        self.auto_expanded_thinking_block = None;
    }

    pub(crate) fn is_auto_expanded_thinking_block(&self, key: (usize, usize)) -> bool {
        self.auto_expanded_thinking_block == Some(key)
    }

    pub(crate) fn auto_expand_streaming_thought(&mut self, thread: &AcpThread, cx: &App) -> bool {
        let thinking_display = AgentSettings::get_global(cx).thinking_display;

        if !matches!(
            thinking_display,
            ThinkingBlockDisplay::Auto | ThinkingBlockDisplay::Preview
        ) {
            return false;
        }

        let last_ix = thread.entries().len().saturating_sub(1);
        let key = match thread.entries().get(last_ix) {
            Some(AgentThreadEntry::AssistantMessage(message)) => match message.chunks.last() {
                Some(AssistantMessageChunk::Thought { .. }) => {
                    Some((last_ix, message.chunks.len() - 1))
                }
                _ => None,
            },
            _ => None,
        };

        if let Some(key) = key {
            if self.auto_expanded_thinking_block != Some(key) {
                self.auto_expanded_thinking_block = Some(key);
                self.expanded_thinking_blocks.insert(key);
                return true;
            }
        } else if self.auto_expanded_thinking_block.is_some() {
            if thinking_display == ThinkingBlockDisplay::Auto
                && let Some(key) = self.auto_expanded_thinking_block
                && !self.user_toggled_thinking_blocks.contains(&key)
            {
                self.expanded_thinking_blocks.remove(&key);
            }
            self.auto_expanded_thinking_block = None;
            return true;
        }

        false
    }

    pub(crate) fn toggle_thinking_block_expansion(&mut self, key: (usize, usize), cx: &App) {
        match AgentSettings::get_global(cx).thinking_display {
            ThinkingBlockDisplay::Auto => {
                let is_open = self.expanded_thinking_blocks.contains(&key)
                    || self.user_toggled_thinking_blocks.contains(&key);

                if is_open {
                    self.expanded_thinking_blocks.remove(&key);
                    self.user_toggled_thinking_blocks.remove(&key);
                } else {
                    self.expanded_thinking_blocks.insert(key);
                    self.user_toggled_thinking_blocks.insert(key);
                }
            }
            ThinkingBlockDisplay::Preview => {
                let is_user_expanded = self.user_toggled_thinking_blocks.contains(&key);
                let is_in_expanded_set = self.expanded_thinking_blocks.contains(&key);

                if is_user_expanded {
                    self.user_toggled_thinking_blocks.remove(&key);
                    self.expanded_thinking_blocks.remove(&key);
                } else if is_in_expanded_set {
                    self.user_toggled_thinking_blocks.insert(key);
                } else {
                    self.expanded_thinking_blocks.insert(key);
                    self.user_toggled_thinking_blocks.insert(key);
                }
            }
            ThinkingBlockDisplay::AlwaysExpanded => {
                if self.user_toggled_thinking_blocks.contains(&key) {
                    self.user_toggled_thinking_blocks.remove(&key);
                } else {
                    self.user_toggled_thinking_blocks.insert(key);
                }
            }
            ThinkingBlockDisplay::AlwaysCollapsed => {
                if self.user_toggled_thinking_blocks.contains(&key) {
                    self.user_toggled_thinking_blocks.remove(&key);
                    self.expanded_thinking_blocks.remove(&key);
                } else {
                    self.expanded_thinking_blocks.insert(key);
                    self.user_toggled_thinking_blocks.insert(key);
                }
            }
        }
    }

    pub(crate) fn thinking_block_state(&self, key: (usize, usize), cx: &App) -> (bool, bool) {
        let is_user_toggled = self.user_toggled_thinking_blocks.contains(&key);
        let is_in_expanded_set = self.expanded_thinking_blocks.contains(&key);

        match AgentSettings::get_global(cx).thinking_display {
            ThinkingBlockDisplay::Auto => {
                let is_open = is_user_toggled || is_in_expanded_set;
                (is_open, false)
            }
            ThinkingBlockDisplay::Preview => {
                let is_open = is_user_toggled || is_in_expanded_set;
                let is_constrained = is_in_expanded_set && !is_user_toggled;
                (is_open, is_constrained)
            }
            ThinkingBlockDisplay::AlwaysExpanded => (!is_user_toggled, false),
            ThinkingBlockDisplay::AlwaysCollapsed => (is_user_toggled, false),
        }
    }

    pub fn entry(&self, index: usize) -> Option<&Entry> {
        self.entries.get(index)
    }

    pub fn sync_entry(
        &mut self,
        index: usize,
        thread: &Entity<AcpThread>,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.sync_entry_inner(index, thread, window, ToolCallSync::Live, cx);
    }

    /// Like [`Self::sync_entry`], but skips the views of tool calls that are not
    /// visible. Used when restoring a thread's history.
    pub fn sync_entry_initial(
        &mut self,
        index: usize,
        thread: &Entity<AcpThread>,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.sync_entry_inner(index, thread, window, ToolCallSync::Restore, cx);
    }

    /// Materializes a tool call's diff and terminal views without running the
    /// terminal lifecycle, which would collapse an expanded card again.
    pub fn materialize_entry(
        &mut self,
        index: usize,
        thread: &Entity<AcpThread>,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.sync_entry_inner(index, thread, window, ToolCallSync::Materialize, cx);
    }

    fn sync_entry_inner(
        &mut self,
        index: usize,
        thread: &Entity<AcpThread>,
        window: &mut Window,
        tool_call_sync: ToolCallSync,
        cx: &mut Context<Self>,
    ) {
        let materialize_tool_calls = matches!(
            tool_call_sync,
            ToolCallSync::Live | ToolCallSync::Materialize
        );
        let run_terminal_lifecycle =
            matches!(tool_call_sync, ToolCallSync::Live | ToolCallSync::Restore);
        let Some(thread_entry) = thread.read(cx).entries().get(index) else {
            return;
        };

        match thread_entry {
            AgentThreadEntry::UserMessage(message) => {
                let can_rewind = thread
                    .read(cx)
                    .can_rewind_to(message.client_id.as_ref(), cx);
                let source_blocks = message.content.source_blocks();
                let source_version = message.content.source_version();
                let source_is_representable = source_blocks
                    .iter()
                    .all(acp_thread::content::can_convert_to_v1);
                let is_editable = can_rewind && source_is_representable;
                if let Some(Entry::UserMessage {
                    editor,
                    synced_source_version,
                }) = self.entries.get_mut(index)
                {
                    // Read-only messages cannot hold drafts, so focus must not
                    // block new content, nor should unchanged content reset their selection.
                    let was_read_only = editor.read(cx).editor().read(cx).read_only(cx);
                    let refreshed_source = ((!was_read_only
                        || *synced_source_version != source_version)
                        && (!is_editable || !editor.focus_handle(cx).is_focused(window)))
                    .then(|| source_blocks.to_vec());
                    editor.update(cx, |editor, cx| editor.set_read_only(!is_editable, cx));
                    if let Some(source_blocks) = refreshed_source {
                        editor.update(cx, |editor, cx| {
                            editor.set_source_message(source_blocks, window, cx);
                        });
                        *synced_source_version = source_version;
                    }
                } else {
                    let source_blocks = source_blocks.to_vec();
                    let message_editor = cx.new(|cx| {
                        let mut editor = MessageEditor::new(
                            self.workspace.clone(),
                            self.project.clone(),
                            self.thread_store.clone(),
                            self.session_capabilities.clone(),
                            self.agent_id.clone(),
                            "Edit message - @ to include context",
                            editor::EditorMode::AutoHeight {
                                min_lines: 1,
                                max_lines: None,
                            },
                            window,
                            cx,
                        );
                        if !is_editable {
                            editor.set_read_only(true, cx);
                        }
                        editor.set_source_message(source_blocks, window, cx);
                        editor
                    });
                    cx.subscribe(&message_editor, move |_, editor, event, cx| {
                        cx.emit(EntryViewEvent {
                            entry_index: index,
                            view_event: ViewEvent::MessageEditorEvent(editor, event.clone()),
                        })
                    })
                    .detach();
                    self.set_entry(
                        index,
                        Entry::UserMessage {
                            editor: message_editor,
                            synced_source_version: source_version,
                        },
                    );
                }
            }
            AgentThreadEntry::ToolCall(tool_call) => {
                let id = tool_call.id.clone();
                let materialize_content =
                    materialize_tool_calls || self.is_tool_call_content_visible(tool_call);
                let terminals = tool_call.terminals().cloned().collect::<Vec<_>>();
                let diffs = tool_call.diffs().cloned().collect::<Vec<_>>();
                let patch_hunk_buffers = tool_call
                    .content()
                    .iter()
                    .filter_map(|content| match content {
                        acp_thread::ToolCallContent::DiffPatch { render, .. } => {
                            Some(&render.files)
                        }
                        _ => None,
                    })
                    .flat_map(|files| files.iter().flat_map(|file| &file.hunks))
                    .map(|hunk| hunk.buffer.clone())
                    .collect::<Vec<_>>();
                let patch_hunk_ids: HashSet<_> = patch_hunk_buffers
                    .iter()
                    .map(|buffer| buffer.entity_id())
                    .collect();

                let tool_call_entry =
                    if let Some(Entry::ToolCall(tool_call)) = self.entries.get_mut(index) {
                        tool_call
                    } else {
                        self.set_entry(
                            index,
                            Entry::ToolCall(ToolCallEntry {
                                content: HashMap::default(),
                                patch_hunk_ids: HashSet::default(),
                                restored_view_ids: HashSet::default(),
                                focus_handle: cx.focus_handle(),
                            }),
                        );
                        let Some(Entry::ToolCall(tool_call)) = self.entries.get_mut(index) else {
                            unreachable!()
                        };
                        tool_call
                    };
                let ToolCallEntry {
                    content: views,
                    restored_view_ids,
                    ..
                } = tool_call_entry;

                let is_tool_call_completed =
                    matches!(tool_call.status(), acp_thread::ToolCallStatus::Completed);
                let emit_new_view_events = matches!(tool_call_sync, ToolCallSync::Live);

                if materialize_content {
                    for terminal in terminals {
                        let terminal_id = terminal.entity_id();
                        match views.entry(terminal_id) {
                            collections::hash_map::Entry::Vacant(entry) => {
                                let element = create_terminal(
                                    self.workspace.clone(),
                                    self.project.clone(),
                                    terminal.clone(),
                                    window,
                                    cx,
                                )
                                .into_any();
                                let was_restored = restored_view_ids.remove(&terminal_id);
                                if emit_new_view_events && !was_restored {
                                    cx.emit(EntryViewEvent {
                                        entry_index: index,
                                        view_event: ViewEvent::NewTerminal(id.clone()),
                                    });
                                }
                                entry.insert(element);
                            }
                            collections::hash_map::Entry::Occupied(_entry) => {
                                let terminal = terminal.read(cx);
                                if run_terminal_lifecycle
                                    && is_tool_call_completed
                                    && terminal.is_process_backed()
                                    && terminal.output().is_none()
                                {
                                    cx.emit(EntryViewEvent {
                                        entry_index: index,
                                        view_event: ViewEvent::TerminalMovedToBackground(
                                            id.clone(),
                                        ),
                                    });
                                }
                            }
                        }
                    }

                    for diff in diffs {
                        let diff_id = diff.entity_id();
                        if let collections::hash_map::Entry::Vacant(entry) = views.entry(diff_id) {
                            let multibuffer = diff.update(cx, |diff, cx| diff.multibuffer(cx));
                            let editor =
                                create_multibuffer_diff_editor(multibuffer.clone(), window, cx);
                            cx.subscribe(&editor, {
                                let diff = diff.clone();
                                let entry_index = index;
                                move |_this, _editor, event: &EditorEvent, cx| {
                                    if let EditorEvent::OpenExcerptsRequested {
                                        selections_by_buffer,
                                        split,
                                    } = event
                                    {
                                        if let Some((buffer_id, (ranges, _))) =
                                            selections_by_buffer.iter().next()
                                        {
                                            if let Some(buffer) =
                                                multibuffer.read(cx).buffer(*buffer_id)
                                            {
                                                if let Some(range) = ranges.first() {
                                                    let point = buffer
                                                        .read(cx)
                                                        .offset_to_point(range.start.0);
                                                    if let Some(path) = diff.read(cx).file_path(cx)
                                                    {
                                                        cx.emit(EntryViewEvent {
                                                            entry_index,
                                                            view_event:
                                                                ViewEvent::OpenDiffLocation {
                                                                    path,
                                                                    position: point,
                                                                    split: *split,
                                                                },
                                                        });
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            })
                            .detach();
                            let was_restored = restored_view_ids.remove(&diff_id);
                            if emit_new_view_events && !was_restored {
                                cx.emit(EntryViewEvent {
                                    entry_index: index,
                                    view_event: ViewEvent::NewDiff(id.clone()),
                                });
                            }
                            entry.insert(editor.into_any());
                        }
                    }
                    for buffer in patch_hunk_buffers {
                        let buffer_id = buffer.entity_id();
                        if let collections::hash_map::Entry::Vacant(entry) = views.entry(buffer_id)
                        {
                            let editor = create_multibuffer_diff_editor(buffer, window, cx);
                            let was_restored = restored_view_ids.remove(&buffer_id);
                            if emit_new_view_events && !was_restored {
                                cx.emit(EntryViewEvent {
                                    entry_index: index,
                                    view_event: ViewEvent::NewDiff(id.clone()),
                                });
                            }
                            entry.insert(editor.into_any());
                        }
                    }
                } else {
                    restored_view_ids.extend(terminals.iter().map(|terminal| terminal.entity_id()));
                    restored_view_ids.extend(diffs.iter().map(|diff| diff.entity_id()));
                    restored_view_ids.extend(patch_hunk_ids.iter().copied());
                }
                if let Some(Entry::ToolCall(entry)) = self.entries.get_mut(index) {
                    for stale_id in entry.patch_hunk_ids.difference(&patch_hunk_ids) {
                        entry.content.remove(stale_id);
                        entry.restored_view_ids.remove(stale_id);
                    }
                    entry.patch_hunk_ids = patch_hunk_ids;
                }
            }
            AgentThreadEntry::Elicitation(_) => {
                if !matches!(self.entries.get(index), Some(Entry::Elicitation { .. })) {
                    self.set_entry(
                        index,
                        Entry::Elicitation {
                            focus_handle: cx.focus_handle(),
                        },
                    );
                }
            }
            AgentThreadEntry::SessionMessage(_) => {
                if !matches!(self.entries.get(index), Some(Entry::SessionMessage { .. })) {
                    self.set_entry(
                        index,
                        Entry::SessionMessage {
                            focus_handle: cx.focus_handle(),
                        },
                    );
                }
            }
            AgentThreadEntry::AssistantMessage(message) => {
                let entry = if let Some(Entry::AssistantMessage(entry)) =
                    self.entries.get_mut(index)
                {
                    entry
                } else {
                    self.set_entry(
                        index,
                        Entry::AssistantMessage(AssistantMessageEntry {
                            scroll_handles_by_chunk_index: HashMap::default(),
                            last_thought_source_version: None,
                            focus_handle: cx.focus_handle(),
                        }),
                    );
                    let Some(Entry::AssistantMessage(entry)) = self.entries.get_mut(index) else {
                        unreachable!()
                    };
                    entry
                };
                entry.sync(message);
            }
            AgentThreadEntry::ContextCompaction(_) => {
                if !matches!(self.entries.get(index), Some(Entry::ContextCompaction)) {
                    self.set_entry(index, Entry::ContextCompaction);
                }
            }
        };
    }

    fn set_entry(&mut self, index: usize, entry: Entry) {
        if index == self.entries.len() {
            self.entries.push(entry);
        } else {
            self.entries[index] = entry;
        }
    }

    pub fn remove(&mut self, range: Range<usize>) {
        self.entries.drain(range.clone());

        self.expanded_compactions = self
            .expanded_compactions
            .iter()
            .filter_map(|&entry_ix| reindex_after_removal(entry_ix, &range))
            .collect();
        self.expanded_thinking_blocks = self
            .expanded_thinking_blocks
            .iter()
            .filter_map(|&(entry_ix, chunk_ix)| {
                reindex_after_removal(entry_ix, &range).map(|entry_ix| (entry_ix, chunk_ix))
            })
            .collect();
        self.user_toggled_thinking_blocks = self
            .user_toggled_thinking_blocks
            .iter()
            .filter_map(|&(entry_ix, chunk_ix)| {
                reindex_after_removal(entry_ix, &range).map(|entry_ix| (entry_ix, chunk_ix))
            })
            .collect();
        self.auto_expanded_thinking_block =
            self.auto_expanded_thinking_block
                .and_then(|(entry_ix, chunk_ix)| {
                    reindex_after_removal(entry_ix, &range).map(|entry_ix| (entry_ix, chunk_ix))
                });
    }

    pub fn agent_ui_font_size_changed(&mut self, cx: &mut App) {
        for entry in self.entries.iter() {
            match entry {
                Entry::UserMessage { .. }
                | Entry::AssistantMessage { .. }
                | Entry::SessionMessage { .. }
                | Entry::Elicitation { .. }
                | Entry::ContextCompaction => {}
                Entry::ToolCall(ToolCallEntry { content, .. }) => {
                    for view in content.values() {
                        if let Ok(diff_editor) = view.clone().downcast::<Editor>() {
                            diff_editor.update(cx, |diff_editor, cx| {
                                diff_editor.set_text_style_refinement(
                                    diff_editor_text_style_refinement(cx),
                                );
                                cx.notify();
                            })
                        }
                    }
                }
            }
        }
    }
}

impl EventEmitter<EntryViewEvent> for EntryViewState {}

pub struct EntryViewEvent {
    pub entry_index: usize,
    pub view_event: ViewEvent,
}

pub enum ViewEvent {
    NewDiff(acp_v2::ToolCallId),
    NewTerminal(acp_v2::ToolCallId),
    TerminalMovedToBackground(acp_v2::ToolCallId),
    MessageEditorEvent(Entity<MessageEditor>, MessageEditorEvent),
    OpenDiffLocation {
        path: String,
        position: Point,
        split: bool,
    },
}

#[derive(Debug)]
pub struct AssistantMessageEntry {
    scroll_handles_by_chunk_index: HashMap<usize, ScrollHandle>,
    last_thought_source_version: Option<acp_thread::MessageContentVersion>,
    focus_handle: FocusHandle,
}

impl AssistantMessageEntry {
    pub fn scroll_handle_for_chunk(&self, ix: usize) -> Option<ScrollHandle> {
        self.scroll_handles_by_chunk_index.get(&ix).cloned()
    }

    pub fn sync(&mut self, message: &acp_thread::AssistantMessage) {
        if let Some(acp_thread::AssistantMessageChunk::Thought { block, .. }) =
            message.chunks.last()
            && self.last_thought_source_version != Some(block.source_version())
        {
            let ix = message.chunks.len() - 1;
            let handle = self.scroll_handles_by_chunk_index.entry(ix).or_default();
            handle.scroll_to_bottom();
            self.last_thought_source_version = Some(block.source_version());
        }
    }
}

#[derive(Debug)]
pub struct ToolCallEntry {
    content: HashMap<EntityId, AnyEntity>,
    patch_hunk_ids: HashSet<EntityId>,
    restored_view_ids: HashSet<EntityId>,
    focus_handle: FocusHandle,
}

#[derive(Debug)]
pub enum Entry {
    UserMessage {
        editor: Entity<MessageEditor>,
        synced_source_version: acp_thread::MessageContentVersion,
    },
    AssistantMessage(AssistantMessageEntry),
    SessionMessage {
        focus_handle: FocusHandle,
    },
    ToolCall(ToolCallEntry),
    Elicitation {
        focus_handle: FocusHandle,
    },
    ContextCompaction,
}

impl Entry {
    pub fn focus_handle(&self, cx: &App) -> Option<FocusHandle> {
        match self {
            Self::UserMessage { editor, .. } => Some(editor.read(cx).focus_handle(cx)),
            Self::AssistantMessage(message) => Some(message.focus_handle.clone()),
            Self::SessionMessage { focus_handle } => Some(focus_handle.clone()),
            Self::ToolCall(tool_call) => Some(tool_call.focus_handle.clone()),
            Self::Elicitation { focus_handle } => Some(focus_handle.clone()),
            Self::ContextCompaction => None,
        }
    }

    pub fn message_editor(&self) -> Option<&Entity<MessageEditor>> {
        match self {
            Self::UserMessage { editor, .. } => Some(editor),
            Self::AssistantMessage(_)
            | Self::SessionMessage { .. }
            | Self::ToolCall(_)
            | Self::Elicitation { .. }
            | Self::ContextCompaction => None,
        }
    }

    pub fn editor_for_diff(&self, diff: &Entity<acp_thread::Diff>) -> Option<Entity<Editor>> {
        self.content_map()?
            .get(&diff.entity_id())
            .cloned()
            .and_then(|entity| entity.downcast::<Editor>().ok())
    }

    pub fn editor_for_patch_hunk(&self, buffer: &Entity<MultiBuffer>) -> Option<Entity<Editor>> {
        self.content_map()?
            .get(&buffer.entity_id())
            .cloned()
            .and_then(|entity| entity.downcast::<Editor>().ok())
    }

    pub fn terminal(
        &self,
        terminal: &Entity<acp_thread::Terminal>,
    ) -> Option<Entity<TerminalView>> {
        self.content_map()?
            .get(&terminal.entity_id())
            .cloned()
            .and_then(|entity| entity.downcast::<TerminalView>().ok())
    }

    pub fn scroll_handle_for_assistant_message_chunk(
        &self,
        chunk_ix: usize,
    ) -> Option<ScrollHandle> {
        match self {
            Self::AssistantMessage(message) => message.scroll_handle_for_chunk(chunk_ix),
            Self::UserMessage { .. }
            | Self::SessionMessage { .. }
            | Self::ToolCall(_)
            | Self::Elicitation { .. }
            | Self::ContextCompaction => None,
        }
    }

    fn content_map(&self) -> Option<&HashMap<EntityId, AnyEntity>> {
        match self {
            Self::ToolCall(ToolCallEntry { content, .. }) => Some(content),
            _ => None,
        }
    }

    #[cfg(test)]
    pub fn has_content(&self) -> bool {
        match self {
            Self::ToolCall(ToolCallEntry { content, .. }) => !content.is_empty(),
            Self::UserMessage { .. }
            | Self::AssistantMessage(_)
            | Self::SessionMessage { .. }
            | Self::Elicitation { .. }
            | Self::ContextCompaction => false,
        }
    }
}

impl Focusable for ToolCallEntry {
    fn focus_handle(&self, _cx: &App) -> FocusHandle {
        self.focus_handle.clone()
    }
}

impl Focusable for Entry {
    fn focus_handle(&self, cx: &App) -> FocusHandle {
        match self {
            Self::UserMessage { editor, .. } => editor.read(cx).focus_handle(cx),
            Self::AssistantMessage(message) => message.focus_handle.clone(),
            Self::SessionMessage { focus_handle } => focus_handle.clone(),
            Self::ToolCall(tool_call) => tool_call.focus_handle.clone(),
            Self::Elicitation { focus_handle } => focus_handle.clone(),
            Self::ContextCompaction => cx.focus_handle(),
        }
    }
}

fn create_terminal(
    workspace: WeakEntity<Workspace>,
    project: WeakEntity<Project>,
    terminal: Entity<acp_thread::Terminal>,
    window: &mut Window,
    cx: &mut App,
) -> Entity<TerminalView> {
    cx.new(|cx| {
        let read_only = !terminal.read(cx).is_process_backed();
        let mut view = TerminalView::new(
            terminal.read(cx).inner().clone(),
            workspace,
            None,
            project,
            window,
            cx,
        )
        .with_read_only(read_only);

        // GPUI can't clip children to rounded corners, so the terminal has to
        // round its own background to avoid painting over the corners of the
        // tool card it sits in.
        // This matches the `rounded_md`/`rounded_b_md` on that card, which GPUI
        // doesn't expose as a value, so if the card's corner radii ever change,
        // this also needs to be updated.
        view.set_background_corner_radii(
            Some(Corners {
                bottom_left: gpui::rems(0.375),
                bottom_right: gpui::rems(0.375),
                ..Default::default()
            }),
            cx,
        );

        view.set_content_top_padding(gpui::rems(TERMINAL_CONTENT_TOP_PADDING), cx);
        view.set_embedded_mode(Some(1000), cx);
        view
    })
}

fn create_multibuffer_diff_editor(
    multibuffer: Entity<MultiBuffer>,
    window: &mut Window,
    cx: &mut App,
) -> Entity<Editor> {
    cx.new(|cx| {
        let mut editor = Editor::new(
            EditorMode::Full {
                scale_ui_elements_with_buffer_font_size: false,
                show_active_line_background: false,
                sizing_behavior: SizingBehavior::SizeByContent,
            },
            multibuffer,
            None,
            window,
            cx,
        );
        editor.set_show_gutter(false, cx);
        editor.disable_diagnostics(cx);
        editor.set_max_diagnostics_severity(DiagnosticSeverity::Off, cx);
        editor.disable_expand_excerpt_buttons(cx);
        editor.set_show_vertical_scrollbar(false, cx);
        editor.set_minimap_visibility(MinimapVisibility::Disabled, window, cx);
        editor.set_soft_wrap_mode(SoftWrap::None, cx);
        editor.set_forbid_vertical_scroll(true);
        editor.set_show_indent_guides(false, cx);
        editor.set_read_only(true);
        editor.set_delegate_open_excerpts(true);
        editor.set_show_bookmarks(false, cx);
        editor.set_show_breakpoints(false, cx);
        editor.set_show_code_actions(false, cx);
        editor.set_show_git_diff_gutter(false, cx);
        editor.set_expand_all_diff_hunks(cx);
        editor.set_diff_hunk_renderer(Some(Arc::new(HiddenUnstagedDiffHunkRenderer)), cx);
        editor.set_text_style_refinement(diff_editor_text_style_refinement(cx));
        editor
    })
}

fn diff_editor_text_style_refinement(cx: &mut App) -> TextStyleRefinement {
    TextStyleRefinement {
        font_size: Some(
            TextSize::Small
                .rems(cx)
                .to_pixels(ThemeSettings::get_global(cx).agent_ui_font_size(cx))
                .into(),
        ),
        ..Default::default()
    }
}

#[cfg(test)]
mod tests {
    use std::cell::Cell;
    use std::path::Path;
    use std::rc::Rc;
    use std::sync::Arc;

    use acp_thread::{AgentConnection, StubAgentConnection};
    use agent_client_protocol::{v1::schema as acp_v1, v2::schema as acp_v2};
    use buffer_diff::{DiffHunkStatus, DiffHunkStatusKind};
    use editor::RowInfo;
    use fs::FakeFs;
    use gpui::{AppContext as _, TestAppContext};
    use parking_lot::RwLock;

    use crate::entry_view_state::{Entry, EntryViewEvent, EntryViewState, ToolCallSync, ViewEvent};
    use crate::message_editor::SessionCapabilities;
    use multi_buffer::MultiBufferRow;
    use pretty_assertions::assert_matches;
    use project::Project;
    use serde_json::json;
    use settings::SettingsStore;
    use util::path;
    use workspace::{MultiWorkspace, PathList};

    #[test]
    fn test_reindex_after_removal() {
        use super::reindex_after_removal;

        // Entries before the removed range keep their index.
        assert_eq!(reindex_after_removal(0, &(2..4)), Some(0));
        assert_eq!(reindex_after_removal(1, &(2..4)), Some(1));
        // Entries inside the removed range are dropped.
        assert_eq!(reindex_after_removal(2, &(2..4)), None);
        assert_eq!(reindex_after_removal(3, &(2..4)), None);
        // Entries after the removed range slide down by its length.
        assert_eq!(reindex_after_removal(4, &(2..4)), Some(2));
        assert_eq!(reindex_after_removal(5, &(2..4)), Some(3));
        // An empty removal range leaves indices untouched.
        assert_eq!(reindex_after_removal(3, &(2..2)), Some(3));
    }

    #[gpui::test]
    async fn test_legacy_diff_sync(cx: &mut TestAppContext) {
        init_test(cx);
        let fs = FakeFs::new(cx.executor());
        fs.insert_tree(
            "/project",
            json!({
                "hello.txt": "hi world"
            }),
        )
        .await;
        let project = Project::test(fs, [Path::new(path!("/project"))], cx).await;

        let (multi_workspace, cx) =
            cx.add_window_view(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx));
        let workspace = multi_workspace.read_with(cx, |mw, _| mw.workspace().clone());

        for sync_mode in [ToolCallSync::Live, ToolCallSync::Materialize] {
            let tool_call = acp_v1::ToolCall::new("tool", "Tool call")
                .status(acp_v1::ToolCallStatus::InProgress)
                .content(vec![acp_v1::ToolCallContent::Diff(
                    acp_v1::Diff::new("/project/hello.txt", "hello world").old_text("hi world"),
                )]);
            let connection = Rc::new(StubAgentConnection::new());
            let thread = cx
                .update(|_, cx| {
                    connection.clone().new_session(
                        project.clone(),
                        PathList::new(&[Path::new(path!("/project"))]),
                        cx,
                    )
                })
                .await
                .unwrap();
            let session_id = thread.update(cx, |thread, _| thread.session_id().clone());

            cx.update(|_, cx| {
                connection.send_update(session_id, acp_v1::SessionUpdate::ToolCall(tool_call), cx)
            });

            let view_state = cx.new(|_cx| {
                EntryViewState::new(
                    workspace.downgrade(),
                    project.downgrade(),
                    None,
                    Arc::new(RwLock::new(SessionCapabilities::default())),
                    "Test Agent".into(),
                )
            });

            view_state.update_in(cx, |view_state, window, cx| {
                view_state.sync_entry_initial(0, &thread, window, cx)
            });

            let diff = thread.read_with(cx, |thread, _| {
                thread
                    .entries()
                    .get(0)
                    .unwrap()
                    .diffs()
                    .next()
                    .unwrap()
                    .clone()
            });

            view_state.read_with(cx, |view_state, _cx| {
                assert!(
                    view_state
                        .entry(0)
                        .unwrap()
                        .editor_for_diff(&diff)
                        .is_none(),
                    "restoring history should not build a diff editor for a collapsed tool call"
                );
            });

            let saw_new_diff = Rc::new(Cell::new(false));
            let _subscription = cx.update(|_, cx| {
                let saw_new_diff = saw_new_diff.clone();
                cx.subscribe(&view_state, move |_, event: &EntryViewEvent, _| {
                    if matches!(event.view_event, ViewEvent::NewDiff(_)) {
                        saw_new_diff.set(true);
                    }
                })
            });

            view_state.update_in(cx, |view_state, window, cx| {
                view_state.sync_entry_inner(0, &thread, window, sync_mode, cx)
            });

            cx.run_until_parked();

            assert!(
                !saw_new_diff.get(),
                "materializing a restored diff must not emit NewDiff"
            );

            let diff_editor = view_state.read_with(cx, |view_state, _cx| {
                let entry = view_state.entry(0).unwrap();
                let Entry::ToolCall(tool_call) = entry else {
                    panic!("expected tool call");
                };
                assert!(tool_call.restored_view_ids.is_empty());
                entry.editor_for_diff(&diff).unwrap()
            });
            assert_eq!(
                diff_editor.read_with(cx, |editor, cx| editor.text(cx)),
                "hi world\nhello world"
            );
            let row_infos = diff_editor.read_with(cx, |editor, cx| {
                let multibuffer = editor.buffer().read(cx);
                multibuffer
                    .snapshot(cx)
                    .row_infos(MultiBufferRow(0))
                    .collect::<Vec<_>>()
            });
            assert_matches!(
                row_infos.as_slice(),
                [
                    RowInfo {
                        multibuffer_row: Some(MultiBufferRow(0)),
                        diff_status: Some(DiffHunkStatus {
                            kind: DiffHunkStatusKind::Deleted,
                            ..
                        }),
                        ..
                    },
                    RowInfo {
                        multibuffer_row: Some(MultiBufferRow(1)),
                        diff_status: Some(DiffHunkStatus {
                            kind: DiffHunkStatusKind::Added,
                            ..
                        }),
                        ..
                    }
                ]
            );
        }
    }

    #[gpui::test]
    async fn test_elicitation_preserves_entry_index(cx: &mut TestAppContext) {
        init_test(cx);

        let fs = FakeFs::new(cx.executor());
        fs.insert_tree("/project", json!({})).await;
        let project = Project::test(fs, [Path::new(path!("/project"))], cx).await;

        let (multi_workspace, cx) =
            cx.add_window_view(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx));
        let workspace = multi_workspace.read_with(cx, |mw, _| mw.workspace().clone());

        let connection = Rc::new(StubAgentConnection::new());
        let thread = cx
            .update(|_, cx| {
                connection.clone().new_session(
                    project.clone(),
                    PathList::new(&[Path::new(path!("/project"))]),
                    cx,
                )
            })
            .await
            .unwrap();
        let session_id = thread.update(cx, |thread, _| thread.session_id().clone());

        let _response_task = thread.update(cx, |thread, cx| {
            thread
                .request_elicitation(
                    acp_v2::CreateElicitationRequest::new(
                        acp_v2::ElicitationFormMode::new(
                            acp_v2::ElicitationSessionScope::new(session_id.clone()),
                            acp_v2::ElicitationSchema::new().string("name", true),
                        ),
                        "Provide a name",
                    ),
                    cx,
                )
                .unwrap()
        });
        thread.update(cx, |thread, cx| {
            thread
                .append_message_chunk(
                    acp_thread::MessageKind::Assistant,
                    acp_v2::ContentChunk::new("hello".into(), "after-elicitation"),
                    cx,
                )
                .expect("append current message after elicitation");
        });

        let view_state = cx.new(|_cx| {
            EntryViewState::new(
                workspace.downgrade(),
                project.downgrade(),
                None,
                Arc::new(RwLock::new(SessionCapabilities::default())),
                "Test Agent".into(),
            )
        });

        view_state.update_in(cx, |view_state, window, cx| {
            view_state.sync_entry(0, &thread, window, cx);
            view_state.sync_entry(1, &thread, window, cx);
        });

        view_state.read_with(cx, |view_state, _cx| {
            assert!(matches!(
                view_state.entry(0),
                Some(Entry::Elicitation { .. })
            ));
            assert!(matches!(
                view_state.entry(1),
                Some(Entry::AssistantMessage(_))
            ));
        });
    }

    fn init_test(cx: &mut TestAppContext) {
        cx.update(|cx| {
            let mut settings_store = SettingsStore::test(cx);
            settings_store.register_setting::<feature_flags::FeatureFlagsSettings>();
            cx.set_global(settings_store);
            theme_settings::init(theme::LoadThemes::JustBase, cx);
            release_channel::init(semver::Version::new(0, 0, 0), cx);
        });
    }
}