bash-tool-result-wire.test.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import { createAnthropic } from "@ai-sdk/anthropic";
  2. import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
  3. import { generateText } from "ai";
  4. import { describe, expect, it } from "vitest";
  5. import { toAiSdkMessages } from "../src/model/index.js";
  6. const FOREGROUND_CONTENT = "stdout-1\nstderr-1\nstdout-2";
  7. const BACKGROUND_CONTENT =
  8. "Command running in background with ID: exec_bg. Output is being written to: /tmp/exec_bg-stdout.log. You will be notified when it completes. To check interim output, use Read on that file path.";
  9. const NOTIFICATION_CONTENT = [
  10. "<task-notification>",
  11. "<task-id>exec_bg</task-id>",
  12. "<tool-use-id>call_bash_background</tool-use-id>",
  13. "<output-file>/tmp/exec_bg-stdout.log</output-file>",
  14. "<status>completed</status>",
  15. '<summary>Background command "long command" completed (exit code 0)</summary>',
  16. "</task-notification>",
  17. ].join("\n");
  18. const TOOL_CALLS = [
  19. {
  20. id: "call_bash_foreground",
  21. input: { command: "mixed output" },
  22. name: "Bash",
  23. },
  24. {
  25. id: "call_bash_background",
  26. input: { command: "long command", run_in_background: true },
  27. name: "Bash",
  28. },
  29. ];
  30. function bashMessages() {
  31. return toAiSdkMessages([
  32. {
  33. role: "assistant",
  34. content: "",
  35. toolCalls: TOOL_CALLS,
  36. },
  37. {
  38. role: "tool",
  39. content: FOREGROUND_CONTENT,
  40. toolCallId: TOOL_CALLS[0]!.id,
  41. toolName: "Bash",
  42. },
  43. {
  44. role: "tool",
  45. content: BACKGROUND_CONTENT,
  46. toolCallId: TOOL_CALLS[1]!.id,
  47. toolName: "Bash",
  48. },
  49. {
  50. role: "user",
  51. content: NOTIFICATION_CONTENT,
  52. },
  53. ]);
  54. }
  55. describe("Bash provider-visible tool result wire", () => {
  56. it("preserves Bash content in Anthropic tool_result blocks", async () => {
  57. let requestBody: any;
  58. const provider = createAnthropic({
  59. apiKey: "fake-key",
  60. baseURL: "https://api.example.test",
  61. fetch: async (_input, init) => {
  62. requestBody = JSON.parse(String(init?.body));
  63. return new Response(
  64. JSON.stringify({ error: { message: "wire capture", type: "invalid_request_error" } }),
  65. {
  66. headers: { "content-type": "application/json" },
  67. status: 400,
  68. },
  69. );
  70. },
  71. });
  72. await expect(
  73. generateText({
  74. messages: bashMessages(),
  75. model: provider("anthropic-model"),
  76. }),
  77. ).rejects.toBeDefined();
  78. expect(requestBody.messages[1]).toEqual({
  79. role: "user",
  80. content: [
  81. {
  82. content: FOREGROUND_CONTENT,
  83. tool_use_id: TOOL_CALLS[0]!.id,
  84. type: "tool_result",
  85. },
  86. {
  87. content: BACKGROUND_CONTENT,
  88. tool_use_id: TOOL_CALLS[1]!.id,
  89. type: "tool_result",
  90. },
  91. {
  92. text: NOTIFICATION_CONTENT,
  93. type: "text",
  94. },
  95. ],
  96. });
  97. });
  98. it("preserves Bash content in OpenAI-compatible tool messages", async () => {
  99. let requestBody: any;
  100. const provider = createOpenAICompatible({
  101. apiKey: "fake-key",
  102. baseURL: "https://api.example.test/v1",
  103. name: "Bash wire capture",
  104. fetch: async (_input, init) => {
  105. requestBody = JSON.parse(String(init?.body));
  106. return new Response(
  107. JSON.stringify({
  108. choices: [{ finish_reason: "stop", message: { content: "ok", role: "assistant" } }],
  109. id: "chatcmpl-bash-wire",
  110. model: "compatible-model",
  111. }),
  112. { headers: { "content-type": "application/json" } },
  113. );
  114. },
  115. });
  116. await generateText({
  117. messages: bashMessages(),
  118. model: provider("compatible-model"),
  119. });
  120. expect(requestBody.messages.slice(1)).toEqual([
  121. {
  122. role: "tool",
  123. content: FOREGROUND_CONTENT,
  124. tool_call_id: TOOL_CALLS[0]!.id,
  125. },
  126. {
  127. role: "tool",
  128. content: BACKGROUND_CONTENT,
  129. tool_call_id: TOOL_CALLS[1]!.id,
  130. },
  131. {
  132. role: "user",
  133. content: NOTIFICATION_CONTENT,
  134. },
  135. ]);
  136. });
  137. });