feat: 完成 issue #169 [B3] 对话助手重构——消息流+模型选择+引用溯源+离线降级
This commit is contained in:
@@ -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>
|
||||
Reference in New Issue
Block a user