@@ -146,7 +146,7 @@ export function parseJSONObjectFromText(
146
146
const jsonBlockMatch = text . match ( jsonBlockPattern ) ;
147
147
148
148
if ( jsonBlockMatch ) {
149
- const parsingText = normalizeJsonString ( jsonBlockMatch [ 1 ] ) ;
149
+ const parsingText = normalizeJsonString ( text ) ;
150
150
try {
151
151
jsonData = JSON . parse ( parsingText ) ;
152
152
} catch ( e ) {
@@ -155,11 +155,11 @@ export function parseJSONObjectFromText(
155
155
return extractAttributes ( text ) ;
156
156
}
157
157
} else {
158
- const objectPattern = / { [ \s \S ] * ?} / ;
158
+ const objectPattern = / { [ \s \S ] * ?} ? / ;
159
159
const objectMatch = text . match ( objectPattern ) ;
160
160
161
161
if ( objectMatch ) {
162
- const parsingText = normalizeJsonString ( objectMatch [ 0 ] ) ;
162
+ const parsingText = normalizeJsonString ( text ) ;
163
163
try {
164
164
jsonData = JSON . parse ( parsingText ) ;
165
165
} catch ( e ) {
@@ -193,19 +193,20 @@ export function extractAttributes(
193
193
response : string ,
194
194
attributesToExtract ?: string [ ]
195
195
) : { [ key : string ] : string | undefined } {
196
+ response = response . trim ( ) ;
196
197
const attributes : { [ key : string ] : string | undefined } = { } ;
197
198
198
199
if ( ! attributesToExtract || attributesToExtract . length === 0 ) {
199
200
// Extract all attributes if no specific attributes are provided
200
- const matches = response . matchAll ( / " ( [ ^ " ] + ) " \s * : \s * " ( [ ^ " ] * ) " / g) ;
201
+ const matches = response . matchAll ( / " ( [ ^ " ] + ) " \s * : \s * " ( [ ^ " ] * ) " ? / g) ;
201
202
for ( const match of matches ) {
202
203
attributes [ match [ 1 ] ] = match [ 2 ] ;
203
204
}
204
205
} else {
205
206
// Extract only specified attributes
206
207
attributesToExtract . forEach ( ( attribute ) => {
207
208
const match = response . match (
208
- new RegExp ( `"${ attribute } "\\s*:\\s*"([^"]*)"` , "i" )
209
+ new RegExp ( `"${ attribute } "\\s*:\\s*"([^"]*)"? ` , "i" )
209
210
) ;
210
211
if ( match ) {
211
212
attributes [ attribute ] = match [ 1 ] ;
0 commit comments