feat(chat): group exploration tool calls

Collapse consecutive read, search and fetch tool calls in the chat transcript into one expandable summary line, matching the OpenCode TUI. Adds a groupToolCalls preference (default on) with a Settings toggle.
This commit is contained in:
Chris Scott committed 2026-10-07 16:40:38 -04:00
1 parent 81dd00e89f
commit 02d2e89bca
8 files changed
+242 -10

No files matched your search

@@ -0,0 +1,48 @@
import { describe, expect, it } from 'vitest'
import type { SessionMessageAssistant, SessionMessageAssistantTool } from '@opencode-manager/shared/opencode'
import { explorationLabel, groupExplorationParts } from './explorationGroups'
const tool = (id: string, name: string, status: 'completed' | 'running' = 'completed'): SessionMessageAssistantTool =>
status === 'completed'
? { type: 'tool', id, name, state: { status, input: {}, content: [], metadata: {} }, time: { created: 0, ran: 0, completed: 1 } }
: { type: 'tool', id, name, state: { status, input: {} }, time: { created: 0, ran: 0 } }
const text = (value: string): SessionMessageAssistant['content'][number] => ({ type: 'text', text: value })
const reasoning = (value: string): SessionMessageAssistant['content'][number] => ({ type: 'reasoning', text: value })
describe('groupExplorationParts', () => {
it('collapses consecutive exploration tools and breaks runs on visible non-exploration parts', () => {
const items = groupExplorationParts(
[tool('a', 'read'), tool('b', 'grep'), text('found it'), tool('c', 'read'), tool('d', 'shell'), tool('e', 'glob')],
false,
)
expect(items.map((item) => (item.type === 'exploration' ? item.parts.map((part) => part.id) : item.part.type))).toEqual([
['a', 'b'],
'text',
['c'],
'tool',
['e'],
])
})
it('does not let hidden reasoning or blank text split a run', () => {
const items = groupExplorationParts([tool('a', 'read'), reasoning('thinking'), text(' '), tool('b', 'read')], false)
expect(items).toHaveLength(1)
expect(items[0]).toMatchObject({ type: 'exploration' })
})
it('lets visible reasoning split a run', () => {
expect(groupExplorationParts([tool('a', 'read'), reasoning('thinking'), tool('b', 'read')], true)).toHaveLength(3)
})
})
describe('explorationLabel', () => {
it('counts tools with TUI nouns and pluralization', () => {
expect(explorationLabel([tool('a', 'read'), tool('b', 'read'), tool('c', 'grep'), tool('d', 'glob'), tool('e', 'webfetch')]))
.toBe('Explored: 2 reads, 2 searches, 1 fetch')
})
it('reports in-progress groups as exploring', () => {
expect(explorationLabel([tool('a', 'read'), tool('b', 'read', 'running')])).toBe('Exploring: 2 reads')
})
})
+56
View File
@@ -0,0 +1,56 @@
import type { SessionMessageAssistant, SessionMessageAssistantTool } from '@opencode-manager/shared/opencode'
type AssistantContentPart = SessionMessageAssistant['content'][number]
export type AssistantContentItem =
| { type: 'part'; part: AssistantContentPart }
| { type: 'exploration'; parts: SessionMessageAssistantTool[] }
const EXPLORATION_TOOLS = new Set(['read', 'glob', 'grep', 'webfetch', 'websearch'])
function isExplorationTool(part: AssistantContentPart): part is SessionMessageAssistantTool {
return part.type === 'tool' && EXPLORATION_TOOLS.has(part.name.toLowerCase())
}
function isInvisiblePart(part: AssistantContentPart, showReasoning: boolean): boolean {
if (part.type === 'text') return part.text.trim().length === 0
if (part.type === 'reasoning') return !showReasoning || part.text.trim().length === 0
return false
}
/**
* Collapses consecutive read, search, and fetch tool calls into exploration groups, matching the
* OpenCode TUI. Parts that render nothing are dropped so they do not split a run.
*/
export function groupExplorationParts(
content: SessionMessageAssistant['content'],
showReasoning: boolean,
): AssistantContentItem[] {
return content.reduce<AssistantContentItem[]>((items, part) => {
if (isInvisiblePart(part, showReasoning)) return items
const previous = items.at(-1)
if (!isExplorationTool(part)) items.push({ type: 'part', part })
else if (previous?.type === 'exploration') previous.parts.push(part)
else items.push({ type: 'exploration', parts: [part] })
return items
}, [])
}
/** Whether every tool in an exploration group has finished, successfully or not. */
export function isExplorationComplete(parts: SessionMessageAssistantTool[]): boolean {
return parts.every((part) => part.state.status === 'completed' || part.state.status === 'error')
}
/** TUI-style summary such as `Explored: 5 reads, 2 searches`. */
export function explorationLabel(parts: SessionMessageAssistantTool[]): string {
const counts = new Map<string, number>()
parts.forEach((part) => {
const tool = part.name.toLowerCase()
const noun = tool === 'grep' || tool === 'glob' || tool === 'websearch' ? 'search' : tool === 'webfetch' ? 'fetch' : tool
counts.set(noun, (counts.get(noun) ?? 0) + 1)
})
const names = [...counts].map(([noun, count]) =>
`${count} ${count === 1 ? noun : noun === 'search' || noun === 'fetch' ? `${noun}es` : `${noun}s`}`,
)
return `${isExplorationComplete(parts) ? 'Explored' : 'Exploring'}: ${names.join(', ')}`
}