mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 04:44:21 +01:00
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:
1 parent
6c2ebd764b
commit
dacde879d6
383 files changed
+29905
-46566
No files matched your search
@@ -1,172 +1,159 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import type { ModelInfo } from '@opencode-manager/shared/opencode'
|
||||
import type { OpenCodeClient } from '../../src/services/opencode/client'
|
||||
import { resolveOpenCodeModel } from '../../src/services/opencode-models'
|
||||
|
||||
function model(providerID: string, id: string, enabled: boolean = true): ModelInfo {
|
||||
return { providerID, id, enabled } as ModelInfo
|
||||
}
|
||||
|
||||
function createClientStub(input: {
|
||||
models: ModelInfo[]
|
||||
defaultModel?: ModelInfo | null
|
||||
listError?: Error
|
||||
defaultError?: Error
|
||||
}): OpenCodeClient {
|
||||
return {
|
||||
api: {
|
||||
model: {
|
||||
list: vi.fn(async () => {
|
||||
if (input.listError) throw input.listError
|
||||
return { location: { directory: '/workspace/repos/sample-project' }, data: input.models }
|
||||
}),
|
||||
default: vi.fn(async () => {
|
||||
if (input.defaultError) throw input.defaultError
|
||||
return { location: { directory: '/workspace/repos/sample-project' }, data: input.defaultModel ?? null }
|
||||
}),
|
||||
},
|
||||
},
|
||||
} as unknown as OpenCodeClient
|
||||
}
|
||||
|
||||
describe('resolveOpenCodeModel', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
it('returns the preferred model when it is available', async () => {
|
||||
const mockClient = {
|
||||
getJson: vi.fn().mockImplementation((path: string) => {
|
||||
if (path === '/config') {
|
||||
return Promise.resolve({ model: 'openai/gpt-5' })
|
||||
}
|
||||
if (path === '/config/providers') {
|
||||
return Promise.resolve({
|
||||
providers: [
|
||||
{ id: 'openai', models: { 'gpt-5': {}, 'gpt-5-mini': {} } },
|
||||
],
|
||||
default: { openai: 'gpt-5-mini' },
|
||||
})
|
||||
}
|
||||
throw new Error(`Unexpected path: ${path}`)
|
||||
}),
|
||||
} as unknown as OpenCodeClient
|
||||
const client = createClientStub({
|
||||
models: [model('openai', 'gpt-5'), model('openai', 'gpt-5-mini')],
|
||||
defaultModel: model('openai', 'gpt-5-mini'),
|
||||
})
|
||||
|
||||
const result = await resolveOpenCodeModel(mockClient, '/workspace/repos/sample-project', {
|
||||
const result = await resolveOpenCodeModel(client, '/workspace/repos/sample-project', {
|
||||
preferredModel: 'openai/gpt-5',
|
||||
})
|
||||
|
||||
expect(result).toEqual({
|
||||
providerID: 'openai',
|
||||
modelID: 'gpt-5',
|
||||
id: 'gpt-5',
|
||||
model: 'openai/gpt-5',
|
||||
})
|
||||
})
|
||||
|
||||
it('falls back to the configured model when the preferred model is unavailable', async () => {
|
||||
const mockClient = {
|
||||
getJson: vi.fn().mockImplementation((path: string) => {
|
||||
if (path === '/config') {
|
||||
return Promise.resolve({ model: 'openai/gpt-5' })
|
||||
}
|
||||
if (path === '/config/providers') {
|
||||
return Promise.resolve({
|
||||
providers: [
|
||||
{ id: 'openai', models: { 'gpt-5': {}, 'gpt-5-mini': {} } },
|
||||
],
|
||||
default: { openai: 'gpt-5-mini' },
|
||||
})
|
||||
}
|
||||
throw new Error(`Unexpected path: ${path}`)
|
||||
}),
|
||||
} as unknown as OpenCodeClient
|
||||
it('preserves the variant of a preferred model reference', async () => {
|
||||
const client = createClientStub({
|
||||
models: [model('openai', 'gpt-5')],
|
||||
})
|
||||
|
||||
const result = await resolveOpenCodeModel(mockClient, '/workspace/repos/sample-project', {
|
||||
const result = await resolveOpenCodeModel(client, '/workspace/repos/sample-project', {
|
||||
preferredModel: 'openai/gpt-5#high',
|
||||
})
|
||||
|
||||
expect(result).toEqual({
|
||||
providerID: 'openai',
|
||||
id: 'gpt-5',
|
||||
variant: 'high',
|
||||
model: 'openai/gpt-5#high',
|
||||
})
|
||||
})
|
||||
|
||||
it('keeps slashes inside the model id when parsing the preferred model', async () => {
|
||||
const client = createClientStub({
|
||||
models: [model('openrouter', 'anthropic/claude-sonnet-4')],
|
||||
})
|
||||
|
||||
const result = await resolveOpenCodeModel(client, '/workspace/repos/sample-project', {
|
||||
preferredModel: 'openrouter/anthropic/claude-sonnet-4',
|
||||
})
|
||||
|
||||
expect(result).toEqual({
|
||||
providerID: 'openrouter',
|
||||
id: 'anthropic/claude-sonnet-4',
|
||||
model: 'openrouter/anthropic/claude-sonnet-4',
|
||||
})
|
||||
})
|
||||
|
||||
it('falls back to the OpenCode default model when the preferred model is unavailable', async () => {
|
||||
const client = createClientStub({
|
||||
models: [model('openai', 'gpt-5'), model('openai', 'gpt-5-mini')],
|
||||
defaultModel: model('openai', 'gpt-5-mini'),
|
||||
})
|
||||
|
||||
const result = await resolveOpenCodeModel(client, '/workspace/repos/sample-project', {
|
||||
preferredModel: 'openai/retired',
|
||||
})
|
||||
|
||||
expect(result).toEqual({
|
||||
providerID: 'openai',
|
||||
modelID: 'gpt-5',
|
||||
model: 'openai/gpt-5',
|
||||
})
|
||||
})
|
||||
|
||||
it('falls back to the provider default when the configured model is unavailable', async () => {
|
||||
const mockClient = {
|
||||
getJson: vi.fn().mockImplementation((path: string) => {
|
||||
if (path === '/config') {
|
||||
return Promise.resolve({ model: 'openai/gpt-5-unavailable' })
|
||||
}
|
||||
if (path === '/config/providers') {
|
||||
return Promise.resolve({
|
||||
providers: [
|
||||
{ id: 'openai', models: { 'gpt-5-mini': {}, 'gpt-5-turbo': {} } },
|
||||
],
|
||||
default: { openai: 'gpt-5-mini' },
|
||||
})
|
||||
}
|
||||
throw new Error(`Unexpected path: ${path}`)
|
||||
}),
|
||||
} as unknown as OpenCodeClient
|
||||
|
||||
const result = await resolveOpenCodeModel(mockClient, '/workspace/repos/sample-project', {
|
||||
preferredModel: 'openai/retired',
|
||||
})
|
||||
|
||||
expect(result).toEqual({
|
||||
providerID: 'openai',
|
||||
modelID: 'gpt-5-mini',
|
||||
id: 'gpt-5-mini',
|
||||
model: 'openai/gpt-5-mini',
|
||||
})
|
||||
})
|
||||
|
||||
it('never selects the configured small_model', async () => {
|
||||
const mockClient = {
|
||||
getJson: vi.fn().mockImplementation((path: string) => {
|
||||
if (path === '/config') {
|
||||
return Promise.resolve({
|
||||
model: 'openai/gpt-5-unavailable',
|
||||
small_model: 'openai/gpt-5-mini',
|
||||
})
|
||||
}
|
||||
if (path === '/config/providers') {
|
||||
return Promise.resolve({
|
||||
providers: [
|
||||
{ id: 'openai', models: { 'gpt-5-mini': {}, 'gpt-5-turbo': {} } },
|
||||
],
|
||||
default: { openai: 'gpt-5-turbo' },
|
||||
})
|
||||
}
|
||||
throw new Error(`Unexpected path: ${path}`)
|
||||
}),
|
||||
} as unknown as OpenCodeClient
|
||||
it('falls back to the first enabled model when the default model is unavailable', async () => {
|
||||
const client = createClientStub({
|
||||
models: [model('openai', 'gpt-5'), model('anthropic', 'claude-sonnet-4')],
|
||||
defaultModel: model('openai', 'retired'),
|
||||
})
|
||||
|
||||
const result = await resolveOpenCodeModel(mockClient, '/workspace/repos/sample-project', {
|
||||
const result = await resolveOpenCodeModel(client, '/workspace/repos/sample-project', {
|
||||
preferredModel: 'openai/retired',
|
||||
})
|
||||
|
||||
expect(result).toEqual({
|
||||
providerID: 'openai',
|
||||
modelID: 'gpt-5-turbo',
|
||||
model: 'openai/gpt-5-turbo',
|
||||
id: 'gpt-5',
|
||||
model: 'openai/gpt-5',
|
||||
})
|
||||
})
|
||||
|
||||
it('falls back to the first available model when defaults are missing', async () => {
|
||||
const mockClient = {
|
||||
getJson: vi.fn().mockImplementation((path: string) => {
|
||||
if (path === '/config') {
|
||||
return Promise.resolve({})
|
||||
}
|
||||
if (path === '/config/providers') {
|
||||
return Promise.resolve({
|
||||
providers: [
|
||||
{ id: 'anthropic', models: { 'claude-sonnet-4': {}, 'claude-haiku-4': {} } },
|
||||
],
|
||||
})
|
||||
}
|
||||
throw new Error(`Unexpected path: ${path}`)
|
||||
}),
|
||||
} as unknown as OpenCodeClient
|
||||
it('skips disabled models when choosing the first enabled entry', async () => {
|
||||
const client = createClientStub({
|
||||
models: [model('openai', 'gpt-5', false), model('anthropic', 'claude-sonnet-4')],
|
||||
})
|
||||
|
||||
const result = await resolveOpenCodeModel(mockClient, '/workspace/repos/sample-project')
|
||||
const result = await resolveOpenCodeModel(client, '/workspace/repos/sample-project')
|
||||
|
||||
expect(result).toEqual({
|
||||
providerID: 'anthropic',
|
||||
modelID: 'claude-sonnet-4',
|
||||
id: 'claude-sonnet-4',
|
||||
model: 'anthropic/claude-sonnet-4',
|
||||
})
|
||||
})
|
||||
|
||||
it('throws when no configured models are available', async () => {
|
||||
const mockClient = {
|
||||
getJson: vi.fn().mockImplementation((path: string) => {
|
||||
if (path === '/config') {
|
||||
return Promise.resolve({ model: 'openai/gpt-5' })
|
||||
}
|
||||
if (path === '/config/providers') {
|
||||
return Promise.resolve({ providers: [], default: {} })
|
||||
}
|
||||
throw new Error(`Unexpected path: ${path}`)
|
||||
}),
|
||||
} as unknown as OpenCodeClient
|
||||
it('throws when no enabled models are available', async () => {
|
||||
const client = createClientStub({ models: [model('openai', 'gpt-5', false)] })
|
||||
|
||||
await expect(resolveOpenCodeModel(mockClient, '/workspace/repos/sample-project')).rejects.toThrow(
|
||||
await expect(resolveOpenCodeModel(client, '/workspace/repos/sample-project')).rejects.toThrow(
|
||||
'No configured OpenCode models are available',
|
||||
)
|
||||
})
|
||||
|
||||
it('ignores a malformed preferred model reference and falls back to the default', async () => {
|
||||
const client = createClientStub({
|
||||
models: [model('openai', 'gpt-5')],
|
||||
defaultModel: model('openai', 'gpt-5'),
|
||||
})
|
||||
|
||||
const result = await resolveOpenCodeModel(client, '/workspace/repos/sample-project', {
|
||||
preferredModel: 'openai',
|
||||
})
|
||||
|
||||
expect(result).toEqual({
|
||||
providerID: 'openai',
|
||||
id: 'gpt-5',
|
||||
model: 'openai/gpt-5',
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user