Files
YGChatCS/request/api/AgentChatStream.js
2025-08-05 22:46:17 +08:00

153 lines
4.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { BASE_URL } from "../../constant/base";
/// 请求流式数据的API
const API = '/agent/assistant/chat';
/**
* 获取AI聊天流式信息仅微信小程序支持
* @param {Object} params 请求参数
* @param {Function} onChunk 回调,每收到一段数据触发
* @returns {Promise}
*/
function agentChatStream(params, onChunk) {
return new Promise((resolve, reject) => {
const token = uni.getStorageSync('token');
let hasError = false;
console.log("发送请求内容: ", params)
// #ifdef MP-WEIXIN
const requestTask = uni.request({
url: BASE_URL + API, // 替换为你的接口地址
method: 'POST',
data: params,
enableChunked: true,
header: {
Accept: 'text/event-stream',
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`, // 如需token可加
},
responseType: 'arraybuffer',
success(res) {
resolve(res.data);
},
fail(err) {
console.log("====> ", JSON.stringify(err))
reject(err);
},
complete(res) {
if(res.statusCode !== 200) {
console.log("====> ", JSON.stringify(res))
if (onChunk) {
onChunk({ error: true, message: '服务器错误', detail: res });
}
reject(res);
}
}
});
requestTask.onHeadersReceived(res => {
console.log('onHeadersReceived', res);
const status = res.statusCode || (res.header && res.header.statusCode);
if (status && status !== 200) {
hasError = true;
if (onChunk) {
onChunk({ error: true, message: `服务器错误(${status})`, detail: res });
}
requestTask.abort && requestTask.abort();
}
});
requestTask.onChunkReceived(res => {
if (hasError) return;
const base64 = uni.arrayBufferToBase64(res.data);
let data = '';
try {
data = decodeURIComponent(escape(weAtob(base64)));
} catch (e) {
// 某些平台可能不支持 atob可以直接用 base64
data = base64;
}
const messages = parseSSEChunk(data);
messages.forEach(msg => {
if (onChunk) onChunk(msg);
});
});
// #endif
});
}
// window.atob兼容性处理
const weAtob = (string) => {
const b64re =
/^(?:[A-Za-z\d+/]{4})*?(?:[A-Za-z\d+/]{2}(?:==)?|[A-Za-z\d+/]{3}=?)?$/;
const b64 =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
// 去除空白字符
string = String(string).replace(/[\t\n\f\r ]+/g, '');
// 验证 Base64 编码
if (!b64re.test(string)) {
throw new TypeError(
// eslint-disable-next-line quotes
"Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded."
);
}
// 填充字符
string += '=='.slice(2 - (string.length & 3));
let bitmap,
result = '',
r1,
r2,
i = 0;
for (; i < string.length;) {
bitmap =
(b64.indexOf(string.charAt(i++)) << 18) |
(b64.indexOf(string.charAt(i++)) << 12) |
((r1 = b64.indexOf(string.charAt(i++))) << 6) |
(r2 = b64.indexOf(string.charAt(i++)));
if (r1 === 64) {
result += String.fromCharCode((bitmap >> 16) & 255);
} else if (r2 === 64) {
result += String.fromCharCode(
(bitmap >> 16) & 255,
(bitmap >> 8) & 255
);
} else {
result += String.fromCharCode(
(bitmap >> 16) & 255,
(bitmap >> 8) & 255,
bitmap & 255
);
}
}
return result;
};
// 解析SSE分段数据
function parseSSEChunk(raw) {
// 拆分为多段
const lines = raw.split('\n\n');
const results = [];
lines.forEach(line => {
// 只处理包含 data: 的行
const dataMatch = line.match(/data:(\{.*\})/);
if (dataMatch && dataMatch[1]) {
try {
const obj = JSON.parse(dataMatch[1]);
results.push(obj);
} catch (e) {
// 解析失败忽略
}
}
});
return results;
}
export { agentChatStream }