import { createHash } from "node:crypto"; import { constants, type Stats } from "node:fs"; import { chmod, mkdtemp, open, rm, writeFile } from "node:fs/promises"; import { homedir, tmpdir } from "node:os"; import { isAbsolute, join, resolve } from "node:path"; import { parseAudioSendArgs } from "./workstation-audio.ts"; import { canonicalSchedulerHandoffJson } from "./workstation-scheduler-json.ts"; export { canonicalJson } from "./workstation-scheduler-json.ts"; const MAX_HANDOFF_BYTES = 64 * 1024; const PROVIDER_ID = "workstation-inference"; const MODEL_ID = "inkling-small-iq2m-canary"; export const LEGACY_SCHEDULER_HANDOFF_PROFILE_ID = "inkling-tts-canary"; const DEPLOYMENT_ID = "workstation-capability-graph"; const STEP_ID = "inkling-small:0"; export type RecordValue = Record; export type SchedulerHandoff = { handoffPath: string; snapshotDirectory: string; schedulerDb: string; handoffDigest: string; claimGeneration: number; claimExpiresAt: number; attemptNonce: string; consumerId: "pi:audio-turn"; operationKey: "pi:inkling-audio"; reservationId: string; reservationGeneration: number; profileId: typeof LEGACY_SCHEDULER_HANDOFF_PROFILE_ID; graphStepId: typeof STEP_ID; providerId: typeof PROVIDER_ID; modelId: typeof MODEL_ID; }; export type GovernedAudioSend = { path: string; prompt: string; handoffPath: string; schedulerDb: string; }; export function isRecord(value: unknown): value is RecordValue { return typeof value === "object" && value !== null && !Array.isArray(value); } function expandPath(value: string, cwd: string): string { const expanded = value === "~" ? homedir() : value.startsWith("~/") ? join(homedir(), value.slice(2)) : value; return isAbsolute(expanded) ? expanded : resolve(cwd, expanded); } function sameFile(before: Stats, after: Stats): boolean { return ( before.dev === after.dev && before.ino === after.ino && before.size === after.size && before.mtimeMs === after.mtimeMs ); } function commandWords(value: string): string[] { const words: string[] = []; let current = ""; let quote: "'" | '"' | undefined; let escaped = false; const push = () => { if (current) words.push(current); current = ""; }; for (const character of value.trim()) { if (escaped) { current += character; escaped = false; continue; } if (character === "\\" && quote !== "'") { escaped = true; continue; } if (quote) { if (character === quote) quote = undefined; else current += character; continue; } if (character === "'" || character === '"') { quote = character; continue; } if (/\s/.test(character)) push(); else current += character; } if (escaped || quote) throw new Error("audio-send has an unterminated quote or escape"); push(); return words; } export function parseGovernedAudioSendArgs(raw: string, cwd: string): GovernedAudioSend { const separator = raw.indexOf(" -- "); const command = separator >= 0 ? raw.slice(0, separator) : raw; const prompt = separator >= 0 ? raw.slice(separator + 4) : ""; const words = commandWords(command); let handoff: string | undefined; let schedulerDb: string | undefined; const remaining: string[] = []; for (let index = 0; index < words.length; index += 1) { const word = words[index]; if (word === "--handoff" || word === "--scheduler-db") { const next = words[index + 1]; if (!next) throw new Error(`${word} requires a path`); if (word === "--handoff") handoff = next; else schedulerDb = next; index += 1; } else if (word.startsWith("--handoff=")) { handoff = word.slice("--handoff=".length); } else if (word.startsWith("--scheduler-db=")) { schedulerDb = word.slice("--scheduler-db=".length); } else { remaining.push(word); } } if (!handoff || !schedulerDb || remaining.length !== 1) { throw new Error( "usage: audio-send --handoff --scheduler-db