|
| 1 | +import { expect, test } from "bun:test"; |
| 2 | +import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { join } from "node:path"; |
| 5 | +import { createOpencodeClient } from "@opencode-ai/sdk/v2/client"; |
| 6 | +import { z } from "zod"; |
| 7 | +import { createManagedOpencodeServer } from "./managed-opencode.js"; |
| 8 | + |
| 9 | +const binary = process.env.LEGALWORK_TEST_OPENCODE_BIN; |
| 10 | + |
| 11 | +test.skipIf(!binary)("filename mentions and agent file tools work without FFF in the shipped engine", async () => { |
| 12 | + const root = await mkdtemp(join(tmpdir(), "legalwork-search-engine-")); |
| 13 | + const directory = join(root, "matter"); |
| 14 | + const contract = join(directory, "Matter-A", "contract.txt"); |
| 15 | + const tools = [ |
| 16 | + { name: "glob", args: { pattern: "Matter-A/*.txt", path: directory } }, |
| 17 | + { name: "grep", args: { pattern: "fixture-clause", path: directory, include: "Matter-A/*.txt" } }, |
| 18 | + { name: "read", args: { filePath: contract } }, |
| 19 | + ]; |
| 20 | + let nextTool = 0; |
| 21 | + let engine: Awaited<ReturnType<typeof createManagedOpencodeServer>> | undefined; |
| 22 | + const model = Bun.serve({ hostname: "127.0.0.1", port: 0, async fetch(request) { |
| 23 | + const input = z.object({ tools: z.array(z.object({ function: z.object({ name: z.string() }) })).optional() }).parse(await request.json()); |
| 24 | + const selected = input.tools?.length ? tools[nextTool] : undefined; |
| 25 | + if (selected) { |
| 26 | + expect(input.tools?.some(tool => tool.function.name === selected.name)).toBe(true); |
| 27 | + nextTool += 1; |
| 28 | + } |
| 29 | + const delta = selected ? { tool_calls: [{ index: 0, id: `fixture-${nextTool}`, type: "function", function: { |
| 30 | + name: selected.name, arguments: JSON.stringify(selected.args), |
| 31 | + } }] } : { content: "Search check complete." }; |
| 32 | + const chunk = (value: unknown, finish: string | null) => `data: ${JSON.stringify({ |
| 33 | + id: "fixture", object: "chat.completion.chunk", created: 1, model: "fixture", |
| 34 | + choices: [{ index: 0, delta: value, finish_reason: finish }], |
| 35 | + })}\n\n`; |
| 36 | + return new Response(chunk({ role: "assistant", ...delta }, null) |
| 37 | + + chunk({}, selected ? "tool_calls" : "stop") + "data: [DONE]\n\n", { |
| 38 | + headers: { "content-type": "text/event-stream" }, |
| 39 | + }); |
| 40 | + } }); |
| 41 | + try { |
| 42 | + await mkdir(join(directory, "Matter-A"), { recursive: true }); |
| 43 | + await writeFile(contract, "The fixture-clause permits assignment.\n"); |
| 44 | + const config = join(root, "opencode.json"); |
| 45 | + await writeFile(config, JSON.stringify({ |
| 46 | + plugin: [], permission: { "*": "allow" }, |
| 47 | + provider: { fixture: { npm: "@ai-sdk/openai-compatible", name: "Fixture", options: { |
| 48 | + baseURL: model.url.origin + "/v1", apiKey: "fixture", |
| 49 | + }, models: { fixture: { name: "Fixture", limit: { context: 128000, output: 4096 } } } } }, |
| 50 | + enabled_providers: ["fixture"], model: "fixture/fixture", small_model: "fixture/fixture", |
| 51 | + share: "disabled", autoupdate: false, lsp: false, formatter: false, |
| 52 | + })); |
| 53 | + engine = await createManagedOpencodeServer({ bin: binary, cwd: directory, env: { |
| 54 | + OPENCODE_CONFIG: config, OPENCODE_CONFIG_CONTENT: "", OPENCODE_CONFIG_DIR: join(root, "policy"), |
| 55 | + OPENCODE_TEST_HOME: join(root, "home"), OPENCODE_DB: join(root, "engine.db"), |
| 56 | + XDG_CONFIG_HOME: join(root, "config"), XDG_DATA_HOME: join(root, "data"), |
| 57 | + XDG_CACHE_HOME: join(root, "cache"), XDG_STATE_HOME: join(root, "state"), |
| 58 | + OPENCODE_DISABLE_DEFAULT_PLUGINS: "true", OPENCODE_DISABLE_CLAUDE_CODE: "true", OPENCODE_DISABLE_MODELS_FETCH: "true", |
| 59 | + } }); |
| 60 | + const client = createOpencodeClient({ baseUrl: engine.url, directory, |
| 61 | + headers: { authorization: `Basic ${Buffer.from(`${engine.username}:${engine.password}`).toString("base64")}` }, |
| 62 | + }); |
| 63 | + // The filename index fills in the background; use the same SDK call as the composer. |
| 64 | + let matches: string[] = []; |
| 65 | + for (let attempt = 0; attempt < 100 && !matches.length; attempt += 1) { |
| 66 | + const result = await client.find.files({ query: "contract", dirs: "true", limit: 50 }, { throwOnError: true }); |
| 67 | + matches = result.data ?? []; |
| 68 | + if (!matches.length) await Bun.sleep(50); |
| 69 | + } |
| 70 | + expect(matches.map(path => path.replaceAll("\\", "/"))).toContain("Matter-A/contract.txt"); |
| 71 | + const folders = await client.find.files({ query: "Matter-A", type: "directory", limit: 50 }, { throwOnError: true }); |
| 72 | + expect(folders.data?.map(path => path.replaceAll("\\", "/"))).toContain("Matter-A/"); |
| 73 | + |
| 74 | + const session = (await client.session.create({ title: "Search fallback check" }, { throwOnError: true })).data; |
| 75 | + if (!session) throw new Error("Engine did not return a session"); |
| 76 | + await client.session.prompt({ sessionID: session.id, model: { providerID: "fixture", modelID: "fixture" }, |
| 77 | + parts: [{ type: "text", text: "Find the contract and read its assignment clause." }], |
| 78 | + }, { throwOnError: true }); |
| 79 | + const history = await client.session.messages({ sessionID: session.id }, { throwOnError: true }); |
| 80 | + const calls = history.data?.flatMap(message => message.parts).filter(part => part.type === "tool") ?? []; |
| 81 | + expect(nextTool).toBe(3); |
| 82 | + expect(calls.map(call => call.tool)).toEqual(["glob", "grep", "read"]); |
| 83 | + for (const call of calls) { |
| 84 | + expect(call.state.status).toBe("completed"); |
| 85 | + if (call.state.status === "completed") expect(call.state.output).toContain(call.tool === "glob" ? "contract.txt" : "fixture-clause"); |
| 86 | + } |
| 87 | + } finally { |
| 88 | + await engine?.close(); |
| 89 | + model.stop(true); |
| 90 | + await rm(root, { recursive: true, force: true }); |
| 91 | + } |
| 92 | +}, 60_000); |
0 commit comments