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

19.9 KB
Raw Blame
import { register } from "../registry.ts";
import { FORMATS } from "../formats.ts";
import { initThinkState, applyThinkTag, flushThinkBuffer } from "../../utils/thinkTagParser.ts";
import { storeClaudeThinkingBlocks } from "../../services/claudeThinkingStore.ts";

type OpenAIUsage = {
  prompt_tokens: number;
  completion_tokens: number;
  total_tokens: number;
  reasoning_tokens?: number;
  prompt_tokens_details?: {
    cached_tokens?: number;
    cache_creation_tokens?: number;
    // false: cache_creation_tokens is not included in prompt_tokens (#2215).
    cache_creation_in_prompt?: boolean;
  };
  completion_tokens_details?: {
    reasoning_tokens?: number;
  };
};

// Create OpenAI chunk helper
function createChunk(state, delta, finishReason = null) {
  return {
    id: `chatcmpl-${state.messageId}`,
    object: "chat.completion.chunk",
    created: Math.floor(Date.now() / 1000),
    model: state.model,
    choices: [
      {
        index: 0,
        delta,
        finish_reason: finishReason,
      },
    ],
  };
}

// Signed-thinking continuity: the OpenAI client only sees reasoning text, so keep the
// exact signed thinking / redacted_thinking blocks (per content-block index) and hand
// them to claudeThinkingStore, keyed by this turn's tool_use ids, when the message ends.
// openai-to-claude restores them before the owning tool_use on the next tool turn.
function thinkingBlocksOf(state): Map<number, Record<string, string>> {
  if (!(state.claudeThinkingBlocks instanceof Map)) state.claudeThinkingBlocks = new Map();
  return state.claudeThinkingBlocks;
}

function flushThinkingContinuity(state) {
  if (state.claudeThinkingFlushed) return;
  state.claudeThinkingFlushed = true;
  const blocks = thinkingBlocksOf(state);
  if (blocks.size === 0 || !(state.toolCalls instanceof Map) || state.toolCalls.size === 0) return;
  storeClaudeThinkingBlocks({
    toolUseIds: [...state.toolCalls.values()].map((toolCall) => toolCall?.id),
    blocks: [...blocks.entries()].sort(([left], [right]) => left - right).map(([, b]) => b),
    provider: state.provider,
    model: state.model,
  });
}

