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 | import { |
| 2 | BaseAgenticProvider, |
| 3 | type ExecuteToolFn, |
| 4 | type McpServerGetResponse, |
| 5 | type McpUrlResponse, |
| 6 | normalizeToolArguments, |
| 7 | type Tool as ComposioTool, |
| 8 | type ToolExecuteResponse, |
| 9 | } from '@composio/core'; |
| 10 | import { defineTool } from '@earendil-works/pi-coding-agent'; |
| 11 | import type { Static } from 'typebox'; |
| 12 | |
| 13 | import { defaultFormatResult, toPiErrorResult, toPiResult } from './results'; |
| 14 | import { objectInputSchema } from './schemas'; |
| 15 | import { createPiSessionTools } from './session-tools'; |
| 16 | import type { |
| 17 | PiComposioSessionLike, |
| 18 | PiProviderOptions, |
| 19 | PiSessionToolCapabilities, |
| 20 | PiSessionToolOptions, |
| 21 | PiTool, |
| 22 | PiToolCollection, |
| 23 | } from './types'; |
| 24 | |
| 25 | /** |
| 26 | * Provider for integrating Composio tools with Pi SDK custom tools. |
| 27 | */ |
| 28 | export class PiProvider extends BaseAgenticProvider< |
| 29 | PiToolCollection, |
| 30 | PiTool, |
| 31 | McpServerGetResponse |
| 32 | > { |
| 33 | readonly name = 'pi'; |
| 34 | |
| 35 | constructor(private readonly options: PiProviderOptions = {}) { |
| 36 | super(); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Wrap a concrete Composio tool as a Pi custom tool definition. |
| 41 | */ |
| 42 | wrapTool(composioTool: ComposioTool, executeTool: ExecuteToolFn): PiTool { |
| 43 | const formatter = this.options.formatResult ?? defaultFormatResult; |
| 44 | const catchErrors = this.options.catchErrors ?? true; |
| 45 | const schema = objectInputSchema(composioTool.inputParameters); |
| 46 | |
| 47 | return defineTool({ |
| 48 | name: composioTool.slug, |
| 49 | label: `${this.options.labelPrefix ?? 'Composio'}: ${composioTool.name ?? composioTool.slug}`, |
| 50 | description: composioTool.description ?? `Execute ${composioTool.slug} with Composio.`, |
| 51 | promptSnippet: `Use ${composioTool.slug} for ${composioTool.description ?? composioTool.name ?? 'this Composio action'}.`, |
| 52 | parameters: schema, |
| 53 | ...(this.options.executionMode ? { executionMode: this.options.executionMode } : {}), |
| 54 | prepareArguments: args => |
| 55 | normalizeToolArguments(args, composioTool.slug) as Static<typeof schema>, |
| 56 | execute: async (_toolCallId, params) => { |
| 57 | try { |
| 58 | const args = normalizeToolArguments(params, composioTool.slug); |
| 59 | const result = await executeTool(composioTool.slug, args); |
| 60 | return toPiResult(result, formatter, { |
| 61 | slug: composioTool.slug, |
| 62 | error: (result as ToolExecuteResponse | undefined)?.error, |
| 63 | }); |
| 64 | } catch (error) { |
| 65 | if (!catchErrors) throw error; |
| 66 | return toPiErrorResult(error, formatter, { slug: composioTool.slug }); |
| 67 | } |
| 68 | }, |
| 69 | }); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Wrap multiple concrete Composio tools as Pi custom tools. |
| 74 | */ |
| 75 | wrapTools(tools: ComposioTool[], executeTool: ExecuteToolFn): PiToolCollection { |
| 76 | return tools.map(tool => this.wrapTool(tool, executeTool)); |
| 77 | } |
| 78 | |
| 79 | createSessionTools< |
| 80 | TSearchResult = unknown, |
| 81 | TExecuteResult = unknown, |
| 82 | TState = unknown, |
| 83 | TAuthorizeResult = unknown, |
| 84 | TToolkitStates = unknown, |
| 85 | >( |
| 86 | capabilities: PiSessionToolCapabilities< |
| 87 | TSearchResult, |
| 88 | TExecuteResult, |
| 89 | TState, |
| 90 | TAuthorizeResult, |
| 91 | TToolkitStates |
| 92 | > |
| 93 | ): PiToolCollection; |
| 94 | createSessionTools< |
| 95 | TSearchResult = unknown, |
| 96 | TExecuteResult = unknown, |
| 97 | TToolkitStates = unknown, |
| 98 | TAuthorizeResult = unknown, |
| 99 | >( |
| 100 | session: PiComposioSessionLike<TSearchResult, TExecuteResult, TToolkitStates, TAuthorizeResult>, |
| 101 | options?: PiSessionToolOptions |
| 102 | ): PiToolCollection; |
| 103 | /** |
| 104 | * Create Slack-bot-style dynamic Composio helpers. |
| 105 | * |
| 106 | * Prefer passing capabilities (`search`, `execute`, `connections`, `hooks`) |
| 107 | * so app code owns auth, interception, and shared/service-session routing. |
| 108 | * Passing a native Tool Router session is also supported; connection |
| 109 | * management uses `session.toolkits()` + `session.authorize()` when present |
| 110 | * and never executes `COMPOSIO_MANAGE_CONNECTIONS` internally. |
| 111 | */ |
| 112 | createSessionTools( |
| 113 | input: PiComposioSessionLike | PiSessionToolCapabilities, |
| 114 | options: PiSessionToolOptions = {} |
| 115 | ): PiToolCollection { |
| 116 | return createPiSessionTools(input, this.options, options); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Transform MCP URL responses into Pi-compatible standard URL entries. |
| 121 | */ |
| 122 | wrapMcpServerResponse(data: McpUrlResponse): McpServerGetResponse { |
| 123 | return data.map(item => ({ |
| 124 | url: new URL(item.url), |
| 125 | name: item.name, |
| 126 | })) as McpServerGetResponse; |
| 127 | } |
| 128 | } |