aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/nodes/chatbot/response_parsers/StreamedAnswerParser.ts
diff options
context:
space:
mode:
authorNathan-SR <144961007+Nathan-SR@users.noreply.github.com>2025-03-11 17:43:05 +0100
committerNathan-SR <144961007+Nathan-SR@users.noreply.github.com>2025-03-11 17:43:05 +0100
commitfa937182bc93aa2c6faadda80ea998cdfd479b4e (patch)
treecba8e16edcccc6fd2932173484ac444cb79abea2 /src/client/views/nodes/chatbot/response_parsers/StreamedAnswerParser.ts
parentcf91c46cfec6e3e36b9184764016f9c1b5c997d4 (diff)
parent04669ffeb163688c7aefd7b5face7998252abdca (diff)
Merge branch 'master' of https://github.com/brown-dash/Dash-Web into DocCreatorMenu-work
Diffstat (limited to 'src/client/views/nodes/chatbot/response_parsers/StreamedAnswerParser.ts')
-rw-r--r--src/client/views/nodes/chatbot/response_parsers/StreamedAnswerParser.ts79
1 files changed, 79 insertions, 0 deletions
diff --git a/src/client/views/nodes/chatbot/response_parsers/StreamedAnswerParser.ts b/src/client/views/nodes/chatbot/response_parsers/StreamedAnswerParser.ts
new file mode 100644
index 000000000..dbd568faa
--- /dev/null
+++ b/src/client/views/nodes/chatbot/response_parsers/StreamedAnswerParser.ts
@@ -0,0 +1,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;
+ }
+}