From dc58005d5078c2040c70a760823bf3e3188fae80 Mon Sep 17 00:00:00 2001 From: gcalcedo Date: Fri, 20 Sep 2024 18:47:20 +0200 Subject: [PATCH 1/3] fix: avoid unintended metadata parsing --- src/hooks/useChatStream.ts | 8 ++++---- src/utils/json.ts | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) create mode 100644 src/utils/json.ts diff --git a/src/hooks/useChatStream.ts b/src/hooks/useChatStream.ts index 9fb66f6..423c492 100644 --- a/src/hooks/useChatStream.ts +++ b/src/hooks/useChatStream.ts @@ -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.'; @@ -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) { @@ -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. diff --git a/src/utils/json.ts b/src/utils/json.ts new file mode 100644 index 0000000..9c22e57 --- /dev/null +++ b/src/utils/json.ts @@ -0,0 +1,17 @@ +export const extractJsonFromEnd = (chunk: string) => { + const chunkTrimmed = chunk.trim(); + + const jsonObjectRegex = /({[^]*})\s*$/; + const match = chunkTrimmed.match(jsonObjectRegex); + + if (match) { + const jsonStr = match[1]; + try { + const parsedData = JSON.parse(jsonStr); + if (typeof parsedData === 'object' && parsedData !== null && !Array.isArray(parsedData)) { + return parsedData; + } + } catch {} + } + return null; +}; From daa67f494fd49be4ce717667b7cd044e73ca7870 Mon Sep 17 00:00:00 2001 From: gcalcedo Date: Fri, 20 Sep 2024 19:12:50 +0200 Subject: [PATCH 2/3] chore: invert if clause --- src/utils/json.ts | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/utils/json.ts b/src/utils/json.ts index 9c22e57..215d0fd 100644 --- a/src/utils/json.ts +++ b/src/utils/json.ts @@ -4,14 +4,17 @@ export const extractJsonFromEnd = (chunk: string) => { const jsonObjectRegex = /({[^]*})\s*$/; const match = chunkTrimmed.match(jsonObjectRegex); - if (match) { - const jsonStr = match[1]; - try { - const parsedData = JSON.parse(jsonStr); - if (typeof parsedData === 'object' && parsedData !== null && !Array.isArray(parsedData)) { - return parsedData; - } - } catch {} + 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; }; From 3d57054ce308da9b4ab9b26b4a94ef893c9655c8 Mon Sep 17 00:00:00 2001 From: gcalcedo Date: Fri, 20 Sep 2024 21:13:47 +0200 Subject: [PATCH 3/3] v0.5.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 1f3fd93..331ad4d 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@magicul/react-chat-stream", "description": "A React hook that lets you easily integrate your custom ChatGPT-like chat in React.", - "version": "0.5.0", + "version": "0.5.1", "main": "dist/index.js", "types": "dist/index.d.ts", "homepage": "https://github.com/XD2Sketch/react-chat-stream#readme",