The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | // Alchemy modifications are licensed under Apache-2.0. |
| 2 | // This file includes third-party code; see /THIRD_PARTY_LICENSES.md. |
| 3 | import { ms } from "itty-time"; |
| 4 | import type { ResolvedStepConfig, StepState } from "../context.ts"; |
| 5 | import type { WorkflowSleepDuration } from "cloudflare:workers"; |
| 6 | |
| 7 | // oxlint-disable-next-line effecttsgo/extends-native-error -- vendored workers-sdk Workflows code; the error `name` is part of the protocol surfaced to user code across RPC |
| 8 | export class DelayFunctionError extends Error { |
| 9 | constructor(reason: string) { |
| 10 | super(reason); |
| 11 | this.name = "DelayFunctionError"; |
| 12 | } |
| 13 | } |
| 14 | |
| 15 | export function calcRetryDuration( |
| 16 | config: ResolvedStepConfig, |
| 17 | stepState: StepState, |
| 18 | delayValue: unknown |
| 19 | ): number { |
| 20 | const { attemptedCount: attemptCount } = stepState; |
| 21 | const { retries } = config; |
| 22 | |
| 23 | let base: number; |
| 24 | try { |
| 25 | base = ms(delayValue as WorkflowSleepDuration); |
| 26 | } catch { |
| 27 | throw new DelayFunctionError( |
| 28 | 'returned an invalid delay value (expected a number of ms or a duration string like "30 seconds")' |
| 29 | ); |
| 30 | } |
| 31 | if (!Number.isFinite(base) || base < 0) { |
| 32 | throw new DelayFunctionError( |
| 33 | 'returned an invalid delay value (expected a number of ms or a duration string like "30 seconds")' |
| 34 | ); |
| 35 | } |
| 36 | |
| 37 | switch (retries.backoff) { |
| 38 | case "exponential": { |
| 39 | return base * Math.pow(2, attemptCount - 1); |
| 40 | } |
| 41 | case "linear": { |
| 42 | return base * attemptCount; |
| 43 | } |
| 44 | case "constant": |
| 45 | default: { |
| 46 | return base; |
| 47 | } |
| 48 | } |
| 49 | } |