feat(opencode): migrate OpenCode Manager to OpenCode 2 (#359)

* feat(opencode): migrate OpenCode Manager to OpenCode 2

Port the backend lifecycle, generated plugins, event aggregation,
schedules, worktrees, provider credentials, MCP, config handling, the
frontend chat model, and the ocm CLI to OpenCode 2.

Adopt @opencode/client 2.0.15 as the single typed V2 API contract,
require OpenCode 2.0.0 or newer (pinned 2.0.15), and delete every
V1-only code path.

* refactor(opencode): reload config in place and drop v1 compatibility shims

Route config/skill/AGENTS.md changes through the OpenCode location reload
API instead of a full server restart, stop auto-resuming interrupted
sessions, remove the restart coordinator and v1 MCP/version shims, and pin
support to OpenCode >=2.0.15 <3.0.0. Adds integration-ready retries,
service-mode launch, ocm-cli repo proxy, and new frontend form/MCP helpers.

* refactor(opencode): consolidate v2 migration helpers and harden install, proxy, and session flows

- extract shared semver and model-ref utilities, reuse across version checks
- share the OpenCode release download script across Docker, entrypoint, dev setup
- unify install/upgrade failure recovery and guard externally managed OPENCODE_BIN
- scope the repo proxy location rewrite and surface ocm proxy errors
- rework frontend session projection, permission lookup, and cache invalidation

* fix(schedules): reject unavailable agents before creating a session

* feat(opencode): add ocm install, schedule agent fallback, and session move guards
This commit is contained in:
Chris Scott authored and GitHub committed 2026-09-26 13:16:45 -04:00
1 parent 6c2ebd764b
commit dacde879d6
383 files changed
+29905 -46566

No files matched your search

+487 -169
View File
@@ -1,7 +1,49 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { OpenCodeClient } from './opencode'
import {
activateSkill,
cancelForm,
clearRevert,
commitRevert,
compactSession,
createSession,
deleteSession,
findFiles,
forkSession,
getSession,
interruptSession,
listCommands,
listPendingForms,
listPendingPermissions,
listSessionMessages,
listSessionPage,
readSessionSnapshot,
renameSession,
replyForm,
replyPermission,
runCommand,
runShell,
sendPrompt,
stageRevert,
switchSessionAgent,
switchSessionModel,
} from './opencode'
import { FetchError } from './fetchWrapper'
describe('OpenCodeClient', () => {
const jsonResponse = (body: unknown) =>
new Response(JSON.stringify(body), {
status: 200,
headers: { 'Content-Type': 'application/json' },
})
const sessionInfo = (id: string, directory: string) => ({
id,
projectID: 'proj_1',
title: `Session ${id}`,
time: { created: 1000, updated: 2000 },
location: { directory },
})
describe('OpenCode facade', () => {
const fetchMock = vi.fn()
beforeEach(() => {
@@ -13,213 +55,489 @@ describe('OpenCodeClient', () => {
vi.unstubAllGlobals()
})
it('treats empty successful session deletes as success', async () => {
const lastRequest = () => {
const [input, init] = fetchMock.mock.calls.at(-1) ?? []
return { url: String(input), init: init as RequestInit }
}
it('lists root sessions for a directory through the V2 cursor API', async () => {
fetchMock.mockResolvedValue(
jsonResponse({ data: [sessionInfo('ses_1', '/repo')], cursor: { next: 'cursor_next' } }),
)
const page = await listSessionPage({
directory: '/repo',
limit: 25,
order: 'desc',
search: 'deploy',
})
expect(lastRequest().url).toBe(
'http://localhost/api/opencode/api/session?limit=25&order=desc&search=deploy&parentID=null&directory=%2Frepo',
)
expect(lastRequest().init.method).toBe('GET')
expect(page.items).toEqual([sessionInfo('ses_1', '/repo')])
expect(page.nextCursor).toBe('cursor_next')
})
it('passes the directory and cursor for continuation pages', async () => {
fetchMock.mockResolvedValue(jsonResponse({ data: [], cursor: {} }))
const page = await listSessionPage({ directory: '/repo', cursor: 'cursor_1' })
expect(lastRequest().url).toBe(
'http://localhost/api/opencode/api/session?parentID=null&directory=%2Frepo&cursor=cursor_1',
)
expect(lastRequest().init.method).toBe('GET')
expect(page.items).toEqual([])
expect(page.nextCursor).toBeUndefined()
})
it('reads a single session from the V2 session route', async () => {
fetchMock.mockResolvedValue(jsonResponse({ data: sessionInfo('ses_1', '/repo') }))
const session = await getSession('ses_1')
expect(lastRequest().url).toBe('http://localhost/api/opencode/api/session/ses_1')
expect(lastRequest().init.method).toBe('GET')
expect(session).toEqual(sessionInfo('ses_1', '/repo'))
})
it('creates a session with location and a parsed model reference', async () => {
fetchMock.mockResolvedValue(jsonResponse({ data: sessionInfo('ses_new', '/repo') }))
const session = await createSession({
directory: '/repo',
title: 'New session',
agent: 'build',
model: 'anthropic/claude#v1',
})
expect(lastRequest().url).toBe('http://localhost/api/opencode/api/session')
expect(lastRequest().init.method).toBe('POST')
expect(lastRequest().init.body).toBe(JSON.stringify({
title: 'New session',
agent: 'build',
model: { providerID: 'anthropic', id: 'claude', variant: 'v1' },
location: { directory: '/repo' },
}))
expect(session.id).toBe('ses_new')
})
it('creates a session with the required location and no optional fields', async () => {
fetchMock.mockResolvedValue(jsonResponse({ data: sessionInfo('ses_new', '/repo') }))
await createSession({ directory: '/repo' })
expect(lastRequest().init.method).toBe('POST')
expect(lastRequest().init.body).toBe(JSON.stringify({ location: { directory: '/repo' } }))
})
it('deletes a session through the V2 session route', async () => {
fetchMock.mockResolvedValue(new Response(null, { status: 204 }))
await expect(new OpenCodeClient('/api/opencode', '/repo').deleteSession('ses_1')).resolves.toBeUndefined()
await expect(deleteSession('ses_1')).resolves.toBeUndefined()
expect(fetchMock).toHaveBeenCalledWith(
'http://localhost/api/opencode/session/ses_1?directory=%2Frepo',
expect.objectContaining({ method: 'DELETE' }),
)
expect(lastRequest().url).toBe('http://localhost/api/opencode/api/session/ses_1')
expect(lastRequest().init.method).toBe('DELETE')
})
it('deletes workspaces with directory routing', async () => {
it('renames a session with a title update', async () => {
fetchMock.mockResolvedValue(new Response(null, { status: 204 }))
await expect(new OpenCodeClient('/api/opencode', '/repo').deleteWorkspace('wrk_stale')).resolves.toBeUndefined()
await renameSession('ses_1', 'Renamed')
expect(fetchMock).toHaveBeenCalledWith(
'http://localhost/api/opencode/experimental/workspace/wrk_stale?directory=%2Frepo',
expect.objectContaining({ method: 'DELETE' }),
)
expect(lastRequest().url).toBe('http://localhost/api/opencode/api/session/ses_1')
expect(lastRequest().init.method).toBe('PATCH')
expect(lastRequest().init.body).toBe(JSON.stringify({ title: 'Renamed' }))
})
it('preserves text error responses', async () => {
fetchMock.mockResolvedValue(new Response('Workspace not found: wrk_stale', { status: 500 }))
it('forks a session before a message', async () => {
fetchMock.mockResolvedValue(jsonResponse({ data: sessionInfo('ses_child', '/repo') }))
await expect(new OpenCodeClient('/api/opencode', '/repo').deleteSession('ses_1')).rejects.toThrow(
'Workspace not found: wrk_stale',
)
const forked = await forkSession('ses_1', 'msg_1')
expect(lastRequest().url).toBe('http://localhost/api/opencode/api/session/ses_1/fork')
expect(lastRequest().init.method).toBe('POST')
expect(lastRequest().init.body).toBe(JSON.stringify({ before: 'msg_1' }))
expect(forked.id).toBe('ses_child')
})
it('responds to permission requests via the request-scoped reply endpoint', async () => {
fetchMock.mockResolvedValue(new Response(JSON.stringify({}), { status: 200 }))
it('forks a session without a boundary when none is given', async () => {
fetchMock.mockResolvedValue(jsonResponse({ data: sessionInfo('ses_child', '/repo') }))
await new OpenCodeClient('/api/opencode', '/repo').respondToPermission('per_1', 'reject')
await forkSession('ses_1')
expect(fetchMock).toHaveBeenCalledWith(
'http://localhost/api/opencode/permission/per_1/reply?directory=%2Frepo',
expect.objectContaining({
method: 'POST',
headers: expect.objectContaining({ 'Content-Type': 'application/json' }),
body: JSON.stringify({ reply: 'reject' }),
expect(lastRequest().url).toBe('http://localhost/api/opencode/api/session/ses_1/fork')
expect(lastRequest().init.body).toBe(JSON.stringify({}))
})
it('finds files through the V2 filesystem route and returns paths', async () => {
fetchMock.mockResolvedValue(
jsonResponse({
location: { directory: '/repo' },
data: [
{ path: 'src/index.ts', type: 'file' },
{ path: 'src/app.ts', type: 'file' },
],
}),
)
const files = await findFiles({ directory: '/repo', query: 'index', limit: 20 })
expect(lastRequest().url).toBe(
'http://localhost/api/opencode/api/fs/find?location%5Bdirectory%5D=%2Frepo&query=index&type=file&limit=20',
)
expect(lastRequest().init.method).toBe('GET')
expect(files).toEqual(['src/index.ts', 'src/app.ts'])
})
describe('listSessionsPage', () => {
it('handles v1.16.0+ API response format with data envelope and nested location', async () => {
fetchMock.mockResolvedValue(
new Response(
JSON.stringify({
data: [
{
id: 'ses_v2_1',
projectID: 'proj_v2',
parentID: 'parent_v2',
title: 'V2 Session',
time: { created: 3000, updated: 4000 },
location: { directory: '/my-repo', workspaceID: 'ws_v2' },
agent: 'default',
model: { id: 'gpt-4', providerID: 'openai' },
cost: 0.05,
tokens: { input: 100, output: 50, reasoning: 10, cache: { read: 0, write: 0 } },
},
],
cursor: { next: 'v2_cursor' },
}),
{ status: 200 },
),
)
it('finds files without a limit when none is given', async () => {
fetchMock.mockResolvedValue(jsonResponse({ location: { directory: '/repo' }, data: [] }))
const result = await new OpenCodeClient('/api/opencode', '/repo').listSessionsPage({ limit: 10 })
await findFiles({ directory: '/repo', query: 'index' })
expect(result.items).toHaveLength(1)
expect(result.items[0]).toMatchObject({
id: 'ses_v2_1',
projectID: 'proj_v2',
workspaceID: 'ws_v2',
directory: '/repo',
parentID: 'parent_v2',
title: 'V2 Session',
version: 'v2',
})
expect(result.nextCursor).toBe('v2_cursor')
expect(lastRequest().url).toBe(
'http://localhost/api/opencode/api/fs/find?location%5Bdirectory%5D=%2Frepo&query=index&type=file',
)
})
it('reads a session snapshot from the message page, inbox, and active sessions', async () => {
const message = {
id: 'msg_1',
type: 'user' as const,
text: 'Run the tests',
time: { created: 1000 },
}
const inbox = {
id: 'msg_2',
sessionID: 'ses_1',
time: { created: 2000 },
type: 'user' as const,
payload: { text: 'Wait for me' },
delivery: 'queue' as const,
}
fetchMock.mockImplementation((input: unknown) => {
const url = String(input)
if (url.endsWith('/api/session/ses_1/inbox')) {
return Promise.resolve(jsonResponse({ data: [inbox] }))
}
if (url.endsWith('/api/session/active')) {
return Promise.resolve(jsonResponse({ data: { ses_1: { type: 'running' } } }))
}
return Promise.resolve(jsonResponse({ data: [message], cursor: { next: 'cursor_next' } }))
})
it('sends first-page params to /api/session with directory and returns adapted sessions', async () => {
fetchMock.mockResolvedValue(
new Response(
JSON.stringify({
items: [
{
id: 'ses_1',
projectID: 'proj_1',
workspaceID: 'ws_1',
parentID: 'parent_1',
title: 'My Session',
time: { created: 1000, updated: 2000 },
},
],
cursor: { next: 'cursor_next' },
}),
{ status: 200 },
),
)
const snapshot = await readSessionSnapshot('ses_1')
const result = await new OpenCodeClient('/api/opencode', '/repo').listSessionsPage({
limit: 25,
order: 'desc',
search: 'deploy',
})
expect(fetchMock).toHaveBeenCalledWith(
'http://localhost/api/opencode/api/session?limit=25&order=desc&search=deploy&directory=%2Frepo',
expect.any(Object),
)
expect(result.items).toHaveLength(1)
expect(result.items[0]).toMatchObject({
id: 'ses_1',
projectID: 'proj_1',
directory: '/repo',
parentID: 'parent_1',
title: 'My Session',
version: 'v2',
time: { created: 1000, updated: 2000 },
})
expect(snapshot).toEqual({
messages: [message],
nextCursor: 'cursor_next',
pending: [inbox],
status: 'busy',
})
expect(fetchMock.mock.calls.map(([input]) => String(input)).sort()).toEqual([
'http://localhost/api/opencode/api/session/active',
'http://localhost/api/opencode/api/session/ses_1/inbox',
'http://localhost/api/opencode/api/session/ses_1/message?order=desc',
])
})
it('sends cursor-only params without directory for cursor-based requests', async () => {
fetchMock.mockResolvedValue(
new Response(
JSON.stringify({
items: [],
}),
{ status: 200 },
),
)
it('requests the newest message page with descending order', async () => {
fetchMock.mockResolvedValue(
jsonResponse({ data: [{ id: 'msg_3' }, { id: 'msg_2' }], cursor: { next: 'cursor_2' } }),
)
await new OpenCodeClient('/api/opencode', '/repo').listSessionsPage({ cursor: 'cursor_123' })
const page = await listSessionMessages('ses_1', { limit: 20 })
expect(fetchMock).toHaveBeenCalledWith(
'http://localhost/api/opencode/api/session?cursor=cursor_123',
expect.any(Object),
expect(lastRequest().url).toBe(
'http://localhost/api/opencode/api/session/ses_1/message?limit=20&order=desc',
)
expect(page.messages.map((message) => message.id)).toEqual(['msg_2', 'msg_3'])
expect(page.nextCursor).toBe('cursor_2')
})
it('omits order on continuation pages so the server accepts the cursor', async () => {
const server = vi.fn((input: unknown) => {
const url = String(input)
if (url.includes('cursor=') && url.includes('order=')) {
return Promise.resolve(
new Response(
JSON.stringify({ _tag: 'InvalidCursorError', message: 'Cursor cannot be combined with order' }),
{ status: 400, headers: { 'Content-Type': 'application/json' } },
),
)
}
return Promise.resolve(
jsonResponse({ data: [{ id: 'msg_2' }, { id: 'msg_1' }], cursor: { next: 'cursor_2' } }),
)
})
fetchMock.mockImplementation(server)
it('exposes response cursor as nextCursor', async () => {
fetchMock.mockResolvedValue(
new Response(
JSON.stringify({
items: [],
cursor: { next: 'next_1' },
}),
{ status: 200 },
),
)
const page = await listSessionMessages('ses_1', { cursor: 'cursor_1' })
const result = await new OpenCodeClient('/api/opencode', '/repo').listSessionsPage({ limit: 10 })
expect(lastRequest().url).toBe(
'http://localhost/api/opencode/api/session/ses_1/message?cursor=cursor_1',
)
expect(page.messages.map((message) => message.id)).toEqual(['msg_1', 'msg_2'])
expect(page.nextCursor).toBe('cursor_2')
})
expect(result.nextCursor).toBe('next_1')
it('wraps V2 client failures in a FetchError', async () => { fetchMock.mockResolvedValue(new Response('upstream exploded', { status: 500 }))
const failure = await getSession('ses_1').catch((error: unknown) => error)
expect(failure).toBeInstanceOf(FetchError)
expect((failure as FetchError).statusCode).toBe(500)
})
it('maps a declared V2 not-found error to 404 instead of a network failure', async () => {
fetchMock.mockResolvedValue(
new Response(
JSON.stringify({ _tag: 'SessionNotFoundError', sessionID: 'ses_1', message: 'Session not found' }),
{ status: 404, headers: { 'Content-Type': 'application/json' } },
),
)
const failure = await getSession('ses_1').catch((error: unknown) => error)
expect(failure).toBeInstanceOf(FetchError)
expect((failure as FetchError).statusCode).toBe(404)
expect((failure as FetchError).message).toBe('Session not found')
})
it('maps a declared V2 conflict error to 409', async () => {
fetchMock.mockResolvedValue(
new Response(
JSON.stringify({ _tag: 'SessionBusyError', sessionID: 'ses_1', message: 'Session is busy' }),
{ status: 409, headers: { 'Content-Type': 'application/json' } },
),
)
const failure = await sendPrompt({ sessionID: 'ses_1', text: 'Hi' }).catch((error: unknown) => error)
expect(failure).toBeInstanceOf(FetchError)
expect((failure as FetchError).statusCode).toBe(409)
})
it('sends a prompt with files, agents, skills, and delivery', async () => {
fetchMock.mockResolvedValue(
jsonResponse({
data: {
id: 'msg_1',
sessionID: 'ses_1',
time: { created: 1000 },
type: 'user',
payload: { text: 'Hello @src/a.ts' },
delivery: 'queue',
},
}),
)
const inbox = await sendPrompt({
sessionID: 'ses_1',
text: 'Hello @src/a.ts',
files: [{ uri: 'file:///repo/src/a.ts', name: 'a.ts', mention: { start: 6, end: 15, text: '@src/a.ts' } }],
agents: [{ name: 'build', mention: { start: 6, end: 12, text: '@build' } }],
skills: [{ id: 'review' }],
delivery: 'queue',
})
it('uses Untitled Session for empty title', async () => {
fetchMock.mockResolvedValue(
new Response(
JSON.stringify({
items: [
{
id: 'ses_2',
projectID: 'proj_2',
title: '',
time: { created: 1000, updated: 2000 },
},
],
}),
{ status: 200 },
),
)
expect(lastRequest().url).toBe('http://localhost/api/opencode/api/session/ses_1/prompt')
expect(lastRequest().init.method).toBe('POST')
expect(lastRequest().init.body).toBe(JSON.stringify({
text: 'Hello @src/a.ts',
files: [{ uri: 'file:///repo/src/a.ts', name: 'a.ts', mention: { start: 6, end: 15, text: '@src/a.ts' } }],
agents: [{ name: 'build', mention: { start: 6, end: 12, text: '@build' } }],
skills: [{ id: 'review' }],
delivery: 'queue',
}))
expect(inbox.id).toBe('msg_1')
})
const result = await new OpenCodeClient('/api/opencode', '/repo').listSessionsPage()
it('switches the session model through the V2 model route', async () => {
fetchMock.mockResolvedValue(new Response(null, { status: 204 }))
expect(result.items[0].title).toBe('Untitled Session')
await switchSessionModel('ses_1', { providerID: 'anthropic', id: 'claude', variant: 'v1' })
expect(lastRequest().url).toBe('http://localhost/api/opencode/api/session/ses_1/model')
expect(lastRequest().init.method).toBe('POST')
expect(lastRequest().init.body).toBe(JSON.stringify({ model: { providerID: 'anthropic', id: 'claude', variant: 'v1' } }))
})
it('switches the session agent through the V2 agent route', async () => {
fetchMock.mockResolvedValue(new Response(null, { status: 204 }))
await switchSessionAgent('ses_1', 'build')
expect(lastRequest().url).toBe('http://localhost/api/opencode/api/session/ses_1/agent')
expect(lastRequest().init.method).toBe('POST')
expect(lastRequest().init.body).toBe(JSON.stringify({ agent: 'build' }))
})
it('runs a session command', async () => {
fetchMock.mockResolvedValue(new Response(null, { status: 204 }))
await runCommand({
sessionID: 'ses_1',
name: 'init',
text: 'extra context',
agents: [{ name: 'build' }],
})
it('works without directory set', async () => {
fetchMock.mockResolvedValue(
new Response(
JSON.stringify({
items: [
{
id: 'ses_3',
projectID: 'proj_3',
title: 'No Dir',
time: { created: 1000, updated: 2000 },
},
],
}),
{ status: 200 },
),
)
expect(lastRequest().url).toBe('http://localhost/api/opencode/api/session/ses_1/command')
expect(lastRequest().init.method).toBe('POST')
expect(lastRequest().init.body).toBe(JSON.stringify({
name: 'init',
text: 'extra context',
agents: [{ name: 'build' }],
}))
})
const result = await new OpenCodeClient('/api/opencode').listSessionsPage({ limit: 5 })
it('runs a session shell command', async () => {
fetchMock.mockResolvedValue(new Response(null, { status: 204 }))
expect(fetchMock).toHaveBeenCalledWith(
'http://localhost/api/opencode/api/session?limit=5',
expect.any(Object),
)
expect(result.items[0].directory).toBe('')
})
await runShell('ses_1', 'ls -la')
expect(lastRequest().url).toBe('http://localhost/api/opencode/api/session/ses_1/shell')
expect(lastRequest().init.method).toBe('POST')
expect(lastRequest().init.body).toBe(JSON.stringify({ command: 'ls -la' }))
})
it('interrupts a session', async () => {
fetchMock.mockResolvedValue(jsonResponse({ data: { interrupted: true } }))
await interruptSession('ses_1')
expect(lastRequest().url).toBe('http://localhost/api/opencode/api/session/ses_1/interrupt')
expect(lastRequest().init.method).toBe('POST')
})
it('stages, commits, and clears a session revert', async () => {
fetchMock.mockResolvedValue(jsonResponse({ data: { messageID: 'msg_1' } }))
await stageRevert('ses_1', 'msg_1')
expect(lastRequest().url).toBe('http://localhost/api/opencode/api/session/ses_1/revert/stage')
expect(lastRequest().init.method).toBe('POST')
expect(lastRequest().init.body).toBe(JSON.stringify({ messageID: 'msg_1' }))
fetchMock.mockResolvedValue(new Response(null, { status: 204 }))
await commitRevert('ses_1')
expect(lastRequest().url).toBe('http://localhost/api/opencode/api/session/ses_1/revert/commit')
expect(lastRequest().init.method).toBe('POST')
await clearRevert('ses_1')
expect(lastRequest().url).toMatch(/\/revert$/)
expect(lastRequest().init.method).toBe('DELETE')
})
it('compacts a session', async () => {
fetchMock.mockResolvedValue(
jsonResponse({ data: { id: 'cmp_1', sessionID: 'ses_1', time: { created: 1000 }, type: 'compaction', delivery: 'steer' } }),
)
const compaction = await compactSession('ses_1')
expect(lastRequest().url).toBe('http://localhost/api/opencode/api/session/ses_1/compact')
expect(lastRequest().init.method).toBe('POST')
expect(compaction.id).toBe('cmp_1')
})
it('activates a skill on the experimental session route', async () => {
fetchMock.mockResolvedValue(new Response(null, { status: 204 }))
await activateSkill('ses_1', 'review')
expect(lastRequest().url).toBe('http://localhost/api/opencode/api/experimental/session/ses_1/skill')
expect(lastRequest().init.method).toBe('POST')
expect(lastRequest().init.body).toBe(JSON.stringify({ id: 'review' }))
})
it('lists commands scoped to a directory', async () => {
fetchMock.mockResolvedValue(
jsonResponse({ location: { directory: '/repo' }, data: [{ name: 'init', description: 'Init' }] }),
)
const commands = await listCommands('/repo')
expect(lastRequest().url).toBe('http://localhost/api/opencode/api/command?location%5Bdirectory%5D=%2Frepo')
expect(lastRequest().init.method).toBe('GET')
expect(commands).toEqual([{ name: 'init', description: 'Init' }])
})
it('lists pending permissions scoped to a directory', async () => {
fetchMock.mockResolvedValue(
jsonResponse({
location: { directory: '/repo' },
data: [{ id: 'perm_1', sessionID: 'ses_1', action: 'shell', resources: ['ls'] }],
}),
)
const permissions = await listPendingPermissions('/repo')
expect(lastRequest().url).toBe(
'http://localhost/api/opencode/api/permission/request?location%5Bdirectory%5D=%2Frepo',
)
expect(lastRequest().init.method).toBe('GET')
expect(permissions).toEqual([{ id: 'perm_1', sessionID: 'ses_1', action: 'shell', resources: ['ls'] }])
})
it('replies to a permission with a decision and rejection message', async () => {
fetchMock.mockResolvedValue(new Response(null, { status: 204 }))
await replyPermission('ses_1', 'perm_1', 'reject', 'not allowed')
expect(lastRequest().url).toBe(
'http://localhost/api/opencode/api/session/ses_1/permission/perm_1/reply',
)
expect(lastRequest().init.method).toBe('POST')
expect(lastRequest().init.body).toBe(JSON.stringify({ decision: 'reject', message: 'not allowed' }))
})
it('replies to a permission without a message', async () => {
fetchMock.mockResolvedValue(new Response(null, { status: 204 }))
await replyPermission('ses_1', 'perm_1', 'once')
expect(lastRequest().init.body).toBe(JSON.stringify({ decision: 'once' }))
})
it('lists pending forms scoped to a directory', async () => {
const form = {
id: 'form_1',
sessionID: 'ses_1',
title: 'Continue?',
fields: [{ key: 'q0', type: 'string' }],
}
fetchMock.mockResolvedValue(jsonResponse({ location: { directory: '/repo' }, data: [form] }))
const forms = await listPendingForms('/repo')
expect(lastRequest().url).toBe('http://localhost/api/opencode/api/form?location%5Bdirectory%5D=%2Frepo')
expect(lastRequest().init.method).toBe('GET')
expect(forms).toEqual([form])
})
it('replies to a form with an answer keyed by field', async () => {
fetchMock.mockResolvedValue(new Response(null, { status: 204 }))
await replyForm('ses_1', 'form_1', { q0: 'Yes' })
expect(lastRequest().url).toBe(
'http://localhost/api/opencode/api/session/ses_1/form/form_1/reply',
)
expect(lastRequest().init.method).toBe('POST')
expect(lastRequest().init.body).toBe(JSON.stringify({ answer: { q0: 'Yes' } }))
})
it('cancels a form', async () => {
fetchMock.mockResolvedValue(new Response(null, { status: 204 }))
await cancelForm('ses_1', 'form_1')
expect(lastRequest().url).toBe('http://localhost/api/opencode/api/session/ses_1/form/form_1')
expect(lastRequest().init.method).toBe('DELETE')
})
})