ai 2.2.22 → 2.2.24

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.
@@ -527,6 +527,129 @@ var F2 = (t, e) => c.useSWR(t, e);
527
527
 
528
528
  // shared/utils.ts
529
529
  var import_non_secure = require("nanoid/non-secure");
530
+
531
+ // shared/stream-parts.ts
532
+ var textStreamPart = {
533
+ code: "0",
534
+ name: "text",
535
+ parse: (value) => {
536
+ if (typeof value !== "string") {
537
+ throw new Error('"text" parts expect a string value.');
538
+ }
539
+ return { type: "text", value };
540
+ }
541
+ };
542
+ var functionCallStreamPart = {
543
+ code: "1",
544
+ name: "function_call",
545
+ parse: (value) => {
546
+ if (value == null || typeof value !== "object" || !("function_call" in value) || typeof value.function_call !== "object" || value.function_call == null || !("name" in value.function_call) || !("arguments" in value.function_call) || typeof value.function_call.name !== "string" || typeof value.function_call.arguments !== "string") {
547
+ throw new Error(
548
+ '"function_call" parts expect an object with a "function_call" property.'
549
+ );
550
+ }
551
+ return {
552
+ type: "function_call",
553
+ value
554
+ };
555
+ }
556
+ };
557
+ var dataStreamPart = {
558
+ code: "2",
559
+ name: "data",
560
+ parse: (value) => {
561
+ if (!Array.isArray(value)) {
562
+ throw new Error('"data" parts expect an array value.');
563
+ }
564
+ return { type: "data", value };
565
+ }
566
+ };
567
+ var errorStreamPart = {
568
+ code: "3",
569
+ name: "error",
570
+ parse: (value) => {
571
+ if (typeof value !== "string") {
572
+ throw new Error('"error" parts expect a string value.');
573
+ }
574
+ return { type: "error", value };
575
+ }
576
+ };
577
+ var assistantMessage = {
578
+ code: "4",
579
+ name: "assistant_message",
580
+ parse: (value) => {
581
+ if (value == null || typeof value !== "object" || !("id" in value) || !("role" in value) || !("content" in value) || typeof value.id !== "string" || typeof value.role !== "string" || value.role !== "assistant" || !Array.isArray(value.content) || !value.content.every(
582
+ (item) => item != null && typeof item === "object" && "type" in item && item.type === "text" && "text" in item && item.text != null && typeof item.text === "object" && "value" in item.text && typeof item.text.value === "string"
583
+ )) {
584
+ throw new Error(
585
+ '"assistant_message" parts expect an object with an "id", "role", and "content" property.'
586
+ );
587
+ }
588
+ return {
589
+ type: "assistant_message",
590
+ value
591
+ };
592
+ }
593
+ };
594
+ var assistantControlData = {
595
+ code: "5",
596
+ name: "assistant_control_data",
597
+ parse: (value) => {
598
+ if (value == null || typeof value !== "object" || !("threadId" in value) || !("messageId" in value) || typeof value.threadId !== "string" || typeof value.messageId !== "string") {
599
+ throw new Error(
600
+ '"assistant_control_data" parts expect an object with a "threadId" and "messageId" property.'
601
+ );
602
+ }
603
+ return {
604
+ type: "assistant_control_data",
605
+ value: {
606
+ threadId: value.threadId,
607
+ messageId: value.messageId
608
+ }
609
+ };
610
+ }
611
+ };
612
+ var streamParts = [
613
+ textStreamPart,
614
+ functionCallStreamPart,
615
+ dataStreamPart,
616
+ errorStreamPart,
617
+ assistantMessage,
618
+ assistantControlData
619
+ ];
620
+ var streamPartsByCode = {
621
+ [textStreamPart.code]: textStreamPart,
622
+ [functionCallStreamPart.code]: functionCallStreamPart,
623
+ [dataStreamPart.code]: dataStreamPart,
624
+ [errorStreamPart.code]: errorStreamPart,
625
+ [assistantMessage.code]: assistantMessage,
626
+ [assistantControlData.code]: assistantControlData
627
+ };
628
+ var StreamStringPrefixes = {
629
+ [textStreamPart.name]: textStreamPart.code,
630
+ [functionCallStreamPart.name]: functionCallStreamPart.code,
631
+ [dataStreamPart.name]: dataStreamPart.code,
632
+ [errorStreamPart.name]: errorStreamPart.code,
633
+ [assistantMessage.name]: assistantMessage.code,
634
+ [assistantControlData.name]: assistantControlData.code
635
+ };
636
+ var validCodes = streamParts.map((part) => part.code);
637
+ var parseStreamPart = (line) => {
638
+ const firstSeparatorIndex = line.indexOf(":");
639
+ if (firstSeparatorIndex === -1) {
640
+ throw new Error("Failed to parse stream string. No separator found.");
641
+ }
642
+ const prefix = line.slice(0, firstSeparatorIndex);
643
+ if (!validCodes.includes(prefix)) {
644
+ throw new Error(`Failed to parse stream string. Invalid code ${prefix}.`);
645
+ }
646
+ const code = prefix;
647
+ const textValue = line.slice(firstSeparatorIndex + 1);
648
+ const jsonValue = JSON.parse(textValue);
649
+ return streamPartsByCode[code].parse(jsonValue);
650
+ };
651
+
652
+ // shared/utils.ts
530
653
  var nanoid = (0, import_non_secure.customAlphabet)(
531
654
  "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
532
655
  7
@@ -542,33 +665,9 @@ function createChunkDecoder(complex) {
542
665
  }
543
666
  return function(chunk) {
544
667
  const decoded = decoder.decode(chunk, { stream: true }).split("\n").filter((line) => line !== "");
545
- return decoded.map(getStreamStringTypeAndValue).filter(Boolean);
668
+ return decoded.map(parseStreamPart).filter(Boolean);
546
669
  };
547
670
  }
