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 22, 23:15 UTC
feat(cli): introduce lazy config check (#1708)
Co-authored-by: jkomyno <12381818+jkomyno@users.noreply.github.com>
Alberto Schiabel committed
Jun 26, 2025 at 13:46 UTC
b4b7b1f728565e50466e8530788ce3c84a7d345e
2 files changed
+79
-28
ts/packages/cli/src/services/composio-clients.ts
+79
-27
@@ -1,4 +1,5 @@
1
import { pipe, Data, Effect, Option, Schema, Array, Order } from 'effect';
2
+import type { ConfigError } from 'effect/ConfigError';
3
import { APP_CONFIG } from 'src/effects/app-config';
4
import { Composio as _RawComposioClient } from '@composio/client';
5
import { Toolkit, Toolkits } from 'src/models/toolkits';
@@ -61,12 +62,14 @@ export type TriggerResponse = Schema.Schema.Type<typeof TriggerTypesResponse>;
62
// Utility function for calling the Composio API and decoding its response.
63
// eslint-disable-next-line @typescript-eslint/no-explicit-any
64
const callClient = <T, S extends Schema.Schema<any, any>>(
64
- apiCall: () => Promise<T>,
65
+ clientSingleton: ComposioClientSingleton,
66
+ apiCall: (client: _RawComposioClient) => Promise<T>,
67
responseSchema: S
66
-): Effect.Effect<Schema.Schema.Type<S>, HttpError> =>
67
- pipe(
68
- Effect.tryPromise({
69
- try: apiCall,
68
+): Effect.Effect<Schema.Schema.Type<S>, HttpError | ConfigError> =>
69
+ Effect.gen(function* () {
70
+ const client = yield* clientSingleton.get();
71
+ const json = yield* Effect.tryPromise({
72
+ try: () => apiCall(client),
73
catch: error => {
74
const e = error as Error;
75
return new HttpServerError({
@@ -74,51 +77,88 @@ const callClient = <T, S extends Schema.Schema<any, any>>(
77
message: e.message,
78
});
79
},
77
- }),
78
- Effect.flatMap(json => Schema.decodeUnknown(responseSchema)(json)),
79
- Effect.catchTag('ParseError', e => {
80
- return new HttpDecodingError({
81
- cause: e,
82
- message: e.message,
83
- });
84
- })
85
- );
80
+ });
81
+
82
+ return yield* pipe(
83
+ Schema.decodeUnknown(responseSchema)(json),
84
+ Effect.catchTag('ParseError', e => {
85
+ return new HttpDecodingError({
86
+ cause: e,
87
+ message: e.message,
88
+ });
89
+ })
90
+ );
91
+ });
92
93
/**
94
* Services
95
*/
96
97
+/**
98
+ * Singleton service that lazily accesses `Config` only when needed, which is used to build and provide
99
+ * a raw (uneffectful, Promise-based) Composio client instance.
100
+ */
101
+class ComposioClientSingleton extends Effect.Service<ComposioClientSingleton>()(
102
+ 'services/ComposioClientSingleton',
103
+ {
104
+ accessors: true,
105
+ sync: () => {
106
+ let ref = Option.none<_RawComposioClient>();
107
+
108
+ return {
109
+ get: Effect.fn(function* () {
110
+ if (Option.isSome(ref)) {
111
+ return ref.value;
112
+ }
113
+
114
+ const apiKey = yield* APP_CONFIG['API_KEY'];
115
+ const baseURLOpt = yield* APP_CONFIG['BASE_URL'];
116
+ const baseURL = Option.getOrUndefined(baseURLOpt);
117
+
118
+ yield* Effect.logDebug('Creating raw Composio client...');
119
+ const client = new _RawComposioClient({ apiKey, baseURL });
120
+
121
+ ref = Option.some(client);
122
+ return client;
123
+ }) satisfies () => Effect.Effect<_RawComposioClient, ConfigError, never>,
124
+ };
125
+ },
126
+ dependencies: [],
127
+ }
128
+) {}
129
+
130
// Service that wraps the raw Composio client, which is shared by all client services.
131
class ComposioClientLive extends Effect.Service<ComposioClientLive>()(
132
'services/ComposioClientLive',
133
{
134
effect: Effect.gen(function* () {
96
- const apiKey = yield* APP_CONFIG['API_KEY'];
97
- const baseURLOpt = yield* APP_CONFIG['BASE_URL'];
98
- const baseURL = Option.getOrUndefined(baseURLOpt);
99
-
100
- yield* Effect.logDebug('Creating raw Composio client...');
101
- const client = new _RawComposioClient({ apiKey, baseURL });
135
+ const clientSingleton = yield* ComposioClientSingleton;
136
137
return {
138
toolkits: {
139
/**
140
* Retrieves a comprehensive list of toolkits that are available to the authenticated project.
141
*/
108
- list: () => callClient(() => client.toolkits.list(), ToolkitsResponse),
142
+ list: () =>
143
+ callClient(clientSingleton, client => client.toolkits.list(), ToolkitsResponse),
144
},
145
tools: {
146
/**
147
* Retrieve a list of all available tool enumeration values (tool slugs) for the project.
148
*/
114
- retrieveEnum: () => callClient(() => client.tools.retrieveEnum(), ToolsResponse),
149
+ retrieveEnum: () =>
150
+ callClient(clientSingleton, client => client.tools.retrieveEnum(), ToolsResponse),
151
},
152
triggersTypes: {
153
/**
154
* Retrieves a list of all available trigger type enum values that can be used across the API.
155
*/
156
retrieveEnum: () =>
121
- callClient(() => client.triggersTypes.retrieveEnum(), TriggerTypesResponse),
157
+ callClient(
158
+ clientSingleton,
159
+ client => client.triggersTypes.retrieveEnum(),
160
+ TriggerTypesResponse
161
+ ),
162
},
163
v3: {
164
cli: {
@@ -126,24 +166,36 @@ class ComposioClientLive extends Effect.Service<ComposioClientLive>()(
166
* Generates a new CLI session with a random 6-character code.
167
*/
168
createSession: () =>
129
- callClient(() => client.v3.cli.createSession(), CliCreateSessionResponse),
169
+ callClient(
170
+ clientSingleton,
171
+ client => client.v3.cli.createSession(),
172
+ CliCreateSessionResponse
173
+ ),
174
175
/**
176
* Retrieves the current state of a CLI session using either the session ID (UUID) or the 6-character code.
177
*/
178
retrieveSession: (session: { id: string }) =>
135
- callClient(() => client.v3.cli.retrieveSession(session), CliRetrieveSessionResponse),
179
+ callClient(
180
+ clientSingleton,
181
+ client => client.v3.cli.retrieveSession(session),
182
+ CliRetrieveSessionResponse
183
+ ),
184
185
/**
186
* Links a pending CLI session to the currently authenticated user.
187
*/
188
linkSession: (session: { id: string }) =>
141
- callClient(() => client.v3.cli.linkSession(session), CliLinkSessionResponse),
189
+ callClient(
190
+ clientSingleton,
191
+ client => client.v3.cli.linkSession(session),
192
+ CliLinkSessionResponse
193
+ ),
194
},
195
},
196
};
197
}),
146
- dependencies: [],
198
+ dependencies: [ComposioClientSingleton.Default],
199
}
200
) {}
201
ts/packages/cli/src/services/user-config.ts
-1
@@ -36,7 +36,6 @@ const ConfigProviderLive = Effect.gen(function* () {
36
const cacheDir = yield* setupCacheDir;
37
const jsonUserConfigPath = path.join(cacheDir, constants.USER_CONFIG_FILE_NAME);
38
const configProviderFromUserFile = yield* configProviderFromLazyJson(jsonUserConfigPath);
39
- // const configProviderFromUserFile = yield* configProviderFromLazyJson('/tmp');
39
40
// start by reading from env vars
41
const configProvider = ConfigProvider.fromEnv()