mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 21:04:23 +01:00
feat(walkthrough): open the change walkthrough from an agent tool call
This commit is contained in:
1 parent
3eb0b02203
commit
aa1da8d608
26 files changed
+1367
-175
No files matched your search
@@ -0,0 +1,63 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import type { SessionMessageAssistantTool } from '@opencode-manager/shared/opencode'
|
||||
import { readOpenWalkthroughCall } from './walkthroughTool'
|
||||
|
||||
type Json = null | boolean | number | string | Json[] | { [key: string]: Json }
|
||||
|
||||
const ocmPart = (
|
||||
params: Json,
|
||||
status: 'running' | 'completed' = 'completed',
|
||||
): SessionMessageAssistantTool => ({
|
||||
type: 'tool',
|
||||
id: 't1',
|
||||
name: 'ocm',
|
||||
time: { created: 1 },
|
||||
state:
|
||||
status === 'completed'
|
||||
? {
|
||||
status: 'completed',
|
||||
input: { action: 'open_walkthrough', params },
|
||||
content: [{ type: 'text', text: 'Opened the walkthrough.' }],
|
||||
}
|
||||
: {
|
||||
status: 'running',
|
||||
input: { action: 'open_walkthrough', params },
|
||||
metadata: {},
|
||||
},
|
||||
})
|
||||
|
||||
describe('readOpenWalkthroughCall', () => {
|
||||
it('returns the validated source the agent requested', () => {
|
||||
expect(readOpenWalkthroughCall(ocmPart({ source: { kind: 'staged' } }))).toEqual({
|
||||
id: 't1',
|
||||
source: { kind: 'staged' },
|
||||
})
|
||||
})
|
||||
|
||||
it('returns the source with its base for git-backed sources', () => {
|
||||
expect(readOpenWalkthroughCall(ocmPart({ source: { kind: 'branch', base: 'develop' } }))).toEqual({
|
||||
id: 't1',
|
||||
source: { kind: 'branch', base: 'develop' },
|
||||
})
|
||||
})
|
||||
|
||||
it('returns no source when the agent omitted one', () => {
|
||||
expect(readOpenWalkthroughCall(ocmPart({}))).toEqual({ id: 't1', source: undefined })
|
||||
})
|
||||
|
||||
it('ignores a source that does not validate', () => {
|
||||
expect(readOpenWalkthroughCall(ocmPart({ source: { kind: 'nonsense' } }))).toEqual({
|
||||
id: 't1',
|
||||
source: undefined,
|
||||
})
|
||||
})
|
||||
|
||||
it('ignores malformed params', () => {
|
||||
expect(readOpenWalkthroughCall(ocmPart('not-an-object'))).toEqual({ id: 't1', source: undefined })
|
||||
expect(readOpenWalkthroughCall(ocmPart(null))).toEqual({ id: 't1', source: undefined })
|
||||
})
|
||||
|
||||
it('returns undefined for an unfinished call', () => {
|
||||
expect(readOpenWalkthroughCall(ocmPart({ source: { kind: 'staged' } }, 'running'))).toBeUndefined()
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,48 @@
|
||||
import type { SessionMessageAssistantTool, SessionMessageInfo } from '@opencode-manager/shared/opencode'
|
||||
import { 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 {
|
||||
id: string
|
||||
source?: WalkthroughSource
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads the optional walkthrough source the agent passed to `open_walkthrough`, ignoring anything that
|
||||
* does not validate as a `WalkthroughSource`.
|
||||
*/
|
||||
function readOpenWalkthroughSource(params: unknown): WalkthroughSource | undefined {
|
||||
if (typeof params !== 'object' || params === null || Array.isArray(params)) return undefined
|
||||
const parsed = WalkthroughSourceSchema.safeParse((params as Record<string, unknown>).source)
|
||||
return parsed.success ? parsed.data : undefined
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads an assistant tool part as a completed `ocm` `open_walkthrough` call, or undefined for every
|
||||
* other tool, action, and unfinished state. The call carries the validated source the agent asked to
|
||||
* open, or undefined when none was given or it did not validate.
|
||||
*/
|
||||
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
|
||||
return { id: part.id, source: readOpenWalkthroughSource(part.state.input.params) }
|
||||
}
|
||||
|
||||
/** Every completed `open_walkthrough` call in a transcript, in message order. */
|
||||
export function collectOpenWalkthroughCalls(messages: SessionMessageInfo[]): OpenWalkthroughCall[] {
|
||||
const calls: OpenWalkthroughCall[] = []
|
||||
for (const message of messages) {
|
||||
if (message.type !== 'assistant') continue
|
||||
for (const part of message.content) {
|
||||
if (part.type !== 'tool') continue
|
||||
const call = readOpenWalkthroughCall(part)
|
||||
if (call) calls.push(call)
|
||||
}
|
||||
}
|
||||
return calls
|
||||
}
|
||||
Reference in new issue
Block a user