mirror of
https://github.com/wu736139669/hapi.git
synced 2026-08-06 06:41:56 +00:00
refactor: extract session and loop logic into reusable agent base classes
This commit is contained in:
+8
-25
@@ -1,5 +1,6 @@
|
||||
import { MessageQueue2 } from '@/utils/MessageQueue2';
|
||||
import { logger } from '@/ui/logger';
|
||||
import { runLocalRemoteLoop } from '@/agent/loopBase';
|
||||
import { CodexSession } from './session';
|
||||
import { codexLocalLauncher } from './codexLocalLauncher';
|
||||
import { codexRemoteLauncher } from './codexRemoteLauncher';
|
||||
@@ -39,29 +40,11 @@ export async function loop(opts: LoopOptions): Promise<void> {
|
||||
opts.onSessionReady(session);
|
||||
}
|
||||
|
||||
let mode: 'local' | 'remote' = opts.startingMode ?? 'local';
|
||||
|
||||
while (true) {
|
||||
logger.debug(`[codex-loop] Iteration with mode: ${mode}`);
|
||||
|
||||
if (mode === 'local') {
|
||||
const reason = await codexLocalLauncher(session);
|
||||
if (reason === 'exit') {
|
||||
return;
|
||||
}
|
||||
mode = 'remote';
|
||||
session.onModeChange(mode);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (mode === 'remote') {
|
||||
const reason = await codexRemoteLauncher(session);
|
||||
if (reason === 'exit') {
|
||||
return;
|
||||
}
|
||||
mode = 'local';
|
||||
session.onModeChange(mode);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
await runLocalRemoteLoop({
|
||||
session,
|
||||
startingMode: opts.startingMode,
|
||||
logTag: 'codex-loop',
|
||||
runLocal: codexLocalLauncher,
|
||||
runRemote: codexRemoteLauncher
|
||||
});
|
||||
}
|
||||
|
||||
+18
-54
@@ -1,21 +1,9 @@
|
||||
import { ApiClient, ApiSessionClient } from '@/lib';
|
||||
import { MessageQueue2 } from '@/utils/MessageQueue2';
|
||||
import { logger } from '@/ui/logger';
|
||||
import { AgentSessionBase } from '@/agent/sessionBase';
|
||||
import type { EnhancedMode } from './loop';
|
||||
|
||||
export class CodexSession {
|
||||
readonly path: string;
|
||||
readonly logPath: string;
|
||||
readonly api: ApiClient;
|
||||
readonly client: ApiSessionClient;
|
||||
readonly queue: MessageQueue2<EnhancedMode>;
|
||||
readonly _onModeChange: (mode: 'local' | 'remote') => void;
|
||||
|
||||
sessionId: string | null;
|
||||
mode: 'local' | 'remote' = 'local';
|
||||
thinking: boolean = false;
|
||||
private keepAliveInterval: NodeJS.Timeout | null = null;
|
||||
|
||||
export class CodexSession extends AgentSessionBase<EnhancedMode> {
|
||||
constructor(opts: {
|
||||
api: ApiClient;
|
||||
client: ApiSessionClient;
|
||||
@@ -26,41 +14,24 @@ export class CodexSession {
|
||||
onModeChange: (mode: 'local' | 'remote') => void;
|
||||
mode?: 'local' | 'remote';
|
||||
}) {
|
||||
this.path = opts.path;
|
||||
this.api = opts.api;
|
||||
this.client = opts.client;
|
||||
this.logPath = opts.logPath;
|
||||
this.sessionId = opts.sessionId;
|
||||
this.queue = opts.messageQueue;
|
||||
this._onModeChange = opts.onModeChange;
|
||||
this.mode = opts.mode ?? 'local';
|
||||
|
||||
this.client.keepAlive(this.thinking, this.mode);
|
||||
this.keepAliveInterval = setInterval(() => {
|
||||
this.client.keepAlive(this.thinking, this.mode);
|
||||
}, 2000);
|
||||
super({
|
||||
api: opts.api,
|
||||
client: opts.client,
|
||||
path: opts.path,
|
||||
logPath: opts.logPath,
|
||||
sessionId: opts.sessionId,
|
||||
messageQueue: opts.messageQueue,
|
||||
onModeChange: opts.onModeChange,
|
||||
mode: opts.mode,
|
||||
sessionLabel: 'CodexSession',
|
||||
sessionIdLabel: 'Codex',
|
||||
applySessionIdToMetadata: (metadata, sessionId) => ({
|
||||
...metadata,
|
||||
codexSessionId: sessionId
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
onThinkingChange = (thinking: boolean) => {
|
||||
this.thinking = thinking;
|
||||
this.client.keepAlive(thinking, this.mode);
|
||||
};
|
||||
|
||||
onModeChange = (mode: 'local' | 'remote') => {
|
||||
this.mode = mode;
|
||||
this.client.keepAlive(this.thinking, mode);
|
||||
this._onModeChange(mode);
|
||||
};
|
||||
|
||||
onSessionFound = (sessionId: string) => {
|
||||
this.sessionId = sessionId;
|
||||
this.client.updateMetadata((metadata) => ({
|
||||
...metadata,
|
||||
codexSessionId: sessionId
|
||||
}));
|
||||
logger.debug(`[CodexSession] Codex session ID ${sessionId} added to metadata`);
|
||||
};
|
||||
|
||||
sendCodexMessage = (message: unknown): void => {
|
||||
this.client.sendCodexMessage(message);
|
||||
};
|
||||
@@ -68,11 +39,4 @@ export class CodexSession {
|
||||
sendSessionEvent = (event: Parameters<ApiSessionClient['sendSessionEvent']>[0]): void => {
|
||||
this.client.sendSessionEvent(event);
|
||||
};
|
||||
|
||||
stopKeepAlive = (): void => {
|
||||
if (this.keepAliveInterval) {
|
||||
clearInterval(this.keepAliveInterval);
|
||||
this.keepAliveInterval = null;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user