diff --git a/crates/acp_thread/src/acp_thread.rs b/crates/acp_thread/src/acp_thread.rs index 88f43db70ca..742e7607452 100644 --- a/crates/acp_thread/src/acp_thread.rs +++ b/crates/acp_thread/src/acp_thread.rs @@ -1122,7 +1122,7 @@ impl AgentThreadEntry { } } - pub fn location(&self, ix: usize) -> Option<(ToolCallLocation, AgentLocation)> { + pub fn location(&self, ix: usize) -> Option<(ToolCallLocation, Option)> { if let AgentThreadEntry::ToolCall(ToolCall { locations, resolved_locations, @@ -1131,7 +1131,7 @@ impl AgentThreadEntry { { Some(( locations.get(ix)?.clone(), - resolved_locations.get(ix)?.clone()?, + resolved_locations.get(ix).cloned().flatten(), )) } else { None @@ -18400,10 +18400,9 @@ mod tests { assert_eq!(absolute.path, respelled.path); thread.update(cx, |thread, cx| { let (index, call) = thread.tool_call(&id).expect("tool"); - let (location, _) = thread.entries[index] - .location(0) - .expect("resolved location"); + let (location, resolved) = thread.entries[index].location(0).expect("location"); assert_eq!(location, absolute); + assert!(resolved.is_some()); let resolved_locations = call.resolved_locations.clone(); thread .upsert_wire_tool_call(acp_v2::ToolCallUpdate::new("location-migration"), cx) @@ -18427,15 +18426,17 @@ mod tests { let (index, call) = thread.tool_call(&id).expect("tool"); assert_eq!(call.locations, vec![respelled.clone()]); assert!(call.resolved_locations.is_empty()); - assert!(thread.entries[index].location(0).is_none()); + assert_eq!( + thread.entries[index].location(0), + Some((respelled.clone(), None)) + ); }); cx.run_until_parked(); thread.update(cx, |thread, cx| { let (index, _) = thread.tool_call(&id).expect("tool"); - let (location, _) = thread.entries[index] - .location(0) - .expect("refreshed projection"); + let (location, resolved) = thread.entries[index].location(0).expect("location"); assert_eq!(location, respelled); + assert!(resolved.is_some(), "refreshed projection"); thread .update_tool_call( acp_v1::ToolCallUpdate::new( @@ -18452,7 +18453,10 @@ mod tests { let (index, call) = thread.tool_call(&id).expect("tool"); assert_eq!(call.locations, vec![relative.clone()]); assert!(call.resolved_locations.is_empty()); - assert!(thread.entries[index].location(0).is_none()); + assert_eq!( + thread.entries[index].location(0), + Some((relative.clone(), None)) + ); }); cx.run_until_parked(); thread.update(cx, |thread, cx| { @@ -19066,8 +19070,10 @@ mod tests { thread.read_with(cx, |thread, cx| { let (tool_call_location, agent_location) = thread.entries[0] .location(0) - .expect("external tool-call location should resolve"); + .expect("external tool-call location"); assert_eq!(tool_call_location.path, skill_path); + let agent_location = + agent_location.expect("external tool-call location should resolve"); let buffer = agent_location .buffer diff --git a/crates/agent_ui/src/conversation_view/thread_view.rs b/crates/agent_ui/src/conversation_view/thread_view.rs index 52ee7784777..2884360173f 100644 --- a/crates/agent_ui/src/conversation_view/thread_view.rs +++ b/crates/agent_ui/src/conversation_view/thread_view.rs @@ -11817,6 +11817,7 @@ impl ThreadView { active_editor.update_in(cx, |editor, window, cx| { let snapshot = editor.buffer().read(cx).snapshot(cx); if snapshot.as_singleton().is_some() + && let Some(agent_location) = agent_location && let Some(anchor) = snapshot.anchor_in_excerpt(agent_location.position) { editor.change_selections(Default::default(), window, cx, |selections| { @@ -15918,6 +15919,103 @@ mod tests { fn flush(&self) {} } + + #[gpui::test] + async fn test_open_tool_call_location_without_resolved_buffer(cx: &mut gpui::TestAppContext) { + use agent_client_protocol::v1::schema as acp; + + crate::conversation_view::tests::init_test(cx); + cx.update(|cx| { + agent::ThreadStore::init_global(cx); + language_model::LanguageModelRegistry::test(cx); + }); + + let fs = FakeFs::new(cx.executor()); + fs.insert_tree( + path!("/project"), + json!({"notes.txt": "first\nsecond\nthird\n"}), + ) + .await; + fs.insert_file( + path!("/project/screenshot.png"), + b"\x89PNG\r\n\x1a\n\0\0\0\rIHDR\0\0\0\x01".to_vec(), + ) + .await; + let project = Project::test(fs, [path!("/project").as_ref()], 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, |workspace, _| workspace.workspace().clone()); + let panel = workspace.update_in(cx, |workspace, window, cx| { + let panel = cx.new(|cx| crate::AgentPanel::new(workspace, window, cx)); + workspace.add_panel(panel.clone(), window, cx); + panel + }); + let connection = acp_thread::StubAgentConnection::new(); + crate::test_support::open_thread_with_connection(&panel, connection.clone(), cx); + let session_id = crate::test_support::active_session_id(&panel, cx); + let thread_view = panel.read_with(cx, |panel, cx| { + panel.active_thread_view(cx).expect("active thread view") + }); + + let send_read = + |id: &'static str, path: &str, line: Option, cx: &mut gpui::VisualTestContext| { + cx.update(|_window, cx| { + connection.send_update( + session_id.clone(), + acp::SessionUpdate::ToolCall( + acp::ToolCall::new(acp::ToolCallId::new(id), "Read") + .kind(acp::ToolKind::Read) + .locations(vec![acp::ToolCallLocation::new(path).line(line)]), + ), + cx, + ); + }); + thread_view.read_with(cx, |thread_view, cx| { + thread_view + .thread + .read(cx) + .tool_call(&acp_v2::ToolCallId::new(id)) + .expect("tool call") + .0 + }) + }; + let open_location = |entry_ix: usize, cx: &mut gpui::VisualTestContext| { + thread_view.update_in(cx, |thread_view, window, cx| { + thread_view.open_tool_call_location(entry_ix, 0, window, cx); + }); + cx.run_until_parked(); + workspace.read_with(cx, |workspace, cx| { + workspace.active_item(cx).expect("location should open") + }) + }; + + let image_ix = send_read("read-image", path!("/project/screenshot.png"), None, cx); + cx.run_until_parked(); + thread_view.read_with(cx, |thread_view, cx| { + let (_, resolved) = thread_view.thread.read(cx).entries()[image_ix] + .location(0) + .expect("location"); + assert!(resolved.is_none(), "binary files never resolve to a buffer"); + }); + let item = open_location(image_ix, cx); + cx.read(|cx| { + assert_eq!(item.tab_content_text(0, cx), "screenshot.png"); + assert!(item.downcast::().is_none()); + }); + + // Clicking before resolution finishes still opens the reported line. + let text_ix = send_read("read-text", path!("/project/notes.txt"), Some(2), cx); + let editor = open_location(text_ix, cx) + .downcast::() + .expect("text file should open in an editor"); + editor.update_in(cx, |editor, window, cx| { + let snapshot = editor.snapshot(window, cx); + assert_eq!( + editor.selections.newest::(&snapshot).head(), + Point::new(1, 0) + ); + }); + } } const FAST_MODE_WARNING_NAMESPACE: &str = "fast-mode-warning-dismissed";