Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sse.ts handles chunks errors #798

Closed
ningpengtao-coder opened this issue Jan 29, 2024 · 9 comments
Closed

sse.ts handles chunks errors #798

ningpengtao-coder opened this issue Jan 29, 2024 · 9 comments
Labels
bug Something isn't working

Comments

@ningpengtao-coder
Copy link

ningpengtao-coder commented Jan 29, 2024

sse.ts的parseStreamChunk方法未考虑chunk不完整的情况,导致不完整的chunk被丢弃(上半部分)。不完整的chunk(下半部分)会导致后续的chunk无法解析json。最终导致仅显示部分内容。

请考虑使用更加健壮的库处理sse( https://github.com/Azure/fetch-event-source.git )。

export class SSEParseData {
  storeReadData = '';
  storeEventName = '';

  parse(item: { event: string; data: string }) {
    if (item.data === '[DONE]') return { eventName: item.event, data: item.data };

    if (item.event) {
      this.storeEventName = item.event;
    }

    try {
      // 1.不完整的chunk + 完整的chunk
      const formatData = this.storeReadData + item.data;
	  
      // 2.解析错误
      const parseData = JSON.parse(formatData);
      const eventName = this.storeEventName;

      this.storeReadData = '';
      this.storeEventName = '';

      return {
        eventName,
        data: parseData
      };
    } catch (error) {
      if (typeof item.data === 'string' && !item.data.startsWith(': ping')) {
	    //3.不完整的chunk + 完整的chunk 1...n
        this.storeReadData += item.data;
      } else {
        this.storeReadData = '';
      }
    }
	// 4.后续chunk永远是空对象
    return {};
  }
}
@ningpengtao-coder ningpengtao-coder added the bug Something isn't working label Jan 29, 2024
@c121914yu c121914yu changed the title sse.ts处理chunks错误 sse.ts handles chunks errors Jan 29, 2024
@c121914yu
Copy link
Collaborator

catch里不是处理了么

@ningpengtao-coder
Copy link
Author

ningpengtao-coder commented Jan 30, 2024

@c121914yu catch仅考虑了排除ping开头的情况。我遇到的情况是chunk不完整的情况下上半部分丢失,导致parse后续所有的chunk被拼接到storeReadData并一直返回空对象,最终结果是界面上仅显示部分内容。
完整chunk:
event: answer
data: {"id":"","object":"","created":0,"model":"","choices":[{"delta":{"content":"电影"},"index":0,"finish_reason":null}]}
不完整chunk(上半丢失):
event: an
不完整chunk(下半):
swer
data: {"id":"","object":"","created":0,"model":"","choices":[{"delta":{"content":"电影"},"index":0,"finish_reason":null}]}

@c121914yu
Copy link
Collaborator

@c121914yu catch仅考虑了排除ping开头的情况。我遇到的情况是chunk不完整的情况。
完整chunk:
event: answer
data: {"id":"","object":"","created":0,"model":"","choices":[{"delta":{"content":"电影"},"index":0,"finish_reason":null}]}
不完整chunk(上半):
event: an
不完整chunk(下半):
swer
data: {"id":"","object":"","created":0,"model":"","choices":[{"delta":{"content":"电影"},"index":0,"finish_reason":null}]}

对的呀。不是 ping 开头就拼接

@ningpengtao-coder
Copy link
Author

@c121914yu 我更新了一下问题,麻烦你看一下。

@c121914yu
Copy link
Collaborator

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


@c121914yu I updated the question, please take a look.

@c121914yu
Copy link
Collaborator

@c121914yu 我更新了一下问题,麻烦你看一下。

了解了,是指丢包的情况?

@ningpengtao-coder
Copy link
Author

ningpengtao-coder commented Jan 30, 2024

@c121914yu 我更新了一下问题,麻烦你看一下。

了解了,是指丢包的情况?

不存在丢包问题,是一个chunk被分成两个部分(任何位置例如从event:ans开始切成两个部分,事件类型被破坏)。

@c121914yu
Copy link
Collaborator

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


@c121914yu I updated the question, please take a look.

Understood, does it mean packet loss?

There is no packet loss problem. It is a problem caused by a chunk being divided into two parts and the upper part not being placed in storeReadData.

@c121914yu
Copy link
Collaborator

eb5d5ed 这个commit优化了。微软的包很久没更新了,使用了chatNextWeb相同的包。
#804

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants