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

6.6 KB
Raw Blame
/**
 * Event Bus Types
 *
 * Typed event definitions for the real-time event bus.
 * Each event has a name and a typed payload interface.
 */

// ── Event Names ───────────────────────────────────────────────────────────

export type DashboardEventName =
  | "request.started"
  | "request.streaming"
  | "request.completed"
  | "request.failed"
  | "combo.target.attempt"
  | "combo.target.failed"
  | "combo.target.succeeded"
  | "credential.health.changed"
  | "proxy.set_aside"
  | "proxy.pool.exhausted"
  | "compression.completed"
  | "compression.step"
  | "agent.task.updated";

// ── Event Payloads ────────────────────────────────────────────────────────

export interface RequestStartedPayload {
  id: string;
  model: string;
  provider: string;
  timestamp: number;
  comboName?: string;
}

export interface RequestStreamingPayload {
  id: string;
  tokensGenerated: number;
  elapsedMs: number;
}

export interface RequestCompletedPayload {
  id: string;
  status: "success" | "error";
  model: string;
  provider: string;
  tokensInput: number;
  tokensOutput: number;
  latencyMs: number;
  comboName?: string;
  error?: string;
}

export interface RequestFailedPayload {
  id: string;
  error: string;
  statusCode?: number;
  latencyMs: number;
  model?: string;
  provider?: string;
}

export interface ComboTargetAttemptPayload {
  comboName: string;
  targetIndex: number;
  provider: string;
  model: string;
  timestamp: number;
  strategy: string;
}

export interface ComboTargetFailedPayload {
  comboName: string;
  targetIndex: number;
  provider: string;
  model: string;
  error: string;
  latencyMs: number;
}

export interface ComboTargetSucceededPayload {
  comboName: string;
  targetIndex: number;
  provider: string;
  model: string;
  latencyMs: number;
}

export interface CredentialHealthChangedPayload {
  connectionId: string;
  provider: string;
  oldStatus: string;
  newStatus: string;
  timestamp: number;
}

export interface ProxySetAsidePayload {
  reason: "ip_quota_429" | "proxy_unreachable" | "transport" | "slow" | "geo_blocked";
  setAsideUntil: string;
  durationMs: number;
  egressKeyMasked: string;
  timestamp: number;
}

export interface ProxyPoolExhaustedPayload {
  scope: string;
  poolSize: number;
  setAsideCount: number;
  fallback: "fail-closed-serve";
  timestamp: number;
}

export interface CompressionCompletedPayload {
  requestId: string;
  comboId: string | null;
  mode: string;
  originalTokens: number;
  compressedTokens: number;
  savingsPercent: number;
  engineBreakdown: Array<{
    engine: string;
    originalTokens: number;
    compressedTokens: number;
    savingsPercent: number;
    techniquesUsed: string[];
    rulesApplied?: string[];
    durationMs?: number;
  }>;
  validationWarnings?: string[];
  fallbackApplied?: boolean;
  timestamp: number;
}

/**
 * Mid-pipeline per-engine progress for a stacked compression run (F3.3). Emitted once per
 * engine as it completes, before the final `compression.completed`. The studio accumulates
 * these into a live in-flight run so engines appear as they finish (meaningful for slow async
 * engines like LLMLingua); for sub-ms sync engines they arrive as a burst.
 */
export interface CompressionStepPayload {
  requestId: string;
  comboId: string | null;
  mode: string;
  /** 0-based index of this engine among the resolved pipeline steps. */
  stepIndex: number;
  /** Total resolved pipeline steps (hint for the UI). */
  totalSteps: number;
  engine: string;
  state: "running" | "done" | "skipped";
  /** Tokens entering this engine. */
  originalTokens: number;
  /** Tokens leaving this engine. */
  compressedTokens: number;
  savingsPercent: number;
  techniquesUsed?: string[];
  rulesApplied?: string[];
  durationMs?: number;
  timestamp: number;
}

/**
 * Real-time transition of an orchestrated task tracked by the orchestration dashboard
 * (Fase 2). `source` distinguishes which subsystem raised the transition so the canvas can
 * route it to the right node type.
 */
export interface AgentTaskUpdatedPayload {
  source: "cloud-agent" | "a2a" | "conductor";
  taskId: string;
  state: string;
  timestamp: number;
}

// ── Event Map ─────────────────────────────────────────────────────────────

export interface DashboardEventMap {
  "request.started": RequestStartedPayload;
  "request.streaming": RequestStreamingPayload;
  "request.completed": RequestCompletedPayload;
  "request.failed": RequestFailedPayload;
  "combo.target.attempt": ComboTargetAttemptPayload;
  "combo.target.failed": ComboTargetFailedPayload;
  "combo.target.succeeded": ComboTargetSucceededPayload;
  "credential.health.changed": CredentialHealthChangedPayload;
  "proxy.set_aside": ProxySetAsidePayload;
  "proxy.pool.exhausted": ProxyPoolExhaustedPayload;
  "compression.completed": CompressionCompletedPayload;
  "compression.step": CompressionStepPayload;
  "agent.task.updated": AgentTaskUpdatedPayload;
}

// ── Event Bus Listener ────────────────────────────────────────────────────

export type DashboardEventListener<E extends DashboardEventName> = (
  payload: DashboardEventMap[E]
) => void;

// ── Channel Definitions ───────────────────────────────────────────────────

/** Available subscription channels */
export type DashboardChannel =
  "requests" | "combo" | "credentials" | "proxy" | "compression" | "agents";

/** Map channels to their events */
export const CHANNEL_EVENTS: Record<DashboardChannel, DashboardEventName[]> = {
  requests: ["request.started", "request.streaming", "request.completed", "request.failed"],
  combo: ["combo.target.attempt", "combo.target.failed", "combo.target.succeeded"],
  credentials: ["credential.health.changed"],
  proxy: ["proxy.set_aside", "proxy.pool.exhausted"],
  compression: ["compression.completed", "compression.step"],
  agents: ["agent.task.updated"],
};

/** Get channel for an event */
export function getChannelForEvent(event: DashboardEventName): DashboardChannel | undefined {
  for (const [channel, events] of Object.entries(CHANNEL_EVENTS)) {
    if (events.includes(event)) return channel as DashboardChannel;
  }
  return undefined;
}