From f88e54eed5474cab7504835e60b0c0d7d6e39485 Mon Sep 17 00:00:00 2001 From: David Jensen Date: Wed, 18 Nov 2026 12:56:01 +0000 Subject: [PATCH] agent_ui: Open tool call locations for files that are not text (#67092) ## Summary Clicking a tool call's location label ("Read screenshot.png"), or the "Go to File" button above image tool output, did nothing for image files, and each read logged "Binary files are not supported". `ToolCall::resolve_location` resolves locations by opening a text buffer, which fails for binary files, and `AgentThreadEntry::location` required that buffer, so `open_tool_call_location` returned before opening anything. `AgentThreadEntry::location` now returns the resolved location as an `Option`. `open_tool_call_location` already opens the path through the workspace (which picks the image viewer for images), so the resolved anchor is only used to place the cursor, falling back to the reported line when unresolved. This also makes a text file location clickable before resolution finishes. An earlier attempt, #65860, was closed by the CLA bot without review. Known limit, unchanged here: when one tool call lists several locations the label is not clickable (existing `locations.len() == 1` rule), and every image output's "Go to File" opens the first location, because ACP doesn't link content blocks to locations. Implementation and testing were AI-assisted; I understand the fix as described above and tested it locally. Closes #65823 ## Screenshots 1-read-tool-call Read tool call 2-image-opened-from-tool-call Image opened from tool call ## Testing - New test `test_open_tool_call_location_without_resolved_buffer` in `agent_ui`: a read tool call for a PNG opens a non-editor item for the file, and a text file clicked before its location resolves opens at the reported line. Both halves fail on the old code. Existing `acp_thread` location tests are updated for the new return type. - `cargo test -p agent_ui -p acp_thread` passes, except `test_headless_terminal_retains_process_control` in `acp_thread`, which is also flaky on unchanged `main` (failed 1 of 3 full runs). `./script/clippy -p agent_ui -p acp_thread` is clean. macOS only. - Manual on macOS: had pi-acp read a PNG and clicked the tool call label, which opened the image viewer. pi-acp renders images as markdown links rather than ACP image content, so the "Go to File" button on image output is covered by the test only. ## Self-review - [ ] I've reviewed my diff for quality, security, reliability, and performance. - [x] UI changes follow the [checklist](https://zed.dev/docs/development/ui-checklist). - [x] Tests cover the new or changed behavior. Release Notes: - Fixed "Go to File" on agent tool calls doing nothing for image files Co-authored-by: David Jensen <288440181+daejey@users.noreply.github.com> --- crates/acp_thread/src/acp_thread.rs | 28 +++--- .../src/conversation_view/thread_view.rs | 98 +++++++++++++++++++ 2 files changed, 115 insertions(+), 11 deletions(-) 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";