Nan Gao bcaf6db36a
feat(models): add declarative reasoning capability contract (#5780)
* feat(models): add declarative reasoning capability contract (#5073)

Introduce an optional per-model `reasoning:` block beside the legacy
`supports_thinking` / `supports_reasoning_effort` booleans. It declares
thinking availability (unsupported | optional | required), the accepted
effort values with aliases, default, and serialization path, the payload
dialect, and the reasoning-history requirement. When present the booleans
are derived from it and contradictory profiles fail at config load.

`deerflow.models.reasoning` normalizes legacy and declared profiles into
one immutable contract and resolves every caller's generic request against
it. `create_chat_model` is the single enforcement point: required-thinking
models never receive a synthesized disable payload, unsupported effort
values never reach the provider, and the dialect synthesizes the on/off
payload when no template exists. Legacy profiles keep the historical path
byte-for-byte. The lead agent and subagent descriptor resolve the same
policy first so run metadata reports effective values.

`/api/models` and `DeerFlowClient` project the contract as `reasoning`;
the frontend derives mode clamping and effort choices from it through
`core/models/reasoning.ts`, and user preferences accept provider-specific
effort tokens. The setup wizard's Z.AI GLM-5.3-Flash profile migrates to
the contract, restoring its low/high/max effort control.

* docs(models): move the reasoning contract guidance into models/AGENTS.md

backend/AGENTS.md sits within 46 bytes of the 96 KiB effective-chain hard
limit for the middlewares, runtime, sandbox, and subagents guides, so the
new section pushed scripts/check_agent_guidance.py over it. The models
guide is the nearest file for the factory and the contract module, and
its chain has ample room.

* fix(models): address review on the reasoning contract

- vLLM dialect: the enable path now mirrors whichever chat_template_kwargs
  switch the template declares, like the disable path already did, so a
  template using the older `thinking` key no longer also grows
  `enable_thinking`.
- Validate operator-supplied effort values at `effort.path` inside
  `when_thinking_enabled`, `when_thinking_disabled`, and the `thinking`
  shortcut against the contract, not only the profile-level value; they are
  forwarded when the caller chooses nothing, so the "unknown values never
  reach the provider" guarantee now holds for them too.
- Constrain `effort.path` to dotted identifiers and reject single-segment
  paths that would replace a whole mapping (`extra_body`, `thinking`,
  `model_kwargs`, `default_headers`, `default_query`).
- Z.AI GLM-5.3-Flash profile: `default: high` instead of `max`, because the
  default also governs summarization, title generation, and subagent calls
  that never choose an effort; `max` stays selectable. Documented in the
  config example, the configuration manual, and the design note.

* fix(models): close reasoning contract review gaps

* fix(models): preserve native Ollama reasoning config

* fix(models): keep native reasoning strings and project reasoning into the descriptor

- `ModelConfig.reasoning` also accepts the level string ChatOllama takes
  for gpt-oss style models (`low` / `medium` / `high`); the factory forwards
  both the boolean and the string on the legacy path, so profiles that
  passed `reasoning: high` through before the contract field existed keep
  loading and keep sending it.
- The assembly descriptor no longer excludes `reasoning` from
  `model_parameters`: the native ChatOllama value and a declared contract's
  dialect/history/effort change the request payload, so they now move the
  assembly fingerprint.
- e2e: the sidecar spec pinned the pre-contract body where a model without
  effort control still received `reasoning_effort: minimal`; the preset is
  now dropped for such models (the Gateway discarded it anyway), so the
  assertion expects the key to be absent.
2026-09-24 20:39:30 +08:00

203 lines
8.1 KiB
Python

import logging
from typing import Literal
from fastapi import APIRouter, Depends, HTTPException, Request
from pydantic import BaseModel, Field
from app.gateway.authz import (
_AuthorizationUnavailable,
_is_internal_caller,
authorize_model_use,
resolve_model_authorization,
)
from app.gateway.deps import get_config, get_optional_user_from_request
from deerflow.config.app_config import AppConfig
from deerflow.config.model_config import ModelConfig
from deerflow.models.reasoning import reasoning_capabilities_payload, resolve_reasoning_contract
logger = logging.getLogger(__name__)
router = APIRouter(prefix="/api", tags=["models"])
class ReasoningEffortCapabilitiesResponse(BaseModel):
"""Effort values a model accepts, in display order."""
values: list[str] = Field(..., description="Accepted effort values (provider vocabulary)")
default: str | None = Field(default=None, description="Effort applied when the caller does not choose one")
aliases: dict[str, str] = Field(default_factory=dict, description="Generic DeerFlow value -> provider value")
class ReasoningCapabilitiesResponse(BaseModel):
"""Normalized reasoning contract (issue #5073), derived for legacy profiles."""
thinking: Literal["unsupported", "optional", "required"] = Field(..., description="Whether thinking can be toggled, is always on, or is unavailable")
effort: ReasoningEffortCapabilitiesResponse | None = Field(default=None, description="Effort control; null when the model exposes none")
history: Literal["preserve", "clear"] | None = Field(default=None, description="Reasoning-history requirement, when declared")
source: Literal["legacy", "contract"] = Field(..., description="Whether the profile declared a contract or the booleans were projected")
class ModelResponse(BaseModel):
"""Response model for model information."""
name: str = Field(..., description="Unique identifier for the model")
model: str = Field(..., description="Actual provider model identifier")
display_name: str | None = Field(None, description="Human-readable name")
description: str | None = Field(None, description="Model description")
supports_thinking: bool = Field(default=False, description="Whether model supports thinking mode (deprecated: derived from `reasoning`)")
supports_reasoning_effort: bool = Field(default=False, description="Whether model supports reasoning effort (deprecated: derived from `reasoning`)")
reasoning: ReasoningCapabilitiesResponse = Field(..., description="Normalized reasoning capability contract")
def _model_response(model: ModelConfig) -> ModelResponse:
return ModelResponse(
name=model.name,
model=model.model,
display_name=model.display_name,
description=model.description,
supports_thinking=model.supports_thinking,
supports_reasoning_effort=model.supports_reasoning_effort,
reasoning=ReasoningCapabilitiesResponse(**reasoning_capabilities_payload(resolve_reasoning_contract(model))),
)
class TokenUsageResponse(BaseModel):
"""Token usage display configuration."""
enabled: bool = Field(default=False, description="Whether token usage display is enabled")
class ModelsListResponse(BaseModel):
"""Response model for listing all models."""
models: list[ModelResponse]
token_usage: TokenUsageResponse
@router.get(
"/models",
response_model=ModelsListResponse,
summary="List All Models",
description="Retrieve a list of all available AI models configured in the system.",
)
async def list_models(
request: Request,
config: AppConfig = Depends(get_config),
) -> ModelsListResponse:
"""List all available models from configuration.
Returns model information suitable for frontend display,
excluding sensitive fields like API keys and internal configuration.
When ``authorization.enabled`` is true, only models the caller's role may
``list`` are returned (filtered via ``provider.filter_resources``). A
provider error yields an empty list (fail-closed) or all models (fail-open).
Returns:
A list of all configured models with their metadata and token usage display settings.
Example Response:
```json
{
"models": [
{
"name": "gpt-4",
"model": "gpt-4",
"display_name": "GPT-4",
"description": "OpenAI GPT-4 model",
"supports_thinking": false,
"supports_reasoning_effort": false,
"reasoning": {"thinking": "unsupported", "effort": null, "history": null, "source": "legacy"}
},
{
"name": "claude-3-opus",
"model": "claude-3-opus",
"display_name": "Claude 3 Opus",
"description": "Anthropic Claude 3 Opus model",
"supports_thinking": true,
"supports_reasoning_effort": false,
"reasoning": {"thinking": "optional", "effort": null, "history": null, "source": "legacy"}
}
],
"token_usage": {
"enabled": true
}
}
```
"""
visible_models = config.models
fail_closed = config.authorization.fail_closed
user = await get_optional_user_from_request(request)
if user is not None:
try:
provider, principal = resolve_model_authorization(user, is_internal=_is_internal_caller(request, user))
except _AuthorizationUnavailable as exc:
if exc.fail_closed:
visible_models = []
else:
if provider is not None and principal is not None:
try:
allowed_names = provider.filter_resources(principal, "model", [m.name for m in config.models])
if not isinstance(allowed_names, list) or any(not isinstance(n, str) for n in allowed_names):
raise TypeError("AuthorizationProvider.filter_resources must return list[str]")
allowed_set = set(allowed_names)
visible_models = [m for m in config.models if m.name in allowed_set]
except Exception:
logger.warning("Authorization provider failed while filtering models", exc_info=True)
visible_models = [] if fail_closed else config.models
models = [_model_response(model) for model in visible_models]
return ModelsListResponse(
models=models,
token_usage=TokenUsageResponse(enabled=config.token_usage.enabled),
)
@router.get(
"/models/{model_name}",
response_model=ModelResponse,
summary="Get Model Details",
description="Retrieve detailed information about a specific AI model by its name.",
)
async def get_model(
model_name: str,
request: Request,
config: AppConfig = Depends(get_config),
) -> ModelResponse:
"""Get a specific model by name.
Args:
model_name: The unique name of the model to retrieve.
Returns:
Model information if found.
Raises:
HTTPException: 404 if model not found; 403 if the caller's role may not
``use`` the model (only when ``authorization.enabled`` is true). A
provider resolution error yields 403 (fail-closed) or allows the request
(fail-open), mirroring ``list_models``'s provider-error semantics.
Example Response:
```json
{
"name": "gpt-4",
"display_name": "GPT-4",
"description": "OpenAI GPT-4 model",
"supports_thinking": false
}
```
"""
model = config.get_model_config(model_name)
if model is None:
raise HTTPException(status_code=404, detail=f"Model '{model_name}' not found")
# Phase 3: enforce model:use authorization (deny → 403, not 404, since the
# model exists but the role lacks permission to use it).
user = await get_optional_user_from_request(request)
if user is not None:
authorize_model_use(user, model_name, is_internal=_is_internal_caller(request, user), app_config=config)
return _model_response(model)