feat: 后台轮询器 — add_startup 自动执行 role_task

- _role_poller 每10秒扫描 pipeline_tasks 中 submitted 的 role_task
- 逐条 dispatch 给对应角色 agent_loop 异步执行
- 修复 agent_loop 中 agent_ask 导入为相对导入
This commit is contained in:
yumoqing 2026-08-05 17:20:26 +08:00
parent 4cb406003e
commit 4c0b349718
2 changed files with 33 additions and 1 deletions

View File

@ -166,7 +166,7 @@ async def role_agent_run(project_id: str, role: str, agent_id: str = None, model
# 缺信息 → 提问回路:任务置 waiting等主agent/客户回答
if parsed.get("status") == "need_info" and parsed.get("question"):
from pipeline_service.questions import agent_ask
from .questions import agent_ask
qid = await agent_ask(project_id, task_id, role, parsed["question"],
context={"partial": parsed.get("partial", ""), "title": title})
return {"status": "need_info", "task_id": task_id,

View File

@ -8,6 +8,8 @@
"""
import json
import asyncio
import logging
from ahserver.serverenv import ServerEnv
from appPublic.uniqueID import getID
from appPublic.log import debug
@ -427,5 +429,35 @@ def load_pipeline_service():
env.question_list = list_questions
env.question_qna = get_task_qna
# Background poller: auto-dispatch submitted role_tasks to role agents
_logger = logging.getLogger("pipeline.agent_poller")
async def _role_poller():
"""轮询 pipeline_tasks 中待执行的 role_task交给对应角色agent执行。"""
from sqlor.dbpools import DBPools
poll_db = DBPools()
while True:
try:
async with poll_db.sqlorContext("pipeline") as sor:
recs = await sor.sqlExe(
"SELECT id, tenant_id, role FROM pipeline_tasks "
"WHERE state='submitted' AND pipeline_id='role_task' "
"AND claimed_by IS NULL ORDER BY created_at ASC LIMIT 5",
{})
for rec in (recs or []):
tid = getattr(rec, 'id', '')
pid = getattr(rec, 'tenant_id', '')
r = getattr(rec, 'role', '')
if tid and pid and r:
_logger.info(f"dispatching task={tid} role={r}")
asyncio.ensure_future(
role_agent_loop(pid, r, agent_id=f"poller-{r}"))
except Exception as e:
_logger.error(f"error: {e}")
await asyncio.sleep(10)
from ahserver.configuredServer import add_startup
add_startup(_role_poller())
debug(f"[{MODULE_NAME}] v{MODULE_VERSION} loaded — pipeline engine with role-agent + question loop")
return True