Files
ocp/unified-chunk.mjs
T
taodengandClaude Opus 4.6 1f1b3871da feat: Phase 1 — modular backend architecture (v4.0-alpha)
Internal refactor: extract BackendAdapter, ModelRegistry, AgentRouter,
SessionManager, and StatsCollector as standalone modules. Claude CLI
logic encapsulated in ClaudeCliAdapter.

External API completely unchanged — all existing endpoints behave
identically. Legacy request path (callClaude/callClaudeStreaming)
untouched. v4 modules load in parallel for validation.

New endpoints:
  GET /backends  — backend health and status
  GET /routing   — agent routing table

Architecture:
  unified-chunk.mjs    — streaming protocol (delta/done/error)
  backends/base.mjs    — BackendAdapter interface
  backends/claude-cli.mjs — Claude CLI adapter (core)
  model-registry.mjs   — model → backend mapping (Layer 1)
  agent-router.mjs     — agent policy routing (Layer 2)
  session-manager.mjs  — conversation session management
  stats-collector.mjs  — per-model/backend/agent metrics

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-25 08:02:53 +10:00

43 lines
1.4 KiB
JavaScript

/**
* UnifiedChunk — the internal streaming protocol for OCP.
*
* All backend adapters MUST convert their native output into this format.
* server.mjs only handles UnifiedChunks → OpenAI SSE conversion.
*/
/**
* @typedef {Object} UnifiedChunk
* @property {"delta"|"done"|"error"} type
* @property {string} [content] — text content (for "delta")
* @property {string} model — canonical model id
* @property {string} backend — backend id (e.g. "claude-cli")
* @property {Object} [usage] — token usage (for "done")
* @property {number} [usage.promptTokens]
* @property {number} [usage.completionTokens]
* @property {Object} [cost] — cost info (for "done")
* @property {number} [cost.value]
* @property {"actual"|"estimated"|"free"} [cost.type]
* @property {string} [error] — error message (for "error")
*/
/**
* Create a delta chunk (streaming content)
*/
export function delta(content, model, backend) {
return { type: "delta", content, model, backend };
}
/**
* Create a done chunk (stream finished)
*/
export function done(model, backend, usage = null, cost = null) {
return { type: "done", model, backend, ...(usage && { usage }), ...(cost && { cost }) };
}
/**
* Create an error chunk
*/
export function error(message, model, backend) {
return { type: "error", error: message, model, backend };
}