@devries-cameron20 / composio Mirror · read-only / commits / 4a764c100
Fork 0

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, 16:00 UTC

perf(cli): fetch the toolkit catalog once per process

A single command can ask for the full toolkit list several times — toolkit resolution, validation, telemetry — and the file cache only answers when FORCE_USE_CACHE is set, so the default path paid for the same ~800 KB two-page download up to four times. Memoize the lookup for the lifetime of the cached repository layer. The first caller pays and the rest await the same result. Effect.cached memoizes the outcome rather than only the success, so a failed fetch stays failed for the process: the right trade for a one-shot CLI, where retrying a broken network per call only multiplies the wait before the same error surfaces. That surfaced a second doubling underneath. The caching helper wrapped its whole body — cache lookup and the underlying request alike — in one catchAll that fell back to re-running the request, so a request that failed on its own was immediately sent again and failed the same way. Cache concerns now degrade to a miss on their own, and the request runs exactly once with its failure passed straight through.

jkomyno committed Aug 4, 2026 at 19:11 UTC 4a764c100a729f57a6b311f4700655062894cf23
4 files changed +296 -50
ts/packages/cli/CHANGELOG.md
+7
@@ -37,6 +37,13 @@
37 to the `composio_search` toolkit, whose slug their names happen to start
38 with. A failed meta call used to suggest linking an app that had nothing to
39 do with the call.
40 +- Commands that consult the toolkit catalog more than once now fetch it once.
41 + `composio tools execute` was downloading the full ~800 KB list up to four
42 + times per run.
43 +- A cached API request that fails is no longer sent a second time. The caching
44 + layer treated the request's own failure as a cache failure and retried it,
45 + so every failed toolkit, tool, or trigger listing cost two round trips and
46 + twice the wait before reporting the same error.
47
48 ## 0.3.1
49
ts/packages/cli/src/services/composio-clients-cached.ts
+85 -50
@@ -55,69 +55,90 @@ function createCachedEffect<T, E, R>(
55 computation: Effect.Effect<T, E, R>,
56 cacheFilter?: (data: T) => Effect.Effect<T, E, never>
57 ): Effect.Effect<T, E, R> {
58 - // First define the cache-handling function that will run with all required services
59 - const cacheEffect = Effect.gen(function* () {
60 - const fs = yield* FileSystem.FileSystem;
61 - const path = yield* Path.Path;
62 - const cacheDir = yield* setupCacheDir;
58 + /**
59 + * The cached value, or none when there is nothing usable to serve: caching
60 + * is off, the file is absent, it does not parse, or it does not cover the
61 + * request. Every one of those is a cache miss, never a failure — the caller
62 + * falls through to the computation.
63 + */
64 + const readFromCache = (fs: FileSystem.FileSystem, cacheFilePath: string) =>
65 + Effect.gen(function* () {
66 + const consumeFromCache = yield* FORCE_CONFIG['USE_CACHE'];
67 + if (!consumeFromCache) {
68 + return Option.none<T>();
69 + }
70
64 - const cacheFilePath = path.join(cacheDir, cacheFileName);
65 - const cacheFileExists = yield* fs
66 - .exists(cacheFilePath)
67 - .pipe(Effect.orElse(() => Effect.succeed(false)));
68 - const consumeFromCache = yield* FORCE_CONFIG['USE_CACHE'];
71 + const cacheFileExists = yield* fs
72 + .exists(cacheFilePath)
73 + .pipe(Effect.orElse(() => Effect.succeed(false)));
74 + if (!cacheFileExists) {
75 + return Option.none<T>();
76 + }
77
70 - if (consumeFromCache && cacheFileExists) {
78 yield* Effect.logDebug(`Cache HIT for ${cacheFileName}`);
79
73 - // Try to read from cache
74 - const cachedResult = yield* fs.readFileString(cacheFilePath).pipe(
75 - Effect.flatMap(decoder),
76 - Effect.asSome,
77 - Effect.catchAll(error => {
78 - // Log cache read/parse errors but don't fail - fall through to computation
79 - return Effect.logWarning(`Failed to read/parse cache ${cacheFilePath}: ${error}`).pipe(
80 - Effect.as(Option.none<T>())
81 - );
82 - })
83 - );
80 + const cached = yield* fs.readFileString(cacheFilePath).pipe(Effect.flatMap(decoder));
81 +
82 + // A filter that rejects the cached data means the cache does not answer
83 + // this request — e.g. it predates a toolkit the caller asked for.
84 + return Option.some(cacheFilter ? yield* cacheFilter(cached) : cached);
85 + }).pipe(
86 + Effect.catchAll(error =>
87 + Effect.logWarning(`Ignoring cache ${cacheFilePath}: ${error}`).pipe(
88 + Effect.as(Option.none<T>())
89 + )
90 + )
91 + );
92 +
93 + const writeToCache = (fs: FileSystem.FileSystem, cacheFilePath: string, result: T) =>
94 + encoder(result).pipe(
95 + Effect.flatMap(content => fs.writeFileString(cacheFilePath, content)),
96 + Effect.catchAll(error =>
97 + Effect.logWarning(`Failed to write to cache ${cacheFilePath}: ${error}`)
98 + )
99 + );
100 +
101 + const cacheEffect = Effect.gen(function* () {
102 + const fs = yield* FileSystem.FileSystem;
103 + const path = yield* Path.Path;
104
85 - if (Option.isSome(cachedResult)) {
86 - return yield* cacheFilter
87 - ? cacheFilter(cachedResult.value)
88 - : Effect.succeed(cachedResult.value);
105 + // A cache directory we cannot resolve or create means no caching, not a
106 + // failed command.
107 + const cacheFilePath = yield* setupCacheDir.pipe(
108 + Effect.map(cacheDir => Option.some(path.join(cacheDir, cacheFileName))),
109 + Effect.catchAll(error =>
110 + Effect.logWarning(`Cache unavailable for ${cacheFileName}: ${error}`).pipe(
111 + Effect.as(Option.none<string>())
112 + )
113 + )
114 + );
115 +
116 + if (Option.isSome(cacheFilePath)) {
117 + const cached = yield* readFromCache(fs, cacheFilePath.value);
118 + if (Option.isSome(cached)) {
119 + return cached.value;
120 }
121 }
122
123 yield* Effect.logDebug(`Cache MISS for ${cacheFileName}`);
124
94 - // Fetch from the underlying service
125 + // Fetch from the underlying service. Its failure is the caller's failure:
126 + // retrying here would double every failed request, and the retry would
127 + // fail the same way.
128 const result = yield* computation;
129
130 // Write to cache only if we're fetching the full dataset (no cacheFilter).
131 // Filtered API calls fetch partial data that would corrupt the shared cache file.
99 - if (!cacheFilter) {
100 - yield* encoder(result).pipe(
101 - Effect.flatMap(content => fs.writeFileString(cacheFilePath, content)),
102 - Effect.catchAll(error =>
103 - Effect.logWarning(`Failed to write to cache ${cacheFilePath}: ${error}`)
104 - )
105 - );
132 + if (!cacheFilter && Option.isSome(cacheFilePath)) {
133 + yield* writeToCache(fs, cacheFilePath.value, result);
134 }
135
136 return result;
137 });
138
111 - // Handle any cache errors by falling back to the original computation
112 - const handledCacheEffect = cacheEffect.pipe(
113 - Effect.catchAll(error =>
114 - Effect.logWarning(`Cache operation failed: ${error}`).pipe(Effect.flatMap(() => computation))
115 - )
116 - );
117 -
139 // This ensures the returned effect has the same error type as the original computation
140 // by providing all the required cache services
120 - return handledCacheEffect.pipe(
141 + return cacheEffect.pipe(
142 Effect.provide(Layer.mergeAll(BunFileSystem.layer, Path.layer, NodeOs.Default))
143 ) as Effect.Effect<T, E, R>;
144 }
@@ -133,16 +154,30 @@ export const ComposioToolkitsRepositoryCached = Layer.effect(
154 Effect.gen(function* () {
155 const underlyingRepository = yield* ComposioToolkitsRepository;
156
157 + // The full toolkit list is ~800 KB over two pages, and a single command can
158 + // ask for it several times (toolkit resolution, validation, telemetry). The
159 + // file cache only helps when `FORCE_USE_CACHE` is on, so memoize the whole
160 + // lookup for the lifetime of the layer: the first caller pays, the rest
161 + // await the same result.
162 + //
163 + // `Effect.cached` memoizes the outcome, not just the success — a failed
164 + // first fetch stays failed for the rest of the process. That is the right
165 + // trade for a one-shot CLI, where retrying a broken network per call would
166 + // only multiply the wait before the same error surfaces.
167 + const cachedGetToolkits = yield* Effect.cached(
168 + createCachedEffect(
169 + CACHE_FILES.toolkits,
170 + toolkitsFromJSON,
171 + toolkitsToJSON,
172 + underlyingRepository.getToolkits()
173 + )
174 + );
175 +
176 // Create the cached implementation that wraps the original implementation
177 return ComposioToolkitsRepository.make({
138 - getToolkits: () => {
139 - return createCachedEffect(
140 - CACHE_FILES.toolkits,
141 - toolkitsFromJSON,
142 - toolkitsToJSON,
143 - underlyingRepository.getToolkits()
144 - );
145 - },
178 + // Memoized per layer instance; `getToolkitsBySlugs` stays unmemoized
179 + // because its result depends on the requested slugs.
180 + getToolkits: () => cachedGetToolkits,
181
182 getToolkitsBySlugs: slugs => {
183 const cacheFilter = (data: Toolkits) => {
ts/packages/cli/test/__utils__/services/toolkits-repository-stub.ts new
+71
@@ -0,0 +1,71 @@
1 +import { Effect, Layer } from 'effect';
2 +import { ComposioToolkitsRepository } from 'src/services/composio-clients';
3 +import type { Toolkits } from 'src/models/toolkits';
4 +
5 +export type GetToolkitsError = Effect.Effect.Error<
6 + ReturnType<ComposioToolkitsRepository['getToolkits']>
7 +>;
8 +
9 +const notUsed = (method: string) => () => Effect.die(`${method} is not used in this test`);
10 +
11 +/**
12 + * Every repository method the toolkit-catalog suites do not exercise. A call
13 + * to any of them is a defect in the test, not a scenario to handle.
14 + */
15 +const unusedRepositoryMethods = {
16 + getToolkitsBySlugs: notUsed('getToolkitsBySlugs'),
17 + getMetrics: notUsed('getMetrics'),
18 + getToolsAsEnums: notUsed('getToolsAsEnums'),
19 + getTools: notUsed('getTools'),
20 + getToolsByVersionSpecs: notUsed('getToolsByVersionSpecs'),
21 + getTriggerTypesAsEnums: notUsed('getTriggerTypesAsEnums'),
22 + getTriggerTypes: notUsed('getTriggerTypes'),
23 + getTriggerTypeDetailed: notUsed('getTriggerTypeDetailed'),
24 + validateToolkits: notUsed('validateToolkits'),
25 + validateToolkitVersions: notUsed('validateToolkitVersions'),
26 + // The repository's one synchronous method cannot return a dying Effect.
27 + filterToolkitsBySlugs: (): never => {
28 + throw new Error('filterToolkitsBySlugs is not used in this test');
29 + },
30 + searchToolkits: notUsed('searchToolkits'),
31 + getToolkitDetailed: notUsed('getToolkitDetailed'),
32 + searchTools: notUsed('searchTools'),
33 + getToolDetailed: notUsed('getToolDetailed'),
34 + listAuthConfigs: notUsed('listAuthConfigs'),
35 + getAuthConfig: notUsed('getAuthConfig'),
36 + createAuthConfig: notUsed('createAuthConfig'),
37 + deleteAuthConfig: notUsed('deleteAuthConfig'),
38 + listConnectedAccounts: notUsed('listConnectedAccounts'),
39 + getConnectedAccount: notUsed('getConnectedAccount'),
40 + deleteConnectedAccount: notUsed('deleteConnectedAccount'),
41 + createConnectedAccountLink: notUsed('createConnectedAccountLink'),
42 + listActiveTriggers: notUsed('listActiveTriggers'),
43 + createTrigger: notUsed('createTrigger'),
44 + enableTrigger: notUsed('enableTrigger'),
45 + disableTrigger: notUsed('disableTrigger'),
46 + deleteTrigger: notUsed('deleteTrigger'),
47 +} as const;
48 +
49 +/**
50 + * A `ComposioToolkitsRepository` layer that counts catalog fetches, so a test
51 + * can assert not just what was resolved but what it cost.
52 + */
53 +export const countingToolkitsRepository = (
54 + getToolkits: () => Effect.Effect<Toolkits, GetToolkitsError>
55 +) => {
56 + let calls = 0;
57 +
58 + const layer = Layer.succeed(
59 + ComposioToolkitsRepository,
60 + new ComposioToolkitsRepository({
61 + ...unusedRepositoryMethods,
62 + getToolkits: () =>
63 + Effect.suspend(() => {
64 + calls += 1;
65 + return getToolkits();
66 + }),
67 + })
68 + );
69 +
70 + return { layer, calls: () => calls };
71 +};
ts/packages/cli/test/src/services/composio-clients-cached.test.ts new
+133
@@ -0,0 +1,133 @@
1 +import { describe, expect, it } from '@effect/vitest';
2 +import { ConfigProvider, DateTime, Effect, Layer } from 'effect';
3 +import * as tempy from 'tempy';
4 +import { ComposioToolkitsRepository, HttpServerError } from 'src/services/composio-clients';
5 +import { FileSystem } from '@effect/platform';
6 +import { BunFileSystem } from '@effect/platform-bun';
7 +import {
8 + CACHE_FILES,
9 + ComposioToolkitsRepositoryCached,
10 +} from 'src/services/composio-clients-cached';
11 +import { toolkitsToJSON, type Toolkits } from 'src/models/toolkits';
12 +import { makeToolkitFixture } from 'test/__utils__/models/toolkits';
13 +import {
14 + countingToolkitsRepository,
15 + type GetToolkitsError,
16 +} from 'test/__utils__/services/toolkits-repository-stub';
17 +
18 +const testToolkits: Toolkits = [makeToolkitFixture('github'), makeToolkitFixture('gmail')];
19 +
20 +/**
21 + * Cached repository over a counting stub, with the cache directory pointed at a
22 + * fresh temp dir so nothing leaks between tests or into the developer's
23 + * `~/.composio`. `FORCE_USE_CACHE` stays unset unless a test passes it in, i.e.
24 + * the file cache is write-only — the default path this suite is about.
25 + */
26 +const withCountingRepository = <A>(
27 + getToolkits: () => Effect.Effect<Toolkits, GetToolkitsError>,
28 + program: (context: {
29 + readonly calls: () => number;
30 + readonly cacheDir: string;
31 + }) => Effect.Effect<A, GetToolkitsError, ComposioToolkitsRepository | FileSystem.FileSystem>,
32 + config: ReadonlyArray<readonly [string, string]> = []
33 +) =>
34 + Effect.suspend(() => {
35 + const cacheDir = tempy.temporaryDirectory();
36 + const repository = countingToolkitsRepository(getToolkits);
37 +
38 + return program({ calls: repository.calls, cacheDir }).pipe(
39 + Effect.provide(
40 + Layer.merge(
41 + Layer.provide(ComposioToolkitsRepositoryCached, repository.layer),
42 + BunFileSystem.layer
43 + )
44 + ),
45 + Effect.withConfigProvider(
46 + ConfigProvider.fromMap(new Map([['CACHE_DIR', cacheDir], ...config]))
47 + )
48 + );
49 + });
50 +
51 +describe('ComposioToolkitsRepositoryCached', () => {
52 + it.effect('fetches the toolkit list once per layer, however many callers ask for it', () =>
53 + withCountingRepository(
54 + () => Effect.succeed(testToolkits),
55 + ({ calls }) =>
56 + Effect.gen(function* () {
57 + const repository = yield* ComposioToolkitsRepository;
58 +
59 + const results = yield* Effect.all([
60 + repository.getToolkits(),
61 + repository.getToolkits(),
62 + repository.getToolkits(),
63 + ]);
64 +
65 + expect(results.map(toolkits => toolkits.map(t => t.slug))).toEqual([
66 + ['github', 'gmail'],
67 + ['github', 'gmail'],
68 + ['github', 'gmail'],
69 + ]);
70 + expect(calls()).toBe(1);
71 + })
72 + )
73 + );
74 +
75 + // `it.live`: the stubbed fetch takes real time, so the callers genuinely overlap.
76 + it.live('fetches once even when concurrent callers ask at the same time', () =>
77 + withCountingRepository(
78 + () => Effect.succeed(testToolkits).pipe(Effect.delay('10 millis')),
79 + ({ calls }) =>
80 + Effect.gen(function* () {
81 + const repository = yield* ComposioToolkitsRepository;
82 +
83 + yield* Effect.all(
84 + [repository.getToolkits(), repository.getToolkits(), repository.getToolkits()],
85 + { concurrency: 'unbounded' }
86 + );
87 +
88 + expect(calls()).toBe(1);
89 + })
90 + )
91 + );
92 +
93 + it.effect('surfaces a failed fetch once, and does not retry it', () =>
94 + withCountingRepository(
95 + () => Effect.fail(new HttpServerError({ cause: 'network down', status: 503 })),
96 + ({ calls }) =>
97 + Effect.gen(function* () {
98 + const repository = yield* ComposioToolkitsRepository;
99 +
100 + const first = yield* Effect.either(repository.getToolkits());
101 + const second = yield* Effect.either(repository.getToolkits());
102 +
103 + expect(second).toEqual(first);
104 + // One attempt for both callers: the caching layer must not mistake
105 + // the fetch's own failure for a cache failure worth retrying.
106 + expect(calls()).toBe(1);
107 + })
108 + )
109 + );
110 +
111 + it.effect('serves the cached file when FORCE_USE_CACHE is on, without fetching', () =>
112 + withCountingRepository(
113 + () => Effect.die('the cached file should have answered this'),
114 + ({ calls, cacheDir }) =>
115 + Effect.gen(function* () {
116 + const fs = yield* FileSystem.FileSystem;
117 + yield* fs
118 + .writeFileString(
119 + `${cacheDir}/${CACHE_FILES.toolkits}`,
120 + yield* toolkitsToJSON(testToolkits).pipe(Effect.orDie)
121 + )
122 + .pipe(Effect.orDie);
123 +
124 + const repository = yield* ComposioToolkitsRepository;
125 + const toolkits = yield* repository.getToolkits();
126 +
127 + expect(toolkits.map(t => t.slug)).toEqual(['github', 'gmail']);
128 + expect(calls()).toBe(0);
129 + }),
130 + [['FORCE_USE_CACHE', 'true']]
131 + )
132 + );
133 +});