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

970 B
Raw Blame
import { appendBoundedText } from "./streamHelpers.ts";

type ClaudeDeltaState = {
  accumulatedContent?: string;
  accumulatedReasoning?: string;
};

export function collectClaudeDelta(delta: unknown, state?: ClaudeDeltaState) {
  const record =
    delta && typeof delta === "object" && !Array.isArray(delta)
      ? (delta as Record<string, unknown>)
      : {};
  const text = record.text;
  const thinking = record.thinking;
  let contentLength = 0;

  if (typeof text === "string" && text) {
    contentLength += text.length;
    if (state?.accumulatedContent !== undefined)
      state.accumulatedContent = appendBoundedText(state.accumulatedContent, text);
  }
  if (typeof thinking === "string" && thinking) {
    contentLength += thinking.length;
    if (state?.accumulatedReasoning !== undefined)
      state.accumulatedReasoning = appendBoundedText(state.accumulatedReasoning, thinking);
  }

  return { contentLength, hasText: typeof text === "string" };
}