import path from "node:path"; import { fileURLToPath } from "node:url"; import { buildApp } from "../apps/server/src/app.js"; import type { SessionRegistry } from "../packages/harness/src/session.js"; const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), ".."); process.loadEnvFile(path.join(root, ".env")); const app = await buildApp(); const registry = (app as unknown as { registry: SessionRegistry }).registry; const session = await registry.create(); await registry.models.configure(session.sessionId, { providerId: "anthropic", api: "anthropic-messages", baseUrl: process.env.LLM_BASE_URL!, modelId: process.env.AGENT_MODEL!, apiKey: process.env.ANTHROPIC_API_KEY!, supportsImages: true, }); registry.events.subscribe(session.sessionId, e => { if (e.type === "approval.required") registry.approvals.resolve(String(e.payload.approvalId), e.payload.toolName === "generate_pptx" ? "approved" : "denied"); }); try { await registry.run(session, "请生成一页测试PPT,标题为Beryl文件交付测试,正文为本页仅用于验证PPT生成与下载,不包含真实业务数据。直接使用generate_pptx的plain模式和purpose=final,不调用网络或调研工具。不要使用模板。没有商业事实,无需外部来源。"); const deadline = Date.now() + 300_000; while (session.busy && Date.now() < deadline) await new Promise(r => setTimeout(r, 500)); if (session.busy) { await registry.cancel(session.sessionId); throw new Error("TIMEOUT"); } const events = registry.events.listAfter(session.sessionId); const saved = events.find(e => e.type === "tool.completed" && e.payload.toolName === "generate_pptx"); const id = (saved?.payload.result as { details?: { artifactId?: string } })?.details?.artifactId; if (!id || !events.some(e => e.type === "run.completed")) throw new Error("PPT_RUN_FAILED"); const response = await app.inject({ url: `/api/sessions/${session.sessionId}/presentations/${id}` }); if (response.statusCode !== 200 || !response.headers["content-disposition"]?.includes("attachment")) throw new Error("DOWNLOAD_FAILED"); console.log(JSON.stringify({ passed: true, model: process.env.AGENT_MODEL, downloadStatus: response.statusCode, bytes: response.rawPayload.length, directory: session.workspace.resolve(`presentations/${id}`) })); } finally { session.agent?.dispose(); await app.close(); }