/** * 端到端测试支持:一个假 LLM(node:http),同时会说 anthropic 与 openai 两种线格式的流式协议。 * * 行为**由最后一条带文本的 user 消息的文字决定**,而不是按请求序号 —— 这样 Task 5 里用户的完整 * 扩展集多发几个请求也不会串: * 含 ESSAY → 每 120ms 一个文本增量 `step0 `、`step1 `…直到客户端断开(上限 300 个); * 含 SLEEP → 文本 `Running a long command.` 加一次 bash 工具调用 `sleep 40`,以 tool-use 停止; * 含 ECHO → 先是文本加一次 `echo hi` 的 bash 调用(秒级成功跑完);工具结果回来后的那次请求则 * 「静默思考」:只发开头、不发任何增量,一直挂到客户端断开; * 其余 → 文本 `4.` 正常结束。 * 每个请求体都原样记在 `requests` 里,测试用 `summarize` 把它压成一行行可断言的文字。 */ import http from "node:http"; import type { AddressInfo } from "node:net"; export type Api = "anthropic" | "openai"; export const PROMPTS = { stream: "Write a long ESSAY about the abacus.", tool: "Run the SLEEP command for a while.", quiet: "Run the ECHO command, then keep thinking quietly.", next: "Stop that. Instead just tell me what 2+2 is.", }; export interface FakeLlm { baseUrl: string; requests: any[]; close(): Promise; } const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)); interface Emitter { start(): void; text(t: string): void; tool(id: string, name: string, args: string): void; end(kind: "stop" | "tool"): void; } function makeEmitter(api: Api, res: http.ServerResponse, n: number): Emitter { if (api === "openai") { const send = (delta: object, finish: string | null = null) => res.write(`data: ${JSON.stringify({ id: `c${n}`, object: "chat.completion.chunk", created: 0, model: "fake-1", choices: [{ index: 0, delta, finish_reason: finish }], })}\n\n`); return { start: () => void send({ role: "assistant", content: "" }), text: (t) => void send({ content: t }), tool: (id, name, args) => void send({ tool_calls: [{ index: 0, id, type: "function", function: { name, arguments: args } }] }), end: (kind) => { send({}, kind === "tool" ? "tool_calls" : "stop"); res.write("data: [DONE]\n\n"); res.end(); }, }; } let index = -1; let open = false; const event = (type: string, data: object) => res.write(`event: ${type}\ndata: ${JSON.stringify({ type, ...data })}\n\n`); const closeBlock = () => { if (!open) return; event("content_block_stop", { index }); open = false; }; return { start: () => void event("message_start", { message: { id: `msg_${n}`, type: "message", role: "assistant", model: "fake-1", content: [], stop_reason: null, stop_sequence: null, usage: { input_tokens: 10, output_tokens: 1 } }, }), text: (t) => { if (!open) { index++; event("content_block_start", { index, content_block: { type: "text", text: "" } }); open = true; } event("content_block_delta", { index, delta: { type: "text_delta", text: t } }); }, tool: (id, name, args) => { closeBlock(); index++; event("content_block_start", { index, content_block: { type: "tool_use", id, name, input: {} } }); event("content_block_delta", { index, delta: { type: "input_json_delta", partial_json: args } }); open = true; }, end: (kind) => { closeBlock(); event("message_delta", { delta: { stop_reason: kind === "tool" ? "tool_use" : "end_turn", stop_sequence: null }, usage: { output_tokens: 5 } }); event("message_stop", {}); res.end(); }, }; } /** 最后一条「带非空文本」的 user 消息的文字(只含 tool_result 的 user 消息要跳过)。 */ function lastUserText(body: any): string { const messages: any[] = body?.messages ?? []; for (let i = messages.length - 1; i >= 0; i--) { const m = messages[i]; if (m?.role !== "user") continue; const text = typeof m.content === "string" ? m.content : Array.isArray(m.content) ? m.content.filter((p: any) => p?.type === "text").map((p: any) => p.text).join("") : ""; if (text.trim() !== "") return text; } return ""; } /** 最后一条消息是不是工具结果(anthropic:user 消息里带 tool_result 块;openai:role=tool)。 */ function endsWithToolResult(body: any): boolean { const last = (body?.messages ?? []).at(-1); if (last?.role === "tool") return true; return last?.role === "user" && Array.isArray(last.content) && last.content.some((p: any) => p?.type === "tool_result"); } export async function startFakeLlm(api: Api): Promise { const requests: any[] = []; const endpoint = api === "anthropic" ? "/v1/messages" : "/v1/chat/completions"; const server = http.createServer(async (req, res) => { if (req.method !== "POST" || !req.url?.includes(endpoint)) { res.writeHead(404).end(); return; } const chunks: Buffer[] = []; for await (const chunk of req) chunks.push(chunk as Buffer); const body = JSON.parse(Buffer.concat(chunks).toString("utf8")); const n = requests.push(body); res.writeHead(200, { "content-type": "text/event-stream", "cache-control": "no-cache" }); let gone = false; res.on("close", () => { gone = true; }); const out = makeEmitter(api, res, n); out.start(); const prompt = lastUserText(body); if (prompt.includes("ECHO")) { if (endsWithToolResult(body)) { // 工具已经成功跑完,模型接下来「静默思考」:不发任何增量,直到客户端断开 while (!gone) await sleep(50); } else { out.text("Running echo."); out.tool("toolu_2", "bash", JSON.stringify({ command: "echo hi" })); out.end("tool"); } } else if (prompt.includes("ESSAY")) { for (let i = 0; i < 300 && !gone; i++) { out.text(`step${i} `); await sleep(120); } if (!gone) out.end("stop"); } else if (prompt.includes("SLEEP")) { out.text("Running a long command."); out.tool("toolu_1", "bash", JSON.stringify({ command: "sleep 40" })); out.end("tool"); } else { out.text("4."); out.end("stop"); } }); await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve)); const port = (server.address() as AddressInfo).port; return { baseUrl: api === "anthropic" ? `http://127.0.0.1:${port}` : `http://127.0.0.1:${port}/v1`, requests, close: () => new Promise((resolve) => { server.closeAllConnections(); server.close(() => resolve()); }), }; } /** 供 `PI_CODING_AGENT_DIR/models.json` 用:一个叫 fake 的 provider、一个叫 fake-1 的模型。 */ export function modelsJson(api: Api, baseUrl: string): object { return { providers: { fake: { baseUrl, api: api === "anthropic" ? "anthropic-messages" : "openai-completions", apiKey: "x", models: [{ id: "fake-1", name: "fake-1", reasoning: false, input: ["text"], contextWindow: 100000, maxTokens: 4096, cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, }], }, }, }; } /** * 把一次请求体压成可断言的文字:非 system/developer 的每条消息一行 `: <各部分用 ' ⟂ ' 连接>`。 * 文本取原文;工具调用记 ``,工具结果记 ``,thinking 记 ``。 */ export function summarize(body: any, api: Api): string[] { const lines: string[] = []; for (const m of body?.messages ?? []) { if (m.role === "system" || m.role === "developer") continue; if (api === "openai" && m.role === "tool") { lines.push("tool: "); continue; } const parts: string[] = []; if (typeof m.content === "string") { parts.push(m.content); } else if (Array.isArray(m.content)) { for (const b of m.content) { if (b.type === "text") parts.push(b.text); else if (b.type === "tool_use") parts.push(""); else if (b.type === "tool_result") parts.push(""); else if (b.type === "thinking") parts.push(""); } } if (Array.isArray(m.tool_calls) && m.tool_calls.length > 0) parts.push(""); lines.push(`${m.role}: ${parts.join(" ⟂ ")}`); } return lines; }