@dremio/js-sdk 0.43.1 → 0.44.0

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/README.md CHANGED
@@ -482,7 +482,6 @@ const conversationMachine = dremio.ai("PROJECT_ID").conversationMachine;
482
482
  const actor = createActor(conversationMachine, {
483
483
  input: {
484
484
  conversationId: null,
485
- initialMessage: UserChatMessage.new("Hello, can you help me?"),
486
485
  },
487
486
  });
488
487
 
@@ -125,6 +125,17 @@ export declare class AIResource {
125
125
  message: string;
126
126
  };
127
127
  role: "agent";
128
+ } | {
129
+ conversationId: string;
130
+ modelName: string;
131
+ modelProviderId: string;
132
+ runId: string;
133
+ createdAt: import("temporal-polyfill").Temporal.Instant;
134
+ id: string;
135
+ content: {
136
+ chunkType: "endOfStream";
137
+ };
138
+ role: "agent";
128
139
  } | {
129
140
  conversationId: string;
130
141
  modelName: string;
@@ -221,6 +232,17 @@ export declare class AIResource {
221
232
  message: string;
222
233
  };
223
234
  role: "agent";
235
+ } | {
236
+ conversationId: string;
237
+ modelName: string;
238
+ modelProviderId: string;
239
+ runId: string;
240
+ createdAt: import("temporal-polyfill").Temporal.Instant;
241
+ id: string;
242
+ content: {
243
+ chunkType: "endOfStream";
244
+ };
245
+ role: "agent";
224
246
  } | {
225
247
  conversationId: string;
226
248
  modelName: string;
@@ -324,6 +346,17 @@ export declare class AIResource {
324
346
  message: string;
325
347
  };
326
348
  role: "agent";
349
+ } | {
350
+ conversationId: string;
351
+ modelName: string;
352
+ modelProviderId: string;
353
+ runId: string;
354
+ createdAt: import("temporal-polyfill").Temporal.Instant;
355
+ id: string;
356
+ content: {
357
+ chunkType: "endOfStream";
358
+ };
359
+ role: "agent";
327
360
  } | {
328
361
  conversationId: string;
329
362
  modelName: string;
@@ -435,6 +468,17 @@ export declare class AIResource {
435
468
  message: string;
436
469
  };
437
470
  role: "agent";
471
+ } | {
472
+ conversationId: string;
473
+ modelName: string;
474
+ modelProviderId: string;
475
+ runId: string;
476
+ createdAt: import("temporal-polyfill").Temporal.Instant;
477
+ id: string;
478
+ content: {
479
+ chunkType: "endOfStream";
480
+ };
481
+ role: "agent";
438
482
  } | {
439
483
  conversationId: string;
440
484
  modelName: string;
@@ -535,6 +579,17 @@ export declare class AIResource {
535
579
  message: string;
536
580
  };
537
581
  role: "agent";
582
+ } | {
583
+ conversationId: string;
584
+ modelName: string;
585
+ modelProviderId: string;
586
+ runId: string;
587
+ createdAt: import("temporal-polyfill").Temporal.Instant;
588
+ id: string;
589
+ content: {
590
+ chunkType: "endOfStream";
591
+ };
592
+ role: "agent";
538
593
  } | {
539
594
  conversationId: string;
540
595
  modelName: string;
@@ -650,6 +705,17 @@ export declare class AIResource {
650
705
  message: string;
651
706
  };
652
707
  role: "agent";
708
+ } | {
709
+ conversationId: string;
710
+ modelName: string;
711
+ modelProviderId: string;
712
+ runId: string;
713
+ createdAt: import("temporal-polyfill").Temporal.Instant;
714
+ id: string;
715
+ content: {
716
+ chunkType: "endOfStream";
717
+ };
718
+ role: "agent";
653
719
  } | {
654
720
  conversationId: string;
655
721
  modelName: string;
@@ -713,7 +779,13 @@ export declare class AIResource {
713
779
  id: string | undefined;
714
780
  }, never, never, never, "uninitialized" | "creating_conversation" | "retrieving_history" | "idle" | "submitting_message" | "retrieve_history_failed" | "submitting_message_failed" | {
715
781
  streaming: "monitoring_replies" | "stopping";
716
- }, string, import("../../enterprise/ai/conversations/createConversationMachine.ts").ConversationMachineInput, import("xstate").NonReducibleUnknown, import("xstate").EventObject, import("xstate").MetaObject, {
782
+ }, string, import("../../enterprise/ai/conversations/createConversationMachine.ts").ConversationMachineInput, import("xstate").NonReducibleUnknown, {
783
+ type: "chatEventReceived";
784
+ chatEvent: ChatEvent;
785
+ } | {
786
+ type: "conversationCreated";
787
+ id: string;
788
+ }, import("xstate").MetaObject, {
717
789
  id: "agentConversation";
718
790
  states: {
719
791
  readonly creating_conversation: {};
@@ -67,6 +67,17 @@ export declare class AIResource {
67
67
  message: string;
68
68
  };
69
69
  role: "agent";
70
+ } | {
71
+ conversationId: string;
72
+ modelName: string;
73
+ modelProviderId: string;
74
+ runId: string;
75
+ createdAt: import("temporal-polyfill").Temporal.Instant;
76
+ id: string;
77
+ content: {
78
+ chunkType: "endOfStream";
79
+ };
80
+ role: "agent";
70
81
  } | {
71
82
  conversationId: string;
72
83
  modelName: string;
@@ -163,6 +174,17 @@ export declare class AIResource {
163
174
  message: string;
164
175
  };
165
176
  role: "agent";
177
+ } | {
178
+ conversationId: string;
179
+ modelName: string;
180
+ modelProviderId: string;
181
+ runId: string;
182
+ createdAt: import("temporal-polyfill").Temporal.Instant;
183
+ id: string;
184
+ content: {
185
+ chunkType: "endOfStream";
186
+ };
187
+ role: "agent";
166
188
  } | {
167
189
  conversationId: string;
168
190
  modelName: string;
@@ -266,6 +288,17 @@ export declare class AIResource {
266
288
  message: string;
267
289
  };
268
290
  role: "agent";
291
+ } | {
292
+ conversationId: string;
293
+ modelName: string;
294
+ modelProviderId: string;
295
+ runId: string;
296
+ createdAt: import("temporal-polyfill").Temporal.Instant;
297
+ id: string;
298
+ content: {
299
+ chunkType: "endOfStream";
300
+ };
301
+ role: "agent";
269
302
  } | {
270
303
  conversationId: string;
271
304
  modelName: string;
@@ -377,6 +410,17 @@ export declare class AIResource {
377
410
  message: string;
378
411
  };
379
412
  role: "agent";
413
+ } | {
414
+ conversationId: string;
415
+ modelName: string;
416
+ modelProviderId: string;
417
+ runId: string;
418
+ createdAt: import("temporal-polyfill").Temporal.Instant;
419
+ id: string;
420
+ content: {
421
+ chunkType: "endOfStream";
422
+ };
423
+ role: "agent";
380
424
  } | {
381
425
  conversationId: string;
382
426
  modelName: string;
@@ -477,6 +521,17 @@ export declare class AIResource {
477
521
  message: string;
478
522
  };
479
523
  role: "agent";
524
+ } | {
525
+ conversationId: string;
526
+ modelName: string;
527
+ modelProviderId: string;
528
+ runId: string;
529
+ createdAt: import("temporal-polyfill").Temporal.Instant;
530
+ id: string;
531
+ content: {
532
+ chunkType: "endOfStream";
533
+ };
534
+ role: "agent";
480
535
  } | {
481
536
  conversationId: string;
482
537
  modelName: string;
@@ -592,6 +647,17 @@ export declare class AIResource {
592
647
  message: string;
593
648
  };
594
649
  role: "agent";
650
+ } | {
651
+ conversationId: string;
652
+ modelName: string;
653
+ modelProviderId: string;
654
+ runId: string;
655
+ createdAt: import("temporal-polyfill").Temporal.Instant;
656
+ id: string;
657
+ content: {
658
+ chunkType: "endOfStream";
659
+ };
660
+ role: "agent";
595
661
  } | {
596
662
  conversationId: string;
597
663
  modelName: string;
@@ -655,7 +721,13 @@ export declare class AIResource {
655
721
  id: string | undefined;
656
722
  }, never, never, never, "uninitialized" | "creating_conversation" | "retrieving_history" | "idle" | "submitting_message" | "retrieve_history_failed" | "submitting_message_failed" | {
657
723
  streaming: "monitoring_replies" | "stopping";
658
- }, string, import("./conversations/createConversationMachine.ts").ConversationMachineInput, import("xstate").NonReducibleUnknown, import("xstate").EventObject, import("xstate").MetaObject, {
724
+ }, string, import("./conversations/createConversationMachine.ts").ConversationMachineInput, import("xstate").NonReducibleUnknown, {
725
+ type: "chatEventReceived";
726
+ chatEvent: ChatEvent;
727
+ } | {
728
+ type: "conversationCreated";
729
+ id: string;
730
+ }, import("xstate").MetaObject, {
659
731
  id: "agentConversation";
660
732
  states: {
661
733
  readonly creating_conversation: {};
@@ -57,6 +57,14 @@ export declare const chatEventCodec: z.ZodMiniCodec<z.ZodMiniDiscriminatedUnion<
57
57
  * anyways, so the parser should throw to the generic response error handler.
58
58
  */
59
59
  message: z.ZodMiniString<string>;
60
+ }, z.core.$strip>, z.ZodMiniObject<{
61
+ chunkType: z.ZodMiniLiteral<"endOfStream">;
62
+ conversationId: z.ZodMiniString<string>;
63
+ createdAt: z.ZodMiniString<string>;
64
+ messageId: z.ZodMiniString<string>;
65
+ modelName: z.ZodMiniString<string>;
66
+ modelProviderId: z.ZodMiniString<string>;
67
+ runId: z.ZodMiniString<string>;
60
68
  }, z.core.$strip>, z.ZodMiniObject<{
61
69
  chunkType: z.ZodMiniLiteral<"conversationUpdate">;
62
70
  summary: z.ZodMiniOptional<z.ZodMiniString<string>>;
@@ -140,6 +148,17 @@ export declare const chatEventCodec: z.ZodMiniCodec<z.ZodMiniDiscriminatedUnion<
140
148
  message: z.ZodMiniString<string>;
141
149
  }, z.core.$strip>;
142
150
  role: z.ZodMiniLiteral<"agent">;
151
+ }, z.core.$strict>, z.ZodMiniObject<{
152
+ conversationId: z.ZodMiniString<string>;
153
+ modelName: z.ZodMiniString<string>;
154
+ modelProviderId: z.ZodMiniString<string>;
155
+ runId: z.ZodMiniString<string>;
156
+ createdAt: z.ZodMiniCustom<Temporal.Instant, Temporal.Instant>;
157
+ id: z.ZodMiniString<string>;
158
+ content: z.ZodMiniObject<{
159
+ chunkType: z.ZodMiniLiteral<"endOfStream">;
160
+ }, z.core.$strip>;
161
+ role: z.ZodMiniLiteral<"agent">;
143
162
  }, z.core.$strict>, z.ZodMiniObject<{
144
163
  conversationId: z.ZodMiniString<string>;
145
164
  modelName: z.ZodMiniString<string>;
@@ -49,6 +49,9 @@ const conversationUpdateChunkSchema = z.object({
49
49
  summary: z.optional(z.string()),
50
50
  title: z.optional(z.string()),
51
51
  });
52
+ const endOfStreamChunkSchema = z.object({
53
+ chunkType: z.literal("endOfStream"),
54
+ });
52
55
  const modelChunkSchema = z.object({
53
56
  chunkType: z.literal("model"),
54
57
  name: z.string().check(z.trim(), z.minLength(1)),
@@ -96,6 +99,7 @@ export const chatEventV1Schema = z.discriminatedUnion("chunkType", [
96
99
  ]);
97
100
  const chatEventInputSchema = z.discriminatedUnion("chunkType", [
98
101
  errorChunkSchema,
102
+ z.extend(endOfStreamChunkSchema, chatEventSharedSchema),
99
103
  z.extend(conversationUpdateChunkSchema, chatEventSharedSchema),
100
104
  z.extend(modelChunkSchema, chatEventSharedSchema),
101
105
  z.extend(toolRequestChunkSchema, chatEventSharedSchema),
@@ -112,6 +116,10 @@ const chatEventOutputSchema = z.union([
112
116
  role: z.literal("agent"),
113
117
  }),
114
118
  z.extend(chatEventOutputSharedSchema, { content: errorChunkSchema, role: z.literal("agent") }),
119
+ z.extend(chatEventOutputSharedSchema, {
120
+ content: endOfStreamChunkSchema,
121
+ role: z.literal("agent"),
122
+ }),
115
123
  z.extend(chatEventOutputSharedSchema, { content: modelChunkSchema, role: z.literal("agent") }),
116
124
  z.extend(chatEventOutputSharedSchema, {
117
125
  content: toolRequestChunkSchema,
@@ -162,6 +170,14 @@ export const chatEventCodec = z.codec(chatEventInputSchema, chatEventOutputSchem
162
170
  },
163
171
  role: "agent",
164
172
  };
173
+ case "endOfStream":
174
+ return {
175
+ ...sharedProperties,
176
+ content: {
177
+ chunkType: "endOfStream",
178
+ },
179
+ role: "agent",
180
+ };
165
181
  case "model":
166
182
  return {
167
183
  ...sharedProperties,
@@ -1 +1 @@
1
- {"version":3,"file":"chatEventSchema.js","sourceRoot":"","sources":["../../../../src/enterprise/ai/chat/chatEventSchema.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAC7C,OAAO,KAAK,CAAC,MAAM,UAAU,CAAC;AAE9B;;;GAGG;AACH,MAAM,uBAAuB,GAAG;IAC9B,SAAS,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,QAAQ,EAAE,CAAC;IAC7C,SAAS,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;IACrD,eAAe,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;IAC3D,SAAS,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC,I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AC,IAAI;wBACZ,MAAM,EAAE,CAAC,CAAC,MAAM;qBACjB;oBACD,IAAI,EAAE,OAAO;iBAC4C,CAAC;YAC9D,KAAK,aAAa;gBAChB,OAAO;oBACL,GAAG,gBAAgB;oBACnB,OAAO,EAAE,EAAE,SAAS,EAAE,aAAa,EAAE,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE;oBACnD,IAAI,EAAE,MAAM;iBAC6C,CAAC;QAChE,CAAC;IACH,CAAC;IACD,MAAM,CAAC,CAAC;QACN,OAAO;YACL,YAAY;YACZ,cAAc,EAAE,CAAC,CAAC,cAAc;YAChC,SAAS,EAAE,CAAC,CAAC,SAAS,CAAC,QAAQ,EAAE;YACjC,SAAS,EAAE,CAAC,CAAC,EAAE;YACf,SAAS,EAAE,CAAC,CAAC,SAAS;YACtB,eAAe,EAAE,CAAC,CAAC,eAAe;YAClC,KAAK,EAAE,CAAC,CAAC,KAAK;YACd,GAAG,CAAC,CAAC,OAAO;SAC2C,CAAC;IAC5D,CAAC;CACF,CAAC,CAAC","sourcesContent":["/*\n * Copyright (C) 2024-2025 Dremio Corporation\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { Temporal } from \"temporal-polyfill\";\nimport * as z from \"zod/mini\";\n\n/**\n * Shared chat event fields for all responses (except error response)\n * @deprecated\n */\nconst chatEventV1SharedSchema = {\n createdAt: z.string().check(z.iso.datetime()),\n modelName: z.string().check(z.trim(), z.minLength(1)),\n modelProviderId: z.string().check(z.trim(), z.minLength(1)),\n sessionId: z.string().check(z.trim(), z.minLength(1)),\n};\n\n/**\n * Shared chat event fields for all responses (except error response)\n */\nconst chatEventSharedSchema = {\n conversationId: z.string(),\n createdAt: z.string().check(z.iso.datetime()),\n messageId: z.string(),\n modelName: z.string(),\n modelProviderId: z.string(),\n runId: z.string(),\n};\n\nconst errorChunkSchema = z.object({\n chunkType: z.literal(\"error\"),\n /**\n * If the message is not present or very short, it won't be useful to show\n * anyways, so the parser should throw to the generic response error handler.\n */\n message: z.string().check(z.trim(), z.minLength(2)),\n});\n\nconst conversationUpdateChunkSchema = z.object({\n chunkType: z.literal(\"conversationUpdate\"),\n summary: z.optional(z.string()),\n title: z.optional(z.string()),\n});\n\nconst modelChunkSchema = z.object({\n chunkType: z.literal(\"model\"),\n name: z.string().check(z.trim(), z.minLength(1)),\n /**\n * It's useful to indicate (to the type system) that the result contents\n * at this stage can only be data (de)serializable from JSON. Because\n * model responses can vary depending on runtime flags and edition, we can\n * leave this field generic and refine it more precisely in later stages.\n */\n result: z.record(z.string(), z.unknown()),\n});\n\nconst toolRequestChunkSchema = z.object({\n arguments: z.record(z.string(), z.unknown()),\n callId: z.string().check(z.trim(), z.minLength(1)),\n chunkType: z.literal(\"toolRequest\"),\n commentary: z.optional(z.string()),\n name: z.string(),\n summarizedTitle: z.optional(z.string()),\n});\n\nconst toolResponseChunkSchema = z.object({\n callId: z.string(),\n chunkType: z.literal(\"toolResponse\"),\n commentary: z.optional(z.string()),\n name: z.string(),\n result: z.record(z.string(), z.unknown()),\n});\n\nconst userMessageChunkSchema = z.object({\n chunkType: z.literal(\"userMessage\"),\n text: z.string(),\n});\n\n/**\n * Captures all of the possible chunks of data that the Agent can reply\n * with. We're lenient to extra/unexpected fields being present (`z.object`\n * instead of `z.strictObject`), but for the fields that are expected,\n * we validate that they match the most general type category they belong in.\n * For example, it's good enough to validate that the `callId` is a string\n * for type safety -- we don't need to verify that it's also a `z.uuid()`.\n * @deprecated\n */\nexport const chatEventV1Schema = z.discriminatedUnion(\"chunkType\", [\n errorChunkSchema,\n z.extend(modelChunkSchema, chatEventV1SharedSchema),\n z.extend(toolRequestChunkSchema, chatEventV1SharedSchema),\n z.extend(toolResponseChunkSchema, chatEventV1SharedSchema),\n]);\n\n/**\n * @deprecated\n */\nexport type ChatEventV1 = z.infer<typeof chatEventV1Schema>;\n\nconst chatEventInputSchema = z.discriminatedUnion(\"chunkType\", [\n errorChunkSchema,\n z.extend(conversationUpdateChunkSchema, chatEventSharedSchema),\n z.extend(modelChunkSchema, chatEventSharedSchema),\n z.extend(toolRequestChunkSchema, chatEventSharedSchema),\n z.extend(toolResponseChunkSchema, chatEventSharedSchema),\n z.extend(userMessageChunkSchema, chatEventSharedSchema),\n]);\n\nconst chatEventOutputSharedSchema = z.extend(\n z.omit(z.strictObject(chatEventSharedSchema), { createdAt: true, messageId: true }),\n {\n createdAt: z.instanceof(Temporal.Instant),\n id: z.string(),\n },\n);\n\nconst chatEventOutputSchema = z.union([\n z.extend(chatEventOutputSharedSchema, {\n content: conversationUpdateChunkSchema,\n role: z.literal(\"agent\"),\n }),\n z.extend(chatEventOutputSharedSchema, { content: errorChunkSchema, role: z.literal(\"agent\") }),\n z.extend(chatEventOutputSharedSchema, { content: modelChunkSchema, role: z.literal(\"agent\") }),\n z.extend(chatEventOutputSharedSchema, {\n content: toolRequestChunkSchema,\n role: z.literal(\"agent\"),\n }),\n z.extend(chatEventOutputSharedSchema, {\n content: toolResponseChunkSchema,\n role: z.literal(\"agent\"),\n }),\n z.extend(chatEventOutputSharedSchema, {\n content: userMessageChunkSchema,\n role: z.literal(\"user\"),\n }),\n]);\n\nexport const chatEventCodec = z.codec(chatEventInputSchema, chatEventOutputSchema, {\n decode(v) {\n if (v.chunkType === \"error\") {\n return {\n content: {\n chunkType: \"error\",\n message: v.message,\n },\n conversationId: \"\",\n createdAt: Temporal.Now.instant(),\n id: \"\",\n modelName: \"\",\n modelProviderId: \"\",\n role: \"agent\",\n runId: \"\",\n } as const satisfies z.output<typeof chatEventOutputSchema>;\n }\n\n const sharedProperties = {\n conversationId: v.conversationId,\n createdAt: Temporal.Instant.from(v.createdAt),\n id: v.messageId,\n modelName: v.modelName,\n modelProviderId: v.modelProviderId,\n runId: v.runId,\n } as const satisfies Omit<z.output<typeof chatEventOutputSchema>, \"content\" | \"role\">;\n\n switch (v.chunkType) {\n case \"conversationUpdate\":\n return {\n ...sharedProperties,\n content: {\n chunkType: \"conversationUpdate\",\n summary: v.summary,\n title: v.title,\n },\n role: \"agent\",\n } as const satisfies z.output<typeof chatEventOutputSchema>;\n case \"model\":\n return {\n ...sharedProperties,\n content: {\n chunkType: \"model\",\n name: v.name,\n result: v.result,\n },\n role: \"agent\",\n } as const satisfies z.output<typeof chatEventOutputSchema>;\n case \"toolRequest\":\n return {\n ...sharedProperties,\n content: {\n arguments: v.arguments,\n callId: v.callId,\n chunkType: \"toolRequest\",\n commentary: v.commentary,\n name: v.name,\n summarizedTitle: v.summarizedTitle,\n },\n role: \"agent\",\n } as const satisfies z.output<typeof chatEventOutputSchema>;\n case \"toolResponse\":\n return {\n ...sharedProperties,\n content: {\n callId: v.callId,\n chunkType: \"toolResponse\",\n commentary: v.commentary,\n name: v.name,\n result: v.result,\n },\n role: \"agent\",\n } as const satisfies z.output<typeof chatEventOutputSchema>;\n case \"userMessage\":\n return {\n ...sharedProperties,\n content: { chunkType: \"userMessage\", text: v.text },\n role: \"user\",\n } as const satisfies z.output<typeof chatEventOutputSchema>;\n }\n },\n encode(v) {\n return {\n //@ts-ignore\n conversationId: v.conversationId,\n createdAt: v.createdAt.toString(),\n messageId: v.id,\n modelName: v.modelName,\n modelProviderId: v.modelProviderId,\n runId: v.runId,\n ...v.content,\n } as const satisfies z.infer<typeof chatEventInputSchema>;\n },\n});\n\nexport type ChatEvent = z.output<typeof chatEventCodec>;\nexport type ChatEventWithChunkType<T extends z.input<typeof chatEventCodec>[\"chunkType\"]> = Extract<\n ChatEvent,\n { content: { chunkType: T } }\n>;\n"]}
1
+ {"version":3,"file":"chatEventSchema.js","sourceRoot":"","sources":["../../../../src/enterprise/ai/chat/chatEventSchema.ts"],"names":[],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AO,EAAE,EAAE,SAAS,EAAE,aAAa,EAAE,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE;oBACnD,IAAI,EAAE,MAAM;iBAC6C,CAAC;QAChE,CAAC;IACH,CAAC;IACD,MAAM,CAAC,CAAC;QACN,OAAO;YACL,YAAY;YACZ,cAAc,EAAE,CAAC,CAAC,cAAc;YAChC,SAAS,EAAE,CAAC,CAAC,SAAS,CAAC,QAAQ,EAAE;YACjC,SAAS,EAAE,CAAC,CAAC,EAAE;YACf,SAAS,EAAE,CAAC,CAAC,SAAS;YACtB,eAAe,EAAE,CAAC,CAAC,eAAe;YAClC,KAAK,EAAE,CAAC,CAAC,KAAK;YACd,GAAG,CAAC,CAAC,OAAO;SAC2C,CAAC;IAC5D,CAAC;CACF,CAAC,CAAC","sourcesContent":["/*\n * Copyright (C) 2024-2025 Dremio Corporation\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { Temporal } from \"temporal-polyfill\";\nimport * as z from \"zod/mini\";\n\n/**\n * Shared chat event fields for all responses (except error response)\n * @deprecated\n */\nconst chatEventV1SharedSchema = {\n createdAt: z.string().check(z.iso.datetime()),\n modelName: z.string().check(z.trim(), z.minLength(1)),\n modelProviderId: z.string().check(z.trim(), z.minLength(1)),\n sessionId: z.string().check(z.trim(), z.minLength(1)),\n};\n\n/**\n * Shared chat event fields for all responses (except error response)\n */\nconst chatEventSharedSchema = {\n conversationId: z.string(),\n createdAt: z.string().check(z.iso.datetime()),\n messageId: z.string(),\n modelName: z.string(),\n modelProviderId: z.string(),\n runId: z.string(),\n};\n\nconst errorChunkSchema = z.object({\n chunkType: z.literal(\"error\"),\n /**\n * If the message is not present or very short, it won't be useful to show\n * anyways, so the parser should throw to the generic response error handler.\n */\n message: z.string().check(z.trim(), z.minLength(2)),\n});\n\nconst conversationUpdateChunkSchema = z.object({\n chunkType: z.literal(\"conversationUpdate\"),\n summary: z.optional(z.string()),\n title: z.optional(z.string()),\n});\n\nconst endOfStreamChunkSchema = z.object({\n chunkType: z.literal(\"endOfStream\"),\n});\n\nconst modelChunkSchema = z.object({\n chunkType: z.literal(\"model\"),\n name: z.string().check(z.trim(), z.minLength(1)),\n /**\n * It's useful to indicate (to the type system) that the result contents\n * at this stage can only be data (de)serializable from JSON. Because\n * model responses can vary depending on runtime flags and edition, we can\n * leave this field generic and refine it more precisely in later stages.\n */\n result: z.record(z.string(), z.unknown()),\n});\n\nconst toolRequestChunkSchema = z.object({\n arguments: z.record(z.string(), z.unknown()),\n callId: z.string().check(z.trim(), z.minLength(1)),\n chunkType: z.literal(\"toolRequest\"),\n commentary: z.optional(z.string()),\n name: z.string(),\n summarizedTitle: z.optional(z.string()),\n});\n\nconst toolResponseChunkSchema = z.object({\n callId: z.string(),\n chunkType: z.literal(\"toolResponse\"),\n commentary: z.optional(z.string()),\n name: z.string(),\n result: z.record(z.string(), z.unknown()),\n});\n\nconst userMessageChunkSchema = z.object({\n chunkType: z.literal(\"userMessage\"),\n text: z.string(),\n});\n\n/**\n * Captures all of the possible chunks of data that the Agent can reply\n * with. We're lenient to extra/unexpected fields being present (`z.object`\n * instead of `z.strictObject`), but for the fields that are expected,\n * we validate that they match the most general type category they belong in.\n * For example, it's good enough to validate that the `callId` is a string\n * for type safety -- we don't need to verify that it's also a `z.uuid()`.\n * @deprecated\n */\nexport const chatEventV1Schema = z.discriminatedUnion(\"chunkType\", [\n errorChunkSchema,\n z.extend(modelChunkSchema, chatEventV1SharedSchema),\n z.extend(toolRequestChunkSchema, chatEventV1SharedSchema),\n z.extend(toolResponseChunkSchema, chatEventV1SharedSchema),\n]);\n\n/**\n * @deprecated\n */\nexport type ChatEventV1 = z.infer<typeof chatEventV1Schema>;\n\nconst chatEventInputSchema = z.discriminatedUnion(\"chunkType\", [\n errorChunkSchema,\n z.extend(endOfStreamChunkSchema, chatEventSharedSchema),\n z.extend(conversationUpdateChunkSchema, chatEventSharedSchema),\n z.extend(modelChunkSchema, chatEventSharedSchema),\n z.extend(toolRequestChunkSchema, chatEventSharedSchema),\n z.extend(toolResponseChunkSchema, chatEventSharedSchema),\n z.extend(userMessageChunkSchema, chatEventSharedSchema),\n]);\n\nconst chatEventOutputSharedSchema = z.extend(\n z.omit(z.strictObject(chatEventSharedSchema), { createdAt: true, messageId: true }),\n {\n createdAt: z.instanceof(Temporal.Instant),\n id: z.string(),\n },\n);\n\nconst chatEventOutputSchema = z.union([\n z.extend(chatEventOutputSharedSchema, {\n content: conversationUpdateChunkSchema,\n role: z.literal(\"agent\"),\n }),\n z.extend(chatEventOutputSharedSchema, { content: errorChunkSchema, role: z.literal(\"agent\") }),\n z.extend(chatEventOutputSharedSchema, {\n content: endOfStreamChunkSchema,\n role: z.literal(\"agent\"),\n }),\n z.extend(chatEventOutputSharedSchema, { content: modelChunkSchema, role: z.literal(\"agent\") }),\n z.extend(chatEventOutputSharedSchema, {\n content: toolRequestChunkSchema,\n role: z.literal(\"agent\"),\n }),\n z.extend(chatEventOutputSharedSchema, {\n content: toolResponseChunkSchema,\n role: z.literal(\"agent\"),\n }),\n z.extend(chatEventOutputSharedSchema, {\n content: userMessageChunkSchema,\n role: z.literal(\"user\"),\n }),\n]);\n\nexport const chatEventCodec = z.codec(chatEventInputSchema, chatEventOutputSchema, {\n decode(v) {\n if (v.chunkType === \"error\") {\n return {\n content: {\n chunkType: \"error\",\n message: v.message,\n },\n conversationId: \"\",\n createdAt: Temporal.Now.instant(),\n id: \"\",\n modelName: \"\",\n modelProviderId: \"\",\n role: \"agent\",\n runId: \"\",\n } as const satisfies z.output<typeof chatEventOutputSchema>;\n }\n\n const sharedProperties = {\n conversationId: v.conversationId,\n createdAt: Temporal.Instant.from(v.createdAt),\n id: v.messageId,\n modelName: v.modelName,\n modelProviderId: v.modelProviderId,\n runId: v.runId,\n } as const satisfies Omit<z.output<typeof chatEventOutputSchema>, \"content\" | \"role\">;\n\n switch (v.chunkType) {\n case \"conversationUpdate\":\n return {\n ...sharedProperties,\n content: {\n chunkType: \"conversationUpdate\",\n summary: v.summary,\n title: v.title,\n },\n role: \"agent\",\n } as const satisfies z.output<typeof chatEventOutputSchema>;\n case \"endOfStream\":\n return {\n ...sharedProperties,\n content: {\n chunkType: \"endOfStream\",\n },\n role: \"agent\",\n } as const satisfies z.output<typeof chatEventOutputSchema>;\n case \"model\":\n return {\n ...sharedProperties,\n content: {\n chunkType: \"model\",\n name: v.name,\n result: v.result,\n },\n role: \"agent\",\n } as const satisfies z.output<typeof chatEventOutputSchema>;\n case \"toolRequest\":\n return {\n ...sharedProperties,\n content: {\n arguments: v.arguments,\n callId: v.callId,\n chunkType: \"toolRequest\",\n commentary: v.commentary,\n name: v.name,\n summarizedTitle: v.summarizedTitle,\n },\n role: \"agent\",\n } as const satisfies z.output<typeof chatEventOutputSchema>;\n case \"toolResponse\":\n return {\n ...sharedProperties,\n content: {\n callId: v.callId,\n chunkType: \"toolResponse\",\n commentary: v.commentary,\n name: v.name,\n result: v.result,\n },\n role: \"agent\",\n } as const satisfies z.output<typeof chatEventOutputSchema>;\n case \"userMessage\":\n return {\n ...sharedProperties,\n content: { chunkType: \"userMessage\", text: v.text },\n role: \"user\",\n } as const satisfies z.output<typeof chatEventOutputSchema>;\n }\n },\n encode(v) {\n return {\n //@ts-ignore\n conversationId: v.conversationId,\n createdAt: v.createdAt.toString(),\n messageId: v.id,\n modelName: v.modelName,\n modelProviderId: v.modelProviderId,\n runId: v.runId,\n ...v.content,\n } as const satisfies z.infer<typeof chatEventInputSchema>;\n },\n});\n\nexport type ChatEvent = z.output<typeof chatEventCodec>;\nexport type ChatEventWithChunkType<T extends z.input<typeof chatEventCodec>[\"chunkType\"]> = Extract<\n ChatEvent,\n { content: { chunkType: T } }\n>;\n"]}
@@ -58,6 +58,17 @@ export declare class AgentConversation implements AgentConversationProperties {
58
58
  message: string;
59
59
  };
60
60
  role: "agent";
61
+ } | {
62
+ conversationId: string;
63
+ modelName: string;
64
+ modelProviderId: string;
65
+ runId: string;
66
+ createdAt: import("temporal-polyfill").Temporal.Instant;
67
+ id: string;
68
+ content: {
69
+ chunkType: "endOfStream";
70
+ };
71
+ role: "agent";
61
72
  } | {
62
73
  conversationId: string;
63
74
  modelName: string;
@@ -140,6 +151,17 @@ export declare class AgentConversation implements AgentConversationProperties {
140
151
  message: string;
141
152
  };
142
153
  role: "agent";
154
+ } | {
155
+ conversationId: string;
156
+ modelName: string;
157
+ modelProviderId: string;
158
+ runId: string;
159
+ createdAt: import("temporal-polyfill").Temporal.Instant;
160
+ id: string;
161
+ content: {
162
+ chunkType: "endOfStream";
163
+ };
164
+ role: "agent";
143
165
  } | {
144
166
  conversationId: string;
145
167
  modelName: string;
@@ -2,6 +2,7 @@ import type { UserChatMessage } from "../chat/UserChatMessage.ts";
2
2
  import { AgentConversation } from "./AgentConversation.ts";
3
3
  import type { ConversationExchange } from "./reduceChatEvents.ts";
4
4
  import type { SonarV4Config } from "../../../common/Config.ts";
5
+ import { type ChatEvent } from "../chat/chatEventSchema.ts";
5
6
  export type ConversationMachineInput = {
6
7
  conversationId: string | null;
7
8
  };
@@ -22,6 +23,13 @@ export type ConversationMachineEvents = {
22
23
  } | {
23
24
  type: "REFRESH_HISTORY";
24
25
  };
26
+ export type ConversationMachineEmitted = {
27
+ type: "chatEventReceived";
28
+ chatEvent: ChatEvent;
29
+ } | {
30
+ type: "conversationCreated";
31
+ id: string;
32
+ };
25
33
  export declare const createConversationMachine: (config: SonarV4Config) => import("xstate").StateMachine<ConversationMachineContext, {
26
34
  type: "STOP_RUN";
27
35
  } | {
@@ -73,6 +81,17 @@ export declare const createConversationMachine: (config: SonarV4Config) => impor
73
81
  message: string;
74
82
  };
75
83
  role: "agent";
84
+ } | {
85
+ conversationId: string;
86
+ modelName: string;
87
+ modelProviderId: string;
88
+ runId: string;
89
+ createdAt: import("temporal-polyfill").Temporal.Instant;
90
+ id: string;
91
+ content: {
92
+ chunkType: "endOfStream";
93
+ };
94
+ role: "agent";
76
95
  } | {
77
96
  conversationId: string;
78
97
  modelName: string;
@@ -169,6 +188,17 @@ export declare const createConversationMachine: (config: SonarV4Config) => impor
169
188
  message: string;
170
189
  };
171
190
  role: "agent";
191
+ } | {
192
+ conversationId: string;
193
+ modelName: string;
194
+ modelProviderId: string;
195
+ runId: string;
196
+ createdAt: import("temporal-polyfill").Temporal.Instant;
197
+ id: string;
198
+ content: {
199
+ chunkType: "endOfStream";
200
+ };
201
+ role: "agent";
172
202
  } | {
173
203
  conversationId: string;
174
204
  modelName: string;
@@ -272,6 +302,17 @@ export declare const createConversationMachine: (config: SonarV4Config) => impor
272
302
  message: string;
273
303
  };
274
304
  role: "agent";
305
+ } | {
306
+ conversationId: string;
307
+ modelName: string;
308
+ modelProviderId: string;
309
+ runId: string;
310
+ createdAt: import("temporal-polyfill").Temporal.Instant;
311
+ id: string;
312
+ content: {
313
+ chunkType: "endOfStream";
314
+ };
315
+ role: "agent";
275
316
  } | {
276
317
  conversationId: string;
277
318
  modelName: string;
@@ -383,6 +424,17 @@ export declare const createConversationMachine: (config: SonarV4Config) => impor
383
424
  message: string;
384
425
  };
385
426
  role: "agent";
427
+ } | {
428
+ conversationId: string;
429
+ modelName: string;
430
+ modelProviderId: string;
431
+ runId: string;
432
+ createdAt: import("temporal-polyfill").Temporal.Instant;
433
+ id: string;
434
+ content: {
435
+ chunkType: "endOfStream";
436
+ };
437
+ role: "agent";
386
438
  } | {
387
439
  conversationId: string;
388
440
  modelName: string;
@@ -483,6 +535,17 @@ export declare const createConversationMachine: (config: SonarV4Config) => impor
483
535
  message: string;
484
536
  };
485
537
  role: "agent";
538
+ } | {
539
+ conversationId: string;
540
+ modelName: string;
541
+ modelProviderId: string;
542
+ runId: string;
543
+ createdAt: import("temporal-polyfill").Temporal.Instant;
544
+ id: string;
545
+ content: {
546
+ chunkType: "endOfStream";
547
+ };
548
+ role: "agent";
486
549
  } | {
487
550
  conversationId: string;
488
551
  modelName: string;
@@ -598,6 +661,17 @@ export declare const createConversationMachine: (config: SonarV4Config) => impor
598
661
  message: string;
599
662
  };
600
663
  role: "agent";
664
+ } | {
665
+ conversationId: string;
666
+ modelName: string;
667
+ modelProviderId: string;
668
+ runId: string;
669
+ createdAt: import("temporal-polyfill").Temporal.Instant;
670
+ id: string;
671
+ content: {
672
+ chunkType: "endOfStream";
673
+ };
674
+ role: "agent";
601
675
  } | {
602
676
  conversationId: string;
603
677
  modelName: string;
@@ -661,7 +735,13 @@ export declare const createConversationMachine: (config: SonarV4Config) => impor
661
735
  id: string | undefined;
662
736
  }, never, never, never, "uninitialized" | "creating_conversation" | "retrieving_history" | "idle" | "submitting_message" | "retrieve_history_failed" | "submitting_message_failed" | {
663
737
  streaming: "monitoring_replies" | "stopping";
664
- }, string, ConversationMachineInput, import("xstate").NonReducibleUnknown, import("xstate").EventObject, import("xstate").MetaObject, {
738
+ }, string, ConversationMachineInput, import("xstate").NonReducibleUnknown, {
739
+ type: "chatEventReceived";
740
+ chatEvent: ChatEvent;
741
+ } | {
742
+ type: "conversationCreated";
743
+ id: string;
744
+ }, import("xstate").MetaObject, {
665
745
  id: "agentConversation";
666
746
  states: {
667
747
  readonly creating_conversation: {};
@@ -13,7 +13,7 @@
13
13
  * See the License for the specific language governing permissions and
14
14
  * limitations under the License.
15
15
  */
16
- import { setup, fromObservable, fromPromise, assign } from "xstate";
16
+ import { setup, fromObservable, fromPromise, assign, enqueueActions, emit } from "xstate";
17
17
  import { AgentConversation } from "./AgentConversation.js";
18
18
  import { createConversation, createConversationCodec } from "./methods/createConversation.js";
19
19
  import * as z from "zod/mini";
@@ -81,6 +81,7 @@ export const createConversationMachine = (config) => setup({
81
81
  },
82
82
  types: {
83
83
  context: {},
84
+ emitted: {},
84
85
  events: {},
85
86
  input: {},
86
87
  },
@@ -102,21 +103,27 @@ export const createConversationMachine = (config) => setup({
102
103
  }),
103
104
  onDone: [
104
105
  {
105
- actions: assign({
106
- conversationId: ({ event }) => event.output.id,
107
- // Add the message attempt into the snapshot manually
108
- conversationSnapshot: ({ context, event }) => [
109
- ...(context.conversationSnapshot ?? []),
110
- {
111
- id: event.output.currentRunId,
112
- messages: new Map(),
113
- submittedUserMessage: context.messageAttempt,
114
- toolCalls: new Map(),
115
- },
116
- ],
117
- currentRunId: ({ event }) => event.output.currentRunId,
118
- messageAttempt: undefined,
119
- }),
106
+ actions: [
107
+ assign({
108
+ conversationId: ({ event }) => event.output.id,
109
+ // Add the message attempt into the snapshot manually
110
+ conversationSnapshot: ({ context, event }) => [
111
+ ...(context.conversationSnapshot ?? []),
112
+ {
113
+ id: event.output.currentRunId,
114
+ messages: new Map(),
115
+ submittedUserMessage: context.messageAttempt,
116
+ toolCalls: new Map(),
117
+ },
118
+ ],
119
+ currentRunId: ({ event }) => event.output.currentRunId,
120
+ messageAttempt: undefined,
121
+ }),
122
+ emit(({ event }) => ({
123
+ id: event.output.id,
124
+ type: "conversationCreated",
125
+ })),
126
+ ],
120
127
  // Don't transition to streaming in a situation where we didn't receive a currentRunId
121
128
  guard: ({ event }) => !!event.output.currentRunId,
122
129
  target: "streaming",
@@ -224,16 +231,19 @@ export const createConversationMachine = (config) => setup({
224
231
  target: "idle",
225
232
  },
226
233
  onSnapshot: {
227
- actions: assign({
228
- conversationSnapshot: ({ context, event }) => {
229
- const chatEvent = event.snapshot.context;
230
- if (!chatEvent) {
231
- return context.conversationSnapshot;
232
- }
233
- return AgentConversation.reduceChatEvents(context.conversationSnapshot, [
234
- chatEvent,
235
- ]);
236
- },
234
+ actions: enqueueActions(({ context, enqueue, event }) => {
235
+ const chatEvent = event.snapshot.context;
236
+ if (!chatEvent) {
237
+ return;
238
+ }
239
+ const nextSnapshot = AgentConversation.reduceChatEvents(context.conversationSnapshot, [chatEvent]);
240
+ enqueue.assign({
241
+ conversationSnapshot: nextSnapshot,
242
+ });
243
+ enqueue.emit({
244
+ chatEvent,
245
+ type: "chatEventReceived",
246
+ });
237
247
  }),
238
248
  },
239
249
  src: "streamRun",
@@ -1 +1 @@
1
- {"version":3,"file":"createConversationMachine.js","sourceRoot":"","sources":["../../../../src/enterprise/ai/conversations/createConversationMachine.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAAE,KAAK,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAEpE,OAAO,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAG3D,OAAO,EAAE,kBAAkB,EAAE,uBAAuB,EAAE,MAAM,iCAAiC,CAAC;AAC9F,OAAO,KAAK,CAAC,MAAM,UAAU,CAAC;AAC9B,OAAO,EAAE,oBAAoB,EAAE,MAAM,mCAAmC,CAAC;AACzE,OAAO,EAAE,2BAA2B,EAAE,MAAM,0CAA0C,CAAC;AACvF,OAAO,EAAE,iBAAiB,EAAE,MAAM,gCAAgC,CAAC;AACnE,OAAO,EAAE,eAAe,EAAE,MAAM,8BAA8B,CAAC;AAC/D,OAAO,EAAE,eAAe,EAAE,MAAM,8BAA8B,CAAC;AAC/D,OAAO,EAAE,mBAAmB,EAAE,MAAM,wCAAwC,CAAC;AAC7E,OAAO,EAAE,GAAG,EAAE,MAAM,MAAM,CAAC;AAC3B,OAAO,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAC;AAmB5D,MAAM,CAAC,MAAM,yBAAyB,GAAG,CAAC,MAAqB,EAAE,EAAE,CACjE,KAAK,CAAC;IACJ,MAAM,EAAE;QACN,kBAAkB,EAAE,WAAW,CAC7B,CAAC,EAAE,KAAK,EAAsD,EAAE,EAAE,CAChE,kBAAkB,CAAC,MAAM,CAAC,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,EAAE;YAChD,IAAI,MAAM,CAAC,KAA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AC7B;gBACD,aAAa,EAAE;oBACb,OAAO,EAAE,MAAM,CAAC,EAAE,SAAS,EAAE,SAAS,EAAE,CAAC;oBACzC,MAAM,EAAE,oBAAoB;iBAC7B;gBACD,mBAAmB,EAAE;oBACnB,OAAO,EAAE,MAAM,CAAC;wBACd,SAAS,EAAE,SAAS;wBACpB,cAAc,EAAE,CAAC,EAAE,KAAK,EAAE,EAAE,EAAE,CAAC,KAAK,CAAC,OAAO;qBAC7C,CAAC;oBACF,MAAM,EAAE,oBAAoB;iBAC7B;aACF;SACF;QACD,aAAa,EAAE;YACb,MAAM,EAAE;gBACN,KAAK,EAAE,CAAC,EAAE,OAAO,EAAE,EAAE,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,cAAc;gBAChD,MAAM,EAAE,oBAAoB;aAC7B;YACD,WAAW,EACT,gFAAgF;YAClF,EAAE,EAAE;gBACF,mBAAmB,EAAE;oBACnB,OAAO,EAAE,MAAM,CAAC;wBACd,cAAc,EAAE,CAAC,EAAE,KAAK,EAAE,EAAE,EAAE,CAAC,KAAK,CAAC,OAAO;qBAC7C,CAAC;oBACF,MAAM,EAAE,uBAAuB;iBAChC;aACF;SACF;KACF;CACF,CAAC,CAAC","sourcesContent":["/*\n * Copyright (C) 2024-2025 Dremio Corporation\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { setup, fromObservable, fromPromise, assign } from \"xstate\";\nimport type { UserChatMessage } from \"../chat/UserChatMessage.ts\";\nimport { AgentConversation } from \"./AgentConversation.ts\";\nimport type { ConversationExchange } from \"./reduceChatEvents.ts\";\nimport type { SonarV4Config } from \"../../../common/Config.ts\";\nimport { createConversation, createConversationCodec } from \"./methods/createConversation.ts\";\nimport * as z from \"zod/mini\";\nimport { retrieveConversation } from \"./methods/retrieveConversation.ts\";\nimport { retrieveConversationHistory } from \"./methods/retrieveConversationHistory.ts\";\nimport { createExchangeRun } from \"./methods/createExchangeRun.ts\";\nimport { stopExchangeRun } from \"./methods/stopExchangeRun.ts\";\nimport { streamRunEvents } from \"./methods/streamRunEvents.ts\";\nimport { fromTextEventStream } from \"../../../common/fromTextEventStream.ts\";\nimport { map } from \"rxjs\";\nimport { chatEventCodec } from \"../chat/chatEventSchema.ts\";\n\nexport type ConversationMachineInput = {\n conversationId: string | null;\n};\n\nexport type ConversationMachineContext = {\n conversationId: string | null;\n conversationSnapshot?: ConversationExchange[];\n currentRunId?: string | null;\n lastError?: unknown;\n messageAttempt?: UserChatMessage;\n};\nexport type ConversationMachineEvents =\n | { type: \"STOP_RUN\" }\n | { type: \"SUBMIT_USER_MESSAGE\"; message: UserChatMessage }\n | { type: \"RETRY_MESSAGE\" }\n | { type: \"REFRESH_HISTORY\" };\n\nexport const createConversationMachine = (config: SonarV4Config) =>\n setup({\n actors: {\n createConversation: fromPromise(\n ({ input }: { input: z.input<typeof createConversationCodec> }) =>\n createConversation(config)(input).then((result) => {\n if (result.isErr()) {\n throw result.error;\n }\n return result.value;\n }),\n ),\n retrieveConversation: fromPromise(\n ({ input, signal }: { input: { conversationId: string }; signal: AbortSignal }) =>\n retrieveConversation(config)(input.conversationId, { signal }).then((result) => {\n if (result.isErr()) {\n throw result.error;\n }\n return result.value;\n }),\n ),\n retrieveHistory: fromPromise(\n ({ input, signal }: { input: { conversationId: string }; signal: AbortSignal }) =>\n retrieveConversationHistory(config)(input.conversationId, { signal }).then((result) => {\n if (result.isErr()) {\n throw result.error;\n }\n return result.value;\n }),\n ),\n retrieveState: fromPromise(\n async ({ input, signal }: { input: { conversationId: string }; signal: AbortSignal }) => {\n const [conversation, history] = await Promise.all([\n retrieveConversation(config)(input.conversationId, { signal }).then((result) => {\n if (result.isErr()) {\n throw result.error;\n }\n return result.value;\n }),\n retrieveConversationHistory(config)(input.conversationId, { signal }).then((result) => {\n if (result.isErr()) {\n throw result.error;\n }\n return result.value;\n }),\n ]);\n return {\n conversation,\n history,\n };\n },\n ),\n startRun: fromPromise(\n ({ input }: { input: { conversationId: string; message: UserChatMessage } }) =>\n createExchangeRun(config)(input.conversationId, { message: input.message }).then(\n (result) => {\n if (result.isErr()) {\n throw result.error;\n }\n return result.value;\n },\n ),\n ),\n stopRun: fromPromise(({ input }: { input: { conversationId: string; runId: string } }) =>\n stopExchangeRun(config)(input.conversationId, input.runId).then((result) => {\n if (result.isErr()) {\n throw result.error;\n }\n return result.value;\n }),\n ),\n streamRun: fromObservable(({ input }: { input: { conversationId: string; runId: string } }) =>\n fromTextEventStream(({ signal }) =>\n streamRunEvents(config)(\n { conversationId: input.conversationId, runId: input.runId },\n { signal },\n ),\n ).pipe(\n map((event) =>\n z.decode(chatEventCodec, JSON.parse(event.data) as z.input<typeof chatEventCodec>),\n ),\n ),\n ),\n },\n types: {\n context: {} as ConversationMachineContext,\n events: {} as ConversationMachineEvents,\n input: {} as ConversationMachineInput,\n },\n }).createMachine({\n context: ({ input }) => ({\n conversationId: input.conversationId,\n conversationSnapshot: undefined,\n currentRunId: undefined,\n messageAttempt: undefined,\n }),\n id: \"agentConversation\",\n initial: \"uninitialized\",\n states: {\n creating_conversation: {\n description: \"Creating a new conversation object\",\n invoke: {\n input: ({ context }) => ({\n message: context.messageAttempt!,\n }),\n onDone: [\n {\n actions: assign({\n conversationId: ({ event }) => event.output.id,\n // Add the message attempt into the snapshot manually\n conversationSnapshot: ({ context, event }) => [\n ...(context.conversationSnapshot ?? []),\n {\n id: event.output.currentRunId!,\n messages: new Map(),\n submittedUserMessage: context.messageAttempt,\n toolCalls: new Map(),\n } satisfies ConversationExchange,\n ],\n currentRunId: ({ event }) => event.output.currentRunId,\n messageAttempt: undefined,\n }),\n // Don't transition to streaming in a situation where we didn't receive a currentRunId\n guard: ({ event }) => !!event.output.currentRunId,\n target: \"streaming\",\n },\n /**\n * Fall back to the retrieving_history state if we didn't receive a currentRunId\n */\n {\n actions: assign({\n conversationId: ({ event }) => event.output.id,\n currentRunId: ({ event }) => event.output.currentRunId,\n messageAttempt: undefined,\n }),\n target: \"retrieving_history\",\n },\n ],\n onError: {\n target: \"uninitialized\",\n },\n src: \"createConversation\",\n },\n },\n idle: {\n description: \"Waiting for user input to start the next reply stream\",\n on: {\n REFRESH_HISTORY: {\n target: \"retrieving_history\",\n },\n SUBMIT_USER_MESSAGE: {\n actions: assign({\n messageAttempt: ({ event }) => event.message,\n }),\n target: \"submitting_message\",\n },\n },\n },\n retrieve_history_failed: {\n on: {\n REFRESH_HISTORY: {\n actions: assign({ lastError: undefined }),\n target: \"retrieving_history\",\n },\n },\n },\n retrieving_history: {\n description: \"Retrieving latest copy of conversation history\",\n invoke: {\n input: ({ context }) => ({ conversationId: context.conversationId! }),\n onDone: [\n {\n actions: assign({\n conversationSnapshot: ({ context, event }) =>\n AgentConversation.reduceChatEvents(\n context.conversationSnapshot,\n event.output.history.data,\n ),\n currentRunId: ({ event }) => event.output.conversation.currentRunId,\n }),\n guard: ({ event }) => !!event.output.conversation.currentRunId,\n target: \"streaming\",\n },\n {\n actions: assign({\n conversationSnapshot: ({ context, event }) =>\n AgentConversation.reduceChatEvents(\n context.conversationSnapshot,\n event.output.history.data,\n ),\n currentRunId: ({ event }) => event.output.conversation.currentRunId,\n }),\n target: \"idle\",\n },\n ],\n onError: [\n {\n guard: ({ event }) => {\n const e = event.error as Error & { code?: string };\n return (\n e?.name === \"AbortError\" ||\n e?.message?.includes(\"aborted\") ||\n e?.code === \"ABORT_ERR\"\n );\n },\n target: \"uninitialized\",\n },\n {\n actions: assign({\n lastError: ({ event }) => event.error,\n }),\n target: \"retrieve_history_failed\",\n },\n ],\n src: \"retrieveState\",\n },\n },\n streaming: {\n description: \"Manages monitoring and canceling the current run\",\n initial: \"monitoring_replies\",\n invoke: {\n input: ({ context }) => {\n if (!context.currentRunId) {\n throw new Error(\"Attempted to stream without a runId\");\n }\n return {\n conversationId: context.conversationId!,\n runId: context.currentRunId,\n };\n },\n onDone: {\n actions: assign({ currentRunId: null }),\n target: \"idle\",\n },\n onError: {\n actions: assign({ currentRunId: null }),\n target: \"idle\",\n },\n onSnapshot: {\n actions: assign({\n conversationSnapshot: ({ context, event }) => {\n const chatEvent = event.snapshot.context;\n\n if (!chatEvent) {\n return context.conversationSnapshot;\n }\n\n return AgentConversation.reduceChatEvents(context.conversationSnapshot, [\n chatEvent,\n ]);\n },\n }),\n },\n src: \"streamRun\",\n },\n states: {\n monitoring_replies: {\n description: \"Attached to the run event stream\",\n on: {\n STOP_RUN: {\n target: \"stopping\",\n },\n },\n },\n stopping: {\n description: \"Background cancelation of the run stream\",\n invoke: {\n input: ({ context }) => ({\n conversationId: context.conversationId!,\n runId: context.currentRunId!,\n }),\n onDone: {\n actions: assign({ currentRunId: undefined }),\n target: \"#agentConversation.idle\",\n },\n onError: {\n target: \"#agentConversation.retrieving_history\",\n },\n src: \"stopRun\",\n },\n },\n },\n },\n submitting_message: {\n description: \"Submitting message to start a new reply stream\",\n invoke: {\n input: ({ context, event }) => {\n switch (event.type) {\n case \"RETRY_MESSAGE\":\n if (!context.messageAttempt) {\n throw new Error(\"Tried to resubmit nonexistent message\");\n }\n return {\n conversationId: context.conversationId!,\n message: context.messageAttempt,\n };\n case \"SUBMIT_USER_MESSAGE\":\n return {\n conversationId: context.conversationId!,\n message: event.message,\n };\n }\n throw new Error(`Unexpected event type received in submitting_message: ${event.type}`);\n },\n onDone: {\n actions: assign({\n // Add the message attempt into the snapshot manually\n conversationSnapshot: ({ context, event }) => [\n ...(context.conversationSnapshot ?? []),\n {\n id: event.output.currentRunId,\n messages: new Map(),\n submittedUserMessage: context.messageAttempt || undefined,\n toolCalls: new Map(),\n } satisfies ConversationExchange,\n ],\n currentRunId: ({ event }) => event.output.currentRunId,\n messageAttempt: undefined,\n }),\n target: \"streaming\",\n },\n onError: {\n actions: assign({\n currentRunId: () => null,\n lastError: ({ event }) => event.error,\n }),\n target: \"submitting_message_failed\",\n },\n src: \"startRun\",\n },\n },\n submitting_message_failed: {\n description: \"Failed to submit message\",\n on: {\n REFRESH_HISTORY: {\n actions: assign({ lastError: undefined }),\n target: \"retrieving_history\",\n },\n RETRY_MESSAGE: {\n actions: assign({ lastError: undefined }),\n target: \"submitting_message\",\n },\n SUBMIT_USER_MESSAGE: {\n actions: assign({\n lastError: undefined,\n messageAttempt: ({ event }) => event.message,\n }),\n target: \"submitting_message\",\n },\n },\n },\n uninitialized: {\n always: {\n guard: ({ context }) => !!context.conversationId,\n target: \"retrieving_history\",\n },\n description:\n \"Entry state that the machine remains in when no conversation has been provided\",\n on: {\n SUBMIT_USER_MESSAGE: {\n actions: assign({\n messageAttempt: ({ event }) => event.message,\n }),\n target: \"creating_conversation\",\n },\n },\n },\n },\n });\n\nexport type AgentConversationMachine = ReturnType<typeof createConversationMachine>;\n"]}
1
+ {"version":3,"file":"createConversationMachine.js","sourceRoot":"","sources":["../../../../src/enterprise/ai/conversations/createConversationMachine.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAAE,KAAK,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,EAAE,cAAc,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAE1F,OAAO,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAG3D,OAAO,EAAE,kBAAkB,EAAE,uBAAuB,EAAE,MAAM,iCAAiC,CAAC;AAC9F,OAAO,KAAK,CAAC,MAAM,UAAU,CAAC;AAC9B,OAAO,EAAE,oBAAoB,EAAE,MAAM,mCAAmC,CAAC;AACzE,OAAO,EAAE,2BAA2B,EAAE,MAAM,0CAA0C,CAAC;AACvF,OAAO,EAAE,iBAAiB,EAAE,MAAM,gCAAgC,CAAC;AACnE,OAAO,EAAE,eAAe,EAAE,MAAM,8BAA8B,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E,CAAC,EAAE,OAAO,EAAE,EAAE,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,cAAc;gBAChD,MAAM,EAAE,oBAAoB;aAC7B;YACD,WAAW,EACT,gFAAgF;YAClF,EAAE,EAAE;gBACF,mBAAmB,EAAE;oBACnB,OAAO,EAAE,MAAM,CAAC;wBACd,cAAc,EAAE,CAAC,EAAE,KAAK,EAAE,EAAE,EAAE,CAAC,KAAK,CAAC,OAAO;qBAC7C,CAAC;oBACF,MAAM,EAAE,uBAAuB;iBAChC;aACF;SACF;KACF;CACF,CAAC,CAAC","sourcesContent":["/*\n * Copyright (C) 2024-2025 Dremio Corporation\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { setup, fromObservable, fromPromise, assign, enqueueActions, emit } from \"xstate\";\nimport type { UserChatMessage } from \"../chat/UserChatMessage.ts\";\nimport { AgentConversation } from \"./AgentConversation.ts\";\nimport type { ConversationExchange } from \"./reduceChatEvents.ts\";\nimport type { SonarV4Config } from \"../../../common/Config.ts\";\nimport { createConversation, createConversationCodec } from \"./methods/createConversation.ts\";\nimport * as z from \"zod/mini\";\nimport { retrieveConversation } from \"./methods/retrieveConversation.ts\";\nimport { retrieveConversationHistory } from \"./methods/retrieveConversationHistory.ts\";\nimport { createExchangeRun } from \"./methods/createExchangeRun.ts\";\nimport { stopExchangeRun } from \"./methods/stopExchangeRun.ts\";\nimport { streamRunEvents } from \"./methods/streamRunEvents.ts\";\nimport { fromTextEventStream } from \"../../../common/fromTextEventStream.ts\";\nimport { map } from \"rxjs\";\nimport { chatEventCodec, type ChatEvent } from \"../chat/chatEventSchema.ts\";\n\nexport type ConversationMachineInput = {\n conversationId: string | null;\n};\n\nexport type ConversationMachineContext = {\n conversationId: string | null;\n conversationSnapshot?: ConversationExchange[];\n currentRunId?: string | null;\n lastError?: unknown;\n messageAttempt?: UserChatMessage;\n};\nexport type ConversationMachineEvents =\n | { type: \"STOP_RUN\" }\n | { type: \"SUBMIT_USER_MESSAGE\"; message: UserChatMessage }\n | { type: \"RETRY_MESSAGE\" }\n | { type: \"REFRESH_HISTORY\" };\n\nexport type ConversationMachineEmitted =\n | {\n type: \"chatEventReceived\";\n chatEvent: ChatEvent;\n }\n | {\n type: \"conversationCreated\";\n id: string;\n };\n\nexport const createConversationMachine = (config: SonarV4Config) =>\n setup({\n actors: {\n createConversation: fromPromise(\n ({ input }: { input: z.input<typeof createConversationCodec> }) =>\n createConversation(config)(input).then((result) => {\n if (result.isErr()) {\n throw result.error;\n }\n return result.value;\n }),\n ),\n retrieveConversation: fromPromise(\n ({ input, signal }: { input: { conversationId: string }; signal: AbortSignal }) =>\n retrieveConversation(config)(input.conversationId, { signal }).then((result) => {\n if (result.isErr()) {\n throw result.error;\n }\n return result.value;\n }),\n ),\n retrieveHistory: fromPromise(\n ({ input, signal }: { input: { conversationId: string }; signal: AbortSignal }) =>\n retrieveConversationHistory(config)(input.conversationId, { signal }).then((result) => {\n if (result.isErr()) {\n throw result.error;\n }\n return result.value;\n }),\n ),\n retrieveState: fromPromise(\n async ({ input, signal }: { input: { conversationId: string }; signal: AbortSignal }) => {\n const [conversation, history] = await Promise.all([\n retrieveConversation(config)(input.conversationId, { signal }).then((result) => {\n if (result.isErr()) {\n throw result.error;\n }\n return result.value;\n }),\n retrieveConversationHistory(config)(input.conversationId, { signal }).then((result) => {\n if (result.isErr()) {\n throw result.error;\n }\n return result.value;\n }),\n ]);\n return {\n conversation,\n history,\n };\n },\n ),\n startRun: fromPromise(\n ({ input }: { input: { conversationId: string; message: UserChatMessage } }) =>\n createExchangeRun(config)(input.conversationId, { message: input.message }).then(\n (result) => {\n if (result.isErr()) {\n throw result.error;\n }\n return result.value;\n },\n ),\n ),\n stopRun: fromPromise(({ input }: { input: { conversationId: string; runId: string } }) =>\n stopExchangeRun(config)(input.conversationId, input.runId).then((result) => {\n if (result.isErr()) {\n throw result.error;\n }\n return result.value;\n }),\n ),\n streamRun: fromObservable(({ input }: { input: { conversationId: string; runId: string } }) =>\n fromTextEventStream(({ signal }) =>\n streamRunEvents(config)(\n { conversationId: input.conversationId, runId: input.runId },\n { signal },\n ),\n ).pipe(\n map((event) =>\n z.decode(chatEventCodec, JSON.parse(event.data) as z.input<typeof chatEventCodec>),\n ),\n ),\n ),\n },\n types: {\n context: {} as ConversationMachineContext,\n emitted: {} as ConversationMachineEmitted,\n events: {} as ConversationMachineEvents,\n input: {} as ConversationMachineInput,\n },\n }).createMachine({\n context: ({ input }) => ({\n conversationId: input.conversationId,\n conversationSnapshot: undefined,\n currentRunId: undefined,\n messageAttempt: undefined,\n }),\n id: \"agentConversation\",\n initial: \"uninitialized\",\n states: {\n creating_conversation: {\n description: \"Creating a new conversation object\",\n invoke: {\n input: ({ context }) => ({\n message: context.messageAttempt!,\n }),\n onDone: [\n {\n actions: [\n assign({\n conversationId: ({ event }) => event.output.id,\n // Add the message attempt into the snapshot manually\n conversationSnapshot: ({ context, event }) => [\n ...(context.conversationSnapshot ?? []),\n {\n id: event.output.currentRunId!,\n messages: new Map(),\n submittedUserMessage: context.messageAttempt,\n toolCalls: new Map(),\n } satisfies ConversationExchange,\n ],\n currentRunId: ({ event }) => event.output.currentRunId,\n messageAttempt: undefined,\n }),\n emit(({ event }) => ({\n id: event.output.id,\n type: \"conversationCreated\",\n })),\n ],\n // Don't transition to streaming in a situation where we didn't receive a currentRunId\n guard: ({ event }) => !!event.output.currentRunId,\n target: \"streaming\",\n },\n /**\n * Fall back to the retrieving_history state if we didn't receive a currentRunId\n */\n {\n actions: assign({\n conversationId: ({ event }) => event.output.id,\n currentRunId: ({ event }) => event.output.currentRunId,\n messageAttempt: undefined,\n }),\n target: \"retrieving_history\",\n },\n ],\n onError: {\n target: \"uninitialized\",\n },\n src: \"createConversation\",\n },\n },\n idle: {\n description: \"Waiting for user input to start the next reply stream\",\n on: {\n REFRESH_HISTORY: {\n target: \"retrieving_history\",\n },\n SUBMIT_USER_MESSAGE: {\n actions: assign({\n messageAttempt: ({ event }) => event.message,\n }),\n target: \"submitting_message\",\n },\n },\n },\n retrieve_history_failed: {\n on: {\n REFRESH_HISTORY: {\n actions: assign({ lastError: undefined }),\n target: \"retrieving_history\",\n },\n },\n },\n retrieving_history: {\n description: \"Retrieving latest copy of conversation history\",\n invoke: {\n input: ({ context }) => ({ conversationId: context.conversationId! }),\n onDone: [\n {\n actions: assign({\n conversationSnapshot: ({ context, event }) =>\n AgentConversation.reduceChatEvents(\n context.conversationSnapshot,\n event.output.history.data,\n ),\n currentRunId: ({ event }) => event.output.conversation.currentRunId,\n }),\n guard: ({ event }) => !!event.output.conversation.currentRunId,\n target: \"streaming\",\n },\n {\n actions: assign({\n conversationSnapshot: ({ context, event }) =>\n AgentConversation.reduceChatEvents(\n context.conversationSnapshot,\n event.output.history.data,\n ),\n currentRunId: ({ event }) => event.output.conversation.currentRunId,\n }),\n target: \"idle\",\n },\n ],\n onError: [\n {\n guard: ({ event }) => {\n const e = event.error as Error & { code?: string };\n return (\n e?.name === \"AbortError\" ||\n e?.message?.includes(\"aborted\") ||\n e?.code === \"ABORT_ERR\"\n );\n },\n target: \"uninitialized\",\n },\n {\n actions: assign({\n lastError: ({ event }) => event.error,\n }),\n target: \"retrieve_history_failed\",\n },\n ],\n src: \"retrieveState\",\n },\n },\n streaming: {\n description: \"Manages monitoring and canceling the current run\",\n initial: \"monitoring_replies\",\n invoke: {\n input: ({ context }) => {\n if (!context.currentRunId) {\n throw new Error(\"Attempted to stream without a runId\");\n }\n return {\n conversationId: context.conversationId!,\n runId: context.currentRunId,\n };\n },\n onDone: {\n actions: assign({ currentRunId: null }),\n target: \"idle\",\n },\n onError: {\n actions: assign({ currentRunId: null }),\n target: \"idle\",\n },\n onSnapshot: {\n actions: enqueueActions(({ context, enqueue, event }) => {\n const chatEvent = event.snapshot.context;\n if (!chatEvent) {\n return;\n }\n\n const nextSnapshot = AgentConversation.reduceChatEvents(\n context.conversationSnapshot,\n [chatEvent],\n );\n\n enqueue.assign({\n conversationSnapshot: nextSnapshot,\n });\n enqueue.emit({\n chatEvent,\n type: \"chatEventReceived\",\n });\n }),\n },\n src: \"streamRun\",\n },\n states: {\n monitoring_replies: {\n description: \"Attached to the run event stream\",\n on: {\n STOP_RUN: {\n target: \"stopping\",\n },\n },\n },\n stopping: {\n description: \"Background cancelation of the run stream\",\n invoke: {\n input: ({ context }) => ({\n conversationId: context.conversationId!,\n runId: context.currentRunId!,\n }),\n onDone: {\n actions: assign({ currentRunId: undefined }),\n target: \"#agentConversation.idle\",\n },\n onError: {\n target: \"#agentConversation.retrieving_history\",\n },\n src: \"stopRun\",\n },\n },\n },\n },\n submitting_message: {\n description: \"Submitting message to start a new reply stream\",\n invoke: {\n input: ({ context, event }) => {\n switch (event.type) {\n case \"RETRY_MESSAGE\":\n if (!context.messageAttempt) {\n throw new Error(\"Tried to resubmit nonexistent message\");\n }\n return {\n conversationId: context.conversationId!,\n message: context.messageAttempt,\n };\n case \"SUBMIT_USER_MESSAGE\":\n return {\n conversationId: context.conversationId!,\n message: event.message,\n };\n }\n throw new Error(`Unexpected event type received in submitting_message: ${event.type}`);\n },\n onDone: {\n actions: assign({\n // Add the message attempt into the snapshot manually\n conversationSnapshot: ({ context, event }) => [\n ...(context.conversationSnapshot ?? []),\n {\n id: event.output.currentRunId,\n messages: new Map(),\n submittedUserMessage: context.messageAttempt || undefined,\n toolCalls: new Map(),\n } satisfies ConversationExchange,\n ],\n currentRunId: ({ event }) => event.output.currentRunId,\n messageAttempt: undefined,\n }),\n target: \"streaming\",\n },\n onError: {\n actions: assign({\n currentRunId: () => null,\n lastError: ({ event }) => event.error,\n }),\n target: \"submitting_message_failed\",\n },\n src: \"startRun\",\n },\n },\n submitting_message_failed: {\n description: \"Failed to submit message\",\n on: {\n REFRESH_HISTORY: {\n actions: assign({ lastError: undefined }),\n target: \"retrieving_history\",\n },\n RETRY_MESSAGE: {\n actions: assign({ lastError: undefined }),\n target: \"submitting_message\",\n },\n SUBMIT_USER_MESSAGE: {\n actions: assign({\n lastError: undefined,\n messageAttempt: ({ event }) => event.message,\n }),\n target: \"submitting_message\",\n },\n },\n },\n uninitialized: {\n always: {\n guard: ({ context }) => !!context.conversationId,\n target: \"retrieving_history\",\n },\n description:\n \"Entry state that the machine remains in when no conversation has been provided\",\n on: {\n SUBMIT_USER_MESSAGE: {\n actions: assign({\n messageAttempt: ({ event }) => event.message,\n }),\n target: \"creating_conversation\",\n },\n },\n },\n },\n });\n\nexport type AgentConversationMachine = ReturnType<typeof createConversationMachine>;\n"]}
@@ -26,6 +26,17 @@ export declare const retrieveConversationHistory: (config: SonarV4Config) => (id
26
26
  message: string;
27
27
  };
28
28
  role: "agent";
29
+ } | {
30
+ conversationId: string;
31
+ modelName: string;
32
+ modelProviderId: string;
33
+ runId: string;
34
+ createdAt: import("temporal-polyfill").Temporal.Instant;
35
+ id: string;
36
+ content: {
37
+ chunkType: "endOfStream";
38
+ };
39
+ role: "agent";
29
40
  } | {
30
41
  conversationId: string;
31
42
  modelName: string;
@@ -2,7 +2,7 @@ import type { ChatEvent, ChatEventWithChunkType } from "../chat/chatEventSchema.
2
2
  import type { UserChatMessage } from "../chat/UserChatMessage.ts";
3
3
  export type AgentToolCall = {
4
4
  id: string;
5
- state: "error" | "pending" | "success";
5
+ state: "canceled" | "error" | "pending" | "success";
6
6
  request: ChatEventWithChunkType<"toolRequest"> | undefined;
7
7
  result: ChatEventWithChunkType<"toolResponse"> | undefined;
8
8
  };
@@ -29,6 +29,14 @@ function applyChatEventToConversation(draft, chatEvent) {
29
29
  }
30
30
  function applyChatEventToConversationExchange(conversationExchange, chatEvent) {
31
31
  switch (chatEvent.content.chunkType) {
32
+ case "endOfStream": {
33
+ for (const [, toolCall] of conversationExchange.toolCalls) {
34
+ if (!toolCall.result) {
35
+ toolCall.state = "canceled";
36
+ }
37
+ }
38
+ break;
39
+ }
32
40
  case "error":
33
41
  case "model":
34
42
  case "userMessage": {
@@ -1 +1 @@
1
- {"version":3,"file":"reduceChatEvents.js","sourceRoot":"","sources":["../../../../src/enterprise/ai/conversations/reduceChatEvents.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAAE,MAAM,EAAc,MAAM,UAAU,CAAC;AAyB9C,SAAS,4BAA4B,CACnC,KAAmC,EACnC,SAAoB;IAEpB,IAAI,oBAAoB,GAAG,KAAK,CAAC,QAAQ,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,QAAQ,CAAC,EAAE,KAAK,SAAS,CAAC,KAAK,CAAC,CAAC;IAEzF,IAAI,CAAC,oBAAoB,EAAE,CAAC;QAC1B,oBAAoB,GAAG;YACrB,EAAE,EAAE,SAAS,CAAC,KAAK;YACnB,QAAQ,EAAE,IAAI,GAAG,EAAE;YACnB,SAAS,EAAE,IAAI,GAAG,EAAE;SACrB,CAAC;QACF,KAAK,CAAC,IAAI,CAAC,oBAAoB,CAAC,CAAC;QACjC,oBAAoB,GAAG,KAAK,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;IACjD,CAAC;IAED,oCAAoC,CAAC,oBAAqB,EAAE,SAAS,CAAC,CAAC;AACzE,CAAC;AAED,SAAS,oCAAoC,CAC3C,oBAAiD,EACjD,SAAoB;IAEpB,QAAQ,SAAS,CAAC,OAAO,CAAC,SAAS,EAAE,CAAC;QACpC,KAAK,OAAO,CAAC;QACb,KAAK,OAAO,CAAC;QACb,KAAK,aAAa,CAAC,CAAC,CAAC;YACnB,oBAAoB,CAAC,QAAQ,CAAC,GAAG,CAC/B,SAAS,CAAC,EAAE,EACZ,SAA+C,CAChD,CAAC;YACF,MAAM;QACR,CAAC;QACD,KAAK,aAAa,CAAC;QACnB,KA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Corporation\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { create, type Draft } from \"mutative\";\nimport type { ChatEvent, ChatEventWithChunkType } from \"../chat/chatEventSchema.ts\";\nimport type { UserChatMessage } from \"../chat/UserChatMessage.ts\";\n\nexport type AgentToolCall = {\n id: string;\n state: \"error\" | \"pending\" | \"success\";\n request: ChatEventWithChunkType<\"toolRequest\"> | undefined;\n result: ChatEventWithChunkType<\"toolResponse\"> | undefined;\n};\n\nexport type ConversationExchangeMessage =\n | ChatEventWithChunkType<\"error\">\n | ChatEventWithChunkType<\"model\">\n | ChatEventWithChunkType<\"userMessage\">;\n\nexport type ConversationExchange = {\n id: string;\n messages: Map<string, ConversationExchangeMessage>;\n submittedUserMessage?: UserChatMessage;\n toolCalls: Map<string, AgentToolCall>;\n};\n\nexport type ChatEventReducerState = ConversationExchange[];\n\nfunction applyChatEventToConversation(\n draft: Draft<ChatEventReducerState>,\n chatEvent: ChatEvent,\n): void {\n let conversationExchange = draft.findLast((exchange) => exchange.id === chatEvent.runId);\n\n if (!conversationExchange) {\n conversationExchange = {\n id: chatEvent.runId,\n messages: new Map(),\n toolCalls: new Map(),\n };\n draft.push(conversationExchange);\n conversationExchange = draft[draft.length - 1];\n }\n\n applyChatEventToConversationExchange(conversationExchange!, chatEvent);\n}\n\nfunction applyChatEventToConversationExchange(\n conversationExchange: Draft<ConversationExchange>,\n chatEvent: ChatEvent,\n): void {\n switch (chatEvent.content.chunkType) {\n case \"error\":\n case \"model\":\n case \"userMessage\": {\n conversationExchange.messages.set(\n chatEvent.id,\n chatEvent as Draft<ConversationExchangeMessage>,\n );\n break;\n }\n case \"toolRequest\":\n case \"toolResponse\": {\n const callId = chatEvent.content.callId;\n\n if (!conversationExchange.toolCalls.has(callId)) {\n conversationExchange.toolCalls.set(callId, {\n id: callId,\n request: undefined,\n result: undefined,\n state: \"pending\",\n });\n }\n\n const toolCall = conversationExchange.toolCalls.get(callId)!;\n\n switch (chatEvent.content.chunkType) {\n case \"toolRequest\":\n toolCall.request = chatEvent as unknown as Draft<ChatEventWithChunkType<\"toolRequest\">>;\n break;\n case \"toolResponse\":\n toolCall.result = chatEvent as unknown as Draft<ChatEventWithChunkType<\"toolResponse\">>;\n if (\"errorMessage\" in toolCall.result.content.result) {\n toolCall.state = \"error\";\n } else {\n toolCall.state = \"success\";\n }\n break;\n }\n }\n }\n}\n\nexport function reduceChatEvents(state: ChatEventReducerState = [], chatEvents: ChatEvent[]) {\n return create(state, (draft) => {\n for (const chatEvent of chatEvents) {\n applyChatEventToConversation(draft, Object.freeze(chatEvent));\n }\n });\n}\n"]}
1
+ {"version":3,"file":"reduceChatEvents.js","sourceRoot":"","sources":["../../../../src/enterprise/ai/conversations/reduceChatEvents.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAAE,MAAM,EAAc,MAAM,UAAU,CAAC;AAyB9C,SAAS,4BAA4B,CACnC,KAAmC,EACnC,SAAoB;IAEpB,IAAI,oBAAoB,GAAG,KAAK,CAAC,QAAQ,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,QAAQ,CAAC,EAAE,KAAK,SAAS,CAAC,KAAK,CAAC,CAAC;IAEzF,IAAI,CAAC,oBAAoB,EAAE,CAAC;QAC1B,oBAAoB,GAAG;YACrB,EAAE,EAAE,SAAS,CAAC,KAAK;YACnB,QAAQ,EAAE,IAAI,GAAG,EAAE;YACnB,SAAS,EAAE,IAAI,GAAG,EAAE;SACrB,CAAC;QACF,KAAK,CAAC,IAAI,CAAC,oBAAoB,CAAC,CAAC;QACjC,oBAAoB,GAAG,KAAK,CAAC,KAA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AAC,OAAO,GAAG,SAAoE,CAAC;oBACxF,MAAM;gBACR,KAAK,cAAc;oBACjB,QAAQ,CAAC,MAAM,GAAG,SAAqE,CAAC;oBACxF,IAAI,cAAc,IAAI,QAAQ,CAAC,MAAM,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC;wBACrD,QAAQ,CAAC,KAAK,GAAG,OAAO,CAAC;oBAC3B,CAAC;yBAAM,CAAC;wBACN,QAAQ,CAAC,KAAK,GAAG,SAAS,CAAC;oBAC7B,CAAC;oBACD,MAAM;YACV,CAAC;QACH,CAAC;IACH,CAAC;AACH,CAAC;AAED,MAAM,UAAU,gBAAgB,CAAC,QAA+B,EAAE,EAAE,UAAuB;IACzF,OAAO,MAAM,CAAC,KAAK,EAAE,CAAC,KAAK,EAAE,EAAE;QAC7B,KAAK,MAAM,SAAS,IAAI,UAAU,EAAE,CAAC;YACnC,4BAA4B,CAAC,KAAK,EAAE,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC,CAAC;QAChE,CAAC;IACH,CAAC,CAAC,CAAC;AACL,CAAC","sourcesContent":["/*\n * Copyright (C) 2024-2025 Dremio Corporation\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { create, type Draft } from \"mutative\";\nimport type { ChatEvent, ChatEventWithChunkType } from \"../chat/chatEventSchema.ts\";\nimport type { UserChatMessage } from \"../chat/UserChatMessage.ts\";\n\nexport type AgentToolCall = {\n id: string;\n state: \"canceled\" | \"error\" | \"pending\" | \"success\";\n request: ChatEventWithChunkType<\"toolRequest\"> | undefined;\n result: ChatEventWithChunkType<\"toolResponse\"> | undefined;\n};\n\nexport type ConversationExchangeMessage =\n | ChatEventWithChunkType<\"error\">\n | ChatEventWithChunkType<\"model\">\n | ChatEventWithChunkType<\"userMessage\">;\n\nexport type ConversationExchange = {\n id: string;\n messages: Map<string, ConversationExchangeMessage>;\n submittedUserMessage?: UserChatMessage;\n toolCalls: Map<string, AgentToolCall>;\n};\n\nexport type ChatEventReducerState = ConversationExchange[];\n\nfunction applyChatEventToConversation(\n draft: Draft<ChatEventReducerState>,\n chatEvent: ChatEvent,\n): void {\n let conversationExchange = draft.findLast((exchange) => exchange.id === chatEvent.runId);\n\n if (!conversationExchange) {\n conversationExchange = {\n id: chatEvent.runId,\n messages: new Map(),\n toolCalls: new Map(),\n };\n draft.push(conversationExchange);\n conversationExchange = draft[draft.length - 1];\n }\n\n applyChatEventToConversationExchange(conversationExchange!, chatEvent);\n}\n\nfunction applyChatEventToConversationExchange(\n conversationExchange: Draft<ConversationExchange>,\n chatEvent: ChatEvent,\n): void {\n switch (chatEvent.content.chunkType) {\n case \"endOfStream\": {\n for (const [, toolCall] of conversationExchange.toolCalls) {\n if (!toolCall.result) {\n toolCall.state = \"canceled\";\n }\n }\n break;\n }\n case \"error\":\n case \"model\":\n case \"userMessage\": {\n conversationExchange.messages.set(\n chatEvent.id,\n chatEvent as Draft<ConversationExchangeMessage>,\n );\n break;\n }\n case \"toolRequest\":\n case \"toolResponse\": {\n const callId = chatEvent.content.callId;\n\n if (!conversationExchange.toolCalls.has(callId)) {\n conversationExchange.toolCalls.set(callId, {\n id: callId,\n request: undefined,\n result: undefined,\n state: \"pending\",\n });\n }\n\n const toolCall = conversationExchange.toolCalls.get(callId)!;\n\n switch (chatEvent.content.chunkType) {\n case \"toolRequest\":\n toolCall.request = chatEvent as unknown as Draft<ChatEventWithChunkType<\"toolRequest\">>;\n break;\n case \"toolResponse\":\n toolCall.result = chatEvent as unknown as Draft<ChatEventWithChunkType<\"toolResponse\">>;\n if (\"errorMessage\" in toolCall.result.content.result) {\n toolCall.state = \"error\";\n } else {\n toolCall.state = \"success\";\n }\n break;\n }\n }\n }\n}\n\nexport function reduceChatEvents(state: ChatEventReducerState = [], chatEvents: ChatEvent[]) {\n return create(state, (draft) => {\n for (const chatEvent of chatEvents) {\n applyChatEventToConversation(draft, Object.freeze(chatEvent));\n }\n });\n}\n"]}
@@ -23890,6 +23890,9 @@ Set the \`cycles\` parameter to \`"ref"\` to resolve cyclical schemas with defs.
23890
23890
  summary: optional(string2()),
23891
23891
  title: optional(string2())
23892
23892
  });
23893
+ var endOfStreamChunkSchema = object({
23894
+ chunkType: literal("endOfStream")
23895
+ });
23893
23896
  var modelChunkSchema = object({
23894
23897
  chunkType: literal("model"),
23895
23898
  name: string2().check(_trim(), _minLength(1)),
@@ -23928,6 +23931,7 @@ Set the \`cycles\` parameter to \`"ref"\` to resolve cyclical schemas with defs.
23928
23931
  ]);
23929
23932
  var chatEventInputSchema = discriminatedUnion("chunkType", [
23930
23933
  errorChunkSchema,
23934
+ extend2(endOfStreamChunkSchema, chatEventSharedSchema),
23931
23935
  extend2(conversationUpdateChunkSchema, chatEventSharedSchema),
23932
23936
  extend2(modelChunkSchema, chatEventSharedSchema),
23933
23937
  extend2(toolRequestChunkSchema, chatEventSharedSchema),
@@ -23944,6 +23948,10 @@ Set the \`cycles\` parameter to \`"ref"\` to resolve cyclical schemas with defs.
23944
23948
  role: literal("agent")
23945
23949
  }),
23946
23950
  extend2(chatEventOutputSharedSchema, { content: errorChunkSchema, role: literal("agent") }),
23951
+ extend2(chatEventOutputSharedSchema, {
23952
+ content: endOfStreamChunkSchema,
23953
+ role: literal("agent")
23954
+ }),
23947
23955
  extend2(chatEventOutputSharedSchema, { content: modelChunkSchema, role: literal("agent") }),
23948
23956
  extend2(chatEventOutputSharedSchema, {
23949
23957
  content: toolRequestChunkSchema,
@@ -23994,6 +24002,14 @@ Set the \`cycles\` parameter to \`"ref"\` to resolve cyclical schemas with defs.
23994
24002
  },
23995
24003
  role: "agent"
23996
24004
  };
24005
+ case "endOfStream":
24006
+ return {
24007
+ ...sharedProperties,
24008
+ content: {
24009
+ chunkType: "endOfStream"
24010
+ },
24011
+ role: "agent"
24012
+ };
23997
24013
  case "model":
23998
24014
  return {
23999
24015
  ...sharedProperties,
@@ -26042,6 +26058,14 @@ Set the \`cycles\` parameter to \`"ref"\` to resolve cyclical schemas with defs.
26042
26058
  }
26043
26059
  function applyChatEventToConversationExchange(conversationExchange, chatEvent) {
26044
26060
  switch (chatEvent.content.chunkType) {
26061
+ case "endOfStream": {
26062
+ for (const [, toolCall] of conversationExchange.toolCalls) {
26063
+ if (!toolCall.result) {
26064
+ toolCall.state = "canceled";
26065
+ }
26066
+ }
26067
+ break;
26068
+ }
26045
26069
  case "error":
26046
26070
  case "model":
26047
26071
  case "userMessage": {
@@ -30950,6 +30974,7 @@ ${err.message}`);
30950
30974
  },
30951
30975
  types: {
30952
30976
  context: {},
30977
+ emitted: {},
30953
30978
  events: {},
30954
30979
  input: {}
30955
30980
  }
@@ -30971,21 +30996,27 @@ ${err.message}`);
30971
30996
  }),
30972
30997
  onDone: [
30973
30998
  {
30974
- actions: assign({
30975
- conversationId: ({ event }) => event.output.id,
30976
- // Add the message attempt into the snapshot manually
30977
- conversationSnapshot: ({ context: context2, event }) => [
30978
- ...context2.conversationSnapshot ?? [],
30979
- {
30980
- id: event.output.currentRunId,
30981
- messages: /* @__PURE__ */ new Map(),
30982
- submittedUserMessage: context2.messageAttempt,
30983
- toolCalls: /* @__PURE__ */ new Map()
30984
- }
30985
- ],
30986
- currentRunId: ({ event }) => event.output.currentRunId,
30987
- messageAttempt: void 0
30988
- }),
30999
+ actions: [
31000
+ assign({
31001
+ conversationId: ({ event }) => event.output.id,
31002
+ // Add the message attempt into the snapshot manually
31003
+ conversationSnapshot: ({ context: context2, event }) => [
31004
+ ...context2.conversationSnapshot ?? [],
31005
+ {
31006
+ id: event.output.currentRunId,
31007
+ messages: /* @__PURE__ */ new Map(),
31008
+ submittedUserMessage: context2.messageAttempt,
31009
+ toolCalls: /* @__PURE__ */ new Map()
31010
+ }
31011
+ ],
31012
+ currentRunId: ({ event }) => event.output.currentRunId,
31013
+ messageAttempt: void 0
31014
+ }),
31015
+ emit(({ event }) => ({
31016
+ id: event.output.id,
31017
+ type: "conversationCreated"
31018
+ }))
31019
+ ],
30989
31020
  // Don't transition to streaming in a situation where we didn't receive a currentRunId
30990
31021
  guard: ({ event }) => !!event.output.currentRunId,
30991
31022
  target: "streaming"
@@ -31091,16 +31122,19 @@ ${err.message}`);
31091
31122
  target: "idle"
31092
31123
  },
31093
31124
  onSnapshot: {
31094
- actions: assign({
31095
- conversationSnapshot: ({ context: context2, event }) => {
31096
- const chatEvent = event.snapshot.context;
31097
- if (!chatEvent) {
31098
- return context2.conversationSnapshot;
31099
- }
31100
- return AgentConversation.reduceChatEvents(context2.conversationSnapshot, [
31101
- chatEvent
31102
- ]);
31125
+ actions: enqueueActions(({ context: context2, enqueue, event }) => {
31126
+ const chatEvent = event.snapshot.context;
31127
+ if (!chatEvent) {
31128
+ return;
31103
31129
  }
31130
+ const nextSnapshot = AgentConversation.reduceChatEvents(context2.conversationSnapshot, [chatEvent]);
31131
+ enqueue.assign({
31132
+ conversationSnapshot: nextSnapshot
31133
+ });
31134
+ enqueue.emit({
31135
+ chatEvent,
31136
+ type: "chatEventReceived"
31137
+ });
31104
31138
  })
31105
31139
  },
31106
31140
  src: "streamRun"
@@ -24945,6 +24945,9 @@ Set the \`cycles\` parameter to \`"ref"\` to resolve cyclical schemas with defs.
24945
24945
  summary: optional(string2()),
24946
24946
  title: optional(string2())
24947
24947
  });
24948
+ var endOfStreamChunkSchema = object({
24949
+ chunkType: literal("endOfStream")
24950
+ });
24948
24951
  var modelChunkSchema = object({
24949
24952
  chunkType: literal("model"),
24950
24953
  name: string2().check(_trim(), _minLength(1)),
@@ -24983,6 +24986,7 @@ Set the \`cycles\` parameter to \`"ref"\` to resolve cyclical schemas with defs.
24983
24986
  ]);
24984
24987
  var chatEventInputSchema = discriminatedUnion("chunkType", [
24985
24988
  errorChunkSchema,
24989
+ extend2(endOfStreamChunkSchema, chatEventSharedSchema),
24986
24990
  extend2(conversationUpdateChunkSchema, chatEventSharedSchema),
24987
24991
  extend2(modelChunkSchema, chatEventSharedSchema),
24988
24992
  extend2(toolRequestChunkSchema, chatEventSharedSchema),
@@ -24999,6 +25003,10 @@ Set the \`cycles\` parameter to \`"ref"\` to resolve cyclical schemas with defs.
24999
25003
  role: literal("agent")
25000
25004
  }),
25001
25005
  extend2(chatEventOutputSharedSchema, { content: errorChunkSchema, role: literal("agent") }),
25006
+ extend2(chatEventOutputSharedSchema, {
25007
+ content: endOfStreamChunkSchema,
25008
+ role: literal("agent")
25009
+ }),
25002
25010
  extend2(chatEventOutputSharedSchema, { content: modelChunkSchema, role: literal("agent") }),
25003
25011
  extend2(chatEventOutputSharedSchema, {
25004
25012
  content: toolRequestChunkSchema,
@@ -25049,6 +25057,14 @@ Set the \`cycles\` parameter to \`"ref"\` to resolve cyclical schemas with defs.
25049
25057
  },
25050
25058
  role: "agent"
25051
25059
  };
25060
+ case "endOfStream":
25061
+ return {
25062
+ ...sharedProperties,
25063
+ content: {
25064
+ chunkType: "endOfStream"
25065
+ },
25066
+ role: "agent"
25067
+ };
25052
25068
  case "model":
25053
25069
  return {
25054
25070
  ...sharedProperties,
@@ -26748,6 +26764,14 @@ Set the \`cycles\` parameter to \`"ref"\` to resolve cyclical schemas with defs.
26748
26764
  }
26749
26765
  function applyChatEventToConversationExchange(conversationExchange, chatEvent) {
26750
26766
  switch (chatEvent.content.chunkType) {
26767
+ case "endOfStream": {
26768
+ for (const [, toolCall] of conversationExchange.toolCalls) {
26769
+ if (!toolCall.result) {
26770
+ toolCall.state = "canceled";
26771
+ }
26772
+ }
26773
+ break;
26774
+ }
26751
26775
  case "error":
26752
26776
  case "model":
26753
26777
  case "userMessage": {
@@ -31656,6 +31680,7 @@ ${err.message}`);
31656
31680
  },
31657
31681
  types: {
31658
31682
  context: {},
31683
+ emitted: {},
31659
31684
  events: {},
31660
31685
  input: {}
31661
31686
  }
@@ -31677,21 +31702,27 @@ ${err.message}`);
31677
31702
  }),
31678
31703
  onDone: [
31679
31704
  {
31680
- actions: assign({
31681
- conversationId: ({ event }) => event.output.id,
31682
- // Add the message attempt into the snapshot manually
31683
- conversationSnapshot: ({ context: context2, event }) => [
31684
- ...context2.conversationSnapshot ?? [],
31685
- {
31686
- id: event.output.currentRunId,
31687
- messages: /* @__PURE__ */ new Map(),
31688
- submittedUserMessage: context2.messageAttempt,
31689
- toolCalls: /* @__PURE__ */ new Map()
31690
- }
31691
- ],
31692
- currentRunId: ({ event }) => event.output.currentRunId,
31693
- messageAttempt: void 0
31694
- }),
31705
+ actions: [
31706
+ assign({
31707
+ conversationId: ({ event }) => event.output.id,
31708
+ // Add the message attempt into the snapshot manually
31709
+ conversationSnapshot: ({ context: context2, event }) => [
31710
+ ...context2.conversationSnapshot ?? [],
31711
+ {
31712
+ id: event.output.currentRunId,
31713
+ messages: /* @__PURE__ */ new Map(),
31714
+ submittedUserMessage: context2.messageAttempt,
31715
+ toolCalls: /* @__PURE__ */ new Map()
31716
+ }
31717
+ ],
31718
+ currentRunId: ({ event }) => event.output.currentRunId,
31719
+ messageAttempt: void 0
31720
+ }),
31721
+ emit(({ event }) => ({
31722
+ id: event.output.id,
31723
+ type: "conversationCreated"
31724
+ }))
31725
+ ],
31695
31726
  // Don't transition to streaming in a situation where we didn't receive a currentRunId
31696
31727
  guard: ({ event }) => !!event.output.currentRunId,
31697
31728
  target: "streaming"
@@ -31797,16 +31828,19 @@ ${err.message}`);
31797
31828
  target: "idle"
31798
31829
  },
31799
31830
  onSnapshot: {
31800
- actions: assign({
31801
- conversationSnapshot: ({ context: context2, event }) => {
31802
- const chatEvent = event.snapshot.context;
31803
- if (!chatEvent) {
31804
- return context2.conversationSnapshot;
31805
- }
31806
- return AgentConversation.reduceChatEvents(context2.conversationSnapshot, [
31807
- chatEvent
31808
- ]);
31831
+ actions: enqueueActions(({ context: context2, enqueue, event }) => {
31832
+ const chatEvent = event.snapshot.context;
31833
+ if (!chatEvent) {
31834
+ return;
31809
31835
  }
31836
+ const nextSnapshot = AgentConversation.reduceChatEvents(context2.conversationSnapshot, [chatEvent]);
31837
+ enqueue.assign({
31838
+ conversationSnapshot: nextSnapshot
31839
+ });
31840
+ enqueue.emit({
31841
+ chatEvent,
31842
+ type: "chatEventReceived"
31843
+ });
31810
31844
  })
31811
31845
  },
31812
31846
  src: "streamRun"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dremio/js-sdk",
3
- "version": "0.43.1",
3
+ "version": "0.44.0",
4
4
  "description": "JavaScript library for the Dremio API",
5
5
  "keywords": [
6
6
  "dremio",