@agentica/core 0.32.3-dev.1 → 0.32.3-dev.2
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/lib/index.mjs +47 -33
- package/lib/index.mjs.map +1 -1
- package/lib/utils/ChatGptCompletionStreamingUtil.js +41 -29
- package/lib/utils/ChatGptCompletionStreamingUtil.js.map +1 -1
- package/lib/utils/ChatGptCompletionStreamingUtil.spec.d.ts +1 -0
- package/lib/utils/ChatGptCompletionStreamingUtil.spec.js +855 -0
- package/lib/utils/ChatGptCompletionStreamingUtil.spec.js.map +1 -0
- package/lib/utils/MPSC.js +8 -6
- package/lib/utils/MPSC.js.map +1 -1
- package/lib/utils/StreamUtil.d.ts +1 -1
- package/lib/utils/StreamUtil.js +2 -2
- package/lib/utils/StreamUtil.js.map +1 -1
- package/package.json +1 -1
- package/src/utils/ChatGptCompletionStreamingUtil.spec.ts +908 -0
- package/src/utils/ChatGptCompletionStreamingUtil.ts +45 -36
- package/src/utils/MPSC.ts +8 -6
- package/src/utils/StreamUtil.ts +2 -2
package/lib/index.mjs
CHANGED
|
@@ -1131,13 +1131,15 @@ class MPSC {
|
|
|
1131
1131
|
constructor() {
|
|
1132
1132
|
this.queue = new AsyncQueue;
|
|
1133
1133
|
this.consumer = new ReadableStream({
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1134
|
+
start: async controller => {
|
|
1135
|
+
while (true) {
|
|
1136
|
+
const {value, done} = await this.queue.dequeue();
|
|
1137
|
+
if (done === true) {
|
|
1138
|
+
controller.close();
|
|
1139
|
+
return;
|
|
1140
|
+
}
|
|
1141
|
+
controller.enqueue(value);
|
|
1139
1142
|
}
|
|
1140
|
-
controller.enqueue(value);
|
|
1141
1143
|
}
|
|
1142
1144
|
});
|
|
1143
1145
|
}
|
|
@@ -1185,10 +1187,10 @@ async function reduce(stream, reducer, initial) {
|
|
|
1185
1187
|
return acc;
|
|
1186
1188
|
}
|
|
1187
1189
|
|
|
1188
|
-
function from(value) {
|
|
1190
|
+
function from(...value) {
|
|
1189
1191
|
const stream = new ReadableStream({
|
|
1190
1192
|
start: controller => {
|
|
1191
|
-
controller.enqueue(
|
|
1193
|
+
value.forEach((v => controller.enqueue(v)));
|
|
1192
1194
|
controller.close();
|
|
1193
1195
|
}
|
|
1194
1196
|
});
|
|
@@ -1247,37 +1249,35 @@ async function reduceStreamingWithDispatch(stream, eventProcessor) {
|
|
|
1247
1249
|
const acc = await accPromise;
|
|
1248
1250
|
const registerContext = choices => {
|
|
1249
1251
|
for (const choice of choices) {
|
|
1252
|
+
if (choice.delta.content != null && choice.delta.content !== "") {
|
|
1253
|
+
if (streamContext.has(choice.index)) {
|
|
1254
|
+
const context = streamContext.get(choice.index);
|
|
1255
|
+
context.content += choice.delta.content;
|
|
1256
|
+
context.mpsc.produce(choice.delta.content);
|
|
1257
|
+
} else {
|
|
1258
|
+
const mpsc = new MPSC;
|
|
1259
|
+
streamContext.set(choice.index, {
|
|
1260
|
+
content: choice.delta.content,
|
|
1261
|
+
mpsc
|
|
1262
|
+
});
|
|
1263
|
+
mpsc.produce(choice.delta.content);
|
|
1264
|
+
eventProcessor({
|
|
1265
|
+
stream: streamDefaultReaderToAsyncGenerator(mpsc.consumer.getReader()),
|
|
1266
|
+
done: () => mpsc.done(),
|
|
1267
|
+
get: () => streamContext.get(choice.index)?.content ?? "",
|
|
1268
|
+
join: async () => {
|
|
1269
|
+
await mpsc.waitClosed();
|
|
1270
|
+
return streamContext.get(choice.index).content;
|
|
1271
|
+
}
|
|
1272
|
+
});
|
|
1273
|
+
}
|
|
1274
|
+
}
|
|
1250
1275
|
if (choice.finish_reason != null) {
|
|
1251
1276
|
const context = streamContext.get(choice.index);
|
|
1252
1277
|
if (context != null) {
|
|
1253
1278
|
context.mpsc.close();
|
|
1254
1279
|
}
|
|
1255
|
-
continue;
|
|
1256
|
-
}
|
|
1257
|
-
if (choice.delta.content == null || choice.delta.content === "") {
|
|
1258
|
-
continue;
|
|
1259
|
-
}
|
|
1260
|
-
if (streamContext.has(choice.index)) {
|
|
1261
|
-
const context = streamContext.get(choice.index);
|
|
1262
|
-
context.content += choice.delta.content;
|
|
1263
|
-
context.mpsc.produce(choice.delta.content);
|
|
1264
|
-
continue;
|
|
1265
1280
|
}
|
|
1266
|
-
const mpsc = new MPSC;
|
|
1267
|
-
streamContext.set(choice.index, {
|
|
1268
|
-
content: choice.delta.content,
|
|
1269
|
-
mpsc
|
|
1270
|
-
});
|
|
1271
|
-
mpsc.produce(choice.delta.content);
|
|
1272
|
-
eventProcessor({
|
|
1273
|
-
stream: streamDefaultReaderToAsyncGenerator(mpsc.consumer.getReader()),
|
|
1274
|
-
done: () => mpsc.done(),
|
|
1275
|
-
get: () => streamContext.get(choice.index)?.content ?? "",
|
|
1276
|
-
join: async () => {
|
|
1277
|
-
await mpsc.waitClosed();
|
|
1278
|
-
return streamContext.get(choice.index).content;
|
|
1279
|
-
}
|
|
1280
|
-
});
|
|
1281
1281
|
}
|
|
1282
1282
|
};
|
|
1283
1283
|
if (acc.object === "chat.completion.chunk") {
|
|
@@ -1290,6 +1290,20 @@ async function reduceStreamingWithDispatch(stream, eventProcessor) {
|
|
|
1290
1290
|
if (nullableCompletion == null) {
|
|
1291
1291
|
throw new Error("StreamUtil.reduce did not produce a ChatCompletion. Possible causes: the input stream was empty, invalid, or closed prematurely. " + "To debug: check that the stream is properly initialized and contains valid ChatCompletionChunk data. " + "You may also enable verbose logging upstream to inspect the stream contents. " + `Stream locked: ${stream.locked}.`);
|
|
1292
1292
|
}
|
|
1293
|
+
if (nullableCompletion.object === "chat.completion.chunk") {
|
|
1294
|
+
const completion = ChatGptCompletionMessageUtil.merge([ nullableCompletion ]);
|
|
1295
|
+
completion.choices.forEach((choice => {
|
|
1296
|
+
if (choice.message.content != null && choice.message.content !== "") {
|
|
1297
|
+
eventProcessor({
|
|
1298
|
+
stream: toAsyncGenerator(choice.message.content),
|
|
1299
|
+
done: () => true,
|
|
1300
|
+
get: () => choice.message.content,
|
|
1301
|
+
join: async () => choice.message.content
|
|
1302
|
+
});
|
|
1303
|
+
}
|
|
1304
|
+
}));
|
|
1305
|
+
return completion;
|
|
1306
|
+
}
|
|
1293
1307
|
return nullableCompletion;
|
|
1294
1308
|
}
|
|
1295
1309
|
|