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 73 lines 2.68 KB
Raw
1 /**
2 * Tool-argument normalization.
3 *
4 * Models (and MCP transports) occasionally emit tool-call arguments as a JSON
5 * *string* instead of an object. `COMPOSIO_MULTI_EXECUTE_TOOL` and Vercel AI
6 * SDK streaming are common triggers. Forwarding that raw string to execution
7 * surfaces opaque downstream errors such as
8 * `tool_use.input: Input should be a valid dictionary`.
9 *
10 * `normalizeToolArguments` is the single coercion every provider routes through
11 * so the behaviour is identical everywhere instead of being re-implemented
12 * (inconsistently) per provider.
13 *
14 * @see https://github.com/ComposioHQ/composio/issues/2406
15 */
16 import { ComposioInvalidToolArgumentsError } from '../errors/ToolErrors';
17
18 /**
19 * Coerce model-supplied tool arguments into a plain object.
20 *
21 * Rules:
22 * - `null` / `undefined` → `{}` (some models send no arguments for no-arg tools).
23 * - A plain object is returned unchanged.
24 * - A string is JSON-parsed; an empty / whitespace-only string becomes `{}`.
25 * - Anything that does not resolve to a plain object (arrays, primitives,
26 * unparseable strings, JSON that parses to a non-object) throws a typed
27 * {@link ComposioInvalidToolArgumentsError} so callers get an actionable
28 * message instead of a raw `SyntaxError` or a silent malformed payload.
29 *
30 * @param input - Raw arguments as received from the model / framework.
31 * @param toolSlug - Optional tool slug, used to enrich the error message.
32 * @returns The normalized arguments as a `Record<string, unknown>`.
33 */
34 export function normalizeToolArguments(input: unknown, toolSlug?: string): Record<string, unknown> {
35 if (input === null || input === undefined) {
36 return {};
37 }
38
39 if (typeof input === 'string') {
40 const trimmed = input.trim();
41 if (trimmed === '') {
42 return {};
43 }
44
45 let parsed: unknown;
46 try {
47 parsed = JSON.parse(trimmed);
48 } catch (cause) {
49 throw new ComposioInvalidToolArgumentsError(
50 `${describeTool(toolSlug)} received arguments as a string that is not valid JSON`,
51 { cause: cause instanceof Error ? cause : undefined }
52 );
53 }
54 return assertPlainObject(parsed, toolSlug);
55 }
56
57 return assertPlainObject(input, toolSlug);
58 }
59
60 function assertPlainObject(value: unknown, toolSlug?: string): Record<string, unknown> {
61 if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
62 return value as Record<string, unknown>;
63 }
64
65 const actual = Array.isArray(value) ? 'array' : typeof value;
66 throw new ComposioInvalidToolArgumentsError(
67 `${describeTool(toolSlug)} expected arguments to be an object, received ${actual}`
68 );
69 }
70
71 function describeTool(toolSlug?: string): string {
72 return toolSlug ? `Tool '${toolSlug}'` : 'Tool';
73 }