crabcode 0.1.7__py3-none-any.whl → 0.1.8__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.
- {crabcode-0.1.7.dist-info → crabcode-0.1.8.dist-info}/METADATA +1 -1
- {crabcode-0.1.7.dist-info → crabcode-0.1.8.dist-info}/RECORD +14 -14
- crabcode_core/_version.py +1 -1
- crabcode_core/api/anthropic_adapter.py +4 -3
- crabcode_core/api/azure_adapter.py +1 -1
- crabcode_core/api/base.py +2 -2
- crabcode_core/api/codex_adapter.py +2 -1
- crabcode_core/api/model_discovery.py +66 -5
- crabcode_core/api/network.py +12 -5
- crabcode_core/api/openai_adapter.py +4 -1
- crabcode_gateway/routes/config.py +11 -3
- crabcode_gateway/schemas.py +24 -0
- {crabcode-0.1.7.dist-info → crabcode-0.1.8.dist-info}/WHEEL +0 -0
- {crabcode-0.1.7.dist-info → crabcode-0.1.8.dist-info}/entry_points.txt +0 -0
|
@@ -11,7 +11,7 @@ crabcode_cli/session_picker.py,sha256=Pett41MV4xcg6vDR4Rbq8C0bh40u-GVF_cjQldNQIo
|
|
|
11
11
|
crabcode_cli/render/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
12
12
|
crabcode_cli/render/messages.py,sha256=n9OsbLIAU3FyO9gwGMMJld7zq-71N0caz9ecmE2kIjo,568
|
|
13
13
|
crabcode_core/__init__.py,sha256=ArpgEg5xC3iR4DRT0HBg7DwU3K-r1MnmDOHpIb1b0E4,142
|
|
14
|
-
crabcode_core/_version.py,sha256=
|
|
14
|
+
crabcode_core/_version.py,sha256=wnwhPnAP1aSzJ69EjcIqL2fSUO0KufQNNkLPy4cTEVo,82
|
|
15
15
|
crabcode_core/_windows_process.py,sha256=PjRQqZfLSHdvexANQYHZDQNxyu2s1Yaxt1YNlawwNIU,2949
|
|
16
16
|
crabcode_core/agent_manager.py,sha256=Wu7kHfA5_zSTHYLemqOD85deblcwDdtvnZVE4rmq-Vc,71475
|
|
17
17
|
crabcode_core/command_line.py,sha256=MWbAF4QUDVdwGM8cKoHYnIgr8yOgqk3Y_0ndt9f-t6Y,544
|
|
@@ -32,17 +32,17 @@ crabcode_core/turn_details.py,sha256=Km_qHTGz2OhnrRFJP9kuO02AcX1q-uTmR010vIh4WBE
|
|
|
32
32
|
crabcode_core/usage.py,sha256=vNydrtitdH3Q690k-3x738LhKiJsXGcMw-ruOEL0csU,16307
|
|
33
33
|
crabcode_core/utf8_sanitize.py,sha256=RRTGgwA9jqK-HLR1WUSFwgG4xJn7ZukTDu7M6Oi3fHI,936
|
|
34
34
|
crabcode_core/api/__init__.py,sha256=IXs4hLgC4cT85TLdmS4KgtPKYDytadkwvrDyBshr5ag,674
|
|
35
|
-
crabcode_core/api/anthropic_adapter.py,sha256=
|
|
36
|
-
crabcode_core/api/azure_adapter.py,sha256=
|
|
37
|
-
crabcode_core/api/base.py,sha256=
|
|
38
|
-
crabcode_core/api/codex_adapter.py,sha256=
|
|
35
|
+
crabcode_core/api/anthropic_adapter.py,sha256=YusxU59dh_wm87xVTSv09T-Cx-Azz_wUK0zgjGs2VqI,26486
|
|
36
|
+
crabcode_core/api/azure_adapter.py,sha256=2wFAMlcrpTqySD9A5QxgR0Klig3MthJI_SUgBCmo2L8,2452
|
|
37
|
+
crabcode_core/api/base.py,sha256=lwVcfz6byMu4Yw0w-Yi5dxHlO2PHXPaiA5OlnvySmhk,8538
|
|
38
|
+
crabcode_core/api/codex_adapter.py,sha256=NKQuR-C-6HB96-4ixPCdrB4tsT1ehNYLqMJ5lzhkb2E,54813
|
|
39
39
|
crabcode_core/api/error_logging.py,sha256=2GMD8naR-Tk9Eki_i2GJQShAWGVvt6XZATs8SLXRR8Q,5326
|
|
40
40
|
crabcode_core/api/gemini_adapter.py,sha256=RVk3zlLPqDtzG8w5psu4aSoFyFLua5qQx3subqz-5DQ,1130
|
|
41
|
-
crabcode_core/api/model_discovery.py,sha256=
|
|
41
|
+
crabcode_core/api/model_discovery.py,sha256=Yow48Sa1ZAePIZOq1FfHJ8Yhn2iHAF8yzIJgZY_IkUA,10396
|
|
42
42
|
crabcode_core/api/model_info.py,sha256=hAP8bOiELV80E_sbZxXfOXriiBjpZ8nmTcYAMrp0x_0,8661
|
|
43
|
-
crabcode_core/api/network.py,sha256=
|
|
43
|
+
crabcode_core/api/network.py,sha256=PziTbJI8D6EvBvN4OVHqNwGLbdkRBIFq1oXLfcZM-lc,2798
|
|
44
44
|
crabcode_core/api/ollama_adapter.py,sha256=ZpTg11zOUGFE0J88qBXL7AvxDQU5PjWg2wWvm4uxUvY,1158
|
|
45
|
-
crabcode_core/api/openai_adapter.py,sha256=
|
|
45
|
+
crabcode_core/api/openai_adapter.py,sha256=Ea7W543mtgutmdKC7H5GbSIZg1X4bWvur3Ul759Zg_Y,12364
|
|
46
46
|
crabcode_core/api/registry.py,sha256=tCVRE74bMUn9ukCbGp3bDtSOYZ6_EmsNfdVnnkGN6k4,2413
|
|
47
47
|
crabcode_core/compact/__init__.py,sha256=9445RwiG5BI8b6mrXMcNkcPLs5NH3RSKFxDrXsgmmK8,395
|
|
48
48
|
crabcode_core/compact/compact.py,sha256=_5BjLhPJA7WkvQzBAor7d9QXF9Nluui7bZTtBNvPLpY,19580
|
|
@@ -158,7 +158,7 @@ crabcode_gateway/event_bus.py,sha256=lWLCeoKkJ9fhDT_Kwc_esokDsbeRkPi5hsK87kPxRRY
|
|
|
158
158
|
crabcode_gateway/middleware.py,sha256=bn0pUJHWINc_28ZgZ6e7i-AarkqhfQZMaxbknAVU5UA,11293
|
|
159
159
|
crabcode_gateway/protocol.py,sha256=psXRNQjkfwliXZdETlo1qsGM0IcE_OIUarCLnSNuq6o,466
|
|
160
160
|
crabcode_gateway/safe_file.py,sha256=uOdkxWdZNQ5RJ2jJ9urnIRiF_2uAP8Dkj8wR08vbDaU,2108
|
|
161
|
-
crabcode_gateway/schemas.py,sha256=
|
|
161
|
+
crabcode_gateway/schemas.py,sha256=iNmsQhYV1nZkblV6IR2qzZ-q0Bl9NMFf8SlmjCT1hEM,57381
|
|
162
162
|
crabcode_gateway/server.py,sha256=ShCrO1Zw-Ed37HfUYihyOToJasGJjL7Kzy1BFA4CJ64,20408
|
|
163
163
|
crabcode_gateway/session_registry.py,sha256=HhvLvlGDx0EgmGwjk2zHCCtWURmNDZHHmpBjroX35fk,1214
|
|
164
164
|
crabcode_gateway/task_registry.py,sha256=Ij8USxQ13ockqhoZ286hn8jXckFeLglNSgwgcnQ5tX4,26461
|
|
@@ -177,7 +177,7 @@ crabcode_gateway/routes/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG
|
|
|
177
177
|
crabcode_gateway/routes/agent.py,sha256=ZI97BVRMPgwpv45IqWFoESO_Khr0uFfy6J-tDojNWOU,13955
|
|
178
178
|
crabcode_gateway/routes/auth.py,sha256=kIynPq7LUBlKwS--1Qp1hJHpLqebB5Unt1qAI8V_dUI,4945
|
|
179
179
|
crabcode_gateway/routes/computer_use.py,sha256=_NtMi3zciudhDRcha53fXL3jECp6Oj5cWWvWKD_JLAY,4622
|
|
180
|
-
crabcode_gateway/routes/config.py,sha256=
|
|
180
|
+
crabcode_gateway/routes/config.py,sha256=rcfM2ZKVd1tVET_x6oOLyNwZ6Y4gWl6iqnWXn2WWOxM,71251
|
|
181
181
|
crabcode_gateway/routes/document.py,sha256=j01jt88A29GF6fHYcX2AparZAOPsUrwllec9YGtFswg,78665
|
|
182
182
|
crabcode_gateway/routes/event.py,sha256=WOr1WHJ-5nCcpCWIkpIos2aAl2ASJUyYKSaLZ5bCRS0,155226
|
|
183
183
|
crabcode_gateway/routes/git_review.py,sha256=-fbMOWo8JKnHCMsKew3i02lTE7Ohj9w6ocgFKCsYeio,10460
|
|
@@ -198,7 +198,7 @@ crabcode_search/embedder.py,sha256=NlNuOHS7t4JvaWXkNU-T_xxnkjRxFrr3l57dlB5IRkc,1
|
|
|
198
198
|
crabcode_search/indexer.py,sha256=bBL3OE6HMqdIRBCnI--XjaHl7_G3H4VO2d-RMKtjlkI,7745
|
|
199
199
|
crabcode_search/store.py,sha256=s1hsyRRGcAO_hdJli6M0C8rhkMW4tXj5ZVMPuCP3uhQ,8074
|
|
200
200
|
crabcode_search/tool.py,sha256=xJjAN3esvJIH3z7ILNQ1gQs5Ty5fu6u7CYX6P2WB_BY,10238
|
|
201
|
-
crabcode-0.1.
|
|
202
|
-
crabcode-0.1.
|
|
203
|
-
crabcode-0.1.
|
|
204
|
-
crabcode-0.1.
|
|
201
|
+
crabcode-0.1.8.dist-info/METADATA,sha256=JrxGaP-8IR-KHrZ5XS-kCxVE0ks51VfgaDSOnSzHO0k,2799
|
|
202
|
+
crabcode-0.1.8.dist-info/WHEEL,sha256=W3fkpkm7-wf9vBI5Z-7s0eWkeM-spu78I8Neb98DeEg,87
|
|
203
|
+
crabcode-0.1.8.dist-info/entry_points.txt,sha256=tpYKME-yDLFb2eqwryWJG-d-HW4dkvDbzwogRl2jays,52
|
|
204
|
+
crabcode-0.1.8.dist-info/RECORD,,
|
crabcode_core/_version.py
CHANGED
|
@@ -123,6 +123,7 @@ def _tools_to_api(tools: list[dict[str, Any]]) -> list[dict[str, Any]]:
|
|
|
123
123
|
class AnthropicAdapter(APIAdapter):
|
|
124
124
|
"""Adapter for Anthropic's Messages API (direct, first-party)."""
|
|
125
125
|
|
|
126
|
+
_sdk = anthropic
|
|
126
127
|
emits_response_item_events = True
|
|
127
128
|
|
|
128
129
|
def __init__(self, config: ApiConfig):
|
|
@@ -143,7 +144,7 @@ class AnthropicAdapter(APIAdapter):
|
|
|
143
144
|
kwargs["default_headers"] = config.http_headers
|
|
144
145
|
|
|
145
146
|
from crabcode_core.api.network import sdk_options
|
|
146
|
-
self.client = anthropic.AsyncAnthropic(**kwargs, **sdk_options(config))
|
|
147
|
+
self.client = anthropic.AsyncAnthropic(**kwargs, **sdk_options(config, self._sdk))
|
|
147
148
|
self._api_key = api_key
|
|
148
149
|
|
|
149
150
|
def _messages_url(self) -> str | None:
|
|
@@ -632,7 +633,7 @@ class BedrockAdapter(AnthropicAdapter):
|
|
|
632
633
|
def __init__(self, config: ApiConfig):
|
|
633
634
|
self.config = config
|
|
634
635
|
from crabcode_core.api.network import sdk_options
|
|
635
|
-
self.client = anthropic.AsyncAnthropicBedrock(**sdk_options(config))
|
|
636
|
+
self.client = anthropic.AsyncAnthropicBedrock(**sdk_options(config, self._sdk))
|
|
636
637
|
|
|
637
638
|
|
|
638
639
|
class VertexAdapter(AnthropicAdapter):
|
|
@@ -644,5 +645,5 @@ class VertexAdapter(AnthropicAdapter):
|
|
|
644
645
|
self.client = anthropic.AsyncAnthropicVertex(
|
|
645
646
|
region=os.environ.get("CLOUD_ML_REGION", "us-east5"),
|
|
646
647
|
project_id=os.environ.get("ANTHROPIC_VERTEX_PROJECT_ID"),
|
|
647
|
-
**sdk_options(config),
|
|
648
|
+
**sdk_options(config, self._sdk),
|
|
648
649
|
)
|
|
@@ -57,7 +57,7 @@ class AzureOpenAIAdapter(OpenAIAdapter):
|
|
|
57
57
|
kwargs["default_headers"] = config.http_headers
|
|
58
58
|
|
|
59
59
|
from crabcode_core.api.network import sdk_options
|
|
60
|
-
return openai.AsyncAzureOpenAI(**kwargs, **sdk_options(config))
|
|
60
|
+
return openai.AsyncAzureOpenAI(**kwargs, **sdk_options(config, self._sdk))
|
|
61
61
|
|
|
62
62
|
def _resolve_model(self, config: ModelConfig) -> str:
|
|
63
63
|
"""Azure uses deployment names instead of model names."""
|
crabcode_core/api/base.py
CHANGED
|
@@ -188,13 +188,13 @@ class APIAdapter(ABC):
|
|
|
188
188
|
async def reset_network_client(self) -> None:
|
|
189
189
|
"""Replace a failed SDK connection pool without changing its routing policy."""
|
|
190
190
|
import asyncio
|
|
191
|
-
from crabcode_core.api.network import
|
|
191
|
+
from crabcode_core.api.network import sdk_http_client
|
|
192
192
|
|
|
193
193
|
client = getattr(self, "client", None)
|
|
194
194
|
copy = getattr(client, "with_options", None)
|
|
195
195
|
if not callable(copy):
|
|
196
196
|
return
|
|
197
|
-
transport =
|
|
197
|
+
transport = sdk_http_client(self.config, getattr(self, "_sdk", None))
|
|
198
198
|
try:
|
|
199
199
|
replacement = copy(http_client=transport, max_retries=0)
|
|
200
200
|
except BaseException:
|
|
@@ -486,6 +486,7 @@ class CodexAdapter(APIAdapter):
|
|
|
486
486
|
def __init__(self, config: ApiConfig):
|
|
487
487
|
import openai
|
|
488
488
|
|
|
489
|
+
self._sdk = openai
|
|
489
490
|
self.config = config
|
|
490
491
|
api_key = None
|
|
491
492
|
oauth_account_id = None
|
|
@@ -524,7 +525,7 @@ class CodexAdapter(APIAdapter):
|
|
|
524
525
|
# differing built-in policy (which also retries 429) and apply the
|
|
525
526
|
# configured policy explicitly in _create_sdk_response_stream().
|
|
526
527
|
from crabcode_core.api.network import sdk_options
|
|
527
|
-
kwargs.update(sdk_options(config))
|
|
528
|
+
kwargs.update(sdk_options(config, self._sdk))
|
|
528
529
|
self.client = openai.AsyncOpenAI(**kwargs)
|
|
529
530
|
|
|
530
531
|
async def resolve_context_window(self) -> int:
|
|
@@ -6,6 +6,7 @@ import asyncio
|
|
|
6
6
|
import json
|
|
7
7
|
import os
|
|
8
8
|
from collections.abc import Mapping
|
|
9
|
+
from typing import Any
|
|
9
10
|
from urllib.parse import urlsplit, urlunsplit
|
|
10
11
|
|
|
11
12
|
import httpx
|
|
@@ -18,6 +19,63 @@ class ModelDiscoveryError(ValueError):
|
|
|
18
19
|
"""A safe, user-facing discovery error (never a raw provider response)."""
|
|
19
20
|
|
|
20
21
|
|
|
22
|
+
def _token_limit(*values: Any) -> int | None:
|
|
23
|
+
# Metadata is optional: ignore malformed values without losing the catalog.
|
|
24
|
+
limits = [value for value in values if type(value) is int and 0 < value <= 2_147_483_647]
|
|
25
|
+
return min(limits) if limits else None
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def _model_metadata(entry: Mapping[str, Any]) -> dict[str, Any]:
|
|
29
|
+
"""Normalize explicit limits; never guess capabilities from a model name."""
|
|
30
|
+
# https://platform.claude.com/docs/en/api/models/list
|
|
31
|
+
# https://openrouter.ai/docs/api/api-reference/models/list-all-models-and-their-properties
|
|
32
|
+
top = entry.get("top_provider")
|
|
33
|
+
top = top if isinstance(top, dict) else {}
|
|
34
|
+
result: dict[str, Any] = {}
|
|
35
|
+
context = _token_limit(*(entry.get(key) for key in (
|
|
36
|
+
"context_length", "context_window", "max_context_length", "max_model_len", "max_input_tokens",
|
|
37
|
+
)), top.get("context_length"))
|
|
38
|
+
output = _token_limit(*(entry.get(key) for key in (
|
|
39
|
+
"max_output_tokens", "max_completion_tokens", "max_tokens",
|
|
40
|
+
)), top.get("max_completion_tokens"))
|
|
41
|
+
if context is not None:
|
|
42
|
+
result["context_window"] = context
|
|
43
|
+
if output is not None:
|
|
44
|
+
result["max_tokens"] = output
|
|
45
|
+
capabilities = entry.get("capabilities")
|
|
46
|
+
thinking = capabilities.get("thinking") if isinstance(capabilities, dict) else None
|
|
47
|
+
supported = thinking.get("supported") if isinstance(thinking, dict) else None
|
|
48
|
+
if type(supported) is bool:
|
|
49
|
+
result["thinking_enabled"] = supported
|
|
50
|
+
return result
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def model_import_config(metadata: Mapping[str, Any], group: ApiConfig) -> dict[str, Any]:
|
|
54
|
+
"""Apply only supported settings, retaining explicit group preferences."""
|
|
55
|
+
result: dict[str, Any] = {}
|
|
56
|
+
context = _token_limit(metadata.get("context_window"))
|
|
57
|
+
if context is not None and group.context_window is None:
|
|
58
|
+
result["context_window"] = context
|
|
59
|
+
output = _token_limit(metadata.get("max_tokens"))
|
|
60
|
+
if output is not None:
|
|
61
|
+
# A deliberately smaller output budget stays in force. Otherwise use
|
|
62
|
+
# the published model limit instead of the generic 16k default.
|
|
63
|
+
result["max_tokens"] = min(output, group.max_tokens) if "max_tokens" in group.model_fields_set else output
|
|
64
|
+
thinking = metadata.get("thinking_enabled")
|
|
65
|
+
if type(thinking) is bool:
|
|
66
|
+
result["thinking_enabled"] = thinking and group.thinking_enabled
|
|
67
|
+
protocol = (group.format or "openai") if group.provider == "router" else (group.provider or "anthropic")
|
|
68
|
+
if protocol == "anthropic" and output is not None and result.get("thinking_enabled", group.thinking_enabled):
|
|
69
|
+
# Anthropic budgeted thinking requires at least 1024 tokens, strictly
|
|
70
|
+
# below max_tokens. Importing a smaller limit must not break requests.
|
|
71
|
+
budget = min(group.thinking_budget, result["max_tokens"] - 1)
|
|
72
|
+
if budget < 1024:
|
|
73
|
+
result["thinking_enabled"] = False
|
|
74
|
+
elif budget != group.thinking_budget:
|
|
75
|
+
result["thinking_budget"] = budget
|
|
76
|
+
return result
|
|
77
|
+
|
|
78
|
+
|
|
21
79
|
def _connection(config: ApiConfig, env: Mapping[str, str]) -> tuple[str, httpx.Headers, bool]:
|
|
22
80
|
provider = config.provider or "anthropic"
|
|
23
81
|
protocol = (config.format or "openai") if provider == "router" else provider
|
|
@@ -63,9 +121,9 @@ def _connection(config: ApiConfig, env: Mapping[str, str]) -> tuple[str, httpx.H
|
|
|
63
121
|
return endpoint, headers, anthropic
|
|
64
122
|
|
|
65
123
|
|
|
66
|
-
async def _fetch(config: ApiConfig, env: Mapping[str, str]) -> list[dict[str,
|
|
124
|
+
async def _fetch(config: ApiConfig, env: Mapping[str, str]) -> list[dict[str, Any]]:
|
|
67
125
|
endpoint, headers, anthropic = _connection(config, env)
|
|
68
|
-
models: dict[str, dict[str,
|
|
126
|
+
models: dict[str, dict[str, Any]] = {}
|
|
69
127
|
cursors: set[str] = set()
|
|
70
128
|
params: dict[str, str | int] = {"limit": 1000} if anthropic else {}
|
|
71
129
|
remaining_bytes = 4 * 1024 * 1024
|
|
@@ -93,10 +151,13 @@ async def _fetch(config: ApiConfig, env: Mapping[str, str]) -> list[dict[str, st
|
|
|
93
151
|
if not isinstance(model_id, str) or not model_id.strip() or len(model_id) > 512 or any(ord(char) < 32 for char in model_id):
|
|
94
152
|
raise ModelDiscoveryError("模型列表包含无效的模型 ID,请检查服务端响应")
|
|
95
153
|
model_id = model_id.strip()
|
|
96
|
-
model = {"id": model_id}
|
|
97
|
-
display_name = entry.get("display_name")
|
|
154
|
+
model: dict[str, Any] = {"id": model_id}
|
|
155
|
+
display_name = entry.get("display_name") or entry.get("name")
|
|
98
156
|
if isinstance(display_name, str) and display_name.strip():
|
|
99
157
|
model["name"] = display_name.strip()[:512]
|
|
158
|
+
settings = model_import_config(_model_metadata(entry), config)
|
|
159
|
+
if settings:
|
|
160
|
+
model["config"] = settings
|
|
100
161
|
models.setdefault(model_id, model)
|
|
101
162
|
if len(models) > 5000:
|
|
102
163
|
raise ModelDiscoveryError("模型列表过大,请缩小服务端返回范围后重试")
|
|
@@ -110,7 +171,7 @@ async def _fetch(config: ApiConfig, env: Mapping[str, str]) -> list[dict[str, st
|
|
|
110
171
|
raise ModelDiscoveryError("模型列表分页过多,请缩小服务端返回范围后重试")
|
|
111
172
|
|
|
112
173
|
|
|
113
|
-
async def discover_models(config: ApiConfig, settings_env: Mapping[str, str] | None = None) -> list[dict[str,
|
|
174
|
+
async def discover_models(config: ApiConfig, settings_env: Mapping[str, str] | None = None) -> list[dict[str, Any]]:
|
|
114
175
|
try:
|
|
115
176
|
# Match session initialization's setdefault precedence without mutating
|
|
116
177
|
# the Gateway environment or requiring an initialized chat session.
|
crabcode_core/api/network.py
CHANGED
|
@@ -26,17 +26,24 @@ def http_options(config: Any) -> dict[str, Any]:
|
|
|
26
26
|
return {"trust_env": False, "proxy": proxy}
|
|
27
27
|
|
|
28
28
|
|
|
29
|
-
def request_timeout(config: Any) ->
|
|
29
|
+
def request_timeout(config: Any, sdk: Any = None) -> Any:
|
|
30
30
|
timeout = float(getattr(config, "timeout", 300))
|
|
31
|
-
|
|
31
|
+
# SDK exports follow their supported transport (httpx or httpx2).
|
|
32
|
+
timeout_type = sdk.Timeout if sdk is not None else httpx.Timeout
|
|
33
|
+
return timeout_type(timeout, connect=min(timeout, 10.0))
|
|
32
34
|
|
|
33
35
|
|
|
34
|
-
def
|
|
36
|
+
def sdk_http_client(config: Any, sdk: Any = None) -> Any:
|
|
37
|
+
client_type = sdk.DefaultAsyncHttpxClient if sdk is not None else httpx.AsyncClient
|
|
38
|
+
return client_type(**http_options(config), timeout=request_timeout(config, sdk))
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def sdk_options(config: Any, sdk: Any) -> dict[str, Any]:
|
|
35
42
|
# The query loop owns retry budgets; nested SDK retries hide progress.
|
|
36
|
-
options: dict[str, Any] = {"max_retries": 0, "timeout": request_timeout(config)}
|
|
43
|
+
options: dict[str, Any] = {"max_retries": 0, "timeout": request_timeout(config, sdk)}
|
|
37
44
|
routing = http_options(config)
|
|
38
45
|
if routing:
|
|
39
|
-
options["http_client"] =
|
|
46
|
+
options["http_client"] = sdk_http_client(config, sdk)
|
|
40
47
|
return options
|
|
41
48
|
|
|
42
49
|
|
|
@@ -168,6 +168,9 @@ class OpenAIAdapter(APIAdapter):
|
|
|
168
168
|
"""
|
|
169
169
|
|
|
170
170
|
def __init__(self, config: ApiConfig):
|
|
171
|
+
import openai
|
|
172
|
+
|
|
173
|
+
self._sdk = openai
|
|
171
174
|
self.config = config
|
|
172
175
|
self.client = self._create_client(config)
|
|
173
176
|
|
|
@@ -190,7 +193,7 @@ class OpenAIAdapter(APIAdapter):
|
|
|
190
193
|
kwargs["default_headers"] = config.http_headers
|
|
191
194
|
|
|
192
195
|
from crabcode_core.api.network import sdk_options
|
|
193
|
-
return openai.AsyncOpenAI(**kwargs, **sdk_options(config))
|
|
196
|
+
return openai.AsyncOpenAI(**kwargs, **sdk_options(config, self._sdk))
|
|
194
197
|
|
|
195
198
|
def _resolve_model(self, config: ModelConfig) -> str:
|
|
196
199
|
"""Resolve the model name for the API call. Override in subclasses."""
|
|
@@ -29,6 +29,7 @@ from crabcode_gateway.schemas import (
|
|
|
29
29
|
GoalRequest,
|
|
30
30
|
GoalState,
|
|
31
31
|
ModelInfo,
|
|
32
|
+
ModelDiscoveryResponse,
|
|
32
33
|
ModelSettingsEntry,
|
|
33
34
|
ModelSettingsMutationRequest,
|
|
34
35
|
ModelSettingsResponse,
|
|
@@ -66,7 +67,7 @@ class GroupModelsRequest(BaseModel):
|
|
|
66
67
|
cwd: str | None = None
|
|
67
68
|
|
|
68
69
|
|
|
69
|
-
@router.post("/config/discover-models")
|
|
70
|
+
@router.post("/config/discover-models", response_model=ModelDiscoveryResponse, response_model_exclude_none=True)
|
|
70
71
|
async def discover_group_models(req: GroupModelsRequest, request: Request) -> dict[str, Any]:
|
|
71
72
|
from crabcode_core.api.model_discovery import ModelDiscoveryError, discover_models
|
|
72
73
|
|
|
@@ -316,7 +317,10 @@ def _redact_model_settings(value: Any, key: str = "") -> Any:
|
|
|
316
317
|
"""Keep useful configuration visible without echoing embedded credentials."""
|
|
317
318
|
normalized_key = key.lower().replace("-", "_")
|
|
318
319
|
is_reference = normalized_key.endswith("_env") or normalized_key.endswith("_path")
|
|
319
|
-
|
|
320
|
+
is_token_count = type(value) is int and normalized_key in {
|
|
321
|
+
"max_tokens", "max_output_tokens", "max_completion_tokens", "budget_tokens", "compact_buffer_tokens",
|
|
322
|
+
}
|
|
323
|
+
if key and not is_reference and not is_token_count and any(part in normalized_key for part in _SENSITIVE_CONFIG_KEYS):
|
|
320
324
|
return "[redacted]"
|
|
321
325
|
if isinstance(value, dict):
|
|
322
326
|
return {
|
|
@@ -626,11 +630,13 @@ def _mutate_model_settings(request: Request, req: ModelSettingsMutationRequest)
|
|
|
626
630
|
|
|
627
631
|
if action == "import_group_models":
|
|
628
632
|
import re
|
|
633
|
+
from crabcode_core.api.model_discovery import model_import_config
|
|
629
634
|
|
|
630
635
|
name = _validate_model_settings_name(req.name)
|
|
631
636
|
preview = _model_settings_from_files(cwd)
|
|
632
637
|
if name not in preview.groups:
|
|
633
638
|
raise HTTPException(status_code=404, detail="配置组不存在,请刷新模型目录")
|
|
639
|
+
group = ConfigManager(cwd=cwd).load().groups[name]
|
|
634
640
|
models = current.setdefault("models", {})
|
|
635
641
|
if not isinstance(models, dict):
|
|
636
642
|
raise HTTPException(status_code=422, detail="models must be a JSON object")
|
|
@@ -654,7 +660,9 @@ def _mutate_model_settings(request: Request, req: ModelSettingsMutationRequest)
|
|
|
654
660
|
tail = f" ({suffix})"
|
|
655
661
|
candidate = stem[:120 - len(tail)] + tail
|
|
656
662
|
suffix += 1
|
|
657
|
-
|
|
663
|
+
metadata = req.model_metadata.get(model_id)
|
|
664
|
+
settings = model_import_config(metadata.model_dump(exclude_none=True), group) if metadata else {}
|
|
665
|
+
models[candidate] = {"group": name, "model": model_id, **settings}
|
|
658
666
|
used_names.add(candidate)
|
|
659
667
|
elif action in {"upsert_model", "delete_model"}:
|
|
660
668
|
name = _validate_model_settings_name(req.name)
|
crabcode_gateway/schemas.py
CHANGED
|
@@ -277,6 +277,27 @@ class SetPermissionModeRequest(BaseModel):
|
|
|
277
277
|
session_id: str | None = None
|
|
278
278
|
|
|
279
279
|
|
|
280
|
+
class DiscoveredModelSettings(BaseModel):
|
|
281
|
+
"""Allowlisted settings from the model catalog, never connection credentials."""
|
|
282
|
+
|
|
283
|
+
model_config = {"extra": "forbid"}
|
|
284
|
+
context_window: int | None = Field(default=None, strict=True, gt=0, le=2_147_483_647)
|
|
285
|
+
max_tokens: int | None = Field(default=None, strict=True, gt=0, le=2_147_483_647)
|
|
286
|
+
thinking_enabled: bool | None = Field(default=None, strict=True)
|
|
287
|
+
thinking_budget: int | None = Field(default=None, strict=True, ge=1024, le=2_147_483_647)
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
class ModelDiscoveryEntry(BaseModel):
|
|
291
|
+
id: str
|
|
292
|
+
name: str | None = None
|
|
293
|
+
config: DiscoveredModelSettings | None = None
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
class ModelDiscoveryResponse(BaseModel):
|
|
297
|
+
group: str
|
|
298
|
+
models: list[ModelDiscoveryEntry]
|
|
299
|
+
|
|
300
|
+
|
|
280
301
|
class ModelSettingsMutationRequest(BaseModel):
|
|
281
302
|
"""A focused mutation of the named model configuration catalog."""
|
|
282
303
|
|
|
@@ -296,6 +317,7 @@ class ModelSettingsMutationRequest(BaseModel):
|
|
|
296
317
|
config: dict[str, Any] | None = None
|
|
297
318
|
remove_fields: list[str] = Field(default_factory=list)
|
|
298
319
|
model_ids: list[str] = Field(default_factory=list, max_length=1000)
|
|
320
|
+
model_metadata: dict[str, DiscoveredModelSettings] = Field(default_factory=dict, max_length=1000)
|
|
299
321
|
|
|
300
322
|
@model_validator(mode="after")
|
|
301
323
|
def validate_mutation(self) -> "ModelSettingsMutationRequest":
|
|
@@ -310,6 +332,8 @@ class ModelSettingsMutationRequest(BaseModel):
|
|
|
310
332
|
for value in self.model_ids
|
|
311
333
|
):
|
|
312
334
|
raise ValueError("Select valid model IDs to add to the group")
|
|
335
|
+
if not self.model_metadata.keys() <= {value.strip() for value in self.model_ids}:
|
|
336
|
+
raise ValueError("Model metadata must belong to the selected model IDs")
|
|
313
337
|
if self.name is not None:
|
|
314
338
|
self.name = self.name.strip()
|
|
315
339
|
return self
|
|
File without changes
|
|
File without changes
|