contract.test.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { describe, expect, it } from "vitest";
  2. import type { ModelToolCall } from "@zcode/contracts";
  3. import {
  4. createProviderToolRoundEntries,
  5. hasProviderToolRoundContent,
  6. isProviderExecutedToolPartMetadata,
  7. providerToolOutputText,
  8. providerToolRoundsCompactGroupKey,
  9. withCompactGroupKey,
  10. type ProviderToolRound,
  11. } from "../../src/runtime/provider-tool-rounds/contract.js";
  12. const toolCall: ModelToolCall = {
  13. id: "call_image",
  14. name: "analyze_image",
  15. input: { imageSource: "https://cdn.example.test/shot.png" },
  16. providerExecuted: true,
  17. };
  18. // 端到端行为(流切轮、落盘、卡片事件、冷恢复)见 adapters/tests/provider-builtin-tool-rounds.test.ts。
  19. describe("provider-tool-rounds contract", () => {
  20. it("replays a completed round as assistant tool_use followed by its tool result", () => {
  21. const round: ProviderToolRound = {
  22. messageId: "msg_provider_round",
  23. reasoning: [],
  24. text: "先看图",
  25. toolCall,
  26. result: { output: "Order Console", isError: false },
  27. };
  28. const entries = createProviderToolRoundEntries([round]);
  29. expect(entries.map((entry) => "message" in entry && entry.message.role)).toEqual([
  30. "assistant",
  31. "tool",
  32. ]);
  33. expect(entries[0]).toMatchObject({
  34. message: { content: "先看图", toolCalls: [{ id: "call_image", name: "analyze_image" }] },
  35. });
  36. expect(entries[1]).toMatchObject({
  37. message: { toolCallId: "call_image", content: "Order Console", isError: false },
  38. });
  39. });
  40. it("replays an interrupted round without a dangling tool_use", () => {
  41. const entries = createProviderToolRoundEntries([
  42. { messageId: "msg_provider_round", reasoning: [], text: "先看图", toolCall },
  43. ]);
  44. expect(entries).toHaveLength(1);
  45. expect(entries[0]).toMatchObject({ message: { role: "assistant", content: "先看图" } });
  46. expect("message" in entries[0]! && entries[0].message.toolCalls).toBeUndefined();
  47. });
  48. it("normalizes provider tool output and recognizes the persisted marker", () => {
  49. expect(
  50. providerToolOutputText([
  51. { type: "text", text: "a" },
  52. { type: "text", text: "b" },
  53. ]),
  54. ).toBe("ab");
  55. expect(isProviderExecutedToolPartMetadata({ providerExecuted: true })).toBe(true);
  56. expect(isProviderExecutedToolPartMetadata({})).toBe(false);
  57. expect(hasProviderToolRoundContent([])).toBe(false);
  58. });
  59. it("tags every assistant entry of one stored response with the same compact group key", () => {
  60. const round: ProviderToolRound = {
  61. messageId: "msg_provider_round",
  62. reasoning: [],
  63. text: "先看图",
  64. toolCall,
  65. result: { output: "Order Console", isError: false },
  66. };
  67. const key = providerToolRoundsCompactGroupKey([round]);
  68. // 键是持久化消息 id,而不是只在单次响应内唯一的工具调用 id。
  69. expect(key).toBe("msg_provider_round");
  70. expect(providerToolRoundsCompactGroupKey([])).toBeUndefined();
  71. const entries = withCompactGroupKey(createProviderToolRoundEntries([round]), key);
  72. expect(entries.map((entry) => "compactGroupKey" in entry && entry.compactGroupKey)).toEqual([
  73. key,
  74. false,
  75. ]);
  76. // 没有轮次时不打键,保持原来每条 assistant 各自成组。
  77. expect(withCompactGroupKey(entries, undefined)).toEqual(entries);
  78. });
  79. });