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
}
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) => {