coderouter-cli 2.16.0__py3-none-any.whl → 2.18.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- coderouter/adapters/anthropic_native.py +4 -0
- coderouter/adapters/base.py +20 -0
- coderouter/adapters/openai_compat.py +6 -0
- coderouter/adapters/slot_affinity.py +54 -0
- coderouter/cli.py +2 -1
- coderouter/config/schemas.py +58 -1
- coderouter/doctor_claude_code.py +12 -0
- coderouter/ingress/anthropic_errors.py +24 -5
- coderouter/ingress/anthropic_routes.py +20 -6
- coderouter/ingress/openai_routes.py +7 -2
- coderouter/routing/auto_router.py +46 -2
- coderouter/routing/capability.py +4 -1
- coderouter/routing/fallback.py +94 -6
- coderouter/routing/fallback_trace.py +8 -1
- coderouter/translation/anthropic.py +3 -0
- coderouter/translation/convert.py +4 -0
- coderouter/vscode_init.py +10 -1
- {coderouter_cli-2.16.0.dist-info → coderouter_cli-2.18.0.dist-info}/METADATA +1 -1
- {coderouter_cli-2.16.0.dist-info → coderouter_cli-2.18.0.dist-info}/RECORD +22 -21
- {coderouter_cli-2.16.0.dist-info → coderouter_cli-2.18.0.dist-info}/WHEEL +0 -0
- {coderouter_cli-2.16.0.dist-info → coderouter_cli-2.18.0.dist-info}/entry_points.txt +0 -0
- {coderouter_cli-2.16.0.dist-info → coderouter_cli-2.18.0.dist-info}/licenses/LICENSE +0 -0
|
@@ -46,6 +46,7 @@ from coderouter.adapters.base import (
|
|
|
46
46
|
StreamChunk,
|
|
47
47
|
StreamTruncatedError,
|
|
48
48
|
)
|
|
49
|
+
from coderouter.adapters.slot_affinity import apply_slot_affinity
|
|
49
50
|
from coderouter.credentials import resolve_provider_credential
|
|
50
51
|
from coderouter.logging import (
|
|
51
52
|
get_logger,
|
|
@@ -243,6 +244,9 @@ class AnthropicAdapter(BaseAdapter):
|
|
|
243
244
|
body.update(dumped)
|
|
244
245
|
body["model"] = self.config.model
|
|
245
246
|
body["stream"] = stream
|
|
247
|
+
apply_slot_affinity(
|
|
248
|
+
body, slots=self.config.slot_affinity_slots, key=req.session_key
|
|
249
|
+
)
|
|
246
250
|
return body
|
|
247
251
|
|
|
248
252
|
# ------------------------------------------------------------------
|
coderouter/adapters/base.py
CHANGED
|
@@ -65,6 +65,9 @@ class ChatRequest(BaseModel):
|
|
|
65
65
|
|
|
66
66
|
# CodeRouter-specific extension (not sent upstream)
|
|
67
67
|
profile: str | None = Field(default=None, exclude=True)
|
|
68
|
+
# v2.18.0: conversation key for KV-slot affinity (see
|
|
69
|
+
# ``ProviderConfig.slot_affinity_slots``). Never serialized.
|
|
70
|
+
session_key: str | None = Field(default=None, exclude=True)
|
|
68
71
|
|
|
69
72
|
|
|
70
73
|
class ChatResponse(BaseModel):
|
|
@@ -200,6 +203,23 @@ class StreamTruncatedError(AdapterError):
|
|
|
200
203
|
self.tool_call_in_flight = tool_call_in_flight
|
|
201
204
|
|
|
202
205
|
|
|
206
|
+
# v2.17.0: the provider did not produce its first real streamed output within
|
|
207
|
+
# ``ProviderConfig.ttft_timeout_s``. Raised by the engine (not the adapter),
|
|
208
|
+
# before any byte reached the client, so the chain falls back normally and the
|
|
209
|
+
# self-healing guards learn from it like any other retryable failure.
|
|
210
|
+
class TTFTTimeoutError(AdapterError):
|
|
211
|
+
"""No first token within ``ttft_timeout_s``."""
|
|
212
|
+
|
|
213
|
+
def __init__(self, *, provider: str, ttft_timeout_s: float) -> None:
|
|
214
|
+
super().__init__(
|
|
215
|
+
f"timeout: no first token within {ttft_timeout_s:g}s (ttft_timeout_s)",
|
|
216
|
+
provider=provider,
|
|
217
|
+
status_code=None,
|
|
218
|
+
retryable=True,
|
|
219
|
+
)
|
|
220
|
+
self.ttft_timeout_s = ttft_timeout_s
|
|
221
|
+
|
|
222
|
+
|
|
203
223
|
# v0.6-B: per-call overrides resolved from the active profile. The engine
|
|
204
224
|
# builds one instance per request (since a profile is invariant across its
|
|
205
225
|
# chain) and threads it through every adapter call on that chain. Adapters
|
|
@@ -30,6 +30,7 @@ from coderouter.adapters.base import (
|
|
|
30
30
|
StreamChunk,
|
|
31
31
|
StreamTruncatedError,
|
|
32
32
|
)
|
|
33
|
+
from coderouter.adapters.slot_affinity import apply_slot_affinity
|
|
33
34
|
from coderouter.credentials import resolve_provider_credential
|
|
34
35
|
from coderouter.logging import (
|
|
35
36
|
get_logger,
|
|
@@ -182,6 +183,11 @@ class OpenAICompatAdapter(BaseAdapter):
|
|
|
182
183
|
value = getattr(request, field, None)
|
|
183
184
|
if value is not None:
|
|
184
185
|
body[field] = value
|
|
186
|
+
apply_slot_affinity(
|
|
187
|
+
body,
|
|
188
|
+
slots=self.config.slot_affinity_slots,
|
|
189
|
+
key=getattr(request, "session_key", None),
|
|
190
|
+
)
|
|
185
191
|
if stream:
|
|
186
192
|
# Request a terminal usage chunk. Providers that honor this
|
|
187
193
|
# (OpenAI, OpenRouter, Ollama >=0.x) will send one extra chunk
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"""v2.18.0: KV-slot affinity for llama-server (``id_slot``).
|
|
2
|
+
|
|
3
|
+
llama-server started with ``--parallel N`` serves N slots, each holding its
|
|
4
|
+
own prompt cache. Without a hint it picks a free slot, so consecutive turns
|
|
5
|
+
of one conversation can land on different slots and re-prefill the whole
|
|
6
|
+
prompt. Sending ``id_slot`` pins a conversation to one slot.
|
|
7
|
+
|
|
8
|
+
The slot is ``sha256(key) % N`` — stable across processes and restarts
|
|
9
|
+
(Python's ``hash`` is salted per process).
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
import hashlib
|
|
15
|
+
from collections.abc import Mapping
|
|
16
|
+
from typing import Any
|
|
17
|
+
|
|
18
|
+
# Request classes whose prompt shares the main conversation's prefix: they
|
|
19
|
+
# belong on the main conversation's slot. Everything else (sub-agents,
|
|
20
|
+
# auxiliary calls such as the auto-mode classifier or titles, workflows) has
|
|
21
|
+
# its own prompt and gets its own key, so it neither waits on the main slot
|
|
22
|
+
# nor evicts its cache.
|
|
23
|
+
_MAIN_CLASSES = frozenset({"", "main", "compaction"})
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def affinity_key(hints: Mapping[str, str]) -> str | None:
|
|
27
|
+
"""Conversation key for slot affinity, from Claude Code's hint headers.
|
|
28
|
+
|
|
29
|
+
``None`` when there is no session id. ``main`` / ``compaction`` (and a
|
|
30
|
+
missing class) share ``<session>``; other classes get
|
|
31
|
+
``<session>:<class>:<agent_type>``.
|
|
32
|
+
"""
|
|
33
|
+
session = hints.get("session_id")
|
|
34
|
+
if not session:
|
|
35
|
+
return None
|
|
36
|
+
request_class = hints.get("request_class", "").lower()
|
|
37
|
+
if request_class in _MAIN_CLASSES:
|
|
38
|
+
return session
|
|
39
|
+
return f"{session}:{request_class}:{hints.get('agent_type', '').lower()}"
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def slot_for(key: str, slots: int) -> int:
|
|
43
|
+
digest = hashlib.sha256(key.encode("utf-8")).digest()
|
|
44
|
+
return int.from_bytes(digest[:8], "big") % slots
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def apply_slot_affinity(body: dict[str, Any], *, slots: int | None, key: str | None) -> None:
|
|
48
|
+
"""Set ``body["id_slot"]`` when affinity is configured and a key exists.
|
|
49
|
+
|
|
50
|
+
An ``id_slot`` already present (e.g. from ``extra_body``) wins.
|
|
51
|
+
"""
|
|
52
|
+
if not slots or not key or "id_slot" in body:
|
|
53
|
+
return
|
|
54
|
+
body["id_slot"] = slot_for(key, slots)
|
coderouter/cli.py
CHANGED
|
@@ -483,7 +483,8 @@ def _build_parser() -> argparse.ArgumentParser:
|
|
|
483
483
|
help=(
|
|
484
484
|
"v2.16.0: do not write the Claude Code gateway settings "
|
|
485
485
|
"(ENABLE_TOOL_SEARCH=false, CLAUDE_CODE_AUTO_MODE_SERVER=0, "
|
|
486
|
-
"and --max-context-tokens).
|
|
486
|
+
"CLAUDE_CODE_GATEWAY_HINT_HEADERS=1 and --max-context-tokens). "
|
|
487
|
+
"Use when the chain is "
|
|
487
488
|
"api.anthropic.com only."
|
|
488
489
|
),
|
|
489
490
|
)
|
coderouter/config/schemas.py
CHANGED
|
@@ -550,6 +550,45 @@ class ProviderConfig(BaseModel):
|
|
|
550
550
|
description="If true, only used when ALLOW_PAID=true (plan.md §2.3).",
|
|
551
551
|
)
|
|
552
552
|
timeout_s: float = Field(default=30.0, ge=1.0, le=86400.0)
|
|
553
|
+
# v2.17.0: time-to-first-token cut-off for streaming requests. When the
|
|
554
|
+
# provider has not produced its first real output (text / tool_use /
|
|
555
|
+
# thinking delta) within this many seconds of the attempt starting, the
|
|
556
|
+
# attempt is abandoned and the chain moves on — before any byte reached
|
|
557
|
+
# the client. Never applied to the last provider of a chain (nothing to
|
|
558
|
+
# move on to), nor to non-streaming requests (``timeout_s`` covers
|
|
559
|
+
# those). Before the first *event* it always applies; between the first
|
|
560
|
+
# event and the first real output it needs
|
|
561
|
+
# ``empty_response_action: fallback`` on the profile, which is what
|
|
562
|
+
# withholds those opening events from the client. ``None`` (default)
|
|
563
|
+
# disables it. Typical use: a local llama-server still loading a model
|
|
564
|
+
# or chewing through a long prompt while a cloud fallback could answer.
|
|
565
|
+
# v2.18.0: KV-slot affinity for llama-server started with
|
|
566
|
+
# ``--parallel N``. When set to N and the request carries a conversation
|
|
567
|
+
# key (Claude Code's ``x-claude-code-session-id`` hint header), the body
|
|
568
|
+
# gets ``id_slot = stable_hash(key) % N`` so every turn of a conversation
|
|
569
|
+
# lands on the same slot and reuses its prompt cache instead of
|
|
570
|
+
# re-prefilling on whichever slot is free. llama.cpp only (other servers
|
|
571
|
+
# ignore or reject ``id_slot``); ``None`` (default) sends nothing.
|
|
572
|
+
slot_affinity_slots: int | None = Field(
|
|
573
|
+
default=None,
|
|
574
|
+
ge=1,
|
|
575
|
+
le=256,
|
|
576
|
+
description=(
|
|
577
|
+
"v2.18.0: llama-server --parallel N. Pins each conversation to "
|
|
578
|
+
"slot hash(session) % N via id_slot so its KV cache is reused. "
|
|
579
|
+
"llama.cpp only. None disables."
|
|
580
|
+
),
|
|
581
|
+
)
|
|
582
|
+
ttft_timeout_s: float | None = Field(
|
|
583
|
+
default=None,
|
|
584
|
+
gt=0.0,
|
|
585
|
+
le=3600.0,
|
|
586
|
+
description=(
|
|
587
|
+
"v2.17.0: seconds to wait for the first real streamed output "
|
|
588
|
+
"before falling back to the next provider (not applied to the "
|
|
589
|
+
"last provider in a chain). None disables."
|
|
590
|
+
),
|
|
591
|
+
)
|
|
553
592
|
|
|
554
593
|
# v2.6 language-tax track: path to a LOCAL ``tokenizer.json`` for this
|
|
555
594
|
# provider's model, used to measure the CJK over-count vs the char/4
|
|
@@ -1617,6 +1656,21 @@ class RuleMatcher(BaseModel):
|
|
|
1617
1656
|
# declares any tools); ``False`` is rejected by ``_exactly_one``
|
|
1618
1657
|
# since a "no-tools" rule would shadow the default fall-through.
|
|
1619
1658
|
has_tools: bool | None = None
|
|
1659
|
+
# v2.18.0: Claude Code gateway hint headers. Claude Code 2.1.273+ sends
|
|
1660
|
+
# these to a custom ANTHROPIC_BASE_URL when
|
|
1661
|
+
# ``CLAUDE_CODE_GATEWAY_HINT_HEADERS=1`` is set (``coderouter
|
|
1662
|
+
# vscode-init`` sets it). They say what a request *is* — something the
|
|
1663
|
+
# body heuristics above can only guess:
|
|
1664
|
+
# * ``request_class`` — ``x-claude-code-request-class``: ``main`` /
|
|
1665
|
+
# ``subagent`` / ``workflow`` / ``compaction`` / ``auxiliary``
|
|
1666
|
+
# (auto-mode classifier, titles, …). Case-insensitive exact match.
|
|
1667
|
+
# * ``agent_type`` — ``x-claude-code-agent-type`` (e.g. ``Explore``),
|
|
1668
|
+
# case-insensitive exact match.
|
|
1669
|
+
# * ``compaction: True`` — ``x-claude-code-compaction`` is truthy.
|
|
1670
|
+
# Without the headers these matchers simply never match.
|
|
1671
|
+
request_class: str | None = None
|
|
1672
|
+
agent_type: str | None = None
|
|
1673
|
+
compaction: bool | None = None
|
|
1620
1674
|
|
|
1621
1675
|
_MATCHER_FIELDS: tuple[str, ...] = (
|
|
1622
1676
|
"has_image",
|
|
@@ -1627,12 +1681,15 @@ class RuleMatcher(BaseModel):
|
|
|
1627
1681
|
"content_token_count_min",
|
|
1628
1682
|
"has_tools",
|
|
1629
1683
|
"cjk_ratio_min",
|
|
1684
|
+
"request_class",
|
|
1685
|
+
"agent_type",
|
|
1686
|
+
"compaction",
|
|
1630
1687
|
)
|
|
1631
1688
|
|
|
1632
1689
|
# Boolean matcher fields carry meaning only at ``True`` — the runtime
|
|
1633
1690
|
# evaluator uses ``is True`` — so a ``False`` value is a dead rule that
|
|
1634
1691
|
# never matches. ``_exactly_one`` rejects it explicitly (see below).
|
|
1635
|
-
_BOOL_MATCHER_FIELDS: tuple[str, ...] = ("has_image", "has_tools")
|
|
1692
|
+
_BOOL_MATCHER_FIELDS: tuple[str, ...] = ("has_image", "has_tools", "compaction")
|
|
1636
1693
|
|
|
1637
1694
|
@model_validator(mode="after")
|
|
1638
1695
|
def _exactly_one(self) -> Self:
|
coderouter/doctor_claude_code.py
CHANGED
|
@@ -125,6 +125,18 @@ def check(version: Version | None, env: Mapping[str, str]) -> list[ClaudeCodeFin
|
|
|
125
125
|
)
|
|
126
126
|
)
|
|
127
127
|
|
|
128
|
+
if env.get("CLAUDE_CODE_GATEWAY_HINT_HEADERS", "").lower() in ("1", "true"):
|
|
129
|
+
out.append(ClaudeCodeFinding("ok", "CLAUDE_CODE_GATEWAY_HINT_HEADERS=1"))
|
|
130
|
+
else:
|
|
131
|
+
out.append(
|
|
132
|
+
ClaudeCodeFinding(
|
|
133
|
+
"info",
|
|
134
|
+
"CLAUDE_CODE_GATEWAY_HINT_HEADERS is not set: auto_router rules "
|
|
135
|
+
"on request_class / agent_type / compaction (v2.18.0) never "
|
|
136
|
+
"match without it.",
|
|
137
|
+
)
|
|
138
|
+
)
|
|
139
|
+
|
|
128
140
|
if env.get("CLAUDE_CODE_AUTO_MODE_SERVER") == "0":
|
|
129
141
|
out.append(ClaudeCodeFinding("ok", "CLAUDE_CODE_AUTO_MODE_SERVER=0"))
|
|
130
142
|
else:
|
|
@@ -33,6 +33,13 @@ terminal error 429 429 ``rate_limit_error``
|
|
|
33
33
|
anything else 502 ``api_error``
|
|
34
34
|
==================================== ====== ========================
|
|
35
35
|
|
|
36
|
+
v2.17.0: every mapped response also carries ``x-should-retry`` — the header
|
|
37
|
+
Claude Code reads (with ``retry-after``) to decide whether to spend its retry
|
|
38
|
+
budget. ``false`` for the 4xx rows (resending the same request cannot
|
|
39
|
+
help), ``true`` for 429, and for 502 ``true`` only when at least one
|
|
40
|
+
upstream failure was retryable — a chain that failed purely on, say, 401s
|
|
41
|
+
will fail the same way on every retry.
|
|
42
|
+
|
|
36
43
|
Upstream 401 / 403 deliberately fall into the last row: they describe
|
|
37
44
|
CodeRouter's *own* upstream credentials, not the client's, and handing a
|
|
38
45
|
401 to Claude Code would send the user to ``/login`` for nothing.
|
|
@@ -164,17 +171,26 @@ def map_no_providers_error(exc: NoProvidersAvailableError) -> AnthropicErrorResp
|
|
|
164
171
|
msg = upstream_message(err)
|
|
165
172
|
if not msg.lower().startswith(PROMPT_TOO_LONG_PREFIX):
|
|
166
173
|
msg = f"{PROMPT_TOO_LONG_PREFIX}: {msg}"
|
|
167
|
-
return AnthropicErrorResponse(
|
|
174
|
+
return AnthropicErrorResponse(
|
|
175
|
+
400, "invalid_request_error", msg, {"x-should-retry": "false"}
|
|
176
|
+
)
|
|
168
177
|
|
|
169
178
|
terminal = errors[-1] if errors else None
|
|
170
179
|
if terminal is not None and terminal.status_code is not None:
|
|
171
180
|
status = terminal.status_code
|
|
172
181
|
if status in (400, 422) and not terminal.retryable:
|
|
173
|
-
return AnthropicErrorResponse(
|
|
182
|
+
return AnthropicErrorResponse(
|
|
183
|
+
400,
|
|
184
|
+
"invalid_request_error",
|
|
185
|
+
upstream_message(terminal),
|
|
186
|
+
{"x-should-retry": "false"},
|
|
187
|
+
)
|
|
174
188
|
if status == 413:
|
|
175
|
-
return AnthropicErrorResponse(
|
|
189
|
+
return AnthropicErrorResponse(
|
|
190
|
+
413, "request_too_large", upstream_message(terminal), {"x-should-retry": "false"}
|
|
191
|
+
)
|
|
176
192
|
if status == 429:
|
|
177
|
-
headers: dict[str, str] = {}
|
|
193
|
+
headers: dict[str, str] = {"x-should-retry": "true"}
|
|
178
194
|
retry_after = getattr(terminal, "retry_after", None)
|
|
179
195
|
if retry_after:
|
|
180
196
|
headers["retry-after"] = retry_after
|
|
@@ -182,4 +198,7 @@ def map_no_providers_error(exc: NoProvidersAvailableError) -> AnthropicErrorResp
|
|
|
182
198
|
429, "rate_limit_error", upstream_message(terminal), headers
|
|
183
199
|
)
|
|
184
200
|
|
|
185
|
-
|
|
201
|
+
retry = any(e.retryable for e in errors)
|
|
202
|
+
return AnthropicErrorResponse(
|
|
203
|
+
502, "api_error", redact(str(exc)), {"x-should-retry": "true" if retry else "false"}
|
|
204
|
+
)
|
|
@@ -31,6 +31,7 @@ from fastapi import APIRouter, Header, HTTPException, Request
|
|
|
31
31
|
from fastapi.responses import JSONResponse, Response, StreamingResponse
|
|
32
32
|
|
|
33
33
|
from coderouter.adapters.base import StreamTruncatedError
|
|
34
|
+
from coderouter.adapters.slot_affinity import affinity_key
|
|
34
35
|
from coderouter.guards.context_budget import ContextBudgetRejectError
|
|
35
36
|
from coderouter.guards.tool_loop import ToolCountExceededError, ToolLoopBreakError
|
|
36
37
|
from coderouter.ingress.anthropic_errors import (
|
|
@@ -49,7 +50,7 @@ from coderouter.routing import (
|
|
|
49
50
|
MidStreamError,
|
|
50
51
|
NoProvidersAvailableError,
|
|
51
52
|
)
|
|
52
|
-
from coderouter.routing.auto_router import RESERVED_PROFILE_NAME, classify
|
|
53
|
+
from coderouter.routing.auto_router import RESERVED_PROFILE_NAME, classify, extract_gateway_hints
|
|
53
54
|
from coderouter.routing.fallback_trace import (
|
|
54
55
|
SSE_FALLBACK_EVENT,
|
|
55
56
|
current_fallback_trace,
|
|
@@ -226,6 +227,11 @@ async def messages(
|
|
|
226
227
|
if anthropic_beta:
|
|
227
228
|
anth_req.anthropic_beta = anthropic_beta
|
|
228
229
|
|
|
230
|
+
# v2.18.0: Claude Code gateway hint headers — routing matchers below,
|
|
231
|
+
# and the conversation key for llama-server KV-slot affinity.
|
|
232
|
+
gateway_hints = extract_gateway_hints(request.headers)
|
|
233
|
+
anth_req.session_key = affinity_key(gateway_hints)
|
|
234
|
+
|
|
229
235
|
# Profile selection — body field wins over header (same policy as OpenAI route).
|
|
230
236
|
if anth_req.profile is None and x_coderouter_profile:
|
|
231
237
|
anth_req.profile = x_coderouter_profile
|
|
@@ -253,7 +259,7 @@ async def messages(
|
|
|
253
259
|
# AnthropicRequest), so both OpenAI and Anthropic ingress use the same
|
|
254
260
|
# classifier without a shared request shim.
|
|
255
261
|
if anth_req.profile is None and config.default_profile == RESERVED_PROFILE_NAME:
|
|
256
|
-
anth_req.profile = classify(payload, config)
|
|
262
|
+
anth_req.profile = classify(payload, config, gateway_hints)
|
|
257
263
|
|
|
258
264
|
if anth_req.profile is not None:
|
|
259
265
|
try:
|
|
@@ -276,7 +282,9 @@ async def messages(
|
|
|
276
282
|
)
|
|
277
283
|
if ts_action == "reject":
|
|
278
284
|
return _anthropic_error_response(
|
|
279
|
-
AnthropicErrorResponse(
|
|
285
|
+
AnthropicErrorResponse(
|
|
286
|
+
400, "invalid_request_error", REJECT_MESSAGE, {"x-should-retry": "false"}
|
|
287
|
+
)
|
|
280
288
|
)
|
|
281
289
|
if ts_action == "expand":
|
|
282
290
|
anth_req = expand_tool_search(anth_req)
|
|
@@ -290,7 +298,9 @@ async def messages(
|
|
|
290
298
|
# v2.16.0: ``context_budget_action: reject`` — Anthropic's own
|
|
291
299
|
# overflow answer, so Claude Code compacts the conversation.
|
|
292
300
|
return _anthropic_error_response(
|
|
293
|
-
AnthropicErrorResponse(
|
|
301
|
+
AnthropicErrorResponse(
|
|
302
|
+
400, "invalid_request_error", str(exc), {"x-should-retry": "false"}
|
|
303
|
+
)
|
|
294
304
|
)
|
|
295
305
|
|
|
296
306
|
if anth_req.stream:
|
|
@@ -362,7 +372,9 @@ async def messages(
|
|
|
362
372
|
# swap) — same answer as the pre-dispatch check above.
|
|
363
373
|
await _aclose_quietly(source)
|
|
364
374
|
return _anthropic_error_response(
|
|
365
|
-
AnthropicErrorResponse(
|
|
375
|
+
AnthropicErrorResponse(
|
|
376
|
+
400, "invalid_request_error", str(exc), {"x-should-retry": "false"}
|
|
377
|
+
)
|
|
366
378
|
)
|
|
367
379
|
except (ToolLoopBreakError, ToolCountExceededError, MidStreamError) as exc:
|
|
368
380
|
# Policy errors and mid-stream failures keep their existing
|
|
@@ -415,7 +427,9 @@ async def messages(
|
|
|
415
427
|
except ContextBudgetRejectError as exc:
|
|
416
428
|
# See the streaming branch: the engine can re-run the guard.
|
|
417
429
|
return _anthropic_error_response(
|
|
418
|
-
AnthropicErrorResponse(
|
|
430
|
+
AnthropicErrorResponse(
|
|
431
|
+
400, "invalid_request_error", str(exc), {"x-should-retry": "false"}
|
|
432
|
+
)
|
|
419
433
|
)
|
|
420
434
|
except NoProvidersAvailableError as exc:
|
|
421
435
|
# v2.16.0: Anthropic-shaped error with the upstream's own wording
|
|
@@ -32,9 +32,10 @@ from fastapi import APIRouter, Header, HTTPException, Request
|
|
|
32
32
|
from fastapi.responses import JSONResponse, StreamingResponse
|
|
33
33
|
|
|
34
34
|
from coderouter.adapters.base import ChatRequest
|
|
35
|
+
from coderouter.adapters.slot_affinity import affinity_key
|
|
35
36
|
from coderouter.logging import get_logger
|
|
36
37
|
from coderouter.routing import FallbackEngine, NoProvidersAvailableError
|
|
37
|
-
from coderouter.routing.auto_router import RESERVED_PROFILE_NAME, classify
|
|
38
|
+
from coderouter.routing.auto_router import RESERVED_PROFILE_NAME, classify, extract_gateway_hints
|
|
38
39
|
from coderouter.routing.fallback_trace import current_fallback_trace
|
|
39
40
|
|
|
40
41
|
router = APIRouter()
|
|
@@ -199,6 +200,10 @@ async def chat_completions(
|
|
|
199
200
|
except Exception as exc: # pydantic.ValidationError, etc.
|
|
200
201
|
raise HTTPException(status_code=422, detail=str(exc)) from exc
|
|
201
202
|
|
|
203
|
+
# v2.18.0: gateway hint headers (routing matchers + KV-slot affinity key).
|
|
204
|
+
gateway_hints = extract_gateway_hints(request.headers)
|
|
205
|
+
chat_req.session_key = affinity_key(gateway_hints)
|
|
206
|
+
|
|
202
207
|
# Header-based override (body wins if both are set — see module docstring)
|
|
203
208
|
if chat_req.profile is None and x_coderouter_profile:
|
|
204
209
|
chat_req.profile = x_coderouter_profile
|
|
@@ -226,7 +231,7 @@ async def chat_completions(
|
|
|
226
231
|
# When inactive, the engine still falls through to
|
|
227
232
|
# ``config.default_profile`` on its own — same semantics as pre-v1.6.
|
|
228
233
|
if chat_req.profile is None and config.default_profile == RESERVED_PROFILE_NAME:
|
|
229
|
-
chat_req.profile = classify(payload, config)
|
|
234
|
+
chat_req.profile = classify(payload, config, gateway_hints)
|
|
230
235
|
|
|
231
236
|
# Validate profile exists before we kick off any upstream call
|
|
232
237
|
if chat_req.profile is not None:
|
|
@@ -36,6 +36,7 @@ from __future__ import annotations
|
|
|
36
36
|
|
|
37
37
|
import logging
|
|
38
38
|
import re
|
|
39
|
+
from collections.abc import Mapping
|
|
39
40
|
from typing import TYPE_CHECKING, Any
|
|
40
41
|
|
|
41
42
|
from coderouter.config.schemas import AutoRouterConfig, AutoRouteRule, RuleMatcher
|
|
@@ -169,6 +170,32 @@ def _has_tools_in_body(body: dict[str, Any]) -> bool:
|
|
|
169
170
|
return isinstance(functions, list) and len(functions) > 0
|
|
170
171
|
|
|
171
172
|
|
|
173
|
+
# v2.18.0: Claude Code gateway hint headers → hint keys.
|
|
174
|
+
_HINT_HEADERS: dict[str, str] = {
|
|
175
|
+
"x-claude-code-request-class": "request_class",
|
|
176
|
+
"x-claude-code-agent-type": "agent_type",
|
|
177
|
+
"x-claude-code-compaction": "compaction",
|
|
178
|
+
"x-claude-code-session-id": "session_id",
|
|
179
|
+
"x-claude-code-prompt-id": "prompt_id",
|
|
180
|
+
}
|
|
181
|
+
_TRUTHY = frozenset({"1", "true", "yes", "on"})
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
def extract_gateway_hints(headers: Mapping[str, str]) -> dict[str, str]:
|
|
185
|
+
"""Pull Claude Code's gateway hint headers out of a request.
|
|
186
|
+
|
|
187
|
+
Returns only the hints that are present (lower-cased keys from
|
|
188
|
+
:data:`_HINT_HEADERS`). Header lookup is case-insensitive whenever the
|
|
189
|
+
mapping is (Starlette's ``Headers`` is).
|
|
190
|
+
"""
|
|
191
|
+
out: dict[str, str] = {}
|
|
192
|
+
for header, key in _HINT_HEADERS.items():
|
|
193
|
+
value = headers.get(header)
|
|
194
|
+
if value:
|
|
195
|
+
out[key] = value.strip()
|
|
196
|
+
return out
|
|
197
|
+
|
|
198
|
+
|
|
172
199
|
def _match_rule(
|
|
173
200
|
rule: AutoRouteRule,
|
|
174
201
|
message: dict[str, Any] | None,
|
|
@@ -176,8 +203,17 @@ def _match_rule(
|
|
|
176
203
|
model: str | None,
|
|
177
204
|
estimated_tokens: int,
|
|
178
205
|
has_tools: bool,
|
|
206
|
+
hints: Mapping[str, str] | None = None,
|
|
179
207
|
) -> bool:
|
|
180
208
|
m = rule.match
|
|
209
|
+
if m.request_class is not None:
|
|
210
|
+
value = (hints or {}).get("request_class")
|
|
211
|
+
return value is not None and value.lower() == m.request_class.lower()
|
|
212
|
+
if m.agent_type is not None:
|
|
213
|
+
value = (hints or {}).get("agent_type")
|
|
214
|
+
return value is not None and value.lower() == m.agent_type.lower()
|
|
215
|
+
if m.compaction is True:
|
|
216
|
+
return (hints or {}).get("compaction", "").lower() in _TRUTHY
|
|
181
217
|
if m.has_image is True:
|
|
182
218
|
return message is not None and _has_image(message)
|
|
183
219
|
if m.code_fence_ratio_min is not None:
|
|
@@ -237,7 +273,11 @@ def _extract_model(body: dict[str, Any]) -> str | None:
|
|
|
237
273
|
return None
|
|
238
274
|
|
|
239
275
|
|
|
240
|
-
def classify(
|
|
276
|
+
def classify(
|
|
277
|
+
body: dict[str, Any],
|
|
278
|
+
config: CodeRouterConfig,
|
|
279
|
+
hints: Mapping[str, str] | None = None,
|
|
280
|
+
) -> str:
|
|
241
281
|
"""Resolve an incoming request body to a profile name.
|
|
242
282
|
|
|
243
283
|
Rule order (first match wins) is determined by:
|
|
@@ -252,6 +292,10 @@ def classify(body: dict[str, Any], config: CodeRouterConfig) -> str:
|
|
|
252
292
|
|
|
253
293
|
Emits one of two log events: ``auto-router-resolved`` on match, or
|
|
254
294
|
``auto-router-fallthrough`` on default-rule fall.
|
|
295
|
+
|
|
296
|
+
v2.18.0: ``hints`` carries Claude Code's gateway hint headers (see
|
|
297
|
+
:func:`extract_gateway_hints`) for the ``request_class`` /
|
|
298
|
+
``agent_type`` / ``compaction`` matchers.
|
|
255
299
|
"""
|
|
256
300
|
user_msg = _latest_user_message(body)
|
|
257
301
|
text = _extract_text(user_msg) if user_msg is not None else ""
|
|
@@ -294,7 +338,7 @@ def classify(body: dict[str, Any], config: CodeRouterConfig) -> str:
|
|
|
294
338
|
# present — they short out via ``_match_rule``'s message-None
|
|
295
339
|
# handling.
|
|
296
340
|
for rule in rules:
|
|
297
|
-
if _match_rule(rule, user_msg, text, model, estimated_tokens, has_tools):
|
|
341
|
+
if _match_rule(rule, user_msg, text, model, estimated_tokens, has_tools, hints):
|
|
298
342
|
_emit_resolved(rule, user_msg, text, model, estimated_tokens, has_tools)
|
|
299
343
|
return rule.profile
|
|
300
344
|
|
coderouter/routing/capability.py
CHANGED
|
@@ -194,12 +194,13 @@ def strip_thinking(request: AnthropicRequest) -> AnthropicRequest:
|
|
|
194
194
|
|
|
195
195
|
# model_dump() serializes extras; roundtripping via validate rebuilds a
|
|
196
196
|
# clean instance without the dropped key. exclude=True fields (profile,
|
|
197
|
-
# anthropic_beta) are omitted by model_dump, so we reassign them.
|
|
197
|
+
# anthropic_beta, session_key) are omitted by model_dump, so we reassign them.
|
|
198
198
|
dumped = request.model_dump()
|
|
199
199
|
dumped.pop("thinking", None)
|
|
200
200
|
stripped = AnthropicRequest.model_validate(dumped)
|
|
201
201
|
stripped.profile = request.profile
|
|
202
202
|
stripped.anthropic_beta = request.anthropic_beta
|
|
203
|
+
stripped.session_key = request.session_key
|
|
203
204
|
return stripped
|
|
204
205
|
|
|
205
206
|
|
|
@@ -450,6 +451,7 @@ def emulate_tool_choice(request: AnthropicRequest) -> AnthropicRequest:
|
|
|
450
451
|
emulated = AnthropicRequest.model_validate(dumped)
|
|
451
452
|
emulated.profile = request.profile
|
|
452
453
|
emulated.anthropic_beta = request.anthropic_beta
|
|
454
|
+
emulated.session_key = request.session_key
|
|
453
455
|
return emulated
|
|
454
456
|
|
|
455
457
|
|
|
@@ -522,6 +524,7 @@ def strip_cache_control(request: AnthropicRequest) -> tuple[AnthropicRequest, in
|
|
|
522
524
|
stripped = AnthropicRequest.model_validate(dumped)
|
|
523
525
|
stripped.profile = request.profile
|
|
524
526
|
stripped.anthropic_beta = request.anthropic_beta
|
|
527
|
+
stripped.session_key = request.session_key
|
|
525
528
|
return stripped, removed
|
|
526
529
|
|
|
527
530
|
|
coderouter/routing/fallback.py
CHANGED
|
@@ -47,6 +47,7 @@ from coderouter.adapters.base import (
|
|
|
47
47
|
ProviderCallOverrides,
|
|
48
48
|
StreamChunk,
|
|
49
49
|
StreamTruncatedError,
|
|
50
|
+
TTFTTimeoutError,
|
|
50
51
|
)
|
|
51
52
|
from coderouter.adapters.registry import build_adapter
|
|
52
53
|
from coderouter.config.schemas import CodeRouterConfig, ProviderConfig
|
|
@@ -111,6 +112,7 @@ from coderouter.routing.fallback_trace import (
|
|
|
111
112
|
REASON_PAID_GATE,
|
|
112
113
|
REASON_SELF_HEALING_EXCLUDED,
|
|
113
114
|
REASON_STREAM_TRUNCATED,
|
|
115
|
+
REASON_TTFT_TIMEOUT,
|
|
114
116
|
REASON_UNKNOWN_PROVIDER,
|
|
115
117
|
FallbackTrace,
|
|
116
118
|
begin_fallback_trace,
|
|
@@ -898,6 +900,38 @@ def _emit_cache_observed_streaming(
|
|
|
898
900
|
)
|
|
899
901
|
|
|
900
902
|
|
|
903
|
+
async def _await_within_ttft(
|
|
904
|
+
awaitable: Any,
|
|
905
|
+
*,
|
|
906
|
+
deadline: float | None,
|
|
907
|
+
provider: str,
|
|
908
|
+
ttft_timeout_s: float | None,
|
|
909
|
+
) -> Any:
|
|
910
|
+
"""v2.17.0: await ``awaitable`` but give up at the TTFT ``deadline``.
|
|
911
|
+
|
|
912
|
+
``deadline`` is a ``time.monotonic()`` instant (``None`` = no limit). On
|
|
913
|
+
expiry raises :class:`TTFTTimeoutError` — a retryable ``AdapterError``, so
|
|
914
|
+
every existing pre-content fallback branch handles it unchanged. The
|
|
915
|
+
cancelled await unwinds the adapter's generator, which releases its
|
|
916
|
+
upstream connection.
|
|
917
|
+
"""
|
|
918
|
+
if deadline is None:
|
|
919
|
+
return await awaitable
|
|
920
|
+
remaining = deadline - time.monotonic()
|
|
921
|
+
if remaining <= 0:
|
|
922
|
+
close = getattr(awaitable, "close", None)
|
|
923
|
+
if close is not None:
|
|
924
|
+
close()
|
|
925
|
+
raise TTFTTimeoutError(provider=provider, ttft_timeout_s=ttft_timeout_s or 0.0)
|
|
926
|
+
try:
|
|
927
|
+
async with asyncio.timeout(remaining):
|
|
928
|
+
return await awaitable
|
|
929
|
+
except TimeoutError as exc:
|
|
930
|
+
raise TTFTTimeoutError(
|
|
931
|
+
provider=provider, ttft_timeout_s=ttft_timeout_s or 0.0
|
|
932
|
+
) from exc
|
|
933
|
+
|
|
934
|
+
|
|
901
935
|
class NoProvidersAvailableError(CodeRouterError):
|
|
902
936
|
"""Raised when every provider in the chain has failed (or was filtered out)."""
|
|
903
937
|
|
|
@@ -3354,7 +3388,7 @@ class FallbackEngine:
|
|
|
3354
3388
|
# keeps the default byte-for-byte identical to v2.14.0.
|
|
3355
3389
|
last_empty_stream_truncated = False
|
|
3356
3390
|
|
|
3357
|
-
for adapter, will_degrade in chain:
|
|
3391
|
+
for chain_idx, (adapter, will_degrade) in enumerate(chain):
|
|
3358
3392
|
is_native = isinstance(adapter, AnthropicAdapter)
|
|
3359
3393
|
downgrading = (not is_native) and bool(request.tools)
|
|
3360
3394
|
effective_request = request
|
|
@@ -3414,6 +3448,14 @@ class FallbackEngine:
|
|
|
3414
3448
|
# only the non-streaming generate_anthropic recorded, so the
|
|
3415
3449
|
# busiest path produced no adaptive signal.
|
|
3416
3450
|
attempt_started = time.monotonic()
|
|
3451
|
+
# v2.17.0: TTFT cut-off. Never on the last provider — there is
|
|
3452
|
+
# nothing left to fall back to, so waiting is the better answer.
|
|
3453
|
+
ttft_s = getattr(adapter.config, "ttft_timeout_s", None)
|
|
3454
|
+
ttft_deadline = (
|
|
3455
|
+
attempt_started + ttft_s
|
|
3456
|
+
if ttft_s is not None and chain_idx < len(chain) - 1
|
|
3457
|
+
else None
|
|
3458
|
+
)
|
|
3417
3459
|
event_iter: AsyncIterator[AnthropicStreamEvent]
|
|
3418
3460
|
first: AnthropicStreamEvent
|
|
3419
3461
|
try:
|
|
@@ -3422,11 +3464,21 @@ class FallbackEngine:
|
|
|
3422
3464
|
# mypy narrows for stream_anthropic (not on BaseAdapter).
|
|
3423
3465
|
if isinstance(adapter, AnthropicAdapter):
|
|
3424
3466
|
event_iter = adapter.stream_anthropic(effective_request, overrides=overrides)
|
|
3425
|
-
first = await
|
|
3467
|
+
first = await _await_within_ttft(
|
|
3468
|
+
anext(event_iter),
|
|
3469
|
+
deadline=ttft_deadline,
|
|
3470
|
+
provider=adapter.name,
|
|
3471
|
+
ttft_timeout_s=ttft_s,
|
|
3472
|
+
)
|
|
3426
3473
|
elif downgrading:
|
|
3427
3474
|
# v0.3-D downgrade: run non-streaming, repair, replay.
|
|
3428
3475
|
chat_req = to_chat_request(effective_request)
|
|
3429
3476
|
chat_req.stream = False
|
|
3477
|
+
# v2.17.0: no TTFT deadline here. This branch is a
|
|
3478
|
+
# *non-streaming* call (the tool-turn downgrade), so its
|
|
3479
|
+
# "first output" is the whole answer — a TTFT cut would
|
|
3480
|
+
# become a total-generation limit and throw away slow
|
|
3481
|
+
# but healthy local replies. ``timeout_s`` bounds it.
|
|
3430
3482
|
chat_resp = await adapter.generate(chat_req, overrides=overrides)
|
|
3431
3483
|
# H-4: same as the non-streaming branch — keep the
|
|
3432
3484
|
# CPU-bound repair scan off the event loop.
|
|
@@ -3441,7 +3493,12 @@ class FallbackEngine:
|
|
|
3441
3493
|
event_iter = stream_chat_to_anthropic_events(
|
|
3442
3494
|
adapter.stream(chat_req, overrides=overrides)
|
|
3443
3495
|
)
|
|
3444
|
-
first = await
|
|
3496
|
+
first = await _await_within_ttft(
|
|
3497
|
+
anext(event_iter),
|
|
3498
|
+
deadline=ttft_deadline,
|
|
3499
|
+
provider=adapter.name,
|
|
3500
|
+
ttft_timeout_s=ttft_s,
|
|
3501
|
+
)
|
|
3445
3502
|
except StopAsyncIteration:
|
|
3446
3503
|
# M2: record the empty-stream failure.
|
|
3447
3504
|
self._adaptive.record_attempt(
|
|
@@ -3557,7 +3614,18 @@ class FallbackEngine:
|
|
|
3557
3614
|
yield buffered_ev
|
|
3558
3615
|
buffer = []
|
|
3559
3616
|
try:
|
|
3560
|
-
|
|
3617
|
+
if content_started:
|
|
3618
|
+
pending = await iterator.__anext__()
|
|
3619
|
+
else:
|
|
3620
|
+
# v2.17.0: the TTFT deadline also covers the
|
|
3621
|
+
# withheld preamble — first *real* output is
|
|
3622
|
+
# what counts, not the first event.
|
|
3623
|
+
pending = await _await_within_ttft(
|
|
3624
|
+
iterator.__anext__(),
|
|
3625
|
+
deadline=ttft_deadline,
|
|
3626
|
+
provider=adapter.name,
|
|
3627
|
+
ttft_timeout_s=ttft_s,
|
|
3628
|
+
)
|
|
3561
3629
|
except StopAsyncIteration:
|
|
3562
3630
|
break
|
|
3563
3631
|
except AdapterError as exc:
|
|
@@ -3609,8 +3677,28 @@ class FallbackEngine:
|
|
|
3609
3677
|
# message — so the hop is recorded as ``stream-truncated``
|
|
3610
3678
|
# and the empty-response log is skipped. The adapter has
|
|
3611
3679
|
# already emitted ``stream-truncation-detected``.
|
|
3612
|
-
|
|
3613
|
-
|
|
3680
|
+
#
|
|
3681
|
+
# v2.17.0: a TTFT cut-off lands here too, and its buffer
|
|
3682
|
+
# is just as unterminated (opening events, no
|
|
3683
|
+
# ``message_stop``). Treat it like a truncation so the
|
|
3684
|
+
# chain-exhausted flush below never replays it as a
|
|
3685
|
+
# "complete empty answer" — that would hand the client a
|
|
3686
|
+
# 200 stream that never ends. Close the abandoned upstream
|
|
3687
|
+
# stream now rather than waiting for GC.
|
|
3688
|
+
ttft_cut = isinstance(exc, TTFTTimeoutError)
|
|
3689
|
+
truncated = isinstance(exc, StreamTruncatedError) or ttft_cut
|
|
3690
|
+
if ttft_cut:
|
|
3691
|
+
aclose_iter = getattr(event_iter, "aclose", None)
|
|
3692
|
+
if aclose_iter is not None:
|
|
3693
|
+
with contextlib.suppress(Exception):
|
|
3694
|
+
await aclose_iter()
|
|
3695
|
+
trace.record_failure(
|
|
3696
|
+
adapter.name,
|
|
3697
|
+
REASON_TTFT_TIMEOUT,
|
|
3698
|
+
detail=describe_adapter_error(exc),
|
|
3699
|
+
stream=True,
|
|
3700
|
+
)
|
|
3701
|
+
elif truncated:
|
|
3614
3702
|
trace.record_failure(
|
|
3615
3703
|
adapter.name,
|
|
3616
3704
|
REASON_STREAM_TRUNCATED,
|
|
@@ -111,6 +111,8 @@ REASON_EMPTY_STREAM: Final = "empty-stream"
|
|
|
111
111
|
# ``connection`` (the transport itself broke). Produced only when the profile
|
|
112
112
|
# sets ``stream_truncation_action: error``.
|
|
113
113
|
REASON_STREAM_TRUNCATED: Final = "stream-truncated"
|
|
114
|
+
# v2.17.0: no first real output within the provider's ``ttft_timeout_s``.
|
|
115
|
+
REASON_TTFT_TIMEOUT: Final = "ttft-timeout"
|
|
114
116
|
|
|
115
117
|
# -- degrade reasons (pre-existing strings; never produce a hop) -----------
|
|
116
118
|
# Kept here so the whole ``reason`` vocabulary is readable in one place.
|
|
@@ -141,6 +143,7 @@ ATTEMPT_FAILURE_REASONS: Final[frozenset[str]] = frozenset(
|
|
|
141
143
|
REASON_EMPTY_RESPONSE,
|
|
142
144
|
REASON_EMPTY_STREAM,
|
|
143
145
|
REASON_STREAM_TRUNCATED,
|
|
146
|
+
REASON_TTFT_TIMEOUT,
|
|
144
147
|
}
|
|
145
148
|
)
|
|
146
149
|
|
|
@@ -207,6 +210,8 @@ def describe_adapter_error(exc: AdapterError) -> str:
|
|
|
207
210
|
# Not a transport fault — the HTTP body ended cleanly; it was the
|
|
208
211
|
# LLM protocol inside it that never closed.
|
|
209
212
|
return "no-terminator"
|
|
213
|
+
if reason == REASON_TTFT_TIMEOUT:
|
|
214
|
+
return "no-first-token"
|
|
210
215
|
return f"transport={reason}"
|
|
211
216
|
|
|
212
217
|
|
|
@@ -227,10 +232,12 @@ def classify_adapter_error(exc: AdapterError) -> str:
|
|
|
227
232
|
``adapters.base`` is a TYPE_CHECKING-only import at module scope here
|
|
228
233
|
(the runtime import order would otherwise cycle through the registry).
|
|
229
234
|
"""
|
|
230
|
-
from coderouter.adapters.base import StreamTruncatedError
|
|
235
|
+
from coderouter.adapters.base import StreamTruncatedError, TTFTTimeoutError
|
|
231
236
|
|
|
232
237
|
if isinstance(exc, StreamTruncatedError):
|
|
233
238
|
return REASON_STREAM_TRUNCATED
|
|
239
|
+
if isinstance(exc, TTFTTimeoutError):
|
|
240
|
+
return REASON_TTFT_TIMEOUT
|
|
234
241
|
status = exc.status_code
|
|
235
242
|
if status is None:
|
|
236
243
|
message = str(exc).lower()
|
|
@@ -277,6 +277,9 @@ class AnthropicRequest(BaseModel):
|
|
|
277
277
|
# beta-gated body fields like `context_management`, `cache_control`,
|
|
278
278
|
# `thinking` beyond what the default minor version accepts.
|
|
279
279
|
anthropic_beta: str | None = Field(default=None, exclude=True)
|
|
280
|
+
# v2.18.0: conversation key for KV-slot affinity (Claude Code's
|
|
281
|
+
# ``x-claude-code-session-id``). Never serialized.
|
|
282
|
+
session_key: str | None = Field(default=None, exclude=True)
|
|
280
283
|
|
|
281
284
|
@model_validator(mode="before")
|
|
282
285
|
@classmethod
|
|
@@ -285,6 +285,8 @@ def to_chat_request(req: AnthropicRequest) -> ChatRequest:
|
|
|
285
285
|
)
|
|
286
286
|
# Propagate CodeRouter routing hint.
|
|
287
287
|
chat_req.profile = req.profile
|
|
288
|
+
# v2.18.0: carry the KV-slot affinity key across the translation.
|
|
289
|
+
chat_req.session_key = req.session_key
|
|
288
290
|
return chat_req
|
|
289
291
|
|
|
290
292
|
|
|
@@ -1114,6 +1116,8 @@ def to_anthropic_request(chat_req: ChatRequest) -> AnthropicRequest:
|
|
|
1114
1116
|
)
|
|
1115
1117
|
# Propagate CodeRouter routing hint.
|
|
1116
1118
|
req.profile = chat_req.profile
|
|
1119
|
+
# v2.18.0: KV-slot affinity key (exclude=True, so set by hand).
|
|
1120
|
+
req.session_key = chat_req.session_key
|
|
1117
1121
|
return req
|
|
1118
1122
|
|
|
1119
1123
|
|
coderouter/vscode_init.py
CHANGED
|
@@ -127,16 +127,25 @@ _MANAGED_ENV_KEYS: tuple[str, ...] = (
|
|
|
127
127
|
# is given. Since 2.1.285 Claude Code assumes a 1M window behind a
|
|
128
128
|
# gateway for Opus 4.7+ / Sonnet 5+ / Fable ids, so auto-compaction would
|
|
129
129
|
# wait far past what a local model holds.
|
|
130
|
+
# * ``CLAUDE_CODE_GATEWAY_HINT_HEADERS=1`` (v2.18.0) — makes Claude Code send
|
|
131
|
+
# ``x-claude-code-request-class`` / ``-agent-type`` / ``-compaction`` …, which
|
|
132
|
+
# the auto router's ``request_class`` / ``agent_type`` / ``compaction``
|
|
133
|
+
# matchers read. Harmless when no rule uses them.
|
|
130
134
|
CLAUDE_CODE_ENV_KEYS: tuple[str, ...] = (
|
|
131
135
|
"ENABLE_TOOL_SEARCH",
|
|
132
136
|
"CLAUDE_CODE_AUTO_MODE_SERVER",
|
|
137
|
+
"CLAUDE_CODE_GATEWAY_HINT_HEADERS",
|
|
133
138
|
"CLAUDE_CODE_MAX_CONTEXT_TOKENS",
|
|
134
139
|
)
|
|
135
140
|
|
|
136
141
|
|
|
137
142
|
def claude_code_env(*, max_context_tokens: int | None = None) -> dict[str, str]:
|
|
138
143
|
"""v2.16.0: the Claude Code env vars ``vscode-init`` writes by default."""
|
|
139
|
-
env = {
|
|
144
|
+
env = {
|
|
145
|
+
"ENABLE_TOOL_SEARCH": "false",
|
|
146
|
+
"CLAUDE_CODE_AUTO_MODE_SERVER": "0",
|
|
147
|
+
"CLAUDE_CODE_GATEWAY_HINT_HEADERS": "1",
|
|
148
|
+
}
|
|
140
149
|
if max_context_tokens is not None:
|
|
141
150
|
if max_context_tokens <= 0:
|
|
142
151
|
raise ValueError("max_context_tokens must be positive")
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: coderouter-cli
|
|
3
|
-
Version: 2.
|
|
3
|
+
Version: 2.18.0
|
|
4
4
|
Summary: Local-first, free-first, fallback-built-in LLM router. Claude Code / OpenAI compatible.
|
|
5
5
|
Project-URL: Homepage, https://github.com/zephel01/CodeRouter
|
|
6
6
|
Project-URL: Repository, https://github.com/zephel01/CodeRouter
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
coderouter/__init__.py,sha256=ghdjPrLtnRzY8fyQ4CJZI1UJKADyNTLtA3G7se8H7Ns,696
|
|
2
2
|
coderouter/__main__.py,sha256=-LCgxJnvgUV240HjQKv7ly-mn2NuKHpC4nCpvTHjeSU,130
|
|
3
|
-
coderouter/cli.py,sha256
|
|
3
|
+
coderouter/cli.py,sha256=qqwMv5dmWU6bq6okgbIK6YuDHPkWYhv5Y3jAXSY44-U,44825
|
|
4
4
|
coderouter/cli_stats.py,sha256=CCjzc1G4hTRHZ2gG1XhxhDpUkJnnl3NXbcbp1T18jpg,29894
|
|
5
5
|
coderouter/cost.py,sha256=32h6uzb4nxh2eA5d2Hn3kD9yJbtis6CFDAbeIy5KRkM,7431
|
|
6
6
|
coderouter/credentials.py,sha256=R_PdrcA941_jJX_U8hxrjPDTCVVzLd4dsw2c00KwAwE,12991
|
|
7
7
|
coderouter/doctor.py,sha256=0EUqKaEw2GaqX6zGKjR-bVDdlmBznFIklSz3yIoQbE0,91969
|
|
8
8
|
coderouter/doctor_apply.py,sha256=tZT3bEd1G6t67YRhf1TGY4NlSvZuJJbnEqN-3AKy8Hc,29972
|
|
9
|
-
coderouter/doctor_claude_code.py,sha256=
|
|
9
|
+
coderouter/doctor_claude_code.py,sha256=DuG4abNQWigo0IlJQJWHYGJ0lsu2oL3cNek4DvAYPXA,7091
|
|
10
10
|
coderouter/env_security.py,sha256=FEBZnXfJ0xE39kmMMn39zk0W_DRRnmcB_REmP9f4xWo,14796
|
|
11
11
|
coderouter/errors.py,sha256=Xmq67lheyw8iv3Ox39jh2c4tvNI5RcUR4QkoxVDN6l4,1130
|
|
12
12
|
coderouter/gguf_introspect.py,sha256=Scu0JjUfLHj7T7TL0HQIm1AZcGYrXsjkaMMSN9HkTro,16121
|
|
@@ -21,17 +21,18 @@ coderouter/rollback.py,sha256=Bqxx1MALTDLYc0BtGMEMqRyiZIWFoAZJH_RBQw-M5ow,7675
|
|
|
21
21
|
coderouter/secret_redaction.py,sha256=Rg71gqKN6jnybZ6UvCvXIvnThI9uRBvmOqC2grSUSUk,23159
|
|
22
22
|
coderouter/token_estimation.py,sha256=7klQYA4FIg1vtjQGWAhwiBhiLwyz8KTetrLVXFJLGIc,16957
|
|
23
23
|
coderouter/token_estimation_accurate.py,sha256=GTfzrBVnvAGjeVzmzAeUdOYZvWZKLAxcxPpFiJGlzjk,4609
|
|
24
|
-
coderouter/vscode_init.py,sha256=
|
|
24
|
+
coderouter/vscode_init.py,sha256=eSkp2mOLhwCbv8EUdZ8Bkb3K2wMkjgHBCzVfZl7-MWo,36316
|
|
25
25
|
coderouter/adapters/__init__.py,sha256=7dIDSZ-FE_0iSqLSDc_lK1idRdLTKcM2hP9tCJipgPI,463
|
|
26
|
-
coderouter/adapters/anthropic_native.py,sha256=
|
|
27
|
-
coderouter/adapters/base.py,sha256=
|
|
28
|
-
coderouter/adapters/openai_compat.py,sha256
|
|
26
|
+
coderouter/adapters/anthropic_native.py,sha256=PXxgnERTm8bsCYrxpOaaJmPVq0FJVfocBjZ7HLNZA2c,30367
|
|
27
|
+
coderouter/adapters/base.py,sha256=83mFELqGq8i1E4uESTSjBgdPGSB1rj8FM9WRfNlIjPE,15484
|
|
28
|
+
coderouter/adapters/openai_compat.py,sha256=DKFIgZFpqzYyqsN49-MB1CA23CdV_1t19QHkvBhhlGI,24350
|
|
29
29
|
coderouter/adapters/registry.py,sha256=XNc1wmFv_oZlqKS0rA693TRDdbq1sZ-OIcofXOVOtq0,3680
|
|
30
|
+
coderouter/adapters/slot_affinity.py,sha256=iu4nqOFmwBoh7i3a1tR4jYgioq3Re2GH66Xr1XiDfvs,2067
|
|
30
31
|
coderouter/config/__init__.py,sha256=FODEn74fN-qZnt4INPSHswqhOlEgpL6-_onxsitSx8g,274
|
|
31
32
|
coderouter/config/capability_registry.py,sha256=RZgzHF54dXy0CCDocRCz2ee21qXGbbgTSKTkRI9iLL4,16936
|
|
32
33
|
coderouter/config/env_file.py,sha256=CoMK27fuAXm-NtoLzXb8yN2E-wDFjHQuFwiIlmgTBQw,10356
|
|
33
34
|
coderouter/config/loader.py,sha256=7FoJXWaO_vN9FAjUStJDwI4aAYcUQQgMJtq5BNy8Pnc,12419
|
|
34
|
-
coderouter/config/schemas.py,sha256=
|
|
35
|
+
coderouter/config/schemas.py,sha256=Xwkx8-6f6Ch0JMVnoUzBJLeD_ykIOOHt_p9KxRfx_EE,151645
|
|
35
36
|
coderouter/data/__init__.py,sha256=uNyfD9jaCvTWsBAWtaw1Fr25OSxzv3psGMfBjT1z0Cc,328
|
|
36
37
|
coderouter/data/model-capabilities.yaml,sha256=S9jt6SC6-3s2-icZ_n-a14iEMnc2yB1C2R6q-N_tZWQ,19309
|
|
37
38
|
coderouter/guards/__init__.py,sha256=5qliYBqygvVPneej7nx0uSjxDKsz7t8VzvrDgVBJlvU,1170
|
|
@@ -46,13 +47,13 @@ coderouter/guards/memory_pressure.py,sha256=9Xm-b7rv9Td5kPNmAFoL3sNmaOR5zT8NLdn6
|
|
|
46
47
|
coderouter/guards/self_healing.py,sha256=XeUlgFWGqMQ5E9YvvzR2CsHRJOAJEyDChqRN0xmZ0vo,16841
|
|
47
48
|
coderouter/guards/tool_loop.py,sha256=EzeMcmU7BLeTW2jsRVevU81l5rhWcn1oUr7EpzgXjVM,15209
|
|
48
49
|
coderouter/ingress/__init__.py,sha256=WQsCH2CGJCAhy0mS6GSEdeYZRkkQu2OHDsP4CJWTLug,155
|
|
49
|
-
coderouter/ingress/anthropic_errors.py,sha256=
|
|
50
|
-
coderouter/ingress/anthropic_routes.py,sha256=
|
|
50
|
+
coderouter/ingress/anthropic_errors.py,sha256=q8F4LmH57f8Jh2hoV4Mmnvf9sZH32uiXhQXPRBZPjCE,8122
|
|
51
|
+
coderouter/ingress/anthropic_routes.py,sha256=2ADG_JgvmZteTm4rJfjdM5FU4IA9Wdtd9gZXm2-vI8Y,40561
|
|
51
52
|
coderouter/ingress/app.py,sha256=ZOx7dSvMxaWS4CzXOOEwgzFuycAVgAeWKCN6lFNF-F0,22433
|
|
52
53
|
coderouter/ingress/dashboard_routes.py,sha256=nPW5_pJe27-hzqCs4sqVVn0BrgXB_7Wcd4qZ6cirvWo,26736
|
|
53
54
|
coderouter/ingress/launcher_routes.py,sha256=uP_Otp9JE0fydUt1YP-wYcnS8Kj5RPLNIFf88YpfkPM,141535
|
|
54
55
|
coderouter/ingress/metrics_routes.py,sha256=4s_f9CY8nVIdQBfXxyUMlN7r5_cXHOaAu7PvNbUuoxg,4609
|
|
55
|
-
coderouter/ingress/openai_routes.py,sha256=
|
|
56
|
+
coderouter/ingress/openai_routes.py,sha256=Nc_Vh8rjLtc88oNbXfU9fwm-XZs0tTHVg-D5GEAuh_I,14484
|
|
56
57
|
coderouter/ingress/tool_search.py,sha256=JYnJ8SUqPmBfZmsl41TdBQNnux0mwbidKdsr5qn15Hk,3753
|
|
57
58
|
coderouter/ingress/ui_auth.py,sha256=uCWB5IaMQRNFYdY39FQ5DDVhsoRQJoqimZNSAu_QK_Q,3742
|
|
58
59
|
coderouter/metrics/__init__.py,sha256=7Es351DPS7yLM0yVF_F0eesmiD83n7Zzhie44chht38,1465
|
|
@@ -64,11 +65,11 @@ coderouter/plugins/loader.py,sha256=KLnKgO6_GE7OJulwo292cTYPvxqWZlLL8S0k_O3ZDcg,
|
|
|
64
65
|
coderouter/plugins/registry.py,sha256=8UDmRpHnRdB-ES_xpprvrvbDlhIhonOvFFXobR6MGJA,3739
|
|
65
66
|
coderouter/routing/__init__.py,sha256=oLQXPvKfrwy0OiGYNXngM9G_OxmJVLShv-nqyDaqSOs,441
|
|
66
67
|
coderouter/routing/adaptive.py,sha256=G2o377twGSjbUh65wiIFx6klnpFGjsD_nI3oDvcBwhY,21257
|
|
67
|
-
coderouter/routing/auto_router.py,sha256=
|
|
68
|
+
coderouter/routing/auto_router.py,sha256=XkRTf0oT4zdTTeJagZdvnyW6hOdgtZ_arwb8NN1h01Y,14980
|
|
68
69
|
coderouter/routing/budget.py,sha256=PblmVKJGs_BwNa9uDHAA8hmZ4XIVKv38mHAeU0V3OMs,8451
|
|
69
|
-
coderouter/routing/capability.py,sha256=
|
|
70
|
-
coderouter/routing/fallback.py,sha256=
|
|
71
|
-
coderouter/routing/fallback_trace.py,sha256=
|
|
70
|
+
coderouter/routing/capability.py,sha256=_RM9I6qG_oQLN_ZN4n7Xd7__g-CtClX9cdhtKvuJUvA,28018
|
|
71
|
+
coderouter/routing/fallback.py,sha256=vx1Vrl_vy7-dLxkKvipfba_pbhQPbuhRVFjmuMMWyaI,176825
|
|
72
|
+
coderouter/routing/fallback_trace.py,sha256=LwUw56mhVIkP4Gu-fNvZmOjL8hXxY1ci6b1MuGjl5m0,20099
|
|
72
73
|
coderouter/state/__init__.py,sha256=XoGcPmmBQSiZWML2S0juSveQ78xfhtdeCliNnVyzu7E,1088
|
|
73
74
|
coderouter/state/audit_log.py,sha256=CEFNJht2WZ8mqesI8CzeuL63NfzFzAZ7wF88auyl6l0,11444
|
|
74
75
|
coderouter/state/replay.py,sha256=Z_YHKroTKZdrL8qObFxcoLOAQWWXZvXFdLfxzvBhEJg,11230
|
|
@@ -76,12 +77,12 @@ coderouter/state/request_log.py,sha256=JlEvkWHwGtoLesnqr0XJy9CyXQDX4ZRHFMTPJ_0Fe
|
|
|
76
77
|
coderouter/state/store.py,sha256=h-rsMJq8GILsOfCP94nI40cuHaj4Vqycsm9UNN77REI,7445
|
|
77
78
|
coderouter/state/suggest_rules.py,sha256=FvdhEvao5NvdKp9zs8AkcoFKHY4yqqXY2HekvSjpDFA,16670
|
|
78
79
|
coderouter/translation/__init__.py,sha256=PYXN7XVEwpG1uC8RLy6fvnGbzEZhhrEuUapH8IYOtG8,1788
|
|
79
|
-
coderouter/translation/anthropic.py,sha256=
|
|
80
|
-
coderouter/translation/convert.py,sha256=
|
|
80
|
+
coderouter/translation/anthropic.py,sha256=49OMMIKBRZqsWhl0VhlMck8R-W34vxaTgpIlKI0kvVg,13655
|
|
81
|
+
coderouter/translation/convert.py,sha256=9lkfk8ByfbgiVh2WP6Hwh8UorOtrTxZBknvu_dsHnnI,53261
|
|
81
82
|
coderouter/translation/tool_repair.py,sha256=1p8rmdyyscGX2g0DDOYitMnLv-nKfR1SLuLaRB4z7GM,64244
|
|
82
83
|
coderouter/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
83
|
-
coderouter_cli-2.
|
|
84
|
-
coderouter_cli-2.
|
|
85
|
-
coderouter_cli-2.
|
|
86
|
-
coderouter_cli-2.
|
|
87
|
-
coderouter_cli-2.
|
|
84
|
+
coderouter_cli-2.18.0.dist-info/METADATA,sha256=kaaVhGuXqVYMlOZ5Yjug5iLaiKj02s-Yd1ae5ySPcJc,18221
|
|
85
|
+
coderouter_cli-2.18.0.dist-info/WHEEL,sha256=W3fkpkm7-wf9vBI5Z-7s0eWkeM-spu78I8Neb98DeEg,87
|
|
86
|
+
coderouter_cli-2.18.0.dist-info/entry_points.txt,sha256=-dnLfD1YZ2WjH2zSdNCvlO65wYltM9bsHt9Fhg3yGss,51
|
|
87
|
+
coderouter_cli-2.18.0.dist-info/licenses/LICENSE,sha256=wkEzoR86jFw33jvfOHjULqmkGEfxTFMgMaJnpR8mPRw,1065
|
|
88
|
+
coderouter_cli-2.18.0.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|