import type { Context } from '@opencode/plugin/tui/context' import { readInstallNotice } from './state.js' import { resolveManagerAuth } from './manager-auth.js' import { fetchRepos, toRemoteRepoSummaries } from './manager-repos.js' import { ManagerApi, ManagerApiError } from './manager-api.js' import type { MirrorTargetPlanResponse } from '@opencode-manager/shared/schemas' import { prepareMirror, checkPushDivergence, describePushDivergence, mirrorUpFast, pickMatchedRepo } from './mirror.js' import type { MirrorPlan, RemoteRepoSummary } from './mirror.js' import { getBranchName } from './local-repo.js' import { transferSession, moveReminderText } from './session-move.js' import { createManagerSessionTransfer } from './remote-session.js' import type { ManagerSessionTransfer } from './remote-session.js' import { confirmDialog, selectDialog } from './tui-dialogs.js' import { setPendingWarp, runPendingWarp } from './warp.js' import { pushPhaseProgress, importProgress } from './move-progress.js' import { warmRepoProxy } from './repo-proxy.js' import type { MoveProgress } from './move-progress.js' import { runGoalCommand } from './tui-goal.js' import { runMultiRunCommand } from './tui-multi-run.js' import type { GoalStore } from './goal-store.js' import type { RemoteContext } from './remote-context.js' export type MoveProgressSetter = (progress: MoveProgress | null) => void export type OcmFeatures = { remote: RemoteContext | undefined goals: GoalStore | undefined } export async function setupOcm(context: Context, setMoveProgress: MoveProgressSetter, features: OcmFeatures): Promise<() => void> { showInstallNotice(context) context.ui.slot({ append: 'app', render: () => { context.keymap.layer(() => ({ mode: 'global', commands: [ { id: 'ocm.session.move', title: 'Move session to Manager', description: 'Push repo state and move this session to OpenCode Manager', group: 'OpenCode Manager', palette: true, slash: { name: 'ocm-move' }, run: () => runSessionMove(context, setMoveProgress), }, { id: 'ocm.goal', title: 'Goal', description: 'Start, pause, resume, or cancel a Manager goal for this session', group: 'OpenCode Manager', palette: true, slash: { name: 'goal', arguments: true }, run: (input) => runGoalCommand(context, { remote: features.remote, store: features.goals }, input), }, { id: 'ocm.multirun', title: 'Multi-run', description: 'Run one prompt across several models on OpenCode Manager', group: 'OpenCode Manager', palette: true, slash: { name: 'multirun', arguments: true }, run: (input) => runMultiRunCommand(context, { remote: features.remote }, input), }, ], })) return null }, }) return () => runPendingWarp() } function showInstallNotice(context: Context): void { const notice = readInstallNotice() if (!notice) return context.ui.toast.show({ variant: 'success', title: 'ocm installed', message: notice.pathMissing ? `Linked at ${notice.link}. Add export PATH="$HOME/.local/bin:$PATH" to your shell rc if ocm is unavailable.` : `Linked at ${notice.link}`, duration: 10000, }) } async function describeMoveBlocker(transfer: ManagerSessionTransfer, sessionID: string, parentID: string | undefined): Promise { if (await transfer.sessionExists(sessionID)) { return `Session ${sessionID} is already on the Manager, so it cannot be moved again. Nothing was pushed. Run \`ocm\` to attach to it.` } if (parentID && !(await transfer.sessionExists(parentID))) { return `This is a subagent session and its parent ${parentID} is not on the Manager. Move the parent session first. Nothing was pushed.` } return null } async function describeRemoteDiscard(repoRoot: string, managerApi: ManagerApi, repoId: number): Promise { try { return describePushDivergence(await checkPushDivergence(repoRoot, managerApi, repoId)) } catch (error) { if (error instanceof ManagerApiError && error.status === 404) return [] throw error } } function describeMoveTarget(repoName: string, target: MirrorTargetPlanResponse): string { switch (target.kind) { case 'in-place': return `Replace the repo state of ${repoName} (${target.fullPath}) with your local working tree and move this session there?` case 'existing': return `${repoName} is checked out on ${target.currentBranch ?? 'another branch'}; branch ${target.branch} lives in worktree ${target.localPath} (${target.fullPath}).\n\nReplace that worktree with your local working tree and move this session there?` case 'new': return `${repoName} is checked out on ${target.currentBranch ?? 'another branch'}; it will not be touched.\n\nCreate worktree ${target.localPath} (${target.fullPath}) for branch ${target.branch}, push your local working tree there, and move this session?` } } function moveConfirmMessage(repoName: string, target: MirrorTargetPlanResponse, discardReasons: string[]): string { const base = describeMoveTarget(repoName, target) if (discardReasons.length === 0) return base return `${base}\n\nThis discards server-side work:\n${discardReasons.map((r) => ` - ${r}`).join('\n')}` } async function resolveMoveTarget(managerApi: ManagerApi, matched: RemoteRepoSummary, remoteDirectory: string, localBranch: string | null): Promise { if (!localBranch) { return { kind: 'in-place', repoId: matched.repoId, fullPath: remoteDirectory, localPath: remoteDirectory, branch: '', currentBranch: null } } return managerApi.mirrorTargetPlan(matched.repoId, localBranch) } async function runSessionMove(context: Context, setMoveProgress: MoveProgressSetter): Promise { try { const current = context.ui.router.current() if (current.type !== 'session') { context.ui.toast.show({ variant: 'error', message: 'Not in a session' }) return } const sessionID = current.sessionID const session = context.data.session.get(sessionID) if (!session?.location.directory) { context.ui.toast.show({ variant: 'error', message: 'Session has no directory' }) return } const auth = await resolveManagerAuth() if (!auth.ok) { context.ui.toast.show({ variant: 'error', message: auth.message }) return } const repos = await fetchRepos(auth.managerUrl, auth.token) const plan = await prepareMirror(session.location.directory, toRemoteRepoSummaries(repos)) if (plan.matched.length === 0) { context.ui.toast.show({ variant: 'error', message: 'No matching Manager repo; run `ocm push --create` first' }) return } const localBranch = getBranchName(plan.repoRoot) const matched = pickMatchedRepo(plan.matched, localBranch) ?? await selectDialog(context, 'Move session to Manager repo', plan.matched.map((r) => ({ title: r.name, description: `id=${r.repoId} branch=${r.branch ?? '-'}`, value: r }))) if (!matched) return const matchedRepoId = matched.repoId const remoteRepo = repos.find((r) => r.repoId === matchedRepoId)! await warmRepoProxy(auth.managerUrl, auth.token, matchedRepoId) const transfer = createManagerSessionTransfer(auth.managerUrl, auth.token) const blocker = await describeMoveBlocker(transfer, sessionID, session.parentID) if (blocker) { context.ui.toast.show({ variant: 'error', message: blocker }) return } const managerApi = new ManagerApi(auth.managerUrl, auth.token) const target = await resolveMoveTarget(managerApi, matched, remoteRepo.directory, localBranch) const discardReasons = target.repoId === null ? [] : await describeRemoteDiscard(plan.repoRoot, managerApi, target.repoId) const proceed = await confirmDialog(context, { title: 'Move session to Manager', message: moveConfirmMessage(matched.name, target, discardReasons), }) if (!proceed) return let targetRepoId = target.repoId if (targetRepoId === null) { setMoveProgress({ label: `creating worktree ${target.localPath}`, fraction: null }) targetRepoId = (await managerApi.mirrorEnsureTarget(matched.repoId, target.branch)).repoId } const selectedPlan: MirrorPlan = { ...plan, matched: [{ ...matched, repoId: targetRepoId }] } const pushed = await mirrorUpFast(selectedPlan, { api: managerApi, force: true, requireCurrentBranch: true, onPhase: (phase) => setMoveProgress(pushPhaseProgress(phase)), }) const remoteDirectory = pushed.fullPath const result = await transferSession( { sessionID, localRoot: plan.repoRoot, remoteDirectory }, { exportSession: (id) => context.client.session.export({ sessionID: id }), importSession: transfer.importSession, onProgress: (transferred, total) => setMoveProgress(importProgress(transferred, total)), }, ) switch (result.kind) { case 'moved': { setMoveProgress({ label: 'notifying moved session', fraction: null }) await transfer.addReminder(result.sessionID, moveReminderText(remoteDirectory)).catch(() => undefined) setMoveProgress(null) const warp = await confirmDialog(context, { title: 'Attach to moved session?', message: 'Exit this TUI and attach to the moved session on the Manager now?' }) if (warp) { await warmRepoProxy(auth.managerUrl, auth.token, pushed.repoId) setPendingWarp({ managerUrl: auth.managerUrl, token: auth.token, repoId: pushed.repoId, sessionID: result.sessionID, repoName: matched.name }) context.keymap.dispatch('app.exit') return } context.ui.toast.show({ variant: 'success', message: `Session moved to Manager (${result.importedMessages} messages). Local copy kept — run \`ocm\` to attach.` }) break } case 'import-failed': context.ui.toast.show({ variant: 'error', message: `Session import failed: ${result.message}` }) break } } catch (err) { context.ui.toast.show({ variant: 'error', message: err instanceof Error ? err.message : String(err) }) } finally { setMoveProgress(null) } }