fix(ai): sanitize Mistral tool schemas closes #3361
This commit is contained in:
61
packages/ai/test/mistral-tool-schema.test.ts
Normal file
61
packages/ai/test/mistral-tool-schema.test.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import { Type } from "@sinclair/typebox";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { getModel } from "../src/models.js";
|
||||
import { complete } from "../src/stream.js";
|
||||
import type { Context, Model } from "../src/types.js";
|
||||
|
||||
interface MistralToolPayload {
|
||||
tools?: Array<{
|
||||
type: "function";
|
||||
function: {
|
||||
name: string;
|
||||
parameters: Record<string, unknown>;
|
||||
};
|
||||
}>;
|
||||
}
|
||||
|
||||
describe("Mistral tool schema serialization", () => {
|
||||
it("strips TypeBox symbol keys before the SDK validates tool schemas", async () => {
|
||||
const model: Model<"mistral-conversations"> = {
|
||||
...getModel("mistral", "devstral-medium-latest"),
|
||||
baseUrl: "http://127.0.0.1:9",
|
||||
};
|
||||
const parameters = Type.Object({
|
||||
nested: Type.Object({
|
||||
value: Type.String(),
|
||||
}),
|
||||
});
|
||||
const context: Context = {
|
||||
messages: [{ role: "user", content: "Hi", timestamp: Date.now() }],
|
||||
tools: [
|
||||
{
|
||||
name: "inspect_schema",
|
||||
description: "Inspect the schema",
|
||||
parameters,
|
||||
},
|
||||
],
|
||||
};
|
||||
let capturedPayload: MistralToolPayload | undefined;
|
||||
|
||||
const response = await complete(model, context, {
|
||||
apiKey: "fake-key",
|
||||
onPayload: (payload) => {
|
||||
capturedPayload = payload as MistralToolPayload;
|
||||
return payload;
|
||||
},
|
||||
});
|
||||
|
||||
expect(capturedPayload?.tools).toHaveLength(1);
|
||||
const payloadParameters = capturedPayload?.tools?.[0]?.function.parameters;
|
||||
expect(payloadParameters).toBeDefined();
|
||||
expect(Object.getOwnPropertySymbols(payloadParameters ?? {})).toHaveLength(0);
|
||||
const properties = payloadParameters?.properties;
|
||||
expect(properties).toBeTruthy();
|
||||
expect(Object.getOwnPropertySymbols((properties as Record<string, unknown>) ?? {})).toHaveLength(0);
|
||||
const nested = (properties as Record<string, unknown> | undefined)?.nested;
|
||||
expect(nested).toBeTruthy();
|
||||
expect(Object.getOwnPropertySymbols((nested as Record<string, unknown>) ?? {})).toHaveLength(0);
|
||||
expect(response.stopReason).toBe("error");
|
||||
expect(response.errorMessage).not.toContain("Input validation failed");
|
||||
});
|
||||
});
|
||||
@@ -767,11 +767,12 @@ describe("Generate E2E Tests", () => {
|
||||
|
||||
it("should handle thinking mode", { retry: 3 }, async () => {
|
||||
const llm = getModel("mistral", "magistral-medium-latest");
|
||||
await handleThinking(llm, { reasoningEffort: "medium" });
|
||||
await handleThinking(llm, { promptMode: "reasoning" });
|
||||
});
|
||||
|
||||
it("should handle multi-turn with thinking and tools", { retry: 3 }, async () => {
|
||||
await multiTurn(llm, { reasoningEffort: "medium" });
|
||||
const llm = getModel("mistral", "magistral-medium-latest");
|
||||
await multiTurn(llm, { promptMode: "reasoning" });
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user