Compare commits
5
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ca39e2b351 | ||
|
|
faddf24d11 | ||
|
|
19c832b244 | ||
|
|
92634cbf73 | ||
|
|
78078822d7 |
@@ -0,0 +1 @@
|
||||
probe
|
||||
@@ -9,11 +9,39 @@
|
||||
web/chat/
|
||||
├── chat_api.py 对话后端 API(http.server):场景分发 + 统一 JSON
|
||||
├── chat_widget.html 前端对话组件(内联 HTML/CSS/JS,深色主题对齐驾驶舱)
|
||||
├── assistant.html 增强版对话页(#133:引用溯源/路由分级/DLP 拦截反馈)
|
||||
├── assistant.css 增强版样式
|
||||
├── assistant.js 增强版逻辑(直连推理服务 /v1/chat/completions)
|
||||
├── citations.json RAG 引用语料(scripts/build_citations.py 编译产物)
|
||||
├── scripts/
|
||||
│ └── build_citations.py 模板知识库文档 → 引用语料编译脚本
|
||||
├── tests/
|
||||
│ └── test_chat_api.py 场景分发 / 端点 / 错误处理测试
|
||||
└── README.md
|
||||
```
|
||||
|
||||
## 增强版对话页(issue #133 / PRD 5.4)
|
||||
|
||||
`assistant.html` 在 chat_widget 雏形上完善并直连已部署推理服务
|
||||
(`http://39.101.182.167:30800`,OpenAI 兼容):
|
||||
|
||||
- **消息流 + 元信息**:每条回答带路由分级 / 模型 / 耗时;
|
||||
- **RAG 引用溯源**(PRD 5.4 强制):`citations.json`(由
|
||||
`python web/chat/scripts/build_citations.py` 从 `templates/*/rag-kb/documents/`
|
||||
编译)关键词检索,回答下方列出「文档 + 章节 + 摘要」引用;未命中显式标注无引用;
|
||||
- **路由分级提示**:镜像 `core/llm-gateway/router.py`——敏感/核心 → 本地
|
||||
(数据不出厂),通用/脱敏 → 云端,DLP 命中 → 拦截(fail-closed);
|
||||
- **DLP 拦截反馈**:镜像 `dlp.py` 的 `DLP_DEFAULT_RULES`(身份证/手机号/邮箱/
|
||||
IPv4/访问密钥/密钥键值对/配方敏感词),命中即阻止外发并展示命中规则,
|
||||
可选择「转本地模型处理」;
|
||||
- **报警解释**(PRD 场景A):severity + 点位结构化输入 → 「原因 + 处置建议」,
|
||||
P0 告警附「需值班长确认」提示(PRD 高利害人工确认);
|
||||
- **交接班报告**(PRD 场景C):按 `handover_brief.ti.yaml` 章节模板
|
||||
(生产概况/异常事项/安全注意事项/能耗/待办)生成,耗时展示(目标 ≤ 2 分钟)。
|
||||
|
||||
运行:`cd web/chat && python -m http.server 8082`,打开
|
||||
`http://127.0.0.1:8082/assistant.html`(Chrome / Edge 最新两版)。
|
||||
|
||||
## 快速运行
|
||||
|
||||
```bash
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
/* iAOP 对话助手增强版样式(issue #133)。深色主题,与 chat_widget.html / cockpit 一致。 */
|
||||
:root {
|
||||
--bg: #0f172a; --surface: #111c33; --surface-2: #1e293b;
|
||||
--fg: #e2e8f0; --fg-muted: #94a3b8; --accent: #38bdf8; --warn: #f5a623;
|
||||
}
|
||||
* { box-sizing: border-box; }
|
||||
body { margin: 0; font-family: "Microsoft YaHei", "PingFang SC", sans-serif;
|
||||
background: var(--bg); color: var(--fg); }
|
||||
#app { max-width: 860px; margin: 0 auto; padding: 16px; }
|
||||
|
||||
#topbar { display: flex; align-items: center; justify-content: space-between; }
|
||||
h1 { font-size: 18px; color: var(--accent); margin: 0; }
|
||||
h1 .sub { font-size: 12px; color: var(--fg-muted); font-weight: 400; }
|
||||
.hint { font-size: 11px; color: #64748b; }
|
||||
.dot { font-size: 13px; }
|
||||
.dot.ok { color: #2ecc71; }
|
||||
.dot.down { color: #ff3b30; }
|
||||
|
||||
#messages { height: 480px; overflow-y: auto; border: 1px solid var(--surface-2);
|
||||
border-radius: 8px; padding: 12px; background: var(--surface); margin-top: 10px; }
|
||||
.msg { margin: 10px 0; }
|
||||
.msg .who { font-size: 12px; color: var(--fg-muted); }
|
||||
.msg .text { display: inline-block; max-width: 88%; padding: 8px 12px; border-radius: 8px;
|
||||
white-space: pre-wrap; font-size: 14px; line-height: 1.6; }
|
||||
.user .text { background: #1d4ed8; }
|
||||
.bot .text { background: var(--surface-2); }
|
||||
.meta { font-size: 11px; color: #64748b; margin-top: 3px; display: flex; gap: 8px;
|
||||
flex-wrap: wrap; align-items: center; }
|
||||
|
||||
/* 路由分级标签(本地/云端/拦截) */
|
||||
.route-tag { padding: 1px 8px; border-radius: 10px; font-size: 11px; }
|
||||
.route-local { background: rgba(46,204,113,.15); color: #2ecc71; }
|
||||
.route-cloud { background: rgba(56,189,248,.15); color: var(--accent); }
|
||||
.route-block { background: rgba(255,59,48,.15); color: #ff3b30; }
|
||||
.human-tag { background: rgba(245,166,35,.15); color: var(--warn);
|
||||
padding: 1px 8px; border-radius: 10px; font-size: 11px; }
|
||||
|
||||
/* RAG 引用溯源(PRD 5.4 强制) */
|
||||
.citations { margin-top: 6px; font-size: 12px; border-left: 3px solid var(--accent);
|
||||
padding: 4px 10px; background: rgba(56,189,248,.06); border-radius: 0 6px 6px 0; }
|
||||
.citations .cite-title { color: var(--accent); font-size: 11px; margin-bottom: 2px; }
|
||||
.citations .cite-item { color: var(--fg-muted); margin: 2px 0; }
|
||||
.no-citation { margin-top: 6px; font-size: 11px; color: #64748b; }
|
||||
|
||||
/* 场景输入区 */
|
||||
.scenario-inputs { display: flex; gap: 8px; margin-top: 10px; align-items: center; }
|
||||
.scenario-inputs input { flex: 1; }
|
||||
|
||||
/* DLP 拦截反馈条 */
|
||||
#dlp-banner { margin-top: 10px; padding: 10px 12px; border-radius: 8px; font-size: 13px;
|
||||
background: rgba(255,59,48,.10); border: 1px solid #ff3b30; color: #ffb4ad; }
|
||||
#dlp-banner button { margin-left: 8px; padding: 4px 10px; font-size: 12px; border: none;
|
||||
border-radius: 5px; cursor: pointer; background: var(--surface-2);
|
||||
color: var(--fg); }
|
||||
|
||||
#panel { margin-top: 10px; display: flex; gap: 8px; }
|
||||
select, input { padding: 8px; border-radius: 6px; border: 1px solid #334155;
|
||||
background: #0b1526; color: var(--fg); }
|
||||
input { flex: 1; }
|
||||
button#send { padding: 8px 16px; border: none; border-radius: 6px; background: #0ea5e9;
|
||||
color: #fff; cursor: pointer; }
|
||||
button#send:disabled { opacity: .5; }
|
||||
#footer-hint { margin-top: 8px; }
|
||||
@@ -0,0 +1,63 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>iAOP 对话助手(增强版)</title>
|
||||
<link rel="stylesheet" href="assistant.css">
|
||||
</head>
|
||||
<body>
|
||||
<!-- iAOP 对话助手增强版(issue #133 / PRD 5.4「④ LLM 网关 + RAG」)
|
||||
在 chat_widget.html(#77 雏形)之上完善:消息流 + RAG 引用溯源 +
|
||||
路由分级提示(本地/云端)+ DLP 拦截反馈,直连已部署推理服务。
|
||||
既有 chat_widget.html 保留不动(仍由 chat_api.py 托管联调)。 -->
|
||||
<div id="app">
|
||||
<header id="topbar">
|
||||
<h1>iAOP 对话助手 <span class="sub">NL 查询 / 报警解释 / 交接班报告 · PRD 5.4</span></h1>
|
||||
<div id="service-status">
|
||||
<span id="infer-dot" class="dot">●</span>
|
||||
<span id="infer-text" class="hint">推理服务检测中…</span>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<div id="messages"></div>
|
||||
|
||||
<!-- 报警解释场景的结构化输入(PRD 场景A:告警 → 原因+处置建议) -->
|
||||
<div id="alarm-inputs" class="scenario-inputs" hidden>
|
||||
<select id="alarm-severity">
|
||||
<option value="P0">P0(红色告警)</option>
|
||||
<option value="P1">P1(加强监控)</option>
|
||||
<option value="P2">P2(提示)</option>
|
||||
</select>
|
||||
<input id="alarm-point" placeholder="告警点位,如 CLF-01.TEMP">
|
||||
</div>
|
||||
<!-- 交接班报告场景输入(PRD 场景C:按模板章节生成,≤ 2 分钟) -->
|
||||
<div id="handover-inputs" class="scenario-inputs" hidden>
|
||||
<input id="handover-shift" placeholder="班次,如 早班 08:00-16:00">
|
||||
<span class="hint">章节模板:生产概况/异常事项/安全注意事项/能耗/待办(handover_brief.ti.yaml)</span>
|
||||
</div>
|
||||
|
||||
<!-- DLP 拦截反馈条(PRD 5.4:拦截可感知) -->
|
||||
<div id="dlp-banner" hidden>
|
||||
<strong>DLP 拦截:</strong><span id="dlp-detail"></span>
|
||||
<button id="btn-local-fallback">转本地模型处理</button>
|
||||
<button id="btn-dlp-dismiss">取消发送</button>
|
||||
</div>
|
||||
|
||||
<div id="panel">
|
||||
<select id="scenario">
|
||||
<option value="nl_query">自然语言查询</option>
|
||||
<option value="alarm_explain">报警解释</option>
|
||||
<option value="shift_handover">交接班报告</option>
|
||||
</select>
|
||||
<input id="question" placeholder="请输入问题,如:氯气流量最近1小时趋势">
|
||||
<button id="send">发送</button>
|
||||
</div>
|
||||
<div class="hint" id="footer-hint">
|
||||
路由分级:敏感/核心 → 本地(数据不出厂);脱敏/通用 → 云端;DLP 命中 → 拦截。
|
||||
回答附 RAG 引用溯源(知识库命中时)。
|
||||
</div>
|
||||
</div>
|
||||
<script src="assistant.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,283 @@
|
||||
/* iAOP 对话助手增强版(issue #133 / PRD 5.4「④ LLM 网关 + RAG」)。
|
||||
*
|
||||
* 在 chat_widget.html(#77 雏形)之上完善:
|
||||
* - 消息流 + 每消息元信息(路由分级 / 模型 / 耗时 / 高利害确认提示)
|
||||
* - RAG 引用溯源:citations.json(模板知识库语料)关键词检索,回答附引用列表
|
||||
* - 路由分级提示:镜像 core/llm-gateway/router.py 语义(local/cloud/block)
|
||||
* - DLP 拦截反馈:镜像 core/llm-gateway/dlp.py 的 DLP_DEFAULT_RULES 内置保底规则,
|
||||
* 命中即阻止外发并给出可感知的拦截条(可转本地处理)
|
||||
* - 三场景:NL 查询 / 报警解释(severity+点位 → 原因+处置建议,P0 提示人工确认)/
|
||||
* 交接班报告(按 handover_brief.ti.yaml 章节模板生成,计时展示 ≤2min 目标)
|
||||
*
|
||||
* 直连已部署推理服务 http://39.101.182.167:30800(OpenAI 兼容 /v1/chat/completions)。
|
||||
*/
|
||||
"use strict";
|
||||
|
||||
var INFER_BASE = "http://39.101.182.167:30800";
|
||||
var state = {
|
||||
model: null,
|
||||
citations: null, // citations.json 语料
|
||||
pending: null // 被 DLP 拦截待处置的消息
|
||||
};
|
||||
|
||||
/* ---------------- DLP 内置保底规则(镜像 dlp.py DLP_DEFAULT_RULES) ---------------- */
|
||||
var DLP_RULES = [
|
||||
{ name: "pii_id_card", category: "pii", kind: "regex",
|
||||
pattern: /\b\d{17}[\dXx]\b/, description: "身份证号(18 位)" },
|
||||
{ name: "pii_mobile", category: "pii", kind: "regex",
|
||||
pattern: /\b1[3-9]\d{9}\b/, description: "中国大陆手机号" },
|
||||
{ name: "pii_email", category: "pii", kind: "regex",
|
||||
pattern: /[\w.+-]+@[\w-]+\.[\w.-]+/, description: "邮箱地址" },
|
||||
{ name: "pii_ipv4", category: "pii", kind: "regex",
|
||||
pattern: /\b(?:\d{1,3}\.){3}\d{1,3}\b/, description: "IPv4 地址" },
|
||||
{ name: "credential_ak", category: "credential", kind: "regex",
|
||||
pattern: /\b(?:AKIA|LTAI)[0-9A-Z]{16,}\b/, description: "云访问密钥 ID" },
|
||||
{ name: "credential_kv", category: "credential", kind: "regex",
|
||||
pattern: /(?:access[_-]?key|secret|password|token)\s*[:=]\s*\S+/i,
|
||||
description: "密钥/口令键值对" },
|
||||
{ name: "process_formula", category: "process-keyword", kind: "keyword",
|
||||
pattern: "配方", description: "配方类敏感词" }
|
||||
];
|
||||
|
||||
function dlpCheck(text) {
|
||||
var hits = [];
|
||||
DLP_RULES.forEach(function (r) {
|
||||
var m = text.match(r.pattern);
|
||||
if (m) hits.push({ rule: r, matched: m[0] });
|
||||
});
|
||||
return hits;
|
||||
}
|
||||
|
||||
/* ---------------- 路由分级(镜像 router.py:block > local > cloud > 默认 local) ---------------- */
|
||||
// 通用/脱敏问法(可云端):不含点位/工艺敏感词的常识类问题
|
||||
var GENERIC_RE = /^(什么是|请解释|介绍一下|怎么理解|如何理解)/;
|
||||
// 本地(敏感/核心)线索:点位号、生产/工艺词
|
||||
var LOCAL_HINT_RE = /([A-Z]{2,}-\d+\.|[点位炉班批纯杂温度压力流量生产工艺工况])/;
|
||||
|
||||
function routeDecision(text, dlpHits) {
|
||||
if (dlpHits.length) {
|
||||
return { target: "block", reason: "DLP 出站检查命中,拦截外发(fail-closed)" };
|
||||
}
|
||||
if (GENERIC_RE.test(text) && !LOCAL_HINT_RE.test(text)) {
|
||||
return { target: "cloud", reason: "通用/脱敏问题,DLP 放行,可路由云端" };
|
||||
}
|
||||
return { target: "local", reason: "敏感/核心或未知(默认保守),本地处理(数据不出厂)" };
|
||||
}
|
||||
|
||||
/* ---------------- RAG 引用检索(citations.json 关键词重叠,Demo 级) ---------------- */
|
||||
function retrieveCitations(query, topK) {
|
||||
if (!state.citations) return [];
|
||||
var terms = query.replace(/[^\w一-鿿]+/g, " ").split(" ")
|
||||
.filter(function (t) { return t.length >= 2; });
|
||||
var scored = [];
|
||||
state.citations.documents.forEach(function (doc) {
|
||||
doc.sections.forEach(function (sec) {
|
||||
var hay = doc.title + " " + sec.heading + " " + sec.snippet;
|
||||
var score = 0;
|
||||
terms.forEach(function (t) { if (hay.indexOf(t) >= 0) score += t.length; });
|
||||
if (score > 0) {
|
||||
scored.push({ title: doc.title, source: doc.source,
|
||||
heading: sec.heading, snippet: sec.snippet, score: score });
|
||||
}
|
||||
});
|
||||
});
|
||||
scored.sort(function (a, b) { return b.score - a.score; });
|
||||
return scored.slice(0, topK || 3);
|
||||
}
|
||||
|
||||
/* ---------------- 推理服务 ---------------- */
|
||||
function checkHealth() {
|
||||
fetch(INFER_BASE + "/health").then(function (r) {
|
||||
if (!r.ok) throw new Error(r.status);
|
||||
document.getElementById("infer-dot").className = "dot ok";
|
||||
document.getElementById("infer-text").textContent = "推理服务在线";
|
||||
}).catch(function () {
|
||||
document.getElementById("infer-dot").className = "dot down";
|
||||
document.getElementById("infer-text").textContent = "推理服务不可达";
|
||||
});
|
||||
}
|
||||
function loadModel() {
|
||||
return fetch(INFER_BASE + "/v1/models").then(function (r) { return r.json(); })
|
||||
.then(function (d) {
|
||||
// 兼容 {"models":[...]} 与 OpenAI {"data":[{"id":...}]} 两种形态
|
||||
if (d && d.models && d.models.length) state.model = d.models[0];
|
||||
else if (d && d.data && d.data.length) state.model = d.data[0].id;
|
||||
}).catch(function () {});
|
||||
}
|
||||
function chatCompletions(messages) {
|
||||
return fetch(INFER_BASE + "/v1/chat/completions", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ model: state.model || "iaop-ti-cl4-v1",
|
||||
messages: messages, max_tokens: 1024 })
|
||||
}).then(function (r) {
|
||||
if (!r.ok) throw new Error("HTTP " + r.status);
|
||||
return r.json();
|
||||
}).then(function (d) {
|
||||
var c = d && d.choices && d.choices[0];
|
||||
return (c && c.message && c.message.content) || "(空响应)";
|
||||
});
|
||||
}
|
||||
|
||||
/* ---------------- 消息渲染 ---------------- */
|
||||
function el(tag, cls, text) {
|
||||
var n = document.createElement(tag);
|
||||
if (cls) n.className = cls;
|
||||
if (text !== undefined) n.textContent = text;
|
||||
return n;
|
||||
}
|
||||
function addMsg(who, text, meta) {
|
||||
var box = document.getElementById("messages");
|
||||
var div = el("div", "msg " + who);
|
||||
div.appendChild(el("div", "who", who === "user" ? "我" : "iAOP"));
|
||||
var t = el("div", "text");
|
||||
t.textContent = text;
|
||||
div.appendChild(t);
|
||||
if (meta) {
|
||||
var m = el("div", "meta");
|
||||
meta.forEach(function (node) { m.appendChild(node); });
|
||||
div.appendChild(m);
|
||||
}
|
||||
box.appendChild(div);
|
||||
box.scrollTop = box.scrollHeight;
|
||||
return div;
|
||||
}
|
||||
function routeTag(route) {
|
||||
var map = { local: ["route-local", "路由:本地(数据不出厂)"],
|
||||
cloud: ["route-cloud", "路由:云端(脱敏放行)"],
|
||||
block: ["route-block", "路由:拦截"] };
|
||||
var v = map[route] || map.local;
|
||||
return el("span", "route-tag " + v[0], v[1]);
|
||||
}
|
||||
function appendCitations(msgDiv, citations) {
|
||||
if (!citations.length) {
|
||||
msgDiv.appendChild(el("div", "no-citation", "未命中知识库,以上为模型直接回答(无引用)"));
|
||||
return;
|
||||
}
|
||||
var box = el("div", "citations");
|
||||
box.appendChild(el("div", "cite-title", "引用溯源(RAG,共 " + citations.length + " 条)"));
|
||||
citations.forEach(function (c, i) {
|
||||
box.appendChild(el("div", "cite-item",
|
||||
"[" + (i + 1) + "] 《" + c.title + "》 " + c.heading + " — " + c.snippet));
|
||||
});
|
||||
msgDiv.appendChild(box);
|
||||
}
|
||||
|
||||
/* ---------------- 场景 Prompt 构建 ---------------- */
|
||||
function buildMessages(scenario, question, citations) {
|
||||
if (scenario === "alarm_explain") {
|
||||
var sev = document.getElementById("alarm-severity").value;
|
||||
var point = document.getElementById("alarm-point").value.trim() || "(未填点位)";
|
||||
return [
|
||||
{ role: "system", content: "你是 iAOP 工业报警解释助手。针对告警给出结构化回答:"
|
||||
+ "【可能原因】分条列出;【处置建议】分条列出可执行步骤;【是否需要人工确认】。"
|
||||
+ "用简体中文,简洁专业。" },
|
||||
{ role: "user", content: "告警级别 " + sev + ",点位 " + point +
|
||||
",告警描述:" + question }
|
||||
];
|
||||
}
|
||||
if (scenario === "shift_handover") {
|
||||
var shift = document.getElementById("handover-shift").value.trim() || "本班";
|
||||
return [
|
||||
{ role: "system", content: "你是 iAOP 交接班报告生成助手。按以下章节模板生成摘要"
|
||||
+ "(对齐 handover_brief.ti.yaml):一、生产概况;二、异常事项;三、安全注意事项;"
|
||||
+ "四、能耗;五、待办(交下一班)。每章节 1-3 条,简体中文。" },
|
||||
{ role: "user", content: "请生成 " + shift + " 的交接班报告。本班关键信息:" + question }
|
||||
];
|
||||
}
|
||||
// nl_query:带 RAG 上下文
|
||||
var ctx = citations.map(function (c, i) {
|
||||
return "[" + (i + 1) + "] 《" + c.title + "》" + c.heading + ":" + c.snippet;
|
||||
}).join("\n");
|
||||
return [
|
||||
{ role: "system", content: "你是 iAOP 工业驾驶舱 NL 查询助手,回答工艺/质量/能耗问题。"
|
||||
+ (ctx ? "可参考以下知识库片段(回答中可用 [编号] 标注引用):\n" + ctx
|
||||
: "知识库未命中,直接回答即可。")
|
||||
+ "用简体中文,简洁准确。" },
|
||||
{ role: "user", content: question }
|
||||
];
|
||||
}
|
||||
|
||||
/* ---------------- 发送主流程 ---------------- */
|
||||
function doSend(question, forceLocal) {
|
||||
var scenario = document.getElementById("scenario").value;
|
||||
var citations = scenario === "nl_query" ? retrieveCitations(question, 3) : [];
|
||||
var route = forceLocal
|
||||
? { target: "local", reason: "DLP 拦截后用户确认转本地处理" }
|
||||
: routeDecision(question + " " + (citations.map(function (c) {
|
||||
return c.snippet; }).join(" ")), dlpCheck(question));
|
||||
|
||||
addMsg("user", question);
|
||||
document.getElementById("question").value = "";
|
||||
var sendBtn = document.getElementById("send");
|
||||
sendBtn.disabled = true;
|
||||
var started = Date.now();
|
||||
|
||||
chatCompletions(buildMessages(scenario, question, citations))
|
||||
.then(function (answer) {
|
||||
var cost = ((Date.now() - started) / 1000).toFixed(1);
|
||||
var meta = [routeTag(route.target),
|
||||
el("span", null, "模型: " + (state.model || "default")),
|
||||
el("span", null, "耗时: " + cost + "s" +
|
||||
(scenario === "shift_handover" ? "(目标 ≤ 2min)" : ""))];
|
||||
// 高利害人工确认(PRD line 333):P0 报警解释提示值班长确认
|
||||
if (scenario === "alarm_explain" &&
|
||||
document.getElementById("alarm-severity").value === "P0") {
|
||||
meta.push(el("span", "human-tag", "⚠ P0 高利害:需值班长确认后执行"));
|
||||
}
|
||||
var div = addMsg("bot", answer, meta);
|
||||
if (scenario === "nl_query") appendCitations(div, citations);
|
||||
})
|
||||
.catch(function (e) {
|
||||
addMsg("bot", "请求失败:" + e.message + "(请确认推理服务可达)");
|
||||
})
|
||||
.finally(function () { sendBtn.disabled = false; });
|
||||
}
|
||||
|
||||
function trySend() {
|
||||
var question = document.getElementById("question").value.trim();
|
||||
if (!question) return;
|
||||
var hits = dlpCheck(question);
|
||||
if (hits.length) {
|
||||
// DLP 拦截反馈(PRD 5.4:拦截可感知)——不发送,展示命中详情
|
||||
var banner = document.getElementById("dlp-banner");
|
||||
document.getElementById("dlp-detail").textContent =
|
||||
"命中规则 " + hits.map(function (h) {
|
||||
return h.rule.name + "(" + h.rule.description + ")";
|
||||
}).join("、") + ",已阻止外发云端(fail-closed)";
|
||||
banner.hidden = false;
|
||||
state.pending = question;
|
||||
return;
|
||||
}
|
||||
doSend(question, false);
|
||||
}
|
||||
|
||||
/* ---------------- 启动 ---------------- */
|
||||
(function init() {
|
||||
document.getElementById("send").onclick = trySend;
|
||||
document.getElementById("question").addEventListener("keydown", function (e) {
|
||||
if (e.key === "Enter") trySend();
|
||||
});
|
||||
document.getElementById("scenario").onchange = function (e) {
|
||||
var v = e.target.value;
|
||||
document.getElementById("alarm-inputs").hidden = v !== "alarm_explain";
|
||||
document.getElementById("handover-inputs").hidden = v !== "shift_handover";
|
||||
document.getElementById("question").placeholder = {
|
||||
nl_query: "请输入问题,如:氯气流量最近1小时趋势",
|
||||
alarm_explain: "描述告警现象,如:炉温骤升至 920℃ 且持续 5 分钟",
|
||||
shift_handover: "输入本班关键事件/数据,如:氯化炉运行平稳,P1 告警 2 起已确认"
|
||||
}[v];
|
||||
};
|
||||
document.getElementById("btn-local-fallback").onclick = function () {
|
||||
document.getElementById("dlp-banner").hidden = true;
|
||||
if (state.pending) { doSend(state.pending, true); state.pending = null; }
|
||||
};
|
||||
document.getElementById("btn-dlp-dismiss").onclick = function () {
|
||||
document.getElementById("dlp-banner").hidden = true;
|
||||
state.pending = null;
|
||||
};
|
||||
fetch("citations.json").then(function (r) { return r.ok ? r.json() : null; })
|
||||
.then(function (d) { state.citations = d; }).catch(function () {});
|
||||
checkHealth();
|
||||
loadModel();
|
||||
})();
|
||||
@@ -0,0 +1,85 @@
|
||||
{
|
||||
"schema": "iAOP-chat-citations-v1",
|
||||
"documents": [
|
||||
{
|
||||
"title": "GB/T 5475 离子交换树脂取样方法",
|
||||
"source": "templates/resin/rag-kb/documents/GB-T-5475-离子交换树脂取样方法.md",
|
||||
"sections": [
|
||||
{
|
||||
"heading": "全文",
|
||||
"snippet": "# GB/T 5475 离子交换树脂取样方法 - 规定离子交换树脂产品取样方法:从批中按规定数量抽取样品。 - 取样要求代表性:不同包装单元均匀取样,混合后缩分。 - 样品量:每批取样量按标准规定,样品密封保存并标识批次。 - 取样记录:批次号、取样时间、取样人、样品编号(对齐 sample_id)。"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"title": "GB/T 8144 阳离子交换树脂交换容量测定方法",
|
||||
"source": "templates/resin/rag-kb/documents/GB-T-8144-阳离子交换树脂交换容量测定方法.md",
|
||||
"sections": [
|
||||
{
|
||||
"heading": "全文",
|
||||
"snippet": "# GB/T 8144 阳离子交换树脂交换容量测定方法 - 规定阳离子交换树脂交换容量(mmol/g)测定方法。 - 原理:树脂经酸/碱转型后,用标准溶液滴定测定交换容量。 - 结果用于质量分级:交换容量 ≥ 规定值(如 ≥ 4.2 mmol/g)为合格品。 - 测定结果按批次录入 LIMS(tag_id: LIMS."
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"title": "GB/T 8330 离子交换树脂湿真密度测定方法",
|
||||
"source": "templates/resin/rag-kb/documents/GB-T-8330-离子交换树脂湿真密度测定方法.md",
|
||||
"sections": [
|
||||
{
|
||||
"heading": "全文",
|
||||
"snippet": "# GB/T 8330 离子交换树脂湿真密度测定方法 - 规定离子交换树脂湿真密度测定方法(比重瓶法)。 - 湿真密度反映树脂交联度与孔结构,与交换容量、强度相关。 - 测定条件:恒温(25℃)、湿态样品、排除气泡后称量。 - 结果记录:按批次录入,作为工艺优化与质量追溯的辅助指标。"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"title": "交接班报告生成规范",
|
||||
"source": "templates/resin/rag-kb/documents/交接班报告生成规范.md",
|
||||
"sections": [
|
||||
{
|
||||
"heading": "全文",
|
||||
"snippet": "# 交接班报告生成规范 - 交接班报告须包含:生产概况、设备运行状态、异常与处置、安全注意事项、待办事项。 - 异常事项必须标注发生时间、处理人与处置结果。 - 待办事项需明确责任人与期望完成时间。 - 每班次结束前 30 分钟生成,当班班长审核后交下一班。"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"title": "交联度超标处置流程",
|
||||
"source": "templates/resin/rag-kb/documents/交联度超标处置流程.md",
|
||||
"sections": [
|
||||
{
|
||||
"heading": "全文",
|
||||
"snippet": "# 交联度超标处置流程 1. 化验确认交联度超标(相对配方设计值偏差 > 5%)后,立即隔离该批树脂。 2. 通知当班班长与质检科,暂停同一配方后续批次投料。 3. 原因排查:DVB 计量偏差、搅拌不均、温度曲线漂移。 4. 处置选项:回用(与低交联度批次调配)或降级(非标用途),由 QE 评审放行。 5. 处置结果录"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"title": "吸附树脂合成工艺规范",
|
||||
"source": "templates/resin/rag-kb/documents/吸附树脂合成工艺规范.md",
|
||||
"sections": [
|
||||
{
|
||||
"heading": "全文",
|
||||
"snippet": "# 吸附树脂合成工艺规范 - 吸附树脂系列:ASC / ASD / HPR / ACD,采用悬浮聚合法,间歇/配方驱动生产。 - 主要单体:苯乙烯(ST)+ 交联剂二乙烯苯(DVB),按配方比例投料。 - 关键工艺参数:聚合温度 70–90℃,引发剂滴加速率按配方设定,搅拌转速 100–300 rpm。 - 交联度(D"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"title": "树脂合成异常处置SOP",
|
||||
"source": "templates/resin/rag-kb/documents/树脂合成异常处置SOP.md",
|
||||
"sections": [
|
||||
{
|
||||
"heading": "全文",
|
||||
"snippet": "# 树脂合成异常处置SOP - SOP-R-001:釜温超上限(>95℃)→ 立即停引发剂滴加、加大冷却水,10 分钟未回落按紧急排料流程。 - SOP-R-002:釜压异常升高 → 停止加热、检查放空阀,联系仪表检修。 - SOP-R-003:搅拌故障 → 降速至停止,启用备用搅拌(如有),未恢复则终止本批。 - S"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"title": "离子交换树脂生产操作手册",
|
||||
"source": "templates/resin/rag-kb/documents/离子交换树脂生产操作手册.md",
|
||||
"sections": [
|
||||
{
|
||||
"heading": "全文",
|
||||
"snippet": "# 离子交换树脂生产操作手册 - 开机前确认:原料计量罐、反应釜密封、加热/冷却系统、尾气处理正常。 - 投料顺序:先加去离子水与分散剂,再按配方加入苯乙烯/二乙烯苯与引发剂。 - 反应期间每 30 分钟记录一次釜温、釜压、搅拌转速。 - 聚合完成判断:釜温回落且无明显放热,取样测交联度。 - 洗涤:去离子水洗至 pH"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""对话助手 RAG 引用语料编译脚本(issue #133 / PRD 5.4 引用溯源)。
|
||||
|
||||
扫描行业模板知识库文档(``templates/*/rag-kb/documents/*.md``),抽取
|
||||
「文档标题 + 章节 + 章节摘要」编译为 ``web/chat/citations.json``,
|
||||
供增强版对话页(assistant.html)做前端检索与**引用溯源展示**。
|
||||
|
||||
与 rag-kb 内核的关系:``core/rag-kb`` 是完整检索管线(暂无 HTTP 服务),
|
||||
本脚本只把文档资产转成前端可 fetch 的静态语料(Demo 级),
|
||||
引用条目带「文档名 + 章节」,对齐 PRD 5.4「强制引用溯源」的展示语义。
|
||||
|
||||
用法(仓库根目录下):
|
||||
python web/chat/scripts/build_citations.py
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import glob
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
|
||||
_REPO_ROOT = os.path.dirname(
|
||||
os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
)
|
||||
_DOC_GLOB = os.path.join(_REPO_ROOT, "templates", "*", "rag-kb", "documents", "*.md")
|
||||
_OUT_PATH = os.path.join(_REPO_ROOT, "web", "chat", "citations.json")
|
||||
|
||||
#: 每个章节摘要的最大字符数(控制语料体积,前端检索只看要点)
|
||||
SNIPPET_MAX_CHARS = 160
|
||||
|
||||
|
||||
def extract_sections(path: str) -> dict:
|
||||
"""从一份 Markdown 文档抽取标题与章节摘要。"""
|
||||
with open(path, encoding="utf-8") as f:
|
||||
text = f.read()
|
||||
rel = os.path.relpath(path, _REPO_ROOT).replace(os.sep, "/")
|
||||
title_match = re.search(r"^#\s+(.+)$", text, flags=re.M)
|
||||
title = title_match.group(1).strip() if title_match else os.path.basename(path)
|
||||
|
||||
sections = []
|
||||
# 按 ## 章节切分;无章节时整篇作为一节
|
||||
parts = re.split(r"^##\s+(.+)$", text, flags=re.M)
|
||||
if len(parts) >= 3:
|
||||
for i in range(1, len(parts), 2):
|
||||
heading = parts[i].strip()
|
||||
body = parts[i + 1] if i + 1 < len(parts) else ""
|
||||
snippet = re.sub(r"\s+", " ", body).strip()[:SNIPPET_MAX_CHARS]
|
||||
if snippet:
|
||||
sections.append({"heading": heading, "snippet": snippet})
|
||||
else:
|
||||
snippet = re.sub(r"\s+", " ", text).strip()[:SNIPPET_MAX_CHARS]
|
||||
if snippet:
|
||||
sections.append({"heading": "全文", "snippet": snippet})
|
||||
|
||||
return {"title": title, "source": rel, "sections": sections}
|
||||
|
||||
|
||||
def main() -> int:
|
||||
docs = []
|
||||
for path in sorted(glob.glob(_DOC_GLOB)):
|
||||
try:
|
||||
docs.append(extract_sections(path))
|
||||
print(f"[OK] {os.path.basename(path)} "
|
||||
f"({len(docs[-1]['sections'])} 章节)")
|
||||
except Exception as exc: # noqa: BLE001 - 聚合全部失败
|
||||
print(f"[FAIL] {path}: {exc}")
|
||||
return 1
|
||||
if not docs:
|
||||
print("未找到任何知识库文档")
|
||||
return 1
|
||||
with open(_OUT_PATH, "w", encoding="utf-8") as f:
|
||||
json.dump({"schema": "iAOP-chat-citations-v1", "documents": docs},
|
||||
f, ensure_ascii=False, indent=2)
|
||||
print(f"共 {len(docs)} 份文档 -> {os.path.relpath(_OUT_PATH, _REPO_ROOT)}")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
@@ -0,0 +1,39 @@
|
||||
# web/studio — iAOP 模板配置台 UI
|
||||
|
||||
issue #132 / PRD 5.7「⑤.7 模板配置台」。纯静态单页应用(无构建链),
|
||||
覆盖 PRD 5.7 关键页面线框:导入页 / 超参配置页 / 驾驶舱编排页 / 版本页,
|
||||
三级 RBAC(Viewer / Editor / Publisher)。
|
||||
|
||||
## 使用
|
||||
|
||||
```bash
|
||||
cd web/studio && python -m http.server 8081
|
||||
# 浏览器打开 http://localhost:8081 (Chrome / Edge 最新两版)
|
||||
```
|
||||
|
||||
## 页面与后端语义对齐
|
||||
|
||||
配置台后端内核 `core/template-console/` 是纯标准库引擎(暂无 HTTP 服务),
|
||||
本 UI 在前端镜像其语义;配置与版本持久化在浏览器 localStorage(Demo 级)。
|
||||
|
||||
| 页面 | 能力 | 对齐内核模块 |
|
||||
|------|------|--------------|
|
||||
| ① 点位导入 | CSV 上传/粘贴、校验进度条、错误列表(行号+原因)、下载错误模板 | `point_importer.py`(表头列序/必填/数据类型/采样率/重复点号/OPC 节点/模板量纲收窄) |
|
||||
| ② 模型超参 | 左树 4 类模型(质量预测/异常检测/工艺优化/跨工序关联)、右表单(默认值/单位/范围)、实时 JSON 预览 | `config_store.py`(ConfigKind.MODEL_PARAM)、`core/model-framework` 示例 |
|
||||
| ③ 驾驶舱编排 | 组件库(5 种 widget)/ 12 列画布 / 数据源绑定面板,产出 `iAOP-cockpit-layout-v1` JSON | `config_store.py`(ConfigKind.LAYOUT)、`core/cockpit/layout.py` |
|
||||
| ④ 版本发布 | 版本列表(状态/时间/作者)、diff 视图、回滚(不删历史、事件可追溯)、导出模板资产包 | `release.py`(快照固化 / semver 单调递增 / 回滚可追溯) |
|
||||
|
||||
## RBAC(对齐 core/template-console/rbac.py)
|
||||
|
||||
- **Viewer(readonly)**:只读预览,全部编辑/发布按钮禁用;
|
||||
- **Editor(engineer)**:可配置(导入校验/超参/编排),不可发布与回滚;
|
||||
- **Publisher(admin)**:可配置 + 发布 + 回滚。
|
||||
|
||||
顶栏角色切换即切换权限视图(Demo 级,未接认证——认证由 issue #134 覆盖)。
|
||||
|
||||
## 耦合边界
|
||||
|
||||
配置台唯一产出物是「模板资产包」JSON(版本页导出,
|
||||
`pack_schema: iAOP-template-pack-v1`,含版本快照:超参/布局/点位校验报告),
|
||||
供内核灰度推送(对齐 `push_channel.py` 的 PushManifest 语义),
|
||||
不直接改内核运行时。
|
||||
@@ -0,0 +1,150 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>iAOP 模板配置台</title>
|
||||
<link rel="stylesheet" href="studio.css">
|
||||
</head>
|
||||
<body>
|
||||
<!-- iAOP 模板配置台(issue #132 / PRD 5.7「⑤.7 模板配置台」)
|
||||
纯静态单页应用(无构建链):导入页 / 超参配置页 / 驾驶舱编排页 / 版本页。
|
||||
三级 RBAC 对齐 core/template-console/rbac.py 的 readonly/engineer/admin;
|
||||
配置台只产出「模板资产包」(导出 JSON),不直接改内核运行时(耦合边界)。 -->
|
||||
<header id="topbar">
|
||||
<h1>iAOP 模板配置台 <span class="sub">Template Console · PRD 5.7</span></h1>
|
||||
<div id="topbar-right">
|
||||
<label for="role-select">当前角色</label>
|
||||
<select id="role-select">
|
||||
<option value="readonly">Viewer(只读)</option>
|
||||
<option value="engineer" selected>Editor(配置)</option>
|
||||
<option value="admin">Publisher(发布+回滚)</option>
|
||||
</select>
|
||||
<span id="role-hint" class="hint"></span>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<nav id="tabs">
|
||||
<button class="tab active" data-page="import">① 点位导入</button>
|
||||
<button class="tab" data-page="hyper">② 模型超参</button>
|
||||
<button class="tab" data-page="layout">③ 驾驶舱编排</button>
|
||||
<button class="tab" data-page="version">④ 版本发布</button>
|
||||
</nav>
|
||||
|
||||
<!-- ======================= ① 导入页 ======================= -->
|
||||
<section class="page active" id="page-import">
|
||||
<div class="panel">
|
||||
<h2>点位字典 CSV 导入</h2>
|
||||
<p class="hint">字段规范对齐 PRD 5.1 / core/edge-gateway/point_dict:
|
||||
device_id, point_id, name, unit, dataType, sampleRate, qualityCode, opcNode, protocol</p>
|
||||
<div id="drop-zone" class="drop-zone">
|
||||
拖拽 CSV 文件到这里,或
|
||||
<input type="file" id="csv-file" accept=".csv,text/csv">
|
||||
<button id="btn-download-template">下载错误模板(示例 CSV)</button>
|
||||
</div>
|
||||
<textarea id="csv-text" rows="6" placeholder="或直接粘贴 CSV 内容…"></textarea>
|
||||
<div class="row">
|
||||
<label>行业模板
|
||||
<select id="import-template">
|
||||
<option value="ti">氯化(ti-cl4)</option>
|
||||
<option value="resin">树脂(resin)</option>
|
||||
</select>
|
||||
</label>
|
||||
<button id="btn-validate" class="primary need-edit">开始校验</button>
|
||||
</div>
|
||||
<div id="import-progress-wrap" hidden>
|
||||
<div id="import-progress"><div id="import-progress-bar"></div></div>
|
||||
<div id="import-progress-text" class="hint"></div>
|
||||
</div>
|
||||
<div id="import-summary"></div>
|
||||
<table id="issue-table" hidden>
|
||||
<thead><tr><th>行号</th><th>级别</th><th>字段</th><th>原因</th></tr></thead>
|
||||
<tbody></tbody>
|
||||
</table>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<!-- ======================= ② 超参配置页 ======================= -->
|
||||
<section class="page" id="page-hyper">
|
||||
<div class="split">
|
||||
<aside class="tree panel">
|
||||
<h2>模型类型(4 类)</h2>
|
||||
<ul id="model-tree"></ul>
|
||||
</aside>
|
||||
<div class="panel" id="hyper-form-panel">
|
||||
<h2 id="hyper-title">超参配置</h2>
|
||||
<form id="hyper-form"></form>
|
||||
</div>
|
||||
<div class="panel">
|
||||
<h2>实时 JSON 预览</h2>
|
||||
<pre id="hyper-json" class="json-view"></pre>
|
||||
<div class="row">
|
||||
<button id="btn-hyper-save" class="primary need-edit">保存到配置项</button>
|
||||
<span id="hyper-save-hint" class="hint"></span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<!-- ======================= ③ 驾驶舱编排页 ======================= -->
|
||||
<section class="page" id="page-layout">
|
||||
<div class="split">
|
||||
<aside class="panel" id="widget-lib">
|
||||
<h2>组件库</h2>
|
||||
<p class="hint">点击添加到画布(组件类型对齐 iAOP-cockpit-layout-v1)</p>
|
||||
<ul id="widget-lib-list"></ul>
|
||||
</aside>
|
||||
<div class="panel">
|
||||
<h2>画布(12 列栅格)</h2>
|
||||
<div id="canvas"></div>
|
||||
<div class="row">
|
||||
<button id="btn-layout-clear" class="need-edit">清空画布</button>
|
||||
<button id="btn-layout-load-ti" class="need-edit">载入 ti-cl4 基线</button>
|
||||
</div>
|
||||
</div>
|
||||
<aside class="panel" id="bind-panel">
|
||||
<h2>数据源绑定</h2>
|
||||
<div id="bind-form-wrap"><p class="hint">选中画布中的组件后进行绑定</p></div>
|
||||
<h2>布局 JSON</h2>
|
||||
<pre id="layout-json" class="json-view"></pre>
|
||||
</aside>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<!-- ======================= ④ 版本页 ======================= -->
|
||||
<section class="page" id="page-version">
|
||||
<div class="split-2">
|
||||
<div class="panel">
|
||||
<h2>版本列表</h2>
|
||||
<div class="row">
|
||||
<input id="release-version" placeholder="版本号(semver,如 1.0.0)" value="1.0.0">
|
||||
<input id="release-author" placeholder="作者" value="engineer">
|
||||
<button id="btn-publish" class="primary need-publish">发布当前配置</button>
|
||||
</div>
|
||||
<p class="hint">发布 = 固化当前超参 + 布局 + 校验结果为快照(对齐 core/template-console/release.py);
|
||||
配置台只产出模板资产包,不直接改内核运行时。</p>
|
||||
<table id="version-table">
|
||||
<thead><tr><th>版本</th><th>状态</th><th>时间</th><th>作者</th><th>操作</th></tr></thead>
|
||||
<tbody></tbody>
|
||||
</table>
|
||||
</div>
|
||||
<div class="panel">
|
||||
<h2>diff 视图</h2>
|
||||
<div class="row">
|
||||
<select id="diff-left"></select>
|
||||
<span class="hint">vs</span>
|
||||
<select id="diff-right"></select>
|
||||
<button id="btn-diff">对比</button>
|
||||
</div>
|
||||
<pre id="diff-view" class="json-view"></pre>
|
||||
<div class="row">
|
||||
<button id="btn-export-pack" class="primary">导出模板资产包</button>
|
||||
<span class="hint">资产包 = 选中版本快照(超参/布局/点位校验报告),供内核灰度推送</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<script src="studio.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,106 @@
|
||||
/* iAOP 模板配置台样式(issue #132 / PRD 5.7)。深色主题,与 web/cockpit 一致。 */
|
||||
:root {
|
||||
--cockpit-bg: #0b1220;
|
||||
--cockpit-surface: #13203a;
|
||||
--cockpit-fg: #e6edf6;
|
||||
--cockpit-fg-muted: #8aa0bd;
|
||||
--cockpit-accent: #18d3c8;
|
||||
--cockpit-warn: #f5a623;
|
||||
--cockpit-grid-line: rgba(255,255,255,0.06);
|
||||
}
|
||||
* { box-sizing: border-box; }
|
||||
body { margin: 0; font-family: "Microsoft YaHei", "PingFang SC", sans-serif;
|
||||
background: var(--cockpit-bg); color: var(--cockpit-fg); }
|
||||
|
||||
#topbar { display: flex; align-items: center; justify-content: space-between;
|
||||
padding: 10px 16px; background: var(--cockpit-surface);
|
||||
border-bottom: 1px solid var(--cockpit-grid-line); }
|
||||
#topbar h1 { font-size: 18px; margin: 0; color: var(--cockpit-accent); }
|
||||
#topbar .sub { font-size: 12px; color: var(--cockpit-fg-muted); font-weight: 400; }
|
||||
#topbar-right { display: flex; align-items: center; gap: 8px; font-size: 13px;
|
||||
color: var(--cockpit-fg-muted); }
|
||||
|
||||
#tabs { display: flex; gap: 4px; padding: 8px 16px 0; }
|
||||
.tab { padding: 8px 18px; border: 1px solid var(--cockpit-grid-line); border-bottom: none;
|
||||
border-radius: 8px 8px 0 0; background: transparent; color: var(--cockpit-fg-muted);
|
||||
cursor: pointer; font-size: 14px; }
|
||||
.tab.active { background: var(--cockpit-surface); color: var(--cockpit-accent); }
|
||||
|
||||
.page { display: none; padding: 12px 16px 24px; }
|
||||
.page.active { display: block; }
|
||||
.panel { background: var(--cockpit-surface); border: 1px solid var(--cockpit-grid-line);
|
||||
border-radius: 8px; padding: 14px 16px; }
|
||||
.panel h2 { font-size: 14px; margin: 0 0 10px; color: var(--cockpit-accent); }
|
||||
.hint { font-size: 12px; color: var(--cockpit-fg-muted); }
|
||||
.row { display: flex; align-items: center; gap: 10px; margin: 10px 0; flex-wrap: wrap; }
|
||||
|
||||
select, input, textarea { padding: 7px 10px; border-radius: 6px; font-size: 13px;
|
||||
border: 1px solid var(--cockpit-grid-line); background: var(--cockpit-bg);
|
||||
color: var(--cockpit-fg); }
|
||||
textarea { width: 100%; font-family: Consolas, monospace; }
|
||||
button { padding: 7px 16px; border: none; border-radius: 6px; cursor: pointer;
|
||||
background: #1e293b; color: var(--cockpit-fg); font-size: 13px; }
|
||||
button.primary { background: var(--cockpit-accent); color: #04202a; }
|
||||
button:disabled { opacity: .4; cursor: not-allowed; }
|
||||
button.danger { background: #7a1f1a; color: #ffb4ad; }
|
||||
|
||||
/* 三栏布局(超参页 / 编排页) */
|
||||
.split { display: grid; grid-template-columns: 220px 1fr 320px; gap: 12px; }
|
||||
.split-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
|
||||
|
||||
/* ① 导入页 */
|
||||
.drop-zone { border: 2px dashed var(--cockpit-grid-line); border-radius: 8px;
|
||||
padding: 18px; text-align: center; color: var(--cockpit-fg-muted);
|
||||
margin-bottom: 10px; }
|
||||
.drop-zone.dragover { border-color: var(--cockpit-accent); color: var(--cockpit-accent); }
|
||||
#import-progress { height: 10px; background: var(--cockpit-bg); border-radius: 5px;
|
||||
overflow: hidden; margin-top: 8px; }
|
||||
#import-progress-bar { height: 100%; width: 0; background: var(--cockpit-accent);
|
||||
transition: width .1s; }
|
||||
table { width: 100%; border-collapse: collapse; margin-top: 10px; font-size: 12px; }
|
||||
th, td { padding: 6px 8px; border-bottom: 1px solid var(--cockpit-grid-line);
|
||||
text-align: left; }
|
||||
th { color: var(--cockpit-fg-muted); font-weight: 400; }
|
||||
.sev-error { color: #ff3b30; }
|
||||
.sev-warning { color: var(--cockpit-warn); }
|
||||
#import-summary { margin-top: 10px; font-size: 13px; }
|
||||
|
||||
/* ② 超参配置页 */
|
||||
.tree ul { list-style: none; padding: 0; margin: 0; }
|
||||
.tree li { padding: 8px 10px; border-radius: 6px; cursor: pointer; font-size: 13px; }
|
||||
.tree li.active { background: var(--cockpit-bg); color: var(--cockpit-accent); }
|
||||
.form-item { margin-bottom: 12px; }
|
||||
.form-item label { display: block; font-size: 13px; margin-bottom: 4px; }
|
||||
.form-item .meta { font-size: 11px; color: var(--cockpit-fg-muted); margin-top: 2px; }
|
||||
.form-item input { width: 100%; }
|
||||
.json-view { background: var(--cockpit-bg); border-radius: 6px; padding: 10px;
|
||||
font-size: 12px; font-family: Consolas, monospace; overflow: auto;
|
||||
max-height: 480px; white-space: pre; }
|
||||
|
||||
/* ③ 驾驶舱编排页 */
|
||||
#widget-lib-list { list-style: none; padding: 0; margin: 0; }
|
||||
#widget-lib-list li { padding: 8px 10px; margin-bottom: 6px; border-radius: 6px;
|
||||
background: var(--cockpit-bg); cursor: pointer; font-size: 13px;
|
||||
border: 1px solid var(--cockpit-grid-line); }
|
||||
#widget-lib-list li:hover { border-color: var(--cockpit-accent); }
|
||||
#canvas { display: grid; grid-template-columns: repeat(12, 1fr); grid-auto-rows: 34px;
|
||||
gap: 6px; min-height: 320px; background: var(--cockpit-bg); border-radius: 8px;
|
||||
padding: 8px; }
|
||||
.canvas-widget { border: 1px solid var(--cockpit-accent); border-radius: 6px;
|
||||
background: rgba(24,211,200,.08); font-size: 12px; padding: 4px 6px;
|
||||
cursor: pointer; overflow: hidden; position: relative; }
|
||||
.canvas-widget.selected { outline: 2px solid var(--cockpit-warn); }
|
||||
.canvas-widget .ops { position: absolute; right: 4px; top: 2px; }
|
||||
.canvas-widget .ops button { padding: 0 6px; font-size: 11px; background: transparent;
|
||||
color: var(--cockpit-fg-muted); }
|
||||
|
||||
/* ④ 版本页 */
|
||||
.status-published { color: var(--cockpit-accent); }
|
||||
.status-rolledback { color: var(--cockpit-warn); }
|
||||
.diff-add { color: #2ecc71; }
|
||||
.diff-del { color: #ff3b30; }
|
||||
.diff-ctx { color: var(--cockpit-fg-muted); }
|
||||
|
||||
@media (max-width: 1100px) {
|
||||
.split, .split-2 { grid-template-columns: 1fr; }
|
||||
}
|
||||
@@ -0,0 +1,604 @@
|
||||
/* iAOP 模板配置台(issue #132 / PRD 5.7「⑤.7 模板配置台」)。
|
||||
*
|
||||
* 纯静态单页应用(无构建链),Demo 级交付:
|
||||
* ① 导入页 —— 点位字典 CSV 上传/校验进度/错误列表(行号+原因)/下载错误模板
|
||||
* ② 超参配置页 —— 左树 4 类模型 + 右表单(默认值/单位/范围)+ 实时 JSON 预览
|
||||
* ③ 编排页 —— 组件库 / 12 列画布 / 数据源绑定面板,产出 iAOP-cockpit-layout-v1 JSON
|
||||
* ④ 版本页 —— 版本列表(状态/时间/作者)/ diff 视图 / 回滚 / 导出模板资产包
|
||||
*
|
||||
* 语义对齐后端内核 core/template-console(rbac/point_importer/config_store/release):
|
||||
* 后端是纯标准库引擎、暂无 HTTP 服务,本页面在前端镜像其校验与版本语义,
|
||||
* 配置与版本持久化在 localStorage;耦合边界 = 配置台只导出「模板资产包」JSON,
|
||||
* 不直接改内核运行时(PRD 5.7)。
|
||||
*/
|
||||
"use strict";
|
||||
|
||||
/* ================= RBAC(对齐 core/template-console/rbac.py) ================= */
|
||||
// 角色:readonly=Viewer(只读)/ engineer=Editor(配置)/ admin=Publisher(发布+回滚)
|
||||
var RBAC = {
|
||||
readonly: { edit: false, publish: false, label: "Viewer:只读预览" },
|
||||
engineer: { edit: true, publish: false, label: "Editor:可配置,不可发布" },
|
||||
admin: { edit: true, publish: true, label: "Publisher:可配置/发布/回滚" }
|
||||
};
|
||||
var currentRole = "engineer";
|
||||
|
||||
function applyRbac() {
|
||||
var perm = RBAC[currentRole];
|
||||
document.getElementById("role-hint").textContent = perm.label;
|
||||
document.querySelectorAll(".need-edit").forEach(function (b) { b.disabled = !perm.edit; });
|
||||
document.querySelectorAll(".need-publish").forEach(function (b) { b.disabled = !perm.publish; });
|
||||
}
|
||||
|
||||
/* ================= 工具 ================= */
|
||||
function el(tag, cls, text) {
|
||||
var n = document.createElement(tag);
|
||||
if (cls) n.className = cls;
|
||||
if (text !== undefined) n.textContent = text;
|
||||
return n;
|
||||
}
|
||||
function download(filename, text, mime) {
|
||||
var a = document.createElement("a");
|
||||
a.href = URL.createObjectURL(new Blob([text], { type: mime || "application/octet-stream" }));
|
||||
a.download = filename;
|
||||
a.click();
|
||||
URL.revokeObjectURL(a.href);
|
||||
}
|
||||
function store(key, obj) { localStorage.setItem(key, JSON.stringify(obj)); }
|
||||
function load(key, fallback) {
|
||||
try { var v = localStorage.getItem(key); return v ? JSON.parse(v) : fallback; }
|
||||
catch (e) { return fallback; }
|
||||
}
|
||||
|
||||
/* ================= ① 导入页(对齐 point_importer.py 校验维度) ================= */
|
||||
// 表头列序对齐 core/edge-gateway/point_dict/schema.py 的 CSV_HEADERS
|
||||
var CSV_HEADERS = ["device_id", "point_id", "name", "unit", "dataType",
|
||||
"sampleRate", "qualityCode", "opcNode", "protocol"];
|
||||
var VALID_DATA_TYPES = ["float", "int", "bool", "string"];
|
||||
var OPC_NODE_RE = /^ns=\d+;s=\S+$/; // OPC 节点格式(point_importer._validate_opc_node)
|
||||
// 模板级量纲收窄(point_importer._unit_set,演示版内置常用子集)
|
||||
var TEMPLATE_UNITS = {
|
||||
ti: ["℃", "kPa", "Pa", "Nm³/h", "kg/h", "t/h", "%", "kWh", "mV", "ppm", "-"],
|
||||
resin: ["℃", "kPa", "r/min", "mmol/g", "%", "kWh/t", "m³/h", "t", "-"]
|
||||
};
|
||||
|
||||
function validateCsv(text, template) {
|
||||
var issues = [];
|
||||
var lines = text.replace(/\r\n?/g, "\n").split("\n")
|
||||
.filter(function (l) { return l.trim() !== ""; });
|
||||
if (!lines.length) {
|
||||
issues.push({ line: 0, severity: "error", field: "-", reason: "CSV 内容为空" });
|
||||
return { issues: issues, total: 0 };
|
||||
}
|
||||
// 1) 表头列序校验(point_importer._validate_header_order)
|
||||
var headers = lines[0].split(",").map(function (h) { return h.trim(); });
|
||||
if (headers.join(",") !== CSV_HEADERS.join(",")) {
|
||||
issues.push({ line: 1, severity: "error", field: "header",
|
||||
reason: "表头列序不符,期望: " + CSV_HEADERS.join(",") });
|
||||
}
|
||||
// 2) 行级校验
|
||||
var seen = {};
|
||||
var units = TEMPLATE_UNITS[template] || TEMPLATE_UNITS.ti;
|
||||
for (var i = 1; i < lines.length; i++) {
|
||||
var ln = i + 1;
|
||||
var cols = lines[i].split(",").map(function (c) { return c.trim(); });
|
||||
if (cols.length !== CSV_HEADERS.length) {
|
||||
issues.push({ line: ln, severity: "error", field: "-",
|
||||
reason: "列数 " + cols.length + " ≠ " + CSV_HEADERS.length });
|
||||
continue;
|
||||
}
|
||||
var row = {};
|
||||
CSV_HEADERS.forEach(function (h, k) { row[h] = cols[k]; });
|
||||
["device_id", "point_id", "name"].forEach(function (f) {
|
||||
if (!row[f]) issues.push({ line: ln, severity: "error", field: f, reason: f + " 不能为空" });
|
||||
});
|
||||
if (row.point_id) {
|
||||
if (seen[row.point_id]) {
|
||||
issues.push({ line: ln, severity: "error", field: "point_id",
|
||||
reason: "点号重复(首次出现于第 " + seen[row.point_id] + " 行)" });
|
||||
} else seen[row.point_id] = ln;
|
||||
}
|
||||
if (VALID_DATA_TYPES.indexOf(row.dataType) < 0) {
|
||||
issues.push({ line: ln, severity: "error", field: "dataType",
|
||||
reason: "数据类型非法: " + row.dataType + "(允许 " + VALID_DATA_TYPES.join("/") + ")" });
|
||||
}
|
||||
if (!/^\d+$/.test(row.sampleRate) || parseInt(row.sampleRate, 10) <= 0) {
|
||||
issues.push({ line: ln, severity: "error", field: "sampleRate",
|
||||
reason: "采样率必须为正整数毫秒: " + row.sampleRate });
|
||||
}
|
||||
if (row.qualityCode !== "true" && row.qualityCode !== "false") {
|
||||
issues.push({ line: ln, severity: "warning", field: "qualityCode",
|
||||
reason: "qualityCode 建议 true/false: " + row.qualityCode });
|
||||
}
|
||||
if (row.opcNode && !OPC_NODE_RE.test(row.opcNode)) {
|
||||
issues.push({ line: ln, severity: "error", field: "opcNode",
|
||||
reason: "OPC 节点格式非法(期望 ns=<n>;s=<id>): " + row.opcNode });
|
||||
}
|
||||
if (row.unit && units.indexOf(row.unit) < 0) {
|
||||
issues.push({ line: ln, severity: "warning", field: "unit",
|
||||
reason: "量纲超出 " + template + " 模板收窄范围: " + row.unit });
|
||||
}
|
||||
}
|
||||
return { issues: issues, total: lines.length - 1 };
|
||||
}
|
||||
|
||||
function runImportValidation() {
|
||||
var text = document.getElementById("csv-text").value;
|
||||
var template = document.getElementById("import-template").value;
|
||||
var wrap = document.getElementById("import-progress-wrap");
|
||||
var bar = document.getElementById("import-progress-bar");
|
||||
var ptext = document.getElementById("import-progress-text");
|
||||
var summary = document.getElementById("import-summary");
|
||||
var table = document.getElementById("issue-table");
|
||||
wrap.hidden = false;
|
||||
summary.textContent = "";
|
||||
table.hidden = true;
|
||||
|
||||
// 校验进度条(按行推进,演示校验过程;行数小时立即完成)
|
||||
var totalLines = Math.max(text.split("\n").length, 1);
|
||||
var done = 0;
|
||||
var timer = setInterval(function () {
|
||||
done += Math.ceil(totalLines / 20);
|
||||
if (done >= totalLines) {
|
||||
done = totalLines;
|
||||
clearInterval(timer);
|
||||
var report = validateCsv(text, template);
|
||||
var errors = report.issues.filter(function (x) { return x.severity === "error"; });
|
||||
summary.textContent = "校验完成:共 " + report.total + " 行," +
|
||||
errors.length + " 个错误 / " + (report.issues.length - errors.length) + " 个警告" +
|
||||
(errors.length ? ",请修正后重新导入" : ",可进入下一步配置");
|
||||
summary.className = errors.length ? "sev-error" : "";
|
||||
if (report.issues.length) {
|
||||
var tbody = table.querySelector("tbody");
|
||||
tbody.innerHTML = "";
|
||||
report.issues.forEach(function (x) {
|
||||
var tr = el("tr");
|
||||
tr.appendChild(el("td", null, String(x.line)));
|
||||
tr.appendChild(el("td", x.severity === "error" ? "sev-error" : "sev-warning",
|
||||
x.severity === "error" ? "错误" : "警告"));
|
||||
tr.appendChild(el("td", null, x.field));
|
||||
tr.appendChild(el("td", null, x.reason));
|
||||
tbody.appendChild(tr);
|
||||
});
|
||||
table.hidden = false;
|
||||
}
|
||||
// 最近一次校验报告纳入资产包(版本页发布时随快照固化)
|
||||
studioState.importReport = {
|
||||
template: template, total: report.total,
|
||||
errors: errors.length, warnings: report.issues.length - errors.length,
|
||||
issues: report.issues.slice(0, 200)
|
||||
};
|
||||
}
|
||||
bar.style.width = Math.round(done / totalLines * 100) + "%";
|
||||
ptext.textContent = "校验中… " + done + "/" + totalLines + " 行";
|
||||
}, 60);
|
||||
}
|
||||
|
||||
/* ================= ② 超参配置页(4 类模型,对齐 core/model-framework) ================= */
|
||||
// 字段:key / 标签 / 默认值 / 单位 / 范围(对齐 quality_predict_ti.example.json 等内核示例)
|
||||
var MODEL_SPECS = {
|
||||
quality_forecast: {
|
||||
name: "① 质量预测(quality_forecast)",
|
||||
fields: [
|
||||
{ key: "max_depth", label: "树最大深度", def: 6, unit: "层", min: 1, max: 16, type: "int" },
|
||||
{ key: "eta", label: "学习率", def: 0.1, unit: "-", min: 0.001, max: 1, type: "float" },
|
||||
{ key: "n_estimators", label: "迭代树数", def: 300, unit: "棵", min: 10, max: 2000, type: "int" },
|
||||
{ key: "train_window", label: "训练窗口", def: "180d", unit: "天/班次", type: "str" },
|
||||
{ key: "alarm_zscore_k", label: "告警 zscore k", def: 3.0, unit: "σ", min: 1, max: 6, type: "float" },
|
||||
{ key: "drift_psi_limit", label: "漂移 PSI 上限", def: 0.2, unit: "-", min: 0.05, max: 1, type: "float" }
|
||||
]
|
||||
},
|
||||
anomaly_detection: {
|
||||
name: "② 异常检测(anomaly_detection)",
|
||||
fields: [
|
||||
{ key: "contamination", label: "异常比例先验", def: 0.05, unit: "比例", min: 0.001, max: 0.3, type: "float" },
|
||||
{ key: "window", label: "检测窗口", def: "10m", unit: "分钟", type: "str" },
|
||||
{ key: "zscore_k", label: "zscore 阈值 k", def: 3.0, unit: "σ", min: 1, max: 6, type: "float" },
|
||||
{ key: "min_train_samples", label: "最小训练样本数", def: 1000, unit: "条", min: 100, max: 100000, type: "int" }
|
||||
]
|
||||
},
|
||||
process_optimizer: {
|
||||
name: "③ 工艺优化(process_optimizer)",
|
||||
fields: [
|
||||
{ key: "learning_rate", label: "优化学习率", def: 0.05, unit: "-", min: 0.001, max: 1, type: "float" },
|
||||
{ key: "horizon", label: "优化时域", def: "1shift", unit: "班/小时", type: "str" },
|
||||
{ key: "explore_ratio", label: "探索比例", def: 0.1, unit: "比例", min: 0, max: 0.5, type: "float" },
|
||||
{ key: "constraint_penalty", label: "约束惩罚系数", def: 100, unit: "-", min: 1, max: 10000, type: "float" }
|
||||
]
|
||||
},
|
||||
cross_process_optimizer: {
|
||||
name: "④ 跨工序关联(cross_process_optimizer)",
|
||||
fields: [
|
||||
{ key: "coupling_strength", label: "工序耦合强度", def: 0.5, unit: "-", min: 0, max: 1, type: "float" },
|
||||
{ key: "sync_window", label: "同步窗口", def: "1h", unit: "小时", type: "str" },
|
||||
{ key: "max_iter", label: "最大迭代数", def: 500, unit: "次", min: 10, max: 5000, type: "int" },
|
||||
{ key: "tolerance", label: "收敛容差", def: 0.0001, unit: "-", min: 0.000001, max: 0.1, type: "float" }
|
||||
]
|
||||
}
|
||||
};
|
||||
var currentModel = "quality_forecast";
|
||||
|
||||
function renderModelTree() {
|
||||
var ul = document.getElementById("model-tree");
|
||||
ul.innerHTML = "";
|
||||
Object.keys(MODEL_SPECS).forEach(function (k) {
|
||||
var li = el("li", k === currentModel ? "active" : null, MODEL_SPECS[k].name);
|
||||
li.onclick = function () { currentModel = k; renderModelTree(); renderHyperForm(); };
|
||||
ul.appendChild(li);
|
||||
});
|
||||
}
|
||||
|
||||
function hyperValues(model) {
|
||||
// 已保存值优先,否则用默认值
|
||||
var saved = (studioState.config.model_param || {})[model] || {};
|
||||
var values = {};
|
||||
MODEL_SPECS[model].fields.forEach(function (f) {
|
||||
values[f.key] = saved[f.key] !== undefined ? saved[f.key] : f.def;
|
||||
});
|
||||
return values;
|
||||
}
|
||||
|
||||
function renderHyperForm() {
|
||||
var spec = MODEL_SPECS[currentModel];
|
||||
document.getElementById("hyper-title").textContent = spec.name;
|
||||
var form = document.getElementById("hyper-form");
|
||||
form.innerHTML = "";
|
||||
var values = hyperValues(currentModel);
|
||||
spec.fields.forEach(function (f) {
|
||||
var item = el("div", "form-item");
|
||||
item.appendChild(el("label", null, f.label + "(" + f.key + ")"));
|
||||
var input = document.createElement("input");
|
||||
input.value = values[f.key];
|
||||
input.dataset.key = f.key;
|
||||
input.dataset.type = f.type;
|
||||
input.disabled = !RBAC[currentRole].edit;
|
||||
input.oninput = renderHyperJson;
|
||||
item.appendChild(input);
|
||||
var meta = "默认 " + f.def + " · 单位 " + (f.unit || "-");
|
||||
if (f.min !== undefined) meta += " · 范围 [" + f.min + ", " + f.max + "]";
|
||||
item.appendChild(el("div", "meta", meta));
|
||||
form.appendChild(item);
|
||||
});
|
||||
renderHyperJson();
|
||||
}
|
||||
|
||||
function readHyperForm() {
|
||||
var out = {};
|
||||
document.querySelectorAll("#hyper-form input").forEach(function (input) {
|
||||
var v = input.value;
|
||||
if (input.dataset.type === "int") v = parseInt(v, 10);
|
||||
else if (input.dataset.type === "float") v = parseFloat(v);
|
||||
out[input.dataset.key] = v;
|
||||
});
|
||||
return out;
|
||||
}
|
||||
|
||||
function renderHyperJson() {
|
||||
var json = { model_id: currentModel, hyperparams: readHyperForm() };
|
||||
document.getElementById("hyper-json").textContent = JSON.stringify(json, null, 2);
|
||||
}
|
||||
|
||||
function saveHyper() {
|
||||
studioState.config.model_param = studioState.config.model_param || {};
|
||||
studioState.config.model_param[currentModel] = readHyperForm();
|
||||
persistConfig();
|
||||
document.getElementById("hyper-save-hint").textContent = "已保存(待发布固化)";
|
||||
}
|
||||
|
||||
/* ================= ③ 驾驶舱编排页(iAOP-cockpit-layout-v1) ================= */
|
||||
var WIDGET_TYPES = [
|
||||
{ type: "process_view", name: "工艺流程视图", w: 12, h: 4 },
|
||||
{ type: "trend", name: "实时趋势", w: 6, h: 2 },
|
||||
{ type: "kpi_card", name: "KPI 卡片", w: 3, h: 2 },
|
||||
{ type: "alarm_panel", name: "告警面板", w: 9, h: 3 },
|
||||
{ type: "nl_query", name: "NL 查询入口", w: 3, h: 3 }
|
||||
];
|
||||
// ti-cl4 基线(对齐 templates/ti-cl4/dashboard/cockpit.ti.yaml)
|
||||
var TI_BASELINE = [
|
||||
{ type: "process_view", src: "ti_four_state.svg", x: 0, y: 0, w: 12, h: 4,
|
||||
description: "四状态工艺流程(氯化 → 精制 → 还原 → 蒸馏)" },
|
||||
{ type: "trend", bind: "CLF-01.TEMP", x: 0, y: 4, w: 6, h: 2, description: "氯化炉温度实时趋势" },
|
||||
{ type: "trend", bind: "CLF-01.CL2", x: 6, y: 4, w: 6, h: 2, description: "氯气流量实时趋势" },
|
||||
{ type: "kpi_card", metric: "ticl4_purity", bind: "RF-01.PURITY", label: "TiCl₄纯度",
|
||||
x: 0, y: 6, w: 3, h: 2, description: "还原 TiCl₄ 纯度(%)" },
|
||||
{ type: "kpi_card", metric: "ticl4_impurity", bind: "RF-01.IMP", label: "杂质含量",
|
||||
x: 3, y: 6, w: 3, h: 2, description: "还原杂质含量(%)" },
|
||||
{ type: "kpi_card", metric: "energy_per_ton", bind: "E-01.KWH", label: "累计电耗",
|
||||
x: 6, y: 6, w: 3, h: 2, description: "车间累计电耗(kWh)" },
|
||||
{ type: "kpi_card", metric: "steam_flow", bind: "ST-01.STEAM", label: "蒸汽流量",
|
||||
x: 9, y: 6, w: 3, h: 2, description: "蒸汽流量(t/h)" },
|
||||
{ type: "alarm_panel", x: 0, y: 8, w: 9, h: 3, description: "告警面板" },
|
||||
{ type: "nl_query", x: 9, y: 8, w: 3, h: 3, description: "自然语言查询入口" }
|
||||
];
|
||||
var selectedWidget = -1;
|
||||
|
||||
function nextFreeY(widgets, h) {
|
||||
// 简单自动落位:追加到当前最大行底(编排页 Demo 级,手动微调由 bind 面板改 x/y/w/h)
|
||||
var maxY = 0;
|
||||
widgets.forEach(function (w) { maxY = Math.max(maxY, w.y + w.h); });
|
||||
return maxY;
|
||||
}
|
||||
|
||||
function renderWidgetLib() {
|
||||
var ul = document.getElementById("widget-lib-list");
|
||||
ul.innerHTML = "";
|
||||
WIDGET_TYPES.forEach(function (t) {
|
||||
var li = el("li", null, t.name + "(" + t.type + ")");
|
||||
li.onclick = function () {
|
||||
if (!RBAC[currentRole].edit) return;
|
||||
studioState.config.layout.widgets.push({
|
||||
type: t.type, x: 0, y: nextFreeY(studioState.config.layout.widgets, t.h),
|
||||
w: t.w, h: t.h, description: t.name
|
||||
});
|
||||
persistConfig(); renderCanvas();
|
||||
};
|
||||
ul.appendChild(li);
|
||||
});
|
||||
}
|
||||
|
||||
function renderCanvas() {
|
||||
var canvas = document.getElementById("canvas");
|
||||
canvas.innerHTML = "";
|
||||
studioState.config.layout.widgets.forEach(function (w, i) {
|
||||
var card = el("div", "canvas-widget" + (i === selectedWidget ? " selected" : ""));
|
||||
card.style.gridColumn = (w.x + 1) + " / span " + w.w;
|
||||
card.style.gridRow = (w.y + 1) + " / span " + w.h;
|
||||
card.textContent = w.type + (w.label ? " · " + w.label : w.bind ? " · " + w.bind : "");
|
||||
var ops = el("span", "ops");
|
||||
var del = el("button", null, "✕");
|
||||
del.title = "删除";
|
||||
del.onclick = function (e) {
|
||||
e.stopPropagation();
|
||||
if (!RBAC[currentRole].edit) return;
|
||||
studioState.config.layout.widgets.splice(i, 1);
|
||||
selectedWidget = -1;
|
||||
persistConfig(); renderCanvas();
|
||||
};
|
||||
ops.appendChild(del);
|
||||
card.appendChild(ops);
|
||||
card.onclick = function () { selectedWidget = i; renderCanvas(); renderBindPanel(); };
|
||||
canvas.appendChild(card);
|
||||
});
|
||||
renderLayoutJson();
|
||||
}
|
||||
|
||||
function renderBindPanel() {
|
||||
var wrap = document.getElementById("bind-form-wrap");
|
||||
wrap.innerHTML = "";
|
||||
var w = studioState.config.layout.widgets[selectedWidget];
|
||||
if (!w) { wrap.appendChild(el("p", "hint", "选中画布中的组件后进行绑定")); return; }
|
||||
// 按组件类型给出绑定字段(对齐 #50 schema 条件必填)
|
||||
var fields = ["x", "y", "w", "h", "description"];
|
||||
if (w.type === "process_view") fields.push("src");
|
||||
if (w.type === "trend") fields.push("bind");
|
||||
if (w.type === "kpi_card") fields.push("metric", "bind", "label");
|
||||
fields.forEach(function (f) {
|
||||
var item = el("div", "form-item");
|
||||
item.appendChild(el("label", null, f));
|
||||
var input = document.createElement("input");
|
||||
input.value = w[f] !== undefined ? w[f] : "";
|
||||
input.disabled = !RBAC[currentRole].edit;
|
||||
input.oninput = function () {
|
||||
var v = input.value;
|
||||
if (["x", "y", "w", "h"].indexOf(f) >= 0) v = Math.max(f === "w" || f === "h" ? 1 : 0, parseInt(v, 10) || 0);
|
||||
if (v === "") delete w[f]; else w[f] = v;
|
||||
persistConfig(); renderCanvas();
|
||||
};
|
||||
item.appendChild(input);
|
||||
wrap.appendChild(item);
|
||||
});
|
||||
}
|
||||
|
||||
function renderLayoutJson() {
|
||||
var layout = {
|
||||
"$schema": "iAOP-cockpit-layout-v1",
|
||||
title: studioState.config.layout.title,
|
||||
theme: "dark",
|
||||
widgets: studioState.config.layout.widgets
|
||||
};
|
||||
document.getElementById("layout-json").textContent = JSON.stringify(layout, null, 2);
|
||||
}
|
||||
|
||||
/* ================= ④ 版本页(对齐 release.py:快照固化/semver 递增/回滚可追溯) ================= */
|
||||
var SEMVER_RE = /^\d+\.\d+\.\d+$/;
|
||||
|
||||
function semverKey(v) { return v.split(".").map(function (n) { return parseInt(n, 10) * 1e6; })
|
||||
.reduce(function (a, b) { return a + b; }, 0); }
|
||||
|
||||
function currentSnapshot() {
|
||||
return {
|
||||
model_param: studioState.config.model_param || {},
|
||||
layout: studioState.config.layout,
|
||||
import_report: studioState.importReport || null
|
||||
};
|
||||
}
|
||||
|
||||
function renderVersions() {
|
||||
var tbody = document.getElementById("version-table").querySelector("tbody");
|
||||
tbody.innerHTML = "";
|
||||
var selL = document.getElementById("diff-left");
|
||||
var selR = document.getElementById("diff-right");
|
||||
selL.innerHTML = ""; selR.innerHTML = "";
|
||||
studioState.releases.forEach(function (r, i) {
|
||||
var tr = el("tr");
|
||||
tr.appendChild(el("td", null, r.version));
|
||||
tr.appendChild(el("td", r.status === "published" ? "status-published" : "status-rolledback",
|
||||
r.status === "published" ? "已发布" : "回滚事件"));
|
||||
tr.appendChild(el("td", null, r.time));
|
||||
tr.appendChild(el("td", null, r.author));
|
||||
var ops = el("td");
|
||||
var rb = el("button", "danger", "回滚到此版本");
|
||||
rb.disabled = !RBAC[currentRole].publish;
|
||||
rb.onclick = function () { rollbackTo(i); };
|
||||
ops.appendChild(rb);
|
||||
tr.appendChild(ops);
|
||||
tbody.appendChild(tr);
|
||||
[selL, selR].forEach(function (sel) {
|
||||
var opt = document.createElement("option");
|
||||
opt.value = i; opt.textContent = r.version;
|
||||
sel.appendChild(opt);
|
||||
});
|
||||
});
|
||||
if (studioState.releases.length >= 2) {
|
||||
selL.value = String(studioState.releases.length - 2);
|
||||
selR.value = String(studioState.releases.length - 1);
|
||||
}
|
||||
}
|
||||
|
||||
function publish() {
|
||||
var version = document.getElementById("release-version").value.trim();
|
||||
var author = document.getElementById("release-author").value.trim() || "unknown";
|
||||
if (!SEMVER_RE.test(version)) { alert("版本号必须是 semver(如 1.0.0)"); return; }
|
||||
var last = studioState.releases[studioState.releases.length - 1];
|
||||
if (last && semverKey(version) <= semverKey(last.version)) {
|
||||
alert("版本号必须单调递增(当前最新 " + last.version + ")"); return;
|
||||
}
|
||||
studioState.releases.push({
|
||||
version: version, status: "published",
|
||||
time: new Date().toLocaleString("zh-CN"), author: author,
|
||||
changelog: "发布模板资产包", snapshot: currentSnapshot()
|
||||
});
|
||||
store("studio.releases.v1", studioState.releases);
|
||||
renderVersions();
|
||||
}
|
||||
|
||||
function rollbackTo(index) {
|
||||
var target = studioState.releases[index];
|
||||
if (!target) return;
|
||||
if (!confirm("回滚到 " + target.version + "?历史版本不删除,回滚事件将记录为新条目。")) return;
|
||||
// 恢复快照到当前配置(不删历史,回滚事件可追溯——对齐 release.py)
|
||||
studioState.config.model_param = JSON.parse(JSON.stringify(target.snapshot.model_param || {}));
|
||||
studioState.config.layout = JSON.parse(JSON.stringify(target.snapshot.layout));
|
||||
studioState.importReport = target.snapshot.import_report || null;
|
||||
persistConfig();
|
||||
var last = studioState.releases[studioState.releases.length - 1];
|
||||
var parts = last.version.split(".").map(Number);
|
||||
parts[2] += 1;
|
||||
studioState.releases.push({
|
||||
version: parts.join("."), status: "rolledback",
|
||||
time: new Date().toLocaleString("zh-CN"),
|
||||
author: document.getElementById("release-author").value.trim() || "unknown",
|
||||
changelog: "回滚自 " + target.version, snapshot: currentSnapshot()
|
||||
});
|
||||
store("studio.releases.v1", studioState.releases);
|
||||
renderVersions(); renderCanvas(); renderHyperForm();
|
||||
}
|
||||
|
||||
/* 行级 diff(LCS,行数受控,版本页演示足够) */
|
||||
function diffLines(aText, bText) {
|
||||
var a = aText.split("\n"), b = bText.split("\n");
|
||||
var n = a.length, m = b.length;
|
||||
var dp = [];
|
||||
var i, j;
|
||||
for (i = 0; i <= n; i++) { dp.push(new Array(m + 1).fill(0)); }
|
||||
for (i = n - 1; i >= 0; i--) {
|
||||
for (j = m - 1; j >= 0; j--) {
|
||||
dp[i][j] = a[i] === b[j] ? dp[i + 1][j + 1] + 1 : Math.max(dp[i + 1][j], dp[i][j + 1]);
|
||||
}
|
||||
}
|
||||
var out = [];
|
||||
i = 0; j = 0;
|
||||
while (i < n && j < m) {
|
||||
if (a[i] === b[j]) { out.push({ cls: "diff-ctx", text: " " + a[i] }); i++; j++; }
|
||||
else if (dp[i + 1][j] >= dp[i][j + 1]) { out.push({ cls: "diff-del", text: "- " + a[i] }); i++; }
|
||||
else { out.push({ cls: "diff-add", text: "+ " + b[j] }); j++; }
|
||||
}
|
||||
while (i < n) { out.push({ cls: "diff-del", text: "- " + a[i++] }); }
|
||||
while (j < m) { out.push({ cls: "diff-add", text: "+ " + b[j++] }); }
|
||||
return out;
|
||||
}
|
||||
|
||||
function renderDiff() {
|
||||
var view = document.getElementById("diff-view");
|
||||
view.innerHTML = "";
|
||||
var l = studioState.releases[parseInt(document.getElementById("diff-left").value, 10)];
|
||||
var r = studioState.releases[parseInt(document.getElementById("diff-right").value, 10)];
|
||||
if (!l || !r) { view.textContent = "(需要至少 2 个版本)"; return; }
|
||||
diffLines(JSON.stringify(l.snapshot, null, 2), JSON.stringify(r.snapshot, null, 2))
|
||||
.forEach(function (d) { view.appendChild(el("div", d.cls, d.text)); });
|
||||
}
|
||||
|
||||
function exportPack() {
|
||||
var idx = parseInt(document.getElementById("diff-right").value, 10);
|
||||
var r = studioState.releases[idx] || studioState.releases[studioState.releases.length - 1];
|
||||
if (!r) { alert("请先发布一个版本"); return; }
|
||||
// 模板资产包:配置台唯一产出物,供内核灰度推送(耦合边界)
|
||||
var pack = {
|
||||
pack_schema: "iAOP-template-pack-v1",
|
||||
version: r.version, author: r.author, time: r.time,
|
||||
snapshot: r.snapshot
|
||||
};
|
||||
download("template-pack-" + r.version + ".json", JSON.stringify(pack, null, 2),
|
||||
"application/json");
|
||||
}
|
||||
|
||||
/* ================= 状态与启动 ================= */
|
||||
var studioState = {
|
||||
config: load("studio.config.v1", {
|
||||
model_param: {},
|
||||
layout: { title: "新建行业驾驶舱", widgets: [] }
|
||||
}),
|
||||
releases: load("studio.releases.v1", []),
|
||||
importReport: null
|
||||
};
|
||||
function persistConfig() { store("studio.config.v1", studioState.config); }
|
||||
|
||||
(function init() {
|
||||
// 页签切换
|
||||
document.querySelectorAll(".tab").forEach(function (tab) {
|
||||
tab.onclick = function () {
|
||||
document.querySelectorAll(".tab").forEach(function (t) { t.classList.remove("active"); });
|
||||
document.querySelectorAll(".page").forEach(function (p) { p.classList.remove("active"); });
|
||||
tab.classList.add("active");
|
||||
document.getElementById("page-" + tab.dataset.page).classList.add("active");
|
||||
};
|
||||
});
|
||||
// RBAC
|
||||
document.getElementById("role-select").onchange = function (e) {
|
||||
currentRole = e.target.value;
|
||||
applyRbac(); renderHyperForm(); renderCanvas(); renderBindPanel(); renderVersions();
|
||||
};
|
||||
// ① 导入页
|
||||
var drop = document.getElementById("drop-zone");
|
||||
var fileInput = document.getElementById("csv-file");
|
||||
function readFile(file) {
|
||||
var reader = new FileReader();
|
||||
reader.onload = function () { document.getElementById("csv-text").value = reader.result; };
|
||||
reader.readAsText(file, "utf-8");
|
||||
}
|
||||
fileInput.onchange = function () { if (fileInput.files[0]) readFile(fileInput.files[0]); };
|
||||
drop.ondragover = function (e) { e.preventDefault(); drop.classList.add("dragover"); };
|
||||
drop.ondragleave = function () { drop.classList.remove("dragover"); };
|
||||
drop.ondrop = function (e) {
|
||||
e.preventDefault(); drop.classList.remove("dragover");
|
||||
if (e.dataTransfer.files[0]) readFile(e.dataTransfer.files[0]);
|
||||
};
|
||||
document.getElementById("btn-download-template").onclick = function () {
|
||||
download("point_dict.template.csv",
|
||||
CSV_HEADERS.join(",") + "\n" +
|
||||
"CLF-01,CLF-01.TEMP,炉温,℃,float,1000,true,ns=2;s=CLF.Temp,simulator\n" +
|
||||
"CLF-01,CLF-01.CL2,氯气流量,Nm³/h,float,1000,true,ns=2;s=CLF.Cl2,simulator\n",
|
||||
"text/csv");
|
||||
};
|
||||
document.getElementById("btn-validate").onclick = runImportValidation;
|
||||
// ② 超参页
|
||||
document.getElementById("btn-hyper-save").onclick = saveHyper;
|
||||
// ③ 编排页
|
||||
document.getElementById("btn-layout-clear").onclick = function () {
|
||||
studioState.config.layout.widgets = []; selectedWidget = -1;
|
||||
persistConfig(); renderCanvas(); renderBindPanel();
|
||||
};
|
||||
document.getElementById("btn-layout-load-ti").onclick = function () {
|
||||
studioState.config.layout = { title: "海绵钛车间驾驶舱", widgets: JSON.parse(JSON.stringify(TI_BASELINE)) };
|
||||
selectedWidget = -1;
|
||||
persistConfig(); renderCanvas(); renderBindPanel();
|
||||
};
|
||||
// ④ 版本页
|
||||
document.getElementById("btn-publish").onclick = publish;
|
||||
document.getElementById("btn-diff").onclick = renderDiff;
|
||||
document.getElementById("btn-export-pack").onclick = exportPack;
|
||||
|
||||
applyRbac();
|
||||
renderModelTree(); renderHyperForm();
|
||||
renderWidgetLib(); renderCanvas(); renderBindPanel();
|
||||
renderVersions();
|
||||
})();
|
||||
Reference in New Issue
Block a user