Commit 15ee66aa by Liu

fix:提质增效欢迎语

parent d8478402
......@@ -62,6 +62,9 @@ export const Chat: React.FC = () => {
const processingConversationIdRef = useRef<string | null>(null)
// 标记是否为提质增效模式的自动调用(用于提取欢迎语)
const isAutoSubmitQualityImprovementRef = useRef<boolean>(false)
// 提质增效自动调用欢迎语候选(用于流结束后写入 sessionStorage)
const autoWelcomeCandidateRef = useRef<string>('')
const hasAutoWelcomeCandidateRef = useRef<boolean>(false)
// 当外部系统直接以 /chat/:id 链接进入(没有 location.state,且 URL 中也没有 toolId)时,
// 视为一次新的会话入口:重置为制度活化,清除历史遗留的工具模式状态
......@@ -145,7 +148,22 @@ export const Chat: React.FC = () => {
const lastIndex = newItems.length - 1
const isEmptyQuestion = !question.trim()
// 提质增效模式下,自动调用时第一个数据块作为欢迎语
// 提质增效模式下,自动调用时收集首个有效 answer 作为欢迎语候选(按流式增量累积)
if (isAutoSubmitQualityImprovementRef.current && isEmptyQuestion) {
const chunkAnswer = msg?.content?.data?.answer
if (typeof chunkAnswer === 'string') {
// 仅对当前分片做基础清洗,避免重复累积整段答案导致内容重复
let cleanedChunk = chunkAnswer.replace(/\([^)]*\)/g, '')
cleanedChunk = cleanedChunk.replace(/\[参考文档(?:[^]*》\s*)+\]/g, '')
cleanedChunk = trimSpacesOnly(cleanedChunk)
if (cleanedChunk.trim()) {
autoWelcomeCandidateRef.current = (autoWelcomeCandidateRef.current || '') + cleanedChunk
hasAutoWelcomeCandidateRef.current = true
}
}
}
// 提质增效模式下,自动调用时第一个数据块作为欢迎语(兼容旧逻辑,仍保留对 system.welcomeText 的写入)
if (isAutoSubmitQualityImprovementRef.current && isEmptyQuestion && msg.content.data.answer) {
// 判断是否为第一个数据块(只有 system 记录或最后一个记录是 system)
const isFirstChunk = newItems.length === 1 && newItems[0].role === 'system'
......@@ -263,6 +281,9 @@ export const Chat: React.FC = () => {
const isQualityImprovement = resolvedToolId === '6712395743241'
isAutoSubmitQualityImprovementRef.current = isEmptyQuestion && isQualityImprovement
if (isAutoSubmitQualityImprovementRef.current) {
// 重置欢迎语候选
autoWelcomeCandidateRef.current = ''
hasAutoWelcomeCandidateRef.current = false
console.log('[Chat] 提质增效自动调用 - handleSubmitQuestion 被调用', {
question,
resolvedToolId,
......@@ -447,8 +468,20 @@ export const Chat: React.FC = () => {
)
.then(() => {
// 流结束时执行(通过 Promise resolve 判断)
// 如果是提质增效自动调用且本轮存在有效欢迎语候选,将其写入 sessionStorage 供欢迎语使用
if (isAutoSubmitQualityImprovementRef.current && hasAutoWelcomeCandidateRef.current) {
try {
const key = 'qualityWelcomeText'
sessionStorage.setItem(key, autoWelcomeCandidateRef.current)
}
catch (error) {
console.error('[Chat] 写入提质增效欢迎语到 sessionStorage 失败:', error)
}
}
// 清除自动调用标记
isAutoSubmitQualityImprovementRef.current = false
autoWelcomeCandidateRef.current = ''
hasAutoWelcomeCandidateRef.current = false
// 设置最后一条 AI 回答的 endAnswerFlag,用于控制打字效果结束
setAllItems((prevItems) => {
const newItems = [...prevItems]
......
......@@ -13,22 +13,32 @@ export const ChatWelcome: React.FC<ChatWelcomeProps> = ({ toolName: _toolName, w
// 根据不同的 toolName 显示不同的提示语
const getWelcomeText = () => {
// 优先使用从接口返回的欢迎语(提质增效模式自动调用时
// 优先使用从接口返回的欢迎语(历史逻辑
if (welcomeText) {
return welcomeText
}
// 回退到原有的 toolId 判断逻辑
// 回退到基于当前工具的欢迎语逻辑
const currentToolId = typeof window !== 'undefined' ? sessionStorage.getItem('currentToolId') : ''
if (currentToolId === '6712395743240') {
return 'Hi~我是您的数据助手,可以帮你查询业务数据哦'
}
// 提质增效:若 sessionStorage 中已有自动调用写入的欢迎语,优先使用该文案
if (currentToolId === '6712395743241') {
try {
const stored = typeof window !== 'undefined' ? sessionStorage.getItem('qualityWelcomeText') : null
if (stored && stored.trim()) {
return stored
}
}
catch {
// 读取失败时回退到默认文案
}
return 'Hi,我是AI提质增效助手,可以为您解答研发需求计划和业务功能流程等相关问题。请问有什么可以帮您?'
}
if (currentToolId === '6712395743240') {
return 'Hi~我是您的数据助手,可以帮你查询业务数据哦'
}
return 'Hi,我是AI制度活化助手,可以为您提供公司规章制度、政策条款的查询与解读服务。请问您想了解什么呢?'
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment