crabcode 0.1.6__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: crabcode
3
- Version: 0.1.6
3
+ Version: 0.1.8
4
4
  Summary: CrabCode - AI coding assistant in the terminal
5
5
  Requires-Python: >=3.10
6
6
  Requires-Dist: anthropic>=0.40.0
@@ -2,16 +2,16 @@ crabcode_cli/__init__.py,sha256=vBL_d7W0Vt2kIaYI3iZ2yS7kZleJEQXTTlIBICm5GRY,136
2
2
  crabcode_cli/__main__.py,sha256=ekwH_ayJ5YpefhbXSDj-IM9mS9YrCwWpoDIH92V6tjg,75
3
3
  crabcode_cli/app.py,sha256=o7P9cQBTo7mY6OFDhaJVx39qdfj_Zqc2Qfku-z3eGSU,31579
4
4
  crabcode_cli/banner.py,sha256=WlQPxVLLnzIE6xlP6gFbpCDYmDewzIwLtHKFLf9GC-I,1339
5
- crabcode_cli/computer_use.py,sha256=n4N--P5i9-GBzcfsAbhM5J5ebPFt0P2_shbaHq1ZpyU,13570
5
+ crabcode_cli/computer_use.py,sha256=F2ROhv4c7-j70N_-qPKRv69PkTIwkt2sWFC_qPEfpiQ,13973
6
6
  crabcode_cli/model_manager.py,sha256=vEoD3JVyWGAnIoUAe4jspPA8dt5d3pvbRNQ_A0Ank6o,21459
7
7
  crabcode_cli/model_picker.py,sha256=ghxsVySXy9WZVJLVb3ElTf9D2sWeT6lidcPWB0gX-_Y,8352
8
8
  crabcode_cli/pipe.py,sha256=hIOqrkUNbm3jXXz7NRX-HlbbWb3uPlWZwO6UUpIhtvg,5431
9
- crabcode_cli/repl.py,sha256=Bs_gBAa4cIyMjX0no_QEm9Ow68vghpIQE43_z-sVMq4,191906
9
+ crabcode_cli/repl.py,sha256=Kl_z99bdBWxe1Ghfer2ebO4kQ2ApEA_Ewwy49HA-tJY,193026
10
10
  crabcode_cli/session_picker.py,sha256=Pett41MV4xcg6vDR4Rbq8C0bh40u-GVF_cjQldNQIoQ,10307
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=Dt9sZjC9UYWEhBzyy8qd6c9WoL9oj_Ct2KhDhWrK14E,82
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,16 +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=VrQPYxAkd1hyq0GzOC-YQYp7GWJC43DDOAAXEmnoYZw,26432
36
- crabcode_core/api/azure_adapter.py,sha256=pzh_4oq-Oqn8hmIU4orldtK9U_R_yONqc5BmoZHCiUE,2441
37
- crabcode_core/api/base.py,sha256=2fAiYazrzbk5kb4oKARClnePT4IVn9cF2tgxxoeKbP4,8579
38
- crabcode_core/api/codex_adapter.py,sha256=pgX8rzfdotUMQNDHWPiSW9QbmFrv0-MqPJ130ayCfNc,54775
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_info.py,sha256=xovzBB52FxkS7mFweltYYRtxiIHWVh13F9iel6xEt2c,8367
42
- crabcode_core/api/network.py,sha256=108VR8Ix67bklhZoV2FIPNtQ_jrI7JXUjS9A98p81-I,2438
41
+ crabcode_core/api/model_discovery.py,sha256=Yow48Sa1ZAePIZOq1FfHJ8Yhn2iHAF8yzIJgZY_IkUA,10396
42
+ crabcode_core/api/model_info.py,sha256=hAP8bOiELV80E_sbZxXfOXriiBjpZ8nmTcYAMrp0x_0,8661
43
+ crabcode_core/api/network.py,sha256=PziTbJI8D6EvBvN4OVHqNwGLbdkRBIFq1oXLfcZM-lc,2798
43
44
  crabcode_core/api/ollama_adapter.py,sha256=ZpTg11zOUGFE0J88qBXL7AvxDQU5PjWg2wWvm4uxUvY,1158
