@copilotkit/shared 0.37.0 → 0.38.0-beta.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.
Files changed (80) hide show
  1. package/.turbo/turbo-build.log +73 -106
  2. package/CHANGELOG.md +6 -0
  3. package/dist/{chunk-QVD5GLKQ.mjs → chunk-2VLE6D3W.mjs} +1 -1
  4. package/dist/chunk-2VLE6D3W.mjs.map +1 -0
  5. package/dist/chunk-CIPF7PMC.mjs +80 -0
  6. package/dist/chunk-CIPF7PMC.mjs.map +1 -0
  7. package/dist/chunk-CYDWEPFL.mjs +1 -0
  8. package/dist/constants/index.d.ts +0 -2
  9. package/dist/constants/index.js +2 -9
  10. package/dist/constants/index.js.map +1 -1
  11. package/dist/constants/index.mjs +2 -6
  12. package/dist/index.d.ts +2 -6
  13. package/dist/index.js +79 -371
  14. package/dist/index.js.map +1 -1
  15. package/dist/index.mjs +11 -43
  16. package/dist/types/index.d.ts +1 -1
  17. package/dist/types/openai-assistant.d.ts +7 -46
  18. package/dist/types/openai-assistant.js.map +1 -1
  19. package/dist/utils/index.d.ts +1 -4
  20. package/dist/utils/index.js +80 -367
  21. package/dist/utils/index.js.map +1 -1
  22. package/dist/utils/index.mjs +6 -34
  23. package/dist/utils/json-schema.d.ts +30 -0
  24. package/dist/utils/json-schema.js +104 -0
  25. package/dist/utils/json-schema.js.map +1 -0
  26. package/dist/utils/json-schema.mjs +7 -0
  27. package/package.json +4 -4
  28. package/src/constants/index.ts +0 -1
  29. package/src/types/openai-assistant.ts +7 -54
  30. package/src/utils/index.ts +1 -4
  31. package/src/utils/json-schema.ts +120 -0
  32. package/dist/chunk-3ZU7SB62.mjs +0 -7
  33. package/dist/chunk-3ZU7SB62.mjs.map +0 -1
  34. package/dist/chunk-4MTSDAP6.mjs +0 -32
  35. package/dist/chunk-4MTSDAP6.mjs.map +0 -1
  36. package/dist/chunk-CBF2BIA7.mjs +0 -116
  37. package/dist/chunk-CBF2BIA7.mjs.map +0 -1
  38. package/dist/chunk-DBW3BY7E.mjs +0 -157
  39. package/dist/chunk-DBW3BY7E.mjs.map +0 -1
  40. package/dist/chunk-HW4V75UQ.mjs +0 -60
  41. package/dist/chunk-HW4V75UQ.mjs.map +0 -1
  42. package/dist/chunk-QVD5GLKQ.mjs.map +0 -1
  43. package/dist/chunk-UAPRMZEY.mjs +0 -1
  44. package/dist/constants/copilot-protocol.d.ts +0 -3
  45. package/dist/constants/copilot-protocol.js +0 -31
  46. package/dist/constants/copilot-protocol.js.map +0 -1
  47. package/dist/constants/copilot-protocol.mjs +0 -7
  48. package/dist/utils/decode-chat-completion-as-text.d.ts +0 -7
  49. package/dist/utils/decode-chat-completion-as-text.js +0 -56
  50. package/dist/utils/decode-chat-completion-as-text.js.map +0 -1
  51. package/dist/utils/decode-chat-completion-as-text.mjs +0 -7
  52. package/dist/utils/decode-chat-completion-as-text.mjs.map +0 -1
  53. package/dist/utils/decode-chat-completion.d.ts +0 -27
  54. package/dist/utils/decode-chat-completion.js +0 -140
  55. package/dist/utils/decode-chat-completion.js.map +0 -1
  56. package/dist/utils/decode-chat-completion.mjs +0 -7
  57. package/dist/utils/decode-chat-completion.mjs.map +0 -1
  58. package/dist/utils/parse-chat-completion.d.ts +0 -30
  59. package/dist/utils/parse-chat-completion.js +0 -84
  60. package/dist/utils/parse-chat-completion.js.map +0 -1
  61. package/dist/utils/parse-chat-completion.mjs +0 -7
  62. package/dist/utils/parse-chat-completion.mjs.map +0 -1
  63. package/dist/utils/utils.d.ts +0 -103
  64. package/dist/utils/utils.js +0 -189
  65. package/dist/utils/utils.js.map +0 -1
  66. package/dist/utils/utils.mjs +0 -23
  67. package/dist/utils/utils.mjs.map +0 -1
  68. package/dist/utils/utils.test.d.ts +0 -2
  69. package/dist/utils/utils.test.js +0 -9
  70. package/dist/utils/utils.test.js.map +0 -1
  71. package/dist/utils/utils.test.mjs +0 -7
  72. package/dist/utils/utils.test.mjs.map +0 -1
  73. package/src/constants/copilot-protocol.ts +0 -1
  74. package/src/utils/decode-chat-completion-as-text.ts +0 -33
  75. package/src/utils/decode-chat-completion.ts +0 -169
  76. package/src/utils/parse-chat-completion.ts +0 -112
  77. package/src/utils/utils.test.ts +0 -7
  78. package/src/utils/utils.ts +0 -298
  79. /package/dist/{chunk-UAPRMZEY.mjs.map → chunk-CYDWEPFL.mjs.map} +0 -0
  80. /package/dist/{constants/copilot-protocol.mjs.map → utils/json-schema.mjs.map} +0 -0
