Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/quality/eslint-suppressions.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
},
"open-sse/executors/base.ts": {
"@typescript-eslint/no-unused-vars": {
"count": 6
"count": 5
}
},
"open-sse/executors/blackbox-web.ts": {
Expand Down
192 changes: 185 additions & 7 deletions open-sse/config/anthropicHeaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
getClaudeCodeUserAgent,
} from "@/shared/constants/claudeCodeClient";
import { modelSupportsContext1mBeta } from "../config/context1m.ts";
import { usesCcWireImage } from "../services/ccWireImageBuiltins.ts";

export const ANTHROPIC_VERSION_HEADER = "2023-06-01";

Expand All @@ -28,7 +29,6 @@ const ANTHROPIC_BETA_BASE = Object.freeze([
"extended-cache-ttl-2025-04-11",
"cache-diagnosis-2026-04-07",
"code-execution-2025-08-25",
"skills-2025-10-02",
]);

const CLAUDE_OAUTH_EXTRA_BETAS = Object.freeze(["fine-grained-tool-streaming-2025-05-14"]);
Expand All @@ -43,6 +43,165 @@ export const ANTHROPIC_BETA_CLAUDE_OAUTH = [
...ANTHROPIC_BETA_BASE.slice(3),
].join(",");

/**
* Anthropic Skills beta flag. Must only be emitted when a code_execution tool
* is declared in the request body; sending it on tool-less requests causes
* upstream Anthropic to reject with HTTP 400 (#14200).
*/
export const SKILLS_BETA_HEADER = "skills-2025-10-02";

/**
* Append an Anthropic Beta header token to an existing headers object.
* Preserves case of existing anthropic-beta / Anthropic-Beta header,
* deduplicates tokens, and creates anthropic-beta if not present.
*/
export function appendAnthropicBetaHeader(
headers: Record<string, string>,
betaHeader: string
): void {
const existingKey = Object.keys(headers).find((key) => key.toLowerCase() === "anthropic-beta");
if (!existingKey) {
headers["anthropic-beta"] = betaHeader;
return;
}

const existingValues = String(headers[existingKey] || "")
.split(",")
.map((value) => value.trim())
.filter(Boolean);

if (!existingValues.includes(betaHeader)) {
headers[existingKey] = [...existingValues, betaHeader].join(",");
}
}

/**
* Remove an Anthropic Beta header token from an existing headers object.
* Preserves case of existing anthropic-beta / Anthropic-Beta header.
*/
export function removeAnthropicBetaHeader(
headers: Record<string, string>,
betaHeader: string
): void {
if (!headers || typeof headers !== "object") return;
const existingKey = Object.keys(headers).find((key) => key.toLowerCase() === "anthropic-beta");
if (!existingKey) return;

const existingValues = String(headers[existingKey] || "")
.split(",")
.map((value) => value.trim())
.filter((value) => value && value !== betaHeader);

if (existingValues.length > 0) {
headers[existingKey] = existingValues.join(",");
} else {
delete headers[existingKey];
}
}

/**
* Detect whether a request body contains an Anthropic code_execution tool.
* Anthropic requires the skills-2025-10-02 beta header only when a code_execution
* tool is present in the request body; sending it on tool-less requests causes
* upstream to reject with HTTP 400 (#14200).
*
* Accepts both parsed JSON objects and serialized JSON strings for robustness.
*/
export function hasCodeExecutionTool(body: unknown): boolean {
if (!body) return false;
let parsed: unknown = body;
if (typeof body === "string") {
try {
parsed = JSON.parse(body);
} catch {
return false;
}
}
if (!parsed || typeof parsed !== "object") return false;
const tools = (parsed as { tools?: unknown }).tools;
if (!Array.isArray(tools) || tools.length === 0) return false;
return tools.some((tool) => {
if (!tool || typeof tool !== "object") return false;
const t = tool as { type?: unknown; name?: unknown; function?: { name?: unknown } };
if (
typeof t.type === "string" &&
(t.type === "code_execution" || t.type.startsWith("code_execution_"))
) {
return true;
}
if (t.name === "code_execution") {
return true;
}
if (
t.type === "function" &&
t.function &&
typeof t.function === "object" &&
t.function.name === "code_execution"
) {
return true;
}
return false;
});
}

/**
* Append the Skills beta header to outbound Anthropic-family request headers
* if and only if a code_execution tool is present in the request body.
*
* Single source of truth across base executor, default executor, and provider headers (#14200).
*/
export function maybeAppendSkillsBeta(
headers: Record<string, string>,
provider: string | undefined | null,
body: unknown,
extraCondition = false
): void {
if (!headers || typeof headers !== "object") return;
if (!hasCodeExecutionTool(body)) return;

const p = typeof provider === "string" ? provider : "";
const isAnthropicFamily =
p === "anthropic" ||
p === "claude" ||
p.startsWith("anthropic-compatible-") ||
usesCcWireImage(p) ||
extraCondition;

if (isAnthropicFamily) {
appendAnthropicBetaHeader(headers, SKILLS_BETA_HEADER);
}
}

