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

1.7 KB
Raw Blame
/**
 * #12398 — decides whether a Claude-format stream must be aborted with an
 * upstream error at flush time because the client got no usable content.
 *
 * Covers two shapes:
 *  - "partial lifecycle": message_start (and optionally message_delta /
 *    message_stop) arrived but no content block ever did — this was already
 *    correctly handled before #12398 and is preserved here unchanged.
 *  - "truly empty": the upstream connection closed having sent literally
 *    zero bytes (HTTP 200, not even a message_start). The lifecycle flags
 *    above can never catch this shape since none of them are ever set — the
 *    caller must additionally know whether ANY upstream chunk ever arrived.
 *
 * Callers must additionally require a Claude-format client (this function
 * does not take that flag — both call sites in stream.ts only ever reach
 * here already scoped to a Claude-format response).
 */
type ClaudeEmptyLifecycleLike = {
  hasError: boolean;
  hasContentBlock: boolean;
  hasMessageStart: boolean;
  hasMessageDelta: boolean;
  hasMessageStop: boolean;
  stopReason?: string | null;
};

const CLEAN_EMPTY_STOP_REASONS = new Set(["end_turn", "stop_sequence"]);

export function shouldAbortEmptyClaudeStream(
  lifecycle: ClaudeEmptyLifecycleLike,
  sawAnyUpstreamPayload: boolean
): boolean {
  if (lifecycle.hasError || lifecycle.hasContentBlock) return false;
  if (
    sawAnyUpstreamPayload &&
    lifecycle.hasMessageStop &&
    CLEAN_EMPTY_STOP_REASONS.has(lifecycle.stopReason || "")
  ) {
    return false;
  }
  const hasPartialLifecycle =
    lifecycle.hasMessageStart || lifecycle.hasMessageDelta || lifecycle.hasMessageStop;
  return hasPartialLifecycle || !sawAnyUpstreamPayload;
}