mirror of
https://github.com/wu736139669/hapi.git
synced 2026-08-05 06:24:37 +00:00
refactor: use ref objects for session tracking in closures
This commit is contained in:
@@ -130,9 +130,9 @@ export async function runClaude(options: StartOptions = {}): Promise<void> {
|
||||
logger.debug(`[START] HAPI MCP server started at ${happyServer.url}`);
|
||||
|
||||
// Variable to track current session instance (updated via onSessionReady callback)
|
||||
let currentSession: Session | null = null;
|
||||
const currentSessionRef: { current: Session | null } = { current: null };
|
||||
let exitCode = 0;
|
||||
let archiveReason: string | null = null;
|
||||
let archiveReason: string | undefined;
|
||||
|
||||
const formatFailureReason = (message: string): string => {
|
||||
const maxLength = 200;
|
||||
@@ -147,6 +147,7 @@ export async function runClaude(options: StartOptions = {}): Promise<void> {
|
||||
onSessionHook: (sessionId, data) => {
|
||||
logger.debug(`[START] Session hook received: ${sessionId}`, data);
|
||||
|
||||
const currentSession = currentSessionRef.current;
|
||||
if (currentSession) {
|
||||
const previousSessionId = currentSession.sessionId;
|
||||
if (previousSessionId !== sessionId) {
|
||||
@@ -394,7 +395,7 @@ export async function runClaude(options: StartOptions = {}): Promise<void> {
|
||||
}));
|
||||
},
|
||||
onSessionReady: (sessionInstance) => {
|
||||
currentSession = sessionInstance;
|
||||
currentSessionRef.current = sessionInstance;
|
||||
},
|
||||
mcpServers: {
|
||||
'hapi': {
|
||||
@@ -409,7 +410,7 @@ export async function runClaude(options: StartOptions = {}): Promise<void> {
|
||||
hookSettingsPath
|
||||
});
|
||||
|
||||
const localFailure = currentSession?.localLaunchFailure;
|
||||
const localFailure = currentSessionRef.current?.localLaunchFailure;
|
||||
if (localFailure?.exitReason === 'exit') {
|
||||
exitCode = 1;
|
||||
archiveReason = `Local launch failed: ${formatFailureReason(localFailure.message)}`;
|
||||
|
||||
@@ -131,7 +131,7 @@ export async function runCodex(opts: {
|
||||
messageQueue.push(message.content.text, enhancedMode);
|
||||
});
|
||||
|
||||
let sessionWrapper: CodexSession | null = null;
|
||||
const sessionWrapperRef: { current: CodexSession | null } = { current: null };
|
||||
|
||||
let cleanupStarted = false;
|
||||
let exitCode = 0;
|
||||
@@ -153,6 +153,7 @@ export async function runCodex(opts: {
|
||||
logger.debug('[codex] Cleanup start');
|
||||
restoreTerminalState();
|
||||
try {
|
||||
const sessionWrapper = sessionWrapperRef.current;
|
||||
if (sessionWrapper) {
|
||||
sessionWrapper.stopKeepAlive();
|
||||
}
|
||||
@@ -215,7 +216,7 @@ export async function runCodex(opts: {
|
||||
}));
|
||||
},
|
||||
onSessionReady: (instance) => {
|
||||
sessionWrapper = instance;
|
||||
sessionWrapperRef.current = instance;
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
@@ -224,7 +225,7 @@ export async function runCodex(opts: {
|
||||
archiveReason = 'Session crashed';
|
||||
logger.debug('[codex] Loop error:', error);
|
||||
} finally {
|
||||
const localFailure = sessionWrapper?.localLaunchFailure;
|
||||
const localFailure = sessionWrapperRef.current?.localLaunchFailure;
|
||||
if (localFailure?.exitReason === 'exit') {
|
||||
exitCode = 1;
|
||||
archiveReason = `Local launch failed: ${formatFailureReason(localFailure.message)}`;
|
||||
|
||||
Reference in New Issue
Block a user