/**
* Synchronize the Skills beta header with the finalized request body.
* If the body contains a code_execution tool, ensures skills-2025-10-02 is present.
* If the body does not contain a code_execution tool (or tools were stripped/transformed),
* ensures skills-2025-10-02 is removed (#14200).
*/
export function syncSkillsBeta(
headers: Record<string, string>,
provider: string | undefined | null,
body: unknown,
extraCondition = false
): void {
if (!headers || typeof headers !== "object") return;
const p = typeof provider === "string" ? provider : "";
const isAnthropicFamily =
p === "anthropic" ||
p === "claude" ||
p.startsWith("anthropic-compatible-") ||
usesCcWireImage(p) ||
extraCondition;

if (!isAnthropicFamily) return;

if (hasCodeExecutionTool(body)) {
appendAnthropicBetaHeader(headers, SKILLS_BETA_HEADER);
} else {
removeAnthropicBetaHeader(headers, SKILLS_BETA_HEADER);
}
}

/**
* Client-negotiated `anthropic-beta` values that are safe to forward to the
* claude.ai backend on top of OmniRoute's own set. Kept to betas the backend
Expand Down Expand Up @@ -102,12 +261,19 @@ export const FORWARDABLE_CLIENT_BETAS = Object.freeze([
* there with "long context beta is not yet available for this subscription"
* (#10119). When no model is supplied (legacy callers without model resolution),
* the prior forwarding behavior is preserved.
*
* `body` (optional) gate: when supplied, `skills-2025-10-02` is forwarded only
* if the request body contains a `code_execution` tool (#14200). Sending the
* skills beta on tool-less requests causes upstream Anthropic to reject with
* HTTP 400 "Skills beta requires the code_execution tool". When omitted or null,
* backward-compatible allowlist forwarding is preserved.
*/
export function mergeClientAnthropicBeta(
base: string,
clientBeta: string | null | undefined,
allow: readonly string[] = FORWARDABLE_CLIENT_BETAS,
model?: string | null
model?: string | null,
body?: unknown
): string {
const baseList = base
.split(",")
Expand All @@ -118,9 +284,15 @@ export function mergeClientAnthropicBeta(
const allowList = allow
.map((s) => s.toLowerCase())
.filter((lower) => {
if (lower !== "context-1m-2025-08-07") return true;
if (model === undefined || model === null || model === "") return true;
return modelSupportsContext1mBeta(model);
if (lower === "context-1m-2025-08-07") {
if (model === undefined || model === null || model === "") return true;
return modelSupportsContext1mBeta(model);
}
if (lower === "skills-2025-10-02") {
if (body === undefined || body === null) return true;
return hasCodeExecutionTool(body);
}
return true;
});
const allowSet = new Set(allowList);
for (const token of clientBeta
Expand Down Expand Up @@ -150,13 +322,19 @@ export function mergeClientAnthropicBeta(
export function applyClientAnthropicBeta(
headers: Record<string, string>,
clientBeta: string | null | undefined,
options: { seedWhenAbsent?: boolean; model?: string | null } = {}
options: { seedWhenAbsent?: boolean; model?: string | null; body?: unknown } = {}
): void {
if (typeof clientBeta !== "string" || !clientBeta.trim()) return;
const existingKey = Object.keys(headers).find((key) => key.toLowerCase() === "anthropic-beta");
const key = existingKey ?? (options.seedWhenAbsent ? "anthropic-beta" : null);
if (!key) return;
const merged = mergeClientAnthropicBeta(headers[key] ?? "", clientBeta, undefined, options.model);
const merged = mergeClientAnthropicBeta(
headers[key] ?? "",
clientBeta,
undefined,
options.model,
options.body
);
if (merged) headers[key] = merged;
}

Expand Down
11 changes: 10 additions & 1 deletion open-sse/executors/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
mergeCcHeaders,
mergeClientAnthropicBeta,
normalizeAnthropicHeaderVariants,
maybeAppendSkillsBeta,
syncSkillsBeta,
} from "../config/anthropicHeaders.ts";
import { applyContextEditingToBody } from "../config/contextEditing.ts";
import { createCopilotIdentityFallback } from "./copilotIdentityFallback.ts";
Expand Down Expand Up @@ -515,6 +517,8 @@ export class BaseExecutor {

headers["Accept"] = stream ? "text/event-stream" : "application/json";

maybeAppendSkillsBeta(headers, this.provider, body, this.usesClaudeCodeProtocol(credentials));

normalizeAnthropicHeaderVariants(headers);

return headers;
Expand Down Expand Up @@ -885,6 +889,9 @@ export class BaseExecutor {
clampNestedThinkingBudget(transformedBody, thinkingBudgetClampedMax);
}

// Re-synchronize skills beta with the finalized transformed body (#14200):
syncSkillsBeta(headers, this.provider, transformedBody, usesClaudeCodeProtocol);

// Timeout only covers response start; stream stalls are handled downstream.
// #11526: streaming requests cap the headers-wait phase to a client-realistic
// ceiling (see fetchStartTimeoutPolicy.ts) — non-streaming keeps the flat default.
Expand Down Expand Up @@ -1228,7 +1235,9 @@ export class BaseExecutor {
// Gate the client-negotiated context-1m beta on the RESOLVED target:
// combo/fallback can route a request negotiated for a [1m] sibling onto a
// model that does not qualify (e.g. Haiku), which Anthropic rejects (#10119).
model
model,
// Gate skills-2025-10-02 on presence of code_execution tool in transformed body (#14200):
tb
),
"anthropic-dangerous-direct-browser-access": "true",
"x-app": "cli",
Expand Down
9 changes: 8 additions & 1 deletion open-sse/executors/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { getModelTargetFormat } from "../config/providerModels.ts";
import {
applyClientAnthropicBeta,
normalizeAnthropicHeaderVariants,
maybeAppendSkillsBeta,
} from "../config/anthropicHeaders.ts";
import { isOfficialAnthropicBaseUrl } from "../utils/anthropicHost.ts";
import { applyProviderRequestDefaults } from "../services/providerRequestDefaults.ts";
Expand Down Expand Up @@ -473,7 +474,9 @@ export class DefaultExecutor extends BaseExecutor {
credentials,
stream = true,
clientHeaders?: Record<string, string> | null,
model?: string | null
model?: string | null,
_health?: unknown,
body?: unknown
) {
const { headers, effectiveKey } = this.buildHeadersPreamble(credentials, stream);

Expand Down Expand Up @@ -690,12 +693,16 @@ export class DefaultExecutor extends BaseExecutor {
// `model` gates the client-negotiated context-1m beta on the RESOLVED target:
// combo/fallback can route a request negotiated for a [1m] sibling onto a model
// that does not qualify (e.g. Haiku), which Anthropic rejects (#10119).
// `body` gates skills-2025-10-02 on presence of code_execution tool (#14200).
applyClientAnthropicBeta(headers, clientBeta, {
seedWhenAbsent: this.provider?.startsWith?.("anthropic-compatible-") === true,
model,
body,
});
}

maybeAppendSkillsBeta(headers, this.provider, body, this.usesClaudeCodeProtocol(credentials));

normalizeAnthropicHeaderVariants(headers);

return headers;
Expand Down
27 changes: 8 additions & 19 deletions open-sse/services/claudeCodeCompatible.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,25 +141,14 @@ export function joinClaudeCodeCompatibleUrl(baseUrl: string, path: string): stri
return joinNormalizedBaseUrlAndPath(stripClaudeCodeCompatibleEndpointSuffix(baseUrl), path);
}

export function appendAnthropicBetaHeader(
headers: Record<string, string>,
betaHeader: string
): void {
const existingKey = Object.keys(headers).find((key) => key.toLowerCase() === "anthropic-beta");
if (!existingKey) {
headers["anthropic-beta"] = betaHeader;
return;
}

const existingValues = String(headers[existingKey] || "")
.split(",")
.map((value) => value.trim())
.filter(Boolean);

if (!existingValues.includes(betaHeader)) {
headers[existingKey] = [...existingValues, betaHeader].join(",");
}
}
export {
appendAnthropicBetaHeader,
removeAnthropicBetaHeader,
hasCodeExecutionTool,
maybeAppendSkillsBeta,
syncSkillsBeta,
SKILLS_BETA_HEADER,
} from "../config/anthropicHeaders.ts";

// Re-exported from the shared context1m module so existing importers of this
// helper (base.ts) keep working; the eligibility list now has one source of truth.
Expand Down
8 changes: 7 additions & 1 deletion open-sse/services/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
buildClaudeCodeCompatibleHeaders,
CLAUDE_CODE_COMPATIBLE_DEFAULT_CHAT_PATH,
joinClaudeCodeCompatibleUrl,
maybeAppendSkillsBeta,
} from "./claudeCodeCompatible.ts";
import { getClaudeCodeCompatibleRequestDefaults } from "@/lib/providers/requestDefaults";
import { buildClineHeaders } from "@/shared/utils/clineAuth";
Expand Down Expand Up @@ -348,7 +349,6 @@ export function buildProviderUrl(

// Build provider headers
export function buildProviderHeaders(provider, credentials, stream = true, body = null) {
void body;
const config = getProviderConfig(provider);
const entry = getRegistryEntry(provider);
const headers = {
Expand Down Expand Up @@ -383,6 +383,9 @@ export function buildProviderHeaders(provider, credentials, stream = true, body
ccHeaders["Authorization"] = `Bearer ${token}`;
}
}
// For CC-compatible providers returning early, ensure skills beta is conditionally applied
// (within this block, isClaudeCodeCompatible(provider) is guaranteed true):
maybeAppendSkillsBeta(ccHeaders, provider, body, true);
return ccHeaders;
}
if (isAnthropicCompatible(provider)) {
Expand Down Expand Up @@ -446,6 +449,9 @@ export function buildProviderHeaders(provider, credentials, stream = true, body
headers["Accept"] = "text/event-stream";
}

// For standard/compatible provider paths, ensure skills beta is conditionally applied
maybeAppendSkillsBeta(headers, provider, body);

return headers;
}

Expand Down
Loading
Loading