feat: 完成 issue #169 [B3] 对话助手重构——消息流+模型选择+引用溯源+离线降级

This commit is contained in:
2026-08-06 12:57:21 +08:00
parent df6776caba
commit 81222ad990
2 changed files with 282 additions and 0 deletions
@@ -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,197 @@
<script setup lang="ts">
/**
* 对话助手(Epic #163 / issue #169 [B3])
* 对齐旧 web/chat/assistant.js:
* - 消息列表 + 输入区(AntD)
* - infer.ts chat completions + 模型选择(/v1/models)
* - citations.json 引用来源展示(PRD 5.4 溯源)
* - 网关离线/dry-run 降级提示
*/
import { computed, onMounted, ref } from 'vue';
import { ask, fetchHealth, fetchModels } from '#/api/iaop/infer';
interface Msg {
role: 'user' | 'assistant';
content: string;
meta?: Record<string, unknown>;
citations?: string[];
}
const messages = ref<Msg[]>([
{ role: 'assistant', content: '你好,我是 iAOP 工艺助手。可查询工艺规程、报警解释、交接班要点(PRD 5.4)。' },
]);
const input = ref('');
const loading = ref(false);
const models = ref<string[]>([]);
const model = ref('');
const offline = ref(false);
const citations = ref<Record<string, string[]>>({});
const listRef = ref<HTMLElement | null>(null);
onMounted(async () => {
try {
models.value = await fetchModels();
model.value = models.value[0] || '';
const h = await fetchHealth();
offline.value = !h;
} catch {
offline.value = true;
}
try {
const resp = await fetch('/iaop/citations.json');
const d = await resp.json();
citations.value = (d as { documents?: Record<string, string[]> }).documents || {};
} catch {
citations.value = {};
}
});
const scrollBottom = () => {
setTimeout(() => {
if (listRef.value) listRef.value.scrollTop = listRef.value.scrollHeight;
}, 50);
};
async function send() {
const q = input.value.trim();
if (!q || loading.value) return;
messages.value.push({ role: 'user', content: q });
input.value = '';
loading.value = true;
scrollBottom();
try {
const r = await ask(q, { model: model.value, maxTokens: 512 });
const meta = (r.meta || {}) as Record<string, unknown>;
offline.value = Boolean(meta.dry_run);
// 引用溯源:按关键词在 citations 文档中检索(演示:命中即附来源)
const hits = Object.keys(citations.value).filter((k) =>
q.includes(k) || k.split('·')[0].length > 0 && q.includes(k.split('·')[0]),
).slice(0, 3);
messages.value.push({ role: 'assistant', content: r.text, meta, citations: hits });
} catch {
offline.value = true;
messages.value.push({ role: 'assistant', content: '推理服务请求失败,请检查网关(/v1/health)。' });
} finally {
loading.value = false;
scrollBottom();
}
}
</script>
<template>
<div class="chat-page">
<div class="chat-head">
<h1>iAOP 对话助手</h1>
<div class="chat-opts">
<a-select v-model:value="model" style="width: 220px" placeholder="模型">
<a-select-option v-for="m in models" :key="m" :value="m">{{ m }}</a-select-option>
</a-select>
<a-tag :color="offline ? 'red' : 'green'">
{{ offline ? '网关离线 / dry-run' : '网关在线' }}
</a-tag>
</div>
</div>
<a-alert v-if="offline" type="warning" show-icon style="margin-bottom: 12px">
<template #message>推理服务离线或 dry-run 占位:回答可能为占位内容(未连接上游推理服务)。</template>
</a-alert>
<div ref="listRef" class="chat-list">
<div v-for="(m, i) in messages" :key="i" class="chat-row" :class="'role-' + m.role">
<div class="bubble">
<div class="bubble-text">{{ m.content }}</div>
<div v-if="m.citations && m.citations.length" class="citations">
<span v-for="c in m.citations" :key="c" class="cit-tag">📄 {{ c }}</span>
</div>
</div>
</div>
<div v-if="loading" class="chat-row role-assistant">
<div class="bubble bubble-loading">思考中…</div>
</div>
</div>
<div class="chat-input-row">
<a-input
v-model:value="input"
placeholder="输入工艺问题,Enter 发送(如:氯化炉温异常如何处理?)"
@press-enter="send"
/>
<a-button type="primary" :loading="loading" @click="send">发送</a-button>
</div>
</div>
</template>
<style scoped>
.chat-page {
max-width: 900px;
margin: 0 auto;
padding: 12px;
display: flex;
flex-direction: column;
height: calc(100vh - 150px);
}
.chat-head {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 10px;
}
.chat-head h1 {
font-size: 18px;
margin: 0;
}
.chat-opts {
display: flex;
gap: 10px;
align-items: center;
}
.chat-list {
flex: 1;
overflow: auto;
background: rgba(0, 0, 0, 0.02);
border-radius: 8px;
padding: 12px;
}
.chat-row {
display: flex;
margin-bottom: 10px;
}
.role-user {
justify-content: flex-end;
}
.bubble {
max-width: 78%;
padding: 9px 12px;
border-radius: 10px;
font-size: 14px;
line-height: 1.7;
background: #fff;
border: 1px solid rgba(0, 0, 0, 0.08);
}
.role-user .bubble {
background: #1677ff;
color: #fff;
border-color: transparent;
}
.bubble-loading {
color: #999;
}
.citations {
margin-top: 6px;
}
.cit-tag {
display: inline-block;
font-size: 11px;
color: #1677ff;
background: rgba(22, 119, 255, 0.08);
border-radius: 10px;
padding: 2px 8px;
margin-right: 6px;
}
.chat-input-row {
display: flex;
gap: 8px;
margin-top: 12px;
}
</style>