Skip to content

Commit

Permalink
fix: cancel ai chatStream request when cancel button is clicked
Browse files Browse the repository at this point in the history
  • Loading branch information
coetzeexu committed Dec 9, 2024
1 parent f157b9b commit 7087a58
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions packages/libro-ai-native/src/ai-native-for-cell-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export function LibroAINativeForCellRender() {
className={`libro-ai-native-for-cell-cancel-btn ${msgItem?.state === AnswerState.FAIL ? 'error' : ''}`}
onClick={() => {
instance.showAI = false;
instance.cancelRequest();
if (!libroAINativeService.cellAIChatMap.get(instance.cell.id)) {
cancelCellAIClassname(instance.cell);
}
Expand Down Expand Up @@ -89,6 +90,7 @@ export class LibroAINativeForCellView extends BaseView {
@prop()
libroAIChatMessageItemModel?: LibroAIChatMessageItemModel;

private requestAbortController: AbortController;
@prop()
showAI = false;
constructor(@inject(ViewOption) options: IAINativeForCellViewOption) {
Expand All @@ -97,9 +99,23 @@ export class LibroAINativeForCellView extends BaseView {
this.cell = options.cell;
}

private initAbortController() {
const abortController = new AbortController();
this.requestAbortController = abortController;
}

cancelRequest = () => {
if (this.requestAbortController) {
this.requestAbortController.abort();
}
};

chatStream = async (option: IChatMessage) => {
const { chat_key, content, language } = option;

this.initAbortController();

const signal = this.requestAbortController.signal;
const url = `/libro/api/chatstream`;
const msg = {
chat_key: chat_key,
Expand All @@ -112,6 +128,7 @@ export class LibroAINativeForCellView extends BaseView {
},
responseType: 'stream',
adapter: 'fetch',
signal: signal,
});
if (res.status === 200) {
let reader;
Expand Down

0 comments on commit 7087a58

Please sign in to comment.