import { logger } from '@/ui/logger'; import type { AgentSessionBase } from './sessionBase'; export type LoopLauncher = (session: TSession) => Promise<'switch' | 'exit'>; export type SessionMode = 'local' | 'remote' | 'pty'; export async function runLocalRemoteSession>(opts: { session: TSession; startingMode?: SessionMode; logTag: string; runLocal: LoopLauncher; runRemote: LoopLauncher; runPty?: LoopLauncher; onSessionReady?: (session: TSession) => void; }): Promise { if (opts.onSessionReady) { opts.onSessionReady(opts.session); } await runLocalRemoteLoop({ session: opts.session, startingMode: opts.startingMode, logTag: opts.logTag, runLocal: opts.runLocal, runRemote: opts.runRemote, runPty: opts.runPty, }); } export async function runLocalRemoteLoop>(opts: { session: TSession; startingMode?: SessionMode; logTag: string; runLocal: LoopLauncher; runRemote: LoopLauncher; runPty?: LoopLauncher; }): Promise { let mode: SessionMode = opts.startingMode ?? 'local'; while (true) { logger.debug(`[${opts.logTag}] Iteration with mode: ${mode}`); if (mode === 'local') { const reason = await opts.runLocal(opts.session); if (reason === 'exit') { return; } // Leaving local mode returns to this session's remote variant. PTY // is OPT-IN: only a session that started in PTY mode hands off to the // PTY launcher. A normal local/remote session must still use the SDK // remote launcher merely because a PTY launcher is available. mode = opts.startingMode === 'pty' && opts.runPty ? 'pty' : 'remote'; opts.session.onModeChange(mode === 'pty' ? 'remote' : mode); continue; } if (mode === 'remote') { const reason = await opts.runRemote(opts.session); if (reason === 'exit') { return; } mode = 'local'; opts.session.onModeChange(mode); continue; } if (mode === 'pty') { if (!opts.runPty) { throw new Error('PTY mode selected but no runPty launcher provided'); } const reason = await opts.runPty(opts.session); if (reason === 'exit') { return; } mode = 'local'; opts.session.onModeChange(mode); continue; } } }