@bams-app/ui-quick-qa 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,890 +1,890 @@
1
- <template>
2
- <div class="quick-qa">
3
- <!-- 顶部标题条 -->
4
- <header class="qa-header">
5
- <div class="qa-header-left">
6
-
7
- <div class="qa-header-text">
8
- <span class="qa-sub">{{ props.subTitle }}</span>
9
- </div>
10
- </div>
11
- <div class="qa-header-right">
12
- <button v-if="hasMessages" class="btn ghost" :disabled="isSending" @click="handleClear">清空对话</button>
13
- </div>
14
- </header>
15
-
16
- <!-- 预设问题区(页面上方常驻,不再区分场景;首屏全铺,提问后自动收成一行) -->
17
- <section class="preset" :class="{ 'is-collapsed': !presetExpanded }">
18
- <span class="preset-label">预设问题</span>
19
- <div class="preset-list">
20
- <button
21
- v-for="q in props.presetQuestions"
22
- :key="q"
23
- class="preset-tag"
24
- :disabled="isSending"
25
- @click="handleQuickQuestion(q)"
26
- >{{ q }}</button>
27
- </div>
28
- <button v-if="hasMessages" class="preset-toggle" @click="presetExpanded = !presetExpanded">
29
- {{ presetExpanded ? '收起' : '展开' }}
30
- <span class="preset-caret">{{ presetExpanded ? '▴' : '▾' }}</span>
31
- </button>
32
- </section>
33
-
34
- <div v-if="!hasMessages" class="chat-empty">
35
- <div class="chat-empty-avatar">AI</div>
36
- <p class="chat-empty-text">点击上方预设问题,或在下方输入你的问题</p>
37
- </div>
38
-
39
- <!-- 聊天消息流 -->
40
- <section v-else ref="messageListRef" class="message-list">
41
- <!-- 每条消息原位渲染:AI 等待首字时气泡内展示思考过程卡片(紧跟本条提问,步骤真实计时) -->
42
- <div
43
- v-for="msg in messages"
44
- :key="msg.id"
45
- class="message"
46
- :class="`message-${msg.role}`"
47
- >
48
- <div class="message-avatar">
49
- <template v-if="msg.role === 'user'">我</template>
50
- <template v-else>AI</template>
51
- </div>
52
- <div class="message-body">
53
- <!-- AI loading 且尚无内容:正在深度思考(真实等待首字,实时计时;首字到达后切流式文本) -->
54
- <div
55
- v-if="msg.role === 'assistant' && msg.loading && !msg.content"
56
- class="message-bubble process-bubble"
57
- >
58
- <div class="process-head">
59
- <span class="process-head-dot"></span>
60
- <span class="process-head-text">正在深度思考…</span>
61
- <span class="process-time">已思考 {{ thinkingElapsed.toFixed(1) }}s</span>
62
- </div>
63
- </div>
64
- <!-- 普通气泡:用户消息 / AI 流式文本 / AI 完成消息 -->
65
- <div v-else class="message-bubble">
66
- <div v-if="msg.role === 'assistant' && msg.duration != null" class="message-meta">
67
- <span class="message-meta-dot"></span>
68
- <span>已深度思考(用时 {{ msg.duration }}s)</span>
69
- </div>
70
- <div v-if="msg.role === 'user'" class="message-text">{{ msg.content }}</div>
71
- <div v-else class="message-rich" v-html="renderMarkdown(msg.content)"></div>
72
- <!-- 结构化结果:后端在 RUN_FINISHED 里可选返回,没有则不渲染空区域 -->
73
- <div
74
- v-if="msg.role === 'assistant' && msg.tableMarkdown"
75
- class="message-rich"
76
- v-html="renderMarkdown(msg.tableMarkdown)"
77
- ></div>
78
- <figure v-if="msg.role === 'assistant' && msg.chartUrl" class="message-chart">
79
- <img :src="msg.chartUrl" alt="问数结果图表" />
80
- </figure>
81
- </div>
82
- </div>
83
- </div>
84
- </section>
85
-
86
- <!-- 底部输入区 -->
87
- <footer class="qa-input-bar">
88
- <textarea
89
- ref="inputRef"
90
- v-model="inputText"
91
- class="qa-textarea"
92
- rows="2"
93
- :placeholder="props.placeholder"
94
- @keydown="handleKeydown"
95
- :disabled="isSending"
96
- />
97
- <button
98
- class="qa-send-btn"
99
- :class="{ disabled: !canSend }"
100
- :disabled="!canSend"
101
- @click="handleSend"
102
- >
103
- <span v-if="isSending">生成中…</span>
104
- <span v-else>发送</span>
105
- </button>
106
- </footer>
107
- </div>
108
- </template>
109
-
110
- <script setup>
111
- import '../styles/theme-vars.css'
112
- /**
113
- * 智能问答
114
- * 所属模块:业务应用 → 智能问答
115
- * 菜单路径:/business/ai-qa
116
- *
117
- * 内容(领导要求:不再区分场景,问题在上方、对话在中间、输入在下方):
118
- * - 预设问题区(页面上方常驻,12 条平铺;提问后自动收成一行,可手动展开)
119
- * - 聊天消息流(用户/AI 双气泡 + Markdown 渲染)
120
- * - 底部输入框(Enter 发送,Shift+Enter 换行)+ 发送按钮
121
- *
122
- * 实现要点:
123
- * - 调用后端 AskData / AG-UI 接口(POST + SSE),llmService.aiChatStream 封装解析
124
- * - 问题原文直传后端,角色/历史由后端管理;sessionId 身份头随请求携带
125
- * - 后端把整段回答一次性下发(无逐字增量),打字机由前端本地逐字揭示(见 sendToAi 内 pendingText/revealedText)
126
- * - Markdown 渲染:./utils/markdownRenderer(marked + DOMPurify),输出安全 HTML
127
- */
128
- import { ref, reactive, computed, watch, nextTick, onMounted } from 'vue'
129
- import { aiChatStream } from '../utils/llmService'
130
- import { renderMarkdown } from '../utils/markdownRenderer'
131
- import { notify } from '../utils/notify'
132
-
133
- /* ============ Props(副标题 / 输入占位文案 / 预设问题,支持外部传入) ============ */
134
-
135
- const props = defineProps({
136
- /** 顶部副标题 */
137
- subTitle: {
138
- type: String,
139
- default: 'AI 数据助手'
140
- },
141
- /** 输入框占位文案 */
142
- placeholder: {
143
- type: String,
144
- default: '输入问题,如:某个指标的环比变化'
145
- },
146
- /** AI 接口路径(空串则使用 llmService 内置默认值) */
147
- endpoint: {
148
- type: String,
149
- default: ''
150
- },
151
- /** 额外/覆盖的请求头(与默认 Content-Type / Accept / sessionId 合并,可覆盖默认值) */
152
- headers: {
153
- type: Object,
154
- default: () => ({})
155
- },
156
- /** 预设问题列表 */
157
- presetQuestions: {
158
- type: Array,
159
- default: () => [
160
- '今日各工厂水泥成本与产量',
161
- '今日各工厂熟料成本与产量'
162
- ]
163
- }
164
- })
165
-
166
- /* ============ 状态 ============ */
167
-
168
- /** 消息列表:每条消息 = { id, role, content, loading, error } */
169
- const messages = ref([])
170
- /** 预设问题区是否展开:首屏全铺(默认),开始对话后自动收成一行 */
171
- const presetExpanded = ref(true)
172
- /** 输入框文本 */
173
- const inputText = ref('')
174
- /** 是否正在请求 AI(流式返回中) */
175
- const isSending = ref(false)
176
- /** 当前请求的 AbortController(用于中止) */
177
- const currentAbort = ref(null)
178
- /** 思考计时(等待首字期间的实时秒数,100ms 刷新) */
179
- const thinkingElapsed = ref(0)
180
- let thinkingTimer = null
181
- /** 当前打字机的停止函数("清空对话"时中止揭示) */
182
- let activeTypingStop = null
183
-
184
- /** AskData 会话 id 的 localStorage 键(手册 §7/§8.3:刷新页面后可恢复会话) */
185
- const SESSION_KEY = 'askdata_session_id'
186
- /** 当前 AskData 会话 id:每轮由后端 RUN_FINISHED.result.sessionId 下发,续轮经 state.opencodeSessionId 回传 */
187
- let askSessionId = localStorage.getItem(SESSION_KEY) || null
188
- /** 清除当前会话(会话失效 / 清空对话时调用;下一次提问自然成为首轮请求) */
189
- function clearAskSession() {
190
- askSessionId = null
191
- localStorage.removeItem(SESSION_KEY)
192
- }
193
- /** 消息流容器引用 */
194
- const messageListRef = ref(null)
195
- /** 输入框引用 */
196
- const inputRef = ref(null)
197
-
198
- /* ============ 计算属性 ============ */
199
-
200
- /** 是否有消息(决定空态 / 聊天态切换) */
201
- const hasMessages = computed(() => messages.value.length > 0)
202
-
203
- const canSend = computed(() => !isSending.value && inputText.value.trim().length > 0)
204
-
205
- /** 有/无消息切换时自动折叠或全铺;只在切换瞬间生效,不覆盖用户手动展开/收起 */
206
- watch(hasMessages, (v) => {
207
- presetExpanded.value = !v
208
- })
209
-
210
- /* ============ 业务方法 ============ */
211
-
212
- /** 生成消息 id(时间戳 + 随机数) */
213
- function nextMessageId() {
214
- return `${Date.now()}-${Math.random().toString(36).slice(2, 8)}`
215
- }
216
-
217
- /**
218
- * 滚动消息流到底部
219
- * - 使用 nextTick 保证 DOM 已更新
220
- * - 强制置底以跟随流式输出
221
- */
222
- async function scrollToBottom() {
223
- await nextTick()
224
- const el = messageListRef.value
225
- if (el) el.scrollTop = el.scrollHeight
226
- }
227
-
228
- /**
229
- * 发送问题到 AI(后端 AskData / AG-UI 接口,POST + SSE)
230
- * - 追加用户消息 + AI 占位消息(等待首字期展示"正在深度思考"卡,实时计时)
231
- * - 后端整段下发 → 本地打字机逐字揭示写入 aiMsg.content(见下方 pendingText/revealedText)
232
- * - RUN_FINISHED 的 result.answer 作为权威全文收尾(防揭示异常截断)
233
- * - result 里的 sessionId / tableMarkdown / chartUrl:会话 id 存下供续轮关联,表格与图表在打字机收尾时展示
234
- * - 完成后回答上方展示"已深度思考(用时 x.xs)"(发送 → done 总耗时)
235
- * - AbortController 挂到 currentAbort,"清空对话"可中止
236
- */
237
- async function sendToAi(text) {
238
- if (!text || isSending.value) return
239
- isSending.value = true
240
- // 1. 追加用户消息
241
- const userMsg = { id: nextMessageId(), role: 'user', content: text }
242
- messages.value.push(userMsg)
243
- // 2. 追加 AI 占位消息(loading 期间展示思考卡)
244
- // 必须用 reactive() 包装:ref([]) 的 push 只追踪数组长度变化,
245
- // 不追踪普通对象内部属性变化;reactive 包装后 content 赋值才能触发响应式
246
- const aiMsg = reactive({
247
- id: nextMessageId(),
248
- role: 'assistant',
249
- content: '',
250
- loading: true,
251
- error: false,
252
- duration: null,
253
- // 结构化结果(RUN_FINISHED 可选返回);等打字机跑完再展示,避免正文未出表格先弹
254
- tableMarkdown: null,
255
- chartUrl: null
256
- })
257
- messages.value.push(aiMsg)
258
- inputText.value = ''
259
- scrollToBottom()
260
-
261
- // 3. 开启等待期秒表(thinkingElapsed 实时刷新,首字到达卡片消失即停表意义不大,统一到流结束停)
262
- const t0 = performance.now()
263
- thinkingElapsed.value = 0
264
- if (thinkingTimer) clearInterval(thinkingTimer)
265
- thinkingTimer = setInterval(() => {
266
- thinkingElapsed.value = (performance.now() - t0) / 1000
267
- }, 100)
268
-
269
- const controller = new AbortController()
270
- currentAbort.value = controller
271
-
272
- // ---- 本地打字机 ----
273
- // 后端 AG-UI 把整段回答放在单个 TEXT_MESSAGE_CONTENT 里一次性下发(无逐字增量),
274
- // 所以收到文本后由前端逐字揭示,保住打字机观感;RUN_FINISHED 的 result.answer 作为收尾基准。
275
- let pendingText = '' // 已收到、未揭示
276
- let revealedText = '' // 已揭示
277
- let finalText = '' // 后端给的权威全文
278
- let finalTable = null // 后端给的可选 Markdown 表格
279
- let finalChart = null // 后端给的可选图表 URL
280
- let streamDone = false
281
- let typingTimer = null
282
- const stopTyping = () => {
283
- if (typingTimer) { clearInterval(typingTimer); typingTimer = null }
284
- if (activeTypingStop === stopTyping) activeTypingStop = null
285
- }
286
- const tickTyping = () => {
287
- if (pendingText) {
288
- // 自适应步长:长文约 60 帧揭示完,短句至少 2 字/帧,30ms 一帧
289
- const step = Math.max(2, Math.ceil(pendingText.length / 60))
290
- revealedText += pendingText.slice(0, step)
291
- pendingText = pendingText.slice(step)
292
- aiMsg.content = revealedText
293
- scrollToBottom()
294
- return
295
- }
296
- if (streamDone) {
297
- aiMsg.content = finalText || revealedText
298
- // 结构化结果在 RUN_FINISHED 才到,晚于打字机起点:正文揭示完再一并展示
299
- aiMsg.tableMarkdown = finalTable
300
- aiMsg.chartUrl = finalChart
301
- stopTyping()
302
- }
303
- }
304
- const startTyping = () => {
305
- if (!typingTimer) typingTimer = setInterval(tickTyping, 30)
306
- }
307
- activeTypingStop = stopTyping
308
-
309
- try {
310
- // 问题原文直传,语义理解/查数/组织语言都在后端完成
311
- await aiChatStream(text, {
312
- sessionId: askSessionId,
313
- endpoint: props.endpoint,
314
- headers: props.headers,
315
- signal: controller.signal,
316
- onDelta: (delta) => {
317
- pendingText += delta
318
- startTyping()
319
- },
320
- onDone: (result) => {
321
- // 完整回答作为收尾基准(揭示动画跑完后再落定)
322
- if (result.answer) finalText = result.answer
323
- // 后端常把同一张表既写进 answer 正文、又单独给 tableMarkdown;
324
- // 正文已包含时不再重复渲染,否则页面会出现两张一模一样的表
325
- const strip = (s) => (s || '').replace(/\s+/g, '')
326
- const table = result.tableMarkdown
327
- finalTable = table && !strip(finalText).includes(strip(table)) ? table : null
328
- finalChart = result.chartUrl
329
- // 保存会话 id,供下一轮经 state.opencodeSessionId 关联上下文
330
- if (result.sessionId) {
331
- askSessionId = result.sessionId
332
- localStorage.setItem(SESSION_KEY, result.sessionId)
333
- }
334
- }
335
- })
336
- aiMsg.duration = +((performance.now() - t0) / 1000).toFixed(1)
337
- } catch (e) {
338
- // 失败立即整段展示,不做打字机
339
- streamDone = true
340
- stopTyping()
341
- // 会话失效:清掉本地会话,用户下一次提问自然回到首轮(手册 §5.4/§8.3)
342
- // 有意不自动重发——手册 §10.2 要求不要无条件重新提交同一问题
343
- const expired = e && e.code === 'OPENCODE_SESSION_INVALID'
344
- if (expired) clearAskSession()
345
- const msg = expired
346
- ? '会话已过期,请重新提问'
347
- : (e && e.message ? e.message : '请求失败,请稍后重试')
348
- aiMsg.content = `[错误] ${msg}`
349
- aiMsg.error = true
350
- notify(msg, 'error', 1400)
351
- } finally {
352
- if (thinkingTimer) { clearInterval(thinkingTimer); thinkingTimer = null }
353
- streamDone = true
354
- if (controller.signal.aborted) {
355
- stopTyping() // 已中止:不再补揭示
356
- } else if (aiMsg.error) {
357
- // 错误已整段展示
358
- } else if (pendingText || revealedText) {
359
- startTyping() // 让残余文本继续揭示,tickTyping 自会收尾
360
- } else {
361
- aiMsg.content = finalText || '(暂无回答,请稍后重试)'
362
- aiMsg.tableMarkdown = finalTable
363
- aiMsg.chartUrl = finalChart
364
- }
365
- aiMsg.loading = false
366
- if (currentAbort.value === controller) currentAbort.value = null
367
- isSending.value = false
368
- scrollToBottom()
369
- }
370
- }
371
- /**
372
- * 点击"发送"按钮
373
- */
374
- function handleSend() {
375
- const text = inputText.value.trim()
376
- if (!text || isSending.value) return
377
- sendToAi(text)
378
- }
379
-
380
- /**
381
- * 点击快捷问题:直接发送(不带前缀)
382
- */
383
- function handleQuickQuestion(question) {
384
- if (isSending.value) return
385
- sendToAi(question)
386
- }
387
-
388
- /**
389
- * 清空对话
390
- * - 中止正在进行的请求
391
- * - 重置消息列表
392
- * - 一并清掉 AskData 会话,否则后端仍记得旧话题、与空白的页面不一致
393
- */
394
- function handleClear() {
395
- if (currentAbort.value) currentAbort.value.abort()
396
- currentAbort.value = null
397
- if (activeTypingStop) activeTypingStop()
398
- isSending.value = false
399
- messages.value = []
400
- clearAskSession()
401
- }
402
-
403
- /**
404
- * 键盘事件:Enter 发送,Shift+Enter 换行
405
- */
406
- function handleKeydown(e) {
407
- if (e.key === 'Enter' && !e.shiftKey) {
408
- e.preventDefault()
409
- handleSend()
410
- }
411
- }
412
-
413
- /* ============ 生命周期 ============ */
414
-
415
- onMounted(() => {
416
- // 默认聚焦输入框
417
- if (inputRef.value) inputRef.value.focus()
418
- })
419
- </script>
420
-
421
- <style scoped>
422
- .quick-qa {
423
- display: flex;
424
- flex-direction: column;
425
- height: 100%;
426
- min-height: 0;
427
- }
428
-
429
- /* ===== 顶部 header ===== */
430
- .qa-header {
431
- display: flex;
432
- align-items: center;
433
- justify-content: space-between;
434
- padding: 0 0 12px;
435
- margin-bottom: 16px;
436
- }
437
- .qa-header-left { display: flex; align-items: center; gap: 10px; min-width: 0; }
438
- .qa-logo {
439
- width: 34px;
440
- height: 34px;
441
- border-radius: var(--radius-circle);
442
- background: linear-gradient(135deg, var(--brand), #8f7fff);
443
- color: #ffffff;
444
- box-shadow: 0 0 12px color-mix(in srgb, var(--brand) 40%, transparent);
445
- display: flex;
446
- align-items: center;
447
- justify-content: center;
448
- font-size: 13px;
449
- font-weight: 600;
450
- flex-shrink: 0;
451
- }
452
- .qa-header-text { display: flex; flex-direction: column; gap: 1px; min-width: 0; }
453
- .qa-title { font-size: 15px; font-weight: 600; color: var(--text); line-height: 1.4; }
454
- .qa-sub { font-size: 12px; color: var(--muted); }
455
- .qa-header-right { display: flex; align-items: center; gap: 10px; flex-shrink: 0; }
456
-
457
- /* ===== 预设问题区(页面上方常驻,不区分场景) ===== */
458
- .preset {
459
- display: flex;
460
- flex-wrap: wrap;
461
- align-items: center;
462
- gap: 8px;
463
- padding: 10px 12px;
464
- border: 1px solid var(--line);
465
- border-radius: var(--radius-lg);
466
- background: var(--overlay-card);
467
- margin-bottom: 12px;
468
- }
469
- .preset-label {
470
- flex: 1 1 auto;
471
- font-size: 12px;
472
- font-weight: 600;
473
- color: var(--muted);
474
- }
475
- .preset-label::before {
476
- content: '';
477
- display: inline-block;
478
- width: 6px;
479
- height: 6px;
480
- border-radius: var(--radius-circle);
481
- background: linear-gradient(135deg, var(--brand), #8f7fff);
482
- margin-right: 6px;
483
- vertical-align: 1px;
484
- }
485
- /* 问题列表:展开=自动换行全铺;折叠=一行横向滚动(见 .is-collapsed) */
486
- .preset-list {
487
- order: 3;
488
- flex: 1 1 100%;
489
- display: flex;
490
- flex-wrap: wrap;
491
- gap: 8px;
492
- }
493
- .preset-tag {
494
- border: 1px solid var(--line);
495
- background: var(--overlay-card-2);
496
- color: var(--text);
497
- padding: 5px 12px;
498
- border-radius: var(--radius-pill);
499
- cursor: pointer;
500
- font-size: 12px;
501
- line-height: 1.5;
502
- white-space: nowrap;
503
- font-family: inherit;
504
- transition: 0.15s ease;
505
- }
506
- .preset-tag:hover:not(:disabled) {
507
- border-color: var(--brand);
508
- color: var(--brand);
509
- background: var(--overlay-hover);
510
- transform: translateY(-1px);
511
- }
512
- .preset-tag:disabled { opacity: 0.5; cursor: not-allowed; }
513
- .preset-toggle {
514
- flex-shrink: 0;
515
- border: 1px solid var(--line);
516
- background: transparent;
517
- color: var(--muted);
518
- padding: 2px 10px;
519
- border-radius: var(--radius-pill);
520
- cursor: pointer;
521
- font-size: 12px;
522
- font-family: inherit;
523
- transition: 0.15s ease;
524
- }
525
- .preset-toggle:hover {
526
- border-color: var(--brand);
527
- color: var(--brand);
528
- background: var(--overlay-hover);
529
- }
530
- .preset-caret { font-size: 10px; margin-left: 2px; }
531
-
532
- /* 折叠态:预设问题收成一行,标签在左、展开按钮在最右 */
533
- .preset.is-collapsed { flex-wrap: nowrap; }
534
- .preset.is-collapsed .preset-label { flex: 0 0 auto; }
535
- .preset.is-collapsed .preset-list {
536
- order: 1;
537
- flex: 1 1 auto;
538
- min-width: 0;
539
- flex-wrap: nowrap;
540
- overflow-x: auto;
541
- padding-bottom: 2px;
542
- }
543
- .preset.is-collapsed .preset-toggle { order: 2; }
544
-
545
- /* ===== 对话区空态 ===== */
546
- .chat-empty {
547
- flex: 1;
548
- min-height: 0;
549
- display: flex;
550
- flex-direction: column;
551
- align-items: center;
552
- justify-content: center;
553
- gap: 12px;
554
- text-align: center;
555
- padding: 24px 0;
556
- }
557
- .chat-empty-avatar {
558
- width: 52px;
559
- height: 52px;
560
- border-radius: var(--radius-circle);
561
- background: linear-gradient(135deg, var(--brand), #8f7fff);
562
- color: #ffffff;
563
- display: flex;
564
- align-items: center;
565
- justify-content: center;
566
- font-size: 19px;
567
- font-weight: 600;
568
- box-shadow: 0 0 18px color-mix(in srgb, var(--brand) 30%, transparent);
569
- }
570
- .chat-empty-text {
571
- margin: 0;
572
- font-size: 13px;
573
- color: var(--muted);
574
- }
575
-
576
- /* ===== 消息流 ===== */
577
- .message-list {
578
- flex: 1;
579
- min-height: 0;
580
- overflow-y: auto;
581
- display: flex;
582
- flex-direction: column;
583
- gap: 16px;
584
- padding: 4px 0 12px;
585
- }
586
- .message { display: flex; gap: 10px; align-items: flex-start; animation: msg-in 0.25s ease; }
587
- @keyframes msg-in {
588
- from { opacity: 0; transform: translateY(8px); }
589
- to { opacity: 1; transform: none; }
590
- }
591
- .message-user { flex-direction: row-reverse; }
592
- .message-avatar {
593
- width: 30px;
594
- height: 30px;
595
- border-radius: var(--radius-circle);
596
- display: flex;
597
- align-items: center;
598
- justify-content: center;
599
- font-weight: 600;
600
- font-size: 11px;
601
- flex-shrink: 0;
602
- }
603
- .message-assistant .message-avatar {
604
- background: linear-gradient(135deg, var(--brand), #8f7fff);
605
- color: #ffffff;
606
- box-shadow: 0 0 10px color-mix(in srgb, var(--brand) 35%, transparent);
607
- }
608
- .message-user .message-avatar {
609
- background: var(--overlay-card-2);
610
- color: var(--text);
611
- }
612
- .message-body { flex: 1; min-width: 0; display: flex; }
613
- .message-user .message-body { justify-content: flex-end; }
614
- .message-bubble {
615
- max-width: 78%;
616
- padding: 10px 14px;
617
- border-radius: 10px;
618
- border: 1px solid var(--line);
619
- background: var(--overlay-card);
620
- font-size: 13px;
621
- line-height: 1.65;
622
- word-break: break-word;
623
- }
624
- .message-assistant .message-bubble { border-top-left-radius: 2px; }
625
- .message-user .message-bubble {
626
- background: var(--brand);
627
- border-color: var(--brand);
628
- color: var(--btn-active-text, #ffffff);
629
- border-top-right-radius: 2px;
630
- }
631
- .message-text { white-space: pre-wrap; }
632
- .message-rich { font-size: 13px; line-height: 1.65; color: var(--text); }
633
-
634
- /* ===== Markdown 渲染内容样式(marked 输出) ===== */
635
- /* 用 :deep() 穿透 scoped,让 v-html 插入的子元素能继承到样式 */
636
- .message-rich :deep(h1),
637
- .message-rich :deep(h2),
638
- .message-rich :deep(h3),
639
- .message-rich :deep(h4) {
640
- font-weight: 600;
641
- color: var(--text);
642
- margin: 10px 0 6px;
643
- line-height: 1.4;
644
- }
645
- .message-rich :deep(h1) { font-size: 16px; }
646
- .message-rich :deep(h2) { font-size: 15px; }
647
- .message-rich :deep(h3) { font-size: 14px; }
648
- .message-rich :deep(h4) { font-size: 13px; }
649
- .message-rich :deep(h1):first-child,
650
- .message-rich :deep(h2):first-child,
651
- .message-rich :deep(h3):first-child,
652
- .message-rich :deep(h4):first-child { margin-top: 0; }
653
-
654
- .message-rich :deep(p) { margin: 4px 0; }
655
- .message-rich :deep(p):first-child { margin-top: 0; }
656
- .message-rich :deep(p):last-child { margin-bottom: 0; }
657
-
658
- .message-rich :deep(ul),
659
- .message-rich :deep(ol) { margin: 6px 0; padding-left: 22px; }
660
- .message-rich :deep(li) { margin: 2px 0; }
661
- .message-rich :deep(li > p) { margin: 0; }
662
-
663
- .message-rich :deep(strong) { color: var(--text); font-weight: 600; }
664
- .message-rich :deep(em) { color: var(--text); font-style: italic; }
665
-
666
- .message-rich :deep(a) {
667
- color: var(--brand);
668
- text-decoration: none;
669
- border-bottom: 1px dashed var(--brand);
670
- transition: color 0.15s ease;
671
- }
672
- .message-rich :deep(a:hover) { color: var(--brand-2); border-bottom-color: var(--brand-2); }
673
-
674
- .message-rich :deep(blockquote) {
675
- margin: 6px 0;
676
- padding: 6px 12px;
677
- border-left: 3px solid var(--brand);
678
- background: var(--overlay-card-2);
679
- color: var(--muted);
680
- border-radius: var(--radius-none) var(--radius-sm) var(--radius-sm) var(--radius-none);
681
- }
682
-
683
- .message-rich :deep(hr) {
684
- border: none;
685
- border-top: 1px solid var(--line);
686
- margin: 10px 0;
687
- }
688
-
689
- /* 行内 code(marked 输出 <code>,代码块也是 <pre><code>,靠父元素区分) */
690
- .message-rich :deep(code) {
691
- font-family: var(--mono);
692
- background: var(--overlay-mask);
693
- padding: 1px 6px;
694
- border-radius: var(--radius-sm);
695
- font-size: 12px;
696
- color: var(--chip-info-text);
697
- }
698
-
699
- /* 代码块(pre 内的 code 不加行内 padding) */
700
- .message-rich :deep(pre) {
701
- margin: 6px 0;
702
- padding: 8px 12px;
703
- background: var(--overlay-mask);
704
- border: 1px solid var(--line);
705
- border-radius: var(--radius-md);
706
- overflow-x: auto;
707
- font-family: var(--mono);
708
- font-size: 12px;
709
- line-height: 1.5;
710
- }
711
- .message-rich :deep(pre code) {
712
- background: transparent;
713
- padding: 0;
714
- color: var(--text);
715
- font-size: inherit;
716
- }
717
-
718
- /* 表格(marked GFM 输出 <table>) */
719
- .message-rich :deep(table) {
720
- width: 100%;
721
- border-collapse: separate;
722
- border-spacing: 0;
723
- font-size: 13px;
724
- margin: 8px 0;
725
- border: 1px solid var(--line);
726
- border-radius: var(--radius-md);
727
- overflow: hidden;
728
- background: var(--overlay-card-2);
729
- }
730
- .message-rich :deep(thead) {
731
- background: var(--panel-strong);
732
- }
733
- .message-rich :deep(th),
734
- .message-rich :deep(td) {
735
- padding: 8px 10px;
736
- border-bottom: 1px solid var(--line);
737
- border-right: 1px solid var(--line);
738
- text-align: left;
739
- color: var(--text);
740
- }
741
- .message-rich :deep(th) { font-weight: 600; }
742
- .message-rich :deep(th):last-child,
743
- .message-rich :deep(td):last-child { border-right: none; }
744
- .message-rich :deep(tbody tr):last-child td { border-bottom: none; }
745
- .message-rich :deep(tbody tr:nth-child(even) td) { background: var(--overlay-hover); }
746
- .message-rich :deep(tbody tr:hover td) { background: var(--hover-row-bg); }
747
-
748
- /* 图表(后端 RUN_FINISHED.result.chartUrl,直接作为图像地址使用,不自行拼接) */
749
- .message-chart { margin: 8px 0 2px; }
750
- .message-chart img {
751
- display: block;
752
- max-width: 100%;
753
- height: auto;
754
- border: 1px solid var(--line);
755
- border-radius: var(--radius-md);
756
- background: var(--overlay-card-2);
757
- }
758
-
759
- /* ===== AI 思考过程卡片 ===== */
760
- .process-bubble { min-width: 240px; }
761
- .process-head {
762
- display: flex;
763
- align-items: center;
764
- gap: 8px;
765
- margin-bottom: 0;
766
- }
767
- .process-head-dot {
768
- width: 8px;
769
- height: 8px;
770
- border-radius: var(--radius-circle);
771
- background: linear-gradient(135deg, var(--brand), #8f7fff);
772
- animation: head-pulse 1.4s ease-in-out infinite;
773
- }
774
- .process-head-text { font-size: 13px; font-weight: 600; color: var(--text); }
775
- .process-time {
776
- margin-left: auto;
777
- font-size: 11px;
778
- color: var(--muted);
779
- font-variant-numeric: tabular-nums;
780
- }
781
-
782
- @keyframes head-pulse {
783
- 0%, 100% { opacity: 1; transform: scale(1); }
784
- 50% { opacity: 0.45; transform: scale(0.85); }
785
- }
786
-
787
- /* ===== AI 回答底部信息(耗时等) ===== */
788
- .message-meta {
789
- display: flex;
790
- align-items: center;
791
- gap: 6px;
792
- margin: 0 0 8px;
793
- padding: 0 0 8px;
794
- border-bottom: 1px dashed var(--line);
795
- font-size: 11px;
796
- color: var(--muted);
797
- }
798
- .message-meta-dot {
799
- width: 5px;
800
- height: 5px;
801
- border-radius: var(--radius-circle);
802
- background: linear-gradient(135deg, var(--brand), #8f7fff);
803
- flex-shrink: 0;
804
- }
805
-
806
- /* ===== 底部输入区 ===== */
807
- .qa-input-bar {
808
- display: flex;
809
- align-items: stretch;
810
- gap: 10px;
811
- margin-top: 12px;
812
- padding: 10px 12px;
813
- border: 1px solid var(--line);
814
- border-radius: var(--radius-lg);
815
- background: var(--overlay-card);
816
- transition: border-color 0.15s ease;
817
- }
818
- .qa-input-bar:focus-within {
819
- border-color: var(--brand);
820
- box-shadow: 0 0 0 3px color-mix(in srgb, var(--brand) 12%, transparent),
821
- 0 4px 18px color-mix(in srgb, var(--brand) 14%, transparent);
822
- }
823
- .qa-textarea {
824
- flex: 1;
825
- min-width: 0;
826
- resize: none;
827
- border: none;
828
- background: transparent;
829
- color: var(--text);
830
- padding: 6px 4px;
831
- font-family: inherit;
832
- font-size: 13px;
833
- line-height: 1.5;
834
- outline: none;
835
- }
836
- .qa-textarea:disabled { opacity: 0.6; cursor: not-allowed; }
837
- .qa-textarea::placeholder { color: var(--muted); }
838
- .qa-send-btn {
839
- flex-shrink: 0;
840
- border: none;
841
- background: linear-gradient(135deg, var(--brand), var(--brand2));
842
- color: var(--btn-active-text, #ffffff);
843
- padding: 0 22px;
844
- border-radius: var(--radius-lg);
845
- cursor: pointer;
846
- font-size: 13px;
847
- font-weight: 500;
848
- transition: 0.18s ease;
849
- font-family: inherit;
850
- box-shadow: 0 2px 10px color-mix(in srgb, var(--brand) 25%, transparent);
851
- }
852
- .qa-send-btn:hover:not(:disabled) {
853
- transform: translateY(-1px);
854
- box-shadow: 0 4px 16px color-mix(in srgb, var(--brand) 40%, transparent);
855
- }
856
- .qa-send-btn:disabled,
857
- .qa-send-btn.disabled { opacity: 0.45; cursor: not-allowed; }
858
-
859
- /* ===== 通用按钮 ===== */
860
- .btn {
861
- border: 1px solid var(--line);
862
- background: var(--overlay-card-2);
863
- color: var(--text);
864
- padding: 4px 12px;
865
- border-radius: var(--radius-lg);
866
- cursor: pointer;
867
- transition: 0.15s ease;
868
- font-size: 12px;
869
- font-family: inherit;
870
- }
871
- .btn:hover:not(:disabled) { background: var(--overlay-hover); }
872
- .btn:disabled { opacity: 0.45; cursor: not-allowed; }
873
- .btn.ghost { background: transparent; }
874
-
875
- /* ===== 滚动条 ===== */
876
- .message-list::-webkit-scrollbar,
877
- .preset-list::-webkit-scrollbar {
878
- width: 6px;
879
- height: 6px;
880
- }
881
- .message-list::-webkit-scrollbar-thumb,
882
- .preset-list::-webkit-scrollbar-thumb {
883
- background: var(--line-strong);
884
- border-radius: var(--radius-pill);
885
- }
886
- .message-list::-webkit-scrollbar-track,
887
- .preset-list::-webkit-scrollbar-track {
888
- background: transparent;
889
- }
890
- </style>
1
+ <template>
2
+ <div class="quick-qa">
3
+ <!-- 顶部标题条 -->
4
+ <header class="qa-header">
5
+ <div class="qa-header-left">
6
+
7
+ <div class="qa-header-text">
8
+ <span class="qa-sub">{{ props.subTitle }}</span>
9
+ </div>
10
+ </div>
11
+ <div class="qa-header-right">
12
+ <button v-if="hasMessages" class="btn ghost" :disabled="isSending" @click="handleClear">清空对话</button>
13
+ </div>
14
+ </header>
15
+
16
+ <!-- 预设问题区(页面上方常驻,不再区分场景;首屏全铺,提问后自动收成一行) -->
17
+ <section class="preset" :class="{ 'is-collapsed': !presetExpanded }">
18
+ <span class="preset-label">预设问题</span>
19
+ <div class="preset-list">
20
+ <button
21
+ v-for="q in props.presetQuestions"
22
+ :key="q"
23
+ class="preset-tag"
24
+ :disabled="isSending"
25
+ @click="handleQuickQuestion(q)"
26
+ >{{ q }}</button>
27
+ </div>
28
+ <button v-if="hasMessages" class="preset-toggle" @click="presetExpanded = !presetExpanded">
29
+ {{ presetExpanded ? '收起' : '展开' }}
30
+ <span class="preset-caret">{{ presetExpanded ? '▴' : '▾' }}</span>
31
+ </button>
32
+ </section>
33
+
34
+ <div v-if="!hasMessages" class="chat-empty">
35
+ <div class="chat-empty-avatar">AI</div>
36
+ <p class="chat-empty-text">点击上方预设问题,或在下方输入你的问题</p>
37
+ </div>
38
+
39
+ <!-- 聊天消息流 -->
40
+ <section v-else ref="messageListRef" class="message-list">
41
+ <!-- 每条消息原位渲染:AI 等待首字时气泡内展示思考过程卡片(紧跟本条提问,步骤真实计时) -->
42
+ <div
43
+ v-for="msg in messages"
44
+ :key="msg.id"
45
+ class="message"
46
+ :class="`message-${msg.role}`"
47
+ >
48
+ <div class="message-avatar">
49
+ <template v-if="msg.role === 'user'">我</template>
50
+ <template v-else>AI</template>
51
+ </div>
52
+ <div class="message-body">
53
+ <!-- AI loading 且尚无内容:正在深度思考(真实等待首字,实时计时;首字到达后切流式文本) -->
54
+ <div
55
+ v-if="msg.role === 'assistant' && msg.loading && !msg.content"
56
+ class="message-bubble process-bubble"
57
+ >
58
+ <div class="process-head">
59
+ <span class="process-head-dot"></span>
60
+ <span class="process-head-text">正在深度思考…</span>
61
+ <span class="process-time">已思考 {{ thinkingElapsed.toFixed(1) }}s</span>
62
+ </div>
63
+ </div>
64
+ <!-- 普通气泡:用户消息 / AI 流式文本 / AI 完成消息 -->
65
+ <div v-else class="message-bubble">
66
+ <div v-if="msg.role === 'assistant' && msg.duration != null" class="message-meta">
67
+ <span class="message-meta-dot"></span>
68
+ <span>已深度思考(用时 {{ msg.duration }}s)</span>
69
+ </div>
70
+ <div v-if="msg.role === 'user'" class="message-text">{{ msg.content }}</div>
71
+ <div v-else class="message-rich" v-html="renderMarkdown(msg.content)"></div>
72
+ <!-- 结构化结果:后端在 RUN_FINISHED 里可选返回,没有则不渲染空区域 -->
73
+ <div
74
+ v-if="msg.role === 'assistant' && msg.tableMarkdown"
75
+ class="message-rich"
76
+ v-html="renderMarkdown(msg.tableMarkdown)"
77
+ ></div>
78
+ <figure v-if="msg.role === 'assistant' && msg.chartUrl" class="message-chart">
79
+ <img :src="msg.chartUrl" alt="问数结果图表" />
80
+ </figure>
81
+ </div>
82
+ </div>
83
+ </div>
84
+ </section>
85
+
86
+ <!-- 底部输入区 -->
87
+ <footer class="qa-input-bar">
88
+ <textarea
89
+ ref="inputRef"
90
+ v-model="inputText"
91
+ class="qa-textarea"
92
+ rows="2"
93
+ :placeholder="props.placeholder"
94
+ @keydown="handleKeydown"
95
+ :disabled="isSending"
96
+ />
97
+ <button
98
+ class="qa-send-btn"
99
+ :class="{ disabled: !canSend }"
100
+ :disabled="!canSend"
101
+ @click="handleSend"
102
+ >
103
+ <span v-if="isSending">生成中…</span>
104
+ <span v-else>发送</span>
105
+ </button>
106
+ </footer>
107
+ </div>
108
+ </template>
109
+
110
+ <script setup>
111
+ import '../styles/theme-vars.css'
112
+ /**
113
+ * 智能问答
114
+ * 所属模块:业务应用 → 智能问答
115
+ * 菜单路径:/business/ai-qa
116
+ *
117
+ * 内容(领导要求:不再区分场景,问题在上方、对话在中间、输入在下方):
118
+ * - 预设问题区(页面上方常驻,12 条平铺;提问后自动收成一行,可手动展开)
119
+ * - 聊天消息流(用户/AI 双气泡 + Markdown 渲染)
120
+ * - 底部输入框(Enter 发送,Shift+Enter 换行)+ 发送按钮
121
+ *
122
+ * 实现要点:
123
+ * - 调用后端 AskData / AG-UI 接口(POST + SSE),llmService.aiChatStream 封装解析
124
+ * - 问题原文直传后端,角色/历史由后端管理;sessionId 身份头随请求携带
125
+ * - 后端把整段回答一次性下发(无逐字增量),打字机由前端本地逐字揭示(见 sendToAi 内 pendingText/revealedText)
126
+ * - Markdown 渲染:./utils/markdownRenderer(marked + DOMPurify),输出安全 HTML
127
+ */
128
+ import { ref, reactive, computed, watch, nextTick, onMounted } from 'vue'
129
+ import { aiChatStream } from '../utils/llmService'
130
+ import { renderMarkdown } from '../utils/markdownRenderer'
131
+ import { notify } from '../utils/notify'
132
+
133
+ /* ============ Props(副标题 / 输入占位文案 / 预设问题,支持外部传入) ============ */
134
+
135
+ const props = defineProps({
136
+ /** 顶部副标题 */
137
+ subTitle: {
138
+ type: String,
139
+ default: 'AI 数据助手'
140
+ },
141
+ /** 输入框占位文案 */
142
+ placeholder: {
143
+ type: String,
144
+ default: '输入问题,如:某个指标的环比变化'
145
+ },
146
+ /** AI 接口路径(空串则使用 llmService 内置默认值) */
147
+ endpoint: {
148
+ type: String,
149
+ default: ''
150
+ },
151
+ /** 额外/覆盖的请求头(与默认 Content-Type / Accept / sessionId 合并,可覆盖默认值) */
152
+ headers: {
153
+ type: Object,
154
+ default: () => ({})
155
+ },
156
+ /** 预设问题列表 */
157
+ presetQuestions: {
158
+ type: Array,
159
+ default: () => [
160
+ '今日各工厂水泥成本与产量',
161
+ '今日各工厂熟料成本与产量'
162
+ ]
163
+ }
164
+ })
165
+
166
+ /* ============ 状态 ============ */
167
+
168
+ /** 消息列表:每条消息 = { id, role, content, loading, error } */
169
+ const messages = ref([])
170
+ /** 预设问题区是否展开:首屏全铺(默认),开始对话后自动收成一行 */
171
+ const presetExpanded = ref(true)
172
+ /** 输入框文本 */
173
+ const inputText = ref('')
174
+ /** 是否正在请求 AI(流式返回中) */
175
+ const isSending = ref(false)
176
+ /** 当前请求的 AbortController(用于中止) */
177
+ const currentAbort = ref(null)
178
+ /** 思考计时(等待首字期间的实时秒数,100ms 刷新) */
179
+ const thinkingElapsed = ref(0)
180
+ let thinkingTimer = null
181
+ /** 当前打字机的停止函数("清空对话"时中止揭示) */
182
+ let activeTypingStop = null
183
+
184
+ /** AskData 会话 id 的 localStorage 键(手册 §7/§8.3:刷新页面后可恢复会话) */
185
+ const SESSION_KEY = 'askdata_session_id'
186
+ /** 当前 AskData 会话 id:每轮由后端 RUN_FINISHED.result.sessionId 下发,续轮经 state.opencodeSessionId 回传 */
187
+ let askSessionId = localStorage.getItem(SESSION_KEY) || null
188
+ /** 清除当前会话(会话失效 / 清空对话时调用;下一次提问自然成为首轮请求) */
189
+ function clearAskSession() {
190
+ askSessionId = null
191
+ localStorage.removeItem(SESSION_KEY)
192
+ }
193
+ /** 消息流容器引用 */
194
+ const messageListRef = ref(null)
195
+ /** 输入框引用 */
196
+ const inputRef = ref(null)
197
+
198
+ /* ============ 计算属性 ============ */
199
+
200
+ /** 是否有消息(决定空态 / 聊天态切换) */
201
+ const hasMessages = computed(() => messages.value.length > 0)
202
+
203
+ const canSend = computed(() => !isSending.value && inputText.value.trim().length > 0)
204
+
205
+ /** 有/无消息切换时自动折叠或全铺;只在切换瞬间生效,不覆盖用户手动展开/收起 */
206
+ watch(hasMessages, (v) => {
207
+ presetExpanded.value = !v
208
+ })
209
+
210
+ /* ============ 业务方法 ============ */
211
+
212
+ /** 生成消息 id(时间戳 + 随机数) */
213
+ function nextMessageId() {
214
+ return `${Date.now()}-${Math.random().toString(36).slice(2, 8)}`
215
+ }
216
+
217
+ /**
218
+ * 滚动消息流到底部
219
+ * - 使用 nextTick 保证 DOM 已更新
220
+ * - 强制置底以跟随流式输出
221
+ */
222
+ async function scrollToBottom() {
223
+ await nextTick()
224
+ const el = messageListRef.value
225
+ if (el) el.scrollTop = el.scrollHeight
226
+ }
227
+
228
+ /**
229
+ * 发送问题到 AI(后端 AskData / AG-UI 接口,POST + SSE)
230
+ * - 追加用户消息 + AI 占位消息(等待首字期展示"正在深度思考"卡,实时计时)
231
+ * - 后端整段下发 → 本地打字机逐字揭示写入 aiMsg.content(见下方 pendingText/revealedText)
232
+ * - RUN_FINISHED 的 result.answer 作为权威全文收尾(防揭示异常截断)
233
+ * - result 里的 sessionId / tableMarkdown / chartUrl:会话 id 存下供续轮关联,表格与图表在打字机收尾时展示
234
+ * - 完成后回答上方展示"已深度思考(用时 x.xs)"(发送 → done 总耗时)
235
+ * - AbortController 挂到 currentAbort,"清空对话"可中止
236
+ */
237
+ async function sendToAi(text) {
238
+ if (!text || isSending.value) return
239
+ isSending.value = true
240
+ // 1. 追加用户消息
241
+ const userMsg = { id: nextMessageId(), role: 'user', content: text }
242
+ messages.value.push(userMsg)
243
+ // 2. 追加 AI 占位消息(loading 期间展示思考卡)
244
+ // 必须用 reactive() 包装:ref([]) 的 push 只追踪数组长度变化,
245
+ // 不追踪普通对象内部属性变化;reactive 包装后 content 赋值才能触发响应式
246
+ const aiMsg = reactive({
247
+ id: nextMessageId(),
248
+ role: 'assistant',
249
+ content: '',
250
+ loading: true,
251
+ error: false,
252
+ duration: null,
253
+ // 结构化结果(RUN_FINISHED 可选返回);等打字机跑完再展示,避免正文未出表格先弹
254
+ tableMarkdown: null,
255
+ chartUrl: null
256
+ })
257
+ messages.value.push(aiMsg)
258
+ inputText.value = ''
259
+ scrollToBottom()
260
+
261
+ // 3. 开启等待期秒表(thinkingElapsed 实时刷新,首字到达卡片消失即停表意义不大,统一到流结束停)
262
+ const t0 = performance.now()
263
+ thinkingElapsed.value = 0
264
+ if (thinkingTimer) clearInterval(thinkingTimer)
265
+ thinkingTimer = setInterval(() => {
266
+ thinkingElapsed.value = (performance.now() - t0) / 1000
267
+ }, 100)
268
+
269
+ const controller = new AbortController()
270
+ currentAbort.value = controller
271
+
272
+ // ---- 本地打字机 ----
273
+ // 后端 AG-UI 把整段回答放在单个 TEXT_MESSAGE_CONTENT 里一次性下发(无逐字增量),
274
+ // 所以收到文本后由前端逐字揭示,保住打字机观感;RUN_FINISHED 的 result.answer 作为收尾基准。
275
+ let pendingText = '' // 已收到、未揭示
276
+ let revealedText = '' // 已揭示
277
+ let finalText = '' // 后端给的权威全文
278
+ let finalTable = null // 后端给的可选 Markdown 表格
279
+ let finalChart = null // 后端给的可选图表 URL
280
+ let streamDone = false
281
+ let typingTimer = null
282
+ const stopTyping = () => {
283
+ if (typingTimer) { clearInterval(typingTimer); typingTimer = null }
284
+ if (activeTypingStop === stopTyping) activeTypingStop = null
285
+ }
286
+ const tickTyping = () => {
287
+ if (pendingText) {
288
+ // 自适应步长:长文约 60 帧揭示完,短句至少 2 字/帧,30ms 一帧
289
+ const step = Math.max(2, Math.ceil(pendingText.length / 60))
290
+ revealedText += pendingText.slice(0, step)
291
+ pendingText = pendingText.slice(step)
292
+ aiMsg.content = revealedText
293
+ scrollToBottom()
294
+ return
295
+ }
296
+ if (streamDone) {
297
+ aiMsg.content = finalText || revealedText
298
+ // 结构化结果在 RUN_FINISHED 才到,晚于打字机起点:正文揭示完再一并展示
299
+ aiMsg.tableMarkdown = finalTable
300
+ aiMsg.chartUrl = finalChart
301
+ stopTyping()
302
+ }
303
+ }
304
+ const startTyping = () => {
305
+ if (!typingTimer) typingTimer = setInterval(tickTyping, 30)
306
+ }
307
+ activeTypingStop = stopTyping
308
+
309
+ try {
310
+ // 问题原文直传,语义理解/查数/组织语言都在后端完成
311
+ await aiChatStream(text, {
312
+ sessionId: askSessionId,
313
+ endpoint: props.endpoint,
314
+ headers: props.headers,
315
+ signal: controller.signal,
316
+ onDelta: (delta) => {
317
+ pendingText += delta
318
+ startTyping()
319
+ },
320
+ onDone: (result) => {
321
+ // 完整回答作为收尾基准(揭示动画跑完后再落定)
322
+ if (result.answer) finalText = result.answer
323
+ // 后端常把同一张表既写进 answer 正文、又单独给 tableMarkdown;
324
+ // 正文已包含时不再重复渲染,否则页面会出现两张一模一样的表
325
+ const strip = (s) => (s || '').replace(/\s+/g, '')
326
+ const table = result.tableMarkdown
327
+ finalTable = table && !strip(finalText).includes(strip(table)) ? table : null
328
+ finalChart = result.chartUrl
329
+ // 保存会话 id,供下一轮经 state.opencodeSessionId 关联上下文
330
+ if (result.sessionId) {
331
+ askSessionId = result.sessionId
332
+ localStorage.setItem(SESSION_KEY, result.sessionId)
333
+ }
334
+ }
335
+ })
336
+ aiMsg.duration = +((performance.now() - t0) / 1000).toFixed(1)
337
+ } catch (e) {
338
+ // 失败立即整段展示,不做打字机
339
+ streamDone = true
340
+ stopTyping()
341
+ // 会话失效:清掉本地会话,用户下一次提问自然回到首轮(手册 §5.4/§8.3)
342
+ // 有意不自动重发——手册 §10.2 要求不要无条件重新提交同一问题
343
+ const expired = e && e.code === 'OPENCODE_SESSION_INVALID'
344
+ if (expired) clearAskSession()
345
+ const msg = expired
346
+ ? '会话已过期,请重新提问'
347
+ : (e && e.message ? e.message : '请求失败,请稍后重试')
348
+ aiMsg.content = `[错误] ${msg}`
349
+ aiMsg.error = true
350
+ notify(msg, 'error', 1400)
351
+ } finally {
352
+ if (thinkingTimer) { clearInterval(thinkingTimer); thinkingTimer = null }
353
+ streamDone = true
354
+ if (controller.signal.aborted) {
355
+ stopTyping() // 已中止:不再补揭示
356
+ } else if (aiMsg.error) {
357
+ // 错误已整段展示
358
+ } else if (pendingText || revealedText) {
359
+ startTyping() // 让残余文本继续揭示,tickTyping 自会收尾
360
+ } else {
361
+ aiMsg.content = finalText || '(暂无回答,请稍后重试)'
362
+ aiMsg.tableMarkdown = finalTable
363
+ aiMsg.chartUrl = finalChart
364
+ }
365
+ aiMsg.loading = false
366
+ if (currentAbort.value === controller) currentAbort.value = null
367
+ isSending.value = false
368
+ scrollToBottom()
369
+ }
370
+ }
371
+ /**
372
+ * 点击"发送"按钮
373
+ */
374
+ function handleSend() {
375
+ const text = inputText.value.trim()
376
+ if (!text || isSending.value) return
377
+ sendToAi(text)
378
+ }
379
+
380
+ /**
381
+ * 点击快捷问题:直接发送(不带前缀)
382
+ */
383
+ function handleQuickQuestion(question) {
384
+ if (isSending.value) return
385
+ sendToAi(question)
386
+ }
387
+
388
+ /**
389
+ * 清空对话
390
+ * - 中止正在进行的请求
391
+ * - 重置消息列表
392
+ * - 一并清掉 AskData 会话,否则后端仍记得旧话题、与空白的页面不一致
393
+ */
394
+ function handleClear() {
395
+ if (currentAbort.value) currentAbort.value.abort()
396
+ currentAbort.value = null
397
+ if (activeTypingStop) activeTypingStop()
398
+ isSending.value = false
399
+ messages.value = []
400
+ clearAskSession()
401
+ }
402
+
403
+ /**
404
+ * 键盘事件:Enter 发送,Shift+Enter 换行
405
+ */
406
+ function handleKeydown(e) {
407
+ if (e.key === 'Enter' && !e.shiftKey) {
408
+ e.preventDefault()
409
+ handleSend()
410
+ }
411
+ }
412
+
413
+ /* ============ 生命周期 ============ */
414
+
415
+ onMounted(() => {
416
+ // 默认聚焦输入框
417
+ if (inputRef.value) inputRef.value.focus()
418
+ })
419
+ </script>
420
+
421
+ <style scoped>
422
+ .quick-qa {
423
+ display: flex;
424
+ flex-direction: column;
425
+ height: 100%;
426
+ min-height: 0;
427
+ }
428
+
429
+ /* ===== 顶部 header ===== */
430
+ .qa-header {
431
+ display: flex;
432
+ align-items: center;
433
+ justify-content: space-between;
434
+ padding: 0 0 12px;
435
+ margin-bottom: 16px;
436
+ }
437
+ .qa-header-left { display: flex; align-items: center; gap: 10px; min-width: 0; }
438
+ .qa-logo {
439
+ width: 34px;
440
+ height: 34px;
441
+ border-radius: var(--radius-circle);
442
+ background: linear-gradient(135deg, var(--brand), #8f7fff);
443
+ color: #ffffff;
444
+ box-shadow: 0 0 12px color-mix(in srgb, var(--brand) 40%, transparent);
445
+ display: flex;
446
+ align-items: center;
447
+ justify-content: center;
448
+ font-size: 13px;
449
+ font-weight: 600;
450
+ flex-shrink: 0;
451
+ }
452
+ .qa-header-text { display: flex; flex-direction: column; gap: 1px; min-width: 0; }
453
+ .qa-title { font-size: 15px; font-weight: 600; color: var(--text); line-height: 1.4; }
454
+ .qa-sub { font-size: 12px; color: var(--muted); }
455
+ .qa-header-right { display: flex; align-items: center; gap: 10px; flex-shrink: 0; }
456
+
457
+ /* ===== 预设问题区(页面上方常驻,不区分场景) ===== */
458
+ .preset {
459
+ display: flex;
460
+ flex-wrap: wrap;
461
+ align-items: center;
462
+ gap: 8px;
463
+ padding: 10px 12px;
464
+ border: 1px solid var(--line);
465
+ border-radius: var(--radius-lg);
466
+ background: var(--overlay-card);
467
+ margin-bottom: 12px;
468
+ }
469
+ .preset-label {
470
+ flex: 1 1 auto;
471
+ font-size: 12px;
472
+ font-weight: 600;
473
+ color: var(--muted);
474
+ }
475
+ .preset-label::before {
476
+ content: '';
477
+ display: inline-block;
478
+ width: 6px;
479
+ height: 6px;
480
+ border-radius: var(--radius-circle);
481
+ background: linear-gradient(135deg, var(--brand), #8f7fff);
482
+ margin-right: 6px;
483
+ vertical-align: 1px;
484
+ }
485
+ /* 问题列表:展开=自动换行全铺;折叠=一行横向滚动(见 .is-collapsed) */
486
+ .preset-list {
487
+ order: 3;
488
+ flex: 1 1 100%;
489
+ display: flex;
490
+ flex-wrap: wrap;
491
+ gap: 8px;
492
+ }
493
+ .preset-tag {
494
+ border: 1px solid var(--line);
495
+ background: var(--overlay-card-2);
496
+ color: var(--text);
497
+ padding: 5px 12px;
498
+ border-radius: var(--radius-pill);
499
+ cursor: pointer;
500
+ font-size: 12px;
501
+ line-height: 1.5;
502
+ white-space: nowrap;
503
+ font-family: inherit;
504
+ transition: 0.15s ease;
505
+ }
506
+ .preset-tag:hover:not(:disabled) {
507
+ border-color: var(--brand);
508
+ color: var(--brand);
509
+ background: var(--overlay-hover);
510
+ transform: translateY(-1px);
511
+ }
512
+ .preset-tag:disabled { opacity: 0.5; cursor: not-allowed; }
513
+ .preset-toggle {
514
+ flex-shrink: 0;
515
+ border: 1px solid var(--line);
516
+ background: transparent;
517
+ color: var(--muted);
518
+ padding: 2px 10px;
519
+ border-radius: var(--radius-pill);
520
+ cursor: pointer;
521
+ font-size: 12px;
522
+ font-family: inherit;
523
+ transition: 0.15s ease;
524
+ }
525
+ .preset-toggle:hover {
526
+ border-color: var(--brand);
527
+ color: var(--brand);
528
+ background: var(--overlay-hover);
529
+ }
530
+ .preset-caret { font-size: 10px; margin-left: 2px; }
531
+
532
+ /* 折叠态:预设问题收成一行,标签在左、展开按钮在最右 */
533
+ .preset.is-collapsed { flex-wrap: nowrap; }
534
+ .preset.is-collapsed .preset-label { flex: 0 0 auto; }
535
+ .preset.is-collapsed .preset-list {
536
+ order: 1;
537
+ flex: 1 1 auto;
538
+ min-width: 0;
539
+ flex-wrap: nowrap;
540
+ overflow-x: auto;
541
+ padding-bottom: 2px;
542
+ }
543
+ .preset.is-collapsed .preset-toggle { order: 2; }
544
+
545
+ /* ===== 对话区空态 ===== */
546
+ .chat-empty {
547
+ flex: 1;
548
+ min-height: 0;
549
+ display: flex;
550
+ flex-direction: column;
551
+ align-items: center;
552
+ justify-content: center;
553
+ gap: 12px;
554
+ text-align: center;
555
+ padding: 24px 0;
556
+ }
557
+ .chat-empty-avatar {
558
+ width: 52px;
559
+ height: 52px;
560
+ border-radius: var(--radius-circle);
561
+ background: linear-gradient(135deg, var(--brand), #8f7fff);
562
+ color: #ffffff;
563
+ display: flex;
564
+ align-items: center;
565
+ justify-content: center;
566
+ font-size: 19px;
567
+ font-weight: 600;
568
+ box-shadow: 0 0 18px color-mix(in srgb, var(--brand) 30%, transparent);
569
+ }
570
+ .chat-empty-text {
571
+ margin: 0;
572
+ font-size: 13px;
573
+ color: var(--muted);
574
+ }
575
+
576
+ /* ===== 消息流 ===== */
577
+ .message-list {
578
+ flex: 1;
579
+ min-height: 0;
580
+ overflow-y: auto;
581
+ display: flex;
582
+ flex-direction: column;
583
+ gap: 16px;
584
+ padding: 4px 0 12px;
585
+ }
586
+ .message { display: flex; gap: 10px; align-items: flex-start; animation: msg-in 0.25s ease; }
587
+ @keyframes msg-in {
588
+ from { opacity: 0; transform: translateY(8px); }
589
+ to { opacity: 1; transform: none; }
590
+ }
591
+ .message-user { flex-direction: row-reverse; }
592
+ .message-avatar {
593
+ width: 30px;
594
+ height: 30px;
595
+ border-radius: var(--radius-circle);
596
+ display: flex;
597
+ align-items: center;
598
+ justify-content: center;
599
+ font-weight: 600;
600
+ font-size: 11px;
601
+ flex-shrink: 0;
602
+ }
603
+ .message-assistant .message-avatar {
604
+ background: linear-gradient(135deg, var(--brand), #8f7fff);
605
+ color: #ffffff;
606
+ box-shadow: 0 0 10px color-mix(in srgb, var(--brand) 35%, transparent);
607
+ }
608
+ .message-user .message-avatar {
609
+ background: var(--overlay-card-2);
610
+ color: var(--text);
611
+ }
612
+ .message-body { flex: 1; min-width: 0; display: flex; }
613
+ .message-user .message-body { justify-content: flex-end; }
614
+ .message-bubble {
615
+ max-width: 78%;
616
+ padding: 10px 14px;
617
+ border-radius: 10px;
618
+ border: 1px solid var(--line);
619
+ background: var(--overlay-card);
620
+ font-size: 13px;
621
+ line-height: 1.65;
622
+ word-break: break-word;
623
+ }
624
+ .message-assistant .message-bubble { border-top-left-radius: 2px; }
625
+ .message-user .message-bubble {
626
+ background: var(--brand);
627
+ border-color: var(--brand);
628
+ color: var(--btn-active-text, #ffffff);
629
+ border-top-right-radius: 2px;
630
+ }
631
+ .message-text { white-space: pre-wrap; }
632
+ .message-rich { font-size: 13px; line-height: 1.65; color: var(--text); }
633
+
634
+ /* ===== Markdown 渲染内容样式(marked 输出) ===== */
635
+ /* 用 :deep() 穿透 scoped,让 v-html 插入的子元素能继承到样式 */
636
+ .message-rich :deep(h1),
637
+ .message-rich :deep(h2),
638
+ .message-rich :deep(h3),
639
+ .message-rich :deep(h4) {
640
+ font-weight: 600;
641
+ color: var(--text);
642
+ margin: 10px 0 6px;
643
+ line-height: 1.4;
644
+ }
645
+ .message-rich :deep(h1) { font-size: 16px; }
646
+ .message-rich :deep(h2) { font-size: 15px; }
647
+ .message-rich :deep(h3) { font-size: 14px; }
648
+ .message-rich :deep(h4) { font-size: 13px; }
649
+ .message-rich :deep(h1):first-child,
650
+ .message-rich :deep(h2):first-child,
651
+ .message-rich :deep(h3):first-child,
652
+ .message-rich :deep(h4):first-child { margin-top: 0; }
653
+
654
+ .message-rich :deep(p) { margin: 4px 0; }
655
+ .message-rich :deep(p):first-child { margin-top: 0; }
656
+ .message-rich :deep(p):last-child { margin-bottom: 0; }
657
+
658
+ .message-rich :deep(ul),
659
+ .message-rich :deep(ol) { margin: 6px 0; padding-left: 22px; }
660
+ .message-rich :deep(li) { margin: 2px 0; }
661
+ .message-rich :deep(li > p) { margin: 0; }
662
+
663
+ .message-rich :deep(strong) { color: var(--text); font-weight: 600; }
664
+ .message-rich :deep(em) { color: var(--text); font-style: italic; }
665
+
666
+ .message-rich :deep(a) {
667
+ color: var(--brand);
668
+ text-decoration: none;
669
+ border-bottom: 1px dashed var(--brand);
670
+ transition: color 0.15s ease;
671
+ }
672
+ .message-rich :deep(a:hover) { color: var(--brand-2); border-bottom-color: var(--brand-2); }
673
+
674
+ .message-rich :deep(blockquote) {
675
+ margin: 6px 0;
676
+ padding: 6px 12px;
677
+ border-left: 3px solid var(--brand);
678
+ background: var(--overlay-card-2);
679
+ color: var(--muted);
680
+ border-radius: var(--radius-none) var(--radius-sm) var(--radius-sm) var(--radius-none);
681
+ }
682
+
683
+ .message-rich :deep(hr) {
684
+ border: none;
685
+ border-top: 1px solid var(--line);
686
+ margin: 10px 0;
687
+ }
688
+
689
+ /* 行内 code(marked 输出 <code>,代码块也是 <pre><code>,靠父元素区分) */
690
+ .message-rich :deep(code) {
691
+ font-family: var(--mono);
692
+ background: var(--overlay-mask);
693
+ padding: 1px 6px;
694
+ border-radius: var(--radius-sm);
695
+ font-size: 12px;
696
+ color: var(--chip-info-text);
697
+ }
698
+
699
+ /* 代码块(pre 内的 code 不加行内 padding) */
700
+ .message-rich :deep(pre) {
701
+ margin: 6px 0;
702
+ padding: 8px 12px;
703
+ background: var(--overlay-mask);
704
+ border: 1px solid var(--line);
705
+ border-radius: var(--radius-md);
706
+ overflow-x: auto;
707
+ font-family: var(--mono);
708
+ font-size: 12px;
709
+ line-height: 1.5;
710
+ }
711
+ .message-rich :deep(pre code) {
712
+ background: transparent;
713
+ padding: 0;
714
+ color: var(--text);
715
+ font-size: inherit;
716
+ }
717
+
718
+ /* 表格(marked GFM 输出 <table>) */
719
+ .message-rich :deep(table) {
720
+ width: 100%;
721
+ border-collapse: separate;
722
+ border-spacing: 0;
723
+ font-size: 13px;
724
+ margin: 8px 0;
725
+ border: 1px solid var(--line);
726
+ border-radius: var(--radius-md);
727
+ overflow: hidden;
728
+ background: var(--overlay-card-2);
729
+ }
730
+ .message-rich :deep(thead) {
731
+ background: var(--panel-strong);
732
+ }
733
+ .message-rich :deep(th),
734
+ .message-rich :deep(td) {
735
+ padding: 8px 10px;
736
+ border-bottom: 1px solid var(--line);
737
+ border-right: 1px solid var(--line);
738
+ text-align: left;
739
+ color: var(--text);
740
+ }
741
+ .message-rich :deep(th) { font-weight: 600; }
742
+ .message-rich :deep(th):last-child,
743
+ .message-rich :deep(td):last-child { border-right: none; }
744
+ .message-rich :deep(tbody tr):last-child td { border-bottom: none; }
745
+ .message-rich :deep(tbody tr:nth-child(even) td) { background: var(--overlay-hover); }
746
+ .message-rich :deep(tbody tr:hover td) { background: var(--hover-row-bg); }
747
+
748
+ /* 图表(后端 RUN_FINISHED.result.chartUrl,直接作为图像地址使用,不自行拼接) */
749
+ .message-chart { margin: 8px 0 2px; }
750
+ .message-chart img {
751
+ display: block;
752
+ max-width: 100%;
753
+ height: auto;
754
+ border: 1px solid var(--line);
755
+ border-radius: var(--radius-md);
756
+ background: var(--overlay-card-2);
757
+ }
758
+
759
+ /* ===== AI 思考过程卡片 ===== */
760
+ .process-bubble { min-width: 240px; }
761
+ .process-head {
762
+ display: flex;
763
+ align-items: center;
764
+ gap: 8px;
765
+ margin-bottom: 0;
766
+ }
767
+ .process-head-dot {
768
+ width: 8px;
769
+ height: 8px;
770
+ border-radius: var(--radius-circle);
771
+ background: linear-gradient(135deg, var(--brand), #8f7fff);
772
+ animation: head-pulse 1.4s ease-in-out infinite;
773
+ }
774
+ .process-head-text { font-size: 13px; font-weight: 600; color: var(--text); }
775
+ .process-time {
776
+ margin-left: auto;
777
+ font-size: 11px;
778
+ color: var(--muted);
779
+ font-variant-numeric: tabular-nums;
780
+ }
781
+
782
+ @keyframes head-pulse {
783
+ 0%, 100% { opacity: 1; transform: scale(1); }
784
+ 50% { opacity: 0.45; transform: scale(0.85); }
785
+ }
786
+
787
+ /* ===== AI 回答底部信息(耗时等) ===== */
788
+ .message-meta {
789
+ display: flex;
790
+ align-items: center;
791
+ gap: 6px;
792
+ margin: 0 0 8px;
793
+ padding: 0 0 8px;
794
+ border-bottom: 1px dashed var(--line);
795
+ font-size: 11px;
796
+ color: var(--muted);
797
+ }
798
+ .message-meta-dot {
799
+ width: 5px;
800
+ height: 5px;
801
+ border-radius: var(--radius-circle);
802
+ background: linear-gradient(135deg, var(--brand), #8f7fff);
803
+ flex-shrink: 0;
804
+ }
805
+
806
+ /* ===== 底部输入区 ===== */
807
+ .qa-input-bar {
808
+ display: flex;
809
+ align-items: stretch;
810
+ gap: 10px;
811
+ margin-top: 12px;
812
+ padding: 10px 12px;
813
+ border: 1px solid var(--line);
814
+ border-radius: var(--radius-lg);
815
+ background: var(--overlay-card);
816
+ transition: border-color 0.15s ease;
817
+ }
818
+ .qa-input-bar:focus-within {
819
+ border-color: var(--brand);
820
+ box-shadow: 0 0 0 3px color-mix(in srgb, var(--brand) 12%, transparent),
821
+ 0 4px 18px color-mix(in srgb, var(--brand) 14%, transparent);
822
+ }
823
+ .qa-textarea {
824
+ flex: 1;
825
+ min-width: 0;
826
+ resize: none;
827
+ border: none;
828
+ background: transparent;
829
+ color: var(--text);
830
+ padding: 6px 4px;
831
+ font-family: inherit;
832
+ font-size: 13px;
833
+ line-height: 1.5;
834
+ outline: none;
835
+ }
836
+ .qa-textarea:disabled { opacity: 0.6; cursor: not-allowed; }
837
+ .qa-textarea::placeholder { color: var(--muted); }
838
+ .qa-send-btn {
839
+ flex-shrink: 0;
840
+ border: none;
841
+ background: linear-gradient(135deg, var(--brand), var(--brand2));
842
+ color: var(--btn-active-text, #ffffff);
843
+ padding: 0 22px;
844
+ border-radius: var(--radius-lg);
845
+ cursor: pointer;
846
+ font-size: 13px;
847
+ font-weight: 500;
848
+ transition: 0.18s ease;
849
+ font-family: inherit;
850
+ box-shadow: 0 2px 10px color-mix(in srgb, var(--brand) 25%, transparent);
851
+ }
852
+ .qa-send-btn:hover:not(:disabled) {
853
+ transform: translateY(-1px);
854
+ box-shadow: 0 4px 16px color-mix(in srgb, var(--brand) 40%, transparent);
855
+ }
856
+ .qa-send-btn:disabled,
857
+ .qa-send-btn.disabled { opacity: 0.45; cursor: not-allowed; }
858
+
859
+ /* ===== 通用按钮 ===== */
860
+ .btn {
861
+ border: 1px solid var(--line);
862
+ background: var(--overlay-card-2);
863
+ color: var(--text);
864
+ padding: 4px 12px;
865
+ border-radius: var(--radius-lg);
866
+ cursor: pointer;
867
+ transition: 0.15s ease;
868
+ font-size: 12px;
869
+ font-family: inherit;
870
+ }
871
+ .btn:hover:not(:disabled) { background: var(--overlay-hover); }
872
+ .btn:disabled { opacity: 0.45; cursor: not-allowed; }
873
+ .btn.ghost { background: transparent; }
874
+
875
+ /* ===== 滚动条 ===== */
876
+ .message-list::-webkit-scrollbar,
877
+ .preset-list::-webkit-scrollbar {
878
+ width: 6px;
879
+ height: 6px;
880
+ }
881
+ .message-list::-webkit-scrollbar-thumb,
882
+ .preset-list::-webkit-scrollbar-thumb {
883
+ background: var(--line-strong);
884
+ border-radius: var(--radius-pill);
885
+ }
886
+ .message-list::-webkit-scrollbar-track,
887
+ .preset-list::-webkit-scrollbar-track {
888
+ background: transparent;
889
+ }
890
+ </style>