|
| 1 | +import { parseFiniteNumber } from "openclaw/plugin-sdk/number-runtime"; |
| 2 | + |
1 | 3 | export const CODEX_APP_SERVER_STARTUP_TIMEOUT_FLOOR_MS = 100; |
2 | 4 | export const CODEX_TURN_COMPLETION_IDLE_TIMEOUT_MS = 60_000; |
3 | 5 | export const CODEX_TURN_ASSISTANT_COMPLETION_IDLE_TIMEOUT_MS = 10_000; |
4 | 6 | export const CODEX_POST_REASONING_SOURCE_REPLY_IDLE_TIMEOUT_MS = 5 * 60_000; |
5 | 7 | export const CODEX_TURN_TERMINAL_IDLE_TIMEOUT_MS = 30 * 60_000; |
6 | 8 | |
| 9 | +function resolvePositiveIntegerTimeoutMs(value: number | undefined, fallbackMs: number): number { |
| 10 | +const fallback = parseFiniteNumber(fallbackMs) ?? 1; |
| 11 | +const candidate = parseFiniteNumber(value) ?? fallback; |
| 12 | +return Math.max(1, Math.floor(candidate)); |
| 13 | +} |
| 14 | + |
7 | 15 | export async function withCodexStartupTimeout<T>(params: { |
8 | 16 | timeoutMs: number; |
9 | 17 | signal: AbortSignal; |
@@ -61,53 +69,31 @@ export function resolveCodexStartupTimeoutMs(params: {
|
61 | 69 | timeoutMs: number; |
62 | 70 | timeoutFloorMs?: number; |
63 | 71 | }): number { |
64 | | -return Math.max( |
65 | | -params.timeoutFloorMs ?? CODEX_APP_SERVER_STARTUP_TIMEOUT_FLOOR_MS, |
66 | | -params.timeoutMs, |
| 72 | +const timeoutFloorMs = resolvePositiveIntegerTimeoutMs( |
| 73 | +params.timeoutFloorMs, |
| 74 | +CODEX_APP_SERVER_STARTUP_TIMEOUT_FLOOR_MS, |
67 | 75 | ); |
| 76 | +const timeoutMs = resolvePositiveIntegerTimeoutMs(params.timeoutMs, timeoutFloorMs); |
| 77 | +return Math.max(timeoutFloorMs, timeoutMs); |
68 | 78 | } |
69 | 79 | |
70 | 80 | export function resolveCodexTurnCompletionIdleTimeoutMs(value: number | undefined): number { |
71 | | -if (value === undefined) { |
72 | | -return CODEX_TURN_COMPLETION_IDLE_TIMEOUT_MS; |
73 | | -} |
74 | | -if (!Number.isFinite(value)) { |
75 | | -return CODEX_TURN_COMPLETION_IDLE_TIMEOUT_MS; |
76 | | -} |
77 | | -return Math.max(1, Math.floor(value)); |
| 81 | +return resolvePositiveIntegerTimeoutMs(value, CODEX_TURN_COMPLETION_IDLE_TIMEOUT_MS); |
78 | 82 | } |
79 | 83 | |
80 | 84 | export function resolveCodexTurnAssistantCompletionIdleTimeoutMs( |
81 | 85 | value: number | undefined, |
82 | 86 | ): number { |
83 | | -if (value === undefined) { |
84 | | -return CODEX_TURN_ASSISTANT_COMPLETION_IDLE_TIMEOUT_MS; |
85 | | -} |
86 | | -if (!Number.isFinite(value)) { |
87 | | -return CODEX_TURN_ASSISTANT_COMPLETION_IDLE_TIMEOUT_MS; |
88 | | -} |
89 | | -return Math.max(1, Math.floor(value)); |
| 87 | +return resolvePositiveIntegerTimeoutMs(value, CODEX_TURN_ASSISTANT_COMPLETION_IDLE_TIMEOUT_MS); |
90 | 88 | } |
91 | 89 | |
92 | 90 | export function resolveCodexPostToolRawAssistantCompletionIdleTimeoutMs( |
93 | 91 | value: number | undefined, |
94 | 92 | fallbackMs: number, |
95 | 93 | ): number { |
96 | | -if (value === undefined) { |
97 | | -return fallbackMs; |
98 | | -} |
99 | | -if (!Number.isFinite(value)) { |
100 | | -return fallbackMs; |
101 | | -} |
102 | | -return Math.max(1, Math.floor(value)); |
| 94 | +return resolvePositiveIntegerTimeoutMs(value, fallbackMs); |
103 | 95 | } |
104 | 96 | |
105 | 97 | export function resolveCodexTurnTerminalIdleTimeoutMs(value: number | undefined): number { |
106 | | -if (value === undefined) { |
107 | | -return CODEX_TURN_TERMINAL_IDLE_TIMEOUT_MS; |
108 | | -} |
109 | | -if (!Number.isFinite(value)) { |
110 | | -return CODEX_TURN_TERMINAL_IDLE_TIMEOUT_MS; |
111 | | -} |
112 | | -return Math.max(1, Math.floor(value)); |
| 98 | +return resolvePositiveIntegerTimeoutMs(value, CODEX_TURN_TERMINAL_IDLE_TIMEOUT_MS); |
113 | 99 | } |