mirror of
https://github.com/ThearchyHelios/AgentLab.git
synced 2026-11-15 06:11:58 +00:00
e4f2693918
「引用即渲染」:写报告的模型只写引用标记([[m:指标id]]、[[i:输入字段]]、[[see:…]]),数值由系统按口径卡
填进去,每个数字都挂着一条能复算的证据链。本期是数字层闭环;实体、结论句的概率性证据放在后续几期。
内核(engine/evidence.py,口径卡、出具契约、表达式等)
- 口径卡记录每个指标的来源:原式、代入式(每个输入写成「值 ← 路径」)、复算结果、口径与版本
- 出具契约的数值匹配改为按位置标注;同一个值有几个出处时记为「出处不唯一」并列出候选,不再随便挑一个
- 证据 id 由内容派生,事后对不上就是被改过;零值、结构性数字(列表序号、代码块标签)单独处理
- 口径卡新增 on_missing:缺输入时默认仍是整个节点失败,可以改成记为空值、交给契约判档
报告撰写节点(report)与引用模式的出具契约
- 流式生成时 token 里就是真实数字,不会漏出原始标记;裸数字、不存在的引用当场判违规,交回去重写
(max_repairs,默认 1 次);仍不过时按 on_violation 失败(正式运行默认)或把违规标在报告里(探索运行默认)
- report.checked 事件记下核对结果,落在封存范围内;fail 模式下仍不过时带 failed,运行记录里画成失败
- 出具契约写了 report_from 就按引用核对:取回文档时复验哈希,独立重算一遍,全部带引用才判完整出具
- 成果字段逐字等于报告正文时标注 _evidence;复核只能加说明,不能改写带证据的答案(前后端各拦一道)
证据接口
- GET /api/runs/{id}/evidence 返回证据图;GET …/evidence/segments/{id} 返回一个片段的证据链
- 只认封存范围内的事件能追到的工件,不读 artifacts 表;封存被改过时不把任何一项标成已封存
- 没有报告的旧运行降级为 none 或 legacy_contract
界面
- EvidenceDoc 按块、句、片段渲染报告,片段是和正文一样的按钮;整份报告只占一个 Tab 位,
←/→ 在片段间移动、↑/↓ 按句、n/N 跳到下一处「无证据」、Enter 打开面板、Esc 关闭并把焦点还回去
- EvidencePanel:宽屏侧边、窄屏底部抽屉、360px 画布右栏里栏内展开;显示指标步骤、代入式、复算结果、
封存状态。正文不是封存的那份报告时明确说出来,不拿别的报告的封存画绿
- 出具横幅多一行「N/N 数字有出处 · 无证据 M」和「定位下一处」;没有 _evidence 的旧回答渲染一字不差
- 画布认得报告撰写节点;Copilot 手册补上口径卡、报告节点、出具契约的写法;新增模板「⑨ 可追溯周报」
验证(沙箱):tsc 干净,生产构建通过,pytest 1601 passed,check-all 16 项检查全绿(新增 check-evidence)。
123 lines
3.5 KiB
Python
123 lines
3.5 KiB
Python
from __future__ import annotations
|
|
|
|
import logging
|
|
from contextlib import asynccontextmanager
|
|
from typing import AsyncIterator
|
|
|
|
from fastapi import FastAPI, Request
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from fastapi.responses import JSONResponse
|
|
|
|
from app.api import (
|
|
artifacts, conversations, copilot, datasources, evidence, governance, knowledge, runs,
|
|
sandbox, settings as settings_api, tools, workflows,
|
|
)
|
|
from app.api.coded import CodedHTTPException, coded_handler
|
|
from app.core.config import settings
|
|
from app.db.base import init_db
|
|
from app.engine.runner import run_manager
|
|
from app.seed import seed_defaults
|
|
|
|
logging.basicConfig(
|
|
level=logging.INFO,
|
|
format="%(asctime)s %(levelname)-7s %(name)s | %(message)s",
|
|
datefmt="%H:%M:%S",
|
|
)
|
|
logger = logging.getLogger("agentlab")
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(_: FastAPI) -> AsyncIterator[None]:
|
|
settings.ensure_dirs()
|
|
await init_db()
|
|
await seed_defaults()
|
|
|
|
# 把存的 embedding 配置读进来。不读的话重启后会悄悄退回本地哈希向量,
|
|
# 而库里的向量是上次那个模型建的——检索会整体退回关键词,
|
|
# 用户只看到"重启之后搜得不准了"
|
|
from app.db.base import SessionLocal
|
|
from app.memory.embeddings import load_setting
|
|
|
|
async with SessionLocal() as _session:
|
|
try:
|
|
await load_setting(_session)
|
|
except Exception as e: # noqa: BLE001 - key 没了不该拦住整个服务起不来
|
|
logger.warning("embedding 配置没能生效,暂用本地向量:%s", e)
|
|
|
|
# 上次被强杀时留下的"处理中"文档。后台任务不会自己回来,留着它只会在
|
|
# 界面上永远转圈——和 runner 对 running 的 run 是同一个道理
|
|
from sqlalchemy import update as _update
|
|
|
|
from app.db.models import Document as _Doc
|
|
|
|
async with SessionLocal() as _s:
|
|
await _s.execute(
|
|
_update(_Doc).where(_Doc.status == "processing")
|
|
.values(status="failed", error="服务重启,这份文档没有处理完,请重新上传")
|
|
)
|
|
await _s.commit()
|
|
|
|
await run_manager.setup()
|
|
from app.sandbox.manager import sandbox_manager as _sbm
|
|
|
|
await _sbm.start_reaper()
|
|
logger.info("AgentLab 就绪 · 数据目录 %s", settings.data_dir)
|
|
try:
|
|
yield
|
|
finally:
|
|
await run_manager.shutdown()
|
|
from app.sandbox.manager import sandbox_manager
|
|
|
|
await sandbox_manager.close()
|
|
logger.info("已停止")
|
|
|
|
|
|
app = FastAPI(
|
|
title="AgentLab",
|
|
description="可视化 Agent 编排实验台",
|
|
version="0.1.0",
|
|
lifespan=lifespan,
|
|
)
|
|
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=settings.cors_origins,
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
for router in (
|
|
workflows.router,
|
|
runs.router,
|
|
runs.approvals_router,
|
|
evidence.router,
|
|
settings_api.router,
|
|
tools.router,
|
|
tools.custom_router,
|
|
tools.mcp_router,
|
|
knowledge.memory_router,
|
|
knowledge.kb_router,
|
|
knowledge.skills_router,
|
|
sandbox.router,
|
|
copilot.router,
|
|
conversations.router,
|
|
artifacts.router,
|
|
governance.router,
|
|
datasources.router,
|
|
):
|
|
app.include_router(router)
|
|
|
|
|
|
app.add_exception_handler(CodedHTTPException, coded_handler)
|
|
|
|
|
|
@app.exception_handler(ValueError)
|
|
async def value_error_handler(_: Request, exc: ValueError) -> JSONResponse:
|
|
return JSONResponse(status_code=400, content={"detail": str(exc)})
|
|
|
|
|
|
@app.get("/api/health")
|
|
async def health() -> dict[str, str]:
|
|
return {"status": "ok", "service": "agentlab"}
|