Files
opencode-manager/ocm-cli/src/session-move.ts
T
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

100 lines
3.5 KiB
TypeScript

import { fileURLToPath, pathToFileURL } from 'node:url'
import type { SessionTransferData } from '@opencode-manager/shared/opencode'
export type RewriteContext = { localRoot: string; remoteRoot: string }
export function rewriteTransferForRemote(data: SessionTransferData, ctx: RewriteContext): SessionTransferData {
const cloned = structuredClone(data)
cloned.info.location.directory = relocatePath(cloned.info.location.directory, ctx)
relocateUris(cloned.messages, ctx)
return cloned
}
function relocateUris(value: unknown, ctx: RewriteContext): void {
if (Array.isArray(value)) {
for (const item of value) relocateUris(item, ctx)
return
}
if (!value || typeof value !== 'object') return
const record = value as Record<string, unknown>
if (typeof record.uri === 'string') record.uri = relocateUri(record.uri, ctx)
for (const child of Object.values(record)) relocateUris(child, ctx)
}
function relocateUri(uri: string, ctx: RewriteContext): string {
const fileUrl = parseLocalFileUrl(uri)
if (!fileUrl) return relocatePath(uri, ctx)
const relocated = relocatePath(fileUrl.path, ctx)
if (relocated === fileUrl.path) return uri
return `${pathToFileURL(relocated).href}${fileUrl.search}${fileUrl.hash}`
}
function parseLocalFileUrl(uri: string): { path: string; search: string; hash: string } | null {
if (!uri.startsWith('file:')) return null
let url: URL
try {
url = new URL(uri)
} catch {
return null
}
if (url.protocol !== 'file:' || (url.host !== '' && url.host !== 'localhost')) return null
let path: string
try {
path = fileURLToPath(url)
} catch {
return null
}
return { path, search: url.search, hash: url.hash }
}
function relocatePath(path: string, ctx: RewriteContext): string {
if (path === ctx.localRoot) return ctx.remoteRoot
if (path.startsWith(ctx.localRoot + '/')) return ctx.remoteRoot + path.slice(ctx.localRoot.length)
return path
}
export type TransferDeps = {
exportSession: (sessionID: string) => Promise<SessionTransferData>
importSession: (remoteDirectory: string, data: SessionTransferData) => Promise<{ sessionID: string }>
onProgress?: (transferred: number, total: number) => void
}
export type TransferInput = { sessionID: string; localRoot: string; remoteDirectory: string }
export type TransferResult =
| { kind: 'moved'; sessionID: string; importedMessages: number }
| { kind: 'import-failed'; message: string }
export async function transferSession(input: TransferInput, deps: TransferDeps): Promise<TransferResult> {
let data: SessionTransferData
try {
data = await deps.exportSession(input.sessionID)
} catch (err) {
return { kind: 'import-failed', message: err instanceof Error ? err.message : String(err) }
}
const rewritten = rewriteTransferForRemote(data, {
localRoot: input.localRoot,
remoteRoot: input.remoteDirectory,
})
const total = rewritten.messages.length
deps.onProgress?.(0, total)
let imported: { sessionID: string }
try {
imported = await deps.importSession(input.remoteDirectory, rewritten)
} catch (err) {
return { kind: 'import-failed', message: err instanceof Error ? err.message : String(err) }
}
deps.onProgress?.(total, total)
return { kind: 'moved', sessionID: imported.sessionID, importedMessages: total }
}
export function moveReminderText(directory: string): string {
return `<system-reminder>The user has changed the current working directory to "${directory}". This is still the same project but at a possibly new location; take this into account when working with any files from now on.</system-reminder>`
}