wip: zen
This commit is contained in:
@@ -64,22 +64,20 @@ export const anthropicHelper: ProviderHelper = ({ reqModel, providerModel }) =>
|
|||||||
newBuffer.set(value, buffer.length)
|
newBuffer.set(value, buffer.length)
|
||||||
buffer = newBuffer
|
buffer = newBuffer
|
||||||
|
|
||||||
if (buffer.length < 4) return
|
const messages = []
|
||||||
// The first 4 bytes are the total length (big-endian).
|
|
||||||
|
while (buffer.length >= 4) {
|
||||||
|
// first 4 bytes are the total length (big-endian)
|
||||||
const totalLength = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength).getUint32(0, false)
|
const totalLength = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength).getUint32(0, false)
|
||||||
|
|
||||||
// If we don't have the full message yet, wait for more chunks.
|
// wait for more chunks
|
||||||
if (buffer.length < totalLength) return
|
if (buffer.length < totalLength) break
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Decode exactly the sub-slice for this event.
|
|
||||||
const subView = buffer.subarray(0, totalLength)
|
const subView = buffer.subarray(0, totalLength)
|
||||||
const decoded = codec.decode(subView)
|
const decoded = codec.decode(subView)
|
||||||
|
|
||||||
// Slice the used bytes out of the buffer, removing this message.
|
|
||||||
buffer = buffer.slice(totalLength)
|
buffer = buffer.slice(totalLength)
|
||||||
|
|
||||||
// Process message
|
|
||||||
/* Example of Bedrock data
|
/* Example of Bedrock data
|
||||||
```
|
```
|
||||||
{
|
{
|
||||||
@@ -118,16 +116,22 @@ export const anthropicHelper: ProviderHelper = ({ reqModel, providerModel }) =>
|
|||||||
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"}}}
|
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"}}}
|
||||||
```
|
```
|
||||||
*/
|
*/
|
||||||
if (decoded.headers[":message-type"]?.value !== "event") return
|
if (decoded.headers[":message-type"]?.value === "event") {
|
||||||
const data = decoder.decode(decoded.body, { stream: true })
|
const data = decoder.decode(decoded.body, { stream: true })
|
||||||
|
|
||||||
const parsedDataResult = JSON.parse(data)
|
const parsedDataResult = JSON.parse(data)
|
||||||
delete parsedDataResult.p
|
delete parsedDataResult.p
|
||||||
const utf8 = atob(parsedDataResult.bytes)
|
const bytes = atob(parsedDataResult.bytes)
|
||||||
return encoder.encode(["event: message_start", "\n", "data: " + utf8, "\n\n"].join(""))
|
const eventName = JSON.parse(bytes).type
|
||||||
} catch (e) {
|
messages.push([`event: ${eventName}`, "\n", `data: ${bytes}`, "\n\n"].join(""))
|
||||||
console.log(e)
|
|
||||||
}
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.log("@@@EE@@@")
|
||||||
|
console.log(e)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return encoder.encode(messages.join(""))
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
streamSeparator: "\n\n",
|
streamSeparator: "\n\n",
|
||||||
|
|||||||
Reference in New Issue
Block a user