task-output-wire.test.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. import { createAnthropic } from "@ai-sdk/anthropic";
  2. import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
  3. import {
  4. TASK_OUTPUT_PROVIDER_DESCRIPTION,
  5. TASK_OUTPUT_TOOL_NAME,
  6. TaskOutputInputJsonSchema,
  7. TaskOutputResultJsonSchema,
  8. type ModelToolContract,
  9. } from "@zcode/contracts";
  10. import { generateText } from "ai";
  11. import { describe, expect, it } from "vitest";
  12. import { createGenerateTextOptions } from "../src/model/runner-options.js";
  13. import type { ResolvedAiSdkModel } from "../src/model/runner-runtime.js";
  14. import { createTestModelProperties } from "./test-model-format.js";
  15. const taskOutputContent = [
  16. "<retrieval_status>success</retrieval_status>",
  17. "<task_id>agent_1</task_id>",
  18. "<task_type>local_agent</task_type>",
  19. "<status>completed</status>",
  20. "<output>\nanswer <raw>&\n</output>",
  21. ].join("\n\n");
  22. const unknownTaskContent = "<tool_use_error>No task found with ID: missing</tool_use_error>";
  23. describe("TaskOutput provider wire projection", () => {
  24. it("sends the exact Anthropic tool definition and tool_result text", async () => {
  25. let capturedBody: Record<string, unknown> | undefined;
  26. const provider = createAnthropic({
  27. apiKey: "fake-ak",
  28. baseURL: "https://api.example.test",
  29. fetch: async (_input, init) => {
  30. capturedBody = parseRequestBody(init);
  31. return new Response(
  32. JSON.stringify({
  33. content: [{ text: "ok", type: "text" }],
  34. id: "msg_task_output",
  35. model: "test-model",
  36. role: "assistant",
  37. stop_reason: "end_turn",
  38. stop_sequence: null,
  39. type: "message",
  40. usage: { input_tokens: 1, output_tokens: 1 },
  41. }),
  42. { headers: { "content-type": "application/json" } },
  43. );
  44. },
  45. });
  46. await generateText(
  47. createGenerateTextOptions({
  48. includeModelIO: false,
  49. request: taskOutputRequest(),
  50. resolved: {
  51. properties: createTestModelProperties(),
  52. baseURL: "https://api.example.test",
  53. model: provider("test-model"),
  54. providerKind: "anthropic",
  55. providerId: "test-anthropic",
  56. modelId: "test-model",
  57. } as ResolvedAiSdkModel,
  58. statusContext: {
  59. providerId: "test-anthropic",
  60. modelId: "test-model",
  61. providerKind: "anthropic",
  62. requestId: "req_task_output_anthropic",
  63. sessionId: "sess_task_output_anthropic",
  64. traceId: "trace_task_output_anthropic",
  65. } as never,
  66. }),
  67. );
  68. expect(capturedBody?.tools).toEqual([
  69. {
  70. name: TASK_OUTPUT_TOOL_NAME,
  71. description: TASK_OUTPUT_PROVIDER_DESCRIPTION,
  72. input_schema: TaskOutputInputJsonSchema,
  73. },
  74. ]);
  75. expect(capturedBody?.messages).toEqual([
  76. {
  77. role: "user",
  78. content: [{ type: "text", text: "start the task" }],
  79. },
  80. {
  81. role: "assistant",
  82. content: [
  83. {
  84. type: "tool_use",
  85. id: "tool_task_output",
  86. name: TASK_OUTPUT_TOOL_NAME,
  87. input: { task_id: "agent_1", block: false },
  88. },
  89. ],
  90. },
  91. {
  92. role: "user",
  93. content: [
  94. {
  95. type: "tool_result",
  96. tool_use_id: "tool_task_output",
  97. content: taskOutputContent,
  98. },
  99. ],
  100. },
  101. {
  102. role: "assistant",
  103. content: [
  104. {
  105. type: "tool_use",
  106. id: "tool_task_output_missing",
  107. name: TASK_OUTPUT_TOOL_NAME,
  108. input: { task_id: "missing", block: false },
  109. },
  110. ],
  111. },
  112. {
  113. role: "user",
  114. content: [
  115. {
  116. type: "tool_result",
  117. tool_use_id: "tool_task_output_missing",
  118. content: unknownTaskContent,
  119. is_error: true,
  120. },
  121. ],
  122. },
  123. ]);
  124. });
  125. it("sends the exact OpenAI-compatible tool definition and tool result text", async () => {
  126. let capturedBody: Record<string, unknown> | undefined;
  127. const provider = createOpenAICompatible({
  128. apiKey: "fake-ak",
  129. baseURL: "https://api.example.test/v1",
  130. name: "TaskOutput Compatible",
  131. fetch: async (_input, init) => {
  132. capturedBody = parseRequestBody(init);
  133. return new Response(
  134. JSON.stringify({
  135. choices: [
  136. {
  137. finish_reason: "stop",
  138. message: { content: "ok", role: "assistant" },
  139. },
  140. ],
  141. id: "chatcmpl-task-output",
  142. model: "test-model",
  143. }),
  144. { headers: { "content-type": "application/json" } },
  145. );
  146. },
  147. });
  148. await generateText(
  149. createGenerateTextOptions({
  150. includeModelIO: false,
  151. request: taskOutputRequest(),
  152. resolved: {
  153. properties: createTestModelProperties(),
  154. baseURL: "https://api.example.test/v1",
  155. model: provider("test-model"),
  156. providerKind: "openai-compatible",
  157. providerId: "test-compatible",
  158. modelId: "test-model",
  159. } as ResolvedAiSdkModel,
  160. statusContext: {
  161. providerId: "test-compatible",
  162. modelId: "test-model",
  163. providerKind: "openai-compatible",
  164. requestId: "req_task_output_compatible",
  165. sessionId: "sess_task_output_compatible",
  166. traceId: "trace_task_output_compatible",
  167. } as never,
  168. }),
  169. );
  170. expect(capturedBody?.tools).toEqual([
  171. {
  172. type: "function",
  173. function: {
  174. name: TASK_OUTPUT_TOOL_NAME,
  175. description: TASK_OUTPUT_PROVIDER_DESCRIPTION,
  176. parameters: TaskOutputInputJsonSchema,
  177. },
  178. },
  179. ]);
  180. expect(capturedBody?.messages).toEqual([
  181. { role: "user", content: "start the task" },
  182. {
  183. role: "assistant",
  184. content: null,
  185. tool_calls: [
  186. {
  187. id: "tool_task_output",
  188. type: "function",
  189. function: {
  190. name: TASK_OUTPUT_TOOL_NAME,
  191. arguments: JSON.stringify({ task_id: "agent_1", block: false }),
  192. },
  193. },
  194. ],
  195. },
  196. {
  197. role: "tool",
  198. tool_call_id: "tool_task_output",
  199. content: taskOutputContent,
  200. },
  201. {
  202. role: "assistant",
  203. content: null,
  204. tool_calls: [
  205. {
  206. id: "tool_task_output_missing",
  207. type: "function",
  208. function: {
  209. name: TASK_OUTPUT_TOOL_NAME,
  210. arguments: JSON.stringify({ task_id: "missing", block: false }),
  211. },
  212. },
  213. ],
  214. },
  215. {
  216. role: "tool",
  217. tool_call_id: "tool_task_output_missing",
  218. content: unknownTaskContent,
  219. },
  220. ]);
  221. });
  222. });
  223. function taskOutputRequest() {
  224. return {
  225. messages: [
  226. { role: "user" as const, content: "start the task" },
  227. {
  228. role: "assistant" as const,
  229. content: "",
  230. toolCalls: [
  231. {
  232. id: "tool_task_output",
  233. name: TASK_OUTPUT_TOOL_NAME,
  234. input: { task_id: "agent_1", block: false },
  235. },
  236. ],
  237. },
  238. {
  239. role: "tool" as const,
  240. content: taskOutputContent,
  241. toolCallId: "tool_task_output",
  242. toolName: TASK_OUTPUT_TOOL_NAME,
  243. },
  244. {
  245. role: "assistant" as const,
  246. content: "",
  247. toolCalls: [
  248. {
  249. id: "tool_task_output_missing",
  250. name: TASK_OUTPUT_TOOL_NAME,
  251. input: { task_id: "missing", block: false },
  252. },
  253. ],
  254. },
  255. {
  256. role: "tool" as const,
  257. content: unknownTaskContent,
  258. isError: true,
  259. toolCallId: "tool_task_output_missing",
  260. toolName: TASK_OUTPUT_TOOL_NAME,
  261. },
  262. ],
  263. tools: [taskOutputContract()],
  264. };
  265. }
  266. function taskOutputContract(): ModelToolContract {
  267. return {
  268. name: TASK_OUTPUT_TOOL_NAME,
  269. capability: "read output/logs from a background task",
  270. description: TASK_OUTPUT_PROVIDER_DESCRIPTION,
  271. inputSchema: TaskOutputInputJsonSchema,
  272. outputSchema: TaskOutputResultJsonSchema,
  273. readOnly: true,
  274. concurrentSafe: true,
  275. needsApproval: false,
  276. };
  277. }
  278. function parseRequestBody(init: RequestInit | undefined): Record<string, unknown> {
  279. if (typeof init?.body !== "string") {
  280. throw new Error("Expected a JSON request body");
  281. }
  282. return JSON.parse(init.body) as Record<string, unknown>;
  283. }