Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix: avoid unintended metadata parsing #44

Merged
merged 3 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/hooks/useChatStream.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ChangeEvent, FormEvent, useState } from 'react';
import { decodeStreamToJson, getStream } from '../utils/streams';
import { UseChatStreamChatMessage, UseChatStreamInput } from '../types';
import { extractJsonFromEnd } from '../utils/json';

const BOT_ERROR_MESSAGE = 'Something went wrong fetching AI response.';

Expand Down Expand Up @@ -41,7 +42,6 @@ const useChatStream = (input: UseChatStreamInput) => {
const stream = await getStream(message, input.options, input.method);
const initialMessage = addMessage({ content: '', role: 'bot' });
let response = '';
let metadata = {};

for await (const chunk of decodeStreamToJson(stream)) {
if (!charactersPerSecond) {
Expand All @@ -51,10 +51,10 @@ const useChatStream = (input: UseChatStreamInput) => {
}

if (input.options.useMetadata) {
try {
metadata = JSON.parse(chunk.trim());
const metadata = extractJsonFromEnd(chunk);
if (metadata) {
return { ...initialMessage, content: response, metadata: metadata };
} catch {}
}
}

// Stream characters one by one based on the characters per second that is set.
Expand Down
20 changes: 20 additions & 0 deletions src/utils/json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export const extractJsonFromEnd = (chunk: string) => {
const chunkTrimmed = chunk.trim();

const jsonObjectRegex = /({[^]*})\s*$/;
const match = chunkTrimmed.match(jsonObjectRegex);

if (!match) {
return null;
}

const jsonStr = match[1];
try {
const parsedData = JSON.parse(jsonStr);
if (typeof parsedData === 'object' && parsedData !== null && !Array.isArray(parsedData)) {
return parsedData;
}
} catch {}

return null;
};
Loading