import type { AgentToolResult, ExtensionAPI, } from "@earendil-works/pi-coding-agent"; import { Type } from "typebox"; import { credentials, region } from "./access.ts"; import { object, request } from "./api.ts"; import { availableOutput, persist } from "./artifacts.ts"; import { dbg } from "./debug.ts"; import { estimatedUsage, PRICE_SOURCE } from "./images.ts"; import { observeQuota, quotaLine } from "./quota.ts"; import { renderCall, renderResult } from "./render.ts"; import { estimate, failure, type MediaDetails, meta, outputSchema, result, } from "./tools.ts"; export function registerSpeech( pi: ExtensionAPI, lifetime: () => AbortSignal, ): void { pi.registerTool({ name: "minimax_speech", label: "MiniMax speech", description: "Synthesize M Plan speech-2.8-turbo MP3, text ≤10000 chars, stock voice ID (default English_Graceful_Lady). Absolute caller-authorized outputPath required; never overwrites. No cloning, voice creation, or live Voice service. Uses the shared M Plan allowance; no approval dialog.", parameters: Type.Object( { text: Type.String({ minLength: 1, maxLength: 10000 }), outputPath: Type.String({ minLength: 1, maxLength: 4096 }), voiceId: Type.Optional(Type.Literal("English_Graceful_Lady")), }, { additionalProperties: false }, ), outputSchema, renderCall: renderCall("speech"), renderResult: renderResult("speech"), async execute(_id, params, signal, update, ctx) { let completed: AgentToolResult | undefined; try { const owned = AbortSignal.any([ lifetime(), ...(signal ? [signal] : []), ]); region(pi, ctx); if (!params.text.trim()) throw new Error("Speech text must be nonempty."); await availableOutput(params.outputPath); const key = await credentials(ctx, owned); update?.({ content: [{ type: "text", text: "Synthesizing speech…" }], details: {}, }); const response = await request( key, "/v1/t2a_v2", { model: "speech-2.8-turbo", text: params.text, stream: false, voice_setting: { voice_id: params.voiceId ?? "English_Graceful_Lady", }, audio_setting: { format: "mp3", sample_rate: 16000, bitrate: 32000, channel: 1, }, output_format: "hex", }, owned, ); const data = object(response["data"]), info = object(response["extra_info"]); const hex = data["audio"]; if ( data["status"] !== 2 || typeof hex !== "string" || !hex.length || hex.length > 24 * 1024 * 1024 || hex.length % 2 || !/^[0-9a-f]+$/i.test(hex) ) throw new Error( "MiniMax speech response incomplete/invalid/oversized; no retry.", ); const audio = Buffer.from(hex, "hex"); const characters = info["usage_characters"]; const known = typeof characters === "number" && Number.isFinite(characters) && characters > 0; const providerMeasurements: Record = {}; for (const k of [ "usage_characters", "audio_length", "audio_sample_rate", "audio_size", "bitrate", "word_count", "audio_channel", ]) if (typeof info[k] === "number" && Number.isFinite(info[k])) providerMeasurements[k] = info[k]; completed = result("MiniMax speech completed; MP3 not yet saved.", { operation: "speech", state: "completed", model: "speech-2.8-turbo", access: "observed-this-operation", requestedCharacters: params.text.length, providerMeasurements, providerMeasurementsSource: "extra_info", encodedDuration: "not measured", accounting: { kind: known ? "catalog-estimate" : "unknown", ...(known ? { usd: characters * 0.00006, unit: "provider-reported character", units: characters, rate: 0.00006, source: PRICE_SOURCE, observedAt: "2026-11-09", } : {}), tokens: "not reported", providerCharge: "unknown", creditsConsumed: "unknown", }, }); if (known) completed.usage = estimatedUsage(characters * 0.00006); await persist(params.outputPath, audio, owned); dbg?.("request.finish", { operation: "speech", success: true }); const quota = await observeQuota(pi, key, owned); return { ...completed, ...result( meta( `Saved MP3 → ${params.outputPath}`, `${known ? characters : params.text.length} chars`, known ? estimate(characters * 0.00006) : "est unknown", quotaLine(quota), ), { ...completed.details, artifact: { path: params.outputPath, format: "mp3", bytes: audio.length, expiry: "local file; no remote expiry", }, ...(quota ? { quota } : {}), }, ), }; } catch (error) { if (!completed) return failure(error); const warning = "Synthesis completed; do not retry generation automatically."; return { ...failure( error, { ...completed.details, state: "completed-artifact-not-persisted", warning, }, warning, ), ...(completed.usage ? { usage: completed.usage } : {}), }; } }, }); }