feat: provide closeStream (#6659)

Upstream https://github.com/toeverything/blocksuite/pull/6836
This commit is contained in:
regischen 2024-04-22 14:35:12 +00:00
parent 6ec97b27c4
commit a20a3fbbf8
No known key found for this signature in database
GPG Key ID: 46C9E26A75AB276C
2 changed files with 14 additions and 1 deletions

View File

@ -100,13 +100,15 @@ export class CopilotClient {
async chatText({
sessionId,
messageId,
signal,
}: {
sessionId: string;
messageId: string;
signal?: AbortSignal;
}) {
const url = new URL(`${this.backendUrl}/api/copilot/chat/${sessionId}`);
url.searchParams.set('messageId', messageId);
const response = await fetch(url.toString());
const response = await fetch(url.toString(), { signal });
return response.text();
}

View File

@ -28,6 +28,7 @@ export type TextToTextOptions = {
params?: Record<string, string>;
timeout?: number;
stream?: boolean;
signal?: AbortSignal;
};
export function createChatSession({
@ -102,6 +103,7 @@ export function textToText({
params,
sessionId,
stream,
signal,
timeout = TIMEOUT,
}: TextToTextOptions) {
if (stream) {
@ -120,6 +122,15 @@ export function textToText({
sessionId: message.sessionId,
messageId: message.messageId,
});
if (signal) {
if (signal.aborted) {
eventSource.close();
return;
}
signal.onabort = () => {
eventSource.close();
};
}
for await (const event of toTextStream(eventSource, { timeout })) {
if (event.type === 'message') {
yield event.data;