tool-transform.test.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // ============================================================
  2. // Model Tool Transform Tests
  3. // ============================================================
  4. import { describe, expect, it } from "vitest";
  5. import type { ModelToolContract } from "@zcode/contracts";
  6. import { toAiSdkTools } from "../src/model/index.js";
  7. function createClientToolContract(): ModelToolContract {
  8. return {
  9. name: "Read",
  10. capability: "read_file",
  11. description: "Read a file",
  12. inputSchema: {
  13. type: "object",
  14. properties: {
  15. path: { type: "string" },
  16. },
  17. required: ["path"],
  18. },
  19. outputSchema: { type: "object" },
  20. readOnly: true,
  21. };
  22. }
  23. function readToolJsonSchema(toolValue: unknown): Record<string, unknown> {
  24. return (toolValue as { inputSchema: { jsonSchema: Record<string, unknown> } }).inputSchema
  25. .jsonSchema;
  26. }
  27. describe("model tool transforms", () => {
  28. it("keeps local output unions out of provider tool declarations", () => {
  29. const contract = createClientToolContract();
  30. contract.outputSchema = {
  31. anyOf: [{ type: "string" }, { type: "object", properties: { error: { type: "string" } } }],
  32. };
  33. const tools = toAiSdkTools([contract]);
  34. expect(readToolJsonSchema(tools?.Read)).toEqual(contract.inputSchema);
  35. expect(tools?.Read).not.toHaveProperty("outputSchema");
  36. });
  37. it("opts Anthropic client-side tools out of eager input streaming", () => {
  38. const tools = toAiSdkTools([createClientToolContract()], {
  39. providerKind: "anthropic",
  40. });
  41. expect(tools?.Read).toMatchObject({
  42. providerOptions: {
  43. anthropic: {
  44. eagerInputStreaming: false,
  45. },
  46. },
  47. });
  48. });
  49. it("does not add Anthropic tool provider options for non-Anthropic providers", () => {
  50. const tools = toAiSdkTools([createClientToolContract()], {
  51. providerKind: "openai",
  52. });
  53. expect(
  54. (tools?.Read as { providerOptions?: unknown } | undefined)?.providerOptions,
  55. ).toBeUndefined();
  56. });
  57. it("hoists Kimi K3 local property refs into root defs without mutating the contract", () => {
  58. const contract = createClientToolContract();
  59. contract.inputSchema = {
  60. type: "object",
  61. properties: {
  62. func: {
  63. type: "object",
  64. properties: {
  65. vpc: {
  66. type: "object",
  67. properties: { vpcId: { type: "string" } },
  68. required: ["vpcId"],
  69. },
  70. triggers: {
  71. type: "array",
  72. items: {
  73. type: "object",
  74. properties: { name: { type: "string" } },
  75. required: ["name"],
  76. },
  77. },
  78. },
  79. },
  80. vpc: { $ref: "#/properties/func/properties/vpc" },
  81. triggers: {
  82. type: "array",
  83. items: { $ref: "#/properties/func/properties/triggers/items" },
  84. },
  85. },
  86. };
  87. const tools = toAiSdkTools([contract], {
  88. providerKind: "openai-compatible",
  89. requiresMfjsToolSchema: true,
  90. });
  91. const schema = readToolJsonSchema(tools?.Read);
  92. expect(schema).toMatchObject({
  93. $defs: {
  94. zcode_ref_0: {
  95. type: "object",
  96. properties: { vpcId: { type: "string" } },
  97. required: ["vpcId"],
  98. },
  99. zcode_ref_1: {
  100. type: "object",
  101. properties: { name: { type: "string" } },
  102. required: ["name"],
  103. },
  104. },
  105. properties: {
  106. vpc: { $ref: "#/$defs/zcode_ref_0" },
  107. triggers: {
  108. items: { $ref: "#/$defs/zcode_ref_1" },
  109. },
  110. },
  111. });
  112. expect(contract.inputSchema).toMatchObject({
  113. properties: {
  114. vpc: { $ref: "#/properties/func/properties/vpc" },
  115. triggers: {
  116. items: { $ref: "#/properties/func/properties/triggers/items" },
  117. },
  118. },
  119. });
  120. expect(contract.inputSchema).not.toHaveProperty("$defs");
  121. });
  122. it("keeps local property refs unchanged when MFJS projection is disabled", () => {
  123. const contract = createClientToolContract();
  124. contract.inputSchema = {
  125. type: "object",
  126. properties: {
  127. source: { type: "object", properties: { id: { type: "string" } } },
  128. alias: { $ref: "#/properties/source" },
  129. },
  130. };
  131. const tools = toAiSdkTools([contract], {
  132. providerKind: "openai-compatible",
  133. requiresMfjsToolSchema: false,
  134. });
  135. expect(readToolJsonSchema(tools?.Read)).toBe(contract.inputSchema);
  136. });
  137. it("rejects an unresolvable local ref before request execution", () => {
  138. const contract = createClientToolContract();
  139. contract.inputSchema = {
  140. type: "object",
  141. properties: { alias: { $ref: "#/properties/missing" } },
  142. };
  143. expect(() =>
  144. toAiSdkTools([contract], {
  145. providerKind: "openai-compatible",
  146. requiresMfjsToolSchema: true,
  147. }),
  148. ).toThrowError(/Read.*#\/properties\/missing/u);
  149. });
  150. });