@@ -64,23 +64,21 @@ export const anthropicHelper: ProviderHelper = ({ reqModel, providerModel }) =>
6464 newBuffer . set ( value , buffer . length )
6565 buffer = newBuffer
6666
67- if ( buffer . length < 4 ) return
68- // The first 4 bytes are the total length (big-endian).
69- const totalLength = new DataView ( buffer . buffer , buffer . byteOffset , buffer . byteLength ) . getUint32 ( 0 , false )
67+ const messages = [ ]
7068
71- // If we don't have the full message yet, wait for more chunks.
72- if ( buffer . length < totalLength ) return
69+ while ( buffer . length >= 4 ) {
70+ // first 4 bytes are the total length (big-endian)
71+ const totalLength = new DataView ( buffer . buffer , buffer . byteOffset , buffer . byteLength ) . getUint32 ( 0 , false )
7372
74- try {
75- // Decode exactly the sub-slice for this event.
76- const subView = buffer . subarray ( 0 , totalLength )
77- const decoded = codec . decode ( subView )
73+ // wait for more chunks
74+ if ( buffer . length < totalLength ) break
7875
79- // Slice the used bytes out of the buffer, removing this message.
80- buffer = buffer . slice ( totalLength )
76+ try {
77+ const subView = buffer . subarray ( 0 , totalLength )
78+ const decoded = codec . decode ( subView )
79+ buffer = buffer . slice ( totalLength )
8180
82- // Process message
83- /* Example of Bedrock data
81+ /* Example of Bedrock data
8482 ```
8583 {
8684 bytes: 'eyJ0eXBlIjoibWVzc2FnZV9zdGFydCIsIm1lc3NhZ2UiOnsibW9kZWwiOiJjbGF1ZGUtb3B1cy00LTUtMjAyNTExMDEiLCJpZCI6Im1zZ19iZHJrXzAxMjVGdHRGb2lkNGlwWmZ4SzZMbktxeCIsInR5cGUiOiJtZXNzYWdlIiwicm9sZSI6ImFzc2lzdGFudCIsImNvbnRlbnQiOltdLCJzdG9wX3JlYXNvbiI6bnVsbCwic3RvcF9zZXF1ZW5jZSI6bnVsbCwidXNhZ2UiOnsiaW5wdXRfdG9rZW5zIjo0LCJjYWNoZV9jcmVhdGlvbl9pbnB1dF90b2tlbnMiOjEsImNhY2hlX3JlYWRfaW5wdXRfdG9rZW5zIjoxMTk2MywiY2FjaGVfY3JlYXRpb24iOnsiZXBoZW1lcmFsXzVtX2lucHV0X3Rva2VucyI6MSwiZXBoZW1lcmFsXzFoX2lucHV0X3Rva2VucyI6MH0sIm91dHB1dF90b2tlbnMiOjF9fX0=',
@@ -112,22 +110,28 @@ export const anthropicHelper: ProviderHelper = ({ reqModel, providerModel }) =>
112110 ```
113111 */
114112
115- /* Example of Anthropic data
113+ /* Example of Anthropic data
116114 ```
117115 event: message_delta
118116 data: {"type":"message_start","message":{"model":"claude-opus-4-5-20251101","id":"msg_01ETvwVWSKULxzPdkQ1xAnk2","type":"message","role":"assistant","content":[],"stop_reason":null,"stop_sequence":null,"usage":{"input_tokens":3,"cache_creation_input_tokens":11543,"cache_read_input_tokens":0,"cache_creation":{"ephemeral_5m_input_tokens":11543,"ephemeral_1h_input_tokens":0},"output_tokens":1,"service_tier":"standard"}}}
119117 ```
120118 */
121- if ( decoded . headers [ ":message-type" ] ?. value !== "event" ) return
122- const data = decoder . decode ( decoded . body , { stream : true } )
123-
124- const parsedDataResult = JSON . parse ( data )
125- delete parsedDataResult . p
126- const utf8 = atob ( parsedDataResult . bytes )
127- return encoder . encode ( [ "event: message_start" , "\n" , "data: " + utf8 , "\n\n" ] . join ( "" ) )
128- } catch ( e ) {
129- console . log ( e )
119+ if ( decoded . headers [ ":message-type" ] ?. value === "event" ) {
120+ const data = decoder . decode ( decoded . body , { stream : true } )
121+
122+ const parsedDataResult = JSON . parse ( data )
123+ delete parsedDataResult . p
124+ const bytes = atob ( parsedDataResult . bytes )
125+ const eventName = JSON . parse ( bytes ) . type
126+ messages . push ( [ `event: ${ eventName } ` , "\n" , `data: ${ bytes } ` , "\n\n" ] . join ( "" ) )
127+ }
128+ } catch ( e ) {
129+ console . log ( "@@@EE@@@" )
130+ console . log ( e )
131+ break
132+ }
130133 }
134+ return encoder . encode ( messages . join ( "" ) )
131135 }
132136 } ,
133137 streamSeparator : "\n\n" ,
0 commit comments