|
1 | 1 | import type OpenAI from "openai" |
2 | 2 |
|
| 3 | +import { stripUnsupportedStrictKeywords } from "../../../../../api/transform/strict-tool-schema" |
3 | 4 | import { getNativeTools } from ".." |
4 | 5 |
|
5 | 6 | type FunctionTool = OpenAI.Chat.ChatCompletionFunctionTool |
6 | 7 |
|
7 | | -const UNSUPPORTED_KEYWORDS = new Set([ |
8 | | - "maxItems", |
9 | | - "minimum", |
10 | | - "maximum", |
11 | | - "exclusiveMinimum", |
12 | | - "exclusiveMaximum", |
13 | | - "multipleOf", |
14 | | - "minLength", |
15 | | - "maxLength", |
16 | | - "uniqueItems", |
17 | | - "contains", |
18 | | - "minContains", |
19 | | - "maxContains", |
20 | | - "minProperties", |
21 | | - "maxProperties", |
22 | | -]) |
23 | | - |
24 | | -function collectUnsupportedKeywords(schema: unknown, path = "$"): string[] { |
25 | | - if (typeof schema !== "object" || schema === null) { |
26 | | - return [] |
27 | | - } |
28 | | - |
29 | | - const issues: string[] = [] |
30 | | - for (const [key, value] of Object.entries(schema)) { |
31 | | - if ( |
32 | | - UNSUPPORTED_KEYWORDS.has(key) || |
33 | | - (key === "minItems" && value !== 0 && value !== 1) || |
34 | | - (key === "additionalProperties" && value !== false) |
35 | | - ) { |
36 | | - issues.push(`${path}.${key}`) |
37 | | - } |
38 | | - if (key === "properties" && typeof value === "object" && value !== null && !Array.isArray(value)) { |
39 | | - for (const [propertyName, propertySchema] of Object.entries(value)) { |
40 | | - issues.push(...collectUnsupportedKeywords(propertySchema, `${path}.properties.${propertyName}`)) |
41 | | - } |
42 | | - } else if (Array.isArray(value)) { |
43 | | - issues.push( |
44 | | - ...value.flatMap((entry, index) => collectUnsupportedKeywords(entry, `${path}.${key}[${index}]`)), |
45 | | - ) |
46 | | - } else if (typeof value === "object" && value !== null) { |
47 | | - issues.push(...collectUnsupportedKeywords(value, `${path}.${key}`)) |
48 | | - } |
49 | | - } |
50 | | - return issues |
51 | | -} |
52 | | - |
53 | 8 | describe("native tool strict schema subset", () => { |
54 | 9 | const tools = getNativeTools().filter((tool): tool is FunctionTool => tool.type === "function") |
55 | 10 |
|
56 | 11 | it.each(tools.map((tool) => [tool.function.name, tool] as const))( |
57 | 12 | "keeps %s within the portable strict subset", |
58 | 13 | (_name, tool) => { |
59 | | - expect(collectUnsupportedKeywords(tool.function.parameters)).toEqual([]) |
| 14 | + expect(stripUnsupportedStrictKeywords(tool.function.parameters)).toEqual(tool.function.parameters) |
60 | 15 | }, |
61 | 16 | ) |
62 | 17 | }) |
0 commit comments