Something went wrong. Try again.
source dump of claude code forked from oppi.li/claude-code
Something went wrong. Try again.
1.6 kB · 53 lines
TypeScript
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354import { logForDebugging } from './debug.js'import { gracefulShutdownSync } from './gracefulShutdown.js'
/** * Creates an idle timeout manager for SDK mode. * Automatically exits the process after the specified idle duration. * * @param isIdle Function that returns true if the system is currently idle * @returns Object with start/stop methods to control the idle timer */export function createIdleTimeoutManager(isIdle: () => boolean): { start: () => void stop: () => void} { // Parse CLAUDE_CODE_EXIT_AFTER_STOP_DELAY environment variable const exitAfterStopDelay = process.env.CLAUDE_CODE_EXIT_AFTER_STOP_DELAY const delayMs = exitAfterStopDelay ? parseInt(exitAfterStopDelay, 10) : null const isValidDelay = delayMs && !isNaN(delayMs) && delayMs > 0
let timer: NodeJS.Timeout | null = null let lastIdleTime = 0
return { start() { // Clear any existing timer if (timer) { clearTimeout(timer) timer = null }
// Only start timer if delay is configured and valid if (isValidDelay) { lastIdleTime = Date.now()
timer = setTimeout(() => { // Check if we've been continuously idle for the full duration const idleDuration = Date.now() - lastIdleTime if (isIdle() && idleDuration >= delayMs) { logForDebugging(`Exiting after ${delayMs}ms of idle time`) gracefulShutdownSync() } }, delayMs) } },
stop() { if (timer) { clearTimeout(timer) timer = null } }, }}