mirror of
https://github.com/wu736139669/hapi.git
synced 2026-10-09 19:29:41 +00:00
Consolidates duplicate cleanup, signal handling, and state management logic from runClaude and runCodex into a reusable createRunnerLifecycle factory function. Extracts mode switching handler and controlled user state updates into utilities. Adds runLocalRemoteSession wrapper to handle session ready callbacks in loop base.
62 lines
1.7 KiB
TypeScript
62 lines
1.7 KiB
TypeScript
import { logger } from '@/ui/logger';
|
|
import type { AgentSessionBase } from './sessionBase';
|
|
|
|
export type LoopLauncher<TSession> = (session: TSession) => Promise<'switch' | 'exit'>;
|
|
|
|
export async function runLocalRemoteSession<TSession extends AgentSessionBase<any>>(opts: {
|
|
session: TSession;
|
|
startingMode?: 'local' | 'remote';
|
|
logTag: string;
|
|
runLocal: LoopLauncher<TSession>;
|
|
runRemote: LoopLauncher<TSession>;
|
|
onSessionReady?: (session: TSession) => void;
|
|
}): Promise<void> {
|
|
if (opts.onSessionReady) {
|
|
opts.onSessionReady(opts.session);
|
|
}
|
|
|
|
await runLocalRemoteLoop({
|
|
session: opts.session,
|
|
startingMode: opts.startingMode,
|
|
logTag: opts.logTag,
|
|
runLocal: opts.runLocal,
|
|
runRemote: opts.runRemote
|
|
});
|
|
}
|
|
|
|
export async function runLocalRemoteLoop<TSession extends AgentSessionBase<any>>(opts: {
|
|
session: TSession;
|
|
startingMode?: 'local' | 'remote';
|
|
logTag: string;
|
|
runLocal: LoopLauncher<TSession>;
|
|
runRemote: LoopLauncher<TSession>;
|
|
}): Promise<void> {
|
|
let mode: 'local' | 'remote' = 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;
|
|
}
|
|
|
|
mode = 'remote';
|
|
opts.session.onModeChange(mode);
|
|
continue;
|
|
}
|
|
|
|
if (mode === 'remote') {
|
|
const reason = await opts.runRemote(opts.session);
|
|
if (reason === 'exit') {
|
|
return;
|
|
}
|
|
|
|
mode = 'local';
|
|
opts.session.onModeChange(mode);
|
|
continue;
|
|
}
|
|
}
|
|
}
|