Spaces:
Running
Running
File size: 9,712 Bytes
9f6a4bd e99df76 691dc7c 3934d27 2415446 3934d27 2415446 691dc7c e99df76 a9e27c0 2415446 691dc7c 2415446 8e438ab 2415446 a9e27c0 3934d27 691dc7c 3934d27 2415446 8e438ab a9e27c0 8e438ab a9e27c0 8e438ab 2415446 3934d27 8e438ab 691dc7c 8e438ab 3934d27 8e438ab 2415446 691dc7c 2415446 a9e27c0 2415446 a9e27c0 2415446 a9e27c0 2415446 a9e27c0 2415446 3934d27 691dc7c 3934d27 691dc7c a9e27c0 3934d27 8e438ab 691dc7c 8e438ab a9e27c0 8e438ab a9e27c0 8e438ab 3934d27 691dc7c 2415446 691dc7c e99df76 691dc7c f7872b8 691dc7c e99df76 691dc7c f7872b8 691dc7c 9f6a4bd 2415446 3934d27 691dc7c 3934d27 a9e27c0 3934d27 e99df76 691dc7c e99df76 691dc7c 3934d27 e99df76 3934d27 691dc7c 3934d27 691dc7c 3934d27 e99df76 3934d27 8e438ab 2415446 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | from __future__ import annotations
"""Model-list response construction for FCC clients."""
from enum import StrEnum
from typing import Literal
from pydantic import BaseModel, Field
from free_claude_code.application.model_catalog import (
ModelCatalog,
context_window_for_client,
read_model_catalog,
)
from free_claude_code.application.ports import ModelCatalogPort
from free_claude_code.application.routing import supports_native_messages
from free_claude_code.config.constants import ANTHROPIC_DEFAULT_MAX_OUTPUT_TOKENS
from free_claude_code.config.settings import Settings
from free_claude_code.core.gateway_model_ids import (
desktop_model_id,
gateway_model_id,
no_thinking_gateway_model_id,
)
from free_claude_code.core.model_capabilities import ModelInputModality
DISCOVERED_MODEL_CREATED_AT = "1970-01-01T00:00:00Z"
# Keep clients waiting while FCC owns transport retries and cancellation.
_INFERENCE_IDLE_TIMEOUT_SECONDS = 2_147_483
_REASONING_EFFORTS = ("none", "minimal", "low", "medium", "high", "xhigh", "max")
class ModelCatalogView(StrEnum):
"""Client-specific projections of the application model inventory."""
CLAUDE = "claude"
CLAUDE_DESKTOP = "claude-desktop"
MESSAGES = "messages"
RESPONSES = "responses"
class MuseModelLimits(BaseModel):
context: int
output: int
class MuseModelMetadata(BaseModel):
"""Muse's native model visibility and capability metadata."""
name: str
is_hidden: Literal[False] = False
reasoning: bool | None = None
limit: MuseModelLimits
class MuseMetadataEnvelope(BaseModel):
muse_code: MuseModelMetadata = Field(serialization_alias="muse-code")
class ModelResponse(BaseModel):
object: Literal["model"] = "model"
created: int = 0
owned_by: str = "free-claude-code"
created_at: str
display_name: str
id: str
type: Literal["model"] = "model"
provider_model_ref: str | None = None
api_backend: Literal["responses"] | None = Field(
default=None, serialization_alias="apiBackend"
)
max_retries: Literal[0] | None = Field(
default=None, serialization_alias="maxRetries"
)
supports_reasoning_effort: bool | None = Field(
default=None, serialization_alias="supportsReasoningEffort"
)
supports_reasoning: bool | None = Field(
default=None, serialization_alias="supportsReasoning"
)
input_modalities: tuple[ModelInputModality, ...] | None = Field(
default=None, serialization_alias="inputModalities"
)
context_window_tokens: int | None = Field(
default=None, serialization_alias="contextWindow"
)
context_window_source: Literal["provider", "default"] | None = Field(
default=None, serialization_alias="contextWindowSource"
)
max_output_tokens: int | None = Field(
default=None, serialization_alias="maxCompletionTokens"
)
reasoning_efforts: tuple[str, ...] | None = Field(
default=None, serialization_alias="reasoningEfforts"
)
inference_idle_timeout_seconds: int | None = Field(
default=None, serialization_alias="inferenceIdleTimeoutSecs"
)
metadata: MuseMetadataEnvelope | None = None
class ModelsListResponse(BaseModel):
object: Literal["list"] = "list"
data: list[ModelResponse]
first_id: str | None
has_more: bool
last_id: str | None
default_model_id: str | None = None
SUPPORTED_CLAUDE_MODELS = [
ModelResponse(
id="claude-fable-5-2",
display_name="Claude Fable 5.2",
created_at=DISCOVERED_MODEL_CREATED_AT,
),
ModelResponse(
id="claude-opus-5-5",
display_name="Claude Opus 5.5",
created_at=DISCOVERED_MODEL_CREATED_AT,
),
ModelResponse(
id="claude-sonnet-5-5",
display_name="Claude Sonnet 5.5",
created_at=DISCOVERED_MODEL_CREATED_AT,
),
ModelResponse(
id="claude-haiku-5-5",
display_name="Claude Haiku 5.5",
created_at=DISCOVERED_MODEL_CREATED_AT,
),
]
def build_models_list_response(
settings: Settings,
runtime: ModelCatalogPort,
*,
view: ModelCatalogView = ModelCatalogView.CLAUDE,
) -> ModelsListResponse:
"""Return the application model inventory in the requested client view."""
catalog = read_model_catalog(runtime, settings)
if view in {ModelCatalogView.CLAUDE, ModelCatalogView.CLAUDE_DESKTOP}:
return _build_claude_models_response(
catalog, desktop=view is ModelCatalogView.CLAUDE_DESKTOP
)
return _build_direct_models_response(catalog, view=view)
def build_muse_models_list_response(
settings: Settings, runtime: ModelCatalogPort
) -> ModelsListResponse:
"""Keep routable IDs while making them visible to Muse's native picker."""
catalog = build_models_list_response(
settings, runtime, view=ModelCatalogView.RESPONSES
)
for model in catalog.data:
context = model.context_window_tokens
assert context is not None # The Responses view resolves client context.
# Muse discards all native metadata if either limit is missing.
output = model.max_output_tokens
if output is None:
output = min(ANTHROPIC_DEFAULT_MAX_OUTPUT_TOKENS, context)
model.metadata = MuseMetadataEnvelope(
muse_code=MuseModelMetadata(
name=model.display_name,
reasoning=model.supports_reasoning,
limit=MuseModelLimits(context=context, output=output),
)
)
return catalog
def _build_claude_models_response(
catalog: ModelCatalog, *, desktop: bool = False
) -> ModelsListResponse:
"""Keep shortcuts first and provider variants together in catalog order."""
models = list(SUPPORTED_CLAUDE_MODELS)
for model in catalog.models:
ref = model.provider_model_ref
native = supports_native_messages(ref.partition("/")[0])
if native or model.supports_reasoning is not False:
models.append(
_discovered_model_response(
desktop_model_id(ref) if desktop else gateway_model_id(ref),
display_name=ref if native else model.display_name,
)
)
if native:
continue
models.append(
_discovered_model_response(
desktop_model_id(ref, no_thinking=True)
if desktop
else no_thinking_gateway_model_id(ref),
display_name=(
f"{model.display_name.removesuffix(' (no thinking)')} (no thinking)"
),
)
)
return ModelsListResponse(
data=models,
first_id=models[0].id if models else None,
has_more=False,
last_id=models[-1].id if models else None,
)
def _build_direct_models_response(
catalog: ModelCatalog,
*,
view: ModelCatalogView,
) -> ModelsListResponse:
models: list[ModelResponse] = []
timeout_seconds = (
_INFERENCE_IDLE_TIMEOUT_SECONDS if view is ModelCatalogView.RESPONSES else None
)
default_model_id = catalog.default_model_id
for model in catalog.models:
native = view is ModelCatalogView.MESSAGES and supports_native_messages(
model.provider_model_ref.partition("/")[0]
)
model_id = model.provider_model_ref if native else model.wire_slug
if model.wire_slug == catalog.default_model_id:
default_model_id = model_id
allows_reasoning = model.supports_reasoning is not False
models.append(
ModelResponse(
id=model_id,
display_name=model.provider_model_ref if native else model.display_name,
created_at=DISCOVERED_MODEL_CREATED_AT,
provider_model_ref=model.provider_model_ref,
api_backend=(
"responses" if view is ModelCatalogView.RESPONSES else None
),
max_retries=0 if view is ModelCatalogView.RESPONSES else None,
supports_reasoning=model.supports_reasoning,
input_modalities=_serialize_input_modalities(model.input_modalities),
context_window_tokens=context_window_for_client(model),
context_window_source="provider"
if model.context_window_tokens is not None
and model.context_window_tokens > 0
else "default",
max_output_tokens=model.max_output_tokens,
supports_reasoning_effort=(
allows_reasoning if view is ModelCatalogView.RESPONSES else None
),
reasoning_efforts=(
_REASONING_EFFORTS
if view is ModelCatalogView.RESPONSES and allows_reasoning
else None
),
inference_idle_timeout_seconds=timeout_seconds,
)
)
return ModelsListResponse(
data=models,
default_model_id=default_model_id,
first_id=models[0].id if models else None,
has_more=False,
last_id=models[-1].id if models else None,
)
def _serialize_input_modalities(
modalities: frozenset[ModelInputModality] | None,
) -> tuple[ModelInputModality, ...] | None:
if modalities is None:
return None
return tuple(modality for modality in ModelInputModality if modality in modalities)
def _discovered_model_response(model_id: str, *, display_name: str) -> ModelResponse:
return ModelResponse(
id=model_id,
display_name=display_name,
created_at=DISCOVERED_MODEL_CREATED_AT,
)
|