44
- crabcode_core/api/openai_adapter.py,sha256=HRrsmoc01qEo49hXU2VBbEZiMgUPvwXxgBtx3s_0NHo,12303
45
+ crabcode_core/api/openai_adapter.py,sha256=Ea7W543mtgutmdKC7H5GbSIZg1X4bWvur3Ul759Zg_Y,12364
45
46
  crabcode_core/api/registry.py,sha256=tCVRE74bMUn9ukCbGp3bDtSOYZ6_EmsNfdVnnkGN6k4,2413
46
47
  crabcode_core/compact/__init__.py,sha256=9445RwiG5BI8b6mrXMcNkcPLs5NH3RSKFxDrXsgmmK8,395
47
48
  crabcode_core/compact/compact.py,sha256=_5BjLhPJA7WkvQzBAor7d9QXF9Nluui7bZTtBNvPLpY,19580
@@ -157,7 +158,7 @@ crabcode_gateway/event_bus.py,sha256=lWLCeoKkJ9fhDT_Kwc_esokDsbeRkPi5hsK87kPxRRY
157
158
  crabcode_gateway/middleware.py,sha256=bn0pUJHWINc_28ZgZ6e7i-AarkqhfQZMaxbknAVU5UA,11293
158
159
  crabcode_gateway/protocol.py,sha256=psXRNQjkfwliXZdETlo1qsGM0IcE_OIUarCLnSNuq6o,466
159
160
  crabcode_gateway/safe_file.py,sha256=uOdkxWdZNQ5RJ2jJ9urnIRiF_2uAP8Dkj8wR08vbDaU,2108
160
- crabcode_gateway/schemas.py,sha256=dpW66eWgeYEw_wUls-JavmvPnHKiCyFHNYEBWMulPcM,55935
161
+ crabcode_gateway/schemas.py,sha256=iNmsQhYV1nZkblV6IR2qzZ-q0Bl9NMFf8SlmjCT1hEM,57381
161
162
  crabcode_gateway/server.py,sha256=ShCrO1Zw-Ed37HfUYihyOToJasGJjL7Kzy1BFA4CJ64,20408
162
163
  crabcode_gateway/session_registry.py,sha256=HhvLvlGDx0EgmGwjk2zHCCtWURmNDZHHmpBjroX35fk,1214
163
164
  crabcode_gateway/task_registry.py,sha256=Ij8USxQ13ockqhoZ286hn8jXckFeLglNSgwgcnQ5tX4,26461
@@ -176,7 +177,7 @@ crabcode_gateway/routes/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG
176
177
  crabcode_gateway/routes/agent.py,sha256=ZI97BVRMPgwpv45IqWFoESO_Khr0uFfy6J-tDojNWOU,13955
177
178
  crabcode_gateway/routes/auth.py,sha256=kIynPq7LUBlKwS--1Qp1hJHpLqebB5Unt1qAI8V_dUI,4945
178
179
  crabcode_gateway/routes/computer_use.py,sha256=_NtMi3zciudhDRcha53fXL3jECp6Oj5cWWvWKD_JLAY,4622
179
- crabcode_gateway/routes/config.py,sha256=P4f_RwQoBV3fDpoHSeuuH9EtDtgWe8mgZbK72sULHoY,68334
180
+ crabcode_gateway/routes/config.py,sha256=rcfM2ZKVd1tVET_x6oOLyNwZ6Y4gWl6iqnWXn2WWOxM,71251
180
181
  crabcode_gateway/routes/document.py,sha256=j01jt88A29GF6fHYcX2AparZAOPsUrwllec9YGtFswg,78665
181
182
  crabcode_gateway/routes/event.py,sha256=WOr1WHJ-5nCcpCWIkpIos2aAl2ASJUyYKSaLZ5bCRS0,155226
182
183
  crabcode_gateway/routes/git_review.py,sha256=-fbMOWo8JKnHCMsKew3i02lTE7Ohj9w6ocgFKCsYeio,10460
@@ -197,7 +198,7 @@ crabcode_search/embedder.py,sha256=NlNuOHS7t4JvaWXkNU-T_xxnkjRxFrr3l57dlB5IRkc,1
197
198
  crabcode_search/indexer.py,sha256=bBL3OE6HMqdIRBCnI--XjaHl7_G3H4VO2d-RMKtjlkI,7745
