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
+49
@@ -0,0 +1,49 @@
|
||||
"""Administrator-managed per-model runtime capabilities."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Iterable
|
||||
from typing import Any
|
||||
|
||||
from deerflow.persistence.model_capabilities.base import ModelCapabilityStore
|
||||
from deerflow.persistence.model_capabilities.memory import MemoryModelCapabilityStore
|
||||
from deerflow.persistence.model_capabilities.model import ModelCapabilityRow
|
||||
from deerflow.persistence.model_capabilities.sql import ModelCapabilityRepository
|
||||
|
||||
__all__ = [
|
||||
"MemoryModelCapabilityStore",
|
||||
"ModelCapabilityRepository",
|
||||
"ModelCapabilityRow",
|
||||
"ModelCapabilityStore",
|
||||
"import_legacy_reasoning_effort_capabilities",
|
||||
"make_model_capability_store",
|
||||
]
|
||||
|
||||
|
||||
async def import_legacy_reasoning_effort_capabilities(
|
||||
store: ModelCapabilityStore,
|
||||
models: Iterable[Any],
|
||||
) -> list[str]:
|
||||
"""One-time compatibility import for legacy config-backed ``true`` flags.
|
||||
|
||||
A configured ``false`` never creates a row. Once any database row exists,
|
||||
its value is authoritative and cannot be overwritten by legacy YAML.
|
||||
"""
|
||||
|
||||
imported: list[str] = []
|
||||
for model in models:
|
||||
if not bool(getattr(model, "supports_reasoning_effort", False)):
|
||||
continue
|
||||
model_name = str(getattr(model, "name", "") or "").strip()
|
||||
if model_name and await store.seed_reasoning_effort_support(
|
||||
model_name,
|
||||
updated_by="legacy-config-import",
|
||||
):
|
||||
imported.append(model_name)
|
||||
return imported
|
||||
|
||||
|
||||
def make_model_capability_store(session_factory):
|
||||
if session_factory is None:
|
||||
return MemoryModelCapabilityStore()
|
||||
return ModelCapabilityRepository(session_factory)
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
"""Persistence contract for administrator-managed model capabilities.
|
||||
|
||||
The provider connection details still live in ``config.yaml``. Runtime
|
||||
capabilities that administrators change from the web UI live here instead so
|
||||
they are shared by every Gateway worker and survive application restarts.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import Any
|
||||
|
||||
|
||||
class ModelCapabilityStore(ABC):
|
||||
"""Global capability settings keyed by the stable configured model name."""
|
||||
|
||||
@abstractmethod
|
||||
async def list_capabilities(self) -> list[dict[str, Any]]:
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
async def get_capability(self, model_name: str) -> dict[str, Any] | None:
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
async def set_reasoning_effort_support(
|
||||
self,
|
||||
model_name: str,
|
||||
*,
|
||||
supports_reasoning_effort: bool,
|
||||
default_reasoning_effort: str | None = None,
|
||||
updated_by: str | None = None,
|
||||
) -> dict[str, Any]:
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
async def seed_reasoning_effort_support(
|
||||
self,
|
||||
model_name: str,
|
||||
*,
|
||||
updated_by: str | None = None,
|
||||
) -> bool:
|
||||
"""Insert legacy ``true`` only when no authoritative row exists."""
|
||||
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
async def delete_capability(self, model_name: str) -> None:
|
||||
raise NotImplementedError
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
"""In-memory model-capability store used by memory-mode deployments/tests."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import UTC, datetime
|
||||
from typing import Any
|
||||
|
||||
from deerflow.persistence.model_capabilities.base import ModelCapabilityStore
|
||||
|
||||
|
||||
class MemoryModelCapabilityStore(ModelCapabilityStore):
|
||||
def __init__(self) -> None:
|
||||
self._rows: dict[str, dict[str, Any]] = {}
|
||||
|
||||
async def list_capabilities(self) -> list[dict[str, Any]]:
|
||||
return [dict(self._rows[name]) for name in sorted(self._rows)]
|
||||
|
||||
async def get_capability(self, model_name: str) -> dict[str, Any] | None:
|
||||
row = self._rows.get(model_name)
|
||||
return dict(row) if row is not None else None
|
||||
|
||||
async def set_reasoning_effort_support(
|
||||
self,
|
||||
model_name: str,
|
||||
*,
|
||||
supports_reasoning_effort: bool,
|
||||
default_reasoning_effort: str | None = None,
|
||||
updated_by: str | None = None,
|
||||
) -> dict[str, Any]:
|
||||
row = {
|
||||
"model_name": model_name,
|
||||
"supports_reasoning_effort": bool(supports_reasoning_effort),
|
||||
"default_reasoning_effort": default_reasoning_effort
|
||||
or (self._rows.get(model_name) or {}).get("default_reasoning_effort")
|
||||
or "xhigh",
|
||||
"updated_by": updated_by,
|
||||
"updated_at": datetime.now(UTC).isoformat(),
|
||||
}
|
||||
self._rows[model_name] = row
|
||||
return dict(row)
|
||||
|
||||
async def seed_reasoning_effort_support(
|
||||
self,
|
||||
model_name: str,
|
||||
*,
|
||||
updated_by: str | None = None,
|
||||
) -> bool:
|
||||
if model_name in self._rows:
|
||||
return False
|
||||
await self.set_reasoning_effort_support(
|
||||
model_name,
|
||||
supports_reasoning_effort=True,
|
||||
updated_by=updated_by,
|
||||
)
|
||||
return True
|
||||
|
||||
async def delete_capability(self, model_name: str) -> None:
|
||||
self._rows.pop(model_name, None)
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
"""ORM row for administrator-managed model capabilities."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import UTC, datetime
|
||||
|
||||
from sqlalchemy import Boolean, String, false
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
|
||||
from deerflow.persistence.base import Base
|
||||
from deerflow.persistence.types import BeijingDateTime
|
||||
|
||||
|
||||
class ModelCapabilityRow(Base):
|
||||
__tablename__ = "model_capabilities"
|
||||
|
||||
model_name: Mapped[str] = mapped_column(String(160), primary_key=True)
|
||||
supports_reasoning_effort: Mapped[bool] = mapped_column(
|
||||
Boolean,
|
||||
nullable=False,
|
||||
default=False,
|
||||
server_default=false(),
|
||||
)
|
||||
default_reasoning_effort: Mapped[str] = mapped_column(
|
||||
String(16),
|
||||
nullable=False,
|
||||
default="xhigh",
|
||||
server_default="xhigh",
|
||||
)
|
||||
updated_by: Mapped[str | None] = mapped_column(String(64), nullable=True)
|
||||
updated_at: Mapped[datetime] = mapped_column(
|
||||
BeijingDateTime(),
|
||||
nullable=False,
|
||||
default=lambda: datetime.now(UTC),
|
||||
onupdate=lambda: datetime.now(UTC),
|
||||
)
|
||||
+96
@@ -0,0 +1,96 @@
|
||||
"""SQLAlchemy-backed model-capability store."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import UTC, datetime
|
||||
from typing import Any
|
||||
|
||||
from sqlalchemy import delete, select
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
|
||||
|
||||
from deerflow.persistence.model_capabilities.base import ModelCapabilityStore
|
||||
from deerflow.persistence.model_capabilities.model import ModelCapabilityRow
|
||||
|
||||
|
||||
def _to_dict(row: ModelCapabilityRow) -> dict[str, Any]:
|
||||
return {
|
||||
"model_name": row.model_name,
|
||||
"supports_reasoning_effort": bool(row.supports_reasoning_effort),
|
||||
"default_reasoning_effort": row.default_reasoning_effort or "xhigh",
|
||||
"updated_by": row.updated_by,
|
||||
"updated_at": row.updated_at.isoformat() if isinstance(row.updated_at, datetime) else row.updated_at,
|
||||
}
|
||||
|
||||
|
||||
class ModelCapabilityRepository(ModelCapabilityStore):
|
||||
def __init__(self, session_factory: async_sessionmaker[AsyncSession]) -> None:
|
||||
self._sf = session_factory
|
||||
|
||||
async def list_capabilities(self) -> list[dict[str, Any]]:
|
||||
async with self._sf() as session:
|
||||
result = await session.execute(select(ModelCapabilityRow).order_by(ModelCapabilityRow.model_name.asc()))
|
||||
return [_to_dict(row) for row in result.scalars()]
|
||||
|
||||
async def get_capability(self, model_name: str) -> dict[str, Any] | None:
|
||||
async with self._sf() as session:
|
||||
row = await session.get(ModelCapabilityRow, model_name)
|
||||
return _to_dict(row) if row is not None else None
|
||||
|
||||
async def set_reasoning_effort_support(
|
||||
self,
|
||||
model_name: str,
|
||||
*,
|
||||
supports_reasoning_effort: bool,
|
||||
default_reasoning_effort: str | None = None,
|
||||
updated_by: str | None = None,
|
||||
) -> dict[str, Any]:
|
||||
async with self._sf() as session:
|
||||
row = await session.get(ModelCapabilityRow, model_name)
|
||||
if row is None:
|
||||
row = ModelCapabilityRow(model_name=model_name)
|
||||
session.add(row)
|
||||
row.supports_reasoning_effort = bool(supports_reasoning_effort)
|
||||
if default_reasoning_effort is not None:
|
||||
row.default_reasoning_effort = default_reasoning_effort
|
||||
row.updated_by = updated_by
|
||||
row.updated_at = datetime.now(UTC)
|
||||
await session.commit()
|
||||
await session.refresh(row)
|
||||
return _to_dict(row)
|
||||
|
||||
async def seed_reasoning_effort_support(
|
||||
self,
|
||||
model_name: str,
|
||||
*,
|
||||
updated_by: str | None = None,
|
||||
) -> bool:
|
||||
"""Insert a legacy ``true`` without ever overwriting an existing row.
|
||||
|
||||
The primary-key conflict handles concurrent Gateway startup safely: if
|
||||
another worker or an administrator creates the row after our read, the
|
||||
losing seed transaction rolls back and preserves that authoritative
|
||||
value, including ``false``.
|
||||
"""
|
||||
|
||||
async with self._sf() as session:
|
||||
if await session.get(ModelCapabilityRow, model_name) is not None:
|
||||
return False
|
||||
session.add(
|
||||
ModelCapabilityRow(
|
||||
model_name=model_name,
|
||||
supports_reasoning_effort=True,
|
||||
updated_by=updated_by,
|
||||
)
|
||||
)
|
||||
try:
|
||||
await session.commit()
|
||||
except IntegrityError:
|
||||
await session.rollback()
|
||||
return False
|
||||
return True
|
||||
|
||||
async def delete_capability(self, model_name: str) -> None:
|
||||
async with self._sf() as session:
|
||||
await session.execute(delete(ModelCapabilityRow).where(ModelCapabilityRow.model_name == model_name))
|
||||
await session.commit()
|
||||
Reference in new issue
Block a user