6
import { NodeProcess } from 'src/services/node-process';
7
import {
8
ComposioToolkitsRepository,
9
+ ComposioClientSingleton,
10
getConsumerConnectedToolkits,
11
resolveConsumerProject,
12
} from 'src/services/composio-clients';
13
import { resolveCommandProject } from 'src/services/command-project';
14
+import { resolveToolRouterSessionConnections } from 'src/services/tool-router-session-connections';
15
import { ComposioUserContext } from 'src/services/user-context';
16
17
const CACHE_FILE = 'consumer-short-term-cache.json';
18
const CACHE_TTL_MS = 15 * 60 * 1000;
19
const SEARCH_SESSION_EXTENSION_MS = 5 * 60 * 1000;
20
21
+export type ConsumerToolRouterAuthConfigMappings = {
22
+ readonly authConfigs?: Record<string, string>;
23
+};
24
+
25
type CacheEntry = {
26
readonly toolkits: ReadonlyArray<string>;
27
readonly expiresAt: string;
28
+ readonly toolRouterAuthConfigs?: ConsumerToolRouterAuthConfigMappings;
29
readonly probablyMyCliSessionsByCwdHash?: Record<
30
string,
31
{
128
noAuthToolkits: ReadonlyArray<string>
129
) => [...new Set([...toolkits, ...noAuthToolkits].map(toolkit => toolkit.toLowerCase()))];
130
131
+const normalizeAuthConfigMappings = (
132
+ mappings?: ConsumerToolRouterAuthConfigMappings
133
+): ConsumerToolRouterAuthConfigMappings | undefined => {
134
+ if (!mappings) return undefined;
135
+
136
+ const authConfigs = Object.fromEntries(
137
+ Object.entries(mappings.authConfigs ?? {})
138
+ .map(([toolkit, authConfigId]) => [toolkit.toLowerCase(), authConfigId])
139
+ .filter(([, authConfigId]) => typeof authConfigId === 'string' && authConfigId.length > 0)
140
+ );
141
+ if (Object.keys(authConfigs).length === 0) {
142
+ return undefined;
143
+ }
144
+
145
+ return {
146
+ authConfigs,
147
+ };
148
+};
149
+
150
+const mergeAuthConfigMappings = (params: {
151
+ readonly current?: ConsumerToolRouterAuthConfigMappings;
152
+ readonly next?: ConsumerToolRouterAuthConfigMappings;
153
+}) => {
154
+ const current = normalizeAuthConfigMappings(params.current);
155
+ const next = normalizeAuthConfigMappings(params.next);
156
+ if (!current) return next;
157
+ if (!next) return current;
158
+
159
+ return normalizeAuthConfigMappings({
160
+ authConfigs: {
161
+ ...(current.authConfigs ?? {}),
162
+ ...(next.authConfigs ?? {}),
163
+ },
164
+ });
165
+};
166
+
167
export const getFreshConsumerConnectedToolkitsFromCache = (params: {
168
orgId: string;
169
consumerUserId: string;
185
return Option.some(entry.toolkits);
186
});
187
188
+export const getFreshConsumerToolRouterAuthConfigsFromCache = (params: {
189
+ orgId: string;
190
+ consumerUserId: string;
191
+ toolkits?: ReadonlyArray<string>;
192
+}) =>
193
+ Effect.gen(function* () {
194
+ const disabled = yield* APP_CONFIG.DISABLE_CONNECTED_ACCOUNT_CACHE;
195
+ if (disabled) {
196
+ return Option.none<ConsumerToolRouterAuthConfigMappings>();
197
+ }
198
+ const state = yield* readCache();
199
+ const entry = state[cacheKey(params.orgId, params.consumerUserId)];
200
+ if (!entry) {
201
+ return Option.none<ConsumerToolRouterAuthConfigMappings>();
202
+ }
203
+ const expiresAtMs = Date.parse(entry.expiresAt);
204
+ if (!Number.isFinite(expiresAtMs) || expiresAtMs <= Date.now()) {
205
+ return Option.none<ConsumerToolRouterAuthConfigMappings>();
206
+ }
207
+
208
+ const mappings = normalizeAuthConfigMappings(entry.toolRouterAuthConfigs);
209
+ if (!mappings) {
210
+ return Option.none<ConsumerToolRouterAuthConfigMappings>();
211
+ }
212
+
213
+ if (!params.toolkits || params.toolkits.length === 0) {
214
+ return Option.some(mappings);
215
+ }
216
+
217
+ const requestedToolkits = params.toolkits.map(toolkit => toolkit.toLowerCase());
218
+ const requestedAuthConfigs = requestedToolkits.map(
219
+ toolkit => [toolkit, mappings.authConfigs?.[toolkit]] as const
220
+ );
221
+
222
+ if (requestedAuthConfigs.some(([, authConfigId]) => typeof authConfigId !== 'string')) {
223
+ return Option.none<ConsumerToolRouterAuthConfigMappings>();
224
+ }
225
+
226
+ const filtered = normalizeAuthConfigMappings({
227
+ authConfigs: Object.fromEntries(
228
+ requestedAuthConfigs.map(([toolkit, authConfigId]) => [toolkit, authConfigId as string])
229
+ ),
230
+ });
231
+
232
+ return filtered ? Option.some(filtered) : Option.none<ConsumerToolRouterAuthConfigMappings>();
233
+ });
234
+
235
export const invalidateConsumerConnectedToolkitsCache = () =>
236
Effect.gen(function* () {
237
const fs = yield* FileSystem.FileSystem;
300
return;
301
}
302
213
- const response = yield* getConsumerConnectedToolkits({
214
- baseURL: userContext.data.baseURL,
215
- apiKey,
216
- orgId: scope.orgId,
217
- consumerUserId: scope.consumerUserId,
218
- });
303
+ const clientSingleton = yield* ComposioClientSingleton;
304
+ const directToolkits = yield* Effect.gen(function* () {
305
+ const consumerProject = yield* resolveConsumerProject({
306
+ baseURL: userContext.data.baseURL,
307
+ apiKey,
308
+ orgId: scope.orgId,
309
+ });
310
+ const client = yield* clientSingleton.getFor({
311
+ orgId: scope.orgId,
312
+ projectId: consumerProject.project_id,
313
+ });
314
+ const connectionContext = yield* resolveToolRouterSessionConnections(
315
+ client,
316
+ scope.consumerUserId
317
+ );
318
+ return connectionContext;
319
+ }).pipe(Effect.option);
320
+
321
+ const connectedToolkits =
322
+ Option.isSome(directToolkits) && directToolkits.value.connectedToolkits.length > 0
323
+ ? directToolkits.value.connectedToolkits
324
+ : (yield* getConsumerConnectedToolkits({
325
+ baseURL: userContext.data.baseURL,
326
+ apiKey,
327
+ orgId: scope.orgId,
328
+ consumerUserId: scope.consumerUserId,
329
+ })).toolkits;
330
+
331
const noAuthToolkits = yield* getAlwaysConnectedNoAuthToolkits();
332
const state = yield* readCache();
333
const key = cacheKey(scope.orgId, scope.consumerUserId);
340
yield* writeCache({
341
...state,
342
[key]: {
231
- toolkits: normalizeCachedToolkits(response.toolkits, noAuthToolkits),
343
+ toolkits: normalizeCachedToolkits(connectedToolkits, noAuthToolkits),
344
expiresAt: new Date(Date.now() + CACHE_TTL_MS).toISOString(),
345
+ toolRouterAuthConfigs: mergeAuthConfigMappings({
346
+ current: currentEntry?.toolRouterAuthConfigs,
347
+ next: Option.isSome(directToolkits)
348
+ ? {
349
+ authConfigs: directToolkits.value.authConfigs,
350
+ }
351
+ : undefined,
352
+ }),
353
...searchSessionFields,
354
},
355
});
359
readonly orgId: string;
360
readonly consumerUserId: string;
361
readonly toolkits: ReadonlyArray<string>;
362
+ readonly toolRouterAuthConfigs?: ConsumerToolRouterAuthConfigMappings;
363
}) =>
364
Effect.gen(function* () {
365
const disabled = yield* APP_CONFIG.DISABLE_CONNECTED_ACCOUNT_CACHE;
379
[key]: {
380
toolkits: normalizeCachedToolkits(params.toolkits, noAuthToolkits),
381
expiresAt: new Date(Date.now() + CACHE_TTL_MS).toISOString(),
382
+ toolRouterAuthConfigs: mergeAuthConfigMappings({
383
+ current: currentEntry?.toolRouterAuthConfigs,
384
+ next: params.toolRouterAuthConfigs,
385
+ }),
386
...searchSessionFields,
387
},
388
});