198
199
  crabcode_search/store.py,sha256=s1hsyRRGcAO_hdJli6M0C8rhkMW4tXj5ZVMPuCP3uhQ,8074
199
200
  crabcode_search/tool.py,sha256=xJjAN3esvJIH3z7ILNQ1gQs5Ty5fu6u7CYX6P2WB_BY,10238
200
- crabcode-0.1.6.dist-info/METADATA,sha256=KtN7VCKR2jNBJ7cevwN176mEmxExIYbcmlX8PQZsZC4,2799
201
- crabcode-0.1.6.dist-info/WHEEL,sha256=W3fkpkm7-wf9vBI5Z-7s0eWkeM-spu78I8Neb98DeEg,87
202
- crabcode-0.1.6.dist-info/entry_points.txt,sha256=tpYKME-yDLFb2eqwryWJG-d-HW4dkvDbzwogRl2jays,52
203
- crabcode-0.1.6.dist-info/RECORD,,
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,,
@@ -250,12 +250,20 @@ async def connect_session(session: Any) -> LocalComputerUse | None:
250
250
  backend = LocalComputerUse()
251
251
  if not backend.client.installed and backend.client.bundle is None:
252
252
  return None
253
+ session._computer_use_connecting = True
253
254
  try:
254
255
  await backend.connect()
255
256
  except ComputerUseError as exc:
256
257
  logger.warning("Computer Use unavailable: %s", exc)
257
258
  await backend.close()
258
259
  return None
260
+ except BaseException:
261
+ # REPL shutdown may cancel an in-flight bootstrap. Do not leave a
262
+ # heartbeat or partially connected backend behind.
263
+ await backend.close()
264
+ raise
265
+ finally:
266
+ session._computer_use_connecting = False
259
267
  session.computer_use_backend = backend
260
268
  session.computer_use_host_id = backend.host_id
261
269
  session.computer_use_enabled = True
@@ -267,6 +275,8 @@ def session_status(session: Any) -> str:
267
275
  backend = getattr(session, "computer_use_backend", None)
268
276
  host_id = getattr(session, "computer_use_host_id", None)
269
277
  if backend is None:
278
+ if getattr(session, "_computer_use_connecting", False):
279
+ return "starting"
270
280
  client = NativeClient()
271
281
  return "unavailable (run crabcode computer-use status)" if client.installed or client.bundle else "not installed"
272
282
  if not getattr(session, "computer_use_enabled", False):
