feat(walkthrough): open the change walkthrough from an agent tool call

This commit is contained in:
Chris Scott committed 2026-10-09 11:03:21 -04:00
1 parent 3eb0b02203
commit aa1da8d608
26 files changed
+1367 -175

No files matched your search

@@ -0,0 +1,315 @@
import { describe, it, expect, vi } from 'vitest'
import { renderHook } from '@testing-library/react'
import type {
SessionMessageAssistant,
SessionMessageAssistantTool,
SessionMessageInfo,
} from '@opencode-manager/shared/opencode'
import { useAutoOpenWalkthrough } from '../useAutoOpenWalkthrough'
const openWalkthroughPart = (
id: string,
status: 'running' | 'completed' = 'completed',
): SessionMessageAssistantTool => ({
type: 'tool',
id,
name: 'ocm',
time: { created: 1 },
state:
status === 'completed'
? {
status: 'completed',
input: { action: 'open_walkthrough', params: {} },
content: [{ type: 'text', text: 'Walkthrough opened' }],
}
: {
status: 'running',
input: { action: 'open_walkthrough', params: {} },
metadata: {},
},
})
const otherToolPart = (id: string): SessionMessageAssistantTool => ({
type: 'tool',
id,
name: 'shell',
time: { created: 1, ran: 2, completed: 3 },
state: {
status: 'completed',
input: { command: 'ls' },
content: [{ type: 'text', text: 'ok' }],
},
})
const assistantMessage = (
id: string,
...parts: SessionMessageAssistantTool[]
): SessionMessageAssistant => ({
id,
type: 'assistant',
agent: 'build',
model: { providerID: 'anthropic', id: 'claude' },
time: { created: 1, completed: 2 },
content: parts,
})
interface HookProps {
sessionId: string
messages: SessionMessageInfo[]
enabled: boolean
loading: boolean
onOpen: () => void
}
const renderAutoOpen = (initialProps: HookProps) =>
renderHook((props: HookProps) => useAutoOpenWalkthrough(props), { initialProps })
describe('useAutoOpenWalkthrough', () => {
it('opens once when an open_walkthrough call completes live', () => {
const onOpen = vi.fn()
const { rerender } = renderAutoOpen({
sessionId: 's1',
messages: [],
enabled: true,
loading: false,
onOpen,
})
expect(onOpen).not.toHaveBeenCalled()
rerender({
sessionId: 's1',
messages: [assistantMessage('m1', openWalkthroughPart('t1'))],
enabled: true,
loading: false,
onOpen,
})
expect(onOpen).toHaveBeenCalledTimes(1)
})
it('opens when a running open_walkthrough call later completes', () => {
const onOpen = vi.fn()
const { rerender } = renderAutoOpen({
sessionId: 's1',
messages: [assistantMessage('m1', openWalkthroughPart('t1', 'running'))],
enabled: true,
loading: false,
onOpen,
})
expect(onOpen).not.toHaveBeenCalled()
rerender({
sessionId: 's1',
messages: [assistantMessage('m1', openWalkthroughPart('t1', 'completed'))],
enabled: true,
loading: false,
onOpen,
})
expect(onOpen).toHaveBeenCalledTimes(1)
})
it('passes the requested source to the opener', () => {
const onOpen = vi.fn()
const part: SessionMessageAssistantTool = {
type: 'tool',
id: 't1',
name: 'ocm',
time: { created: 1 },
state: {
status: 'completed',
input: { action: 'open_walkthrough', params: { source: { kind: 'pullRequest', number: 7 } } },
content: [{ type: 'text', text: 'Opened the walkthrough.' }],
},
}
const { rerender } = renderAutoOpen({
sessionId: 's1',
messages: [],
enabled: true,
loading: false,
onOpen,
})
rerender({
sessionId: 's1',
messages: [assistantMessage('m1', part)],
enabled: true,
loading: false,
onOpen,
})
expect(onOpen).toHaveBeenCalledWith({ kind: 'pullRequest', number: 7 })
})
it('passes no source when the call invalidates one', () => {
const onOpen = vi.fn()
const invalid: SessionMessageAssistantTool = {
type: 'tool',
id: 't1',
name: 'ocm',
time: { created: 1 },
state: {
status: 'completed',
input: { action: 'open_walkthrough', params: { source: { kind: 'nonsense' } } },
content: [{ type: 'text', text: 'Opened the walkthrough.' }],
},
}
const { rerender } = renderAutoOpen({
sessionId: 's1',
messages: [],
enabled: true,
loading: false,
onOpen,
})
rerender({
sessionId: 's1',
messages: [assistantMessage('m1', invalid)],
enabled: true,
loading: false,
onOpen,
})
expect(onOpen).toHaveBeenCalledWith(undefined)
})
it('does not open for a call already completed at the first loaded render', () => {
const onOpen = vi.fn()
renderAutoOpen({
sessionId: 's1',
messages: [assistantMessage('m1', openWalkthroughPart('t1'))],
enabled: true,
loading: false,
onOpen,
})
expect(onOpen).not.toHaveBeenCalled()
})
it('does not open for historical calls loaded after the initial pending render', () => {
const onOpen = vi.fn()
const { rerender } = renderAutoOpen({
sessionId: 's1',
messages: [],
enabled: true,
loading: true,
onOpen,
})
rerender({
sessionId: 's1',
messages: [assistantMessage('m1', openWalkthroughPart('t1'))],
enabled: true,
loading: false,
onOpen,
})
expect(onOpen).not.toHaveBeenCalled()
})
it('does not open while the walkthrough is not docked', () => {
const onOpen = vi.fn()
const { rerender } = renderAutoOpen({
sessionId: 's1',
messages: [],
enabled: false,
loading: false,
onOpen,
})
rerender({
sessionId: 's1',
messages: [assistantMessage('m1', openWalkthroughPart('t1'))],
enabled: false,
loading: false,
onOpen,
})
expect(onOpen).not.toHaveBeenCalled()
})
it('opens at most once per tool call id', () => {
const onOpen = vi.fn()
const message = assistantMessage('m1', openWalkthroughPart('t1'))
const { rerender } = renderAutoOpen({
sessionId: 's1',
messages: [],
enabled: true,
loading: false,
onOpen,
})
rerender({ sessionId: 's1', messages: [message], enabled: true, loading: false, onOpen })
rerender({ sessionId: 's1', messages: [message], enabled: true, loading: false, onOpen })
expect(onOpen).toHaveBeenCalledTimes(1)
})
it('ignores other tools and other ocm actions', () => {
const onOpen = vi.fn()
const { rerender } = renderAutoOpen({
sessionId: 's1',
messages: [],
enabled: true,
loading: false,
onOpen,
})
rerender({
sessionId: 's1',
messages: [assistantMessage('m1', otherToolPart('t1'))],
enabled: true,
loading: false,
onOpen,
})
expect(onOpen).not.toHaveBeenCalled()
})
it('does not open a historical call in a different session', () => {
const onOpen = vi.fn()
const { rerender } = renderAutoOpen({
sessionId: 's1',
messages: [],
enabled: true,
loading: false,
onOpen,
})
rerender({
sessionId: 's2',
messages: [assistantMessage('m2', openWalkthroughPart('t2'))],
enabled: true,
loading: false,
onOpen,
})
expect(onOpen).not.toHaveBeenCalled()
})
it('does not open an older call paged in after the initial load', () => {
const onOpen = vi.fn()
const { rerender } = renderAutoOpen({
sessionId: 's1',
messages: [assistantMessage('msg_010')],
enabled: true,
loading: false,
onOpen,
})
expect(onOpen).not.toHaveBeenCalled()
rerender({
sessionId: 's1',
messages: [assistantMessage('msg_001', openWalkthroughPart('t_old')), assistantMessage('msg_010')],
enabled: true,
loading: false,
onOpen,
})
expect(onOpen).not.toHaveBeenCalled()
})
})
@@ -0,0 +1,59 @@
import { useEffect, useRef } from 'react'
import type { SessionMessageInfo } from '@opencode-manager/shared/opencode'
import type { WalkthroughSource } from '@opencode-manager/shared/schemas'
import { collectOpenWalkthroughCalls } from '@/lib/walkthroughTool'
interface UseAutoOpenWalkthroughOptions {
sessionId: string | undefined
messages: SessionMessageInfo[]
enabled: boolean
loading: boolean
onOpen: (source?: WalkthroughSource) => void
}
/**
* Opens the change walkthrough when an `open_walkthrough` tool call completes live in the viewed
* session. Calls present at the first loaded render are treated as history and ignored, and calls in
* messages older than that render's oldest message stay ignored when the transcript pages in older
* history. Each call opens at most once. `enabled` gates the automatic open so a mode where the
* walkthrough is a full-screen dialog (mobile) leaves the user to open it from the tool call row.
*/
export function useAutoOpenWalkthrough({
sessionId,
messages,
enabled,
loading,
onOpen,
}: UseAutoOpenWalkthroughOptions): void {
const seenRef = useRef<Set<string> | null>(null)
const frontierRef = useRef<string | null>(null)
const sessionRef = useRef<string | undefined>(undefined)
const onOpenRef = useRef(onOpen)
onOpenRef.current = onOpen
useEffect(() => {
if (sessionRef.current !== sessionId || loading) {
sessionRef.current = sessionId
seenRef.current = null
frontierRef.current = null
}
if (loading) return
if (seenRef.current === null) {
seenRef.current = new Set(collectOpenWalkthroughCalls(messages).map((call) => call.id))
frontierRef.current = messages[0]?.id ?? null
return
}
const frontier = frontierRef.current
const liveMessages =
frontier === null ? messages : messages.filter((message) => message.id >= frontier)
let opened = false
for (const call of collectOpenWalkthroughCalls(liveMessages)) {
if (seenRef.current.has(call.id)) continue
seenRef.current.add(call.id)
if (enabled && !opened) {
opened = true
onOpenRef.current(call.source)
}
}
}, [sessionId, messages, enabled, loading])
}