/** * Open Code handler. * * Host: `opencode.ai` (Zen endpoint family). * Format: OpenAI-compatible Chat Completions — `body.model` is rewritten to * the mapped target and the request is forwarded to the OmniRoute router. */ import type { IncomingMessage, ServerResponse } from "node:http"; import type { AgentId } from "../types"; import { MitmHandlerBase } from "./base"; export class OpenCodeHandler extends MitmHandlerBase { readonly agentId: AgentId = "open-code"; async intercept( req: IncomingMessage, res: ServerResponse, body: Buffer, mappedModel: string, ): Promise { const startedAt = this.now(); const intercepted = await this.hookBufferStart(req, body, mappedModel); try { const payload = JSON.parse(body.toString()); payload.model = mappedModel; const upstreamStart = this.now(); const upstream = await this.fetchRouter(payload, "/v1/chat/completions", req.headers); if (!upstream.ok) { const errText = await upstream.text().catch(() => ""); throw new Error(`OmniRoute ${upstream.status}: ${errText}`); } let collected = ""; await this.pipeSSE(upstream, res, (chunk) => { collected += chunk.toString(); }); const total = this.now() - startedAt; this.hookBufferUpdate(intercepted, { status: upstream.status, responseHeaders: Object.fromEntries(upstream.headers.entries()), responseBody: collected, responseSize: Buffer.byteLength(collected), proxyLatencyMs: upstreamStart - startedAt, upstreamLatencyMs: total - (upstreamStart - startedAt), }); } catch (err) { await this.hookBufferError(intercepted, err); await this.writeError(res, err); } } }