unavailable-tool-reason.test.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // 未知工具报错后缀:规格见根目录 docs/model-tool-call-validation.md「未知工具的后缀」。
  2. // 走真实 runtime 与执行器,断言下一次模型请求里的 tool result 原文。
  3. import { createSessionId, type ModelRequest } from "@zcode/contracts";
  4. import { describe, expect, it } from "vitest";
  5. import type { AgentRuntimeConfig } from "../src/runtime/types.js";
  6. import { createTestAgentRuntime } from "./test-agent-runtime.js";
  7. import { createTestSessionEventStore } from "./test-event-store.js";
  8. import { createTestModelFactory } from "./test-runtime-model.js";
  9. function toolResultText(request: ModelRequest | undefined, toolCallId: string): string {
  10. const message = request?.messages.find(
  11. (entry) => entry.role === "tool" && "toolCallId" in entry && entry.toolCallId === toolCallId,
  12. );
  13. const content = message?.content;
  14. if (typeof content === "string") return content;
  15. if (!Array.isArray(content)) return "";
  16. return content
  17. .map((block) =>
  18. block && typeof block === "object" && "text" in block ? String(block.text) : "",
  19. )
  20. .join("\n");
  21. }
  22. async function callUnknownTool(
  23. label: string,
  24. name: string,
  25. config: Partial<AgentRuntimeConfig> = {},
  26. ): Promise<string> {
  27. const requests: ModelRequest[] = [];
  28. const runtime = createTestAgentRuntime(
  29. createSessionId(`unavailable-tool-${label}`),
  30. { mode: "build", modelStreaming: "off", titleGeneration: { enabled: false }, ...config },
  31. {
  32. eventStore: createTestSessionEventStore(),
  33. modelFactory: createTestModelFactory({
  34. async generateText(request) {
  35. requests.push(request);
  36. if (requests.length > 1)
  37. return { text: "done", finishReason: "stop", usage: {} } as never;
  38. return {
  39. text: "",
  40. finishReason: "tool-calls",
  41. usage: {},
  42. toolCalls: [{ id: "call", name, input: {} }],
  43. } as never;
  44. },
  45. }),
  46. },
  47. );
  48. try {
  49. await runtime.executeTurn("go");
  50. return toolResultText(requests[1], "call");
  51. } finally {
  52. runtime.beginShutdown();
  53. }
  54. }
  55. describe("unavailable tool reason", () => {
  56. it("points a wrongly cased name to the registered tool", async () => {
  57. expect(await callUnknownTool("case", "read")).toBe(
  58. "<tool_use_error>Error: No such tool available: read. Tool names are case-sensitive: call Read instead.</tool_use_error>",
  59. );
  60. });
  61. it("redirects Grep and Glob to Bash when Bash is registered", async () => {
  62. const config = { toolDisallowlist: ["Grep", "Glob"] };
  63. expect(await callUnknownTool("grep-bash", "Grep", config)).toBe(
  64. "<tool_use_error>Error: No such tool available: Grep. Grep is not available in this session \u2014 search file contents with `grep` via the Bash tool instead.</tool_use_error>",
  65. );
  66. expect(await callUnknownTool("glob-bash", "Glob", config)).toBe(
  67. "<tool_use_error>Error: No such tool available: Glob. Glob is not available in this session \u2014 find files with `find` via the Bash tool instead.</tool_use_error>",
  68. );
  69. });
  70. it("reports Grep and Glob as disabled without Bash", async () => {
  71. const config = { toolDisallowlist: ["Grep", "Glob", "Bash"] };
  72. expect(await callUnknownTool("grep-off", "Grep", config)).toBe(
  73. "<tool_use_error>Error: No such tool available: Grep. Grep is disabled for this session.</tool_use_error>",
  74. );
  75. expect(await callUnknownTool("glob-off", "Glob", config)).toBe(
  76. "<tool_use_error>Error: No such tool available: Glob. Glob is disabled for this session.</tool_use_error>",
  77. );
  78. });
  79. it("tells the model a built-in tool is not registered here", async () => {
  80. expect(await callUnknownTool("builtin", "Write", { toolDisallowlist: ["Write"] })).toBe(
  81. "<tool_use_error>Error: No such tool available: Write. Write is not among the tools available here. Continue with the tools you have.</tool_use_error>",
  82. );
  83. });
  84. it("tells a subagent to finish with its own tools", async () => {
  85. expect(
  86. await callUnknownTool("subagent", "Write", {
  87. taskType: "subagent_child",
  88. toolDisallowlist: ["Write"],
  89. }),
  90. ).toBe(
  91. "<tool_use_error>Error: No such tool available: Write. Write is not available inside subagents. Complete the task with the tools provided and return your findings in your final response.</tool_use_error>",
  92. );
  93. });
  94. it("keeps the base sentence for names that match nothing", async () => {
  95. expect(await callUnknownTool("none", "UnknownFixture")).toBe(
  96. "<tool_use_error>Error: No such tool available: UnknownFixture</tool_use_error>",
  97. );
  98. });
  99. });