sync update from LAN repo (source dhk-1 @ 3b1ec5b4)
This commit is contained in:
1 parent
da6521b6cb
commit
264e2200d6
995 files changed
+148105
-13399
No files matched your search
@@ -1,25 +1,33 @@
|
||||
"""Administrator model configuration backed by DeerFlow's ``config.yaml``.
|
||||
"""Administrator model configuration and runtime capabilities.
|
||||
|
||||
The public ``/api/models`` endpoint intentionally stays small and never
|
||||
returns secrets. This router is the complementary administrator surface: it
|
||||
edits only the top-level ``models:`` section in the active DeerFlow config,
|
||||
keeps a disk backup before every write, and swaps the gateway's runtime config
|
||||
after validation so new requests can use the change without a restart.
|
||||
|
||||
``supports_reasoning_effort`` is deliberately different: it is an
|
||||
administrator-managed runtime capability persisted in the database, never in
|
||||
``config.yaml``. The field remains accepted on the general create/update
|
||||
payload for compatibility with older frontends, while a dedicated endpoint is
|
||||
the canonical mutation surface.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import copy
|
||||
import errno
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
import shutil
|
||||
import sys
|
||||
import tempfile
|
||||
from datetime import UTC, datetime
|
||||
from pathlib import Path
|
||||
from time import perf_counter
|
||||
from typing import Any
|
||||
from time import perf_counter, sleep
|
||||
from typing import Any, Literal
|
||||
|
||||
import yaml
|
||||
from fastapi import APIRouter, HTTPException, Request
|
||||
@@ -30,6 +38,7 @@ from app.gateway.deps import get_optional_user_from_request
|
||||
from deerflow.config.app_config import AppConfig, reload_app_config
|
||||
from deerflow.config.model_config import ModelConfig, WriteFileTransport
|
||||
from deerflow.models.factory import create_chat_model
|
||||
from deerflow.persistence.model_capabilities import ModelCapabilityStore
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
router = APIRouter(prefix="/api/admin/models", tags=["admin-models"])
|
||||
@@ -38,6 +47,56 @@ _MODEL_BLOCK_RE = re.compile(r"(?m)^models:[^\r\n]*(?:\r?\n|$)")
|
||||
_TOP_LEVEL_KEY_RE = re.compile(r"(?m)^[A-Za-z_][A-Za-z0-9_-]*:[^\r\n]*(?:\r?\n|$)")
|
||||
_BACKUP_DIR_NAME = ".model-config-backups"
|
||||
_MUTATION_LOCK = asyncio.Lock()
|
||||
_ATOMIC_REPLACE_RETRIES = 5
|
||||
_ATOMIC_REPLACE_RETRY_ERRNOS = {
|
||||
errno.EBUSY,
|
||||
errno.EAGAIN,
|
||||
getattr(errno, "ETXTBSY", 26),
|
||||
}
|
||||
|
||||
|
||||
def _is_file_mountpoint(path: Path) -> bool:
|
||||
"""Return whether ``path`` itself is mounted (common with Docker file binds)."""
|
||||
if not sys.platform.startswith("linux"):
|
||||
return False
|
||||
try:
|
||||
target = str(path.resolve())
|
||||
with Path("/proc/self/mountinfo").open(encoding="utf-8") as mountinfo:
|
||||
for line in mountinfo:
|
||||
fields = line.split(" - ", maxsplit=1)[0].split()
|
||||
if len(fields) < 5:
|
||||
continue
|
||||
mount_path = re.sub(
|
||||
r"\\([0-7]{3})",
|
||||
lambda match: chr(int(match.group(1), 8)),
|
||||
fields[4],
|
||||
)
|
||||
if mount_path == target:
|
||||
return True
|
||||
except OSError:
|
||||
logger.debug("Unable to inspect Linux mount table for model config path", exc_info=True)
|
||||
return False
|
||||
|
||||
|
||||
def _write_mounted_config_in_place(path: Path, raw_before: str, raw_after: str) -> None:
|
||||
"""Update a single-file bind mount, where Linux refuses atomic rename (EBUSY)."""
|
||||
try:
|
||||
with path.open("w", encoding="utf-8", newline="") as handle:
|
||||
handle.write(raw_after)
|
||||
handle.flush()
|
||||
os.fsync(handle.fileno())
|
||||
except Exception:
|
||||
# The backup was persisted before reaching this point. Restore the
|
||||
# previous contents best-effort so a failed write does not leave YAML
|
||||
# truncated or partially replaced.
|
||||
try:
|
||||
with path.open("w", encoding="utf-8", newline="") as handle:
|
||||
handle.write(raw_before)
|
||||
handle.flush()
|
||||
os.fsync(handle.fileno())
|
||||
except OSError:
|
||||
logger.exception("Failed to restore model config after an in-place bind-mount write failure")
|
||||
raise
|
||||
|
||||
_EDITOR_FIELDS = frozenset(
|
||||
{
|
||||
@@ -63,6 +122,7 @@ _EDITOR_FIELDS = frozenset(
|
||||
"use_responses_api",
|
||||
"output_version",
|
||||
"write_file_transport",
|
||||
"strict_stream_integrity",
|
||||
}
|
||||
)
|
||||
_PRESERVED_FIELDS = frozenset(ModelConfig.model_fields) | _EDITOR_FIELDS
|
||||
@@ -85,6 +145,7 @@ class ModelAdminItem(BaseModel):
|
||||
stream_usage: bool | None = None
|
||||
supports_thinking: bool = False
|
||||
supports_reasoning_effort: bool = False
|
||||
default_reasoning_effort: Literal["low", "medium", "xhigh"] = "xhigh"
|
||||
supports_vision: bool = False
|
||||
when_thinking_enabled: dict[str, Any] | None = None
|
||||
when_thinking_disabled: dict[str, Any] | None = None
|
||||
@@ -92,6 +153,9 @@ class ModelAdminItem(BaseModel):
|
||||
use_responses_api: bool | None = None
|
||||
output_version: str | None = None
|
||||
write_file_transport: WriteFileTransport = WriteFileTransport.NATIVE
|
||||
# ``None`` means the runtime default: auto-enabled only for the historically
|
||||
# affected qwen3.8-flash-next model id (see deerflow.models.chat_stream_state).
|
||||
strict_stream_integrity: bool | None = None
|
||||
extra_settings: dict[str, Any] = Field(default_factory=dict)
|
||||
api_key_configured: bool = False
|
||||
api_key_source: str | None = None
|
||||
@@ -126,7 +190,9 @@ class ModelUpsertRequest(BaseModel):
|
||||
temperature: float | None = Field(default=None, ge=0, le=2)
|
||||
stream_usage: bool | None = None
|
||||
supports_thinking: bool = False
|
||||
supports_reasoning_effort: bool = False
|
||||
# DB-backed capability. Accepted here for stale clients, but never written
|
||||
# into config.yaml; the dedicated capability endpoint is canonical.
|
||||
supports_reasoning_effort: bool | None = None
|
||||
supports_vision: bool = False
|
||||
when_thinking_enabled: dict[str, Any] | None = None
|
||||
when_thinking_disabled: dict[str, Any] | None = None
|
||||
@@ -137,6 +203,9 @@ class ModelUpsertRequest(BaseModel):
|
||||
# bridge setting during edits. New models still resolve to ModelConfig's
|
||||
# native default when the field is omitted.
|
||||
write_file_transport: WriteFileTransport | None = None
|
||||
# Optional on the wire so edits preserve an existing toggle; the dedicated
|
||||
# strict-stream-integrity endpoint is the canonical switch surface.
|
||||
strict_stream_integrity: bool | None = None
|
||||
extra_settings: dict[str, Any] = Field(default_factory=dict)
|
||||
|
||||
@field_validator("name", "use", "model")
|
||||
@@ -157,6 +226,24 @@ class ModelTestRequest(BaseModel):
|
||||
model: ModelUpsertRequest
|
||||
|
||||
|
||||
class ReasoningEffortCapabilityRequest(BaseModel):
|
||||
supports_reasoning_effort: bool
|
||||
default_reasoning_effort: Literal["low", "medium", "xhigh"] | None = None
|
||||
|
||||
|
||||
class WriteFileTransportToggleRequest(BaseModel):
|
||||
"""Per-model 文件流式兼容 switch: off keeps native, on selects buffered_gateway."""
|
||||
|
||||
write_file_transport: Literal["native", "buffered_gateway"]
|
||||
|
||||
|
||||
class StrictStreamIntegrityToggleRequest(BaseModel):
|
||||
"""Per-model 流完整性检测 switch: detect gateway streams that end without
|
||||
a terminal frame and retry instead of committing a half answer."""
|
||||
|
||||
strict_stream_integrity: bool
|
||||
|
||||
|
||||
class ModelOrderRequest(BaseModel):
|
||||
"""The complete ordered set of model stable names."""
|
||||
|
||||
@@ -176,13 +263,14 @@ def _mask_env_source(value: Any) -> str | None:
|
||||
return None
|
||||
|
||||
|
||||
def _to_admin_item(raw: dict[str, Any]) -> ModelAdminItem:
|
||||
def _to_admin_item(
|
||||
raw: dict[str, Any],
|
||||
*,
|
||||
supports_reasoning_effort: bool = False,
|
||||
default_reasoning_effort: str = "xhigh",
|
||||
) -> ModelAdminItem:
|
||||
api_key = raw.get("api_key")
|
||||
extra = {
|
||||
key: copy.deepcopy(value)
|
||||
for key, value in raw.items()
|
||||
if key not in _PRESERVED_FIELDS
|
||||
}
|
||||
extra = {key: copy.deepcopy(value) for key, value in raw.items() if key not in _PRESERVED_FIELDS}
|
||||
return ModelAdminItem(
|
||||
name=str(raw.get("name") or ""),
|
||||
display_name=raw.get("display_name"),
|
||||
@@ -197,7 +285,8 @@ def _to_admin_item(raw: dict[str, Any]) -> ModelAdminItem:
|
||||
temperature=raw.get("temperature"),
|
||||
stream_usage=raw.get("stream_usage"),
|
||||
supports_thinking=bool(raw.get("supports_thinking", False)),
|
||||
supports_reasoning_effort=bool(raw.get("supports_reasoning_effort", False)),
|
||||
supports_reasoning_effort=bool(supports_reasoning_effort),
|
||||
default_reasoning_effort=default_reasoning_effort if default_reasoning_effort in {"low", "medium", "xhigh"} else "xhigh",
|
||||
supports_vision=bool(raw.get("supports_vision", False)),
|
||||
when_thinking_enabled=copy.deepcopy(raw.get("when_thinking_enabled")),
|
||||
when_thinking_disabled=copy.deepcopy(raw.get("when_thinking_disabled")),
|
||||
@@ -205,6 +294,7 @@ def _to_admin_item(raw: dict[str, Any]) -> ModelAdminItem:
|
||||
use_responses_api=raw.get("use_responses_api"),
|
||||
output_version=raw.get("output_version"),
|
||||
write_file_transport=raw.get("write_file_transport", WriteFileTransport.NATIVE),
|
||||
strict_stream_integrity=raw.get("strict_stream_integrity") if isinstance(raw.get("strict_stream_integrity"), bool) else None,
|
||||
extra_settings=extra,
|
||||
api_key_configured=bool(api_key),
|
||||
api_key_source=_mask_env_source(api_key),
|
||||
@@ -215,6 +305,98 @@ def _resolve_config_path() -> Path:
|
||||
return AppConfig.resolve_config_path()
|
||||
|
||||
|
||||
def _get_capability_store(request: Request, *, required: bool = True) -> ModelCapabilityStore | None:
|
||||
app = getattr(request, "app", None)
|
||||
store = getattr(getattr(app, "state", None), "model_capability_store", None)
|
||||
if store is None and required:
|
||||
raise HTTPException(status_code=503, detail="Model capability store not available")
|
||||
return store
|
||||
|
||||
|
||||
async def _capabilities_by_name(request: Request) -> dict[str, dict[str, Any]]:
|
||||
store = _get_capability_store(request, required=False)
|
||||
if store is None:
|
||||
return {}
|
||||
rows = await store.list_capabilities()
|
||||
return {str(row.get("model_name") or ""): row for row in rows if row.get("model_name")}
|
||||
|
||||
|
||||
def _admin_user_id(user: Any) -> str | None:
|
||||
value = getattr(user, "id", None)
|
||||
return str(value) if value is not None else None
|
||||
|
||||
|
||||
def _validate_reasoning_capability(model: dict[str, Any], enabled: bool) -> None:
|
||||
if enabled and not bool(model.get("supports_thinking", False)):
|
||||
raise HTTPException(
|
||||
status_code=422,
|
||||
detail="Reasoning-effort selection requires a model that supports thinking",
|
||||
)
|
||||
|
||||
|
||||
# ``write_file_transport=buffered_gateway`` and ``strict_stream_integrity`` are
|
||||
# implemented only by the patched OpenAI-compatible chat-completions classes
|
||||
# (see deerflow.models.factory); every other driver is rejected there at
|
||||
# construction time, so mirror the same constraint here with a clearer message.
|
||||
_PATCHED_OPENAI_COMPATIBLE_USES = frozenset(
|
||||
{
|
||||
"langchain_openai:ChatOpenAI",
|
||||
"deerflow.models.patched_openai:PatchedChatOpenAI",
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def _validate_patched_openai_use(model: dict[str, Any], feature: str) -> None:
|
||||
if str(model.get("use") or "") not in _PATCHED_OPENAI_COMPATIBLE_USES:
|
||||
raise HTTPException(
|
||||
status_code=422,
|
||||
detail=(
|
||||
f"{feature} requires an OpenAI-compatible "
|
||||
"langchain_openai:ChatOpenAI model"
|
||||
),
|
||||
)
|
||||
if bool(model.get("use_responses_api")):
|
||||
raise HTTPException(
|
||||
status_code=422,
|
||||
detail=(
|
||||
f"{feature} requires the chat-completions API, "
|
||||
"not use_responses_api"
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
def _validate_write_file_transport(model: dict[str, Any], transport: str) -> None:
|
||||
if transport != WriteFileTransport.BUFFERED_GATEWAY.value:
|
||||
return
|
||||
_validate_patched_openai_use(model, "write_file compatibility")
|
||||
|
||||
|
||||
async def _restore_capability(
|
||||
store: ModelCapabilityStore,
|
||||
model_name: str,
|
||||
previous: dict[str, Any] | None,
|
||||
request: Request,
|
||||
) -> None:
|
||||
"""Best-effort semantic rollback for a failed cross-store model update."""
|
||||
|
||||
try:
|
||||
if previous is None:
|
||||
await store.delete_capability(model_name)
|
||||
return
|
||||
await store.set_reasoning_effort_support(
|
||||
model_name,
|
||||
supports_reasoning_effort=bool(previous.get("supports_reasoning_effort", False)),
|
||||
default_reasoning_effort=str(previous.get("default_reasoning_effort") or "xhigh"),
|
||||
updated_by=previous.get("updated_by"),
|
||||
)
|
||||
except Exception:
|
||||
logger.exception("Failed to roll back model capability for %s", model_name)
|
||||
finally:
|
||||
from app.gateway.catalog_cache import invalidate_catalog
|
||||
|
||||
await invalidate_catalog(request, "models")
|
||||
|
||||
|
||||
def _read_config_data(path: Path) -> tuple[str, dict[str, Any], list[dict[str, Any]]]:
|
||||
raw_text = path.read_text(encoding="utf-8")
|
||||
try:
|
||||
@@ -257,17 +439,49 @@ def _replace_models_block(raw_text: str, models: list[dict[str, Any]]) -> str:
|
||||
next_key = _TOP_LEVEL_KEY_RE.search(raw_text, block_match.end())
|
||||
end = next_key.start() if next_key else len(raw_text)
|
||||
previous_section = raw_text[block_match.end() : end]
|
||||
preserved_comments = "".join(
|
||||
line
|
||||
for line in previous_section.splitlines(keepends=True)
|
||||
if line.lstrip().startswith("#")
|
||||
)
|
||||
preserved_comments = "".join(line for line in previous_section.splitlines(keepends=True) if line.lstrip().startswith("#"))
|
||||
rendered = _render_models_block(models, newline)
|
||||
if preserved_comments:
|
||||
if not preserved_comments.endswith(("\n", "\r")):
|
||||
preserved_comments = f"{preserved_comments}{newline}"
|
||||
rendered = f"{rendered}{preserved_comments}"
|
||||
return f"{raw_text[:block_match.start()]}{rendered}{raw_text[end:]}"
|
||||
return f"{raw_text[: block_match.start()]}{rendered}{raw_text[end:]}"
|
||||
|
||||
|
||||
def _replace_config_file_with_retry(
|
||||
temp_name: str,
|
||||
path: Path,
|
||||
raw_before: str,
|
||||
raw_after: str,
|
||||
) -> None:
|
||||
"""Retry short-lived busy errors from shared/container-mounted filesystems.
|
||||
|
||||
Keep the write atomic: never fall back to truncating the live config. The
|
||||
temporary file is unique per mutation, so a bounded retry is safe when
|
||||
overlay/NFS scanners briefly hold a rename operation.
|
||||
"""
|
||||
if _is_file_mountpoint(path):
|
||||
logger.info("Model config is a single-file mount; updating mounted file in place")
|
||||
_write_mounted_config_in_place(path, raw_before, raw_after)
|
||||
os.unlink(temp_name)
|
||||
return
|
||||
|
||||
for attempt in range(_ATOMIC_REPLACE_RETRIES + 1):
|
||||
try:
|
||||
os.replace(temp_name, path)
|
||||
return
|
||||
except OSError as exc:
|
||||
if exc.errno not in _ATOMIC_REPLACE_RETRY_ERRNOS or attempt >= _ATOMIC_REPLACE_RETRIES:
|
||||
raise
|
||||
delay = min(0.4, 0.025 * (2**attempt))
|
||||
logger.warning(
|
||||
"Model config atomic replace is temporarily busy (errno=%s); retry %d/%d after %.3fs",
|
||||
exc.errno,
|
||||
attempt + 1,
|
||||
_ATOMIC_REPLACE_RETRIES,
|
||||
delay,
|
||||
)
|
||||
sleep(delay)
|
||||
|
||||
|
||||
def _backup_and_write_config(path: Path, raw_before: str, raw_after: str) -> Path:
|
||||
@@ -287,7 +501,7 @@ def _backup_and_write_config(path: Path, raw_before: str, raw_after: str) -> Pat
|
||||
handle.write(raw_after)
|
||||
handle.flush()
|
||||
os.fsync(handle.fileno())
|
||||
os.replace(temp_name, path)
|
||||
_replace_config_file_with_retry(temp_name, path, raw_before, raw_after)
|
||||
except Exception:
|
||||
try:
|
||||
os.unlink(temp_name)
|
||||
@@ -306,7 +520,10 @@ def _restore_backup(path: Path, backup_path: Path) -> None:
|
||||
|
||||
def _request_to_raw_model(payload: ModelUpsertRequest, existing: dict[str, Any] | None = None) -> dict[str, Any]:
|
||||
merged = copy.deepcopy(existing or {})
|
||||
supplied = payload.model_dump(exclude={"extra_settings", "clear_api_key"}, exclude_none=True)
|
||||
supplied = payload.model_dump(
|
||||
exclude={"extra_settings", "clear_api_key", "supports_reasoning_effort"},
|
||||
exclude_none=True,
|
||||
)
|
||||
transport = supplied.get("write_file_transport")
|
||||
if isinstance(transport, WriteFileTransport):
|
||||
supplied["write_file_transport"] = transport.value
|
||||
@@ -323,6 +540,10 @@ def _request_to_raw_model(payload: ModelUpsertRequest, existing: dict[str, Any]
|
||||
elif payload.api_key is None and existing is not None and "api_key" in existing:
|
||||
merged["api_key"] = existing["api_key"]
|
||||
|
||||
# This capability is database-owned. Strip legacy YAML values whenever a
|
||||
# model is rewritten so a later reload cannot silently regain authority.
|
||||
merged.pop("supports_reasoning_effort", None)
|
||||
|
||||
# Validate the raw schema without resolving `$ENV_VAR` values. Environment
|
||||
# resolution happens during the post-write runtime reload and is rolled back
|
||||
# if unavailable.
|
||||
@@ -341,7 +562,7 @@ def _redact_text(value: str, secrets: list[str]) -> str:
|
||||
return result[:800]
|
||||
|
||||
|
||||
async def _require_admin(request: Request) -> None:
|
||||
async def _require_admin(request: Request):
|
||||
user = getattr(request.state, "user", None)
|
||||
if user is None:
|
||||
user = await get_optional_user_from_request(request)
|
||||
@@ -349,10 +570,25 @@ async def _require_admin(request: Request) -> None:
|
||||
raise HTTPException(status_code=401, detail="Authentication is required")
|
||||
if getattr(user, "system_role", None) != "admin":
|
||||
raise HTTPException(status_code=403, detail="Model configuration requires an administrator")
|
||||
return user
|
||||
|
||||
|
||||
def _strip_database_owned_fields(models: list[dict[str, Any]]) -> list[dict[str, Any]]:
|
||||
"""Remove legacy DB-owned capability keys from every rendered YAML row."""
|
||||
|
||||
sanitized: list[dict[str, Any]] = []
|
||||
for item in models:
|
||||
clean = copy.deepcopy(item)
|
||||
clean.pop("supports_reasoning_effort", None)
|
||||
sanitized.append(clean)
|
||||
return sanitized
|
||||
|
||||
|
||||
async def _save_models(request: Request, raw_before: str, models: list[dict[str, Any]]) -> list[dict[str, Any]]:
|
||||
from app.gateway.catalog_cache import invalidate_catalog
|
||||
|
||||
path = _resolve_config_path()
|
||||
models = _strip_database_owned_fields(models)
|
||||
raw_after = _replace_models_block(raw_before, models)
|
||||
backup_path = await asyncio.to_thread(_backup_and_write_config, path, raw_before, raw_after)
|
||||
try:
|
||||
@@ -369,6 +605,7 @@ async def _save_models(request: Request, raw_before: str, models: list[dict[str,
|
||||
status_code=422,
|
||||
detail="The model configuration could not be activated; the previous config was restored.",
|
||||
) from exc
|
||||
await invalidate_catalog(request, "models")
|
||||
return models
|
||||
|
||||
|
||||
@@ -376,10 +613,20 @@ async def _save_models(request: Request, raw_before: str, models: list[dict[str,
|
||||
async def list_model_configurations(request: Request) -> ModelAdminListResponse:
|
||||
await _require_admin(request)
|
||||
path = _resolve_config_path()
|
||||
_, _, models = await asyncio.to_thread(_read_config_data, path)
|
||||
(_, _, models), capabilities = await asyncio.gather(
|
||||
asyncio.to_thread(_read_config_data, path),
|
||||
_capabilities_by_name(request),
|
||||
)
|
||||
updated_at = datetime.fromtimestamp(path.stat().st_mtime, tz=UTC).isoformat()
|
||||
return ModelAdminListResponse(
|
||||
models=[_to_admin_item(item) for item in models],
|
||||
models=[
|
||||
_to_admin_item(
|
||||
item,
|
||||
supports_reasoning_effort=bool(capabilities.get(str(item.get("name") or ""), {}).get("supports_reasoning_effort", False)),
|
||||
default_reasoning_effort=str(capabilities.get(str(item.get("name") or ""), {}).get("default_reasoning_effort") or "xhigh"),
|
||||
)
|
||||
for item in models
|
||||
],
|
||||
config_path=str(path),
|
||||
updated_at=updated_at,
|
||||
)
|
||||
@@ -387,15 +634,36 @@ async def list_model_configurations(request: Request) -> ModelAdminListResponse:
|
||||
|
||||
@router.post("", response_model=ModelAdminItem, status_code=201)
|
||||
async def create_model_configuration(body: ModelUpsertRequest, request: Request) -> ModelAdminItem:
|
||||
await _require_admin(request)
|
||||
admin = await _require_admin(request)
|
||||
async with _MUTATION_LOCK:
|
||||
path = _resolve_config_path()
|
||||
raw_before, _, models = await asyncio.to_thread(_read_config_data, path)
|
||||
if any(str(item.get("name")) == body.name for item in models):
|
||||
raise HTTPException(status_code=409, detail=f"A model named '{body.name}' already exists")
|
||||
created = _request_to_raw_model(body)
|
||||
await _save_models(request, raw_before, [*models, created])
|
||||
return _to_admin_item(created)
|
||||
capability_changed = "supports_reasoning_effort" in body.model_fields_set and body.supports_reasoning_effort is not None
|
||||
capability_store = _get_capability_store(request, required=capability_changed)
|
||||
previous_capability = None
|
||||
if capability_changed and capability_store is not None:
|
||||
_validate_reasoning_capability(created, bool(body.supports_reasoning_effort))
|
||||
previous_capability = await capability_store.get_capability(body.name)
|
||||
await capability_store.set_reasoning_effort_support(
|
||||
body.name,
|
||||
supports_reasoning_effort=bool(body.supports_reasoning_effort),
|
||||
default_reasoning_effort="xhigh",
|
||||
updated_by=_admin_user_id(admin),
|
||||
)
|
||||
try:
|
||||
await _save_models(request, raw_before, [*models, created])
|
||||
except Exception:
|
||||
if capability_changed and capability_store is not None:
|
||||
await _restore_capability(capability_store, body.name, previous_capability, request)
|
||||
raise
|
||||
return _to_admin_item(
|
||||
created,
|
||||
supports_reasoning_effort=bool(body.supports_reasoning_effort) if capability_changed else False,
|
||||
default_reasoning_effort="xhigh",
|
||||
)
|
||||
|
||||
|
||||
@router.put("/order", response_model=list[ModelAdminItem])
|
||||
@@ -425,12 +693,143 @@ async def reorder_model_configurations(body: ModelOrderRequest, request: Request
|
||||
ordered_models = [models_by_name[name] for name in body.names]
|
||||
await _save_models(request, raw_before, ordered_models)
|
||||
|
||||
return [_to_admin_item(item) for item in ordered_models]
|
||||
capabilities = await _capabilities_by_name(request)
|
||||
return [
|
||||
_to_admin_item(
|
||||
item,
|
||||
supports_reasoning_effort=bool(capabilities.get(str(item.get("name") or ""), {}).get("supports_reasoning_effort", False)),
|
||||
default_reasoning_effort=str(capabilities.get(str(item.get("name") or ""), {}).get("default_reasoning_effort") or "xhigh"),
|
||||
)
|
||||
for item in ordered_models
|
||||
]
|
||||
|
||||
|
||||
@router.put(
|
||||
"/capabilities/reasoning-effort/{model_name:path}",
|
||||
response_model=ModelAdminItem,
|
||||
)
|
||||
async def update_reasoning_effort_capability(
|
||||
model_name: str,
|
||||
body: ReasoningEffortCapabilityRequest,
|
||||
request: Request,
|
||||
) -> ModelAdminItem:
|
||||
"""Persist the per-model reasoning-effort selector capability in SQL."""
|
||||
|
||||
from app.gateway.catalog_cache import invalidate_catalog
|
||||
|
||||
admin = await _require_admin(request)
|
||||
async with _MUTATION_LOCK:
|
||||
path = _resolve_config_path()
|
||||
_, _, models = await asyncio.to_thread(_read_config_data, path)
|
||||
model = next((item for item in models if str(item.get("name")) == model_name), None)
|
||||
if model is None:
|
||||
raise HTTPException(status_code=404, detail=f"Model '{model_name}' was not found")
|
||||
_validate_reasoning_capability(model, body.supports_reasoning_effort)
|
||||
store = _get_capability_store(request)
|
||||
assert store is not None
|
||||
await store.set_reasoning_effort_support(
|
||||
model_name,
|
||||
supports_reasoning_effort=body.supports_reasoning_effort,
|
||||
default_reasoning_effort=body.default_reasoning_effort,
|
||||
updated_by=_admin_user_id(admin),
|
||||
)
|
||||
await invalidate_catalog(request, "models")
|
||||
return _to_admin_item(
|
||||
model,
|
||||
supports_reasoning_effort=body.supports_reasoning_effort,
|
||||
default_reasoning_effort=body.default_reasoning_effort or "xhigh",
|
||||
)
|
||||
|
||||
|
||||
@router.put(
|
||||
"/write-file-transport/{model_name:path}",
|
||||
response_model=ModelAdminItem,
|
||||
)
|
||||
async def update_write_file_transport(
|
||||
model_name: str,
|
||||
body: WriteFileTransportToggleRequest,
|
||||
request: Request,
|
||||
) -> ModelAdminItem:
|
||||
"""Toggle one model's buffered-gateway write_file compatibility in config.yaml.
|
||||
|
||||
The global system-settings switch keeps overriding every compatible model
|
||||
while it is enabled; this per-model value becomes effective as soon as the
|
||||
global switch is turned off.
|
||||
"""
|
||||
|
||||
await _require_admin(request)
|
||||
async with _MUTATION_LOCK:
|
||||
path = _resolve_config_path()
|
||||
raw_before, _, models = await asyncio.to_thread(_read_config_data, path)
|
||||
index = next((i for i, item in enumerate(models) if str(item.get("name")) == model_name), None)
|
||||
if index is None:
|
||||
raise HTTPException(status_code=404, detail=f"Model '{model_name}' was not found")
|
||||
updated = copy.deepcopy(models[index])
|
||||
_validate_write_file_transport(updated, body.write_file_transport)
|
||||
updated["write_file_transport"] = body.write_file_transport
|
||||
try:
|
||||
ModelConfig.model_validate(updated)
|
||||
except Exception as exc:
|
||||
raise HTTPException(status_code=422, detail=f"Invalid model configuration: {exc}") from exc
|
||||
next_models = [*models]
|
||||
next_models[index] = updated
|
||||
await _save_models(request, raw_before, next_models)
|
||||
capabilities = await _capabilities_by_name(request)
|
||||
capability = capabilities.get(model_name, {})
|
||||
return _to_admin_item(
|
||||
updated,
|
||||
supports_reasoning_effort=bool(capability.get("supports_reasoning_effort", False)),
|
||||
default_reasoning_effort=str(capability.get("default_reasoning_effort") or "xhigh"),
|
||||
)
|
||||
|
||||
|
||||
@router.put(
|
||||
"/strict-stream-integrity/{model_name:path}",
|
||||
response_model=ModelAdminItem,
|
||||
)
|
||||
async def update_strict_stream_integrity(
|
||||
model_name: str,
|
||||
body: StrictStreamIntegrityToggleRequest,
|
||||
request: Request,
|
||||
) -> ModelAdminItem:
|
||||
"""Toggle one model's strict stream integrity check in config.yaml.
|
||||
|
||||
When enabled, a provider stream that ends without a terminal frame raises
|
||||
a retriable ``IncompleteChatStreamError`` instead of committing a half
|
||||
answer — the LLM error middleware then retries the call. Explicit ``false``
|
||||
also overrides the auto-enable default for the affected qwen3.8-flash-next
|
||||
model id; omitting the key entirely keeps that default.
|
||||
"""
|
||||
|
||||
await _require_admin(request)
|
||||
async with _MUTATION_LOCK:
|
||||
path = _resolve_config_path()
|
||||
raw_before, _, models = await asyncio.to_thread(_read_config_data, path)
|
||||
index = next((i for i, item in enumerate(models) if str(item.get("name")) == model_name), None)
|
||||
if index is None:
|
||||
raise HTTPException(status_code=404, detail=f"Model '{model_name}' was not found")
|
||||
updated = copy.deepcopy(models[index])
|
||||
_validate_patched_openai_use(updated, "strict stream integrity")
|
||||
updated["strict_stream_integrity"] = body.strict_stream_integrity
|
||||
try:
|
||||
ModelConfig.model_validate(updated)
|
||||
except Exception as exc:
|
||||
raise HTTPException(status_code=422, detail=f"Invalid model configuration: {exc}") from exc
|
||||
next_models = [*models]
|
||||
next_models[index] = updated
|
||||
await _save_models(request, raw_before, next_models)
|
||||
capabilities = await _capabilities_by_name(request)
|
||||
capability = capabilities.get(model_name, {})
|
||||
return _to_admin_item(
|
||||
updated,
|
||||
supports_reasoning_effort=bool(capability.get("supports_reasoning_effort", False)),
|
||||
default_reasoning_effort=str(capability.get("default_reasoning_effort") or "xhigh"),
|
||||
)
|
||||
|
||||
|
||||
@router.put("/{model_name:path}", response_model=ModelAdminItem)
|
||||
async def update_model_configuration(model_name: str, body: ModelUpsertRequest, request: Request) -> ModelAdminItem:
|
||||
await _require_admin(request)
|
||||
admin = await _require_admin(request)
|
||||
if body.name != model_name:
|
||||
raise HTTPException(status_code=422, detail="A model's stable name cannot be changed; edit its display name instead")
|
||||
async with _MUTATION_LOCK:
|
||||
@@ -440,10 +839,40 @@ async def update_model_configuration(model_name: str, body: ModelUpsertRequest,
|
||||
if index is None:
|
||||
raise HTTPException(status_code=404, detail=f"Model '{model_name}' was not found")
|
||||
updated = _request_to_raw_model(body, models[index])
|
||||
capability_explicit = "supports_reasoning_effort" in body.model_fields_set and body.supports_reasoning_effort is not None
|
||||
capability_store = _get_capability_store(request, required=capability_explicit)
|
||||
previous_capability = await capability_store.get_capability(model_name) if capability_store is not None else None
|
||||
auto_disable_capability = bool(capability_store is not None and not bool(updated.get("supports_thinking", False)) and previous_capability and previous_capability.get("supports_reasoning_effort", False))
|
||||
capability_changed = capability_explicit or auto_disable_capability
|
||||
desired_capability = bool(body.supports_reasoning_effort) if capability_explicit else False
|
||||
if capability_changed and capability_store is not None:
|
||||
_validate_reasoning_capability(updated, desired_capability)
|
||||
await capability_store.set_reasoning_effort_support(
|
||||
model_name,
|
||||
supports_reasoning_effort=desired_capability,
|
||||
default_reasoning_effort=str((previous_capability or {}).get("default_reasoning_effort") or "xhigh"),
|
||||
updated_by=_admin_user_id(admin),
|
||||
)
|
||||
next_models = [*models]
|
||||
next_models[index] = updated
|
||||
await _save_models(request, raw_before, next_models)
|
||||
return _to_admin_item(updated)
|
||||
try:
|
||||
await _save_models(request, raw_before, next_models)
|
||||
except Exception:
|
||||
if capability_changed and capability_store is not None:
|
||||
await _restore_capability(capability_store, model_name, previous_capability, request)
|
||||
raise
|
||||
if capability_changed:
|
||||
supports_reasoning_effort = desired_capability
|
||||
elif capability_store is not None:
|
||||
current_capability = await capability_store.get_capability(model_name)
|
||||
supports_reasoning_effort = bool(current_capability and current_capability.get("supports_reasoning_effort", False))
|
||||
else:
|
||||
supports_reasoning_effort = False
|
||||
return _to_admin_item(
|
||||
updated,
|
||||
supports_reasoning_effort=supports_reasoning_effort,
|
||||
default_reasoning_effort=str((previous_capability or {}).get("default_reasoning_effort") or "xhigh"),
|
||||
)
|
||||
|
||||
|
||||
@router.delete("/{model_name:path}", status_code=204)
|
||||
@@ -457,7 +886,16 @@ async def delete_model_configuration(model_name: str, request: Request) -> None:
|
||||
index = next((i for i, item in enumerate(models) if str(item.get("name")) == model_name), None)
|
||||
if index is None:
|
||||
raise HTTPException(status_code=404, detail=f"Model '{model_name}' was not found")
|
||||
await _save_models(request, raw_before, [*models[:index], *models[index + 1 :]])
|
||||
capability_store = _get_capability_store(request, required=False)
|
||||
previous_capability = await capability_store.get_capability(model_name) if capability_store is not None else None
|
||||
if capability_store is not None:
|
||||
await capability_store.delete_capability(model_name)
|
||||
try:
|
||||
await _save_models(request, raw_before, [*models[:index], *models[index + 1 :]])
|
||||
except Exception:
|
||||
if capability_store is not None:
|
||||
await _restore_capability(capability_store, model_name, previous_capability, request)
|
||||
raise
|
||||
|
||||
|
||||
@router.post("/test", response_model=ModelTestResponse)
|
||||
|
||||
Reference in new issue
Block a user