Skip to content

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

jkomyno9d29b2176662
diff --git a/ts/packages/cli/src/bin.ts b/ts/packages/cli/src/bin.ts
index 159b3f834..5fccae7f4 100644
--- a/ts/packages/cli/src/bin.ts
+++ b/ts/packages/cli/src/bin.ts
@@ -13,6 +13,7 @@ import {
ComposioSessionRepository,
ComposioToolkitsRepository,
} from 'src/services/composio-clients';
+import { ComposioToolkitsRepositoryCached } from 'src/services/composio-clients-cached';
import { NodeOs } from 'src/services/node-os';
import { NodeProcess } from 'src/services/node-process';
import { EnvLangDetector } from 'src/services/env-lang-detector';
@@ -53,6 +54,11 @@ export const ComposioToolkitsRepositoryLive = Layer.provide(
Layer.mergeAll(BunFileSystem.layer, NodeOs.Default, ConfigLive)
) satisfies RequiredLayer;
+export const ComposioToolkitsRepositoryCachedLive = Layer.provide(
+ ComposioToolkitsRepositoryCached,
+ ComposioToolkitsRepositoryLive
+) satisfies RequiredLayer;
+
export const UpgradeBinaryLive = Layer.provide(
UpgradeBinary.Default,
Layer.mergeAll(BunFileSystem.layer, FetchHttpClient.layer)
@@ -65,7 +71,7 @@ const layers = Layer.mergeAll(
UpgradeBinaryLive,
ComposioUserContextLive,
ComposioSessionRepositoryLive,
- ComposioToolkitsRepositoryLive,
+ ComposioToolkitsRepositoryCachedLive, // Use the cached layer instead of the regular one
EnvLangDetector.Default,
JsPackageManagerDetector.Default,
BunContext.layer,
diff --git a/ts/packages/cli/src/effects/debug-config.ts b/ts/packages/cli/src/effects/debug-config.ts
index 7bdaa796d..2a7ce87b2 100644
--- a/ts/packages/cli/src/effects/debug-config.ts
+++ b/ts/packages/cli/src/effects/debug-config.ts
@@ -7,8 +7,7 @@ type DEBUG_CONFIG = Config.Config.Wrap<{
/**
* Describe debug configuration keys used at runtime.
- * Keys are read from environment variables (with the `DEBUG_OVERRIDE_<key>` format),
- * or from the user config file (in JSON format).
+ * Keys are read from environment variables (with the `DEBUG_OVERRIDE_<key>` format).
*/
export const DEBUG_OVERRIDE_CONFIG = {
// The local binary used when upgrading the Composio CLI (for debugging).
diff --git a/ts/packages/cli/src/effects/force-config.ts b/ts/packages/cli/src/effects/force-config.ts
new file mode 100644
index 000000000..00618e93b
--- /dev/null
+++ b/ts/packages/cli/src/effects/force-config.ts
@@ -0,0 +1,13 @@
+import { Config, Option } from 'effect';
+
+type FORCE_CONFIG = Config.Config.Wrap<{
+ USE_CACHE: boolean;
+}>;
+
+/**
+ * Describe force configuration keys used at runtime.
+ * Keys are read from environment variables (with the `FORCE_<key>` format).
+ */
+export const FORCE_CONFIG = {
+ USE_CACHE: Config.boolean('FORCE_USE_CACHE').pipe(Config.withDefault(false)),
+} satisfies FORCE_CONFIG;
diff --git a/ts/packages/cli/src/effects/setup-cache-dir.ts b/ts/packages/cli/src/effects/setup-cache-dir.ts
index 21cec81ba..00228dce8 100644
--- a/ts/packages/cli/src/effects/setup-cache-dir.ts
+++ b/ts/packages/cli/src/effects/setup-cache-dir.ts
@@ -15,7 +15,8 @@ export const setupCacheDir = Effect.gen(function* () {
Effect.map(Option.getOrElse(() => path.join(os.homedir, constants.USER_COMPOSIO_DIR)))
);
- yield* fs.makeDirectory(cacheDir, { recursive: true });
+ // Ensure cache directory exists
+ yield* fs.makeDirectory(cacheDir, { recursive: true }).pipe(Effect.catchAll(() => Effect.void));
return cacheDir;
});
diff --git a/ts/packages/cli/src/services/composio-clients-cached.ts b/ts/packages/cli/src/services/composio-clients-cached.ts
new file mode 100644
index 000000000..151dddf4d
--- /dev/null
+++ b/ts/packages/cli/src/services/composio-clients-cached.ts
@@ -0,0 +1,153 @@
+import path from 'node:path';
+import { Effect, Option, ParseResult, Layer } from 'effect';
+import { FileSystem } from '@effect/platform';
+import { BunFileSystem } from '@effect/platform-bun';
+import { setupCacheDir } from 'src/effects/setup-cache-dir';
+import { FORCE_CONFIG } from 'src/effects/force-config';
+import { ComposioToolkitsRepository } from './composio-clients';
+import { NodeOs } from './node-os';
+import { toolkitsFromJSON, toolkitsToJSON } from 'src/models/toolkits';
+import { toolsFromJSON, toolsToJSON } from 'src/models/tools';
+import {
+ TriggerTypesAsEnumsFromJSON,
+ TriggerTypesAsEnumsToJSON,
+ TriggerTypesFromJSON,
+ TriggerTypesToJSON,
+} from 'src/models/trigger-types';
+import { ConfigLive } from './config';
+
+/**
+ * Cache file names for different data types
+ */
+export const CACHE_FILES = {
+ toolkits: 'toolkits.json',
+ tools: 'tools.json',
+ triggerTypesAsEnums: 'trigger-types-as-enums.json',
+ triggerTypes: 'trigger-types.json',
+} as const;
+
+/**
+ * Generic cache helper function that handles both cache read/write with graceful error handling.
+ */
+function createCachedEffect<T, E, R>(
+ cacheFileName: string,
+ decoder: (input: string) => Effect.Effect<T, ParseResult.ParseError>,
+ encoder: (input: T) => Effect.Effect<string, ParseResult.ParseError>,
+ computation: Effect.Effect<T, E, R>
+): Effect.Effect<T, E, R> {
+ // First define the cache-handling function that will run with all required services
+ const cacheEffect = Effect.gen(function* () {
+ const fs = yield* FileSystem.FileSystem;
+ const cacheDir = yield* setupCacheDir;
+
+ const cacheFilePath = path.join(cacheDir, cacheFileName);
+ const cacheFileExists = yield* fs
+ .exists(cacheFilePath)
+ .pipe(Effect.orElse(() => Effect.succeed(false)));
+ const consumeFromCache = yield* FORCE_CONFIG['USE_CACHE'];
+
+ if (consumeFromCache && cacheFileExists) {
+ yield* Effect.logDebug(`Cache HIT for ${cacheFileName}`);
+
+ // Try to read from cache
+ const cachedResult = yield* fs.readFileString(cacheFilePath).pipe(
+ Effect.flatMap(decoder),
+ Effect.asSome,
+ Effect.catchAll(error => {
+ // Log cache read/parse errors but don't fail - fall through to computation
+ return Effect.logWarning(`Failed to read/parse cache ${cacheFilePath}: ${error}`).pipe(
+ Effect.as(Option.none<T>())
+ );
+ })
+ );
+
+ if (Option.isSome(cachedResult)) {
+ return cachedResult.value;
+ }
+ }
+
+ yield* Effect.logDebug(`Cache MISS for ${cacheFileName}`);
+
+ // Fetch from the underlying service
+ const result = yield* computation;
+
+ // Write to cache (best effort: don't fail if cache write fails)
+ yield* encoder(result).pipe(
+ Effect.flatMap(content => fs.writeFileString(cacheFilePath, content)),
+ Effect.catchAll(error =>
+ Effect.logWarning(`Failed to write to cache ${cacheFilePath}: ${error}`)
+ )
+ );
+
+ return result;
+ });
+
+ // Handle any cache errors by falling back to the original computation
+ const handledCacheEffect = cacheEffect.pipe(
+ Effect.catchAll(error =>
+ Effect.logWarning(`Cache operation failed: ${error}`).pipe(Effect.flatMap(() => computation))
+ )
+ );
+
+ // This ensures the returned effect has the same error type as the original computation
+ // by providing all the required cache services
+ return handledCacheEffect.pipe(
+ Effect.provide(Layer.mergeAll(BunFileSystem.layer, NodeOs.Default))
+ ) as Effect.Effect<T, E, R>;
+}
+
+/**
+ * Cached implementation of ComposioToolkitsRepository using the wrapper layer pattern
+ *
+ * This layer adds file-based caching to the repository methods while preserving the
+ * exact same interface and error types.
+ */
+export const ComposioToolkitsRepositoryCached = Layer.effect(
+ ComposioToolkitsRepository,
+ Effect.gen(function* () {
+ const underlyingRepository = yield* ComposioToolkitsRepository;
+
+ // Create the cached implementation that wraps the original implementation
+ return ComposioToolkitsRepository.make({
+ getToolkits: () => {
+ return createCachedEffect(
+ CACHE_FILES.toolkits,
+ toolkitsFromJSON,
+ toolkitsToJSON,
+ underlyingRepository.getToolkits()
+ );
+ },
+
+ getTools: () => {
+ return createCachedEffect(
+ CACHE_FILES.tools,
+ toolsFromJSON,
+ toolsToJSON,
+ underlyingRepository.getTools()
+ );
+ },
+
+ getTriggerTypesAsEnums: () => {
+ return createCachedEffect(
+ CACHE_FILES.triggerTypesAsEnums,
+ TriggerTypesAsEnumsFromJSON,
+ TriggerTypesAsEnumsToJSON,
+ underlyingRepository.getTriggerTypesAsEnums()
+ );
+ },
+
+ getTriggerTypes: (limit: number) => {
+ return createCachedEffect(
+ // We don't care about the limit in the cache file name
+ CACHE_FILES.triggerTypes,
+ TriggerTypesFromJSON,
+ TriggerTypesToJSON,
+ underlyingRepository.getTriggerTypes(limit)
+ );
+ },
+ });
+ })
+).pipe(
+ // Provide the required dependencies for the layer
+ Layer.provide(Layer.mergeAll(BunFileSystem.layer, NodeOs.Default, ConfigLive))
+);
diff --git a/ts/packages/cli/src/services/composio-clients.ts b/ts/packages/cli/src/services/composio-clients.ts
index e953a1bd6..594a9fe16 100644
--- a/ts/packages/cli/src/services/composio-clients.ts
+++ b/ts/packages/cli/src/services/composio-clients.ts
@@ -261,7 +261,7 @@ export class ComposioToolkitsRepository extends Effect.Service<ComposioToolkitsR
// Sort apps by slug.
// TODO: make sure this happens on the server-side.
const orderBySlug = Order.mapInput(Order.string, (app: Toolkit) => app.slug);
- return Array.sort(toolkits, orderBySlug);
+ return Array.sort(toolkits, orderBySlug) as ReadonlyArray<Toolkit>;
})
)
),
@@ -277,7 +277,7 @@ export class ComposioToolkitsRepository extends Effect.Service<ComposioToolkitsR
// Sort apps by slug.
// TODO: make sure this happens on the server-side.
const orderBySlug = Order.mapInput(Order.string, (app: TriggerType) => app.slug);
- return Array.sort(triggerTypes, orderBySlug);
+ return Array.sort(triggerTypes, orderBySlug) as ReadonlyArray<TriggerType>;
})
)
),