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 | * The browser login that `composio login` started without waiting: its session key and URL, |
| 3 | * cached in `<cache dir>/pending-login-session.json` so that `composio login --poll` can |
| 4 | * complete it from another process. `composio dev init` starts the same browser login, so the |
| 5 | * file outlives any one command. |
| 6 | */ |
| 7 | import * as FileSystem from 'effect/FileSystem'; |
| 8 | import * as Path from 'effect/Path'; |
| 9 | import type { Config, PlatformError } from 'effect'; |
| 10 | import { ExpectedFailure } from 'src/effects/expected-failure'; |
| 11 | import { Clock, Context, Data, DateTime, Effect, Layer, Schema } from 'effect'; |
| 12 | import { setupCacheDir } from 'src/effects/setup-cache-dir'; |
| 13 | import { atomicWritePrivateFileString, ensurePrivateFileMode } from 'src/utils/atomic-write'; |
| 14 | import { NodeOs } from './node-os'; |
| 15 | |
| 16 | export const PENDING_LOGIN_FILE_NAME = 'pending-login-session.json'; |
| 17 | |
| 18 | /** How long a cached login stays usable, counted from when it was cached. */ |
| 19 | const PENDING_LOGIN_TTL_MS = 10 * 60 * 1000; |
| 20 | |
| 21 | export const PendingLogin = Schema.Struct({ |
| 22 | key: Schema.String, |
| 23 | loginUrl: Schema.String, |
| 24 | expiresAt: Schema.String, |
| 25 | cachedAt: Schema.String, |
| 26 | }); |
| 27 | export type PendingLogin = Schema.Schema.Type<typeof PendingLogin>; |
| 28 | |
| 29 | export class PendingLoginError extends Data.TaggedError('commands/PendingLoginError')<{ |
| 30 | readonly message: string; |
| 31 | readonly reason: 'invalid' | 'io' | 'missing' | 'expired'; |
| 32 | readonly cause?: unknown; |
| 33 | }> { |
| 34 | /** No login was started, or it expired; a cache that cannot be read or decoded is a defect. */ |
| 35 | get [ExpectedFailure]() { |
| 36 | return this.reason === 'missing' || this.reason === 'expired'; |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | export type PendingLoginSessionShape = { |
| 41 | /** Caches a started login, stamped with the current time, in a file only the user can read. */ |
| 42 | readonly write: ( |
| 43 | session: Omit<PendingLogin, 'cachedAt'> |
| 44 | ) => Effect.Effect<void, Config.ConfigError | PlatformError.PlatformError>; |
| 45 | /** |
| 46 | * The cached login. Fails with `missing` when there is none, `io` or `invalid` when the file |
| 47 | * cannot be read or decoded, and `expired` (after deleting it) when it is past its TTL. |
| 48 | */ |
| 49 | readonly read: Effect.Effect< |
| 50 | PendingLogin, |
| 51 | PendingLoginError | Config.ConfigError | PlatformError.PlatformError |
| 52 | >; |
| 53 | /** Deletes the cached login; a missing file is not an error. */ |
| 54 | readonly clear: Effect.Effect<void>; |
| 55 | }; |
| 56 | |
| 57 | const decodePendingLogin = Schema.decodeUnknownEffect(Schema.fromJsonString(PendingLogin)); |
| 58 | |
| 59 | const isExpired = (cachedAt: string, now: number) => { |
| 60 | const cachedAtMs = Date.parse(cachedAt); |
| 61 | return !Number.isFinite(cachedAtMs) || now - cachedAtMs > PENDING_LOGIN_TTL_MS; |
| 62 | }; |
| 63 | |
| 64 | const makePendingLoginSession = Effect.gen(function* () { |
| 65 | const fs = yield* FileSystem.FileSystem; |
| 66 | const path = yield* Path.Path; |
| 67 | const os = yield* NodeOs; |
| 68 | |
| 69 | // A login without a terminal on stdin is what caches a pending login. PowerShell has no |
| 70 | // `<` redirection, so it gets an empty pipeline instead. |
| 71 | const headlessLoginCommand = |
| 72 | os.platform === 'win32' ? '$null | composio login' : 'composio login < /dev/null'; |
| 73 | |
| 74 | // Resolved on every call: the cache directory comes from configuration read at call time. |
| 75 | const filePath = setupCacheDir.pipe( |
| 76 | Effect.map(cacheDir => path.join(cacheDir, PENDING_LOGIN_FILE_NAME)), |
| 77 | Effect.provideService(FileSystem.FileSystem, fs), |
| 78 | Effect.provideService(Path.Path, path), |
| 79 | Effect.provideService(NodeOs, os) |
| 80 | ); |
| 81 | |
| 82 | const clear = filePath.pipe( |
| 83 | Effect.flatMap(target => fs.remove(target)), |
| 84 | Effect.ignore |
| 85 | ); |
| 86 | |
| 87 | const write: PendingLoginSessionShape['write'] = session => |
| 88 | Effect.gen(function* () { |
| 89 | const target = yield* filePath; |
| 90 | const cachedAt = DateTime.formatIso(yield* DateTime.now); |
| 91 | const payload: PendingLogin = { ...session, cachedAt }; |
| 92 | yield* atomicWritePrivateFileString({ |
| 93 | fs, |
| 94 | target, |
| 95 | contents: `${JSON.stringify(payload, null, 2)}\n`, |
| 96 | }); |
| 97 | }); |
| 98 | |
| 99 | const read = Effect.gen(function* () { |
| 100 | const target = yield* filePath; |
| 101 | yield* fs.exists(target).pipe( |
| 102 | Effect.filterOrFail( |
| 103 | exists => exists, |
| 104 | () => |
| 105 | new PendingLoginError({ |
| 106 | message: `No pending login found. Run \`${headlessLoginCommand}\` first.`, |
| 107 | reason: 'missing', |
| 108 | }) |
| 109 | ) |
| 110 | ); |
| 111 | const raw = yield* ensurePrivateFileMode({ fs, target }).pipe( |
| 112 | Effect.andThen(fs.readFileString(target, 'utf8')), |
| 113 | Effect.mapError( |
| 114 | cause => |
| 115 | new PendingLoginError({ |
| 116 | message: 'Failed to read pending login cache', |
| 117 | reason: 'io', |
| 118 | cause, |
| 119 | }) |
| 120 | ) |
| 121 | ); |
| 122 | const session = yield* decodePendingLogin(raw).pipe( |
| 123 | Effect.mapError( |
| 124 | cause => |
| 125 | new PendingLoginError({ |
| 126 | message: 'Pending login cache is invalid', |
| 127 | reason: 'invalid', |
| 128 | cause, |
| 129 | }) |
| 130 | ) |
| 131 | ); |
| 132 | const now = yield* Clock.currentTimeMillis; |
| 133 | if (isExpired(session.cachedAt, now)) { |
| 134 | yield* clear; |
| 135 | return yield* new PendingLoginError({ |
| 136 | message: `Pending login expired. Run \`${headlessLoginCommand}\` again.`, |
| 137 | reason: 'expired', |
| 138 | }); |
| 139 | } |
| 140 | return session; |
| 141 | }); |
| 142 | |
| 143 | return { write, read, clear } satisfies PendingLoginSessionShape; |
| 144 | }); |
| 145 | |
| 146 | export class PendingLoginSession extends Context.Service< |
| 147 | PendingLoginSession, |
| 148 | PendingLoginSessionShape |
| 149 | >()('services/PendingLoginSession') { |
| 150 | static readonly Default: Layer.Layer< |
| 151 | PendingLoginSession, |
| 152 | never, |
| 153 | FileSystem.FileSystem | Path.Path | NodeOs |
| 154 | > = Layer.effect(PendingLoginSession, makePendingLoginSession); |
| 155 | } |