model-tool-error-transform.test.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import { createAnthropic } from "@ai-sdk/anthropic";
  2. import { generateText } from "ai";
  3. import { describe, expect, it } from "vitest";
  4. import { toAiSdkMessages } from "../src/model/index.js";
  5. describe("AI SDK model tool result error transform", () => {
  6. it("maps provider-neutral failed tool results to AI SDK error output", () => {
  7. const toolCallId = "call_failed_tool";
  8. const messages = toAiSdkMessages([
  9. {
  10. role: "tool",
  11. content: "Command was aborted before completion",
  12. isError: true,
  13. toolCallId,
  14. toolName: "Bash",
  15. },
  16. ]);
  17. expect((messages[0] as any).content[0]).toEqual({
  18. type: "tool-result",
  19. toolCallId,
  20. toolName: "Bash",
  21. output: {
  22. type: "error-text",
  23. value: "Command was aborted before completion",
  24. },
  25. });
  26. });
  27. it("keeps reminders folded into a failed tool result in the error text", () => {
  28. // core 把提醒以空行接在失败结果的字符串后面;error-text 必须原样带上,不能只留错误本身。
  29. const folded = "command failed\n\n<system-reminder>\nTODO_AFTER_ERROR\n</system-reminder>";
  30. const messages = toAiSdkMessages([
  31. {
  32. role: "tool",
  33. content: folded,
  34. isError: true,
  35. toolCallId: "call_folded_error",
  36. toolName: "Bash",
  37. },
  38. ]);
  39. expect((messages[0] as any).content[0].output).toEqual({ type: "error-text", value: folded });
  40. });
  41. it("serializes failed tool results as Anthropic is_error tool_result blocks", async () => {
  42. let requestBody: any;
  43. const provider = createAnthropic({
  44. apiKey: "fake-key",
  45. baseURL: "https://api.example.test",
  46. fetch: async (_input, init) => {
  47. requestBody = JSON.parse(String(init?.body));
  48. return new Response(
  49. JSON.stringify({ error: { message: "wire capture", type: "invalid_request_error" } }),
  50. {
  51. headers: { "content-type": "application/json" },
  52. status: 400,
  53. },
  54. );
  55. },
  56. });
  57. const toolCallId = "call_malformed_ask";
  58. const validationContent = [
  59. "<tool_use_error>InputValidationError: AskUserQuestion failed due to the following issue:",
  60. "The required parameter `questions` is missing</tool_use_error>",
  61. ].join("\n");
  62. const messages = toAiSdkMessages([
  63. {
  64. role: "assistant",
  65. content: "",
  66. toolCalls: [
  67. {
  68. id: toolCallId,
  69. input: {},
  70. name: "AskUserQuestion",
  71. },
  72. ],
  73. },
  74. {
  75. role: "tool",
  76. content: validationContent,
  77. isError: true,
  78. toolCallId,
  79. toolName: "AskUserQuestion",
  80. },
  81. ]);
  82. await expect(
  83. generateText({
  84. messages,
  85. model: provider("anthropic-model"),
  86. }),
  87. ).rejects.toBeDefined();
  88. expect(requestBody.messages).toEqual([
  89. {
  90. role: "assistant",
  91. content: [
  92. {
  93. id: toolCallId,
  94. input: {},
  95. name: "AskUserQuestion",
  96. type: "tool_use",
  97. },
  98. ],
  99. },
  100. {
  101. role: "user",
  102. content: [
  103. {
  104. content: validationContent,
  105. is_error: true,
  106. tool_use_id: toolCallId,
  107. type: "tool_result",
  108. },
  109. ],
  110. },
  111. ]);
  112. });
  113. });