-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
98 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,69 @@ | ||
export const extractJsonFromEnd = (chunk: string) => { | ||
const chunkTrimmed = chunk.trim(); | ||
/** | ||
* Extracts JSON objects from a string containing one or more dumps of JSON objects. | ||
* | ||
* This function is used to parse the stream only when the `useMetadata` option is enabled. | ||
* Then, this hook expects to receive JSON dumps instead of plain text. These JSON dumps are of | ||
* the following format: | ||
* | ||
* - For content to be used in the chat: | ||
* ```json | ||
* { | ||
* "type": "content", | ||
* "data": "Hello, world!" | ||
* } | ||
* ``` | ||
* | ||
* - For metadata: | ||
* ```json | ||
* { | ||
* "type": "metadata", | ||
* "data": { | ||
* "key": "value", | ||
* "key2": "value2", | ||
* ... | ||
* } | ||
* } | ||
* ``` | ||
* | ||
* @param chunk - The string containing one or more JSON object dumps. | ||
* @returns An array of parsed JSON objects. | ||
* | ||
* @example | ||
* ```typescript | ||
* const chunk = '{"type": "content", "data": "Hello, world!"}{"type": "metadata", "data": {"key": "value"}}'; | ||
* const jsonObjects = getJsonObjectsFromChunks(chunk); | ||
* console.log(jsonObjects); | ||
* // Output: [ | ||
* // { type: 'content', data: 'Hello, world!' }, | ||
* // { type: 'metadata', data: { key: 'value' } } | ||
* // ] | ||
* ``` | ||
*/ | ||
export const getJsonObjectsFromChunks = (chunk: string) => { | ||
const jsonObjects = []; | ||
const braceStack = []; | ||
let currentJsonStart = null; | ||
|
||
const jsonObjectRegex = /({[^]*})\s*$/; | ||
const match = chunkTrimmed.match(jsonObjectRegex); | ||
for (let i = 0; i < chunk.length; i++) { | ||
const char = chunk[i]; | ||
|
||
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; | ||
if (char === '{') { | ||
if (braceStack.length === 0) { | ||
currentJsonStart = i; | ||
} | ||
braceStack.push('{'); | ||
} else if (char === '}') { | ||
braceStack.pop(); | ||
if (braceStack.length === 0 && currentJsonStart !== null) { | ||
const potentialJson = chunk.substring(currentJsonStart, i + 1); | ||
try { | ||
const parsedJson = JSON.parse(potentialJson); | ||
jsonObjects.push(parsedJson); | ||
} catch {} | ||
currentJsonStart = null; | ||
} | ||
} | ||
} catch {} | ||
} | ||
|
||
return null; | ||
return jsonObjects; | ||
}; |