aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/nodes/ChatBox/StreamedAnswerParser.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/client/views/nodes/ChatBox/StreamedAnswerParser.ts')
-rw-r--r--src/client/views/nodes/ChatBox/StreamedAnswerParser.ts73
1 files changed, 73 insertions, 0 deletions
diff --git a/src/client/views/nodes/ChatBox/StreamedAnswerParser.ts b/src/client/views/nodes/ChatBox/StreamedAnswerParser.ts
new file mode 100644
index 000000000..3585cab4a
--- /dev/null
+++ b/src/client/views/nodes/ChatBox/StreamedAnswerParser.ts
@@ -0,0 +1,73 @@
+import { threadId } from 'worker_threads';
+
+enum ParserState {
+ Outside,
+ InGroundedText,
+ InNormalText,
+}
+
+export class StreamedAnswerParser {
+ private state: ParserState = ParserState.Outside;
+ private buffer: string = '';
+ private result: string = '';
+ private isStartOfLine: boolean = true;
+
+ public parse(char: string): string {
+ switch (this.state) {
+ case ParserState.Outside:
+ if (char === '<') {
+ this.buffer = '<';
+ } else if (char === '>') {
+ if (this.buffer.startsWith('<grounded_text')) {
+ this.state = ParserState.InGroundedText;
+ } else if (this.buffer.startsWith('<normal_text')) {
+ this.state = ParserState.InNormalText;
+ }
+ this.buffer = '';
+ } else {
+ this.buffer += char;
+ }
+ break;
+
+ case ParserState.InGroundedText:
+ case ParserState.InNormalText:
+ if (char === '<') {
+ this.buffer = '<';
+ } else if (this.buffer.startsWith('</grounded_text') && char === '>') {
+ this.state = ParserState.Outside;
+ this.buffer = '';
+ } else if (this.buffer.startsWith('</normal_text') && char === '>') {
+ this.state = ParserState.Outside;
+ this.buffer = '';
+ } else if (this.buffer.startsWith('<')) {
+ this.buffer += char;
+ } else {
+ this.processChar(char);
+ }
+ break;
+ }
+
+ return this.result.trim();
+ }
+
+ private processChar(char: string): void {
+ if (this.isStartOfLine && char === ' ') {
+ // Skip leading spaces
+ return;
+ }
+ if (char === '\n') {
+ this.result += char;
+ this.isStartOfLine = true;
+ } else {
+ this.result += char;
+ this.isStartOfLine = false;
+ }
+ }
+
+ public reset(): void {
+ this.state = ParserState.Outside;
+ this.buffer = '';
+ this.result = '';
+ this.isStartOfLine = true;
+ }
+}