Files
opencode-manager/backend/test/helpers/stub-schedule-api.ts
Chris Scott dacde879d6 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
2026-09-26 13:16:45 -04:00

102 lines
2.7 KiB
TypeScript

import { vi } from 'vitest'
import type {
OpenCodeApi,
SessionMessageAssistant,
SessionMessageInfo,
SkillInfo,
} from '@opencode-manager/shared/opencode'
export interface ScheduleApiState {
sessionID: string
messages: SessionMessageInfo[]
active: Record<string, { type: 'running' }>
skills: SkillInfo[]
agents: string[]
createError?: Error
promptError?: Error
interruptError?: Error
messageError?: Error
activeError?: Error
skillError?: Error
}
export interface ScheduleApiStub {
api: OpenCodeApi
state: ScheduleApiState
}
export function createStubScheduleApi(state: Partial<ScheduleApiState> = {}): ScheduleApiStub {
const resolved: ScheduleApiState = {
sessionID: 'ses-run-1',
messages: [],
active: {},
skills: [],
agents: ['build', 'plan'],
...state,
}
const session = {
create: vi.fn(async () => {
if (resolved.createError) throw resolved.createError
return { id: resolved.sessionID }
}),
prompt: vi.fn(async () => {
if (resolved.promptError) throw resolved.promptError
return {}
}),
interrupt: vi.fn(async () => {
if (resolved.interruptError) throw resolved.interruptError
return { interrupted: true }
}),
active: vi.fn(async () => {
if (resolved.activeError) throw resolved.activeError
return resolved.active
}),
}
const api = {
session,
message: {
list: vi.fn(async () => {
if (resolved.messageError) throw resolved.messageError
return { data: resolved.messages, cursor: {} }
}),
},
skill: {
list: vi.fn(async () => {
if (resolved.skillError) throw resolved.skillError
return { location: { directory: '' }, data: resolved.skills }
}),
},
agent: {
list: vi.fn(async () => ({ location: { directory: '' }, data: resolved.agents.map((id) => ({ id, name: id })) })),
},
}
return { api: api as unknown as OpenCodeApi, state: resolved }
}
let messageSequence = 0
export function assistantMessage(
text: string,
options: { completed?: boolean; error?: string; content?: SessionMessageAssistant['content'] } = {},
): SessionMessageInfo {
messageSequence += 1
const created = Date.now()
return {
id: `msg_assistant_${messageSequence}`,
type: 'assistant',
agent: 'build',
model: { id: 'gpt-5', providerID: 'openai' },
time: { created, ...(options.completed ? { completed: created } : {}) },
content: options.content ?? [{ type: 'text', text }],
...(options.error ? { error: { type: 'ProviderError', message: options.error } } : {}),
} as SessionMessageInfo
}
export function skill(id: string): SkillInfo {
return { id, name: id, path: `/skills/${id}/SKILL.md`, content: '' }
}