feat: 插件管理及域名和智能体对应
This commit is contained in:
1 parent
eb61b332a2
commit
e9e4c181b9
9 files changed
+414
-27
No files matched your search
@@ -4,6 +4,7 @@ from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from typing import Literal
|
||||
from urllib.parse import urlparse
|
||||
|
||||
from fastapi import APIRouter, HTTPException, Request
|
||||
from pydantic import BaseModel, Field
|
||||
@@ -14,6 +15,8 @@ from deerflow.config import get_app_config
|
||||
from deerflow.config.system_settings import (
|
||||
LeaderboardSettings,
|
||||
ModelConcurrencySettings,
|
||||
PluginDomainAgentMapping,
|
||||
PluginManagementSettings,
|
||||
PromptPrefixSettings,
|
||||
PublishSquare,
|
||||
PublishSquaresSettings,
|
||||
@@ -43,6 +46,21 @@ class PromptPrefixUpdateRequest(BaseModel):
|
||||
prompt_prefix: str | None = None
|
||||
|
||||
|
||||
class PluginDomainAgentMappingItem(BaseModel):
|
||||
id: str = Field(..., description="Stable row id")
|
||||
domain: str = Field(..., description="Normalized host matched against pageContext.url")
|
||||
agent_id: str = Field(..., description="Agent id selected for the host")
|
||||
enabled: bool = Field(default=True, description="Whether the mapping is active")
|
||||
|
||||
|
||||
class PluginManagementResponse(BaseModel):
|
||||
domain_agent_mappings: list[PluginDomainAgentMappingItem] = Field(default_factory=list)
|
||||
|
||||
|
||||
class PluginManagementUpdateRequest(BaseModel):
|
||||
domain_agent_mappings: list[PluginDomainAgentMappingItem] | None = None
|
||||
|
||||
|
||||
class PrivateSquareStatusResponse(BaseModel):
|
||||
"""Status of the private-square feature, readable by any logged-in user.
|
||||
|
||||
@@ -91,6 +109,34 @@ def _to_response(settings: PromptPrefixSettings) -> PromptPrefixResponse:
|
||||
)
|
||||
|
||||
|
||||
def _normalize_plugin_domain(value: str | None) -> str:
|
||||
raw = (value or "").strip()
|
||||
if not raw:
|
||||
return ""
|
||||
candidate = raw if "://" in raw else f"https://{raw}"
|
||||
try:
|
||||
parsed = urlparse(candidate)
|
||||
except Exception:
|
||||
return ""
|
||||
return (parsed.netloc or parsed.path or "").strip().lower()
|
||||
|
||||
|
||||
def _plugin_management_to_response(
|
||||
settings: PluginManagementSettings,
|
||||
) -> PluginManagementResponse:
|
||||
return PluginManagementResponse(
|
||||
domain_agent_mappings=[
|
||||
PluginDomainAgentMappingItem(
|
||||
id=item.id,
|
||||
domain=item.domain,
|
||||
agent_id=item.agent_id,
|
||||
enabled=item.enabled,
|
||||
)
|
||||
for item in settings.domain_agent_mappings
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
async def _require_admin(request: Request) -> None:
|
||||
user = await get_optional_user_from_request(request)
|
||||
if user is None:
|
||||
@@ -117,6 +163,50 @@ async def update_prompt_prefix(body: PromptPrefixUpdateRequest, request: Request
|
||||
return _to_response(saved.prompt_prefix)
|
||||
|
||||
|
||||
@router.get("/plugin-management", response_model=PluginManagementResponse)
|
||||
async def get_plugin_management() -> PluginManagementResponse:
|
||||
"""Return the iframe plugin domain -> agent mappings."""
|
||||
return _plugin_management_to_response(load_system_settings().plugin_management)
|
||||
|
||||
|
||||
@router.put("/plugin-management", response_model=PluginManagementResponse)
|
||||
async def update_plugin_management(
|
||||
body: PluginManagementUpdateRequest, request: Request
|
||||
) -> PluginManagementResponse:
|
||||
"""Replace the iframe plugin domain -> agent mappings. Admin only."""
|
||||
from uuid import uuid4
|
||||
|
||||
await _require_admin(request)
|
||||
|
||||
current = load_system_settings()
|
||||
mappings: list[PluginDomainAgentMapping] = []
|
||||
seen_domains: set[str] = set()
|
||||
for item in body.domain_agent_mappings or []:
|
||||
domain = _normalize_plugin_domain(item.domain)
|
||||
agent_id = (item.agent_id or "").strip()
|
||||
if not domain or not agent_id:
|
||||
continue
|
||||
if domain in seen_domains:
|
||||
continue
|
||||
seen_domains.add(domain)
|
||||
mappings.append(
|
||||
PluginDomainAgentMapping(
|
||||
id=(item.id or "").strip() or uuid4().hex,
|
||||
domain=domain,
|
||||
agent_id=agent_id,
|
||||
enabled=bool(item.enabled),
|
||||
)
|
||||
)
|
||||
|
||||
updated = current.plugin_management.model_copy(
|
||||
update={"domain_agent_mappings": mappings}
|
||||
)
|
||||
saved = save_system_settings(
|
||||
current.model_copy(update={"plugin_management": updated})
|
||||
)
|
||||
return _plugin_management_to_response(saved.plugin_management)
|
||||
|
||||
|
||||
class SkillCompressionResponse(BaseModel):
|
||||
enabled: bool = Field(..., description="Whether skill compression is active")
|
||||
search_top_n: int = Field(..., description="Max skills returned by the search_skills tool")
|
||||
|
||||
@@ -206,6 +206,26 @@ class AgentModuleSettings(BaseModel):
|
||||
model_config = ConfigDict(extra="ignore")
|
||||
|
||||
|
||||
class PluginDomainAgentMapping(BaseModel):
|
||||
"""One configured host -> agent mapping for iframe chat auto-selection."""
|
||||
|
||||
id: str = Field(..., description="Stable row id for client-side editing")
|
||||
domain: str = Field(..., description="Normalized host matched against pageContext.url")
|
||||
agent_id: str = Field(..., description="Agent id selected when the host matches")
|
||||
enabled: bool = Field(default=True, description="Whether this mapping is active")
|
||||
model_config = ConfigDict(extra="ignore")
|
||||
|
||||
|
||||
class PluginManagementSettings(BaseModel):
|
||||
"""Admin-configurable iframe plugin mappings."""
|
||||
|
||||
domain_agent_mappings: list[PluginDomainAgentMapping] = Field(
|
||||
default_factory=list,
|
||||
description="Ordered host -> agent mappings used by the iframe chat entry page.",
|
||||
)
|
||||
model_config = ConfigDict(extra="ignore")
|
||||
|
||||
|
||||
def get_module_tag_ids(settings: AgentModuleSettings, key: str) -> list[str]:
|
||||
"""Return the configured agent-tag ids for ``key`` (empty when unset)."""
|
||||
return list(settings.modules.get(key, []))
|
||||
@@ -280,6 +300,7 @@ class SystemSettings(BaseModel):
|
||||
citation_display: CitationDisplaySettings = Field(default_factory=CitationDisplaySettings)
|
||||
leaderboard: LeaderboardSettings = Field(default_factory=LeaderboardSettings)
|
||||
skill_compression: SkillCompressionSettings = Field(default_factory=SkillCompressionSettings)
|
||||
plugin_management: PluginManagementSettings = Field(default_factory=PluginManagementSettings)
|
||||
agent_modules: AgentModuleSettings = Field(default_factory=AgentModuleSettings)
|
||||
concurrency_monitor: ConcurrencyMonitorSettings = Field(default_factory=ConcurrencyMonitorSettings)
|
||||
model_concurrency: ModelConcurrencySettings = Field(default_factory=ModelConcurrencySettings)
|
||||
|
||||
Reference in new issue
Block a user