@devries-cameron20 / composio Mirror · read-only
Fork 0

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 21, 03:37 UTC

next
ts 49 lines 1.76 KB
Raw
1 import { Clock, Duration, Effect, Schedule } from 'effect';
2
3 /**
4 * How long the release waits for npm and PyPI to serve an upload and its provenance, and how
5 * often it looks. npm scans every new version before serving it, which takes about 5 to 15
6 * minutes, so the deadline leaves room for a slow scan.
7 */
8 export const REGISTRY_WAIT = {
9 deadline: '20 minutes',
10 firstDelay: '5 seconds',
11 maxDelay: '1 minute',
12 requestTimeout: '30 seconds',
13 } as const satisfies Record<string, Duration.Input>;
14
15 export const isSuccess = (status: number) => status >= 200 && status < 300;
16
17 /** Answers that waiting can fix: not served yet, rate limited, or a registry outage. */
18 export const isPending = (status: number) => status === 404 || status === 429 || status >= 500;
19
20 export type RegistryDeadline = number;
21
22 export const registryDeadline: Effect.Effect<RegistryDeadline> = Effect.map(
23 Clock.currentTimeMillis,
24 now => now + Duration.toMillis(REGISTRY_WAIT.deadline)
25 );
26
27 const backoff = Schedule.exponential(REGISTRY_WAIT.firstDelay).pipe(
28 Schedule.modifyDelay(({ duration }) =>
29 Effect.succeed(Duration.min(duration, Duration.fromInputUnsafe(REGISTRY_WAIT.maxDelay)))
30 ),
31 Schedule.jittered
32 );
33
34 /**
35 * Retries `effect` while its error is `pending`, backing off exponentially with jitter, until
36 * `deadline` (the registry wait deadline from now, when omitted). Other errors fail at once.
37 */
38 export const pollRegistry = <A, E, R>(
39 effect: Effect.Effect<A, E, R>,
40 pending: (error: E) => boolean,
41 deadline?: RegistryDeadline
42 ) =>
43 Effect.gen(function* () {
44 const until = deadline ?? (yield* registryDeadline);
45 return yield* Effect.retry(effect, {
46 schedule: backoff,
47 while: error => pending(error) && Effect.map(Clock.currentTimeMillis, now => now < until),
48 });
49 });