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 | #!/usr/bin/env bun |
| 2 | |
| 3 | /** |
| 4 | * Record CLI demo SVGs and asciicasts using VHS. |
| 5 | * |
| 6 | * Usage: `bun scripts/record.ts` |
| 7 | * |
| 8 | * Reads `recordings/recordings.yaml` and, for each recording entry: |
| 9 | * 1. Generates a VHS `.tape` file → `recordings/tapes/<group>/<name>.tape` |
| 10 | * 2. Runs VHS to produce an SVG → `recordings/svgs/<group>/<name>.svg` |
| 11 | * 3. Runs VHS to produce an asciicast → `recordings/ascii/<group>/<name>.ascii` |
| 12 | * |
| 13 | * Recordings run in parallel (concurrency = cpu count − 1). |
| 14 | * Requires `vhs` and `composio` on PATH, and COMPOSIO_API_KEY in the environment. |
| 15 | */ |
| 16 | |
| 17 | import process from 'node:process'; |
| 18 | import path from 'node:path'; |
| 19 | import os from 'node:os'; |
| 20 | import { Config, ConfigProvider, Effect, Logger, Layer, References, Ref } from 'effect'; |
| 21 | import * as BunServices from '@effect/platform-bun/BunServices'; |
| 22 | import * as BunRuntime from '@effect/platform-bun/BunRuntime'; |
| 23 | import { FileSystem } from 'effect/FileSystem'; |
| 24 | import { teardown } from './_shared'; |
| 25 | import { $ } from 'bun'; |
| 26 | import * as p from '@clack/prompts'; |
| 27 | import color from 'picocolors'; |
| 28 | |
| 29 | // --- Env vars --- |
| 30 | |
| 31 | declare module 'bun' { |
| 32 | interface Env { |
| 33 | COMPOSIO_API_KEY: string; |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Validates that env vars needed by the `composio` cli during VHS recording don't have undefined values. |
| 39 | * |
| 40 | * @param env - Environment variable dictionary |
| 41 | * @throws Error if any env vars have undefined values |
| 42 | */ |
| 43 | function validateRequiredEnvVars(env: Record<string, string | undefined> | undefined): void { |
| 44 | if (!env) return; |
| 45 | |
| 46 | const missingVars = Object.entries(env) |
| 47 | .filter(([, value]) => value === undefined) |
| 48 | .map(([key]) => key); |
| 49 | |
| 50 | if (missingVars.length > 0) { |
| 51 | throw new Error( |
| 52 | `Missing required environment variables: ${missingVars.join(', ')}\n` + |
| 53 | `Set these variables before running the tests.` |
| 54 | ); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | // Env vars to forward into VHS recordings. |
| 59 | const env = { |
| 60 | // Disables Clack interactive decorations so .ascii output is readable. |
| 61 | // See https://github.com/bombshell-dev/clack/pull/169 |
| 62 | CI: 'true', |
| 63 | COMPOSIO_API_KEY: Bun.env.COMPOSIO_API_KEY, |
| 64 | } as const; |
| 65 | |
| 66 | validateRequiredEnvVars(env); |
| 67 | |
| 68 | // --- Constannts --- |
| 69 | |
| 70 | const CONCURRENCY = Math.max(os.cpus().length - 1, 1); |
| 71 | |
| 72 | // --- Types --- |
| 73 | |
| 74 | interface VhsConfig { |
| 75 | shell: string; |
| 76 | width: number; |
| 77 | height: number; |
| 78 | fontSize: number; |
| 79 | fontFamily: string; |
| 80 | padding: number; |
| 81 | theme: string; |
| 82 | typingSpeed: string; |
| 83 | framerate: number; |
| 84 | cursorBlink: boolean; |
| 85 | windowBar: string; |
| 86 | sleepAfterEnter: string; |
| 87 | } |
| 88 | |
| 89 | interface Recording { |
| 90 | name: string; |
| 91 | command: string; |
| 92 | description?: string; |
| 93 | sleepAfterEnter?: string; |
| 94 | height?: 'dynamic' | number; |
| 95 | } |
| 96 | |
| 97 | interface RecordingsConfig { |
| 98 | vhs: VhsConfig; |
| 99 | recordings: Record<string, Recording[]>; |
| 100 | } |
| 101 | |
| 102 | // --- Tape generation --- |
| 103 | |
| 104 | /** |
| 105 | * Wraps a value in a VHS-compatible quoted string. |
| 106 | * |
| 107 | * VHS supports three string delimiters (`"`, `'`, `` ` ``) and NONE support |
| 108 | * escape sequences — the delimiter character always terminates the string. |
| 109 | * We pick a delimiter that doesn't appear in the value. |
| 110 | * |
| 111 | * @see https://github.com/charmbracelet/vhs/discussions/141 |
| 112 | */ |
| 113 | function vhsQuote(value: string): string { |
| 114 | if (!value.includes('"')) return `"${value}"`; |
| 115 | if (!value.includes('`')) return `\`${value}\``; |
| 116 | if (!value.includes("'")) return `'${value}'`; |
| 117 | throw new Error( |
| 118 | `Cannot quote VHS string — value contains all three delimiters (" ' \`): ${value}` |
| 119 | ); |
| 120 | } |
| 121 | |
| 122 | function generateSharedTape(vhs: VhsConfig): string { |
| 123 | // VHS requires ALL Set directives before any non-Set directive (Require, Env, etc.). |
| 124 | // Placing Require/Env before Set causes VHS to silently ignore the Set directives. |
| 125 | const lines = [ |
| 126 | `Set Shell ${vhs.shell}`, |
| 127 | `Set Width ${vhs.width}`, |
| 128 | // Height is intentionally omitted — set per-recording tape to support dynamic heights. |
| 129 | `Set FontSize ${vhs.fontSize}`, |
| 130 | `Set FontFamily ${vhsQuote(vhs.fontFamily)}`, |
| 131 | `Set Padding ${vhs.padding}`, |
| 132 | `Set Theme ${vhsQuote(vhs.theme)}`, |
| 133 | `Set TypingSpeed ${vhs.typingSpeed}`, |
| 134 | `Set Framerate ${vhs.framerate}`, |
| 135 | `Set CursorBlink ${vhs.cursorBlink}`, |
| 136 | `Set WindowBar ${vhs.windowBar}`, |
| 137 | `Require composio`, |
| 138 | `Env CI ${vhsQuote(env.CI)}`, |
| 139 | ]; |
| 140 | return lines.join('\n') + '\n'; |
| 141 | } |
| 142 | |
| 143 | function generateRecordingTape(opts: { |
| 144 | sharedTapePath: string; |
| 145 | outputPaths: string[]; |
| 146 | command: string; |
| 147 | description?: string; |
| 148 | sleepAfterEnter: string; |
| 149 | height: number; |
| 150 | }): string { |
| 151 | const lines = [`Set Height ${opts.height}`, `Source ${vhsQuote(opts.sharedTapePath)}`]; |
| 152 | lines.push(...opts.outputPaths.map(p => `Output ${vhsQuote(p)}`), ''); |
| 153 | |
| 154 | if (opts.description) { |
| 155 | lines.push('Hide'); |
| 156 | lines.push(`Type ${vhsQuote(`# ${opts.description}`)}`); |
| 157 | lines.push('Enter'); |
| 158 | lines.push('Show'); |
| 159 | } |
| 160 | |
| 161 | // Split multi-line commands into separate Type/Enter directives. |
| 162 | // For single-line commands, the loop runs once with the usual Sleep/Enter/Sleep after. |
| 163 | // For multi-line commands (with `\` continuations), each line gets its own Type/Enter |
| 164 | // pair so bash enters continuation mode between lines. |
| 165 | const commandLines = opts.command.split('\n'); |
| 166 | for (let i = 0; i < commandLines.length; i++) { |
| 167 | lines.push(`Type ${vhsQuote(commandLines[i]!)}`); |
| 168 | if (i < commandLines.length - 1) { |
| 169 | lines.push('Enter'); |
| 170 | } |
| 171 | } |
| 172 | lines.push('Sleep 300ms', 'Enter', `Sleep ${opts.sleepAfterEnter}`); |
| 173 | |
| 174 | return lines.join('\n') + '\n'; |
| 175 | } |
| 176 | |
| 177 | // --- Dynamic height --- |
| 178 | |
| 179 | /** Pixel overhead for window bar + padding + chrome (VHS height 750 → 480px content area). */ |
| 180 | const CHROME_PX = 270; |
| 181 | |
| 182 | /** |
| 183 | * Parse a VHS-generated SVG to find the maximum `y` attribute from `<text>` elements |
| 184 | * in the last animation frame. Returns the pixel offset of the lowest text line. |
| 185 | */ |
| 186 | function parseMaxContentY(svg: string): number { |
| 187 | // Find the last animation frame (highest translate X value). |
| 188 | const frameRe = /<g transform="translate\((\d+),0\)">/g; |
| 189 | let maxX = 0; |
| 190 | let lastFrameIdx = 0; |
| 191 | let m: RegExpExecArray | null; |
| 192 | while ((m = frameRe.exec(svg)) !== null) { |
| 193 | const x = parseInt(m[1]!, 10); |
| 194 | if (x >= maxX) { |
| 195 | maxX = x; |
| 196 | lastFrameIdx = m.index; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | // Slice the last frame (up to the next frame or closing container tag). |
| 201 | const nextFrame = svg.indexOf('<g transform="translate(', lastFrameIdx + 1); |
| 202 | const frame = svg.slice(lastFrameIdx, nextFrame > lastFrameIdx ? nextFrame : undefined); |
| 203 | |
| 204 | // Find the max y across all <text> elements in that frame. |
| 205 | const yRe = /<text y="(\d+)"/g; |
| 206 | let maxY = 0; |
| 207 | while ((m = yRe.exec(frame)) !== null) { |
| 208 | maxY = Math.max(maxY, parseInt(m[1]!, 10)); |
| 209 | } |
| 210 | return maxY; |
| 211 | } |
| 212 | |
| 213 | // --- Progress bar --- |
| 214 | |
| 215 | const BAR_SIZE = 30; |
| 216 | |
| 217 | function drawBar(current: number, max: number): string { |
| 218 | const filled = Math.round((current / max) * BAR_SIZE); |
| 219 | const empty = BAR_SIZE - filled; |
| 220 | return `${color.green('━'.repeat(filled))}${color.gray('─'.repeat(empty))}`; |
| 221 | } |
| 222 | |
| 223 | // --- Recording --- |
| 224 | |
| 225 | function runVhs(tapePath: string, cliRoot: string, label: string) { |
| 226 | return Effect.tryPromise({ |
| 227 | try: async () => { |
| 228 | const r = await $`vhs ${tapePath}`.cwd(cliRoot).quiet().nothrow(); |
| 229 | if (r.exitCode !== 0) { |
| 230 | throw new Error(`vhs exited with code ${r.exitCode}\n${r.stderr.toString()}`); |
| 231 | } |
| 232 | }, |
| 233 | catch: err => new Error(`Failed to record ${label}: ${err}`), |
| 234 | }); |
| 235 | } |
| 236 | |
| 237 | function recordCommand(opts: { |
| 238 | group: string; |
| 239 | recording: Recording; |
| 240 | defaultSleepAfterEnter: string; |
| 241 | sharedTapePath: string; |
| 242 | svgsDir: string; |
| 243 | asciiDir: string; |
| 244 | tapesDir: string; |
| 245 | cliRoot: string; |
| 246 | vhsHeight: number; |
| 247 | }) { |
| 248 | return Effect.gen(function* () { |
| 249 | const fs = yield* FileSystem; |
| 250 | const { |
| 251 | group, |
| 252 | recording, |
| 253 | defaultSleepAfterEnter, |
| 254 | sharedTapePath, |
| 255 | svgsDir, |
| 256 | asciiDir, |
| 257 | tapesDir, |
| 258 | cliRoot, |
| 259 | vhsHeight, |
| 260 | } = opts; |
| 261 | const rel = (absPath: string) => path.relative(cliRoot, absPath); |
| 262 | const label = `${group}/${recording.name}`; |
| 263 | |
| 264 | const sleepAfterEnter = recording.sleepAfterEnter ?? defaultSleepAfterEnter; |
| 265 | const maxHeight = vhsHeight * 2; |
| 266 | |
| 267 | // Resolve the recording height: explicit number, dynamic (two-pass), or default. |
| 268 | let height = vhsHeight; |
| 269 | if (typeof recording.height === 'number') { |
| 270 | height = recording.height; |
| 271 | } else if (recording.height === 'dynamic') { |
| 272 | const probeSvgPath = path.join(tapesDir, group, `${recording.name}.probe.svg`); |
| 273 | const probeTapePath = path.join(tapesDir, group, `${recording.name}.probe.tape`); |
| 274 | |
| 275 | const probeTape = generateRecordingTape({ |
| 276 | sharedTapePath: rel(sharedTapePath), |
| 277 | outputPaths: [rel(probeSvgPath)], |
| 278 | command: recording.command, |
| 279 | description: recording.description, |
| 280 | sleepAfterEnter, |
| 281 | height: maxHeight, |
| 282 | }); |
| 283 | yield* fs.writeFileString(probeTapePath, probeTape); |
| 284 | yield* runVhs(probeTapePath, cliRoot, `${label} (probe)`); |
| 285 | |
| 286 | // Parse the probe SVG for the lowest text line in the final frame. |
| 287 | const svg = yield* fs.readFileString(probeSvgPath); |
| 288 | const maxY = parseMaxContentY(svg); |
| 289 | height = Math.min(Math.max(maxY + CHROME_PX, vhsHeight), maxHeight); |
| 290 | |
| 291 | yield* Effect.logDebug(`[probe] ${label}: maxY=${maxY} → height=${height}`); |
| 292 | |
| 293 | // Clean up probe artifacts. |
| 294 | yield* fs.remove(probeSvgPath).pipe(Effect.orElseSucceed(() => undefined)); |
| 295 | yield* fs.remove(probeTapePath).pipe(Effect.orElseSucceed(() => undefined)); |
| 296 | } |
| 297 | |
| 298 | // Final recording pass (or the only pass for fixed-height recordings). |
| 299 | const tapeContent = generateRecordingTape({ |
| 300 | sharedTapePath: rel(sharedTapePath), |
| 301 | outputPaths: [ |
| 302 | rel(path.join(svgsDir, group, `${recording.name}.svg`)), |
| 303 | rel(path.join(asciiDir, group, `${recording.name}.ascii`)), |
| 304 | ], |
| 305 | command: recording.command, |
| 306 | description: recording.description, |
| 307 | sleepAfterEnter, |
| 308 | height, |
| 309 | }); |
| 310 | |
| 311 | const tapePath = path.join(tapesDir, group, `${recording.name}.tape`); |
| 312 | yield* fs.writeFileString(tapePath, tapeContent); |
| 313 | yield* runVhs(tapePath, cliRoot, label); |
| 314 | }); |
| 315 | } |
| 316 | |
| 317 | function recordAll() { |
| 318 | return Effect.gen(function* () { |
| 319 | const fs = yield* FileSystem; |
| 320 | |
| 321 | yield* Effect.sync(() => p.intro('composio record')); |
| 322 | |
| 323 | // Tape-embedded paths are relative to the cli package root (ts/packages/cli/). |
| 324 | const scriptDir = path.resolve( |
| 325 | import.meta.dirname ?? path.dirname(new URL(import.meta.url).pathname) |
| 326 | ); |
| 327 | const cliRoot = path.resolve(scriptDir, '..'); |
| 328 | const recordingsDir = path.resolve(scriptDir, '..', 'recordings'); |
| 329 | const configPath = path.join(recordingsDir, 'recordings.yaml'); |
| 330 | |
| 331 | yield* Effect.logDebug(`Reading config from ${configPath}`); |
| 332 | |
| 333 | const configRaw = yield* fs.readFileString(configPath); |
| 334 | const config = Bun.YAML.parse(configRaw) as RecordingsConfig; |
| 335 | |
| 336 | // Output directories for generated tapes, SVGs, and asciicasts. |
| 337 | const tapesDir = path.join(recordingsDir, 'tapes'); |
| 338 | const svgsDir = path.join(recordingsDir, 'svgs'); |
| 339 | const asciiDir = path.join(recordingsDir, 'ascii'); |
| 340 | yield* fs.makeDirectory(tapesDir, { recursive: true }); |
| 341 | yield* fs.makeDirectory(svgsDir, { recursive: true }); |
| 342 | yield* fs.makeDirectory(asciiDir, { recursive: true }); |
| 343 | |
| 344 | // Write shared config tape. |
| 345 | const sharedTapePath = path.join(tapesDir, 'shared-config.tape'); |
| 346 | yield* fs.writeFileString(sharedTapePath, generateSharedTape(config.vhs)); |
| 347 | |
| 348 | yield* Effect.sync(() => |
| 349 | p.log.step(`Loaded config from ${color.dim(path.relative(process.cwd(), configPath))}`) |
| 350 | ); |
| 351 | |
| 352 | // Flatten all recordings across groups. |
| 353 | const groups = Object.entries(config.recordings); |
| 354 | const allRecordings = groups.flatMap(([group, recordings]) => |
| 355 | recordings.map(recording => ({ group, recording })) |
| 356 | ); |
| 357 | const total = allRecordings.length; |
| 358 | |
| 359 | // Create all group directories upfront. |
| 360 | const groupNames = [...new Set(groups.map(([g]) => g))]; |
| 361 | yield* Effect.forEach( |
| 362 | groupNames, |
| 363 | group => |
| 364 | Effect.all([ |
| 365 | fs.makeDirectory(path.join(svgsDir, group), { recursive: true }), |
| 366 | fs.makeDirectory(path.join(asciiDir, group), { recursive: true }), |
| 367 | fs.makeDirectory(path.join(tapesDir, group), { recursive: true }), |
| 368 | ]), |
| 369 | { concurrency: 'unbounded' } |
| 370 | ); |
| 371 | |
| 372 | // Record all with bounded concurrency and a single progress spinner. |
| 373 | yield* Effect.sync(() => |
| 374 | p.log.step( |
| 375 | `Recording ${color.bold(String(total))} commands ${color.dim(`(concurrency: ${CONCURRENCY})`)}` |
| 376 | ) |
| 377 | ); |
| 378 | const completed = yield* Ref.make(0); |
| 379 | const spin = p.spinner(); |
| 380 | yield* Effect.sync(() => spin.start(`${drawBar(0, total)} ${color.dim(`0/${total}`)}`)); |
| 381 | |
| 382 | yield* Effect.forEach( |
| 383 | allRecordings, |
| 384 | ({ group, recording }) => |
| 385 | recordCommand({ |
| 386 | group, |
| 387 | recording, |
| 388 | defaultSleepAfterEnter: config.vhs.sleepAfterEnter, |
| 389 | sharedTapePath, |
| 390 | svgsDir, |
| 391 | asciiDir, |
| 392 | tapesDir, |
| 393 | cliRoot, |
| 394 | vhsHeight: config.vhs.height, |
| 395 | }).pipe( |
| 396 | Effect.tap(() => |
| 397 | Ref.updateAndGet(completed, c => c + 1).pipe( |
| 398 | Effect.tap(n => |
| 399 | Effect.sync(() => |
| 400 | spin.message( |
| 401 | `${drawBar(n, total)} ${color.dim(`${n}/${total}`)} ${group}/${recording.name}` |
| 402 | ) |
| 403 | ) |
| 404 | ) |
| 405 | ) |
| 406 | ), |
| 407 | Effect.tapError(err => |
| 408 | Ref.get(completed).pipe( |
| 409 | Effect.tap(n => |
| 410 | Effect.sync(() => { |
| 411 | spin.error( |
| 412 | `${drawBar(n, total)} ${color.bold(group)} failed at ${recording.name}` |
| 413 | ); |
| 414 | p.log.error(String(err)); |
| 415 | }) |
| 416 | ) |
| 417 | ) |
| 418 | ) |
| 419 | ), |
| 420 | { concurrency: CONCURRENCY } |
| 421 | ); |
| 422 | |
| 423 | yield* Effect.sync(() => |
| 424 | spin.stop(`${drawBar(total, total)} ${color.dim(`${total}/${total}`)} ${color.green('done')}`) |
| 425 | ); |
| 426 | |
| 427 | // Collect file sizes. |
| 428 | let totalBytes = 0n; |
| 429 | for (const { group, recording } of allRecordings) { |
| 430 | for (const filePath of [ |
| 431 | path.join(svgsDir, group, `${recording.name}.svg`), |
| 432 | path.join(asciiDir, group, `${recording.name}.ascii`), |
| 433 | ]) { |
| 434 | const stat = yield* fs |
| 435 | .stat(filePath) |
| 436 | .pipe(Effect.orElseSucceed((): { size: bigint } => ({ size: 0n }))); |
| 437 | totalBytes += BigInt(stat.size); |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | const totalKB = (Number(totalBytes) / 1024).toFixed(1); |
| 442 | |
| 443 | yield* Effect.sync(() => |
| 444 | p.note( |
| 445 | [ |
| 446 | `${color.bold(String(total))} recordings`, |
| 447 | `${color.bold(totalKB)} KB total`, |
| 448 | color.dim(`${path.relative(process.cwd(), recordingsDir)}/`), |
| 449 | ].join('\n'), |
| 450 | 'Summary' |
| 451 | ) |
| 452 | ); |
| 453 | |
| 454 | yield* Effect.sync(() => p.outro('Recording complete')); |
| 455 | }); |
| 456 | } |
| 457 | |
| 458 | // --- Bootstrap --- |
| 459 | |
| 460 | const ConfigLive = Effect.gen(function* () { |
| 461 | const logLevel = yield* Config.LogLevel('COMPOSIO_LOG_LEVEL').pipe(Config.withDefault('Info')); |
| 462 | |
| 463 | return Layer.succeed(References.MinimumLogLevel, logLevel); |
| 464 | }).pipe(Layer.unwrap, Layer.merge(ConfigProvider.layer(ConfigProvider.fromEnv()))); |
| 465 | |
| 466 | recordAll().pipe( |
| 467 | Effect.provide(ConfigLive), |
| 468 | Effect.provide(Logger.layer([Logger.consolePretty()])), |
| 469 | Effect.provide(BunServices.layer), |
| 470 | Effect.map(() => ({ message: 'Process completed successfully.' })), |
| 471 | BunRuntime.runMain({ |
| 472 | teardown, |
| 473 | }) |
| 474 | ); |