File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -1046,8 +1046,9 @@ function formatPartialJSON(raw: string): string {
10461046// found.
10471047export function extractResponseJSON ( rawJson : string ) : StreamExtraction | null {
10481048 let obj : unknown ;
1049+ const jsonText = stripLoggedResponseEnvelope ( rawJson ) ;
10491050 try {
1050- obj = JSON . parse ( rawJson ) ;
1051+ obj = JSON . parse ( jsonText ) ;
10511052 } catch {
10521053 return null ;
10531054 }
@@ -1225,6 +1226,20 @@ export function extractResponseJSON(rawJson: string): StreamExtraction | null {
12251226 return out . detected ? out : null ;
12261227}
12271228
1229+ function stripLoggedResponseEnvelope ( text : string ) : string {
1230+ const trimmed = text . trim ( ) ;
1231+ if ( / ^ [ \[ { ] / . test ( trimmed ) ) return trimmed ;
1232+
1233+ const normalized = text . replace ( / \r \n / g, "\n" ) ;
1234+ if ( ! / ^ \s * S t a t u s : \s * \d { 3 } \b / m. test ( normalized ) ) return trimmed ;
1235+
1236+ const bodySeparator = / \n [ \t ] * \n / . exec ( normalized ) ;
1237+ const body = bodySeparator
1238+ ? normalized . slice ( bodySeparator . index + bodySeparator [ 0 ] . length ) . trim ( )
1239+ : "" ;
1240+ return body && / ^ [ \[ { ] / . test ( body ) ? body : trimmed ;
1241+ }
1242+
12281243function appendResponseMarkdown ( out : StreamExtraction , chunk : string ) {
12291244 if ( ! chunk ) return ;
12301245 out . content += ( out . content ? "\n\n" : "" ) + chunk ;
You can’t perform that action at this time.
0 commit comments