sync update from LAN repo (source dhk-1 @ 3b1ec5b4)

This commit is contained in:
gozeros committed 2026-11-05 10:56:34 +08:00
1 parent da6521b6cb
commit 264e2200d6
995 files changed
+148105 -13399

No files matched your search

@@ -5,9 +5,10 @@ import os
import re
import threading
from typing import Any
from urllib.parse import urlsplit
import yaml
from pydantic import BaseModel, field_validator, model_validator
from pydantic import BaseModel, Field, field_validator, model_validator
from deerflow.config.paths import get_paths
@@ -82,6 +83,124 @@ class CommonQuestion(BaseModel):
return value
_CHAT_LINK_URL_MAX = 2048
_CHAT_LINK_PARAM_MAX = 30
class AgentChatRouteParam(BaseModel):
"""One query parameter appended when an agent's gallery 「对话」 button opens its link.
Same shape as a light-app route param: login values are resolved in the
browser from the current user, theme values follow the active theme.
"""
key: str = ""
paramType: str = "login"
source: str | None = None
customValue: str | None = None
themeValue: str | None = None
class AgentChatLinkConfig(BaseModel):
"""Optional external (or in-app) destination for the gallery chat button."""
url: str = ""
open_mode: str = "blank"
route_params: list[AgentChatRouteParam] = Field(default_factory=list)
def _is_allowed_agent_chat_url(url: str) -> bool:
lowered = url.lower()
if lowered.startswith(("javascript:", "data:", "vbscript:", "file:")):
return False
if url.startswith("#/"):
return True
if url.startswith("/") and not url.startswith("//") and not url.startswith("/\\"):
return True
parsed = urlsplit(url)
return parsed.scheme in {"http", "https"} and bool(parsed.netloc)
def normalize_agent_chat_link(value: Any, *, strict: bool = False) -> dict[str, Any] | None:
"""Return a clean chat-link dict, or None when the link is unset.
``strict=True`` raises ``ValueError`` for a non-empty but illegal link so
the API can reject it. Disk loads stay lenient: a bad value is dropped
instead of making the whole agent unloadable.
"""
if value is None:
return None
if hasattr(value, "model_dump"):
value = value.model_dump()
if not isinstance(value, dict):
if strict:
raise ValueError("对话跳转配置格式不正确")
return None
url = str(value.get("url") or "").strip()
if not url:
return None
if len(url) > _CHAT_LINK_URL_MAX or not _is_allowed_agent_chat_url(url):
if strict:
raise ValueError("对话链接只支持 http(s) 地址,或以 /、#/ 开头的站内路径")
return None
mode = str(value.get("open_mode") or "blank").strip() or "blank"
if mode not in {"self", "blank"}:
if strict:
raise ValueError("打开方式只支持当前窗口或新窗口")
mode = "blank"
raw_params = value.get("route_params") or []
if raw_params is None:
raw_params = []
if not isinstance(raw_params, list):
if strict:
raise ValueError("路由参数格式不正确")
raw_params = []
if len(raw_params) > _CHAT_LINK_PARAM_MAX:
if strict:
raise ValueError("路由参数最多 30 个")
raw_params = raw_params[:_CHAT_LINK_PARAM_MAX]
params: list[dict[str, Any]] = []
for item in raw_params:
if hasattr(item, "model_dump"):
item = item.model_dump()
if not isinstance(item, dict):
if strict:
raise ValueError("路由参数格式不正确")
continue
key = str(item.get("key") or "").strip()
if not key:
if strict:
raise ValueError("路由参数键名不可为空")
continue
if len(key) > 128:
if strict:
raise ValueError("路由参数键名过长")
continue
param_type = str(item.get("paramType") or "login").strip() or "login"
if param_type not in {"login", "theme"}:
if strict:
raise ValueError("路由参数类型只支持登录参数或主题参数")
continue
source = None if item.get("source") is None else str(item.get("source"))[:64]
custom = None if item.get("customValue") is None else str(item.get("customValue"))[:2048]
theme = None if item.get("themeValue") is None else str(item.get("themeValue"))[:2048]
params.append(
{
"key": key,
"paramType": param_type,
"source": source if param_type == "login" else None,
"customValue": custom if param_type == "login" and source == "other" else None,
"themeValue": theme if param_type == "theme" else None,
}
)
return {"url": url, "open_mode": mode, "route_params": params}
class AgentConfig(BaseModel):
"""Configuration for a custom agent."""
@@ -89,6 +208,10 @@ class AgentConfig(BaseModel):
name: str
description: str = ""
model: str | None = None
# Whether this agent inherits the administrator-configured prompt suffix
# for its selected model. Existing configs omit the field, so True keeps
# their current behavior without a migration.
model_prompt_suffix_enabled: bool = True
tool_groups: list[str] | None = None
# skills controls which skills are loaded into a custom agent's prompt:
# - None (or omitted): no skills (custom agents require explicit selection)
@@ -112,6 +235,15 @@ class AgentConfig(BaseModel):
# DeerFlow mapping ids this agent may offer in its new-conversation
# knowledge selector. WeKnora ids and credentials never enter agent files.
llmwiki_knowledge_base_ids: list[str] | None = None
# Gallery 「对话」 button destination. None / empty url keeps the in-app chat.
# ``open_mode`` is ``blank`` (new window) or ``self`` (current window).
# ``route_params`` match light-app route params and are resolved in the browser.
chat_link: dict[str, Any] | None = None
@field_validator("chat_link", mode="before")
@classmethod
def _normalize_chat_link(cls, value: Any) -> dict[str, Any] | None:
return normalize_agent_chat_link(value, strict=False)
@field_validator("llmwiki_knowledge_base_ids")
@classmethod
@@ -0,0 +1,18 @@
"""Optional Redis cache for high-traffic read-only catalog APIs."""
from __future__ import annotations
from pydantic import BaseModel, Field
class ApiReadCacheConfig(BaseModel):
"""Cache bounds and connection settings for agents/skills list responses."""
enabled: bool = Field(default=False, description="Enable the optional Redis cache for homepage agent and skill lists.")
url: str | None = Field(default=None, description="Redis URL. If empty, the enabled concurrency.redis connection may be reused.")
password: str | None = Field(default=None, description="Optional password when the Redis URL does not include credentials.")
namespace: str = Field(default="cmzs_deerflow:homepage", description="Separate Redis key prefix for homepage list cache entries.")
ttl_seconds: int = Field(default=300, ge=1, le=3600)
max_value_bytes: int = Field(default=524288, ge=1024, le=5242880)
socket_connect_timeout_ms: int = Field(default=250, ge=1, le=10000)
socket_timeout_ms: int = Field(default=750, ge=1, le=10000)
@@ -10,6 +10,7 @@ from pydantic import BaseModel, ConfigDict, Field
from deerflow.config.acp_config import ACPAgentConfig, load_acp_config_from_dict
from deerflow.config.agents_api_config import AgentsApiConfig, load_agents_api_config_from_dict
from deerflow.config.api_read_cache_config import ApiReadCacheConfig
from deerflow.config.browser_context_config import BrowserContextConfig
from deerflow.config.checkpointer_config import CheckpointerConfig, load_checkpointer_config_from_dict
from deerflow.config.concurrency_config import ConcurrencyConfig
@@ -27,6 +28,7 @@ from deerflow.config.memory_config import MemoryConfig, load_memory_config_from_
from deerflow.config.model_config import ModelConfig
from deerflow.config.report_collaboration_config import ReportCollaborationConfig
from deerflow.config.run_events_config import RunEventsConfig
from deerflow.config.runtime_contention_config import RuntimeContentionConfig
from deerflow.config.runtime_paths import existing_project_file
from deerflow.config.runtime_performance_config import RuntimePerformanceConfig
from deerflow.config.sandbox_config import SandboxConfig
@@ -119,7 +121,11 @@ class AppConfig(BaseModel):
"""Config for the DeerFlow application"""
log_level: str = Field(default="info", description="Logging level for deerflow and app modules (debug/info/warning/error); third-party libraries are not affected")
runtime_performance: RuntimePerformanceConfig = Field(default_factory=RuntimePerformanceConfig, description="Skill-view reuse and cache default on; model timing and the Gateway executor stay off unless set. Set a skill-view switch false to disable it.")
runtime_performance: RuntimePerformanceConfig = Field(
default_factory=RuntimePerformanceConfig,
description="Skill-view reuse/cache default on; model timing and the Gateway executor stay off unless set. Set a skill-view switch false to disable it.",
)
runtime_contention: RuntimeContentionConfig = Field(default_factory=RuntimeContentionConfig)
token_usage: TokenUsageConfig = Field(default_factory=TokenUsageConfig, description="Token usage tracking configuration")
models: list[ModelConfig] = Field(default_factory=list, description="Available models")
vision: VisionConfig = Field(default_factory=VisionConfig, description="Image recognition (识图) configuration")
@@ -150,6 +156,10 @@ class AppConfig(BaseModel):
checkpointer: CheckpointerConfig | None = Field(default=None, description="Checkpointer configuration")
stream_bridge: StreamBridgeConfig | None = Field(default=None, description="Stream bridge configuration")
concurrency: ConcurrencyConfig = Field(default_factory=ConcurrencyConfig, description="Optional cross-worker concurrency coordination")
api_read_cache: ApiReadCacheConfig = Field(
default_factory=ApiReadCacheConfig,
description="Optional Redis cache for homepage agents/skills lists; MySQL remains authoritative.",
)
browser_context: BrowserContextConfig = Field(default_factory=BrowserContextConfig, description="Optional browser page capture integration")
knowledge: KnowledgeConfig = Field(default_factory=KnowledgeConfig, description="Product knowledge base (obsidian-wiki compatible vault) configuration")
knowledge_ingest: KnowledgeIngestConfig = Field(default_factory=KnowledgeIngestConfig, description="External knowledge-ingest endpoint configuration")
@@ -472,12 +482,13 @@ def _load_and_cache_app_config(config_path: str | None = None) -> AppConfig:
)
performance = _app_config.runtime_performance
logger.info(
"RUNTIME_PERFORMANCE skill_view_reuse_enabled=%s skill_view_cache_enabled=%s model_timing_nonblocking_enabled=%s asyncio_thread_pool_enabled=%s asyncio_thread_pool_size=%d",
"RUNTIME_PERFORMANCE skill_view_reuse_enabled=%s skill_view_cache_enabled=%s model_timing_nonblocking_enabled=%s asyncio_thread_pool_enabled=%s asyncio_thread_pool_size=%d tool_metrics_enabled=%s",
performance.skill_view_reuse_enabled,
performance.skill_view_cache_enabled,
performance.model_timing_nonblocking_enabled,
performance.asyncio_thread_pool_enabled,
performance.asyncio_thread_pool_size,
performance.tool_metrics_enabled,
)
return _app_config
@@ -44,7 +44,7 @@ from __future__ import annotations
import os
from typing import Literal
from pydantic import BaseModel, Field
from pydantic import BaseModel, Field, field_validator, model_validator
class DatabaseConfig(BaseModel):
@@ -116,6 +116,7 @@ class DatabaseConfig(BaseModel):
default=1800,
description=(
"Recycle pooled connections after this many seconds (postgres/gauss/mysql only). "
"When omitted, PostgreSQL defaults to 300s; other backends default to 1800s. "
"Prevents stale-connection errors when a firewall, pgbouncer, or MySQL "
"wait_timeout silently drops idle connections. pool_pre_ping catches most "
"cases, but pool_recycle is the belt-and-suspenders safety net. "
@@ -123,6 +124,31 @@ class DatabaseConfig(BaseModel):
"connections; total across workers must stay below the DB's max_connections."
),
)
command_timeout: float | None = Field(
default=30,
gt=0,
allow_inf_nan=False,
description=(
"asyncpg command timeout in seconds for the PostgreSQL/openGauss app ORM. "
"Independent of pool_timeout (connection checkout) and the psycopg checkpoint pool. "
"Null disables the command deadline. Not passed to MySQL or SQLite drivers."
),
)
@model_validator(mode="before")
@classmethod
def _postgres_pool_default(cls, values):
# Keep MySQL's existing 1800s default and all explicitly set values.
if isinstance(values, dict) and values.get("backend") == "postgres" and "pool_recycle" not in values:
return {**values, "pool_recycle": 300}
return values
@field_validator("command_timeout", mode="before")
@classmethod
def _reject_boolean_timeout(cls, value):
if isinstance(value, bool):
raise ValueError("command_timeout must be a positive finite number or null")
return value
# -- Derived helpers (not user-configured) --
@@ -1,13 +1,26 @@
"""Browser runtime configuration for generated HTML artifacts."""
"""Fallback browser runtime configuration for generated HTML artifacts.
The settings dialog stores the addresses that new pages actually use. These
fields remain so an older ``config.yaml`` still loads; they apply only when
the dialog has not saved a value yet.
"""
from pydantic import BaseModel, Field
DEFAULT_HTML_RUNTIME_BASE_URL = "http://localhost:8001/api/public/html-runtime"
DEFAULT_HTML_MAP_TILE_URL = "https://mt0.google.com/vt/lyrs=m&hl=zh-CN&x={x}&y={y}&z={z}"
class HtmlRuntimeConfig(BaseModel):
"""Resources used by standalone HTML pages generated in the sandbox."""
"""Legacy fallback used before the settings dialog saves an address."""
base_url: str = Field(
default="http://localhost:8001/api/public/html-runtime",
default=DEFAULT_HTML_RUNTIME_BASE_URL,
min_length=1,
description="Browser-accessible Gateway base URL for generated HTML runtime files",
description="Fallback browser-accessible Gateway base URL for generated HTML runtime files",
)
map_tile_url: str = Field(
default=DEFAULT_HTML_MAP_TILE_URL,
min_length=1,
description="Fallback Leaflet tile URL template used when HTML maps use the map-webpage engine",
)
@@ -135,7 +135,16 @@ class LocalWikiIndexConfig(BaseModel):
default="http://127.0.0.1:5174",
description="DeerFlow frontend base URL used to build directly usable Wiki links in public vector-search results.",
)
auto_sync: bool = True
frontend_base_urls: list[str] = Field(
default_factory=lambda: [
"http://127.0.0.1:5173",
"http://localhost:5173",
"http://127.0.0.1:5174",
"http://localhost:5174",
],
description="Allowed local dev frontend origins; wiki_url uses the request Origin when it matches.",
)
auto_sync: bool = False
sync_interval_seconds: int = Field(default=30, ge=10, le=86400)
sync_page_size: int = Field(default=100, ge=1, le=500)
sync_concurrency: int = Field(default=3, ge=1, le=32)
@@ -174,6 +183,29 @@ class LocalWikiIndexConfig(BaseModel):
raise ValueError("local Wiki index frontend_base_url is required")
return normalized
@field_validator("frontend_base_urls")
@classmethod
def _validate_frontend_base_urls(cls, value: list[str]) -> list[str]:
normalized: list[str] = []
for item in value:
url = normalize_service_url(item)
if url:
normalized.append(url)
return list(dict.fromkeys(normalized))
def resolve_frontend_base_url(self, origin: str | None) -> str:
allowed = set(self.frontend_base_urls)
allowed.add(self.frontend_base_url)
candidate = ""
if origin and str(origin).strip():
try:
candidate = normalize_service_url(origin)
except ValueError:
candidate = ""
if candidate and candidate in allowed:
return candidate
return self.frontend_base_url
@model_validator(mode="before")
@classmethod
def _migrate_legacy_flags(cls, value):
@@ -16,6 +16,18 @@ class WriteFileTransport(StrEnum):
TEXT_BRIDGE = "text_bridge"
class ToolCallTransport(StrEnum):
"""How provider tool calls reach DeerFlow.
``deepseek_dsml`` is an application-side compatibility fallback for
OpenAI-compatible gateways that leave DeepSeek V4/V4.1 DSML in assistant
``content`` instead of returning standard ``message.tool_calls``.
"""
NATIVE = "native"
DEEPSEEK_DSML = "deepseek_dsml"
class ModelConfig(BaseModel):
"""Config section for a model"""
@@ -51,8 +63,22 @@ class ModelConfig(BaseModel):
"text_bridge remains available only for backward compatibility."
),
)
tool_call_transport: ToolCallTransport = Field(
default=ToolCallTransport.NATIVE,
description=(
"How DeerFlow receives provider tool calls. Use deepseek_dsml only "
"for a DeepSeek V4/V4.1 OpenAI-compatible endpoint that leaks raw "
"DSML through assistant content instead of message.tool_calls."
),
)
supports_thinking: bool = Field(default_factory=lambda: False, description="Whether the model supports thinking")
supports_reasoning_effort: bool = Field(default_factory=lambda: False, description="Whether the model supports reasoning effort")
supports_reasoning_effort: bool = Field(
default_factory=lambda: False,
description=(
"Legacy parser compatibility only. Gateway reasoning-effort capability is stored in the "
"model_capabilities database table."
),
)
when_thinking_enabled: dict | None = Field(
default_factory=lambda: None,
description="Extra settings to be passed to the model when thinking is enabled",
@@ -3,8 +3,8 @@
Controls where run events (messages + execution traces) are persisted.
Backends:
- memory: In-memory storage, data lost on restart. Suitable for
development and testing.
- memory: Compatibility name for a bounded-IO local SQLite event spool.
Completed events remain on disk instead of accumulating in process RAM.
- db: SQL database via SQLAlchemy ORM. Provides full query capability.
Suitable for production deployments.
- jsonl: Append-only JSONL files. Lightweight alternative for
@@ -21,7 +21,7 @@ from pydantic import BaseModel, Field
class RunEventsConfig(BaseModel):
backend: Literal["memory", "db", "jsonl"] = Field(
default="memory",
description="Storage backend for run events. 'memory' for development (no persistence), 'db' for production (SQL queries), 'jsonl' for lightweight single-node persistence.",
description="Event backend: 'memory' uses a durable local disk spool, 'db' uses the business SQL database, 'jsonl' uses per-run files.",
)
max_trace_content: int = Field(
default=10240,
@@ -0,0 +1,23 @@
"""Resource budgets shared by users within a Gateway process."""
from pydantic import BaseModel, ConfigDict, Field
class RuntimeContentionConfig(BaseModel):
model_config = ConfigDict(extra="forbid", frozen=True)
completed_run_retention_seconds: int = Field(default=300, ge=60)
run_sweep_seconds: int = Field(default=10, ge=1)
subagent_concurrency: int = Field(default=3, ge=1, le=64)
subagent_per_user: int = Field(default=2, ge=1, le=64)
background_concurrency: int = Field(default=4, ge=1, le=64)
background_per_user: int = Field(default=2, ge=1, le=64)
queue_limit: int = Field(default=256, ge=1, le=10000)
def get_runtime_contention_config() -> RuntimeContentionConfig:
from deerflow.config.app_config import get_app_config
try:
return get_app_config().runtime_contention
except Exception:
return RuntimeContentionConfig()
@@ -3,7 +3,8 @@
Skill-view reuse and cache are on when those keys are absent. Set them to
false in config.yaml to restore a full refresh on every tool call. Model
timing and the Gateway thread pool stay off unless configured. If application
configuration cannot be loaded, every switch stays off.
configuration cannot be loaded, every switch stays off. Tool-call metric writes
are paused by default; explicitly enable them to resume audit statistics.
"""
from pydantic import BaseModel, ConfigDict, Field
@@ -14,6 +15,7 @@ class RuntimePerformanceConfig(BaseModel):
skill_view_reuse_enabled: bool = Field(default=True, description="Reuse one verified skill view within each sandbox tool invocation. Set false to refresh on every call.")
skill_view_cache_enabled: bool = Field(default=True, description="Reuse unchanged skill views after validating source, target and availability versions. Set false to rebuild every call.")
tool_metrics_enabled: bool = Field(default=False, description="Persist tool/skill audit statistics to tool_call_metrics. Disabled by default to avoid competing with interactive database traffic; historical reads remain available.")
model_timing_nonblocking_enabled: bool = Field(default=False, description="Dispatch model timing logs through a dedicated bounded queue, outside the shared tool executor")
asyncio_thread_pool_enabled: bool = Field(default=False, description="Override the Gateway event loop's default executor at startup; disabled preserves Python defaults")
asyncio_thread_pool_size: int = Field(default=100, ge=1, le=1024, strict=True, description="Maximum Gateway executor threads when enabled, unless overridden by the documented environment variables")
@@ -147,16 +147,37 @@ class CustomThemePreset(BaseModel):
return _normalize_optional_hex_color(value, "Theme text color")
WordHeadingNumberingStyle = Literal[
"hybrid_decimal",
"official_chinese",
"decimal",
"chapter_section",
"none",
]
class PromptPrefixSettings(BaseModel):
"""Admin-configurable first-message prompt prefix."""
enabled: bool = Field(default=False, description="Whether the prefix toggle is offered to users at all")
image_generation_unavailable_hint_enabled: bool = Field(
default=False,
description="Tell users image generation is unavailable instead of attempting to generate images.",
)
python_dependency_delete_protection_enabled: bool = Field(
default=False,
description="Inject guidance against standalone Python dependency deletion; installation and replacement remain allowed.",
)
switch_label: str = Field(default="加入立场前缀", description="Toggle label shown in the chat input")
prompt_prefix: str = Field(default="", description="Text prepended to the first user message when enabled")
ordinary_qa_markdown_format_enabled: bool = Field(
default=False,
description="Whether the formal Markdown heading rules are injected into ordinary Q&A system prompts.",
)
word_heading_numbering_style: WordHeadingNumberingStyle = Field(
default="hybrid_decimal",
description="Heading-numbering preset used by every Markdown-to-Word export.",
)
model_config = ConfigDict(extra="ignore")
@@ -251,6 +272,30 @@ class AppearanceSettings(BaseModel):
default="compact",
description="Default interface layout for users who haven't picked their own — compact (hidden) / full (sidebar + header)",
)
default_composer_width_full: int = Field(
default=100,
description="Default ordinary-chat composer width percent for full layout, used when a user has not chosen their own. 100 keeps the built-in width.",
)
default_composer_width_compact: int = Field(
default=100,
description="Default ordinary-chat composer width percent for compact layout, used when a user has not chosen their own. 100 keeps the built-in width.",
)
default_composer_height_full: int = Field(
default=100,
description="Default ordinary-chat composer height percent for full layout, used when a user has not chosen their own. 100 keeps the built-in height.",
)
default_composer_height_compact: int = Field(
default=100,
description="Default ordinary-chat composer height percent for compact layout, used when a user has not chosen their own. 100 keeps the built-in height.",
)
default_agent_composer_width: int = Field(
default=100,
description="Default agent-chat composer width percent, used when a user has not chosen their own. 100 keeps the built-in width; the page still caps it at the parent column.",
)
default_agent_composer_height: int = Field(
default=100,
description="Default agent-chat composer height percent, used when a user has not chosen their own. 100 keeps the built-in height.",
)
compact_shortcut_buttons: list[CompactShortcutButton] = Field(
default_factory=_default_compact_shortcut_buttons,
description="Shortcut buttons rendered before the theme switcher in top bars",
@@ -261,6 +306,36 @@ class AppearanceSettings(BaseModel):
)
model_config = ConfigDict(extra="ignore")
@staticmethod
def _clamp_scale(value: object, low: int, high: int) -> int:
if value is None or value == "":
return 100
try:
parsed = int(round(float(value))) # type: ignore[arg-type]
except (TypeError, ValueError):
return 100
return min(high, max(low, parsed))
@field_validator("default_composer_width_full", "default_composer_width_compact", mode="before")
@classmethod
def _clamp_default_composer_width(cls, value: object) -> int:
return cls._clamp_scale(value, 60, 150)
@field_validator(
"default_composer_height_full",
"default_composer_height_compact",
"default_agent_composer_height",
mode="before",
)
@classmethod
def _clamp_default_composer_height(cls, value: object) -> int:
return cls._clamp_scale(value, 70, 200)
@field_validator("default_agent_composer_width", mode="before")
@classmethod
def _clamp_default_agent_composer_width(cls, value: object) -> int:
return cls._clamp_scale(value, 60, 400)
@field_validator("default_theme")
@classmethod
def _validate_default_theme(cls, value: str) -> str:
@@ -273,6 +348,7 @@ class AppearanceSettings(BaseModel):
class CitationDisplaySettings(BaseModel):
"""System-wide controls for citation mode entry and display cleanup."""
retrieved_files_enabled: bool = True
reference_mode_enabled: bool = Field(
default=False,
description="Whether the reference-mode entry points are exposed to users.",
@@ -463,6 +539,16 @@ def set_module_tag_ids(settings: AgentModuleSettings, key: str, tag_ids: list[st
return settings.model_copy(update={"modules": modules})
class AuthenticationSettings(BaseModel):
"""Public login capabilities; administrator writes take effect immediately."""
password_login_enabled: bool = False
registration_enabled: bool = False
sso_enabled: bool = True
single_session_enabled: bool = False
model_config = ConfigDict(extra="forbid")
class LoginWhitelistSettings(BaseModel):
"""登录白名单总开关(管理员可在「白名单管理」页切换,即时生效、免重启)。
@@ -538,6 +624,37 @@ class MemoryRuntimeSettings(BaseModel):
model_config = ConfigDict(extra="ignore")
class ConversationContextSettings(BaseModel):
"""Deployment-wide short-context policy for chat model requests.
The recent-turn window is deliberately opt-in. When enabled, the lead
agent keeps the persisted conversation intact but sends only the latest
three real user turns (plus their assistant/tool continuations) to the
model. This avoids an extra summarization-model call while preserving the
complete transcript for the UI and checkpoint recovery.
"""
max_run_steps: int = Field(default=1000, ge=20, le=10000, strict=True)
recent_turn_window_enabled: bool = Field(
default=False,
description="Whether model requests use the fixed recent-three-turn sliding window.",
)
max_conversation_rounds: int = Field(
default=20,
ge=1,
le=50,
description="Maximum real user turns allowed in one ordinary chat thread.",
)
use_question_as_title: bool = Field(
default=False,
description=(
"Whether the first user question is used as the final conversation title, "
"skipping post-answer LLM title generation."
),
)
model_config = ConfigDict(extra="ignore")
class WriteFileCompatibilitySettings(BaseModel):
"""Deployment-wide compatibility for gateways buffering tool arguments."""
@@ -551,6 +668,74 @@ class WriteFileCompatibilitySettings(BaseModel):
model_config = ConfigDict(extra="ignore")
class RetryKeepPartialSettings(BaseModel):
"""Keep already-streamed partial answers visible across silent LLM retries.
The LLM error middleware silently retries failed model calls. The failed
attempt never reaches the checkpoint, so the display reconciliation drops
its already-streamed partial answer — users see the text "cleared" when
the retry starts thinking again. Enabled keeps that partial visible in
the chat (frontend display concern, read at stream time).
"""
enabled: bool = Field(
default=True,
description=(
"Keep the already-streamed partial answer visible when a model "
"call is silently retried, instead of letting the display "
"reconciliation clear it."
),
)
model_config = ConfigDict(extra="ignore")
class RoundtableRoomLimitSettings(BaseModel):
"""Global runtime limits for multi-agent roundtable step 2.
The count is deployment-wide: every user's foreground step-2 session and
every non-terminal background roundtable job share one pool. One room is
one session, not one seat and not one model call. Turning the switch off
keeps recording occupancy but stops rejecting new entries.
"""
enabled: bool = Field(
default=True,
description="Whether a full roundtable room pool blocks entry into step 2.",
)
max_rooms: int = Field(
default=3,
ge=1,
le=20,
description="Maximum simultaneous step-2 roundtable rooms across all users.",
)
max_hold_minutes: int = Field(
default=30,
ge=1,
le=1440,
description=(
"Hard lifetime of one step-2 room. Heartbeats preserve network liveness "
"but never extend this deadline."
),
)
max_cycles: int = Field(
default=8,
ge=2,
le=100,
description=(
"Coordinator round that switches to tool-free final closeout. "
"The value is captured when an orchestration starts."
),
)
write_file_compatibility_enabled: bool = Field(
default=False,
description=(
"When true, roundtable leader/seat/step-3 runs use buffered-gateway "
"write_file streaming. Ordinary chat keeps the global switch."
),
)
model_config = ConfigDict(extra="ignore")
class LocalWikiEncoderSettings(BaseModel):
"""Runtime override for the administrator-managed offline Wiki encoder."""
@@ -574,6 +759,81 @@ class LocalWikiEncoderSettings(BaseModel):
model_config = ConfigDict(extra="ignore")
class HtmlMapSettings(BaseModel):
"""Which map engine newly generated HTML pages should use."""
engine: Literal["echarts", "leaflet"] = Field(
default="echarts",
description="echarts uses local GeoJSON; leaflet is the tile-based map webpage",
)
model_config = ConfigDict(extra="ignore")
_APP_IDENTITY_LOGO_FILE_RE = re.compile(r"^logo-[a-f0-9]{32}\.(?:png|jpg|webp)$")
class AppIdentitySettings(BaseModel):
"""Name and logo shown in this application's own chrome.
Empty ``system_name`` and a missing logo keep the built-in title and the
static theme logos. This block is independent of generated-page branding.
"""
system_name: str = Field(default="", max_length=80)
logo_filename: str | None = None
logo_media_type: Literal["image/png", "image/jpeg", "image/webp"] | None = None
logo_version: str | None = None
model_config = ConfigDict(extra="ignore")
@field_validator("system_name")
@classmethod
def _normalize_app_system_name(cls, value: str | None) -> str:
normalized = " ".join(str(value or "").split())
return normalized
@field_validator("logo_filename")
@classmethod
def _validate_app_logo_filename(cls, value: str | None) -> str | None:
if value is None:
return None
normalized = str(value).strip().lower()
if not _APP_IDENTITY_LOGO_FILE_RE.fullmatch(normalized):
raise ValueError("Invalid application logo filename")
return normalized
def app_identity_logo_path(
settings: AppIdentitySettings | None = None,
) -> Path | None:
"""Resolve the uploaded application logo without accepting an arbitrary path."""
current = settings or load_system_settings().app_identity
if not current.logo_filename:
return None
path = runtime_home() / "app-identity" / current.logo_filename
return path if path.is_file() else None
class HtmlRuntimeSettings(BaseModel):
"""Addresses written into newly generated HTML pages.
Empty strings mean the settings dialog has not saved this block. Readers
then keep an older config.yaml value, and finally the built-in default.
"""
base_url: str = Field(
default="",
max_length=2048,
description="Browser-reachable Gateway base for ECharts, G6, Leaflet and generated-page images",
)
map_tile_url: str = Field(
default="",
max_length=2048,
description="Leaflet tile template containing {z}, {x} and {y}",
)
model_config = ConfigDict(extra="ignore")
class StrategyProjectSettings(BaseModel):
"""Global visibility controls for the strategy-project experience."""
@@ -584,11 +844,21 @@ class StrategyProjectSettings(BaseModel):
model_config = ConfigDict(extra="ignore")
class ModelPromptSuffix(BaseModel):
"""Administrator-owned instructions appended to one configured model's system prompt."""
enabled: bool = False
prompt: str = Field(default="", max_length=20000)
model_config = ConfigDict(extra="forbid")
class SystemSettings(BaseModel):
"""Top-level system settings document."""
prompt_prefix: PromptPrefixSettings = Field(default_factory=PromptPrefixSettings)
model_prompt_suffixes: dict[str, ModelPromptSuffix] = Field(default_factory=dict)
login_whitelist: LoginWhitelistSettings = Field(default_factory=LoginWhitelistSettings)
authentication: AuthenticationSettings = Field(default_factory=AuthenticationSettings)
private_square: PrivateSquareSettings = Field(default_factory=PrivateSquareSettings)
publish_squares: PublishSquaresSettings = Field(default_factory=PublishSquaresSettings)
appearance: AppearanceSettings = Field(default_factory=AppearanceSettings)
@@ -601,11 +871,19 @@ class SystemSettings(BaseModel):
concurrency_monitor: ConcurrencyMonitorSettings = Field(default_factory=ConcurrencyMonitorSettings)
model_concurrency: ModelConcurrencySettings = Field(default_factory=ModelConcurrencySettings)
memory: MemoryRuntimeSettings = Field(default_factory=MemoryRuntimeSettings)
conversation_context: ConversationContextSettings = Field(default_factory=ConversationContextSettings)
write_file_compatibility: WriteFileCompatibilitySettings = Field(
default_factory=WriteFileCompatibilitySettings
)
retry_keep_partial: RetryKeepPartialSettings = Field(
default_factory=RetryKeepPartialSettings
)
roundtable_room_limit: RoundtableRoomLimitSettings = Field(default_factory=RoundtableRoomLimitSettings)
local_wiki_encoder: LocalWikiEncoderSettings = Field(default_factory=LocalWikiEncoderSettings)
strategy_projects: StrategyProjectSettings = Field(default_factory=StrategyProjectSettings)
html_map: HtmlMapSettings = Field(default_factory=HtmlMapSettings)
html_runtime: HtmlRuntimeSettings = Field(default_factory=HtmlRuntimeSettings)
app_identity: AppIdentitySettings = Field(default_factory=AppIdentitySettings)
llmwiki: LlmWikiRuntimeOverride = Field(default_factory=LlmWikiRuntimeOverride)
skill_knowledge_auto_sync: SkillKnowledgeAutoSyncSettings = Field(default_factory=SkillKnowledgeAutoSyncSettings)
knowledge_base_product_version: Literal["v1", "v2"] = Field(
@@ -625,9 +903,36 @@ def _settings_path() -> Path:
return home / _SETTINGS_FILE
# ── mtime 缓存 ────────────────────────────────────────────────────────────
# ``load_system_settings`` 处在登录等热路径上(一次登录会读 3 次:登录方式
# 开关、白名单、单会话策略),而每次调用都是「整文件读盘 + JSON 解析 +
# pydantic 校验」。按 (路径, mtime) 缓存解析结果:文件未变直接返回缓存值的
# 深拷贝(调用方可能就地改写返回值,不能共享实例);任何写入——
# ``save_system_settings`` 的原子替换——都会更新 mtime,下一次调用自动重读,
# 无需显式清缓存。
_settings_cache_key: tuple[str, int] | None = None
_settings_cache_value: SystemSettings | None = None
def load_system_settings() -> SystemSettings:
"""Read the on-disk system settings, returning defaults when missing."""
"""Read the on-disk system settings, returning defaults when missing.
Parsed results are cached by (path, mtime); unchanged files return a deep
copy so callers may safely mutate the returned model without poisoning the
shared cache.
"""
global _settings_cache_key, _settings_cache_value
path = _settings_path()
try:
mtime = path.stat().st_mtime_ns
except OSError:
return SystemSettings()
key = (str(path), mtime)
if _settings_cache_value is not None and _settings_cache_key == key:
return _settings_cache_value.model_copy(deep=True)
if not path.is_file():
return SystemSettings()
try:
@@ -635,10 +940,13 @@ def load_system_settings() -> SystemSettings:
if not isinstance(raw, dict):
logger.warning("System settings file %s is not a JSON object; ignoring", path)
return SystemSettings()
return SystemSettings.model_validate(raw)
settings = SystemSettings.model_validate(raw)
except Exception as exc:
logger.warning("Failed to read system settings from %s: %s", path, exc)
return SystemSettings()
_settings_cache_key = key
_settings_cache_value = settings
return settings.model_copy(deep=True)
def get_skill_compression_settings() -> SkillCompressionSettings:
@@ -677,6 +985,15 @@ def get_memory_runtime_settings() -> MemoryRuntimeSettings:
return MemoryRuntimeSettings()
def get_conversation_context_settings() -> ConversationContextSettings:
"""Return the admin-controlled recent-turn policy, failing closed."""
try:
return load_system_settings().conversation_context
except Exception:
logger.debug("Failed to read conversation context setting; keeping the recent-turn window disabled", exc_info=True)
return ConversationContextSettings()
def get_write_file_compatibility_settings() -> WriteFileCompatibilitySettings:
"""Return the global buffered-gateway switch, failing closed."""
@@ -690,8 +1007,18 @@ def get_write_file_compatibility_settings() -> WriteFileCompatibilitySettings:
return WriteFileCompatibilitySettings()
def get_roundtable_room_limit_settings() -> RoundtableRoomLimitSettings:
"""Return the admin-controlled roundtable room cap, defaulting to 3 enabled rooms."""
try:
return load_system_settings().roundtable_room_limit
except Exception:
logger.debug("Failed to read roundtable room limit; keeping the default cap", exc_info=True)
return RoundtableRoomLimitSettings()
def save_system_settings(settings: SystemSettings) -> SystemSettings:
"""Persist the settings document atomically, returning the saved value."""
global _settings_cache_key, _settings_cache_value
path = _settings_path()
payload = settings.model_dump(mode="json")
with _lock:
@@ -703,4 +1030,8 @@ def save_system_settings(settings: SystemSettings) -> SystemSettings:
tmp.replace(path)
finally:
tmp.unlink(missing_ok=True)
# mtime 变化本就足以让缓存失效;这里显式清一次,兜底个别文件系统
# mtime 粒度过粗(同一秒内连续保存两次)读回旧值的极端情况。
_settings_cache_key = None
_settings_cache_value = None
return settings