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
| 1 | import * as BunPath from '@effect/platform-bun/BunPath'; |
| 2 | import * as FileSystem from 'effect/FileSystem'; |
| 3 | import * as Path from 'effect/Path'; |
| 4 | import * as BunFileSystem from '@effect/platform-bun/BunFileSystem'; |
| 5 | import { DateTime, Effect, Layer, Option, Schema } from 'effect'; |
| 6 | import { setupCacheDir } from 'src/effects/setup-cache-dir'; |
| 7 | import { writeFileAtomic } from 'src/effects/write-file-atomic'; |
| 8 | import { JSONTransformSchema } from 'src/models/utils/json-transform-schema'; |
| 9 | import { NodeOs } from './node-os'; |
| 10 | |
| 11 | /** |
| 12 | * Toolkit slugs this machine has learned since the CLI was built. |
| 13 | * |
| 14 | * The CLI ships with the catalog it knew at build time |
| 15 | * (`src/generated/toolkit-slugs.ts`). Toolkits released after that are |
| 16 | * discovered the first time someone runs one of their tools, and remembered |
| 17 | * here so the next run costs nothing. |
| 18 | * |
| 19 | * This is derived data, not an HTTP response cache: it is read unconditionally |
| 20 | * rather than under `FORCE_USE_CACHE`, which stays what it has always been — |
| 21 | * an opt-in to replaying previously cached API responses. Being wrong about |
| 22 | * this file can only cost a fetch, never an answer: an unknown slug falls |
| 23 | * through to the catalog, and the backend never removes a toolkit, so a slug |
| 24 | * recorded here never stops being real. |
| 25 | * |
| 26 | * Custom toolkits are the exception: they belong to a project, can be deleted, |
| 27 | * and this file is shared by every project on the machine. The resolver checks |
| 28 | * custom matches against the active project's catalog before accepting them. |
| 29 | * Learned custom slugs are only a best-effort fallback when that lookup fails. |
| 30 | */ |
| 31 | |
| 32 | export const KNOWN_TOOLKIT_SLUGS_FILE = 'known-toolkit-slugs.json'; |
| 33 | |
| 34 | const KnownToolkitSlugs = Schema.Struct({ |
| 35 | slugs: Schema.Array(Schema.String), |
| 36 | refreshedAt: Schema.DateTimeUtcFromString, |
| 37 | }); |
| 38 | |
| 39 | export type KnownToolkitSlugs = Schema.Schema.Type<typeof KnownToolkitSlugs>; |
| 40 | |
| 41 | const KnownToolkitSlugsJSON = JSONTransformSchema(KnownToolkitSlugs); |
| 42 | |
| 43 | /** |
| 44 | * The file lives in the cache directory but is not part of the toolkit cache, |
| 45 | * so it self-provides its platform services: callers stay free of filesystem |
| 46 | * dependencies in their own environments. |
| 47 | */ |
| 48 | const provideFileSystem = <A, E>( |
| 49 | effect: Effect.Effect<A, E, FileSystem.FileSystem | Path.Path | NodeOs> |
| 50 | ): Effect.Effect<A, E> => |
| 51 | effect.pipe(Effect.provide(Layer.mergeAll(BunFileSystem.layer, BunPath.layer, NodeOs.Default))); |
| 52 | |
| 53 | const knownToolkitSlugsPath = Effect.gen(function* () { |
| 54 | const path = yield* Path.Path; |
| 55 | const cacheDir = yield* setupCacheDir; |
| 56 | return path.join(cacheDir, KNOWN_TOOLKIT_SLUGS_FILE); |
| 57 | }); |
| 58 | |
| 59 | /** |
| 60 | * Reads the learned slugs. A missing, unreadable, or corrupted file is not a |
| 61 | * failure — it means "nothing learned yet", and the next refresh rewrites it. |
| 62 | */ |
| 63 | export const readKnownToolkitSlugs: Effect.Effect<Option.Option<KnownToolkitSlugs>> = Effect.gen( |
| 64 | function* () { |
| 65 | const fs = yield* FileSystem.FileSystem; |
| 66 | const filePath = yield* knownToolkitSlugsPath; |
| 67 | const content = yield* fs.readFileString(filePath); |
| 68 | return yield* Schema.decodeEffect(KnownToolkitSlugsJSON)(content); |
| 69 | } |
| 70 | ).pipe( |
| 71 | Effect.asSome, |
| 72 | Effect.catch(error => |
| 73 | Effect.logDebug(`No usable ${KNOWN_TOOLKIT_SLUGS_FILE}: ${error}`).pipe( |
| 74 | Effect.as(Option.none<KnownToolkitSlugs>()) |
| 75 | ) |
| 76 | ), |
| 77 | provideFileSystem |
| 78 | ); |
| 79 | |
| 80 | /** |
| 81 | * Records `slugs`, sorted and deduped, with the last full catalog refresh time. |
| 82 | * |
| 83 | * Never fails: this is an optimization, and a machine that cannot write to its |
| 84 | * own cache directory should still be able to run tools. Concurrent CLI |
| 85 | * processes are safe by construction — the write is atomic, and since slugs |
| 86 | * are only ever added, whichever writer lands last leaves a usable file. |
| 87 | */ |
| 88 | export const writeKnownToolkitSlugs = ( |
| 89 | slugs: ReadonlyArray<string>, |
| 90 | refreshedAt: DateTime.Utc |
| 91 | ): Effect.Effect<void> => |
| 92 | Effect.gen(function* () { |
| 93 | const filePath = yield* knownToolkitSlugsPath; |
| 94 | |
| 95 | const content = yield* Schema.encodeEffect(KnownToolkitSlugsJSON)({ |
| 96 | slugs: [...new Set(slugs.map(slug => slug.toLowerCase()))].sort(), |
| 97 | refreshedAt, |
| 98 | }); |
| 99 | |
| 100 | yield* writeFileAtomic(filePath, content); |
| 101 | yield* Effect.logDebug(`Recorded ${slugs.length} toolkit slugs in ${filePath}`); |
| 102 | }).pipe( |
| 103 | Effect.catch(error => Effect.logDebug(`Failed to record known toolkit slugs: ${error}`)), |
| 104 | provideFileSystem |
| 105 | ); |