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 * as BunPath from '@effect/platform-bun/BunPath'; |
| 2 | import { Cache, Effect, Option, Schema, Layer, Array as Arr } from 'effect'; |
| 3 | import * as FileSystem from 'effect/FileSystem'; |
| 4 | import * as Path from 'effect/Path'; |
| 5 | import * as BunFileSystem from '@effect/platform-bun/BunFileSystem'; |
| 6 | import { setupCacheDir } from 'src/effects/setup-cache-dir'; |
| 7 | import { DEBUG_CONFIG } from 'src/config'; |
| 8 | import { writeFileAtomic } from 'src/effects/write-file-atomic'; |
| 9 | import { ComposioToolkitsRepository, type ToolkitProjectScope } from './composio-clients'; |
| 10 | import { InvalidToolkitsError } from './toolkit-version-validation'; |
| 11 | import { NodeOs } from './node-os'; |
| 12 | import { toolkitsFromJSON, toolkitsToJSON, type Toolkits } from 'src/models/toolkits'; |
| 13 | import { |
| 14 | toolsAsEnumsFromJSON, |
| 15 | toolsAsEnumsToJSON, |
| 16 | ToolsFromJSON, |
| 17 | ToolsToJSON, |
| 18 | type Tools, |
| 19 | } from 'src/models/tools'; |
| 20 | import { |
| 21 | TriggerTypesAsEnumsFromJSON, |
| 22 | TriggerTypesAsEnumsToJSON, |
| 23 | TriggerTypesFromJSON, |
| 24 | TriggerTypesToJSON, |
| 25 | type TriggerTypes, |
| 26 | } from 'src/models/trigger-types'; |
| 27 | import { LogLevelFromConfigLive } from 'src/effects/with-log-level'; |
| 28 | |
| 29 | /** |
| 30 | * Cache file names for different data types |
| 31 | */ |
| 32 | export const CACHE_FILES = { |
| 33 | toolkits: 'toolkits.json', |
| 34 | tools: 'tools.json', |
| 35 | toolsAsEnums: 'tools-as-enums.json', |
| 36 | triggerTypesAsEnums: 'trigger-types-as-enums.json', |
| 37 | triggerTypes: 'trigger-types.json', |
| 38 | } as const; |
| 39 | |
| 40 | /** |
| 41 | * Cache filter that keeps only items whose slug starts with one of the |
| 42 | * requested toolkits' `<TOOLKIT>_` prefixes. |
| 43 | */ |
| 44 | const filterBySlugPrefixes = |
| 45 | (toolkitSlugs: ReadonlyArray<string>) => |
| 46 | <T extends { readonly slug: string }>(data: ReadonlyArray<T>) => { |
| 47 | const prefixes = toolkitSlugs.map(s => `${s.toUpperCase()}_`); |
| 48 | return Effect.succeed(data.filter(t => prefixes.some(p => t.slug.toUpperCase().startsWith(p)))); |
| 49 | }; |
| 50 | |
| 51 | /** |
| 52 | * Generic cache helper function that handles both cache read/write with graceful error handling. |
| 53 | */ |
| 54 | function createCachedEffect<T, E, R>( |
| 55 | cacheFileName: string, |
| 56 | decoder: (input: string) => Effect.Effect<T, Schema.SchemaError>, |
| 57 | encoder: (input: T) => Effect.Effect<string, Schema.SchemaError>, |
| 58 | computation: Effect.Effect<T, E, R>, |
| 59 | cacheFilter?: (data: T) => Effect.Effect<T, E, never> |
| 60 | ): Effect.Effect<T, E, R> { |
| 61 | /** |
| 62 | * The cached value, or none when there is nothing usable to serve: caching |
| 63 | * is off, the file is absent, it does not parse, or it does not cover the |
| 64 | * request. Every one of those is a cache miss, never a failure — the caller |
| 65 | * falls through to the computation. |
| 66 | */ |
| 67 | const readFromCache = (fs: FileSystem.FileSystem, cacheFilePath: string) => |
| 68 | Effect.gen(function* () { |
| 69 | const consumeFromCache = yield* DEBUG_CONFIG.FORCE_USE_CACHE; |
| 70 | if (!consumeFromCache) { |
| 71 | return Option.none<T>(); |
| 72 | } |
| 73 | |
| 74 | const cacheFileExists = yield* fs |
| 75 | .exists(cacheFilePath) |
| 76 | .pipe(Effect.catch(() => Effect.succeed(false))); |
| 77 | if (!cacheFileExists) { |
| 78 | return Option.none<T>(); |
| 79 | } |
| 80 | |
| 81 | yield* Effect.logDebug(`Cache HIT for ${cacheFileName}`); |
| 82 | |
| 83 | const cached = yield* fs.readFileString(cacheFilePath).pipe(Effect.flatMap(decoder)); |
| 84 | |
| 85 | // A filter that rejects the cached data means the cache does not answer |
| 86 | // this request — e.g. it predates a toolkit the caller asked for. |
| 87 | return Option.some(cacheFilter ? yield* cacheFilter(cached) : cached); |
| 88 | }).pipe( |
| 89 | Effect.catch(error => |
| 90 | Effect.logWarning(`Ignoring cache ${cacheFilePath}: ${error}`).pipe( |
| 91 | Effect.as(Option.none<T>()) |
| 92 | ) |
| 93 | ) |
| 94 | ); |
| 95 | |
| 96 | /** |
| 97 | * Atomic: these files are hundreds of KB, and a run killed mid-write — or |
| 98 | * two CLI processes writing at once — would otherwise leave behind a |
| 99 | * truncated file that the next run reads back as a parse failure. |
| 100 | */ |
| 101 | const writeToCache = (cacheFilePath: string, result: T) => |
| 102 | encoder(result).pipe( |
| 103 | Effect.flatMap(content => writeFileAtomic(cacheFilePath, content)), |
| 104 | Effect.catch(error => |
| 105 | Effect.logWarning(`Failed to write to cache ${cacheFilePath}: ${error}`) |
| 106 | ) |
| 107 | ); |
| 108 | |
| 109 | const cacheEffect = Effect.gen(function* () { |
| 110 | const fs = yield* FileSystem.FileSystem; |
| 111 | const path = yield* Path.Path; |
| 112 | |
| 113 | // A cache directory we cannot resolve or create means no caching, not a |
| 114 | // failed command. |
| 115 | const cacheFilePath = yield* setupCacheDir.pipe( |
| 116 | Effect.map(cacheDir => Option.some(path.join(cacheDir, cacheFileName))), |
| 117 | Effect.catch(error => |
| 118 | Effect.logWarning(`Cache unavailable for ${cacheFileName}: ${error}`).pipe( |
| 119 | Effect.as(Option.none<string>()) |
| 120 | ) |
| 121 | ) |
| 122 | ); |
| 123 | |
| 124 | if (Option.isSome(cacheFilePath)) { |
| 125 | const cached = yield* readFromCache(fs, cacheFilePath.value); |
| 126 | if (Option.isSome(cached)) { |
| 127 | return cached.value; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | yield* Effect.logDebug(`Cache MISS for ${cacheFileName}`); |
| 132 | |
| 133 | // Fetch from the underlying service. Its failure is the caller's failure: |
| 134 | // retrying here would double every failed request, and the retry would |
| 135 | // fail the same way. |
| 136 | const result = yield* computation; |
| 137 | |
| 138 | // Write to cache only if we're fetching the full dataset (no cacheFilter). |
| 139 | // Filtered API calls fetch partial data that would corrupt the shared cache file. |
| 140 | if (!cacheFilter && Option.isSome(cacheFilePath)) { |
| 141 | yield* writeToCache(cacheFilePath.value, result); |
| 142 | } |
| 143 | |
| 144 | return result; |
| 145 | }); |
| 146 | |
| 147 | // This ensures the returned effect has the same error type as the original computation |
| 148 | // by providing all the required cache services |
| 149 | return cacheEffect.pipe( |
| 150 | Effect.provide(Layer.mergeAll(BunFileSystem.layer, BunPath.layer, NodeOs.Default)) |
| 151 | ) as Effect.Effect<T, E, R>; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Cached implementation of ComposioToolkitsRepository using the wrapper layer pattern. |
| 156 | * |
| 157 | * Only full-catalog fetches are cached: `getToolkits`, `getToolkitsBySlugs`, |
| 158 | * `getToolsAsEnums`, `getTriggerTypesAsEnums`, `getTriggerTypes`, and |
| 159 | * `getTools`. Every other method passes through to the underlying repository, |
| 160 | * so adding a method there needs no edit here. In particular these stay |
| 161 | * uncached on purpose: |
| 162 | * - version-specific tool fetches, which would need per-version cache keys and |
| 163 | * must not pollute the `latest` cache; |
| 164 | * - version validation, because `available_versions` changes as versions ship; |
| 165 | * - searches and single-item details, which depend on the query or must be fresh; |
| 166 | * - auth config, connected account, and trigger instance CRUD. |
| 167 | */ |
| 168 | export const ComposioToolkitsRepositoryCached = Layer.effect( |
| 169 | ComposioToolkitsRepository, |
| 170 | Effect.gen(function* () { |
| 171 | const underlyingRepository = yield* ComposioToolkitsRepository; |
| 172 | |
| 173 | // The full toolkit list is ~800 KB over two pages, and a single command can |
| 174 | // ask for it several times (toolkit resolution, validation, telemetry). The |
| 175 | // file cache only helps when `FORCE_USE_CACHE` is on, so memoize the whole |
| 176 | // lookup for the lifetime of the layer: the first caller pays, the rest |
| 177 | // await the same result. |
| 178 | // |
| 179 | // `Effect.cached` memoizes the outcome, not just the success — a failed |
| 180 | // first fetch stays failed for the rest of the process. That is the right |
| 181 | // trade for a one-shot CLI, where retrying a broken network per call would |
| 182 | // only multiply the wait before the same error surfaces. |
| 183 | const cachedGetToolkits = yield* Effect.cached( |
| 184 | createCachedEffect( |
| 185 | CACHE_FILES.toolkits, |
| 186 | toolkitsFromJSON, |
| 187 | toolkitsToJSON, |
| 188 | underlyingRepository.getToolkits() |
| 189 | ) |
| 190 | ); |
| 191 | |
| 192 | // Project toolkits are never written to `toolkits.json`: that file holds |
| 193 | // the Composio-managed catalog, and its readers assume nothing else is in |
| 194 | // it. They are still memoized, because every toolkit resolution that |
| 195 | // misses locally asks for them. Under `FORCE_USE_CACHE` replay the fetch |
| 196 | // still reaches the API, and a failure only costs toolkit resolution its |
| 197 | // fallback guess. |
| 198 | // |
| 199 | // The list depends on the project it is asked for, so each scope is one |
| 200 | // cache entry (the unscoped call is one more key), with the same |
| 201 | // failure-is-final semantics as `cachedGetToolkits`: an entry never |
| 202 | // expires, whichever way the fetch ended. Concurrent first callers of a |
| 203 | // scope share its in-flight fetch. The key is a plain record, which |
| 204 | // `Equal` and `Hash` compare structurally. |
| 205 | const projectToolkitsByScope = yield* Cache.make({ |
| 206 | capacity: 64, |
| 207 | lookup: (scope: { readonly orgId?: string; readonly projectId?: string }) => |
| 208 | underlyingRepository.getProjectToolkits( |
| 209 | scope.orgId !== undefined && scope.projectId !== undefined |
| 210 | ? { orgId: scope.orgId, projectId: scope.projectId } |
| 211 | : undefined |
| 212 | ), |
| 213 | }); |
| 214 | const cachedGetProjectToolkits = (scope?: ToolkitProjectScope) => |
| 215 | Cache.get(projectToolkitsByScope, { |
| 216 | orgId: scope?.orgId, |
| 217 | projectId: scope?.projectId, |
| 218 | }); |
| 219 | |
| 220 | // Create the cached implementation that wraps the original implementation |
| 221 | return ComposioToolkitsRepository.of({ |
| 222 | ...underlyingRepository, |
| 223 | // Memoized per layer instance; `getToolkitsBySlugs` stays unmemoized |
| 224 | // because its result depends on the requested slugs. |
| 225 | getToolkits: () => cachedGetToolkits, |
| 226 | |
| 227 | getProjectToolkits: cachedGetProjectToolkits, |
| 228 | |
| 229 | getToolkitsBySlugs: slugs => { |
| 230 | const cacheFilter = (data: Toolkits) => { |
| 231 | const slugSet = new Set(slugs.map(s => s.toUpperCase())); |
| 232 | const filtered = data.filter(t => slugSet.has(t.slug.toUpperCase())); |
| 233 | |
| 234 | // Validate all requested slugs were found in the cache |
| 235 | const foundSlugs = new Set(filtered.map(t => t.slug.toUpperCase())); |
| 236 | const missingSlugs = slugs.filter(s => !foundSlugs.has(s.toUpperCase())); |
| 237 | |
| 238 | if (Arr.isReadonlyArrayNonEmpty(missingSlugs)) { |
| 239 | return Effect.fail( |
| 240 | new InvalidToolkitsError({ |
| 241 | invalidToolkits: missingSlugs, |
| 242 | availableToolkits: data.map(t => t.slug), |
| 243 | }) |
| 244 | ); |
| 245 | } |
| 246 | |
| 247 | return Effect.succeed(filtered); |
| 248 | }; |
| 249 | return createCachedEffect( |
| 250 | CACHE_FILES.toolkits, |
| 251 | toolkitsFromJSON, |
| 252 | toolkitsToJSON, |
| 253 | underlyingRepository.getToolkitsBySlugs(slugs), |
| 254 | cacheFilter |
| 255 | ); |
| 256 | }, |
| 257 | |
| 258 | getToolsAsEnums: () => { |
| 259 | return createCachedEffect( |
| 260 | CACHE_FILES.toolsAsEnums, |
| 261 | toolsAsEnumsFromJSON, |
| 262 | toolsAsEnumsToJSON, |
| 263 | underlyingRepository.getToolsAsEnums() |
| 264 | ); |
| 265 | }, |
| 266 | |
| 267 | getTriggerTypesAsEnums: () => { |
| 268 | return createCachedEffect( |
| 269 | CACHE_FILES.triggerTypesAsEnums, |
| 270 | TriggerTypesAsEnumsFromJSON, |
| 271 | TriggerTypesAsEnumsToJSON, |
| 272 | underlyingRepository.getTriggerTypesAsEnums() |
| 273 | ); |
| 274 | }, |
| 275 | |
| 276 | getTriggerTypes: (toolkitSlugs?: ReadonlyArray<string>) => { |
| 277 | const cacheFilter = |
| 278 | toolkitSlugs && toolkitSlugs.length > 0 |
| 279 | ? (data: TriggerTypes) => filterBySlugPrefixes(toolkitSlugs)(data) |
| 280 | : undefined; |
| 281 | return createCachedEffect( |
| 282 | CACHE_FILES.triggerTypes, |
| 283 | TriggerTypesFromJSON, |
| 284 | TriggerTypesToJSON, |
| 285 | underlyingRepository.getTriggerTypes(toolkitSlugs), |
| 286 | cacheFilter |
| 287 | ); |
| 288 | }, |
| 289 | |
| 290 | getTools: (toolkitSlugs?: ReadonlyArray<string>) => { |
| 291 | const cacheFilter = |
| 292 | toolkitSlugs && toolkitSlugs.length > 0 |
| 293 | ? (data: Tools) => filterBySlugPrefixes(toolkitSlugs)(data) |
| 294 | : undefined; |
| 295 | return createCachedEffect( |
| 296 | CACHE_FILES.tools, |
| 297 | ToolsFromJSON, |
| 298 | ToolsToJSON, |
| 299 | underlyingRepository.getTools(toolkitSlugs), |
| 300 | cacheFilter |
| 301 | ); |
| 302 | }, |
| 303 | }); |
| 304 | }) |
| 305 | ).pipe( |
| 306 | // Provide the required dependencies for the layer |
| 307 | Layer.provide(Layer.mergeAll(BunFileSystem.layer, NodeOs.Default, LogLevelFromConfigLive)) |
| 308 | ); |