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
| 1 | /** |
| 2 | * @example |
| 3 | * |
| 4 | * ```python |
| 5 | * class ABLY: |
| 6 | * """Map of Composio's ABLY toolkit.""" |
| 7 | * |
| 8 | * slug: str = "ably" |
| 9 | * |
| 10 | * class tools: |
| 11 | * BATCH_PRESENCE = "ABLY_BATCH_PRESENCE" |
| 12 | * BATCH_PRESENCE_HISTORY = "ABLY_BATCH_PRESENCE_HISTORY" |
| 13 | * CREATE_CHANNEL = "ABLY_CREATE_CHANNEL" |
| 14 | * DELETE_CHANNEL_SUBSCRIPTION = "ABLY_DELETE_CHANNEL_SUBSCRIPTION" |
| 15 | * |
| 16 | * class triggers: |
| 17 | * NEW_GMAIL_MESSAGE = { |
| 18 | * 'config': { |
| 19 | * # ... |
| 20 | * }, |
| 21 | * description: "Triggers when a new message is received in Gmail.", |
| 22 | * instructions: |
| 23 | * "\n **Instructions for Setting Up the Trigger:**\n\n - Ensure that the Gmail API is enabled for your Google account.\n - Provide the user ID (usually 'me' for the authenticated user).\n - Optionally, provide label IDs to filter messages.\n ", |
| 24 | * name: "New Gmail Message Received Trigger", |
| 25 | * payload: { |
| 26 | * # ... |
| 27 | * }, |
| 28 | * slug: 'GMAIL_NEW_GMAIL_MESSAGE', |
| 29 | * type: 'poll', |
| 30 | * } |
| 31 | * ``` |
| 32 | */ |
| 33 | |
| 34 | import { Match, pipe, Record } from 'effect'; |
| 35 | import type { ToolkitName } from 'src/models/toolkits'; |
| 36 | import type { ToolkitIndex, ToolkitIndexData } from 'src/generation/create-toolkit-index'; |
| 37 | import type { SourceFile } from 'src/generation/types'; |
| 38 | import type { TriggerType } from 'src/models/trigger-types'; |
| 39 | |
| 40 | /** |
| 41 | * Converts a JavaScript value to Python dictionary/literal syntax |
| 42 | */ |
| 43 | function jsToPython(value: unknown, indent = 0): string { |
| 44 | const spacePad = ' '.repeat(indent); |
| 45 | |
| 46 | if (value === null) { |
| 47 | return 'None'; |
| 48 | } |
| 49 | |
| 50 | if (value === undefined) { |
| 51 | return 'None'; |
| 52 | } |
| 53 | |
| 54 | if (typeof value === 'string') { |
| 55 | // Escape quotes and return as Python string |
| 56 | return JSON.stringify(value); |
| 57 | } |
| 58 | |
| 59 | if (typeof value === 'number') { |
| 60 | return value.toString(); |
| 61 | } |
| 62 | |
| 63 | if (typeof value === 'boolean') { |
| 64 | return value ? 'True' : 'False'; |
| 65 | } |
| 66 | |
| 67 | if (Array.isArray(value)) { |
| 68 | if (value.length === 0) { |
| 69 | return '[]'; |
| 70 | } |
| 71 | const items = value.map(item => jsToPython(item, indent + 2)); |
| 72 | return `[${items.join(', ')}]`; |
| 73 | } |
| 74 | |
| 75 | if (typeof value === 'object' && value !== null) { |
| 76 | const entries = Object.entries(value); |
| 77 | if (entries.length === 0) { |
| 78 | return '{}'; |
| 79 | } |
| 80 | |
| 81 | const formattedEntries = entries.map(([key, val]) => { |
| 82 | const formattedKey = JSON.stringify(key); |
| 83 | const formattedValue = jsToPython(val, indent + 2); |
| 84 | return `${spacePad} ${formattedKey}: ${formattedValue}`; |
| 85 | }); |
| 86 | |
| 87 | return `{\n${formattedEntries.join(',\n')}\n${spacePad}}`; |
| 88 | } |
| 89 | |
| 90 | // Fallback for unknown types |
| 91 | return JSON.stringify(value); |
| 92 | } |
| 93 | |
| 94 | const PYTHON_IDENTIFIER_PATTERN = /^[A-Za-z_][A-Za-z0-9_]*$/; |
| 95 | |
| 96 | function orderPythonAssignmentEntries<T>(entries: Array<[string, T]>): Array<[string, T]> { |
| 97 | const fallbackEntries: Array<[string, T]> = []; |
| 98 | const identifierEntries: Array<[string, T]> = []; |
| 99 | |
| 100 | for (const entry of entries) { |
| 101 | if (PYTHON_IDENTIFIER_PATTERN.test(entry[0])) { |
| 102 | identifierEntries.push(entry); |
| 103 | } else { |
| 104 | fallbackEntries.push(entry); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | return [...fallbackEntries, ...identifierEntries]; |
| 109 | } |
| 110 | |
| 111 | function generatePythonAssignment(name: string, valueLiteral: string, spacing: number): string { |
| 112 | const spacePad = ' '.repeat(spacing); |
| 113 | |
| 114 | if (PYTHON_IDENTIFIER_PATTERN.test(name)) { |
| 115 | return `${spacePad}${name} = ${valueLiteral}`; |
| 116 | } |
| 117 | |
| 118 | return `${spacePad}locals()[${jsToPython(name)}] = ${valueLiteral}`; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Generates Python dictionary syntax for a trigger type object |
| 123 | */ |
| 124 | function generateTriggerTypePythonDict(triggerType: TriggerType, indent = 0): string { |
| 125 | return jsToPython(triggerType, indent); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Generates a list of Python source files that should be written to disk by the caller. |
| 130 | */ |
| 131 | export function generatePythonToolkitSources(banner: string) { |
| 132 | return (index: ToolkitIndex): Array<SourceFile> => { |
| 133 | const toolkitSources = pipe( |
| 134 | index, |
| 135 | Record.mapEntries(generatePythonToolkitSource(banner)), |
| 136 | Record.toEntries |
| 137 | ); |
| 138 | |
| 139 | return toolkitSources; |
| 140 | }; |
| 141 | } |
| 142 | |
| 143 | function generatePythonToolkitSource(banner: string) { |
| 144 | return (toolkit: ToolkitIndexData, toolkitName: ToolkitName): SourceFile => { |
| 145 | const filename = `${toolkit.slug}.py`; |
| 146 | |
| 147 | const toolsEntries = (spacing: number) => { |
| 148 | if (Object.keys(toolkit.typeableTools.value).length > 0) { |
| 149 | const tools = Match.value(toolkit.typeableTools).pipe( |
| 150 | Match.when({ withTypes: true }, ({ value }) => |
| 151 | Record.mapEntries(value, (toolValue, toolName) => [toolName, toolValue.slug]) |
| 152 | ), |
| 153 | Match.when({ withTypes: false }, ({ value }) => |
| 154 | Record.mapEntries(value, (toolValue, toolName) => [toolName, toolValue]) |
| 155 | ), |
| 156 | Match.exhaustive |
| 157 | ); |
| 158 | |
| 159 | return orderPythonAssignmentEntries(Object.entries(tools)) |
| 160 | .map(([toolName, toolValue]) => |
| 161 | generatePythonAssignment(toolName, jsToPython(toolValue), spacing) |
| 162 | ) |
| 163 | .join('\n'); |
| 164 | } |
| 165 | |
| 166 | return `${' '.repeat(spacing)}pass`; |
| 167 | }; |
| 168 | |
| 169 | const triggerTypesEntries = (spacing: number) => { |
| 170 | if (Record.size(toolkit.triggerTypes) > 0) { |
| 171 | return orderPythonAssignmentEntries(Object.entries(toolkit.triggerTypes)) |
| 172 | .map(([triggerName, triggerValue]) => { |
| 173 | const pythonDict = generateTriggerTypePythonDict(triggerValue, spacing); |
| 174 | return generatePythonAssignment(triggerName, pythonDict, spacing); |
| 175 | }) |
| 176 | .join('\n'); |
| 177 | } |
| 178 | |
| 179 | return `${' '.repeat(spacing)}pass`; |
| 180 | }; |
| 181 | |
| 182 | // Build version comment if version override was used |
| 183 | const versionComment = toolkit.version ? `# @toolkit-version: ${toolkit.version}\n` : ''; |
| 184 | |
| 185 | const filesource = `# ${banner}. |
| 186 | ${versionComment} |
| 187 | class ${toolkitName}: |
| 188 | """Map of Composio's ${toolkitName} toolkit.""" |
| 189 | |
| 190 | slug: str = "${toolkit.slug}" |
| 191 | |
| 192 | class tools: |
| 193 | ${toolsEntries(8)} |
| 194 | |
| 195 | class triggers: |
| 196 | ${triggerTypesEntries(8)} |
| 197 | `; |
| 198 | |
| 199 | return [filename, filesource]; |
| 200 | }; |
| 201 | } |