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 type { ExecuteToolFn, ToolExecuteResponse } from '@composio/core'; |
| 2 | import type { ToolContext } from 'eve/tools'; |
| 3 | import { extractComposioConnectLinks } from '../auth-links'; |
| 4 | |
| 5 | type MaybePromise<T> = T | Promise<T>; |
| 6 | type Next = () => Promise<ToolExecuteResponse>; |
| 7 | |
| 8 | export interface EveHookControls { |
| 9 | deny(reason: string): ToolExecuteResponse; |
| 10 | } |
| 11 | |
| 12 | export interface EveHookContext extends EveHookControls { |
| 13 | request: { slug: string; args: Record<string, unknown> }; |
| 14 | readonly context: { readonly slug: string; readonly eve: ToolContext }; |
| 15 | } |
| 16 | |
| 17 | export interface EveAuthLinkContext extends EveHookControls { |
| 18 | readonly url: string; |
| 19 | readonly result: ToolExecuteResponse; |
| 20 | readonly context: { readonly slug: string; readonly eve: ToolContext }; |
| 21 | } |
| 22 | |
| 23 | export type EveHook = (ctx: EveHookContext, next: Next) => MaybePromise<ToolExecuteResponse | void>; |
| 24 | export type EveAuthLinkHook = ( |
| 25 | ctx: EveAuthLinkContext, |
| 26 | next: Next |
| 27 | ) => MaybePromise<ToolExecuteResponse | void>; |
| 28 | |
| 29 | export interface EveProviderHooks { |
| 30 | search?: EveHook; |
| 31 | manageConnections?: EveHook; |
| 32 | execute?: EveHook; |
| 33 | remoteWorkbench?: EveHook; |
| 34 | remoteBash?: EveHook; |
| 35 | onAuthLink?: EveAuthLinkHook; |
| 36 | } |
| 37 | |
| 38 | export const denyEveToolCall = (reason: string): ToolExecuteResponse => ({ |
| 39 | data: {}, |
| 40 | error: reason, |
| 41 | successful: false, |
| 42 | }); |
| 43 | |
| 44 | const HOOK_BY_SLUG = { |
| 45 | COMPOSIO_SEARCH_TOOLS: 'search', |
| 46 | COMPOSIO_MANAGE_CONNECTIONS: 'manageConnections', |
| 47 | COMPOSIO_MULTI_EXECUTE_TOOL: 'execute', |
| 48 | COMPOSIO_EXECUTE_TOOL: 'execute', |
| 49 | COMPOSIO_REMOTE_WORKBENCH: 'remoteWorkbench', |
| 50 | COMPOSIO_REMOTE_BASH_TOOL: 'remoteBash', |
| 51 | } as const; |
| 52 | |
| 53 | type MetaSlug = keyof typeof HOOK_BY_SLUG; |
| 54 | |
| 55 | const hookForSlug = (hooks: EveProviderHooks, slug: string): EveHook | undefined => { |
| 56 | const hookName = HOOK_BY_SLUG[slug as MetaSlug]; |
| 57 | if (!hookName) return undefined; |
| 58 | return hooks[hookName]; |
| 59 | }; |
| 60 | |
| 61 | const extractAuthLinks = (result: ToolExecuteResponse): string[] => { |
| 62 | const links = new Set(extractComposioConnectLinks(result.data)); |
| 63 | for (const link of extractComposioConnectLinks(result.error)) { |
| 64 | links.add(link); |
| 65 | } |
| 66 | return Array.from(links); |
| 67 | }; |
| 68 | |
| 69 | const runHook = async <C>( |
| 70 | hook: ((ctx: C, next: Next) => MaybePromise<ToolExecuteResponse | void>) | undefined, |
| 71 | ctx: C, |
| 72 | getDefault: Next |
| 73 | ): Promise<ToolExecuteResponse> => { |
| 74 | if (!hook) return getDefault(); |
| 75 | |
| 76 | let pending: Promise<ToolExecuteResponse> | undefined; |
| 77 | const next: Next = () => { |
| 78 | if (!pending) pending = getDefault(); |
| 79 | return pending; |
| 80 | }; |
| 81 | |
| 82 | const hookResult = await hook(ctx, next); |
| 83 | if (hookResult !== undefined && hookResult !== null) return hookResult; |
| 84 | if (pending) return pending; |
| 85 | return next(); |
| 86 | }; |
| 87 | |
| 88 | export async function applyHooks( |
| 89 | hooks: EveProviderHooks, |
| 90 | slug: string, |
| 91 | args: Record<string, unknown>, |
| 92 | executeTool: ExecuteToolFn, |
| 93 | eveContext: ToolContext |
| 94 | ): Promise<ToolExecuteResponse> { |
| 95 | const context = { slug, eve: eveContext }; |
| 96 | const ctx: EveHookContext = { request: { slug, args }, context, deny: denyEveToolCall }; |
| 97 | const hook = hookForSlug(hooks, slug); |
| 98 | const result = await runHook(hook, ctx, () => executeTool(ctx.request.slug, ctx.request.args)); |
| 99 | |
| 100 | const { onAuthLink } = hooks; |
| 101 | if (!onAuthLink) return result; |
| 102 | |
| 103 | let current = result; |
| 104 | for (const url of extractAuthLinks(result)) { |
| 105 | const previous = current; |
| 106 | current = await runHook( |
| 107 | onAuthLink, |
| 108 | { url, result: previous, context, deny: denyEveToolCall }, |
| 109 | async () => previous |
| 110 | ); |
| 111 | } |
| 112 | return current; |
| 113 | } |