Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
ts 71 lines 2.48 KB
Raw
1 import process from 'node:process';
2 import { Config, ConfigProvider, Console, Effect, Logger, Layer, References } from 'effect';
3 import * as BunServices from '@effect/platform-bun/BunServices';
4 import * as BunRuntime from '@effect/platform-bun/BunRuntime';
5 import { FileSystem } from 'effect/FileSystem';
6 import { teardown } from './_shared';
7 import path from 'node:path';
8 import { $ } from 'bun';
9 import { CACHE_FILES } from '../src/services/composio-clients-cached';
10 import { setupCacheDir } from 'src/effects/setup-cache-dir';
11 import { NodeOs } from 'src/services/node-os';
12
13 /**
14 * Usage: `bun scripts/copy-mocks-from-cache.ts`.
15 *
16 * Copies cached mock responses from the cache directory to the specified mocks directory.
17 */
18 export function copyMocksFromCache() {
19 return Effect.gen(function* () {
20 const fs = yield* FileSystem;
21 const defaultMocksDir = path.join(process.cwd(), 'test', '__mocks__');
22
23 const cacheDir = yield* setupCacheDir;
24
25 const mocksDir = yield* Config.String('COMPOSIO_MOCKS_DIR').pipe(
26 Config.withDefault(defaultMocksDir)
27 );
28
29 yield* Effect.logDebug(`Copying mocks to ${mocksDir}`);
30
31 for (const [key, cacheFileName] of Object.entries(CACHE_FILES)) {
32 const cacheFilePath = path.join(cacheDir, cacheFileName);
33 const mocksFilePath = path.join(mocksDir, cacheFileName);
34
35 yield* Effect.logDebug(`Copying ${key} from ${cacheFilePath}`);
36
37 const cacheFileExists = yield* fs
38 .exists(cacheFilePath)
39 .pipe(Effect.orElseSucceed(() => false));
40
41 if (!cacheFileExists) {
42 yield* Effect.logWarning(`Cache file ${cacheFilePath} does not exist, skipping.`);
43 continue;
44 }
45
46 yield* Effect.tryPromise(() => $`cp ${cacheFilePath} ${mocksFilePath}`.quiet());
47 }
48
49 yield* Console.log('Cached responses successfully copied into mocks folder:', mocksDir);
50 });
51 }
52
53 const ConfigLive = Effect.gen(function* () {
54 const logLevel = yield* Config.LogLevel('COMPOSIO_LOG_LEVEL').pipe(Config.withDefault('Info'));
55
56 return Layer.succeed(References.MinimumLogLevel, logLevel);
57 }).pipe(Layer.unwrap, Layer.merge(ConfigProvider.layer(ConfigProvider.fromEnv())));
58
59 if (require.main === module) {
60 copyMocksFromCache().pipe(
61 Effect.provide(ConfigLive),
62 Effect.provide(Logger.layer([Logger.consolePretty()])),
63 Effect.provide(BunServices.layer),
64 Effect.provide(NodeOs.Default),
65 Effect.scoped,
66 Effect.map(() => ({ message: 'Process completed successfully.' })),
67 BunRuntime.runMain({
68 teardown,
69 })
70 );
71 }