Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
ts 39 lines 1.56 KB
Raw
1 import { Data } from 'effect';
2 import type {
3 SessionExecuteMetaResponse,
4 SessionExecuteResponse,
5 SessionProxyExecuteResponse,
6 } from '@composio/client/resources/tool-router';
7
8 type ExecuteFamilyResponse =
9 SessionExecuteResponse | SessionExecuteMetaResponse | SessionProxyExecuteResponse;
10
11 type InputRequiredResponse = Extract<ExecuteFamilyResponse, { result_type: 'input_required' }>;
12
13 /**
14 * A tool execution or proxied call that did not run because it needs input
15 * from the user first, for example an approval. The API answers such a call
16 * with `result_type: "input_required"` instead of a result, and the CLI has no
17 * flow to collect and submit the answers.
18 */
19 export class ToolInputRequiredError extends Data.TaggedError('utils/ToolInputRequiredError')<{
20 readonly message: string;
21 readonly inputRequests: InputRequiredResponse['input_requests'];
22 readonly requestState: string | undefined;
23 }> {}
24
25 /**
26 * Builds the error for an `input_required` answer. `subject` names the call
27 * that did not run, for example `Tool GMAIL_SEND_EMAIL`.
28 */
29 export const toolInputRequiredError = (
30 subject: string,
31 response: Pick<InputRequiredResponse, 'input_requests' | 'request_state'>
32 ): ToolInputRequiredError => {
33 const count = Object.keys(response.input_requests).length;
34 return new ToolInputRequiredError({
35 message: `${subject} requires user input before it can run (${count} input request${count === 1 ? '' : 's'}) and was not executed. The CLI cannot answer input requests yet.`,
36 inputRequests: response.input_requests,
37 requestState: response.request_state,
38 });
39 };