Skip to content

Commit 67b1ddb

Browse files
test(tools): use strict schema sanitizer in native guard
1 parent ee5d919 commit 67b1ddb

1 file changed

Lines changed: 2 additions & 47 deletions

File tree

Lines changed: 2 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,17 @@
11
import type OpenAI from "openai"
22

3+
import { stripUnsupportedStrictKeywords } from "../../../../../api/transform/strict-tool-schema"
34
import { getNativeTools } from ".."
45

56
type FunctionTool = OpenAI.Chat.ChatCompletionFunctionTool
67

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-
538
describe("native tool strict schema subset", () => {
549
const tools = getNativeTools().filter((tool): tool is FunctionTool => tool.type === "function")
5510

5611
it.each(tools.map((tool) => [tool.function.name, tool] as const))(
5712
"keeps %s within the portable strict subset",
5813
(_name, tool) => {
59-
expect(collectUnsupportedKeywords(tool.function.parameters)).toEqual([])
14+
expect(stripUnsupportedStrictKeywords(tool.function.parameters)).toEqual(tool.function.parameters)
6015
},
6116
)
6217
})

0 commit comments

Comments
 (0)