| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- // ============================================================
- // Model Tool Transform Tests
- // ============================================================
- import { describe, expect, it } from "vitest";
- import type { ModelToolContract } from "@zcode/contracts";
- import { toAiSdkTools } from "../src/model/index.js";
- function createClientToolContract(): ModelToolContract {
- return {
- name: "Read",
- capability: "read_file",
- description: "Read a file",
- inputSchema: {
- type: "object",
- properties: {
- path: { type: "string" },
- },
- required: ["path"],
- },
- outputSchema: { type: "object" },
- readOnly: true,
- };
- }
- function readToolJsonSchema(toolValue: unknown): Record<string, unknown> {
- return (toolValue as { inputSchema: { jsonSchema: Record<string, unknown> } }).inputSchema
- .jsonSchema;
- }
- describe("model tool transforms", () => {
- it("keeps local output unions out of provider tool declarations", () => {
- const contract = createClientToolContract();
- contract.outputSchema = {
- anyOf: [{ type: "string" }, { type: "object", properties: { error: { type: "string" } } }],
- };
- const tools = toAiSdkTools([contract]);
- expect(readToolJsonSchema(tools?.Read)).toEqual(contract.inputSchema);
- expect(tools?.Read).not.toHaveProperty("outputSchema");
- });
- it("opts Anthropic client-side tools out of eager input streaming", () => {
- const tools = toAiSdkTools([createClientToolContract()], {
- providerKind: "anthropic",
- });
- expect(tools?.Read).toMatchObject({
- providerOptions: {
- anthropic: {
- eagerInputStreaming: false,
- },
- },
- });
- });
- it("does not add Anthropic tool provider options for non-Anthropic providers", () => {
- const tools = toAiSdkTools([createClientToolContract()], {
- providerKind: "openai",
- });
- expect(
- (tools?.Read as { providerOptions?: unknown } | undefined)?.providerOptions,
- ).toBeUndefined();
- });
- it("hoists Kimi K3 local property refs into root defs without mutating the contract", () => {
- const contract = createClientToolContract();
- contract.inputSchema = {
- type: "object",
- properties: {
- func: {
- type: "object",
- properties: {
- vpc: {
- type: "object",
- properties: { vpcId: { type: "string" } },
- required: ["vpcId"],
- },
- triggers: {
- type: "array",
- items: {
- type: "object",
- properties: { name: { type: "string" } },
- required: ["name"],
- },
- },
- },
- },
- vpc: { $ref: "#/properties/func/properties/vpc" },
- triggers: {
- type: "array",
- items: { $ref: "#/properties/func/properties/triggers/items" },
- },
- },
- };
- const tools = toAiSdkTools([contract], {
- providerKind: "openai-compatible",
- requiresMfjsToolSchema: true,
- });
- const schema = readToolJsonSchema(tools?.Read);
- expect(schema).toMatchObject({
- $defs: {
- zcode_ref_0: {
- type: "object",
- properties: { vpcId: { type: "string" } },
- required: ["vpcId"],
- },
- zcode_ref_1: {
- type: "object",
- properties: { name: { type: "string" } },
- required: ["name"],
- },
- },
- properties: {
- vpc: { $ref: "#/$defs/zcode_ref_0" },
- triggers: {
- items: { $ref: "#/$defs/zcode_ref_1" },
- },
- },
- });
- expect(contract.inputSchema).toMatchObject({
- properties: {
- vpc: { $ref: "#/properties/func/properties/vpc" },
- triggers: {
- items: { $ref: "#/properties/func/properties/triggers/items" },
- },
- },
- });
- expect(contract.inputSchema).not.toHaveProperty("$defs");
- });
- it("keeps local property refs unchanged when MFJS projection is disabled", () => {
- const contract = createClientToolContract();
- contract.inputSchema = {
- type: "object",
- properties: {
- source: { type: "object", properties: { id: { type: "string" } } },
- alias: { $ref: "#/properties/source" },
- },
- };
- const tools = toAiSdkTools([contract], {
- providerKind: "openai-compatible",
- requiresMfjsToolSchema: false,
- });
- expect(readToolJsonSchema(tools?.Read)).toBe(contract.inputSchema);
- });
- it("rejects an unresolvable local ref before request execution", () => {
- const contract = createClientToolContract();
- contract.inputSchema = {
- type: "object",
- properties: { alias: { $ref: "#/properties/missing" } },
- };
- expect(() =>
- toAiSdkTools([contract], {
- providerKind: "openai-compatible",
- requiresMfjsToolSchema: true,
- }),
- ).toThrowError(/Read.*#\/properties\/missing/u);
- });
- });
|