tool-transform.test.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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("opts Anthropic client-side tools out of eager input streaming", () => {
  29. const tools = toAiSdkTools([createClientToolContract()], {
  30. providerKind: "anthropic",
  31. });
  32. expect(tools?.Read).toMatchObject({
  33. providerOptions: {
  34. anthropic: {
  35. eagerInputStreaming: false,
  36. },
  37. },
  38. });
  39. });
  40. it("does not add Anthropic tool provider options for non-Anthropic providers", () => {
  41. const tools = toAiSdkTools([createClientToolContract()], {
  42. providerKind: "openai",
  43. });
  44. expect(
  45. (tools?.Read as { providerOptions?: unknown } | undefined)?.providerOptions,
  46. ).toBeUndefined();
  47. });
  48. it("hoists Kimi K3 local property refs into root defs without mutating the contract", () => {
  49. const contract = createClientToolContract();
  50. contract.inputSchema = {
  51. type: "object",
  52. properties: {
  53. func: {
  54. type: "object",
  55. properties: {
  56. vpc: {
  57. type: "object",
  58. properties: { vpcId: { type: "string" } },
  59. required: ["vpcId"],
  60. },
  61. triggers: {
  62. type: "array",
  63. items: {
  64. type: "object",
  65. properties: { name: { type: "string" } },
  66. required: ["name"],
  67. },
  68. },
  69. },
  70. },
  71. vpc: { $ref: "#/properties/func/properties/vpc" },
  72. triggers: {
  73. type: "array",
  74. items: { $ref: "#/properties/func/properties/triggers/items" },
  75. },
  76. },
  77. };
  78. const tools = toAiSdkTools([contract], {
  79. providerKind: "openai-compatible",
  80. requiresMfjsToolSchema: true,
  81. });
  82. const schema = readToolJsonSchema(tools?.Read);
  83. expect(schema).toMatchObject({
  84. $defs: {
  85. zcode_ref_0: {
  86. type: "object",
  87. properties: { vpcId: { type: "string" } },
  88. required: ["vpcId"],
  89. },
  90. zcode_ref_1: {
  91. type: "object",
  92. properties: { name: { type: "string" } },
  93. required: ["name"],
  94. },
  95. },
  96. properties: {
  97. vpc: { $ref: "#/$defs/zcode_ref_0" },
  98. triggers: {
  99. items: { $ref: "#/$defs/zcode_ref_1" },
  100. },
  101. },
  102. });
  103. expect(contract.inputSchema).toMatchObject({
  104. properties: {
  105. vpc: { $ref: "#/properties/func/properties/vpc" },
  106. triggers: {
  107. items: { $ref: "#/properties/func/properties/triggers/items" },
  108. },
  109. },
  110. });
  111. expect(contract.inputSchema).not.toHaveProperty("$defs");
  112. });
  113. it("applies Kimi K3 tool schema compatibility to the 256K variant", () => {
  114. const contract = createClientToolContract();
  115. contract.inputSchema = {
  116. type: "object",
  117. properties: {
  118. source: {
  119. type: "object",
  120. properties: { id: { type: "string" } },
  121. required: ["id"],
  122. },
  123. alias: { $ref: "#/properties/source" },
  124. },
  125. };
  126. const tools = toAiSdkTools([contract], {
  127. providerKind: "openai-compatible",
  128. requiresMfjsToolSchema: true,
  129. });
  130. expect(readToolJsonSchema(tools?.Read)).toMatchObject({
  131. $defs: {
  132. zcode_ref_0: {
  133. type: "object",
  134. properties: { id: { type: "string" } },
  135. required: ["id"],
  136. },
  137. },
  138. properties: {
  139. alias: { $ref: "#/$defs/zcode_ref_0" },
  140. },
  141. });
  142. });
  143. it("keeps local property refs unchanged when MFJS projection is disabled", () => {
  144. const contract = createClientToolContract();
  145. contract.inputSchema = {
  146. type: "object",
  147. properties: {
  148. source: { type: "object", properties: { id: { type: "string" } } },
  149. alias: { $ref: "#/properties/source" },
  150. },
  151. };
  152. const tools = toAiSdkTools([contract], {
  153. providerKind: "openai-compatible",
  154. requiresMfjsToolSchema: false,
  155. });
  156. expect(readToolJsonSchema(tools?.Read)).toBe(contract.inputSchema);
  157. });
  158. it("rejects an unresolvable local ref before request execution", () => {
  159. const contract = createClientToolContract();
  160. contract.inputSchema = {
  161. type: "object",
  162. properties: { alias: { $ref: "#/properties/missing" } },
  163. };
  164. expect(() =>
  165. toAiSdkTools([contract], {
  166. providerKind: "openai-compatible",
  167. requiresMfjsToolSchema: true,
  168. }),
  169. ).toThrowError(/Read.*#\/properties\/missing/u);
  170. });
  171. });