feat: provide closeStream (#6659)
Upstream https://github.com/toeverything/blocksuite/pull/6836
This commit is contained in:
@@ -100,13 +100,15 @@ export class CopilotClient {
|
|||||||
async chatText({
|
async chatText({
|
||||||
sessionId,
|
sessionId,
|
||||||
messageId,
|
messageId,
|
||||||
|
signal,
|
||||||
}: {
|
}: {
|
||||||
sessionId: string;
|
sessionId: string;
|
||||||
messageId: string;
|
messageId: string;
|
||||||
|
signal?: AbortSignal;
|
||||||
}) {
|
}) {
|
||||||
const url = new URL(`${this.backendUrl}/api/copilot/chat/${sessionId}`);
|
const url = new URL(`${this.backendUrl}/api/copilot/chat/${sessionId}`);
|
||||||
url.searchParams.set('messageId', messageId);
|
url.searchParams.set('messageId', messageId);
|
||||||
const response = await fetch(url.toString());
|
const response = await fetch(url.toString(), { signal });
|
||||||
return response.text();
|
return response.text();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ export type TextToTextOptions = {
|
|||||||
params?: Record<string, string>;
|
params?: Record<string, string>;
|
||||||
timeout?: number;
|
timeout?: number;
|
||||||
stream?: boolean;
|
stream?: boolean;
|
||||||
|
signal?: AbortSignal;
|
||||||
};
|
};
|
||||||
|
|
||||||
export function createChatSession({
|
export function createChatSession({
|
||||||
@@ -102,6 +103,7 @@ export function textToText({
|
|||||||
params,
|
params,
|
||||||
sessionId,
|
sessionId,
|
||||||
stream,
|
stream,
|
||||||
|
signal,
|
||||||
timeout = TIMEOUT,
|
timeout = TIMEOUT,
|
||||||
}: TextToTextOptions) {
|
}: TextToTextOptions) {
|
||||||
if (stream) {
|
if (stream) {
|
||||||
@@ -120,6 +122,15 @@ export function textToText({
|
|||||||
sessionId: message.sessionId,
|
sessionId: message.sessionId,
|
||||||
messageId: message.messageId,
|
messageId: message.messageId,
|
||||||
});
|
});
|
||||||
|
if (signal) {
|
||||||
|
if (signal.aborted) {
|
||||||
|
eventSource.close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
signal.onabort = () => {
|
||||||
|
eventSource.close();
|
||||||
|
};
|
||||||
|
}
|
||||||
for await (const event of toTextStream(eventSource, { timeout })) {
|
for await (const event of toTextStream(eventSource, { timeout })) {
|
||||||
if (event.type === 'message') {
|
if (event.type === 'message') {
|
||||||
yield event.data;
|
yield event.data;
|
||||||
|
|||||||
Reference in New Issue
Block a user