Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | import { DateTime, Duration, Effect, Option, Ref, Semaphore, Context, Layer } from 'effect'; |
| 2 | import { BAKED_TOOLKIT_SLUGS } from 'src/generated/toolkit-slugs'; |
| 3 | import { ComposioToolkitsRepository } from 'src/services/composio-clients'; |
| 4 | import { |
| 5 | readKnownToolkitSlugs, |
| 6 | writeKnownToolkitSlugs, |
| 7 | type KnownToolkitSlugs, |
| 8 | } from 'src/services/known-toolkit-slugs'; |
| 9 | import { makeLongestPrefixMatcher } from 'src/utils/toolkit-from-tool-slug'; |
| 10 | |
| 11 | /** |
| 12 | * How long learned slugs are trusted before a background refresh is started. |
| 13 | * Only relevant for toolkits released after the last refresh: a slug already |
| 14 | * known is never wrong, so this bounds how long a *new* toolkit can be |
| 15 | * mistaken for a shorter one that shares its prefix (a hypothetical |
| 16 | * `google_analytics_v2` resolving as `google_analytics`). |
| 17 | */ |
| 18 | const REFRESH_AFTER = Duration.days(7); |
| 19 | |
| 20 | /** |
| 21 | * A successful run ends by event-loop drain rather than an explicit exit, so a |
| 22 | * daemon fiber mid-fetch holds the finished command open until the network |
| 23 | * answers. This bounds that lingering; an interrupted refresh costs a fetch on |
| 24 | * a later run, nothing more. |
| 25 | */ |
| 26 | const REFRESH_TIMEOUT = Duration.seconds(10); |
| 27 | |
| 28 | const learnedSlugs = (learned: Option.Option<KnownToolkitSlugs>): ReadonlyArray<string> => |
| 29 | Option.match(learned, { |
| 30 | onNone: () => [], |
| 31 | onSome: known => known.slugs, |
| 32 | }); |
| 33 | |
| 34 | /** |
| 35 | * Re-reads the catalog and records it, in the background. Failures are |
| 36 | * swallowed: a refresh that does not happen costs a fetch later, nothing more. |
| 37 | * The project's custom toolkits are optional to it — without them, a custom |
| 38 | * toolkit still uses its learned slug if the scoped fetch also fails. Refresh |
| 39 | * and foreground discoveries merge through the same serialized writer. |
| 40 | */ |
| 41 | const refreshKnownToolkitSlugs = ( |
| 42 | remember: (slugs: ReadonlyArray<string>) => Effect.Effect<void> |
| 43 | ) => |
| 44 | Effect.gen(function* () { |
| 45 | const repository = yield* ComposioToolkitsRepository; |
| 46 | const [toolkits, projectToolkits] = yield* Effect.all( |
| 47 | [ |
| 48 | repository.getToolkits(), |
| 49 | repository.getProjectToolkits().pipe(Effect.orElseSucceed(() => [])), |
| 50 | ], |
| 51 | { concurrency: 'unbounded' } |
| 52 | ); |
| 53 | yield* remember([ |
| 54 | ...BAKED_TOOLKIT_SLUGS, |
| 55 | ...[...toolkits, ...projectToolkits].map(t => t.slug), |
| 56 | ]); |
| 57 | }).pipe(Effect.timeout(REFRESH_TIMEOUT), Effect.ignore); |
| 58 | |
| 59 | /** |
| 60 | * Starts a refresh when the learned slugs are missing or older than |
| 61 | * {@link REFRESH_AFTER}. A `refreshedAt` in the future (a clock that jumped) |
| 62 | * counts as fresh — the point is to bound staleness, not to police clocks. |
| 63 | */ |
| 64 | const refreshInBackgroundIfStale = ( |
| 65 | learned: Option.Option<KnownToolkitSlugs>, |
| 66 | remember: (slugs: ReadonlyArray<string>) => Effect.Effect<void> |
| 67 | ) => |
| 68 | Effect.gen(function* () { |
| 69 | const now = yield* DateTime.now; |
| 70 | const isFresh = Option.exists(learned, known => |
| 71 | Duration.isLessThan(DateTime.distance(known.refreshedAt, now), REFRESH_AFTER) |
| 72 | ); |
| 73 | |
| 74 | if (isFresh) return; |
| 75 | |
| 76 | yield* Effect.forkDetach(refreshKnownToolkitSlugs(remember)); |
| 77 | }); |
| 78 | |
| 79 | /** |
| 80 | * The toolkit slugs a run can match against without asking the API: the |
| 81 | * catalog baked in at build time plus whatever this machine has learned since. |
| 82 | */ |
| 83 | export interface LocalToolkitSlugs { |
| 84 | readonly slugs: ReadonlyArray<string>; |
| 85 | /** |
| 86 | * Longest-prefix match over {@link slugs}. Returns the matched toolkit slug, |
| 87 | * or undefined when nothing local matches — which is what a toolkit released |
| 88 | * after this binary looks like. |
| 89 | */ |
| 90 | readonly longestPrefix: (toolSlug: string) => string | undefined; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Owns the local half of toolkit resolution, resolved once per process. |
| 95 | * |
| 96 | * Reading it costs a file read, a JSON parse, a schema decode, and a lookup set |
| 97 | * over a thousand-odd slugs — cheap next to the catalog fetch it replaces, but |
| 98 | * a single `composio execute` resolves a toolkit four times over (bootstrap |
| 99 | * telemetry, the execute context, the connection check, the execution itself), |
| 100 | * and `--parallel` multiplies that by the number of tools. None of it can |
| 101 | * change under a running process, so `Effect.cached` memoizes it for the |
| 102 | * lifetime of the layer: the first caller pays, the rest await the same result. |
| 103 | * |
| 104 | * Memoizing also gates the staleness refresh to one fork per run, where an |
| 105 | * unmemoized read forked — and rewrote the file — once per resolution. |
| 106 | */ |
| 107 | const makeToolkitSlugCatalog = Effect.gen(function* () { |
| 108 | const recorded = yield* Ref.make<ReadonlySet<string>>(new Set()); |
| 109 | const writes = yield* Semaphore.make(1); |
| 110 | const refreshedAt = yield* Ref.make(DateTime.makeUnsafe(0)); |
| 111 | |
| 112 | /** |
| 113 | * Records slugs learned from a catalog fetch, in the background, only when |
| 114 | * they add something. Most misses in a run merge the same memoized fetch, |
| 115 | * and rewriting an identical file would only hold the finished command open |
| 116 | * longer; but a later miss can see a catalog an earlier one did not (the |
| 117 | * command's own project, after an unscoped startup lookup), and those slugs |
| 118 | * must not be dropped. Writes are serialized and each writes everything |
| 119 | * recorded so far, so a slow earlier write never lands over a later one. |
| 120 | */ |
| 121 | const remember = (slugs: ReadonlyArray<string>, refresh = false): Effect.Effect<void> => |
| 122 | Effect.gen(function* () { |
| 123 | const learnedSomething = yield* Ref.modify(recorded, current => { |
| 124 | const next = new Set([...current, ...slugs.map(slug => slug.toLowerCase())]); |
| 125 | return [next.size > current.size, next] as const; |
| 126 | }); |
| 127 | if (!learnedSomething && !refresh) return; |
| 128 | // Partial foreground discoveries do not prove the native catalog is |
| 129 | // current. Only a successful background refresh advances freshness. |
| 130 | if (refresh) yield* Ref.set(refreshedAt, yield* DateTime.now); |
| 131 | yield* Effect.forkDetach( |
| 132 | writes.withPermits(1)( |
| 133 | Effect.gen(function* () { |
| 134 | const slugs = yield* Ref.get(recorded); |
| 135 | const lastRefresh = yield* Ref.get(refreshedAt); |
| 136 | yield* writeKnownToolkitSlugs([...slugs], lastRefresh); |
| 137 | }) |
| 138 | ) |
| 139 | ); |
| 140 | }); |
| 141 | |
| 142 | const local = yield* Effect.cached( |
| 143 | Effect.gen(function* () { |
| 144 | const learned = yield* readKnownToolkitSlugs; |
| 145 | const slugs = [...BAKED_TOOLKIT_SLUGS, ...learnedSlugs(learned)]; |
| 146 | if (Option.isSome(learned)) yield* Ref.set(refreshedAt, learned.value.refreshedAt); |
| 147 | yield* Ref.update(recorded, current => new Set([...current, ...slugs])); |
| 148 | yield* refreshInBackgroundIfStale(learned, slugs => remember(slugs, true)); |
| 149 | return { slugs, longestPrefix: makeLongestPrefixMatcher(slugs) } satisfies LocalToolkitSlugs; |
| 150 | }) |
| 151 | ); |
| 152 | |
| 153 | return { local, remember }; |
| 154 | }); |
| 155 | |
| 156 | export type ToolkitSlugCatalogShape = Effect.Success<typeof makeToolkitSlugCatalog>; |
| 157 | |
| 158 | export class ToolkitSlugCatalog extends Context.Service< |
| 159 | ToolkitSlugCatalog, |
| 160 | ToolkitSlugCatalogShape |
| 161 | >()('services/ToolkitSlugCatalog') { |
| 162 | static readonly Default = Layer.effect(ToolkitSlugCatalog, makeToolkitSlugCatalog); |
| 163 | } |