@mmmbuto/zai-codex-bridge 0.1.12 → 0.1.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/server.js +18 -3
package/package.json
CHANGED
package/src/server.js
CHANGED
|
@@ -267,16 +267,30 @@ async function makeUpstreamRequest(path, body, headers) {
|
|
|
267
267
|
/**
|
|
268
268
|
* Handle streaming response from Z.AI
|
|
269
269
|
*/
|
|
270
|
-
async function streamChatToResponses(stream, res, responseId) {
|
|
270
|
+
async function streamChatToResponses(stream, res, responseId, itemId) {
|
|
271
271
|
const decoder = new TextDecoder();
|
|
272
272
|
let buffer = '';
|
|
273
273
|
let chunkCount = 0;
|
|
274
274
|
let deltaCount = 0;
|
|
275
275
|
let lastParsed = null;
|
|
276
|
-
const itemId = 'item_' + Date.now();
|
|
277
276
|
|
|
278
277
|
log('debug', 'Starting to process stream');
|
|
279
278
|
|
|
279
|
+
// Send initial event to create the output item
|
|
280
|
+
const addEvent = {
|
|
281
|
+
type: 'response.output_item.add',
|
|
282
|
+
item: {
|
|
283
|
+
type: 'message',
|
|
284
|
+
role: 'assistant',
|
|
285
|
+
content: [{ type: 'output_text', text: '' }],
|
|
286
|
+
id: itemId
|
|
287
|
+
},
|
|
288
|
+
output_index: 0,
|
|
289
|
+
response_id: responseId
|
|
290
|
+
};
|
|
291
|
+
res.write(`data: ${JSON.stringify(addEvent)}\n\n`);
|
|
292
|
+
log('debug', 'Sent output_item.add event');
|
|
293
|
+
|
|
280
294
|
for await (const chunk of stream) {
|
|
281
295
|
buffer += decoder.decode(chunk, { stream: true });
|
|
282
296
|
const lines = buffer.split('\n');
|
|
@@ -440,6 +454,7 @@ async function handlePostRequest(req, res) {
|
|
|
440
454
|
// Handle streaming response
|
|
441
455
|
if (upstreamBody.stream) {
|
|
442
456
|
const responseId = 'resp_' + Date.now() + '_' + Math.random().toString(36).substr(2, 9);
|
|
457
|
+
const itemId = 'item_' + Date.now() + '_' + Math.random().toString(36).substr(2, 9);
|
|
443
458
|
log('info', 'Starting streaming response');
|
|
444
459
|
res.writeHead(200, {
|
|
445
460
|
'Content-Type': 'text/event-stream; charset=utf-8',
|
|
@@ -448,7 +463,7 @@ async function handlePostRequest(req, res) {
|
|
|
448
463
|
});
|
|
449
464
|
|
|
450
465
|
try {
|
|
451
|
-
await streamChatToResponses(upstreamResponse.body, res, responseId);
|
|
466
|
+
await streamChatToResponses(upstreamResponse.body, res, responseId, itemId);
|
|
452
467
|
log('info', 'Streaming completed');
|
|
453
468
|
} catch (e) {
|
|
454
469
|
log('error', 'Streaming error:', e);
|