ai 2.2.23 → 2.2.25

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/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,36 +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
- if (firstSeperatorIndex === -1) {
71
- throw new Error("Failed to parse stream string");
72
- }
73
- const prefix = line.slice(0, firstSeperatorIndex);
74
- const type = Object.keys(StreamStringPrefixes).find(
75
- (key) => StreamStringPrefixes[key] === Number(prefix)
76
- );
77
- const val = line.slice(firstSeperatorIndex + 1);
78
- let parsedVal = val;
79
- if (!val) {
80
- return { type, value: "" };
81
- }
82
- try {
83
- parsedVal = JSON.parse(val);
84
- } catch (e) {
85
- console.error("Failed to parse JSON value:", val);
86
- }
87
- return { type, value: parsedVal };
88
- };
89
185
 
90
186
  // vue/use-chat.ts
91
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,36 +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
- if (firstSeperatorIndex === -1) {
34
- throw new Error("Failed to parse stream string");
35
- }
36
- const prefix = line.slice(0, firstSeperatorIndex);
37
- const type = Object.keys(StreamStringPrefixes).find(
38
- (key) => StreamStringPrefixes[key] === Number(prefix)
39
- );
40
- const val = line.slice(firstSeperatorIndex + 1);
41
- let parsedVal = val;
42
- if (!val) {
43
- return { type, value: "" };
44
- }
45
- try {
46
- parsedVal = JSON.parse(val);
47
- } catch (e) {
48
- console.error("Failed to parse JSON value:", val);
49
- }
50
- return { type, value: parsedVal };
51
- };
52
148
 
53
149
  // vue/use-chat.ts
54
150
  var uniqueId = 0;