aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/nodes/ChatBox/response_parsers/StreamedAnswerParser.ts
diff options
context:
space:
mode:
authorA.J. Shulman <Shulman.aj@gmail.com>2024-09-07 12:43:05 -0400
committerA.J. Shulman <Shulman.aj@gmail.com>2024-09-07 12:43:05 -0400
commit4791cd23af08da70895204a3a7fbaf889d9af2d5 (patch)
treec4c2534e64724d62bae9152763f1a74cd5a963e0 /src/client/views/nodes/ChatBox/response_parsers/StreamedAnswerParser.ts
parent210f8f5f1cd19e9416a12524cce119b273334fd3 (diff)
completely restructured, added comments, and significantly reduced the length of the prompt (~72% shorter and cheaper)
Diffstat (limited to 'src/client/views/nodes/ChatBox/response_parsers/StreamedAnswerParser.ts')
-rw-r--r--src/client/views/nodes/ChatBox/response_parsers/StreamedAnswerParser.ts73
1 files changed, 0 insertions, 73 deletions
diff --git a/src/client/views/nodes/ChatBox/response_parsers/StreamedAnswerParser.ts b/src/client/views/nodes/ChatBox/response_parsers/StreamedAnswerParser.ts
deleted file mode 100644
index 3585cab4a..000000000
--- a/src/client/views/nodes/ChatBox/response_parsers/StreamedAnswerParser.ts
+++ /dev/null
@@ -1,73 +0,0 @@
-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;
- }
-}