@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, 04:45 UTC

next
ts 73 lines 2.75 KB
Raw
1 import { Deferred, Effect, Exit } from 'effect';
2
3 export interface MemoizedInProcess<I, A, E, R> {
4 (input: I): Effect.Effect<A, E, R>;
5 /** Forgets every cached result of this memo. */
6 readonly clear: () => void;
7 }
8
9 const memos = new Set<{ readonly clear: () => void }>();
10
11 /**
12 * Forgets every result memoized by {@link memoizeInProcess} anywhere in the
13 * process. A vitest `beforeEach` calls this so a value cached by one test case
14 * (a tool version, a connected-account list) cannot leak into the next one;
15 * production code has no reason to call it.
16 */
17 export const clearInProcessMemos = (): void => {
18 for (const memo of memos) memo.clear();
19 };
20
21 /**
22 * Memoizes an Effect per key for the lifetime of the process, sharing one run
23 * between concurrent callers. A success stays cached; a failure or defect is
24 * delivered to the callers already waiting and then dropped so the next caller
25 * retries instead of replaying it.
26 *
27 * The run is owned by a detached fiber rather than by whichever caller arrived
28 * first. Interrupting a caller therefore cancels only that caller's wait: the
29 * run completes and every other waiter still receives its result. With
30 * `Effect.cached` the first caller owns the run, so interrupting it would store
31 * the interrupt and replay it to every fiber already parked on the cell.
32 *
33 * Meant for reads that cannot change within one CLI invocation (a tool's
34 * latest version, the user's connected accounts) but were being fetched more
35 * than once by independent code paths on the same command.
36 */
37 export const memoizeInProcess = <I, A, E, R>(options: {
38 readonly keyOf: (input: I) => string;
39 readonly make: (input: I) => Effect.Effect<A, E, R>;
40 }): MemoizedInProcess<I, A, E, R> => {
41 const cache = new Map<string, Deferred.Deferred<A, E>>();
42
43 const load = (input: I): Effect.Effect<A, E, R> =>
44 // The lookup, the insert, and the fork run without an interruption point
45 // between them, so a cell is never left in the map without an owner.
46 Effect.uninterruptible(
47 Effect.suspend(() => {
48 const key = options.keyOf(input);
49 const existing = cache.get(key);
50 if (existing) {
51 return Effect.succeed(existing);
52 }
53
54 const cell = Deferred.makeUnsafe<A, E>();
55 cache.set(key, cell);
56 return options.make(input).pipe(
57 Effect.exit,
58 Effect.flatMap(exit => {
59 if (!Exit.isSuccess(exit) && cache.get(key) === cell) {
60 cache.delete(key);
61 }
62 return Deferred.done(cell, exit);
63 }),
64 Effect.forkDetach,
65 Effect.as(cell)
66 );
67 })
68 ).pipe(Effect.flatMap(cell => Deferred.await(cell)));
69
70 const memo = Object.assign(load, { clear: () => cache.clear() });
71 memos.add(memo);
72 return memo;
73 };