claude

package
v8.0.49 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 31, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package claude provides request translation functionality for Claude Code API compatibility. It handles parsing and transforming Claude Code API requests into the internal client format, extracting model information, system instructions, message contents, and tool declarations. The package also performs JSON data cleaning and transformation to ensure compatibility between Claude Code API format and the internal client's expected format.

Package claude provides response translation functionality for Codex to Claude Code API compatibility. This package handles the conversion of Codex API responses into Claude Code-compatible Server-Sent Events (SSE) format, implementing a sophisticated state machine that manages different response types including text content, thinking processes, and function calls. The translation ensures proper sequencing of SSE events and maintains state across multiple response chunks to provide a seamless streaming experience.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ClaudeTokenCount

func ClaudeTokenCount(_ context.Context, count int64) []byte

func ConvertClaudeRequestToCodex

func ConvertClaudeRequestToCodex(modelName string, inputRawJSON []byte, stream bool) ([]byte, error)

ConvertClaudeRequestToCodex parses and transforms a Claude Code API request into the internal client format. It extracts the model name, system instruction, message contents, and tool declarations from the raw JSON request and returns them in the format expected by the internal client. The function performs the following transformations: 1. Sets up a template with the model name and empty instructions field 2. Processes system messages and converts them to developer input content 3. Transforms message contents (text, image, document, tool_use, tool_result) to appropriate formats 4. Converts tools declarations to the expected format 5. Adds additional configuration parameters for the Codex API 6. Maps Claude thinking configuration to Codex reasoning settings 7. Maps Claude output_config format to Codex text format

Parameters:

  • modelName: The name of the model to use for the request
  • rawJSON: The raw JSON request data from the Claude Code API
  • stream: A boolean indicating if the request is for a streaming response (unused in current implementation)

Returns:

  • []byte: The transformed request data in internal client format

func ConvertClaudeRequestToCodexContext added in v8.0.49

func ConvertClaudeRequestToCodexContext(ctx context.Context, modelName string, inputRawJSON []byte, stream bool) ([]byte, error)

func ConvertClaudeRequestToCodexWithCompat

func ConvertClaudeRequestToCodexWithCompat(modelName string, inputRawJSON []byte, stream bool) ([]byte, error)

ConvertClaudeRequestToCodexWithCompat preserves assistant thinking blocks with empty or unknown-format signatures for configured compatibility endpoints.

func ConvertClaudeRequestToCodexWithCompatContext added in v8.0.49

func ConvertClaudeRequestToCodexWithCompatContext(ctx context.Context, modelName string, inputRawJSON []byte, stream bool) ([]byte, error)

func ConvertCodexResponseToClaude

func ConvertCodexResponseToClaude(ctx context.Context, _ string, originalRequestRawJSON, _ []byte, rawJSON []byte, param *any) [][]byte

ConvertCodexResponseToClaude performs sophisticated streaming response format conversion. This function implements a complex state machine that translates Codex API responses into Claude Code-compatible Server-Sent Events (SSE) format. It manages different response types and handles state transitions between content blocks, thinking processes, and function calls.

Response type states: 0=none, 1=content, 2=thinking, 3=function The function maintains state across multiple calls to ensure proper SSE event sequencing.

Parameters:

  • ctx: The context for the request, used for cancellation and timeout handling
  • modelName: The name of the model being used for the response (unused in current implementation)
  • rawJSON: The raw JSON response from the Codex API
  • param: A pointer to a parameter object for maintaining state between calls

Returns:

  • [][]byte: A slice of Claude Code-compatible JSON responses

func ConvertCodexResponseToClaudeNonStream

func ConvertCodexResponseToClaudeNonStream(ctx context.Context, _ string, originalRequestRawJSON, _ []byte, rawJSON []byte, _ *any) []byte

ConvertCodexResponseToClaudeNonStream converts a non-streaming Codex response to a non-streaming Claude Code response. This function processes the complete Codex response and transforms it into a single Claude Code-compatible JSON response. It handles message content, tool calls, reasoning content, and usage metadata, combining all the information into a single response that matches the Claude Code API format.

Types

type ConvertCodexResponseToClaudeParams

type ConvertCodexResponseToClaudeParams struct {
	// ShortToOriginalToolNames maps shortened upstream tool names back to the
	// client-declared names. It is resolved once per stream on first tool use;
	// callers pass the same request bytes for the stream's lifetime.
	ShortToOriginalToolNames map[string]string
	ToolNamesResolved        bool
	HasEmittedToolUse        bool
	BlockIndex               int
	HasTextDelta             bool
	TextBlockOpen            bool
	// CommentaryMessageOpen is set while the current `message` output item carries
	// phase "commentary": a preamble GPT-5.x emits before its final_answer item.
	// Its text goes into a thinking block, so clients that render or speak text
	// blocks get the answer once instead of the preamble followed by the answer.
	CommentaryMessageOpen   bool
	PublicCommentaryText    strings.Builder
	VisiblePublicCommentary bool
	ThinkingBlockOpen       bool
	ThinkingSignature       string
	ThinkingSummarySeen     bool
	WebSearchToolUseIDs     map[string]struct{}
	WebSearchToolResultIDs  map[string]struct{}
	LastWebSearchToolUseID  string
	FunctionCalls           map[string]*codexFunctionCallStream
	FunctionCallQueue       []*codexFunctionCallStream
	ActiveFunctionCall      *codexFunctionCallStream
	LastFunctionCall        *codexFunctionCallStream
	DeferredStreamEvents    [][]byte
}

ConvertCodexResponseToClaudeParams holds parameters for response conversion.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL