Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
feat(cli): add cache for ComposioToolkitsRepository
jkomyno committed
Jul 29, 2025 at 06:17 UTC
9d29b2176662dff9b1d82eb0852a16c890e66bd5
6 files changed
+178
-6
ts/packages/cli/src/bin.ts
+7
-1
@@ -13,6 +13,7 @@ import {
13
ComposioSessionRepository,
14
ComposioToolkitsRepository,
15
} from 'src/services/composio-clients';
16
+import { ComposioToolkitsRepositoryCached } from 'src/services/composio-clients-cached';
17
import { NodeOs } from 'src/services/node-os';
18
import { NodeProcess } from 'src/services/node-process';
19
import { EnvLangDetector } from 'src/services/env-lang-detector';
@@ -53,6 +54,11 @@ export const ComposioToolkitsRepositoryLive = Layer.provide(
54
Layer.mergeAll(BunFileSystem.layer, NodeOs.Default, ConfigLive)
55
) satisfies RequiredLayer;
56
57
+export const ComposioToolkitsRepositoryCachedLive = Layer.provide(
58
+ ComposioToolkitsRepositoryCached,
59
+ ComposioToolkitsRepositoryLive
60
+) satisfies RequiredLayer;
61
+
62
export const UpgradeBinaryLive = Layer.provide(
63
UpgradeBinary.Default,
64
Layer.mergeAll(BunFileSystem.layer, FetchHttpClient.layer)
@@ -65,7 +71,7 @@ const layers = Layer.mergeAll(
71
UpgradeBinaryLive,
72
ComposioUserContextLive,
73
ComposioSessionRepositoryLive,
68
- ComposioToolkitsRepositoryLive,
74
+ ComposioToolkitsRepositoryCachedLive, // Use the cached layer instead of the regular one
75
EnvLangDetector.Default,
76
JsPackageManagerDetector.Default,
77
BunContext.layer,
ts/packages/cli/src/effects/debug-config.ts
+1
-2
@@ -7,8 +7,7 @@ type DEBUG_CONFIG = Config.Config.Wrap<{
7
8
/**
9
* Describe debug configuration keys used at runtime.
10
- * Keys are read from environment variables (with the `DEBUG_OVERRIDE_<key>` format),
11
- * or from the user config file (in JSON format).
10
+ * Keys are read from environment variables (with the `DEBUG_OVERRIDE_<key>` format).
11
*/
12
export const DEBUG_OVERRIDE_CONFIG = {
13
// The local binary used when upgrading the Composio CLI (for debugging).
ts/packages/cli/src/effects/force-config.ts
new
+13
@@ -0,0 +1,13 @@
1
+import { Config, Option } from 'effect';
2
+
3
+type FORCE_CONFIG = Config.Config.Wrap<{
4
+ USE_CACHE: boolean;
5
+}>;
6
+
7
+/**
8
+ * Describe force configuration keys used at runtime.
9
+ * Keys are read from environment variables (with the `FORCE_<key>` format).
10
+ */
11
+export const FORCE_CONFIG = {
12
+ USE_CACHE: Config.boolean('FORCE_USE_CACHE').pipe(Config.withDefault(false)),
13
+} satisfies FORCE_CONFIG;
ts/packages/cli/src/effects/setup-cache-dir.ts
+2
-1
@@ -15,7 +15,8 @@ export const setupCacheDir = Effect.gen(function* () {
15
Effect.map(Option.getOrElse(() => path.join(os.homedir, constants.USER_COMPOSIO_DIR)))
16
);
17
18
- yield* fs.makeDirectory(cacheDir, { recursive: true });
18
+ // Ensure cache directory exists
19
+ yield* fs.makeDirectory(cacheDir, { recursive: true }).pipe(Effect.catchAll(() => Effect.void));
20
21
return cacheDir;
22
});
ts/packages/cli/src/services/composio-clients-cached.ts
new
+153
@@ -0,0 +1,153 @@
1
+import path from 'node:path';
2
+import { Effect, Option, ParseResult, Layer } from 'effect';
3
+import { FileSystem } from '@effect/platform';
4
+import { BunFileSystem } from '@effect/platform-bun';
5
+import { setupCacheDir } from 'src/effects/setup-cache-dir';
6
+import { FORCE_CONFIG } from 'src/effects/force-config';
7
+import { ComposioToolkitsRepository } from './composio-clients';
8
+import { NodeOs } from './node-os';
9
+import { toolkitsFromJSON, toolkitsToJSON } from 'src/models/toolkits';
10
+import { toolsFromJSON, toolsToJSON } from 'src/models/tools';
11
+import {
12
+ TriggerTypesAsEnumsFromJSON,
13
+ TriggerTypesAsEnumsToJSON,
14
+ TriggerTypesFromJSON,
15
+ TriggerTypesToJSON,
16
+} from 'src/models/trigger-types';
17
+import { ConfigLive } from './config';
18
+
19
+/**
20
+ * Cache file names for different data types
21
+ */
22
+export const CACHE_FILES = {
23
+ toolkits: 'toolkits.json',
24
+ tools: 'tools.json',
25
+ triggerTypesAsEnums: 'trigger-types-as-enums.json',
26
+ triggerTypes: 'trigger-types.json',
27
+} as const;
28
+
29
+/**
30
+ * Generic cache helper function that handles both cache read/write with graceful error handling.
31
+ */
32
+function createCachedEffect<T, E, R>(
33
+ cacheFileName: string,
34
+ decoder: (input: string) => Effect.Effect<T, ParseResult.ParseError>,
35
+ encoder: (input: T) => Effect.Effect<string, ParseResult.ParseError>,
36
+ computation: Effect.Effect<T, E, R>
37
+): Effect.Effect<T, E, R> {
38
+ // First define the cache-handling function that will run with all required services
39
+ const cacheEffect = Effect.gen(function* () {
40
+ const fs = yield* FileSystem.FileSystem;
41
+ const cacheDir = yield* setupCacheDir;
42
+
43
+ const cacheFilePath = path.join(cacheDir, cacheFileName);
44
+ const cacheFileExists = yield* fs
45
+ .exists(cacheFilePath)
46
+ .pipe(Effect.orElse(() => Effect.succeed(false)));
47
+ const consumeFromCache = yield* FORCE_CONFIG['USE_CACHE'];
48
+
49
+ if (consumeFromCache && cacheFileExists) {
50
+ yield* Effect.logDebug(`Cache HIT for ${cacheFileName}`);
51
+
52
+ // Try to read from cache
53
+ const cachedResult = yield* fs.readFileString(cacheFilePath).pipe(
54
+ Effect.flatMap(decoder),
55
+ Effect.asSome,
56
+ Effect.catchAll(error => {
57
+ // Log cache read/parse errors but don't fail - fall through to computation
58
+ return Effect.logWarning(`Failed to read/parse cache ${cacheFilePath}: ${error}`).pipe(
59
+ Effect.as(Option.none<T>())
60
+ );
61
+ })
62
+ );
63
+
64
+ if (Option.isSome(cachedResult)) {
65
+ return cachedResult.value;
66
+ }
67
+ }
68
+
69
+ yield* Effect.logDebug(`Cache MISS for ${cacheFileName}`);
70
+
71
+ // Fetch from the underlying service
72
+ const result = yield* computation;
73
+
74
+ // Write to cache (best effort: don't fail if cache write fails)
75
+ yield* encoder(result).pipe(
76
+ Effect.flatMap(content => fs.writeFileString(cacheFilePath, content)),
77
+ Effect.catchAll(error =>
78
+ Effect.logWarning(`Failed to write to cache ${cacheFilePath}: ${error}`)
79
+ )
80
+ );
81
+
82
+ return result;
83
+ });
84
+
85
+ // Handle any cache errors by falling back to the original computation
86
+ const handledCacheEffect = cacheEffect.pipe(
87
+ Effect.catchAll(error =>
88
+ Effect.logWarning(`Cache operation failed: ${error}`).pipe(Effect.flatMap(() => computation))
89
+ )
90
+ );
91
+
92
+ // This ensures the returned effect has the same error type as the original computation
93
+ // by providing all the required cache services
94
+ return handledCacheEffect.pipe(
95
+ Effect.provide(Layer.mergeAll(BunFileSystem.layer, NodeOs.Default))
96
+ ) as Effect.Effect<T, E, R>;
97
+}
98
+
99
+/**
100
+ * Cached implementation of ComposioToolkitsRepository using the wrapper layer pattern
101
+ *
102
+ * This layer adds file-based caching to the repository methods while preserving the
103
+ * exact same interface and error types.
104
+ */
105
+export const ComposioToolkitsRepositoryCached = Layer.effect(
106
+ ComposioToolkitsRepository,
107
+ Effect.gen(function* () {
108
+ const underlyingRepository = yield* ComposioToolkitsRepository;
109
+
110
+ // Create the cached implementation that wraps the original implementation
111
+ return ComposioToolkitsRepository.make({
112
+ getToolkits: () => {
113
+ return createCachedEffect(
114
+ CACHE_FILES.toolkits,
115
+ toolkitsFromJSON,
116
+ toolkitsToJSON,
117
+ underlyingRepository.getToolkits()
118
+ );
119
+ },
120
+
121
+ getTools: () => {
122
+ return createCachedEffect(
123
+ CACHE_FILES.tools,
124
+ toolsFromJSON,
125
+ toolsToJSON,
126
+ underlyingRepository.getTools()
127
+ );
128
+ },
129
+
130
+ getTriggerTypesAsEnums: () => {
131
+ return createCachedEffect(
132
+ CACHE_FILES.triggerTypesAsEnums,
133
+ TriggerTypesAsEnumsFromJSON,
134
+ TriggerTypesAsEnumsToJSON,
135
+ underlyingRepository.getTriggerTypesAsEnums()
136
+ );
137
+ },
138
+
139
+ getTriggerTypes: (limit: number) => {
140
+ return createCachedEffect(
141
+ // We don't care about the limit in the cache file name
142
+ CACHE_FILES.triggerTypes,
143
+ TriggerTypesFromJSON,
144
+ TriggerTypesToJSON,
145
+ underlyingRepository.getTriggerTypes(limit)
146
+ );
147
+ },
148
+ });
149
+ })
150
+).pipe(
151
+ // Provide the required dependencies for the layer
152
+ Layer.provide(Layer.mergeAll(BunFileSystem.layer, NodeOs.Default, ConfigLive))
153
+);
ts/packages/cli/src/services/composio-clients.ts
+2
-2
@@ -261,7 +261,7 @@ export class ComposioToolkitsRepository extends Effect.Service<ComposioToolkitsR
261
// Sort apps by slug.
262
// TODO: make sure this happens on the server-side.
263
const orderBySlug = Order.mapInput(Order.string, (app: Toolkit) => app.slug);
264
- return Array.sort(toolkits, orderBySlug);
264
+ return Array.sort(toolkits, orderBySlug) as ReadonlyArray<Toolkit>;
265
})
266
)
267
),
@@ -277,7 +277,7 @@ export class ComposioToolkitsRepository extends Effect.Service<ComposioToolkitsR
277
// Sort apps by slug.
278
// TODO: make sure this happens on the server-side.
279
const orderBySlug = Order.mapInput(Order.string, (app: TriggerType) => app.slug);
280
- return Array.sort(triggerTypes, orderBySlug);
280
+ return Array.sort(triggerTypes, orderBySlug) as ReadonlyArray<TriggerType>;
281
})
282
)
283
),