contract.test.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. PROVIDER_BUILTIN_TOOL_SERVER_NAME,
  4. rewriteProviderBuiltinToolContent,
  5. } from "../../src/model/provider-builtin-tool-compat/contract.js";
  6. import { deltaTexts, run, serverToolUse, textBlock } from "./stream-test-helpers.js";
  7. // SSE 端到端(经 anthropic-stream-compat 与 AI SDK)见 model-execution.test.ts。
  8. describe("provider-builtin-tool-compat contract", () => {
  9. it("rewrites an unknown server tool and its paired bare result into MCP blocks", () => {
  10. const output = run([
  11. {
  12. type: "content_block_start",
  13. index: 0,
  14. content_block: { type: "server_tool_use", id: "call_1", name: "analyze_image", input: {} },
  15. },
  16. { type: "content_block_stop", index: 0 },
  17. {
  18. type: "content_block_start",
  19. index: 1,
  20. content_block: { type: "tool_result", tool_use_id: "call_1", content: "ok" },
  21. },
  22. { type: "content_block_stop", index: 1 },
  23. ]);
  24. expect(output[0]).toMatchObject({
  25. content_block: {
  26. type: "mcp_tool_use",
  27. name: "analyze_image",
  28. server_name: PROVIDER_BUILTIN_TOOL_SERVER_NAME,
  29. },
  30. });
  31. expect(output[2]).toMatchObject({
  32. content_block: {
  33. type: "mcp_tool_result",
  34. tool_use_id: "call_1",
  35. content: [{ type: "text", text: "ok" }],
  36. },
  37. });
  38. });
  39. it("leaves SDK-known server tools untouched", () => {
  40. const start = {
  41. type: "content_block_start",
  42. index: 0,
  43. content_block: { type: "server_tool_use", id: "srv", name: "web_search", input: {} },
  44. };
  45. expect(run([start])).toEqual([start]);
  46. });
  47. it("drops the rendered copies in non-streaming content", () => {
  48. const { changed, content } = rewriteProviderBuiltinToolContent([
  49. { type: "text", text: "**🌐 tool header**" },
  50. { type: "server_tool_use", id: "call_1", name: "analyze_image", input: {} },
  51. { type: "text", text: "**Output:** ok" },
  52. { type: "tool_result", tool_use_id: "call_1", content: "ok" },
  53. { type: "text", text: "done" },
  54. ]);
  55. expect(changed).toBe(true);
  56. expect(content.map((block) => (block as { type: string }).type)).toEqual([
  57. "mcp_tool_use",
  58. "mcp_tool_result",
  59. "text",
  60. ]);
  61. });
  62. const pairedResult = (index: number) => [
  63. {
  64. type: "content_block_start",
  65. index,
  66. content_block: { type: "tool_result", tool_use_id: "call_1", content: "ok" },
  67. },
  68. { type: "content_block_stop", index },
  69. ];
  70. it("drops a result copy only when the paired tool_result follows it", () => {
  71. const output = run([
  72. ...serverToolUse(0),
  73. ...textBlock(1, "**Output:** copy"),
  74. ...pairedResult(2),
  75. ]);
  76. expect(deltaTexts(output)).toEqual([]);
  77. expect(output.at(-2)).toMatchObject({ content_block: { type: "mcp_tool_result" } });
  78. });
  79. it.each(["**结论:** 图片包含一辆红色汽车", "**Output:** copy"])(
  80. "keeps bold text %j after a server tool when no paired result follows",
  81. (answer) => {
  82. // 评审探针:加粗开头的正文紧跟服务端调用、服务端没下发配对结果时,必须原样保留。
  83. const output = run([
  84. ...serverToolUse(0),
  85. ...textBlock(1, answer),
  86. ...textBlock(2, "**总结**"),
  87. ]);
  88. expect(deltaTexts(output)).toEqual([answer, "**总结**"]);
  89. const { content } = rewriteProviderBuiltinToolContent([
  90. { type: "server_tool_use", id: "call_1", name: "analyze_image", input: {} },
  91. { type: "text", text: answer },
  92. { type: "text", text: "**总结**" },
  93. ]);
  94. expect(content).toEqual([
  95. expect.objectContaining({ type: "mcp_tool_use" }),
  96. { type: "text", text: answer },
  97. { type: "text", text: "**总结**" },
  98. ]);
  99. },
  100. );
  101. it("keeps bold text when the next block is a result for a different call", () => {
  102. const { content } = rewriteProviderBuiltinToolContent([
  103. { type: "server_tool_use", id: "call_1", name: "analyze_image", input: {} },
  104. { type: "text", text: "**结论:** 图片包含一辆红色汽车" },
  105. { type: "tool_result", tool_use_id: "call_other", content: "x" },
  106. ]);
  107. expect(content).toEqual([
  108. expect.objectContaining({ type: "mcp_tool_use" }),
  109. { type: "text", text: "**结论:** 图片包含一辆红色汽车" },
  110. ]);
  111. });
  112. });