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 FileSystem from 'effect/FileSystem'; |
| 2 | import * as Path from 'effect/Path'; |
| 3 | import { Effect, Context, Layer, Option, PlatformError, Predicate, Schema } from 'effect'; |
| 4 | import { |
| 5 | type UserDataWithDefaults, |
| 6 | UserData, |
| 7 | userDataFromJSON, |
| 8 | userDataToJSON, |
| 9 | } from 'src/models/user-data'; |
| 10 | import { JsonRecordSchema } from 'src/effects/json'; |
| 11 | import { setupCacheDir } from 'src/effects/setup-cache-dir'; |
| 12 | import * as constants from 'src/constants'; |
| 13 | import { APP_CONFIG } from 'src/config'; |
| 14 | import { |
| 15 | backendMismatch, |
| 16 | resolveBackend, |
| 17 | type BackendMismatch, |
| 18 | type StoredBackend, |
| 19 | } from 'src/config/backend'; |
| 20 | import { KeyringService, KeyringLiveWithBackend } from '@composio/cli-keyring/effect'; |
| 21 | import type { KeyringServiceShape } from '@composio/cli-keyring/effect'; |
| 22 | import { KeyringError, type MacOSBackend } from '@composio/cli-keyring'; |
| 23 | import { ComposioCliUserConfig, ComposioCliUserConfigLive } from 'src/services/cli-user-config'; |
| 24 | import { atomicWritePrivateFileString, ensurePrivateFileMode } from 'src/utils/atomic-write'; |
| 25 | import { redactSensitiveLogValue } from 'src/utils/redact-sensitive'; |
| 26 | |
| 27 | /** |
| 28 | * Keyring specifier for the Composio API key. `service` is a reverse |
| 29 | * DNS identifier shared by every composio CLI install; `user` is a |
| 30 | * single fixed slot because the API key is user-scoped (one per |
| 31 | * logged-in account, independent of which org/project the user is |
| 32 | * currently working in). |
| 33 | */ |
| 34 | const KEYRING_SERVICE = 'com.composio.cli'; |
| 35 | const KEYRING_USER = 'default'; |
| 36 | const decodeUserDataJsonObject = Schema.decodeUnknownEffect( |
| 37 | Schema.fromJsonString(JsonRecordSchema) |
| 38 | ); |
| 39 | |
| 40 | const normalizeEncodedUserData = (encoded: string, omitApiKey: boolean) => |
| 41 | Effect.gen(function* () { |
| 42 | const decoded = yield* decodeUserDataJsonObject(encoded); |
| 43 | const parsed = { ...decoded }; |
| 44 | if (parsed.project_id === null) delete parsed.project_id; |
| 45 | if (omitApiKey && parsed.api_key === null) delete parsed.api_key; |
| 46 | return JSON.stringify(parsed); |
| 47 | }); |
| 48 | |
| 49 | // ----------------------------------------------------------------------------- |
| 50 | // Keyring helpers — extracted from the generator so it stays within |
| 51 | // the max-lines-per-function lint limit. Each helper takes its deps |
| 52 | // as parameters rather than closing over the generator's locals. |
| 53 | // ----------------------------------------------------------------------------- |
| 54 | |
| 55 | type KeyringDeps = { |
| 56 | keyring: KeyringServiceShape; |
| 57 | useLegacyStorage: boolean; |
| 58 | }; |
| 59 | |
| 60 | /** |
| 61 | * Write `password` into the OS keyring. Returns `true` on success, |
| 62 | * `false` on `NoStorageAccess` or unexpected errors so the caller |
| 63 | * can fall back to plaintext. Never propagates — the CLI always |
| 64 | * prefers "keep working with plaintext fallback" over "crash". |
| 65 | */ |
| 66 | const writeKeyring = (deps: KeyringDeps, password: string) => |
| 67 | Effect.gen(function* () { |
| 68 | if (deps.useLegacyStorage) return false; |
| 69 | return yield* deps.keyring.setPassword(KEYRING_SERVICE, KEYRING_USER, password).pipe( |
| 70 | Effect.map(() => true), |
| 71 | Effect.catch(err => |
| 72 | Effect.gen(function* () { |
| 73 | if (err instanceof KeyringError && err.kind === 'NoStorageAccess') { |
| 74 | yield* Effect.logDebug( |
| 75 | 'OS keyring unavailable, storing api_key in user_data.json as plaintext. ' + |
| 76 | `Reason: ${err.message}` |
| 77 | ); |
| 78 | } else { |
| 79 | yield* Effect.logDebug( |
| 80 | 'Unexpected keyring error while writing api_key, falling back to plaintext: ' + |
| 81 | (Predicate.isError(err) ? err.message : String(err)) |
| 82 | ); |
| 83 | } |
| 84 | return false; |
| 85 | }) |
| 86 | ) |
| 87 | ); |
| 88 | }); |
| 89 | |
| 90 | /** |
| 91 | * Read the API key from the OS keyring. Returns `Option.some(value)` |
| 92 | * if found, `Option.none()` on NoEntry or any failure (with warning |
| 93 | * logged). The caller falls back to plaintext on `none`. |
| 94 | */ |
| 95 | const readKeyring = (deps: KeyringDeps) => |
| 96 | Effect.gen(function* () { |
| 97 | if (deps.useLegacyStorage) return Option.none<string>(); |
| 98 | return yield* deps.keyring.getPassword(KEYRING_SERVICE, KEYRING_USER).pipe( |
| 99 | Effect.map(Option.some), |
| 100 | Effect.catch(err => |
| 101 | Effect.gen(function* () { |
| 102 | if (err instanceof KeyringError && err.kind === 'NoEntry') { |
| 103 | yield* Effect.logDebug('No keyring entry found for Composio API key'); |
| 104 | } else if (err instanceof KeyringError && err.kind === 'NoStorageAccess') { |
| 105 | // Expected normal path on headless Linux / containers / |
| 106 | // CI — log at debug to avoid polluting stdout on every |
| 107 | // CLI invocation. Only the first write attempt would |
| 108 | // warn loudly (see `writeKeyring`). |
| 109 | yield* Effect.logDebug( |
| 110 | 'OS keyring unavailable, falling back to user_data.json. ' + `Reason: ${err.message}` |
| 111 | ); |
| 112 | } else { |
| 113 | yield* Effect.logDebug( |
| 114 | 'Unexpected keyring error while reading api_key, falling back to plaintext: ' + |
| 115 | (Predicate.isError(err) ? err.message : String(err)) |
| 116 | ); |
| 117 | } |
| 118 | return Option.none<string>(); |
| 119 | }) |
| 120 | ) |
| 121 | ); |
| 122 | }); |
| 123 | |
| 124 | /** |
| 125 | * Best-effort keyring delete. Swallows `NoEntry` (idempotent) and |
| 126 | * `NoStorageAccess` (logout should not fail because the keyring is |
| 127 | * unreachable). |
| 128 | */ |
| 129 | const deleteKeyring = (deps: KeyringDeps) => |
| 130 | Effect.gen(function* () { |
| 131 | if (deps.useLegacyStorage) return; |
| 132 | yield* deps.keyring.deleteCredential(KEYRING_SERVICE, KEYRING_USER).pipe( |
| 133 | Effect.catch(err => |
| 134 | Effect.gen(function* () { |
| 135 | if ( |
| 136 | err instanceof KeyringError && |
| 137 | (err.kind === 'NoEntry' || err.kind === 'NoStorageAccess') |
| 138 | ) { |
| 139 | yield* Effect.logDebug(`Keyring delete skipped: ${err.kind}`); |
| 140 | } else { |
| 141 | yield* Effect.logWarning( |
| 142 | 'Keyring delete failed: ' + (Predicate.isError(err) ? err.message : String(err)) |
| 143 | ); |
| 144 | } |
| 145 | }) |
| 146 | ) |
| 147 | ); |
| 148 | }); |
| 149 | |
| 150 | // ----------------------------------------------------------------------------- |
| 151 | // Service definition |
| 152 | // ----------------------------------------------------------------------------- |
| 153 | |
| 154 | export class ComposioUserContext extends Context.Service< |
| 155 | ComposioUserContext, |
| 156 | { |
| 157 | /** The stored user data, with `baseURL` and `webURL` resolved by `resolveBackend`. */ |
| 158 | readonly data: UserDataWithDefaults; |
| 159 | /** Set when an env var sends the stored login's key to a backend other than its issuer. */ |
| 160 | readonly backendMismatch: Option.Option<BackendMismatch>; |
| 161 | isLoggedIn: () => boolean; |
| 162 | logout: Effect.Effect<void, Schema.SchemaError | PlatformError.PlatformError, never>; |
| 163 | /** |
| 164 | * Stores a login and records the backend that issued its key: by default the backend this |
| 165 | * run resolved (the one the caller verified the key against), or with `issuer: |
| 166 | * 'environment'` the backend the env vars select, ignoring any earlier stored login, for a |
| 167 | * key no stored backend issued (an agent identity from agents.composio.dev). |
| 168 | */ |
| 169 | login: ( |
| 170 | apiKey: string, |
| 171 | orgId?: string, |
| 172 | testUserId?: string, |
| 173 | options?: LoginOptions |
| 174 | ) => Effect.Effect<void, Schema.SchemaError | PlatformError.PlatformError, never>; |
| 175 | /** Changes stored fields. The login's backend is only ever recorded by `login`. */ |
| 176 | update: ( |
| 177 | data: Partial<Omit<UserData, 'baseURL' | 'webURL'>> |
| 178 | ) => Effect.Effect<void, Schema.SchemaError | PlatformError.PlatformError, never>; |
| 179 | } |
| 180 | >()('ComposioUserData') {} |
| 181 | |
| 182 | export interface LoginOptions { |
| 183 | /** Which backend issued the key; `'resolved'` when absent. */ |
| 184 | readonly issuer?: 'resolved' | 'environment'; |
| 185 | } |
| 186 | |
| 187 | export const rawComposioUserContextLive = Layer.effect( |
| 188 | ComposioUserContext, |
| 189 | Effect.gen(function* () { |
| 190 | const fs = yield* FileSystem.FileSystem; |
| 191 | const path = yield* Path.Path; |
| 192 | const apiKey = Option.fromNullishOr(yield* APP_CONFIG.USER_API_KEY); |
| 193 | const envBackend = yield* APP_CONFIG.BACKEND; |
| 194 | const cliConfig = yield* ComposioCliUserConfig; |
| 195 | const keyring = yield* KeyringService; |
| 196 | |
| 197 | // `security: "auto"` and `"json"` both keep the API key in |
| 198 | // plaintext `user_data.json` (the legacy path — same behavior |
| 199 | // as every prior CLI release). `"keychain-subprocess"` and |
| 200 | // `"keychain"` opt into the OS credential store. |
| 201 | const useLegacyStorage = |
| 202 | cliConfig.data.security === 'auto' || cliConfig.data.security === 'json'; |
| 203 | const kDeps: KeyringDeps = { keyring, useLegacyStorage }; |
| 204 | // Whether the API key must stay in user_data.json: always for the legacy storage, and in |
| 205 | // the keychain modes while the keyring could not take it. |
| 206 | let keepApiKeyInFile = useLegacyStorage; |
| 207 | |
| 208 | const cacheDir = yield* setupCacheDir; |
| 209 | const jsonUserConfigPath = path.join(cacheDir, constants.USER_CONFIG_FILE_NAME); |
| 210 | |
| 211 | // The stored login's key (user_data.json or the keyring). `userData.apiKey` is the key |
| 212 | // requests use, COMPOSIO_USER_API_KEY first; only this one is ever written back, so an |
| 213 | // update made while that variable is set cannot pair its key with the stored backend. |
| 214 | let storedApiKey: Option.Option<string> = Option.none(); |
| 215 | |
| 216 | // `baseURL` and `webURL` hold what is stored: the backend the stored login was issued by. |
| 217 | let userData = UserData.make({ |
| 218 | apiKey, |
| 219 | baseURL: Option.none(), |
| 220 | webURL: Option.none(), |
| 221 | orgId: Option.none(), |
| 222 | projectId: Option.none(), |
| 223 | testUserId: Option.none(), |
| 224 | }); |
| 225 | |
| 226 | // The backend the stored login was issued by. A key from COMPOSIO_USER_API_KEY is not |
| 227 | // the stored login's, and a recorded backend without a stored key is no login. |
| 228 | const storedLogin = (): Option.Option<StoredBackend> => |
| 229 | Option.isSome(apiKey) || Option.isNone(storedApiKey) |
| 230 | ? Option.none() |
| 231 | : Option.map(userData.baseURL, baseURL => ({ baseURL, webURL: userData.webURL })); |
| 232 | const currentBackend = () => resolveBackend(envBackend, storedLogin()); |
| 233 | |
| 234 | const writeJson = (snapshot: UserData) => |
| 235 | Effect.gen(function* () { |
| 236 | const onDisk: UserData = { |
| 237 | ...snapshot, |
| 238 | apiKey: keepApiKeyInFile ? storedApiKey : Option.none(), |
| 239 | }; |
| 240 | const encoded = yield* userDataToJSON(onDisk); |
| 241 | const normalized = yield* normalizeEncodedUserData(encoded, !keepApiKeyInFile); |
| 242 | yield* Effect.logDebug('Saving user data:', redactSensitiveLogValue(onDisk)); |
| 243 | yield* atomicWritePrivateFileString({ |
| 244 | fs, |
| 245 | target: jsonUserConfigPath, |
| 246 | contents: normalized, |
| 247 | }); |
| 248 | }); |
| 249 | |
| 250 | const logout = Effect.gen(function* () { |
| 251 | yield* deleteKeyring(kDeps); |
| 252 | storedApiKey = Option.none(); |
| 253 | const cleared: UserData = { |
| 254 | apiKey: Option.none(), |
| 255 | baseURL: Option.none(), |
| 256 | webURL: Option.none(), |
| 257 | orgId: Option.none(), |
| 258 | projectId: Option.none(), |
| 259 | testUserId: Option.none(), |
| 260 | }; |
| 261 | userData = cleared; |
| 262 | yield* writeJson(cleared); |
| 263 | }); |
| 264 | |
| 265 | const login = (apiKey: string, orgId?: string, testUserId?: string, options?: LoginOptions) => |
| 266 | Effect.gen(function* () { |
| 267 | const backend = |
| 268 | options?.issuer === 'environment' |
| 269 | ? resolveBackend(envBackend, Option.none()) |
| 270 | : currentBackend(); |
| 271 | const keyringOk = yield* writeKeyring(kDeps, apiKey); |
| 272 | // Keyring write failed (NoStorageAccess / headless Linux): keep api_key in the JSON |
| 273 | // so it survives across process restarts. |
| 274 | keepApiKeyInFile = useLegacyStorage || !keyringOk; |
| 275 | storedApiKey = Option.some(apiKey); |
| 276 | const next: UserData = { |
| 277 | ...userData, |
| 278 | apiKey: Option.some(apiKey), |
| 279 | baseURL: Option.some(backend.baseURL), |
| 280 | webURL: Option.some(backend.webURL), |
| 281 | orgId: Option.fromNullishOr(orgId), |
| 282 | projectId: userData.projectId, |
| 283 | testUserId: Option.fromNullishOr(testUserId), |
| 284 | }; |
| 285 | userData = next; |
| 286 | yield* writeJson(next); |
| 287 | }); |
| 288 | |
| 289 | const update = (data: Partial<Omit<UserData, 'baseURL' | 'webURL'>>) => |
| 290 | Effect.gen(function* () { |
| 291 | const nextUserData = { |
| 292 | ...userData, |
| 293 | ...data, |
| 294 | baseURL: userData.baseURL, |
| 295 | webURL: userData.webURL, |
| 296 | } satisfies UserData; |
| 297 | userData = nextUserData; |
| 298 | yield* writeJson(nextUserData); |
| 299 | yield* Effect.logDebug('User data updated:', redactSensitiveLogValue(userData)); |
| 300 | }); |
| 301 | |
| 302 | const load = Effect.gen(function* () { |
| 303 | yield* Effect.logDebug('Loading user data from', jsonUserConfigPath); |
| 304 | yield* ensurePrivateFileMode({ fs, target: jsonUserConfigPath }); |
| 305 | const userDataJson = yield* fs.readFileString(jsonUserConfigPath, 'utf8'); |
| 306 | const parsedUserData = (yield* userDataFromJSON(userDataJson)) satisfies UserData; |
| 307 | yield* Effect.logDebug('User data (parsed):', redactSensitiveLogValue(parsedUserData)); |
| 308 | storedApiKey = parsedUserData.apiKey; |
| 309 | |
| 310 | const overriddenUserData = { |
| 311 | ...userData, |
| 312 | ...parsedUserData, |
| 313 | apiKey: apiKey.pipe(Option.orElse(() => parsedUserData.apiKey)), |
| 314 | orgId: parsedUserData.orgId, |
| 315 | projectId: parsedUserData.projectId, |
| 316 | testUserId: parsedUserData.testUserId, |
| 317 | } satisfies UserData; |
| 318 | |
| 319 | yield* Effect.logDebug( |
| 320 | 'User data (API key overridden from env vars):', |
| 321 | redactSensitiveLogValue(overriddenUserData) |
| 322 | ); |
| 323 | userData = overriddenUserData; |
| 324 | return userData; |
| 325 | }); |
| 326 | |
| 327 | if (yield* fs.exists(jsonUserConfigPath)) { |
| 328 | yield* Effect.logDebug('User data file exists, loading it'); |
| 329 | yield* load.pipe( |
| 330 | Effect.catch(error => |
| 331 | Effect.gen(function* () { |
| 332 | yield* Effect.logDebug( |
| 333 | 'Failed to load user data file (empty or corrupted), resetting to defaults:', |
| 334 | error |
| 335 | ); |
| 336 | yield* writeJson(userData); |
| 337 | }) |
| 338 | ) |
| 339 | ); |
| 340 | } else { |
| 341 | yield* Effect.logDebug('User data file does not exist, creating a new one'); |
| 342 | yield* writeJson(userData); |
| 343 | } |
| 344 | |
| 345 | // Resolve API key: env var > keyring > legacy plaintext. |
| 346 | if (Option.isNone(apiKey) && !useLegacyStorage) { |
| 347 | const plaintextOnDisk = userData.apiKey; |
| 348 | const fromKeyring = yield* readKeyring(kDeps); |
| 349 | |
| 350 | if (Option.isSome(fromKeyring)) { |
| 351 | storedApiKey = fromKeyring; |
| 352 | userData = { ...userData, apiKey: fromKeyring }; |
| 353 | if (Option.isSome(plaintextOnDisk)) { |
| 354 | yield* Effect.logDebug('Clearing stale plaintext api_key from user_data.json'); |
| 355 | yield* writeJson(userData); |
| 356 | } |
| 357 | } else if (Option.isSome(plaintextOnDisk)) { |
| 358 | yield* Effect.logDebug('Migrating legacy api_key from user_data.json to the OS keyring'); |
| 359 | const migrated = yield* writeKeyring(kDeps, plaintextOnDisk.value); |
| 360 | keepApiKeyInFile = !migrated; |
| 361 | if (migrated) { |
| 362 | yield* Effect.logInfo('Composio API key migrated from user_data.json to the OS keyring'); |
| 363 | yield* writeJson(userData); |
| 364 | } |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | const isLoggedIn = () => Option.isSome(userData.apiKey); |
| 369 | |
| 370 | const snapshot = (): UserDataWithDefaults => ({ |
| 371 | ...userData, |
| 372 | ...currentBackend(), |
| 373 | orgId: userData.orgId, |
| 374 | projectId: userData.projectId, |
| 375 | testUserId: userData.testUserId, |
| 376 | }); |
| 377 | |
| 378 | return ComposioUserContext.of({ |
| 379 | get data() { |
| 380 | return snapshot(); |
| 381 | }, |
| 382 | get backendMismatch() { |
| 383 | return backendMismatch(envBackend, storedLogin()); |
| 384 | }, |
| 385 | isLoggedIn, |
| 386 | update, |
| 387 | login, |
| 388 | logout, |
| 389 | }); |
| 390 | }) |
| 391 | ); |
| 392 | |
| 393 | /** |
| 394 | * Map the config's `security` field onto the cli-keyring package's |
| 395 | * `MacOSBackend` parameter. |
| 396 | * |
| 397 | * Only `"keychain"` opts into the FFI path. Everything else uses the |
| 398 | * subprocess backend — including `"auto"` and `"json"`, which never |
| 399 | * actually call the keyring (useLegacyStorage gates them off), but |
| 400 | * we still build a usable subprocess store so the layer wiring stays |
| 401 | * uniform. |
| 402 | */ |
| 403 | const resolveMacOSBackend = ( |
| 404 | security: 'auto' | 'json' | 'keychain-subprocess' | 'keychain' |
| 405 | ): MacOSBackend => (security === 'keychain' ? 'ffi' : 'auto'); |
| 406 | |
| 407 | /** |
| 408 | * Public layer that pre-provides the keyring and CLI-config deps, |
| 409 | * leaving only `FileSystem` and `Path` as external requirements. The keyring |
| 410 | * backend is chosen dynamically from the user's `config.json` |
| 411 | * `security` field — `"auto"` / `"keychain-subprocess"` select the |
| 412 | * subprocess path (default); `"keychain"` opts into the experimental |
| 413 | * FFI path (requires Developer ID-signed binary to avoid dialogs). |
| 414 | */ |
| 415 | export const ComposioUserContextLive = Layer.unwrap( |
| 416 | Effect.gen(function* () { |
| 417 | const cliConfig = yield* ComposioCliUserConfig; |
| 418 | const backend = resolveMacOSBackend(cliConfig.data.security); |
| 419 | return Layer.provide( |
| 420 | rawComposioUserContextLive, |
| 421 | Layer.mergeAll(KeyringLiveWithBackend(backend), ComposioCliUserConfigLive) |
| 422 | ); |
| 423 | }).pipe(Effect.provide(ComposioCliUserConfigLive)) |
| 424 | ); |