feat: group consecutive draft writes in the AI chat transcript (#12597)

* feat: group consecutive draft writes in the AI chat transcript

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix: keep agent drafts out of draft write groups

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix: keep a resource write out of draft groups until its type streams in

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Guilhem
2026-11-24 22:50:05 +01:00
committed by GitHub
co-authored by Claude Opus 5.5
parent 6a0283a950
commit f399a22c10
7 changed files with 108 additions and 13 deletions
@@ -188,9 +188,9 @@ export interface ChatViewHost {
* included. A consumer narrows it by tool metadata; assembling its own list from the
* sources behind it is how the advertised set and the sent set drift apart. */
readonly tools: readonly Tool<any>[]
/** Tools whose rows may fold into a run of lookups or of test runs. Every tool the
* transcript's chat could have called, not only `tools`: a row outlives the request that
* carried its tool. */
/** Tools whose rows may fold into a run of lookups, of test runs or of draft writes. Every
* tool the transcript's chat could have called, not only `tools`: a row outlives the request
* that carried its tool. */
readonly groupingTools: GroupingTools
autonomyMode: AIAutonomyMode
setAutonomyMode: (mode: AIAutonomyMode) => void
@@ -3832,6 +3832,7 @@ export const globalTools: SessionTool<{}>[] = [
},
{
requires: WRITE_SCHEDULE_DRAFT,
draftWrite: 'schedule',
def: createToolDef(
writeScheduleToolSchema,
'write_schedule',
@@ -3858,6 +3859,7 @@ export const globalTools: SessionTool<{}>[] = [
},
{
requires: WRITE_TRIGGER_DRAFT,
draftWrite: 'trigger',
def: createToolDef(
writeTriggerSchema,
'write_trigger',
@@ -4348,6 +4350,7 @@ export const globalTools: SessionTool<{}>[] = [
},
{
requires: WRITE_RESOURCE_DRAFT,
draftWrite: 'resource',
def: createToolDef(
writeResourceSchema,
'write_resource',
@@ -4364,6 +4367,7 @@ export const globalTools: SessionTool<{}>[] = [
},
{
requires: WRITE_VARIABLE_DRAFT,
draftWrite: 'variable',
def: createToolDef(
writeVariableSchema,
'write_variable',
@@ -430,3 +430,17 @@ describe('test-run tools', () => {
expect([...kinds].filter(([, values]) => values.size > 1).map(([n]) => n)).toEqual([])
})
})
describe('draft write tools', () => {
// Script, flow and app writes have a diff card or an edit group. Every other item kind's
// writes need its own draft capability and show only the item's path, so a new one must say
// how its rows fold.
it('include every write needing a per-kind draft capability', () => {
const ownCard = new Set(['write_draft', 'write_flow_draft', 'write_app_draft'])
const perKind = (t: (typeof globalTools)[number]) =>
t.requires?.some((c) => /^write_\w+_draft$/.test(c) && !ownCard.has(c))
expect(
globalTools.filter((t) => perKind(t) && !t.draftWrite).map((t) => t.def.function.name)
).toEqual([])
})
})
@@ -1497,6 +1497,10 @@ export interface Tool<T> {
* lookups. Requires `planModeSafe`, which also admits writes (plan documents, artifacts)
* and a test run; absence keeps the row on its own. */
lookup?: boolean
/** The noun a draft write of an item with no diff card of its own is counted as, e.g.
* `schedule`: consecutive ones fold into one group, `Drafted 2 schedules, 1 trigger`. A write
* with a diff card leaves it unset, as the diff is what the user reads. */
draftWrite?: string
/** Test-runs an item of this kind (a step test runs its flow), so the transcript may fold
* consecutive runs of one item, and the edits between them, into one row. A run of the
* deployed version never sets it; absence keeps the row on its own. */
@@ -9,7 +9,8 @@ import {
type ToolGroup
} from './toolGroups'
// What the chat's tool definitions mark `lookup` and `testRun`, for the calls these cases make.
// What the chat's tool definitions mark `lookup`, `testRun` and `draftWrite`, for the calls these
// cases make.
const TOOLS: GroupingTools = {
lookups: new Set([
'read_workspace_item',
@@ -33,6 +34,12 @@ const TOOLS: GroupingTools = {
['test_run_flow', 'flow'],
['test_run_app_runnable', 'app'],
['test_run_agent', 'agent']
]),
draftWrites: new Map([
['write_schedule', 'schedule'],
['write_trigger', 'trigger'],
['write_resource', 'resource'],
['write_variable', 'variable']
])
}
@@ -260,6 +267,27 @@ describe('groupToolRuns', () => {
).toEqual([{ explore: [0, 1] }, { edit: [2, 3] }])
})
it('folds draft writes of any item kind, leaving out one awaiting approval and an agent', () => {
const run = [
tool('write_schedule', { path: 'f/a/daily' }),
assistant('', { reasoning: 'next' }),
tool('write_schedule', { path: 'f/a/hourly' }),
tool('write_trigger', { kind: 'http', config: { path: 'f/a/hook' } }),
tool('write_variable', { path: 'f/a/token' }, { error: 'invalid value' }),
tool('write_resource', { path: 'f/a/pg', resource_type: 'postgresql' }),
// Its `resource_type` has not streamed in yet: it may still turn out to be an agent.
tool(
'write_resource',
{},
{ parameters: '{"path":"f/a/bot","value":{"model"', isStreamingArguments: true }
),
tool('write_resource', { path: 'f/a/db' }, { needsConfirmation: true, isLoading: true }),
tool('write_resource', { path: 'f/a/bot', resource_type: 'ai_agent' })
]
expect(shape(run)).toEqual([{ draft: [0, 1, 2, 3, 4, 5] }, 6, 7, 8])
expect(header(run)).toBe('Drafted 2 schedules, 1 trigger, 1 resource')
})
it('folds consecutive lookups of any tool, split by a write or a row with its own card', () => {
expect(
shape([
@@ -8,8 +8,8 @@ import {
import { DRAFT_CONFLICT_RESULT, DRAFT_SAVE_FAILED_RESULT } from './draftWriteResults'
import { webSearchResultOf } from './webSearchResult'
// A tool missing from these lists, whose definition sets neither `lookup` nor `testRun`,
// renders as its own row, so a new write never gets hidden by default.
// A tool missing from these lists, whose definition sets none of `lookup`, `testRun` and
// `draftWrite`, renders as its own row, so a new write never gets hidden by default.
type EditedItemKind = 'flow' | 'app'
@@ -56,35 +56,42 @@ const PATHLESS_FLOW_MODE_TOOLS = new Set([
const SCRIPT_EDIT_TOOLS = new Set(['write_script', 'edit_script', 'edit_code'])
/** What the definitions in the chat showing the transcript declare, by tool name: the tools
* that set `lookup`, and those that set `testRun` with the kind of item they run. A name that
* chat does not define (a flow's own agent tools) is neither. */
* that set `lookup`, those that set `testRun` with the kind of item they run, and those that
* set `draftWrite` with the noun they count as. A name that chat does not define (a flow's own
* agent tools) is none of these. */
export type GroupingTools = {
lookups: ReadonlySet<string>
testRuns: ReadonlyMap<string, TestedItemKind>
draftWrites: ReadonlyMap<string, string>
}
export function groupingToolsOf(tools: Iterable<Tool<any>>): GroupingTools {
const lookups = new Set<string>()
const testRuns = new Map<string, TestedItemKind>()
const draftWrites = new Map<string, string>()
for (const tool of tools) {
const name = tool.def.function.name
if (tool.lookup) lookups.add(name)
if (tool.testRun) testRuns.set(name, tool.testRun)
if (tool.draftWrite) draftWrites.set(name, tool.draftWrite)
}
return { lookups, testRuns }
return { lookups, testRuns, draftWrites }
}
export type ToolGroup = {
kind: 'group'
/** 'edit': edits of one flow or app. 'explore': consecutive lookups of anything.
/** 'edit': edits of one flow or app. 'draft': draft writes of schedules, triggers,
* resources and variables. 'explore': consecutive lookups of anything.
* 'waiting': calls queued behind the one running or awaiting the user. 'test': test runs
* of one item and its edits between them; it always ends on a run, whose card stays shown.
*/
groupKind: 'edit' | 'explore' | 'waiting' | 'test'
groupKind: 'edit' | 'draft' | 'explore' | 'waiting' | 'test'
/** First call's id, so the group keeps its identity (and expand state) as it grows. */
key: string
/** Edit and test groups: the item's path, or '' for the item open in the editor. */
target: string
/** Draft groups: the noun each draft write tool's calls are counted as. */
draftNouns?: ReadonlyMap<string, string>
entries: { message: DisplayMessage; index: number }[]
/** Test groups: the kind of item run, and how many of the entries are its runs. */
tested?: { kind: TestedItemKind; runs: number }
@@ -191,6 +198,24 @@ function sameItem(a: Membership | undefined, b: Membership): boolean {
return a !== undefined && a.kind === b.kind && (a.path === undefined || a.path === b.path)
}
// An agent is drafted through write_resource but gets its own way in (a preview chip, or an
// Open agent action), and a group shows only one chip: it stays a row. Read from the
// arguments, not the chip, which lands only once the save does. `resource_type` streams after
// the value, so until it has arrived the call may be an agent and joins no group.
function isNonAgentResourceWrite(call: ToolDisplayMessage): boolean {
const type =
typeof call.parameters === 'string'
? /"resource_type"\s*:\s*"([^"]*)"/.exec(call.parameters)?.[1]
: call.parameters?.resource_type
return typeof type === 'string' && type !== 'ai_agent'
}
function isDraftWrite(message: DisplayMessage, tools: GroupingTools): boolean {
const call = groupableCall(message)
if (call === undefined || !tools.draftWrites.has(call.toolName!)) return false
return call.toolName !== 'write_resource' || isNonAgentResourceWrite(call)
}
function isReadCall(message: DisplayMessage, tools: GroupingTools): boolean {
const call = groupableCall(message)
return call !== undefined && tools.lookups.has(call.toolName!)
@@ -383,9 +408,14 @@ function kindGroupAt(
): ToolGroup | undefined {
const explores = (m: DisplayMessage, j: number) =>
isReadCall(m, tools) && !editGroupAt(messages, j)
const drafts = (m: DisplayMessage) => isDraftWrite(m, tools)
const draftGroup = drafts(messages[start])
? toolGroup(messages, start, runEnd(messages, start, drafts), 'draft', '')
: undefined
return (
testGroupAt(messages, start, tools) ??
editGroupAt(messages, start) ??
(draftGroup && { ...draftGroup, draftNouns: tools.draftWrites }) ??
(isReadCall(messages[start], tools)
? toolGroup(messages, start, runEnd(messages, start, explores), 'explore', '')
: undefined)
@@ -418,6 +448,7 @@ function sameChatItem(a: ChatItem, b: ChatItem): boolean {
a.groupKind === b.groupKind &&
a.key === b.key &&
a.target === b.target &&
a.draftNouns === b.draftNouns &&
a.tested?.kind === b.tested?.kind &&
a.tested?.runs === b.tested?.runs &&
a.entries.length === b.entries.length &&
@@ -567,6 +598,20 @@ export function groupHeader(
more: []
}
}
if (group.groupKind === 'draft') {
const drafted = new Map<string, number>()
for (const call of calls) {
if (callFailed(call)) continue
const kind = group.draftNouns?.get(call.toolName!) ?? 'draft'
drafted.set(kind, (drafted.get(kind) ?? 0) + 1)
}
const kinds = [...drafted].map(([kind, n]) => plural(n, kind))
return {
prefix: { queued: 'Draft', running: 'Drafting', settled: 'Drafted' }[state],
label: kinds.length ? kinds.join(', ') : plural(0, 'draft'),
more: []
}
}
const counts = new Map<string, number>()
for (const call of calls) counts.set(callName(call), (counts.get(callName(call)) ?? 0) + 1)
const kinds = [...counts].map(([name, n]) => repeated(name, n))
@@ -946,8 +946,8 @@ export class FlowChatViewHost implements ChatViewHost, DraftSender<ComposerAttac
attachmentAccept = 'image/*,application/pdf,.pdf'
tools = []
// The agent's tools are the flow author's, named as they chose: a name that matches a
// copilot lookup or test run says nothing about what the tool does.
groupingTools: GroupingTools = { lookups: new Set(), testRuns: new Map() }
// copilot lookup, test run or draft write says nothing about what the tool does.
groupingTools: GroupingTools = { lookups: new Set(), testRuns: new Map(), draftWrites: new Map() }
// The enum's value, written out so this module never imports the copilot manager at
// runtime: its unit test would otherwise load the manager and the editor it pulls in.
autonomyMode = 'default' as AIAutonomyMode.DEFAULT