|
| 1 | +import { expect, test } from "bun:test" |
| 2 | +import { $ } from "bun" |
| 3 | +import path from "path" |
| 4 | +import { tmpdir } from "../fixture/fixture" |
| 5 | + |
| 6 | +/** |
| 7 | + * The full build compiles src/index.ts and the lite build compiles src/headless.ts. |
| 8 | + * The same `aictrl run` must send the same system prompt from either entry point (#152). |
| 9 | + */ |
| 10 | +const entries = { |
| 11 | + full: path.resolve(import.meta.dir, "../../src/index.ts"), |
| 12 | + lite: path.resolve(import.meta.dir, "../../src/headless.ts"), |
| 13 | +} |
| 14 | + |
| 15 | +type Request = { messages: { role: string; content?: unknown }[] } |
| 16 | + |
| 17 | +/** Runs `aictrl run` from `entry` and returns the system messages of the first model request. */ |
| 18 | +async function firstSystemPrompt(entry: string, root: string) { |
| 19 | + const requests: Request[] = [] |
| 20 | + const server = Bun.serve({ |
| 21 | + hostname: "127.0.0.1", |
| 22 | + port: 0, |
| 23 | + async fetch(req) { |
| 24 | + requests.push((await req.json()) as Request) |
| 25 | + const chunks = [ |
| 26 | + { delta: { role: "assistant", content: "done" }, finish_reason: null }, |
| 27 | + { delta: {}, finish_reason: "stop" }, |
| 28 | + ].map( |
| 29 | + (choice) => |
| 30 | + `data: ${JSON.stringify({ id: "fixture", object: "chat.completion.chunk", created: 1, model: "fixture", choices: [{ index: 0, ...choice }] })}\n\n`, |
| 31 | + ) |
| 32 | + return new Response(chunks.join("") + "data: [DONE]\n\n", { headers: { "content-type": "text/event-stream" } }) |
| 33 | + }, |
| 34 | + }) |
| 35 | + const config = { |
| 36 | + provider: { |
| 37 | + fixture: { |
| 38 | + npm: "@ai-sdk/openai-compatible", |
| 39 | + options: { apiKey: "fixture", baseURL: `http://127.0.0.1:${server.port}/v1` }, |
| 40 | + models: { fixture: { name: "fixture", tool_call: true, limit: { context: 100000, output: 1000 } } }, |
| 41 | + }, |
| 42 | + }, |
| 43 | + agent: { title: { disable: true } }, |
| 44 | + } |
| 45 | + const proc = Bun.spawn( |
| 46 | + ["bun", "run", "--conditions=browser", entry, "run", "--format", "json", "--model", "fixture/fixture", "Say done."], |
| 47 | + { |
| 48 | + cwd: path.join(root, "project"), |
| 49 | + env: { |
| 50 | + ...process.env, |
| 51 | + AICTRL_CONFIG_CONTENT: JSON.stringify(config), |
| 52 | + HOME: path.join(root, "home"), |
| 53 | + AICTRL_TEST_HOME: path.join(root, "home"), |
| 54 | + AICTRL_TEST_MANAGED_CONFIG_DIR: path.join(root, "managed"), |
| 55 | + XDG_DATA_HOME: path.join(root, "data"), |
| 56 | + XDG_CONFIG_HOME: path.join(root, "config"), |
| 57 | + XDG_STATE_HOME: path.join(root, "state"), |
| 58 | + XDG_CACHE_HOME: path.join(root, "cache"), |
| 59 | + }, |
| 60 | + stdout: "pipe", |
| 61 | + stderr: "pipe", |
| 62 | + }, |
| 63 | + ) |
| 64 | + const timeout = setTimeout(() => proc.kill("SIGKILL"), 15000) |
| 65 | + try { |
| 66 | + const [stderr, exit] = await Promise.all([new Response(proc.stderr).text(), proc.exited]) |
| 67 | + expect(exit, stderr).toBe(0) |
| 68 | + return requests[0].messages.filter((message) => message.role === "system") |
| 69 | + } finally { |
| 70 | + clearTimeout(timeout) |
| 71 | + server.stop(true) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +test("full and lite entry points send the same system prompt for `aictrl run` (#152)", async () => { |
| 76 | + await using tmp = await tmpdir() |
| 77 | + const project = path.join(tmp.path, "project") |
| 78 | + await Bun.write(path.join(tmp.path, "config", "aictrl", "AGENTS.md"), "# Global aictrl instructions") |
| 79 | + await Bun.write(path.join(project, ".keep"), "") |
| 80 | + await $`git init`.cwd(project).quiet() |
| 81 | + |
| 82 | + const full = await firstSystemPrompt(entries.full, tmp.path) |
| 83 | + const lite = await firstSystemPrompt(entries.lite, tmp.path) |
| 84 | + |
| 85 | + expect(lite).toEqual(full) |
| 86 | + const prompt = JSON.stringify(full) |
| 87 | + expect(prompt).toContain(`Working directory: ${project}`) |
| 88 | + expect(prompt).toContain("# Global aictrl instructions") |
| 89 | +}, 40000) |
0 commit comments