contract.test.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. PROVIDER_BUILTIN_TOOL_SERVER_NAME,
  4. ProviderBuiltinToolStreamRewriter,
  5. rewriteProviderBuiltinToolContent,
  6. type SseFrame,
  7. } from "../../src/model/provider-builtin-tool-compat/contract.js";
  8. function frame(data: Record<string, unknown>): {
  9. frame: SseFrame;
  10. parsed: Record<string, unknown>;
  11. } {
  12. return {
  13. frame: {
  14. frame: `event: ${String(data.type)}\ndata: ${JSON.stringify(data)}`,
  15. separator: "\n\n",
  16. },
  17. parsed: data,
  18. };
  19. }
  20. function run(events: Array<Record<string, unknown>>): Array<Record<string, unknown>> {
  21. const rewriter = new ProviderBuiltinToolStreamRewriter();
  22. const out: SseFrame[] = [];
  23. for (const { frame: input, parsed } of events.map(frame))
  24. out.push(...rewriter.push(input, parsed));
  25. out.push(...rewriter.finish());
  26. return out.map((item) => JSON.parse(item.frame.split("data: ")[1]!) as Record<string, unknown>);
  27. }
  28. // SSE 端到端(经 anthropic-stream-compat 与 AI SDK)见 model-execution.test.ts。
  29. describe("provider-builtin-tool-compat contract", () => {
  30. it("rewrites an unknown server tool and its paired bare result into MCP blocks", () => {
  31. const output = run([
  32. {
  33. type: "content_block_start",
  34. index: 0,
  35. content_block: { type: "server_tool_use", id: "call_1", name: "analyze_image", input: {} },
  36. },
  37. { type: "content_block_stop", index: 0 },
  38. {
  39. type: "content_block_start",
  40. index: 1,
  41. content_block: { type: "tool_result", tool_use_id: "call_1", content: "ok" },
  42. },
  43. { type: "content_block_stop", index: 1 },
  44. ]);
  45. expect(output[0]).toMatchObject({
  46. content_block: {
  47. type: "mcp_tool_use",
  48. name: "analyze_image",
  49. server_name: PROVIDER_BUILTIN_TOOL_SERVER_NAME,
  50. },
  51. });
  52. expect(output[2]).toMatchObject({
  53. content_block: {
  54. type: "mcp_tool_result",
  55. tool_use_id: "call_1",
  56. content: [{ type: "text", text: "ok" }],
  57. },
  58. });
  59. });
  60. it("leaves SDK-known server tools untouched", () => {
  61. const start = {
  62. type: "content_block_start",
  63. index: 0,
  64. content_block: { type: "server_tool_use", id: "srv", name: "web_search", input: {} },
  65. };
  66. expect(run([start])).toEqual([start]);
  67. });
  68. it("drops the rendered copies in non-streaming content", () => {
  69. const { changed, content } = rewriteProviderBuiltinToolContent([
  70. { type: "text", text: "**🌐 tool header**" },
  71. { type: "server_tool_use", id: "call_1", name: "analyze_image", input: {} },
  72. { type: "text", text: "**Output:** ok" },
  73. { type: "tool_result", tool_use_id: "call_1", content: "ok" },
  74. { type: "text", text: "done" },
  75. ]);
  76. expect(changed).toBe(true);
  77. expect(content.map((block) => (block as { type: string }).type)).toEqual([
  78. "mcp_tool_use",
  79. "mcp_tool_result",
  80. "text",
  81. ]);
  82. });
  83. const serverToolUse = (index: number) => [
  84. {
  85. type: "content_block_start",
  86. index,
  87. content_block: { type: "server_tool_use", id: "call_1", name: "analyze_image", input: {} },
  88. },
  89. { type: "content_block_stop", index },
  90. ];
  91. const textBlock = (index: number, text: string) => [
  92. { type: "content_block_start", index, content_block: { type: "text", text: "" } },
  93. { type: "content_block_delta", index, delta: { type: "text_delta", text } },
  94. { type: "content_block_stop", index },
  95. ];
  96. const pairedResult = (index: number) => [
  97. {
  98. type: "content_block_start",
  99. index,
  100. content_block: { type: "tool_result", tool_use_id: "call_1", content: "ok" },
  101. },
  102. { type: "content_block_stop", index },
  103. ];
  104. const deltaTexts = (events: Array<Record<string, unknown>>) =>
  105. events
  106. .filter((event) => event.type === "content_block_delta")
  107. .map((event) => (event.delta as { text: string }).text);
  108. it("drops a result copy only when the paired tool_result follows it", () => {
  109. const output = run([
  110. ...serverToolUse(0),
  111. ...textBlock(1, "**Output:** copy"),
  112. ...pairedResult(2),
  113. ]);
  114. expect(deltaTexts(output)).toEqual([]);
  115. expect(output.at(-2)).toMatchObject({ content_block: { type: "mcp_tool_result" } });
  116. });
  117. it.each(["**结论:** 图片包含一辆红色汽车", "**Output:** copy"])(
  118. "keeps bold text %j after a server tool when no paired result follows",
  119. (answer) => {
  120. // 评审探针:加粗开头的正文紧跟服务端调用、服务端没下发配对结果时,必须原样保留。
  121. const output = run([
  122. ...serverToolUse(0),
  123. ...textBlock(1, answer),
  124. ...textBlock(2, "**总结**"),
  125. ]);
  126. expect(deltaTexts(output)).toEqual([answer, "**总结**"]);
  127. const { content } = rewriteProviderBuiltinToolContent([
  128. { type: "server_tool_use", id: "call_1", name: "analyze_image", input: {} },
  129. { type: "text", text: answer },
  130. { type: "text", text: "**总结**" },
  131. ]);
  132. expect(content).toEqual([
  133. expect.objectContaining({ type: "mcp_tool_use" }),
  134. { type: "text", text: answer },
  135. { type: "text", text: "**总结**" },
  136. ]);
  137. },
  138. );
  139. it("keeps bold text when the next block is a result for a different call", () => {
  140. const { content } = rewriteProviderBuiltinToolContent([
  141. { type: "server_tool_use", id: "call_1", name: "analyze_image", input: {} },
  142. { type: "text", text: "**结论:** 图片包含一辆红色汽车" },
  143. { type: "tool_result", tool_use_id: "call_other", content: "x" },
  144. ]);
  145. expect(content).toEqual([
  146. expect.objectContaining({ type: "mcp_tool_use" }),
  147. { type: "text", text: "**结论:** 图片包含一辆红色汽车" },
  148. ]);
  149. });
  150. });