helps

package
v8.0.55 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2026 License: MIT Imports: 89 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ClaudeCodeSessionHeader = "X-Claude-Code-Session-Id"
	ClaudeCodeAgentHeader   = "X-Claude-Code-Agent-Id"
	ClaudeCodeMainAgentID   = "main"
)
View Source
const (
	// ConnectFlagData marks an uncompressed Connect-proto data frame.
	ConnectFlagData byte = 0x00
	// ConnectFlagCompressed marks a gzipped Connect-proto frame.
	ConnectFlagCompressed byte = 0x01
	// ConnectFlagEndStream marks the terminal Connect-proto trailer frame.
	ConnectFlagEndStream byte = 0x02

	// DevinDefaultBaseURL is the default upstream Codeium/Devin endpoint.
	DevinDefaultBaseURL = "https://server.codeium.com"
	// DevinChatPath is the Connect-RPC endpoint for chat completions.
	DevinChatPath = "/exa.api_server_pb.ApiServerService/GetChatMessage"

	// DevinDefaultClientName is the client identifier declared in metadata.
	DevinDefaultClientName = "chisel"
	// DevinDefaultClientVersion is the client version declared in metadata.
	DevinDefaultClientVersion = "3000.10.21"
	// DevinFingerprintHexLen is the mandatory 732-hex-character length for metadata #31.
	DevinFingerprintHexLen = 732

	// DevinDefaultMaxTokens is the fallback max completion tokens.
	DevinDefaultMaxTokens = 128000
)
View Source
const (
	// GeminiInteractionsAPIRevision is the Api-Revision header required for the Interactions API.
	GeminiInteractionsAPIRevision = "2026-05-20"
	// StreamScannerBuffer sets the scanner buffer capacity for streaming response chunks (50MB).
	StreamScannerBuffer = 52_428_800
)
View Source
const (
	// DefaultXAIFallbackClientVersion is the stable Grok CLI version used when npm
	// registry resolution fails. dist-tag latest is 1.0.46; 1.0.50 is the alpha tag.
	DefaultXAIFallbackClientVersion = "1.0.46"
	// XAIClientVersionServerFloor is the minimum Grok CLI version cli-chat-proxy accepts.
	// Older clients are rejected with HTTP 426 (#6249).
	XAIClientVersionServerFloor = "1.0.13"
	// XAIVersionRefreshInterval is the periodic interval to check for Grok CLI updates from npm.
	XAIVersionRefreshInterval = 3 * time.Hour
	// XAIVersionFetchTimeout is the maximum duration for a single npm registry lookup.
	XAIVersionFetchTimeout = 10 * time.Second
)
View Source
const ApplyPatchUpstreamErrorMessage = "Invalid apply_patch tool arguments received from upstream."

ApplyPatchUpstreamErrorMessage deliberately excludes upstream JSON and patch text.

View Source
const (
	// CodexEmptyIncompleteStreamMessage is the error message emitted when upstream ChatGPT Codex
	// terminates with response.incomplete having 0 output tokens and 0 content output.
	CodexEmptyIncompleteStreamMessage = "stream error: upstream terminated with incomplete empty response (0 tokens)"
)
View Source
const DefaultTransportCacheCapacity = 64

DefaultTransportCacheCapacity bounds how many transports a TransportCache keeps alive at once. Every cached transport owns an independent connection pool, so an unbounded cache would let idle sockets and the goroutines managing them grow without limit whenever keys churn, for example when a credential's proxy is rotated through the management API or when an SDK embedder supplies a freshly built base transport per request.

Variables

View Source
var (
	ClaudeDeviceProfileBeforeCandidateStore func(ClaudeDeviceProfile)
)

Functions

func APIKeyFromContext

func APIKeyFromContext(ctx context.Context) string

func APIKeyModelIsCompat

func APIKeyModelIsCompat(req cliproxyexecutor.Request) bool

APIKeyModelIsCompat reports whether the selected API-key model enables compatibility handling for Claude thinking blocks.

func AccessProviderFromContext added in v8.0.24

func AccessProviderFromContext(ctx context.Context) string

AccessProviderFromContext extracts the client authentication provider identifier from the context.

func AdvanceProxyPoolGeneration added in v8.0.50

func AdvanceProxyPoolGeneration(publisher uint64, cfg *config.Config)

AdvanceProxyPoolGeneration publishes cfg as the current snapshot of publisher and retires only that publisher's older snapshots. cfg must be a private snapshot not yet visible to request handlers. Pass 0 when the caller owns no publisher (direct SDK embedding, helper probes, tests); those snapshots fence each other.

func AllocateClaudeMCPToolAlias

func AllocateClaudeMCPToolAlias(secret, original string, reserved map[string]bool) (string, bool)

AllocateClaudeMCPToolAlias picks an alias that is not already reserved. Attempts are capped at the wordlist size so names that sanitize to the same suffix cannot spin forever. ok is false only when every one-word tool ID for this semantic is already reserved.

func AppendAPIResponseChunk

func AppendAPIResponseChunk(ctx context.Context, cfg *config.Config, chunk []byte)

AppendAPIResponseChunk appends an upstream response chunk to Gin context for request logging.

func AppendAPIWebsocketResponse

func AppendAPIWebsocketResponse(ctx context.Context, cfg *config.Config, payload []byte)

AppendAPIWebsocketResponse stores an upstream websocket response frame in Gin context.

func AppendCodexAPIWebsocketResponse

func AppendCodexAPIWebsocketResponse(ctx context.Context, cfg *config.Config, payload []byte)

AppendCodexAPIWebsocketResponse stores a codex upstream websocket response frame and merges any quota event headers carried by the frame into the request log.

func ApplyClaudeCredentialMetadata

func ApplyClaudeCredentialMetadata(payload []byte, auth *cliproxyauth.Auth, sessionID string) ([]byte, string, error)

ApplyClaudeCredentialMetadata rewrites the identity exception shared by native and cloaked OAuth requests.

func ApplyClaudeCredentialMetadataDoc added in v8.0.52

func ApplyClaudeCredentialMetadataDoc(doc *ojson.Doc, auth *cliproxyauth.Auth, sessionID string) (string, error)

ApplyClaudeCredentialMetadataDoc rewrites the identity exception in place on doc.

func ApplyClaudeDefaultDeviceProfileHeaders

func ApplyClaudeDefaultDeviceProfileHeaders(r *http.Request, cfg *config.Config)

func ApplyClaudeDeviceProfileHeaders

func ApplyClaudeDeviceProfileHeaders(r *http.Request, profile ClaudeDeviceProfile)

func ApplyClaudeLegacyDeviceHeaders

func ApplyClaudeLegacyDeviceHeaders(r *http.Request, ginHeaders http.Header, cfg *config.Config, confirmedClaudeCode bool, keepNewerVersions ...bool)

func ApplyGeminiInteractionsRequestHeaders added in v8.0.21

func ApplyGeminiInteractionsRequestHeaders(req *http.Request, headers http.Header)

ApplyGeminiInteractionsRequestHeaders forwards any incoming Api-Revision header to upstream requests.

func ApplyGeminiInteractionsRevisionHeader added in v8.0.21

func ApplyGeminiInteractionsRevisionHeader(req *http.Request)

ApplyGeminiInteractionsRevisionHeader sets the default Api-Revision header on outgoing HTTP requests.

func ApplyGeminiInteractionsThinking added in v8.0.21

func ApplyGeminiInteractionsThinking(body []byte, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) ([]byte, error)

ApplyGeminiInteractionsThinking applies thinking/reasoning configuration to the interactions payload.

func ApplyGeminiInteractionsThinkingDoc added in v8.0.52

func ApplyGeminiInteractionsThinkingDoc(doc *ojson.Doc, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) error

ApplyGeminiInteractionsThinkingDoc applies thinking/reasoning configuration to doc in place.

func ApplyGeminiRequestImageConfig added in v8.0.48

func ApplyGeminiRequestImageConfig(detail *usage.Detail, requestPayload []byte)

ApplyGeminiRequestImageConfig populates detail.RequestedImageDetails from a Gemini or Vertex request payload without overwriting upstream-returned detail.ImageDetails.

func ApplyMediaPayloadConfig added in v8.0.14

func ApplyMediaPayloadConfig(cfg *config.Config, executor, model, protocol string, body []byte, contentType string, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) ([]byte, string, error)

ApplyMediaPayloadConfig exposes multipart fields as JSON business values. File parts use {filename, content_type, data} with base64 data; framing stays opaque.

func ApplyPatchOriginalRequest added in v8.0.9

func ApplyPatchOriginalRequest(req cliproxyexecutor.Request, opts cliproxyexecutor.Options) []byte

ApplyPatchOriginalRequest retains source declarations when SDK callers omit OriginalRequest.

func ApplyPatchRequested added in v8.0.9

func ApplyPatchRequested(original []byte) bool

ApplyPatchRequested resolves only original winning custom declarations.

func ApplyPatchTranslationError added in v8.0.9

func ApplyPatchTranslationError(param any) error

ApplyPatchTranslationError consumes only the optional translator error state.

func ApplyPayloadConfigWithRequest

func ApplyPayloadConfigWithRequest(cfg *config.Config, model, protocol, fromProtocol, root string, payload, original []byte, requestedModel string, requestPath string, headers http.Header) []byte

ApplyPayloadConfigWithRequest applies payload config using source protocol and request header gates.

func ApplyPayloadConfigWithRequestDoc added in v8.0.52

func ApplyPayloadConfigWithRequestDoc(cfg *config.Config, model, protocol, fromProtocol, root string, doc *ojson.Doc, original []byte, requestedModel string, requestPath string, headers http.Header)

ApplyPayloadConfigWithRequestDoc applies payload config in place on doc using source protocol and request header gates.

func ApplyPayloadConfigWithRequestForExecutor added in v8.0.6

func ApplyPayloadConfigWithRequestForExecutor(cfg *config.Config, targetExecutor, model, protocol, fromProtocol, root string, payload, original []byte, requestedModel string, requestPath string, headers http.Header) []byte

ApplyPayloadConfigWithRequestForExecutor applies payload config with explicit target executor context.

func ApplyPayloadConfigWithRequestForExecutorDoc added in v8.0.52

func ApplyPayloadConfigWithRequestForExecutorDoc(cfg *config.Config, targetExecutor, model, protocol, fromProtocol, root string, doc *ojson.Doc, original []byte, requestedModel string, requestPath string, headers http.Header)

ApplyPayloadConfigWithRequestForExecutorDoc applies payload config in place on doc with explicit target executor context.

func ApplyPayloadConfigWithRequestTracked

func ApplyPayloadConfigWithRequestTracked(cfg *config.Config, model, protocol, fromProtocol, root string, payload, original []byte, requestedModel string, requestPath string, headers http.Header, trackedPath string) ([]byte, bool)

ApplyPayloadConfigWithRequestTracked applies payload config and reports whether an applied rule targeted trackedPath or one of its descendants.

func ApplyPayloadConfigWithRequestTrackedDoc added in v8.0.52

func ApplyPayloadConfigWithRequestTrackedDoc(cfg *config.Config, model, protocol, fromProtocol, root string, doc *ojson.Doc, original []byte, requestedModel string, requestPath string, headers http.Header, trackedPath string) bool

ApplyPayloadConfigWithRequestTrackedDoc applies payload config in place on doc and reports whether an applied rule targeted trackedPath or one of its descendants.

func ApplyPayloadConfigWithRoot

func ApplyPayloadConfigWithRoot(cfg *config.Config, model, protocol, root string, payload, original []byte, requestedModel string, requestPath string) []byte

ApplyPayloadConfigWithRoot behaves like applyPayloadConfig but treats all parameter paths as relative to the provided root path and restricts matches to the given protocol when supplied. Defaults are checked against the original payload when provided. requestedModel carries the client-visible model name before alias resolution so payload rules can target aliases precisely. requestPath is the inbound HTTP request path (when available) used for endpoint-scoped gates.

func ApplyPayloadConfigWithRootDoc added in v8.0.52

func ApplyPayloadConfigWithRootDoc(cfg *config.Config, model, protocol, root string, doc *ojson.Doc, original []byte, requestedModel string, requestPath string)

ApplyPayloadConfigWithRootDoc applies payload config in place on doc with parameter paths relative to root.

func ApplyPayloadConfigWithTrackedPaths

func ApplyPayloadConfigWithTrackedPaths(cfg *config.Config, model, protocol, fromProtocol, root string, payload, original []byte, requestedModel string, requestPath string, headers http.Header, trackedPaths ...string) ([]byte, map[string]bool)

ApplyPayloadConfigWithTrackedPaths applies payload config and reports which tracked paths (or their descendants) were targeted by an applied rule.

func ApplyPayloadConfigWithTrackedPathsDoc added in v8.0.52

func ApplyPayloadConfigWithTrackedPathsDoc(cfg *config.Config, model, protocol, fromProtocol, root string, doc *ojson.Doc, original []byte, requestedModel string, requestPath string, headers http.Header, trackedPaths ...string) map[string]bool

ApplyPayloadConfigWithTrackedPathsDoc applies payload config in place on doc and reports which tracked paths (or their descendants) were targeted by an applied rule.

func ApplyPayloadConfigWithTrackedPathsForExecutor added in v8.0.6

func ApplyPayloadConfigWithTrackedPathsForExecutor(cfg *config.Config, targetExecutor, model, protocol, fromProtocol, root string, payload, original []byte, requestedModel string, requestPath string, headers http.Header, trackedPaths ...string) ([]byte, map[string]bool)

