mirror of
https://github.com/wu736139669/hapi.git
synced 2026-10-31 23:31:39 +00:00
refactor: extract session and loop logic into reusable agent base classes
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
import { logger } from '@/ui/logger';
|
||||
import type { AgentSessionBase } from './sessionBase';
|
||||
|
||||
export type LoopLauncher<TSession> = (session: TSession) => Promise<'switch' | 'exit'>;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user