import { createHash } from "node:crypto"; import type { Usage } from "@earendil-works/pi-ai"; import type { AgentToolResult, ExtensionAPI, ExtensionContext, } from "@earendil-works/pi-coding-agent"; import { Type } from "typebox"; import { approveVideo, credentials, region } from "./access.ts"; import { ApiError, object, request } from "./api.ts"; import { availableOutput, download, persist } from "./artifacts.ts"; import { dbg } from "./debug.ts"; import { estimatedUsage, PRICE_SOURCE } from "./images.ts"; import { PREVIEW_KEY, type Preview, posterStrip } from "./preview.ts"; import { observeQuota, quotaLine } from "./quota.ts"; import { renderCall, renderResult } from "./render.ts"; import { estimate, failure, type MediaDetails, meta, outputSchema, result, } from "./tools.ts"; const ENTRY = "minimax.h3.job.v1"; type State = | "submitting" | "pending" | "completed" | "failed" | "cancelled" | "deleted" | "unknown"; interface Job { owner: string; account: string; job: string; taskId?: string; state: State; requestedDuration: number; resolution: "768P" | "2K"; usageCallId?: string; usageRootId?: string; failureKind?: string; observedAt: number; } function account(key: string): string { return createHash("sha256").update(key).digest("hex"); } function ownedJobs(ctx: ExtensionContext): Map { const jobs = new Map(); for (const entry of ctx.sessionManager.getEntries()) { if (entry.type !== "custom" || entry.customType !== ENTRY) continue; const data = object(entry.data); if ( data["owner"] !== ctx.sessionManager.getSessionId() || typeof data["job"] !== "string" || typeof data["account"] !== "string" ) continue; const job = { ...data } as unknown as Job; if (job.state === "submitting") job.state = "unknown"; jobs.set(job.job, job); } return jobs; } function details(job: Job): MediaDetails { return { operation: "video", job: job.job, ...(job.taskId ? { taskId: job.taskId } : {}), state: job.state, ...(job.failureKind ? { failureKind: job.failureKind } : {}), model: "MiniMax-H3", requestedDuration: job.requestedDuration, resolution: job.resolution, observedAt: job.observedAt, expiry: "upstream retrieval expiry unknown; query covers last seven days", providerCharge: "unknown", creditsConsumed: "unknown", encodedDuration: "not measured", ...(job.state === "deleted" ? { accounting: { kind: "unknown", reason: "Task may have completed before record deletion; usage cannot be recovered. No zero-charge or refund claim.", }, } : {}), }; } /** One model-facing line per job: identity, state, artifact. */ function line(job: Job, note?: string, artifact?: string): string { return meta( `H3 job ${job.job}`, job.taskId ? `task ${job.taskId}` : "no task id", job.state + (artifact ? ` → ${artifact}` : ""), note, ); } export function registerVideo( pi: ExtensionAPI, lifetime: () => AbortSignal, ): void { const busy = new Set(); const offered = new Set(); const parents = new Map(); // Session ids granted "Allow H3 for this session"; memory only. const allowed = new Set(); pi.on("session_start", () => { offered.clear(); parents.clear(); allowed.clear(); }); pi.on("turn_start", () => { offered.clear(); parents.clear(); }); pi.on("tool_call", (event) => { if (event.parentToolCallId) parents.set(event.toolCallId, event.parentToolCallId); }); pi.on("tool_result", (event, ctx) => { let root = event.parentToolCallId ?? event.toolCallId; for (let depth = 0; depth < 128 && parents.has(root); depth++) root = parents.get(root) ?? root; parents.delete(event.toolCallId); if (event.toolName !== "minimax_video" || !event.usage) return; const data = object(event.details); if (data["operation"] !== "video" || typeof data["job"] !== "string") return; const job = ownedJobs(ctx).get(data["job"]); if (!job || job.taskId !== data["taskId"]) return; // Identity-only receipt for a finalized native result, not a second cost ledger. // Pi aggregates ALL nested usage, including calls omitted from its bounded record. job.usageRootId = root; try { save(job, ctx); } catch { /* Preserve native result if session persistence fails. */ } }); function accounted(ctx: ExtensionContext, job: Job): boolean { if (offered.has(job.owner + ":" + job.job)) return true; return ctx.sessionManager.getEntries().some((entry) => { if ( entry.type !== "message" || entry.message.role !== "toolResult" || !entry.message.usage ) return false; const msg = entry.message, data = object(msg.details); if (job.usageRootId === msg.toolCallId) return true; if (data["operation"] === "video" && data["taskId"] === job.taskId) return true; return ( !!job.usageCallId && !!msg.nestedCalls?.calls.some( (call) => call.id === job.usageCallId && call.name === "minimax_video" && call.status !== "unfinished", ) ); }); } function deliver( value: AgentToolResult, usage: Usage | undefined, job: Job | undefined, id: string, ctx: ExtensionContext, ): AgentToolResult { if (!usage || !job) return value; job.usageCallId = id; try { save(job, ctx); } catch { /* Native result still carries estimate; persistence failure must not mask it. */ } offered.add(job.owner + ":" + job.job); return { ...value, usage }; } function save(job: Job, ctx: ExtensionContext): void { if (ctx.sessionManager.getSessionId() !== job.owner) throw new Error( "MiniMax session changed; job belongs to original session.", ); pi.appendEntry(ENTRY, { ...job }); } pi.registerTool({ name: "minimax_video", label: "MiniMax H3 video", description: "Owned resumable M Plan MiniMax-H3 text-to-video. submit requires unique job name + prompt; duration 4–15s (default 4), resolution 768P|2K, ratio default 1:1. Go has no included H3; Explore/Build documented, account access unverified. Each submit asks the user (once or for the session) unless USER setting minimax.videoApproval=always; noninteractive submit needs that setting. Identity persisted before any observation. status performs ONE bounded query, never submits. list discovers owned jobs locally. cancel only queued work, may race with terminal record deletion; local abort never promises remote cancellation/refund. retrieve requires authorized absolute outputPath, HTTPS ≤128 MiB, no overwrite. References/variants unverified.", parameters: Type.Object( { action: Type.Union([ Type.Literal("submit"), Type.Literal("status"), Type.Literal("cancel"), Type.Literal("retrieve"), Type.Literal("list"), ]), job: Type.Optional(Type.String({ pattern: "^[A-Za-z0-9_-]{1,80}$" })), prompt: Type.Optional(Type.String({ minLength: 1, maxLength: 7000 })), duration: Type.Optional(Type.Integer({ minimum: 4, maximum: 15 })), resolution: Type.Optional( Type.Union([Type.Literal("768P"), Type.Literal("2K")]), ), ratio: Type.Optional( Type.Union([ Type.Literal("21:9"), Type.Literal("16:9"), Type.Literal("4:3"), Type.Literal("1:1"), Type.Literal("3:4"), Type.Literal("9:16"), ]), ), outputPath: Type.Optional( Type.String({ minLength: 1, maxLength: 4096 }), ), }, { additionalProperties: false }, ), outputSchema, renderCall: renderCall("video"), renderResult: renderResult("video"), async execute(_id, params, signal, update, ctx) { const owned = AbortSignal.any([lifetime(), ...(signal ? [signal] : [])]); let current: Job | undefined; let usage: Usage | undefined; let lock: string | undefined; const measurements: Record = {}; let accounting: MediaDetails | undefined; try { const jobs = ownedJobs(ctx); if (params.action === "list") return result( jobs.size ? [...jobs.values()].map((job) => line(job)).join("\n") : "No owned H3 jobs in this session.", { jobs: [...jobs.values()].map(details), networkRequests: 0 }, ); if (!params.job) throw new Error( "MiniMax video requires job name; use list to recover owned jobs.", ); const lockKey = ctx.sessionManager.getSessionId() + ":" + params.job; if (busy.has(lockKey)) throw new Error( "MiniMax job already being operated on; no concurrent mutation.", ); lock = lockKey; busy.add(lockKey); region(pi, ctx); const key = await credentials(ctx, owned); const identity = account(key); if (params.action === "submit") { const existing = jobs.get(params.job); if (existing) return result( line( existing, "already recorded; not resubmitted. Use status/retrieve.", ), details(existing), ); if (!params.prompt?.trim()) throw new Error("H3 submit requires nonempty prompt."); if (params.outputPath) throw new Error( "Use retrieve with outputPath after submit; submission never waits for artifact.", ); await approveVideo( pi, ctx, { duration: params.duration ?? 4, resolution: params.resolution ?? "768P", }, allowed, owned, ); update?.({ content: [{ type: "text", text: "Submitting H3 job…" }], details: {}, }); current = { owner: ctx.sessionManager.getSessionId(), account: identity, job: params.job, state: "submitting", requestedDuration: params.duration ?? 4, resolution: params.resolution ?? "768P", observedAt: Date.now(), }; // Intent precedes consuming request: interruption must not silently duplicate work. save(current, ctx); const response = await request( key, "/v2/video_generation", { model: "MiniMax-H3", content: [{ type: "text", text: params.prompt }], duration: current.requestedDuration, resolution: current.resolution, ratio: params.ratio ?? "1:1", }, owned, ); if ( typeof response["task_id"] !== "string" || !/^[A-Za-z0-9_-]{1,128}$/.test(response["task_id"]) ) throw new Error( "H3 acknowledgment lacks valid task identity; completion unknown. Do not resubmit.", ); current.taskId = response["task_id"]; current.state = "pending"; current.observedAt = Date.now(); // Durable task identity before returning, never wait inside submission. save(current, ctx); dbg?.("request.finish", { operation: "video", success: true }); const quota = await observeQuota(pi, key, owned); return result(line(current, quotaLine(quota)), { ...details(current), ...(quota ? { quota } : {}), }); } if ( params.prompt !== undefined || params.duration !== undefined || params.resolution !== undefined || params.ratio !== undefined ) throw new Error("Generation settings apply only to submit."); current = jobs.get(params.job); if (!current) throw new Error( "MiniMax job not owned by this session; no remote request sent. Use list.", ); if (current.account !== identity) throw new Error( "MiniMax credentials changed since submission; restore original account before observing job.", ); if (!current.taskId) return result( line( current, current.state === "failed" ? "submission rejected; no automatic retry." : "submission completion unknown; do not resubmit automatically.", ), details(current), ); if (current.state === "deleted" || current.state === "cancelled") return result(line(current, "no further request."), details(current)); if (params.action === "retrieve") { if (!params.outputPath) throw new Error("Retrieve requires explicit absolute outputPath."); await availableOutput(params.outputPath); } else if (params.outputPath) throw new Error("outputPath applies only to retrieve."); const wasCompleted = current.state === "completed"; update?.({ content: [{ type: "text", text: "Querying H3 job…" }], details: {}, }); const response = await request( key, `/v2/query/video_generation/${encodeURIComponent(current.taskId)}`, undefined, owned, "GET", ); const task = object(response["task"]); if (task["id"] !== current.taskId || task["model"] !== "MiniMax-H3") throw new Error( "MiniMax task identity/model mismatch; observation refused.", ); const state = task["status"]; current.state = state === "queued" || state === "running" ? "pending" : state === "succeeded" ? "completed" : state === "failed" ? "failed" : state === "cancelled" ? "cancelled" : "unknown"; current.observedAt = Date.now(); const reported = object(task["usage"]); for (const name of [ "total_seconds", "input_seconds", "output_seconds", "input_image_count", "total_tokens", "prompt_tokens", "completion_tokens", ]) if ( typeof reported[name] === "number" && Number.isFinite(reported[name]) && reported[name] >= 0 ) measurements[name] = reported[name]; const seconds = measurements["output_seconds"]; // Price only reported output seconds for known text-only job; token equivalents are NOT chat tokens. const known = current.state === "completed" && seconds !== undefined && seconds > 0 && measurements["input_seconds"] === 0 && measurements["input_image_count"] === 0; const resolution = task["resolution"] === "768P" || task["resolution"] === "2K" ? task["resolution"] : current.resolution; const rate = resolution === "768P" ? 0.08 : 0.13; accounting = { kind: known ? "catalog-estimate" : "unknown", ...(known ? { usd: seconds * rate, units: seconds, unit: "provider-reported output second", rate, source: PRICE_SOURCE, rateBasis: task["resolution"] === resolution ? "provider-reported resolution" : "requested resolution; provider resolution not reported", observedAt: "2026-11-09", } : {}), providerCharge: "unknown", creditsConsumed: "unknown", tokenEquivalents: "preserved as provider measurements; not chat-priced", }; if (known && !accounted(ctx, current)) usage = estimatedUsage(seconds * rate); save(current, ctx); // First completed observation is when allowance consumption becomes visible. const quota = current.state === "completed" && !wasCompleted ? await observeQuota(pi, key, owned) : undefined; // Mention the estimate only where native usage carries it; repeats say so. const note = meta( usage && typeof accounting["usd"] === "number" ? estimate(accounting["usd"]) : known ? "counted earlier" : undefined, quotaLine(quota), ); let res = result(line(current, note || undefined), { ...details(current), ...(quota ? { quota } : {}), providerStatus: typeof state === "string" && ["queued", "running", "succeeded", "failed", "cancelled"].includes( state, ) ? state : "unrecognized", providerMeasurements: measurements, providerMeasurementsSource: "task.usage", providerSettings: { resolution: task["resolution"] === "768P" || task["resolution"] === "2K" ? task["resolution"] : "not reported", durationSeconds: typeof task["duration"] === "number" && Number.isFinite(task["duration"]) ? task["duration"] : "not reported", }, accounting, ...(known ? { accountingDelivery: usage ? "first observation" : "already emitted; not recounted", } : {}), }); if (params.action === "cancel") { if (state !== "queued") return deliver( { ...res, content: [ { type: "text", text: line( current, "remote cancellation unavailable: only queued tasks cancel; local cancellation does not stop remote work.", ), }, ], }, usage, current, _id, ctx, ); const deleted = await request( key, `/v2/video_generation/${encodeURIComponent(current.taskId)}`, undefined, owned, "DELETE", ); if ( deleted["task_id"] !== current.taskId || !["cancelled", "deleted"].includes(String(deleted["action"])) ) throw new Error( "MiniMax cancellation result unrecognized; remote outcome unknown.", ); current.state = deleted["action"] === "cancelled" ? "cancelled" : "deleted"; current.observedAt = Date.now(); save(current, ctx); res = result(line(current, "upstream action; no refund claim."), { ...details(current), upstreamAction: deleted["action"], warning: "Cancellation may race with terminal record deletion; no refund claim.", }); } if (params.action === "retrieve" && current.state === "completed") { const url = object(task["content"])["url"]; if (typeof url !== "string") throw new Error( "Completed H3 task has no artifact URL; do not regenerate.", ); const bytes = await download(url, 128 * 1024 * 1024, owned); if (bytes.length < 12 || bytes.subarray(4, 8).toString() !== "ftyp") throw new Error( "H3 artifact is not recognized MP4; no regeneration.", ); if (!params.outputPath) throw new Error("Retrieve destination missing."); await persist(params.outputPath, bytes, owned); // Artifact exists now: preview failure must never fail this retrieve. let preview: Preview | undefined; try { preview = await posterStrip( params.outputPath, current.requestedDuration, owned, ); } catch { preview = undefined; } res = result(line(current, note || undefined, params.outputPath), { ...res.details, artifact: { path: params.outputPath, format: "mp4", bytes: bytes.length, expiry: "local file; upstream expiry unknown", }, ...(preview ? { [PREVIEW_KEY]: preview } : {}), }); } return deliver(res, usage, current, _id, ctx); } catch (error) { if (current && current.state === "submitting") { current.state = error instanceof ApiError && [ "authentication", "entitlement", "allowance-exhausted", "rate-limit", ].includes(error.kind) ? "failed" : "unknown"; if (error instanceof ApiError) current.failureKind = error.kind; current.observedAt = Date.now(); try { save(current, ctx); } catch { /* Original intent remains unknown after reload. */ } } return deliver( failure(error, { ...(current ? details(current) : {}), providerMeasurements: measurements, ...(accounting ? { accounting } : {}), warning: "Local failure/cancellation is not remote cancellation. No automatic submission retry.", }), usage, current, _id, ctx, ); } finally { if (lock) busy.delete(lock); } }, }); }