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 { |
| 2 | ComposioToolInputRequiredError, |
| 3 | type ToolRouterInputRequest, |
| 4 | type ToolRouterInputResponse, |
| 5 | type ToolRouterSessionExecuteOptions, |
| 6 | } from '@composio/core'; |
| 7 | import { z } from 'zod'; |
| 8 | |
| 9 | export type AskUser = (question: ToolRouterInputRequest) => Promise<ToolRouterInputResponse>; |
| 10 | |
| 11 | // This terminal example supports the decision forms used for tool approvals. |
| 12 | // A general elicitation UI should render and validate the whole JSON Schema. |
| 13 | const approvalSchema = z.object({ |
| 14 | type: z.literal('object'), |
| 15 | properties: z.object({ |
| 16 | decision: z.object({ |
| 17 | type: z.literal('string'), |
| 18 | oneOf: z.array(z.object({ const: z.string(), title: z.string().optional() })).optional(), |
| 19 | enum: z.array(z.string()).optional(), |
| 20 | }), |
| 21 | }), |
| 22 | }); |
| 23 | |
| 24 | export async function askForApproval( |
| 25 | question: ToolRouterInputRequest, |
| 26 | read: (prompt: string) => Promise<string>, |
| 27 | show: (message: string) => void = console.log |
| 28 | ): Promise<ToolRouterInputResponse> { |
| 29 | if (question.mode !== 'form') throw new Error('This example supports form elicitation only'); |
| 30 | const schema = approvalSchema.parse(question.requestedSchema); |
| 31 | const field = schema.properties.decision; |
| 32 | const choices = field.oneOf?.map(choice => choice.const) ?? field.enum ?? []; |
| 33 | if (choices.length === 0) throw new Error('Approval form has no decision choices'); |
| 34 | show(question.message); |
| 35 | while (true) { |
| 36 | const answer = (await read(`Decision (${choices.join('/')}; decline/cancel): `)).trim(); |
| 37 | if (answer === 'decline' || answer === 'cancel') return { action: answer }; |
| 38 | if (choices.includes(answer)) return { action: 'accept', content: { decision: answer } }; |
| 39 | show('Choose one of the displayed answers.'); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | export async function runWithApprovals<T>( |
| 44 | execute: (options: ToolRouterSessionExecuteOptions) => Promise<T>, |
| 45 | askUser: AskUser |
| 46 | ): Promise<T> { |
| 47 | let answers: ToolRouterSessionExecuteOptions = {}; |
| 48 | while (true) { |
| 49 | try { |
| 50 | return await execute(answers); |
| 51 | } catch (error) { |
| 52 | // Only input-required responses may resume. Other errors must reach the caller. |
| 53 | if (!(error instanceof ComposioToolInputRequiredError)) throw error; |
| 54 | const inputResponses: NonNullable<ToolRouterSessionExecuteOptions['inputResponses']> = {}; |
| 55 | for (const [id, question] of Object.entries(error.inputRequests)) { |
| 56 | inputResponses[id] = await askUser(question); |
| 57 | } |
| 58 | answers = { inputResponses, requestState: error.requestState }; |
| 59 | } |
| 60 | } |
| 61 | } |