fix(typebox): migrate to v1 with extension compat (#3474)
* fix(typebox): migrate to v1 with extension compat Replace AJV-based validation with TypeBox-native validation, keep legacy extension imports working (including @sinclair/typebox/compiler), and restore coercion for serialized/plain JSON schemas. This change closes #3112. * fix(typebox): use canonical imports and harden coercion Switch first-party code to canonical typebox imports while retaining legacy extension aliases in the loader. Remove obsolete runtime codegen guards, expand serialized JSON-schema coercion coverage, and update related tests and fixtures. Fixes #3112. --------- Co-authored-by: Mario Zechner <badlogicgames@gmail.com>
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { Type } from "@sinclair/typebox";
|
||||
import { Type } from "typebox";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { getModel } from "../src/models.js";
|
||||
import { stream } from "../src/stream.js";
|
||||
|
||||
@@ -22,8 +22,8 @@
|
||||
* Fixtures are generated fresh on each run.
|
||||
*/
|
||||
|
||||
import { Type } from "@sinclair/typebox";
|
||||
import { writeFileSync } from "fs";
|
||||
import { Type } from "typebox";
|
||||
import { beforeAll, describe, expect, it } from "vitest";
|
||||
import { getModel } from "../src/models.js";
|
||||
import { completeSimple, getEnvApiKey } from "../src/stream.js";
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Type } from "@sinclair/typebox";
|
||||
import { Type } from "typebox";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { streamGoogleGeminiCli } from "../src/providers/google-gemini-cli.js";
|
||||
import type { Context, Model, ToolCall } from "../src/types.js";
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { readFileSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import { Type } from "@sinclair/typebox";
|
||||
import { Type } from "typebox";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import type { Api, Context, Model, Tool, ToolResultMessage } from "../src/index.js";
|
||||
import { complete, getModel } from "../src/index.js";
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Type } from "@sinclair/typebox";
|
||||
import { Type } from "typebox";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { getEnvApiKey } from "../src/env-api-keys.js";
|
||||
import { getModel } from "../src/models.js";
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Type } from "@sinclair/typebox";
|
||||
import { Type } from "typebox";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { getModel } from "../src/models.js";
|
||||
import { complete } from "../src/stream.js";
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Type } from "@sinclair/typebox";
|
||||
import { Type } from "typebox";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { getModel } from "../src/models.js";
|
||||
import { streamOpenAICompletions } from "../src/providers/openai-completions.js";
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Type } from "@sinclair/typebox";
|
||||
import { Type } from "typebox";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { getModel } from "../src/models.js";
|
||||
import { streamSimple } from "../src/stream.js";
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Type } from "@sinclair/typebox";
|
||||
import { Type } from "typebox";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { getModel } from "../src/models.js";
|
||||
import { complete, getEnvApiKey } from "../src/stream.js";
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { readFileSync } from "node:fs";
|
||||
import { dirname, join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { Type } from "@sinclair/typebox";
|
||||
import type { ResponseFunctionCallOutputItemList } from "openai/resources/responses/responses.js";
|
||||
import { Type } from "typebox";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import type { Api, Context, Model, StreamOptions, Tool, ToolResultMessage } from "../src/index.js";
|
||||
import { complete, getModel } from "../src/index.js";
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { Type } from "@sinclair/typebox";
|
||||
import { type ChildProcess, execSync, spawn } from "child_process";
|
||||
import { readFileSync } from "fs";
|
||||
import { dirname, join } from "path";
|
||||
import { Type } from "typebox";
|
||||
import { fileURLToPath } from "url";
|
||||
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
||||
import { getModel } from "../src/models.js";
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
* Regression test for: https://github.com/badlogic/pi-mono/issues/1022
|
||||
*/
|
||||
|
||||
import { Type } from "@sinclair/typebox";
|
||||
import { Type } from "typebox";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { getModel } from "../src/models.js";
|
||||
import { completeSimple, getEnvApiKey } from "../src/stream.js";
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Type } from "@sinclair/typebox";
|
||||
import { Type } from "typebox";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { getModel } from "../src/models.js";
|
||||
import { complete } from "../src/stream.js";
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Type } from "@sinclair/typebox";
|
||||
import { Type } from "typebox";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { getModel } from "../src/models.js";
|
||||
import { complete } from "../src/stream.js";
|
||||
|
||||
@@ -1,17 +1,41 @@
|
||||
import { Type } from "@sinclair/typebox";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import type { ToolCall } from "../src/types.js";
|
||||
import { Type } from "typebox";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import type { Tool, ToolCall } from "../src/types.js";
|
||||
import { validateToolArguments } from "../src/utils/validation.js";
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
function createToolCallWithPlainSchema(
|
||||
schema: Tool["parameters"],
|
||||
value: unknown,
|
||||
): {
|
||||
tool: Tool;
|
||||
toolCall: ToolCall;
|
||||
} {
|
||||
const tool: Tool = {
|
||||
name: "echo",
|
||||
description: "Echo tool",
|
||||
parameters: {
|
||||
type: "object",
|
||||
properties: {
|
||||
value: schema,
|
||||
},
|
||||
required: ["value"],
|
||||
} as Tool["parameters"],
|
||||
};
|
||||
|
||||
const toolCall: ToolCall = {
|
||||
type: "toolCall",
|
||||
id: "tool-1",
|
||||
name: "echo",
|
||||
arguments: { value },
|
||||
};
|
||||
|
||||
return { tool, toolCall };
|
||||
}
|
||||
|
||||
describe("validateToolArguments", () => {
|
||||
it("falls back to raw arguments without writing to stderr when runtime code generation is blocked", () => {
|
||||
it("still validates when Function constructor is unavailable", () => {
|
||||
const originalFunction = globalThis.Function;
|
||||
const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {});
|
||||
const tool = {
|
||||
const tool: Tool = {
|
||||
name: "echo",
|
||||
description: "Echo tool",
|
||||
parameters: Type.Object({
|
||||
@@ -30,10 +54,63 @@ describe("validateToolArguments", () => {
|
||||
}) as unknown as FunctionConstructor;
|
||||
|
||||
try {
|
||||
expect(validateToolArguments(tool, toolCall)).toEqual(toolCall.arguments);
|
||||
expect(errorSpy).not.toHaveBeenCalled();
|
||||
expect(validateToolArguments(tool, toolCall)).toEqual({ count: 42 });
|
||||
} finally {
|
||||
globalThis.Function = originalFunction;
|
||||
}
|
||||
});
|
||||
|
||||
it("coerces serialized plain JSON schemas with AJV-compatible primitive rules", () => {
|
||||
const passingCases: Array<{
|
||||
schema: Tool["parameters"];
|
||||
input: unknown;
|
||||
expected: unknown;
|
||||
}> = [
|
||||
{ schema: { type: "number" } as Tool["parameters"], input: "42", expected: 42 },
|
||||
{ schema: { type: "number" } as Tool["parameters"], input: true, expected: 1 },
|
||||
{ schema: { type: "number" } as Tool["parameters"], input: null, expected: 0 },
|
||||
{ schema: { type: "integer" } as Tool["parameters"], input: "42", expected: 42 },
|
||||
{ schema: { type: "boolean" } as Tool["parameters"], input: "true", expected: true },
|
||||
{ schema: { type: "boolean" } as Tool["parameters"], input: "false", expected: false },
|
||||
{ schema: { type: "boolean" } as Tool["parameters"], input: 1, expected: true },
|
||||
{ schema: { type: "boolean" } as Tool["parameters"], input: 0, expected: false },
|
||||
{ schema: { type: "string" } as Tool["parameters"], input: null, expected: "" },
|
||||
{ schema: { type: "string" } as Tool["parameters"], input: true, expected: "true" },
|
||||
{ schema: { type: "null" } as Tool["parameters"], input: "", expected: null },
|
||||
{ schema: { type: "null" } as Tool["parameters"], input: 0, expected: null },
|
||||
{ schema: { type: "null" } as Tool["parameters"], input: false, expected: null },
|
||||
{
|
||||
schema: { type: ["number", "string"] } as Tool["parameters"],
|
||||
input: "1",
|
||||
expected: "1",
|
||||
},
|
||||
{
|
||||
schema: { type: ["boolean", "number"] } as Tool["parameters"],
|
||||
input: "1",
|
||||
expected: 1,
|
||||
},
|
||||
];
|
||||
|
||||
for (const testCase of passingCases) {
|
||||
const { tool, toolCall } = createToolCallWithPlainSchema(testCase.schema, testCase.input);
|
||||
expect(validateToolArguments(tool, toolCall)).toEqual({ value: testCase.expected });
|
||||
}
|
||||
});
|
||||
|
||||
it("rejects invalid coercions for serialized plain JSON schemas", () => {
|
||||
const failingCases: Array<{
|
||||
schema: Tool["parameters"];
|
||||
input: unknown;
|
||||
}> = [
|
||||
{ schema: { type: "boolean" } as Tool["parameters"], input: "1" },
|
||||
{ schema: { type: "boolean" } as Tool["parameters"], input: "0" },
|
||||
{ schema: { type: "null" } as Tool["parameters"], input: "null" },
|
||||
{ schema: { type: "integer" } as Tool["parameters"], input: "42.1" },
|
||||
];
|
||||
|
||||
for (const testCase of failingCases) {
|
||||
const { tool, toolCall } = createToolCallWithPlainSchema(testCase.schema, testCase.input);
|
||||
expect(() => validateToolArguments(tool, toolCall)).toThrow("Validation failed");
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user