548
- var StreamStringPrefixes = {
549
- text: 0,
550
- function_call: 1,
551
- data: 2
552
- // user_err: 3?
553
- };
554
- var getStreamStringTypeAndValue = (line) => {
555
- const firstSeperatorIndex = line.indexOf(":");
556
- const prefix = line.slice(0, firstSeperatorIndex);
557
- const type = Object.keys(StreamStringPrefixes).find(
558
- (key) => StreamStringPrefixes[key] === Number(prefix)
559
- );
560
- const val = line.slice(firstSeperatorIndex + 1);
561
- let parsedVal = val;
562
- if (!val) {
563
- return { type, value: "" };
564
- }
565
- try {
566
- parsedVal = JSON.parse(val);
567
- } catch (e) {
568
- console.error("Failed to parse JSON value:", val);
569
- }
570
- return { type, value: parsedVal };
571
- };
572
671
 
573
672
  // svelte/use-chat.ts
574
673
  var getStreamedResponse = async (api, chatRequest, mutate, extraMetadata, previousMessages, abortControllerRef, onFinish, onResponse, sendExtraMessageFields) => {
@@ -500,6 +500,129 @@ var F2 = (t, e) => c.useSWR(t, e);
500
500
 
501
501
  // shared/utils.ts
502
502
  import { customAlphabet } from "nanoid/non-secure";
503
+
504
+ // shared/stream-parts.ts
505
+ var textStreamPart = {
506
+ code: "0",
507
+ name: "text",
508
+ parse: (value) => {
509
+ if (typeof value !== "string") {
510
+ throw new Error('"text" parts expect a string value.');
511
+ }
512
+ return { type: "text", value };
513
+ }
514
+ };
515
+ var functionCallStreamPart = {
516
+ code: "1",
517
+ name: "function_call",
518
+ parse: (value) => {
519
+ if (value == null || typeof value !== "object" || !("function_call" in value) || typeof value.function_call !== "object" || value.function_call == null || !("name" in value.function_call) || !("arguments" in value.function_call) || typeof value.function_call.name !== "string" || typeof value.function_call.arguments !== "string") {
520
+ throw new Error(
521
+ '"function_call" parts expect an object with a "function_call" property.'
522
+ );
523
+ }
524
+ return {
525
+ type: "function_call",
526
+ value
527
+ };
528
+ }
529
+ };
530
+ var dataStreamPart = {
531
+ code: "2",
532
+ name: "data",
533
+ parse: (value) => {
534
+ if (!Array.isArray(value)) {
535
+ throw new Error('"data" parts expect an array value.');
536
+ }
537
+ return { type: "data", value };
538
+ }
539
+ };
540
+ var errorStreamPart = {
541
+ code: "3",
542
+ name: "error",
543
+ parse: (value) => {
544
+ if (typeof value !== "string") {
545
+ throw new Error('"error" parts expect a string value.');
546
+ }
547
+ return { type: "error", value };
548
+ }
549
+ };
550
+ var assistantMessage = {
551
+ code: "4",
552
+ name: "assistant_message",
553
+ parse: (value) => {
554
+ if (value == null || typeof value !== "object" || !("id" in value) || !("role" in value) || !("content" in value) || typeof value.id !== "string" || typeof value.role !== "string" || value.role !== "assistant" || !Array.isArray(value.content) || !value.content.every(
555
+ (item) => item != null && typeof item === "object" && "type" in item && item.type === "text" && "text" in item && item.text != null && typeof item.text === "object" && "value" in item.text && typeof item.text.value === "string"
556
+ )) {
557
+ throw new Error(
558
+ '"assistant_message" parts expect an object with an "id", "role", and "content" property.'
559
+ );
560
+ }
561
+ return {
562
+ type: "assistant_message",
563
+ value
564
+ };
565
+ }
566
+ };
567
+ var assistantControlData = {
568
+ code: "5",
569
+ name: "assistant_control_data",
570
+ parse: (value) => {
571
+ if (value == null || typeof value !== "object" || !("threadId" in value) || !("messageId" in value) || typeof value.threadId !== "string" || typeof value.messageId !== "string") {
572
+ throw new Error(
573
+ '"assistant_control_data" parts expect an object with a "threadId" and "messageId" property.'
574
+ );
575
+ }
576
+ return {
577
+ type: "assistant_control_data",
578
+ value: {
579
+ threadId: value.threadId,
580
+ messageId: value.messageId
581
+ }
582
+ };
583
+ }
584
+ };
585
+ var streamParts = [
586
+ textStreamPart,
587
+ functionCallStreamPart,
588
+ dataStreamPart,
589
+ errorStreamPart,
590
+ assistantMessage,
591
+ assistantControlData
592
+ ];
593
+ var streamPartsByCode = {
594
+ [textStreamPart.code]: textStreamPart,
595
+ [functionCallStreamPart.code]: functionCallStreamPart,
596
+ [dataStreamPart.code]: dataStreamPart,
597
+ [errorStreamPart.code]: errorStreamPart,
598
+ [assistantMessage.code]: assistantMessage,
599
+ [assistantControlData.code]: assistantControlData
600
+ };
601
+ var StreamStringPrefixes = {
602
+ [textStreamPart.name]: textStreamPart.code,
603
+ [functionCallStreamPart.name]: functionCallStreamPart.code,
604
+ [dataStreamPart.name]: dataStreamPart.code,
605
+ [errorStreamPart.name]: errorStreamPart.code,
606
+ [assistantMessage.name]: assistantMessage.code,
607
+ [assistantControlData.name]: assistantControlData.code
608
+ };
609
+ var validCodes = streamParts.map((part) => part.code);
610
+ var parseStreamPart = (line) => {
611
+ const firstSeparatorIndex = line.indexOf(":");
612
+ if (firstSeparatorIndex === -1) {
613
+ throw new Error("Failed to parse stream string. No separator found.");
614
+ }
615
+ const prefix = line.slice(0, firstSeparatorIndex);
616
+ if (!validCodes.includes(prefix)) {
617
+ throw new Error(`Failed to parse stream string. Invalid code ${prefix}.`);
618
+ }
619
+ const code = prefix;
620
+ const textValue = line.slice(firstSeparatorIndex + 1);
621
+ const jsonValue = JSON.parse(textValue);
622
+ return streamPartsByCode[code].parse(jsonValue);
623
+ };
624
+
625
+ // shared/utils.ts
503
626
  var nanoid = customAlphabet(
504
627
  "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
505
628
  7
@@ -515,33 +638,9 @@ function createChunkDecoder(complex) {
515
638
  }
516
639
  return function(chunk) {
517
640
  const decoded = decoder.decode(chunk, { stream: true }).split("\n").filter((line) => line !== "");
518
- return decoded.map(getStreamStringTypeAndValue).filter(Boolean);
641
+ return decoded.map(parseStreamPart).filter(Boolean);
519
642
  };
520
643
  }
521
- var StreamStringPrefixes = {
522
- text: 0,
523
- function_call: 1,
524
- data: 2
525
- // user_err: 3?
526
- };
527
- var getStreamStringTypeAndValue = (line) => {
528
- const firstSeperatorIndex = line.indexOf(":");
529
- const prefix = line.slice(0, firstSeperatorIndex);
530
- const type = Object.keys(StreamStringPrefixes).find(
531
- (key) => StreamStringPrefixes[key] === Number(prefix)
532
- );
533
- const val = line.slice(firstSeperatorIndex + 1);
534
- let parsedVal = val;
535
- if (!val) {
536
- return { type, value: "" };
537
- }
538
- try {
539
- parsedVal = JSON.parse(val);
540
- } catch (e) {
541
- console.error("Failed to parse JSON value:", val);
542
- }
543
- return { type, value: parsedVal };
544
- };
545
644
 
546
645
  // svelte/use-chat.ts
547
646
  var getStreamedResponse = async (api, chatRequest, mutate, extraMetadata, previousMessages, abortControllerRef, onFinish, onResponse, sendExtraMessageFields) => {
@@ -64,6 +64,7 @@ type ChatRequest = {
64
64
  options?: RequestOptions;
65
65
  functions?: Array<Function>;
66
66
  function_call?: FunctionCall;
67
+ data?: Record<string, string>;
67
68
  };
68
69
  type FunctionCallHandler = (chatMessages: Message[], functionCall: FunctionCall) => Promise<ChatRequest | void>;
69
70
  type RequestOptions = {
package/vue/dist/index.js CHANGED
@@ -41,6 +41,129 @@ var import_vue = require("vue");
41
41
 
42
42
  // shared/utils.ts
43
43
  var import_non_secure = require("nanoid/non-secure");
44
+
45
+ // shared/stream-parts.ts
46
+ var textStreamPart = {
47
+ code: "0",
48
+ name: "text",
49
+ parse: (value) => {
50
+ if (typeof value !== "string") {
51
+ throw new Error('"text" parts expect a string value.');
52
+ }
53
+ return { type: "text", value };
54
+ }
55
+ };
56
+ var functionCallStreamPart = {
57
+ code: "1",
58
+ name: "function_call",
59
+ parse: (value) => {
60
+ if (value == null || typeof value !== "object" || !("function_call" in value) || typeof value.function_call !== "object" || value.function_call == null || !("name" in value.function_call) || !("arguments" in value.function_call) || typeof value.function_call.name !== "string" || typeof value.function_call.arguments !== "string") {
61
+ throw new Error(
62
+ '"function_call" parts expect an object with a "function_call" property.'
63
+ );
64
+ }
65
+ return {
66
+ type: "function_call",
67
+ value
68
+ };
69
+ }
70
+ };
71
+ var dataStreamPart = {
72
+ code: "2",
73
+ name: "data",
74
+ parse: (value) => {
75
+ if (!Array.isArray(value)) {
76
+ throw new Error('"data" parts expect an array value.');
77
+ }
78
+ return { type: "data", value };
79
+ }
80
+ };
81
+ var errorStreamPart = {
82
+ code: "3",
83
+ name: "error",
84
+ parse: (value) => {
85
+ if (typeof value !== "string") {
86
+ throw new Error('"error" parts expect a string value.');
87
+ }
88
+ return { type: "error", value };
89
+ }
90
+ };
91
+ var assistantMessage = {
92
+ code: "4",
93
+ name: "assistant_message",
94
+ parse: (value) => {
95
+ if (value == null || typeof value !== "object" || !("id" in value) || !("role" in value) || !("content" in value) || typeof value.id !== "string" || typeof value.role !== "string" || value.role !== "assistant" || !Array.isArray(value.content) || !value.content.every(
96
+ (item) => item != null && typeof item === "object" && "type" in item && item.type === "text" && "text" in item && item.text != null && typeof item.text === "object" && "value" in item.text && typeof item.text.value === "string"
97
+ )) {
98
+ throw new Error(
99
+ '"assistant_message" parts expect an object with an "id", "role", and "content" property.'
100
+ );
101
+ }
102
+ return {
103
+ type: "assistant_message",
104
+ value
105
+ };
106
+ }
107
+ };
108
+ var assistantControlData = {
109
+ code: "5",
110
+ name: "assistant_control_data",
111
+ parse: (value) => {
112
+ if (value == null || typeof value !== "object" || !("threadId" in value) || !("messageId" in value) || typeof value.threadId !== "string" || typeof value.messageId !== "string") {
113
+ throw new Error(
114
+ '"assistant_control_data" parts expect an object with a "threadId" and "messageId" property.'
115
+ );
116
+ }
117
+ return {
118
+ type: "assistant_control_data",
119
+ value: {
120
+ threadId: value.threadId,
121
+ messageId: value.messageId
122
+ }
123
+ };
124
+ }
125
+ };
126
+ var streamParts = [
127
+ textStreamPart,
128
+ functionCallStreamPart,
129
+ dataStreamPart,
130
+ errorStreamPart,
131
+ assistantMessage,
132
+ assistantControlData
133
+ ];
134
+ var streamPartsByCode = {
135
+ [textStreamPart.code]: textStreamPart,
136
+ [functionCallStreamPart.code]: functionCallStreamPart,
137
+ [dataStreamPart.code]: dataStreamPart,
138
+ [errorStreamPart.code]: errorStreamPart,
139
+ [assistantMessage.code]: assistantMessage,
140
+ [assistantControlData.code]: assistantControlData
141
+ };
142
+ var StreamStringPrefixes = {
143
+ [textStreamPart.name]: textStreamPart.code,
144
+ [functionCallStreamPart.name]: functionCallStreamPart.code,
145
+ [dataStreamPart.name]: dataStreamPart.code,
146
+ [errorStreamPart.name]: errorStreamPart.code,
147
+ [assistantMessage.name]: assistantMessage.code,
148
+ [assistantControlData.name]: assistantControlData.code
149
+ };
150
+ var validCodes = streamParts.map((part) => part.code);
151
+ var parseStreamPart = (line) => {
152
+ const firstSeparatorIndex = line.indexOf(":");
153
+ if (firstSeparatorIndex === -1) {
154
+ throw new Error("Failed to parse stream string. No separator found.");
155
+ }
156
+ const prefix = line.slice(0, firstSeparatorIndex);
157
+ if (!validCodes.includes(prefix)) {
158
+ throw new Error(`Failed to parse stream string. Invalid code ${prefix}.`);
159
+ }
160
+ const code = prefix;
161
+ const textValue = line.slice(firstSeparatorIndex + 1);
162
+ const jsonValue = JSON.parse(textValue);
163
+ return streamPartsByCode[code].parse(jsonValue);
164
+ };
165
+
166
+ // shared/utils.ts
44
167
  var nanoid = (0, import_non_secure.customAlphabet)(
45
168
  "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
46
169
  7
@@ -56,33 +179,9 @@ function createChunkDecoder(complex) {
56
179
  }
57
180
  return function(chunk) {
58
181
  const decoded = decoder.decode(chunk, { stream: true }).split("\n").filter((line) => line !== "");
59
- return decoded.map(getStreamStringTypeAndValue).filter(Boolean);
182
+ return decoded.map(parseStreamPart).filter(Boolean);
60
183
  };
61
184
  }
62
- var StreamStringPrefixes = {
63
- text: 0,
64
- function_call: 1,
65
- data: 2
66
- // user_err: 3?
67
- };
68
- var getStreamStringTypeAndValue = (line) => {
69
- const firstSeperatorIndex = line.indexOf(":");
70
- const prefix = line.slice(0, firstSeperatorIndex);
71
- const type = Object.keys(StreamStringPrefixes).find(
72
- (key) => StreamStringPrefixes[key] === Number(prefix)
73
- );
74
- const val = line.slice(firstSeperatorIndex + 1);
75
- let parsedVal = val;
76
- if (!val) {
77
- return { type, value: "" };
78
- }
79
- try {
80
- parsedVal = JSON.parse(val);
81
- } catch (e) {
82
- console.error("Failed to parse JSON value:", val);
83
- }
84
- return { type, value: parsedVal };
85
- };
86
185
 
87
186
  // vue/use-chat.ts
88
187
  var uniqueId = 0;
@@ -4,6 +4,129 @@ import { ref, unref } from "vue";
4
4
 
5
5
  // shared/utils.ts
6
6
  import { customAlphabet } from "nanoid/non-secure";
7
+
8
+ // shared/stream-parts.ts
9
+ var textStreamPart = {
10
+ code: "0",
11
+ name: "text",
12
+ parse: (value) => {
13
+ if (typeof value !== "string") {
14
+ throw new Error('"text" parts expect a string value.');
15
+ }
16
+ return { type: "text", value };
17
+ }
18
+ };
19
+ var functionCallStreamPart = {
20
+ code: "1",
21
+ name: "function_call",
22
+ parse: (value) => {
23
+ if (value == null || typeof value !== "object" || !("function_call" in value) || typeof value.function_call !== "object" || value.function_call == null || !("name" in value.function_call) || !("arguments" in value.function_call) || typeof value.function_call.name !== "string" || typeof value.function_call.arguments !== "string") {
24
+ throw new Error(
25
+ '"function_call" parts expect an object with a "function_call" property.'
26
+ );
27
+ }
28
+ return {
29
+ type: "function_call",
30
+ value
31
+ };
32
+ }
33
+ };
34
+ var dataStreamPart = {
35
+ code: "2",
36
+ name: "data",
37
+ parse: (value) => {
38
+ if (!Array.isArray(value)) {
39
+ throw new Error('"data" parts expect an array value.');
40
+ }
41
+ return { type: "data", value };
42
+ }
43
+ };
44
+ var errorStreamPart = {
45
+ code: "3",
46
+ name: "error",
47
+ parse: (value) => {
48
+ if (typeof value !== "string") {
49
+ throw new Error('"error" parts expect a string value.');
50
+ }
51
+ return { type: "error", value };
52
+ }
53
+ };
54
+ var assistantMessage = {
55
+ code: "4",
56
+ name: "assistant_message",
57
+ parse: (value) => {
58
+ if (value == null || typeof value !== "object" || !("id" in value) || !("role" in value) || !("content" in value) || typeof value.id !== "string" || typeof value.role !== "string" || value.role !== "assistant" || !Array.isArray(value.content) || !value.content.every(
59
+ (item) => item != null && typeof item === "object" && "type" in item && item.type === "text" && "text" in item && item.text != null && typeof item.text === "object" && "value" in item.text && typeof item.text.value === "string"
60
+ )) {
61
+ throw new Error(
62
+ '"assistant_message" parts expect an object with an "id", "role", and "content" property.'
63
+ );
64
+ }
65
+ return {
66
+ type: "assistant_message",
67
+ value
68
+ };
69
+ }
70
+ };
71
+ var assistantControlData = {
72
+ code: "5",
73
+ name: "assistant_control_data",
74
+ parse: (value) => {
75
+ if (value == null || typeof value !== "object" || !("threadId" in value) || !("messageId" in value) || typeof value.threadId !== "string" || typeof value.messageId !== "string") {
76
+ throw new Error(
77
+ '"assistant_control_data" parts expect an object with a "threadId" and "messageId" property.'
78
+ );
79
+ }
80
+ return {
81
+ type: "assistant_control_data",
82
+ value: {
83
+ threadId: value.threadId,
84
+ messageId: value.messageId
85
+ }
86
+ };
87
+ }
88
+ };
89
+ var streamParts = [
90
+ textStreamPart,
91
+ functionCallStreamPart,
92
+ dataStreamPart,
93
+ errorStreamPart,
94
+ assistantMessage,
95
+ assistantControlData
96
+ ];
97
+ var streamPartsByCode = {
98
+ [textStreamPart.code]: textStreamPart,
99
+ [functionCallStreamPart.code]: functionCallStreamPart,
100
+ [dataStreamPart.code]: dataStreamPart,
101
+ [errorStreamPart.code]: errorStreamPart,
102
+ [assistantMessage.code]: assistantMessage,
103
+ [assistantControlData.code]: assistantControlData
104
+ };
105
+ var StreamStringPrefixes = {
106
+ [textStreamPart.name]: textStreamPart.code,
107
+ [functionCallStreamPart.name]: functionCallStreamPart.code,
108
+ [dataStreamPart.name]: dataStreamPart.code,
109
+ [errorStreamPart.name]: errorStreamPart.code,
110
+ [assistantMessage.name]: assistantMessage.code,
111
+ [assistantControlData.name]: assistantControlData.code
112
+ };
113
+ var validCodes = streamParts.map((part) => part.code);
114
+ var parseStreamPart = (line) => {
115
+ const firstSeparatorIndex = line.indexOf(":");
116
+ if (firstSeparatorIndex === -1) {
117
+ throw new Error("Failed to parse stream string. No separator found.");
118
+ }
119
+ const prefix = line.slice(0, firstSeparatorIndex);
120
+ if (!validCodes.includes(prefix)) {
121
+ throw new Error(`Failed to parse stream string. Invalid code ${prefix}.`);
122
+ }
123
+ const code = prefix;
124
+ const textValue = line.slice(firstSeparatorIndex + 1);
125
+ const jsonValue = JSON.parse(textValue);
126
+ return streamPartsByCode[code].parse(jsonValue);
127
+ };
128
+
129
+ // shared/utils.ts
7
130
  var nanoid = customAlphabet(
8
131
  "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
9
132
  7
@@ -19,33 +142,9 @@ function createChunkDecoder(complex) {
19
142
  }
20
143
  return function(chunk) {
21
144
  const decoded = decoder.decode(chunk, { stream: true }).split("\n").filter((line) => line !== "");
22
- return decoded.map(getStreamStringTypeAndValue).filter(Boolean);
145
+ return decoded.map(parseStreamPart).filter(Boolean);
23
146
  };
24
147
  }
25
- var StreamStringPrefixes = {
26
- text: 0,
27
- function_call: 1,
28
- data: 2
29
- // user_err: 3?
30
- };
31
- var getStreamStringTypeAndValue = (line) => {
32
- const firstSeperatorIndex = line.indexOf(":");
33
- const prefix = line.slice(0, firstSeperatorIndex);
34
- const type = Object.keys(StreamStringPrefixes).find(
35
- (key) => StreamStringPrefixes[key] === Number(prefix)
36
- );
37
- const val = line.slice(firstSeperatorIndex + 1);
38
- let parsedVal = val;
39
- if (!val) {
40
- return { type, value: "" };
41
- }
42
- try {
43
- parsedVal = JSON.parse(val);
44
- } catch (e) {
45
- console.error("Failed to parse JSON value:", val);
46
- }
47
- return { type, value: parsedVal };
48
- };
49
148
 
50
149
  // vue/use-chat.ts
51
150
  var uniqueId = 0;