// Convert Claude stream chunk to OpenAI format
export function claudeToOpenAIResponse(chunk, state) {
  if (!chunk) return null;

  const results = [];
  const event = chunk.type;

  switch (event) {
    case "message_start": {
      state.messageId = chunk.message?.id || `msg_${Date.now()}`;
      state.model = chunk.message?.model;
      state.toolCallIndex = 0;
      state.claudeThinkingBlocks = new Map();
      state.claudeThinkingFlushed = false;
      // #13558: MiniMax-M3's Anthropic-compatible endpoint puts its reasoning
      // inline as <think>...</think> inside ordinary text/text_delta blocks
      // instead of a structured thinking/thinking_delta block. Reuse the
      // passthrough-mode think-tag parser here (gated the same way, by
      // shouldParseTextualReasoningTags) so it strips <think> markup out of
      // delta.content and re-emits it as delta.reasoning_content.
      state.thinkState = initThinkState(true, state.provider, state.model);
      const startUsage = chunk.message?.usage;
      if (startUsage && typeof startUsage === "object") {
        const inputTokens =
          typeof startUsage.input_tokens === "number"
            ? startUsage.input_tokens
            : typeof startUsage.prompt_tokens === "number"
              ? startUsage.prompt_tokens
              : 0;
        const outputTokens =
          typeof startUsage.output_tokens === "number"
            ? startUsage.output_tokens
            : typeof startUsage.completion_tokens === "number"
              ? startUsage.completion_tokens
              : 0;
        const cacheRead =
          typeof startUsage.cache_read_input_tokens === "number"
            ? startUsage.cache_read_input_tokens
            : 0;
        const cacheCreation =
          typeof startUsage.cache_creation_input_tokens === "number"
            ? startUsage.cache_creation_input_tokens
            : 0;
        if (inputTokens > 0 || outputTokens > 0 || cacheRead > 0 || cacheCreation > 0) {
          const billableInputTokens = inputTokens + cacheRead;
          state.usage = {
            prompt_tokens: billableInputTokens,
            completion_tokens: outputTokens,
            input_tokens: billableInputTokens,
            output_tokens: outputTokens,
          };
          if (cacheRead > 0) state.usage.cache_read_input_tokens = cacheRead;
          if (cacheCreation > 0) state.usage.cache_creation_input_tokens = cacheCreation;
        }
      }
      results.push(createChunk(state, { role: "assistant" }));
      break;
    }

    case "content_block_start": {
      const block = chunk.content_block;
      if (block?.type === "text") {
        state.textBlockStarted = true;
      } else if (block?.type === "thinking") {
        thinkingBlocksOf(state).set(chunk.index, {
          type: "thinking",
          thinking: typeof block.thinking === "string" ? block.thinking : "",
          signature: typeof block.signature === "string" ? block.signature : "",
        });
        state.inThinkingBlock = true;
        state.currentBlockIndex = chunk.index;
        // Emit empty reasoning_content to signal thinking block start
        // (clients like Claude Code look for reasoning_content, not <think> tags)
        results.push(createChunk(state, { reasoning_content: "" }));
      } else if (block?.type === "redacted_thinking") {
        // Opaque to the client (nothing is emitted); kept only for continuity.
        thinkingBlocksOf(state).set(chunk.index, { type: "redacted_thinking", data: block.data });
      } else if (block?.type === "tool_use") {
        const toolCallIndex = state.toolCallIndex++;
        // Restore original tool name from mapping (Claude OAuth)
        const toolName = state.toolNameMap?.get(block.name) || block.name;
        const toolCall = {
          index: toolCallIndex,
          id: block.id,
          type: "function",
          function: {
            name: toolName,
            arguments: "",
          },
        };
        state.toolCalls.set(chunk.index, toolCall);
        results.push(createChunk(state, { tool_calls: [toolCall] }));
      }
      break;
    }

    case "content_block_delta": {
      const delta = chunk.delta;
      if (delta?.type === "text_delta" && delta.text) {
        // Flush the deferred </think> close marker before the first text delta so
        // clients like Claude Code / Cursor (that scan content for </think>) see it
        // immediately before the assistant reply begins — but NOT in tool-use streams
        // where no text_delta ever arrives (#5123).
        if (state.pendingThinkClose) {
          // Suppressed for clients that render the marker verbatim (#5245);
          // still clear the flag so it never re-fires later in the stream.
          if (!state.suppressThinkClose) {
            results.push(createChunk(state, { content: "</think>" }));
          }
          state.pendingThinkClose = false;
        }
        const textDelta: { content: unknown; reasoning_content?: string } = {
          content: delta.text,
        };
        if (state.thinkState) applyThinkTag(state.thinkState, textDelta);
        if (textDelta.reasoning_content) {
          results.push(createChunk(state, { reasoning_content: textDelta.reasoning_content }));
        }
        if (textDelta.content) {
          results.push(createChunk(state, { content: textDelta.content }));
        }
      } else if (delta?.type === "thinking_delta" && delta.thinking) {
        const tracked = thinkingBlocksOf(state).get(chunk.index);
        if (tracked?.type === "thinking") tracked.thinking += delta.thinking;
        // Map Claude thinking_delta → OpenAI reasoning_content
        // Clients (Claude Code, Cursor, etc.) display reasoning_content as the thinking panel
        results.push(createChunk(state, { reasoning_content: delta.thinking }));
      } else if (delta?.type === "signature_delta" && typeof delta.signature === "string") {
        // Never emitted to the client; kept only for continuity.
        const tracked = thinkingBlocksOf(state).get(chunk.index);
        if (tracked?.type === "thinking") tracked.signature += delta.signature;
      } else if (delta?.type === "input_json_delta" && delta.partial_json) {
        const toolCall = state.toolCalls.get(chunk.index);
        if (toolCall) {
          toolCall.function.arguments += delta.partial_json;
          results.push(
            createChunk(state, {
              tool_calls: [
                {
                  index: toolCall.index,
                  function: { arguments: delta.partial_json },
                },
              ],
            })
          );
        }
      }
      break;
    }

    case "content_block_stop": {
      // #13558: flush any <think>/reasoning text still buffered by the
      // textual think-tag parser (e.g. a block that ends mid-tag, or a
      // reasoning tail with no trailing visible content) before the block
      // closes, so it is never silently dropped.
      if (state.thinkState?.active) {
        const flushed = flushThinkBuffer(state.thinkState);
        if (flushed.reasoningDelta) {
          results.push(createChunk(state, { reasoning_content: flushed.reasoningDelta }));
        }
        if (flushed.contentDelta) {
          results.push(createChunk(state, { content: flushed.contentDelta }));
        }
      }
      if (state.inThinkingBlock && chunk.index === state.currentBlockIndex) {
        // Defer the </think> close marker instead of emitting immediately.
        // If the next block is tool_use there will be no text_delta, so the
        // marker would appear as a spurious assistant text chunk before
        // tool_calls, corrupting OpenAI-compatible clients (#5123).
        // The marker is flushed in the text_delta branch (for pure-text
        // thinking responses — preserving the #4633 / decolua/9router#454
        // behavior) or in the message_delta finish path when no tool_calls
        // were collected.
        state.pendingThinkClose = true;
        state.inThinkingBlock = false;
      }
      state.textBlockStarted = false;
      state.thinkingBlockStarted = false;
      break;
    }

    case "message_delta": {
      // Extract usage from message_delta event (Claude native format)
      // Normalize to OpenAI format (prompt_tokens/completion_tokens) for consistent logging
      if (chunk.usage && typeof chunk.usage === "object") {
        const previousUsage = state.usage && typeof state.usage === "object" ? state.usage : {};
        const previousInputTokens =
          typeof previousUsage.input_tokens === "number"
            ? previousUsage.input_tokens
            : typeof previousUsage.prompt_tokens === "number"
              ? previousUsage.prompt_tokens
              : 0;
        const previousCacheReadTokens =
          typeof previousUsage.cache_read_input_tokens === "number"
            ? previousUsage.cache_read_input_tokens
            : 0;
        const previousCacheCreationTokens =
          typeof previousUsage.cache_creation_input_tokens === "number"
            ? previousUsage.cache_creation_input_tokens
            : 0;
        const inputTokens =
          typeof chunk.usage.input_tokens === "number" ? chunk.usage.input_tokens : 0;
        const outputTokens =
          typeof chunk.usage.output_tokens === "number" ? chunk.usage.output_tokens : 0;
        const thinkingTokens =
          typeof chunk.usage.output_tokens_details?.thinking_tokens === "number"
            ? chunk.usage.output_tokens_details.thinking_tokens
            : undefined;
        const cacheReadTokens =
          typeof chunk.usage.cache_read_input_tokens === "number"
            ? chunk.usage.cache_read_input_tokens
            : 0;
        const cacheCreationTokens =
          typeof chunk.usage.cache_creation_input_tokens === "number"
            ? chunk.usage.cache_creation_input_tokens
            : 0;

        // Use OpenAI format keys for consistent logging in stream.js
        // Issue #1426: Include cache_read tokens in prompt_tokens so cached input
        // is visible to downstream billing systems.
        // Issue #2215: Exclude cache_creation_input_tokens from prompt_tokens —
        // Anthropic's cache-creation pads short prompts up to a 1024-token
        // minimum, so a 2-token "hi" can be reported as ~2008 prompt_tokens and
        // inflate downstream billing ~250x. cache_creation is still exposed
        // separately via prompt_tokens_details.cache_creation_tokens below.
        const billableInputTokens =
          inputTokens > 0 || cacheReadTokens > 0 || cacheCreationTokens > 0
            ? inputTokens + cacheReadTokens
            : previousInputTokens;
        state.usage = {
          prompt_tokens: billableInputTokens,
          completion_tokens: outputTokens,
          input_tokens: billableInputTokens,
          output_tokens: outputTokens,
        };

        // Anthropic includes thinking in output_tokens. Surface the separately
        // reported portion without adding it to completion_tokens a second time.
        if (thinkingTokens !== undefined) {
          state.usage.reasoning_tokens = thinkingTokens;
          state.usage.completion_tokens_details = { reasoning_tokens: thinkingTokens };
          state.usage.output_tokens_details = { thinking_tokens: thinkingTokens };
        }

        // Store cache tokens if present (needed for prompt_tokens_details in final chunk)
        const effectiveCacheReadTokens = cacheReadTokens || previousCacheReadTokens;
        const effectiveCacheCreationTokens = cacheCreationTokens || previousCacheCreationTokens;
        if (effectiveCacheReadTokens > 0) {
          state.usage.cache_read_input_tokens = effectiveCacheReadTokens;
        }
        if (effectiveCacheCreationTokens > 0) {
          state.usage.cache_creation_input_tokens = effectiveCacheCreationTokens;
        }
      }

      if (chunk.delta?.stop_reason) {
        flushThinkingContinuity(state);
        // Flush any deferred </think> close marker now that we know the stream
        // is finishing. Only emit when there are no tool_calls — if tool_calls
        // were collected the marker must stay suppressed (#5123). Text-based
        // responses that had no text_delta (edge case: thinking-only with
        // immediate stop) still receive the marker here.
        if (state.pendingThinkClose && state.toolCalls.size === 0) {
          // Suppressed for clients that render the marker verbatim (#5245).
          if (!state.suppressThinkClose) {
            results.push(createChunk(state, { content: "</think>" }));
          }
          state.pendingThinkClose = false;
        }
        state.finishReason = convertStopReason(chunk.delta.stop_reason);
        const finalChunk: {
          id: string;
          object: string;
          created: number;
          model: string;
          choices: Array<{
            index: number;
            delta: { content?: string };
            finish_reason: string | null;
          }>;
          usage?: OpenAIUsage;
        } = {
          id: `chatcmpl-${state.messageId}`,
          object: "chat.completion.chunk",
          created: Math.floor(Date.now() / 1000),
          model: state.model,
          choices: [
            {
              index: 0,
              delta: {},
              finish_reason: state.finishReason,
            },
          ],
        };

        // Include usage in final chunk if available
        if (state.usage && typeof state.usage === "object") {
          const inputTokens = state.usage.input_tokens || 0;
          const outputTokens = state.usage.output_tokens || 0;
          const cachedTokens = state.usage.cache_read_input_tokens || 0;
          const cacheCreationTokens = state.usage.cache_creation_input_tokens || 0;

          // prompt_tokens = input_tokens (input + cache_read, per #2215 —
          // cache_creation is exposed separately in prompt_tokens_details below).
          // completion_tokens = output_tokens
          // total_tokens = prompt_tokens + completion_tokens
          const promptTokens = inputTokens;
          const completionTokens = outputTokens;
          const totalTokens = promptTokens + completionTokens;

          finalChunk.usage = {
            prompt_tokens: promptTokens,
            completion_tokens: completionTokens,
            total_tokens: totalTokens,
          };

          const reasoningTokens = state.usage.reasoning_tokens;
          if (typeof reasoningTokens === "number") {
            finalChunk.usage.reasoning_tokens = reasoningTokens;
            finalChunk.usage.completion_tokens_details = {
              reasoning_tokens: reasoningTokens,
            };
          }

          // Add prompt_tokens_details if cached tokens exist
          if (cachedTokens > 0 || cacheCreationTokens > 0) {
            finalChunk.usage.prompt_tokens_details = {};
            if (cachedTokens > 0) {
              finalChunk.usage.prompt_tokens_details.cached_tokens = cachedTokens;
            }
            if (cacheCreationTokens > 0) {
              finalChunk.usage.prompt_tokens_details.cache_creation_tokens = cacheCreationTokens;
              finalChunk.usage.prompt_tokens_details.cache_creation_in_prompt = false;
            }
          }
        }

        results.push(finalChunk);
        state.finishReasonSent = true;
      }
      break;
    }

    case "message_stop": {
      flushThinkingContinuity(state);
      if (!state.finishReasonSent) {
        const finishReason =
          state.finishReason || (state.toolCalls?.size > 0 ? "tool_calls" : "stop");
        const cachedTokens = state.usage?.cache_read_input_tokens || 0;
        const cacheCreationTokens = state.usage?.cache_creation_input_tokens || 0;
        const usageObj =
          state.usage && typeof state.usage === "object"
            ? {
                usage: {
                  prompt_tokens: state.usage.input_tokens || 0,
                  completion_tokens: state.usage.output_tokens || 0,
                  total_tokens: (state.usage.input_tokens || 0) + (state.usage.output_tokens || 0),
                  ...(typeof state.usage.reasoning_tokens === "number"
                    ? {
                        reasoning_tokens: state.usage.reasoning_tokens,
                        completion_tokens_details: {
                          reasoning_tokens: state.usage.reasoning_tokens,
                        },
                      }
                    : {}),
                  ...(cachedTokens > 0 || cacheCreationTokens > 0
                    ? {
                        prompt_tokens_details: {
                          ...(cachedTokens > 0 ? { cached_tokens: cachedTokens } : {}),
                          ...(cacheCreationTokens > 0
                            ? {
                                cache_creation_tokens: cacheCreationTokens,
                                cache_creation_in_prompt: false,
                              }
                            : {}),
                        },
                      }
                    : {}),
                },
              }
            : {};
        results.push({
          id: `chatcmpl-${state.messageId}`,
          object: "chat.completion.chunk",
          created: Math.floor(Date.now() / 1000),
          model: state.model,
          choices: [
            {
              index: 0,
              delta: {},
              finish_reason: finishReason,
            },
          ],
          ...usageObj,
        });
        state.finishReasonSent = true;
      }
      break;
    }
  }

  return results.length > 0 ? results : null;
}

// Convert Claude stop_reason to OpenAI finish_reason
function convertStopReason(reason) {
  switch (reason) {
    case "end_turn":
      return "stop";
    case "max_tokens":
      return "length";
    case "tool_use":
      return "tool_calls";
    case "stop_sequence":
      return "stop";
    case "refusal":
      return "content_filter";
    case "model_context_window_exceeded":
      return "length";
    default:
      return "stop";
  }
}

// Register
register(FORMATS.CLAUDE, FORMATS.OPENAI, null, claudeToOpenAIResponse);