Compare commits

..

No commits in common. "b2f7f67e3929790a1ce520c46674562471f97f48" and "0bec4a30b383c51e1d3243dca3b76a373a61a2af" have entirely different histories.

6 changed files with 306 additions and 579 deletions

View File

@ -1,107 +0,0 @@
<template>
<uni-popup position="center" :mask-click-able="false" ref="popupRef" type="center" :safe-area="false" :custom-style="{width: '100%', height: '100vh', borderRadius: 0, position: 'fixed', top: '0', left: '0', zIndex: 9999}">
<view class="recording-popup">
<view class="recording-wave">
<!-- 波形动画 -->
<view class="wave-animation"></view>
</view>
<view class="recording-text">
{{ isSlideToText ? '松开发送 转文字' : '松开发送' }}
</view>
<view class="recording-cancel" @click="handleCancel">
取消
</view>
</view>
</uni-popup>
</template>
<script setup>
import { ref } from 'vue'
const props = defineProps({
isSlideToText: Boolean
})
const emit = defineEmits(['cancel'])
const popupRef = ref(null)
//
const open = () => {
if (popupRef.value) {
popupRef.value.open()
}
}
//
const close = () => {
if (popupRef.value) {
popupRef.value.close()
}
}
//
const handleCancel = () => {
emit('cancel')
close()
}
//
defineExpose({
open,
close
})
</script>
<style scoped lang="scss">
/* 录音弹窗样式 */
.recording-popup {
width: 100% !important;
height: 100vh !important;
background-color: rgba(0, 0, 0, 0.7);
border-radius: 0;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
color: white;
padding: 40px 0;
position: fixed;
top: 0;
left: 0;
z-index: 9999;
}
.recording-wave {
width: 240px;
height: 240px;
border-radius: 50%;
background-color: rgba(76, 217, 100, 0.3);
display: flex;
justify-content: center;
align-items: center;
margin-bottom: 40px;
}
.wave-animation {
width: 160px;
height: 160px;
/* 这里可以添加波形动画 */
background-image: url('/static/wave_icon.png');
background-size: contain;
background-repeat: no-repeat;
background-position: center;
}
.recording-text {
font-size: 20px;
margin-bottom: 40px;
}
.recording-cancel {
font-size: 18px;
color: #CCCCCC;
margin-top: 20px;
}
</style>

View File

@ -1,140 +0,0 @@
<template>
<uni-popup position="center" :mask-click-able="false" ref="popupRef" type="center" :safe-area="false" :custom-style="{ width: '100%', maxWidth: '100vw', borderRadius: 0, position: 'fixed', top: '0', left: '0', zIndex: 9999 }">
<view class="voice-result-popup">
<view class="voice-result-bubble">
<textarea v-model="editedVoiceText" class="editable-textarea" placeholder="语音转换结果"></textarea>
</view>
<view class="voice-result-actions">
<view class="action-button cancel" @click="handleCancel">取消</view>
<view class="action-button send" @click="handleSendText">发送</view>
</view>
</view>
</uni-popup>
</template>
<script setup>
import { ref, watch } from 'vue'
const props = defineProps({
voiceText: String
})
const emit = defineEmits(['cancel', 'sendText'])
const popupRef = ref(null)
const editedVoiceText = ref(props.voiceText || '')
// props
watch(() => props.voiceText,
(newValue) => {
editedVoiceText.value = newValue || ''
}
)
//
const open = () => {
if (popupRef.value) {
popupRef.value.open()
}
}
//
const close = () => {
if (popupRef.value) {
popupRef.value.close()
}
}
//
const handleCancel = () => {
emit('cancel')
close()
}
//
const handleSendText = () => {
emit('sendText', { text: editedVoiceText.value })
close()
}
//
defineExpose({
open,
close
})
</script>
<style scoped lang="scss">
/* 语音结果弹窗样式 */
.voice-result-popup {
width: 100% !important;
height: 100vh;
background-color: rgba(0, 0, 0, 0.7);
padding: 40px 20px;
display: flex;
flex-direction: column;
justify-content: flex-end;
box-sizing: border-box;
}
.voice-result-bubble {
background-color: #00A6FF;
color: white;
box-sizing: border-box;
width: 100%;
padding: 16px;
border-radius: 10px;
margin-bottom: 40px;
min-height: 120px;
max-height: 400px;
font-size: 16px;
overflow-y: auto;
box-shadow: 2px 2px 6px 0px rgba(0,0,0,0.1);
border-radius: 20px 4px 20px 20px;
border: 1px solid;
border-color: #FFFFFF;
}
.editable-textarea {
width: 100%;
height: 100%;
background: transparent;
color: white;
font-size: 16px;
}
.editable-textarea:focus {
outline: none;
}
.voice-result-actions {
display: flex;
justify-content: center;
gap: 20px;
width: 100%;
max-width: 600px;
margin-top: 20px;
padding-bottom: 20px;
}
.action-button {
padding: 12px 24px;
border-radius: 30px;
font-size: 18px;
min-width: 120px;
text-align: center;
}
.cancel {
color: #F5F5F5;
border: 1px solid;
border-color: #F5F5F5;
}
.send {
color: #333;
background-color: #F5F5F5;
}
</style>

