@devries-cameron20 / composio Mirror · read-only
Fork 0

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

next
ts 138 lines 4.39 KB
Raw
1 /**
2 * The schema the provider shows the model, rendered by the real AI SDK, and
3 * the validation the AI SDK runs on tool-call arguments.
4 *
5 * The sibling suites mock `tool()`, so they see what the provider passes but
6 * not the JSON Schema the AI SDK sends. These tests check that schema itself:
7 * the source keywords reach the model, and strict mode sends only what strict
8 * structured outputs accept.
9 */
10 import { asSchema } from 'ai';
11 import { describe, expect, it } from 'vitest';
12 import type { Tool } from '@composio/core';
13 import { VercelProvider } from '../src';
14 import {
15 acceptedFor,
16 loadObjectCases,
17 } from '../../../core/test/fixtures/json-schema-conversion/corpus';
18
19 type Json = Record<string, unknown>;
20
21 const STRICT_STRING_FORMATS = [
22 'date-time',
23 'time',
24 'date',
25 'duration',
26 'email',
27 'hostname',
28 'ipv4',
29 'ipv6',
30 'uuid',
31 ];
32
33 const buildTool = (inputParameters: Json): Tool =>
34 ({
35 slug: 'TEST_TOOL',
36 name: 'Test Tool',
37 description: 'A tool under test',
38 version: '20260901_00',
39 availableVersions: ['20260901_00'],
40 tags: [],
41 inputParameters,
42 }) as unknown as Tool;
43
44 const annotatedTool = buildTool({
45 type: 'object',
46 properties: {
47 file: { type: 'string', format: 'binary', title: 'File' },
48 hex: {
49 type: 'string',
50 contentEncoding: 'base16',
51 contentMediaType: 'application/octet-stream',
52 },
53 link: { type: 'string', format: 'uri' },
54 mail: { type: 'string', format: 'email' },
55 when: { type: 'string', format: 'date-time' },
56 links: { type: 'array', items: { type: 'string', format: 'uri' } },
57 },
58 required: ['file', 'mail'],
59 });
60
61 const wrap = (tool: Tool, strict: boolean) =>
62 asSchema(
63 new VercelProvider({ strict }).wrapTool(tool, async () => ({
64 data: {},
65 error: null,
66 successful: true,
67 })).inputSchema
68 );
69
70 const render = async (tool: Tool, strict: boolean): Promise<Json> =>
71 (await wrap(tool, strict).jsonSchema) as Json;
72
73 /** Every schema node of `schema`, including nested properties, items, and branches. */
74 const nodes = (schema: unknown): Json[] => {
75 if (typeof schema !== 'object' || schema === null) return [];
76 if (Array.isArray(schema)) return schema.flatMap(nodes);
77 const node = schema as Json;
78 return [
79 node,
80 ...Object.values((node.properties as Json | undefined) ?? {}).flatMap(nodes),
81 ...nodes(node.items),
82 ...nodes(node.anyOf),
83 ...nodes(node.oneOf),
84 ...nodes(node.allOf),
85 ];
86 };
87
88 describe('VercelProvider schema sent to the model', () => {
89 it('sends the source keywords a Zod schema cannot carry', async () => {
90 const properties = (await render(annotatedTool, false)).properties as Record<string, Json>;
91
92 expect(properties.file).toEqual({ type: 'string', format: 'binary', title: 'File' });
93 expect(properties.hex).toEqual({
94 type: 'string',
95 contentEncoding: 'base16',
96 contentMediaType: 'application/octet-stream',
97 });
98 expect(properties.mail).toEqual({ type: 'string', format: 'email' });
99 });
100
101 it('carries only the string formats strict structured outputs accept under strict mode', async () => {
102 const schema = await render(annotatedTool, true);
103 const properties = schema.properties as Record<string, Json>;
104 const formats = nodes(schema)
105 .map(node => node.format)
106 .filter(format => format !== undefined);
107
108 expect(properties.link).not.toHaveProperty('format');
109 expect(properties.file).not.toHaveProperty('format');
110 expect(properties.mail).toMatchObject({ format: 'email' });
111 expect(properties.hex).toMatchObject({
112 contentEncoding: 'base16',
113 contentMediaType: 'application/octet-stream',
114 });
115 for (const format of formats) {
116 expect(STRICT_STRING_FORMATS).toContain(format);
117 }
118 });
119
120 it('adds no pattern the source schema does not declare under strict mode', async () => {
121 const patterns = nodes(await render(annotatedTool, true)).filter(node => 'pattern' in node);
122
123 expect(patterns).toEqual([]);
124 });
125 });
126
127 describe('VercelProvider tool-call validation', () => {
128 for (const testCase of loadObjectCases()) {
129 it(`matches the shared corpus on ${testCase.id}`, async () => {
130 const schema = wrap(buildTool(testCase.schema), false);
131
132 for (const instance of testCase.instances) {
133 const result = await schema.validate!(instance.input);
134 expect(result.success, JSON.stringify(instance.input)).toBe(acceptedFor(instance, 'zod'));
135 }
136 });
137 }
138 });