ApplyPayloadConfigWithTrackedPathsForExecutor applies payload config with target executor awareness. When headers indicate a Codex client and the target executor is not Codex or Codex WebSocket, it normalizes tool parameter integer types to satisfy client-side integer deserialization (#6237). For Codex and Codex WebSocket targets, integer normalization is skipped (#6244).

func ApplyPayloadConfigWithTrackedPathsForExecutorDoc added in v8.0.52

func ApplyPayloadConfigWithTrackedPathsForExecutorDoc(cfg *config.Config, targetExecutor, model, protocol, fromProtocol, root string, doc *ojson.Doc, original []byte, requestedModel, requestPath string, headers http.Header, trackedPaths ...string) map[string]bool

ApplyPayloadConfigWithTrackedPathsForExecutorDoc applies payload config in place on doc with target executor awareness.

func ApplyRequestThinking

func ApplyRequestThinking(body []byte, req cliproxyexecutor.Request, opts cliproxyexecutor.Options, fromFormat, toFormat, provider string, normalizedUpdatesChanged ...bool) ([]byte, error)

ApplyRequestThinking preserves the registry lookup path unless the auth manager bound authoritative model capabilities to this execution attempt.

func ApplyRequestThinkingDoc added in v8.0.52

func ApplyRequestThinkingDoc(doc *ojson.Doc, req cliproxyexecutor.Request, opts cliproxyexecutor.Options, fromFormat, toFormat, providerKey string, PreserveConfigurationUpdates ...bool) error

ApplyRequestThinkingDoc applies request thinking onto doc in place, preserving the registry lookup path unless the auth manager bound authoritative model capabilities to this execution attempt.

func ApplyRequestThinkingWithModelInfo added in v8.0.42

func ApplyRequestThinkingWithModelInfo(body []byte, req cliproxyexecutor.Request, opts cliproxyexecutor.Options, fromFormat, toFormat, provider string, fallbackModelInfo *registry.ModelInfo, normalizedUpdatesChanged ...bool) ([]byte, error)

ApplyRequestThinkingWithModelInfo applies request thinking and falls back to fallbackModelInfo when neither request metadata nor the global registry resolves a model definition.

func ApplyRequestThinkingWithModelInfoDoc added in v8.0.52

func ApplyRequestThinkingWithModelInfoDoc(doc *ojson.Doc, req cliproxyexecutor.Request, opts cliproxyexecutor.Options, fromFormat, toFormat, provider string, fallbackModelInfo *registry.ModelInfo, normalizedUpdatesChanged ...bool) error

ApplyRequestThinkingWithModelInfoDoc applies request thinking onto doc in place and falls back to fallbackModelInfo when neither request metadata nor the global registry resolves a model definition.

func ApplyThinkingWithSourcePayload

func ApplyThinkingWithSourcePayload(body, currentSourcePayload, originalSourcePayload []byte, model, fromFormat, toFormat, providerKey string) ([]byte, error)

ApplyThinkingWithSourcePayload preserves summary visibility from the original client payload while applying thinking configuration to its translated target payload. currentSourcePayload is the payload that was translated, while originalSourcePayload retains intent removed by an earlier interceptor.

func ApplyThinkingWithSourcePayloadDoc added in v8.0.52

func ApplyThinkingWithSourcePayloadDoc(doc *ojson.Doc, currentSourcePayload, originalSourcePayload []byte, model, fromFormat, toFormat, providerKey string) error

ApplyThinkingWithSourcePayloadDoc preserves summary visibility from the original client payload while applying thinking configuration onto doc in place.

func AugmentClaudeBuiltinToolRegistry

func AugmentClaudeBuiltinToolRegistry(body []byte, registry map[string]bool) map[string]bool

func BeginClaudeContinuity

func BeginClaudeContinuity(credentialIdentity, sessionID string, isNewPromptTurn bool, explicitPromptID string) (key string, sequence uint64, previousMessageID, previousRequestID, promptID string)

BeginClaudeContinuity starts one request generation for a stable credential identity and Claude conversation. It tracks the previous upstream message ID, previous upstream request ID (cc_prev_req), and active prompt ID (cc_prompt_id). If explicitPromptID is provided and valid, it is adopted. Otherwise if isNewPromptTurn is true (or if no prompt ID exists), a fresh UUIDv4 is generated.

func BeginClaudeDiagnostics

func BeginClaudeDiagnostics(credentialIdentity, sessionID string) (key string, sequence uint64, previousMessageID string)

BeginClaudeDiagnostics starts one request generation for a stable credential identity and Claude conversation. It returns the last successfully completed upstream message ID, if any. Only a SHA-256 digest of the credential identity and session is retained as the cache key, so access-token rotation does not interrupt continuity.

func BuildAntigravityCompactionResponse

func BuildAntigravityCompactionResponse(modelName, capsule string, inputTokens, outputTokens, totalTokens int) []byte

BuildAntigravityCompactionResponse creates JSON response for non-stream response.compaction.

func BuildAntigravityCompactionStreamChunks

func BuildAntigravityCompactionStreamChunks(modelName, capsule string, inputTokens, outputTokens, totalTokens int) [][]byte

BuildAntigravityCompactionStreamChunks creates SSE frames for compaction stream response.

func BuildDevinClientMetadataBytes

func BuildDevinClientMetadataBytes(sessionToken, deviceSeed, osName string) []byte

BuildDevinClientMetadataBytes constructs the serialized bytes for Field 1 (ClientMetadata).

func BuildDevinGetChatMessageRequest

func BuildDevinGetChatMessageRequest(
	sessionToken string,
	deviceSeed string,
	chatModelUID string,
	systemPrompt string,
	prompts []DevinPrompt,
	tools []DevinTool,
	temperature *float64,
	maxTokens int,
	sessionID string,
	cascadeID string,
	matcher *SensitiveWordMatcher,
) []byte

BuildDevinGetChatMessageRequest encodes an entire GetChatMessageRequest protobuf payload.

func BuildDevinUpstreamLogBody

func BuildDevinUpstreamLogBody(
	interactionsPayload []byte,
	isInteractionsSource bool,
	chatModelUID string,
	systemPrompt string,
	prompts []DevinPrompt,
	tools []DevinTool,
	temp *float64,
	maxTokens int,
	sessionID string,
	cascadeID string,
) []byte

BuildDevinUpstreamLogBody formats the intermediate interactions and the decoded Devin request into a clear, aligned log body without synthetic wrapping.

func BuildDevinUpstreamResponseLogBody

func BuildDevinUpstreamResponseLogBody(respLog *DevinUpstreamResponseLog, interactionsJSON []byte) []byte

BuildDevinUpstreamResponseLogBody formats the decoded Devin response and the intermediate interactions into a clear, aligned log body.

func BuildOpenAIUsageJSON

func BuildOpenAIUsageJSON(count int64) []byte

BuildOpenAIUsageJSON returns a minimal usage structure understood by downstream translators.

func BuildResponsesCompactionResponseFromSummary added in v8.0.35

func BuildResponsesCompactionResponseFromSummary(modelName string, summaryPayload []byte) ([]byte, error)

BuildResponsesCompactionResponseFromSummary seals summary text from an OpenAI Responses payload into a response.compaction payload.

func BuildResponsesCompactionStreamChunksFromResponse added in v8.0.35

func BuildResponsesCompactionStreamChunksFromResponse(modelName string, compactionPayload []byte) ([][]byte, error)

BuildResponsesCompactionStreamChunksFromResponse converts a sealed response.compaction payload into SSE stream chunks.

func CachedSessionID

func CachedSessionID(apiKey string) string

CachedSessionID returns a stable session UUID per apiKey, refreshing the TTL on each access.

func CachedSessionIDRequired

func CachedSessionIDRequired(ctx context.Context, apiKey string) (string, error)

CachedSessionIDRequired returns a stable session UUID per apiKey for request-time paths.

func CachedUserID

func CachedUserID(apiKey string) string

func CachedUserIDRequired

func CachedUserIDRequired(ctx context.Context, apiKey string) (string, error)

CachedUserIDRequired returns a stable fake user ID per apiKey for request-time paths.

func CanBeDevinBareApplyPatchPrefix added in v8.0.45

func CanBeDevinBareApplyPatchPrefix(raw string) bool

CanBeDevinBareApplyPatchPrefix reports whether raw can still become a valid bare apply_patch payload as further streaming chunks arrive.

func CanFinalizeResponseStream added in v8.0.16

func CanFinalizeResponseStream(param any) bool

CanFinalizeResponseStream reports whether EOF can safely synthesize the source terminator.

func CaptureTranslatedClaudeStreamError added in v8.0.29

func CaptureTranslatedClaudeStreamError(responseFormat sdktranslator.Format, lines [][]byte, retainedErr *error, buildErr func(string) error)

CaptureTranslatedClaudeStreamError retains the first SSE error event payload so failure usage and terminal StreamChunk.Err can be emitted after any trailing usage chunk.

func ClaudeAgentSessionUUID

func ClaudeAgentSessionUUID(headers http.Header, originalPayload, translatedPayload []byte, metadataSets ...map[string]any) string

ClaudeAgentSessionUUID maps the downstream agent conversation to one stable UUID, preserving native Claude Code session signals.

func ClaudeAgentSessionUUIDForRequest

func ClaudeAgentSessionUUIDForRequest(headers http.Header, originalPayload, translatedPayload []byte, confirmedClaudeCode bool, metadataSets ...map[string]any) string

ClaudeAgentSessionUUIDForRequest preserves Claude-specific session signals only for a confirmed native caller. Other callers use protocol session fields, execution metadata, or the stable derived conversation root.

func ClaudeCLIAuthIdentitySeed

func ClaudeCLIAuthIdentitySeed(auth *cliproxyauth.Auth) string

ClaudeCLIAuthIdentitySeed returns a stable credential identity that does not rotate with delegated-provider access tokens.

func ClaudeCodeExecutionScope

func ClaudeCodeExecutionScope(ctx context.Context, payload []byte, headers http.Header) (string, bool)

ClaudeCodeExecutionScope returns the stable root-session and agent identity used by Codex execution state.

func ClaudeCredentialAccountUUID

func ClaudeCredentialAccountUUID(auth *cliproxyauth.Auth) string

ClaudeCredentialAccountUUID returns the selected upstream credential's account UUID.

func ClaudeDeterministicPromptID

func ClaudeDeterministicPromptID(seed string) string

ClaudeDeterministicPromptID generates a deterministic RFC 4122 UUIDv4 from a seed string.

func ClaudeDeviceProfileStabilizationEnabled

func ClaudeDeviceProfileStabilizationEnabled(cfg *config.Config) bool

func ClaudeExecutionMetadataFromContext

func ClaudeExecutionMetadataFromContext(ctx context.Context) bool

ClaudeExecutionMetadataFromContext retrieves whether execution metadata was present from ctx.

func ClaudeHeadersIndicateUnifiedRateLimitRejection

func ClaudeHeadersIndicateUnifiedRateLimitRejection(headers http.Header) bool

ClaudeHeadersIndicateUnifiedRateLimitRejection reports whether response headers explicitly declare an Anthropic shared 5h or 7d rate-limit rejection. An overage-only or Fable-only rejection remains model-scoped when shared subscription windows are not rejected (status "allowed", "allowed_warning", or unexhausted when omitted).

func ClaudeMCPAliasWordCount

func ClaudeMCPAliasWordCount() int

ClaudeMCPAliasWordCount is the BIP-39 English dictionary size used for the virtual server pair and the one-word tool ID.

func ClaudeMCPToolAlias

func ClaudeMCPToolAlias(secret, original string, attempt uint32) string

ClaudeMCPToolAlias derives a Claude Code-style MCP tool name. Aliases from one caller share a virtual server component. The tool component combines a stable keyed ID with a truncated semantic suffix so the model can distinguish tools by name while the request-local symbol table restores the exact original. A higher attempt linearly probes the next word when a collision must be avoided. Server and tool IDs use BIP-39 English words so weak models are less likely to drift high-entropy Base32 fragments.

func ClaudeModelLacksToolSearch added in v8.0.24

func ClaudeModelLacksToolSearch(model string) bool

ClaudeModelLacksToolSearch reports Anthropic models that predate tool search. Unknown names, including third-party models, are treated as supported.

func ClaudePayloadHas1hTTL

func ClaudePayloadHas1hTTL(payload []byte) bool

ClaudePayloadHas1hTTL reports whether the request payload contains any cache_control block with ttl set to "1h".

func ClaudeRequestHasExecutionMetadata

func ClaudeRequestHasExecutionMetadata(metadataSets ...map[string]any) bool

ClaudeRequestHasExecutionMetadata reports whether the request explicitly carried an internal execution session metadata key.

func ClaudeSessionDatePinExpiryForTest added in v8.0.32

func ClaudeSessionDatePinExpiryForTest(sessionID string) time.Time

ClaudeSessionDatePinExpiryForTest returns the expiry of the retained date pin for a session, or the zero time when no pin exists.

func ClaudeSessionDatePinKey added in v8.0.32

func ClaudeSessionDatePinKey(sessionID string) string

ClaudeSessionDatePinKey returns the credential-independent store key for a session date pin, or "" for an empty session ID.

func ClaudeSessionIDFromContext

func ClaudeSessionIDFromContext(ctx context.Context) string

ClaudeSessionIDFromContext retrieves the Claude session ID from ctx, if present.

func ClaudeSubagentRequests1h

func ClaudeSubagentRequests1h(headers http.Header, body []byte) bool

ClaudeSubagentRequests1h reports whether a subagent request explicitly requests 1h cache TTL either via a cache_control block with ttl="1h" in the payload or via extended-cache-ttl-2025-04-11 in incoming Anthropic-Beta headers.

func CodexPromptCacheKey

func CodexPromptCacheKey(modelName string, userScope string) string

CodexPromptCacheKey builds the Home KV key for a model/user prompt cache.

func CommitClaudeContinuity

func CommitClaudeContinuity(key string, sequence uint64, messageID, requestID string, promptIDs ...string)

CommitClaudeContinuity advances continuity only after a response completes. It commits the upstream message ID (msg_01...), request ID (req_01...), and prompt ID (cc_prompt_id). A non-empty messageID is required so incomplete/truncated streams do not advance sequence.

func CommitClaudeDiagnostics

func CommitClaudeDiagnostics(key string, sequence uint64, messageID string)

CommitClaudeDiagnostics advances continuity only after a response completes. A response from an older concurrently-started request cannot overwrite a newer committed generation, including after TTL expiry or capacity eviction.

func CountClaudeInputTokens

func CountClaudeInputTokens(payload []byte) (int64, error)

CountClaudeInputTokens estimates tokens for a Claude request with the O200kBase tokenizer.

func CountOpenAIChatTokens

func CountOpenAIChatTokens(enc tokenizer.Codec, payload []byte) (int64, error)

CountOpenAIChatTokens approximates prompt tokens for OpenAI chat completions payloads.

func CreditsUsed

func CreditsUsed(ctx context.Context) bool

CreditsUsed returns true if the request used AI credits.

func DecodeDevinToolCallID added in v8.0.23

func DecodeDevinToolCallID(id string) string

DecodeDevinToolCallID removes exactly one canonical encoding layer. Ordinary legal IDs, malformed encodings and arbitrary base64-looking IDs stay intact.

func DecodeResponseBody added in v8.0.44

func DecodeResponseBody(body io.ReadCloser, header http.Header) (io.ReadCloser, error)

DecodeResponseBody decodes declared encodings in reverse order, or sniffs gzip/zstd without a header. Closing the returned body also closes all decoders and the original body, including on setup errors.

func DefaultClaudeVersion

func DefaultClaudeVersion(cfg *config.Config) string

DefaultClaudeVersion returns the version string (e.g. "2.1.280") from the current baseline device profile. It extracts the version from the User-Agent.

func DeleteJSONField

func DeleteJSONField(body []byte, key string) []byte

DeleteJSONField removes a top-level or nested JSON field from a payload.

func DeleteJSONFieldDoc added in v8.0.52

func DeleteJSONFieldDoc(doc *ojson.Doc, key string) bool

DeleteJSONFieldDoc removes a top-level or nested JSON field from doc in place.

func DerivedAntigravitySessionID

func DerivedAntigravitySessionID(metadataSets ...map[string]any) string

DerivedAntigravitySessionID maps a derived session identity to Antigravity's negative decimal format.

func DerivedSessionID

func DerivedSessionID(metadataSets ...map[string]any) string

DerivedSessionID returns the first context-derived session identity in metadata order.

func DerivedSessionUUID

func DerivedSessionUUID(provider string, metadataSets ...map[string]any) string

DerivedSessionUUID maps a derived session identity to a provider-scoped stable UUID.

func DevinPayloadDefaultsSource added in v8.0.14

func DevinPayloadDefaultsSource(native, interactions []byte) []byte

DevinPayloadDefaultsSource maps caller field presence to the native business view so defaults can replace built-in values without replacing caller values.

func DevinQuotaRetryAfter added in v8.0.32

func DevinQuotaRetryAfter(resetAtSignal string, now time.Time) *time.Duration

DevinQuotaRetryAfter converts an observed quota reset timestamp (stored in auth Quota.Signals by the executor's Refresh path) into a retry delay. It returns nil unless the signal parses and is strictly in the future within a plausible daily/weekly horizon, in which case the caller should leave the cooldown duration to the conductor's credential backoff.

func DropForeignResponsesCompactionItems added in v8.0.35

func DropForeignResponsesCompactionItems(payload []byte, provider string) []byte

DropForeignResponsesCompactionItems removes compaction items whose encrypted_content does not use the CPA capsule prefix.

func DropForeignResponsesCompactionItemsDoc added in v8.0.52

func DropForeignResponsesCompactionItemsDoc(doc *ojson.Doc, provider string) bool

DropForeignResponsesCompactionItemsDoc removes non-CPA compaction items in place on doc.

func DropGeminiTrailingThoughtOnlyContent added in v8.0.24

func DropGeminiTrailingThoughtOnlyContent(payload []byte, path string) []byte

DropGeminiTrailingThoughtOnlyContent removes trailing model turns made only of thought parts, keeping at least one turn. Such a tail comes from a replayed trailing reasoning item; dropping it avoids relying only on the synthetic empty user turn, which did not prevent Antigravity (#6530) or Vertex (#6630) from rejecting the request as ending with a model turn.

func DropGeminiTrailingThoughtOnlyContentDoc added in v8.0.52

func DropGeminiTrailingThoughtOnlyContentDoc(doc *ojson.Doc, path string) bool

DropGeminiTrailingThoughtOnlyContentDoc removes trailing model turns made only of thought parts in place on doc, keeping at least one turn.

func EmitWebSocketResponseEvent

func EmitWebSocketResponseEvent(ctx context.Context, opts cliproxyexecutor.Options, auth *cliproxyauth.Auth, provider, model string, payload []byte)

EmitWebSocketResponseEvent delivers an upstream WebSocket response frame to any configured observer.

func EncodeDevinToolCallID added in v8.0.23

func EncodeDevinToolCallID(id string) string

EncodeDevinToolCallID reserves a namespace so native IDs cannot collide with encoded IDs. Empty deltas remain empty until upstream supplies the call ID.

func EndApplyPatchStream added in v8.0.9

func EndApplyPatchStream(ctx context.Context, param any, reporter *UsageReporter, out chan<- cliproxyexecutor.StreamChunk, gatewayErr error) bool

EndApplyPatchStream checks EOF before any synthetic success or usage publication.

func EnsureClaudeCLIFingerprintIdentity

func EnsureClaudeCLIFingerprintIdentity(auth *cliproxyauth.Auth, seed string, synthesizeMissing bool) error

EnsureClaudeCLIFingerprintIdentity prepares auth.Metadata so the shared ApplyClaudeCredentialMetadata path can run.

When synthesizeMissing is false (real OAuth), this is a no-op: missing account or device data must surface as credential errors. When synthesizeMissing is true (fingerprint-profile=claude-code-cli on API keys), missing account_uuid / device pool are filled with stable values derived from seed. Callers that hold a shared Auth must use PrepareClaudeCLIFingerprintAuth instead.

func EnsureClaudeCredentialDevicePoolRequired

func EnsureClaudeCredentialDevicePoolRequired(ctx context.Context, auth *cliproxyauth.Auth) ([]string, error)

EnsureClaudeCredentialDevicePoolRequired initializes a credential pool locally, or coordinates it through Home KV when the selected auth is a remote dispatch clone.

func EnsureGeminiBoundaryUserContent

func EnsureGeminiBoundaryUserContent(payload []byte, path string) []byte

EnsureGeminiBoundaryUserContent ensures that the contents array at the given path both starts and ends with a user turn when sending to Gemini/Antigravity upstreams.

func EnsureGeminiBoundaryUserContentDoc added in v8.0.52

func EnsureGeminiBoundaryUserContentDoc(doc *ojson.Doc, path string) bool

EnsureGeminiBoundaryUserContentDoc ensures that the contents array at the given path both starts and ends with a user turn in place on doc.

func EnsureGeminiLeadingUserContent

func EnsureGeminiLeadingUserContent(payload []byte, path string) []byte

EnsureGeminiLeadingUserContent ensures that the contents array at the given path starts with a user turn when sending to Gemini/Antigravity upstreams.

func EnsureGeminiLeadingUserContentDoc added in v8.0.52

func EnsureGeminiLeadingUserContentDoc(doc *ojson.Doc, path string) bool

EnsureGeminiLeadingUserContentDoc ensures that the contents array at the given path starts with a user turn in place on doc.

func EnsureGeminiTrailingUserContent

func EnsureGeminiTrailingUserContent(payload []byte, path string) []byte

EnsureGeminiTrailingUserContent ensures that the contents array at the given path ends with a user turn when sending to Gemini/Antigravity upstreams. If the final turn contains a functionResponse, it is preserved without appending an empty user turn because upstream expects the model to generate the response. A model-role functionResponse turn carrying nested media is the exception: Antigravity rejects it as the final turn with 400 INVALID_ARGUMENT (#6524).

func EnsureGeminiTrailingUserContentDoc added in v8.0.52

func EnsureGeminiTrailingUserContentDoc(doc *ojson.Doc, path string) bool

EnsureGeminiTrailingUserContentDoc ensures that the contents array at the given path ends with a user turn in place on doc.

func EnsureMetaMuseToolNamespace added in v8.0.50

func EnsureMetaMuseToolNamespace(body []byte) []byte

EnsureMetaMuseToolNamespace appends the canonical namespace to tool-bearing Responses requests, preserving caller tool indexes. It leaves tool-less requests and existing Muse namespaces untouched. Call before payload rules so user overrides and filters stay final.

func EnsureMetaMuseToolNamespaceDoc added in v8.0.52

func EnsureMetaMuseToolNamespaceDoc(doc *ojson.Doc) bool

EnsureMetaMuseToolNamespaceDoc appends the canonical namespace to tool-bearing Responses requests in place on doc.

func EnsureResponsesUsageDetails

func EnsureResponsesUsageDetails(payload []byte) []byte

EnsureResponsesUsageDetails ensures that Responses usage objects contain output_tokens_details (defaulting reasoning_tokens to 0) and input_tokens_details (defaulting cached_tokens to 0). It supports plain JSON payloads, single-line SSE data: lines, and multi-line SSE frames (e.g. event: ...\ndata: ...).

func EnsureResponsesUsageDetailsDoc added in v8.0.52

func EnsureResponsesUsageDetailsDoc(doc *ojson.Doc) bool

EnsureResponsesUsageDetailsDoc ensures that Responses usage objects in doc contain output_tokens_details and input_tokens_details in place.

func EnsureSessionContext

func EnsureSessionContext(ctx context.Context, opts cliproxyexecutor.Options, payload []byte) context.Context

EnsureSessionContext ensures that ctx carries the internal session identity for $CPA-SESSION-ID expansion in custom headers.

func ExecutorTypeName

func ExecutorTypeName(executor any) string

func ExpandAntigravityCompactionCapsules

func ExpandAntigravityCompactionCapsules(payload []byte) ([]byte, error)

ExpandAntigravityCompactionCapsules expands compaction items in input into developer context.

func ExpandAntigravityCompactionCapsulesDoc added in v8.0.52

func ExpandAntigravityCompactionCapsulesDoc(doc *ojson.Doc) (bool, error)

ExpandAntigravityCompactionCapsulesDoc expands compaction items in input into developer context in place on doc.

func ExpandRecognizedAntigravityCompactionCapsules added in v8.0.48

func ExpandRecognizedAntigravityCompactionCapsules(payload []byte) ([]byte, error)

ExpandRecognizedAntigravityCompactionCapsules expands recognized CPA compaction items in input into developer context while leaving non-CPA compaction items untouched.

func ExpandRecognizedAntigravityCompactionCapsulesDoc added in v8.0.52

func ExpandRecognizedAntigravityCompactionCapsulesDoc(doc *ojson.Doc) (bool, error)

ExpandRecognizedAntigravityCompactionCapsulesDoc expands recognized CPA compaction items in input in place on doc.

func ExpandRecognizedResponsesCompactionCapsules added in v8.0.48

func ExpandRecognizedResponsesCompactionCapsules(req *cliproxyexecutor.Request, opts *cliproxyexecutor.Options) error

ExpandRecognizedResponsesCompactionCapsules expands recognized CPA compaction capsules in req and opts while preserving foreign compaction items.

func ExpandResponsesCompactionCapsules added in v8.0.35

func ExpandResponsesCompactionCapsules(req *cliproxyexecutor.Request, opts *cliproxyexecutor.Options, provider string) error

ExpandResponsesCompactionCapsules drops foreign compaction items and expands recognized CPA compaction capsules in req and opts.

func ExpireClaudeDiagnosticsForTest added in v8.0.32

func ExpireClaudeDiagnosticsForTest(expireContinuity bool, pinExpiryOffset *time.Duration)

ExpireClaudeDiagnosticsForTest expires continuity entries and retained date pins without waiting for wall-clock TTLs. Pin expiry is relative to now.

func ExtractAntigravitySummaryText

func ExtractAntigravitySummaryText(respPayload []byte) (string, error)

ExtractAntigravitySummaryText extracts summary text from Antigravity or Responses response.

func ExtractClaudeBillingTags

func ExtractClaudeBillingTags(body []byte) (prevReq, promptID string)

ExtractClaudeBillingTags extracts existing cc_prev_req and cc_prompt_id values from a billing header in system text, if present.

func ExtractClaudeCodeAgentID

func ExtractClaudeCodeAgentID(ctx context.Context, headers http.Header) string

ExtractClaudeCodeAgentID resolves the Claude Code agent ID and uses a stable sentinel for the root agent.

func ExtractClaudeCodeSessionID

func ExtractClaudeCodeSessionID(ctx context.Context, payload []byte, headers http.Header) string

ExtractClaudeCodeSessionID resolves a Claude Code session ID, preferring X-Claude-Code-Session-Id over payload metadata.

func ExtractResponsesCompactionUsage added in v8.0.35

func ExtractResponsesCompactionUsage(payload []byte) (inputTokens, outputTokens, totalTokens, cachedTokens, reasoningTokens int)

ExtractResponsesCompactionUsage extracts usage counters from an OpenAI Responses summary payload.

func ExtractStreamJSONPayload

func ExtractStreamJSONPayload(line []byte) []byte

ExtractStreamJSONPayload extracts SSE data/json payload from a raw line.

func FetchXAINPMLatestVersion added in v8.0.20

func FetchXAINPMLatestVersion(ctx context.Context, client *http.Client) (string, error)

FetchXAINPMLatestVersion performs a single request to the npm registry to query the latest version of @xai-official/grok. The returned version is a strict numeric semver at or above XAIClientVersionServerFloor.

func FilterSSEUsageMetadata

func FilterSSEUsageMetadata(payload []byte) []byte

FilterSSEUsageMetadata removes usageMetadata from SSE events that are not terminal (finishReason != "stop"). Stop chunks are left untouched. This function is shared between aistudio and antigravity executors.

func FinalizeApplyPatchStream added in v8.0.9

func FinalizeApplyPatchStream(param any) [][]byte

FinalizeApplyPatchStream notifies request-local state of a transport ending. It must never manufacture a successful protocol terminator.

func FinalizeDevinPayload added in v8.0.14

func FinalizeDevinPayload(wire []byte, finalize PayloadFinalizer) ([]byte, []byte, error)

FinalizeDevinPayload projects the already-normalized protobuf business fields to JSON for user rules, then replaces only those fields in the wire message.

func FinalizePayload added in v8.0.14

func FinalizePayload(ctx context.Context, body []byte) []byte

FinalizePayload runs immediately before serialization or transport framing.

func FinalizePayloadDoc added in v8.0.52

func FinalizePayloadDoc(ctx context.Context, doc *ojson.Doc)

FinalizePayloadDoc runs immediately before serialization or transport framing, mutating doc in place.

func GeminiInteractionsSSEDone added in v8.0.21

func GeminiInteractionsSSEDone(frame []byte) bool

GeminiInteractionsSSEDone reports whether the SSE frame signals stream completion.

func GeminiInteractionsSSEPayload added in v8.0.21

func GeminiInteractionsSSEPayload(frame []byte) []byte

GeminiInteractionsSSEPayload extracts SSE data payload from raw SSE frames.

func GenerateDevinDeviceFingerprint

func GenerateDevinDeviceFingerprint(seed string) string

GenerateDevinDeviceFingerprint generates a 732-character hex device fingerprint. When seed is empty, it generates a cryptographically random 732-character hex string per request (matching native devin-cli). When seed is provided, it derives a deterministic 732-character hex fingerprint.

func GenerateDevinSentryTrace

func GenerateDevinSentryTrace() string

GenerateDevinSentryTrace generates a Sentry distributed tracing header in the format: "<32-hex-trace-id>-<16-hex-span-id>-1"

func GenerateFakeUserID

func GenerateFakeUserID() string

func GenerateFakeUserIDWithSessionID

func GenerateFakeUserIDWithSessionID(sessionID string) string

func GetXAIClientVersion added in v8.0.20

func GetXAIClientVersion() string

GetXAIClientVersion returns the current Grok CLI client version. If the background updater has fetched a newer acceptable version from npm, it returns that version; otherwise it returns DefaultXAIFallbackClientVersion.

func HasCodexMultiAgentV2NamespaceConflict

func HasCodexMultiAgentV2NamespaceConflict(payload []byte) bool

HasCodexMultiAgentV2NamespaceConflict reports whether the request defines the reserved optimized namespace, which must remain untouched.

func HasCodexMultiAgentV2NamespaceConflictDoc added in v8.0.52

func HasCodexMultiAgentV2NamespaceConflictDoc(doc *ojson.Doc) bool

HasCodexMultiAgentV2NamespaceConflictDoc reports whether doc defines the reserved optimized namespace, which must remain untouched.

func HasDevinEffortSuffix

func HasDevinEffortSuffix(model string) bool

HasDevinEffortSuffix reports whether the model name already ends with a known Devin effort suffix.

func HasMeaningfulCodexOutputDelta

func HasMeaningfulCodexOutputDelta(eventData []byte) bool

HasMeaningfulCodexOutputDelta reports whether an event carries non-empty generated content (such as non-empty text, reasoning, or function call arguments delta).

func HasRecognizedResponsesCompactionItem added in v8.0.48

func HasRecognizedResponsesCompactionItem(payload []byte) bool

HasRecognizedResponsesCompactionItem checks whether input contains a CPA-minted compaction item.

func HasResponsesCompactionItem

func HasResponsesCompactionItem(payload []byte) bool

HasResponsesCompactionItem checks whether input contains a compaction item.

func HasResponsesCompactionTrigger

func HasResponsesCompactionTrigger(payload []byte) bool

HasResponsesCompactionTrigger checks whether input contains a compaction_trigger item.

func HasResponsesCompactionTriggerRequest added in v8.0.35

func HasResponsesCompactionTriggerRequest(req cliproxyexecutor.Request, opts cliproxyexecutor.Options) bool

HasResponsesCompactionTriggerRequest reports whether req.Payload or opts.OriginalRequest contains a compaction_trigger item.

func HeaderValueCaseInsensitive

func HeaderValueCaseInsensitive(headers http.Header, name string) string

HeaderValueCaseInsensitive returns the first non-empty header value matching name case-insensitively.

func HeaderValuesCaseInsensitive

func HeaderValuesCaseInsensitive(headers http.Header, name string) []string

HeaderValuesCaseInsensitive returns all non-empty header values matching name case-insensitively.

func IncomingHeadersFromContext

func IncomingHeadersFromContext(ctx context.Context) http.Header

IncomingHeadersFromContext retrieves incoming HTTP headers from ctx, if present.

func InitializeApplyPatchStream added in v8.0.9

func InitializeApplyPatchStream(ctx context.Context, from, to sdktranslator.Format, model string, original, effective []byte, param *any)

InitializeApplyPatchStream prepares optional canonical state even for an empty EOF. An empty source has no progress events and does not opt native Codex into bridging.

func InjectClaudeBillingTags

func InjectClaudeBillingTags(body []byte, prevReq, promptID string) []byte

InjectClaudeBillingTags appends cc_prev_req and cc_prompt_id to the billing header in body if present.

func InjectClaudeBillingTagsDoc added in v8.0.52

func InjectClaudeBillingTagsDoc(doc *ojson.Doc, prevReq, promptID string) bool

InjectClaudeBillingTagsDoc appends cc_prev_req and cc_prompt_id to the billing header in doc in place.

func IsAnthropicUpstreamURL

func IsAnthropicUpstreamURL(u *url.URL) bool

IsAnthropicUpstreamURL reports whether a resolved request targets Anthropic's first-party API origin. Claude-specific body, header, HTTP, and TLS behavior must all use this gate so they cannot drift onto custom ports or userinfo URLs.

func IsApplyPatchUpstreamTool added in v8.0.9

func IsApplyPatchUpstreamTool(original []byte, name string) bool

IsApplyPatchUpstreamTool preserves the original declaration's provenance.

func IsChatTokenEvent

func IsChatTokenEvent(payload []byte) bool

IsChatTokenEvent reports whether the given OpenAI-compatible Chat Completions SSE or JSON chunk carries substantive output token content (such as text delta, reasoning content, or tool call arguments). It filters out container metadata, role announcements, and empty delta frames.

func IsClaudeCodeUserAgent added in v8.0.34

func IsClaudeCodeUserAgent(userAgent string) bool

IsClaudeCodeUserAgent reports whether userAgent identifies a Claude Code CLI client.

func IsClaudeMCPToolName

func IsClaudeMCPToolName(name string) bool

IsClaudeMCPToolName reports whether name follows Claude Code's MCP tool convention and contains only characters accepted by Anthropic tool names.

func IsClaudeNewPromptTurn

func IsClaudeNewPromptTurn(body []byte) bool

IsClaudeNewPromptTurn inspects the request messages to determine whether this request starts a new user prompt turn (requiring a new cc_prompt_id) versus a tool continuation step (which reuses the current cc_prompt_id).

func IsClaudeProbeOrHelperRequest

func IsClaudeProbeOrHelperRequest(body []byte) bool

IsClaudeProbeOrHelperRequest reports whether the request is a minimal probe/preflight (max_tokens: 1) or an automated title generation helper, which in native Claude Code do not emit cc_prompt_id or cc_prev_req.

func IsClaudeServerToolType

func IsClaudeServerToolType(toolType string) bool

IsClaudeServerToolType reports whether a typed declaration is a recognized Anthropic-operated tool. Client-defined type:"custom" declarations are not server tools and must remain eligible for MCP aliasing.

func IsClaudeSessionDatePinKVActive added in v8.0.55

func IsClaudeSessionDatePinKVActive() bool

IsClaudeSessionDatePinKVActive reports whether a persistent KV store (local bbolt or Home KV) is configured as the backing store for Claude session date pins.

func IsClaudeSubagentRequest

func IsClaudeSubagentRequest(headers http.Header, body []byte) bool

IsClaudeSubagentRequest reports whether the incoming request originates from or represents a Claude Code subagent.

func IsClaudeTokenEvent

func IsClaudeTokenEvent(payload []byte) bool

IsClaudeTokenEvent reports whether an Anthropic Messages SSE chunk carries substantive output token content (such as text delta, thinking delta, or tool input delta). It filters out message_start, ping, content_block_stop, and empty containers.

func IsCodexTerminalEmptyIncomplete

func IsCodexTerminalEmptyIncomplete(eventData []byte, outputItemsCount int, sawOutputDelta bool) bool

IsCodexTerminalEmptyIncomplete reports whether a response.incomplete event represents an upstream silent abort where explicitly zero output tokens were generated and zero output content (no output items and no non-empty output deltas) was produced.

func IsCodexUserAgent added in v8.0.5

func IsCodexUserAgent(headers http.Header) bool

IsCodexUserAgent reports whether headers contain a User-Agent indicating a Codex client.

func IsGeminiTerminalStreamChunk added in v8.0.37

func IsGeminiTerminalStreamChunk(chunk []byte) bool

IsGeminiTerminalStreamChunk recognizes terminal output from Gemini stream translators.

func IsGeminiTokenEvent

func IsGeminiTokenEvent(payload []byte) bool

IsGeminiTokenEvent reports whether a Google Gemini / Antigravity streaming chunk carries substantive output token content (such as text parts, thought traces, or function calls). It filters out metadata-only chunks (e.g. standalone usageMetadata) and empty parts.

func IsGoogleVertexHost added in v8.0.34

func IsGoogleVertexHost(baseURL string) bool

IsGoogleVertexHost reports whether baseURL is empty or points to Google's official aiplatform.googleapis.com or Regional Endpoint (REP) domain.

func IsModelSubstituted

func IsModelSubstituted(requested, served string) bool

IsModelSubstituted reports whether the upstream served a model other than the requested one for any provider; dated aliases and snapshot pins are accepted.

func IsNativeCodexRequest

func IsNativeCodexRequest(body []byte, opts cliproxyexecutor.Options) bool

IsNativeCodexRequest checks the client dialect for use inside Codex executors.

func IsResponsesTokenEvent

func IsResponsesTokenEvent(payload []byte) bool

IsResponsesTokenEvent reports whether the given OpenAI/xAI Responses API WebSocket or SSE event payload carries an actual output token, reasoning trace, tool call argument, or multimodal delta, according to the official Responses API streaming / websocket specification plus Codex-compatible private events.

func IsValidClaudePromptID

func IsValidClaudePromptID(id string) bool

IsValidClaudePromptID verifies whether an explicit prompt ID adheres to strict RFC 4122 UUIDv4 semantics.

func IsValidUserID

func IsValidUserID(userID string) bool

func IsVertexOpenAPIModel added in v8.0.34

func IsVertexOpenAPIModel(model string) bool

IsVertexOpenAPIModel reports whether the given model identifier targets a non-Google Vertex partner or open model (MaaS) served by endpoints/openapi.

func IterateStreamLines

func IterateStreamLines(payload []byte, fn func(line []byte))

IterateStreamLines splits payload by newline and invokes fn for non-empty lines.

func JSONPayload

func JSONPayload(line []byte) []byte

func JoinRawJSONArray

func JoinRawJSONArray(items [][]byte) []byte

JoinRawJSONArray joins validated raw JSON array items without re-encoding them.

func JoinRawJSONStrings

func JoinRawJSONStrings(items []string) []byte

JoinRawJSONStrings joins raw JSON array items held as strings.

func LogWithRequestID

func LogWithRequestID(ctx context.Context) *log.Entry

logWithRequestID returns a logrus Entry with request_id field populated from context. If no request ID is found in context, it returns the standard logger.

func LookupClaudeSessionDatePin added in v8.0.32

func LookupClaudeSessionDatePin(sessionID string, retention time.Duration) string

LookupClaudeSessionDatePin returns the retained pinned date for a session without creating or refreshing it. When a KV store is active, pins are stored permanently and retention is ignored; otherwise non-positive retention returns "".

func MapStainlessArch

func MapStainlessArch() string

func MapStainlessOS

func MapStainlessOS() string

func MarkCreditsUsed

func MarkCreditsUsed(ctx context.Context)

MarkCreditsUsed flags the request as having used AI credits for billing.

func MergeStreamUsageDetail

func MergeStreamUsageDetail(existing, update usage.Detail) usage.Detail

MergeStreamUsageDetail merges existing stream usage with a newer update.

func NewDevinHTTPClient

func NewDevinHTTPClient(ctx context.Context, cfg *config.Config, auth *cliproxyauth.Auth, timeout time.Duration) (clientOut *http.Client)

NewDevinHTTPClient creates an HTTP client customized for Devin Connect-RPC upstream. Suppresses automatic Accept-Encoding: gzip while preserving connection reuse across requests.

func NewProxyAwareHTTPClient

func NewProxyAwareHTTPClient(ctx context.Context, cfg *config.Config, auth *cliproxyauth.Auth, timeout time.Duration) (clientOut *http.Client)

NewProxyAwareHTTPClient creates an HTTP client with proper proxy configuration priority: 1. Use the execution-scoped request proxy if configured (highest priority) 2. Use auth.ProxyURL if configured 3. Use cfg.ProxyURL if auth proxy is not configured 4. Use RoundTripper from context if none are configured

Parameters:

  • ctx: The context containing optional RoundTripper
  • cfg: The application configuration
  • auth: The authentication information
  • timeout: The client timeout (0 means no timeout)

Returns:

  • *http.Client: An HTTP client with configured proxy or transport

func NewUtlsHTTPClient

func NewUtlsHTTPClient(ctx context.Context, cfg *config.Config, auth *cliproxyauth.Auth, timeout time.Duration) (clientOut *http.Client)

NewUtlsHTTPClient creates an HTTP client using provider-specific TLS fingerprints for protected hosts. It uses Claude Code's Node/OpenSSL profile for Anthropic and a Chrome profile for ChatGPT, with a standard-transport fallback for other hosts.

func NextDevinSessionTurnIndex

func NextDevinSessionTurnIndex(sessionID string) int

NextDevinSessionTurnIndex returns the next 0-based request ordinal for a session (Field 15.2). In native devin-cli, the counter is process-scoped per session: First request in a session returns 0 (which is omitted on the wire). Subsequent requests return 1, 2, 3... monotonically.

func NormalizeApplyPatchResponsesRequest added in v8.0.9

func NormalizeApplyPatchResponsesRequest(body []byte, original ...[]byte) ([]byte, error)

NormalizeApplyPatchResponsesRequest opts a non-Codex executor into the patch contract.

func NormalizeApplyPatchResponsesRequestDoc added in v8.0.52

func NormalizeApplyPatchResponsesRequestDoc(doc *ojson.Doc, original ...[]byte) error

NormalizeApplyPatchResponsesRequestDoc opts a non-Codex executor into the patch contract in place on doc.

func NormalizeClaudeToolCallIDs added in v8.0.23

func NormalizeClaudeToolCallIDs(body []byte) []byte

NormalizeClaudeToolCallIDs repairs only client tool ID fields. Existing legal IDs (including Devin encodings) are never re-encoded. Legacy collisions get a deterministic underscore-prefixed alias, without a process-wide ledger.

func NormalizeClaudeToolCallIDsDoc added in v8.0.52

func NormalizeClaudeToolCallIDsDoc(doc *ojson.Doc) bool

NormalizeClaudeToolCallIDsDoc repairs client tool ID fields in place on doc.

func NormalizeClaudeToolChoice added in v8.0.44

func NormalizeClaudeToolChoice(provider, model string, payload []byte) []byte

NormalizeClaudeToolChoice downgrades forced tool use on Claude 5.5 to auto. Use the routed upstream model, not the payload model, and run before thinking preparation so forced-mode cleanup cannot discard thinking or signed history. Delegated providers keep their own tool-choice contract. User payload rules must run afterwards and remain authoritative.

func NormalizeClaudeToolChoiceDoc added in v8.0.52

func NormalizeClaudeToolChoiceDoc(provider, model string, doc *ojson.Doc) bool

NormalizeClaudeToolChoiceDoc downgrades forced tool use on Claude 5.5 to auto in place on doc.

func NormalizeCodexToolIntegerTypes added in v8.0.5

func NormalizeCodexToolIntegerTypes(body []byte, headers http.Header) []byte

NormalizeCodexToolIntegerTypes normalizes specified tool parameter declarations from number to integer for Codex clients across supported tool formats.

func NormalizeCodexToolIntegerTypesDoc added in v8.0.52

func NormalizeCodexToolIntegerTypesDoc(doc *ojson.Doc, headers http.Header) bool

NormalizeCodexToolIntegerTypesDoc normalizes specified tool parameter declarations from number to integer for Codex clients directly on doc in place.

func NormalizeCodexToolSchemas

func NormalizeCodexToolSchemas(body []byte) []byte

NormalizeCodexToolSchemas inspects function tools in a Codex request payload and simplifies pure constant union combinations (e.g. large oneOf branch sets representing enums with descriptions, as emitted by MCP servers) into semantically equivalent enum lists, and sanitizes unsupported schema patterns. It does not normalize tool integer types because upstream OpenAI Codex models strictly validate reserved tool schemas (e.g. collaboration.wait_agent).

func NormalizeCodexToolSchemasDoc added in v8.0.52

func NormalizeCodexToolSchemasDoc(doc *ojson.Doc) bool

NormalizeCodexToolSchemasDoc inspects function tools in doc and simplifies pure constant union combinations and sanitizes unsupported schema patterns in place.

func NormalizeKimiResponsesInput added in v8.0.5

func NormalizeKimiResponsesInput(body []byte) ([]byte, error)

NormalizeKimiResponsesInput normalizes the input array for Kimi Responses API requests. When parallel function calls are emitted, Kimi requires all corresponding tool outputs to follow immediately without intervening non-tool messages (e.g. developer/user messages). This function defers intervening non-tool items until all outputs for the current batch of function calls have been emitted, keeping the outputs contiguous.

func NormalizeKimiResponsesInputDoc added in v8.0.52

func NormalizeKimiResponsesInputDoc(doc *ojson.Doc) (bool, error)

NormalizeKimiResponsesInputDoc normalizes the input array for Kimi Responses API requests in place on doc.

func NormalizeOpenAIMaxTokens

func NormalizeOpenAIMaxTokens(payload []byte, useMaxCompletionTokens bool) []byte

NormalizeOpenAIMaxTokens normalizes max_tokens and max_completion_tokens according to the model's preference. When useMaxCompletionTokens is true, it ensures max_completion_tokens is set and max_tokens is removed. When useMaxCompletionTokens is false, it ensures max_tokens is used and max_completion_tokens is removed.

func NormalizeOpenAIMaxTokensDoc added in v8.0.52

func NormalizeOpenAIMaxTokensDoc(doc *ojson.Doc, useMaxCompletionTokens bool) bool

NormalizeOpenAIMaxTokensDoc normalizes max_tokens and max_completion_tokens in place on doc.

func NormalizeOpenAIToolResultsTextOnly

func NormalizeOpenAIToolResultsTextOnly(payload []byte) []byte

NormalizeOpenAIToolResultsTextOnly converts tool message content to strings and strips relayed tool result images for text-only compatibility models. Text parts are preserved and image parts are replaced with a short marker.

func NormalizeOpenAIToolResultsTextOnlyDoc added in v8.0.52

func NormalizeOpenAIToolResultsTextOnlyDoc(doc *ojson.Doc) bool

NormalizeOpenAIToolResultsTextOnlyDoc converts tool message content to strings and strips relayed tool result images in place on doc for text-only compatibility models.

func NormalizeThinkingLevel

func NormalizeThinkingLevel(level string, budgetTokens int) string

NormalizeThinkingLevel converts numeric budgets or loose effort strings to canonical Devin efforts.

func NormalizeVertexOpenAPIModel added in v8.0.34

func NormalizeVertexOpenAPIModel(model string) string

NormalizeVertexOpenAPIModel normalizes a Vertex model identifier into the "<publisher>/<model>" form expected by Vertex's endpoints/openapi route while preserving any thinking suffix.

func ObfuscateSensitiveWords

func ObfuscateSensitiveWords(payload []byte, matcher *SensitiveWordMatcher) []byte

ObfuscateSensitiveWords processes the payload and obfuscates sensitive words in system blocks and message content.

func ObfuscateSensitiveWordsDoc added in v8.0.52

func ObfuscateSensitiveWordsDoc(doc *ojson.Doc, matcher *SensitiveWordMatcher) bool

ObfuscateSensitiveWordsDoc processes doc in place and obfuscates sensitive words in system blocks and message content.

func ObfuscateSensitiveWordsInSystemInstruction

func ObfuscateSensitiveWordsInSystemInstruction(payload []byte, matcher *SensitiveWordMatcher) []byte

ObfuscateSensitiveWordsInSystemInstruction obfuscates sensitive words in an Antigravity system instruction.

func ObfuscateSensitiveWordsInSystemInstructionDoc added in v8.0.52

func ObfuscateSensitiveWordsInSystemInstructionDoc(doc *ojson.Doc, matcher *SensitiveWordMatcher) bool

ObfuscateSensitiveWordsInSystemInstructionDoc obfuscates sensitive words in an Antigravity system instruction in place on doc.

func ObserveChatTokenEvent

func ObserveChatTokenEvent(reporter *UsageReporter, payload []byte)

ObserveChatTokenEvent inspects an OpenAI Chat Completions chunk and records TTFT if the frame represents the first meaningful token event. It records first-packet arrival time as fallback and returns immediately with zero allocations once effective token TTFT is set.

func ObserveClaudeTokenEvent

func ObserveClaudeTokenEvent(reporter *UsageReporter, payload []byte)

ObserveClaudeTokenEvent inspects an Anthropic Messages SSE chunk and records TTFT if the frame represents the first meaningful token event. It records first-packet arrival time as fallback and returns immediately with zero allocations once effective token TTFT is set.

func ObserveGeminiTokenEvent

func ObserveGeminiTokenEvent(reporter *UsageReporter, payload []byte)

ObserveGeminiTokenEvent inspects a Google Gemini / Antigravity streaming chunk and records TTFT if the frame represents the first meaningful token event. It records first-packet arrival time as fallback and returns immediately with zero allocations once effective token TTFT is set.

func ObserveMergedStreamUsage

func ObserveMergedStreamUsage(buffer *StreamUsageBuffer, update usage.Detail)

ObserveMergedStreamUsage updates buffer with merged usage details.

func ObservePluginExecutorStreamTTFT

func ObservePluginExecutorStreamTTFT(protocol string, reporter *UsageReporter, payload []byte)

ObservePluginExecutorStreamTTFT inspects a streaming payload and records TTFT on the reporter.

func ObservePluginExecutorStreamUsage

func ObservePluginExecutorStreamUsage(protocol string, payload []byte, buffer *StreamUsageBuffer)

ObservePluginExecutorStreamUsage parses streaming chunks and updates a stream usage buffer.

func ObserveResponsesTokenEvent

func ObserveResponsesTokenEvent(reporter *UsageReporter, payload []byte)

ObserveResponsesTokenEvent inspects a Responses API frame payload and records TTFT if the frame represents the first meaningful token event. It records the first packet arrival time as a fallback and exits immediately with zero allocations once effective token TTFT is set.

func OptimizeCodexMultiAgentV2Request

func OptimizeCodexMultiAgentV2Request(ctx context.Context, headers http.Header, payload []byte, cfg *config.Config) ([]byte, bool)

OptimizeCodexMultiAgentV2Request rewrites an eligible spawn_agent request and reports whether the collaboration namespace was renamed for upstream use.

func OptimizeCodexMultiAgentV2RequestDoc added in v8.0.52

func OptimizeCodexMultiAgentV2RequestDoc(doc *ojson.Doc, ctx context.Context, headers http.Header, cfg *config.Config) bool

OptimizeCodexMultiAgentV2RequestDoc rewrites an eligible spawn_agent request in place on doc and reports whether the collaboration namespace was renamed for upstream use.

func OptimizeCodexMultiAgentV2RequestForAuth

func OptimizeCodexMultiAgentV2RequestForAuth(ctx context.Context, headers http.Header, payload []byte, cfg *config.Config, auth *cliproxyauth.Auth, isCompat bool) ([]byte, bool)

OptimizeCodexMultiAgentV2RequestForAuth applies the standard Codex MultiAgentV2 request optimization and uses the execution attempt's resolved compatibility flag to convert agent_message items into portable message/user input, proactively stripping author, recipient, and internal passthrough metadata.

func OptimizeCodexMultiAgentV2RequestForAuthDoc added in v8.0.52

func OptimizeCodexMultiAgentV2RequestForAuthDoc(doc *ojson.Doc, ctx context.Context, headers http.Header, cfg *config.Config, auth *cliproxyauth.Auth, isCompat bool) bool

OptimizeCodexMultiAgentV2RequestForAuthDoc applies the standard Codex MultiAgentV2 request optimization in place on doc and reports whether the collaboration namespace was renamed for upstream use.

func OverrideXAINPMRegistryURLForTest added in v8.0.20

func OverrideXAINPMRegistryURLForTest(url string) func()

OverrideXAINPMRegistryURLForTest overrides the npm registry URL for testing purposes.

func ParseAntigravityStreamUsage

func ParseAntigravityStreamUsage(line []byte) (usage.Detail, bool)

func ParseAntigravityUsage

func ParseAntigravityUsage(data []byte) usage.Detail

func ParseClaudeRateLimitReset

func ParseClaudeRateLimitReset(headers http.Header, now time.Time) *time.Duration

ParseClaudeRateLimitReset inspects Anthropic response headers for shared and Fable-specific unified rate-limit and standard Retry-After reset information, returning the conservative cooldown duration including a bounded non-negative random grace period. If no valid future reset information is present, it returns nil.

func ParseClaudeRateLimitResetForError added in v8.0.33

func ParseClaudeRateLimitResetForError(headers http.Header, body []byte, now time.Time) *time.Duration

ParseClaudeRateLimitResetForError is ParseClaudeRateLimitReset with one narrow, body-aware exception. An explicit per-minute rate-limit error carries no quota window, so an unclassified unified reset must not stretch its short Retry-After into an hours-long cooldown. The exception applies only when the body is exactly one complete per-minute message, no status header declares a quota window, and Retry-After is a single unambiguous value usable by the downstream deadline filter. Every other input keeps the conservative header-only result.

func ParseClaudeStreamUsage

func ParseClaudeStreamUsage(line []byte) (usage.Detail, bool)

func ParseClaudeUsage

func ParseClaudeUsage(data []byte) usage.Detail

func ParseCodexImageToolUsage

func ParseCodexImageToolUsage(data []byte) (usage.Detail, bool)

func ParseCodexImageToolUsageWithOutputItems added in v8.0.48

func ParseCodexImageToolUsageWithOutputItems(requestBody, data []byte, itemsByIndex map[int64][]byte, fallback [][]byte) (usage.Detail, bool)

func ParseCodexQuotaEventHeaders

func ParseCodexQuotaEventHeaders(payload []byte) http.Header

ParseCodexQuotaEventHeaders converts one Codex websocket quota event into the same bounded header representation used by HTTP usage observations. Codex sends additional_rate_limits as an object on the websocket path, while the /wham/usage probe exposes the equivalent data as an array; both forms are normalized into namespaced X-Codex headers here.

The websocket payload identifies each additional limit only by limit name ("GPT-5.3-Codex-Spark"), while HTTP responses namespace the same limit by a short name (x-codex-bengalfox-*). The two forms therefore cannot produce identical header names; the X-Codex-Additional- prefix marks the websocket origin, and the snapshot-replacement semantics in QuotaState keep the two spellings from accumulating side by side.

func ParseCodexUsage

func ParseCodexUsage(data []byte) (usage.Detail, bool)

func ParseCodexUsageWithOutputItems added in v8.0.48

func ParseCodexUsageWithOutputItems(data []byte, itemsByIndex map[int64][]byte, fallback [][]byte) (usage.Detail, bool)

func ParseDevinResponseDimensionGroups

func ParseDevinResponseDimensionGroups(groups ...[]byte) (promptTokens, completionTokens, cachedTokens int64, found bool)

ParseDevinResponseDimensionGroups parses Field 28 (ResponseDimensionGroups) entries to extract Token Usage metrics: input_tokens, output_tokens, cached_input_tokens. Accepts one or more group payloads (each corresponding to a Field 28 value), or an outer envelope containing Tag 28.

func ParseDevinTrailerError

func ParseDevinTrailerError(payload []byte) (statusCode int, err error)

ParseDevinTrailerError inspects Connect-RPC EOS trailer frames and maps error status codes.

func ParseGeminiStreamUsage

func ParseGeminiStreamUsage(line []byte) (usage.Detail, bool)

func ParseGeminiUsage

func ParseGeminiUsage(data []byte) usage.Detail

func ParseInteractionsStreamUsage

func ParseInteractionsStreamUsage(line []byte) (usage.Detail, bool)

func ParseInteractionsUsage

func ParseInteractionsUsage(data []byte) usage.Detail

func ParseOpenAIImageUsage added in v8.0.48

func ParseOpenAIImageUsage(requestPayload, responsePayload []byte, contentType ...string) usage.Detail

ParseOpenAIImageUsage extracts token usage, generated image count, and separate request vs upstream response image parameters from an OpenAI or xAI image request/response.

func ParseOpenAIStreamUsage

func ParseOpenAIStreamUsage(line []byte) (usage.Detail, bool)

func ParseOpenAIUsage

func ParseOpenAIUsage(data []byte) usage.Detail

func ParsePluginExecutorResponseUsage

func ParsePluginExecutorResponseUsage(protocol string, payload []byte) usage.Detail

ParsePluginExecutorResponseUsage extracts token usage from a non-streaming plugin executor response.

func ParseRequestImageDetails added in v8.0.48

func ParseRequestImageDetails(requestPayload []byte, contentType ...string) *usage.ImageDetails

ParseRequestImageDetails extracts requested image/video parameters strictly from a request payload, preserving each upstream protocol's native field names.

func ParseRetryDelay

func ParseRetryDelay(errorBody []byte) (*time.Duration, error)

ParseRetryDelay extracts the retry delay from a Google API 429 error response.

func ParseXAIImageUsage added in v8.0.48

func ParseXAIImageUsage(requestPayload, responsePayload []byte) usage.Detail

ParseXAIImageUsage extracts token usage, generated image count, and image parameters from an xAI image generation/edit request and response.

func ParseXAIVideoUsage added in v8.0.48

func ParseXAIVideoUsage(requestPayload, responsePayload []byte) usage.Detail

ParseXAIVideoUsage extracts token usage and video parameters from an xAI video response.

func PatchResponsesCompactionStreamUsage added in v8.0.35

func PatchResponsesCompactionStreamUsage(chunk []byte, inputTokens, outputTokens, totalTokens, cachedTokens, reasoningTokens int) []byte

PatchResponsesCompactionStreamUsage updates usage counters on a compaction SSE chunk.

func PayloadRequestPath

func PayloadRequestPath(opts cliproxyexecutor.Options) string

func PayloadRequestedModel

func PayloadRequestedModel(opts cliproxyexecutor.Options, fallback string) string

func PinClaudeSessionDate added in v8.0.12

func PinClaudeSessionDate(key, sessionID, date string, retention time.Duration) string

PinClaudeSessionDate returns the calendar date pinned for a session, recording date on the first call and returning the pinned value on later calls. When a KV store is active, the pin is persisted permanently under a session-only KV key and retention is ignored. Otherwise, a non-positive retention keeps the legacy continuity-scoped pin (whose lifetime follows claudeDiagnosticsTTL) and a positive retention stores the pin in memory under a session-only key for retention. An unknown continuity key returns date unchanged, matching per-request dates.

func PrepareAntigravityClaudeStructuredOutput added in v8.0.44

func PrepareAntigravityClaudeStructuredOutput(model string, payload []byte) ([]byte, bool)

PrepareAntigravityClaudeStructuredOutput replaces native output schemas with best-effort prompt instructions for Claude 5.5, which rejects forced tool use. Call after sensitive-word obfuscation and before schema cleaning or payload rules. The boolean tells the request builder not to restore VALIDATED tool mode.

func PrepareAntigravityClaudeStructuredOutputDoc added in v8.0.52

func PrepareAntigravityClaudeStructuredOutputDoc(model string, doc *ojson.Doc) bool

PrepareAntigravityClaudeStructuredOutputDoc replaces native output schemas with best-effort prompt instructions in place on doc for Claude 5.5.

func PrepareAntigravityCompactionSummaryPayload

func PrepareAntigravityCompactionSummaryPayload(payload []byte, modelName string) []byte

PrepareAntigravityCompactionSummaryPayload prepares a payload for non-stream Antigravity summary generation.

func PrepareAntigravityCompactionSummaryPayloadDoc added in v8.0.52

func PrepareAntigravityCompactionSummaryPayloadDoc(doc *ojson.Doc, modelName string)

PrepareAntigravityCompactionSummaryPayloadDoc prepares doc in place for non-stream Antigravity summary generation.

func PrepareClaudeCLIFingerprintAuth

func PrepareClaudeCLIFingerprintAuth(auth *cliproxyauth.Auth, seed string, synthesizeMissing bool) (*cliproxyauth.Auth, error)

PrepareClaudeCLIFingerprintAuth returns the auth object that should receive ApplyClaudeCredentialMetadata. Synthesized API-key / delegated-provider identity is written to a clone so the shared credential metadata map is not mutated on the request path.

func ProviderSessionUUID

func ProviderSessionUUID(provider string, metadataSets ...map[string]any) string

ProviderSessionUUID prefers a long-lived execution session and falls back to the derived identity.

func PublishTranslatedClaudeStreamFailure added in v8.0.29

func PublishTranslatedClaudeStreamFailure(ctx context.Context, retainedErr error, reporter *UsageReporter, streamUsage *StreamUsageBuffer) bool

PublishTranslatedClaudeStreamFailure publishes failure usage with buffered stream usage when a translated Claude stream retained an SSE error event.

func ReadConnectFrame

func ReadConnectFrame(r io.Reader) (flag byte, payload []byte, err error)

ReadConnectFrame reads a single framed message from a Connect-proto stream.

func RecognizedAntigravityCompactionCapsule added in v8.0.22

func RecognizedAntigravityCompactionCapsule(encryptedContent string) bool

RecognizedAntigravityCompactionCapsule reports whether encrypted content uses the CPA capsule prefix.

func RecordAPIRequest

func RecordAPIRequest(ctx context.Context, cfg *config.Config, info UpstreamRequestLog)

RecordAPIRequest stores the upstream request metadata in Gin context for request logging.

func RecordAPIResponseError

func RecordAPIResponseError(ctx context.Context, cfg *config.Config, err error)

RecordAPIResponseError adds an error entry for the latest attempt when no HTTP response is available.

func RecordAPIResponseMetadata

func RecordAPIResponseMetadata(ctx context.Context, cfg *config.Config, status int, headers http.Header)

RecordAPIResponseMetadata captures upstream response status/header information for the latest attempt.

func RecordAPIWebsocketError

func RecordAPIWebsocketError(ctx context.Context, cfg *config.Config, stage string, err error)

RecordAPIWebsocketError stores an upstream websocket error event in Gin context.

func RecordAPIWebsocketHandshake

func RecordAPIWebsocketHandshake(ctx context.Context, cfg *config.Config, status int, headers http.Header)

RecordAPIWebsocketHandshake stores the upstream websocket handshake response metadata.

func RecordAPIWebsocketRequest

func RecordAPIWebsocketRequest(ctx context.Context, cfg *config.Config, info UpstreamRequestLog)

RecordAPIWebsocketRequest stores an upstream websocket request event in Gin context.

func RecordAPIWebsocketUpgradeRejection

func RecordAPIWebsocketUpgradeRejection(ctx context.Context, cfg *config.Config, info UpstreamRequestLog, status int, headers http.Header, body []byte)

RecordAPIWebsocketUpgradeRejection stores a rejected websocket upgrade as an HTTP attempt.

func RecordApplyPatchStreamFailure added in v8.0.9

func RecordApplyPatchStreamFailure(ctx context.Context, param any, reporter *UsageReporter, gatewayErr error) bool

RecordApplyPatchStreamFailure records validation before delivery can be canceled.

func RecordApplyPatchStreamFailureWithUsage added in v8.0.17

func RecordApplyPatchStreamFailureWithUsage(ctx context.Context, param any, reporter *UsageReporter, streamUsage *StreamUsageBuffer, gatewayErr error) bool

RecordApplyPatchStreamFailureWithUsage records validation with buffered upstream usage.

func RecordClaudeWebSearchResults added in v8.0.34

func RecordClaudeWebSearchResults(sessionKey string, data []byte, ttl time.Duration, limit int)

RecordClaudeWebSearchResults stores the web_search_tool_result blocks of a Claude response for sessionKey. data is either a complete Messages response or SSE text (one or more lines); the result block arrives whole in its content_block_start event.

func RecordClaudeWebSearchResultsContext added in v8.0.35

func RecordClaudeWebSearchResultsContext(ctx context.Context, sessionKey string, data []byte, ttl time.Duration, limit int)

RecordClaudeWebSearchResultsContext stores the web_search_tool_result blocks of a Claude response for sessionKey using ctx for Home KV operations.

func RecoverDevinBareApplyPatch added in v8.0.45

func RecoverDevinBareApplyPatch(raw string) (string, bool)

RecoverDevinBareApplyPatch validates that raw is a complete, well-framed bare Codex apply_patch payload and wraps it into {"input": "..."}.

func RefreshAuthViaHome

func RefreshAuthViaHome(ctx context.Context, cfg *config.Config, auth *cliproxyauth.Auth) (*cliproxyauth.Auth, bool, error)

RefreshAuthViaHome replaces local refresh logic when home control plane integration is enabled. It returns (updatedAuth, true, nil) when home refresh succeeds; (nil, true, err) when home is enabled but refresh fails; and (nil, false, nil) when home is disabled.

func RegisterProxyPoolPublisher added in v8.0.50

func RegisterProxyPoolPublisher() uint64

RegisterProxyPoolPublisher reserves one publisher identity per runtime. Pass the result to AdvanceProxyPoolGeneration on every publication.

func ResetClaudeDeviceProfileCache

func ResetClaudeDeviceProfileCache()

func ResetClaudeDiagnosticsForTest

func ResetClaudeDiagnosticsForTest()

ResetClaudeDiagnosticsForTest resets in-memory continuity state for test isolation.

func ResetDevinSessionTurnIndex

func ResetDevinSessionTurnIndex(sessionID string)

ResetDevinSessionTurnIndex clears the session counter (used for testing or explicit session reset).

func ResetProxyTransports added in v8.0.50

func ResetProxyTransports()

ResetProxyTransports closes idle connections in cached explicit proxy transports.

func ResolveAntigravityGroundingURLs

func ResolveAntigravityGroundingURLs(ctx context.Context, cfg *config.Config, auth *cliproxyauth.Auth, payload []byte) []byte

ResolveAntigravityGroundingURLs replaces Vertex Search redirect URLs in grounding chunks with their target URLs.

func ResolveAntigravityGroundingURLsDoc added in v8.0.52

func ResolveAntigravityGroundingURLsDoc(ctx context.Context, cfg *config.Config, auth *cliproxyauth.Auth, doc *ojson.Doc) bool

ResolveAntigravityGroundingURLsDoc replaces Vertex Search redirect URLs in grounding chunks in place on doc.

func ResolveDevinChatModelUID

func ResolveDevinChatModelUID(rawModel string, thinkingLevel string, budgetTokens int) string

ResolveDevinChatModelUID resolves a model identifier into a valid upstream Devin chat_model_uid. It prioritizes dynamic catalog metadata from devin_models.json, automatically clamps requested efforts to supported levels, applies sensible default efforts for thinking models, and ensures bare non-thinking models remain bare.

func ResolveKimiBaseURL

func ResolveKimiBaseURL(auth *cliproxyauth.Auth) string

ResolveKimiBaseURL resolves the upstream API base URL for Kimi API requests based on auth.

func ResolveKimiChatURL

func ResolveKimiChatURL(auth *cliproxyauth.Auth) string

ResolveKimiChatURL resolves the upstream URL for Kimi Chat Completions requests.

func ResolveKimiClaudeBaseURL

func ResolveKimiClaudeBaseURL(auth *cliproxyauth.Auth) string

ResolveKimiClaudeBaseURL resolves the base URL for Kimi Claude Messages delegation.

func ResolveKimiResponsesURL

func ResolveKimiResponsesURL(auth *cliproxyauth.Auth) string

ResolveKimiResponsesURL resolves the upstream URL for Kimi Responses API requests.

func ResolveVertexCompatConfig added in v8.0.37

func ResolveVertexCompatConfig(cfg *config.Config, auth *cliproxyauth.Auth) *config.VertexCompatKey

ResolveVertexCompatConfig finds the API-key configuration belonging to auth.

func ResponsesCompactionSourcePayload added in v8.0.35

func ResponsesCompactionSourcePayload(req cliproxyexecutor.Request, opts cliproxyexecutor.Options) []byte

ResponsesCompactionSourcePayload selects the source payload containing the compaction trigger or request history.

func ResponsesHasToolSearch added in v8.0.24

func ResponsesHasToolSearch(payload []byte) bool

ResponsesHasToolSearch reports whether a Responses payload carries a client tool_search declaration or tool_search history items.

func RestoreClaudeWebSearchResults added in v8.0.34

func RestoreClaudeWebSearchResults(body []byte, sessionKey string, ttl ...time.Duration) []byte

RestoreClaudeWebSearchResults fills assistant web_search_tool_result blocks whose content is missing or empty with the content stored for sessionKey. Blocks that already carry content, and blocks without a stored entry, are left unchanged.

func RestoreClaudeWebSearchResultsContext added in v8.0.35

func RestoreClaudeWebSearchResultsContext(ctx context.Context, body []byte, sessionKey string, ttl ...time.Duration) []byte

RestoreClaudeWebSearchResultsContext fills assistant web_search_tool_result blocks whose content is missing or empty with the content stored for sessionKey using ctx for Home KV operations.

func RestoreClaudeWebSearchResultsContextDoc added in v8.0.52

func RestoreClaudeWebSearchResultsContextDoc(ctx context.Context, doc *ojson.Doc, sessionKey string, ttl ...time.Duration) bool

RestoreClaudeWebSearchResultsContextDoc fills assistant web_search_tool_result blocks in doc whose content is missing or empty with the content stored for sessionKey using ctx for Home KV operations, and drops web_search_result_location citations from assistant turns whose web_search_tool_result remains empty.

func RestoreClaudeWebSearchResultsDoc added in v8.0.52

func RestoreClaudeWebSearchResultsDoc(doc *ojson.Doc, sessionKey string, ttl ...time.Duration) bool

RestoreClaudeWebSearchResultsDoc fills assistant web_search_tool_result blocks in doc whose content is missing or empty with the content stored for sessionKey.

func RestoreCodexMultiAgentV2Response

func RestoreCodexMultiAgentV2Response(payload []byte, optimized bool) []byte

RestoreCodexMultiAgentV2Response restores optimized collaboration namespace values before an upstream response is translated and returned to the client.

func RestoreDevinToolCallIDs added in v8.0.23

func RestoreDevinToolCallIDs(prompts []DevinPrompt)

RestoreDevinToolCallIDs runs before protobuf construction and user payload rules, keeping assistant calls and tool results in the upstream namespace.

func RetireProxyPoolPublisher added in v8.0.50

func RetireProxyPoolPublisher(publisher uint64)

RetireProxyPoolPublisher closes all pooled transports owned by publisher and removes its fence state when the owning service shuts down. Any snapshot carrying a non-zero generation for this publisher will see current[publisher]==0 and remain fenced out of the shared cache.

func RewriteCodexMultiAgentV2Input

func RewriteCodexMultiAgentV2Input(ctx context.Context, headers http.Header, payload []byte, cfg *config.Config) []byte

RewriteCodexMultiAgentV2Input converts official Codex multi-agent input into standard Responses API messages when multi-agent v2 optimization is enabled.

func RewriteCodexMultiAgentV2InputDoc added in v8.0.52

func RewriteCodexMultiAgentV2InputDoc(doc *ojson.Doc, ctx context.Context, headers http.Header, cfg *config.Config) bool

RewriteCodexMultiAgentV2InputDoc converts official Codex multi-agent input into standard Responses API messages in place on doc when multi-agent v2 optimization is enabled.

func RewriteCodexOrphanDelegationInput

func RewriteCodexOrphanDelegationInput(ctx context.Context, headers http.Header, payload []byte, cfg *config.Config) []byte

RewriteCodexOrphanDelegationInput converts orphan Codex delegation outputs into standard user messages when orphan delegation compatibility is enabled and the request carries the X-Openai-Subagent: collab_spawn header.

func RewriteCodexOrphanDelegationInputDoc added in v8.0.52

func RewriteCodexOrphanDelegationInputDoc(doc *ojson.Doc, ctx context.Context, headers http.Header, cfg *config.Config) bool

RewriteCodexOrphanDelegationInputDoc converts orphan Codex delegation outputs into standard user messages in place on doc when orphan delegation compatibility is enabled.

func RewriteCodexSpawnAgentDescription

func RewriteCodexSpawnAgentDescription(ctx context.Context, headers http.Header, payload []byte, cfg *config.Config) []byte

RewriteCodexSpawnAgentDescription optimizes spawn_agent definitions for official Codex clients when multi-agent v2 optimization is enabled.

func SanitizeCodexInputItemIDs

func SanitizeCodexInputItemIDs(body []byte) []byte

SanitizeCodexInputItemIDs normalizes supported input item IDs for Codex, removes encrypted reasoning items whose IDs exceed the Codex limit, and deterministically shortens other overlong input item IDs.

func SanitizeCodexInputItemIDsDoc added in v8.0.52

func SanitizeCodexInputItemIDsDoc(doc *ojson.Doc) bool

SanitizeCodexInputItemIDsDoc normalizes supported input item IDs for Codex in place on doc, removes encrypted reasoning items whose IDs exceed the Codex limit, and deterministically shortens other overlong input item IDs.

func SanitizeDevinSystemPrompt

func SanitizeDevinSystemPrompt(prompt string, matcher *SensitiveWordMatcher) string

SanitizeDevinSystemPrompt strips Claude Code attribution and CLI identity headers (referencing Antigravity conventions), and applies zero-width obfuscation for configured sensitive words.

func SanitizeGeminiInteractionsUnsupportedInputIDs added in v8.0.21

func SanitizeGeminiInteractionsUnsupportedInputIDs(body []byte, allowVertexBareTink bool) []byte

SanitizeGeminiInteractionsUnsupportedInputIDs aligns input step IDs with the official Gemini Interactions API schema: - `function_call` (FunctionCallStep) requires `id` and rejects `call_id` - `function_result` (FunctionResultStep) requires `call_id` and rejects `id` - other steps and content parts do not support `id`

allowVertexBareTink must be true only for the Vertex AI Interactions endpoint; other endpoints reject Vertex bare-Tink thought signatures, so those thought steps are dropped like any other incompatible signature.

func SanitizeGeminiInteractionsUnsupportedInputIDsDoc added in v8.0.52

func SanitizeGeminiInteractionsUnsupportedInputIDsDoc(doc *ojson.Doc, allowVertexBareTink bool) bool

SanitizeGeminiInteractionsUnsupportedInputIDsDoc aligns input step IDs with the official Gemini Interactions API schema in place on doc.

func SanitizeMetaWebSearchTools added in v8.0.3

func SanitizeMetaWebSearchTools(body []byte) []byte

SanitizeMetaWebSearchTools strips unsupported fields from web_search tool definitions for Meta Muse upstreams. Specifically, Meta rejects search_content_types when tool type is "web_search" (it is only accepted on "web_search_preview" tools).

func SanitizeMetaWebSearchToolsDoc added in v8.0.52

func SanitizeMetaWebSearchToolsDoc(doc *ojson.Doc) bool

SanitizeMetaWebSearchToolsDoc strips unsupported fields from web_search tool definitions in place on doc for Meta Muse upstreams.

func SealAntigravityCompaction

func SealAntigravityCompaction(summary, modelName string) (string, error)

SealAntigravityCompaction encrypts summary data into an opaque capsule using AES-GCM.

func SetBoolIfDifferent

func SetBoolIfDifferent(payload []byte, path string, value bool) []byte

SetBoolIfDifferent updates path only when its value is not already the canonical JSON boolean. Values with another JSON type are still normalized.

func SetBoolIfDifferentDoc added in v8.0.52

func SetBoolIfDifferentDoc(doc *ojson.Doc, path string, value bool) bool

SetBoolIfDifferentDoc updates path on doc in place only when its value is not already the canonical JSON boolean. Values with another JSON type are still normalized.

func SetCodexCache

func SetCodexCache(key string, cache CodexCache)

SetCodexCache stores a cache entry.

func SetCodexCacheBestEffort

func SetCodexCacheBestEffort(ctx context.Context, key string, cache CodexCache) bool

SetCodexCacheBestEffort stores a cache entry without failing completed responses.

func SetCodexCacheRequired

func SetCodexCacheRequired(ctx context.Context, key string, cache CodexCache) error

SetCodexCacheRequired stores a cache entry for request-time paths.

func SetRawIfDifferent

func SetRawIfDifferent(payload []byte, path string, value []byte) []byte

SetRawIfDifferent updates path only when the existing raw JSON is identical.

func SetRawIfDifferentDoc added in v8.0.52

func SetRawIfDifferentDoc(doc *ojson.Doc, path string, value []byte) bool

SetRawIfDifferentDoc updates path on doc in place only when the existing raw JSON differs.

func SetStringIfDifferent

func SetStringIfDifferent(payload []byte, path, value string) []byte

SetStringIfDifferent updates path only when its value is not already the canonical JSON string. Values with another JSON type are still normalized.

func SetStringIfDifferentDoc added in v8.0.52

func SetStringIfDifferentDoc(doc *ojson.Doc, path, value string) bool

SetStringIfDifferentDoc updates path on doc in place only when its value is not already the canonical JSON string. Values with another JSON type are still normalized.

func SetXAIClientVersionForTest added in v8.0.20

func SetXAIClientVersionForTest(version string) func()

SetXAIClientVersionForTest sets the cached Grok CLI version directly for testing purposes.

func SetXAIVersionRefreshedHookForTest added in v8.0.20

func SetXAIVersionRefreshedHookForTest(ch chan struct{}) func()

SetXAIVersionRefreshedHookForTest receives one signal after each refresh attempt.

func ShouldExecuteVertexOpenAPI added in v8.0.34

func ShouldExecuteVertexOpenAPI(auth *cliproxyauth.Auth, model string) bool

ShouldExecuteVertexOpenAPI reports whether the request should be routed through Vertex AI's OpenAI-compatible endpoints/openapi route.

func ShouldExecuteVertexOpenAPIForRequest added in v8.0.37

func ShouldExecuteVertexOpenAPIForRequest(cfg *config.Config, auth *cliproxyauth.Auth, model string, opts cliproxyexecutor.Options) bool

ShouldExecuteVertexOpenAPIForRequest resolves model, credential, and automatic endpoint selection in that order, preserving alias-specific overrides.

func ShouldNormalizeOpenAIToolResultsForModel

func ShouldNormalizeOpenAIToolResultsForModel(compat *config.OpenAICompatibility, upstreamModel, requestedModel string) bool

ShouldNormalizeOpenAIToolResultsForModel reports whether the selected model explicitly excludes image input through its input-modalities configuration.

func ShouldUseMaxCompletionTokensForModel

func ShouldUseMaxCompletionTokensForModel(compat *config.OpenAICompatibility, upstreamModel, requestedModel string) bool

ShouldUseMaxCompletionTokensForModel reports whether the resolved model under an OpenAI compatibility configuration has use-max-completion-tokens set to true.

func StableClaudeCLIAccountUUID

func StableClaudeCLIAccountUUID(seed string) string

StableClaudeCLIAccountUUID returns a deterministic UUIDv5 account ID derived from a seed.

func StableClaudeCLIDeviceID

func StableClaudeCLIDeviceID(seed string) string

StableClaudeCLIDeviceID returns a deterministic device ID derived from a seed.

func StartXAIVersionUpdater added in v8.0.20

func StartXAIVersionUpdater(ctx context.Context, proxyURL string)

StartXAIVersionUpdater starts a background goroutine that periodically refreshes the Grok CLI version from npm. A later call cancels the previous goroutine and binds the new service context and proxy URL. It executes a single fetch on startup and then polls every 3 hours without multiple retries on failure.

func StopApplyPatchStream added in v8.0.9

func StopApplyPatchStream(ctx context.Context, param any, reporter *UsageReporter, out chan<- cliproxyexecutor.StreamChunk, gatewayErr error) bool

StopApplyPatchStream propagates a retained failure after its one translated frame. The caller supplies its existing sanitized gateway status error.

func StopApplyPatchStreamWithUsage added in v8.0.17

func StopApplyPatchStreamWithUsage(ctx context.Context, param any, reporter *UsageReporter, streamUsage *StreamUsageBuffer, out chan<- cliproxyexecutor.StreamChunk, gatewayErr error) bool

StopApplyPatchStreamWithUsage propagates a retained failure with buffered upstream usage.

func StopTranslatedClaudeStream added in v8.0.29

func StopTranslatedClaudeStream(ctx context.Context, retainedErr *error, reporter *UsageReporter, streamUsage *StreamUsageBuffer, out chan<- cliproxyexecutor.StreamChunk) bool

StopTranslatedClaudeStream publishes failure usage and propagates a terminal StreamChunk.Err once after a translated Claude stream emitted an SSE error event.

func StripClaudeBillingTags

func StripClaudeBillingTags(body []byte) []byte

StripClaudeBillingTags removes cc_prev_req and cc_prompt_id from the billing header in body.

func StripClaudeBillingTagsDoc added in v8.0.52

func StripClaudeBillingTagsDoc(doc *ojson.Doc) bool

StripClaudeBillingTagsDoc removes cc_prev_req and cc_prompt_id from the billing header in doc in place.

func StripResponsesToolSearch added in v8.0.24

func StripResponsesToolSearch(payload []byte) []byte

StripResponsesToolSearch removes client tool_search declarations and every tool_search_call/tool_search_output input item, so the Claude translator produces the same output it did before tool_search support. Hosted declarations are left for the translator. An unchanged payload is returned as is.

func StripResponsesToolSearchDoc added in v8.0.52

func StripResponsesToolSearchDoc(doc *ojson.Doc) bool

StripResponsesToolSearchDoc removes client tool_search declarations and every tool_search_call/tool_search_output input item in place on doc.

func StripUsageMetadataFromJSON

func StripUsageMetadataFromJSON(rawJSON []byte) ([]byte, bool)

StripUsageMetadataFromJSON drops usageMetadata unless finishReason is present (terminal). It handles both formats: - Aistudio: candidates.0.finishReason - Antigravity: response.candidates.0.finishReason

func StripVertexOpenAIResponsesToolCallIDs

func StripVertexOpenAIResponsesToolCallIDs(payload []byte, sourceFormat string) []byte

StripVertexOpenAIResponsesToolCallIDs removes OpenAI Responses call IDs that Vertex rejects in Gemini functionCall/functionResponse payloads.

func StripVertexOpenAIResponsesToolCallIDsDoc added in v8.0.52

func StripVertexOpenAIResponsesToolCallIDsDoc(doc *ojson.Doc, sourceFormat string) bool

StripVertexOpenAIResponsesToolCallIDsDoc removes OpenAI Responses call IDs that Vertex rejects in Gemini functionCall/functionResponse payloads in place on doc.

func SummarizeErrorBody

func SummarizeErrorBody(contentType string, body []byte) string

func TokenizerForModel

func TokenizerForModel(model string) (tokenizer.Codec, error)

TokenizerForModel returns a tokenizer codec suitable for an OpenAI-style model id.

func TranslateGeminiInteractionsRequestBody added in v8.0.21

func TranslateGeminiInteractionsRequestBody(ctx context.Context, cfg *config.Config, model string, payload []byte, opts cliproxyexecutor.Options, stream, isCompat bool) ([]byte, error)

TranslateGeminiInteractionsRequestBody translates a request payload to FormatInteractions.

func TranslateGeminiInteractionsRequestPair added in v8.0.21

func TranslateGeminiInteractionsRequestPair(ctx context.Context, cfg *config.Config, model string, payload []byte, opts cliproxyexecutor.Options, stream, isCompat bool) (original, working []byte, err error)

TranslateGeminiInteractionsRequestPair translates the working payload and the payload-config baseline. Identical inputs are translated once, including plugin hooks. The baseline is captured before model and thinking mutations, and the working buffer is a separate copy so those mutations cannot change it.

func TranslateRequestEnvelopePairWithCodexMultiAgentV2

func TranslateRequestEnvelopePairWithCodexMultiAgentV2(ctx context.Context, headers http.Header, cfg *config.Config, from, to sdktranslator.Format, req sdktranslator.RequestEnvelope, originalPayload, requestPayload []byte) (original, working []byte, err error)

TranslateRequestEnvelopePairWithCodexMultiAgentV2 translates the baseline and working payload while preserving request-scoped metadata in req. The returned error is the one for the working payload, which is the payload sent upstream; a failure of the baseline alone never rejects a working payload that translated.

func TranslateRequestEnvelopeWithCodexMultiAgentV2

func TranslateRequestEnvelopeWithCodexMultiAgentV2(ctx context.Context, headers http.Header, cfg *config.Config, from, to sdktranslator.Format, req sdktranslator.RequestEnvelope) sdktranslator.RequestEnvelope

TranslateRequestEnvelopeWithCodexMultiAgentV2 normalizes official Codex multi-agent input while preserving the complete request envelope.

func TranslateRequestPairReturningError added in v8.0.20

func TranslateRequestPairReturningError(ctx context.Context, headers http.Header, cfg *config.Config, from, to sdktranslator.Format, model string, originalPayload, requestPayload []byte, stream, isCompat bool) ([]byte, []byte, error)

TranslateRequestPairReturningError reports a content part the target cannot represent.

func TranslateRequestPairWithAPIKeyModelCompatibility

func TranslateRequestPairWithAPIKeyModelCompatibility(ctx context.Context, headers http.Header, cfg *config.Config, from, to sdktranslator.Format, model string, originalPayload, requestPayload []byte, stream, isCompat bool) (original, working []byte)

TranslateRequestPairWithAPIKeyModelCompatibility avoids translating identical inputs twice while retaining separate buffers and invoking plugins once per input.

func TranslateRequestPairWithAPIKeyModelCompatibilityAndUpdateIntent

func TranslateRequestPairWithAPIKeyModelCompatibilityAndUpdateIntent(ctx context.Context, headers http.Header, cfg *config.Config, from, to sdktranslator.Format, model string, originalPayload, requestPayload []byte, stream, isCompat bool) (original, working []byte, updatesChanged bool, err error)

TranslateRequestPairWithAPIKeyModelCompatibilityAndUpdateIntent returns the normalizer decision for the working payload, not for the baseline payload, and the working payload's error for a content part the target cannot represent. A baseline failure alone is ignored.

func TranslateRequestPairWithCodexMultiAgentV2

func TranslateRequestPairWithCodexMultiAgentV2(ctx context.Context, headers http.Header, cfg *config.Config, from, to sdktranslator.Format, model string, originalPayload, requestPayload []byte, stream bool) (original, working []byte)

TranslateRequestPairWithCodexMultiAgentV2 translates the untouched baseline payload and the working payload that later stages mutate in place. Executors normally assign the original payload to the request before translating, so the same input is translated once, including plugin hooks, then copied into a separate working buffer. This avoids duplicate plugin side effects and a full extra pass over payloads that can reach tens of megabytes.

func TranslateRequestReturningError added in v8.0.20

func TranslateRequestReturningError(ctx context.Context, headers http.Header, cfg *config.Config, from, to sdktranslator.Format, model string, payload []byte, stream, isCompat bool) ([]byte, error)

TranslateRequestReturningError reports a content part the target cannot represent.

func TranslateRequestWithAPIKeyModelCompatibility

func TranslateRequestWithAPIKeyModelCompatibility(ctx context.Context, headers http.Header, cfg *config.Config, from, to sdktranslator.Format, model string, payload []byte, stream, isCompat bool) []byte

TranslateRequestWithAPIKeyModelCompatibility applies compatibility-aware request translators when a configured API-key model enables compatibility mode.

func TranslateRequestWithAPIKeyModelCompatibilityAndUpdateIntent

func TranslateRequestWithAPIKeyModelCompatibilityAndUpdateIntent(ctx context.Context, headers http.Header, cfg *config.Config, from, to sdktranslator.Format, model string, payload []byte, stream, isCompat bool) ([]byte, bool, error)

TranslateRequestWithAPIKeyModelCompatibilityAndUpdateIntent returns the plugin normalizer's request-scoped update decision for Responses targets.

func TranslateRequestWithAPIKeyModelCompatibilityAndUpdateIntentForExecutor added in v8.0.7

func TranslateRequestWithAPIKeyModelCompatibilityAndUpdateIntentForExecutor(ctx context.Context, headers http.Header, cfg *config.Config, targetExecutor string, from, to sdktranslator.Format, model string, payload []byte, stream, isCompat bool) ([]byte, bool, error)

TranslateRequestWithAPIKeyModelCompatibilityAndUpdateIntentForExecutor translates a request while applying client schema compatibility for the actual target executor.

func TranslateRequestWithAPIKeyModelCompatibilityForExecutor added in v8.0.7

func TranslateRequestWithAPIKeyModelCompatibilityForExecutor(ctx context.Context, headers http.Header, cfg *config.Config, targetExecutor string, from, to sdktranslator.Format, model string, payload []byte, stream, isCompat bool) []byte

TranslateRequestWithAPIKeyModelCompatibilityForExecutor applies compatibility-aware request translation while preserving the actual target executor's schema policy.

func TranslateRequestWithCodexMultiAgentV2

func TranslateRequestWithCodexMultiAgentV2(ctx context.Context, headers http.Header, cfg *config.Config, from, to sdktranslator.Format, model string, payload []byte, stream bool) []byte

TranslateRequestWithCodexMultiAgentV2 normalizes official Codex multi-agent input before translating it to a non-Codex target protocol.

func TranslateRequestWithCodexMultiAgentV2ForExecutor added in v8.0.7

func TranslateRequestWithCodexMultiAgentV2ForExecutor(ctx context.Context, headers http.Header, cfg *config.Config, targetExecutor string, from, to sdktranslator.Format, model string, payload []byte, stream bool) []byte

TranslateRequestWithCodexMultiAgentV2ForExecutor applies Codex client compatibility while respecting the actual target executor identity.

func TranslateStreamWithClaudeInputTokens

func TranslateStreamWithClaudeInputTokens(
	ctx context.Context,
	upstreamFormat, responseFormat sdktranslator.Format,
	model string,
	originalRequestRawJSON, requestRawJSON, rawJSON []byte,
	param *any,
	state *ClaudeInputTokenState,
) [][]byte

TranslateStreamWithClaudeInputTokens translates a stream chunk and estimates Claude message_start input usage once.

func TranslatedClaudeErrorPayload added in v8.0.29

func TranslatedClaudeErrorPayload(responseFormat sdktranslator.Format, out []byte) (string, bool)

TranslatedClaudeErrorPayload returns the raw JSON payload when a non-streaming translation to Claude format produced a terminal error object instead of a message.

func TranslatedClaudeStreamErrorPayload added in v8.0.29

func TranslatedClaudeStreamErrorPayload(responseFormat sdktranslator.Format, lines [][]byte) (string, bool)

TranslatedClaudeStreamErrorPayload returns the raw JSON data payload when a translated Claude SSE chunk contains an error event.

func UnsealAntigravityCompaction

func UnsealAntigravityCompaction(encryptedContent string) (string, error)

UnsealAntigravityCompaction decrypts and validates an opaque capsule.

func ValidateResponsesCompactionSummaryStatus added in v8.0.35

func ValidateResponsesCompactionSummaryStatus(summaryPayload []byte) error

ValidateResponsesCompactionSummaryStatus ensures the upstream summary response finished with status "completed".

func VertexAPIKeyOpenAPIBaseURL added in v8.0.34

func VertexAPIKeyOpenAPIBaseURL(baseURL, version, projectID, location string) string

VertexAPIKeyOpenAPIBaseURL builds the OpenAI-compatible base URL for a third-party Vertex-compatible API-key credential.

func VertexAuthBaseURL added in v8.0.34

func VertexAuthBaseURL(auth *cliproxyauth.Auth) string

VertexAuthBaseURL extracts an optional custom base_url from a Vertex credential.

func VertexAuthProjectID added in v8.0.34

func VertexAuthProjectID(auth *cliproxyauth.Auth) string

VertexAuthProjectID extracts an optional project_id from a Vertex credential.

func VertexExplicitOpenAPILocation added in v8.0.37

func VertexExplicitOpenAPILocation(cfg *config.Config, auth *cliproxyauth.Auth) string

VertexExplicitOpenAPILocation returns an explicitly configured OpenAPI location from auth attributes/metadata or the matching VertexCompatKey config, or "" if unset.

func VertexOpenAPIHostBaseURL added in v8.0.34

func VertexOpenAPIHostBaseURL(location string) string

VertexOpenAPIHostBaseURL returns the Vertex regional, multi-region REP, or global host URL.

func VertexOpenAPILocation added in v8.0.34

func VertexOpenAPILocation(auth *cliproxyauth.Auth, defaultLocation string) string

VertexOpenAPILocation returns the target Vertex location for OpenAPI requests, preferring an explicit openapi_location override on the credential.

func VertexServiceAccountOpenAPIBaseURL added in v8.0.34

func VertexServiceAccountOpenAPIBaseURL(baseURL, version, projectID, location string) string

VertexServiceAccountOpenAPIBaseURL builds the OpenAI-compatible base URL for a Vertex service-account credential (.../v1/projects/{project}/locations/{location}/endpoints/openapi).

func WebsocketUpgradeRequestURL

func WebsocketUpgradeRequestURL(rawURL string) string

WebsocketUpgradeRequestURL converts a websocket URL back to its HTTP handshake URL for logging.

func WithAntigravityConnectionTrace added in v8.0.11

func WithAntigravityConnectionTrace(req *http.Request, auth *cliproxyauth.Auth, operation string) *http.Request

WithAntigravityConnectionTrace reports upstream connection reuse only in debug mode. It preserves existing trace hooks and never logs URLs, headers, or credential material.

func WithAntigravityHTTPClientTrace added in v8.0.11

func WithAntigravityHTTPClientTrace(client *http.Client, auth *cliproxyauth.Auth, operation string) *http.Client

WithAntigravityHTTPClientTrace traces each actual request, including redirects. The client copy keeps the original pooled transport and client settings.

func WithClaudeContinuityContext

func WithClaudeContinuityContext(ctx context.Context, cc *ClaudeContinuityContext) context.Context

WithClaudeContinuityContext attaches a mutable ClaudeContinuityContext to ctx.

func WithClaudeExecutionMetadata

func WithClaudeExecutionMetadata(ctx context.Context, present bool) context.Context

WithClaudeExecutionMetadata attaches whether the request had an explicit execution session metadata to ctx.

func WithClaudeSessionID

func WithClaudeSessionID(ctx context.Context, sessionID string) context.Context

WithClaudeSessionID attaches a known Claude session ID to ctx.

func WithClaudeWebSearchResultRestore added in v8.0.53

func WithClaudeWebSearchResultRestore(ctx context.Context, sessionKey string, ttl ...time.Duration) context.Context

WithClaudeWebSearchResultRestore attaches a request-scoped cache lookup so Responses -> Claude request translation can restore web_search_tool_result blocks before deciding whether assistant citations remain backed by results.

func WithCodexCommentaryPolicy added in v8.0.49

func WithCodexCommentaryPolicy(ctx context.Context, cfg *config.Config) context.Context

WithCodexCommentaryPolicy keeps an in-flight stream's policy stable across config reloads. It changes protocol display/replay only, not the payload rules.

func WithIncomingHeaders

func WithIncomingHeaders(ctx context.Context, headers http.Header) context.Context

WithIncomingHeaders attaches incoming HTTP headers to ctx.

func WithPayloadFinalizer added in v8.0.14

func WithPayloadFinalizer(ctx context.Context, finalize PayloadFinalizer) context.Context

WithPayloadFinalizer carries the final barrier through shared request builders.

func WrapConnectEnvelope

func WrapConnectEnvelope(protoBytes []byte) []byte

WrapConnectEnvelope wraps raw payload bytes into a standard 5-byte Connect envelope: [1 byte flag: 0x00] + [4 byte big-endian length] + [payload].

func WrapConnectEnvelopeWithFlag

func WrapConnectEnvelopeWithFlag(flag byte, protoBytes []byte) []byte

WrapConnectEnvelopeWithFlag wraps payload bytes with an explicit Connect flag.

Types

type AntigravityInteractionsState added in v8.0.14

type AntigravityInteractionsState struct {
	// contains filtered or unexported fields
}

AntigravityInteractionsState tracks a single request, including streamed steps. The cache namespace is computed before continuation rewriting and payload rules.

func PrepareAntigravityInteractions added in v8.0.14

func PrepareAntigravityInteractions(auth *cliproxyauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options, model string, body []byte) (*AntigravityInteractionsState, []byte)

PrepareAntigravityInteractions restores only a matching tool-result suffix. Explicit continuation always wins, including an explicitly empty field.

func PrepareAntigravityInteractionsDoc added in v8.0.52

func PrepareAntigravityInteractionsDoc(auth *cliproxyauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options, model string, doc *ojson.Doc) *AntigravityInteractionsState

PrepareAntigravityInteractionsDoc restores only a matching tool-result suffix in place on doc.

func (*AntigravityInteractionsState) Observe added in v8.0.14

func (s *AntigravityInteractionsState) Observe(payload []byte)

Observe records only completed requires_action interactions, never failed or truncated streams. Stream events may carry steps outside the final snapshot.

type ApplyPatchResponsesState added in v8.0.9

type ApplyPatchResponsesState struct {
	Bridge *translatorcommon.ApplyPatchResponsesBridge
	// contains filtered or unexported fields
}

ApplyPatchResponsesState is owned explicitly by a non-native executor, never inferred from the wire format or a configured function schema. Supporting state stays request-local.

func NewApplyPatchResponsesState added in v8.0.9

func NewApplyPatchResponsesState(source sdktranslator.Format, original, declarations []byte) *ApplyPatchResponsesState

func (*ApplyPatchResponsesState) Active added in v8.0.9

func (s *ApplyPatchResponsesState) Active() bool

Active reports whether this explicitly non-native state owns a winning patch declaration.

func (*ApplyPatchResponsesState) AddDispatcher added in v8.0.9

func (s *ApplyPatchResponsesState) AddDispatcher(name, namespace string)

AddDispatcher marks only xAI folded namespaces that contain a winning custom patch.

func (*ApplyPatchResponsesState) Finish added in v8.0.9

func (s *ApplyPatchResponsesState) Finish() error

Finish validates source response closure independently of completed tool input. Common Finish remains argument-only because it also runs inside terminal conversion.

func (*ApplyPatchResponsesState) FinishStream added in v8.0.9

func (s *ApplyPatchResponsesState) FinishStream() ([][]byte, error)

FinishStream emits the local failure once on EOF without a validated completion.

func (*ApplyPatchResponsesState) RememberDispatcherArguments added in v8.0.9

func (s *ApplyPatchResponsesState) RememberDispatcherArguments(event []byte)

RememberDispatcherArguments keeps full snapshots on every matched candidate, including unnamed calls. The common bridge retains all identity/type contradictions until acquisition.

func (*ApplyPatchResponsesState) RememberDispatcherEvent added in v8.0.9

func (s *ApplyPatchResponsesState) RememberDispatcherEvent(event []byte)

RememberDispatcherEvent retains upstream evidence before namespace restoration. A wrapper alone never proves dispatcher provenance: only a declared dispatcher name does.

func (*ApplyPatchResponsesState) Stream added in v8.0.9

func (s *ApplyPatchResponsesState) Stream(line []byte) ([][]byte, error)

Stream preserves SSE framing and updates event-name lines to match converted JSON. A premature [DONE] is checked before publishing any success marker.

func (*ApplyPatchResponsesState) Transform added in v8.0.9

func (s *ApplyPatchResponsesState) Transform(event []byte) ([][]byte, error)

type ClaudeCodeRequestDetection

type ClaudeCodeRequestDetection struct {
	Confirmed       bool
	ConfirmedShape  bool
	StrongSignals   bool
	NativeClient    bool
	XAppCLI         bool
	UserAgent       bool
	BetasPresent    bool
	MetadataUserID  bool
	HelperProfile   bool
	Entrypoint      string
	Subclient       string
	AgentSDKVersion string
}

ClaudeCodeRequestDetection records the strong signals and first-party subclient identity used to distinguish an official Claude Code request from a client that only copied its User-Agent.

func DetectClaudeCodeRequest

func DetectClaudeCodeRequest(headers http.Header, payload []byte, countTokens bool, configs ...*config.Config) ClaudeCodeRequestDetection

DetectClaudeCodeRequest first mirrors CCH's strong-signal contract, then applies CPA's native-client policy. Standard Messages requests require all four strong signals; count_tokens omits metadata.user_id. A separate narrow profile recognizes measured native Haiku helper requests that intentionally omit claude-code-20250219. Unverified entrypoints (such as generic sdk-py) remain unconfirmed by default and receive CLI cloaking unless cloak_mode is never or the entrypoint is allowlisted in header-defaults.native-entrypoints.

type ClaudeContinuityContext

type ClaudeContinuityContext struct {
	Key               string
	Sequence          uint64
	PreviousMessageID string
	PreviousRequestID string
	PromptID          string
	// PinnedDate is the calendar date this session was first seen on. The
	// cloaked currentDate reminder reuses it so the reminder text stays
	// byte-stable within a session even when the local date flips mid-session.
	PinnedDate  string
	Initialized bool
}

ClaudeContinuityContext holds request-scoped continuity state across cloaking and execution.

func ClaudeContinuityContextFromContext

func ClaudeContinuityContextFromContext(ctx context.Context) *ClaudeContinuityContext

ClaudeContinuityContextFromContext retrieves the ClaudeContinuityContext from ctx, if present.

type ClaudeDeviceProfile

type ClaudeDeviceProfile struct {
	UserAgent      string
	PackageVersion string
	RuntimeVersion string
	OS             string
	Arch           string
	// contains filtered or unexported fields
}

func ResolveClaudeDeviceProfile

func ResolveClaudeDeviceProfile(auth *cliproxyauth.Auth, apiKey string, headers http.Header, cfg *config.Config) ClaudeDeviceProfile

func ResolveClaudeDeviceProfileRequired

func ResolveClaudeDeviceProfileRequired(ctx context.Context, auth *cliproxyauth.Auth, apiKey string, headers http.Header, cfg *config.Config) (ClaudeDeviceProfile, error)

ResolveClaudeDeviceProfileRequired resolves a stable Claude Code device profile for request-time paths.

type ClaudeInputTokenState

type ClaudeInputTokenState struct {
	// contains filtered or unexported fields
}

ClaudeInputTokenState tracks the one-time input token update for a translated Claude stream.

func NewClaudeInputTokenState

func NewClaudeInputTokenState(sourceFormat, upstreamFormat, responseFormat sdktranslator.Format, originalRequest []byte) *ClaudeInputTokenState

NewClaudeInputTokenState creates request-scoped state for translated Claude input token usage.

type CodexCache

type CodexCache struct {
	ID     string
	Expire time.Time
}

func ClaudeCodePromptCache

func ClaudeCodePromptCache(ctx context.Context, modelName string, payload []byte, headers http.Header) (CodexCache, bool, error)

ClaudeCodePromptCache derives a deterministic upstream prompt_cache_key for one Claude Code agent.

func GetCodexCache

func GetCodexCache(key string) (CodexCache, bool)

GetCodexCache retrieves a cached entry, returning ok=false if not found or expired.

func GetCodexCacheRequired

func GetCodexCacheRequired(ctx context.Context, key string) (CodexCache, bool, error)

GetCodexCacheRequired retrieves a cached entry for request-time paths.

type CodexSSEScanner added in v8.0.28

type CodexSSEScanner struct {
	// contains filtered or unexported fields
}

CodexSSEScanner reads an upstream Responses SSE stream line by line, as bufio.Scanner does, but delivers an event whose data the upstream split across several "data:" lines as one "data:" line. In SSE an event's data may span several data lines, joined with "\n". Taking each data line as a whole event instead cuts such an event: on a split response.completed the stream ends at the first fragment and the rest is never read.

Every event on this path is one JSON value. A data line that is complete JSON on its own, or cannot start JSON at all (an empty value, [DONE]), is delivered byte-for-byte as read with no lookahead, which keeps upstreams that omit the blank line between events working as before. A data line that opens a JSON object or array without completing it starts an event that collects the following data lines until one of these:

  • their joined value closes its outermost object or array;
  • a blank line ends the event;
  • the stream ends;
  • the collected bytes would exceed the line limit.

A completed event is delivered compacted onto one line. One that never becomes valid JSON is delivered as the same multi-line data field, so it still fails downstream. The first such event in a stream gets one warning that describes its framing without any of its bytes.

The scanner merges data lines and nothing else. It holds only the data lines of an event being collected and delivers every other line the moment it reads it, in the order read. A comment, id: or retry: line inside such an event therefore comes before the event's data instead of between its pieces, which SSE treats alike: an event's fields take effect when it is dispatched. The blank line or over-limit line that ends a collection follows the event's data.

SetAssembly(false) makes the scanner read exactly as bufio.Scanner does, for a caller that budgets every upstream line while it holds a stream.

func NewCodexSSEScanner added in v8.0.28

func NewCodexSSEScanner(ctx context.Context, r io.Reader, maxBytes int) *CodexSSEScanner

NewCodexSSEScanner returns a CodexSSEScanner over r, with assembly on. maxBytes bounds one line, as bufio.Scanner.Buffer does, and also the data lines collected for one event.

func (*CodexSSEScanner) Bytes added in v8.0.28

func (s *CodexSSEScanner) Bytes() []byte

Bytes returns the current line. It stays valid until the next call to Scan.

func (*CodexSSEScanner) Err added in v8.0.28

func (s *CodexSSEScanner) Err() error

Err returns the error that stopped the scan, as bufio.Scanner does.

func (*CodexSSEScanner) HeldBytes added in v8.0.28

func (s *CodexSSEScanner) HeldBytes() int

HeldBytes returns how many bytes are currently held in an incomplete event.

func (*CodexSSEScanner) HeldLines added in v8.0.28

func (s *CodexSSEScanner) HeldLines() int

HeldLines returns how many data lines are currently held in an incomplete event.

func (*CodexSSEScanner) RawBytes added in v8.0.28

func (s *CodexSSEScanner) RawBytes() int

RawBytes returns the total bytes of physical upstream lines consumed to produce the current event.

func (*CodexSSEScanner) RawLines added in v8.0.28

func (s *CodexSSEScanner) RawLines() int

RawLines returns the count of physical upstream lines consumed to produce the current event.

func (*CodexSSEScanner) Scan added in v8.0.28

func (s *CodexSSEScanner) Scan() bool

Scan advances to the next line, or to the next assembled event data line.

func (*CodexSSEScanner) SetAssembly added in v8.0.28

func (s *CodexSSEScanner) SetAssembly(on bool)

SetAssembly turns event assembly on or off. While it is off, no data line starts a collection and every line is delivered as read, exactly as bufio.Scanner delivers it. An event already being collected is still completed.

func (*CodexSSEScanner) SetBudgetChecker added in v8.0.28

func (s *CodexSSEScanner) SetBudgetChecker(fn func(line []byte, heldLines int, heldBytes int) bool)

SetBudgetChecker sets an optional callback invoked on each raw line read while an event is being held. If it returns true, the scanner yields from Scan() with an empty payload to signal that the caller should release buffering, while preserving the held assembly state for subsequent Scan() calls.

func (*CodexSSEScanner) SetOnLine added in v8.0.28

func (s *CodexSSEScanner) SetOnLine(fn func(line []byte))

SetOnLine sets a callback invoked for every physical line read from upstream.

type DevinApplyPatchDiagnostics added in v8.0.45

type DevinApplyPatchDiagnostics struct {
	// contains filtered or unexported fields
}

DevinApplyPatchDiagnostics records only argument lengths, a short prefix for shape classification, and fixed error codes. It never logs argument text. One instance belongs to one upstream response.

func NewDevinApplyPatchDiagnostics added in v8.0.45

func NewDevinApplyPatchDiagnostics(original []byte) *DevinApplyPatchDiagnostics

NewDevinApplyPatchDiagnostics resolves apply_patch calls against the original client request declarations.

func (*DevinApplyPatchDiagnostics) MarkRecovered added in v8.0.45

func (d *DevinApplyPatchDiagnostics) MarkRecovered(key int)

MarkRecovered marks the call identified by key as recovered from a complete bare patch in invalid_json_str so subsequent failures on later calls report the actually failing call.

func (*DevinApplyPatchDiagnostics) Observe added in v8.0.45

Observe accumulates one Devin tool-call delta for the call identified by key.

func (*DevinApplyPatchDiagnostics) ObserveDelta added in v8.0.45

ObserveDelta resolves the call key for a raw wire delta and records its shape.

func (*DevinApplyPatchDiagnostics) Render added in v8.0.45

func (d *DevinApplyPatchDiagnostics) Render(param any, reason string) string

Render returns a bounded log diagnostic using only fixed error codes.

type DevinFrameResult

type DevinFrameResult struct {
	OutputID                string
	Timestamp               uint64
	ContentText             string
	DeltaTokens             uint64
	StopReason              uint64 // 2/4=stop, 10=tool_calls
	ToolCallDeltas          []DevinToolCallDelta
	ThinkingText            string
	DeltaSignature          []byte
	DeltaSignatureType      string
	Latency                 float64
	MessageID               string
	Usage                   *DevinUsage
	ResponseDimensionGroups [][]byte
	UnknownFieldNumbers     []int
}

DevinFrameResult represents decoded content from a single Connect-proto frame.

func ParseDevinFrame

func ParseDevinFrame(payload []byte) (DevinFrameResult, error)

ParseDevinFrame extracts deltas, tool calls, thinking, signatures, and usage from a single response frame.

type DevinImage

type DevinImage struct {
	Base64Data string
	MimeType   string
}

DevinImage represents an image attachment in a DevinPrompt (Prompt #10).

type DevinImageLogItem

type DevinImageLogItem struct {
	MimeType string `json:"mime_type"`
	DataLen  int    `json:"data_len"`
}

DevinImageLogItem represents an image attached to a prompt in the request log.

type DevinPrompt

type DevinPrompt struct {
	MessageID          string
	Source             int // 1=user, 2=assistant, 4=tool
	Content            string
	Images             []DevinImage
	ToolCalls          []DevinToolCall
	ToolCallID         string // For source=4 (tool result)
	OriginalToolCallID string // Retained when downgraded from source=4 to source=1
	IsOrphanedTool     bool   // Explicit flag marking downgraded tool results
	DroppedPart        string // Type of a user media part Devin cannot send; never put on the wire
	Thinking           string
	Signature          []byte
	SignatureType      string
}

DevinPrompt represents a single turn in the request history (repeated Field 3).

func CheckDevinUserTurns added in v8.0.20

func CheckDevinUserTurns(prompts []DevinPrompt) ([]DevinPrompt, error)

CheckDevinUserTurns applies the shared user-turn policy to the parsed prompts. Devin can only receive inline images, so a user prompt that dropped media it cannot fetch or carry (a remote uri, audio, video, a document) is refused with an unsupported content part error when its whole user turn has nothing else to send. A tool result or real text in the same turn keeps the turn alive; the emptied prompt is then removed so that no empty user prompt, and no placeholder standing in for the media, reaches upstream.

type DevinPromptLogItem

type DevinPromptLogItem struct {
	ID            string              `json:"id,omitempty"`
	Source        int                 `json:"source"`
	Role          string              `json:"role,omitempty"`
	Content       string              `json:"content,omitempty"`
	Thinking      string              `json:"thinking,omitempty"`
	Signature     string              `json:"signature,omitempty"`
	SignatureType string              `json:"signature_type,omitempty"`
	ToolCalls     []DevinToolCall     `json:"tool_calls,omitempty"`
	ToolCallID    string              `json:"tool_call_id,omitempty"`
	Images        []DevinImageLogItem `json:"images,omitempty"`
}

DevinPromptLogItem represents a single prompt item in the request log.

type DevinQuotaPeriod added in v8.0.32

type DevinQuotaPeriod int

DevinQuotaPeriod identifies an account-wide Devin usage quota window.

const (
	// DevinQuotaPeriodNone means the error is not an account-wide
	// daily/weekly usage quota exhaustion.
	DevinQuotaPeriodNone DevinQuotaPeriod = iota
	// DevinQuotaPeriodDaily marks daily usage quota exhaustion.
	DevinQuotaPeriodDaily
	// DevinQuotaPeriodWeekly marks weekly usage quota exhaustion.
	DevinQuotaPeriodWeekly
)

func DevinUsageQuotaExhaustionPeriod added in v8.0.32

func DevinUsageQuotaExhaustionPeriod(message string) DevinQuotaPeriod

DevinUsageQuotaExhaustionPeriod reports whether the upstream error message explicitly says the account-wide daily or weekly usage quota has been exhausted (issue #6690). Matching is deliberately narrow: the period word, "quota", and "exhausted" must all be present, so monthly ACU messages, generic rate limits, and transient capacity errors never match and stay on the short per-model backoff path. If the message somehow names both periods, weekly wins because it is the longer window.

type DevinTool

type DevinTool struct {
	Name        string
	Description string
	Parameters  []byte
}

DevinTool represents a tool definition in GetChatMessageRequest.

type DevinToolCall

type DevinToolCall struct {
	ID        string
	Name      string
	Arguments string
}

DevinToolCall represents a tool call in a ChatMessagePrompt.

type DevinToolCallDelta

type DevinToolCallDelta struct {
	ID               string
	Name             string
	Arguments        string
	InvalidJSONStr   string
	InvalidJSONErr   string
	IsCustomToolCall bool
}

DevinToolCallDelta represents a streaming tool call chunk from response Field 6.

type DevinToolLogItem

type DevinToolLogItem struct {
	Name        string          `json:"name"`
	Description string          `json:"description,omitempty"`
	Parameters  json.RawMessage `json:"parameters,omitempty"`
}

DevinToolLogItem represents a tool declared in the request log.

type DevinUpstreamRequestLog

type DevinUpstreamRequestLog struct {
	Model        string               `json:"model"`
	SessionID    string               `json:"session_id,omitempty"`
	CascadeID    string               `json:"cascade_id,omitempty"`
	SystemPrompt string               `json:"system_prompt,omitempty"`
	Temperature  *float64             `json:"temperature,omitempty"`
	MaxTokens    int                  `json:"max_tokens,omitempty"`
	Prompts      []DevinPromptLogItem `json:"prompts,omitempty"`
	Tools        []DevinToolLogItem   `json:"tools,omitempty"`
}

DevinUpstreamRequestLog represents the human-readable JSON representation of GetChatMessageRequest for request logs.

type DevinUpstreamResponseLog

type DevinUpstreamResponseLog struct {
	Status                string                      `json:"status,omitempty"`
	FramesCount           int                         `json:"frames_count"`
	Content               string                      `json:"content,omitempty"`
	Thinking              string                      `json:"thinking,omitempty"`
	Signature             string                      `json:"signature,omitempty"`
	SignatureType         string                      `json:"signature_type,omitempty"`
	ToolCalls             []DevinToolCall             `json:"tool_calls,omitempty"`
	Usage                 *DevinUsage                 `json:"usage,omitempty"`
	UnknownFields         []int                       `json:"unknown_fields,omitempty"`
	ApplyPatchDiagnostics *DevinApplyPatchDiagnostics `json:"-"`
}

DevinUpstreamResponseLog represents the decoded response frames from Devin Connect-RPC.

type DevinUsage

type DevinUsage struct {
	PromptTokens     int64             `json:"prompt_tokens"`
	CompletionTokens int64             `json:"completion_tokens"`
	CachedTokens     int64             `json:"cached_tokens"`
	CacheWriteTokens int64             `json:"cache_write_tokens,omitempty"`
	StatusCode       uint64            `json:"status_code,omitempty"`
	RequestID        string            `json:"request_id,omitempty"`
	ModelName        string            `json:"model_name,omitempty"`
	Headers          map[string]string `json:"headers,omitempty"`
}

DevinUsage captures token accounting from response Field 7.

type OpenAIStreamUsageObserver added in v8.0.48

type OpenAIStreamUsageObserver struct {
	// contains filtered or unexported fields
}

OpenAIStreamUsageObserver assembles SSE events for accounting while callers continue forwarding the original network chunks unchanged.

func NewOpenAIStreamUsageObserver added in v8.0.48

func NewOpenAIStreamUsageObserver(usage *StreamUsageBuffer, maxBytes int) *OpenAIStreamUsageObserver

NewOpenAIStreamUsageObserver bounds both incomplete lines and assembled events.

func (*OpenAIStreamUsageObserver) Feed added in v8.0.48

func (o *OpenAIStreamUsageObserver) Feed(chunk []byte)

Feed consumes arbitrary network chunks, retaining incomplete lines across reads.

func (*OpenAIStreamUsageObserver) Finish added in v8.0.48

func (o *OpenAIStreamUsageObserver) Finish()

Finish observes a trailing event even if the upstream omitted its final newline.

type PayloadFinalizer added in v8.0.14

type PayloadFinalizer func([]byte) []byte

PayloadFinalizer applies user rules to a fully prepared business payload. Each rebuilt attempt must start from the unconfigured body, not a previous result.

func NewPayloadFinalizer added in v8.0.14

func NewPayloadFinalizer(cfg *config.Config, executor, model, protocol, root string, original []byte, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) PayloadFinalizer

NewPayloadFinalizer snapshots matching context before built-in request mutations.

func (PayloadFinalizer) FinalizeDoc added in v8.0.52

func (f PayloadFinalizer) FinalizeDoc(doc *ojson.Doc)

FinalizeDoc applies the finalizer to doc in place without probing custom wrappers.

type RelaySSEBuffer added in v8.0.48

type RelaySSEBuffer struct {
	// contains filtered or unexported fields
}

RelaySSEBuffer reassembles SSE frames across arbitrary relay chunk boundaries. Bare JSON relay messages remain whole, including pretty-printed JSON.

func (*RelaySSEBuffer) Feed added in v8.0.48

func (b *RelaySSEBuffer) Feed(chunk []byte) [][]byte

Feed returns complete frames and retains an unfinished SSE frame for the next chunk.

func (*RelaySSEBuffer) Finish added in v8.0.48

func (b *RelaySSEBuffer) Finish() [][]byte

Finish flushes a final frame from relays that omit its trailing blank line.

type SensitiveWordMatcher

type SensitiveWordMatcher struct {
	// contains filtered or unexported fields
}

SensitiveWordMatcher holds the compiled regex for matching sensitive words.

func BuildSensitiveWordMatcher

func BuildSensitiveWordMatcher(words []string) *SensitiveWordMatcher

BuildSensitiveWordMatcher compiles a regex from the word list. Words are sorted by length (longest first) for proper matching.

func (*SensitiveWordMatcher) Matches

func (m *SensitiveWordMatcher) Matches(text string) bool

Matches reports whether the text contains any of the configured sensitive words.

func (*SensitiveWordMatcher) ObfuscateText

func (m *SensitiveWordMatcher) ObfuscateText(text string) string

ObfuscateText replaces all sensitive words in the text.

type StreamResponseModelObserver

type StreamResponseModelObserver struct {
	// contains filtered or unexported fields
}

StreamResponseModelObserver accumulates streaming chunks using a bounded buffer and extracts complete SSE lines or events to observe the response model, ensuring extraction does not depend on network chunk boundaries.

func NewStreamResponseModelObserver

func NewStreamResponseModelObserver(reporter *UsageReporter) *StreamResponseModelObserver

NewStreamResponseModelObserver creates an observer for tracking response models from arbitrary streaming network chunks.

func (*StreamResponseModelObserver) Feed

func (o *StreamResponseModelObserver) Feed(chunk []byte)

Feed ingests a raw chunk of bytes from a stream, splitting into lines and events.

func (*StreamResponseModelObserver) Finish

func (o *StreamResponseModelObserver) Finish()

Finish flushes any pending buffered line or event at the end of the stream.

type StreamUsageBuffer

type StreamUsageBuffer struct {
	// contains filtered or unexported fields
}

StreamUsageBuffer keeps the latest usage detail observed in a stream.

func (*StreamUsageBuffer) Detail

func (b *StreamUsageBuffer) Detail() (usage.Detail, bool)

Detail returns the latest observed usage detail.

func (*StreamUsageBuffer) Observe

func (b *StreamUsageBuffer) Observe(detail usage.Detail, ok bool)

Observe records detail when ok is true, allowing the final stream usage to win.

func (*StreamUsageBuffer) ObserveAntigravityStream added in v8.0.48

func (b *StreamUsageBuffer) ObserveAntigravityStream(line []byte)

ObserveAntigravityStream records and merges usage and candidate tool/media artifacts from an Antigravity SSE line.

func (*StreamUsageBuffer) ObserveClaudeSSEEvent added in v8.0.48

func (b *StreamUsageBuffer) ObserveClaudeSSEEvent(rawEvent []byte)

ObserveClaudeSSEEvent extracts joined multi-line SSE data payloads from rawEvent and records any model or usage details when a frame was split across lines.

func (*StreamUsageBuffer) ObserveClaudeStream

func (b *StreamUsageBuffer) ObserveClaudeStream(line []byte)

ObserveClaudeStream records and merges usage from a Claude SSE line.

func (*StreamUsageBuffer) ObserveGeminiStream added in v8.0.48

func (b *StreamUsageBuffer) ObserveGeminiStream(line []byte)

ObserveGeminiStream records and merges usage and candidate tool/media artifacts from a Gemini SSE line.

func (*StreamUsageBuffer) ObserveOpenAIStream

func (b *StreamUsageBuffer) ObserveOpenAIStream(line []byte)

ObserveOpenAIStream records response-tier state and the latest usage from an OpenAI-style stream while avoiding JSON parsing for irrelevant chunks.

func (*StreamUsageBuffer) ObserveResponsesStream added in v8.0.48

func (b *StreamUsageBuffer) ObserveResponsesStream(line []byte) bool

ObserveResponsesStream collects response.output_item.done items across a Responses SSE/WebSocket stream and parses terminal response.usage payloads with those collected items so tool usage is preserved when response.output is empty.

func (*StreamUsageBuffer) Publish

func (b *StreamUsageBuffer) Publish(ctx context.Context, reporter *UsageReporter) bool

Publish emits the latest observed usage detail, if any.

func (*StreamUsageBuffer) PublishFailure

func (b *StreamUsageBuffer) PublishFailure(ctx context.Context, reporter *UsageReporter, errs ...error) bool

PublishFailure emits the latest observed usage detail together with failure details.

func (*StreamUsageBuffer) ResponseModel

func (b *StreamUsageBuffer) ResponseModel() string

ResponseModel returns the latest model observed in the stream buffer.

func (*StreamUsageBuffer) SetRequestPayload added in v8.0.48

func (b *StreamUsageBuffer) SetRequestPayload(requestPayload []byte, contentType ...string)

SetRequestPayload records request-side image/media configuration on the stream buffer so it can be attached as RequestedImageDetails if the stream produces images.

type TransportCache

type TransportCache[K comparable] struct {
	// contains filtered or unexported fields
}

TransportCache memoizes HTTP transports under a comparable key using a bounded LRU. Evicting an entry closes its idle connections so neither the pool nor its background goroutines outlive the cache entry.

The key type is generic so callers can mix value identity (a normalized proxy URL) with pointer identity (a base transport supplied by the caller) without the cache retaining either beyond the LRU window.

func NewTransportCache

func NewTransportCache[K comparable](capacity int) *TransportCache[K]

NewTransportCache returns a cache holding at most capacity transports. A non-positive capacity falls back to DefaultTransportCacheCapacity.

func (*TransportCache[K]) CloseKey

func (c *TransportCache[K]) CloseKey(key K) bool

CloseKey removes the transport cached under key (if present) and closes its idle connections. Returns true if the entry was found and closed.

func (*TransportCache[K]) CloseMatching

func (c *TransportCache[K]) CloseMatching(predicate func(key K) bool) int

CloseMatching removes and closes idle connections for every cached transport whose key satisfies predicate. Returns the number of closed transports.

func (*TransportCache[K]) Contains

func (c *TransportCache[K]) Contains(key K) bool

Contains reports whether key exists in the cache.

func (*TransportCache[K]) Get

func (c *TransportCache[K]) Get(key K, build func() (*http.Transport, error)) (*http.Transport, error)

Get returns the transport cached under key, calling build on the first use of that key. Concurrent callers observe the same instance.

A build error is propagated without being cached, so a later call can retry and a failed lookup never occupies a cache slot. build must not call back into the same cache.

func (*TransportCache[K]) Len

func (c *TransportCache[K]) Len() int

Len reports how many transports the cache currently holds.

func (*TransportCache[K]) Purge

func (c *TransportCache[K]) Purge()

Purge drops every entry and closes the idle connections it was holding.

type UTF8SplitBuffer

type UTF8SplitBuffer struct {
	// contains filtered or unexported fields
}

UTF8SplitBuffer buffers incomplete UTF-8 byte sequences across chunk boundaries.

func (*UTF8SplitBuffer) Feed

func (b *UTF8SplitBuffer) Feed(chunk []byte) string

Feed consumes a byte chunk, prepending any pending remainder, and returns complete UTF-8 strings.

type UpstreamRequestLog

type UpstreamRequestLog struct {
	URL       string
	Method    string
	Headers   http.Header
	Body      []byte
	Provider  string
	AuthID    string
	AuthLabel string
	AuthType  string
	AuthValue string
}

UpstreamRequestLog captures the outbound upstream request details for logging.

type UsageReporter

type UsageReporter struct {
	// contains filtered or unexported fields
}

func NewExecutorUsageReporter

func NewExecutorUsageReporter(ctx context.Context, executor usageExecutor, model string, auth *cliproxyauth.Auth) *UsageReporter

func NewUsageReporter

func NewUsageReporter(ctx context.Context, provider, model string, auth *cliproxyauth.Auth) *UsageReporter

func (*UsageReporter) AccessProvider added in v8.0.24

func (r *UsageReporter) AccessProvider() string

AccessProvider identifies the client request authentication provider.

func (*UsageReporter) EnsurePublished

func (r *UsageReporter) EnsurePublished(ctx context.Context)

ensurePublished guarantees that a usage record is emitted exactly once. It is safe to call multiple times; only the first call wins due to once.Do. This is used to ensure request counting even when upstream responses do not include any usage fields (tokens), especially for streaming paths.

func (*UsageReporter) IsFirstPacketSet

func (r *UsageReporter) IsFirstPacketSet() bool

func (*UsageReporter) IsNativeKey added in v8.0.24

func (r *UsageReporter) IsNativeKey() bool

IsNativeKey reports whether the client API key was authenticated by CPA's native config provider.

func (*UsageReporter) IsResponseModelFinal

func (r *UsageReporter) IsResponseModelFinal() bool

IsResponseModelFinal reports whether the response model was already finalized by a terminal event.

func (*UsageReporter) IsTTFTSet

func (r *UsageReporter) IsTTFTSet() bool

func (*UsageReporter) MarkFirstResponseByte

func (r *UsageReporter) MarkFirstResponseByte()

func (*UsageReporter) ObserveResponse

func (r *UsageReporter) ObserveResponse(resp *http.Response)

func (*UsageReporter) ObserveResponseModel

func (r *UsageReporter) ObserveResponseModel(payload []byte)

ObserveResponseModel stores the model reported by an upstream response or event and ignores payloads without one; the substitution warning is emitted at publish time.

func (*UsageReporter) ObserveResponsePacketOnly

func (r *UsageReporter) ObserveResponsePacketOnly(resp *http.Response)

func (*UsageReporter) ObserveTokenEvent

func (r *UsageReporter) ObserveTokenEvent(isToken bool)

ObserveTokenEvent records the first packet fallback time on the first frame, and if isToken is true, records the effective TTFT. It uses a fast-path read check to return immediately with zero write lock contention once TTFT is set or when subsequent non-token metadata frames arrive after the first packet.

func (*UsageReporter) Publish

func (r *UsageReporter) Publish(ctx context.Context, detail usage.Detail)

func (*UsageReporter) PublishAdditionalModel

func (r *UsageReporter) PublishAdditionalModel(ctx context.Context, model string, detail usage.Detail)

func (*UsageReporter) PublishFailure

func (r *UsageReporter) PublishFailure(ctx context.Context, errs ...error)

func (*UsageReporter) PublishFailureWithDetail

func (r *UsageReporter) PublishFailureWithDetail(ctx context.Context, detail usage.Detail, errs ...error)

func (*UsageReporter) RecordFirstPacket

func (r *UsageReporter) RecordFirstPacket()

RecordFirstPacket records the arrival time of the first packet/chunk from upstream as a fallback.

func (*UsageReporter) RequestID

func (r *UsageReporter) RequestID() string

RequestID returns the execution instance request ID for this reporter.

func (*UsageReporter) ResponseModel

func (r *UsageReporter) ResponseModel() string

ResponseModel returns the latest model reported by the upstream response.

func (*UsageReporter) SelectionID added in v8.0.44

func (r *UsageReporter) SelectionID() string

SelectionID returns the scheduler selection ID for this reporter.

func (*UsageReporter) SetResponseModel

func (r *UsageReporter) SetResponseModel(model string)

SetResponseModel sets the reported model directly if valid and not already marked final.

func (*UsageReporter) SetSessionHierarchy

func (r *UsageReporter) SetSessionHierarchy(sessionID, parentSessionID string)

SetSessionHierarchy sets the explicit session and parent session identifiers. Callers should invoke this method before Publish or EnsurePublished on the request thread.

func (*UsageReporter) SetStream

func (r *UsageReporter) SetStream(stream bool)

SetStream records whether the request was executed in streaming mode.

func (*UsageReporter) SetTranslatedReasoningEffort

func (r *UsageReporter) SetTranslatedReasoningEffort(payload []byte, format string)

SetTranslatedReasoningEffort is retained for existing executor call sites. It captures all post-translation request metadata used by usage records.

func (*UsageReporter) SetTranslatedRequestMetadata added in v8.0.39

func (r *UsageReporter) SetTranslatedRequestMetadata(payload []byte, format string)

SetTranslatedRequestMetadata captures usage-relevant values from the final outbound payload after translation and payload defaults or overrides.

func (*UsageReporter) SetUpstreamModel

func (r *UsageReporter) SetUpstreamModel(model string)

SetUpstreamModel records the upstream model expected to be served when it differs from the requested model (e.g. due to provider-specific mapping or canonicalization). Model substitution detection compares the response against this upstream model, while usage accounting preserves the client's requested model.

func (*UsageReporter) StartResponseTTFT

func (r *UsageReporter) StartResponseTTFT()

func (*UsageReporter) TraceID

func (r *UsageReporter) TraceID() string

TraceID returns the parent inbound request ID for this reporter.

func (*UsageReporter) TrackFailure

func (r *UsageReporter) TrackFailure(ctx context.Context, errPtr *error)

func (*UsageReporter) TrackHTTPClient

func (r *UsageReporter) TrackHTTPClient(client *http.Client) *http.Client

func (*UsageReporter) TrackHTTPClientRoundTripOnly

func (r *UsageReporter) TrackHTTPClientRoundTripOnly(client *http.Client) *http.Client

TrackHTTPClientRoundTripOnly records the TTFT start time upon sending the request and captures first-packet arrival fallback on initial body reads, while keeping effective TTFT unset. This allows protocol-aware streaming executors (like Codex SSE) to mark effective TTFT explicitly upon receiving substantive token events, while preserving first-packet fallback metrics for non-2xx error bodies or keepalive streams.

func (*UsageReporter) UpdateAccessTokenFingerprint

func (r *UsageReporter) UpdateAccessTokenFingerprint(auth *cliproxyauth.Auth)

UpdateAccessTokenFingerprint records the token version actually used upstream.

func (*UsageReporter) UpstreamModel

func (r *UsageReporter) UpstreamModel() string

UpstreamModel returns the expected upstream model, or an empty string if not explicitly set.

Source Files

Jump to

Keyboard shortcuts

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