fixed streaming error leaving out some text in chat UI

pull/5241/head
STetsing 1 month ago
parent 6e5ed32f22
commit 707699bc23
  1. 2
      libs/remix-ai-core/src/helpers/streamHandler.ts
  2. 3
      libs/remix-ai-core/src/types/remix-project.code-workspace
  3. 45
      libs/remix-ai-core/src/types/types.ts
  4. 1
      libs/remix-ui/remix-ai/src/lib/components/Default.tsx

@ -19,7 +19,7 @@ export const HandleSimpleResponse = async (response,
}
export const HandleStreamResponse = async (streamResponse,
cb?: (streamText: string) => void,
cb: (streamText: string) => void,
done_cb?: (result: string) => void) => {
try {
let resultText = ''

@ -2,6 +2,9 @@
"folders": [
{
"path": "../../../.."
},
{
"path": "../../../../../remix-wildcard"
}
]
}

@ -88,6 +88,10 @@ export enum RemoteBackendOPModel{
MISTRAL
}
interface GeneratedTextObject {
generatedText: string;
isGenerating: boolean;
}
export class JsonStreamParser {
buffer: string
constructor() {
@ -97,33 +101,34 @@ export class JsonStreamParser {
safeJsonParse<T>(chunk: string): T[] | null {
this.buffer += chunk;
const results = [];
let startIndex = 0;
let endIndex: number;
while ((endIndex = this.buffer.indexOf('}', startIndex)) !== -1) {
// check if next character is a opening curly bracket
let modifiedEndIndex = endIndex;
if ((modifiedEndIndex = this.buffer.indexOf('{', endIndex)) !== -1 ) {
endIndex = modifiedEndIndex - 1;
}
// eslint-disable-next-line no-constant-condition
while (true) {
if (((modifiedEndIndex = this.buffer.indexOf('{', endIndex)) === -1) &&
(this.buffer.indexOf('}', endIndex) < this.buffer.length)) {
endIndex = this.buffer.indexOf('}', endIndex+1) <0 ? this.buffer.length - 1 : this.buffer.indexOf('}', endIndex+1);
}
const jsonStr = this.buffer.slice(startIndex, endIndex + 1);
try {
const result = JSON.parse(this.buffer);
results.push(result);
this.buffer = '';
break;
const obj: GeneratedTextObject = JSON.parse(jsonStr);
console.log('parsed:', obj);
results.push(obj);
} catch (error) {
// eslint-disable-next-line no-useless-escape
const match = /^([^\{]*\{[^\}]*\})(.*)/.exec(this.buffer);
if (match) {
try {
const result = JSON.parse(match[1]);
results.push(result);
this.buffer = match[2];
} catch (e) {
break;
}
} else {
break;
}
console.error('Error parsing JSON:', error);
}
startIndex = endIndex + 1;
}
this.buffer = this.buffer.slice(startIndex);
return results;
}
safeJsonParseSingle<T>(chunk: string): T[] | null {
return JSON.parse(this.buffer);
}

@ -29,6 +29,7 @@ export const Default = (props) => {
if (GenerationParams.return_stream_response) HandleStreamResponse(response,
(text) => {observer.next(text)},
(result) => {
observer.next(' ') // Add a space to flush
ChatHistory.pushHistory(prompt, result)
observer.complete()
}

Loading…
Cancel
Save