You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
gpt-pilot/src/agents/timeout.ts

47 lines
1.5 KiB
TypeScript

import type { OpenClawConfig } from "../config/config.js";
const DEFAULT_AGENT_TIMEOUT_SECONDS = 600;
const normalizeNumber = (value: unknown): number | undefined =>
typeof value === "number" && Number.isFinite(value) ? Math.floor(value) : undefined;
export function resolveAgentTimeoutSeconds(cfg?: OpenClawConfig): number {
const raw = normalizeNumber(cfg?.agents?.defaults?.timeoutSeconds);
const seconds = raw ?? DEFAULT_AGENT_TIMEOUT_SECONDS;
return Math.max(seconds, 1);
}
export function resolveAgentTimeoutMs(opts: {
cfg?: OpenClawConfig;
overrideMs?: number | null;
overrideSeconds?: number | null;
minMs?: number;
}): number {
const minMs = Math.max(normalizeNumber(opts.minMs) ?? 1, 1);
const defaultMs = resolveAgentTimeoutSeconds(opts.cfg) * 1000;
// Use a very large timeout value (30 days) to represent "no timeout"
// when explicitly set to 0. This avoids setTimeout issues with Infinity.
const NO_TIMEOUT_MS = 30 * 24 * 60 * 60 * 1000;
const overrideMs = normalizeNumber(opts.overrideMs);
if (overrideMs !== undefined) {
if (overrideMs === 0) {
return NO_TIMEOUT_MS;
}
if (overrideMs < 0) {
return defaultMs;
}
return Math.max(overrideMs, minMs);
}
const overrideSeconds = normalizeNumber(opts.overrideSeconds);
if (overrideSeconds !== undefined) {
if (overrideSeconds === 0) {
return NO_TIMEOUT_MS;
}
if (overrideSeconds < 0) {
return defaultMs;
}
return Math.max(overrideSeconds * 1000, minMs);
}
return Math.max(defaultMs, minMs);
}