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 | import { pipe, String, Record, Match } from 'effect'; |
| 2 | import type { Simplify } from 'effect/Types'; |
| 3 | import { Toolkit, Toolkits, ToolkitName } from 'src/models/toolkits'; |
| 4 | import { ToolsAsEnums, Tools, ToolAsEnum, Tool } from 'src/models/tools'; |
| 5 | import { TriggerType, TriggerTypes } from 'src/models/trigger-types'; |
| 6 | import type { ToolkitVersionOverrides } from 'src/effects/toolkit-version-overrides'; |
| 7 | |
| 8 | const startsWith = |
| 9 | <const P extends string>(prefix: P) => |
| 10 | (str: string): str is `${P}${string}` => |
| 11 | str.startsWith(prefix); |
| 12 | |
| 13 | interface CreateToolkitIndexInput { |
| 14 | toolkits: Toolkits; // e.g., [ { slug: 'gmail', ... }] |
| 15 | typeableTools: { withTypes: false; tools: ToolsAsEnums } | { withTypes: true; tools: Tools }; // e.g., [ 'GMAIL_SEND_EMAIL' ] | [ { slug: 'GMAIL_SEND_EMAIL', ... } ] |
| 16 | triggerTypes: TriggerTypes; // e.g., [ { slug: 'GMAIL_NEW_EMAIL', ... } ] |
| 17 | /** Map of lowercase toolkit slug to version (only includes non-'latest' versions) */ |
| 18 | versionMap?: ToolkitVersionOverrides; |
| 19 | } |
| 20 | |
| 21 | export type ToolkitIndexData = Simplify<{ |
| 22 | slug: string; |
| 23 | /** Version override for this toolkit (only present if not 'latest') */ |
| 24 | version?: string; |
| 25 | typeableTools: |
| 26 | | { withTypes: false; value: Record<`${ToolkitName}_${string}`, ToolAsEnum> } |
| 27 | | { withTypes: true; value: Record<`${ToolkitName}_${string}`, Tool> }; |
| 28 | triggerTypes: Record<`${ToolkitName}_${string}`, TriggerType>; |
| 29 | }>; |
| 30 | |
| 31 | export type ToolkitIndex = Record<ToolkitName, ToolkitIndexData>; |
| 32 | |
| 33 | /** |
| 34 | * Creates a toolkit index from the given input. |
| 35 | * Tools and triggerTypes are grouped by their corresponding toolkit name, |
| 36 | * which is found by looking at the prefix of the tool or trigger type. |
| 37 | */ |
| 38 | export function createToolkitIndex(input: CreateToolkitIndexInput): Simplify<ToolkitIndex> { |
| 39 | const { versionMap } = input; |
| 40 | |
| 41 | return pipe( |
| 42 | input, |
| 43 | groupByToolkits, |
| 44 | Record.fromEntries, |
| 45 | Record.mapEntries((value, key) => { |
| 46 | const stripPrefix = String.slice(key.length + 1); |
| 47 | |
| 48 | const { slug } = value.toolkit; |
| 49 | |
| 50 | // Look up version override for this toolkit (lowercase key lookup) |
| 51 | const version = versionMap?.get(String.toLowerCase(slug)); |
| 52 | |
| 53 | const typeableTools = Match.value(value.typeableTools).pipe( |
| 54 | Match.when({ withTypes: true }, ({ withTypes, tools }) => { |
| 55 | return { |
| 56 | withTypes, |
| 57 | value: Record.fromEntries(tools.map(tool => [stripPrefix(tool.slug), tool] as const)), |
| 58 | }; |
| 59 | }), |
| 60 | Match.when({ withTypes: false }, ({ withTypes, tools }) => { |
| 61 | return { |
| 62 | withTypes, |
| 63 | value: Record.fromEntries(tools.map(tool => [stripPrefix(tool), tool] as const)), |
| 64 | }; |
| 65 | }), |
| 66 | Match.exhaustive |
| 67 | ); |
| 68 | |
| 69 | const triggerTypes = value.triggerTypes.map( |
| 70 | triggerType => [stripPrefix(triggerType.slug), triggerType] as const |
| 71 | ); |
| 72 | |
| 73 | return [ |
| 74 | key, |
| 75 | { |
| 76 | slug, |
| 77 | // Only include version if it's not 'latest' (i.e., was overridden) |
| 78 | ...(version ? { version } : {}), |
| 79 | typeableTools, |
| 80 | triggerTypes: Record.fromEntries(triggerTypes), |
| 81 | }, |
| 82 | ] as const; |
| 83 | }) |
| 84 | ); |
| 85 | } |
| 86 | |
| 87 | type ToolkitPrefixedSlug = `${ToolkitName}_${string}`; |
| 88 | |
| 89 | type TriggerTypeWithToolkitPrefix = Omit<TriggerType, 'slug'> & { |
| 90 | slug: ToolkitPrefixedSlug; |
| 91 | }; |
| 92 | |
| 93 | type GroupByToolkitOutput = [ |
| 94 | ToolkitName, |
| 95 | { |
| 96 | toolkit: Toolkit; |
| 97 | typeableTools: |
| 98 | | { withTypes: false; tools: Array<ToolkitPrefixedSlug> } |
| 99 | | { withTypes: true; tools: Array<Tool & { slug: ToolkitPrefixedSlug }> }; |
| 100 | triggerTypes: Array<TriggerTypeWithToolkitPrefix>; |
| 101 | }, |
| 102 | ]; |
| 103 | |
| 104 | const hasToolkitSlugPrefix = <A extends { readonly slug: string }>(toolkitName: ToolkitName) => { |
| 105 | const hasPrefix = startsWith(`${toolkitName}_`); |
| 106 | return (value: A): value is A & { readonly slug: ToolkitPrefixedSlug } => hasPrefix(value.slug); |
| 107 | }; |
| 108 | |
| 109 | const groupByToolkit = |
| 110 | (toolkit: Toolkit) => |
| 111 | ({ |
| 112 | typeableTools, |
| 113 | triggerTypes, |
| 114 | }: Omit<CreateToolkitIndexInput, 'toolkits'>): GroupByToolkitOutput => { |
| 115 | const toolkitName = pipe(toolkit.slug, String.toUpperCase, ToolkitName); |
| 116 | const hasMatchingSlug = hasToolkitSlugPrefix(toolkitName); |
| 117 | const hasMatchingToolSlug = ( |
| 118 | tool: Tool |
| 119 | ): tool is Tool & { readonly slug: ToolkitPrefixedSlug } => hasMatchingSlug(tool); |
| 120 | const hasMatchingTriggerSlug = ( |
| 121 | triggerType: TriggerType |
| 122 | ): triggerType is TriggerTypeWithToolkitPrefix => hasMatchingSlug(triggerType); |
| 123 | |
| 124 | const filteredTypeableTools = Match.value(typeableTools).pipe( |
| 125 | Match.when({ withTypes: true }, ({ withTypes, tools }) => ({ |
| 126 | withTypes, |
| 127 | tools: tools.filter(hasMatchingToolSlug), |
| 128 | })), |
| 129 | Match.when({ withTypes: false }, ({ withTypes, tools }) => ({ |
| 130 | withTypes, |
| 131 | tools: tools.filter((tool: string) => startsWith(`${toolkitName}_`)(tool)), |
| 132 | })), |
| 133 | Match.exhaustive |
| 134 | ); |
| 135 | |
| 136 | return [ |
| 137 | toolkitName, |
| 138 | { |
| 139 | toolkit, |
| 140 | typeableTools: filteredTypeableTools, |
| 141 | triggerTypes: triggerTypes.filter(hasMatchingTriggerSlug), |
| 142 | }, |
| 143 | ]; |
| 144 | }; |
| 145 | |
| 146 | function groupByToolkits({ toolkits, ...rest }: CreateToolkitIndexInput): GroupByToolkitOutput[] { |
| 147 | return toolkits.map(toolkit => groupByToolkit(toolkit)(rest)); |
| 148 | } |