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 | /** |
| 2 | * Effect.ts integration for `@composio/cli-keyring`. |
| 3 | * |
| 4 | * The core package is intentionally Promise-based so Node and Bun |
| 5 | * consumers can use it without pulling in Effect. This entry point |
| 6 | * wraps `Entry` in an Effect service so the Composio CLI (which is |
| 7 | * built on Effect) can access the keyring through its normal layer |
| 8 | * composition: |
| 9 | * |
| 10 | * ```ts |
| 11 | * import { Effect, Layer } from 'effect'; |
| 12 | * import { KeyringService, KeyringLive } from '@composio/cli-keyring/effect'; |
| 13 | * |
| 14 | * const program = Effect.gen(function* () { |
| 15 | * const keyring = yield* KeyringService; |
| 16 | * yield* keyring.setPassword('com.composio.cli', 'default', apiKey); |
| 17 | * }); |
| 18 | * |
| 19 | * program.pipe(Effect.provide(KeyringLive)); |
| 20 | * ``` |
| 21 | * |
| 22 | * This module has a peer dependency on `effect`. Consumers that don't |
| 23 | * use Effect should import from `@composio/cli-keyring` directly and |
| 24 | * never touch this file. |
| 25 | */ |
| 26 | |
| 27 | import { Context, Effect, Layer } from 'effect'; |
| 28 | import { Entry } from './core/entry'; |
| 29 | import { KeyringError } from './core/errors'; |
| 30 | import { createDefaultStore, type MacOSBackend } from './index'; |
| 31 | import { type CredentialStore, type EntryModifiers, setDefaultStore } from './core/store'; |
| 32 | |
| 33 | /** |
| 34 | * Service shape exposed to Effect consumers. Every method returns |
| 35 | * `Effect.Effect<T, KeyringError>` — callers branch on |
| 36 | * `Effect.catchTag`-style error handling by matching on |
| 37 | * `error.details.kind`. |
| 38 | */ |
| 39 | export interface KeyringServiceShape { |
| 40 | /** Store a UTF-8 password under (service, user). */ |
| 41 | readonly setPassword: ( |
| 42 | service: string, |
| 43 | user: string, |
| 44 | password: string, |
| 45 | modifiers?: EntryModifiers |
| 46 | ) => Effect.Effect<void, KeyringError>; |
| 47 | |
| 48 | /** Fetch a UTF-8 password; fails with `NoEntry` if missing. */ |
| 49 | readonly getPassword: ( |
| 50 | service: string, |
| 51 | user: string, |
| 52 | modifiers?: EntryModifiers |
| 53 | ) => Effect.Effect<string, KeyringError>; |
| 54 | |
| 55 | /** Store raw bytes under (service, user). */ |
| 56 | readonly setSecret: ( |
| 57 | service: string, |
| 58 | user: string, |
| 59 | secret: Uint8Array, |
| 60 | modifiers?: EntryModifiers |
| 61 | ) => Effect.Effect<void, KeyringError>; |
| 62 | |
| 63 | /** Fetch raw bytes; fails with `NoEntry` if missing. */ |
| 64 | readonly getSecret: ( |
| 65 | service: string, |
| 66 | user: string, |
| 67 | modifiers?: EntryModifiers |
| 68 | ) => Effect.Effect<Uint8Array, KeyringError>; |
| 69 | |
| 70 | /** Delete the credential; fails with `NoEntry` if missing. */ |
| 71 | readonly deleteCredential: ( |
| 72 | service: string, |
| 73 | user: string, |
| 74 | modifiers?: EntryModifiers |
| 75 | ) => Effect.Effect<void, KeyringError>; |
| 76 | |
| 77 | /** |
| 78 | * Probe whether the backing store is currently usable. Catches |
| 79 | * `NoStorageAccess` to return `false` — useful for deciding whether |
| 80 | * to offer keyring-backed login or fall back to on-disk config. |
| 81 | */ |
| 82 | readonly isAvailable: Effect.Effect<boolean>; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Context tag. Resolve with `yield* KeyringService` inside an |
| 87 | * `Effect.gen` block. |
| 88 | */ |
| 89 | export class KeyringService extends Context.Service<KeyringService, KeyringServiceShape>()( |
| 90 | 'composio/cli-keyring/KeyringService' |
| 91 | ) {} |
| 92 | |
| 93 | /** |
| 94 | * Build a `KeyringServiceShape` from a concrete `CredentialStore`. |
| 95 | * Factored out so tests can swap in an in-memory mock without going |
| 96 | * through `setDefaultStore`. |
| 97 | */ |
| 98 | export function makeKeyringService(store: CredentialStore): KeyringServiceShape { |
| 99 | const entryFor = (service: string, user: string, modifiers?: EntryModifiers): Entry => |
| 100 | new Entry(service, user, modifiers ?? {}, store); |
| 101 | |
| 102 | const unsafe = <T>(op: () => Promise<T>): Effect.Effect<T, KeyringError> => |
| 103 | Effect.tryPromise({ |
| 104 | try: op, |
| 105 | // Store operations already throw KeyringError, so we preserve |
| 106 | // the error verbatim. The `as KeyringError` cast is safe |
| 107 | // because every path into this helper comes from an `Entry` |
| 108 | // method or the underlying store, both of which only throw |
| 109 | // KeyringError. |
| 110 | catch: err => err as KeyringError, |
| 111 | }); |
| 112 | |
| 113 | return { |
| 114 | setPassword: (service, user, password, modifiers) => |
| 115 | unsafe(() => entryFor(service, user, modifiers).setPassword(password)), |
| 116 | getPassword: (service, user, modifiers) => |
| 117 | unsafe(() => entryFor(service, user, modifiers).getPassword()), |
| 118 | setSecret: (service, user, secret, modifiers) => |
| 119 | unsafe(() => entryFor(service, user, modifiers).setSecret(secret)), |
| 120 | getSecret: (service, user, modifiers) => |
| 121 | unsafe(() => entryFor(service, user, modifiers).getSecret()), |
| 122 | deleteCredential: (service, user, modifiers) => |
| 123 | unsafe(() => entryFor(service, user, modifiers).deleteCredential()), |
| 124 | isAvailable: Effect.gen(function* () { |
| 125 | // Probe with a non-existent specifier — we expect NoEntry if |
| 126 | // the store is healthy, NoStorageAccess if it isn't. |
| 127 | const probeService = '__composio_cli_keyring_probe__'; |
| 128 | const probeUser = '__probe__'; |
| 129 | const probe = unsafe(() => new Entry(probeService, probeUser, {}, store).getSecret()); |
| 130 | return yield* probe.pipe( |
| 131 | Effect.match({ |
| 132 | onSuccess: () => true, |
| 133 | onFailure: err => err.details.kind === 'NoEntry', |
| 134 | }) |
| 135 | ); |
| 136 | }), |
| 137 | }; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Live layer that instantiates the platform store on effect-build and |
| 142 | * registers it as the process-global default (so non-Effect callers |
| 143 | * in the same process also see it). Provide this once at startup: |
| 144 | * |
| 145 | * ```ts |
| 146 | * BunRuntime.runMain(program.pipe(Effect.provide(KeyringLive))); |
| 147 | * ``` |
| 148 | * |
| 149 | * The layer uses `Layer.effect` rather than `Layer.sync` because the |
| 150 | * macOS FFI backend is dynamically imported — it must stay out of the |
| 151 | * Node bundle so `bun:ffi` doesn't crash at module load. The extra |
| 152 | * layer-build cost is a one-shot `import()` per process. |
| 153 | * |
| 154 | * Construction is fallible — the backend choice can fail to load its |
| 155 | * binary or spawn its subprocess — so the layer's error channel carries |
| 156 | * `KeyringError` instead of dying as a defect. (`KeyringLive` is |
| 157 | * `KeyringLiveWithBackend('auto')`.) |
| 158 | */ |
| 159 | export const KeyringLive: Layer.Layer<KeyringService, KeyringError> = Layer.effect( |
| 160 | KeyringService, |
| 161 | buildKeyringService() |
| 162 | ); |
| 163 | |
| 164 | /** |
| 165 | * Build a `KeyringService` from a freshly constructed default store. |
| 166 | * |
| 167 | * Store construction is the one genuinely fallible promise in this |
| 168 | * wrapper — `createDefaultStore` loads the macOS FFI backend or spawns |
| 169 | * subprocesses — so it goes through `Effect.tryPromise` and surfaces a |
| 170 | * typed `KeyringError` on the layer's error channel rather than an |
| 171 | * `Effect.promise` defect. Non-`KeyringError` causes (e.g. a failed |
| 172 | * dynamic `import()`) are normalized into a `PlatformFailure`. |
| 173 | */ |
| 174 | function buildKeyringService(options?: { macOSBackend?: MacOSBackend }) { |
| 175 | return Effect.tryPromise({ |
| 176 | try: async () => { |
| 177 | const store = await createDefaultStore(options); |
| 178 | setDefaultStore(store); |
| 179 | return makeKeyringService(store); |
| 180 | }, |
| 181 | catch: cause => |
| 182 | cause instanceof KeyringError ? cause : new KeyringError({ kind: 'PlatformFailure', cause }), |
| 183 | }); |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Build a `KeyringLive` layer with an explicit macOS backend choice. |
| 188 | * Use this instead of `KeyringLive` when the CLI lets the user pick |
| 189 | * between `subprocess` (safe default) and `ffi` (experimental, |
| 190 | * requires Developer ID signing). The `backend` parameter is |
| 191 | * ignored on Linux and other platforms. |
| 192 | * |
| 193 | * Fails with `KeyringError` if the store cannot be constructed — see |
| 194 | * `buildKeyringService`. |
| 195 | */ |
| 196 | export const KeyringLiveWithBackend = ( |
| 197 | macOSBackend: MacOSBackend |
| 198 | ): Layer.Layer<KeyringService, KeyringError> => |
| 199 | Layer.effect(KeyringService, buildKeyringService({ macOSBackend })); |
| 200 | |
| 201 | /** Layer built from an explicit store — used by tests. */ |
| 202 | export const KeyringLayer = (store: CredentialStore): Layer.Layer<KeyringService> => |
| 203 | Layer.succeed(KeyringService, makeKeyringService(store)); |