diff --git a/packages/cli-rs/crates/mini/src/footers/permission.rs b/packages/cli-rs/crates/mini/src/footers/permission.rs index e88f165384cd9..ada1d9316583b 100644 --- a/packages/cli-rs/crates/mini/src/footers/permission.rs +++ b/packages/cli-rs/crates/mini/src/footers/permission.rs @@ -148,6 +148,8 @@ pub struct Info { pub title: String, pub lines: Vec, pub diff: Option, + /// `(label, file, diff)` for each file when a request edits more than one. + pub files: Vec<(String, String, String)>, pub patch: Option, /// The file the diff belongs to, for its highlighting. pub file: Option, @@ -163,12 +165,15 @@ pub fn info(request: &Request, format_path: &dyn Fn(&str) -> String, mono: bool) &request.metadata, format_path, ); + let files: Vec<(String, String, String)> = + info.files.into_iter().map(|(file, diff)| (format_path(&file), file, diff)).collect(); if !mono { return Info { icon: info.icon.to_owned(), title: info.title, lines: info.lines, diff: info.diff, + files, patch: info.patch, file: info.file, }; @@ -176,13 +181,16 @@ pub fn info(request: &Request, format_path: &dyn Fn(&str) -> String, mono: bool) Info { icon: mono_prefix(info.icon, true), title: info.title, - lines: info - .diff - .or(info.patch) - .into_iter() - .chain(info.lines.iter().map(|line| mono_prefix(line, true))) - .collect(), + lines: if files.is_empty() { + info.diff.or(info.patch).into_iter().collect::>() + } else { + files.into_iter().flat_map(|(label, _, diff)| [label, diff]).collect() + } + .into_iter() + .chain(info.lines.iter().map(|line| mono_prefix(line, true))) + .collect(), diff: None, + files: Vec::new(), patch: None, file: None, } @@ -339,23 +347,33 @@ pub fn paint(app: &App, root: &View, body: &Body<'_>) -> Painted { ); // A diff draws as the scrollback draws an edit, a patch as highlighted text; narrow, both stay text. if let Some(diff) = &info.diff { - if width >= 40 { - let syntax = body.run.syntax.clone().unwrap_or_default(); - let filetype = info.file.as_deref().and_then(crate::tools::filetype); - let look = crate::tools::diff_look(&body.run.block, theme.text); - let diff = opencode_ui::diff::view_with( - app, - &look, - &syntax, - diff, - opentui::widget::DiffView::Unified, - filetype, - WrapMode::Word, - 0, - ); - content.add(&diff.shrink(0.0)); + let shown = if info.files.is_empty() { + vec![(None, info.file.as_deref(), diff.as_str())] } else { - content.add(&line(diff.clone(), theme.text)); + info.files.iter().map(|(label, file, diff)| (Some(label), Some(file.as_str()), diff.as_str())).collect() + }; + for (label, file, diff) in shown { + if let Some(label) = label { + content.add(&line(label.clone(), theme.muted)); + } + if width >= 40 { + let syntax = body.run.syntax.clone().unwrap_or_default(); + let filetype = file.and_then(crate::tools::filetype); + let look = crate::tools::diff_look(&body.run.block, theme.text); + let diff = opencode_ui::diff::view_with( + app, + &look, + &syntax, + diff, + opentui::widget::DiffView::Unified, + filetype, + WrapMode::Word, + 0, + ); + content.add(&diff.shrink(0.0)); + } else { + content.add(&line(diff.to_owned(), theme.text)); + } } } else if let Some(patch) = &info.patch { match &body.run.syntax { diff --git a/packages/cli-rs/crates/ui/src/permission.rs b/packages/cli-rs/crates/ui/src/permission.rs index 9f559251d31b8..988c869465a7e 100644 --- a/packages/cli-rs/crates/ui/src/permission.rs +++ b/packages/cli-rs/crates/ui/src/permission.rs @@ -11,6 +11,8 @@ pub struct Presentation { pub title: String, pub lines: Vec, pub diff: Option, + /// `(file, diff)` for each file when a request edits more than one. + pub files: Vec<(String, String)>, pub patch: Option, pub file: Option, } @@ -42,14 +44,25 @@ pub fn presentation( match ToolKind::of(action) { ToolKind::Edit => { let file = or(path, &first); - let files = metadata.get("files").and_then(Value::as_array); - let changed = dict(files.and_then(|files| files.first()).unwrap_or(&Value::Null)); - let diff = some(or(&or(&text(&changed, "patch"), &text(&changed, "diff")), &text(&metadata, "diff"))); + let entries = metadata.get("files").and_then(Value::as_array).map(Vec::as_slice).unwrap_or_default(); + let files: Vec<(String, String)> = entries + .iter() + .map(dict) + .filter_map(|entry| { + some(or(&text(&entry, "patch"), &text(&entry, "diff"))).map(|diff| (text(&entry, "file"), diff)) + }) + .collect(); + let diff = files.first().map(|(_, diff)| diff.clone()).or_else(|| some(text(&metadata, "diff"))); Presentation { icon: "→", - title: format!("Edit {}", format_path(&file)), + title: if entries.len() > 1 { + format!("Edit {} files", entries.len()) + } else { + format!("Edit {}", format_path(&file)) + }, patch: if diff.is_some() { None } else { EditInput(input).patch_text().non_empty().map(str::to_owned) }, diff, + files: if files.len() > 1 { files } else { Vec::new() }, file: Some(file), ..Presentation::default() } @@ -191,3 +204,35 @@ fn wildcard_directory(value: &str) -> String { fn dict(value: &Value) -> Map { value.as_object().cloned().unwrap_or_default() } + +#[cfg(test)] +mod tests { + use serde_json::json; + + use super::presentation; + + fn edit(metadata: serde_json::Value) -> super::Presentation { + presentation("edit", &["src/a.ts".to_owned()], None, &json!({}), &metadata, &|value| value.to_owned()) + } + + #[test] + fn multi_file_edit_keeps_every_file_diff() { + let info = edit(json!({ + "diff": "combined", + "files": [{ "file": "src/a.ts", "patch": "a-diff" }, { "file": "src/b.ts", "patch": "b-diff" }], + })); + assert_eq!(info.title, "Edit 2 files"); + assert_eq!( + info.files, + vec![("src/a.ts".to_owned(), "a-diff".to_owned()), ("src/b.ts".to_owned(), "b-diff".to_owned())] + ); + } + + #[test] + fn single_file_edit_keeps_title_and_diff() { + let info = edit(json!({ "files": [{ "file": "src/a.ts", "patch": "a-diff" }] })); + assert_eq!(info.title, "Edit src/a.ts"); + assert_eq!(info.diff.as_deref(), Some("a-diff")); + assert!(info.files.is_empty()); + } +} diff --git a/packages/tui/src/mini/footer.permission.tsx b/packages/tui/src/mini/footer.permission.tsx index 235aa5a0b4a45..4de7346f8251f 100644 --- a/packages/tui/src/mini/footer.permission.tsx +++ b/packages/tui/src/mini/footer.permission.tsx @@ -29,7 +29,7 @@ import { type PermissionOption, } from "./permission.shared" import { stringWidth } from "../util/string-width" -import { toolFiletype } from "./tool" +import { toolFiletype, toolPath } from "./tool" import { transparent, type RunBlockTheme, type RunFooterTheme } from "./theme" import type { MiniPermissionRequest, PermissionReply } from "./types" import { PatchDiff } from "../component/patch-diff" @@ -155,7 +155,6 @@ export function RunPermissionBody(props: { const [state, setState] = createSignal(createPermissionBodyState(props.request)) const stage = createMemo(() => state().stage) const info = createMemo(() => permissionInfo(props.request, props.directory?.(), props.mono)) - const ft = createMemo(() => toolFiletype(info().file)) let scroll: ScrollBoxRenderable | undefined const scrollbar = createMemo(() => ({ visible: !props.mono, @@ -453,36 +452,47 @@ export function RunPermissionBody(props: { } > - = 40} - fallback={ - - {info().diff} - - } - > - - + + {(item) => ( + + + + {toolPath(item.file, { home: true, directory: props.directory?.() })} + + + = 40} + fallback={ + + {item.diff} + + } + > + + + + )} + diff --git a/packages/tui/src/mini/permission.shared.ts b/packages/tui/src/mini/permission.shared.ts index 38d2de31354ac..06e9d3a00c701 100644 --- a/packages/tui/src/mini/permission.shared.ts +++ b/packages/tui/src/mini/permission.shared.ts @@ -62,10 +62,15 @@ export function permissionInfo(request: MiniPermissionRequest, directory?: strin ...info, icon: monoPrefix(info.icon, true), lines: [ - ...(info.diff || info.patch ? [info.diff ?? info.patch!] : []), + ...(info.files + ? info.files.flatMap((item) => [toolPath(item.file, { home: true, directory }), item.diff]) + : info.diff || info.patch + ? [info.diff ?? info.patch!] + : []), ...info.lines.map((line) => monoPrefix(line, true)), ], diff: undefined, + files: undefined, patch: undefined, } } diff --git a/packages/tui/src/routes/session/permission.tsx b/packages/tui/src/routes/session/permission.tsx index 3618f19e0f852..5cfad5c9365d7 100644 --- a/packages/tui/src/routes/session/permission.tsx +++ b/packages/tui/src/routes/session/permission.tsx @@ -19,15 +19,21 @@ import { useToast } from "../../ui/toast" type PermissionStage = "permission" | "reject" -function EditBody(props: { file?: string; diff?: string; patch?: string }) { +function EditBody(props: { + file?: string + diff?: string + files?: { file: string; diff: string }[] + patch?: string + formatPath: (value: string) => string +}) { const theme = useTheme() const themes = useThemes() const syntax = themes.currentSyntax const config = useConfig().data const dimensions = useTerminalDimensions() - const filepath = createMemo(() => props.file ?? "") const diff = createMemo(() => props.diff ?? "") + const files = createMemo(() => props.files ?? [{ file: props.file ?? "", diff: diff() }]) const view = createMemo(() => { const diffView = config.diffs?.view @@ -36,7 +42,6 @@ function EditBody(props: { file?: string; diff?: string; patch?: string }) { return dimensions().width > 120 ? "split" : "unified" }) - const ft = createMemo(() => filetype(filepath())) const scrollAcceleration = createMemo(() => getScrollAcceleration(config)) return ( @@ -52,26 +57,37 @@ function EditBody(props: { file?: string; diff?: string; patch?: string }) { }, }} > - + + {(item) => ( + + + + {props.formatPath(item.file)} + + + + + )} + @@ -167,7 +183,13 @@ export function PermissionPrompt(props: { request: PermissionRequest; directory? ) const presentationBody = () => props.request.action === "edit" ? ( - + ) : props.request.action === "external_directory" ? ( 0}> diff --git a/packages/tui/src/util/permission.ts b/packages/tui/src/util/permission.ts index 09d607ff373ea..3f6cee807c951 100644 --- a/packages/tui/src/util/permission.ts +++ b/packages/tui/src/util/permission.ts @@ -8,6 +8,8 @@ export type PermissionPresentation = { title: string lines: string[] diff?: string + /** Every file's own diff when a request edits more than one file. */ + files?: { file: string; diff: string }[] patch?: string file?: string } @@ -31,13 +33,18 @@ export function permissionPresentation( if (action === "edit") { const file = text(input.path) || resources[0] || "" - const first = dict(Array.isArray(metadata.files) ? metadata.files[0] : undefined) - const diff = text(first.patch) || text(first.diff) || text(metadata.diff) || undefined + const entries = (Array.isArray(metadata.files) ? metadata.files : []).map(dict) + const files = entries.flatMap((entry) => { + const diff = text(entry.patch) || text(entry.diff) + return diff ? [{ file: text(entry.file), diff }] : [] + }) + const diff = files[0]?.diff || text(metadata.diff) || undefined return { icon: "→", - title: `Edit ${formatPath(file)}`, + title: entries.length > 1 ? `Edit ${entries.length} files` : `Edit ${formatPath(file)}`, lines: [], diff, + files: files.length > 1 ? files : undefined, patch: diff ? undefined : text(input.patchText) || undefined, file, } diff --git a/packages/tui/test/cli/tui/permission.edit.test.tsx b/packages/tui/test/cli/tui/permission.edit.test.tsx new file mode 100644 index 0000000000000..8dcfa994f6508 --- /dev/null +++ b/packages/tui/test/cli/tui/permission.edit.test.tsx @@ -0,0 +1,70 @@ +/** @jsxImportSource @opentui/solid */ +import type { PermissionRequest } from "@opencode/client" +import { testRender } from "@opentui/solid" +import { expect, test } from "bun:test" +import { ConfigProvider } from "../../../src/config" +import { ClientProvider } from "../../../src/context/client" +import { DataProvider } from "../../../src/context/data" +import { Keymap } from "../../../src/context/keymap" +import { InteractivityProvider } from "../../../src/context/interactivity" +import { LocationProvider } from "../../../src/context/location" +import { ThemeProvider } from "../../../src/context/theme" +import { PermissionPrompt } from "../../../src/routes/session/permission" +import { ToastProvider } from "../../../src/ui/toast" +import { emptyThemeSource, tmpdir } from "../../fixture/fixture" +import { createApi, createEventStream, createFetch } from "../../fixture/tui-client" +import { TestTuiContexts } from "../../fixture/tui-environment" +import { createTuiResolvedConfig } from "../../fixture/tui-runtime" + +const file = (name: string, patch: string) => ({ file: name, patch, additions: 1, deletions: 1, status: "modified" }) + +test("multi-file edit prompt shows every file's diff under its path", async () => { + await using tmp = await tmpdir() + const request = { + id: "per_edit", + sessionID: "ses_edit", + action: "edit", + resources: ["src/a.ts", "src/b.ts"], + metadata: { + files: [ + file("src/a.ts", "@@ -1 +1 @@\n-one\n+FIRST_DIFF\n"), + file("src/b.ts", "@@ -1 +1 @@\n-two\n+SECOND_DIFF\n"), + ], + }, + } satisfies PermissionRequest + const transport = createFetch(() => undefined, createEventStream()) + const app = await testRender( + () => ( + + + + + + + + + + + + + + + + + + + + ), + { width: 100, height: 40, kittyKeyboard: true }, + ) + try { + app.renderer.start() + await app.renderOnce() + await app.renderOnce() + const frame = app.captureCharFrame() + for (const text of ["Edit 2 files", "src/a.ts", "FIRST_DIFF", "src/b.ts", "SECOND_DIFF"]) + expect(frame).toContain(text) + } finally { + app.renderer.destroy() + } +}) diff --git a/packages/tui/test/mini/blocker.responsive.test.tsx b/packages/tui/test/mini/blocker.responsive.test.tsx index b851f1b8bebe3..dded72511af0e 100644 --- a/packages/tui/test/mini/blocker.responsive.test.tsx +++ b/packages/tui/test/mini/blocker.responsive.test.tsx @@ -497,6 +497,39 @@ test("form review scrolls from the first answer to the last", async () => { } }) +test("multi-file edit permission shows every file's diff under its path", async () => { + const app = await testRender( + () => ( + "/project"} + theme={RUN_THEME_FALLBACK.footer} + block={RUN_THEME_FALLBACK.block} + onReply={() => {}} + /> + ), + { width: 100, height: 30 }, + ) + try { + await settle(app) + const frame = app.captureCharFrame() + for (const text of ["Edit 2 files", "src/a.ts", "FIRST_DIFF", "src/b.ts", "SECOND_DIFF"]) + expect(frame).toContain(text) + } finally { + app.renderer.destroy() + } +}) + test("long permission paths, diffs and persistent scopes remain keyboard accessible", async () => { const app = await testRender( () => ( diff --git a/packages/tui/test/mini/permission.shared.test.ts b/packages/tui/test/mini/permission.shared.test.ts index 0b0af972745e3..8eacada19a771 100644 --- a/packages/tui/test/mini/permission.shared.test.ts +++ b/packages/tui/test/mini/permission.shared.test.ts @@ -128,6 +128,25 @@ describe("run permission shared", () => { }) }) + test("mono mode keeps every file diff of a multi-file edit, each under its path", () => { + expect( + permissionInfo( + req({ + action: "edit", + resources: ["a.ts", "b.ts"], + metadata: { + files: [ + { file: "a.ts", patch: "a-diff", additions: 1, deletions: 0, status: "modified" }, + { file: "b.ts", patch: "b-diff", additions: 1, deletions: 0, status: "modified" }, + ], + }, + }), + undefined, + true, + ), + ).toMatchObject({ title: "Edit 2 files", lines: ["a.ts", "a-diff", "b.ts", "b-diff"] }) + }) + test("prefers canonical request metadata over source tool metadata", () => { expect( permissionInfo( diff --git a/packages/tui/test/util/permission.test.ts b/packages/tui/test/util/permission.test.ts index 4bd7c7cae42ae..342e29fe73e2a 100644 --- a/packages/tui/test/util/permission.test.ts +++ b/packages/tui/test/util/permission.test.ts @@ -21,3 +21,27 @@ test("preserves permission roots and self-contained metadata", () => { }, ) }) + +const info = (file: string, patch: string) => ({ file, patch, additions: 1, deletions: 1, status: "modified" }) + +test("edit permission exposes every file of a multi-file patch", () => { + const result = permissionPresentation({ + action: "edit", + resources: ["src/old.ts", "src/new.ts", "src/other.ts"], + metadata: { diff: "combined", files: [info("src/new.ts", "new-diff"), info("src/other.ts", "other-diff")] }, + }) + expect(result.title).toBe("Edit 2 files") + expect(result.files).toEqual([ + { file: "src/new.ts", diff: "new-diff" }, + { file: "src/other.ts", diff: "other-diff" }, + ]) +}) + +test("edit permission keeps the single-file title and diff", () => { + const result = permissionPresentation({ + action: "edit", + resources: ["src/a.ts"], + metadata: { files: [info("src/a.ts", "a-diff")] }, + }) + expect(result).toMatchObject({ title: "Edit src/a.ts", diff: "a-diff", files: undefined }) +})