feat(agent): 移除control_node实例化,新增系统节点命名与人设管理前端
当前阶段只保留regulatory+consciousness两个系统节点,control_node代码保留但不再实例化。 系统节点新增display_name字段支持自定义显示名称,前端新增人设管理Tab支持模板CRUD。 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
+134
-13
@@ -16,7 +16,7 @@
|
||||
from typing import Union
|
||||
from kilostar.utils.ray_hook import ray_actor_hook
|
||||
from fastapi import APIRouter, Depends, Request
|
||||
from pydantic import BaseModel
|
||||
from pydantic import BaseModel, field_validator
|
||||
from kilostar.utils.access import Accessor, TokenData
|
||||
from kilostar.core.postgres_database.model import AgentType
|
||||
from fastapi import HTTPException
|
||||
@@ -36,6 +36,8 @@ class AgentRegister(BaseModel):
|
||||
model_id: str
|
||||
individual_name: str
|
||||
tools: Optional[List[str]] = None
|
||||
custom_system_prompt: Optional[str] = None
|
||||
display_name: Optional[str] = None
|
||||
|
||||
|
||||
class AgentLocalRegister(BaseModel):
|
||||
@@ -50,7 +52,7 @@ class AgentLocalRegister(BaseModel):
|
||||
async def get_system_nodes(
|
||||
_: TokenData = Depends(RoleChecker(allowed_roles=UserAuthority.USER)),
|
||||
):
|
||||
"""返回三大系统节点(regulatory/consciousness/control)当前的持久化配置。"""
|
||||
"""返回两大系统节点(regulatory/consciousness)当前的持久化配置。"""
|
||||
postgres_database = ray_actor_hook("postgres_database").postgres_database
|
||||
configs = await postgres_database.get_all_system_node_configs.remote()
|
||||
return {"system_nodes": configs}
|
||||
@@ -60,7 +62,7 @@ async def get_system_nodes(
|
||||
async def load_agent(
|
||||
agent_register: Union[AgentRegister, AgentLocalRegister],
|
||||
request: Request,
|
||||
_: TokenData = Depends(RoleChecker(allowed_roles=UserAuthority.USER)),
|
||||
_: TokenData = Depends(RoleChecker(allowed_roles=UserAuthority.SUPER_ADMINISTRATOR)),
|
||||
):
|
||||
"""加载/重载某个系统节点的 Agent:先持久化配置,再调用对应节点 Actor 的 ``create_agent``。"""
|
||||
global_state_machine = ray_actor_hook("global_state_machine").global_state_machine
|
||||
@@ -77,10 +79,13 @@ async def load_agent(
|
||||
agent_register.provider_title,
|
||||
agent_register.model_id,
|
||||
agent_register.tools,
|
||||
agent_register.custom_system_prompt,
|
||||
agent_register.display_name,
|
||||
)
|
||||
|
||||
scope = agent_register.individual_name
|
||||
toolsets = await get_all_toolsets_for_scope(scope)
|
||||
custom_prompt = agent_register.custom_system_prompt
|
||||
|
||||
match scope:
|
||||
case "regulatory_node":
|
||||
@@ -92,6 +97,7 @@ async def load_agent(
|
||||
agent_register.tools,
|
||||
toolsets,
|
||||
accept_lang,
|
||||
custom_prompt,
|
||||
)
|
||||
case "consciousness_node":
|
||||
node = ray_actor_hook("consciousness_node").consciousness_node
|
||||
@@ -102,16 +108,7 @@ async def load_agent(
|
||||
agent_register.tools,
|
||||
toolsets,
|
||||
accept_lang,
|
||||
)
|
||||
case "control_node":
|
||||
node = ray_actor_hook("control_node").control_node
|
||||
await node.create_agent.remote(
|
||||
global_state_machine,
|
||||
agent_register.provider_title,
|
||||
agent_register.model_id,
|
||||
agent_register.tools,
|
||||
toolsets,
|
||||
accept_lang,
|
||||
custom_prompt,
|
||||
)
|
||||
case _:
|
||||
pass
|
||||
@@ -122,6 +119,9 @@ async def load_agent(
|
||||
return {"message": "创建成功"}
|
||||
|
||||
|
||||
_VALID_AFFINITIES = {"cpu", "core", "gpu"}
|
||||
|
||||
|
||||
class WorkerIndividualCreate(BaseModel):
|
||||
"""``POST /worker`` 入参:创建一个 Worker Agent 所需的完整配置。"""
|
||||
|
||||
@@ -135,6 +135,14 @@ class WorkerIndividualCreate(BaseModel):
|
||||
bound_skill: Dict[str, List[str]]
|
||||
workspace: List[str]
|
||||
tools: Optional[List[str]] = None
|
||||
node_affinity: str = "cpu"
|
||||
|
||||
@field_validator("node_affinity")
|
||||
@classmethod
|
||||
def _check_affinity(cls, v: str) -> str:
|
||||
if v not in _VALID_AFFINITIES:
|
||||
raise ValueError(f"node_affinity 必须是 cpu/core/gpu,收到: {v}")
|
||||
return v
|
||||
|
||||
|
||||
class WorkerIndividualUpdate(BaseModel):
|
||||
@@ -150,6 +158,14 @@ class WorkerIndividualUpdate(BaseModel):
|
||||
bound_skill: Optional[Dict[str, List[str]]] = None
|
||||
workspace: Optional[List[str]] = None
|
||||
tools: Optional[List[str]] = None
|
||||
node_affinity: Optional[str] = None
|
||||
|
||||
@field_validator("node_affinity")
|
||||
@classmethod
|
||||
def _check_affinity(cls, v: Optional[str]) -> Optional[str]:
|
||||
if v is not None and v not in _VALID_AFFINITIES:
|
||||
raise ValueError(f"node_affinity 必须是 cpu/core/gpu,收到: {v}")
|
||||
return v
|
||||
|
||||
|
||||
@agent_router.post("/worker")
|
||||
@@ -258,3 +274,108 @@ async def delete_worker_individual(
|
||||
)
|
||||
await postgres_database.delete_worker_individual.remote(agent_id=agent_id)
|
||||
return {"message": "success"}
|
||||
|
||||
|
||||
# ──────────────────────────────── Persona Template ────────────────────────────
|
||||
|
||||
class PersonaTemplateCreate(BaseModel):
|
||||
name: str
|
||||
description: str = ""
|
||||
system_prompt: str = ""
|
||||
agent_type: AgentType = "ordinary"
|
||||
provider_title: Optional[str] = None
|
||||
model_id: Optional[str] = None
|
||||
tools: Optional[List[str]] = None
|
||||
tags: Optional[List[str]] = None
|
||||
|
||||
|
||||
class PersonaTemplateUpdate(BaseModel):
|
||||
name: Optional[str] = None
|
||||
description: Optional[str] = None
|
||||
system_prompt: Optional[str] = None
|
||||
agent_type: Optional[AgentType] = None
|
||||
provider_title: Optional[str] = None
|
||||
model_id: Optional[str] = None
|
||||
tools: Optional[List[str]] = None
|
||||
tags: Optional[List[str]] = None
|
||||
|
||||
|
||||
@agent_router.get("/template")
|
||||
async def list_templates(
|
||||
include_builtin: bool = True,
|
||||
token_data: TokenData = Depends(Accessor.get_current_user),
|
||||
):
|
||||
postgres_database = ray_actor_hook("postgres_database").postgres_database
|
||||
templates = await postgres_database.list_templates.remote(
|
||||
owner_id=token_data.user_id, include_builtin=include_builtin
|
||||
)
|
||||
return {"templates": templates}
|
||||
|
||||
|
||||
@agent_router.post("/template")
|
||||
async def create_template(
|
||||
data: PersonaTemplateCreate,
|
||||
token_data: TokenData = Depends(RoleChecker(allowed_roles=UserAuthority.USER)),
|
||||
):
|
||||
postgres_database = ray_actor_hook("postgres_database").postgres_database
|
||||
tpl = await postgres_database.add_template.remote(
|
||||
**data.model_dump(), owner_id=token_data.user_id, is_builtin=False
|
||||
)
|
||||
return {"message": "success", "template_id": tpl.template_id}
|
||||
|
||||
|
||||
@agent_router.put("/template/{template_id}")
|
||||
async def update_template(
|
||||
template_id: str,
|
||||
data: PersonaTemplateUpdate,
|
||||
token_data: TokenData = Depends(Accessor.get_current_user),
|
||||
):
|
||||
postgres_database = ray_actor_hook("postgres_database").postgres_database
|
||||
tpl = await postgres_database.get_template.remote(template_id)
|
||||
if not tpl:
|
||||
raise HTTPException(status_code=404, detail="Template not found")
|
||||
if not tpl.is_builtin and tpl.owner_id != token_data.user_id:
|
||||
raise HTTPException(status_code=403, detail="Forbidden")
|
||||
updated = await postgres_database.update_template.remote(
|
||||
template_id, **data.model_dump(exclude_unset=True)
|
||||
)
|
||||
return {"message": "success", "template": updated}
|
||||
|
||||
|
||||
@agent_router.delete("/template/{template_id}")
|
||||
async def delete_template(
|
||||
template_id: str,
|
||||
token_data: TokenData = Depends(Accessor.get_current_user),
|
||||
):
|
||||
postgres_database = ray_actor_hook("postgres_database").postgres_database
|
||||
tpl = await postgres_database.get_template.remote(template_id)
|
||||
if not tpl:
|
||||
raise HTTPException(status_code=404, detail="Template not found")
|
||||
if tpl.is_builtin or tpl.owner_id != token_data.user_id:
|
||||
raise HTTPException(status_code=403, detail="Forbidden")
|
||||
await postgres_database.delete_template.remote(template_id)
|
||||
return {"message": "success"}
|
||||
|
||||
|
||||
@agent_router.post("/worker/from-template/{template_id}")
|
||||
async def create_worker_from_template(
|
||||
template_id: str,
|
||||
token_data: TokenData = Depends(RoleChecker(allowed_roles=UserAuthority.USER)),
|
||||
):
|
||||
"""从人设模板快速创建一个 Worker Agent,字段直接从模板复制。"""
|
||||
postgres_database = ray_actor_hook("postgres_database").postgres_database
|
||||
tpl = await postgres_database.get_template.remote(template_id)
|
||||
if not tpl:
|
||||
raise HTTPException(status_code=404, detail="Template not found")
|
||||
worker = await postgres_database.add_worker_individual.remote(
|
||||
agent_name=tpl.name,
|
||||
agent_type=tpl.agent_type,
|
||||
description=tpl.description,
|
||||
system_prompt=tpl.system_prompt,
|
||||
provider_title=tpl.provider_title or "",
|
||||
model_id=tpl.model_id or "",
|
||||
tools=tpl.tools or [],
|
||||
owner_id=token_data.user_id,
|
||||
template_origin_id=template_id,
|
||||
)
|
||||
return {"message": "success", "agent_id": worker.agent_id}
|
||||
|
||||
Reference in New Issue
Block a user