mirror of
https://github.com/ThearchyHelios/AgentLab.git
synced 2026-11-22 14:51:58 +00:00
第三波前两批:六个「看着成功其实没干活」的平台漏洞堵上,记录页能回放,管理页与画布收尾
起因是一次探索运行:Copilot 改节点时把工具绑定整份抹掉,agent 没有工具只能写「假设调用」, 校验节点的自动修复编出 total_count=0 让 schema 通过,协作成员把工具调用标记当文字输出了四轮, 轮数用完后节点把原话当成功结果交出去——运行标成已完成,数据库一次都没查。 平台可信度(先写失败测试) - Copilot 修改节点改为合并:没写的字段保留,null 才删除;协作成员按名字合并。前后端同一套规则, 工具集合变小时自查报警、改图回执列出变化 - 新校验规则:提示词点名了某个工具而节点没有绑定它,判错误,Copilot 自查会交回去修 - 校验修复只许改格式:冒出原文没有的值就判修复无效;修复这次模型调用计入用量 - 识别模型把工具调用写成文字的情况(DSML、<tool_call> 等标记):纠正一次,仍然如此就判失败并说明原因 - 协作团队用完轮数不再按成功交付:默认失败,可选降档,点出一次都没派到的成员 - 审批恢复后,同一节点里已经执行过的工具调用复用结果,不再重复执行 后端 - 查询时限交给数据库执行(Oracle call_timeout、PG statement_timeout、MySQL max_execution_time、 SQLite progress handler),可按数据源配置;超时报错与实际耗时一致 - 等审批的运行可以真正取消;会话列表带最后一轮状态;测连接结果持久化;探查结构可只看不存; 自定义工具可草稿试跑、写坏的参数定义保存时拒绝;MCP 服务改动后作废旧工具缓存; 检索命中带贡献分解;重建索引在后台跑并报进度;报错统一收进 core/errors 前端 - 公共层:管理页共用件、失败说明(lib/explain)、健康状态进公共库;目录按表各自加载并有超时, 一张表卡住不再拖垮整站;上传显示真实字节进度;离线横幅只播报不变的句子 - 画布:卡片只说游标所在的那一次执行;等审批时可放弃运行;团队收尾判定与未完成说清楚; 回放用量取那一刻的值 - 编排页:Copilot 合并语义与工具变化回执;「用完轮数时」选项;正式运行期间锁住所有改图入口 - 回答流 / 问数据:新失败类型说人话并给下一步;画布与右栏互相定位;数据源范围、回收站、重新核对 - 记录页:「航迹」页签复用画布时间轴,可拖动回放、列出关键时刻;列表行带执行 / 等人 / 失败缩略条 - 外壳:统一的离开前确认(data router),导航、⌘K、快捷键、后退都会先问 验证(沙箱):tsc 干净,pytest 838 passed(两遍),14 个检查脚本中 13 个全绿; check-ui 余 1 项旧断言(按旧日期格式找运行行),3c 更新。
This commit is contained in:
@@ -13,15 +13,16 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from typing import Any
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from pydantic import BaseModel, Field
|
||||
from sqlalchemy import delete, func, select
|
||||
from sqlalchemy import and_, delete, func, select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.db.base import get_session, utcnow
|
||||
from app.db.models import Conversation, ConversationTurn
|
||||
from app.db.models import Approval, Conversation, ConversationTurn, Run
|
||||
|
||||
router = APIRouter(prefix="/api/conversations", tags=["conversations"])
|
||||
|
||||
@@ -71,6 +72,8 @@ class TurnPatch(BaseModel):
|
||||
status: str | None = Field(default=None, pattern="^(running|done|error)$")
|
||||
error: str | None = None
|
||||
review: dict[str, Any] | None = None
|
||||
#: 可信度元数据,前端整块写、整块读,后端不解读它的内容(列表状态除外,见 last_status)
|
||||
meta: dict[str, Any] | None = None
|
||||
|
||||
|
||||
class TurnOut(BaseModel):
|
||||
@@ -85,6 +88,8 @@ class TurnOut(BaseModel):
|
||||
error: str
|
||||
#: 复核结论。None = 这一轮没复核过,和「复核过、没发现问题」不是一回事
|
||||
review: dict[str, Any] | None = None
|
||||
#: 可信度元数据。老轮次挂在 review.meta 下,读出来时统一放到这里
|
||||
meta: dict[str, Any] | None = None
|
||||
created_at: Any = None
|
||||
|
||||
model_config = {"from_attributes": True}
|
||||
@@ -101,6 +106,11 @@ class ConversationOut(BaseModel):
|
||||
turn_count: int = 0
|
||||
#: 列表里给个副标题,让人一眼认出是哪次聊天
|
||||
last_question: str = ""
|
||||
#: 最后一轮停在哪:running | waiting | error | cancelled | suspended | done,
|
||||
#: 没有轮次时为 None。见 last_status()
|
||||
last_status: str | None = None
|
||||
#: 最后一轮的运行。建图阶段就断了的没有
|
||||
last_run_id: str | None = None
|
||||
|
||||
model_config = {"from_attributes": True}
|
||||
|
||||
@@ -114,14 +124,119 @@ class ConversationDetail(ConversationOut):
|
||||
model_config = {"from_attributes": False}
|
||||
|
||||
|
||||
def turn_meta(turn: Any) -> dict[str, Any] | None:
|
||||
"""这一轮的元数据:meta 列优先,老数据退回 review.meta。"""
|
||||
if isinstance(turn.meta, dict):
|
||||
return turn.meta
|
||||
bridged = turn.review.get("meta") if isinstance(turn.review, dict) else None
|
||||
return bridged if isinstance(bridged, dict) else None
|
||||
|
||||
|
||||
def _turn_out(turn: ConversationTurn) -> TurnOut:
|
||||
out = TurnOut.model_validate(turn)
|
||||
out.meta = turn_meta(turn)
|
||||
return out
|
||||
|
||||
|
||||
def _detail(row: Conversation, turns: list[ConversationTurn]) -> ConversationDetail:
|
||||
base = ConversationOut.model_validate(row)
|
||||
return ConversationDetail(
|
||||
**base.model_dump(),
|
||||
turns=[TurnOut.model_validate(t) for t in turns],
|
||||
turns=[_turn_out(t) for t in turns],
|
||||
)
|
||||
|
||||
|
||||
#: 还没有运行的轮次停在 running 超过这么久,就当建图断了。建图跟着浏览器那条流走,
|
||||
#: 页面一关就停,轮次却一直是 running;最长的正常情况是模型超时再加两轮自查修正
|
||||
BUILD_STALE = timedelta(minutes=15)
|
||||
|
||||
|
||||
def last_status(
|
||||
turn: Any, run_status: str | None, pending: bool, now: datetime | None = None,
|
||||
) -> str:
|
||||
"""一轮停在哪。轮次状态只有 running / done / error 三种,细分要看运行和元数据。
|
||||
|
||||
轮次状态是前端写的:页面关掉、服务重启时它停在 running,这时要以运行的状态为准;
|
||||
运行也没有的,是断在了建图阶段。已经收尾的轮次以轮次自己为准——它写下的就是
|
||||
用户看到的结局(取消、服务重启挂起在 error 里分开记)。
|
||||
"""
|
||||
outcome = (turn_meta(turn) or {}).get("outcome")
|
||||
if turn.status == "done":
|
||||
return "done"
|
||||
if turn.status == "error":
|
||||
if outcome == "cancelled" or (turn.error or "").strip() == "已取消":
|
||||
return "cancelled"
|
||||
if outcome == "suspended":
|
||||
# 挂起之后可能已经在别处接着跑了
|
||||
live = _from_run(run_status, pending)
|
||||
return live if live in ("running", "waiting") else "suspended"
|
||||
return "error"
|
||||
live = _from_run(run_status, pending)
|
||||
if live:
|
||||
return live
|
||||
updated = turn.updated_at
|
||||
if updated is not None and updated.tzinfo is None:
|
||||
updated = updated.replace(tzinfo=timezone.utc)
|
||||
stale = updated is not None and (now or utcnow()) - updated > BUILD_STALE
|
||||
return "error" if stale else "running"
|
||||
|
||||
|
||||
def _from_run(run_status: str | None, pending: bool) -> str | None:
|
||||
return {
|
||||
"queued": "running", "running": "running",
|
||||
"interrupted": "waiting" if pending else "suspended",
|
||||
"succeeded": "done", "failed": "error", "cancelled": "cancelled",
|
||||
}.get(run_status or "")
|
||||
|
||||
|
||||
async def _last_turns(
|
||||
session: AsyncSession, ids: list[str]
|
||||
) -> dict[str, ConversationTurn]:
|
||||
"""每个会话 seq 最大的那一轮。一次查完,列表长了也只是一次往返。"""
|
||||
latest = (
|
||||
select(ConversationTurn.conversation_id, func.max(ConversationTurn.seq).label("seq"))
|
||||
.where(ConversationTurn.conversation_id.in_(ids))
|
||||
.group_by(ConversationTurn.conversation_id)
|
||||
.subquery()
|
||||
)
|
||||
rows = (await session.execute(
|
||||
select(ConversationTurn).join(latest, and_(
|
||||
ConversationTurn.conversation_id == latest.c.conversation_id,
|
||||
ConversationTurn.seq == latest.c.seq,
|
||||
))
|
||||
)).scalars()
|
||||
return {t.conversation_id: t for t in rows}
|
||||
|
||||
|
||||
async def _statuses(
|
||||
session: AsyncSession, turns: dict[str, ConversationTurn]
|
||||
) -> dict[str, tuple[str, str | None]]:
|
||||
"""会话 id → (last_status, last_run_id)。运行状态和待审批各一次查询。"""
|
||||
run_of = {
|
||||
cid: t.run_id or (turn_meta(t) or {}).get("runId") or None for cid, t in turns.items()
|
||||
}
|
||||
run_ids = {r for r in run_of.values() if r}
|
||||
runs: dict[str, str] = {}
|
||||
waiting: set[str] = set()
|
||||
if run_ids:
|
||||
runs = dict((await session.execute(
|
||||
select(Run.id, Run.status).where(Run.id.in_(run_ids))
|
||||
)).all())
|
||||
stopped = [r for r, st in runs.items() if st == "interrupted"]
|
||||
if stopped:
|
||||
waiting = set((await session.execute(
|
||||
select(Approval.run_id).where(
|
||||
Approval.run_id.in_(stopped), Approval.status == "pending")
|
||||
)).scalars())
|
||||
now = utcnow()
|
||||
out: dict[str, tuple[str, str | None]] = {}
|
||||
for cid, turn in turns.items():
|
||||
run_id = run_of[cid] if run_of[cid] in runs else None
|
||||
out[cid] = (last_status(turn, runs.get(run_id or ""), run_id in waiting, now),
|
||||
run_id)
|
||||
return out
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# 会话
|
||||
# --------------------------------------------------------------------------
|
||||
@@ -152,34 +267,24 @@ async def list_conversations(
|
||||
if not rows:
|
||||
return []
|
||||
|
||||
# 轮数和最后一问一次查完。每条会话各查一次的话,列表长了就是几十次往返
|
||||
# 轮数、最后一轮、它的运行状态一次查完。每条会话各查一次的话,列表长了就是
|
||||
# 几十次往返。状态要带上:没打开过的会话也得标得出「在等审批」「失败了」
|
||||
ids = [r.id for r in rows]
|
||||
counts = dict((await session.execute(
|
||||
select(ConversationTurn.conversation_id, func.count(ConversationTurn.id))
|
||||
.where(ConversationTurn.conversation_id.in_(ids))
|
||||
.group_by(ConversationTurn.conversation_id)
|
||||
)).all())
|
||||
last_seq = dict((await session.execute(
|
||||
select(ConversationTurn.conversation_id, func.max(ConversationTurn.seq))
|
||||
.where(ConversationTurn.conversation_id.in_(ids))
|
||||
.group_by(ConversationTurn.conversation_id)
|
||||
)).all())
|
||||
last_questions: dict[str, str] = {}
|
||||
if last_seq:
|
||||
pairs = list(last_seq.items())
|
||||
rows_q = list((await session.execute(
|
||||
select(ConversationTurn.conversation_id, ConversationTurn.seq, ConversationTurn.question)
|
||||
.where(ConversationTurn.conversation_id.in_([c for c, _ in pairs]))
|
||||
)).all())
|
||||
for conv_id, seq, question in rows_q:
|
||||
if last_seq.get(conv_id) == seq:
|
||||
last_questions[conv_id] = question or ""
|
||||
last = await _last_turns(session, ids)
|
||||
status = await _statuses(session, last)
|
||||
|
||||
out: list[ConversationOut] = []
|
||||
for row in rows:
|
||||
item = ConversationOut.model_validate(row)
|
||||
item.turn_count = int(counts.get(row.id, 0))
|
||||
item.last_question = last_questions.get(row.id, "")
|
||||
if row.id in last:
|
||||
item.last_question = last[row.id].question or ""
|
||||
item.last_status, item.last_run_id = status[row.id]
|
||||
out.append(item)
|
||||
return out
|
||||
|
||||
@@ -212,6 +317,8 @@ async def get_conversation(
|
||||
out = _detail(row, turns)
|
||||
out.turn_count = len(turns)
|
||||
out.last_question = turns[-1].question if turns else ""
|
||||
if turns:
|
||||
out.last_status, out.last_run_id = (await _statuses(session, {row.id: turns[-1]}))[row.id]
|
||||
return out
|
||||
|
||||
|
||||
@@ -275,7 +382,7 @@ async def create_turn(
|
||||
|
||||
await session.commit()
|
||||
await session.refresh(turn)
|
||||
return TurnOut.model_validate(turn)
|
||||
return _turn_out(turn)
|
||||
|
||||
|
||||
@router.patch("/{conversation_id}/turns/{turn_id}", response_model=TurnOut)
|
||||
@@ -290,7 +397,7 @@ async def patch_turn(
|
||||
raise HTTPException(404, "这一轮对话不存在,可能已经被删了")
|
||||
|
||||
for field in (
|
||||
"question", "answer", "explanation", "graph", "run_id", "status", "error", "review",
|
||||
"question", "answer", "explanation", "graph", "run_id", "status", "error", "review", "meta",
|
||||
):
|
||||
value = getattr(payload, field)
|
||||
if value is not None:
|
||||
@@ -301,7 +408,7 @@ async def patch_turn(
|
||||
conversation.last_active_at = utcnow()
|
||||
await session.commit()
|
||||
await session.refresh(turn)
|
||||
return TurnOut.model_validate(turn)
|
||||
return _turn_out(turn)
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
+384
-43
@@ -1,7 +1,9 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import copy
|
||||
import json
|
||||
import re
|
||||
import time
|
||||
from typing import Any
|
||||
|
||||
@@ -11,11 +13,11 @@ from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
# 起别名:本文件底下有个叫 explain 的接口,generate 里又有个局部变量叫 raw,同名会互相盖掉
|
||||
from app.api.errors import explain as explain_error, graph_error, not_configured, raw as raw_error
|
||||
from app.core.errors import explain as explain_error, graph_error, not_configured, raw as raw_error
|
||||
from app.db.base import get_session
|
||||
from app.db.models import Workflow
|
||||
from app.engine.layout import CORRIDOR_MIN, MIN_ROW_GAP, NODE_W, _height, auto_layout
|
||||
from app.engine.schema import GraphSpec, NodeType, validate_graph
|
||||
from app.engine.schema import GraphSpec, NodeType, ValidationIssue, validate_graph
|
||||
from app.engine.state import message_text
|
||||
from app.providers.factory import ModelSpec, ProviderNotConfigured, get_chat_model
|
||||
from app.tools.registry import all_specs
|
||||
@@ -23,7 +25,40 @@ from app.tools.registry import all_specs
|
||||
router = APIRouter(prefix="/api/copilot", tags=["copilot"])
|
||||
|
||||
|
||||
async def _tool_catalog(session: AsyncSession) -> str:
|
||||
async def _sources(session: AsyncSession, scope: list[str] | None) -> list[Any]:
|
||||
"""这一轮给助手看的数据源:启用着的,限定了范围就只留点名的(id、名字都认)。
|
||||
|
||||
点名的一个都不在(删了、停用了)就直接拒绝:悄悄退回「全部数据源」等于
|
||||
无视用户的限定,而他正是因为想换一个库问才点的。
|
||||
"""
|
||||
from app.db.models import DataSource
|
||||
|
||||
rows = list((
|
||||
await session.execute(
|
||||
select(DataSource).where(DataSource.enabled.is_(True)).order_by(DataSource.name)
|
||||
)
|
||||
).scalars())
|
||||
wanted = {s.strip() for s in scope or [] if isinstance(s, str) and s.strip()}
|
||||
if not wanted:
|
||||
return rows
|
||||
picked = [r for r in rows if r.id in wanted or r.name in wanted]
|
||||
if not picked:
|
||||
raise HTTPException(
|
||||
400, "限定的数据源都不在了:可能已经被删掉或停用。去掉限定再问,"
|
||||
"或者到「数据」页确认它还在、而且是启用的",
|
||||
)
|
||||
return picked
|
||||
|
||||
|
||||
def _scope_note(rows: list[Any], scoped: bool) -> str:
|
||||
if not scoped:
|
||||
return ""
|
||||
names = "、".join(r.name for r in rows)
|
||||
return (f"\n\n这一轮用户把数据源限定为:{names}。只查它们,只能用它们的 db_query__ / "
|
||||
"db_schema__ 工具;前几轮用过别的库,这一轮也不要沿用")
|
||||
|
||||
|
||||
def _tool_catalog(rows: list[Any]) -> str:
|
||||
"""给 Copilot 看的工具清单。
|
||||
|
||||
all_specs() 只有静态注册的内置工具,数据源工具是运行时按库动态生成的
|
||||
@@ -36,14 +71,8 @@ async def _tool_catalog(session: AsyncSession) -> str:
|
||||
]
|
||||
|
||||
from app.data import introspect as _introspect
|
||||
from app.db.models import DataSource
|
||||
from app.tools.datasource import QUERY_PREFIX, SCHEMA_PREFIX
|
||||
|
||||
rows = list((
|
||||
await session.execute(
|
||||
select(DataSource).where(DataSource.enabled.is_(True)).order_by(DataSource.name)
|
||||
)
|
||||
).scalars())
|
||||
for row in rows:
|
||||
tables = _introspect.table_names(row)
|
||||
listed = "、".join(tables[:12]) + (f" 等 {len(tables)} 个对象" if len(tables) > 12 else "")
|
||||
@@ -68,16 +97,10 @@ _DATASOURCE_BUDGET_CHARS = 6000
|
||||
_DETAIL_TABLE_LIMIT = 40
|
||||
|
||||
|
||||
async def _datasource_section(session: AsyncSession) -> str:
|
||||
def _datasource_section(rows: list[Any]) -> str:
|
||||
"""数据源与结构摘要。没有数据源时返回空串,不占 prompt。"""
|
||||
from app.data import introspect as _introspect
|
||||
from app.db.models import DataSource
|
||||
|
||||
rows = list((
|
||||
await session.execute(
|
||||
select(DataSource).where(DataSource.enabled.is_(True)).order_by(DataSource.name)
|
||||
)
|
||||
).scalars())
|
||||
if not rows:
|
||||
return ""
|
||||
|
||||
@@ -190,6 +213,8 @@ class GenerateIn(BaseModel):
|
||||
intent: str = Field(default="build")
|
||||
#: 属于哪次对话。带上它,这一轮才知道前面聊过什么
|
||||
conversation_id: str | None = None
|
||||
#: 这一轮只查这几个数据源(id 或名字)。不传或空表示不限
|
||||
datasource_ids: list[str] | None = None
|
||||
|
||||
|
||||
def _user_message(payload: GenerateIn, *, patch: bool) -> str:
|
||||
@@ -395,6 +420,8 @@ class GenerateOut(BaseModel):
|
||||
explanation: str = ""
|
||||
issues: list[dict[str, Any]] = Field(default_factory=list)
|
||||
layout: dict[str, Any] = Field(default_factory=dict)
|
||||
#: 改图前后都在、工具绑定却变了的节点和成员,见 tool_changes()
|
||||
tool_changes: list[dict[str, Any]] = Field(default_factory=list)
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
@@ -545,8 +572,10 @@ async def generate(
|
||||
产物会经过和手工编排完全相同的校验与排版,所以生成完就是可运行、可读的,
|
||||
而不是一段还要人去修的 JSON。
|
||||
"""
|
||||
tool_list = await _tool_catalog(session)
|
||||
datasources = await _datasource_section(session)
|
||||
sources = await _sources(session, payload.datasource_ids)
|
||||
scope = {r.name for r in sources} if payload.datasource_ids else None
|
||||
tool_list = _tool_catalog(sources)
|
||||
datasources = _datasource_section(sources) + _scope_note(sources, scope is not None)
|
||||
|
||||
system = (
|
||||
"你是一个 agent 工作流编排专家。根据用户需求产出一张可执行的工作流图。\n\n"
|
||||
@@ -620,11 +649,16 @@ async def generate(
|
||||
|
||||
spec, layout = _layout_keeping(spec, _pinned_positions(payload.base_graph))
|
||||
report = validate_graph(spec)
|
||||
out = spec.model_dump(mode="json")
|
||||
changes = tool_changes((payload.base_graph or {}).get("nodes") or [], out["nodes"])
|
||||
return GenerateOut(
|
||||
graph=spec.model_dump(mode="json"),
|
||||
graph=out,
|
||||
explanation=str(raw.get("explanation", "")),
|
||||
issues=[i.model_dump() for i in report.issues],
|
||||
issues=[i.model_dump() for i in report.issues]
|
||||
+ _issue_dicts(scope_issues(out["nodes"], scope), "datasource_out_of_scope")
|
||||
+ dropped_tool_warnings(changes, payload.instruction),
|
||||
layout=layout,
|
||||
tool_changes=changes,
|
||||
)
|
||||
|
||||
|
||||
@@ -638,13 +672,16 @@ _STREAM_PROTOCOL = """\
|
||||
- 可用操作:
|
||||
{"op":"plan","summary":"一句话说明打算怎么搭"} ← 第一行
|
||||
{"op":"add_node","node":{"id":"...","type":"...","label":"中文标签","config":{...}}}
|
||||
{"op":"update_node","id":"...","label":"...","config":{...}} ← 修改现有节点(config 整体替换)
|
||||
{"op":"update_node","id":"...","label":"...","config":{...}} ← 修改现有节点(config 只写要改的字段)
|
||||
{"op":"remove_node","id":"..."}
|
||||
{"op":"add_edge","edge":{"source":"...","target":"...","sourceHandle":"..."}}
|
||||
{"op":"remove_edge","source":"...","target":"...","sourceHandle":"..."}
|
||||
{"op":"done","explanation":"两三句话说明这张图怎么跑","run":true} ← 最后一行
|
||||
- 按执行顺序添加节点(先入口后出口);每加一个节点,立刻把连向它的边(两端都已存在的)输出出来,让图连贯地生长
|
||||
- 修改现有图时只输出改动,没提到的节点不要动
|
||||
- update_node 按字段合并:config 只写要改的字段,没写的(tools、assign_to…)原样保留;
|
||||
要删掉某个字段就写成 null。supervisor 的 agents 按成员 name 合并:只写要改的成员和字段,
|
||||
成员没写 tools 就沿用它原来的工具;新名字是新增成员;去掉成员写 {"name":"…","remove":true}
|
||||
- done 里的 run:这张图建完要不要立刻执行。默认 true;用户要的是**流程本身**
|
||||
("设计一个每天跑的工作流")时给 false —— 他要的是这张图,不是这一次的结果
|
||||
- 如果这句话根本不需要工作流(见下面的路径约定),**第一行也是最后一行**就输出:
|
||||
@@ -669,6 +706,64 @@ def _parse_op_line(line: str) -> dict[str, Any] | None:
|
||||
|
||||
_NODE_TYPES = frozenset(t.value for t in NodeType)
|
||||
|
||||
#: 协作成员条目里写 "remove": true 就把这个成员拿掉。不复用 null:null 在合并里
|
||||
#: 的意思是「删这个字段」,成员本身不是一个字段
|
||||
_REMOVE_MEMBER = "remove"
|
||||
|
||||
|
||||
def _member_key(member: dict[str, Any], index: int) -> str:
|
||||
# 和 engine/nodes/multi.py 的叫法一致:没起名字的成员按位置叫 agentN
|
||||
return str(member.get("name") or f"agent{index}")
|
||||
|
||||
|
||||
def _merge_fields(old: dict[str, Any], patch: dict[str, Any]) -> dict[str, Any]:
|
||||
"""按字段合并:写了的覆盖,写 null 的删掉,没写的原样保留。返回新 dict。"""
|
||||
out = dict(old)
|
||||
for key, value in patch.items():
|
||||
if value is None:
|
||||
out.pop(key, None)
|
||||
else:
|
||||
out[key] = value
|
||||
return out
|
||||
|
||||
|
||||
def _merge_members(old: list[Any], patch: list[Any]) -> list[Any]:
|
||||
"""协作成员按 name 合并。原有成员保持原来的次序,新名字接在后面。"""
|
||||
merged: list[Any] = list(old)
|
||||
where = {_member_key(m, i): i for i, m in enumerate(merged) if isinstance(m, dict)}
|
||||
dropped: set[int] = set()
|
||||
for i, entry in enumerate(patch):
|
||||
if not isinstance(entry, dict):
|
||||
continue
|
||||
key = _member_key(entry, i)
|
||||
if entry.get(_REMOVE_MEMBER) is True:
|
||||
if key in where:
|
||||
dropped.add(where[key])
|
||||
continue
|
||||
fields = {k: v for k, v in entry.items() if k != _REMOVE_MEMBER}
|
||||
if key in where:
|
||||
merged[where[key]] = _merge_fields(merged[where[key]], fields)
|
||||
else:
|
||||
where[key] = len(merged)
|
||||
merged.append({k: v for k, v in fields.items() if v is not None})
|
||||
return [m for i, m in enumerate(merged) if i not in dropped]
|
||||
|
||||
|
||||
def merge_node_config(
|
||||
node_type: str, old: dict[str, Any], patch: dict[str, Any]
|
||||
) -> dict[str, Any]:
|
||||
"""update_node 的 config 怎么落到节点上。前端 store/studio.ts 必须是同一套规则。
|
||||
|
||||
以前是整体替换:模型只想改一句提示词,照协议也得把整份 config 抄一遍,
|
||||
漏抄的字段就没了。真出过事——改写提示词时漏了 tools,三个要查库的 agent
|
||||
全丢了工具,一次库都没查,最后把一堆工具调用的原始标记当答案交了出去。
|
||||
改成按字段合并之后,漏写等于不改;真要删一个字段得显式写 null。
|
||||
"""
|
||||
if node_type == NodeType.SUPERVISOR.value and isinstance(patch.get("agents"), list):
|
||||
members = old.get("agents") if isinstance(old.get("agents"), list) else []
|
||||
patch = {**patch, "agents": _merge_members(members, patch["agents"])}
|
||||
return _merge_fields(old, patch)
|
||||
|
||||
|
||||
def _apply_op(nodes: dict[str, dict[str, Any]], edges: list[dict[str, Any]], op: dict[str, Any]) -> bool:
|
||||
"""把一个操作应用到服务端维护的图状态。返回是否真的改了图。"""
|
||||
@@ -690,10 +785,12 @@ def _apply_op(nodes: dict[str, dict[str, Any]], edges: list[dict[str, Any]], op:
|
||||
node = nodes.get(op.get("id") or "")
|
||||
if not node:
|
||||
return False
|
||||
data = node.setdefault("data", {})
|
||||
if op.get("label") is not None:
|
||||
node["data"]["label"] = op["label"]
|
||||
if op.get("config") is not None:
|
||||
node["data"]["config"] = op["config"]
|
||||
data["label"] = op["label"]
|
||||
if isinstance(op.get("config"), dict):
|
||||
data["config"] = merge_node_config(
|
||||
str(node.get("type") or ""), data.get("config") or {}, op["config"])
|
||||
return True
|
||||
if kind == "remove_node":
|
||||
node_id = op.get("id")
|
||||
@@ -809,21 +906,250 @@ def _sse(obj: dict[str, Any]) -> str:
|
||||
return f"data: {json.dumps(obj, ensure_ascii=False)}\n\n"
|
||||
|
||||
|
||||
def _blocking_issues(nodes: dict[str, dict[str, Any]], edges: list[dict[str, Any]]) -> list[Any] | None:
|
||||
"""按运行时同一套校验,挑出会挡住运行的问题。图本身不成形时返回 None。"""
|
||||
def _blocking_issues(
|
||||
nodes: dict[str, dict[str, Any]], edges: list[dict[str, Any]], scope: set[str] | None = None,
|
||||
) -> list[dict[str, Any]] | None:
|
||||
"""按运行时同一套校验,挑出会挡住运行的问题。图本身不成形时返回 None。
|
||||
|
||||
限定了数据源时,用了范围外的库也算:用户点了「只查这个库」,拿别的库的数
|
||||
回答他,比跑不起来更糟——答案看起来是对的。
|
||||
|
||||
每条是 ValidationIssue 的字典形状(node_id、field 都在),和 final.issues 一样:
|
||||
界面靠 node_id 定位卡片、靠 field 落到具体的输入框,不用从一行字里抠节点 id。
|
||||
"""
|
||||
try:
|
||||
spec = GraphSpec.model_validate({"nodes": list(nodes.values()), "edges": edges})
|
||||
except Exception: # noqa: BLE001 - 结构不合法留给收尾那一步报
|
||||
return None
|
||||
return [i for i in validate_graph(spec).issues if i.level == "error"]
|
||||
return ([i.model_dump() for i in validate_graph(spec).issues if i.level == "error"]
|
||||
+ _issue_dicts(scope_issues(list(nodes.values()), scope), "datasource_out_of_scope"))
|
||||
|
||||
|
||||
def _issue_line(issue: Any) -> str:
|
||||
return f"「{issue.node_id}」{issue.message}" if issue.node_id else issue.message
|
||||
def _source_of(tool: str) -> str | None:
|
||||
from app.tools.datasource import QUERY_PREFIX, SCHEMA_PREFIX
|
||||
|
||||
for prefix in (QUERY_PREFIX, SCHEMA_PREFIX):
|
||||
if tool.startswith(prefix):
|
||||
return tool[len(prefix):]
|
||||
return None
|
||||
|
||||
|
||||
def scope_issues(nodes: list[dict[str, Any]], scope: set[str] | None) -> list[ValidationIssue]:
|
||||
"""用了限定范围之外的数据源的节点,一个节点一条。"""
|
||||
if scope is None:
|
||||
return []
|
||||
out: list[ValidationIssue] = []
|
||||
for node in nodes:
|
||||
cfg = (node.get("data") or {}).get("config") or {}
|
||||
names = _tool_names(cfg.get("tools")) + _tool_names([cfg.get("tool")])
|
||||
for member in cfg.get("agents") or []:
|
||||
if isinstance(member, dict):
|
||||
names += _tool_names(member.get("tools"))
|
||||
outside = sorted({src for src in map(_source_of, names) if src and src not in scope})
|
||||
if outside:
|
||||
out.append(ValidationIssue(
|
||||
level="error", node_id=node.get("id"),
|
||||
message=f"用了限定范围之外的数据源 {'、'.join(outside)}:这一轮只查 "
|
||||
f"{'、'.join(sorted(scope))},改用它们的 db_query__ / db_schema__ 工具",
|
||||
))
|
||||
return out
|
||||
|
||||
|
||||
def _issue_dicts(issues: list[ValidationIssue], code: str) -> list[dict[str, Any]]:
|
||||
return [{**i.model_dump(), "code": code} for i in issues]
|
||||
|
||||
|
||||
def _issue_line(issue: dict[str, Any]) -> str:
|
||||
"""交回模型的修正请求里,一条问题一行,节点 id 在前。"""
|
||||
return f"「{issue['node_id']}」{issue['message']}" if issue.get("node_id") else issue["message"]
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# 工具绑定变化:改图前后,每个 agent 和每个协作成员各绑了哪些工具
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
|
||||
def _tool_names(value: Any) -> list[str]:
|
||||
return [t for t in value if isinstance(t, str) and t] if isinstance(value, list) else []
|
||||
|
||||
|
||||
def _tool_bindings(nodes: list[dict[str, Any]]) -> dict[tuple[str, str | None], dict[str, Any]]:
|
||||
"""(节点 id, 成员名) → {label, tools, field}。成员名为 None 的是 agent 节点本身。"""
|
||||
out: dict[tuple[str, str | None], dict[str, Any]] = {}
|
||||
for node in nodes:
|
||||
data = node.get("data") or {}
|
||||
cfg = data.get("config") or {}
|
||||
label = data.get("label") or node.get("id")
|
||||
if node.get("type") == NodeType.AGENT.value:
|
||||
out[(node["id"], None)] = {"label": label, "field": "tools",
|
||||
"tools": _tool_names(cfg.get("tools"))}
|
||||
elif node.get("type") == NodeType.SUPERVISOR.value:
|
||||
for i, member in enumerate(cfg.get("agents") or []):
|
||||
if isinstance(member, dict):
|
||||
out[(node["id"], _member_key(member, i))] = {
|
||||
"label": label, "field": f"agents[{i}].tools",
|
||||
"tools": _tool_names(member.get("tools")),
|
||||
}
|
||||
return out
|
||||
|
||||
|
||||
def tool_changes(
|
||||
before: list[dict[str, Any]], after: list[dict[str, Any]]
|
||||
) -> list[dict[str, Any]]:
|
||||
"""改图前后都在、而绑定的工具不一样了的节点和成员,按改后的图排序。
|
||||
|
||||
新加的、删掉的节点不算:那是结构变化,改图回执本来就会列出来。这里要抓的
|
||||
是「节点还在、工具悄悄没了」——画布上看起来什么都没变,跑起来才发现查不了库。
|
||||
"""
|
||||
old = _tool_bindings(before)
|
||||
out: list[dict[str, Any]] = []
|
||||
for (node_id, member), now in _tool_bindings(after).items():
|
||||
was = old.get((node_id, member))
|
||||
if was is None or set(was["tools"]) == set(now["tools"]):
|
||||
continue
|
||||
out.append({
|
||||
"node_id": node_id, "label": now["label"], "member": member, "field": now["field"],
|
||||
"before": was["tools"], "after": now["tools"],
|
||||
"added": [t for t in now["tools"] if t not in was["tools"]],
|
||||
"removed": [t for t in was["tools"] if t not in now["tools"]],
|
||||
})
|
||||
return out
|
||||
|
||||
|
||||
# 指令里有没有让删工具。只认「删除动词直接管着工具」:动词和「工具 / 工具名」
|
||||
# 挨着、在同一个分句里、中间没有夹别的动作。以前是整句里有「不要 / 不用 / 取消」、
|
||||
# 别处又有「工具」就算——「这次要真的用工具查库,不要编造数字」被当成让删工具,
|
||||
# 警告关掉,没工具的图照样自动运行。宁可在真让删时多提醒一句,也不能漏掉这种。
|
||||
|
||||
#: 删除动词。「不要 / 不用 / 别」只在直接接着工具(或接「用 / 绑」再接工具)时才算
|
||||
_DROP_VERB = re.compile(
|
||||
r"去掉|去除|删掉|删除|删了|删去|移除|拿掉|撤掉|撤下|解绑|解除绑定|禁用|停用|卸掉|剔除|砍掉|清空|取消"
|
||||
r"|(?:不要|不用|不需要|无需|用不着|不再|别)再?(?:使用|调用|绑定|用|绑|挂)?"
|
||||
r"|\b(?:remov(?:e|ing)|drop(?:ping)?|unbind|detach|disable|without|get rid of|no longer use)\b"
|
||||
r"|\b(?:don't|do not|never|stop) us(?:e|ing)\b"
|
||||
)
|
||||
#: 工具在前、动词在后:「把查表的工具去掉」「db_schema__shop 不要了」
|
||||
_DROP_AFTER = re.compile(
|
||||
r"去掉|去除|删掉|删除|删了|删去|移除|拿掉|撤掉|撤下|解绑|禁用|停用|卸掉|剔除|砍掉|清空"
|
||||
r"|(?:不要|不用|不需要|用不着)再?(?:用|绑)?(?:了|$)|\b(?:removed|dropped)\b"
|
||||
)
|
||||
#: 「只保留 X」:别的可以删,但 X 本身不能跟着没了,更不能删光。「只用」只认点了名的——
|
||||
#: 「只用工具查库」说的是别编,不是让删哪个工具
|
||||
_KEEP_ONLY = re.compile(r"(只保留|只留下|只留|仅保留|\bkeep only\b|\bonly keep\b)"
|
||||
r"|(只用|仅用|\bonly use\b)")
|
||||
#: 动词和工具之间夹了这些,动词管的就不是工具了:「不用改工具」「别用假设的方式调用工具」
|
||||
_GAP_BREAK = re.compile(
|
||||
r"改|动|换|变成|编|捏造|虚构|假设|假装|模拟|猜|然后|接着|并且|同时|但|而是|让|把|将|保留|留着|保持|只|必须|一定"
|
||||
r"|make up|made up|making up|\b(?:then|but|instead|change|edit|modify|keep|fake|invent|pretend)\b"
|
||||
)
|
||||
#: 否定。中文只往前看几个字(「不要去掉工具」「别把工具删了」),英文的否定离动词远,看整个分句
|
||||
_NEGATED = re.compile(r"不要|不用|不能|不许|不准|不得|不可|不必|不想|不希望|不让|无需|没让|别|勿|千万")
|
||||
_NEGATED_EN = re.compile(r"\b(?:don't|dont|do not|never|no need|not|can't|cannot)\b")
|
||||
#: 工具后面紧跟着这些,说的是留着它:「工具保留」「工具不用改」
|
||||
_KEPT_AFTER = re.compile(r"\s*(?:都|也|就|还|先)?(?:保留|留着|不变|照旧|保持|别动|不动|不要动|不用动|"
|
||||
r"不要改|不用改|别改|别删|不要删|不能删)|\s*(?:stay|unchanged|as is)\b")
|
||||
_CLAUSE = re.compile(r"[,,。;;!!??\n]|\.(?=\s|$)")
|
||||
_GENERIC = ("工具", "tool", "tools")
|
||||
_GAP, _GAP_AFTER, _LOOKBACK = 16, 8, 10
|
||||
|
||||
|
||||
def _tool_object(removed: list[str]) -> re.Pattern[str]:
|
||||
"""「工具」或一个工具名:这次删掉的、内置的,以及带 __ 的(db_query__x、MCP 工具)。
|
||||
|
||||
只认像工具的名字:表名、字段名也是下划线写法,「去掉 sales_daily 相关的工具」
|
||||
里点名的不是工具。
|
||||
"""
|
||||
names = sorted({t.lower() for t in [*removed, *all_specs()] if t}, key=len, reverse=True)
|
||||
return re.compile("|".join([*map(re.escape, names), r"[a-z][a-z0-9_]*__[a-z0-9_-]+",
|
||||
"工具", r"\btools?\b"]))
|
||||
|
||||
|
||||
def _negated(clause: str, pos: int) -> bool:
|
||||
return bool(_NEGATED.search(clause, max(0, pos - _LOOKBACK), pos)
|
||||
or _NEGATED_EN.search(clause, 0, pos))
|
||||
|
||||
|
||||
def _drop_request(instruction: str, removed: list[str]) -> tuple[bool, set[str], set[str] | None]:
|
||||
"""指令里让删了哪些工具:(泛指「工具」, 点了名的, 「只保留」留下的——没说只保留是 None)。"""
|
||||
target = _tool_object(removed)
|
||||
dropped: set[str] = set()
|
||||
kept: set[str] | None = None
|
||||
|
||||
def near(clause: str, start: int) -> re.Match[str] | None:
|
||||
obj = target.search(clause, start)
|
||||
return obj if obj and obj.start() - start <= _GAP else None
|
||||
|
||||
for clause in _CLAUSE.split((instruction or "").lower()):
|
||||
for verb in _DROP_VERB.finditer(clause):
|
||||
obj = near(clause, verb.end())
|
||||
if not obj:
|
||||
continue
|
||||
gap = clause[verb.end():obj.start()]
|
||||
if (_GAP_BREAK.search(gap) or _DROP_VERB.search(gap) or _negated(clause, verb.start())
|
||||
or _KEPT_AFTER.match(clause, obj.end())):
|
||||
continue
|
||||
dropped.add(obj.group())
|
||||
for obj in target.finditer(clause):
|
||||
verb = _DROP_AFTER.search(clause, obj.end())
|
||||
if not verb or verb.start() - obj.end() > _GAP_AFTER:
|
||||
continue
|
||||
gap = clause[obj.end():verb.start()]
|
||||
if _GAP_BREAK.search(gap) or _NEGATED.search(gap) or _negated(clause, obj.start()):
|
||||
continue
|
||||
dropped.add(obj.group())
|
||||
for verb in _KEEP_ONLY.finditer(clause):
|
||||
obj = near(clause, verb.end())
|
||||
if obj and not (verb.group(2) and obj.group() in _GENERIC):
|
||||
kept = (kept or set()) | {obj.group()}
|
||||
return bool(dropped & set(_GENERIC)), dropped - set(_GENERIC), kept
|
||||
|
||||
|
||||
def _unasked_drops(instruction: str, change: dict[str, Any]) -> list[str]:
|
||||
"""这次少掉的工具里,哪些是指令没让删的。"""
|
||||
generic, named, kept = _drop_request(instruction, change["removed"])
|
||||
if generic:
|
||||
return []
|
||||
|
||||
def asked(tool: str) -> bool:
|
||||
if tool.lower() in named:
|
||||
return True
|
||||
# 「只保留 X」:X 以外的可以删;但一个都不剩就不是「保留」了
|
||||
return kept is not None and bool(change["after"]) and tool.lower() not in kept
|
||||
|
||||
return [t for t in change["removed"] if not asked(t)]
|
||||
|
||||
|
||||
def dropped_tool_warnings(changes: list[dict[str, Any]], instruction: str) -> list[dict[str, Any]]:
|
||||
"""工具集合变小了、而这一轮没让删工具的,逐个报 warning。
|
||||
|
||||
只报变小的:把 db_query__a 换成 db_query__b 是正常的改图,数量没少不必大惊
|
||||
小怪。也不打回去让模型重改——删工具有可能是对的,该让人看一眼,而不是再
|
||||
赌一次模型。
|
||||
"""
|
||||
out: list[dict[str, Any]] = []
|
||||
for c in changes:
|
||||
if not c["removed"] or len(set(c["after"])) >= len(set(c["before"])):
|
||||
continue
|
||||
unasked = _unasked_drops(instruction, c)
|
||||
if not unasked:
|
||||
continue
|
||||
who = f"「{c['label']}」" + (f"的成员「{c['member']}」" if c["member"] else "")
|
||||
now = f" {'、'.join(c['after'])}" if c["after"] else "空"
|
||||
said = ("这一轮的要求里没有提到去掉工具" if len(unasked) == len(c["removed"])
|
||||
else f"这一轮的要求里没有提到去掉 {'、'.join(unasked)}")
|
||||
cost = ("没有绑定工具,它只能「假设」调用,查不了库" if not c["after"]
|
||||
else f"{'、'.join(unasked)} 它就调不到了")
|
||||
out.append({
|
||||
"level": "warning", "node_id": c["node_id"], "edge_id": None,
|
||||
"code": "tools_dropped", "field": c["field"],
|
||||
"message": f"{who}的工具从 {'、'.join(c['before'])} 变成了{now}。"
|
||||
f"{said},确认一下是不是改漏了:{cost}",
|
||||
})
|
||||
return out
|
||||
|
||||
|
||||
def _repair_request(
|
||||
nodes: dict[str, dict[str, Any]], edges: list[dict[str, Any]], errors: list[Any],
|
||||
nodes: dict[str, dict[str, Any]], edges: list[dict[str, Any]], errors: list[dict[str, Any]],
|
||||
) -> str:
|
||||
graph = {"nodes": list(nodes.values()), "edges": edges}
|
||||
return (
|
||||
@@ -832,7 +1158,8 @@ def _repair_request(
|
||||
"上线前自查(和真正运行时同一套规则)发现下面这些问题,不改的话这张图跑不起来:\n"
|
||||
+ "\n".join(f"- {_issue_line(i)}" for i in errors)
|
||||
+ "\n\n只修这些问题,别的不要动。用操作流输出修正:改节点用 update_node"
|
||||
"(config 整体替换,要带上这个节点完整的 config);缺节点、缺边就补上。最后一行输出 done。"
|
||||
"(config 只写要改的字段,没写的保持原样;要删掉一个字段写 null);"
|
||||
"缺节点、缺边就补上。最后一行输出 done。"
|
||||
)
|
||||
|
||||
|
||||
@@ -845,8 +1172,10 @@ async def generate_stream(payload: GenerateIn, session: AsyncSession = Depends(g
|
||||
"""
|
||||
from fastapi.responses import StreamingResponse
|
||||
|
||||
tool_list = await _tool_catalog(session)
|
||||
datasources = await _datasource_section(session)
|
||||
sources = await _sources(session, payload.datasource_ids)
|
||||
scope = {r.name for r in sources} if payload.datasource_ids else None
|
||||
tool_list = _tool_catalog(sources)
|
||||
datasources = _datasource_section(sources) + _scope_note(sources, scope is not None)
|
||||
system = (
|
||||
"你是一个 agent 工作流编排专家。根据用户需求,以操作流的方式逐步搭出一张可执行的工作流图。\n\n"
|
||||
f"{NODE_REFERENCE}\n\n可用工具:\n{tool_list}{datasources}\n\n"
|
||||
@@ -865,8 +1194,9 @@ async def generate_stream(payload: GenerateIn, session: AsyncSession = Depends(g
|
||||
# 用户摆好的坐标,收尾排版时原样留着
|
||||
pinned = _pinned_positions(payload.base_graph)
|
||||
if payload.base_graph and payload.base_graph.get("nodes"):
|
||||
# 拷一份再改:改之前的样子还要留着比对工具绑定有没有被改掉
|
||||
for n in payload.base_graph["nodes"]:
|
||||
nodes[n["id"]] = n
|
||||
nodes[n["id"]] = copy.deepcopy(n)
|
||||
edges = [
|
||||
{k: v for k, v in e.items() if k in ("source", "target", "sourceHandle")}
|
||||
for e in payload.base_graph.get("edges") or []
|
||||
@@ -889,6 +1219,9 @@ async def generate_stream(payload: GenerateIn, session: AsyncSession = Depends(g
|
||||
("human", _with_history(user, await _history_section(session, payload.conversation_id)))]
|
||||
# 兜底用:模型只发了改动操作时,把它们重放到这张图上
|
||||
prev_graph = await _previous_graph(session, payload.conversation_id)
|
||||
# 比对工具绑定的基准:画布上是改之前的图;问数据页是上一轮的图(沿用它的节点 id)
|
||||
baseline = copy.deepcopy(
|
||||
(payload.base_graph or {}).get("nodes") or (prev_graph or {}).get("nodes") or [])
|
||||
|
||||
async def event_stream():
|
||||
explanation = ""
|
||||
@@ -939,7 +1272,7 @@ async def generate_stream(payload: GenerateIn, session: AsyncSession = Depends(g
|
||||
if not nodes and prev_graph and seen_ops:
|
||||
for n in prev_graph.get("nodes") or []:
|
||||
if n.get("id"):
|
||||
nodes[n["id"]] = n
|
||||
nodes[n["id"]] = copy.deepcopy(n)
|
||||
# 原地改,不能重新绑定:给 edges 赋值会让它变成 event_stream 的局部
|
||||
# 变量,而它在上面 _apply_op 那一路已经被读过了 —— UnboundLocalError
|
||||
edges[:] = [
|
||||
@@ -954,11 +1287,10 @@ async def generate_stream(payload: GenerateIn, session: AsyncSession = Depends(g
|
||||
# 循环 / 分支条件里套了 {{ }}),前面的步骤白跑,报错还停在半路
|
||||
repaired = 0
|
||||
for round_no in range(1, _SELF_CHECK_ROUNDS + 1):
|
||||
errors = _blocking_issues(nodes, edges)
|
||||
errors = _blocking_issues(nodes, edges, scope)
|
||||
if not errors:
|
||||
break
|
||||
yield _sse({"op": "check", "status": "repairing", "round": round_no,
|
||||
"issues": [_issue_line(i) for i in errors]})
|
||||
yield _sse({"op": "check", "status": "repairing", "round": round_no, "issues": errors})
|
||||
fix = [("system", system), ("human", _repair_request(nodes, edges, errors))]
|
||||
try:
|
||||
async for op in _with_heartbeat(_iter_ops(model, fix), phase="repairing"):
|
||||
@@ -973,15 +1305,20 @@ async def generate_stream(payload: GenerateIn, session: AsyncSession = Depends(g
|
||||
"message": f"自查修正没跑成:{explain_error(e)[0]}", "detail": raw_error(e)})
|
||||
break
|
||||
repaired = round_no
|
||||
remaining = _blocking_issues(nodes, edges)
|
||||
remaining = _blocking_issues(nodes, edges, scope)
|
||||
# 工具绑定变化不挡运行,但要在自查这一步就说出来:工具被改没了的图照样
|
||||
# 能跑,只是跑出来的是模型「假设」查过库的答案
|
||||
changes = tool_changes(baseline, list(nodes.values()))
|
||||
warnings = dropped_tool_warnings(changes, payload.instruction)
|
||||
if remaining:
|
||||
# 改不好也照实交付、列出问题,但不自动运行:明知跑不起来的图,开跑只会
|
||||
# 在半路报一个用户看不懂的错
|
||||
autorun = False
|
||||
yield _sse({"op": "check", "status": "failed",
|
||||
"issues": [_issue_line(i) for i in remaining]})
|
||||
yield _sse({"op": "check", "status": "failed", "issues": remaining,
|
||||
"warnings": warnings})
|
||||
elif remaining is not None:
|
||||
yield _sse({"op": "check", "status": "passed", "repaired": repaired})
|
||||
yield _sse({"op": "check", "status": "passed", "repaired": repaired,
|
||||
"warnings": warnings})
|
||||
|
||||
# 收尾:排版 + 校验,把最终图整体交付。改图时旧节点留在原处,只排新节点
|
||||
try:
|
||||
@@ -989,6 +1326,8 @@ async def generate_stream(payload: GenerateIn, session: AsyncSession = Depends(g
|
||||
GraphSpec.model_validate({"nodes": list(nodes.values()), "edges": edges}), pinned,
|
||||
)
|
||||
issues = [i.model_dump() for i in validate_graph(spec).issues]
|
||||
issues += _issue_dicts(scope_issues(list(nodes.values()), scope),
|
||||
"datasource_out_of_scope")
|
||||
# 跳过的节点要说出来,不能安静地少一步。code 给前端认:这一类要单独
|
||||
# 提示「少了一步」,不能和普通校验警告混在一起
|
||||
issues += [
|
||||
@@ -1000,10 +1339,12 @@ async def generate_stream(payload: GenerateIn, session: AsyncSession = Depends(g
|
||||
final = {
|
||||
"op": "final",
|
||||
"graph": spec.model_dump(mode="json"),
|
||||
"issues": issues,
|
||||
"issues": issues + warnings,
|
||||
"explanation": explanation,
|
||||
"autorun": autorun,
|
||||
"layout": layout,
|
||||
# 改图回执:哪些节点、成员的工具变了。画布上看不出来,得明说
|
||||
"tool_changes": changes,
|
||||
}
|
||||
except Exception as e: # noqa: BLE001
|
||||
final = {"op": "error", "message": f"模型交回的工作流结构不对:{graph_error(e)}",
|
||||
|
||||
+131
-14
@@ -8,16 +8,20 @@ from __future__ import annotations
|
||||
from datetime import datetime, timezone
|
||||
from typing import Any
|
||||
|
||||
from fastapi import APIRouter, Depends, File, Form, HTTPException, UploadFile
|
||||
from fastapi import APIRouter, Depends, File, Form, HTTPException, Query, UploadFile
|
||||
from pydantic import BaseModel, Field
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.api.errors import explain, first_line, raw
|
||||
from app.api import health
|
||||
from app.core.errors import explain, first_line, raw
|
||||
from app.core.config import settings
|
||||
from app.core.crypto import encrypt, mask
|
||||
from app.data import introspect as introspect_mod
|
||||
from app.data.engine import SUPPORTED_KINDS, build_url, engine_args, engines
|
||||
from app.data.engine import (
|
||||
MAX_QUERY_TIMEOUT_S, QUERY_TIMEOUT_OPTION, SUPPORTED_KINDS, build_url, engine_args, engines,
|
||||
query_timeout_problem,
|
||||
)
|
||||
from app.db.base import get_session
|
||||
from app.db.models import DataSource
|
||||
from app.tools.datasource import tool_names
|
||||
@@ -79,12 +83,27 @@ class DataSourceOut(BaseModel):
|
||||
#: 这台服务器上还有哪些库/schema 可选。探不到表时它就是下一步的线索
|
||||
available_schemas: list[str] = Field(default_factory=list)
|
||||
tools: list[str] = Field(default_factory=list)
|
||||
#: 缓存是按哪个 schema 探的:"" 是默认 schema,None 是没有缓存(或老缓存没记)。
|
||||
#: 和 options.schema 对不上时,助手看到的结构不是配置里那个 schema 的
|
||||
cached_schema: str | None = None
|
||||
#: 最近一次测连接(见 app/api/health.py)。没测过、或者连接配置改过之后都是 None
|
||||
last_checked_at: str | None = None
|
||||
last_check_ok: bool | None = None
|
||||
last_latency_ms: int | None = None
|
||||
last_error: str | None = None
|
||||
|
||||
|
||||
def _cached_schema(cache: dict[str, Any]) -> str | None:
|
||||
if not cache or "schema" not in cache:
|
||||
return None
|
||||
return str(cache.get("schema") or "")
|
||||
|
||||
|
||||
def _to_out(row: DataSource) -> DataSourceOut:
|
||||
cache = row.schema_cache or {}
|
||||
tables = cache.get("tables") or {}
|
||||
return DataSourceOut(
|
||||
cached_schema=_cached_schema(cache),
|
||||
schema_error=str(cache.get("error") or "") if cache.get("failed") else "",
|
||||
available_schemas=list(cache.get("available_schemas") or []),
|
||||
id=row.id, name=row.name, kind=row.kind, host=row.host, port=row.port,
|
||||
@@ -95,9 +114,27 @@ def _to_out(row: DataSource) -> DataSourceOut:
|
||||
table_count=len(tables),
|
||||
schema_synced_at=row.schema_synced_at.isoformat() if row.schema_synced_at else None,
|
||||
tools=tool_names(row),
|
||||
**health.fields(row.last_check),
|
||||
)
|
||||
|
||||
|
||||
def _connection(source: Any) -> str | None:
|
||||
"""连接配置的指纹:驱动实际拿到的连接串和参数。配置拼不出连接串时为 None。
|
||||
|
||||
按连接串比而不是按字段比:Oracle 的服务名填在「数据库」还是 options 里,
|
||||
连的是同一个库;探查用的 schema 不进连接串,改它也不影响连不连得上。
|
||||
"""
|
||||
try:
|
||||
url, extra = engine_args(source)
|
||||
except Exception: # noqa: BLE001 - 拼不出来就谈不上「测的是同一份」
|
||||
return None
|
||||
return health.fingerprint("datasource", url, extra)
|
||||
|
||||
|
||||
def _probe_record(result: dict[str, Any]) -> dict[str, Any]:
|
||||
return health.record(result.get("ok"), result.get("elapsed_ms"), result.get("error"))
|
||||
|
||||
|
||||
async def _get_or_404(session: AsyncSession, source_id: str) -> DataSource:
|
||||
row = await session.get(DataSource, source_id)
|
||||
if not row:
|
||||
@@ -111,6 +148,19 @@ async def list_sources(session: AsyncSession = Depends(get_session)) -> list[Dat
|
||||
return [_to_out(r) for r in rows]
|
||||
|
||||
|
||||
#: 每种库都有的「查询时限」。数据库按它自己停下语句,不只是后端不再等
|
||||
_TIMEOUT_FIELD = {
|
||||
"key": QUERY_TIMEOUT_OPTION, "label": "查询时限(秒)", "placeholder": "30",
|
||||
"help": f"一条查询最多跑多久,到点由数据库自己停下;留空是 30 秒,最多 {MAX_QUERY_TIMEOUT_S} 秒",
|
||||
}
|
||||
|
||||
|
||||
def _refuse_bad_options(options: dict[str, Any] | None) -> None:
|
||||
problem = query_timeout_problem(options)
|
||||
if problem:
|
||||
raise HTTPException(422, problem)
|
||||
|
||||
|
||||
@router.get("/kinds")
|
||||
async def list_kinds() -> dict[str, Any]:
|
||||
"""支持哪些数据库,以及各自需要填什么——前端表单据此渲染。
|
||||
@@ -124,11 +174,12 @@ async def list_kinds() -> dict[str, Any]:
|
||||
{"value": "mysql", "label": "MySQL / MariaDB", "default_port": 3306,
|
||||
"needs": ["host", "database", "username", "password"],
|
||||
"hint": "字符集默认 utf8mb4,要换就在「高级连接参数」里加 charset",
|
||||
"advanced": [{"key": "charset", "label": "字符集", "placeholder": "utf8mb4"}]},
|
||||
"advanced": [{"key": "charset", "label": "字符集", "placeholder": "utf8mb4"},
|
||||
_TIMEOUT_FIELD]},
|
||||
{"value": "postgres", "label": "PostgreSQL", "default_port": 5432,
|
||||
"needs": ["host", "database", "username", "password"],
|
||||
"hint": "「schema」留空就用 public",
|
||||
"advanced": []},
|
||||
"advanced": [_TIMEOUT_FIELD]},
|
||||
{"value": "oracle", "label": "Oracle", "default_port": 1521,
|
||||
"needs": ["host", "username", "password"],
|
||||
"hint": "service_name 和 SID 二选一:一般填 service_name,老库只给了 SID 就切到 SID。"
|
||||
@@ -139,10 +190,11 @@ async def list_kinds() -> dict[str, Any]:
|
||||
{"key": "service_name", "label": "service_name", "placeholder": "ORCLPDB1",
|
||||
"help": "和 SID 二选一"},
|
||||
{"key": "sid", "label": "SID", "placeholder": "ORCL", "help": "和 service_name 二选一"},
|
||||
_TIMEOUT_FIELD,
|
||||
]},
|
||||
{"value": "sqlite", "label": "SQLite(文件)", "default_port": None,
|
||||
"needs": ["database"], "hint": "「数据库文件路径」填 .db 文件的绝对路径",
|
||||
"advanced": []},
|
||||
"advanced": [_TIMEOUT_FIELD]},
|
||||
],
|
||||
"supported": list(SUPPORTED_KINDS),
|
||||
}
|
||||
@@ -189,12 +241,15 @@ async def create_source(
|
||||
)).scalar_one_or_none()
|
||||
if exists:
|
||||
raise HTTPException(409, f"已经有叫「{payload.name}」的数据源了,换个标识")
|
||||
_refuse_bad_options(payload.options)
|
||||
|
||||
row = DataSource(
|
||||
**payload.model_dump(exclude={"password"}),
|
||||
password=encrypt(payload.password),
|
||||
)
|
||||
_settle_oracle(row, None, None)
|
||||
# 表单里刚测过这份配置:保存下来就带着那次结果,不必再测一遍
|
||||
row.last_check = health.draft_result(_connection(row))
|
||||
session.add(row)
|
||||
await session.commit()
|
||||
await session.refresh(row)
|
||||
@@ -207,13 +262,20 @@ async def update_source(
|
||||
) -> DataSourceOut:
|
||||
row = await _get_or_404(session, source_id)
|
||||
data = payload.model_dump(exclude_unset=True)
|
||||
if "options" in data:
|
||||
_refuse_bad_options(data["options"])
|
||||
if "password" in data:
|
||||
# 空串表示清空,非空表示换新的
|
||||
data["password"] = encrypt(data["password"]) if data["password"] else None
|
||||
old_options, old_database = dict(row.options or {}), row.database
|
||||
before = _connection(row)
|
||||
for key, value in data.items():
|
||||
setattr(row, key, value)
|
||||
_settle_oracle(row, old_options, old_database)
|
||||
after = _connection(row)
|
||||
if after != before:
|
||||
# 连的已经不是测过的那个了:旧结果作废,表单里测过这一份的话换成那一次
|
||||
row.last_check = health.draft_result(after)
|
||||
await session.commit()
|
||||
await session.refresh(row)
|
||||
# 连接参数可能变了,旧连接池不能再用——否则改完密码还在用旧连接,很难排查
|
||||
@@ -386,27 +448,72 @@ async def test_draft(
|
||||
return {"ok": False, "error": f"不支持「{payload.kind}」这种数据库",
|
||||
"hint": f"支持:{'、'.join(SUPPORTED_KINDS)}", "detail": "", "elapsed_ms": 0, "url": ""}
|
||||
saved = await session.get(DataSource, payload.id) if payload.id else None
|
||||
return await _probe(_draft_source(payload, saved), cached=False)
|
||||
draft = _draft_source(payload, saved)
|
||||
result = await _probe(draft, cached=False)
|
||||
last, key = _probe_record(result), _connection(draft)
|
||||
health.remember_draft(key, last)
|
||||
if saved is not None and key is not None and key == _connection(saved):
|
||||
# 编辑框里没改连接、只是再测一次:测的就是已保存的那份
|
||||
saved.last_check = last
|
||||
await session.commit()
|
||||
return result
|
||||
|
||||
|
||||
@router.post("/{source_id}/test")
|
||||
async def test_source(source_id: str, session: AsyncSession = Depends(get_session)) -> dict[str, Any]:
|
||||
"""测连接。失败时说清原因和怎么办,驱动的原始报错放在 detail——这类问题九成靠它定位。"""
|
||||
row = await _get_or_404(session, source_id)
|
||||
return await _probe(row, cached=True)
|
||||
result = await _probe(row, cached=True)
|
||||
row.last_check = _probe_record(result)
|
||||
await session.commit()
|
||||
return result
|
||||
|
||||
|
||||
@router.post("/{source_id}/introspect", response_model=DataSourceOut)
|
||||
class IntrospectPreview(BaseModel):
|
||||
"""只看不存的探查结果。缓存、同步时间都没动。"""
|
||||
|
||||
dry_run: bool = True
|
||||
#: 这次探的是哪个 schema,"" 是默认 schema
|
||||
schema_: str = Field(default="", serialization_alias="schema")
|
||||
table_count: int = 0
|
||||
#: 带 schema 前缀的对象全名,最多 200 个(和真探查的上限一样)
|
||||
tables: list[str] = Field(default_factory=list)
|
||||
truncated: bool = False
|
||||
total: int = 0
|
||||
schema_error: str = ""
|
||||
available_schemas: list[str] = Field(default_factory=list)
|
||||
|
||||
|
||||
@router.post("/{source_id}/introspect", response_model=DataSourceOut | IntrospectPreview)
|
||||
async def introspect_source(
|
||||
source_id: str, schema: str | None = None, session: AsyncSession = Depends(get_session)
|
||||
) -> DataSourceOut:
|
||||
"""探查结构并缓存。显式动作,不做后台轮询——生产库不该被实验工具定时扫。"""
|
||||
source_id: str,
|
||||
schema: str | None = None,
|
||||
dry_run: bool = Query(default=False, description="只返回探到的结构,不写缓存"),
|
||||
session: AsyncSession = Depends(get_session),
|
||||
) -> DataSourceOut | IntrospectPreview:
|
||||
"""探查结构并缓存。显式动作,不做后台轮询——生产库不该被实验工具定时扫。
|
||||
|
||||
dry_run=true 只看不存:「换个 schema 看看」以前一探就把缓存换掉,用户随后
|
||||
选了「不改」,助手此刻看到的也已经是另一套表了。
|
||||
"""
|
||||
row = await _get_or_404(session, source_id)
|
||||
try:
|
||||
row.schema_cache = await introspect_mod.introspect(row, schema=schema)
|
||||
cache = await introspect_mod.introspect(row, schema=schema)
|
||||
except Exception as e: # noqa: BLE001
|
||||
reason, hint = _explain_connect(e, (row.kind or "").lower())
|
||||
raise HTTPException(400, f"探查表结构失败:{reason}" + (f"。{hint}" if hint else "")) from e
|
||||
if dry_run:
|
||||
tables = cache.get("tables") or {}
|
||||
return IntrospectPreview(
|
||||
schema_=str(cache.get("schema") or ""),
|
||||
table_count=len(tables),
|
||||
tables=[m.get("qualified", n) for n, m in tables.items()],
|
||||
truncated=bool(cache.get("truncated")),
|
||||
total=int(cache.get("total") or 0),
|
||||
schema_error=str(cache.get("error") or "") if cache.get("failed") else "",
|
||||
available_schemas=list(cache.get("available_schemas") or []),
|
||||
)
|
||||
row.schema_cache = cache
|
||||
# 失败也要把缓存存下来——里面的 available_schemas 正是下一步的线索——
|
||||
# 但**不能盖上"已同步"的戳**。以前盖了,于是界面显示同步过、工具却说
|
||||
# "还没探查过",两边都不说真话(run 554a0f92)
|
||||
@@ -422,7 +529,17 @@ async def get_schema(source_id: str, table: str | None = None,
|
||||
session: AsyncSession = Depends(get_session)) -> dict[str, Any]:
|
||||
row = await _get_or_404(session, source_id)
|
||||
if table:
|
||||
return {"table": table, "detail": introspect_mod.describe_table(row, table)}
|
||||
meta = introspect_mod.find_table(row, table)
|
||||
return {
|
||||
"table": table,
|
||||
# 给模型看的那段文本照旧;界面画表格用下面的结构化字段
|
||||
"detail": introspect_mod.describe_table(row, table),
|
||||
"found": meta is not None,
|
||||
"qualified": meta.get("qualified", table) if meta else None,
|
||||
"kind": ("view" if meta.get("is_view") else "table") if meta else None,
|
||||
"comment": meta.get("comment") if meta else None,
|
||||
"columns": introspect_mod.table_columns(meta) if meta else [],
|
||||
}
|
||||
return {
|
||||
"tables": introspect_mod.table_names(row),
|
||||
"summary": introspect_mod.summary(row),
|
||||
|
||||
@@ -1,139 +0,0 @@
|
||||
"""面向用户的报错:说清发生了什么、为什么、怎么办,不把 Python 异常类名端上去。
|
||||
|
||||
以前各处都是 f"{type(e).__name__}: {e}",界面首行红字是
|
||||
「_make_query_tool.<locals>._run() got an unexpected keyword argument 'query'」
|
||||
——对业务用户是天书,也没有下一步。原始异常不丢:调用方把 raw(e) 放进响应的
|
||||
detail 字段或写进日志,排查时照样找得到。
|
||||
|
||||
判断按类名走 MRO,不 import 各家 SDK:没装的驱动和 SDK 不该让报错这一层
|
||||
也跟着 ImportError。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from typing import Any
|
||||
|
||||
_TIMEOUT = {"TimeoutError", "TimeoutException", "APITimeoutError", "ReadTimeout",
|
||||
"ConnectTimeout", "PoolTimeout", "WriteTimeout"}
|
||||
_NETWORK = {"ConnectError", "ConnectionError", "APIConnectionError", "gaierror",
|
||||
"ClientConnectorError", "RemoteProtocolError"}
|
||||
_AUTH = {"AuthenticationError", "PermissionDeniedError", "InvalidPasswordError",
|
||||
"InvalidAuthorizationSpecificationError"}
|
||||
#: 这几类是程序自己的毛病,str(e) 里全是内部符号,原样端上去只会更糊涂
|
||||
_BUG = {"TypeError", "AttributeError", "KeyError", "NameError", "IndexError",
|
||||
"UnboundLocalError", "AssertionError", "RecursionError"}
|
||||
|
||||
#: SQLAlchemy 包过的驱动异常开头是「(sqlite3.OperationalError) 」
|
||||
_WRAPPED = re.compile(r"^\([\w.]+\)\s*")
|
||||
|
||||
|
||||
def raw(e: BaseException) -> str:
|
||||
"""原始报错,给「技术细节」折叠区和日志用。"""
|
||||
return f"{type(e).__name__}: {e}"
|
||||
|
||||
|
||||
def _status(e: BaseException) -> int | None:
|
||||
for obj in (e, getattr(e, "response", None)):
|
||||
code = getattr(obj, "status_code", None)
|
||||
if isinstance(code, int):
|
||||
return code
|
||||
return None
|
||||
|
||||
|
||||
def first_line(e: BaseException) -> str:
|
||||
"""异常自己的说明,去掉驱动包装的类名前缀和 SQLAlchemy 的文档链接。"""
|
||||
text = str(e).strip()
|
||||
line = text.splitlines()[0] if text else ""
|
||||
return _WRAPPED.sub("", line)[:300]
|
||||
|
||||
|
||||
def explain(e: BaseException) -> tuple[str, str]:
|
||||
"""(原因, 怎么办)。原因是一句人话;说不出怎么办时第二项是空串。"""
|
||||
names = {c.__name__ for c in type(e).__mro__}
|
||||
low = str(e).lower()
|
||||
status = _status(e)
|
||||
|
||||
if status in (401, 403) or names & _AUTH or "invalid api key" in low or "unauthorized" in low:
|
||||
return "对方拒绝了这把密钥", "核对 API Key 有没有填错、过期,或者这个账号有没有权限"
|
||||
if status == 404 or "NotFoundError" in names:
|
||||
return "对方说找不到", "核对地址的路径(常见的是少了或多了 /v1)和模型名"
|
||||
if status == 429 or "RateLimitError" in names:
|
||||
return "请求被对方限流了", "等一会儿再试;频繁出现就检查账号额度"
|
||||
if isinstance(status, int) and status >= 500:
|
||||
return f"对方服务出错了(HTTP {status})", "这是对方的问题,稍后再试"
|
||||
if names & _TIMEOUT or "timed out" in low:
|
||||
return "等了太久没有响应", "对方可能很忙或者网络不稳,稍后再试;一直超时就检查地址和网络"
|
||||
if names & _NETWORK or any(s in low for s in (
|
||||
"connection refused", "nodename nor servname", "name or service not known",
|
||||
"all connection attempts failed", "network is unreachable",
|
||||
)):
|
||||
return "连不上对方的服务", "核对地址和端口,确认服务已经启动、这台机器访问得到它"
|
||||
if "JSONDecodeError" in names:
|
||||
return "对方返回的不是 JSON", "地址多半指到了网页而不是 API,核对 Base URL"
|
||||
if "ValidationError" in names and callable(getattr(e, "errors", None)):
|
||||
return f"参数不对:{_first_validation(e)}", ""
|
||||
if names & _BUG:
|
||||
return "后端内部出错了", "这不是你操作的问题;把技术细节发给维护者"
|
||||
return first_line(e) or "出了一个没有说明的错误", ""
|
||||
|
||||
|
||||
def message(e: BaseException, lead: str = "") -> str:
|
||||
"""一整句:lead + 原因 + 怎么办。给只能放一个字符串的地方(HTTPException 的 detail)。"""
|
||||
reason, hint = explain(e)
|
||||
text = f"{lead}{reason}" if lead else reason
|
||||
return f"{text}。{hint}" if hint else text
|
||||
|
||||
|
||||
def payload(e: BaseException, lead: str = "") -> dict[str, Any]:
|
||||
"""{ok: false, error, hint, detail}:测试连接这类「失败也是正常结果」的接口用。"""
|
||||
reason, hint = explain(e)
|
||||
return {"ok": False, "error": f"{lead}{reason}", "hint": hint, "detail": raw(e)}
|
||||
|
||||
|
||||
def not_configured(e: BaseException) -> str:
|
||||
"""模型接入没配好时的原话用的是内部叫法(provider 'x'、base_url),换成设置页上的说法。"""
|
||||
text = re.sub(r"provider '([^']*)'", r"模型接入「\1」", str(e))
|
||||
text = re.sub(r"模型 '([^']*)'", r"模型「\1」", text)
|
||||
return text.replace("base_url", "Base URL").rstrip("。")
|
||||
|
||||
|
||||
def _has_cjk(text: str) -> bool:
|
||||
return any("一" <= ch <= "鿿" for ch in text)
|
||||
|
||||
|
||||
def _first_validation(e: Any) -> str:
|
||||
err = (e.errors() or [{}])[0]
|
||||
loc = ".".join(str(p) for p in err.get("loc") or ())
|
||||
msg = str(err.get("msg") or "").removeprefix("Value error, ")
|
||||
if _has_cjk(msg):
|
||||
return msg
|
||||
return f"「{loc}」{'缺了' if err.get('type') == 'missing' else '格式不对'}" if loc else "格式不对"
|
||||
|
||||
|
||||
def graph_error(e: BaseException) -> str:
|
||||
"""工作流结构读不懂时,指到是哪个节点、哪一项。
|
||||
|
||||
pydantic 的原文是一大段英文,还列了全部十几种节点类型和一个文档链接,
|
||||
用户从里面找不到自己错在哪。
|
||||
"""
|
||||
errors = e.errors() if callable(getattr(e, "errors", None)) else None
|
||||
if not errors:
|
||||
return explain(e)[0]
|
||||
err = errors[0]
|
||||
loc = list(err.get("loc") or ())
|
||||
msg = str(err.get("msg") or "").removeprefix("Value error, ")
|
||||
more = f"(另有 {len(errors) - 1} 处)" if len(errors) > 1 else ""
|
||||
|
||||
if len(loc) >= 2 and loc[0] in ("nodes", "edges") and isinstance(loc[1], int):
|
||||
where = f"第 {loc[1] + 1} 个{'节点' if loc[0] == 'nodes' else '连线'}"
|
||||
field = str(loc[2]) if len(loc) > 2 else ""
|
||||
if err.get("type") == "enum" and field == "type":
|
||||
return f"{where}的类型「{err.get('input')}」不认识{more}"
|
||||
if err.get("type") == "missing":
|
||||
return f"{where}缺了「{field}」{more}"
|
||||
if _has_cjk(msg):
|
||||
return f"{where}:{msg}{more}"
|
||||
return f"{where}的「{field or '内容'}」格式不对{more}"
|
||||
if _has_cjk(msg):
|
||||
return msg + more
|
||||
return f"「{'.'.join(str(p) for p in loc) or '整体'}」格式不对{more}"
|
||||
@@ -0,0 +1,69 @@
|
||||
"""最近一次测连接的结果:数据源、模型接入、MCP 服务共用的一套记法。
|
||||
|
||||
以前这份结果只在浏览器的 localStorage 里,换台电脑就全回到「未测试」。现在记在
|
||||
对象上(数据源、MCP 是 last_check 列,模型接入是 extra.last_check),列表接口
|
||||
平铺成 last_checked_at / last_check_ok / last_latency_ms / last_error 四个字段。
|
||||
|
||||
表单里「先测再保存」的那一次也要算数:测的是草稿,保存时后端按连接配置的指纹
|
||||
认出「刚测过的就是这一份」,把结果带到新对象上。指纹只在进程里记一小会儿,
|
||||
用的是哈希,不留明文密码。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
import json
|
||||
import time
|
||||
from typing import Any
|
||||
|
||||
from app.db.base import utcnow
|
||||
|
||||
#: 草稿测完多久之内保存还算数。测完去倒杯水回来再点保存,也该认得出来
|
||||
_DRAFT_TTL = 15 * 60
|
||||
_DRAFT_MAX = 64
|
||||
_drafts: dict[str, tuple[float, dict[str, Any]]] = {}
|
||||
|
||||
|
||||
def record(ok: bool, latency_ms: int | None, error: str | None = None) -> dict[str, Any]:
|
||||
return {
|
||||
"at": utcnow().isoformat(),
|
||||
"ok": bool(ok),
|
||||
"latency_ms": None if latency_ms is None else int(latency_ms),
|
||||
"error": None if ok else (error or "连不上"),
|
||||
}
|
||||
|
||||
|
||||
def fields(last: dict[str, Any] | None) -> dict[str, Any]:
|
||||
"""对象上记的那份 → 列表接口的四个字段。没测过全是 None。"""
|
||||
last = last if isinstance(last, dict) else {}
|
||||
ok = last.get("ok")
|
||||
return {
|
||||
"last_checked_at": last.get("at"),
|
||||
"last_check_ok": ok if isinstance(ok, bool) else None,
|
||||
"last_latency_ms": last.get("latency_ms"),
|
||||
"last_error": last.get("error"),
|
||||
}
|
||||
|
||||
|
||||
def fingerprint(*parts: Any) -> str:
|
||||
"""连接配置的指纹。同一份配置得出同一个值;只拿来比对,不可逆。"""
|
||||
blob = json.dumps(parts, sort_keys=True, ensure_ascii=False, default=str)
|
||||
return hashlib.sha256(blob.encode()).hexdigest()
|
||||
|
||||
|
||||
def remember_draft(key: str | None, last: dict[str, Any]) -> None:
|
||||
if not key:
|
||||
return
|
||||
now = time.monotonic()
|
||||
for stale in [k for k, (at, _) in _drafts.items() if now - at > _DRAFT_TTL]:
|
||||
_drafts.pop(stale, None)
|
||||
if len(_drafts) >= _DRAFT_MAX:
|
||||
_drafts.pop(min(_drafts, key=lambda k: _drafts[k][0]), None)
|
||||
_drafts[key] = (now, last)
|
||||
|
||||
|
||||
def draft_result(key: str | None) -> dict[str, Any] | None:
|
||||
"""这份配置最近在表单里测过的结果,没有或太久了返回 None。"""
|
||||
hit = _drafts.get(key or "")
|
||||
if not hit or time.monotonic() - hit[0] > _DRAFT_TTL:
|
||||
return None
|
||||
return dict(hit[1])
|
||||
@@ -10,7 +10,7 @@ from pydantic import BaseModel, Field
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.api.errors import explain, raw
|
||||
from app.core.errors import explain, raw
|
||||
from app.core.config import settings
|
||||
from app.db.base import get_session
|
||||
from app.db.models import Document, MemoryItem, Skill
|
||||
@@ -433,20 +433,129 @@ async def probe_embedding(base_url: str) -> dict[str, Any]:
|
||||
return {"base_url": base_url, "models": models}
|
||||
|
||||
|
||||
class _ReindexJob:
|
||||
"""一次重建索引。进度按批更新:kb / store 每提交一批回调一次,不插值。"""
|
||||
|
||||
def __init__(self, collection: str | None, chunks: int, memories: int) -> None:
|
||||
from app.db.base import new_id, utcnow
|
||||
|
||||
self.id = new_id()
|
||||
self.collection = collection
|
||||
self.state = "running" # running | done | failed
|
||||
# chunks → index(倒排重建,没有分批进度)→ memories → done
|
||||
self.phase = "chunks"
|
||||
self.chunks = {"total": chunks, "done": 0}
|
||||
self.memories = {"total": memories, "done": 0}
|
||||
self.started_at = utcnow()
|
||||
self.finished_at: Any = None
|
||||
self.error: str | None = None
|
||||
self.hint: str | None = None
|
||||
self.result: dict[str, Any] | None = None
|
||||
|
||||
def chunk_step(self, done: int, total: int) -> None:
|
||||
self.chunks = {"total": total, "done": done}
|
||||
if done >= total:
|
||||
self.phase = "index"
|
||||
|
||||
def memory_step(self, done: int, total: int) -> None:
|
||||
self.memories = {"total": total, "done": done}
|
||||
|
||||
def view(self) -> dict[str, Any]:
|
||||
return {
|
||||
"id": self.id, "state": self.state, "phase": self.phase,
|
||||
"collection": self.collection,
|
||||
"total": self.chunks["total"] + self.memories["total"],
|
||||
"done": self.chunks["done"] + self.memories["done"],
|
||||
"chunks": dict(self.chunks), "memories": dict(self.memories),
|
||||
"started_at": self.started_at.isoformat(),
|
||||
"finished_at": self.finished_at.isoformat() if self.finished_at else None,
|
||||
"error": self.error, "hint": self.hint, "result": self.result,
|
||||
}
|
||||
|
||||
|
||||
#: 最近一次重建。只在进程里记:重建是幂等的,服务重启丢了进度,再点一次就是
|
||||
_REINDEX: _ReindexJob | None = None
|
||||
#: 后台任务要有人拿着引用,否则跑到一半可能被回收
|
||||
_REINDEX_TASK: Any = None
|
||||
|
||||
|
||||
def reindex_job() -> _ReindexJob | None:
|
||||
return _REINDEX
|
||||
|
||||
|
||||
async def _run_reindex(job: _ReindexJob) -> None:
|
||||
from app.db.base import SessionLocal, utcnow
|
||||
|
||||
try:
|
||||
async with SessionLocal() as session:
|
||||
out = await kb.reindex(session, job.collection, on_progress=job.chunk_step)
|
||||
job.phase = "memories"
|
||||
# 记忆不按 collection 分,重建就是全量
|
||||
out["memories_reindexed"] = await store.reindex(session, on_progress=job.memory_step)
|
||||
job.result, job.state, job.phase = out, "done", "done"
|
||||
except Exception as e: # noqa: BLE001 - 失败要落到任务状态上,不能只进日志
|
||||
logger.warning("重建索引失败:%s", raw(e))
|
||||
reason, hint = explain(e)
|
||||
job.state, job.error, job.hint = "failed", f"重建索引没做完:{reason}", hint or None
|
||||
finally:
|
||||
job.finished_at = utcnow()
|
||||
|
||||
|
||||
@kb_router.post("/reindex")
|
||||
async def reindex(
|
||||
collection: str | None = None, session: AsyncSession = Depends(get_session)
|
||||
) -> dict[str, Any]:
|
||||
collection: str | None = None,
|
||||
background: bool = Query(default=False, description="后台跑,立刻返回任务;进度用 GET /kb/reindex 查"),
|
||||
session: AsyncSession = Depends(get_session),
|
||||
):
|
||||
"""用当前 embedder 重算向量。换了模型之后唯一的恢复手段。
|
||||
|
||||
知识库和长期记忆一起重建:它们共用一个 embedder,换模型时一起失效。
|
||||
分成两个按钮的结果是点了一个、以为好了,另一个还在悄悄退回关键词——
|
||||
真踩过,记忆那边连个提示都没有。
|
||||
|
||||
几千段配远端 embedding 要好几分钟,压在请求里界面只能干转圈。background=true
|
||||
时立刻返回 202 和任务,按批更新进度;不带它仍然等做完再返回,老调用方不受影响。
|
||||
同一时间只跑一个:两次重建交错写同一批向量,谁最后写赢都说不清。
|
||||
"""
|
||||
out = await kb.reindex(session, collection)
|
||||
# 记忆不按 collection 分,重建就是全量
|
||||
out["memories_reindexed"] = await store.reindex(session)
|
||||
return out
|
||||
global _REINDEX, _REINDEX_TASK
|
||||
import asyncio
|
||||
|
||||
from fastapi.responses import JSONResponse
|
||||
from sqlalchemy import func
|
||||
|
||||
from app.db.models import Chunk
|
||||
|
||||
count = select(func.count(Chunk.id))
|
||||
if collection:
|
||||
count = count.where(Chunk.collection == collection)
|
||||
chunks = int((await session.execute(count)).scalar_one() or 0)
|
||||
memories = int((await session.execute(select(func.count(MemoryItem.id)))).scalar_one() or 0)
|
||||
# 先数完再查「有没有在跑」,查和占位之间不能有 await:以前先查后数,两个同时
|
||||
# 到的请求都在计数那里让出去,回来都以为没在跑,于是两个都起了
|
||||
if _REINDEX is not None and _REINDEX.state == "running":
|
||||
running = _REINDEX.view()
|
||||
raise HTTPException(
|
||||
409, f"已经在重建索引了({_REINDEX.collection or '全部集合'},"
|
||||
f"已完成 {running['done']}/{running['total']}),等它做完再点",
|
||||
)
|
||||
job = _REINDEX = _ReindexJob(collection, chunks, memories)
|
||||
if background:
|
||||
_REINDEX_TASK = asyncio.create_task(_run_reindex(job))
|
||||
return JSONResponse(job.view(), status_code=202)
|
||||
|
||||
await _run_reindex(job)
|
||||
if job.state == "failed":
|
||||
raise HTTPException(502, job.error + (f"。{job.hint}" if job.hint else ""))
|
||||
return job.result
|
||||
|
||||
|
||||
@kb_router.get("/reindex")
|
||||
async def reindex_status() -> dict[str, Any]:
|
||||
"""最近一次重建的进度:{state, phase, total, done, chunks, memories, result, error…}。
|
||||
|
||||
还没重建过返回 {state: "idle"}。
|
||||
"""
|
||||
return _REINDEX.view() if _REINDEX is not None else {"state": "idle"}
|
||||
|
||||
|
||||
#: 单段返回多少字。切块本来就在千字量级,详情页是给人扫一眼"切得对不对"的,
|
||||
|
||||
+28
-9
@@ -172,9 +172,14 @@ async def start_run(
|
||||
return run
|
||||
|
||||
|
||||
#: workflow_id 取这个值时只要没挂在任何工作流上的运行:画布上没保存就跑的临时图,
|
||||
#: 以及工作流被删掉之后留下来的运行(外键是 SET NULL)
|
||||
UNSAVED = "__none__"
|
||||
|
||||
|
||||
@router.get("", response_model=list[RunOut])
|
||||
async def list_runs(
|
||||
workflow_id: str | None = None,
|
||||
workflow_id: str | None = Query(default=None, description=f"{UNSAVED} 表示没保存的工作流"),
|
||||
status: str | None = Query(default=None, description="逗号分隔,如 failed,cancelled"),
|
||||
run_class: str | None = None,
|
||||
q: str | None = Query(default=None, description="按工作流名模糊匹配"),
|
||||
@@ -188,7 +193,9 @@ async def list_runs(
|
||||
筛选都走服务端,翻页用 created_at 做游标(before 传上一页最后一条的 created_at)。
|
||||
"""
|
||||
stmt = select(Run).order_by(Run.created_at.desc()).limit(limit)
|
||||
if workflow_id:
|
||||
if workflow_id == UNSAVED:
|
||||
stmt = stmt.where(Run.workflow_id.is_(None))
|
||||
elif workflow_id:
|
||||
stmt = stmt.where(Run.workflow_id == workflow_id)
|
||||
if statuses := _csv(status):
|
||||
stmt = stmt.where(Run.status.in_(statuses))
|
||||
@@ -283,13 +290,25 @@ async def list_artifacts(
|
||||
|
||||
|
||||
@router.post("/{run_id}/cancel")
|
||||
async def cancel_run(run_id: str) -> dict[str, Any]:
|
||||
stopped = await run_manager.cancel(run_id)
|
||||
if not stopped:
|
||||
raise HTTPException(
|
||||
409, "这次运行已经不在执行中(可能刚结束,或者服务重启过),不需要再停止。刷新看看最新状态"
|
||||
)
|
||||
return {"ok": True}
|
||||
async def cancel_run(run_id: str, x_actor: str | None = Header(default=None)) -> dict[str, Any]:
|
||||
"""停止一次运行。status 是 stopping(正在执行的,取消已发出,稍后收到 run.cancelled)
|
||||
或 cancelled(停在审批上、或服务重启挂起的,已经收成终态,待审批一并关闭)。"""
|
||||
outcome = await run_manager.cancel(run_id, actor=actor_of(x_actor))
|
||||
if outcome is None:
|
||||
async with SessionLocal() as session:
|
||||
run = await session.get(Run, run_id)
|
||||
if run is None:
|
||||
raise HTTPException(404, "运行记录不存在")
|
||||
raise HTTPException(409, _cannot_cancel(run.status))
|
||||
return {"ok": True, "status": outcome}
|
||||
|
||||
|
||||
def _cannot_cancel(status: str) -> str:
|
||||
return {
|
||||
"succeeded": "这次运行已完成,没有要停止的。",
|
||||
"failed": "这次运行已经失败结束了,没有要停止的;要继续请用「接着跑」。",
|
||||
"cancelled": "这次运行已经取消了。",
|
||||
}.get(status, "这次运行已经不在执行中(可能刚结束,或者服务重启过),不需要再停止。刷新看看最新状态")
|
||||
|
||||
|
||||
class ResumeIn(BaseModel):
|
||||
|
||||
+82
-12
@@ -8,7 +8,8 @@ from pydantic import BaseModel, Field
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.api.errors import explain, not_configured, raw
|
||||
from app.api import health
|
||||
from app.core.errors import explain, not_configured, raw
|
||||
from app.core.config import settings as app_settings
|
||||
from app.core.crypto import encrypt, mask
|
||||
from app.db.base import get_session
|
||||
@@ -57,9 +58,49 @@ class ProviderOut(BaseModel):
|
||||
# 只回掩码,明文 key 永远不出后端
|
||||
api_key_masked: str = ""
|
||||
has_key: bool = False
|
||||
#: 最近一次测连接(见 app/api/health.py)。没测过、或者连接配置改过之后都是 None
|
||||
last_checked_at: str | None = None
|
||||
last_check_ok: bool | None = None
|
||||
last_latency_ms: int | None = None
|
||||
last_error: str | None = None
|
||||
|
||||
|
||||
#: extra 里这一项是后端记的最近一次测连接结果,不是用户填的配置:不回显、
|
||||
#: 不接受客户端写入,保存 extra 时原样留着
|
||||
LAST_CHECK = "last_check"
|
||||
|
||||
|
||||
def _user_extra(extra: dict[str, Any] | None) -> dict[str, Any]:
|
||||
return {k: v for k, v in (extra or {}).items() if k != LAST_CHECK}
|
||||
|
||||
|
||||
def _with_saved_headers(extra: dict[str, Any], saved: dict[str, Any] | None) -> dict[str, Any]:
|
||||
"""请求头的值回显的是 ***:原样带回来的,换回存着的真值。
|
||||
|
||||
设置页保存时会把读到的 extra 整块带回来。以前照单全收,于是改个名字、
|
||||
勾一下启用,自定义请求头就全变成了字面上的 ***,接口从此 401。
|
||||
"""
|
||||
headers = extra.get("headers")
|
||||
if not isinstance(headers, dict):
|
||||
return extra
|
||||
old = (saved or {}).get("headers") or {}
|
||||
return {**extra, "headers": {k: (old.get(k, v) if v == "***" else v) for k, v in headers.items()}}
|
||||
|
||||
|
||||
def _connection(provider: Provider, model: str | None = None) -> str:
|
||||
"""连接配置的指纹:类型、地址、实际用的钥匙、请求头这些、测的哪个模型。"""
|
||||
return health.fingerprint(
|
||||
"provider", provider.kind, (provider.base_url or "").strip(), _resolve_key(provider) or "",
|
||||
_user_extra(provider.extra), model or provider.default_model or "",
|
||||
)
|
||||
|
||||
|
||||
def _set_last_check(row: Provider, last: dict[str, Any] | None) -> None:
|
||||
row.extra = {**_user_extra(row.extra), **({LAST_CHECK: last} if last else {})}
|
||||
|
||||
|
||||
def _to_out(row: Provider) -> ProviderOut:
|
||||
extra = _user_extra(row.extra)
|
||||
return ProviderOut(
|
||||
id=row.id,
|
||||
name=row.name,
|
||||
@@ -68,13 +109,14 @@ def _to_out(row: Provider) -> ProviderOut:
|
||||
default_model=row.default_model,
|
||||
models=row.models or [],
|
||||
enabled=row.enabled,
|
||||
extra={k: v for k, v in (row.extra or {}).items() if k != "headers"} | (
|
||||
{"headers": {k: "***" for k in (row.extra or {}).get("headers", {})}}
|
||||
if (row.extra or {}).get("headers")
|
||||
extra={k: v for k, v in extra.items() if k != "headers"} | (
|
||||
{"headers": {k: "***" for k in extra.get("headers", {})}}
|
||||
if extra.get("headers")
|
||||
else {}
|
||||
),
|
||||
api_key_masked=mask(row.api_key),
|
||||
has_key=bool(row.api_key),
|
||||
**health.fields((row.extra or {}).get(LAST_CHECK)),
|
||||
)
|
||||
|
||||
|
||||
@@ -120,8 +162,10 @@ async def create_provider(
|
||||
models=payload.models,
|
||||
default_model=payload.default_model,
|
||||
enabled=payload.enabled,
|
||||
extra=payload.extra,
|
||||
extra=_user_extra(payload.extra),
|
||||
)
|
||||
# 表单里刚测过这份配置:保存下来就带着那次结果
|
||||
_set_last_check(row, health.draft_result(_connection(row)))
|
||||
session.add(row)
|
||||
await session.commit()
|
||||
await session.refresh(row)
|
||||
@@ -137,11 +181,21 @@ async def update_provider(
|
||||
raise HTTPException(404, "这个模型接入不存在,可能已经被删了")
|
||||
|
||||
data = payload.model_dump(exclude_unset=True)
|
||||
before, last = _connection(row), (row.extra or {}).get(LAST_CHECK)
|
||||
if "api_key" in data:
|
||||
row.api_key = encrypt(data.pop("api_key")) if data["api_key"] else None
|
||||
data.pop("api_key", None)
|
||||
if data.get("extra") is not None:
|
||||
data["extra"] = _with_saved_headers(_user_extra(data["extra"]), row.extra)
|
||||
else:
|
||||
data.pop("extra", None)
|
||||
for key, value in data.items():
|
||||
setattr(row, key, value)
|
||||
after = _connection(row)
|
||||
if after != before:
|
||||
# 测过的已经不是这份配置了:旧结果作废,表单里测过这一份的话换成那一次
|
||||
last = health.draft_result(after)
|
||||
_set_last_check(row, last)
|
||||
await session.commit()
|
||||
await session.refresh(row)
|
||||
return _to_out(row)
|
||||
@@ -224,7 +278,16 @@ async def test_provider(
|
||||
row = await session.get(Provider, provider_id)
|
||||
if not row:
|
||||
raise HTTPException(404, "这个模型接入不存在,可能已经被删了")
|
||||
return await _try_model(row, payload.model, payload.prompt)
|
||||
result = await _try_model(row, payload.model, payload.prompt)
|
||||
# 只记默认模型的结果:拿别的模型测出 invalid_model,不等于这个接入连不上
|
||||
if (payload.model or row.default_model) == row.default_model:
|
||||
_set_last_check(row, _test_record(result))
|
||||
await session.commit()
|
||||
return result
|
||||
|
||||
|
||||
def _test_record(result: dict[str, Any]) -> dict[str, Any]:
|
||||
return health.record(result.get("ok"), result.get("latency_ms"), result.get("error"))
|
||||
|
||||
|
||||
class ProviderDraftIn(BaseModel):
|
||||
@@ -249,11 +312,9 @@ def _draft_provider(payload: ProviderDraftIn, saved: Provider | None) -> Provide
|
||||
编辑时 Key 框留空表示不改、请求头回显的是 ***——测试都得换回存着的真值,
|
||||
否则改个 Base URL 想测一下,只会得到一句"密钥不对"。
|
||||
"""
|
||||
extra = dict(payload.extra or {})
|
||||
headers = extra.get("headers")
|
||||
if saved and isinstance(headers, dict):
|
||||
old = (saved.extra or {}).get("headers") or {}
|
||||
extra["headers"] = {k: (old.get(k, v) if v == "***" else v) for k, v in headers.items()}
|
||||
extra = _user_extra(payload.extra)
|
||||
if saved:
|
||||
extra = _with_saved_headers(extra, saved.extra)
|
||||
return Provider(
|
||||
name=payload.name or (saved.name if saved else "草稿"),
|
||||
kind=payload.kind,
|
||||
@@ -284,7 +345,16 @@ async def test_provider_draft(
|
||||
problem = _config_problem(payload.kind, payload.base_url)
|
||||
if problem:
|
||||
return {"ok": False, "error": problem, "hint": "", "detail": ""}
|
||||
return await _try_model(await _draft(payload, session), payload.model, payload.prompt)
|
||||
saved = await session.get(Provider, payload.id) if payload.id else None
|
||||
draft = _draft_provider(payload, saved)
|
||||
result = await _try_model(draft, payload.model, payload.prompt)
|
||||
last, key = _test_record(result), _connection(draft, payload.model)
|
||||
health.remember_draft(key, last)
|
||||
if saved is not None and key == _connection(saved):
|
||||
# 编辑框里没改连接、只是再测一次:测的就是已保存的那份
|
||||
_set_last_check(saved, last)
|
||||
await session.commit()
|
||||
return result
|
||||
|
||||
|
||||
@router.post("/providers/models")
|
||||
|
||||
+155
-26
@@ -7,13 +7,15 @@ from pydantic import BaseModel, Field
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.api.errors import explain, raw
|
||||
from app.api import health
|
||||
from app.core.errors import explain, raw
|
||||
from app.db.base import get_session
|
||||
from app.db.models import CustomTool, McpServer
|
||||
from app.tools.custom import run_custom_tool
|
||||
from app.tools.custom import ToolFailure, schema_problem, trial
|
||||
from app.tools.mcp_manager import mcp_manager
|
||||
from app.tools.registry import (
|
||||
ToolArgsError, ToolContext, all_specs, build_tools, call_is_dangerous, call_tool, get_spec,
|
||||
ToolArgsError, ToolBuildError, ToolContext, all_specs, build_tools, call_is_dangerous,
|
||||
call_tool, get_spec,
|
||||
)
|
||||
|
||||
router = APIRouter(prefix="/api/tools", tags=["tools"])
|
||||
@@ -55,6 +57,8 @@ async def list_tools(session: AsyncSession = Depends(get_session)) -> list[dict[
|
||||
# 在补上之前不能让标签说它会被审批
|
||||
"runtime_approval": False,
|
||||
"schema": row.parameters or {},
|
||||
# 这道关卡之前存进库的坏参数定义:绑上它的节点会失败,列表上先标出来
|
||||
"problem": schema_problem(row.parameters or {}),
|
||||
}
|
||||
)
|
||||
|
||||
@@ -146,12 +150,13 @@ async def run_tool(
|
||||
import time
|
||||
|
||||
started = time.perf_counter()
|
||||
notes: list[str] = []
|
||||
try:
|
||||
result = await call_tool(tool_name, payload.args, ctx, session=session)
|
||||
result = await call_tool(tool_name, payload.args, ctx, session=session, on_fix=notes.append)
|
||||
except KeyError as e:
|
||||
raise HTTPException(404, str(e.args[0] if e.args else e)) from e
|
||||
except ToolArgsError as e:
|
||||
# 参数对不上时报错里已经写清该填什么,原样给
|
||||
except (ToolArgsError, ToolBuildError) as e:
|
||||
# 参数对不上、工具自己的配置写坏了:报错里已经写清该改什么,原样给
|
||||
return {"ok": False, "error": str(e), "hint": "", "detail": raw(e),
|
||||
"duration_ms": int((time.perf_counter() - started) * 1000)}
|
||||
except Exception as e: # noqa: BLE001
|
||||
@@ -167,6 +172,8 @@ async def run_tool(
|
||||
"ok": True,
|
||||
"result": result,
|
||||
"duration_ms": int((time.perf_counter() - started) * 1000),
|
||||
# 参数名写错、被替换成唯一候选跑通了:运行时会报出来,这里也得说
|
||||
**({"note": notes[0]} if notes else {}),
|
||||
}
|
||||
|
||||
|
||||
@@ -181,49 +188,69 @@ class CustomToolIn(BaseModel):
|
||||
name: str = Field(min_length=1, max_length=100, pattern=r"^[a-zA-Z_][a-zA-Z0-9_]*$")
|
||||
description: str = ""
|
||||
kind: str = "http" # http | python
|
||||
parameters: dict[str, Any] = Field(default_factory=dict)
|
||||
#: JSON Schema。不在这里限定成 dict:写成数组、字符串时要回一句人话(见 _refuse_bad_schema),
|
||||
#: 而不是 pydantic 的英文错误列表
|
||||
parameters: Any = Field(default_factory=dict)
|
||||
config: dict[str, Any] = Field(default_factory=dict)
|
||||
enabled: bool = True
|
||||
|
||||
|
||||
class CustomToolOut(CustomToolIn):
|
||||
id: str
|
||||
parameters: dict[str, Any] = Field(default_factory=dict)
|
||||
#: 参数定义哪里写坏了(保存时的关卡加上之前存进库的)。绑上它的节点会失败
|
||||
problem: str | None = None
|
||||
|
||||
model_config = {"from_attributes": True}
|
||||
|
||||
|
||||
def _custom_out(row: CustomTool) -> CustomToolOut:
|
||||
out = CustomToolOut.model_validate(row)
|
||||
out.problem = schema_problem(row.parameters or {})
|
||||
return out
|
||||
|
||||
|
||||
def _refuse_bad_schema(parameters: Any) -> None:
|
||||
"""参数定义写坏了就不让存:存进去之后,绑了它的节点在运行时才炸。"""
|
||||
problem = schema_problem(parameters)
|
||||
if problem:
|
||||
raise HTTPException(422, problem)
|
||||
|
||||
|
||||
@custom_router.get("", response_model=list[CustomToolOut])
|
||||
async def list_custom(session: AsyncSession = Depends(get_session)) -> list[CustomTool]:
|
||||
return list((await session.execute(select(CustomTool))).scalars())
|
||||
async def list_custom(session: AsyncSession = Depends(get_session)) -> list[CustomToolOut]:
|
||||
return [_custom_out(r) for r in (await session.execute(select(CustomTool))).scalars()]
|
||||
|
||||
|
||||
@custom_router.post("", response_model=CustomToolOut, status_code=201)
|
||||
async def create_custom(
|
||||
payload: CustomToolIn, session: AsyncSession = Depends(get_session)
|
||||
) -> CustomTool:
|
||||
) -> CustomToolOut:
|
||||
if (await session.execute(select(CustomTool).where(CustomTool.name == payload.name))).scalar_one_or_none():
|
||||
raise HTTPException(409, f"已经有叫「{payload.name}」的工具了,换个名字")
|
||||
if payload.name in all_specs():
|
||||
raise HTTPException(409, f"「{payload.name}」和内置工具重名了,换个名字")
|
||||
_refuse_bad_schema(payload.parameters)
|
||||
row = CustomTool(**payload.model_dump())
|
||||
session.add(row)
|
||||
await session.commit()
|
||||
await session.refresh(row)
|
||||
return row
|
||||
return _custom_out(row)
|
||||
|
||||
|
||||
@custom_router.patch("/{tool_id}", response_model=CustomToolOut)
|
||||
async def update_custom(
|
||||
tool_id: str, payload: CustomToolIn, session: AsyncSession = Depends(get_session)
|
||||
) -> CustomTool:
|
||||
) -> CustomToolOut:
|
||||
row = await session.get(CustomTool, tool_id)
|
||||
if not row:
|
||||
raise HTTPException(404, "这个工具不存在,可能已经被删了")
|
||||
_refuse_bad_schema(payload.parameters)
|
||||
for key, value in payload.model_dump().items():
|
||||
setattr(row, key, value)
|
||||
await session.commit()
|
||||
await session.refresh(row)
|
||||
return row
|
||||
return _custom_out(row)
|
||||
|
||||
|
||||
@custom_router.delete("/{tool_id}", status_code=204)
|
||||
@@ -235,6 +262,84 @@ async def delete_custom(tool_id: str, session: AsyncSession = Depends(get_sessio
|
||||
await session.commit()
|
||||
|
||||
|
||||
async def _trial(row: CustomTool, args: dict[str, Any]) -> dict[str, Any]:
|
||||
"""试跑一次,返回和工具库「执行」同一种形状:{ok, result | error/hint/detail, duration_ms, note?}。"""
|
||||
import json
|
||||
import time
|
||||
|
||||
ctx = ToolContext(run_id="playground", node_id="test", sandbox_session="playground")
|
||||
started = time.perf_counter()
|
||||
|
||||
def took() -> int:
|
||||
return int((time.perf_counter() - started) * 1000)
|
||||
|
||||
problem = schema_problem(row.parameters or {})
|
||||
if problem:
|
||||
# 手写的参数定义写坏了:是配置的事,不是「后端内部出错」
|
||||
return {"ok": False, "error": problem,
|
||||
"hint": '参数定义是 JSON Schema,写成 {"type": "object", "properties": '
|
||||
'{"n": {"type": "integer", "description": "…"}}, "required": ["n"]}',
|
||||
"detail": "", "duration_ms": 0}
|
||||
try:
|
||||
result, note = await trial(row, args, ctx)
|
||||
except ToolArgsError as e:
|
||||
# 参数对不上时报错里已经写清该填什么,原样给
|
||||
return {"ok": False, "error": str(e), "hint": "", "detail": raw(e), "duration_ms": took()}
|
||||
except Exception as e: # noqa: BLE001
|
||||
reason, hint = explain(e)
|
||||
return {"ok": False, "error": f"工具执行失败:{reason}", "hint": hint, "detail": raw(e),
|
||||
"duration_ms": took()}
|
||||
# 参数名被纠正过:跑通了也要说,用户照着试跑的参数去配工作流
|
||||
extra = {"note": note} if note else {}
|
||||
if isinstance(result, ToolFailure):
|
||||
detail = str(result.get("error") or "")
|
||||
if "exit_code" in result:
|
||||
error = f"代码运行出错(退出码 {result['exit_code']})"
|
||||
hint = "看下面的报错改代码;调用时传的参数在 args 变量里(一个 dict)"
|
||||
else:
|
||||
error = f"工具执行失败:{detail.splitlines()[0] if detail else '没有说明'}"
|
||||
hint = "HTTP 工具只能访问公网地址,内网、回环地址会被拦截" if "拦截" in detail else ""
|
||||
return {"ok": False, "error": error, "hint": hint,
|
||||
"detail": json.dumps(dict(result), ensure_ascii=False), "duration_ms": took(),
|
||||
**extra}
|
||||
return {"ok": True, "result": result, "duration_ms": took(), **extra}
|
||||
|
||||
|
||||
class CustomToolDraftIn(BaseModel):
|
||||
"""还没保存(或改了还没保存)的一份工具配置,外加这次试跑的参数。"""
|
||||
|
||||
name: str = "draft_tool"
|
||||
kind: str = "http"
|
||||
parameters: Any = Field(default_factory=dict)
|
||||
config: dict[str, Any] = Field(default_factory=dict)
|
||||
args: dict[str, Any] = Field(default_factory=dict)
|
||||
|
||||
|
||||
def _draft_problem(kind: str, config: dict[str, Any]) -> str | None:
|
||||
if kind not in ("http", "python"):
|
||||
return f"不认识「{kind}」这种工具类型:只有 http 和 python 两种"
|
||||
if kind == "http" and not str(config.get("url") or "").strip():
|
||||
return "还没填接口地址"
|
||||
if kind == "python" and not str(config.get("code") or "").strip():
|
||||
return "还没写代码"
|
||||
return None
|
||||
|
||||
|
||||
@custom_router.post("/test")
|
||||
async def test_custom_draft(payload: CustomToolDraftIn) -> dict[str, Any]:
|
||||
"""试跑一份没保存的配置,不落库。
|
||||
|
||||
以前只能试已保存的:新建要先保存才能试,改了代码也得先存——保存等于让节点
|
||||
立刻用上一个还没试过的版本。
|
||||
"""
|
||||
problem = _draft_problem(payload.kind, payload.config)
|
||||
if problem:
|
||||
return {"ok": False, "error": problem, "hint": "", "detail": "", "duration_ms": 0}
|
||||
draft = CustomTool(name=payload.name or "draft_tool", kind=payload.kind,
|
||||
parameters=payload.parameters, config=payload.config)
|
||||
return await _trial(draft, payload.args)
|
||||
|
||||
|
||||
@custom_router.post("/{tool_id}/test")
|
||||
async def test_custom(
|
||||
tool_id: str, payload: RunToolIn, session: AsyncSession = Depends(get_session)
|
||||
@@ -242,13 +347,7 @@ async def test_custom(
|
||||
row = await session.get(CustomTool, tool_id)
|
||||
if not row:
|
||||
raise HTTPException(404, "这个工具不存在,可能已经被删了")
|
||||
ctx = ToolContext(run_id="playground", node_id="test", sandbox_session="playground")
|
||||
try:
|
||||
result = await run_custom_tool(session, row.name, payload.args, ctx)
|
||||
except Exception as e: # noqa: BLE001
|
||||
reason, hint = explain(e)
|
||||
return {"ok": False, "error": f"工具执行失败:{reason}", "hint": hint, "detail": raw(e)}
|
||||
return {"ok": True, "result": result}
|
||||
return await _trial(row, payload.args)
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
@@ -273,40 +372,64 @@ class McpOut(McpIn):
|
||||
status: str
|
||||
last_error: str | None
|
||||
tools_cache: list[Any]
|
||||
#: 最近一次连接的时刻和耗时(见 app/api/health.py)。status 只有结论,没有「何时」
|
||||
last_checked_at: str | None = None
|
||||
last_check_ok: bool | None = None
|
||||
last_latency_ms: int | None = None
|
||||
|
||||
model_config = {"from_attributes": True}
|
||||
|
||||
|
||||
def _mcp_out(row: McpServer) -> McpOut:
|
||||
out = McpOut.model_validate(row)
|
||||
checked = health.fields(row.last_check)
|
||||
out.last_checked_at = checked["last_checked_at"]
|
||||
out.last_check_ok = checked["last_check_ok"]
|
||||
out.last_latency_ms = checked["last_latency_ms"]
|
||||
return out
|
||||
|
||||
|
||||
def _mcp_connection(row: McpServer) -> tuple[Any, ...]:
|
||||
return (row.transport, row.command, list(row.args or []), dict(row.env or {}), row.url)
|
||||
|
||||
|
||||
@mcp_router.get("/servers", response_model=list[McpOut])
|
||||
async def list_servers(session: AsyncSession = Depends(get_session)) -> list[McpServer]:
|
||||
return list((await session.execute(select(McpServer))).scalars())
|
||||
async def list_servers(session: AsyncSession = Depends(get_session)) -> list[McpOut]:
|
||||
return [_mcp_out(r) for r in (await session.execute(select(McpServer))).scalars()]
|
||||
|
||||
|
||||
@mcp_router.post("/servers", response_model=McpOut, status_code=201)
|
||||
async def create_server(
|
||||
payload: McpIn, session: AsyncSession = Depends(get_session)
|
||||
) -> McpServer:
|
||||
) -> McpOut:
|
||||
if (await session.execute(select(McpServer).where(McpServer.name == payload.name))).scalar_one_or_none():
|
||||
raise HTTPException(409, f"已经有叫「{payload.name}」的 MCP 服务了,换个名字")
|
||||
row = McpServer(**payload.model_dump())
|
||||
session.add(row)
|
||||
await session.commit()
|
||||
await session.refresh(row)
|
||||
return row
|
||||
return _mcp_out(row)
|
||||
|
||||
|
||||
@mcp_router.patch("/servers/{server_id}", response_model=McpOut)
|
||||
async def update_server(
|
||||
server_id: str, payload: McpIn, session: AsyncSession = Depends(get_session)
|
||||
) -> McpServer:
|
||||
) -> McpOut:
|
||||
row = await session.get(McpServer, server_id)
|
||||
if not row:
|
||||
raise HTTPException(404, "这个 MCP 服务不存在,可能已经被删了")
|
||||
before, old_name, was_enabled = _mcp_connection(row), row.name, row.enabled
|
||||
for key, value in payload.model_dump().items():
|
||||
setattr(row, key, value)
|
||||
if _mcp_connection(row) != before:
|
||||
# 启动命令、地址换了,上次的「连得上」说的是另一个进程
|
||||
row.status, row.last_error, row.last_check = "unknown", None, None
|
||||
await session.commit()
|
||||
await session.refresh(row)
|
||||
return row
|
||||
if (_mcp_connection(row), row.name, row.enabled) != (before, old_name, was_enabled):
|
||||
# 按旧配置、旧名字建的工具作废,下次用到时按新的连
|
||||
mcp_manager.invalidate(old_name, row.name)
|
||||
return _mcp_out(row)
|
||||
|
||||
|
||||
@mcp_router.delete("/servers/{server_id}", status_code=204)
|
||||
@@ -316,6 +439,7 @@ async def delete_server(server_id: str, session: AsyncSession = Depends(get_sess
|
||||
raise HTTPException(404, "这个 MCP 服务不存在,可能已经被删了")
|
||||
await session.delete(row)
|
||||
await session.commit()
|
||||
mcp_manager.invalidate(row.name)
|
||||
|
||||
|
||||
@mcp_router.post("/servers/{server_id}/probe")
|
||||
@@ -325,10 +449,15 @@ async def probe_server(
|
||||
row = await session.get(McpServer, server_id)
|
||||
if not row:
|
||||
raise HTTPException(404, "这个 MCP 服务不存在,可能已经被删了")
|
||||
import time
|
||||
|
||||
started = time.perf_counter()
|
||||
result = await mcp_manager.probe(row)
|
||||
result["elapsed_ms"] = int((time.perf_counter() - started) * 1000)
|
||||
row.status = "ok" if result.get("ok") else "error"
|
||||
row.last_error = result.get("error")
|
||||
row.tools_cache = [t["name"] for t in result.get("tools", [])]
|
||||
row.last_check = health.record(result.get("ok"), result["elapsed_ms"], result.get("error"))
|
||||
await session.commit()
|
||||
return result
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ from pydantic import BaseModel, Field
|
||||
from sqlalchemy import func, select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.api.errors import graph_error
|
||||
from app.core.errors import graph_error
|
||||
from app.api.runs import actor_of
|
||||
from app.core.artifact_store import graph_hash
|
||||
from app.db.base import get_session
|
||||
|
||||
Reference in New Issue
Block a user