coderouter-cli 2.17.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 +3 -0
- coderouter/adapters/openai_compat.py +6 -0
- coderouter/adapters/slot_affinity.py +54 -0
- coderouter/cli.py +2 -1
- coderouter/config/schemas.py +36 -1
- coderouter/doctor_claude_code.py +12 -0
- coderouter/ingress/anthropic_routes.py +8 -2
- coderouter/ingress/openai_routes.py +7 -2
- coderouter/routing/auto_router.py +46 -2
- coderouter/routing/capability.py +4 -1
- coderouter/translation/anthropic.py +3 -0
- coderouter/translation/convert.py +4 -0
- coderouter/vscode_init.py +10 -1
- {coderouter_cli-2.17.0.dist-info → coderouter_cli-2.18.0.dist-info}/METADATA +1 -1
- {coderouter_cli-2.17.0.dist-info → coderouter_cli-2.18.0.dist-info}/RECORD +19 -18
- {coderouter_cli-2.17.0.dist-info → coderouter_cli-2.18.0.dist-info}/WHEEL +0 -0
- {coderouter_cli-2.17.0.dist-info → coderouter_cli-2.18.0.dist-info}/entry_points.txt +0 -0
- {coderouter_cli-2.17.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):
|
|
@@ -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
|
@@ -562,6 +562,23 @@ class ProviderConfig(BaseModel):
|
|
|
562
562
|
# withholds those opening events from the client. ``None`` (default)
|
|
563
563
|
# disables it. Typical use: a local llama-server still loading a model
|
|
564
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
|
+
)
|
|
565
582
|
ttft_timeout_s: float | None = Field(
|
|
566
583
|
default=None,
|
|
567
584
|
gt=0.0,
|
|
@@ -1639,6 +1656,21 @@ class RuleMatcher(BaseModel):
|
|
|
1639
1656
|
# declares any tools); ``False`` is rejected by ``_exactly_one``
|
|
1640
1657
|
# since a "no-tools" rule would shadow the default fall-through.
|
|
1641
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
|
|
1642
1674
|
|
|
1643
1675
|
_MATCHER_FIELDS: tuple[str, ...] = (
|
|
1644
1676
|
"has_image",
|
|
@@ -1649,12 +1681,15 @@ class RuleMatcher(BaseModel):
|
|
|
1649
1681
|
"content_token_count_min",
|
|
1650
1682
|
"has_tools",
|
|
1651
1683
|
"cjk_ratio_min",
|
|
1684
|
+
"request_class",
|
|
1685
|
+
"agent_type",
|
|
1686
|
+
"compaction",
|
|
1652
1687
|
)
|
|
1653
1688
|
|
|
1654
1689
|
# Boolean matcher fields carry meaning only at ``True`` — the runtime
|
|
1655
1690
|
# evaluator uses ``is True`` — so a ``False`` value is a dead rule that
|
|
1656
1691
|
# never matches. ``_exactly_one`` rejects it explicitly (see below).
|
|
1657
|
-
_BOOL_MATCHER_FIELDS: tuple[str, ...] = ("has_image", "has_tools")
|
|
1692
|
+
_BOOL_MATCHER_FIELDS: tuple[str, ...] = ("has_image", "has_tools", "compaction")
|
|
1658
1693
|
|
|
1659
1694
|
@model_validator(mode="after")
|
|
1660
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:
|
|
@@ -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:
|
|
@@ -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
|
|
|
@@ -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
|
|
@@ -47,12 +48,12 @@ coderouter/guards/self_healing.py,sha256=XeUlgFWGqMQ5E9YvvzR2CsHRJOAJEyDChqRN0xm
|
|
|
47
48
|
coderouter/guards/tool_loop.py,sha256=EzeMcmU7BLeTW2jsRVevU81l5rhWcn1oUr7EpzgXjVM,15209
|
|
48
49
|
coderouter/ingress/__init__.py,sha256=WQsCH2CGJCAhy0mS6GSEdeYZRkkQu2OHDsP4CJWTLug,155
|
|
49
50
|
coderouter/ingress/anthropic_errors.py,sha256=q8F4LmH57f8Jh2hoV4Mmnvf9sZH32uiXhQXPRBZPjCE,8122
|
|
50
|
-
coderouter/ingress/anthropic_routes.py,sha256=
|
|
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,9 +65,9 @@ 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/capability.py,sha256=_RM9I6qG_oQLN_ZN4n7Xd7__g-CtClX9cdhtKvuJUvA,28018
|
|
70
71
|
coderouter/routing/fallback.py,sha256=vx1Vrl_vy7-dLxkKvipfba_pbhQPbuhRVFjmuMMWyaI,176825
|
|
71
72
|
coderouter/routing/fallback_trace.py,sha256=LwUw56mhVIkP4Gu-fNvZmOjL8hXxY1ci6b1MuGjl5m0,20099
|
|
72
73
|
coderouter/state/__init__.py,sha256=XoGcPmmBQSiZWML2S0juSveQ78xfhtdeCliNnVyzu7E,1088
|
|
@@ -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
|