Refactor Workflow and Chat Architecture (#68)

* refactor: overhaul workflow and chat architecture

- Separate Chat and Workflow API endpoints and database models
- Use JSONB to store workflow execution context in Postgres
- Convert workflow engine to use pydantic-ai execution graphs inside a Ray task
- Update frontend React components to support standalone workflow creation
- Remove obsolete and broken workflow runner tests

Co-authored-by: zhaoxi826 <198742034+zhaoxi826@users.noreply.github.com>

* refactor: overhaul workflow and chat architecture

- Separate Chat and Workflow API endpoints and database models
- Use JSONB to store workflow execution context in Postgres
- Convert workflow engine to use pydantic-ai execution graphs inside a Ray task
- Update frontend React components to support standalone workflow creation
- Remove obsolete and broken workflow runner tests

Co-authored-by: zhaoxi826 <198742034+zhaoxi826@users.noreply.github.com>

* refactor: overhaul workflow and chat architecture

- Separate Chat and Workflow API endpoints and database models
- Use JSONB to store workflow execution context in Postgres
- Convert workflow engine to use pydantic-ai execution graphs inside a Ray task
- Update frontend React components to support standalone workflow creation
- Move workflow_engine inside workflow package to keep core root clean
- Remove obsolete and broken workflow runner tests

Co-authored-by: zhaoxi826 <198742034+zhaoxi826@users.noreply.github.com>

---------

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
Co-authored-by: zhaoxi826 <198742034+zhaoxi826@users.noreply.github.com>
This commit is contained in:
2026-05-12 15:47:17 +08:00
committed by GitHub
parent ee9bbbf676
commit ff1ede47a0
33 changed files with 995 additions and 412 deletions
+2
View File
@@ -27,6 +27,7 @@ from .platform.frontend import client_router
from .provider import provider_router
from .resource import resource_router
from .workflow import workflow_router
from .chat import chat_router
from kilostar.utils.error import (
DemandError,
ModelNotExistError,
@@ -48,6 +49,7 @@ app.include_router(resource_router) # 资源路径
app.include_router(cluster_router) # 集群信息路径
app.include_router(agent_router) # agent路径
app.include_router(workflow_router) # workflow路径
app.include_router(chat_router) # chat路径
@app.exception_handler(UserNotExistError)
+109
View File
@@ -0,0 +1,109 @@
# 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 fastapi import APIRouter, Depends
from pydantic import BaseModel
from kilostar.utils.ray_hook import ray_actor_hook
from kilostar.utils.access import Accessor, TokenData
chat_router = APIRouter(prefix="/api/v1/chat", tags=["chat"])
class CreateChatRequest(BaseModel):
title: str = "新对话"
initial_message: str
class SendMessageRequest(BaseModel):
message: str
@chat_router.post("")
async def create_chat_session(
request: CreateChatRequest,
token_data: TokenData = Depends(Accessor.get_current_user),
):
postgres_database = ray_actor_hook("postgres_database").postgres_database
chat = await postgres_database.create_chat_session.remote(
user_id=token_data.user_id, title=request.title
)
# 存入用户消息
await postgres_database.add_chat_message.remote(
chat_id=chat.chat_id, message=request.initial_message, message_owner="user"
)
# 调用监管节点处理简单任务/交流
regulatory_node = ray_actor_hook("regulatory_node").regulatory_node
# 在此发起任务并等待或异步返回结果
response_msg = await regulatory_node.handle_chat_message.remote(
user_id=token_data.user_id,
chat_id=chat.chat_id,
message=request.initial_message,
)
# 存入回复消息
if response_msg:
await postgres_database.add_chat_message.remote(
chat_id=chat.chat_id, message=response_msg, message_owner="regulatory_node"
)
return {"chat_id": chat.chat_id, "reply": response_msg}
@chat_router.get("")
async def list_chat_sessions(
token_data: TokenData = Depends(Accessor.get_current_user),
):
postgres_database = ray_actor_hook("postgres_database").postgres_database
sessions = await postgres_database.list_chat_sessions.remote(
user_id=token_data.user_id
)
return {"sessions": sessions}
@chat_router.get("/{chat_id}")
async def get_chat_history(
chat_id: str, token_data: TokenData = Depends(Accessor.get_current_user)
):
postgres_database = ray_actor_hook("postgres_database").postgres_database
messages = await postgres_database.list_chat_messages.remote(chat_id=chat_id)
return {"messages": messages}
@chat_router.post("/{chat_id}/reply")
async def send_chat_message(
chat_id: str,
request: SendMessageRequest,
token_data: TokenData = Depends(Accessor.get_current_user),
):
postgres_database = ray_actor_hook("postgres_database").postgres_database
# 存用户消息
await postgres_database.add_chat_message.remote(
chat_id=chat_id, message=request.message, message_owner="user"
)
# 调用监管节点
regulatory_node = ray_actor_hook("regulatory_node").regulatory_node
response_msg = await regulatory_node.handle_chat_message.remote(
user_id=token_data.user_id, chat_id=chat_id, message=request.message
)
# 存回复
if response_msg:
await postgres_database.add_chat_message.remote(
chat_id=chat_id, message=response_msg, message_owner="regulatory_node"
)
return {"reply": response_msg}
+2 -2
View File
@@ -16,8 +16,8 @@ import datetime
from pydantic import BaseModel, Field, ConfigDict
from ulid import ULID
from typing import Any, Dict
from kilostar.core.workflow_running_engine.workflow import kilostarWorkflow
import asyncio
from kilostar.core.work.workflow.workflow import KiloStarWorkflow
class kilostarEvent(BaseModel):
@@ -43,7 +43,7 @@ class kilostarEvent(BaseModel):
context: Dict[str, Any] = Field(
default_factory=dict, description="事件上下文内容,可包含工作流模板等信息"
)
workflow: kilostarWorkflow | None = Field(default=None, description="工作流")
workflow: KiloStarWorkflow | None = Field(default=None, description="工作流")
pending_queue: asyncio.Queue[str] | None = Field(
default=None, description="待处理队列"
)
+61 -68
View File
@@ -12,101 +12,95 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from kilostar.utils.ray_hook import ray_actor_hook
from fastapi import APIRouter, Request, HTTPException
from fastapi import APIRouter, Request, HTTPException, Depends
from fastapi.responses import StreamingResponse
from pydantic import BaseModel
from ulid import ULID
import asyncio
from kilostar.utils.access import Accessor, TokenData
workflow_router = APIRouter(prefix="/api/v1/workflow", tags=["workflow"])
class CreateWorkflowRequest(BaseModel):
title: str
command: str
@workflow_router.post("")
async def create_workflow(
request: CreateWorkflowRequest,
token_data: TokenData = Depends(Accessor.get_current_user),
):
postgres_database = ray_actor_hook("postgres_database").postgres_database
trace_id = str(ULID())
await postgres_database.create_workflow.remote(
trace_id=trace_id,
user_id=token_data.user_id,
title=request.title,
command=request.command,
)
# 将需求发送给意识节点去处理构建
consciousness_node = ray_actor_hook("consciousness_node").consciousness_node
# 可以异步通知意识节点开始与用户在特定 Trace ID 下对话
consciousness_node.start_workflow_design.remote(trace_id, request.command)
return {"trace_id": trace_id, "status": "creating"}
@workflow_router.get("/list")
async def get_workflow_list():
"""处理针对 get workflow list 相关的 HTTP API 请求。
该接口负责解析前端传入的载荷数据,调用底层核心业务逻辑进行处理,并组装标准化的 JSON 响应。
Returns: : 序列化后的标准网络响应模型(如包含业务状态码、成功标志及对应的数据载荷 Data)。"""
global_workflow_manager = ray_actor_hook(
"global_workflow_manager"
).global_workflow_manager
events = await global_workflow_manager.list_events.remote()
return events
async def get_workflow_list(token_data: TokenData = Depends(Accessor.get_current_user)):
postgres_database = ray_actor_hook("postgres_database").postgres_database
workflows = await postgres_database.list_workflows.remote(
user_id=token_data.user_id
)
return {"workflows": workflows}
@workflow_router.get("/{trace_id}")
async def get_workflow_detail(trace_id: str):
"""处理针对 get workflow detail 相关的 HTTP API 请求。
该接口负责解析前端传入的载荷数据,调用底层核心业务逻辑进行处理,并组装标准化的 JSON 响应。
Args: trace_id (str): 目标对象的唯一全局标识符 (UUID/ULID),用于在数据库表或缓存结构中精准匹配该 trace 实例。
Returns: : 序列化后的标准网络响应模型(如包含业务状态码、成功标志及对应的数据载荷 Data)。"""
global_workflow_manager = ray_actor_hook(
"global_workflow_manager"
).global_workflow_manager
event = await global_workflow_manager.get_event.remote(trace_id)
if not event:
async def get_workflow_detail(
trace_id: str, token_data: TokenData = Depends(Accessor.get_current_user)
):
postgres_database = ray_actor_hook("postgres_database").postgres_database
wf = await postgres_database.get_workflow.remote(trace_id)
if not wf:
raise HTTPException(status_code=404, detail="Workflow not found")
workflow = event.workflow
if not workflow:
return {
"event_id": trace_id,
"workflow_title": None,
"status": "waiting",
"user_name": event.user_name,
"message": event.message,
"create_time": event.create_time,
"steps": [],
}
context = await postgres_database.get_workflow_context.remote(trace_id)
steps = context.work_link if context and hasattr(context, "work_link") else []
steps = []
for step in workflow.work_link:
steps.append(
{
"step": step.step,
"name": step.name,
"node": step.node,
"action": step.action,
"desc": step.desc,
"status": step.status,
"agent_id": step.agent_id,
}
)
return {
"event_id": trace_id,
"workflow_title": workflow.title,
"status": workflow.status.status,
"command": workflow.command,
"current_step": workflow.status.step,
"user_name": event.user_name,
"message": event.message,
"create_time": event.create_time,
"trace_id": trace_id,
"title": wf.title,
"status": wf.status,
"command": wf.command,
"steps": steps,
"context_blackboard": context.blackboard if context else {},
}
@workflow_router.get("/sse/{trace_id}")
async def get_workflow_sse(trace_id: str, request: Request):
"""处理针对 get workflow sse 相关的 HTTP API 请求。
该接口负责解析前端传入的载荷数据,调用底层核心业务逻辑进行处理,并组装标准化的 JSON 响应。
Args: trace_id (str): 目标对象的唯一全局标识符 (UUID/ULID),用于在数据库表或缓存结构中精准匹配该 trace 实例。 request (Request): FastAPI 框架注入的原生 HTTP 请求对象,包含了完整的 Header 标头、查询参数和正文流。
Returns: : 序列化后的标准网络响应模型(如包含业务状态码、成功标志及对应的数据载荷 Data)。"""
"""
用于与意识节点交互,获取工作流状态或设计阶段的问答消息
"""
global_workflow_manager = ray_actor_hook(
"global_workflow_manager"
).global_workflow_manager
async def event_generator():
"""执行与 event generator 相关的核心业务流转操作。
该方法封装了具体的算法策略或状态控制逻辑,确保操作能够在事务上下文中被原子且一致地执行。"""
try:
while True:
if await request.is_disconnected():
break
# You might also want to send the workflow state periodically or when updated
# Here we just wait for pending messages and send them
message = await global_workflow_manager.get_pending.remote(trace_id)
# Ensure the message is formatted as SSE
yield f"data: {message}\n\n"
if message:
yield f"data: {message}\n\n"
else:
await asyncio.sleep(0.5)
except asyncio.CancelledError:
pass
@@ -115,10 +109,9 @@ async def get_workflow_sse(trace_id: str, request: Request):
@workflow_router.post("/reply/{trace_id}")
async def post_workflow_reply(trace_id: str, request: Request):
"""处理针对 post workflow reply 相关的 HTTP API 请求。
该接口负责解析前端传入的载荷数据,调用底层核心业务逻辑进行处理,并组装标准化的 JSON 响应。
Args: trace_id (str): 目标对象的唯一全局标识符 (UUID/ULID),用于在数据库表或缓存结构中精准匹配该 trace 实例。 request (Request): FastAPI 框架注入的原生 HTTP 请求对象,包含了完整的 Header 标头、查询参数和正文流。
Returns: : 序列化后的标准网络响应模型(如包含业务状态码、成功标志及对应的数据载荷 Data)。"""
"""
用于用户回复意识节点的提问(设计阶段或运行中的中断确认)
"""
data = await request.json()
reply_msg = data.get("message", "")
global_workflow_manager = ray_actor_hook(