blob: dbd568faae55fc71d06d926dcc494938367ded60 (
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
74
75
76
77
78
79
|
/**
* @file StreamedAnswerParser.ts
* @description This file defines the StreamedAnswerParser class, which parses incoming character streams
* to extract grounded or normal text based on the tags found in the input stream. It maintains state
* between grounded text and normal text sections, handling buffered input and ensuring proper text formatting
* for AI assistant responses.
*/
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;
}
}
|