mirror of
https://github.com/decolua/9router.git
synced 2026-05-08 12:01:28 +00:00
- Add claudeHeaderCache.js to intercept and cache live Claude Code client headers - Forward cached headers dynamically to api.anthropic.com via default.js - Strip first-party identity headers (x-app, claude-code-* beta) for non-Anthropic upstreams - Validate and sanitize tool call IDs to match Anthropic pattern (^[a-zA-Z0-9_-]+$) - Skip thinking blocks when applying cache_control; fix max_tokens buffer (+1024) - Strip cache_control from thinking blocks in openai-to-claude translator - Comment out thoughtSignature in Gemini translator (kept for reference) - Expand .gitignore to match all deploy*.sh variants Co-authored-by: kwanLeeFrmVi <quanle96@outlook.com> Closes #433 Made-with: Cursor
28 lines
946 B
JavaScript
28 lines
946 B
JavaScript
import { DEFAULT_MAX_TOKENS, DEFAULT_MIN_TOKENS } from "../../config/runtimeConfig.js";
|
|
|
|
/**
|
|
* Adjust max_tokens based on request context
|
|
* @param {object} body - Request body
|
|
* @returns {number} Adjusted max_tokens
|
|
*/
|
|
export function adjustMaxTokens(body) {
|
|
let maxTokens = body.max_tokens || DEFAULT_MAX_TOKENS;
|
|
|
|
// Auto-increase for tool calling to prevent truncated arguments
|
|
if (body.tools && Array.isArray(body.tools) && body.tools.length > 0) {
|
|
if (maxTokens < DEFAULT_MIN_TOKENS) {
|
|
maxTokens = DEFAULT_MIN_TOKENS;
|
|
}
|
|
}
|
|
|
|
// Ensure max_tokens > thinking.budget_tokens (Claude API requirement)
|
|
// Claude API requires strictly greater, so add buffer instead of using DEFAULT_MAX_TOKENS
|
|
// which could equal budget_tokens when budget_tokens >= 64000
|
|
if (body.thinking?.budget_tokens && maxTokens <= body.thinking.budget_tokens) {
|
|
maxTokens = body.thinking.budget_tokens + 1024;
|
|
}
|
|
|
|
return maxTokens;
|
|
}
|
|
|