chore: 迁移服务器前同步本地修改

This commit is contained in:
2026-08-05 21:26:13 +08:00
parent aa47a19e98
commit 1a914a2517
73 changed files with 908 additions and 1961 deletions
@@ -14,7 +14,6 @@
from typing import Union, overload
from kilostar.utils.ray_compat import actor_class
from kilostar.core.individual.consciousness_node.template import (
ConsciousnessNodeDeps,
ForregulatoryNode,
@@ -28,11 +27,9 @@ from pydantic_ai import Agent, RunContext
from kilostar.core.global_state_machine.global_state_machine import GlobalStateMachine
from kilostar.core.global_state_machine.model_provider.base_provider import Provider
from kilostar.adapter.model_adapter.agent_factory import AgentFactory
from kilostar.utils.ray_hook import ray_actor_hook
from kilostar.utils.prompts import agent_prompt
@actor_class
class ConsciousnessNode:
def __init__(self) -> None:
from kilostar.utils.logger import get_logger
@@ -134,8 +131,10 @@ class ConsciousnessNode:
f"ConsciousnessNode: 开始为 trace_id {trace_id} 设计工作流。原始命令:{command}"
)
# 获取可用技能 (示例)
postgres_database = ray_actor_hook("postgres_database").postgres_database
skills_entities = await postgres_database.get_all_worker_individual.remote()
from kilostar.utils.actor import get_postgres
postgres_database = get_postgres()
skills_entities = await postgres_database.get_all_worker_individual()
available_skills = []
if skills_entities:
for skill in skills_entities:
@@ -151,10 +150,10 @@ class ConsciousnessNode:
original_command=command, available_skills=available_skills
)
global_workflow_manager = ray_actor_hook(
"global_workflow_manager"
).global_workflow_manager
await global_workflow_manager.put_pending.remote(
from kilostar.utils.actor import get_gwm
global_workflow_manager = get_gwm()
await global_workflow_manager.put_pending(
trace_id, "正在为您构建并规划工作流任务节点,请稍候..."
)
@@ -164,20 +163,21 @@ class ConsciousnessNode:
workflow = result.workflow
workflow.trace_id = trace_id
await global_workflow_manager.put_pending.remote(
await global_workflow_manager.put_pending(
trace_id, "工作流构建完成,即将开始执行!"
)
# 直接以 ray task 形式 fire workflow,不再经过 WorkflowRunningEngine 这层中转:
# workflow 是一次性、有头有尾的执行,task 语义比常驻 actor 更贴。
# 以后台任务形式 fire workflow:workflow 是一次性、有头有尾的
# 执行,跑完即结束;持久化交给 PostgresStatePersistence 保证可恢复。
from kilostar.utils.actor import spawn_background
from kilostar.core.work.workflow.workflow_engine import run_workflow_task
run_workflow_task.remote(workflow.model_dump(), trace_id)
spawn_background(run_workflow_task(workflow.model_dump(), trace_id))
else:
await global_workflow_manager.put_pending.remote(
await global_workflow_manager.put_pending(
trace_id, "很抱歉,工作流生成失败。"
)
await postgres_database.update_workflow_status.remote(trace_id, "failed")
await postgres_database.update_workflow_status(trace_id, "failed")
async def working(
self,
@@ -14,24 +14,23 @@
from kilostar.core.work.workflow.workflow import KiloStarWorkflow, WorkflowStep
from kilostar.utils.agent_model import ResponseModel, DepsModel, RequestModel
from pydantic import Field
from pydantic import BaseModel, Field
from typing import Optional, List
# 意识节点回复类
class ConsciousnessNodeResponse(ResponseModel):
class ConsciousnessNodeResponse(BaseModel):
"""Consciousness response model,是意识节点所有回复类型的父类"""
pass
class ConsciousnessNodeDeps(DepsModel):
class ConsciousnessNodeDeps(BaseModel):
"""ConsciousnessNode 在 pydantic-ai Agent 中使用的依赖:原始指令、当前指令以及可用 Skill 列表。"""
original_command: str
command: str
available_skills: Optional[List[dict]] = None
locale: str = "zh"
class ConsciousnessNodeInput(RequestModel):
class ConsciousnessNodeInput(BaseModel):
"""ConsciousnessNode 各类入参的共同基类,仅用于打 schema 标签。"""
pass
@@ -1,17 +0,0 @@
# 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
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from .control_node import ControlNode
__all__ = ["ControlNode"]
@@ -1,135 +0,0 @@
# 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
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from pydantic_ai import Agent, RunContext
from kilostar.utils.ray_compat import actor_class
from kilostar.core.global_state_machine.global_state_machine import GlobalStateMachine
from kilostar.core.global_state_machine.model_provider.base_provider import Provider
from kilostar.adapter.model_adapter.agent_factory import AgentFactory
from kilostar.core.individual.control_node.template import (
ForWorkflow,
ForWorkflowInput,
ControlNodeDeps,
)
from kilostar.utils.prompts import agent_prompt
@actor_class
class ControlNode:
"""ControlNode(控制节点):**已废弃**——名字保留给未来的远程探针/系统控制节点。
历史:早期设计里它是工作流的"单步执行 actor",但 workflow_engine 的 Dispatch
最终只识别 ``consciousness_node`` 和 ``skill_individual``,本类从未真正被调用过。
保留目录与类壳子,避免改名带来的 git 历史断层;**不要新增对它的依赖**。
待远程探针/监控流子项目启动时,本目录将被重写为远程机器控制节点。
"""
def __init__(self):
from kilostar.utils.logger import get_logger
self.logger = get_logger("control_node")
self.agent: Agent | None = None
self._model_settings: dict = {}
async def create_agent(
self,
global_state_machine: GlobalStateMachine,
provider_title: str,
model_id: str,
tools_list: list[str] = None,
toolsets=None,
locale: str | None = None,
custom_system_prompt: str | None = None,
) -> None:
"""
create_agent方法,将agent对象装配到Control的属性内
该方法通过provider_title从global_state_machine中获取provider对象,然后从provider对象中取出供应商形象,装配为pydantic_ai的
Agent实例,
并挂载到self.agent属性
Args:
global_state_machine: 全局状态机
provider_title: 供应商名
model_id: 模型id
locale: 语言代码(zh/en),控制system prompt语言
custom_system_prompt: 管理员自定义追加提示词(可选)
Returns:
无返回
"""
system_prompt: str = agent_prompt("control_node", locale=locale, custom_system_prompt=custom_system_prompt)
output_type = ForWorkflow
from kilostar.utils.get_tool import load_tools_from_list
from kilostar.core.global_state_machine.gsm_snapshot import fetch_snapshot
snapshot = await fetch_snapshot(gsm_actor=global_state_machine)
provider: Provider = snapshot.providers.get(provider_title)
if provider is None:
from kilostar.utils.i18n import t
raise ValueError(t("provider_not_registered", locale=locale, provider_title=provider_title))
agent_factory = AgentFactory()
callables = load_tools_from_list(tools_list)
self.agent = agent_factory.create_agent(
provider=provider,
model_id=model_id,
output_type=output_type,
system_prompt=system_prompt,
deps_type=ControlNodeDeps,
agent_name="control_node",
tools=callables,
toolsets=toolsets,
)
self._model_settings = AgentFactory.resolve_model_settings(provider, model_id)
@self.agent.system_prompt
async def dynamic_prompt(ctx: RunContext[ControlNodeDeps]):
"""运行期动态拼接 system prompt:把当前 workflow_step 的关键字段塞进去。"""
prompt = system_prompt + "\n\n"
prompt += (
f"=== 当前任务步骤上下文 ===\n"
f"- 步骤名称 (Name): {ctx.deps.workflow_step.name}\n"
f"- 步骤目标/描述 (Description): {ctx.deps.workflow_step.desc}\n"
f"- 前置输入(input): {ctx.deps.workflow_step.inputs}\n"
)
return prompt
async def working(self, payload: ForWorkflowInput) -> str:
"""对外入口:执行一次步骤,吞掉异常并返回 ``None`` 以避免拖垮上游 Workflow。"""
try:
result: ForWorkflow = await self._run(payload)
return result
except Exception:
self.logger.exception("ControlNode在执行working时发生严重错误")
return None
async def _run(self, payload: ForWorkflowInput) -> ForWorkflow:
"""实际执行步骤:组装 ``ControlNodeDeps``、调用 Agent,最终把 ``ForWorkflow`` 输出取出。"""
try:
self.agent.retries = 3
deps = ControlNodeDeps(workflow_step=payload.workflow_step)
self.logger.debug(
f"ControlNode: 开始执行工作流节点 [{payload.workflow_step.name}] (原生重试开启)"
)
result = await self.agent.run(
f"请根据提供的 workflow_step 上下文,执行此步骤并输出结果。\n详细指令或附加数据:{payload.workflow_step.model_dump_json()}",
deps=deps,
model_settings=self._model_settings or None,
)
return result.output
except Exception as e:
self.logger.exception(
f"ControlNode 在执行步骤 [{payload.workflow_step.name}] 时最终失败: {str(e)}"
)
raise RuntimeError(f"ControlNode 执行步骤失败: {str(e)}") from e
@@ -1,51 +0,0 @@
# 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
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from pydantic import Field
from kilostar.core.work.workflow.workflow import WorkflowStep
from kilostar.utils.agent_model import ResponseModel, RequestModel, DepsModel
class ControlNodeResponse(ResponseModel):
"""控制节点回复的基类。"""
pass
class ControlNodeInput(RequestModel):
"""控制节点输入的基类,承载一次调度所需的入参。"""
pass
class ControlNodeDeps(DepsModel):
"""控制节点运行期依赖,注入到 pydantic-ai Agent 的 RunContext。"""
workflow_step: WorkflowStep
# In the future, this can be dynamically populated with tools specific to the current task execution
class ForWorkflow(ControlNodeResponse):
"""控制节点执行单个工作流步骤的输出模型。"""
output: str = Field(
..., description="控制节点执行特定工作流步骤的结果。包含执行细节和输出数据。"
)
class ForWorkflowInput(ControlNodeInput):
"""控制节点针对工作流步骤的输入模型。"""
workflow_step: WorkflowStep
@@ -1,14 +0,0 @@
# 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
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
@@ -1,14 +0,0 @@
# 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
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
@@ -15,7 +15,6 @@
import asyncio
import datetime
from typing import Union
from kilostar.utils.ray_compat import actor_class
from kilostar.adapter.model_adapter.agent_factory import AgentFactory
from kilostar.core.global_state_machine.global_state_machine import GlobalStateMachine
from kilostar.core.global_state_machine.model_provider import Provider
@@ -28,7 +27,6 @@ from pydantic_ai import RunContext, Agent
from kilostar.utils.prompts import agent_prompt
@actor_class
class RegulatoryNode:
"""RegulatoryNode(监管节点):用户请求的直接对话节点。
@@ -234,12 +232,12 @@ class RegulatoryNode:
return
try:
import uuid
from kilostar.utils.ray_hook import ray_actor_hook
from kilostar.utils.actor import get_actor
postgres_database = ray_actor_hook("postgres_database").postgres_database
postgres_database = get_actor("postgres_database")
task_id = uuid.uuid4().hex
chat_id = payload.platform_id if payload.platform == "client" else None
await postgres_database.create_task.remote(
await postgres_database.create_task(
task_id=task_id,
user_id=payload.user_name,
command=payload.message,
@@ -13,12 +13,10 @@
# limitations under the License.
from typing import Literal, Optional
from pydantic import Field
from kilostar.utils.agent_model import ResponseModel, DepsModel, RequestModel
from pydantic import BaseModel, Field
class RegulatoryNodeResponse(ResponseModel):
class RegulatoryNodeResponse(BaseModel):
"""
RegulatoryNodeResponse类
一切regulatory_node回复的父类
@@ -26,14 +24,14 @@ class RegulatoryNodeResponse(ResponseModel):
pass
class RegulatoryNodeRequest(RequestModel):
class RegulatoryNodeRequest(BaseModel):
"""
RegulatoryNodeRequest类
向regulatory请求的父类
"""
pass
class RegulatoryNodeDeps(DepsModel):
class RegulatoryNodeDeps(BaseModel):
"""
RegulatoryNodeDeps类
regulatory_node的依赖模型
@@ -44,7 +42,7 @@ class RegulatoryNodeDeps(DepsModel):
retry_count: int = 0
error_history: str = ""
class MessageRequest(RequestModel):
class MessageRequest(BaseModel):
"""
MessageRequest类
任何消息渠道向regulatory_node发送消息请求的模型