import { AXIS_COMMANDS } from "./axis-contract.mjs"; export const COMMAND_ACTIONS = Object.freeze({ ...Object.fromEntries(Object.entries(AXIS_COMMANDS).map(([action, command]) => [command, action])), VISUAL_CLEAR: "vismachClear" }); export class AxisOperationController { constructor(client) { if (!client || typeof client.send !== "function") throw new Error("axis_operation_client_required"); this.client = client; this.counter = 0; this.trace = []; } nextOperationId(kind) { this.counter += 1; return `axis-${String(kind).toLowerCase().replace(/[^a-z0-9]+/g, "-")}-${String(this.counter).padStart(6, "0")}`; } async execute(kind, payload = {}, { state = {}, action = COMMAND_ACTIONS[kind] ?? null, gate = true } = {}) { const operationId = this.nextOperationId(kind); const effectiveGate = state?.gates?.[action] ?? { action, enabled: true, reason: "linuxcnc_wasm_worker_authority" }; this.trace.push(Object.freeze({ operation_id: operationId, kind, action, phase: "pending", reason: effectiveGate.reason })); try { const response = await this.client.send(kind, payload, operationId); const responsePayload = response?.payload && typeof response.payload === "object" ? response.payload : {}; const workerBlocked = response?.kind === "ERROR" || responsePayload.status === "blocked"; this.trace.push(Object.freeze({ operation_id: operationId, kind, action, phase: workerBlocked ? "blocked" : "result", reason: responsePayload.gate_reason ?? responsePayload.message_key ?? responsePayload.code ?? response?.kind ?? null })); return Object.freeze({ operationId, response, gate: effectiveGate }); } catch (error) { this.trace.push(Object.freeze({ operation_id: operationId, kind, action, phase: "error", reason: error?.message ?? String(error) })); throw error; } } exportTrace() { return structuredClone(this.trace); } }