tool-part-metadata.test.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { describe, expect, it } from "vitest";
  2. import type { ToolExecutionResult } from "../src/runtime/deps.js";
  3. import { completedToolPartMetadata } from "../src/runtime/methods/tool-part-metadata.js";
  4. // 工具正常返回、但结果是错误结果时,completed part 写 providerIsError,冷恢复据此还原 is_error
  5. // (session-persistence.md「Completed tool metadata」)。判定与 live tool_result 的 is_error 相同。
  6. function result(toolName: string, output: unknown): ToolExecutionResult {
  7. return {
  8. toolCallId: `call_${toolName}`,
  9. toolName,
  10. success: true,
  11. output,
  12. startedAt: new Date(0),
  13. completedAt: new Date(1),
  14. } as ToolExecutionResult;
  15. }
  16. describe("completed tool part metadata", () => {
  17. it.each([
  18. ["a failed Bash command", "Bash", { status: "failed", exitCode: 3, interrupted: false }],
  19. ["a Bash hard timeout", "Bash", { status: "timed_out", exitCode: 143, interrupted: false }],
  20. [
  21. "an interrupted Bash command",
  22. "Bash",
  23. { status: "completed", exitCode: 0, interrupted: true },
  24. ],
  25. ["an MCP result with isError", "mcp__demo__lookup", { isError: true, content: [] }],
  26. ])("marks %s as a provider error", (_name, toolName, output) => {
  27. expect(completedToolPartMetadata(result(toolName, output)).providerIsError).toBe(true);
  28. });
  29. it.each([
  30. ["a successful Bash command", "Bash", { status: "completed", exitCode: 0, interrupted: false }],
  31. [
  32. "grep without matches",
  33. "Bash",
  34. {
  35. status: "failed",
  36. exitCode: 1,
  37. interrupted: false,
  38. returnCodeInterpretation: "No matches found",
  39. },
  40. ],
  41. ["an MCP result without isError", "mcp__demo__lookup", { content: [] }],
  42. ["a plain text result", "Read", "1\tline"],
  43. ])("does not mark %s", (_name, toolName, output) => {
  44. expect(completedToolPartMetadata(result(toolName, output))).not.toHaveProperty(
  45. "providerIsError",
  46. );
  47. });
  48. });