@@ -1,9 +0,0 @@
1
- "use strict";
2
-
3
- // src/utils/utils.test.ts
4
- describe("emptyTest", () => {
5
- it("should be truthy", () => {
6
- expect(true).toBeTruthy();
7
- });
8
- });
9
- //# sourceMappingURL=utils.test.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../src/utils/utils.test.ts"],"sourcesContent":["import * as utils from \"./utils\";\n\ndescribe(\"emptyTest\", () => {\n it(\"should be truthy\", () => {\n expect(true).toBeTruthy();\n });\n});\n"],"mappings":";;;AAEA,SAAS,aAAa,MAAM;AAC1B,KAAG,oBAAoB,MAAM;AAC3B,WAAO,IAAI,EAAE,WAAW;AAAA,EAC1B,CAAC;AACH,CAAC;","names":[]}
@@ -1,7 +0,0 @@
1
- // src/utils/utils.test.ts
2
- describe("emptyTest", () => {
3
- it("should be truthy", () => {
4
- expect(true).toBeTruthy();
5
- });
6
- });
7
- //# sourceMappingURL=utils.test.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../src/utils/utils.test.ts"],"sourcesContent":["import * as utils from \"./utils\";\n\ndescribe(\"emptyTest\", () => {\n it(\"should be truthy\", () => {\n expect(true).toBeTruthy();\n });\n});\n"],"mappings":";AAEA,SAAS,aAAa,MAAM;AAC1B,KAAG,oBAAoB,MAAM;AAC3B,WAAO,IAAI,EAAE,WAAW;AAAA,EAC1B,CAAC;AACH,CAAC;","names":[]}
@@ -1 +0,0 @@
1
- export const EXCLUDE_FROM_FORWARD_PROPS_KEYS = "exclude_from_forward_props_keys";
@@ -1,33 +0,0 @@
1
- import { ChatCompletionEvent } from "./decode-chat-completion";
2
-
3
- export function decodeChatCompletionAsText(
4
- stream: ReadableStream<ChatCompletionEvent>,
5
- ): ReadableStream<string> {
6
- const reader = stream.getReader();
7
-
8
- return new ReadableStream<string>({
9
- async pull(controller) {
10
- while (true) {
11
- try {
12
- const { done, value } = await reader.read();
13
-
14
- if (done) {
15
- controller.close();
16
- return;
17
- }
18
-
19
- if (value.type === "content") {
20
- controller.enqueue(value.content);
21
- continue;
22
- }
23
- } catch (error) {
24
- controller.error(error);
25
- return;
26
- }
27
- }
28
- },
29
- cancel() {
30
- reader.cancel();
31
- },
32
- });
33
- }
@@ -1,169 +0,0 @@
1
- import { ChatCompletionChunk, ToolCallFunctionCall } from "./parse-chat-completion";
2
-
3
- export interface ChatCompletionContentEvent {
4
- type: "content";
5
- content: string;
6
- }
7
-
8
- export interface ChatCompletionPartialEvent {
9
- type: "partial";
10
- name: string;
11
- arguments: string;
12
- }
13
-
14
- export interface ChatCompletionFunctionEvent {
15
- type: "function";
16
- name: string;
17
- arguments: any;
18
- scope: "client" | "server";
19
- }
20
-
21
- export interface ChatCompletionResultEvent {
22
- type: "result";
23
- content: string;
24
- name: string;
25
- }
26
-
27
- export type ChatCompletionEvent =
28
- | ChatCompletionContentEvent
29
- | ChatCompletionPartialEvent
30
- | ChatCompletionFunctionEvent
31
- | ChatCompletionResultEvent;
32
-
33
- export function decodeChatCompletion(
34
- stream: ReadableStream<ChatCompletionChunk>,
35
- ): ReadableStream<ChatCompletionEvent> {
36
- const reader = stream.getReader();
37
-
38
- type Mode = { type: "function"; function: ToolCallFunctionCall } | { type: "message" };
39
-
40
- let mode: Mode | null = null;
41
- let functionCallName: string = "";
42
- let functionCallArguments: string = "";
43
- let functionCallScope: "client" | "server" = "client";
44
-
45
- async function cleanup(controller?: ReadableStreamDefaultController<any>) {
46
- if (controller) {
47
- try {
48
- controller.close();
49
- } catch (_) {}
50
- }
51
- if (reader) {
52
- try {
53
- await reader.cancel();
54
- } catch (_) {}
55
- }
56
- }
57
-
58
- return new ReadableStream<ChatCompletionEvent>({
59
- async pull(controller) {
60
- const flushFunctionCall = (): boolean => {
61
- let args: any = null;
62
- try {
63
- args = JSON.parse(functionCallArguments);
64
- } catch (error) {
65
- cleanup(controller);
66
- controller.error(error);
67
- return false;
68
- }
69
- controller.enqueue({
70
- type: "function",
71
- name: functionCallName,
72
- arguments: args,
73
- scope: functionCallScope,
74
- });
75
-
76
- mode = null;
77
- functionCallName = "";
78
- functionCallArguments = "";
79
- return true;
80
- };
81
-
82
- while (true) {
83
- try {
84
- const { done, value } = await reader.read();
85
-
86
- if (done) {
87
- if (mode?.type === "function") {
88
- flushFunctionCall();
89
- }
90
- await cleanup(controller);
91
- return;
92
- }
93
-
94
- // In case we are currently handling a function call but the next message is either
95
- // - not a function call
96
- // - or is another function call (indicated by the presence of 'name' field in the next function call object)
97
- // => flush the current function call.
98
- if (
99
- mode?.type === "function" &&
100
- (!value.choices[0].delta.tool_calls?.[0]?.function ||
101
- value.choices[0].delta.tool_calls?.[0]?.function.name)
102
- ) {
103
- if (!flushFunctionCall()) {
104
- return;
105
- }
106
- }
107
-
108
- const maybeFunctionCall = value.choices[0].delta.tool_calls?.[0]?.function;
109
- if (maybeFunctionCall) {
110
- mode = { type: "function", function: maybeFunctionCall };
111
- } else {
112
- mode = { type: "message" };
113
- }
114
-
115
- // if we get a message, emit the content and continue;
116
- if (mode.type === "message") {
117
- // if we got a result message, send a result event
118
- if (value.choices[0].delta.role === "function") {
119
- controller.enqueue({
120
- type: "result",
121
- content: value.choices[0].delta.content!,
122
- name: value.choices[0].delta.name!,
123
- });
124
- }
125
- // otherwise, send a content event
126
- else if (value.choices[0].delta.content) {
127
- controller.enqueue({
128
- type: "content",
129
- content: value.choices[0].delta.content,
130
- });
131
- }
132
- continue;
133
- }
134
- // if we get a function call, buffer the name and arguments, then emit a partial event.
135
- else if (mode.type === "function") {
136
- const maybeFunctionCallName = mode.function.name;
137
- if (maybeFunctionCallName) {
138
- functionCallName = maybeFunctionCallName;
139
- }
140
-
141
- const maybeFunctionCallArguments = mode.function.arguments;
142
- if (maybeFunctionCallArguments) {
143
- functionCallArguments += maybeFunctionCallArguments;
144
- }
145
-
146
- const maybeFunctionCallScope = mode.function.scope;
147
- if (maybeFunctionCallScope) {
148
- functionCallScope = maybeFunctionCallScope;
149
- }
150
-
151
- controller.enqueue({
152
- type: "partial",
153
- name: functionCallName,
154
- arguments: functionCallArguments,
155
- });
156
- continue;
157
- }
158
- } catch (error) {
159
- controller.error(error);
160
- await cleanup(controller);
161
- return;
162
- }
163
- }
164
- },
165
- cancel() {
166
- reader.cancel();
167
- },
168
- });
169
- }
@@ -1,112 +0,0 @@
1
- import { Role } from "../types/openai-assistant";
2
-
3
- export interface ToolCallFunctionCall {
4
- arguments?: string;
5
-
6
- name?: string;
7
- // TODO:
8
- // Temporarily add scope to the OpenAI protocol until we
9
- // have our own protocol.
10
- // When scope is "server", the client will not attempt to
11
- // execute the function.
12
- scope?: "client" | "server";
13
- }
14
-
15
- export interface ToolCallPayload {
16
- index: number;
17
- id?: string;
18
- function: ToolCallFunctionCall;
19
- }
20
-
21
- export interface ChatCompletionChunk {
22
- choices: {
23
- delta: {
24
- id?: string;
25
- role: Role;
26
- content?: string | null;
27
-
28
- // TODO:
29
- // Temporarily add name to the OpenAI protocol until we
30
- // have our own protocol.
31
- // When name is set, we return the result of a server-side
32
- // function call.
33
- name?: string;
34
-
35
- function_call?: {
36
- name?: string;
37
- arguments?: string;
38
- };
39
- tool_calls?: ToolCallPayload[];
40
- };
41
- }[];
42
- }
43
-
44
- // TODO:
45
- // it's possible that unicode characters could be split across chunks
46
- // make sure to properly handle that
47
- export function parseChatCompletion(
48
- stream: ReadableStream<Uint8Array>,
49
- ): ReadableStream<ChatCompletionChunk> {
50
- const reader = stream.getReader();
51
- let buffer = new Uint8Array();
52
-
53
- async function cleanup(controller?: ReadableStreamDefaultController<any>) {
54
- if (controller) {
55
- try {
56
- controller.close();
57
- } catch (_) {}
58
- }
59
- if (reader) {
60
- try {
61
- await reader.cancel();
62
- } catch (_) {}
63
- }
64
- }
65
-
66
- return new ReadableStream<ChatCompletionChunk>({
67
- async pull(controller) {
68
- while (true) {
69
- try {
70
- const { done, value } = await reader.read();
71
-
72
- if (done) {
73
- await cleanup(controller);
74
- return;
75
- }
76
-
77
- const newBuffer = new Uint8Array(buffer.length + value.length);
78
- newBuffer.set(buffer);
79
- newBuffer.set(value, buffer.length);
80
- buffer = newBuffer;
81
-
82
- const valueString = new TextDecoder("utf-8").decode(buffer);
83
- const lines = valueString.split("\n").filter((line) => line.trim() !== "");
84
-
85
- // If the last line isn't complete, keep it in the buffer for next time
86
- buffer = !valueString.endsWith("\n")
87
- ? new TextEncoder().encode(lines.pop() || "")
88
- : new Uint8Array();
89
-
90
- for (const line of lines) {
91
- const cleanedLine = line.replace(/^data: /, "");
92
-
93
- if (cleanedLine === "[DONE]") {
94
- await cleanup(controller);
95
- return;
96
- }
97
-
98
- const json = JSON.parse(cleanedLine);
99
- controller.enqueue(json);
100
- }
101
- } catch (error) {
102
- controller.error(error);
103
- await cleanup(controller);
104
- return;
105
- }
106
- }
107
- },
108
- cancel() {
109
- reader.cancel();
110
- },
111
- });
112
- }
@@ -1,7 +0,0 @@
1
- import * as utils from "./utils";
2
-
3
- describe("emptyTest", () => {
4
- it("should be truthy", () => {
5
- expect(true).toBeTruthy();
6
- });
7
- });
@@ -1,298 +0,0 @@
1
- import { AssistantMessage, FunctionCall, JSONValue } from "../types/openai-assistant";
2
-
3
- export function encodeResult(result: string): string {
4
- if (result === undefined) {
5
- return "";
6
- } else if (typeof result === "string") {
7
- return result;
8
- } else {
9
- return JSON.stringify(result);
10
- }
11
- }
12
-
13
- export function decodeResult(result: string): any {
14
- try {
15
- return JSON.parse(result);
16
- } catch (e) {
17
- return result;
18
- }
19
- }
20
-
21
- export interface StreamPart<CODE extends string, NAME extends string, TYPE> {
22
- code: CODE;
23
- name: NAME;
24
- parse: (value: JSONValue) => { type: NAME; value: TYPE };
25
- }
26
-
27
- const textStreamPart: StreamPart<"0", "text", string> = {
28
- code: "0",
29
- name: "text",
30
- parse: (value: JSONValue) => {
31
- if (typeof value !== "string") {
32
- throw new Error('"text" parts expect a string value.');
33
- }
34
- return { type: "text", value };
35
- },
36
- };
37
-
38
- /**
39
- * This is a utility function that helps in parsing the stream parts.
40
- * It takes a JSONValue as input and returns an object with type and value.
41
- * The type is a string that represents the type of the stream part.
42
- * The value is the actual value of the stream part.
43
- * If the input value is not a string, it throws an error.
44
- */
45
- const functionCallStreamPart: StreamPart<"1", "function_call", { function_call: FunctionCall }> = {
46
- code: "1",
47
- name: "function_call",
48
- parse: (value: JSONValue) => {
49
- if (
50
- value == null ||
51
- typeof value !== "object" ||
52
- !("function_call" in value) ||
53
- typeof value.function_call !== "object" ||
54
- value.function_call == null ||
55
- !("name" in value.function_call) ||
56
- !("arguments" in value.function_call) ||
57
- typeof value.function_call.name !== "string" ||
58
- typeof value.function_call.arguments !== "string"
59
- ) {
60
- throw new Error('"function_call" parts expect an object with a "function_call" property.');
61
- }
62
-
63
- return {
64
- type: "function_call",
65
- value: value as unknown as { function_call: FunctionCall },
66
- };
67
- },
68
- };
69
-
70
- const dataStreamPart: StreamPart<"2", "data", Array<JSONValue>> = {
71
- code: "2",
72
- name: "data",
73
- parse: (value: JSONValue) => {
74
- if (!Array.isArray(value)) {
75
- throw new Error('"data" parts expect an array value.');
76
- }
77
-
78
- return { type: "data", value };
79
- },
80
- };
81
-
82
- const errorStreamPart: StreamPart<"3", "error", string> = {
83
- code: "3",
84
- name: "error",
85
- parse: (value: JSONValue) => {
86
- if (typeof value !== "string") {
87
- throw new Error('"error" parts expect a string value.');
88
- }
89
- return { type: "error", value };
90
- },
91
- };
92
-
93
- const assistantMessage: StreamPart<"4", "assistant_message", AssistantMessage> = {
94
- code: "4",
95
- name: "assistant_message",
96
- parse: (value: JSONValue) => {
97
- if (
98
- value == null ||
99
- typeof value !== "object" ||
100
- !("id" in value) ||
101
- !("role" in value) ||
102
- !("content" in value) ||
103
- typeof value.id !== "string" ||
104
- typeof value.role !== "string" ||
105
- value.role !== "assistant" ||
106
- !Array.isArray(value.content) ||
107
- !value.content.every(
108
- (item) =>
109
- item != null &&
110
- typeof item === "object" &&
111
- "type" in item &&
112
- item.type === "text" &&
113
- "text" in item &&
114
- item.text != null &&
115
- typeof item.text === "object" &&
116
- "value" in item.text &&
117
- typeof item.text.value === "string",
118
- )
119
- ) {
120
- throw new Error(
121
- '"assistant_message" parts expect an object with an "id", "role", and "content" property.',
122
- );
123
- }
124
-
125
- return {
126
- type: "assistant_message",
127
- value: value as AssistantMessage,
128
- };
129
- },
130
- };
131
-
132
- const assistantControlData: StreamPart<
133
- "5",
134
- "assistant_control_data",
135
- {
136
- threadId: string;
137
- messageId: string;
138
- }
139
- > = {
140
- code: "5",
141
- name: "assistant_control_data",
142
- parse: (value: JSONValue) => {
143
- if (
144
- value == null ||
145
- typeof value !== "object" ||
146
- !("threadId" in value) ||
147
- !("messageId" in value) ||
148
- typeof value.threadId !== "string" ||
149
- typeof value.messageId !== "string"
150
- ) {
151
- throw new Error(
152
- '"assistant_control_data" parts expect an object with a "threadId" and "messageId" property.',
153
- );
154
- }
155
-
156
- return {
157
- type: "assistant_control_data",
158
- value: {
159
- threadId: value.threadId,
160
- messageId: value.messageId,
161
- },
162
- };
163
- },
164
- };
165
-
166
- const streamParts = [
167
- textStreamPart,
168
- functionCallStreamPart,
169
- dataStreamPart,
170
- errorStreamPart,
171
- assistantMessage,
172
- assistantControlData,
173
- ] as const;
174
-
175
- // union type of all stream parts
176
- type StreamParts =
177
- | typeof textStreamPart
178
- | typeof functionCallStreamPart
179
- | typeof dataStreamPart
180
- | typeof errorStreamPart
181
- | typeof assistantMessage
182
- | typeof assistantControlData;
183
-
184
- /**
185
- * Maps the type of a stream part to its value type.
186
- */
187
- type StreamPartValueType = {
188
- [P in StreamParts as P["name"]]: ReturnType<P["parse"]>["value"];
189
- };
190
-
191
- export type StreamPartType =
192
- | ReturnType<typeof textStreamPart.parse>
193
- | ReturnType<typeof functionCallStreamPart.parse>
194
- | ReturnType<typeof dataStreamPart.parse>
195
- | ReturnType<typeof errorStreamPart.parse>
196
- | ReturnType<typeof assistantMessage.parse>
197
- | ReturnType<typeof assistantControlData.parse>;
198
-
199
- export const streamPartsByCode = {
200
- [textStreamPart.code]: textStreamPart,
201
- [functionCallStreamPart.code]: functionCallStreamPart,
202
- [dataStreamPart.code]: dataStreamPart,
203
- [errorStreamPart.code]: errorStreamPart,
204
- [assistantMessage.code]: assistantMessage,
205
- [assistantControlData.code]: assistantControlData,
206
- } as const;
207
-
208
- /**
209
- * The map of prefixes for data in the stream
210
- *
211
- * - 0: Text from the LLM response
212
- * - 1: (OpenAI) function_call responses
213
- * - 2: custom JSON added by the user using `Data`
214
- *
215
- * Example:
216
- * ```
217
- * 0:Vercel
218
- * 0:'s
219
- * 0: AI
220
- * 0: AI
221
- * 0: SDK
222
- * 0: is great
223
- * 0:!
224
- * 2: { "someJson": "value" }
225
- * 1: {"function_call": {"name": "get_current_weather", "arguments": "{\\n\\"location\\": \\"Charlottesville, Virginia\\",\\n\\"format\\": \\"celsius\\"\\n}"}}
226
- *```
227
- */
228
- export const StreamStringPrefixes = {
229
- [textStreamPart.name]: textStreamPart.code,
230
- [functionCallStreamPart.name]: functionCallStreamPart.code,
231
- [dataStreamPart.name]: dataStreamPart.code,
232
- [errorStreamPart.name]: errorStreamPart.code,
233
- [assistantMessage.name]: assistantMessage.code,
234
- [assistantControlData.name]: assistantControlData.code,
235
- } as const;
236
-
237
- export const validCodes = streamParts.map((part) => part.code);
238
-
239
- /**
240
- * Parses a stream part from a string.
241
- *
242
- * @param line The string to parse.
243
- * @returns The parsed stream part.
244
- * @throws An error if the string cannot be parsed.
245
- */
246
- export const parseStreamPart = (line: string): StreamPartType => {
247
- const firstSeparatorIndex = line.indexOf(":");
248
-
249
- if (firstSeparatorIndex === -1) {
250
- throw new Error("Failed to parse stream string. No separator found.");
251
- }
252
-
253
- const prefix = line.slice(0, firstSeparatorIndex);
254
-
255
- if (!validCodes.includes(prefix as keyof typeof streamPartsByCode)) {
256
- throw new Error(`Failed to parse stream string. Invalid code ${prefix}.`);
257
- }
258
-
259
- const code = prefix as keyof typeof streamPartsByCode;
260
-
261
- const textValue = line.slice(firstSeparatorIndex + 1);
262
- const jsonValue: JSONValue = JSON.parse(textValue);
263
-
264
- return streamPartsByCode[code].parse(jsonValue);
265
- };
266
-
267
- /**
268
- * Prepends a string with a prefix from the `StreamChunkPrefixes`, JSON-ifies it,
269
- * and appends a new line.
270
- *
271
- * It ensures type-safety for the part type and value.
272
- */
273
- export function formatStreamPart<T extends keyof StreamPartValueType>(
274
- type: T,
275
- value: StreamPartValueType[T],
276
- ): StreamString {
277
- const streamPart = streamParts.find((part) => part.name === type);
278
-
279
- if (!streamPart) {
280
- throw new Error(`Invalid stream part type: ${type}`);
281
- }
282
-
283
- return `${streamPart.code}:${JSON.stringify(value)}\n`;
284
- }
285
-
286
- export const isStreamStringEqualToType = (
287
- type: keyof typeof StreamStringPrefixes,
288
- value: string,
289
- ): value is StreamString =>
290
- value.startsWith(`${StreamStringPrefixes[type]}:`) && value.endsWith("\n");
291
-
292
- export type StreamString =
293
- `${(typeof StreamStringPrefixes)[keyof typeof StreamStringPrefixes]}:${string}\n`;
294
-
295
- /**
296
- * A header sent to the client so it knows how to handle parsing the stream (as a deprecated text response or using the new prefixed protocol)
297
- */
298
- export const COMPLEX_HEADER = "X-Experimental-Stream-Data";