import assert from "node:assert/strict"; import { cp, mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import { dirname, join, resolve } from "node:path"; import { fileURLToPath, pathToFileURL } from "node:url"; // Explicit installed SDK, no installation, network, auth reads, or user settings writes. const sdk = process.argv[2]; if (!sdk) throw new Error("Provide installed pi-coding-agent package path."); const metadata = JSON.parse( await readFile(join(sdk, "package.json"), "utf8"), ) as { name: string; version: string }; assert.equal(metadata.name, "@earendil-works/pi-coding-agent"); const root = await mkdtemp(join(tmpdir(), "minimax-sdk-")); try { const agentDir = join(root, "agent"); await mkdir(agentDir); for (const key of [ "HOME", "USERPROFILE", "XDG_CONFIG_HOME", "XDG_STATE_HOME", "XDG_DATA_HOME", "XDG_CACHE_HOME", ]) process.env[key] = join(root, key); process.env["PI_CODING_AGENT_DIR"] = agentDir; process.env["PI_OFFLINE"] = "1"; process.env["MINIMAX_API_KEY"] = "sk-cp-fixture"; // Cheap operations must run without any approval setting; H3 is enabled later. delete process.env["PI_MINIMAX_VIDEO_APPROVAL"]; process.env["PI_MINIMAX_REGION"] = "global"; process.env["PI_MINIMAX_DEBUG"] = "0"; const api = (await import( pathToFileURL(join(sdk, "dist/index.js")).href )) as typeof import("@earendil-works/pi-coding-agent"); const ai = (await import( pathToFileURL(join(sdk, "../pi-ai/dist/index.js")).href )) as typeof import("@earendil-works/pi-ai"); const source = resolve( dirname(fileURLToPath(import.meta.url)), "../../../dist/minimax", ); const relocated = join(root, "package"); await cp(source, relocated, { recursive: true }); const activity: unknown[] = []; const structured: { toolName: string; structuredContent: unknown }[] = []; const observer: import("@earendil-works/pi-coding-agent").ExtensionFactory = ( pi, ) => { pi.events.on("quota:provider-activity", (data) => { activity.push(data); }); pi.on("tool_result", (event) => { structured.push({ toolName: event.toolName, structuredContent: event.structuredContent, }); }); }; const loader = new api.DefaultResourceLoader({ cwd: root, agentDir, noExtensions: true, noSkills: true, noPromptTemplates: true, noThemes: true, noContextFiles: true, additionalExtensionPaths: [relocated], extensionFactories: [observer], }); await loader.reload(); assert.deepEqual(loader.getExtensions().errors, []); const credentials = new ai.InMemoryCredentialStore(); const runtime = await api.ModelRuntime.create({ credentials, modelsStore: new ai.InMemoryModelsStore(), refreshOnCreate: false, }); const faux = ai.createFauxCore({ provider: "faux", api: "openai", models: [{ id: "fixture", name: "Fixture" }], }); runtime.registerProvider("faux", { apiKey: "fixture", api: "openai", baseUrl: "https://faux.invalid", streamSimple: faux.streamSimple, models: [ { id: "fixture", name: "Fixture", reasoning: false, input: ["text"], contextWindow: 200000, maxTokens: 8192, cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, }, ], }); const model = runtime.getModel("faux", "fixture"); assert.ok(model); const { session } = await api.createAgentSession({ cwd: root, agentDir, model, modelRuntime: runtime, sessionManager: api.SessionManager.inMemory(root), resourceLoader: loader, }); const errors: string[] = []; await session.bindExtensions({ mode: "print", onError: (e) => errors.push(e.error), }); const declarations = session.agent.state.tools.map( ({ name, description, parameters }) => ({ name, description, parameters }), ); const baseline = declarations.filter( (tool) => !tool.name.startsWith("minimax_"), ); console.log( JSON.stringify({ contextMeasurement: "standalone native tool-declaration heuristic; not provider tokens or complete configured Pi context", withoutMiniMax: Math.ceil(JSON.stringify(baseline).length / 4), withMiniMax: Math.ceil(JSON.stringify(declarations).length / 4), delta: Math.ceil(JSON.stringify(declarations).length / 4) - Math.ceil(JSON.stringify(baseline).length / 4), newTools: declarations .filter((tool) => tool.name.startsWith("minimax_")) .map((tool) => tool.name), }), ); const jpeg = Buffer.from([255, 216, 255, 224, 0, 0]).toString("base64"); let requests = 0; let quotaReads = 0; globalThis.fetch = async (url, init) => { requests++; assert.equal( new Headers(init?.headers).get("Authorization"), "Bearer sk-cp-fixture", ); if (String(url) === "https://www.minimax.io/v1/token_plan/remains") { quotaReads++; assert.equal(init?.method, "GET"); assert.equal(init?.redirect, "error"); return Response.json({ base_resp: { status_code: 0 }, model_remains: [ { model_name: "general", current_interval_remaining_percent: 93, current_weekly_remaining_percent: 92, }, ], }); } const path = new URL(String(url)).pathname; if (path === "/v1/image_generation") return Response.json({ data: { image_base64: [jpeg] } }); if (path === "/v1/coding_plan/vlm") return Response.json({ content: "red" }); if (path === "/v1/t2a_v2") return Response.json({ data: { audio: "494433", status: 2 }, extra_info: { usage_characters: 6 }, }); if (path === "/v2/video_generation") return Response.json({ task_id: "sdk-task" }); if (path === "/v2/query/video_generation/sdk-task") return Response.json({ task: { id: "sdk-task", model: "MiniMax-H3", status: "succeeded", usage: { output_seconds: 4, input_seconds: 0, input_image_count: 0 }, }, }); throw new Error("Unexpected SDK fixture request; network blocked."); }; try { const image = runtime.getModelOfType("image", "minimax-media", "image-01"); assert.ok(image); const before = runtime.getModels("minimax"); assert.ok(before.length); const generated = await runtime.generateImages(image, { input: [{ type: "text", text: "A square" }], }); assert.equal(generated.stopReason, "stop", generated.errorMessage ?? ""); assert.equal(generated.usage?.cost.total, 0.0035); assert.deepEqual(runtime.getModels("minimax"), before); assert.match( generated.output.find((b) => b.type === "text")?.text ?? "", /"fiveHourLeft":93/, ); const source = join(root, "input.jpg"); await writeFile(source, Buffer.from(jpeg, "base64")); const run = async ( name: string, args: import("@earendil-works/pi-ai").JsonObject, ) => { faux.setResponses([ ai.fauxAssistantMessage(ai.fauxToolCall(name, args), { stopReason: "toolUse", }), ai.fauxAssistantMessage("Done"), ]); await session.prompt("Run deterministic fixture"); const message = session.messages .filter((m) => m.role === "toolResult" && m.toolName === name) .at(-1); assert.ok(message?.role === "toolResult"); const text = message.content .map((b) => (b.type === "text" ? b.text : "")) .join(""); assert.ok( structured.at(-1)?.toolName === name && structured.at(-1)?.structuredContent, `${name} lacks structuredContent`, ); return { message, text }; }; for (const [name, args] of [ ["minimax_capabilities", {}], ["minimax_understand_image", { prompt: "Color?", source }], [ "minimax_speech", { text: "Hello.", outputPath: join(root, "speech.mp3") }, ], ] as const) { const { message, text } = await run(name, args); assert.equal(message.isError, false, text); if (name !== "minimax_capabilities") assert.match(text, /M Plan left 5h 93% ยท wk 92%/); } const submitArgs = { action: "submit", job: "sdk", prompt: "A ball" }; const requestsBefore = requests; const blocked = await run("minimax_video", submitArgs); assert.equal(blocked.message.isError, true); assert.match(blocked.text, /PI_MINIMAX_VIDEO_APPROVAL=always/); assert.equal(requests, requestsBefore, "blocked H3 submit sent requests"); process.env["PI_MINIMAX_VIDEO_APPROVAL"] = "always"; for (const args of [ submitArgs, { action: "status", job: "sdk" }, { action: "status", job: "sdk" }, ]) { const { message, text } = await run("minimax_video", args); assert.equal(message.isError, false, text); } const video = session.messages.filter( (m) => m.role === "toolResult" && m.toolName === "minimax_video", ); assert.equal( video[2]?.role === "toolResult" ? video[2].usage?.cost.total : undefined, 0.32, ); assert.equal( video[3]?.role === "toolResult" ? video[3].usage : undefined, undefined, ); // image, understanding, speech, H3 submit, first H3 completion. assert.equal(quotaReads, 5); assert.deepEqual( activity, Array.from({ length: 5 }, () => ({ provider: "minimax" })), ); assert.deepEqual(errors, []); console.log( `OK Pi ${metadata.version}: relocated MiniMax native image + four tools; cheap ops ungated, H3 gated; chat preserved; native usage once; ${quotaReads} quota reads + events; ${requests} mocked requests, no live calls`, ); } finally { await session.extensionRunner.emit({ type: "session_shutdown", reason: "quit", }); session.dispose(); await session.settingsManager.flush(); } } finally { await rm(root, { recursive: true, force: true }); }