feat: 清理 control_node + 引入 task 一等公民
- control_node 标注 DEPRECATED:保留目录壳子供未来远程探针节点复用,删除调用路径与相关测试
- 新增 task 表:极简元数据持久化 regulatory_node 完成的短任务(出报告/写文件/查询整理)
- regulatory_node 自标注:MessageResponse 扩展 task_action/title/summary,_run 末尾非阻塞落库
- query_task_list 改查 task 表,符合用户对"任务列表"的直觉,与 workflow 体系解耦
- 新增 /api/v1/task/list|/{id} 只读 API(task 由 regulatory 内部触发,不开放对外创建)
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -36,6 +36,7 @@ from .resource import resource_router
|
||||
from .workflow import workflow_router
|
||||
from .chat import chat_router
|
||||
from .plugin import plugin_router
|
||||
from .task import task_router
|
||||
from kilostar.utils.error import (
|
||||
KiloStarError,
|
||||
BusinessError,
|
||||
@@ -105,6 +106,7 @@ app.include_router(agent_router) # agent路径
|
||||
app.include_router(workflow_router) # workflow路径
|
||||
app.include_router(chat_router) # chat路径
|
||||
app.include_router(plugin_router) # plugin路径
|
||||
app.include_router(task_router) # 短任务路径
|
||||
|
||||
|
||||
@app.exception_handler(BusinessError)
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
# Copyright 2026 zhaoxi826
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
"""Task API:管控节点短任务的查询接口。
|
||||
|
||||
task 由 regulatory_node 在完成短任务时内部建立,因此本路由只暴露读取,
|
||||
不开放对外创建。
|
||||
"""
|
||||
|
||||
from typing import Optional
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
|
||||
from kilostar.utils.access import Accessor, TokenData
|
||||
from kilostar.utils.ray_hook import ray_actor_hook
|
||||
|
||||
task_router = APIRouter(prefix="/api/v1/task", tags=["task"])
|
||||
|
||||
|
||||
@task_router.get("/list")
|
||||
async def list_tasks(
|
||||
status: Optional[str] = None,
|
||||
limit: int = 20,
|
||||
offset: int = 0,
|
||||
token_data: TokenData = Depends(Accessor.get_current_user),
|
||||
):
|
||||
"""列出当前用户的所有短任务,按时间倒序。"""
|
||||
postgres_database = ray_actor_hook("postgres_database").postgres_database
|
||||
tasks = await postgres_database.list_tasks_by_user.remote(
|
||||
user_id=token_data.user_id,
|
||||
status=status,
|
||||
limit=limit,
|
||||
offset=offset,
|
||||
)
|
||||
return {"tasks": tasks, "total": len(tasks)}
|
||||
|
||||
|
||||
@task_router.get("/{task_id}")
|
||||
async def get_task(
|
||||
task_id: str,
|
||||
token_data: TokenData = Depends(Accessor.get_current_user),
|
||||
):
|
||||
"""按 task_id 读取一条 task 详情。仅 owner 可访问。"""
|
||||
postgres_database = ray_actor_hook("postgres_database").postgres_database
|
||||
task = await postgres_database.get_task.remote(task_id)
|
||||
if not task:
|
||||
raise HTTPException(status_code=404, detail="task not found")
|
||||
if task.get("user_id") != token_data.user_id:
|
||||
raise HTTPException(status_code=403, detail="forbidden")
|
||||
return task
|
||||
@@ -27,10 +27,12 @@ from kilostar.utils.prompts import agent_prompt
|
||||
|
||||
@actor_class
|
||||
class ControlNode:
|
||||
"""ControlNode(控制节点):工作流中具体子任务的执行 Actor。
|
||||
"""ControlNode(控制节点):**已废弃**——名字保留给未来的远程探针/系统控制节点。
|
||||
|
||||
它把 ConsciousnessNode 编排出的 ``workflow_step`` 拿来当作输入,借助
|
||||
pydantic-ai Agent + 已绑定的工具集合产出 ``ForWorkflow`` 结构化输出。
|
||||
历史:早期设计里它是工作流的"单步执行 actor",但 workflow_engine 的 Dispatch
|
||||
最终只识别 ``consciousness_node`` 和 ``skill_individual``,本类从未真正被调用过。
|
||||
保留目录与类壳子,避免改名带来的 git 历史断层;**不要新增对它的依赖**。
|
||||
待远程探针/监控流子项目启动时,本目录将被重写为远程机器控制节点。
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
|
||||
@@ -212,7 +212,42 @@ class RegulatoryNode:
|
||||
response: MessageResponse = agent_response.output
|
||||
response.platform = platform
|
||||
response.platform_id = payload.platform_id
|
||||
await self._maybe_persist_task(payload, response)
|
||||
return response
|
||||
except Exception as e:
|
||||
self.logger.exception(f"RegulatoryNode._run failed: {e}")
|
||||
return None
|
||||
return None
|
||||
|
||||
async def _maybe_persist_task(
|
||||
self, payload: MessageRequest, response: MessageResponse
|
||||
) -> None:
|
||||
"""LLM 自标注 task_action=create_task 时落一条 task 记录。
|
||||
|
||||
失败不抛错——task 表是辅助元数据,不能拖垮主回复链路。
|
||||
"""
|
||||
if response.task_action != "create_task":
|
||||
return
|
||||
if not response.task_title or not response.task_summary:
|
||||
self.logger.warning(
|
||||
"task_action=create_task 但 title/summary 为空,跳过落库"
|
||||
)
|
||||
return
|
||||
try:
|
||||
import uuid
|
||||
from kilostar.utils.ray_hook import ray_actor_hook
|
||||
|
||||
postgres_database = ray_actor_hook("postgres_database").postgres_database
|
||||
task_id = uuid.uuid4().hex
|
||||
chat_id = payload.platform_id if payload.platform == "client" else None
|
||||
await postgres_database.create_task.remote(
|
||||
task_id=task_id,
|
||||
user_id=payload.user_name,
|
||||
command=payload.message,
|
||||
title=response.task_title,
|
||||
chat_id=chat_id,
|
||||
status="completed",
|
||||
result_summary=response.task_summary,
|
||||
artifact_refs=None,
|
||||
)
|
||||
except Exception as e:
|
||||
self.logger.warning(f"persist task failed (non-fatal): {e}")
|
||||
@@ -62,3 +62,15 @@ class MessageResponse(RegulatoryNodeResponse):
|
||||
platform: Optional[Literal["client", "onebot"]] = Field(description="系统自动填入的platform")
|
||||
platform_id: Optional[str] = Field(description="系统自动填入的platform_id")
|
||||
reply_message: str = Field(...,description="模型回复的消息")
|
||||
task_action: Optional[Literal["create_task"]] = Field(
|
||||
default=None,
|
||||
description="本次回复是否完成了一个值得记录的短任务。生成文件/出报告/查询整理等填 'create_task',闲聊或简单问答留空。",
|
||||
)
|
||||
task_title: Optional[str] = Field(
|
||||
default=None,
|
||||
description="task 的简短标题(task_action=create_task 时必填,<=80 字)",
|
||||
)
|
||||
task_summary: Optional[str] = Field(
|
||||
default=None,
|
||||
description="task 的结果摘要(task_action=create_task 时必填,描述产出与去向)",
|
||||
)
|
||||
|
||||
@@ -37,6 +37,7 @@ from kilostar.core.postgres_database.model.system_event_log import SystemEventLo
|
||||
from kilostar.core.postgres_database.model.persona_template import PersonaTemplate
|
||||
from kilostar.core.postgres_database.model.org_task import OrgTask
|
||||
from kilostar.core.postgres_database.model.org_task_event import OrgTaskEvent
|
||||
from kilostar.core.postgres_database.model.task import Task
|
||||
|
||||
# 兼容旧代码的别名
|
||||
Provider = ProviderModel
|
||||
@@ -69,5 +70,6 @@ __all__ = [
|
||||
"PersonaTemplate",
|
||||
"OrgTask",
|
||||
"OrgTaskEvent",
|
||||
"Task",
|
||||
"AgentType",
|
||||
]
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
# Copyright 2026 zhaoxi826
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
"""Task:管控节点(regulatory_node)完成的短任务记录。
|
||||
|
||||
与 workflow 不同,task 是上下文内能完成的轻量任务(写文件/查询/出报告等),
|
||||
表里只存最终元数据 + 结果摘要 + 关联 artifact,不入库执行过程。
|
||||
"""
|
||||
|
||||
from sqlalchemy import String, DateTime, Text, func
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
from sqlalchemy.dialects.postgresql import JSONB
|
||||
from .base import BaseDataModel
|
||||
|
||||
|
||||
class Task(BaseDataModel):
|
||||
__tablename__ = "task"
|
||||
|
||||
task_id: Mapped[str] = mapped_column(
|
||||
String(64), primary_key=True, comment="任务唯一 ID(UUID)"
|
||||
)
|
||||
user_id: Mapped[str] = mapped_column(
|
||||
String(64), index=True, comment="所属用户 ID"
|
||||
)
|
||||
chat_id: Mapped[str | None] = mapped_column(
|
||||
String(64), index=True, nullable=True, comment="所属对话(如有)"
|
||||
)
|
||||
command: Mapped[str] = mapped_column(
|
||||
Text, comment="用户原始指令"
|
||||
)
|
||||
title: Mapped[str] = mapped_column(
|
||||
String(255), comment="任务简短标题(LLM 生成)"
|
||||
)
|
||||
status: Mapped[str] = mapped_column(
|
||||
String(20), index=True, default="completed",
|
||||
comment="running / completed / failed",
|
||||
)
|
||||
result_summary: Mapped[str | None] = mapped_column(
|
||||
Text, nullable=True, comment="完成后的结果摘要"
|
||||
)
|
||||
artifact_refs: Mapped[list | None] = mapped_column(
|
||||
JSONB, nullable=True, comment="关联的 artifact url 列表"
|
||||
)
|
||||
created_at: Mapped[str] = mapped_column(
|
||||
DateTime(timezone=True), server_default=func.now(), index=True
|
||||
)
|
||||
updated_at: Mapped[str] = mapped_column(
|
||||
DateTime(timezone=True), server_default=func.now(), onupdate=func.now()
|
||||
)
|
||||
@@ -0,0 +1,104 @@
|
||||
# Copyright 2026 zhaoxi826
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
"""Task DAO:管控节点短任务的最小持久化层。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import List, Optional
|
||||
from sqlalchemy import select, desc, update
|
||||
from sqlalchemy.ext.asyncio import async_sessionmaker, AsyncSession
|
||||
|
||||
from kilostar.core.postgres_database.model.task import Task
|
||||
from kilostar.core.postgres_database.database_exception import database_exception
|
||||
|
||||
|
||||
class TaskDatabase:
|
||||
def __init__(self, async_session_maker: async_sessionmaker[AsyncSession]):
|
||||
self.async_session_maker = async_session_maker
|
||||
|
||||
@database_exception
|
||||
async def create_task(
|
||||
self,
|
||||
task_id: str,
|
||||
user_id: str,
|
||||
command: str,
|
||||
title: str,
|
||||
chat_id: Optional[str] = None,
|
||||
status: str = "completed",
|
||||
result_summary: Optional[str] = None,
|
||||
artifact_refs: Optional[list] = None,
|
||||
) -> None:
|
||||
async with self.async_session_maker() as session:
|
||||
row = Task(
|
||||
task_id=task_id,
|
||||
user_id=user_id,
|
||||
chat_id=chat_id,
|
||||
command=command,
|
||||
title=title,
|
||||
status=status,
|
||||
result_summary=result_summary,
|
||||
artifact_refs=artifact_refs,
|
||||
)
|
||||
session.add(row)
|
||||
await session.commit()
|
||||
|
||||
@database_exception
|
||||
async def update_status(
|
||||
self,
|
||||
task_id: str,
|
||||
status: str,
|
||||
result_summary: Optional[str] = None,
|
||||
) -> None:
|
||||
async with self.async_session_maker() as session:
|
||||
values = {"status": status}
|
||||
if result_summary is not None:
|
||||
values["result_summary"] = result_summary
|
||||
stmt = update(Task).where(Task.task_id == task_id).values(**values)
|
||||
await session.execute(stmt)
|
||||
await session.commit()
|
||||
|
||||
@database_exception
|
||||
async def get_task(self, task_id: str) -> Optional[dict]:
|
||||
async with self.async_session_maker() as session:
|
||||
stmt = select(Task).where(Task.task_id == task_id)
|
||||
row = (await session.execute(stmt)).scalar_one_or_none()
|
||||
if not row:
|
||||
return None
|
||||
return _row_to_dict(row)
|
||||
|
||||
@database_exception
|
||||
async def list_tasks_by_user(
|
||||
self,
|
||||
user_id: str,
|
||||
status: Optional[str] = None,
|
||||
limit: int = 20,
|
||||
offset: int = 0,
|
||||
) -> List[dict]:
|
||||
async with self.async_session_maker() as session:
|
||||
stmt = select(Task).where(Task.user_id == user_id)
|
||||
if status:
|
||||
stmt = stmt.where(Task.status == status)
|
||||
stmt = stmt.order_by(desc(Task.created_at)).offset(offset).limit(limit)
|
||||
rows = (await session.execute(stmt)).scalars().all()
|
||||
return [_row_to_dict(r) for r in rows]
|
||||
|
||||
|
||||
def _row_to_dict(row: Task) -> dict:
|
||||
return {
|
||||
"task_id": row.task_id,
|
||||
"user_id": row.user_id,
|
||||
"chat_id": row.chat_id,
|
||||
"command": row.command,
|
||||
"title": row.title,
|
||||
"status": row.status,
|
||||
"result_summary": row.result_summary,
|
||||
"artifact_refs": row.artifact_refs or [],
|
||||
"created_at": str(row.created_at) if row.created_at else None,
|
||||
"updated_at": str(row.updated_at) if row.updated_at else None,
|
||||
}
|
||||
@@ -59,6 +59,7 @@ from .module.custom_toolset import CustomToolsetDatabase
|
||||
from .module.system_event_log import SystemEventLogDatabase
|
||||
from .module.persona_template import PersonaTemplateDatabase
|
||||
from .module.org_task import OrgTaskDatabase
|
||||
from .module.task import TaskDatabase
|
||||
|
||||
|
||||
@actor_class
|
||||
@@ -93,6 +94,7 @@ class PostgresDatabase:
|
||||
self._system_event_log_database = SystemEventLogDatabase(self.async_session_maker)
|
||||
self._persona_template_database = PersonaTemplateDatabase(self.async_session_maker)
|
||||
self._org_task_database = OrgTaskDatabase(self.async_session_maker)
|
||||
self._task_database = TaskDatabase(self.async_session_maker)
|
||||
|
||||
self.ready_event = asyncio.Event()
|
||||
|
||||
@@ -487,3 +489,49 @@ class PostgresDatabase:
|
||||
async def query_org_events(self, task_id: str, limit=200):
|
||||
await self.ready_event.wait()
|
||||
return await self._org_task_database.query_events(task_id, limit)
|
||||
|
||||
# Task Methods(管控节点短任务)
|
||||
async def create_task(
|
||||
self,
|
||||
task_id: str,
|
||||
user_id: str,
|
||||
command: str,
|
||||
title: str,
|
||||
chat_id: str | None = None,
|
||||
status: str = "completed",
|
||||
result_summary: str | None = None,
|
||||
artifact_refs: list | None = None,
|
||||
):
|
||||
await self.ready_event.wait()
|
||||
return await self._task_database.create_task(
|
||||
task_id=task_id,
|
||||
user_id=user_id,
|
||||
command=command,
|
||||
title=title,
|
||||
chat_id=chat_id,
|
||||
status=status,
|
||||
result_summary=result_summary,
|
||||
artifact_refs=artifact_refs,
|
||||
)
|
||||
|
||||
async def update_task_status(
|
||||
self, task_id: str, status: str, result_summary: str | None = None
|
||||
):
|
||||
await self.ready_event.wait()
|
||||
return await self._task_database.update_status(task_id, status, result_summary)
|
||||
|
||||
async def get_task(self, task_id: str):
|
||||
await self.ready_event.wait()
|
||||
return await self._task_database.get_task(task_id)
|
||||
|
||||
async def list_tasks_by_user(
|
||||
self,
|
||||
user_id: str,
|
||||
status: str | None = None,
|
||||
limit: int = 20,
|
||||
offset: int = 0,
|
||||
):
|
||||
await self.ready_event.wait()
|
||||
return await self._task_database.list_tasks_by_user(
|
||||
user_id=user_id, status=status, limit=limit, offset=offset
|
||||
)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
"""把组织包装成 cabinet 可调用的高阶 tool。
|
||||
|
||||
每个组织 → 一个 ``dispatch_to_<org>(task_description)`` 工具。
|
||||
ConsciousnessNode/ControlNode 通过这个工具向部门派单,等待部门完成。
|
||||
RegulatoryNode/ConsciousnessNode 通过这个工具向部门派单,等待部门完成。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
@@ -18,7 +18,13 @@ _PROMPTS: Dict[str, Dict[str, str]] = {
|
||||
"1. 准确理解用户的意图,提供专业、友好且有帮助的回复。\n"
|
||||
"2. 如果你有可用工具,可以主动调用工具来辅助回答(如搜索、文件操作等)。\n"
|
||||
"3. 如果你收到工作流的执行报告,请将其转化为面向用户的清晰总结。\n"
|
||||
"4. 保持回复简洁、有结构,避免冗余信息。\n"
|
||||
"4. 保持回复简洁、有结构,避免冗余信息。\n\n"
|
||||
"【关于短任务(task)】\n"
|
||||
"如果本次回复完成了一个值得记录的'短任务'(生成文件/出报告/查询整理资料/写代码片段等具体产出),\n"
|
||||
"请把 task_action 设为 'create_task',并填写:\n"
|
||||
"- task_title:简短标题(<=80 字,例如 'Python 学习计划')\n"
|
||||
"- task_summary:结果摘要(说明产出了什么、附件去向)\n"
|
||||
"闲聊、打招呼、纯问答这类不留下产出物的回复,task_action 留空(None)。\n"
|
||||
"请保持专业、友好的沟通风格。"
|
||||
),
|
||||
"en": (
|
||||
@@ -28,7 +34,13 @@ _PROMPTS: Dict[str, Dict[str, str]] = {
|
||||
"1. Accurately understand user intent and provide professional, friendly, and helpful replies.\n"
|
||||
"2. If tools are available, proactively use them to assist your responses (e.g., search, file operations).\n"
|
||||
"3. If you receive a workflow execution report, convert it into a clear user-facing summary.\n"
|
||||
"4. Keep responses concise, well-structured, and free of redundancy.\n"
|
||||
"4. Keep responses concise, well-structured, and free of redundancy.\n\n"
|
||||
"[About short tasks]\n"
|
||||
"If this reply completes a worth-recording short task (generating files / writing reports / collecting information / producing code snippets etc.),\n"
|
||||
"set task_action to 'create_task' and fill:\n"
|
||||
"- task_title: short title (<=80 chars, e.g. 'Python learning plan')\n"
|
||||
"- task_summary: result summary (what was produced, where attachments live)\n"
|
||||
"Leave task_action empty for chit-chat / greetings / plain Q&A that produce no artifact.\n"
|
||||
"Maintain a professional and friendly communication style."
|
||||
),
|
||||
},
|
||||
@@ -72,6 +84,8 @@ _PROMPTS: Dict[str, Dict[str, str]] = {
|
||||
"Ensure all output is logical, rigorous, and high-quality."
|
||||
),
|
||||
},
|
||||
# DEPRECATED: control_node 当前未被任何路径调用,保留 prompt 占位以便未来
|
||||
# 改造为远程探针/系统控制节点时直接复用 key。
|
||||
"control_node": {
|
||||
"zh": (
|
||||
"你叫kilostar,是一个多智能体AI助手系统中的【控制节点 (Control Node)】。\n"
|
||||
|
||||
Reference in New Issue
Block a user