Files
AgentLab/frontend/src/api/blueprintCopilot.ts
T
ThearchyHelios 7739340607 普通用户建应用集成收尾:
- 问数据和个人应用的图放行绑定只读查询等合规工具的调用工具节点,只读和遮罩在这条路径上生效;被治理成写入的工具在只读运行里直接拒;知识库范围在工具层固定(图级默认值、参数名纠错、Agent 都改不了)
- 个人应用的刷新和个人蓝图的试运行只有拥有者能读:运行、事件、证据、工件、文件、运行与审批列表、对比、可验证报告、质询、假设分析、提取工作流一并收紧;个人蓝图的试运行不论谁发起都按个人应用约束
- 共享蓝图只给拥有者、对应应用的管理者、管理员,看不到的一律 404;审核副本归审核人;管理员看别人的个人应用只有元数据、可以归档
- 记录表跨版本汇总(只读挂载旧版本、按当前列 UNION ALL、各版本各自固定高水位,缺的版本照实写进印章);停用前发起的运行可以继续写旧版本;更正沿用原提交人、更正人记审计
- 约定收口:待办种类 review、事件 file.extracted、表单错误 file、模板定义带示例与预置;前端类型一次补齐、去掉临时的局部类型;审核待办卡不再给打不开的链接;读附件事件有中文说法;检查脚本补断言
2026-10-15 13:55:19 +08:00

103 lines
4.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 蓝图助手和填关节(docs/design/normal-user-apps.md §2.2、§2.3、§2.11):两条 SSE,帧同 streamCopilot(一帧一行
* `data: <JSON>`,空行分隔)。状态在 store/blueprint.ts,翻译在 run/decode.ts,这里只管收发。
*
* - POST /api/copilot/blueprint-stream {blueprint_id, instruction, model?}:蓝图对话(服务端 api/copilot_blueprint.py)。
* 操作有 model、thinking、heartbeat、look、plan、bp_set / bp_add / bp_remove / bp_template(模型提的,服务端落地)、
* bp_ask(问回来一句,带候选,这一轮到此结束)、check(自查)、reply(只回答、不改卡片)、done、
* final {blueprint, explanation, issues, asked?}(存好的整份蓝图以它为准;issues 是没采纳的操作)、error。
* 这一轮落进蓝图对话、回答上一轮的反问都由服务端做。
* - POST /api/copilot/fill-joints-stream {blueprint_id, groups?}:按模板填生成关节,不同组并行,每个操作带 group:
* group {group, status: started|retrying|filled|failed, joints?, issues?, message?}、各组自己的 look、plan、fill_joint、check;
* 最后 final {blueprint, groups, problems}。
*
* 开流之前就被拒(4xx/5xx)时 onEnd 带服务端那句话、状态码和机读码;流到一半断了和一开始就连不上一样,说无法连接服务。
* 请求函数从 ./http 导入,不要从 ./client 导入(循环导入,见 http.ts 开头)。
*/
import type { BlueprintStreamEvent, FillJointsEvent } from '../typesBlueprint'
import { ApiError, BASE, NETWORK_MESSAGE, actorHeader, codeOf, describeDetail, isGatewayFailure, parseBody, report } from './http'
export interface BlueprintStreamRequest {
blueprint_id: string
/** 想要什么、要怎么改,或者回答助手的反问 */
instruction: string
model?: string | null
}
export interface FillJointsRequest {
blueprint_id: string
/** 只填这几组;不给 = 还有没填上的关节的那几组 */
groups?: string[]
}
export type StreamEnd = (error?: string, info?: { status?: number; code?: string; aborted?: boolean }) => void
/** 一条 SSE:返回取消函数。帧的形状以服务端为准(typesBlueprint 的 BlueprintStreamEvent、FillJointsEvent),这里不逐帧核对 */
function sse<E>(path: string, body: unknown, onOp: (op: E) => void, onEnd: StreamEnd): () => void {
const controller = new AbortController()
void (async () => {
try {
const res = await fetch(`${BASE}${path}`, {
method: 'POST',
headers: { 'Content-Type': 'application/json', Accept: 'text/event-stream', ...actorHeader() },
body: JSON.stringify(body),
signal: controller.signal,
})
if (!res.ok || !res.body) {
const text = await res.text().catch(() => '')
let parsed: any
let isJson = false
try { parsed = JSON.parse(text); isJson = true } catch { /* 响应体不是 JSON */ }
if (isGatewayFailure(res.status, text, isJson)) {
report(false, new ApiError(res.status, NETWORK_MESSAGE, { kind: 'network' }))
onEnd(NETWORK_MESSAGE)
return
}
report(true)
onEnd(isJson ? describeDetail(parsed?.detail ?? parsed, path)
: res.status >= 500 ? `服务端错误(${res.status}),详情请查看服务日志`
: `请求失败(HTTP ${res.status})`, { status: res.status, code: isJson ? codeOf(parsed) : undefined })
return
}
report(true)
const reader = res.body.getReader()
const decoder = new TextDecoder()
let buffer = ''
const flush = (frame: string) => {
if (!frame.startsWith('data: ')) return
try {
onOp(parseBody(frame.slice(6)))
} catch { /* 半截帧,忽略 */ }
}
for (;;) {
const { done, value } = await reader.read()
if (done) break
buffer += decoder.decode(value, { stream: true })
let idx: number
while ((idx = buffer.indexOf('\n\n')) >= 0) {
flush(buffer.slice(0, idx))
buffer = buffer.slice(idx + 2)
}
}
buffer += decoder.decode()
if (buffer.trim()) flush(buffer.trim())
onEnd()
} catch (e: any) {
if (e?.name === 'AbortError') { onEnd(undefined, { aborted: true }); return }
if (e instanceof TypeError) {
report(false, new ApiError(0, NETWORK_MESSAGE, { kind: 'network', raw: String(e) }))
onEnd(NETWORK_MESSAGE)
return
}
onEnd(e?.message ?? NETWORK_MESSAGE)
}
})()
return () => controller.abort()
}
export const streamBlueprint = (body: BlueprintStreamRequest, onOp: (op: BlueprintStreamEvent) => void, onEnd: StreamEnd) =>
sse('/copilot/blueprint-stream', body, onOp, onEnd)
export const streamFillJoints = (body: FillJointsRequest, onOp: (op: FillJointsEvent) => void, onEnd: StreamEnd) =>
sse('/copilot/fill-joints-stream', body, onOp, onEnd)