2385 lines · 80.3 KB · Go
RawDownload
package executor import (	"bufio"	"bytes"	"compress/flate"	"compress/gzip"	"context"	"crypto/sha256"	"encoding/hex"	"fmt"	"io"	"net/http"	"strings"	"time" 	"github.com/andybalholm/brotli"	"github.com/google/uuid"	"github.com/klauspost/compress/zstd"	claudeauth "github.com/router-for-me/CLIProxyAPI/v7/internal/auth/claude"	"github.com/router-for-me/CLIProxyAPI/v7/internal/config"	"github.com/router-for-me/CLIProxyAPI/v7/internal/misc"	"github.com/router-for-me/CLIProxyAPI/v7/internal/registry"	"github.com/router-for-me/CLIProxyAPI/v7/internal/runtime/executor/helps"	"github.com/router-for-me/CLIProxyAPI/v7/internal/thinking"	"github.com/router-for-me/CLIProxyAPI/v7/internal/util"	cliproxyauth "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/auth"	cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/executor"	sdktranslator "github.com/router-for-me/CLIProxyAPI/v7/sdk/translator"	log "github.com/sirupsen/logrus"	"github.com/tidwall/gjson"	"github.com/tidwall/sjson" 	"github.com/gin-gonic/gin") // ClaudeExecutor is a stateless executor for Anthropic Claude over the messages API.// If api_key is unavailable on auth, it falls back to legacy via ClientAdapter.type ClaudeExecutor struct {	cfg *config.Config} // claudeToolPrefix is empty to match real Claude Code behavior (no tool name prefix).// Previously "proxy_" was used but this is a detectable fingerprint difference.const claudeToolPrefix = "" // oauthToolRenameMap maps OpenCode-style (lowercase) tool names to Claude Code-style// (TitleCase) names. Anthropic uses tool name fingerprinting to detect third-party// clients on OAuth traffic. Renaming to official names avoids extra-usage billing.// All tools are mapped to TitleCase equivalents to match Claude Code naming patterns.var oauthToolRenameMap = map[string]string{	"bash":         "Bash",	"read":         "Read",	"write":        "Write",	"edit":         "Edit",	"glob":         "Glob",	"grep":         "Grep",	"task":         "Task",	"webfetch":     "WebFetch",	"todowrite":    "TodoWrite",	"question":     "Question",	"skill":        "Skill",	"ls":           "LS",	"todoread":     "TodoRead",	"notebookedit": "NotebookEdit",} // The reverse map is now computed per-request in remapOAuthToolNames so that// only names the client actually caused us to rewrite are restored on the// response. A global reverse map — as used previously — corrupted responses// for clients that sent mixed casing (e.g. Amp CLI sends `Bash` TitleCase// alongside `glob` lowercase; the request flagged renames via `glob→Glob`,// then the global reverse map incorrectly rewrote every `Bash` in the// response to `bash`, causing Amp to reject the tool_use as unknown). // oauthToolsToRemove lists tool names that must be stripped from OAuth requests// even after remapping. Currently empty — all tools are mapped instead of removed.var oauthToolsToRemove = map[string]bool{} // Anthropic-compatible upstreams may reject or even crash when Claude models// omit max_tokens. Prefer registered model metadata before using a fallback.const defaultModelMaxTokens = 1024 func NewClaudeExecutor(cfg *config.Config) *ClaudeExecutor { return &ClaudeExecutor{cfg: cfg} } func (e *ClaudeExecutor) Identifier() string { return "claude" } // PrepareRequest injects Claude credentials into the outgoing HTTP request.func (e *ClaudeExecutor) PrepareRequest(req *http.Request, auth *cliproxyauth.Auth) error {	if req == nil {		return nil	}	apiKey, _ := claudeCreds(auth)	if strings.TrimSpace(apiKey) == "" {		return nil	}	useAPIKey := auth != nil && auth.Attributes != nil && strings.TrimSpace(auth.Attributes["api_key"]) != ""	isAnthropicBase := req.URL != nil && strings.EqualFold(req.URL.Scheme, "https") && strings.EqualFold(req.URL.Host, "api.anthropic.com")	if isAnthropicBase && useAPIKey {		req.Header.Del("Authorization")		req.Header.Set("x-api-key", apiKey)	} else {		req.Header.Del("x-api-key")		req.Header.Set("Authorization", "Bearer "+apiKey)	}	var attrs map[string]string	if auth != nil {		attrs = auth.Attributes	}	util.ApplyCustomHeadersFromAttrs(req, attrs)	return nil} // HttpRequest injects Claude credentials into the request and executes it.func (e *ClaudeExecutor) HttpRequest(ctx context.Context, auth *cliproxyauth.Auth, req *http.Request) (*http.Response, error) {	if req == nil {		return nil, fmt.Errorf("claude executor: request is nil")	}	if ctx == nil {		ctx = req.Context()	}	httpReq := req.WithContext(ctx)	if err := e.PrepareRequest(httpReq, auth); err != nil {		return nil, err	}	httpClient := helps.NewUtlsHTTPClient(e.cfg, auth, 0)	return httpClient.Do(httpReq)} func (e *ClaudeExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) (resp cliproxyexecutor.Response, err error) {	if opts.Alt == "responses/compact" {		return resp, statusErr{code: http.StatusNotImplemented, msg: "/responses/compact not supported"}	}	baseModel := thinking.ParseSuffix(req.Model).ModelName 	apiKey, baseURL := claudeCreds(auth)	if baseURL == "" {		baseURL = "https://api.anthropic.com"	} 	reporter := helps.NewUsageReporter(ctx, e.Identifier(), baseModel, auth)	defer reporter.TrackFailure(ctx, &err)	from := opts.SourceFormat	to := sdktranslator.FromString("claude")	// Use streaming translation to preserve function calling, except for claude.	stream := from != to	originalPayloadSource := req.Payload	if len(opts.OriginalRequest) > 0 {		originalPayloadSource = opts.OriginalRequest	}	originalPayload := originalPayloadSource	originalTranslated := sdktranslator.TranslateRequest(from, to, baseModel, originalPayload, stream)	body := sdktranslator.TranslateRequest(from, to, baseModel, req.Payload, stream)	body, _ = sjson.SetBytes(body, "model", baseModel) 	body, err = thinking.ApplyThinking(body, req.Model, from.String(), to.String(), e.Identifier())	if err != nil {		return resp, err	} 	// Apply cloaking (system prompt injection, fake user ID, sensitive word obfuscation)	// based on client type and configuration.	body = applyCloaking(ctx, e.cfg, auth, body, baseModel, apiKey) 	requestedModel := helps.PayloadRequestedModel(opts, req.Model)	requestPath := helps.PayloadRequestPath(opts)	body = helps.ApplyPayloadConfigWithRoot(e.cfg, baseModel, to.String(), "", body, originalTranslated, requestedModel, requestPath)	body = ensureModelMaxTokens(body, baseModel) 	// Disable thinking if tool_choice forces tool use (Anthropic API constraint)	body = disableThinkingIfToolChoiceForced(body)	body = normalizeClaudeTemperatureForThinking(body) 	// Auto-inject cache_control if missing (optimization for ClawdBot/clients without caching support)	if countCacheControls(body) == 0 {		body = ensureCacheControl(body)	} 	// Enforce Anthropic's cache_control block limit (max 4 breakpoints per request).	// Cloaking and ensureCacheControl may push the total over 4 when the client	// (e.g. Amp CLI) already sends multiple cache_control blocks.	body = enforceCacheControlLimit(body, 4) 	// Normalize TTL values to prevent ordering violations under prompt-caching-scope-2026-01-05.	// A 1h-TTL block must not appear after a 5m-TTL block in evaluation order (tools→system→messages).	body = normalizeCacheControlTTL(body) 	// Extract betas from body and convert to header	var extraBetas []string	extraBetas, body = extractAndRemoveBetas(body)	bodyForTranslation := body	bodyForUpstream := body	oauthToken := isClaudeOAuthToken(apiKey)	var oauthToolNamesReverseMap map[string]string	if oauthToken {		bodyForUpstream, oauthToolNamesReverseMap = prepareClaudeOAuthToolNamesForUpstream(bodyForUpstream, claudeToolPrefix, auth.ToolPrefixDisabled())	}	// Enable cch signing by default for OAuth tokens (not just experimental flag).	// Claude Code always computes cch; missing or invalid cch is a detectable fingerprint.	if oauthToken || experimentalCCHSigningEnabled(e.cfg, auth) {		bodyForUpstream = signAnthropicMessagesBody(bodyForUpstream)	} 	url := fmt.Sprintf("%s/v1/messages?beta=true", baseURL)	httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(bodyForUpstream))	if err != nil {		return resp, err	}	applyClaudeHeaders(httpReq, auth, apiKey, false, extraBetas, e.cfg)	var authID, authLabel, authType, authValue string	if auth != nil {		authID = auth.ID		authLabel = auth.Label		authType, authValue = auth.AccountInfo()	}	helps.RecordAPIRequest(ctx, e.cfg, helps.UpstreamRequestLog{		URL:       url,		Method:    http.MethodPost,		Headers:   httpReq.Header.Clone(),		Body:      bodyForUpstream,		Provider:  e.Identifier(),		AuthID:    authID,		AuthLabel: authLabel,		AuthType:  authType,		AuthValue: authValue,	}) 	httpClient := helps.NewUtlsHTTPClient(e.cfg, auth, 0)	httpResp, err := httpClient.Do(httpReq)	if err != nil {		helps.RecordAPIResponseError(ctx, e.cfg, err)		return resp, err	}	helps.RecordAPIResponseMetadata(ctx, e.cfg, httpResp.StatusCode, httpResp.Header.Clone())	if httpResp.StatusCode < 200 || httpResp.StatusCode >= 300 {		// Decompress error responses — pass the Content-Encoding value (may be empty)		// and let decodeResponseBody handle both header-declared and magic-byte-detected		// compression.  This keeps error-path behaviour consistent with the success path.		errBody, decErr := decodeResponseBody(httpResp.Body, httpResp.Header.Get("Content-Encoding"))		if decErr != nil {			helps.RecordAPIResponseError(ctx, e.cfg, decErr)			msg := fmt.Sprintf("failed to decode error response body: %v", decErr)			helps.LogWithRequestID(ctx).Warn(msg)			return resp, statusErr{code: httpResp.StatusCode, msg: msg}		}		b, readErr := io.ReadAll(errBody)		if readErr != nil {			helps.RecordAPIResponseError(ctx, e.cfg, readErr)			msg := fmt.Sprintf("failed to read error response body: %v", readErr)			helps.LogWithRequestID(ctx).Warn(msg)			b = []byte(msg)		}		helps.AppendAPIResponseChunk(ctx, e.cfg, b)		helps.LogWithRequestID(ctx).Debugf("request error, error status: %d, error message: %s", httpResp.StatusCode, helps.SummarizeErrorBody(httpResp.Header.Get("Content-Type"), b))		err = statusErr{code: httpResp.StatusCode, msg: string(b)}		if errClose := errBody.Close(); errClose != nil {			log.Errorf("response body close error: %v", errClose)		}		return resp, err	}	decodedBody, err := decodeResponseBody(httpResp.Body, httpResp.Header.Get("Content-Encoding"))	if err != nil {		helps.RecordAPIResponseError(ctx, e.cfg, err)		if errClose := httpResp.Body.Close(); errClose != nil {			log.Errorf("response body close error: %v", errClose)		}		return resp, err	}	defer func() {		if errClose := decodedBody.Close(); errClose != nil {			log.Errorf("response body close error: %v", errClose)		}	}()	data, err := io.ReadAll(decodedBody)	if err != nil {		helps.RecordAPIResponseError(ctx, e.cfg, err)		return resp, err	}	helps.AppendAPIResponseChunk(ctx, e.cfg, data)	if stream {		if errValidate := validateClaudeStreamingResponse(data); errValidate != nil {			helps.RecordAPIResponseError(ctx, e.cfg, errValidate)			return resp, errValidate		}		lines := bytes.Split(data, []byte("\n"))		for _, line := range lines {			if detail, ok := helps.ParseClaudeStreamUsage(line); ok {				reporter.Publish(ctx, detail)			}		}	} else {		reporter.Publish(ctx, helps.ParseClaudeUsage(data))	}	data = restoreClaudeOAuthToolNamesFromResponse(data, claudeToolPrefix, auth.ToolPrefixDisabled(), oauthToolNamesReverseMap)	var param any	out := sdktranslator.TranslateNonStream(		ctx,		to,		from,		req.Model,		opts.OriginalRequest,		bodyForTranslation,		data,		&param,	)	resp = cliproxyexecutor.Response{Payload: out, Headers: httpResp.Header.Clone()}	return resp, nil} func (e *ClaudeExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) (_ *cliproxyexecutor.StreamResult, err error) {	if opts.Alt == "responses/compact" {		return nil, statusErr{code: http.StatusNotImplemented, msg: "/responses/compact not supported"}	}	baseModel := thinking.ParseSuffix(req.Model).ModelName 	apiKey, baseURL := claudeCreds(auth)	if baseURL == "" {		baseURL = "https://api.anthropic.com"	} 	reporter := helps.NewUsageReporter(ctx, e.Identifier(), baseModel, auth)	defer reporter.TrackFailure(ctx, &err)	from := opts.SourceFormat	to := sdktranslator.FromString("claude")	originalPayloadSource := req.Payload	if len(opts.OriginalRequest) > 0 {		originalPayloadSource = opts.OriginalRequest	}	originalPayload := originalPayloadSource	originalTranslated := sdktranslator.TranslateRequest(from, to, baseModel, originalPayload, true)	body := sdktranslator.TranslateRequest(from, to, baseModel, req.Payload, true)	body, _ = sjson.SetBytes(body, "model", baseModel) 	body, err = thinking.ApplyThinking(body, req.Model, from.String(), to.String(), e.Identifier())	if err != nil {		return nil, err	} 	// Apply cloaking (system prompt injection, fake user ID, sensitive word obfuscation)	// based on client type and configuration.	body = applyCloaking(ctx, e.cfg, auth, body, baseModel, apiKey) 	requestedModel := helps.PayloadRequestedModel(opts, req.Model)	requestPath := helps.PayloadRequestPath(opts)	body = helps.ApplyPayloadConfigWithRoot(e.cfg, baseModel, to.String(), "", body, originalTranslated, requestedModel, requestPath)	body = ensureModelMaxTokens(body, baseModel) 	// Disable thinking if tool_choice forces tool use (Anthropic API constraint)	body = disableThinkingIfToolChoiceForced(body)	body = normalizeClaudeTemperatureForThinking(body) 	// Auto-inject cache_control if missing (optimization for ClawdBot/clients without caching support)	if countCacheControls(body) == 0 {		body = ensureCacheControl(body)	} 	// Enforce Anthropic's cache_control block limit (max 4 breakpoints per request).	body = enforceCacheControlLimit(body, 4) 	// Normalize TTL values to prevent ordering violations under prompt-caching-scope-2026-01-05.	body = normalizeCacheControlTTL(body) 	// Extract betas from body and convert to header	var extraBetas []string	extraBetas, body = extractAndRemoveBetas(body)	bodyForTranslation := body	bodyForUpstream := body	oauthToken := isClaudeOAuthToken(apiKey)	var oauthToolNamesReverseMap map[string]string	if oauthToken {		bodyForUpstream, oauthToolNamesReverseMap = prepareClaudeOAuthToolNamesForUpstream(bodyForUpstream, claudeToolPrefix, auth.ToolPrefixDisabled())	}	// Enable cch signing by default for OAuth tokens (not just experimental flag).	if oauthToken || experimentalCCHSigningEnabled(e.cfg, auth) {		bodyForUpstream = signAnthropicMessagesBody(bodyForUpstream)	} 	url := fmt.Sprintf("%s/v1/messages?beta=true", baseURL)	httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(bodyForUpstream))	if err != nil {		return nil, err	}	applyClaudeHeaders(httpReq, auth, apiKey, true, extraBetas, e.cfg)	var authID, authLabel, authType, authValue string	if auth != nil {		authID = auth.ID		authLabel = auth.Label		authType, authValue = auth.AccountInfo()	}	helps.RecordAPIRequest(ctx, e.cfg, helps.UpstreamRequestLog{		URL:       url,		Method:    http.MethodPost,		Headers:   httpReq.Header.Clone(),		Body:      bodyForUpstream,		Provider:  e.Identifier(),		AuthID:    authID,		AuthLabel: authLabel,		AuthType:  authType,		AuthValue: authValue,	}) 	httpClient := helps.NewUtlsHTTPClient(e.cfg, auth, 0)	httpResp, err := httpClient.Do(httpReq)	if err != nil {		helps.RecordAPIResponseError(ctx, e.cfg, err)		return nil, err	}	helps.RecordAPIResponseMetadata(ctx, e.cfg, httpResp.StatusCode, httpResp.Header.Clone())	if httpResp.StatusCode < 200 || httpResp.StatusCode >= 300 {		// Decompress error responses — pass the Content-Encoding value (may be empty)		// and let decodeResponseBody handle both header-declared and magic-byte-detected		// compression.  This keeps error-path behaviour consistent with the success path.		errBody, decErr := decodeResponseBody(httpResp.Body, httpResp.Header.Get("Content-Encoding"))		if decErr != nil {			helps.RecordAPIResponseError(ctx, e.cfg, decErr)			msg := fmt.Sprintf("failed to decode error response body: %v", decErr)			helps.LogWithRequestID(ctx).Warn(msg)			return nil, statusErr{code: httpResp.StatusCode, msg: msg}		}		b, readErr := io.ReadAll(errBody)		if readErr != nil {			helps.RecordAPIResponseError(ctx, e.cfg, readErr)			msg := fmt.Sprintf("failed to read error response body: %v", readErr)			helps.LogWithRequestID(ctx).Warn(msg)			b = []byte(msg)		}		helps.AppendAPIResponseChunk(ctx, e.cfg, b)		helps.LogWithRequestID(ctx).Debugf("request error, error status: %d, error message: %s", httpResp.StatusCode, helps.SummarizeErrorBody(httpResp.Header.Get("Content-Type"), b))		if errClose := errBody.Close(); errClose != nil {			log.Errorf("response body close error: %v", errClose)		}		err = statusErr{code: httpResp.StatusCode, msg: string(b)}		return nil, err	}	decodedBody, err := decodeResponseBody(httpResp.Body, httpResp.Header.Get("Content-Encoding"))	if err != nil {		helps.RecordAPIResponseError(ctx, e.cfg, err)		if errClose := httpResp.Body.Close(); errClose != nil {			log.Errorf("response body close error: %v", errClose)		}		return nil, err	}	out := make(chan cliproxyexecutor.StreamChunk)	go func() {		defer close(out)		defer func() {			if errClose := decodedBody.Close(); errClose != nil {				log.Errorf("response body close error: %v", errClose)			}		}() 		// If from == to (Claude → Claude), directly forward the SSE stream without translation		if from == to {			scanner := bufio.NewScanner(decodedBody)			scanner.Buffer(nil, 52_428_800) // 50MB			for scanner.Scan() {				line := scanner.Bytes()				helps.AppendAPIResponseChunk(ctx, e.cfg, line)				if detail, ok := helps.ParseClaudeStreamUsage(line); ok {					reporter.Publish(ctx, detail)				}				line = restoreClaudeOAuthToolNamesFromStreamLine(line, claudeToolPrefix, auth.ToolPrefixDisabled(), oauthToolNamesReverseMap)				// Forward the line as-is to preserve SSE format				cloned := make([]byte, len(line)+1)				copy(cloned, line)				cloned[len(line)] = '\n'				select {				case out <- cliproxyexecutor.StreamChunk{Payload: cloned}:				case <-ctx.Done():					return				}			}			if errScan := scanner.Err(); errScan != nil {				helps.RecordAPIResponseError(ctx, e.cfg, errScan)				reporter.PublishFailure(ctx, errScan)				select {				case out <- cliproxyexecutor.StreamChunk{Err: errScan}:				case <-ctx.Done():				}			}			return		} 		// For other formats, use translation		scanner := bufio.NewScanner(decodedBody)		scanner.Buffer(nil, 52_428_800) // 50MB		var param any		for scanner.Scan() {			line := scanner.Bytes()			helps.AppendAPIResponseChunk(ctx, e.cfg, line)			if detail, ok := helps.ParseClaudeStreamUsage(line); ok {				reporter.Publish(ctx, detail)			}			line = restoreClaudeOAuthToolNamesFromStreamLine(line, claudeToolPrefix, auth.ToolPrefixDisabled(), oauthToolNamesReverseMap)			chunks := sdktranslator.TranslateStream(				ctx,				to,				from,				req.Model,				opts.OriginalRequest,

Lines 1–500 of 2385.