feat: 公开定时任务页面、Word 导出、定时任务实时刷新与技能详情重构
- 新增 /public/scheduled-tasks/:taskId 公开只读访问页与对应的 /api/public/scheduled-tasks 后端路由(无需登录) - 新增对话整体 / 单条消息 / Markdown 工件 "另存为 Word (.docx)" 能力 - 定时任务列表与运行列表在任务执行中自动轮询刷新,并新增"正在执行"标记 - 定时任务运行消息接口在事件存储为空时回退到 LangGraph checkpoint, 让正在执行的任务也能立即看到提示词与流式输出 - 技能详情对话框由折叠面板改为 Tab 切换(Detail / SKILL.md), 并支持 Rendered / Raw 切换、一键复制 - config.yaml 增加 database (MySQL) 与 checkpointer (SQLite) 配置块 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
7e75c5b9b9
commit
d339db4799
18 files changed
+1617
-125
No files matched your search
@@ -14,7 +14,16 @@ from fastapi import APIRouter, HTTPException, Query, Request
|
||||
from fastapi.responses import FileResponse, Response, StreamingResponse
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from app.gateway.deps import get_current_user, get_local_provider, get_optional_user_from_request, get_run_event_store, get_run_store, get_tag_store
|
||||
from app.gateway.deps import (
|
||||
get_checkpointer,
|
||||
get_current_user,
|
||||
get_local_provider,
|
||||
get_optional_user_from_request,
|
||||
get_run_event_store,
|
||||
get_run_store,
|
||||
get_tag_store,
|
||||
)
|
||||
from deerflow.runtime import serialize_channel_values
|
||||
from deerflow.runtime.scheduler import DuplicateTaskNameError, get_scheduled_task_service
|
||||
|
||||
router = APIRouter(prefix="/api/scheduled-tasks", tags=["scheduled-tasks"])
|
||||
@@ -235,31 +244,129 @@ async def get_scheduled_task_run(run_id: str, request: Request) -> dict:
|
||||
return run
|
||||
|
||||
|
||||
def _event_type_for_checkpoint_message(message: dict) -> str:
|
||||
"""Map a serialized LangChain message to the same ``event_type`` strings
|
||||
the run-event store uses, so the frontend's existing role labels keep
|
||||
working for checkpoint-derived synthetic rows."""
|
||||
raw_type = message.get("type") or message.get("role") or ""
|
||||
if raw_type in {"human", "user"}:
|
||||
return "llm.human.input"
|
||||
if raw_type in {"ai", "assistant"}:
|
||||
return "llm.ai.response"
|
||||
if raw_type == "tool":
|
||||
return "llm.tool.result"
|
||||
if raw_type == "system":
|
||||
return "llm.system"
|
||||
return f"llm.{raw_type or 'message'}"
|
||||
|
||||
|
||||
async def _checkpoint_messages_for_run(
|
||||
request: Request, agent_run: dict, *, limit: int
|
||||
) -> list[dict]:
|
||||
"""Return a synthetic list of message rows pulled from the LangGraph
|
||||
checkpoint for an in-flight run.
|
||||
|
||||
The event store is updated by the streaming pipeline and may stay empty
|
||||
until the first message tuple is flushed — meanwhile the checkpoint
|
||||
already carries the current state (input + accumulating AI deltas). We
|
||||
project that into the same row shape the frontend uses for the event
|
||||
store, so the run-detail page can show progress immediately."""
|
||||
try:
|
||||
checkpointer = get_checkpointer(request)
|
||||
except Exception:
|
||||
return []
|
||||
thread_id = agent_run.get("thread_id")
|
||||
if not thread_id:
|
||||
return []
|
||||
config = {"configurable": {"thread_id": thread_id, "checkpoint_ns": ""}}
|
||||
try:
|
||||
tup = await checkpointer.aget_tuple(config)
|
||||
except Exception:
|
||||
return []
|
||||
if tup is None:
|
||||
return []
|
||||
checkpoint = getattr(tup, "checkpoint", {}) or {}
|
||||
channel_values = checkpoint.get("channel_values") or {}
|
||||
try:
|
||||
values = serialize_channel_values(channel_values)
|
||||
except Exception:
|
||||
return []
|
||||
messages = values.get("messages") or []
|
||||
|
||||
rows: list[dict] = []
|
||||
for index, message in enumerate(messages[-limit:]):
|
||||
if not isinstance(message, dict):
|
||||
continue
|
||||
rows.append(
|
||||
{
|
||||
"seq": index + 1,
|
||||
"event_type": _event_type_for_checkpoint_message(message),
|
||||
"category": "message",
|
||||
"content": message,
|
||||
"metadata": {"source": "checkpoint"},
|
||||
"created_at": None,
|
||||
}
|
||||
)
|
||||
return rows
|
||||
|
||||
|
||||
@router.get("/runs/{run_id}/messages")
|
||||
async def get_scheduled_task_run_messages(run_id: str, request: Request, limit: int = 500) -> dict:
|
||||
"""Agent messages produced by a scheduled run's underlying agent run.
|
||||
|
||||
Lets the run-detail page show what the agent is doing — useful when a run
|
||||
is stuck in ``running``. Authorization reuses :func:`_load_run` (owner /
|
||||
subscriber / admin); the event-store query then passes ``user_id=None``
|
||||
because access has already been checked.
|
||||
Resolution order (most authoritative first):
|
||||
|
||||
1. Event-store rows for the underlying agent run — these are the per-tuple
|
||||
streaming events the worker writes as the agent runs.
|
||||
2. LangGraph checkpoint snapshot — the canonical conversation state. Used
|
||||
as a fallback when the event store is still empty (early in a run) or
|
||||
when only partial events have been flushed; we project the snapshot
|
||||
into the same row shape the frontend already renders so the user can
|
||||
see the prompt + current AI output while the task is mid-flight.
|
||||
3. Synthetic human-input row built from ``task.prompt`` — shown even
|
||||
before the agent run is registered, so a `pending` task at minimum
|
||||
displays what was queued.
|
||||
"""
|
||||
service = get_scheduled_task_service()
|
||||
user_id = await _user_id(request)
|
||||
run = await _load_run(service, request, run_id, user_id)
|
||||
if run is None:
|
||||
raise HTTPException(status_code=404, detail=f"Run {run_id} not found")
|
||||
capped = max(1, min(limit, 1000))
|
||||
|
||||
# Always available: task prompt as the seeded human input. Lets the
|
||||
# ``execution process`` tab show *something* even before the agent run is
|
||||
# registered, which is the user-visible bug we're fixing.
|
||||
fallback_rows: list[dict] = []
|
||||
task_id = run.get("task_id")
|
||||
if task_id:
|
||||
task = await service.store.get_task_any(task_id)
|
||||
prompt = (task or {}).get("prompt")
|
||||
if prompt:
|
||||
fallback_rows.append(
|
||||
{
|
||||
"seq": 0,
|
||||
"event_type": "llm.human.input",
|
||||
"category": "message",
|
||||
# Frontend treats human-input content as a plain string.
|
||||
"content": str(prompt),
|
||||
"metadata": {"source": "task_prompt"},
|
||||
"created_at": run.get("created_at") or run.get("scheduled_for"),
|
||||
}
|
||||
)
|
||||
|
||||
agent_run_id = run.get("agent_run_id")
|
||||
if not agent_run_id:
|
||||
return {"messages": [], "has_more": False}
|
||||
return {"messages": fallback_rows, "has_more": False}
|
||||
|
||||
# Each scheduled run executes on its own throwaway runtime thread — NOT the
|
||||
# run row's ``scheduler_thread_id`` — so resolve the real thread from the
|
||||
# run store before querying its message events.
|
||||
run_store = get_run_store(request)
|
||||
agent_run = await run_store.get(agent_run_id, user_id=None)
|
||||
if agent_run is None:
|
||||
return {"messages": [], "has_more": False}
|
||||
capped = max(1, min(limit, 1000))
|
||||
return {"messages": fallback_rows, "has_more": False}
|
||||
|
||||
event_store = get_run_event_store(request)
|
||||
rows = await event_store.list_messages_by_run(
|
||||
agent_run["thread_id"],
|
||||
@@ -267,6 +374,16 @@ async def get_scheduled_task_run_messages(run_id: str, request: Request, limit:
|
||||
limit=capped + 1,
|
||||
user_id=None,
|
||||
)
|
||||
|
||||
# Event store has no rows yet (run just started, or events still buffering).
|
||||
# Fall back to the live checkpoint so the user immediately sees the prompt
|
||||
# + whatever AI output has been streamed so far.
|
||||
if not rows:
|
||||
checkpoint_rows = await _checkpoint_messages_for_run(request, agent_run, limit=capped)
|
||||
if checkpoint_rows:
|
||||
return {"messages": checkpoint_rows[:capped], "has_more": False}
|
||||
return {"messages": fallback_rows, "has_more": False}
|
||||
|
||||
has_more = len(rows) > capped
|
||||
return {"messages": rows[:capped] if has_more else rows, "has_more": has_more}
|
||||
|
||||
|
||||
Reference in new issue
Block a user