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

917 B
Raw Blame
/** Preserve the existing #13095 bound for native and legacy output. */
export const MAX_BUFFER_CHARS = 1_048_576;
const TRUNCATION_NOTICE = "\n[...output truncated...]\n";

export function appendCapped(buffer: string, chunk: string): string {
  const combined = buffer + chunk;
  if (combined.length <= MAX_BUFFER_CHARS) return combined;
  return TRUNCATION_NOTICE + combined.slice(-(MAX_BUFFER_CHARS - TRUNCATION_NOTICE.length));
}

/** Bound an incomplete wire frame before the SDK's JSON line accumulator. */
export function boundedFrames(maxBytes = 2 * MAX_BUFFER_CHARS) {
  let pending = 0;
  return new TransformStream<Uint8Array, Uint8Array>({
    transform(chunk, controller) {
      for (const byte of chunk) {
        pending = byte === 10 ? 0 : pending + 1;
        if (pending > maxBytes) throw new Error("ACP frame exceeded the transport limit");
      }
      controller.enqueue(chunk);
    },
  });
}