misc: polish first batch submit logic

This commit is contained in:
WJG 2024-02-25 11:49:16 +08:00
parent 7f464aa9cb
commit af7fedc079
No known key found for this signature in database
GPG Key ID: 258474EF8590014A
2 changed files with 11 additions and 10 deletions

View File

@ -104,7 +104,8 @@ export class StreamResponse {
private _chunks: string[] = [];
private _tempText = "";
private _remainingText: string = "";
private _preSubmitTimestamp = 0;
private _isFirstSubmit = true;
private _submitCount = 0;
private _batchSubmitImmediately() {
if (this._tempText) {
@ -121,18 +122,19 @@ export class StreamResponse {
*/
private _batchSubmit(text: string) {
this._tempText += text;
const isFirstSubmit = this._preSubmitTimestamp === 0;
if (isFirstSubmit) {
this._preSubmitTimestamp = Date.now();
if (this._isFirstSubmit) {
this._isFirstSubmit = false;
// 达到首次消息收集时长后,批量提交消息
setTimeout(() => {
// 当消息长度积攒到一定长度,或达到一定时间间隔后,批量提交消息
if (
this._tempText.length > this.maxSentenceLength ||
Date.now() - this._preSubmitTimestamp > this.firstSubmitTimeout
) {
if (this._submitCount < 1) {
this._batchSubmitImmediately();
}
}, this.firstSubmitTimeout);
} else if (this._submitCount < 1) {
// 当首次消息积攒到一定长度后,也批量提交消息
if (this._tempText.length > this.maxSentenceLength) {
this._batchSubmitImmediately();
}
}
}

View File

@ -77,7 +77,6 @@ async function testSpeakerStreamResponse(speaker: AISpeaker) {
`地球在赤道处稍微膨胀,而在极地处稍微收缩,最终形成一个近似于球体的形状。因此,地球是圆的`
);
await add(`主要原因是由于地球的引力和自转共同作用所致。`);
await sleep(10 * 1000);
console.log("finished!");
stream.finish();
});