openreplay/frontend/app/mstore/aiSummaryStore.ts
Delirium e0799f74e1
feat(ui): ai summary UI (#1868)
* feat(ui): start ai summary UI

* feat(ui): add api

* feat(ui): rm console log

* feat(ui): style fix

* feat(ui): some ui changes

* feat(ui): some ui changes

* feat(ui): some text formatting

* fix(ui): method fix

* fix(ui): fix icon gen

* fix(ui): fix global ts declaration for window env
2024-02-12 15:34:43 +01:00

59 lines
1.4 KiB
TypeScript

import { aiService } from 'App/services';
import { makeAutoObservable } from 'mobx';
export default class AiSummaryStore {
text = '';
constructor() {
makeAutoObservable(this);
}
setText(text: string) {
this.text = text;
}
appendText(text: string) {
this.text += text;
}
getSummary = async (sessionId: string) => {
this.setText('');
const respBody = await aiService.getSummary(sessionId);
if (!respBody) return;
const reader = respBody.getReader();
let lastIncompleteWord = '';
const processTextChunk = (textChunk: string) => {
textChunk = lastIncompleteWord + textChunk;
const words = textChunk.split(' ');
lastIncompleteWord = words.pop() || '';
words.forEach((word) => {
if(word) this.appendText(word + ' ');
});
};
try {
while (true) {
const { done, value } = await reader.read();
if (done) {
// Processing any remaining incomplete word at the end of the stream
if (lastIncompleteWord) {
this.appendText(lastIncompleteWord + ' ');
}
break;
}
let textChunk = new TextDecoder().decode(value, { stream: true });
if (this.text === '') {
textChunk = textChunk.trimStart()
}
processTextChunk(textChunk);
}
} catch (error) {
console.log(error);
}
};
}