import { describe, expect, it } from "vitest"; import { mkdtemp, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { runAgent } from "../../src/agent/runner.js"; import { ToolRegistry } from "../../src/agent/tools.js"; import { readFileTool } from "../../src/agent/workspace.js"; import { createMockModelFactory } from "../integration/helpers.js"; describe("runAgent with AI SDK tool loop", () => { it("executes SDK tools and returns the final assistant response", async () => { const ws = await mkdtemp(join(tmpdir(), "hub-runner-")); try { await writeFile(join(ws, "lesson.txt"), "hello lesson", "utf8"); const tools = new ToolRegistry(); tools.register("read_file", readFileTool); const { modelFactory, models } = createMockModelFactory([ { toolCalls: [{ toolCallId: "call-1", toolName: "read_file", input: { path: "lesson.txt" } }], }, { text: "done", finishReason: "stop" }, ]); const result = await runAgent(modelFactory, tools, { prompt: "read lesson.txt", model: "mock-model", project: { projectId: "p", boundChatId: "c", workspaceDir: ws }, systemPrompt: "system", maxIterations: 5, }); expect(result.status).toBe("completed"); expect(result.messages.at(-1)).toMatchObject({ role: "assistant" }); expect(models.get("mock-model")?.calls).toHaveLength(2); } finally { await rm(ws, { recursive: true, force: true }); } }); });