aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/nodes/ChatBox/StreamedAnswerParser.ts
blob: 3585cab4aad4caa549cfc03352e1f59693edf1d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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;
    }
}