mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 12:54:20 +01:00
fix(walkthrough): address review of agent-opened walkthrough
- Wait for the model catalog once per walkthrough generation instead of before every model call - Report open_walkthrough as requested, not opened, and drop its unused metadata - Share the ocm tool and action names through @opencode-manager/shared - Remove the unused source-request nonce and duplicate session check - Scan only new messages when auto-opening the walkthrough - Assert the request action cannot reach the internal change-walkthroughs route
This commit is contained in:
1 parent
aa1da8d608
commit
c497df5161
14 files changed
+146
-122
No files matched your search
@@ -385,7 +385,7 @@ describe('ChangesWalkthroughSheet', () => {
|
||||
|
||||
it('applies an external source request to the walkthrough source', async () => {
|
||||
mocks.getChangeWalkthrough.mockResolvedValue(state({ walkthrough: null }))
|
||||
renderSheet({ sourceRequest: { sessionId: 'ses_1', source: { kind: 'staged' }, nonce: 1 } })
|
||||
renderSheet({ sourceRequest: { sessionId: 'ses_1', source: { kind: 'staged' } } })
|
||||
|
||||
await waitFor(() => {
|
||||
expect(mocks.getChangeWalkthrough).toHaveBeenCalledWith('ses_1', { kind: 'staged' })
|
||||
@@ -396,7 +396,7 @@ describe('ChangesWalkthroughSheet', () => {
|
||||
it('applies a pull request request with its number and base', async () => {
|
||||
mocks.getChangeWalkthrough.mockResolvedValue(state({ walkthrough: null }))
|
||||
renderSheet({
|
||||
sourceRequest: { sessionId: 'ses_1', source: { kind: 'pullRequest', number: 12 }, nonce: 1 },
|
||||
sourceRequest: { sessionId: 'ses_1', source: { kind: 'pullRequest', number: 12 } },
|
||||
})
|
||||
|
||||
await waitFor(() => {
|
||||
@@ -409,7 +409,7 @@ describe('ChangesWalkthroughSheet', () => {
|
||||
const user = userEvent.setup()
|
||||
mocks.getChangeWalkthrough.mockResolvedValue(state({ walkthrough: null }))
|
||||
const { rerender, onOpenChange } = renderSheet({
|
||||
sourceRequest: { sessionId: 'ses_1', source: { kind: 'staged' }, nonce: 1 },
|
||||
sourceRequest: { sessionId: 'ses_1', source: { kind: 'staged' } },
|
||||
})
|
||||
await waitFor(() => {
|
||||
expect(mocks.getChangeWalkthrough).toHaveBeenCalledWith('ses_1', { kind: 'staged' })
|
||||
@@ -426,7 +426,7 @@ describe('ChangesWalkthroughSheet', () => {
|
||||
sessionId="ses_1"
|
||||
open
|
||||
onOpenChange={onOpenChange}
|
||||
sourceRequest={{ sessionId: 'ses_1', source: { kind: 'staged' }, nonce: 2 }}
|
||||
sourceRequest={{ sessionId: 'ses_1', source: { kind: 'staged' } }}
|
||||
/>,
|
||||
)
|
||||
|
||||
@@ -440,7 +440,7 @@ describe('ChangesWalkthroughSheet', () => {
|
||||
const user = userEvent.setup()
|
||||
mocks.getChangeWalkthrough.mockResolvedValue(state({ walkthrough: null }))
|
||||
const { rerender, onOpenChange } = renderSheet({
|
||||
sourceRequest: { sessionId: 'ses_1', source: { kind: 'staged' }, nonce: 1 },
|
||||
sourceRequest: { sessionId: 'ses_1', source: { kind: 'staged' } },
|
||||
})
|
||||
await waitFor(() => {
|
||||
expect(mocks.getChangeWalkthrough).toHaveBeenCalledWith('ses_1', { kind: 'staged' })
|
||||
@@ -458,7 +458,7 @@ describe('ChangesWalkthroughSheet', () => {
|
||||
sessionId="ses_1"
|
||||
open
|
||||
onOpenChange={onOpenChange}
|
||||
sourceRequest={{ sessionId: 'ses_1', source: undefined, nonce: 2 }}
|
||||
sourceRequest={{ sessionId: 'ses_1', source: undefined }}
|
||||
/>,
|
||||
)
|
||||
|
||||
@@ -467,16 +467,6 @@ describe('ChangesWalkthroughSheet', () => {
|
||||
expect(mocks.getChangeWalkthrough).not.toHaveBeenCalledWith('ses_1', { kind: 'staged' })
|
||||
})
|
||||
|
||||
it('ignores a request from another session', async () => {
|
||||
mocks.getChangeWalkthrough.mockResolvedValue(state({ walkthrough: null }))
|
||||
renderSheet({ sourceRequest: { sessionId: 'ses_other', source: { kind: 'staged' }, nonce: 1 } })
|
||||
|
||||
await waitFor(() => {
|
||||
expect(mocks.getChangeWalkthrough).toHaveBeenCalledWith('ses_1', { kind: 'session' })
|
||||
})
|
||||
expect(mocks.getChangeWalkthrough).not.toHaveBeenCalledWith('ses_1', { kind: 'staged' })
|
||||
})
|
||||
|
||||
it('clears a generation error when the source changes', async () => {
|
||||
const user = userEvent.setup()
|
||||
mocks.getChangeWalkthrough.mockResolvedValue(state({ walkthrough: null }))
|
||||
|
||||
@@ -239,13 +239,12 @@ const WalkthroughSourceInputs = memo(function WalkthroughSourceInputs() {
|
||||
})
|
||||
|
||||
/**
|
||||
* A one-shot request to open the walkthrough on a given source. `nonce` makes a repeated request for
|
||||
* the same source a distinct value, so it re-applies; `source` undefined leaves the selection alone.
|
||||
* A one-shot request to open the walkthrough on a given source. A new request object re-applies, so a
|
||||
* repeated request for the same source takes effect; `source` undefined leaves the selection alone.
|
||||
*/
|
||||
export interface WalkthroughSourceRequest {
|
||||
sessionId: string
|
||||
source?: WalkthroughSource
|
||||
nonce: number
|
||||
}
|
||||
|
||||
interface ChangesWalkthroughProviderProps {
|
||||
@@ -295,7 +294,7 @@ export function ChangesWalkthroughProvider({
|
||||
}, [active, sessionId, sourceKey, walkthrough?.createdAt])
|
||||
|
||||
useEffect(() => {
|
||||
if (!sourceRequest || sourceRequest.sessionId !== sessionId) return
|
||||
if (!sourceRequest) return
|
||||
const requested = sourceRequest.source
|
||||
if (!requested) return
|
||||
setSource(requested)
|
||||
@@ -306,7 +305,7 @@ export function ChangesWalkthroughProvider({
|
||||
if (requested.kind === 'pullRequest') {
|
||||
setNumberInput(String(requested.number))
|
||||
}
|
||||
}, [sourceRequest, sessionId])
|
||||
}, [sourceRequest])
|
||||
|
||||
const hunksById = new Map(walkthrough?.hunks.map((hunk) => [hunk.id, hunk]) ?? [])
|
||||
const stops = walkthrough?.stops ?? []
|
||||
|
||||
@@ -111,6 +111,44 @@ describe('useAutoOpenWalkthrough', () => {
|
||||
expect(onOpen).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
it('opens once when a call completes in the last message across streaming updates', () => {
|
||||
const onOpen = vi.fn()
|
||||
const { rerender } = renderAutoOpen({
|
||||
sessionId: 's1',
|
||||
messages: [assistantMessage('m1', otherToolPart('t1'))],
|
||||
enabled: true,
|
||||
loading: false,
|
||||
onOpen,
|
||||
})
|
||||
|
||||
rerender({
|
||||
sessionId: 's1',
|
||||
messages: [
|
||||
assistantMessage('m1', otherToolPart('t1')),
|
||||
assistantMessage('m2', openWalkthroughPart('t2', 'running')),
|
||||
],
|
||||
enabled: true,
|
||||
loading: false,
|
||||
onOpen,
|
||||
})
|
||||
|
||||
expect(onOpen).not.toHaveBeenCalled()
|
||||
|
||||
rerender({
|
||||
sessionId: 's1',
|
||||
messages: [
|
||||
assistantMessage('m1', otherToolPart('t1')),
|
||||
assistantMessage('m2', openWalkthroughPart('t2', 'completed')),
|
||||
],
|
||||
enabled: true,
|
||||
loading: false,
|
||||
onOpen,
|
||||
})
|
||||
|
||||
expect(onOpen).toHaveBeenCalledTimes(1)
|
||||
expect(onOpen).toHaveBeenCalledWith(undefined)
|
||||
})
|
||||
|
||||
it('passes the requested source to the opener', () => {
|
||||
const onOpen = vi.fn()
|
||||
const part: SessionMessageAssistantTool = {
|
||||
|
||||
@@ -27,6 +27,7 @@ export function useAutoOpenWalkthrough({
|
||||
}: UseAutoOpenWalkthroughOptions): void {
|
||||
const seenRef = useRef<Set<string> | null>(null)
|
||||
const frontierRef = useRef<string | null>(null)
|
||||
const lastScannedRef = useRef<string | null>(null)
|
||||
const sessionRef = useRef<string | undefined>(undefined)
|
||||
const onOpenRef = useRef(onOpen)
|
||||
onOpenRef.current = onOpen
|
||||
@@ -36,16 +37,33 @@ export function useAutoOpenWalkthrough({
|
||||
sessionRef.current = sessionId
|
||||
seenRef.current = null
|
||||
frontierRef.current = null
|
||||
lastScannedRef.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
|
||||
lastScannedRef.current = messages[messages.length - 1]?.id ?? null
|
||||
return
|
||||
}
|
||||
const frontier = frontierRef.current
|
||||
const liveMessages =
|
||||
frontier === null ? messages : messages.filter((message) => message.id >= frontier)
|
||||
const lastScannedId = lastScannedRef.current
|
||||
let scanFrom = -1
|
||||
if (lastScannedId !== null) {
|
||||
for (let index = messages.length - 1; index >= 0; index -= 1) {
|
||||
if (messages[index]?.id === lastScannedId) {
|
||||
scanFrom = index
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
let liveMessages: SessionMessageInfo[]
|
||||
if (scanFrom >= 0) {
|
||||
liveMessages = messages.slice(scanFrom)
|
||||
} else {
|
||||
const frontier = frontierRef.current
|
||||
liveMessages = frontier === null ? messages : messages.filter((message) => message.id >= frontier)
|
||||
}
|
||||
lastScannedRef.current = messages[messages.length - 1]?.id ?? null
|
||||
let opened = false
|
||||
for (const call of collectOpenWalkthroughCalls(liveMessages)) {
|
||||
if (seenRef.current.has(call.id)) continue
|
||||
|
||||
@@ -1,11 +1,8 @@
|
||||
import type { SessionMessageAssistantTool, SessionMessageInfo } from '@opencode-manager/shared/opencode'
|
||||
import { WalkthroughSourceSchema } from '@opencode-manager/shared/schemas'
|
||||
import { MANAGER_TOOL_NAME, MANAGER_TOOL_OPEN_WALKTHROUGH_ACTION, WalkthroughSourceSchema } from '@opencode-manager/shared/schemas'
|
||||
import type { WalkthroughSource } from '@opencode-manager/shared/schemas'
|
||||
|
||||
export const OCM_TOOL_NAME = 'ocm'
|
||||
export const OPEN_WALKTHROUGH_ACTION = 'open_walkthrough'
|
||||
|
||||
export interface OpenWalkthroughCall {
|
||||
interface OpenWalkthroughCall {
|
||||
id: string
|
||||
source?: WalkthroughSource
|
||||
}
|
||||
@@ -28,8 +25,8 @@ function readOpenWalkthroughSource(params: unknown): WalkthroughSource | undefin
|
||||
export function readOpenWalkthroughCall(
|
||||
part: SessionMessageAssistantTool,
|
||||
): OpenWalkthroughCall | undefined {
|
||||
if (part.name !== OCM_TOOL_NAME || part.state.status !== 'completed') return undefined
|
||||
if (part.state.input.action !== OPEN_WALKTHROUGH_ACTION) return undefined
|
||||
if (part.name !== MANAGER_TOOL_NAME || part.state.status !== 'completed') return undefined
|
||||
if (part.state.input.action !== MANAGER_TOOL_OPEN_WALKTHROUGH_ACTION) return undefined
|
||||
return { id: part.id, source: readOpenWalkthroughSource(part.state.input.params) }
|
||||
}
|
||||
|
||||
|
||||
@@ -304,11 +304,10 @@ export function SessionDetail() {
|
||||
const handleShowMcpDialog = useCallback(() => setMcpDialogOpen(true), [setMcpDialogOpen]);
|
||||
const handleShowSkillsDialog = useCallback(() => setSkillsDialogOpen(true), [setSkillsDialogOpen]);
|
||||
const handleShowWalkthrough = useCallback((source?: WalkthroughSource) => {
|
||||
setRequestedWalkthroughSource((current) => ({
|
||||
setRequestedWalkthroughSource({
|
||||
sessionId: sessionId ?? '',
|
||||
source,
|
||||
nonce: (current?.nonce ?? 0) + 1,
|
||||
}));
|
||||
});
|
||||
setWalkthroughOpen(true);
|
||||
}, [sessionId, setWalkthroughOpen]);
|
||||
const handleShowWalkthroughCommand = useCallback(() => handleShowWalkthrough(), [handleShowWalkthrough]);
|
||||
|
||||
Reference in new issue
Block a user