@bams-app/ui-quick-qa 0.0.4 → 0.0.6
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.
- package/README.md +3 -3
- package/index.js +6 -6
- package/package.json +1 -1
- package/src/apis/index.js +4 -4
- package/src/business/MainContent.vue +890 -890
- package/src/constants/index.js +4 -4
- package/src/images/.gitkeep +1 -1
- package/src/styles/theme-vars.css +422 -422
- package/src/utils/llmService.js +185 -185
- package/src/utils/markdownRenderer.js +41 -41
- package/src/utils/notify.js +39 -39
package/src/utils/llmService.js
CHANGED
|
@@ -1,185 +1,185 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 大模型服务封装
|
|
3
|
-
* 所属模块:服务层 → 大模型
|
|
4
|
-
* 业务含义:封装 PDC 智能问答(AskData / AG-UI 协议,SSE)
|
|
5
|
-
*
|
|
6
|
-
* 协议说明(依据《智能问数前端调用手册》):
|
|
7
|
-
* - 端点:{代理前缀}/askdata-api/opencode/ag-ui
|
|
8
|
-
* dev 由根目录 vue.config.dev.js 代理到 http://192.168.7.11:8010/api/v1
|
|
9
|
-
* - 鉴权:AskData API key 由 devServer 代理(生产由 nginx/BFF)注入 Authorization 头,
|
|
10
|
-
* 前端不持有 key,也不要新增任何密钥常量
|
|
11
|
-
* - 请求体:首轮 { question };续轮追加 state.opencodeSessionId(= 上一轮 RUN_FINISHED.result.sessionId)
|
|
12
|
-
* 时间范围等条件必须写在本轮 question 里,sessionId 只关联会话、不承担参数传递
|
|
13
|
-
* - 事件流:RUN_STARTED → TEXT_MESSAGE_START → TEXT_MESSAGE_CONTENT{delta} → TEXT_MESSAGE_END → RUN_FINISHED{result}
|
|
14
|
-
* - 失败事件:RUN_ERROR{code,message};HTTP 错误码见手册 §10.1
|
|
15
|
-
*
|
|
16
|
-
* 注意:后端是「算完整段再下发」,并非逐字 token 流;打字机效果由页面本地实现。
|
|
17
|
-
*/
|
|
18
|
-
const DEFAULT_AI_CHAT_ENDPOINT = '/askdata-api/opencode/ag-ui'
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* 调用 PDC 后端智能问答 SSE 接口
|
|
22
|
-
* @param {string} question 用户本轮问题,时间条件需包含在内
|
|
23
|
-
* @param {{
|
|
24
|
-
* sessionId?: string,
|
|
25
|
-
* endpoint?: string,
|
|
26
|
-
* headers?: Record<string, string>,
|
|
27
|
-
* onDelta?: (text: string) => void,
|
|
28
|
-
* onDone?: (result: {answer: string, sessionId: string|null, tableMarkdown: string|null, chartUrl: string|null}) => void,
|
|
29
|
-
* onError?: (err: Error) => void,
|
|
30
|
-
* signal?: AbortSignal
|
|
31
|
-
* }} opts
|
|
32
|
-
* @returns {Promise<void>}
|
|
33
|
-
*/
|
|
34
|
-
export async function aiChatStream(question, opts = {}) {
|
|
35
|
-
const { sessionId, endpoint, headers, onDelta, onDone, onError, signal } = opts
|
|
36
|
-
let response
|
|
37
|
-
let finished = false
|
|
38
|
-
let result = null
|
|
39
|
-
const finish = () => {
|
|
40
|
-
if (finished) return
|
|
41
|
-
finished = true
|
|
42
|
-
if (typeof onDone === 'function') onDone(result)
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
// 首轮不带 state;续轮用 state.opencodeSessionId 关联上下文
|
|
46
|
-
const body = { question }
|
|
47
|
-
if (sessionId) body.state = { opencodeSessionId: sessionId }
|
|
48
|
-
|
|
49
|
-
try {
|
|
50
|
-
response = await fetch(endpoint || DEFAULT_AI_CHAT_ENDPOINT, {
|
|
51
|
-
method: 'POST',
|
|
52
|
-
headers: {
|
|
53
|
-
'Content-Type': 'application/json',
|
|
54
|
-
Accept: 'text/event-stream',
|
|
55
|
-
sessionId: (typeof window !== 'undefined' && window?.userStore?.sessionId) || '',
|
|
56
|
-
...(headers || {})
|
|
57
|
-
},
|
|
58
|
-
body: JSON.stringify(body),
|
|
59
|
-
signal
|
|
60
|
-
})
|
|
61
|
-
} catch (e) {
|
|
62
|
-
if (e?.name === 'AbortError') return
|
|
63
|
-
const err = new Error(getNetworkErrorMessage(e))
|
|
64
|
-
if (typeof onError === 'function') onError(err)
|
|
65
|
-
throw err
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
if (!response.ok || !response.body) {
|
|
69
|
-
// 手册 §10.1:401 ASKDATA_API_KEY_REQUIRED / 400 OPENCODE_SESSION_INVALID /
|
|
70
|
-
// 422 参数校验错误 / 502 OPENCODE_UNAVAILABLE。后端的 code 与 message 都在响应体里,
|
|
71
|
-
// 取出 code 供上层区分(400 的会话失效与 RUN_ERROR 的同一个 code 走同一套处理)
|
|
72
|
-
const text = await safeReadText(response)
|
|
73
|
-
let message = text
|
|
74
|
-
let code = null
|
|
75
|
-
try {
|
|
76
|
-
const payload = JSON.parse(text)
|
|
77
|
-
message = payload.message || text
|
|
78
|
-
code = payload.code || null
|
|
79
|
-
} catch (_) {
|
|
80
|
-
// 非 JSON 响应,保留原文作为提示
|
|
81
|
-
}
|
|
82
|
-
const err = new Error(message || `请求失败:HTTP ${response.status}`)
|
|
83
|
-
err.status = response.status
|
|
84
|
-
err.code = code
|
|
85
|
-
if (typeof onError === 'function') onError(err)
|
|
86
|
-
throw err
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
const reader = response.body.getReader()
|
|
90
|
-
const decoder = new TextDecoder('utf-8')
|
|
91
|
-
let buffer = ''
|
|
92
|
-
let answer = ''
|
|
93
|
-
|
|
94
|
-
const consumeEvent = (eventBlock) => {
|
|
95
|
-
const dataLine = eventBlock
|
|
96
|
-
.split(/\r?\n/)
|
|
97
|
-
.find((line) => line.startsWith('data:'))
|
|
98
|
-
if (!dataLine) return false
|
|
99
|
-
|
|
100
|
-
const payload = dataLine.slice(5).trim()
|
|
101
|
-
if (!payload) return false
|
|
102
|
-
let event
|
|
103
|
-
try {
|
|
104
|
-
event = JSON.parse(payload)
|
|
105
|
-
} catch (_) {
|
|
106
|
-
throw new Error('智能问答返回数据格式错误')
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
// AG-UI:文本增量(后端一次给一整段,也可能分片)
|
|
110
|
-
if (event.type === 'TEXT_MESSAGE_CONTENT') {
|
|
111
|
-
const delta = typeof event.delta === 'string' ? event.delta : ''
|
|
112
|
-
answer += delta
|
|
113
|
-
if (delta && typeof onDelta === 'function') onDelta(delta)
|
|
114
|
-
return false
|
|
115
|
-
}
|
|
116
|
-
// AG-UI:本轮结束,result 携带权威全文、会话 id 与可选的结构化结果
|
|
117
|
-
if (event.type === 'RUN_FINISHED') {
|
|
118
|
-
const r = event.result || {}
|
|
119
|
-
result = {
|
|
120
|
-
answer: (typeof r.answer === 'string' && r.answer) || answer,
|
|
121
|
-
sessionId: r.sessionId || null,
|
|
122
|
-
tableMarkdown: r.tableMarkdown || null,
|
|
123
|
-
chartUrl: r.chartUrl || null
|
|
124
|
-
}
|
|
125
|
-
finish()
|
|
126
|
-
return true
|
|
127
|
-
}
|
|
128
|
-
// AG-UI:本轮失败;code 透传给上层(OPENCODE_SESSION_INVALID 需清会话)
|
|
129
|
-
if (event.type === 'RUN_ERROR') {
|
|
130
|
-
const err = new Error(event.message || '智能问答失败')
|
|
131
|
-
err.code = event.code
|
|
132
|
-
throw err
|
|
133
|
-
}
|
|
134
|
-
// 其余事件(RUN_STARTED / TEXT_MESSAGE_START / TEXT_MESSAGE_END 等)忽略,向前兼容
|
|
135
|
-
return false
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
try {
|
|
139
|
-
while (!finished) {
|
|
140
|
-
const { value, done } = await reader.read()
|
|
141
|
-
if (done) break
|
|
142
|
-
buffer += decoder.decode(value, { stream: true })
|
|
143
|
-
let index
|
|
144
|
-
while (!finished && (index = buffer.indexOf('\n\n')) >= 0) {
|
|
145
|
-
const eventBlock = buffer.slice(0, index)
|
|
146
|
-
buffer = buffer.slice(index + 2)
|
|
147
|
-
consumeEvent(eventBlock)
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
if (!finished) {
|
|
151
|
-
buffer += decoder.decode()
|
|
152
|
-
if (buffer.trim()) consumeEvent(buffer)
|
|
153
|
-
// 流自然结束但没收到 RUN_FINISHED:以累积文本兜底
|
|
154
|
-
if (!finished) {
|
|
155
|
-
result = { answer, sessionId: null, tableMarkdown: null, chartUrl: null }
|
|
156
|
-
finish()
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
} catch (e) {
|
|
160
|
-
if (e?.name === 'AbortError') return
|
|
161
|
-
const err = e instanceof Error ? e : new Error(getNetworkErrorMessage(e))
|
|
162
|
-
if (typeof onError === 'function') onError(err)
|
|
163
|
-
throw err
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
* 统一网络错误文案
|
|
169
|
-
*/
|
|
170
|
-
function getNetworkErrorMessage(e) {
|
|
171
|
-
if (!e) return '未知网络错误'
|
|
172
|
-
if (e.message) return e.message
|
|
173
|
-
return '网络请求失败,请检查网络或跨域配置'
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
/**
|
|
177
|
-
* 安全读取响应文本
|
|
178
|
-
*/
|
|
179
|
-
async function safeReadText(resp) {
|
|
180
|
-
try {
|
|
181
|
-
return await resp.text()
|
|
182
|
-
} catch (_) {
|
|
183
|
-
return ''
|
|
184
|
-
}
|
|
185
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* 大模型服务封装
|
|
3
|
+
* 所属模块:服务层 → 大模型
|
|
4
|
+
* 业务含义:封装 PDC 智能问答(AskData / AG-UI 协议,SSE)
|
|
5
|
+
*
|
|
6
|
+
* 协议说明(依据《智能问数前端调用手册》):
|
|
7
|
+
* - 端点:{代理前缀}/askdata-api/opencode/ag-ui
|
|
8
|
+
* dev 由根目录 vue.config.dev.js 代理到 http://192.168.7.11:8010/api/v1
|
|
9
|
+
* - 鉴权:AskData API key 由 devServer 代理(生产由 nginx/BFF)注入 Authorization 头,
|
|
10
|
+
* 前端不持有 key,也不要新增任何密钥常量
|
|
11
|
+
* - 请求体:首轮 { question };续轮追加 state.opencodeSessionId(= 上一轮 RUN_FINISHED.result.sessionId)
|
|
12
|
+
* 时间范围等条件必须写在本轮 question 里,sessionId 只关联会话、不承担参数传递
|
|
13
|
+
* - 事件流:RUN_STARTED → TEXT_MESSAGE_START → TEXT_MESSAGE_CONTENT{delta} → TEXT_MESSAGE_END → RUN_FINISHED{result}
|
|
14
|
+
* - 失败事件:RUN_ERROR{code,message};HTTP 错误码见手册 §10.1
|
|
15
|
+
*
|
|
16
|
+
* 注意:后端是「算完整段再下发」,并非逐字 token 流;打字机效果由页面本地实现。
|
|
17
|
+
*/
|
|
18
|
+
const DEFAULT_AI_CHAT_ENDPOINT = '/askdata-api/opencode/ag-ui'
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* 调用 PDC 后端智能问答 SSE 接口
|
|
22
|
+
* @param {string} question 用户本轮问题,时间条件需包含在内
|
|
23
|
+
* @param {{
|
|
24
|
+
* sessionId?: string,
|
|
25
|
+
* endpoint?: string,
|
|
26
|
+
* headers?: Record<string, string>,
|
|
27
|
+
* onDelta?: (text: string) => void,
|
|
28
|
+
* onDone?: (result: {answer: string, sessionId: string|null, tableMarkdown: string|null, chartUrl: string|null}) => void,
|
|
29
|
+
* onError?: (err: Error) => void,
|
|
30
|
+
* signal?: AbortSignal
|
|
31
|
+
* }} opts
|
|
32
|
+
* @returns {Promise<void>}
|
|
33
|
+
*/
|
|
34
|
+
export async function aiChatStream(question, opts = {}) {
|
|
35
|
+
const { sessionId, endpoint, headers, onDelta, onDone, onError, signal } = opts
|
|
36
|
+
let response
|
|
37
|
+
let finished = false
|
|
38
|
+
let result = null
|
|
39
|
+
const finish = () => {
|
|
40
|
+
if (finished) return
|
|
41
|
+
finished = true
|
|
42
|
+
if (typeof onDone === 'function') onDone(result)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// 首轮不带 state;续轮用 state.opencodeSessionId 关联上下文
|
|
46
|
+
const body = { question }
|
|
47
|
+
if (sessionId) body.state = { opencodeSessionId: sessionId }
|
|
48
|
+
|
|
49
|
+
try {
|
|
50
|
+
response = await fetch(endpoint || DEFAULT_AI_CHAT_ENDPOINT, {
|
|
51
|
+
method: 'POST',
|
|
52
|
+
headers: {
|
|
53
|
+
'Content-Type': 'application/json',
|
|
54
|
+
Accept: 'text/event-stream',
|
|
55
|
+
sessionId: (typeof window !== 'undefined' && window?.userStore?.sessionId) || '',
|
|
56
|
+
...(headers || {})
|
|
57
|
+
},
|
|
58
|
+
body: JSON.stringify(body),
|
|
59
|
+
signal
|
|
60
|
+
})
|
|
61
|
+
} catch (e) {
|
|
62
|
+
if (e?.name === 'AbortError') return
|
|
63
|
+
const err = new Error(getNetworkErrorMessage(e))
|
|
64
|
+
if (typeof onError === 'function') onError(err)
|
|
65
|
+
throw err
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (!response.ok || !response.body) {
|
|
69
|
+
// 手册 §10.1:401 ASKDATA_API_KEY_REQUIRED / 400 OPENCODE_SESSION_INVALID /
|
|
70
|
+
// 422 参数校验错误 / 502 OPENCODE_UNAVAILABLE。后端的 code 与 message 都在响应体里,
|
|
71
|
+
// 取出 code 供上层区分(400 的会话失效与 RUN_ERROR 的同一个 code 走同一套处理)
|
|
72
|
+
const text = await safeReadText(response)
|
|
73
|
+
let message = text
|
|
74
|
+
let code = null
|
|
75
|
+
try {
|
|
76
|
+
const payload = JSON.parse(text)
|
|
77
|
+
message = payload.message || text
|
|
78
|
+
code = payload.code || null
|
|
79
|
+
} catch (_) {
|
|
80
|
+
// 非 JSON 响应,保留原文作为提示
|
|
81
|
+
}
|
|
82
|
+
const err = new Error(message || `请求失败:HTTP ${response.status}`)
|
|
83
|
+
err.status = response.status
|
|
84
|
+
err.code = code
|
|
85
|
+
if (typeof onError === 'function') onError(err)
|
|
86
|
+
throw err
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const reader = response.body.getReader()
|
|
90
|
+
const decoder = new TextDecoder('utf-8')
|
|
91
|
+
let buffer = ''
|
|
92
|
+
let answer = ''
|
|
93
|
+
|
|
94
|
+
const consumeEvent = (eventBlock) => {
|
|
95
|
+
const dataLine = eventBlock
|
|
96
|
+
.split(/\r?\n/)
|
|
97
|
+
.find((line) => line.startsWith('data:'))
|
|
98
|
+
if (!dataLine) return false
|
|
99
|
+
|
|
100
|
+
const payload = dataLine.slice(5).trim()
|
|
101
|
+
if (!payload) return false
|
|
102
|
+
let event
|
|
103
|
+
try {
|
|
104
|
+
event = JSON.parse(payload)
|
|
105
|
+
} catch (_) {
|
|
106
|
+
throw new Error('智能问答返回数据格式错误')
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// AG-UI:文本增量(后端一次给一整段,也可能分片)
|
|
110
|
+
if (event.type === 'TEXT_MESSAGE_CONTENT') {
|
|
111
|
+
const delta = typeof event.delta === 'string' ? event.delta : ''
|
|
112
|
+
answer += delta
|
|
113
|
+
if (delta && typeof onDelta === 'function') onDelta(delta)
|
|
114
|
+
return false
|
|
115
|
+
}
|
|
116
|
+
// AG-UI:本轮结束,result 携带权威全文、会话 id 与可选的结构化结果
|
|
117
|
+
if (event.type === 'RUN_FINISHED') {
|
|
118
|
+
const r = event.result || {}
|
|
119
|
+
result = {
|
|
120
|
+
answer: (typeof r.answer === 'string' && r.answer) || answer,
|
|
121
|
+
sessionId: r.sessionId || null,
|
|
122
|
+
tableMarkdown: r.tableMarkdown || null,
|
|
123
|
+
chartUrl: r.chartUrl || null
|
|
124
|
+
}
|
|
125
|
+
finish()
|
|
126
|
+
return true
|
|
127
|
+
}
|
|
128
|
+
// AG-UI:本轮失败;code 透传给上层(OPENCODE_SESSION_INVALID 需清会话)
|
|
129
|
+
if (event.type === 'RUN_ERROR') {
|
|
130
|
+
const err = new Error(event.message || '智能问答失败')
|
|
131
|
+
err.code = event.code
|
|
132
|
+
throw err
|
|
133
|
+
}
|
|
134
|
+
// 其余事件(RUN_STARTED / TEXT_MESSAGE_START / TEXT_MESSAGE_END 等)忽略,向前兼容
|
|
135
|
+
return false
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
try {
|
|
139
|
+
while (!finished) {
|
|
140
|
+
const { value, done } = await reader.read()
|
|
141
|
+
if (done) break
|
|
142
|
+
buffer += decoder.decode(value, { stream: true })
|
|
143
|
+
let index
|
|
144
|
+
while (!finished && (index = buffer.indexOf('\n\n')) >= 0) {
|
|
145
|
+
const eventBlock = buffer.slice(0, index)
|
|
146
|
+
buffer = buffer.slice(index + 2)
|
|
147
|
+
consumeEvent(eventBlock)
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
if (!finished) {
|
|
151
|
+
buffer += decoder.decode()
|
|
152
|
+
if (buffer.trim()) consumeEvent(buffer)
|
|
153
|
+
// 流自然结束但没收到 RUN_FINISHED:以累积文本兜底
|
|
154
|
+
if (!finished) {
|
|
155
|
+
result = { answer, sessionId: null, tableMarkdown: null, chartUrl: null }
|
|
156
|
+
finish()
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
} catch (e) {
|
|
160
|
+
if (e?.name === 'AbortError') return
|
|
161
|
+
const err = e instanceof Error ? e : new Error(getNetworkErrorMessage(e))
|
|
162
|
+
if (typeof onError === 'function') onError(err)
|
|
163
|
+
throw err
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* 统一网络错误文案
|
|
169
|
+
*/
|
|
170
|
+
function getNetworkErrorMessage(e) {
|
|
171
|
+
if (!e) return '未知网络错误'
|
|
172
|
+
if (e.message) return e.message
|
|
173
|
+
return '网络请求失败,请检查网络或跨域配置'
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* 安全读取响应文本
|
|
178
|
+
*/
|
|
179
|
+
async function safeReadText(resp) {
|
|
180
|
+
try {
|
|
181
|
+
return await resp.text()
|
|
182
|
+
} catch (_) {
|
|
183
|
+
return ''
|
|
184
|
+
}
|
|
185
|
+
}
|
|
@@ -1,41 +1,41 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Markdown 渲染工具
|
|
3
|
-
* 所属模块:工具层 → markdown
|
|
4
|
-
* 业务含义:将 AI 返回的 Markdown 文本渲染为安全的 HTML 字符串,供 v-html 渲染
|
|
5
|
-
*
|
|
6
|
-
* 实现:
|
|
7
|
-
* - marked:业界最快、API 最简的 Markdown 解析器(GFM 完整支持)
|
|
8
|
-
* - DOMPurify:清洗 marked 输出的 HTML,剔除 <script>/onerror 等 XSS 攻击向量
|
|
9
|
-
*
|
|
10
|
-
* 安全说明:
|
|
11
|
-
* - AI 返回内容不可信,必须过 DOMPurify
|
|
12
|
-
* - DOMPurify 默认白名单覆盖常用 Markdown 输出(h1-h6, p, ul/ol, code, pre, table, a, img 等)
|
|
13
|
-
* - 不在白名单的标签和属性会被静默移除
|
|
14
|
-
*/
|
|
15
|
-
import { marked } from 'marked'
|
|
16
|
-
import DOMPurify from 'dompurify'
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* marked 全局配置
|
|
20
|
-
* - gfm: 启用 GitHub Flavored Markdown(表格、删除线、任务列表、链接自动识别)
|
|
21
|
-
* - breaks: 单换行变 <br>(中文聊天场景友好;默认是双换行才分段)
|
|
22
|
-
*/
|
|
23
|
-
marked.setOptions({
|
|
24
|
-
gfm: true,
|
|
25
|
-
breaks: true
|
|
26
|
-
})
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* 将 Markdown 文本渲染为安全的 HTML 字符串
|
|
30
|
-
* @param {string} text
|
|
31
|
-
* @returns {string} 可直接用于 v-html 的安全 HTML
|
|
32
|
-
*/
|
|
33
|
-
export function renderMarkdown(text) {
|
|
34
|
-
if (!text || typeof text !== 'string') return ''
|
|
35
|
-
const rawHtml = marked.parse(text)
|
|
36
|
-
return DOMPurify.sanitize(rawHtml)
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Markdown 渲染工具
|
|
3
|
+
* 所属模块:工具层 → markdown
|
|
4
|
+
* 业务含义:将 AI 返回的 Markdown 文本渲染为安全的 HTML 字符串,供 v-html 渲染
|
|
5
|
+
*
|
|
6
|
+
* 实现:
|
|
7
|
+
* - marked:业界最快、API 最简的 Markdown 解析器(GFM 完整支持)
|
|
8
|
+
* - DOMPurify:清洗 marked 输出的 HTML,剔除 <script>/onerror 等 XSS 攻击向量
|
|
9
|
+
*
|
|
10
|
+
* 安全说明:
|
|
11
|
+
* - AI 返回内容不可信,必须过 DOMPurify
|
|
12
|
+
* - DOMPurify 默认白名单覆盖常用 Markdown 输出(h1-h6, p, ul/ol, code, pre, table, a, img 等)
|
|
13
|
+
* - 不在白名单的标签和属性会被静默移除
|
|
14
|
+
*/
|
|
15
|
+
import { marked } from 'marked'
|
|
16
|
+
import DOMPurify from 'dompurify'
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* marked 全局配置
|
|
20
|
+
* - gfm: 启用 GitHub Flavored Markdown(表格、删除线、任务列表、链接自动识别)
|
|
21
|
+
* - breaks: 单换行变 <br>(中文聊天场景友好;默认是双换行才分段)
|
|
22
|
+
*/
|
|
23
|
+
marked.setOptions({
|
|
24
|
+
gfm: true,
|
|
25
|
+
breaks: true
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* 将 Markdown 文本渲染为安全的 HTML 字符串
|
|
30
|
+
* @param {string} text
|
|
31
|
+
* @returns {string} 可直接用于 v-html 的安全 HTML
|
|
32
|
+
*/
|
|
33
|
+
export function renderMarkdown(text) {
|
|
34
|
+
if (!text || typeof text !== 'string') return ''
|
|
35
|
+
const rawHtml = marked.parse(text)
|
|
36
|
+
return DOMPurify.sanitize(rawHtml)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
|
package/src/utils/notify.js
CHANGED
|
@@ -1,39 +1,39 @@
|
|
|
1
|
-
import message from "@bams-app/message";
|
|
2
|
-
|
|
3
|
-
const DEFAULT_MESSAGE_TYPE = "info";
|
|
4
|
-
const DEFAULT_DISPLAY_TIME = 1000;
|
|
5
|
-
const DISPLAY_TIME_UNIT = 1000;
|
|
6
|
-
const MESSAGE_TYPE_LIST = Object.freeze(["info", "success", "warning", "error", "loading"]);
|
|
7
|
-
|
|
8
|
-
// 统一限制消息类型,避免异常类型影响业务流程。
|
|
9
|
-
const getSafeMessageType = type => {
|
|
10
|
-
if (MESSAGE_TYPE_LIST.includes(type)) {
|
|
11
|
-
return type;
|
|
12
|
-
}
|
|
13
|
-
return DEFAULT_MESSAGE_TYPE;
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
// 业务侧按毫秒传入展示时长,@bams-app/message 使用秒级 duration。
|
|
17
|
-
const getMessageDuration = displayTime => {
|
|
18
|
-
if (typeof displayTime !== "number" || Number.isNaN(displayTime)) {
|
|
19
|
-
return DEFAULT_DISPLAY_TIME / DISPLAY_TIME_UNIT;
|
|
20
|
-
}
|
|
21
|
-
return Math.max(displayTime, 0) / DISPLAY_TIME_UNIT;
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
export function notify(messageText, type = DEFAULT_MESSAGE_TYPE, displayTime = DEFAULT_DISPLAY_TIME) {
|
|
25
|
-
const messageType = getSafeMessageType(type);
|
|
26
|
-
const duration = getMessageDuration(displayTime);
|
|
27
|
-
const messageOptions = {
|
|
28
|
-
content: messageText,
|
|
29
|
-
duration,
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
if (message && typeof message[messageType] === "function") {
|
|
33
|
-
message[messageType](messageOptions);
|
|
34
|
-
return;
|
|
35
|
-
}
|
|
36
|
-
if (message && typeof message.info === "function") {
|
|
37
|
-
message.info(messageOptions);
|
|
38
|
-
}
|
|
39
|
-
}
|
|
1
|
+
import message from "@bams-app/message";
|
|
2
|
+
|
|
3
|
+
const DEFAULT_MESSAGE_TYPE = "info";
|
|
4
|
+
const DEFAULT_DISPLAY_TIME = 1000;
|
|
5
|
+
const DISPLAY_TIME_UNIT = 1000;
|
|
6
|
+
const MESSAGE_TYPE_LIST = Object.freeze(["info", "success", "warning", "error", "loading"]);
|
|
7
|
+
|
|
8
|
+
// 统一限制消息类型,避免异常类型影响业务流程。
|
|
9
|
+
const getSafeMessageType = type => {
|
|
10
|
+
if (MESSAGE_TYPE_LIST.includes(type)) {
|
|
11
|
+
return type;
|
|
12
|
+
}
|
|
13
|
+
return DEFAULT_MESSAGE_TYPE;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
// 业务侧按毫秒传入展示时长,@bams-app/message 使用秒级 duration。
|
|
17
|
+
const getMessageDuration = displayTime => {
|
|
18
|
+
if (typeof displayTime !== "number" || Number.isNaN(displayTime)) {
|
|
19
|
+
return DEFAULT_DISPLAY_TIME / DISPLAY_TIME_UNIT;
|
|
20
|
+
}
|
|
21
|
+
return Math.max(displayTime, 0) / DISPLAY_TIME_UNIT;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export function notify(messageText, type = DEFAULT_MESSAGE_TYPE, displayTime = DEFAULT_DISPLAY_TIME) {
|
|
25
|
+
const messageType = getSafeMessageType(type);
|
|
26
|
+
const duration = getMessageDuration(displayTime);
|
|
27
|
+
const messageOptions = {
|
|
28
|
+
content: messageText,
|
|
29
|
+
duration,
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
if (message && typeof message[messageType] === "function") {
|
|
33
|
+
message[messageType](messageOptions);
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
if (message && typeof message.info === "function") {
|
|
37
|
+
message.info(messageOptions);
|
|
38
|
+
}
|
|
39
|
+
}
|