Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | /** |
| 2 | * Last-resort guess: the text before the first underscore. Multi-word toolkit |
| 3 | * slugs (e.g. `google_analytics` in `GOOGLE_ANALYTICS_RUN_REPORT`) cannot be |
| 4 | * recovered from the tool slug alone, so prefer `toolkitFromToolSlug` from |
| 5 | * `src/effects/toolkit-from-tool-slug`, which matches against the known |
| 6 | * toolkit list and only falls back to this guess when that list is |
| 7 | * unavailable. |
| 8 | */ |
| 9 | export const guessToolkitFromToolSlug = (toolSlug: string): string | undefined => { |
| 10 | const idx = toolSlug.indexOf('_'); |
| 11 | if (idx <= 0) return toolSlug.toLowerCase(); |
| 12 | const prefix = toolSlug.slice(0, idx).toLowerCase(); |
| 13 | return prefix === 'composio' ? undefined : prefix; |
| 14 | }; |
| 15 | |
| 16 | /** |
| 17 | * Longest-prefix search over the tool slug's underscore boundaries: for |
| 18 | * `A_B_C_D` the candidates are `a_b_c`, then `a_b`, then `a`, and the first |
| 19 | * candidate present in the known toolkit list wins — so |
| 20 | * `GOOGLE_ANALYTICS_RUN_REPORT` resolves to `google_analytics` even when |
| 21 | * `google` is also a known toolkit. Returns undefined when no candidate is |
| 22 | * known. |
| 23 | */ |
| 24 | export const longestPrefix = ( |
| 25 | toolSlug: string, |
| 26 | knownToolkitSlugs: ReadonlyArray<string> |
| 27 | ): string | undefined => makeLongestPrefixMatcher(knownToolkitSlugs)(toolSlug); |
| 28 | |
| 29 | /** |
| 30 | * {@link longestPrefix} against a list that does not change, with the lookup |
| 31 | * set built once and closed over. The known list runs to a thousand-odd slugs |
| 32 | * and a single command resolves several tool slugs against it, so callers that |
| 33 | * hold onto the returned function pay for the set once instead of per lookup. |
| 34 | */ |
| 35 | export const makeLongestPrefixMatcher = ( |
| 36 | knownToolkitSlugs: ReadonlyArray<string> |
| 37 | ): ((toolSlug: string) => string | undefined) => { |
| 38 | const known = new Set(knownToolkitSlugs.map(slug => slug.toLowerCase())); |
| 39 | |
| 40 | return toolSlug => { |
| 41 | const segments = toolSlug.toLowerCase().split('_'); |
| 42 | for (let end = segments.length - 1; end >= 1; end -= 1) { |
| 43 | const candidate = segments.slice(0, end).join('_'); |
| 44 | if (known.has(candidate)) return candidate; |
| 45 | } |
| 46 | return undefined; |
| 47 | }; |
| 48 | }; |
| 49 | |
| 50 | /** |
| 51 | * Bare `composio`-prefixed slugs are meta tools, not user-linkable toolkits; |
| 52 | * longer matches such as `composio_search` are real toolkits. A `composio` |
| 53 | * match is a resolved answer rather than a miss, so callers must not fall |
| 54 | * through to a wider search on the `undefined` it returns. |
| 55 | */ |
| 56 | export const toolkitFromMatchedPrefix = (match: string): string | undefined => |
| 57 | match === 'composio' ? undefined : match; |
| 58 | |
| 59 | /** |
| 60 | * `longestPrefix` with the toolkit-resolution policy applied on top: the |
| 61 | * `composio` meta guard, and `guessToolkitFromToolSlug` when no candidate is |
| 62 | * known. |
| 63 | */ |
| 64 | export const matchToolkitFromToolSlug = ( |
| 65 | toolSlug: string, |
| 66 | knownToolkitSlugs: ReadonlyArray<string> |
| 67 | ): string | undefined => { |
| 68 | const match = longestPrefix(toolSlug, knownToolkitSlugs); |
| 69 | if (match !== undefined) { |
| 70 | return toolkitFromMatchedPrefix(match); |
| 71 | } |
| 72 | return guessToolkitFromToolSlug(toolSlug); |
| 73 | }; |