modelfusion 0.74.0 → 0.74.1

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.
@@ -6,7 +6,8 @@ const AsyncQueue_js_1 = require("../../../util/AsyncQueue.cjs");
6
6
  const parseEventSourceStream_js_1 = require("../../../util/streaming/parseEventSourceStream.cjs");
7
7
  const parseJSON_js_1 = require("../../../core/schema/parseJSON.cjs");
8
8
  const ZodSchema_js_1 = require("../../../core/schema/ZodSchema.cjs");
9
- const chatResponseStreamEventSchema = new ZodSchema_js_1.ZodSchema(zod_1.z.object({
9
+ const chatCompletionChunkSchema = zod_1.z.object({
10
+ object: zod_1.z.literal("chat.completion.chunk"),
10
11
  id: zod_1.z.string(),
11
12
  choices: zod_1.z.array(zod_1.z.object({
12
13
  delta: zod_1.z.object({
@@ -44,8 +45,15 @@ const chatResponseStreamEventSchema = new ZodSchema_js_1.ZodSchema(zod_1.z.objec
44
45
  created: zod_1.z.number(),
45
46
  model: zod_1.z.string(),
46
47
  system_fingerprint: zod_1.z.string().optional(),
47
- object: zod_1.z.literal("chat.completion.chunk"),
48
- }));
48
+ });
49
+ const chatResponseStreamEventSchema = new ZodSchema_js_1.ZodSchema(zod_1.z.union([
50
+ chatCompletionChunkSchema,
51
+ zod_1.z.object({
52
+ object: zod_1.z.string().refine((obj) => obj !== "chat.completion.chunk", {
53
+ message: "Object must not be 'chat.completion.chunk'",
54
+ }),
55
+ }),
56
+ ]));
49
57
  async function createOpenAIChatDeltaIterableQueue(stream, extractDeltaValue) {
50
58
  const queue = new AsyncQueue_js_1.AsyncQueue();
51
59
  const streamDelta = [];
@@ -68,12 +76,19 @@ async function createOpenAIChatDeltaIterableQueue(stream, extractDeltaValue) {
68
76
  type: "error",
69
77
  error: parseResult.error,
70
78
  });
71
- queue.close();
72
- return;
79
+ // Note: the queue is not closed on purpose. Some providers might add additional
80
+ // chunks that are not parsable, and ModelFusion should be resilient to that.
81
+ continue;
73
82
  }
74
83
  const eventData = parseResult.data;
75
- for (let i = 0; i < eventData.choices.length; i++) {
76
- const eventChoice = eventData.choices[i];
84
+ // ignore objects that are not "chat.completion.chunk" events.
85
+ // Such additional objects are e.g. sent by Azure OpenAI.
86
+ if (eventData.object !== "chat.completion.chunk") {
87
+ continue;
88
+ }
89
+ const completionChunk = eventData;
90
+ for (let i = 0; i < completionChunk.choices.length; i++) {
91
+ const eventChoice = completionChunk.choices[i];
77
92
  const delta = eventChoice.delta;
78
93
  if (streamDelta[i] == null) {
79
94
  streamDelta[i] = {
@@ -3,7 +3,8 @@ import { AsyncQueue } from "../../../util/AsyncQueue.js";
3
3
  import { parseEventSourceStream } from "../../../util/streaming/parseEventSourceStream.js";
4
4
  import { safeParseJSON } from "../../../core/schema/parseJSON.js";
5
5
  import { ZodSchema } from "../../../core/schema/ZodSchema.js";
6
- const chatResponseStreamEventSchema = new ZodSchema(z.object({
6
+ const chatCompletionChunkSchema = z.object({
7
+ object: z.literal("chat.completion.chunk"),
7
8
  id: z.string(),
8
9
  choices: z.array(z.object({
9
10
  delta: z.object({
@@ -41,8 +42,15 @@ const chatResponseStreamEventSchema = new ZodSchema(z.object({
41
42
  created: z.number(),
42
43
  model: z.string(),
43
44
  system_fingerprint: z.string().optional(),
44
- object: z.literal("chat.completion.chunk"),
45
- }));
45
+ });
46
+ const chatResponseStreamEventSchema = new ZodSchema(z.union([
47
+ chatCompletionChunkSchema,
48
+ z.object({
49
+ object: z.string().refine((obj) => obj !== "chat.completion.chunk", {
50
+ message: "Object must not be 'chat.completion.chunk'",
51
+ }),
52
+ }),
53
+ ]));
46
54
  export async function createOpenAIChatDeltaIterableQueue(stream, extractDeltaValue) {
47
55
  const queue = new AsyncQueue();
48
56
  const streamDelta = [];
@@ -65,12 +73,19 @@ export async function createOpenAIChatDeltaIterableQueue(stream, extractDeltaVal
65
73
  type: "error",
66
74
  error: parseResult.error,
67
75
  });
68
- queue.close();
69
- return;
76
+ // Note: the queue is not closed on purpose. Some providers might add additional
77
+ // chunks that are not parsable, and ModelFusion should be resilient to that.
78
+ continue;
70
79
  }
71
80
  const eventData = parseResult.data;
72
- for (let i = 0; i < eventData.choices.length; i++) {
73
- const eventChoice = eventData.choices[i];
81
+ // ignore objects that are not "chat.completion.chunk" events.
82
+ // Such additional objects are e.g. sent by Azure OpenAI.
83
+ if (eventData.object !== "chat.completion.chunk") {
84
+ continue;
85
+ }
86
+ const completionChunk = eventData;
87
+ for (let i = 0; i < completionChunk.choices.length; i++) {
88
+ const eventChoice = completionChunk.choices[i];
74
89
  const delta = eventChoice.delta;
75
90
  if (streamDelta[i] == null) {
76
91
  streamDelta[i] = {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "modelfusion",
3
3
  "description": "The TypeScript library for building multi-modal AI applications.",
4
- "version": "0.74.0",
4
+ "version": "0.74.1",
5
5
  "author": "Lars Grammel",
6
6
  "license": "MIT",
7
7
  "keywords": [