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 { describe, it, expect } from 'vitest'; |
| 2 | import { omitNullToolArguments, toStrictJsonSchema } from '../../src/utils/jsonSchema'; |
| 3 | import { loadStrictCases } from '../fixtures/json-schema-conversion/corpus'; |
| 4 | |
| 5 | /** Structural invariants OpenAI enforces on every node of a strict schema. */ |
| 6 | function assertStrictShape(node: unknown, path = ''): void { |
| 7 | if (Array.isArray(node)) { |
| 8 | node.forEach((item, index) => assertStrictShape(item, `${path}[${index}]`)); |
| 9 | return; |
| 10 | } |
| 11 | if (!node || typeof node !== 'object') return; |
| 12 | const record = node as Record<string, unknown>; |
| 13 | if (record.anyOf !== undefined) { |
| 14 | expect(record.type, `${path}: type beside anyOf`).toBeUndefined(); |
| 15 | } |
| 16 | for (const keyword of [ |
| 17 | 'default', |
| 18 | 'examples', |
| 19 | 'oneOf', |
| 20 | 'patternProperties', |
| 21 | 'allOf', |
| 22 | 'prefixItems', |
| 23 | ]) { |
| 24 | expect(record[keyword], `${path}: ${keyword}`).toBeUndefined(); |
| 25 | } |
| 26 | const isObject = |
| 27 | record.type === 'object' || (Array.isArray(record.type) && record.type.includes('object')); |
| 28 | if (isObject || record.properties !== undefined) { |
| 29 | const properties = (record.properties ?? {}) as Record<string, unknown>; |
| 30 | expect(record.required, `${path}: required`).toEqual(Object.keys(properties)); |
| 31 | expect(record.additionalProperties, `${path}: additionalProperties`).toBe(false); |
| 32 | } |
| 33 | for (const [key, child] of Object.entries(record)) { |
| 34 | if (key === 'enum' || key === 'const') continue; |
| 35 | const childPath = path ? `${path}.${key}` : key; |
| 36 | if ( |
| 37 | (key === 'properties' || key === '$defs' || key === 'definitions') && |
| 38 | child && |
| 39 | typeof child === 'object' |
| 40 | ) { |
| 41 | // Keys of these maps are names, not keywords. |
| 42 | for (const [name, sub] of Object.entries(child as Record<string, unknown>)) { |
| 43 | assertStrictShape(sub, `${childPath}.${name}`); |
| 44 | } |
| 45 | continue; |
| 46 | } |
| 47 | assertStrictShape(child, childPath); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | describe('toStrictJsonSchema (shared corpus)', () => { |
| 52 | for (const testCase of loadStrictCases()) { |
| 53 | it(testCase.id, () => { |
| 54 | const snapshot = JSON.parse(JSON.stringify(testCase.schema)); |
| 55 | const result = toStrictJsonSchema(testCase.schema); |
| 56 | |
| 57 | expect(testCase.schema, 'input mutated').toEqual(snapshot); |
| 58 | if ('unsupported' in testCase.strict) { |
| 59 | expect(result.unsupported.map(({ path, keyword }) => ({ path, keyword }))).toEqual( |
| 60 | testCase.strict.unsupported |
| 61 | ); |
| 62 | } else { |
| 63 | expect(result.unsupported).toEqual([]); |
| 64 | expect(result.schema).toEqual(testCase.strict.schema); |
| 65 | assertStrictShape(result.schema); |
| 66 | const again = toStrictJsonSchema(result.schema); |
| 67 | expect(again.schema, 'not idempotent').toEqual(result.schema); |
| 68 | expect(again.changes).toEqual([]); |
| 69 | } |
| 70 | expect(result.changes.map(({ path, reason }) => ({ path, reason }))).toEqual( |
| 71 | testCase.changes ?? [] |
| 72 | ); |
| 73 | expect(result.totalChanges).toBe(result.changes.length); |
| 74 | for (const { input, output } of testCase.arguments ?? []) { |
| 75 | expect(omitNullToolArguments(input, result.source)).toEqual(output); |
| 76 | } |
| 77 | }); |
| 78 | } |
| 79 | }); |