@copilotkit/shared 0.1.0-alpha.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/.turbo/turbo-build.log +43 -0
- package/CHANGELOG.md +7 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.mjs +138 -0
- package/dist/index.mjs.map +1 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.mjs +1 -0
- package/dist/types/index.mjs.map +1 -0
- package/dist/types/openai-assistant.d.ts +49 -0
- package/dist/types/openai-assistant.mjs +1 -0
- package/dist/types/openai-assistant.mjs.map +1 -0
- package/dist/utils/index.d.ts +2 -0
- package/dist/utils/index.mjs +138 -0
- package/dist/utils/index.mjs.map +1 -0
- package/dist/utils/utils.d.ts +101 -0
- package/dist/utils/utils.mjs +138 -0
- package/dist/utils/utils.mjs.map +1 -0
- package/dist/utils/utils.test.d.ts +2 -0
- package/dist/utils/utils.test.mjs +7 -0
- package/dist/utils/utils.test.mjs.map +1 -0
- package/jest.config.js +5 -0
- package/package.json +33 -0
- package/src/index.ts +2 -0
- package/src/types/index.ts +1 -0
- package/src/types/openai-assistant.ts +55 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/utils.test.ts +7 -0
- package/src/utils/utils.ts +280 -0
- package/tsconfig.json +15 -0
- package/tsup.config.ts +17 -0
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
|
|
2
|
+
> @copilotkit/shared@0.1.0 build /Users/ataibarkai/LocalGit/Recursively.ai/CopilotKit/CopilotKit/packages/shared
|
|
3
|
+
> tsup --treeshake
|
|
4
|
+
|
|
5
|
+
CLI Building entry: src/index.ts, src/types/index.ts, src/types/openai-assistant.ts, src/utils/index.ts, src/utils/utils.test.ts, src/utils/utils.ts
|
|
6
|
+
CLI Using tsconfig: tsconfig.json
|
|
7
|
+
CLI tsup v6.7.0
|
|
8
|
+
CLI Using tsup config: /Users/ataibarkai/LocalGit/Recursively.ai/CopilotKit/CopilotKit/packages/shared/tsup.config.ts
|
|
9
|
+
CLI Target: node14
|
|
10
|
+
CLI Cleaning output folder
|
|
11
|
+
ESM Build start
|
|
12
|
+
Generated an empty chunk: "chunk-YPBKY4KY".
|
|
13
|
+
Generated an empty chunk: "chunk-IAFBVORQ".
|
|
14
|
+
Generated an empty chunk: "chunk-YJLRG5U3".
|
|
15
|
+
ESM dist/chunk-YJLRG5U3.mjs 76.00 B
|
|
16
|
+
ESM dist/chunk-NCTOECKL.mjs 4.62 KB
|
|
17
|
+
ESM dist/utils/utils.test.mjs 202.00 B
|
|
18
|
+
ESM dist/types/openai-assistant.mjs 109.00 B
|
|
19
|
+
ESM dist/utils/utils.mjs 237.00 B
|
|
20
|
+
ESM dist/chunk-YPBKY4KY.mjs 76.00 B
|
|
21
|
+
ESM dist/index.mjs 329.00 B
|
|
22
|
+
ESM dist/utils/index.mjs 269.00 B
|
|
23
|
+
ESM dist/types/index.mjs 130.00 B
|
|
24
|
+
ESM dist/chunk-IAFBVORQ.mjs 76.00 B
|
|
25
|
+
ESM dist/chunk-YJLRG5U3.mjs.map 51.00 B
|
|
26
|
+
ESM dist/utils/utils.test.mjs.map 351.00 B
|
|
27
|
+
ESM dist/types/openai-assistant.mjs.map 51.00 B
|
|
28
|
+
ESM dist/utils/utils.mjs.map 51.00 B
|
|
29
|
+
ESM dist/chunk-YPBKY4KY.mjs.map 51.00 B
|
|
30
|
+
ESM dist/chunk-NCTOECKL.mjs.map 11.39 KB
|
|
31
|
+
ESM dist/index.mjs.map 51.00 B
|
|
32
|
+
ESM dist/utils/index.mjs.map 51.00 B
|
|
33
|
+
ESM dist/types/index.mjs.map 51.00 B
|
|
34
|
+
ESM dist/chunk-IAFBVORQ.mjs.map 51.00 B
|
|
35
|
+
ESM ⚡️ Build success in 55ms
|
|
36
|
+
DTS Build start
|
|
37
|
+
DTS ⚡️ Build success in 654ms
|
|
38
|
+
DTS dist/index.d.ts 306.00 B
|
|
39
|
+
DTS dist/types/openai-assistant.d.ts 1.40 KB
|
|
40
|
+
DTS dist/utils/utils.d.ts 4.17 KB
|
|
41
|
+
DTS dist/types/index.d.ts 92.00 B
|
|
42
|
+
DTS dist/utils/index.d.ts 241.00 B
|
|
43
|
+
DTS dist/utils/utils.test.d.ts 12.00 B
|
package/CHANGELOG.md
ADDED
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export { AssistantMessage, FunctionCall, JSONValue, Message } from './types/openai-assistant.js';
|
|
2
|
+
export { COMPLEX_HEADER, StreamPart, StreamPartType, StreamString, StreamStringPrefixes, formatStreamPart, isStreamStringEqualToType, parseStreamPart, streamPartsByCode, validCodes } from './utils/utils.js';
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
// src/utils/utils.ts
|
|
2
|
+
var textStreamPart = {
|
|
3
|
+
code: "0",
|
|
4
|
+
name: "text",
|
|
5
|
+
parse: (value) => {
|
|
6
|
+
if (typeof value !== "string") {
|
|
7
|
+
throw new Error('"text" parts expect a string value.');
|
|
8
|
+
}
|
|
9
|
+
return { type: "text", value };
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
var functionCallStreamPart = {
|
|
13
|
+
code: "1",
|
|
14
|
+
name: "function_call",
|
|
15
|
+
parse: (value) => {
|
|
16
|
+
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") {
|
|
17
|
+
throw new Error('"function_call" parts expect an object with a "function_call" property.');
|
|
18
|
+
}
|
|
19
|
+
return {
|
|
20
|
+
type: "function_call",
|
|
21
|
+
value
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
var dataStreamPart = {
|
|
26
|
+
code: "2",
|
|
27
|
+
name: "data",
|
|
28
|
+
parse: (value) => {
|
|
29
|
+
if (!Array.isArray(value)) {
|
|
30
|
+
throw new Error('"data" parts expect an array value.');
|
|
31
|
+
}
|
|
32
|
+
return { type: "data", value };
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
var errorStreamPart = {
|
|
36
|
+
code: "3",
|
|
37
|
+
name: "error",
|
|
38
|
+
parse: (value) => {
|
|
39
|
+
if (typeof value !== "string") {
|
|
40
|
+
throw new Error('"error" parts expect a string value.');
|
|
41
|
+
}
|
|
42
|
+
return { type: "error", value };
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
var assistantMessage = {
|
|
46
|
+
code: "4",
|
|
47
|
+
name: "assistant_message",
|
|
48
|
+
parse: (value) => {
|
|
49
|
+
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(
|
|
50
|
+
(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"
|
|
51
|
+
)) {
|
|
52
|
+
throw new Error(
|
|
53
|
+
'"assistant_message" parts expect an object with an "id", "role", and "content" property.'
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
return {
|
|
57
|
+
type: "assistant_message",
|
|
58
|
+
value
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
var assistantControlData = {
|
|
63
|
+
code: "5",
|
|
64
|
+
name: "assistant_control_data",
|
|
65
|
+
parse: (value) => {
|
|
66
|
+
if (value == null || typeof value !== "object" || !("threadId" in value) || !("messageId" in value) || typeof value.threadId !== "string" || typeof value.messageId !== "string") {
|
|
67
|
+
throw new Error(
|
|
68
|
+
'"assistant_control_data" parts expect an object with a "threadId" and "messageId" property.'
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
return {
|
|
72
|
+
type: "assistant_control_data",
|
|
73
|
+
value: {
|
|
74
|
+
threadId: value.threadId,
|
|
75
|
+
messageId: value.messageId
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
var streamParts = [
|
|
81
|
+
textStreamPart,
|
|
82
|
+
functionCallStreamPart,
|
|
83
|
+
dataStreamPart,
|
|
84
|
+
errorStreamPart,
|
|
85
|
+
assistantMessage,
|
|
86
|
+
assistantControlData
|
|
87
|
+
];
|
|
88
|
+
var streamPartsByCode = {
|
|
89
|
+
[textStreamPart.code]: textStreamPart,
|
|
90
|
+
[functionCallStreamPart.code]: functionCallStreamPart,
|
|
91
|
+
[dataStreamPart.code]: dataStreamPart,
|
|
92
|
+
[errorStreamPart.code]: errorStreamPart,
|
|
93
|
+
[assistantMessage.code]: assistantMessage,
|
|
94
|
+
[assistantControlData.code]: assistantControlData
|
|
95
|
+
};
|
|
96
|
+
var StreamStringPrefixes = {
|
|
97
|
+
[textStreamPart.name]: textStreamPart.code,
|
|
98
|
+
[functionCallStreamPart.name]: functionCallStreamPart.code,
|
|
99
|
+
[dataStreamPart.name]: dataStreamPart.code,
|
|
100
|
+
[errorStreamPart.name]: errorStreamPart.code,
|
|
101
|
+
[assistantMessage.name]: assistantMessage.code,
|
|
102
|
+
[assistantControlData.name]: assistantControlData.code
|
|
103
|
+
};
|
|
104
|
+
var validCodes = streamParts.map((part) => part.code);
|
|
105
|
+
var parseStreamPart = (line) => {
|
|
106
|
+
const firstSeparatorIndex = line.indexOf(":");
|
|
107
|
+
if (firstSeparatorIndex === -1) {
|
|
108
|
+
throw new Error("Failed to parse stream string. No separator found.");
|
|
109
|
+
}
|
|
110
|
+
const prefix = line.slice(0, firstSeparatorIndex);
|
|
111
|
+
if (!validCodes.includes(prefix)) {
|
|
112
|
+
throw new Error(`Failed to parse stream string. Invalid code ${prefix}.`);
|
|
113
|
+
}
|
|
114
|
+
const code = prefix;
|
|
115
|
+
const textValue = line.slice(firstSeparatorIndex + 1);
|
|
116
|
+
const jsonValue = JSON.parse(textValue);
|
|
117
|
+
return streamPartsByCode[code].parse(jsonValue);
|
|
118
|
+
};
|
|
119
|
+
function formatStreamPart(type, value) {
|
|
120
|
+
const streamPart = streamParts.find((part) => part.name === type);
|
|
121
|
+
if (!streamPart) {
|
|
122
|
+
throw new Error(`Invalid stream part type: ${type}`);
|
|
123
|
+
}
|
|
124
|
+
return `${streamPart.code}:${JSON.stringify(value)}
|
|
125
|
+
`;
|
|
126
|
+
}
|
|
127
|
+
var isStreamStringEqualToType = (type, value) => value.startsWith(`${StreamStringPrefixes[type]}:`) && value.endsWith("\n");
|
|
128
|
+
var COMPLEX_HEADER = "X-Experimental-Stream-Data";
|
|
129
|
+
export {
|
|
130
|
+
COMPLEX_HEADER,
|
|
131
|
+
StreamStringPrefixes,
|
|
132
|
+
formatStreamPart,
|
|
133
|
+
isStreamStringEqualToType,
|
|
134
|
+
parseStreamPart,
|
|
135
|
+
streamPartsByCode,
|
|
136
|
+
validCodes
|
|
137
|
+
};
|
|
138
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/utils/utils.ts"],"sourcesContent":["import { AssistantMessage, FunctionCall, JSONValue } from \"../types/openai-assistant\";\n\nexport interface StreamPart<CODE extends string, NAME extends string, TYPE> {\n code: CODE;\n name: NAME;\n parse: (value: JSONValue) => { type: NAME; value: TYPE };\n}\n\nconst textStreamPart: StreamPart<\"0\", \"text\", string> = {\n code: \"0\",\n name: \"text\",\n parse: (value: JSONValue) => {\n if (typeof value !== \"string\") {\n throw new Error('\"text\" parts expect a string value.');\n }\n return { type: \"text\", value };\n },\n};\n\n/**\n * This is a utility function that helps in parsing the stream parts.\n * It takes a JSONValue as input and returns an object with type and value.\n * The type is a string that represents the type of the stream part.\n * The value is the actual value of the stream part.\n * If the input value is not a string, it throws an error.\n */\nconst functionCallStreamPart: StreamPart<\"1\", \"function_call\", { function_call: FunctionCall }> = {\n code: \"1\",\n name: \"function_call\",\n parse: (value: JSONValue) => {\n if (\n value == null ||\n typeof value !== \"object\" ||\n !(\"function_call\" in value) ||\n typeof value.function_call !== \"object\" ||\n value.function_call == null ||\n !(\"name\" in value.function_call) ||\n !(\"arguments\" in value.function_call) ||\n typeof value.function_call.name !== \"string\" ||\n typeof value.function_call.arguments !== \"string\"\n ) {\n throw new Error('\"function_call\" parts expect an object with a \"function_call\" property.');\n }\n\n return {\n type: \"function_call\",\n value: value as unknown as { function_call: FunctionCall },\n };\n },\n};\n\nconst dataStreamPart: StreamPart<\"2\", \"data\", Array<JSONValue>> = {\n code: \"2\",\n name: \"data\",\n parse: (value: JSONValue) => {\n if (!Array.isArray(value)) {\n throw new Error('\"data\" parts expect an array value.');\n }\n\n return { type: \"data\", value };\n },\n};\n\nconst errorStreamPart: StreamPart<\"3\", \"error\", string> = {\n code: \"3\",\n name: \"error\",\n parse: (value: JSONValue) => {\n if (typeof value !== \"string\") {\n throw new Error('\"error\" parts expect a string value.');\n }\n return { type: \"error\", value };\n },\n};\n\nconst assistantMessage: StreamPart<\"4\", \"assistant_message\", AssistantMessage> = {\n code: \"4\",\n name: \"assistant_message\",\n parse: (value: JSONValue) => {\n if (\n value == null ||\n typeof value !== \"object\" ||\n !(\"id\" in value) ||\n !(\"role\" in value) ||\n !(\"content\" in value) ||\n typeof value.id !== \"string\" ||\n typeof value.role !== \"string\" ||\n value.role !== \"assistant\" ||\n !Array.isArray(value.content) ||\n !value.content.every(\n (item) =>\n item != null &&\n typeof item === \"object\" &&\n \"type\" in item &&\n item.type === \"text\" &&\n \"text\" in item &&\n item.text != null &&\n typeof item.text === \"object\" &&\n \"value\" in item.text &&\n typeof item.text.value === \"string\",\n )\n ) {\n throw new Error(\n '\"assistant_message\" parts expect an object with an \"id\", \"role\", and \"content\" property.',\n );\n }\n\n return {\n type: \"assistant_message\",\n value: value as AssistantMessage,\n };\n },\n};\n\nconst assistantControlData: StreamPart<\n \"5\",\n \"assistant_control_data\",\n {\n threadId: string;\n messageId: string;\n }\n> = {\n code: \"5\",\n name: \"assistant_control_data\",\n parse: (value: JSONValue) => {\n if (\n value == null ||\n typeof value !== \"object\" ||\n !(\"threadId\" in value) ||\n !(\"messageId\" in value) ||\n typeof value.threadId !== \"string\" ||\n typeof value.messageId !== \"string\"\n ) {\n throw new Error(\n '\"assistant_control_data\" parts expect an object with a \"threadId\" and \"messageId\" property.',\n );\n }\n\n return {\n type: \"assistant_control_data\",\n value: {\n threadId: value.threadId,\n messageId: value.messageId,\n },\n };\n },\n};\n\nconst streamParts = [\n textStreamPart,\n functionCallStreamPart,\n dataStreamPart,\n errorStreamPart,\n assistantMessage,\n assistantControlData,\n] as const;\n\n// union type of all stream parts\ntype StreamParts =\n | typeof textStreamPart\n | typeof functionCallStreamPart\n | typeof dataStreamPart\n | typeof errorStreamPart\n | typeof assistantMessage\n | typeof assistantControlData;\n\n/**\n * Maps the type of a stream part to its value type.\n */\ntype StreamPartValueType = {\n [P in StreamParts as P[\"name\"]]: ReturnType<P[\"parse\"]>[\"value\"];\n};\n\nexport type StreamPartType =\n | ReturnType<typeof textStreamPart.parse>\n | ReturnType<typeof functionCallStreamPart.parse>\n | ReturnType<typeof dataStreamPart.parse>\n | ReturnType<typeof errorStreamPart.parse>\n | ReturnType<typeof assistantMessage.parse>\n | ReturnType<typeof assistantControlData.parse>;\n\nexport const streamPartsByCode = {\n [textStreamPart.code]: textStreamPart,\n [functionCallStreamPart.code]: functionCallStreamPart,\n [dataStreamPart.code]: dataStreamPart,\n [errorStreamPart.code]: errorStreamPart,\n [assistantMessage.code]: assistantMessage,\n [assistantControlData.code]: assistantControlData,\n} as const;\n\n/**\n * The map of prefixes for data in the stream\n *\n * - 0: Text from the LLM response\n * - 1: (OpenAI) function_call responses\n * - 2: custom JSON added by the user using `Data`\n *\n * Example:\n * ```\n * 0:Vercel\n * 0:'s\n * 0: AI\n * 0: AI\n * 0: SDK\n * 0: is great\n * 0:!\n * 2: { \"someJson\": \"value\" }\n * 1: {\"function_call\": {\"name\": \"get_current_weather\", \"arguments\": \"{\\\\n\\\\\"location\\\\\": \\\\\"Charlottesville, Virginia\\\\\",\\\\n\\\\\"format\\\\\": \\\\\"celsius\\\\\"\\\\n}\"}}\n *```\n */\nexport const StreamStringPrefixes = {\n [textStreamPart.name]: textStreamPart.code,\n [functionCallStreamPart.name]: functionCallStreamPart.code,\n [dataStreamPart.name]: dataStreamPart.code,\n [errorStreamPart.name]: errorStreamPart.code,\n [assistantMessage.name]: assistantMessage.code,\n [assistantControlData.name]: assistantControlData.code,\n} as const;\n\nexport const validCodes = streamParts.map((part) => part.code);\n\n/**\n * Parses a stream part from a string.\n *\n * @param line The string to parse.\n * @returns The parsed stream part.\n * @throws An error if the string cannot be parsed.\n */\nexport const parseStreamPart = (line: string): StreamPartType => {\n const firstSeparatorIndex = line.indexOf(\":\");\n\n if (firstSeparatorIndex === -1) {\n throw new Error(\"Failed to parse stream string. No separator found.\");\n }\n\n const prefix = line.slice(0, firstSeparatorIndex);\n\n if (!validCodes.includes(prefix as keyof typeof streamPartsByCode)) {\n throw new Error(`Failed to parse stream string. Invalid code ${prefix}.`);\n }\n\n const code = prefix as keyof typeof streamPartsByCode;\n\n const textValue = line.slice(firstSeparatorIndex + 1);\n const jsonValue: JSONValue = JSON.parse(textValue);\n\n return streamPartsByCode[code].parse(jsonValue);\n};\n\n/**\n * Prepends a string with a prefix from the `StreamChunkPrefixes`, JSON-ifies it,\n * and appends a new line.\n *\n * It ensures type-safety for the part type and value.\n */\nexport function formatStreamPart<T extends keyof StreamPartValueType>(\n type: T,\n value: StreamPartValueType[T],\n): StreamString {\n const streamPart = streamParts.find((part) => part.name === type);\n\n if (!streamPart) {\n throw new Error(`Invalid stream part type: ${type}`);\n }\n\n return `${streamPart.code}:${JSON.stringify(value)}\\n`;\n}\n\nexport const isStreamStringEqualToType = (\n type: keyof typeof StreamStringPrefixes,\n value: string,\n): value is StreamString =>\n value.startsWith(`${StreamStringPrefixes[type]}:`) && value.endsWith(\"\\n\");\n\nexport type StreamString =\n `${typeof StreamStringPrefixes[keyof typeof StreamStringPrefixes]}:${string}\\n`;\n\n/**\n * 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)\n */\nexport const COMPLEX_HEADER = \"X-Experimental-Stream-Data\";\n"],"mappings":";AAQA,IAAM,iBAAkD;AAAA,EACtD,MAAM;AAAA,EACN,MAAM;AAAA,EACN,OAAO,CAAC,UAAqB;AAC3B,QAAI,OAAO,UAAU,UAAU;AAC7B,YAAM,IAAI,MAAM,qCAAqC;AAAA,IACvD;AACA,WAAO,EAAE,MAAM,QAAQ,MAAM;AAAA,EAC/B;AACF;AASA,IAAM,yBAA4F;AAAA,EAChG,MAAM;AAAA,EACN,MAAM;AAAA,EACN,OAAO,CAAC,UAAqB;AAC3B,QACE,SAAS,QACT,OAAO,UAAU,YACjB,EAAE,mBAAmB,UACrB,OAAO,MAAM,kBAAkB,YAC/B,MAAM,iBAAiB,QACvB,EAAE,UAAU,MAAM,kBAClB,EAAE,eAAe,MAAM,kBACvB,OAAO,MAAM,cAAc,SAAS,YACpC,OAAO,MAAM,cAAc,cAAc,UACzC;AACA,YAAM,IAAI,MAAM,yEAAyE;AAAA,IAC3F;AAEA,WAAO;AAAA,MACL,MAAM;AAAA,MACN;AAAA,IACF;AAAA,EACF;AACF;AAEA,IAAM,iBAA4D;AAAA,EAChE,MAAM;AAAA,EACN,MAAM;AAAA,EACN,OAAO,CAAC,UAAqB;AAC3B,QAAI,CAAC,MAAM,QAAQ,KAAK,GAAG;AACzB,YAAM,IAAI,MAAM,qCAAqC;AAAA,IACvD;AAEA,WAAO,EAAE,MAAM,QAAQ,MAAM;AAAA,EAC/B;AACF;AAEA,IAAM,kBAAoD;AAAA,EACxD,MAAM;AAAA,EACN,MAAM;AAAA,EACN,OAAO,CAAC,UAAqB;AAC3B,QAAI,OAAO,UAAU,UAAU;AAC7B,YAAM,IAAI,MAAM,sCAAsC;AAAA,IACxD;AACA,WAAO,EAAE,MAAM,SAAS,MAAM;AAAA,EAChC;AACF;AAEA,IAAM,mBAA2E;AAAA,EAC/E,M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|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { AssistantMessage, FunctionCall, JSONValue, Message } from './openai-assistant.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
interface FunctionCall {
|
|
2
|
+
/**
|
|
3
|
+
* The arguments to call the function with, as generated by the model in JSON
|
|
4
|
+
* format. Note that the model does not always generate valid JSON, and may
|
|
5
|
+
* hallucinate parameters not defined by your function schema. Validate the
|
|
6
|
+
* arguments in your code before calling your function.
|
|
7
|
+
*/
|
|
8
|
+
arguments?: string;
|
|
9
|
+
/**
|
|
10
|
+
* The name of the function to call.
|
|
11
|
+
*/
|
|
12
|
+
name?: string;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Shared types between the API and UI packages.
|
|
16
|
+
*/
|
|
17
|
+
interface Message {
|
|
18
|
+
id: string;
|
|
19
|
+
createdAt?: Date;
|
|
20
|
+
content: string;
|
|
21
|
+
ui?: string | null | undefined;
|
|
22
|
+
role: "system" | "user" | "assistant" | "function";
|
|
23
|
+
/**
|
|
24
|
+
* If the message has a role of `function`, the `name` field is the name of the function.
|
|
25
|
+
* Otherwise, the name field should not be set.
|
|
26
|
+
*/
|
|
27
|
+
name?: string;
|
|
28
|
+
/**
|
|
29
|
+
* If the assistant role makes a function call, the `function_call` field
|
|
30
|
+
* contains the function call name and arguments. Otherwise, the field should
|
|
31
|
+
* not be set.
|
|
32
|
+
*/
|
|
33
|
+
function_call?: string | FunctionCall;
|
|
34
|
+
}
|
|
35
|
+
type AssistantMessage = {
|
|
36
|
+
id: string;
|
|
37
|
+
role: "assistant";
|
|
38
|
+
content: Array<{
|
|
39
|
+
type: "text";
|
|
40
|
+
text: {
|
|
41
|
+
value: string;
|
|
42
|
+
};
|
|
43
|
+
}>;
|
|
44
|
+
};
|
|
45
|
+
type JSONValue = null | string | number | boolean | {
|
|
46
|
+
[x: string]: JSONValue;
|
|
47
|
+
} | Array<JSONValue>;
|
|
48
|
+
|
|
49
|
+
export { AssistantMessage, FunctionCall, JSONValue, Message };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=openai-assistant.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
// src/utils/utils.ts
|
|
2
|
+
var textStreamPart = {
|
|
3
|
+
code: "0",
|
|
4
|
+
name: "text",
|
|
5
|
+
parse: (value) => {
|
|
6
|
+
if (typeof value !== "string") {
|
|
7
|
+
throw new Error('"text" parts expect a string value.');
|
|
8
|
+
}
|
|
9
|
+
return { type: "text", value };
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
var functionCallStreamPart = {
|
|
13
|
+
code: "1",
|
|
14
|
+
name: "function_call",
|
|
15
|
+
parse: (value) => {
|
|
16
|
+
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") {
|
|
17
|
+
throw new Error('"function_call" parts expect an object with a "function_call" property.');
|
|
18
|
+
}
|
|
19
|
+
return {
|
|
20
|
+
type: "function_call",
|
|
21
|
+
value
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
var dataStreamPart = {
|
|
26
|
+
code: "2",
|
|
27
|
+
name: "data",
|
|
28
|
+
parse: (value) => {
|
|
29
|
+
if (!Array.isArray(value)) {
|
|
30
|
+
throw new Error('"data" parts expect an array value.');
|
|
31
|
+
}
|
|
32
|
+
return { type: "data", value };
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
var errorStreamPart = {
|
|
36
|
+
code: "3",
|
|
37
|
+
name: "error",
|
|
38
|
+
parse: (value) => {
|
|
39
|
+
if (typeof value !== "string") {
|
|
40
|
+
throw new Error('"error" parts expect a string value.');
|
|
41
|
+
}
|
|
42
|
+
return { type: "error", value };
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
var assistantMessage = {
|
|
46
|
+
code: "4",
|
|
47
|
+
name: "assistant_message",
|
|
48
|
+
parse: (value) => {
|
|
49
|
+
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(
|
|
50
|
+
(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"
|
|
51
|
+
)) {
|
|
52
|
+
throw new Error(
|
|
53
|
+
'"assistant_message" parts expect an object with an "id", "role", and "content" property.'
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
return {
|
|
57
|
+
type: "assistant_message",
|
|
58
|
+
value
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
var assistantControlData = {
|
|
63
|
+
code: "5",
|
|
64
|
+
name: "assistant_control_data",
|
|
65
|
+
parse: (value) => {
|
|
66
|
+
if (value == null || typeof value !== "object" || !("threadId" in value) || !("messageId" in value) || typeof value.threadId !== "string" || typeof value.messageId !== "string") {
|
|
67
|
+
throw new Error(
|
|
68
|
+
'"assistant_control_data" parts expect an object with a "threadId" and "messageId" property.'
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
return {
|
|
72
|
+
type: "assistant_control_data",
|
|
73
|
+
value: {
|
|
74
|
+
threadId: value.threadId,
|
|
75
|
+
messageId: value.messageId
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
var streamParts = [
|
|
81
|
+
textStreamPart,
|
|
82
|
+
functionCallStreamPart,
|
|
83
|
+
dataStreamPart,
|
|
84
|
+
errorStreamPart,
|
|
85
|
+
assistantMessage,
|
|
86
|
+
assistantControlData
|
|
87
|
+
];
|
|
88
|
+
var streamPartsByCode = {
|
|
89
|
+
[textStreamPart.code]: textStreamPart,
|
|
90
|
+
[functionCallStreamPart.code]: functionCallStreamPart,
|
|
91
|
+
[dataStreamPart.code]: dataStreamPart,
|
|
92
|
+
[errorStreamPart.code]: errorStreamPart,
|
|
93
|
+
[assistantMessage.code]: assistantMessage,
|
|
94
|
+
[assistantControlData.code]: assistantControlData
|
|
95
|
+
};
|
|
96
|
+
var StreamStringPrefixes = {
|
|
97
|
+
[textStreamPart.name]: textStreamPart.code,
|
|
98
|
+
[functionCallStreamPart.name]: functionCallStreamPart.code,
|
|
99
|
+
[dataStreamPart.name]: dataStreamPart.code,
|
|
100
|
+
[errorStreamPart.name]: errorStreamPart.code,
|
|
101
|
+
[assistantMessage.name]: assistantMessage.code,
|
|
102
|
+
[assistantControlData.name]: assistantControlData.code
|
|
103
|
+
};
|
|
104
|
+
var validCodes = streamParts.map((part) => part.code);
|
|
105
|
+
var parseStreamPart = (line) => {
|
|
106
|
+
const firstSeparatorIndex = line.indexOf(":");
|
|
107
|
+
if (firstSeparatorIndex === -1) {
|
|
108
|
+
throw new Error("Failed to parse stream string. No separator found.");
|
|
109
|
+
}
|
|
110
|
+
const prefix = line.slice(0, firstSeparatorIndex);
|
|
111
|
+
if (!validCodes.includes(prefix)) {
|
|
112
|
+
throw new Error(`Failed to parse stream string. Invalid code ${prefix}.`);
|
|
113
|
+
}
|
|
114
|
+
const code = prefix;
|
|
115
|
+
const textValue = line.slice(firstSeparatorIndex + 1);
|
|
116
|
+
const jsonValue = JSON.parse(textValue);
|
|
117
|
+
return streamPartsByCode[code].parse(jsonValue);
|
|
118
|
+
};
|
|
119
|
+
function formatStreamPart(type, value) {
|
|
120
|
+
const streamPart = streamParts.find((part) => part.name === type);
|
|
121
|
+
if (!streamPart) {
|
|
122
|
+
throw new Error(`Invalid stream part type: ${type}`);
|
|
123
|
+
}
|
|
124
|
+
return `${streamPart.code}:${JSON.stringify(value)}
|
|
125
|
+
`;
|
|
126
|
+
}
|
|
127
|
+
var isStreamStringEqualToType = (type, value) => value.startsWith(`${StreamStringPrefixes[type]}:`) && value.endsWith("\n");
|
|
128
|
+
var COMPLEX_HEADER = "X-Experimental-Stream-Data";
|
|
129
|
+
export {
|
|
130
|
+
COMPLEX_HEADER,
|
|
131
|
+
StreamStringPrefixes,
|
|
132
|
+
formatStreamPart,
|
|
133
|
+
isStreamStringEqualToType,
|
|
134
|
+
parseStreamPart,
|
|
135
|
+
streamPartsByCode,
|
|
136
|
+
validCodes
|
|
137
|
+
};
|
|
138
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/utils/utils.ts"],"sourcesContent":["import { AssistantMessage, FunctionCall, JSONValue } from \"../types/openai-assistant\";\n\nexport interface StreamPart<CODE extends string, NAME extends string, TYPE> {\n code: CODE;\n name: NAME;\n parse: (value: JSONValue) => { type: NAME; value: TYPE };\n}\n\nconst textStreamPart: StreamPart<\"0\", \"text\", string> = {\n code: \"0\",\n name: \"text\",\n parse: (value: JSONValue) => {\n if (typeof value !== \"string\") {\n throw new Error('\"text\" parts expect a string value.');\n }\n return { type: \"text\", value };\n },\n};\n\n/**\n * This is a utility function that helps in parsing the stream parts.\n * It takes a JSONValue as input and returns an object with type and value.\n * The type is a string that represents the type of the stream part.\n * The value is the actual value of the stream part.\n * If the input value is not a string, it throws an error.\n */\nconst functionCallStreamPart: StreamPart<\"1\", \"function_call\", { function_call: FunctionCall }> = {\n code: \"1\",\n name: \"function_call\",\n parse: (value: JSONValue) => {\n if (\n value == null ||\n typeof value !== \"object\" ||\n !(\"function_call\" in value) ||\n typeof value.function_call !== \"object\" ||\n value.function_call == null ||\n !(\"name\" in value.function_call) ||\n !(\"arguments\" in value.function_call) ||\n typeof value.function_call.name !== \"string\" ||\n typeof value.function_call.arguments !== \"string\"\n ) {\n throw new Error('\"function_call\" parts expect an object with a \"function_call\" property.');\n }\n\n return {\n type: \"function_call\",\n value: value as unknown as { function_call: FunctionCall },\n };\n },\n};\n\nconst dataStreamPart: StreamPart<\"2\", \"data\", Array<JSONValue>> = {\n code: \"2\",\n name: \"data\",\n parse: (value: JSONValue) => {\n if (!Array.isArray(value)) {\n throw new Error('\"data\" parts expect an array value.');\n }\n\n return { type: \"data\", value };\n },\n};\n\nconst errorStreamPart: StreamPart<\"3\", \"error\", string> = {\n code: \"3\",\n name: \"error\",\n parse: (value: JSONValue) => {\n if (typeof value !== \"string\") {\n throw new Error('\"error\" parts expect a string value.');\n }\n return { type: \"error\", value };\n },\n};\n\nconst assistantMessage: StreamPart<\"4\", \"assistant_message\", AssistantMessage> = {\n code: \"4\",\n name: \"assistant_message\",\n parse: (value: JSONValue) => {\n if (\n value == null ||\n typeof value !== \"object\" ||\n !(\"id\" in value) ||\n !(\"role\" in value) ||\n !(\"content\" in value) ||\n typeof value.id !== \"string\" ||\n typeof value.role !== \"string\" ||\n value.role !== \"assistant\" ||\n !Array.isArray(value.content) ||\n !value.content.every(\n (item) =>\n item != null &&\n typeof item === \"object\" &&\n \"type\" in item &&\n item.type === \"text\" &&\n \"text\" in item &&\n item.text != null &&\n typeof item.text === \"object\" &&\n \"value\" in item.text &&\n typeof item.text.value === \"string\",\n )\n ) {\n throw new Error(\n '\"assistant_message\" parts expect an object with an \"id\", \"role\", and \"content\" property.',\n );\n }\n\n return {\n type: \"assistant_message\",\n value: value as AssistantMessage,\n };\n },\n};\n\nconst assistantControlData: StreamPart<\n \"5\",\n \"assistant_control_data\",\n {\n threadId: string;\n messageId: string;\n }\n> = {\n code: \"5\",\n name: \"assistant_control_data\",\n parse: (value: JSONValue) => {\n if (\n value == null ||\n typeof value !== \"object\" ||\n !(\"threadId\" in value) ||\n !(\"messageId\" in value) ||\n typeof value.threadId !== \"string\" ||\n typeof value.messageId !== \"string\"\n ) {\n throw new Error(\n '\"assistant_control_data\" parts expect an object with a \"threadId\" and \"messageId\" property.',\n );\n }\n\n return {\n type: \"assistant_control_data\",\n value: {\n threadId: value.threadId,\n messageId: value.messageId,\n },\n };\n },\n};\n\nconst streamParts = [\n textStreamPart,\n functionCallStreamPart,\n dataStreamPart,\n errorStreamPart,\n assistantMessage,\n assistantControlData,\n] as const;\n\n// union type of all stream parts\ntype StreamParts =\n | typeof textStreamPart\n | typeof functionCallStreamPart\n | typeof dataStreamPart\n | typeof errorStreamPart\n | typeof assistantMessage\n | typeof assistantControlData;\n\n/**\n * Maps the type of a stream part to its value type.\n */\ntype StreamPartValueType = {\n [P in StreamParts as P[\"name\"]]: ReturnType<P[\"parse\"]>[\"value\"];\n};\n\nexport type StreamPartType =\n | ReturnType<typeof textStreamPart.parse>\n | ReturnType<typeof functionCallStreamPart.parse>\n | ReturnType<typeof dataStreamPart.parse>\n | ReturnType<typeof errorStreamPart.parse>\n | ReturnType<typeof assistantMessage.parse>\n | ReturnType<typeof assistantControlData.parse>;\n\nexport const streamPartsByCode = {\n [textStreamPart.code]: textStreamPart,\n [functionCallStreamPart.code]: functionCallStreamPart,\n [dataStreamPart.code]: dataStreamPart,\n [errorStreamPart.code]: errorStreamPart,\n [assistantMessage.code]: assistantMessage,\n [assistantControlData.code]: assistantControlData,\n} as const;\n\n/**\n * The map of prefixes for data in the stream\n *\n * - 0: Text from the LLM response\n * - 1: (OpenAI) function_call responses\n * - 2: custom JSON added by the user using `Data`\n *\n * Example:\n * ```\n * 0:Vercel\n * 0:'s\n * 0: AI\n * 0: AI\n * 0: SDK\n * 0: is great\n * 0:!\n * 2: { \"someJson\": \"value\" }\n * 1: {\"function_call\": {\"name\": \"get_current_weather\", \"arguments\": \"{\\\\n\\\\\"location\\\\\": \\\\\"Charlottesville, Virginia\\\\\",\\\\n\\\\\"format\\\\\": \\\\\"celsius\\\\\"\\\\n}\"}}\n *```\n */\nexport const StreamStringPrefixes = {\n [textStreamPart.name]: textStreamPart.code,\n [functionCallStreamPart.name]: functionCallStreamPart.code,\n [dataStreamPart.name]: dataStreamPart.code,\n [errorStreamPart.name]: errorStreamPart.code,\n [assistantMessage.name]: assistantMessage.code,\n [assistantControlData.name]: assistantControlData.code,\n} as const;\n\nexport const validCodes = streamParts.map((part) => part.code);\n\n/**\n * Parses a stream part from a string.\n *\n * @param line The string to parse.\n * @returns The parsed stream part.\n * @throws An error if the string cannot be parsed.\n */\nexport const parseStreamPart = (line: string): StreamPartType => {\n const firstSeparatorIndex = line.indexOf(\":\");\n\n if (firstSeparatorIndex === -1) {\n throw new Error(\"Failed to parse stream string. No separator found.\");\n }\n\n const prefix = line.slice(0, firstSeparatorIndex);\n\n if (!validCodes.includes(prefix as keyof typeof streamPartsByCode)) {\n throw new Error(`Failed to parse stream string. Invalid code ${prefix}.`);\n }\n\n const code = prefix as keyof typeof streamPartsByCode;\n\n const textValue = line.slice(firstSeparatorIndex + 1);\n const jsonValue: JSONValue = JSON.parse(textValue);\n\n return streamPartsByCode[code].parse(jsonValue);\n};\n\n/**\n * Prepends a string with a prefix from the `StreamChunkPrefixes`, JSON-ifies it,\n * and appends a new line.\n *\n * It ensures type-safety for the part type and value.\n */\nexport function formatStreamPart<T extends keyof StreamPartValueType>(\n type: T,\n value: StreamPartValueType[T],\n): StreamString {\n const streamPart = streamParts.find((part) => part.name === type);\n\n if (!streamPart) {\n throw new Error(`Invalid stream part type: ${type}`);\n }\n\n return `${streamPart.code}:${JSON.stringify(value)}\\n`;\n}\n\nexport const isStreamStringEqualToType = (\n type: keyof typeof StreamStringPrefixes,\n value: string,\n): value is StreamString =>\n value.startsWith(`${StreamStringPrefixes[type]}:`) && value.endsWith(\"\\n\");\n\nexport type StreamString =\n `${typeof StreamStringPrefixes[keyof typeof StreamStringPrefixes]}:${string}\\n`;\n\n/**\n * 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)\n */\nexport const COMPLEX_HEADER = \"X-Experimental-Stream-Data\";\n"],"mappings":";AAQA,IAAM,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GAAG,eAAe;AAAA,EACtC,CAAC,uBAAuB,IAAI,GAAG,uBAAuB;AAAA,EACtD,CAAC,eAAe,IAAI,GAAG,eAAe;AAAA,EACtC,CAAC,gBAAgB,IAAI,GAAG,gBAAgB;AAAA,EACxC,CAAC,iBAAiB,IAAI,GAAG,iBAAiB;AAAA,EAC1C,CAAC,qBAAqB,IAAI,GAAG,qBAAqB;AACpD;AAEO,IAAM,aAAa,YAAY,IAAI,CAAC,SAAS,KAAK,IAAI;AAStD,IAAM,kBAAkB,CAAC,SAAiC;AAC/D,QAAM,sBAAsB,KAAK,QAAQ,GAAG;AAE5C,MAAI,wBAAwB,IAAI;AAC9B,UAAM,IAAI,MAAM,oDAAoD;AAAA,EACtE;AAEA,QAAM,SAAS,KAAK,MAAM,GAAG,mBAAmB;AAEhD,MAAI,CAAC,WAAW,SAAS,MAAwC,GAAG;AAClE,UAAM,IAAI,MAAM,+CAA+C,SAAS;AAAA,EAC1E;AAEA,QAAM,OAAO;AAEb,QAAM,YAAY,KAAK,MAAM,sBAAsB,CAAC;AACpD,QAAM,YAAuB,KAAK,MAAM,SAAS;AAEjD,SAAO,kBAAkB,IAAI,EAAE,MAAM,SAAS;AAChD;AAQO,SAAS,iBACd,MACA,OACc;AACd,QAAM,aAAa,YAAY,KAAK,CAAC,SAAS,KAAK,SAAS,IAAI;AAEhE,MAAI,CAAC,YAAY;AACf,UAAM,IAAI,MAAM,6BAA6B,MAAM;AAAA,EACrD;AAEA,SAAO,GAAG,WAAW,QAAQ,KAAK,UAAU,KAAK;AAAA;AACnD;AAEO,IAAM,4BAA4B,CACvC,MACA,UAEA,MAAM,WAAW,GAAG,qBAAqB,IAAI,IAAI,KAAK,MAAM,SAAS,IAAI;AAQpE,IAAM,iBAAiB;","names":[]}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { JSONValue, FunctionCall, AssistantMessage } from '../types/openai-assistant.js';
|
|
2
|
+
|
|
3
|
+
interface StreamPart<CODE extends string, NAME extends string, TYPE> {
|
|
4
|
+
code: CODE;
|
|
5
|
+
name: NAME;
|
|
6
|
+
parse: (value: JSONValue) => {
|
|
7
|
+
type: NAME;
|
|
8
|
+
value: TYPE;
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
declare const textStreamPart: StreamPart<"0", "text", string>;
|
|
12
|
+
/**
|
|
13
|
+
* This is a utility function that helps in parsing the stream parts.
|
|
14
|
+
* It takes a JSONValue as input and returns an object with type and value.
|
|
15
|
+
* The type is a string that represents the type of the stream part.
|
|
16
|
+
* The value is the actual value of the stream part.
|
|
17
|
+
* If the input value is not a string, it throws an error.
|
|
18
|
+
*/
|
|
19
|
+
declare const functionCallStreamPart: StreamPart<"1", "function_call", {
|
|
20
|
+
function_call: FunctionCall;
|
|
21
|
+
}>;
|
|
22
|
+
declare const dataStreamPart: StreamPart<"2", "data", Array<JSONValue>>;
|
|
23
|
+
declare const errorStreamPart: StreamPart<"3", "error", string>;
|
|
24
|
+
declare const assistantMessage: StreamPart<"4", "assistant_message", AssistantMessage>;
|
|
25
|
+
declare const assistantControlData: StreamPart<"5", "assistant_control_data", {
|
|
26
|
+
threadId: string;
|
|
27
|
+
messageId: string;
|
|
28
|
+
}>;
|
|
29
|
+
type StreamParts = typeof textStreamPart | typeof functionCallStreamPart | typeof dataStreamPart | typeof errorStreamPart | typeof assistantMessage | typeof assistantControlData;
|
|
30
|
+
/**
|
|
31
|
+
* Maps the type of a stream part to its value type.
|
|
32
|
+
*/
|
|
33
|
+
type StreamPartValueType = {
|
|
34
|
+
[P in StreamParts as P["name"]]: ReturnType<P["parse"]>["value"];
|
|
35
|
+
};
|
|
36
|
+
type StreamPartType = ReturnType<typeof textStreamPart.parse> | ReturnType<typeof functionCallStreamPart.parse> | ReturnType<typeof dataStreamPart.parse> | ReturnType<typeof errorStreamPart.parse> | ReturnType<typeof assistantMessage.parse> | ReturnType<typeof assistantControlData.parse>;
|
|
37
|
+
declare const streamPartsByCode: {
|
|
38
|
+
readonly 0: StreamPart<"0", "text", string>;
|
|
39
|
+
readonly 1: StreamPart<"1", "function_call", {
|
|
40
|
+
function_call: FunctionCall;
|
|
41
|
+
}>;
|
|
42
|
+
readonly 2: StreamPart<"2", "data", JSONValue[]>;
|
|
43
|
+
readonly 3: StreamPart<"3", "error", string>;
|
|
44
|
+
readonly 4: StreamPart<"4", "assistant_message", AssistantMessage>;
|
|
45
|
+
readonly 5: StreamPart<"5", "assistant_control_data", {
|
|
46
|
+
threadId: string;
|
|
47
|
+
messageId: string;
|
|
48
|
+
}>;
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* The map of prefixes for data in the stream
|
|
52
|
+
*
|
|
53
|
+
* - 0: Text from the LLM response
|
|
54
|
+
* - 1: (OpenAI) function_call responses
|
|
55
|
+
* - 2: custom JSON added by the user using `Data`
|
|
56
|
+
*
|
|
57
|
+
* Example:
|
|
58
|
+
* ```
|
|
59
|
+
* 0:Vercel
|
|
60
|
+
* 0:'s
|
|
61
|
+
* 0: AI
|
|
62
|
+
* 0: AI
|
|
63
|
+
* 0: SDK
|
|
64
|
+
* 0: is great
|
|
65
|
+
* 0:!
|
|
66
|
+
* 2: { "someJson": "value" }
|
|
67
|
+
* 1: {"function_call": {"name": "get_current_weather", "arguments": "{\\n\\"location\\": \\"Charlottesville, Virginia\\",\\n\\"format\\": \\"celsius\\"\\n}"}}
|
|
68
|
+
*```
|
|
69
|
+
*/
|
|
70
|
+
declare const StreamStringPrefixes: {
|
|
71
|
+
readonly text: "0";
|
|
72
|
+
readonly function_call: "1";
|
|
73
|
+
readonly data: "2";
|
|
74
|
+
readonly error: "3";
|
|
75
|
+
readonly assistant_message: "4";
|
|
76
|
+
readonly assistant_control_data: "5";
|
|
77
|
+
};
|
|
78
|
+
declare const validCodes: ("0" | "1" | "2" | "3" | "4" | "5")[];
|
|
79
|
+
/**
|
|
80
|
+
* Parses a stream part from a string.
|
|
81
|
+
*
|
|
82
|
+
* @param line The string to parse.
|
|
83
|
+
* @returns The parsed stream part.
|
|
84
|
+
* @throws An error if the string cannot be parsed.
|
|
85
|
+
*/
|
|
86
|
+
declare const parseStreamPart: (line: string) => StreamPartType;
|
|
87
|
+
/**
|
|
88
|
+
* Prepends a string with a prefix from the `StreamChunkPrefixes`, JSON-ifies it,
|
|
89
|
+
* and appends a new line.
|
|
90
|
+
*
|
|
91
|
+
* It ensures type-safety for the part type and value.
|
|
92
|
+
*/
|
|
93
|
+
declare function formatStreamPart<T extends keyof StreamPartValueType>(type: T, value: StreamPartValueType[T]): StreamString;
|
|
94
|
+
declare const isStreamStringEqualToType: (type: keyof typeof StreamStringPrefixes, value: string) => value is `0:${string}\n` | `1:${string}\n` | `2:${string}\n` | `3:${string}\n` | `4:${string}\n` | `5:${string}\n`;
|
|
95
|
+
type StreamString = `${typeof StreamStringPrefixes[keyof typeof StreamStringPrefixes]}:${string}\n`;
|
|
96
|
+
/**
|
|
97
|
+
* 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)
|
|
98
|
+
*/
|
|
99
|
+
declare const COMPLEX_HEADER = "X-Experimental-Stream-Data";
|
|
100
|
+
|
|
101
|
+
export { COMPLEX_HEADER, StreamPart, StreamPartType, StreamString, StreamStringPrefixes, formatStreamPart, isStreamStringEqualToType, parseStreamPart, streamPartsByCode, validCodes };
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
// src/utils/utils.ts
|
|
2
|
+
var textStreamPart = {
|
|
3
|
+
code: "0",
|
|
4
|
+
name: "text",
|
|
5
|
+
parse: (value) => {
|
|
6
|
+
if (typeof value !== "string") {
|
|
7
|
+
throw new Error('"text" parts expect a string value.');
|
|
8
|
+
}
|
|
9
|
+
return { type: "text", value };
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
var functionCallStreamPart = {
|
|
13
|
+
code: "1",
|
|
14
|
+
name: "function_call",
|
|
15
|
+
parse: (value) => {
|
|
16
|
+
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") {
|
|
17
|
+
throw new Error('"function_call" parts expect an object with a "function_call" property.');
|
|
18
|
+
}
|
|
19
|
+
return {
|
|
20
|
+
type: "function_call",
|
|
21
|
+
value
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
var dataStreamPart = {
|
|
26
|
+
code: "2",
|
|
27
|
+
name: "data",
|
|
28
|
+
parse: (value) => {
|
|
29
|
+
if (!Array.isArray(value)) {
|
|
30
|
+
throw new Error('"data" parts expect an array value.');
|
|
31
|
+
}
|
|
32
|
+
return { type: "data", value };
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
var errorStreamPart = {
|
|
36
|
+
code: "3",
|
|
37
|
+
name: "error",
|
|
38
|
+
parse: (value) => {
|
|
39
|
+
if (typeof value !== "string") {
|
|
40
|
+
throw new Error('"error" parts expect a string value.');
|
|
41
|
+
}
|
|
42
|
+
return { type: "error", value };
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
var assistantMessage = {
|
|
46
|
+
code: "4",
|
|
47
|
+
name: "assistant_message",
|
|
48
|
+
parse: (value) => {
|
|
49
|
+
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(
|
|
50
|
+
(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"
|
|
51
|
+
)) {
|
|
52
|
+
throw new Error(
|
|
53
|
+
'"assistant_message" parts expect an object with an "id", "role", and "content" property.'
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
return {
|
|
57
|
+
type: "assistant_message",
|
|
58
|
+
value
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
var assistantControlData = {
|
|
63
|
+
code: "5",
|
|
64
|
+
name: "assistant_control_data",
|
|
65
|
+
parse: (value) => {
|
|
66
|
+
if (value == null || typeof value !== "object" || !("threadId" in value) || !("messageId" in value) || typeof value.threadId !== "string" || typeof value.messageId !== "string") {
|
|
67
|
+
throw new Error(
|
|
68
|
+
'"assistant_control_data" parts expect an object with a "threadId" and "messageId" property.'
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
return {
|
|
72
|
+
type: "assistant_control_data",
|
|
73
|
+
value: {
|
|
74
|
+
threadId: value.threadId,
|
|
75
|
+
messageId: value.messageId
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
var streamParts = [
|
|
81
|
+
textStreamPart,
|
|
82
|
+
functionCallStreamPart,
|
|
83
|
+
dataStreamPart,
|
|
84
|
+
errorStreamPart,
|
|
85
|
+
assistantMessage,
|
|
86
|
+
assistantControlData
|
|
87
|
+
];
|
|
88
|
+
var streamPartsByCode = {
|
|
89
|
+
[textStreamPart.code]: textStreamPart,
|
|
90
|
+
[functionCallStreamPart.code]: functionCallStreamPart,
|
|
91
|
+
[dataStreamPart.code]: dataStreamPart,
|
|
92
|
+
[errorStreamPart.code]: errorStreamPart,
|
|
93
|
+
[assistantMessage.code]: assistantMessage,
|
|
94
|
+
[assistantControlData.code]: assistantControlData
|
|
95
|
+
};
|
|
96
|
+
var StreamStringPrefixes = {
|
|
97
|
+
[textStreamPart.name]: textStreamPart.code,
|
|
98
|
+
[functionCallStreamPart.name]: functionCallStreamPart.code,
|
|
99
|
+
[dataStreamPart.name]: dataStreamPart.code,
|
|
100
|
+
[errorStreamPart.name]: errorStreamPart.code,
|
|
101
|
+
[assistantMessage.name]: assistantMessage.code,
|
|
102
|
+
[assistantControlData.name]: assistantControlData.code
|
|
103
|
+
};
|
|
104
|
+
var validCodes = streamParts.map((part) => part.code);
|
|
105
|
+
var parseStreamPart = (line) => {
|
|
106
|
+
const firstSeparatorIndex = line.indexOf(":");
|
|
107
|
+
if (firstSeparatorIndex === -1) {
|
|
108
|
+
throw new Error("Failed to parse stream string. No separator found.");
|
|
109
|
+
}
|
|
110
|
+
const prefix = line.slice(0, firstSeparatorIndex);
|
|
111
|
+
if (!validCodes.includes(prefix)) {
|
|
112
|
+
throw new Error(`Failed to parse stream string. Invalid code ${prefix}.`);
|
|
113
|
+
}
|
|
114
|
+
const code = prefix;
|
|
115
|
+
const textValue = line.slice(firstSeparatorIndex + 1);
|
|
116
|
+
const jsonValue = JSON.parse(textValue);
|
|
117
|
+
return streamPartsByCode[code].parse(jsonValue);
|
|
118
|
+
};
|
|
119
|
+
function formatStreamPart(type, value) {
|
|
120
|
+
const streamPart = streamParts.find((part) => part.name === type);
|
|
121
|
+
if (!streamPart) {
|
|
122
|
+
throw new Error(`Invalid stream part type: ${type}`);
|
|
123
|
+
}
|
|
124
|
+
return `${streamPart.code}:${JSON.stringify(value)}
|
|
125
|
+
`;
|
|
126
|
+
}
|
|
127
|
+
var isStreamStringEqualToType = (type, value) => value.startsWith(`${StreamStringPrefixes[type]}:`) && value.endsWith("\n");
|
|
128
|
+
var COMPLEX_HEADER = "X-Experimental-Stream-Data";
|
|
129
|
+
export {
|
|
130
|
+
COMPLEX_HEADER,
|
|
131
|
+
StreamStringPrefixes,
|
|
132
|
+
formatStreamPart,
|
|
133
|
+
isStreamStringEqualToType,
|
|
134
|
+
parseStreamPart,
|
|
135
|
+
streamPartsByCode,
|
|
136
|
+
validCodes
|
|
137
|
+
};
|
|
138
|
+
//# sourceMappingURL=utils.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/utils/utils.ts"],"sourcesContent":["import { AssistantMessage, FunctionCall, JSONValue } from \"../types/openai-assistant\";\n\nexport interface StreamPart<CODE extends string, NAME extends string, TYPE> {\n code: CODE;\n name: NAME;\n parse: (value: JSONValue) => { type: NAME; value: TYPE };\n}\n\nconst textStreamPart: StreamPart<\"0\", \"text\", string> = {\n code: \"0\",\n name: \"text\",\n parse: (value: JSONValue) => {\n if (typeof value !== \"string\") {\n throw new Error('\"text\" parts expect a string value.');\n }\n return { type: \"text\", value };\n },\n};\n\n/**\n * This is a utility function that helps in parsing the stream parts.\n * It takes a JSONValue as input and returns an object with type and value.\n * The type is a string that represents the type of the stream part.\n * The value is the actual value of the stream part.\n * If the input value is not a string, it throws an error.\n */\nconst functionCallStreamPart: StreamPart<\"1\", \"function_call\", { function_call: FunctionCall }> = {\n code: \"1\",\n name: \"function_call\",\n parse: (value: JSONValue) => {\n if (\n value == null ||\n typeof value !== \"object\" ||\n !(\"function_call\" in value) ||\n typeof value.function_call !== \"object\" ||\n value.function_call == null ||\n !(\"name\" in value.function_call) ||\n !(\"arguments\" in value.function_call) ||\n typeof value.function_call.name !== \"string\" ||\n typeof value.function_call.arguments !== \"string\"\n ) {\n throw new Error('\"function_call\" parts expect an object with a \"function_call\" property.');\n }\n\n return {\n type: \"function_call\",\n value: value as unknown as { function_call: FunctionCall },\n };\n },\n};\n\nconst dataStreamPart: StreamPart<\"2\", \"data\", Array<JSONValue>> = {\n code: \"2\",\n name: \"data\",\n parse: (value: JSONValue) => {\n if (!Array.isArray(value)) {\n throw new Error('\"data\" parts expect an array value.');\n }\n\n return { type: \"data\", value };\n },\n};\n\nconst errorStreamPart: StreamPart<\"3\", \"error\", string> = {\n code: \"3\",\n name: \"error\",\n parse: (value: JSONValue) => {\n if (typeof value !== \"string\") {\n throw new Error('\"error\" parts expect a string value.');\n }\n return { type: \"error\", value };\n },\n};\n\nconst assistantMessage: StreamPart<\"4\", \"assistant_message\", AssistantMessage> = {\n code: \"4\",\n name: \"assistant_message\",\n parse: (value: JSONValue) => {\n if (\n value == null ||\n typeof value !== \"object\" ||\n !(\"id\" in value) ||\n !(\"role\" in value) ||\n !(\"content\" in value) ||\n typeof value.id !== \"string\" ||\n typeof value.role !== \"string\" ||\n value.role !== \"assistant\" ||\n !Array.isArray(value.content) ||\n !value.content.every(\n (item) =>\n item != null &&\n typeof item === \"object\" &&\n \"type\" in item &&\n item.type === \"text\" &&\n \"text\" in item &&\n item.text != null &&\n typeof item.text === \"object\" &&\n \"value\" in item.text &&\n typeof item.text.value === \"string\",\n )\n ) {\n throw new Error(\n '\"assistant_message\" parts expect an object with an \"id\", \"role\", and \"content\" property.',\n );\n }\n\n return {\n type: \"assistant_message\",\n value: value as AssistantMessage,\n };\n },\n};\n\nconst assistantControlData: StreamPart<\n \"5\",\n \"assistant_control_data\",\n {\n threadId: string;\n messageId: string;\n }\n> = {\n code: \"5\",\n name: \"assistant_control_data\",\n parse: (value: JSONValue) => {\n if (\n value == null ||\n typeof value !== \"object\" ||\n !(\"threadId\" in value) ||\n !(\"messageId\" in value) ||\n typeof value.threadId !== \"string\" ||\n typeof value.messageId !== \"string\"\n ) {\n throw new Error(\n '\"assistant_control_data\" parts expect an object with a \"threadId\" and \"messageId\" property.',\n );\n }\n\n return {\n type: \"assistant_control_data\",\n value: {\n threadId: value.threadId,\n messageId: value.messageId,\n },\n };\n },\n};\n\nconst streamParts = [\n textStreamPart,\n functionCallStreamPart,\n dataStreamPart,\n errorStreamPart,\n assistantMessage,\n assistantControlData,\n] as const;\n\n// union type of all stream parts\ntype StreamParts =\n | typeof textStreamPart\n | typeof functionCallStreamPart\n | typeof dataStreamPart\n | typeof errorStreamPart\n | typeof assistantMessage\n | typeof assistantControlData;\n\n/**\n * Maps the type of a stream part to its value type.\n */\ntype StreamPartValueType = {\n [P in StreamParts as P[\"name\"]]: ReturnType<P[\"parse\"]>[\"value\"];\n};\n\nexport type StreamPartType =\n | ReturnType<typeof textStreamPart.parse>\n | ReturnType<typeof functionCallStreamPart.parse>\n | ReturnType<typeof dataStreamPart.parse>\n | ReturnType<typeof errorStreamPart.parse>\n | ReturnType<typeof assistantMessage.parse>\n | ReturnType<typeof assistantControlData.parse>;\n\nexport const streamPartsByCode = {\n [textStreamPart.code]: textStreamPart,\n [functionCallStreamPart.code]: functionCallStreamPart,\n [dataStreamPart.code]: dataStreamPart,\n [errorStreamPart.code]: errorStreamPart,\n [assistantMessage.code]: assistantMessage,\n [assistantControlData.code]: assistantControlData,\n} as const;\n\n/**\n * The map of prefixes for data in the stream\n *\n * - 0: Text from the LLM response\n * - 1: (OpenAI) function_call responses\n * - 2: custom JSON added by the user using `Data`\n *\n * Example:\n * ```\n * 0:Vercel\n * 0:'s\n * 0: AI\n * 0: AI\n * 0: SDK\n * 0: is great\n * 0:!\n * 2: { \"someJson\": \"value\" }\n * 1: {\"function_call\": {\"name\": \"get_current_weather\", \"arguments\": \"{\\\\n\\\\\"location\\\\\": \\\\\"Charlottesville, Virginia\\\\\",\\\\n\\\\\"format\\\\\": \\\\\"celsius\\\\\"\\\\n}\"}}\n *```\n */\nexport const StreamStringPrefixes = {\n [textStreamPart.name]: textStreamPart.code,\n [functionCallStreamPart.name]: functionCallStreamPart.code,\n [dataStreamPart.name]: dataStreamPart.code,\n [errorStreamPart.name]: errorStreamPart.code,\n [assistantMessage.name]: assistantMessage.code,\n [assistantControlData.name]: assistantControlData.code,\n} as const;\n\nexport const validCodes = streamParts.map((part) => part.code);\n\n/**\n * Parses a stream part from a string.\n *\n * @param line The string to parse.\n * @returns The parsed stream part.\n * @throws An error if the string cannot be parsed.\n */\nexport const parseStreamPart = (line: string): StreamPartType => {\n const firstSeparatorIndex = line.indexOf(\":\");\n\n if (firstSeparatorIndex === -1) {\n throw new Error(\"Failed to parse stream string. No separator found.\");\n }\n\n const prefix = line.slice(0, firstSeparatorIndex);\n\n if (!validCodes.includes(prefix as keyof typeof streamPartsByCode)) {\n throw new Error(`Failed to parse stream string. Invalid code ${prefix}.`);\n }\n\n const code = prefix as keyof typeof streamPartsByCode;\n\n const textValue = line.slice(firstSeparatorIndex + 1);\n const jsonValue: JSONValue = JSON.parse(textValue);\n\n return streamPartsByCode[code].parse(jsonValue);\n};\n\n/**\n * Prepends a string with a prefix from the `StreamChunkPrefixes`, JSON-ifies it,\n * and appends a new line.\n *\n * It ensures type-safety for the part type and value.\n */\nexport function formatStreamPart<T extends keyof StreamPartValueType>(\n type: T,\n value: StreamPartValueType[T],\n): StreamString {\n const streamPart = streamParts.find((part) => part.name === type);\n\n if (!streamPart) {\n throw new Error(`Invalid stream part type: ${type}`);\n }\n\n return `${streamPart.code}:${JSON.stringify(value)}\\n`;\n}\n\nexport const isStreamStringEqualToType = (\n type: keyof typeof StreamStringPrefixes,\n value: string,\n): value is StreamString =>\n value.startsWith(`${StreamStringPrefixes[type]}:`) && value.endsWith(\"\\n\");\n\nexport type StreamString =\n `${typeof StreamStringPrefixes[keyof typeof StreamStringPrefixes]}:${string}\\n`;\n\n/**\n * 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)\n */\nexport const COMPLEX_HEADER = \"X-Experimental-Stream-Data\";\n"],"mappings":";AAQA,IAAM,iBAAkD;AAAA,EACtD,MAAM;AAAA,EACN,MAAM;AAAA,EACN,OAAO,CAAC,UAAqB;AAC3B,QAAI,OAAO,UAAU,UAAU;AAC7B,YAAM,IAAI,MAAM,qCAAqC;AAAA,IACvD;AACA,WAAO,EAAE,MAAM,QAAQ,MAAM;AAAA,EAC/B;AACF;AASA,IAAM,yBAA4F;AAAA,EAChG,MAAM;AAAA,EACN,MAAM;AAAA,EACN,OAAO,CAAC,UAAqB;AAC3B,QACE,SAAS,QACT,OAAO,UAAU,YACjB,EAAE,mBAAmB,UACrB,OAAO,MAAM,kBAAkB,YAC/B,MAAM,iBAAiB,QACvB,EAAE,UAAU,MAAM,kBAClB,EAAE,eAAe,MAAM,kBACvB,OAAO,MAAM,cAAc,SAAS,YACpC,OAAO,MAAM,cAAc,cAAc,UACzC;AACA,YAAM,IAAI,MAAM,yEAAyE;AAAA,IAC3F;AAEA,WAAO;AAAA,MACL,MAAM;AAAA,MACN;AAAA,IACF;AAAA,EACF;AACF;AAEA,IAAM,iBAA4D;AAAA,EAChE,MAAM;AAAA,EACN,MAAM;AAAA,EACN,OAAO,CAAC,UAAqB;AAC3B,QAAI,CAA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AE,MAAM,SAAS;AAChD;AAQO,SAAS,iBACd,MACA,OACc;AACd,QAAM,aAAa,YAAY,KAAK,CAAC,SAAS,KAAK,SAAS,IAAI;AAEhE,MAAI,CAAC,YAAY;AACf,UAAM,IAAI,MAAM,6BAA6B,MAAM;AAAA,EACrD;AAEA,SAAO,GAAG,WAAW,QAAQ,KAAK,UAAU,KAAK;AAAA;AACnD;AAEO,IAAM,4BAA4B,CACvC,MACA,UAEA,MAAM,WAAW,GAAG,qBAAqB,IAAI,IAAI,KAAK,MAAM,SAAS,IAAI;AAQpE,IAAM,iBAAiB;","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
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":[]}
|
package/jest.config.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@copilotkit/shared",
|
|
3
|
+
"private": false,
|
|
4
|
+
"publishConfig": {
|
|
5
|
+
"access": "public"
|
|
6
|
+
},
|
|
7
|
+
"version": "0.1.0-alpha.0",
|
|
8
|
+
"sideEffects": false,
|
|
9
|
+
"main": "./dist/index.js",
|
|
10
|
+
"module": "./dist/index.mjs",
|
|
11
|
+
"exports": "./dist/index.mjs",
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"peerDependencies": {},
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"@types/jest": "^29.5.4",
|
|
17
|
+
"eslint": "^7.32.0",
|
|
18
|
+
"jest": "^29.6.4",
|
|
19
|
+
"ts-jest": "^29.1.1",
|
|
20
|
+
"tsup": "^6.7.0",
|
|
21
|
+
"typescript": "^5.1.3",
|
|
22
|
+
"tsconfig": "0.6.0-alpha.0",
|
|
23
|
+
"eslint-config-custom": "0.2.0"
|
|
24
|
+
},
|
|
25
|
+
"dependencies": {},
|
|
26
|
+
"scripts": {
|
|
27
|
+
"build": "tsup --treeshake",
|
|
28
|
+
"dev": "tsup --watch --no-splitting",
|
|
29
|
+
"test": "jest",
|
|
30
|
+
"check-types": "tsc --noEmit",
|
|
31
|
+
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist && rm -rf .next"
|
|
32
|
+
}
|
|
33
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./openai-assistant";
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
export interface FunctionCall {
|
|
2
|
+
/**
|
|
3
|
+
* The arguments to call the function with, as generated by the model in JSON
|
|
4
|
+
* format. Note that the model does not always generate valid JSON, and may
|
|
5
|
+
* hallucinate parameters not defined by your function schema. Validate the
|
|
6
|
+
* arguments in your code before calling your function.
|
|
7
|
+
*/
|
|
8
|
+
arguments?: string;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The name of the function to call.
|
|
12
|
+
*/
|
|
13
|
+
name?: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Shared types between the API and UI packages.
|
|
18
|
+
*/
|
|
19
|
+
export interface Message {
|
|
20
|
+
id: string;
|
|
21
|
+
createdAt?: Date;
|
|
22
|
+
content: string;
|
|
23
|
+
ui?: string | null | undefined;
|
|
24
|
+
role: "system" | "user" | "assistant" | "function";
|
|
25
|
+
/**
|
|
26
|
+
* If the message has a role of `function`, the `name` field is the name of the function.
|
|
27
|
+
* Otherwise, the name field should not be set.
|
|
28
|
+
*/
|
|
29
|
+
name?: string;
|
|
30
|
+
/**
|
|
31
|
+
* If the assistant role makes a function call, the `function_call` field
|
|
32
|
+
* contains the function call name and arguments. Otherwise, the field should
|
|
33
|
+
* not be set.
|
|
34
|
+
*/
|
|
35
|
+
function_call?: string | FunctionCall;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export type AssistantMessage = {
|
|
39
|
+
id: string;
|
|
40
|
+
role: "assistant";
|
|
41
|
+
content: Array<{
|
|
42
|
+
type: "text";
|
|
43
|
+
text: {
|
|
44
|
+
value: string;
|
|
45
|
+
};
|
|
46
|
+
}>;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export type JSONValue =
|
|
50
|
+
| null
|
|
51
|
+
| string
|
|
52
|
+
| number
|
|
53
|
+
| boolean
|
|
54
|
+
| { [x: string]: JSONValue }
|
|
55
|
+
| Array<JSONValue>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./utils";
|
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
import { AssistantMessage, FunctionCall, JSONValue } from "../types/openai-assistant";
|
|
2
|
+
|
|
3
|
+
export interface StreamPart<CODE extends string, NAME extends string, TYPE> {
|
|
4
|
+
code: CODE;
|
|
5
|
+
name: NAME;
|
|
6
|
+
parse: (value: JSONValue) => { type: NAME; value: TYPE };
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const textStreamPart: StreamPart<"0", "text", string> = {
|
|
10
|
+
code: "0",
|
|
11
|
+
name: "text",
|
|
12
|
+
parse: (value: JSONValue) => {
|
|
13
|
+
if (typeof value !== "string") {
|
|
14
|
+
throw new Error('"text" parts expect a string value.');
|
|
15
|
+
}
|
|
16
|
+
return { type: "text", value };
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* This is a utility function that helps in parsing the stream parts.
|
|
22
|
+
* It takes a JSONValue as input and returns an object with type and value.
|
|
23
|
+
* The type is a string that represents the type of the stream part.
|
|
24
|
+
* The value is the actual value of the stream part.
|
|
25
|
+
* If the input value is not a string, it throws an error.
|
|
26
|
+
*/
|
|
27
|
+
const functionCallStreamPart: StreamPart<"1", "function_call", { function_call: FunctionCall }> = {
|
|
28
|
+
code: "1",
|
|
29
|
+
name: "function_call",
|
|
30
|
+
parse: (value: JSONValue) => {
|
|
31
|
+
if (
|
|
32
|
+
value == null ||
|
|
33
|
+
typeof value !== "object" ||
|
|
34
|
+
!("function_call" in value) ||
|
|
35
|
+
typeof value.function_call !== "object" ||
|
|
36
|
+
value.function_call == null ||
|
|
37
|
+
!("name" in value.function_call) ||
|
|
38
|
+
!("arguments" in value.function_call) ||
|
|
39
|
+
typeof value.function_call.name !== "string" ||
|
|
40
|
+
typeof value.function_call.arguments !== "string"
|
|
41
|
+
) {
|
|
42
|
+
throw new Error('"function_call" parts expect an object with a "function_call" property.');
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return {
|
|
46
|
+
type: "function_call",
|
|
47
|
+
value: value as unknown as { function_call: FunctionCall },
|
|
48
|
+
};
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const dataStreamPart: StreamPart<"2", "data", Array<JSONValue>> = {
|
|
53
|
+
code: "2",
|
|
54
|
+
name: "data",
|
|
55
|
+
parse: (value: JSONValue) => {
|
|
56
|
+
if (!Array.isArray(value)) {
|
|
57
|
+
throw new Error('"data" parts expect an array value.');
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return { type: "data", value };
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const errorStreamPart: StreamPart<"3", "error", string> = {
|
|
65
|
+
code: "3",
|
|
66
|
+
name: "error",
|
|
67
|
+
parse: (value: JSONValue) => {
|
|
68
|
+
if (typeof value !== "string") {
|
|
69
|
+
throw new Error('"error" parts expect a string value.');
|
|
70
|
+
}
|
|
71
|
+
return { type: "error", value };
|
|
72
|
+
},
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
const assistantMessage: StreamPart<"4", "assistant_message", AssistantMessage> = {
|
|
76
|
+
code: "4",
|
|
77
|
+
name: "assistant_message",
|
|
78
|
+
parse: (value: JSONValue) => {
|
|
79
|
+
if (
|
|
80
|
+
value == null ||
|
|
81
|
+
typeof value !== "object" ||
|
|
82
|
+
!("id" in value) ||
|
|
83
|
+
!("role" in value) ||
|
|
84
|
+
!("content" in value) ||
|
|
85
|
+
typeof value.id !== "string" ||
|
|
86
|
+
typeof value.role !== "string" ||
|
|
87
|
+
value.role !== "assistant" ||
|
|
88
|
+
!Array.isArray(value.content) ||
|
|
89
|
+
!value.content.every(
|
|
90
|
+
(item) =>
|
|
91
|
+
item != null &&
|
|
92
|
+
typeof item === "object" &&
|
|
93
|
+
"type" in item &&
|
|
94
|
+
item.type === "text" &&
|
|
95
|
+
"text" in item &&
|
|
96
|
+
item.text != null &&
|
|
97
|
+
typeof item.text === "object" &&
|
|
98
|
+
"value" in item.text &&
|
|
99
|
+
typeof item.text.value === "string",
|
|
100
|
+
)
|
|
101
|
+
) {
|
|
102
|
+
throw new Error(
|
|
103
|
+
'"assistant_message" parts expect an object with an "id", "role", and "content" property.',
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return {
|
|
108
|
+
type: "assistant_message",
|
|
109
|
+
value: value as AssistantMessage,
|
|
110
|
+
};
|
|
111
|
+
},
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
const assistantControlData: StreamPart<
|
|
115
|
+
"5",
|
|
116
|
+
"assistant_control_data",
|
|
117
|
+
{
|
|
118
|
+
threadId: string;
|
|
119
|
+
messageId: string;
|
|
120
|
+
}
|
|
121
|
+
> = {
|
|
122
|
+
code: "5",
|
|
123
|
+
name: "assistant_control_data",
|
|
124
|
+
parse: (value: JSONValue) => {
|
|
125
|
+
if (
|
|
126
|
+
value == null ||
|
|
127
|
+
typeof value !== "object" ||
|
|
128
|
+
!("threadId" in value) ||
|
|
129
|
+
!("messageId" in value) ||
|
|
130
|
+
typeof value.threadId !== "string" ||
|
|
131
|
+
typeof value.messageId !== "string"
|
|
132
|
+
) {
|
|
133
|
+
throw new Error(
|
|
134
|
+
'"assistant_control_data" parts expect an object with a "threadId" and "messageId" property.',
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return {
|
|
139
|
+
type: "assistant_control_data",
|
|
140
|
+
value: {
|
|
141
|
+
threadId: value.threadId,
|
|
142
|
+
messageId: value.messageId,
|
|
143
|
+
},
|
|
144
|
+
};
|
|
145
|
+
},
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
const streamParts = [
|
|
149
|
+
textStreamPart,
|
|
150
|
+
functionCallStreamPart,
|
|
151
|
+
dataStreamPart,
|
|
152
|
+
errorStreamPart,
|
|
153
|
+
assistantMessage,
|
|
154
|
+
assistantControlData,
|
|
155
|
+
] as const;
|
|
156
|
+
|
|
157
|
+
// union type of all stream parts
|
|
158
|
+
type StreamParts =
|
|
159
|
+
| typeof textStreamPart
|
|
160
|
+
| typeof functionCallStreamPart
|
|
161
|
+
| typeof dataStreamPart
|
|
162
|
+
| typeof errorStreamPart
|
|
163
|
+
| typeof assistantMessage
|
|
164
|
+
| typeof assistantControlData;
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Maps the type of a stream part to its value type.
|
|
168
|
+
*/
|
|
169
|
+
type StreamPartValueType = {
|
|
170
|
+
[P in StreamParts as P["name"]]: ReturnType<P["parse"]>["value"];
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
export type StreamPartType =
|
|
174
|
+
| ReturnType<typeof textStreamPart.parse>
|
|
175
|
+
| ReturnType<typeof functionCallStreamPart.parse>
|
|
176
|
+
| ReturnType<typeof dataStreamPart.parse>
|
|
177
|
+
| ReturnType<typeof errorStreamPart.parse>
|
|
178
|
+
| ReturnType<typeof assistantMessage.parse>
|
|
179
|
+
| ReturnType<typeof assistantControlData.parse>;
|
|
180
|
+
|
|
181
|
+
export const streamPartsByCode = {
|
|
182
|
+
[textStreamPart.code]: textStreamPart,
|
|
183
|
+
[functionCallStreamPart.code]: functionCallStreamPart,
|
|
184
|
+
[dataStreamPart.code]: dataStreamPart,
|
|
185
|
+
[errorStreamPart.code]: errorStreamPart,
|
|
186
|
+
[assistantMessage.code]: assistantMessage,
|
|
187
|
+
[assistantControlData.code]: assistantControlData,
|
|
188
|
+
} as const;
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* The map of prefixes for data in the stream
|
|
192
|
+
*
|
|
193
|
+
* - 0: Text from the LLM response
|
|
194
|
+
* - 1: (OpenAI) function_call responses
|
|
195
|
+
* - 2: custom JSON added by the user using `Data`
|
|
196
|
+
*
|
|
197
|
+
* Example:
|
|
198
|
+
* ```
|
|
199
|
+
* 0:Vercel
|
|
200
|
+
* 0:'s
|
|
201
|
+
* 0: AI
|
|
202
|
+
* 0: AI
|
|
203
|
+
* 0: SDK
|
|
204
|
+
* 0: is great
|
|
205
|
+
* 0:!
|
|
206
|
+
* 2: { "someJson": "value" }
|
|
207
|
+
* 1: {"function_call": {"name": "get_current_weather", "arguments": "{\\n\\"location\\": \\"Charlottesville, Virginia\\",\\n\\"format\\": \\"celsius\\"\\n}"}}
|
|
208
|
+
*```
|
|
209
|
+
*/
|
|
210
|
+
export const StreamStringPrefixes = {
|
|
211
|
+
[textStreamPart.name]: textStreamPart.code,
|
|
212
|
+
[functionCallStreamPart.name]: functionCallStreamPart.code,
|
|
213
|
+
[dataStreamPart.name]: dataStreamPart.code,
|
|
214
|
+
[errorStreamPart.name]: errorStreamPart.code,
|
|
215
|
+
[assistantMessage.name]: assistantMessage.code,
|
|
216
|
+
[assistantControlData.name]: assistantControlData.code,
|
|
217
|
+
} as const;
|
|
218
|
+
|
|
219
|
+
export const validCodes = streamParts.map((part) => part.code);
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Parses a stream part from a string.
|
|
223
|
+
*
|
|
224
|
+
* @param line The string to parse.
|
|
225
|
+
* @returns The parsed stream part.
|
|
226
|
+
* @throws An error if the string cannot be parsed.
|
|
227
|
+
*/
|
|
228
|
+
export const parseStreamPart = (line: string): StreamPartType => {
|
|
229
|
+
const firstSeparatorIndex = line.indexOf(":");
|
|
230
|
+
|
|
231
|
+
if (firstSeparatorIndex === -1) {
|
|
232
|
+
throw new Error("Failed to parse stream string. No separator found.");
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
const prefix = line.slice(0, firstSeparatorIndex);
|
|
236
|
+
|
|
237
|
+
if (!validCodes.includes(prefix as keyof typeof streamPartsByCode)) {
|
|
238
|
+
throw new Error(`Failed to parse stream string. Invalid code ${prefix}.`);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
const code = prefix as keyof typeof streamPartsByCode;
|
|
242
|
+
|
|
243
|
+
const textValue = line.slice(firstSeparatorIndex + 1);
|
|
244
|
+
const jsonValue: JSONValue = JSON.parse(textValue);
|
|
245
|
+
|
|
246
|
+
return streamPartsByCode[code].parse(jsonValue);
|
|
247
|
+
};
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Prepends a string with a prefix from the `StreamChunkPrefixes`, JSON-ifies it,
|
|
251
|
+
* and appends a new line.
|
|
252
|
+
*
|
|
253
|
+
* It ensures type-safety for the part type and value.
|
|
254
|
+
*/
|
|
255
|
+
export function formatStreamPart<T extends keyof StreamPartValueType>(
|
|
256
|
+
type: T,
|
|
257
|
+
value: StreamPartValueType[T],
|
|
258
|
+
): StreamString {
|
|
259
|
+
const streamPart = streamParts.find((part) => part.name === type);
|
|
260
|
+
|
|
261
|
+
if (!streamPart) {
|
|
262
|
+
throw new Error(`Invalid stream part type: ${type}`);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
return `${streamPart.code}:${JSON.stringify(value)}\n`;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
export const isStreamStringEqualToType = (
|
|
269
|
+
type: keyof typeof StreamStringPrefixes,
|
|
270
|
+
value: string,
|
|
271
|
+
): value is StreamString =>
|
|
272
|
+
value.startsWith(`${StreamStringPrefixes[type]}:`) && value.endsWith("\n");
|
|
273
|
+
|
|
274
|
+
export type StreamString =
|
|
275
|
+
`${typeof StreamStringPrefixes[keyof typeof StreamStringPrefixes]}:${string}\n`;
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* 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)
|
|
279
|
+
*/
|
|
280
|
+
export const COMPLEX_HEADER = "X-Experimental-Stream-Data";
|
package/tsconfig.json
ADDED
package/tsup.config.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { defineConfig, Options } from "tsup";
|
|
2
|
+
|
|
3
|
+
export default defineConfig((options: Options) => ({
|
|
4
|
+
entry: ["src/**/*.{ts,tsx}"],
|
|
5
|
+
format: ["esm"],
|
|
6
|
+
dts: true,
|
|
7
|
+
minify: false,
|
|
8
|
+
clean: true,
|
|
9
|
+
external: [],
|
|
10
|
+
sourcemap: true,
|
|
11
|
+
exclude: [
|
|
12
|
+
"**/*.test.ts", // Exclude TypeScript test files
|
|
13
|
+
"**/*.test.tsx", // Exclude TypeScript React test files
|
|
14
|
+
"**/__tests__/*", // Exclude any files inside a __tests__ directory
|
|
15
|
+
],
|
|
16
|
+
...options,
|
|
17
|
+
}));
|