crabcode_cli/repl.py CHANGED
@@ -594,6 +594,14 @@ def _configure_composer_layout(
594
594
  if not isinstance(root, HSplit) or not root.children:
595
595
  return
596
596
  main = root.children[0]
597
+ # Newer prompt_toolkit versions wrap the input in a conditional Frame.
598
+ # Follow its active branch before configuring our own frame and status.
599
+ while isinstance(main, ConditionalContainer):
600
+ main = (
601
+ main.content
602
+ if main.filter()
603
+ else getattr(main, "alternative_content", None)
604
+ )
597
605
  if not isinstance(main, FloatContainer) or not isinstance(main.content, HSplit):
598
606
  return
599
607
 
@@ -2228,10 +2236,21 @@ async def run_repl(
2228
2236
  session.on_tool_event = _on_tool_event
2229
2237
 
2230
2238
  computer_use = None
2239
+ computer_use_task: asyncio.Task[None] | None = None
2231
2240
  try:
2232
2241
  from crabcode_cli.computer_use import connect_session
2233
2242
 
2234
- computer_use = await connect_session(session)
2243
+ async def connect_computer_use() -> None:
2244
+ nonlocal computer_use
2245
+ try:
2246
+ computer_use = await connect_session(session)
2247
+ except Exception:
2248
+ logger.exception("Failed to connect optional Computer Use component")
2249
+
2250
+ # The optional native host can take tens of seconds to install, start,
2251
+ # or time out. Keep input and normal chat available while it connects;
2252
+ # ComputerUseTool reads the session's backend dynamically.
2253
+ computer_use_task = asyncio.create_task(connect_computer_use())
2235
2254
  if resume_session_id:
2236
2255
  await session.initialize()
2237
2256
  ok = await session.resume(resume_session_id)
@@ -2748,6 +2767,10 @@ async def run_repl(
2748
2767
  console.print()
2749
2768
  finally:
2750
2769
  with _shutdown_deadline():
2770
+ if computer_use_task is not None:
2771
+ computer_use_task.cancel()
2772
+ with contextlib.suppress(asyncio.CancelledError):
2773
+ await computer_use_task
2751
2774
  if background_consumer is not None:
2752
2775
  background_consumer.cancel()
2753
2776
  with contextlib.suppress(asyncio.CancelledError):
crabcode_core/_version.py CHANGED
@@ -1,3 +1,3 @@
1
1
  """Single source of truth for the CrabCode release version."""
2
2
 
3
- VERSION = "0.1.6"
3
+ VERSION = "0.1.8"
@@ -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 http_options, request_timeout
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 = httpx.AsyncClient(timeout=request_timeout(self.config), **http_options(self.config))
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:
@@ -0,0 +1,197 @@
1
+ """Read a provider's model catalog using a saved group's connection policy."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import asyncio
6
+ import json
7
+ import os
8
+ from collections.abc import Mapping
9
+ from typing import Any
10
+ from urllib.parse import urlsplit, urlunsplit
11
+
12
+ import httpx
13
+
14
+ from crabcode_core.api.network import certificate_failure, http_options
15
+ from crabcode_core.types.config import ApiConfig
16
+
17
+
18
+ class ModelDiscoveryError(ValueError):
19
+ """A safe, user-facing discovery error (never a raw provider response)."""
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
+
79
+ def _connection(config: ApiConfig, env: Mapping[str, str]) -> tuple[str, httpx.Headers, bool]:
80
+ provider = config.provider or "anthropic"
81
+ protocol = (config.format or "openai") if provider == "router" else provider
82
+ if protocol not in {"openai", "codex", "anthropic"}:
83
+ raise ModelDiscoveryError("此配置组暂不支持获取模型列表,请手动添加模型")
84
+ anthropic = protocol == "anthropic"
85
+ key = env.get(config.api_key_env or "") or env.get(
86
+ "ANTHROPIC_API_KEY" if anthropic else "OPENAI_API_KEY"
87
+ )
88
+ base = config.base_url or env.get(
89
+ "ANTHROPIC_BASE_URL" if anthropic else "OPENAI_BASE_URL"
90
+ )
91
+ if protocol == "codex" and not config.base_url and not key:
92
+ raise ModelDiscoveryError("Codex 登录认证暂不支持获取模型列表,请使用配置了 API 地址的组或手动添加")
93
+ base = base or ("https://api.anthropic.com" if anthropic else "https://api.openai.com/v1")
94
+ try:
95
+ url = urlsplit(base.strip())
96
+ if url.scheme not in {"http", "https"} or not url.hostname or url.username or url.password or url.query or url.fragment:
97
+ raise ValueError
98
+ _ = url.port
99
+ except ValueError:
100
+ raise ModelDiscoveryError("请为配置组填写有效的 HTTP(S) Base URL(不含账号密码、查询参数或片段)") from None
101
+ path = url.path.rstrip("/")
102
+ if anthropic and not path.endswith("/v1"):
103
+ path += "/v1"
104
+ elif not path:
105
+ path = "/v1"
106
+ endpoint = urlunsplit((url.scheme, url.netloc, path + "/models", "", ""))
107
+ headers = httpx.Headers({"accept": "application/json"})
108
+ if anthropic:
109
+ headers["anthropic-version"] = "2023-06-01"
110
+ if key:
111
+ headers["x-api-key"] = key
112
+ elif env.get("ANTHROPIC_AUTH_TOKEN"):
113
+ headers["authorization"] = f"Bearer {env['ANTHROPIC_AUTH_TOKEN']}"
114
+ elif key:
115
+ headers["authorization"] = f"Bearer {key}"
116
+ if not anthropic:
117
+ for variable, header in (("OPENAI_ORG_ID", "OpenAI-Organization"), ("OPENAI_PROJECT_ID", "OpenAI-Project")):
118
+ if env.get(variable):
119
+ headers[header] = env[variable]
120
+ headers.update(config.http_headers)
121
+ return endpoint, headers, anthropic
122
+
123
+
124
+ async def _fetch(config: ApiConfig, env: Mapping[str, str]) -> list[dict[str, Any]]:
125
+ endpoint, headers, anthropic = _connection(config, env)
126
+ models: dict[str, dict[str, Any]] = {}
127
+ cursors: set[str] = set()
128
+ params: dict[str, str | int] = {"limit": 1000} if anthropic else {}
129
+ remaining_bytes = 4 * 1024 * 1024
130
+ async with httpx.AsyncClient(
131
+ **http_options(config), timeout=15, follow_redirects=False,
132
+ ) as client:
133
+ for _ in range(50):
134
+ async with client.stream("GET", endpoint, headers=headers, params=params) as response:
135
+ response.raise_for_status()
136
+ chunks = []
137
+ async for chunk in response.aiter_bytes():
138
+ remaining_bytes -= len(chunk)
139
+ if remaining_bytes < 0:
140
+ raise ModelDiscoveryError("模型列表过大,请缩小服务端返回范围后重试")
141
+ chunks.append(chunk)
142
+ try:
143
+ payload = json.loads(b"".join(chunks))
144
+ except (ValueError, UnicodeError):
145
+ raise ModelDiscoveryError("接口没有返回有效的模型列表,请检查 Base URL 是否指向 API") from None
146
+ entries = payload.get("data") if isinstance(payload, dict) else None
147
+ if not isinstance(entries, list):
148
+ raise ModelDiscoveryError("接口没有返回有效的模型列表,请检查 Base URL 是否指向 API")
149
+ for entry in entries:
150
+ model_id = entry.get("id") if isinstance(entry, dict) else None
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):
152
+ raise ModelDiscoveryError("模型列表包含无效的模型 ID,请检查服务端响应")
153
+ model_id = model_id.strip()
154
+ model: dict[str, Any] = {"id": model_id}
155
+ display_name = entry.get("display_name") or entry.get("name")
156
+ if isinstance(display_name, str) and display_name.strip():
157
+ model["name"] = display_name.strip()[:512]
158
+ settings = model_import_config(_model_metadata(entry), config)
159
+ if settings:
160
+ model["config"] = settings
161
+ models.setdefault(model_id, model)
162
+ if len(models) > 5000:
163
+ raise ModelDiscoveryError("模型列表过大,请缩小服务端返回范围后重试")
164
+ if not payload.get("has_more"):
165
+ return sorted(models.values(), key=lambda model: model["id"].casefold())
166
+ cursor = payload.get("last_id")
167
+ if not isinstance(cursor, str) or not cursor or cursor in cursors:
168
+ raise ModelDiscoveryError("模型列表分页无效,请检查服务端响应")
169
+ cursors.add(cursor)
170
+ params["after_id" if anthropic else "after"] = cursor
171
+ raise ModelDiscoveryError("模型列表分页过多,请缩小服务端返回范围后重试")
172
+
173
+
174
+ async def discover_models(config: ApiConfig, settings_env: Mapping[str, str] | None = None) -> list[dict[str, Any]]:
175
+ try:
176
+ # Match session initialization's setdefault precedence without mutating
177
+ # the Gateway environment or requiring an initialized chat session.
178
+ env = {**(settings_env or {}), **os.environ}
179
+ return await asyncio.wait_for(_fetch(config, env), timeout=15)
180
+ except ModelDiscoveryError:
181
+ raise
182
+ except httpx.HTTPStatusError as exc:
183
+ status = exc.response.status_code
184
+ reason = {
185
+ 401: "认证失败,请检查配置组的 API Key 或认证请求头",
186
+ 403: "没有获取模型列表的权限,请检查密钥权限",
187
+ 404: "服务未提供模型列表接口,请检查 Base URL 或手动添加模型",
188
+ 405: "服务未提供模型列表接口,请手动添加模型",
189
+ 429: "请求过于频繁,请稍后重试",
190
+ }.get(status, "获取模型列表失败,请检查服务状态与 API 地址")
191
+ raise ModelDiscoveryError(f"HTTP {status}:{reason}") from None
192
+ except (TimeoutError, httpx.TimeoutException):
193
+ raise ModelDiscoveryError("获取模型列表超时(最多 15 秒),请检查网络和代理") from None
194
+ except Exception as exc:
195
+ if certificate_failure(exc):
196
+ raise ModelDiscoveryError("证书校验失败,请检查证书或网络拦截") from None
197
+ raise ModelDiscoveryError("获取模型列表失败,请检查配置组的 API 地址、认证信息和网络策略") from None
@@ -100,14 +100,17 @@ KNOWN_CONTEXT_WINDOWS: dict[str, int] = {
100
100
  "grok-build-0.1": 256_000,
101
101
  "grok-code-fast": 256_000,
102
102
  # DeepSeek
103
- "deepseek-flash": 1_000_000,
104
- "deepseek-v4-pro": 1_000_000,
105
- "deepseek-v4-flash": 1_000_000,
106
- "deepseek-v4.1": 1_000_000,
103
+ # Official /models metadata: https://api-docs.deepseek.com/api/list-models/
104
+ # deepseek-flash currently serves DeepSeek-V4.1-Flash-1017.
105
+ "deepseek-flash": 1_048_576,
106
+ "deepseek-v4.1-flash-1017": 1_048_576,
107
+ "deepseek-v4.1-flash": 1_048_576,
108
+ "deepseek-v4.1": 1_048_576,
109
+ "deepseek-v4-pro": 1_048_576,
110
+ "deepseek-v4-flash": 1_048_576,
107
111
  "deep-research-preview-04-2026": 1_048_576,
108
112
  "deep-research-max-preview-04-2026": 1_048_576,
109
113
  "antigravity-preview-09-2026": 1_048_576,
110
- "deepseek-v4.1-flash": 1_000_000,
111
114
  "deepseek-chat": 128_000,
112
115
  "deepseek-reasoner": 128_000,
113
116
  # GLM (Zhipu)
@@ -127,6 +130,8 @@ KNOWN_CONTEXT_WINDOWS: dict[str, int] = {
127
130
  "qwen3.6-flash": 1_000_000,
128
131
  "qwen3.5-plus": 1_000_000,
129
132
  "qwen3.5-flash": 1_000_000,
133
+ "kimi-k3.1-pro-highspeed": 1_000_000,
134
+ "kimi-k3.1-pro": 1_000_000,
130
135
  "kimi-k3": 1_000_000,
131
136
  "kimi-k2.7-code": 262_144,
132
137
  "kimi-k2.6": 262_144,
@@ -213,6 +218,7 @@ KNOWN_CONTEXT_WINDOWS: dict[str, int] = {
213
218
  "mistral": 32_000,
214
219
  "mixtral": 32_000,
215
220
  # Gemini
221
+ "gemini-4-argon": 1_048_576,
216
222
  "gemini-3.8-live-extended-thinking": 131_072,
217
223
  "gemini-3.8-live": 131_072,
218
224
  "gemini-3.8-flash": 1_048_576,
@@ -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) -> httpx.Timeout:
29
+ def request_timeout(config: Any, sdk: Any = None) -> Any:
30
30
  timeout = float(getattr(config, "timeout", 300))
31
- return httpx.Timeout(timeout, connect=min(timeout, 10.0))
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 sdk_options(config: Any) -> dict[str, Any]:
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"] = httpx.AsyncClient(**routing, timeout=request_timeout(config))
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,
@@ -61,6 +62,27 @@ class ModelTestRequest(BaseModel):
61
62
  cwd: str | None = None
62
63
 
63
64
 
65
+ class GroupModelsRequest(BaseModel):
66
+ group: str
67
+ cwd: str | None = None
68
+
69
+
70
+ @router.post("/config/discover-models", response_model=ModelDiscoveryResponse, response_model_exclude_none=True)
71
+ async def discover_group_models(req: GroupModelsRequest, request: Request) -> dict[str, Any]:
72
+ from crabcode_core.api.model_discovery import ModelDiscoveryError, discover_models
73
+
74
+ cwd = _resolve_model_settings_cwd(request, req.cwd)
75
+ settings = ConfigManager(cwd=cwd).load()
76
+ config = settings.groups.get(req.group)
77
+ if config is None:
78
+ raise HTTPException(status_code=404, detail="配置组不存在,请刷新模型目录")
79
+ try:
80
+ models = await discover_models(config, settings.env)
81
+ except ModelDiscoveryError as exc:
82
+ raise HTTPException(status_code=400, detail=str(exc)) from None
83
+ return {"group": req.group, "models": models}
84
+
85
+
64
86
  def _model_test_error(exc: Exception) -> str:
65
87
  # Do not return raw provider messages: they may contain keys, headers or URLs.
66
88
  from crabcode_core.api.network import certificate_failure
@@ -295,7 +317,10 @@ def _redact_model_settings(value: Any, key: str = "") -> Any:
295
317
  """Keep useful configuration visible without echoing embedded credentials."""
296
318
  normalized_key = key.lower().replace("-", "_")
297
319
  is_reference = normalized_key.endswith("_env") or normalized_key.endswith("_path")
298
- if key and not is_reference and any(part in normalized_key for part in _SENSITIVE_CONFIG_KEYS):
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):
299
324
  return "[redacted]"
300
325
  if isinstance(value, dict):
301
326
  return {
@@ -603,7 +628,43 @@ def _mutate_model_settings(request: Request, req: ModelSettingsMutationRequest)
603
628
  current = _read_settings_object(path)
604
629
  action = req.action
605
630
 
606
- if action in {"upsert_model", "delete_model"}:
631
+ if action == "import_group_models":
632
+ import re
633
+ from crabcode_core.api.model_discovery import model_import_config
634
+
635
+ name = _validate_model_settings_name(req.name)
636
+ preview = _model_settings_from_files(cwd)
637
+ if name not in preview.groups:
638
+ raise HTTPException(status_code=404, detail="配置组不存在,请刷新模型目录")
639
+ group = ConfigManager(cwd=cwd).load().groups[name]
640
+ models = current.setdefault("models", {})
641
+ if not isinstance(models, dict):
642
+ raise HTTPException(status_code=422, detail="models must be a JSON object")
643
+ existing_ids = {
644
+ model.effective.get("model") for model in preview.models if model.group == name
645
+ }
646
+ used_names = {model.name for model in preview.models} | set(models)
647
+ for model_id in dict.fromkeys(value.strip() for value in req.model_ids):
648
+ if model_id in existing_ids:
649
+ continue
650
+ # Profile names cannot contain slashes; the API ID remains exact.
651
+ stem = re.sub(r"[/\\\x00-\x1f]", "-", model_id)[:120].strip()
652
+ if stem in {".", ".."}:
653
+ stem = "model"
654
+ candidate = stem
655
+ if candidate in used_names:
656
+ stem = f"{name} - {stem}"[:120]
657
+ candidate = stem
658
+ suffix = 2
659
+ while candidate in used_names:
660
+ tail = f" ({suffix})"
661
+ candidate = stem[:120 - len(tail)] + tail
662
+ suffix += 1
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}
666
+ used_names.add(candidate)
667
+ elif action in {"upsert_model", "delete_model"}:
607
668
  name = _validate_model_settings_name(req.name)
608
669
  models = current.get("models")
609
670
  if models is None:
@@ -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
 
@@ -287,6 +308,7 @@ class ModelSettingsMutationRequest(BaseModel):
287
308
  "delete_group",
288
309
  "set_default_model",
289
310
  "clear_default_model",
311
+ "import_group_models",
290
312
  ]
291
313
  source: Literal["userSettings", "projectSettings", "localSettings"] = "projectSettings"
292
314
  cwd: str | None = None
@@ -294,6 +316,8 @@ class ModelSettingsMutationRequest(BaseModel):
294
316
  previous_name: str | None = None
295
317
  config: dict[str, Any] | None = None
296
318
  remove_fields: list[str] = Field(default_factory=list)
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)
297
321
 
298
322
  @model_validator(mode="after")
299
323
  def validate_mutation(self) -> "ModelSettingsMutationRequest":
@@ -302,6 +326,14 @@ class ModelSettingsMutationRequest(BaseModel):
302
326
  raise ValueError("name is required for this model settings action")
303
327
  if self.action in {"upsert_model", "upsert_group"} and self.config is None:
304
328
  raise ValueError("config is required when creating or updating settings")
329
+ if self.action == "import_group_models":
330
+ if not self.model_ids or any(
331
+ not value.strip() or len(value) > 512 or any(ord(char) < 32 for char in value)
332
+ for value in self.model_ids
333
+ ):
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")
305
337
  if self.name is not None:
306
338
  self.name = self.name.strip()
307
339
  return self