openai-compatible-extra-body-wire.test.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
  2. import { generateText } from "ai";
  3. import { describe, expect, it } from "vitest";
  4. import type { ModelInputMessage, ModelToolContract } from "@zcode/contracts";
  5. import { createGenerateTextOptions } from "../src/model/runner-options.js";
  6. import type { ResolvedAiSdkModel } from "../src/model/runner-runtime.js";
  7. import { createTestModelProperties } from "./test-model-format.js";
  8. describe("OpenAI-compatible wire shape", () => {
  9. it("keeps generic OpenAI-compatible tool refs unchanged", async () => {
  10. let capturedBody: Record<string, unknown> | undefined;
  11. const provider = createOpenAICompatible({
  12. apiKey: "fake-ak",
  13. baseURL: "https://api.kimi.com/coding/v1",
  14. name: "Kimi K3 tool schema",
  15. fetch: async (_input, init) => {
  16. if (typeof init?.body !== "string") {
  17. throw new Error("Expected a JSON request body");
  18. }
  19. capturedBody = JSON.parse(init.body) as Record<string, unknown>;
  20. return new Response(
  21. JSON.stringify({
  22. choices: [
  23. {
  24. finish_reason: "stop",
  25. message: { content: "ok", role: "assistant" },
  26. },
  27. ],
  28. id: "chatcmpl-kimi-k3-tools",
  29. model: "kimi-k3",
  30. }),
  31. { headers: { "content-type": "application/json" } },
  32. );
  33. },
  34. });
  35. const manageFunctionsTool: ModelToolContract = {
  36. capability: "cloud_functions",
  37. description: "Manage cloud functions",
  38. inputSchema: {
  39. type: "object",
  40. properties: {
  41. func: {
  42. type: "object",
  43. properties: {
  44. vpc: {
  45. type: "object",
  46. properties: { vpcId: { type: "string" } },
  47. required: ["vpcId"],
  48. },
  49. },
  50. },
  51. vpc: { $ref: "#/properties/func/properties/vpc" },
  52. },
  53. },
  54. name: "manageFunctions",
  55. outputSchema: { type: "object" },
  56. readOnly: false,
  57. };
  58. const options = createGenerateTextOptions({
  59. includeModelIO: false,
  60. request: {
  61. messages: [{ role: "user", content: "hi" }],
  62. tools: [manageFunctionsTool],
  63. },
  64. resolved: {
  65. properties: createTestModelProperties(),
  66. baseURL: "https://api.kimi.com/coding/v1",
  67. model: provider("kimi-k3"),
  68. providerKind: "openai-compatible",
  69. providerId: "moonshot-kimi",
  70. modelId: "kimi-k3",
  71. } as ResolvedAiSdkModel,
  72. statusContext: {
  73. providerId: "moonshot-kimi",
  74. modelId: "kimi-k3",
  75. providerKind: "openai-compatible",
  76. requestId: "req_kimi_k3_tool_schema",
  77. sessionId: "sess_kimi_k3_tool_schema",
  78. traceId: "trace_kimi_k3_tool_schema",
  79. } as never,
  80. });
  81. await generateText(options);
  82. expect(capturedBody).toMatchObject({
  83. tools: [
  84. {
  85. function: {
  86. name: "manageFunctions",
  87. parameters: {
  88. properties: {
  89. vpc: { $ref: "#/properties/func/properties/vpc" },
  90. },
  91. },
  92. },
  93. type: "function",
  94. },
  95. ],
  96. });
  97. expect(manageFunctionsTool.inputSchema).toHaveProperty(
  98. "properties.vpc.$ref",
  99. "#/properties/func/properties/vpc",
  100. );
  101. });
  102. it("does not inject DeepSeek reasoning_content into history", async () => {
  103. const toolCallId = "call_deepseek_empty_reasoning_wire";
  104. const capturedBody = await captureDeepSeekReasoningHistoryBody([
  105. { role: "user", content: "start" },
  106. { role: "assistant", content: "plain answer" },
  107. { role: "user", content: "use a tool" },
  108. {
  109. role: "assistant",
  110. content: [{ type: "reasoning", text: "" }],
  111. toolCalls: [{ id: toolCallId, name: "Read", input: { file_path: "README.md" } }],
  112. },
  113. {
  114. role: "tool",
  115. content: "tool result",
  116. toolCallId,
  117. toolName: "Read",
  118. },
  119. { role: "user", content: "finish" },
  120. ]);
  121. const messages = capturedBody.messages as Record<string, unknown>[];
  122. expect(messages[1]).toEqual({ role: "assistant", content: "plain answer" });
  123. expect(messages[3]).toMatchObject({
  124. role: "assistant",
  125. tool_calls: [{ id: toolCallId, type: "function" }],
  126. });
  127. expect(messages[3]).not.toHaveProperty("reasoning_content");
  128. });
  129. it("serializes a sole empty DeepSeek reasoning block as empty assistant content", async () => {
  130. const capturedBody = await captureDeepSeekReasoningHistoryBody([
  131. { role: "user", content: "start" },
  132. { role: "assistant", content: [{ type: "reasoning", text: "" }] },
  133. { role: "user", content: "finish" },
  134. ]);
  135. const messages = capturedBody.messages as Record<string, unknown>[];
  136. expect(messages[1]).toEqual({ role: "assistant", content: "" });
  137. });
  138. });
  139. async function captureDeepSeekReasoningHistoryBody(
  140. messages: ModelInputMessage[],
  141. ): Promise<Record<string, unknown>> {
  142. const providerName = "DeepSeek reasoning history E2E";
  143. let capturedBody: Record<string, unknown> | undefined;
  144. const provider = createOpenAICompatible({
  145. apiKey: "fake-ak",
  146. baseURL: "https://api.deepseek.example.test/v1",
  147. name: providerName,
  148. fetch: async (_input, init) => {
  149. if (typeof init?.body !== "string") {
  150. throw new Error("Expected a JSON request body");
  151. }
  152. capturedBody = JSON.parse(init.body) as Record<string, unknown>;
  153. return new Response(
  154. JSON.stringify({
  155. choices: [
  156. {
  157. finish_reason: "stop",
  158. message: { content: "ok", role: "assistant" },
  159. },
  160. ],
  161. id: "chatcmpl-deepseek-reasoning-history",
  162. model: "deepseek-v4-pro",
  163. }),
  164. { headers: { "content-type": "application/json" } },
  165. );
  166. },
  167. });
  168. const options = createGenerateTextOptions({
  169. includeModelIO: false,
  170. request: { messages },
  171. resolved: {
  172. properties: createTestModelProperties(),
  173. baseURL: "https://api.deepseek.example.test/v1",
  174. model: provider("deepseek-v4-pro"),
  175. providerKind: "openai-compatible",
  176. providerId: "deepseek",
  177. modelId: "deepseek-v4-pro",
  178. } as ResolvedAiSdkModel,
  179. statusContext: {
  180. providerId: "deepseek",
  181. modelId: "deepseek-v4-pro",
  182. providerKind: "openai-compatible",
  183. requestId: "req_deepseek_reasoning_history_wire",
  184. sessionId: "sess_deepseek_reasoning_history_wire",
  185. traceId: "trace_deepseek_reasoning_history_wire",
  186. } as never,
  187. });
  188. await generateText(options);
  189. if (!capturedBody) {
  190. throw new Error("DeepSeek reasoning history request body was not captured");
  191. }
  192. return capturedBody;
  193. }