@coze/uniapp-api
Version:
Official Coze UniApp SDK for seamless AI integration into your applications | 扣子官方 UniApp SDK,助您轻松集成 AI 能力到应用中
138 lines (137 loc) • 4.8 kB
JavaScript
"use strict";
/* eslint-disable @typescript-eslint/no-magic-numbers -- ignore */
/* eslint-disable security/detect-object-injection -- ignore */
Object.defineProperty(exports, "__esModule", { value: true });
exports.EventSource = void 0;
const decoder_1 = require("../helpers/decoder");
const types_1 = require("./types");
const base_1 = require("./base");
/**
* 为微信小程序处理流式请求
* Process streaming requests for WeChat Mini Program
*/
class EventSource extends base_1.BaseEventSource {
constructor(options) {
super();
Object.defineProperty(this, "options", {
enumerable: true,
configurable: true,
writable: true,
value: options
});
Object.defineProperty(this, "task", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "isAborted", {
enumerable: true,
configurable: true,
writable: true,
value: false
});
Object.defineProperty(this, "cacheChunk", {
enumerable: true,
configurable: true,
writable: true,
value: ''
});
}
start() {
const { url, method, headers, data, timeout } = this.options;
// https://uniapp.dcloud.net.cn/api/request/request.html
this.task = uni.request({
url,
method: method,
header: headers,
data: JSON.stringify(data),
enableChunked: true,
enableCookie: true,
withCredentials: true,
responseType: 'arraybuffer',
timeout,
fail: err => {
this.trigger(types_1.EventName.Fail, { errMsg: err.errMsg });
},
success: res => {
if (res.statusCode !== 200) {
this.trigger(types_1.EventName.Fail, { errMsg: res.errMsg, data: res });
}
else {
this.trigger(types_1.EventName.Success, { data: res });
}
},
});
this.onHeadersReceived();
this.onChunkReceived();
}
abort() {
var _a;
if (!this.isAborted) {
(_a = this.task) === null || _a === void 0 ? void 0 : _a.abort();
this.isAborted = true;
}
}
onHeadersReceived() {
var _a;
(_a = this.task) === null || _a === void 0 ? void 0 : _a.onHeadersReceived(res => {
this.trigger(types_1.EventName.Open, { data: res });
});
}
onChunkReceived() {
var _a, _b;
// @ts-expect-error uniapp 类型定义不完整
(_b = (_a = this.task) === null || _a === void 0 ? void 0 : _a.onChunkReceived) === null || _b === void 0 ? void 0 : _b.call(_a, res => {
const octets = new Uint8Array(res.data);
const decoder = new decoder_1.TextDecoder();
const chunk = decoder.decode(octets);
// 检查请求是否失败
if (!this.cacheChunk) {
const chunkJson = this.safeParseJSON(chunk);
if (chunkJson && chunkJson.code) {
this.trigger(types_1.EventName.Fail, {
errMsg: chunkJson.msg,
data: chunkJson,
});
// 中断请求
this.abort();
return;
}
}
this.cacheChunk += chunk;
if (this.cacheChunk.endsWith('\n\n')) {
const chunkStr = this.cacheChunk;
this.cacheChunk = '';
const messages = [];
let message = {};
const lines = chunkStr.split('\n');
for (let i = 0; i < lines.length - 1; i++) {
const line = lines[i];
const index = line.indexOf(':');
if (index !== -1) {
const field = line.substring(0, index).trim();
const content = line.substring(index + 1).trim();
message[field] = content;
if (field === 'data') {
messages.push(message);
message = {};
}
}
}
messages.map(msg => {
this.trigger(types_1.EventName.Chunk, { data: msg });
});
}
});
}
safeParseJSON(str) {
try {
return JSON.parse(str);
}
catch (e) {
return null;
}
}
}
exports.EventSource = EventSource;