Merge remote-tracking branch 'origin/master' into feat/read-presenter
This commit is contained in:
@@ -52,15 +52,28 @@ function pretty(value: unknown): string {
|
||||
return displayText(serialized ?? String(value))
|
||||
}
|
||||
|
||||
/**
|
||||
* A side's content lines under the terminator rule the Web DiffBlock also
|
||||
* applies: empty text is zero lines (a full deletion's `newText`, a create's
|
||||
* absent `oldText`), and a single trailing newline terminates the last line
|
||||
* rather than adding an empty one. An interior blank line survives. Keeping the
|
||||
* two front ends on the same rule holds their `+A -R` footers in step.
|
||||
*/
|
||||
function diffContentLines(text: string): string[] {
|
||||
if (text === '') return []
|
||||
const body = text.endsWith('\n') ? text.slice(0, -1) : text
|
||||
return body.split('\n')
|
||||
}
|
||||
|
||||
/** A file diff as colored `+`/`-` lines, optionally prefixed with its path. */
|
||||
function diffLines(diff: FileDiff, palette: Palette): string[] {
|
||||
// The card header is a fixed `Tool / <name>` frame that never names a file, so
|
||||
// each hunk always carries its own path header (no redundancy to suppress).
|
||||
const lines = [palette.bold(displayText(diff.path))]
|
||||
if (diff.oldText !== null) {
|
||||
for (const line of displayText(diff.oldText).split('\n')) lines.push(palette.error(`- ${line}`))
|
||||
for (const line of diffContentLines(displayText(diff.oldText))) lines.push(palette.error(`- ${line}`))
|
||||
}
|
||||
for (const line of displayText(diff.newText).split('\n')) lines.push(palette.success(`+ ${line}`))
|
||||
for (const line of diffContentLines(displayText(diff.newText))) lines.push(palette.success(`+ ${line}`))
|
||||
return lines
|
||||
}
|
||||
|
||||
@@ -504,15 +517,19 @@ export class ToolCardComponent implements Component {
|
||||
}
|
||||
if (view.card === 'diff') {
|
||||
// The header no longer names the file, so each diff keeps its own path
|
||||
// header. A trailing footer summarizes the change (`+A -R · N file(s)`).
|
||||
// header. A trailing footer summarizes the change (`+A -R · N file(s)`),
|
||||
// on the same terminator rule and distinct-path count the Web DiffBlock
|
||||
// uses, so the two front ends' footers agree.
|
||||
let added = 0
|
||||
let removed = 0
|
||||
const paths = new Set<string>()
|
||||
const hunks = view.diffs.flatMap((diff, index) => {
|
||||
if (diff.oldText !== null) removed += displayText(diff.oldText).split('\n').length
|
||||
added += displayText(diff.newText).split('\n').length
|
||||
paths.add(diff.path)
|
||||
if (diff.oldText !== null) removed += diffContentLines(displayText(diff.oldText)).length
|
||||
added += diffContentLines(displayText(diff.newText)).length
|
||||
return [...index > 0 ? [''] : [], ...diffLines(diff, this.palette)]
|
||||
})
|
||||
const files = view.diffs.length
|
||||
const files = paths.size
|
||||
const footer = this.palette.dim(`└ +${added} -${removed} · ${files} file${files === 1 ? '' : 's'}`)
|
||||
// A diff's own `+`/`-` colors carry its meaning, so it renders verbatim
|
||||
// rather than under the dim result-output color.
|
||||
|
||||
@@ -4318,6 +4318,25 @@ describe('tool cards and surface replay', () => {
|
||||
diffs: [{ path: 'src/only.ts', oldText: 'old', newText: 'new' }],
|
||||
}),
|
||||
},
|
||||
scatteredDiff: {
|
||||
name: 'scatteredDiff', description: '', parameters: {}, output: UNUSED_TOOL_OUTPUT, execute: async () => [],
|
||||
// Three hunks in ONE file. The first two sides end in the terminator
|
||||
// newline real write/edit content carries; the third removes a line and
|
||||
// leaves an EMPTY added side (a full deletion), so `diffContentLines('')`
|
||||
// returns zero lines. The footer must read `+2 -1 · 1 file`: each trailing
|
||||
// newline terminates its line rather than adding a phantom empty one, the
|
||||
// empty side contributes no `+ ` row, and the three hunks count as the
|
||||
// single distinct path they touch.
|
||||
presentCall: () => ({
|
||||
card: 'diff',
|
||||
title: 'Edit src/scatter.ts',
|
||||
diffs: [
|
||||
{ path: 'src/scatter.ts', oldText: null, newText: 'first\n' },
|
||||
{ path: 'src/scatter.ts', oldText: null, newText: 'second\n' },
|
||||
{ path: 'src/scatter.ts', oldText: 'gone\n', newText: '' },
|
||||
],
|
||||
}),
|
||||
},
|
||||
generic: {
|
||||
name: 'generic', description: '', parameters: {}, output: UNUSED_TOOL_OUTPUT, execute: async () => [],
|
||||
presentCall: () => ({ card: 'generic', title: 'Inspect value', rawInput: { alpha: 1 } }),
|
||||
@@ -4644,6 +4663,35 @@ describe('tool cards and surface replay', () => {
|
||||
await dispose(result)
|
||||
})
|
||||
|
||||
it('counts a same-file diff once and terminates its trailing newline', async () => {
|
||||
// A budget past the card's row count so every hunk row stays visible (the
|
||||
// collapse arithmetic is covered elsewhere); this test is about the
|
||||
// terminator rule and the distinct-path footer count.
|
||||
const result = await setup({ tools, config: { maxToolOutputLines: 20 } })
|
||||
appendUser(result.session, 'scatter edits in one file')
|
||||
appendAssistant(result.session, [
|
||||
{ type: 'text', text: 'Editing' },
|
||||
{ type: 'tool-call', id: 'scatter' as never, name: 'scatteredDiff', arguments: '{}' },
|
||||
])
|
||||
result.session.append('tool/call', {
|
||||
turn: 1, step: 1, callId: 'scatter' as never, name: 'scatteredDiff', arguments: '{}',
|
||||
})
|
||||
await tick()
|
||||
const output = result.terminal.output
|
||||
// Three hunks, one path: distinct-path count, same as the Web DiffBlock.
|
||||
expect(output).toContain('· 1 file')
|
||||
expect(output).not.toContain('· 3 files')
|
||||
// The `first\n`/`second\n` sides each contribute exactly one added line —
|
||||
// the trailing newline terminates rather than adding a phantom empty `+ `.
|
||||
expect(output).toContain('+ first')
|
||||
expect(output).toContain('+ second')
|
||||
// The third hunk removes `gone` and leaves an empty added side, which
|
||||
// contributes no `+ ` row (diffContentLines('') is zero lines).
|
||||
expect(output).toContain('- gone')
|
||||
expect(output).toContain('+2 -1')
|
||||
await dispose(result)
|
||||
})
|
||||
|
||||
it('drops blank rows from a terminal card result that the dim styling wraps', async () => {
|
||||
const blankRowTools: Record<string, ToolDefinition> = {
|
||||
trailing: {
|
||||
|
||||
Reference in New Issue
Block a user