View File

@ -8,12 +8,28 @@
<!-- 输入框/语音按钮容器 --> <!-- 输入框/语音按钮容器 -->
<view class="input-button-container"> <view class="input-button-container">
<textarea ref="textareaRef" class="textarea" type="text" :placeholder="placeholder" cursor-spacing="65" <textarea
confirm-type='done' v-model="inputMessage" @confirm="sendMessage" @focus="handleFocus" @blur="handleBlur" ref="textareaRef"
@touchstart="handleTouchStart" @touchend="handleTouchEnd" :confirm-hold="true" auto-height class="textarea"
:show-confirm-bar='false' :hold-keyboard="holdKeyboard" :adjust-position="true" maxlength="300" /> type="text"
:placeholder="placeholder"
cursor-spacing="65"
confirm-type='done'
v-model="inputMessage"
@confirm="sendMessage"
@focus="handleFocus"
@blur="handleBlur"
@touchstart="handleTouchStart"
@touchend="handleTouchEnd"
:confirm-hold="true"
auto-height
:show-confirm-bar='false'
:hold-keyboard="holdKeyboard"
:adjust-position="true"
maxlength="300"
/>
<!-- <view <view
v-if="isVoiceMode" v-if="isVoiceMode"
class="hold-to-talk-button" class="hold-to-talk-button"
@touchstart.stop="startRecording" @touchstart.stop="startRecording"
@ -21,14 +37,11 @@
@touchmove.stop="handleTouchMove" @touchmove.stop="handleTouchMove"
> >
按住说话 按住说话
</view> -->
<view v-if="isVoiceMode" class="hold-to-talk-button" @click.stop="startRecording">
按住说话
</view> </view>
</view> </view>
<view class="input-container-send"> <view class="input-container-send">
<view class="input-container-send-btn" @click="sendMessage"> <view v-if="!isVoiceMode" class="input-container-send-btn" @click="sendMessage">
<image v-if="props.isSessionActive" src='/static/input_stop_icon.png'></image> <image v-if="props.isSessionActive" src='/static/input_stop_icon.png'></image>
<image v-else src='/static/input_send_icon.png'></image> <image v-else src='/static/input_send_icon.png'></image>
</view> </view>
@ -36,18 +49,40 @@
</view> </view>
<!-- 使用封装的弹窗组件 --> <!-- 录音界面 -->
<RecordingPopup ref="recordingPopupRef" :is-slide-to-text="isSlideToText" @cancel="handleRecordingCancel" /> <uni-popup v-model:show="isRecording" position="center" :mask-click-able="false">
<view class="recording-popup">
<view class="recording-wave">
<!-- 波形动画 -->
<view class="wave-animation"></view>
</view>
<view class="recording-text">
{{ isSlideToText ? '松开发送 转文字' : '松开发送' }}
</view>
<view class="recording-cancel">
取消
</view>
</view>
</uni-popup>
<VoiceResultPopup ref="voiceResultPopupRef" :voice-text="voiceText" @cancel="cancelVoice" @sendVoice="handleSendVoice"
@sendText="handleSendText" />
<!-- 语音结果界面 -->
<uni-popup v-model:show="showVoiceResult" position="center" :mask-click-able="false">
<view class="voice-result-popup">
<view class="voice-result-bubble">
{{ voiceText }}
</view>
<view class="voice-result-actions">
<view class="action-button cancel" @click="cancelVoice">取消</view>
<view class="action-button voice">发送原语音</view>
<view class="action-button send" @click="sendVoiceMessage">发送</view>
</view>
</view>
</uni-popup>
</template> </template>
<script setup> <script setup>
import { ref, watch, nextTick, onMounted } from 'vue' import { ref, watch, nextTick, onMounted, onUnmounted } from 'vue'
import RecordingPopup from '@/components/Speech/RecordingPopup.vue'
import VoiceResultPopup from '@/components/Speech/VoiceResultPopup.vue'
const props = defineProps({ const props = defineProps({
modelValue: String, modelValue: String,
@ -69,8 +104,6 @@ const recordingTimer = ref(null)
const voiceText = ref('') const voiceText = ref('')
const showVoiceResult = ref(false) const showVoiceResult = ref(false)
const isSlideToText = ref(false) const isSlideToText = ref(false)
const recordingPopupRef = ref(null)
const voiceResultPopupRef = ref(null)
// //
@ -93,19 +126,15 @@ const toggleVoiceMode = () => {
// //
const startRecording = () => { const startRecording = () => {
console.log('startRecording')
isRecording.value = true isRecording.value = true
return
recordingTime.value = 0 recordingTime.value = 0
// //
recordingTimer.value = setInterval(() => { recordingTimer.value = setInterval(() => {
recordingTime.value += 1 recordingTime.value += 1
}, 1000) }, 1000)
//
if (recordingPopupRef.value) {
recordingPopupRef.value.open()
}
// uni-appAPI // uni-appAPI
uni.startRecord({ uni.startRecord({
success: (res) => { success: (res) => {
@ -116,39 +145,22 @@ const startRecording = () => {
setTimeout(() => { setTimeout(() => {
voiceText.value = '这是语音转文字的结果' voiceText.value = '这是语音转文字的结果'
showVoiceResult.value = true showVoiceResult.value = true
//
if (voiceResultPopupRef.value) {
voiceResultPopupRef.value.open()
}
}, 1000) }, 1000)
}, },
fail: (err) => { fail: (err) => {
console.error('录音失败:', err) console.error('录音失败:', err)
isRecording.value = false isRecording.value = false
clearInterval(recordingTimer.value) clearInterval(recordingTimer.value)
if (recordingPopupRef.value) {
recordingPopupRef.value.close()
}
} }
}) })
} }
//
const handleRecordingCancel = () => {
isRecording.value = false
clearInterval(recordingTimer.value)
uni.stopRecord()
}
// //
const stopRecording = () => { const stopRecording = () => {
isRecording.value = false isRecording.value = false
clearInterval(recordingTimer.value)
// return
if (recordingPopupRef.value) { clearInterval(recordingTimer.value)
recordingPopupRef.value.close()
}
// 1 // 1
if (recordingTime.value < 1) { if (recordingTime.value < 1) {
@ -160,28 +172,6 @@ const stopRecording = () => {
uni.stopRecord() uni.stopRecord()
} }
//
const handleSendVoice = (data) => {
//
emit('sendVoice', {
text: data.text,
//
})
showVoiceResult.value = false
isVoiceMode.value = false
}
//
const handleSendText = (data) => {
//
emit('sendVoice', {
text: data.text,
//
})
showVoiceResult.value = false
isVoiceMode.value = false
}
// //
const handleTouchMove = (e) => { const handleTouchMove = (e) => {
// //
@ -191,39 +181,23 @@ const handleTouchMove = (e) => {
isSlideToText.value = touchY < 200 isSlideToText.value = touchY < 200
} }
//
const sendVoiceMessage = () => {
//
emit('sendVoice', {
text: voiceText.value,
//
})
showVoiceResult.value = false
isVoiceMode.value = false
}
// //
const cancelVoice = () => { const cancelVoice = () => {
showVoiceResult.value = false showVoiceResult.value = false
isVoiceMode.value = false isVoiceMode.value = false
} }
//
const testPopup = () => {
//
isRecording.value = true
console.log("===========1")
if (recordingPopupRef.value) {
console.log("===========2")
recordingPopupRef.value.open()
}
// 2
setTimeout(() => {
if (recordingPopupRef.value) {
recordingPopupRef.value.close()
}
voiceText.value = '测试语音转文字结果'
showVoiceResult.value = true
if (voiceResultPopupRef.value) {
voiceResultPopupRef.value.open()
}
}, 3000)
}
// //
onMounted(() => { onMounted(() => {
// //
@ -238,11 +212,6 @@ onMounted(() => {
}) })
const sendMessage = () => { const sendMessage = () => {
if (isVoiceMode.value) {
testPopup()
return
}
if (props.isSessionActive) { if (props.isSessionActive) {
// //
if (props.stopRequest) { if (props.stopRequest) {
@ -304,12 +273,12 @@ defineExpose({
</script> </script>
<style scoped lang="scss"> <style scoped lang="scss">
.area-input { .area-input {
display: flex; display: flex;
align-items: center; align-items: center;
border-radius: 22px; border-radius: 22px;
background-color: #FFFFFF; background-color: #FFFFFF;
box-shadow: 0px 0px 20px 0px rgba(52, 25, 204, 0.05); box-shadow: 0px 0px 20px 0px rgba(52,25,204,0.05);
margin: 0 12px; margin: 0 12px;
/* 确保输入框在安全区域内 */ /* 确保输入框在安全区域内 */
margin-bottom: 8px; margin-bottom: 8px;
@ -393,9 +362,97 @@ defineExpose({
.textarea::-webkit-input-placeholder { .textarea::-webkit-input-placeholder {
color: #CCCCCC; color: #CCCCCC;
} }
.textarea:focus { .textarea:focus {
outline: none; outline: none;
} }
} }
/* 录音弹窗样式 */
.recording-popup {
width: 280px;
height: 280px;
background-color: rgba(0, 0, 0, 0.7);
border-radius: 20px;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
color: white;
}
.recording-wave {
width: 160px;
height: 160px;
border-radius: 50%;
background-color: rgba(76, 217, 100, 0.3);
display: flex;
justify-content: center;
align-items: center;
margin-bottom: 20px;
}
.wave-animation {
width: 100px;
height: 100px;
/* 这里可以添加波形动画 */
background-image: url('/static/wave_icon.png');
background-size: contain;
background-repeat: no-repeat;
background-position: center;
}
.recording-text {
font-size: 16px;
margin-bottom: 20px;
}
.recording-cancel {
font-size: 14px;
color: #CCCCCC;
}
/* 语音结果弹窗样式 */
.voice-result-popup {
width: 300px;
background-color: white;
border-radius: 16px;
padding: 20px;
}
.voice-result-bubble {
background-color: #4CD964;
color: white;
padding: 15px;
border-radius: 18px;
border-top-left-radius: 4px;
margin-bottom: 20px;
min-height: 60px;
font-size: 16px;
}
.voice-result-actions {
display: flex;
justify-content: space-between;
}
.action-button {
padding: 8px 16px;
border-radius: 20px;
font-size: 14px;
}
.cancel {
color: #666666;
background-color: #F5F5F5;
}
.voice {
color: #007AFF;
background-color: #E8F0FE;
}
.send {
color: white;
background-color: #007AFF;
}
</style> </style>

View File

@ -103,7 +103,7 @@
import CreateServiceOrder from '@/components/CreateServiceOrder/index.vue' import CreateServiceOrder from '@/components/CreateServiceOrder/index.vue'
import { agentChatStream, stopAbortTask } from '@/request/api/AgentChatStream'; import { agentChatStream } from '@/request/api/AgentChatStream';
import { mainPageData } from '@/request/api/MainPageDataApi'; import { mainPageData } from '@/request/api/MainPageDataApi';
import { conversationMsgList, recentConversation } from '@/request/api/ConversationApi'; import { conversationMsgList, recentConversation } from '@/request/api/ConversationApi';
@ -145,6 +145,8 @@
// //
const isSessionActive = ref(false); const isSessionActive = ref(false);
//
const requestTaskRef = ref(null);
/// ///
let commonType = '' let commonType = ''
@ -355,8 +357,7 @@
conversationId: conversationId.value, conversationId: conversationId.value,
agentId: agentId.value, agentId: agentId.value,
messageType: isInstruct ? 1 : 0, messageType: isInstruct ? 1 : 0,
messageContent: isInstruct ? commonType : message, messageContent: isInstruct ? commonType : message
messageId: 'mid' + new Date().getTime()
} }
// AI // AI
@ -389,8 +390,8 @@
} }
// //
const promise = agentChatStream(args, (chunk) => { const { promise, requestTask } = agentChatStream(args, (chunk) => {
// console.log(':', chunk) console.log('分段内容:', chunk)
if (chunk && chunk.error) { if (chunk && chunk.error) {
chatMsgList.value[aiMsgIndex].msg = '请求错误,请重试'; chatMsgList.value[aiMsgIndex].msg = '请求错误,请重试';
clearInterval(loadingTimer); clearInterval(loadingTimer);
@ -455,6 +456,8 @@
} }
}) })
//
requestTaskRef.value = requestTask;
// Promise/ // Promise/
promise.then(data => { promise.then(data => {
@ -484,7 +487,11 @@
// //
const stopRequest = () => { const stopRequest = () => {
stopAbortTask() if (requestTaskRef.value && requestTaskRef.value.abort) {
//
requestTaskRef.value.isAborted = true;
//
requestTaskRef.value.abort();
// //
isSessionActive.value = false; isSessionActive.value = false;
const msg = chatMsgList.value[currentAIMsgIndex].msg; const msg = chatMsgList.value[currentAIMsgIndex].msg;
@ -501,6 +508,9 @@
typeWriterTimer = null; typeWriterTimer = null;
} }
setTimeoutScrollToBottom() setTimeoutScrollToBottom()
//
requestTaskRef.value = null;
}
} }
</script> </script>

View File

@ -88,8 +88,8 @@
z-index: 1; z-index: 1;
transition: padding-bottom 0.3s ease; transition: padding-bottom 0.3s ease;
/* 确保输入区域始终可见 */ /* 确保输入区域始终可见 */
// transform: translateZ(0); transform: translateZ(0);
// -webkit-transform: translateZ(0); -webkit-transform: translateZ(0);
} }
.area-input { .area-input {

View File

@ -9,72 +9,13 @@ const API = '/agent/assistant/chat';
* @param {Function} onChunk 回调每收到一段数据触发 * @param {Function} onChunk 回调每收到一段数据触发
* @returns {Object} 包含Promise和requestTask的对象 * @returns {Object} 包含Promise和requestTask的对象
*/ */
let requestTask = null; function agentChatStream(params, onChunk) {
let isAborted = false; // 添加终止状态标志 let requestTask;
let currentPromiseReject = null; // 保存当前Promise的reject函数
let requestId = 0; // 请求ID用于区分不同的请求
const stopAbortTask = () => {
console.log('🛑 开始强制终止请求...');
isAborted = true; // 立即设置终止标志
// 立即拒绝当前Promise最强制的终止
if (currentPromiseReject) {
console.log('🛑 立即拒绝Promise');
currentPromiseReject(new Error('请求已被用户终止'));
currentPromiseReject = null;
}
if (requestTask) {
// 先取消所有监听器关键必须在abort之前
try {
if (requestTask.offChunkReceived) {
requestTask.offChunkReceived();
console.log('🛑 已取消 ChunkReceived 监听');
}
} catch (e) {
console.log('🛑 取消 ChunkReceived 监听失败:', e);
}
try {
if (requestTask.offHeadersReceived) {
requestTask.offHeadersReceived();
console.log('🛑 已取消 HeadersReceived 监听');
}
} catch (e) {
console.log('🛑 取消 HeadersReceived 监听失败:', e);
}
// 然后终止网络请求
try {
if (requestTask.abort) {
requestTask.abort();
console.log('🛑 已终止网络请求');
}
} catch (e) {
console.log('🛑 终止网络请求失败:', e);
}
requestTask = null;
}
// 递增请求ID使旧请求的数据无效
requestId++;
console.log('🛑 请求强制终止完成新请求ID:', requestId);
}
const agentChatStream = (params, onChunk) => {
const promise = new Promise((resolve, reject) => { const promise = new Promise((resolve, reject) => {
const token = uni.getStorageSync('token'); const token = uni.getStorageSync('token');
let hasError = false; let hasError = false;
isAborted = false; // 重置终止状态
// 保存当前Promise的reject函数用于强制终止 console.log("发送请求内容: ", params)
currentPromiseReject = reject;
// 为当前请求分配ID
const currentRequestId = ++requestId;
console.log("🚀 发送请求内容: ", params, "请求ID:", currentRequestId)
// #ifdef MP-WEIXIN // #ifdef MP-WEIXIN
requestTask = uni.request({ requestTask = uni.request({
url: BASE_URL + API, // 替换为你的接口地址 url: BASE_URL + API, // 替换为你的接口地址
@ -88,62 +29,38 @@ const agentChatStream = (params, onChunk) => {
}, },
responseType: 'arraybuffer', responseType: 'arraybuffer',
success(res) { success(res) {
if (!isAborted && requestId === currentRequestId) {
console.log("✅ 请求成功ID:", currentRequestId);
resolve(res.data); resolve(res.data);
} else {
console.log("❌ 请求已过期或终止忽略success回调当前ID:", requestId, "请求ID:", currentRequestId);
}
}, },
fail(err) { fail(err) {
if (!isAborted && requestId === currentRequestId) { console.log("====> ", JSON.stringify(err))
console.log("❌ 请求失败ID:", currentRequestId, "错误:", JSON.stringify(err));
reject(err); reject(err);
} else {
console.log("❌ 请求已过期或终止忽略fail回调当前ID:", requestId, "请求ID:", currentRequestId);
}
}, },
complete(res) { complete(res) {
if (!isAborted && requestId === currentRequestId && res.statusCode !== 200) { if(res.statusCode !== 200) {
console.log("❌ 请求完成但状态错误ID:", currentRequestId, "状态:", res.statusCode); console.log("====> ", JSON.stringify(res))
if (onChunk) { if (onChunk) {
onChunk({ error: true, message: '服务器错误', detail: res }); onChunk({ error: true, message: '服务器错误', detail: res });
} }
reject(res); reject(res);
} else if (requestId !== currentRequestId) {
console.log("❌ 请求已过期或终止忽略complete回调当前ID:", requestId, "请求ID:", currentRequestId);
} }
} }
}); });
requestTask.onHeadersReceived(res => { requestTask.onHeadersReceived(res => {
// 检查请求是否已终止或过期 console.log('onHeadersReceived', res);
if (isAborted || requestId !== currentRequestId) {
console.log('🚫 Headers已终止或过期忽略当前ID:', requestId, '请求ID:', currentRequestId);
return;
}
console.log('📡 onHeadersReceivedID:', currentRequestId, res);
const status = res.statusCode || (res.header && res.header.statusCode); const status = res.statusCode || (res.header && res.header.statusCode);
if (status && status !== 200) { if (status && status !== 200) {
hasError = true; hasError = true;
if (onChunk && !isAborted && requestId === currentRequestId) { if (onChunk) {
onChunk({ error: true, message: `服务器错误(${status})`, detail: res }); onChunk({ error: true, message: `服务器错误(${status})`, detail: res });
} }
if (requestTask && requestTask.abort) { requestTask.abort && requestTask.abort();
requestTask.abort();
}
} }
}); });
requestTask.onChunkReceived(res => { requestTask.onChunkReceived(res => {
// 第一道防线立即检查请求ID和终止状态 // 检查请求是否已被中止
if (isAborted || hasError || requestTask === null || requestId !== currentRequestId) { if (hasError || requestTask.isAborted) return;
console.log('🚫 数据块已终止或过期,忽略 - 第一道检查当前ID:', requestId, '请求ID:', currentRequestId);
return;
}
console.log("📦 onChunkReceivedID:", currentRequestId, res)
const base64 = uni.arrayBufferToBase64(res.data); const base64 = uni.arrayBufferToBase64(res.data);
let data = ''; let data = '';
try { try {
@ -152,28 +69,18 @@ const agentChatStream = (params, onChunk) => {
// 某些平台可能不支持 atob可以直接用 base64 // 某些平台可能不支持 atob可以直接用 base64
data = base64; data = base64;
} }
// 第二道防线:解析前再次检查
if (isAborted || hasError || requestTask === null || requestId !== currentRequestId) {
console.log('🚫 数据块已终止或过期,忽略 - 第二道检查当前ID:', requestId, '请求ID:', currentRequestId);
return;
}
const messages = parseSSEChunk(data); const messages = parseSSEChunk(data);
messages.forEach(msg => { messages.forEach(msg => {
// 第三道防线:每个消息处理前都检查 if (onChunk) onChunk(msg);
if (onChunk && !isAborted && !hasError && requestTask !== null && requestId === currentRequestId) {
console.log(`parseSSEChunk ${currentRequestId}:`, msg)
onChunk(msg);
} else {
console.log('🚫 消息已终止或过期忽略处理当前ID:', requestId, '请求ID:', currentRequestId);
}
}); });
}); });
// #endif // #endif
}); });
return promise return {
promise,
requestTask
};
} }
// window.atob兼容性处理 // window.atob兼容性处理
@ -245,4 +152,4 @@ function parseSSEChunk(raw) {
return results; return results;
} }
export { agentChatStream, stopAbortTask } export { agentChatStream }