mirror of
https://github.com/ThearchyHelios/AgentLab.git
synced 2026-11-25 20:03:54 +00:00
Copilot 流式生成:图在画布上实时长出来
此前点击生成后是 20-30 秒黑盒等待,模态框关着,用户看不到任何过程。 改成操作流协议:模型按 NDJSON 逐行输出 add_node / add_edge / update_node / remove_node / done,后端边流边经 SSE 转发,前端每收 一个操作就应用到画布——节点带紫色描边逐个出现(临时摆位跟着入边源), 左下角浮条显示当前动作并可随时取消;done 后后端统一排版+校验, 以 final 事件整体落位,高亮几秒后淡出。修改现有图的场景只输出改动 操作,用户能看到 Copilot 具体动了哪里。 行解析对模型犯规免疫:markdown 围栏、夹杂的解释文字、done 之后的 多余输出都被跳过或截断(单测覆盖跨块行缓冲)。 顺手修结构化输出必失败的 bug:langchain 靠 title 字段识别裸 JSON Schema dict,四处 with_structured_output 的 schema 都缺它,抛 ValueError: Unsupported function 后靠 fallback 文本解析兜底—— 每次都白调一次模型。补上 title(llm 节点 output_schema、branch 语义分类、supervisor 路由、旧版 copilot generate)。 实测:首节点 7.2s 上画布(原先干等 27s),浏览器采样节点数 0→2→3→4→5→6 逐步生长,6 个新节点全部高亮,零页面错误。
This commit is contained in:
@@ -81,6 +81,7 @@ async def _classify_with_llm(
|
||||
)
|
||||
|
||||
schema = {
|
||||
"title": "classify", # langchain 靠 title 识别 JSON Schema dict
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"key": {"type": "string", "enum": keys},
|
||||
|
||||
@@ -176,6 +176,9 @@ async def run_llm(state: GraphState, ctx: NodeContext) -> dict[str, Any]:
|
||||
# 要结构化结果就走原生 structured output,这条路径拿不到 token 流
|
||||
ctx.emit(EventType.LLM_START, structured=True, message_count=len(messages))
|
||||
try:
|
||||
# langchain 靠 "title" 识别裸 JSON Schema dict,缺了会抛 Unsupported function
|
||||
if isinstance(schema, dict) and "title" not in schema:
|
||||
schema = {"title": "output", **schema}
|
||||
structured = model.with_structured_output(schema)
|
||||
value = await structured.ainvoke(messages)
|
||||
except Exception as e: # noqa: BLE001
|
||||
|
||||
@@ -59,6 +59,7 @@ async def run_supervisor(state: GraphState, ctx: NodeContext) -> dict[str, Any]:
|
||||
for n in names
|
||||
)
|
||||
route_schema = {
|
||||
"title": "route", # langchain 靠 title 识别 JSON Schema dict
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"next": {"type": "string", "enum": names + ["FINISH"]},
|
||||
|
||||
Reference in New Issue
Block a user