mirror of
https://github.com/luckyyzh/pi-agent-integrated.git
synced 2026-10-03 11:09:34 +00:00
feat: integrate Pi backend and Pi Web
This commit is contained in:
@@ -0,0 +1,129 @@
|
||||
import { Type } from "typebox";
|
||||
import { Compile } from "typebox/compile";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import type { Tool, ToolCall } from "../src/types.ts";
|
||||
import { validateToolArguments } from "../src/utils/validation.ts";
|
||||
|
||||
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("still validates when Function constructor is unavailable", () => {
|
||||
const originalFunction = globalThis.Function;
|
||||
const tool: Tool = {
|
||||
name: "echo",
|
||||
description: "Echo tool",
|
||||
parameters: Type.Object({
|
||||
count: Type.Number(),
|
||||
}),
|
||||
};
|
||||
const toolCall: ToolCall = {
|
||||
type: "toolCall",
|
||||
id: "tool-1",
|
||||
name: "echo",
|
||||
arguments: { count: "42" as unknown as number },
|
||||
};
|
||||
|
||||
globalThis.Function = (() => {
|
||||
throw new EvalError("Code generation from strings disallowed for this context");
|
||||
}) as unknown as FunctionConstructor;
|
||||
|
||||
try {
|
||||
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("accepts null for nullable array schemas with items", () => {
|
||||
const { tool, toolCall } = createToolCallWithPlainSchema(
|
||||
{ type: ["array", "null"], items: { type: "string" } } as Tool["parameters"],
|
||||
null,
|
||||
);
|
||||
// The CSP test above selects TypeBox's process-wide interpreted fallback, so exercise the generated validator explicitly.
|
||||
const generatedCheck = new Function(Compile(tool.parameters).Code())() as (value: unknown) => boolean;
|
||||
|
||||
expect(generatedCheck(toolCall.arguments)).toBe(true);
|
||||
expect(validateToolArguments(tool, toolCall)).toEqual({ value: null });
|
||||
});
|
||||
|
||||
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