-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
stream-parts.ts
375 lines (340 loc) · 10.1 KB
/
stream-parts.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
import {
AssistantMessage,
DataMessage,
FunctionCall,
JSONValue,
ToolCall,
} from './types';
import { StreamString } from './utils';
export interface StreamPart<CODE extends string, NAME extends string, TYPE> {
code: CODE;
name: NAME;
parse: (value: JSONValue) => { type: NAME; value: TYPE };
}
const textStreamPart: StreamPart<'0', 'text', string> = {
code: '0',
name: 'text',
parse: (value: JSONValue) => {
if (typeof value !== 'string') {
throw new Error('"text" parts expect a string value.');
}
return { type: 'text', value };
},
};
const functionCallStreamPart: StreamPart<
'1',
'function_call',
{ function_call: FunctionCall }
> = {
code: '1',
name: 'function_call',
parse: (value: JSONValue) => {
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'
) {
throw new Error(
'"function_call" parts expect an object with a "function_call" property.',
);
}
return {
type: 'function_call',
value: value as unknown as { function_call: FunctionCall },
};
},
};
const dataStreamPart: StreamPart<'2', 'data', Array<JSONValue>> = {
code: '2',
name: 'data',
parse: (value: JSONValue) => {
if (!Array.isArray(value)) {
throw new Error('"data" parts expect an array value.');
}
return { type: 'data', value };
},
};
const errorStreamPart: StreamPart<'3', 'error', string> = {
code: '3',
name: 'error',
parse: (value: JSONValue) => {
if (typeof value !== 'string') {
throw new Error('"error" parts expect a string value.');
}
return { type: 'error', value };
},
};
const assistantMessageStreamPart: StreamPart<
'4',
'assistant_message',
AssistantMessage
> = {
code: '4',
name: 'assistant_message',
parse: (value: JSONValue) => {
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(
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',
)
) {
throw new Error(
'"assistant_message" parts expect an object with an "id", "role", and "content" property.',
);
}
return {
type: 'assistant_message',
value: value as AssistantMessage,
};
},
};
const assistantControlDataStreamPart: StreamPart<
'5',
'assistant_control_data',
{
threadId: string;
messageId: string;
}
> = {
code: '5',
name: 'assistant_control_data',
parse: (value: JSONValue) => {
if (
value == null ||
typeof value !== 'object' ||
!('threadId' in value) ||
!('messageId' in value) ||
typeof value.threadId !== 'string' ||
typeof value.messageId !== 'string'
) {
throw new Error(
'"assistant_control_data" parts expect an object with a "threadId" and "messageId" property.',
);
}
return {
type: 'assistant_control_data',
value: {
threadId: value.threadId,
messageId: value.messageId,
},
};
},
};
const dataMessageStreamPart: StreamPart<'6', 'data_message', DataMessage> = {
code: '6',
name: 'data_message',
parse: (value: JSONValue) => {
if (
value == null ||
typeof value !== 'object' ||
!('role' in value) ||
!('data' in value) ||
typeof value.role !== 'string' ||
value.role !== 'data'
) {
throw new Error(
'"data_message" parts expect an object with a "role" and "data" property.',
);
}
return {
type: 'data_message',
value: value as DataMessage,
};
},
};
const toolCallStreamPart: StreamPart<
'7',
'tool_calls',
{ tool_calls: ToolCall[] }
> = {
code: '7',
name: 'tool_calls',
parse: (value: JSONValue) => {
if (
value == null ||
typeof value !== 'object' ||
!('tool_calls' in value) ||
typeof value.tool_calls !== 'object' ||
value.tool_calls == null ||
!Array.isArray(value.tool_calls) ||
value.tool_calls.some(
tc =>
tc == null ||
typeof tc !== 'object' ||
!('id' in tc) ||
typeof tc.id !== 'string' ||
!('type' in tc) ||
typeof tc.type !== 'string' ||
!('function' in tc) ||
tc.function == null ||
typeof tc.function !== 'object' ||
!('arguments' in tc.function) ||
typeof tc.function.name !== 'string' ||
typeof tc.function.arguments !== 'string',
)
) {
throw new Error(
'"tool_calls" parts expect an object with a ToolCallPayload.',
);
}
return {
type: 'tool_calls',
value: value as unknown as { tool_calls: ToolCall[] },
};
},
};
const messageAnnotationsStreamPart: StreamPart<
'8',
'message_annotations',
Array<JSONValue>
> = {
code: '8',
name: 'message_annotations',
parse: (value: JSONValue) => {
if (!Array.isArray(value)) {
throw new Error('"message_annotations" parts expect an array value.');
}
return { type: 'message_annotations', value };
},
};
const streamParts = [
textStreamPart,
functionCallStreamPart,
dataStreamPart,
errorStreamPart,
assistantMessageStreamPart,
assistantControlDataStreamPart,
dataMessageStreamPart,
toolCallStreamPart,
messageAnnotationsStreamPart,
] as const;
// union type of all stream parts
type StreamParts =
| typeof textStreamPart
| typeof functionCallStreamPart
| typeof dataStreamPart
| typeof errorStreamPart
| typeof assistantMessageStreamPart
| typeof assistantControlDataStreamPart
| typeof dataMessageStreamPart
| typeof toolCallStreamPart
| typeof messageAnnotationsStreamPart;
/**
* Maps the type of a stream part to its value type.
*/
type StreamPartValueType = {
[P in StreamParts as P['name']]: ReturnType<P['parse']>['value'];
};
export type StreamPartType =
| ReturnType<typeof textStreamPart.parse>
| ReturnType<typeof functionCallStreamPart.parse>
| ReturnType<typeof dataStreamPart.parse>
| ReturnType<typeof errorStreamPart.parse>
| ReturnType<typeof assistantMessageStreamPart.parse>
| ReturnType<typeof assistantControlDataStreamPart.parse>
| ReturnType<typeof dataMessageStreamPart.parse>
| ReturnType<typeof toolCallStreamPart.parse>
| ReturnType<typeof messageAnnotationsStreamPart.parse>;
export const streamPartsByCode = {
[textStreamPart.code]: textStreamPart,
[functionCallStreamPart.code]: functionCallStreamPart,
[dataStreamPart.code]: dataStreamPart,
[errorStreamPart.code]: errorStreamPart,
[assistantMessageStreamPart.code]: assistantMessageStreamPart,
[assistantControlDataStreamPart.code]: assistantControlDataStreamPart,
[dataMessageStreamPart.code]: dataMessageStreamPart,
[toolCallStreamPart.code]: toolCallStreamPart,
[messageAnnotationsStreamPart.code]: messageAnnotationsStreamPart,
} as const;
/**
* The map of prefixes for data in the stream
*
* - 0: Text from the LLM response
* - 1: (OpenAI) function_call responses
* - 2: custom JSON added by the user using `Data`
* - 6: (OpenAI) tool_call responses
*
* Example:
* ```
* 0:Vercel
* 0:'s
* 0: AI
* 0: AI
* 0: SDK
* 0: is great
* 0:!
* 2: { "someJson": "value" }
* 1: {"function_call": {"name": "get_current_weather", "arguments": "{\\n\\"location\\": \\"Charlottesville, Virginia\\",\\n\\"format\\": \\"celsius\\"\\n}"}}
* 6: {"tool_call": {"id": "tool_0", "type": "function", "function": {"name": "get_current_weather", "arguments": "{\\n\\"location\\": \\"Charlottesville, Virginia\\",\\n\\"format\\": \\"celsius\\"\\n}"}}}
*```
*/
export const StreamStringPrefixes = {
[textStreamPart.name]: textStreamPart.code,
[functionCallStreamPart.name]: functionCallStreamPart.code,
[dataStreamPart.name]: dataStreamPart.code,
[errorStreamPart.name]: errorStreamPart.code,
[assistantMessageStreamPart.name]: assistantMessageStreamPart.code,
[assistantControlDataStreamPart.name]: assistantControlDataStreamPart.code,
[dataMessageStreamPart.name]: dataMessageStreamPart.code,
[toolCallStreamPart.name]: toolCallStreamPart.code,
[messageAnnotationsStreamPart.name]: messageAnnotationsStreamPart.code,
} as const;
export const validCodes = streamParts.map(part => part.code);
/**
Parses a stream part from a string.
@param line The string to parse.
@returns The parsed stream part.
@throws An error if the string cannot be parsed.
*/
export const parseStreamPart = (line: string): StreamPartType => {
const firstSeparatorIndex = line.indexOf(':');
if (firstSeparatorIndex === -1) {
throw new Error('Failed to parse stream string. No separator found.');
}
const prefix = line.slice(0, firstSeparatorIndex);
if (!validCodes.includes(prefix as keyof typeof streamPartsByCode)) {
throw new Error(`Failed to parse stream string. Invalid code ${prefix}.`);
}
const code = prefix as keyof typeof streamPartsByCode;
const textValue = line.slice(firstSeparatorIndex + 1);
const jsonValue: JSONValue = JSON.parse(textValue);
return streamPartsByCode[code].parse(jsonValue);
};
/**
Prepends a string with a prefix from the `StreamChunkPrefixes`, JSON-ifies it,
and appends a new line.
It ensures type-safety for the part type and value.
*/
export function formatStreamPart<T extends keyof StreamPartValueType>(
type: T,
value: StreamPartValueType[T],
): StreamString {
const streamPart = streamParts.find(part => part.name === type);
if (!streamPart) {
throw new Error(`Invalid stream part type: ${type}`);
}
return `${streamPart.code}:${JSON.stringify(value)}\n`;
}