(opts: {
startedBy?: 'runner' | 'terminal';
startingMode?: 'local' | 'remote';
permissionMode?: PermissionMode;
model?: string;
modelReasoningEffort?: string | null;
resumeSessionId?: string;
existingSessionId?: string;
workingDirectory?: string;
} = {})
| 18 | import { resolveOpencodeSlashCommand } from './utils/slashCommands'; |
| 19 | |
| 20 | export async function runOpencode(opts: { |
| 21 | startedBy?: 'runner' | 'terminal'; |
| 22 | startingMode?: 'local' | 'remote'; |
| 23 | permissionMode?: PermissionMode; |
| 24 | model?: string; |
| 25 | modelReasoningEffort?: string | null; |
| 26 | resumeSessionId?: string; |
| 27 | existingSessionId?: string; |
| 28 | workingDirectory?: string; |
| 29 | } = {}): Promise<void> { |
| 30 | const workingDirectory = opts.workingDirectory ?? getInvokedCwd(); |
| 31 | const startedBy = opts.startedBy ?? 'terminal'; |
| 32 | |
| 33 | logger.debug(`[opencode] Starting with options: startedBy=${startedBy}, startingMode=${opts.startingMode}`); |
| 34 | |
| 35 | if (startedBy === 'runner' && opts.startingMode === 'local') { |
| 36 | logger.debug('[opencode] Runner spawn requested with local mode; forcing remote mode'); |
| 37 | opts.startingMode = 'remote'; |
| 38 | } |
| 39 | |
| 40 | const startingMode: 'local' | 'remote' = opts.startingMode |
| 41 | ?? (startedBy === 'runner' ? 'remote' : 'local'); |
| 42 | |
| 43 | if (opts.permissionMode === 'plan' && startingMode !== 'remote') { |
| 44 | throw new Error('OpenCode plan mode is only supported in remote mode'); |
| 45 | } |
| 46 | |
| 47 | const initialState: AgentState = { |
| 48 | controlledByUser: false |
| 49 | }; |
| 50 | |
| 51 | // Persist only when the user (or runner) explicitly chose a model on launch. |
| 52 | // Mid-session selections are persisted by the hub via the set-session-config RPC, |
| 53 | // not by this initial bootstrap. |
| 54 | const initialModel = opts.model ?? null; |
| 55 | const initialModelReasoningEffort = opts.modelReasoningEffort ?? null; |
| 56 | |
| 57 | const bootstrap = opts.existingSessionId |
| 58 | ? await bootstrapExistingSession({ |
| 59 | sessionId: opts.existingSessionId, |
| 60 | flavor: 'opencode', |
| 61 | startedBy, |
| 62 | workingDirectory |
| 63 | }) |
| 64 | : await bootstrapSession({ |
| 65 | flavor: 'opencode', |
| 66 | startedBy, |
| 67 | workingDirectory, |
| 68 | agentState: initialState, |
| 69 | model: initialModel ?? undefined, |
| 70 | modelReasoningEffort: initialModelReasoningEffort ?? undefined |
| 71 | }); |
| 72 | const { api, session } = bootstrap; |
| 73 | |
| 74 | setControlledByUser(session, startingMode); |
| 75 | |
| 76 | const messageQueue = new MessageQueue2<OpencodeMode>((mode) => hashObject({ |
| 77 | permissionMode: mode.permissionMode, |
no test coverage detected