Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

3.2 KB
Raw Blame
"use client";
import { useCallback, useState } from "react";
import { previewToRunModel, type CompressionRunModel, type PreviewResponse } from "@/app/(dashboard)/dashboard/compression/studio/compressionFlowModel";
export interface PreviewMessage { role: string; content: unknown; }
export interface Lane { engine: string; run: CompressionRunModel | null; error: string | null; }
export interface PreviewBatch { lanes: Lane[]; combined: CompressionRunModel | null; combinedError: string | null; diff: PreviewResponse["diff"] | null; riskGate: PreviewResponse["riskGate"] | null; heatmap: PreviewResponse["heatmap"] | null; }
export interface RunPreviewArgs { messages: PreviewMessage[]; laneEngines: string[]; activeEngines: string[]; language?: string; fidelityGate?: boolean; fuzzyDedup?: boolean; riskGate?: boolean; quantumLock?: boolean; heatmap?: "ultra" | "universal"; }
async function postPreview(payload: Record<string, unknown>): Promise<PreviewResponse> {
  const res = await fetch("/api/compression/preview", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(payload) });
  const data = await res.json();
  if (!res.ok) throw new Error(data.error ?? "Preview failed");
  return data as PreviewResponse;
}
export async function runPreviewBatch(args: RunPreviewArgs): Promise<PreviewBatch> {
  const { messages, laneEngines, activeEngines, fidelityGate, fuzzyDedup, riskGate, quantumLock, heatmap } = args;
  const extra = {
    ...(fidelityGate ? { fidelityGate: { enabled: true } } : {}),
    ...(fuzzyDedup ? { fuzzyDedup: { enabled: true } } : {}),
    ...(riskGate ? { riskGate: { enabled: true } } : {}),
    ...(quantumLock ? { quantumLock: { enabled: true } } : {}),
    ...(heatmap ? { heatmap } : {}),
  };
  const lanes: Lane[] = await Promise.all(
    laneEngines.map(async (engine): Promise<Lane> => {
      try { const res = await postPreview({ messages, engineId: engine, ...extra }); return { engine, run: previewToRunModel(res, engine), error: null }; }
      catch (e) { return { engine, run: null, error: e instanceof Error ? e.message : "error" }; }
    })
  );
  let combined: CompressionRunModel | null = null;
  let combinedError: string | null = null;
  let diff: PreviewResponse["diff"] | null = null;
  let riskGateStats: PreviewResponse["riskGate"] | null = null;
  let heatmapResult: PreviewResponse["heatmap"] | null = null;
  if (activeEngines.length > 0) {
    try { const res = await postPreview({ messages, pipeline: activeEngines, ...extra }); combined = previewToRunModel(res, activeEngines.join(" → ")); diff = res.diff; riskGateStats = res.riskGate ?? null; heatmapResult = res.heatmap ?? null; }
    catch (e) { combined = null; combinedError = e instanceof Error ? e.message : "error"; }
  }
  return { lanes, combined, combinedError, diff, riskGate: riskGateStats, heatmap: heatmapResult };
}
export function usePreviewCompression() {
  const [batch, setBatch] = useState<PreviewBatch | null>(null);
  const [loading, setLoading] = useState(false);
  const run = useCallback(async (args: RunPreviewArgs) => { setLoading(true); try { setBatch(await runPreviewBatch(args)); } finally { setLoading(false); } }, []);
  return { batch, loading, run };
}