@devries-cameron20 / composio Mirror · read-only
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, 04:45 UTC

next
ts 81 lines 3.52 KB
Raw
1 import { Array as Arr, Effect, Option } from 'effect';
2 import {
3 ComposioToolkitsRepository,
4 type ToolkitProjectScope,
5 } from 'src/services/composio-clients';
6 import { ToolkitSlugCatalog } from 'src/services/toolkit-slug-catalog';
7 import { isRemoteCustomToolkitSlug } from 'src/utils/remote-custom-toolkit';
8 import { isMetaToolSlug } from 'src/utils/meta-tool-slugs';
9 import {
10 guessToolkitFromToolSlug,
11 matchToolkitFromToolSlug,
12 toolkitFromMatchedPrefix,
13 } from 'src/utils/toolkit-from-tool-slug';
14
15 /**
16 * Resolves the toolkit slug for a tool or trigger slug.
17 *
18 * No string split can find where the toolkit name ends —
19 * `GOOGLE_ANALYTICS_RUN_REPORT` belongs to `google_analytics`, not `google` —
20 * so this matches the longest known toolkit slug prefix. "Known" is
21 * {@link ToolkitSlugCatalog}: the catalog baked in at build time plus whatever
22 * this machine has learned since, both local and memoized per run, so the
23 * common case costs no network at all.
24 *
25 * Native matches resolve locally. Custom matches are checked against the
26 * active project; unknown slugs also fall through to the catalog, which
27 * is what a toolkit released after this binary looks like — or a custom
28 * toolkit registered in the current project. Everything else
29 * degrades rather than fails: an unreachable catalog still yields the
30 * first-underscore guess.
31 *
32 * Pass the org/project the command resolved as `scope`: custom toolkits are
33 * listed per project, and without it the list follows the project context,
34 * which in consumer mode is no project at all.
35 */
36 export const toolkitFromToolSlug = (
37 toolSlug: string,
38 scope?: ToolkitProjectScope
39 ): Effect.Effect<string | undefined, never, ComposioToolkitsRepository | ToolkitSlugCatalog> =>
40 Effect.gen(function* () {
41 // Meta tools belong to the session rather than to a toolkit, and their
42 // slugs shadow real ones — `COMPOSIO_SEARCH_TOOLS` prefix-matches the
43 // `composio_search` toolkit, which would send users off to link an app
44 // they do not need.
45 if (isMetaToolSlug(toolSlug)) {
46 return undefined;
47 }
48
49 const catalog = yield* ToolkitSlugCatalog;
50 const local = yield* catalog.local;
51
52 const match = local.longestPrefix(toolSlug);
53 if (match !== undefined && !isRemoteCustomToolkitSlug(match)) {
54 return toolkitFromMatchedPrefix(match);
55 }
56
57 // Project-scoped custom toolkits are never in the Composio-managed
58 // catalog, so a miss asks for both. Either one alone is still worth
59 // matching against; only when neither answers does the guess win.
60 const repository = yield* ComposioToolkitsRepository;
61 const catalogs = yield* Effect.all(
62 [repository.getToolkits(), repository.getProjectToolkits(scope)].map(Effect.option),
63 { concurrency: 'unbounded' }
64 );
65 const fetched = Arr.getSomes(catalogs);
66 if (!Arr.isReadonlyArrayNonEmpty(fetched)) {
67 return matchToolkitFromToolSlug(toolSlug, local.slugs);
68 }
69
70 // A successful project lookup is authoritative: machine-wide custom slugs
71 // may belong to another project, including longer or shorter prefixes.
72 // Retain learned slugs only as a best-effort fallback during an outage.
73 const localSlugs = Option.isSome(catalogs[1])
74 ? local.slugs.filter(slug => !isRemoteCustomToolkitSlug(slug))
75 : local.slugs;
76 const allSlugs = [...localSlugs, ...fetched.flat().map(toolkit => toolkit.slug)];
77
78 yield* catalog.remember(allSlugs);
79
80 return matchToolkitFromToolSlug(toolSlug, allSlugs);
81 }).pipe(Effect.catch(() => Effect.succeed(guessToolkitFromToolSlug(toolSlug))));