@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.
- package/.turbo/turbo-build.log +73 -106
- package/CHANGELOG.md +6 -0
- package/dist/{chunk-QVD5GLKQ.mjs → chunk-2VLE6D3W.mjs} +1 -1
- package/dist/chunk-2VLE6D3W.mjs.map +1 -0
- package/dist/chunk-CIPF7PMC.mjs +80 -0
- package/dist/chunk-CIPF7PMC.mjs.map +1 -0
- package/dist/chunk-CYDWEPFL.mjs +1 -0
- package/dist/constants/index.d.ts +0 -2
- package/dist/constants/index.js +2 -9
- package/dist/constants/index.js.map +1 -1
- package/dist/constants/index.mjs +2 -6
- package/dist/index.d.ts +2 -6
- package/dist/index.js +79 -371
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +11 -43
- package/dist/types/index.d.ts +1 -1
- package/dist/types/openai-assistant.d.ts +7 -46
- package/dist/types/openai-assistant.js.map +1 -1
- package/dist/utils/index.d.ts +1 -4
- package/dist/utils/index.js +80 -367
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/index.mjs +6 -34
- package/dist/utils/json-schema.d.ts +30 -0
- package/dist/utils/json-schema.js +104 -0
- package/dist/utils/json-schema.js.map +1 -0
- package/dist/utils/json-schema.mjs +7 -0
- package/package.json +4 -4
- package/src/constants/index.ts +0 -1
- package/src/types/openai-assistant.ts +7 -54
- package/src/utils/index.ts +1 -4
- package/src/utils/json-schema.ts +120 -0
- package/dist/chunk-3ZU7SB62.mjs +0 -7
- package/dist/chunk-3ZU7SB62.mjs.map +0 -1
- package/dist/chunk-4MTSDAP6.mjs +0 -32
- package/dist/chunk-4MTSDAP6.mjs.map +0 -1
- package/dist/chunk-CBF2BIA7.mjs +0 -116
- package/dist/chunk-CBF2BIA7.mjs.map +0 -1
- package/dist/chunk-DBW3BY7E.mjs +0 -157
- package/dist/chunk-DBW3BY7E.mjs.map +0 -1
- package/dist/chunk-HW4V75UQ.mjs +0 -60
- package/dist/chunk-HW4V75UQ.mjs.map +0 -1
- package/dist/chunk-QVD5GLKQ.mjs.map +0 -1
- package/dist/chunk-UAPRMZEY.mjs +0 -1
- package/dist/constants/copilot-protocol.d.ts +0 -3
- package/dist/constants/copilot-protocol.js +0 -31
- package/dist/constants/copilot-protocol.js.map +0 -1
- package/dist/constants/copilot-protocol.mjs +0 -7
- package/dist/utils/decode-chat-completion-as-text.d.ts +0 -7
- package/dist/utils/decode-chat-completion-as-text.js +0 -56
- package/dist/utils/decode-chat-completion-as-text.js.map +0 -1
- package/dist/utils/decode-chat-completion-as-text.mjs +0 -7
- package/dist/utils/decode-chat-completion-as-text.mjs.map +0 -1
- package/dist/utils/decode-chat-completion.d.ts +0 -27
- package/dist/utils/decode-chat-completion.js +0 -140
- package/dist/utils/decode-chat-completion.js.map +0 -1
- package/dist/utils/decode-chat-completion.mjs +0 -7
- package/dist/utils/decode-chat-completion.mjs.map +0 -1
- package/dist/utils/parse-chat-completion.d.ts +0 -30
- package/dist/utils/parse-chat-completion.js +0 -84
- package/dist/utils/parse-chat-completion.js.map +0 -1
- package/dist/utils/parse-chat-completion.mjs +0 -7
- package/dist/utils/parse-chat-completion.mjs.map +0 -1
- package/dist/utils/utils.d.ts +0 -103
- package/dist/utils/utils.js +0 -189
- package/dist/utils/utils.js.map +0 -1
- package/dist/utils/utils.mjs +0 -23
- package/dist/utils/utils.mjs.map +0 -1
- package/dist/utils/utils.test.d.ts +0 -2
- package/dist/utils/utils.test.js +0 -9
- package/dist/utils/utils.test.js.map +0 -1
- package/dist/utils/utils.test.mjs +0 -7
- package/dist/utils/utils.test.mjs.map +0 -1
- package/src/constants/copilot-protocol.ts +0 -1
- package/src/utils/decode-chat-completion-as-text.ts +0 -33
- package/src/utils/decode-chat-completion.ts +0 -169
- package/src/utils/parse-chat-completion.ts +0 -112
- package/src/utils/utils.test.ts +0 -7
- package/src/utils/utils.ts +0 -298
- /package/dist/{chunk-UAPRMZEY.mjs.map → chunk-CYDWEPFL.mjs.map} +0 -0
- /package/dist/{constants/copilot-protocol.mjs.map → utils/json-schema.mjs.map} +0 -0
|
@@ -1,140 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/utils/decode-chat-completion.ts
|
|
21
|
-
var decode_chat_completion_exports = {};
|
|
22
|
-
__export(decode_chat_completion_exports, {
|
|
23
|
-
decodeChatCompletion: () => decodeChatCompletion
|
|
24
|
-
});
|
|
25
|
-
module.exports = __toCommonJS(decode_chat_completion_exports);
|
|
26
|
-
function decodeChatCompletion(stream) {
|
|
27
|
-
const reader = stream.getReader();
|
|
28
|
-
let mode = null;
|
|
29
|
-
let functionCallName = "";
|
|
30
|
-
let functionCallArguments = "";
|
|
31
|
-
let functionCallScope = "client";
|
|
32
|
-
async function cleanup(controller) {
|
|
33
|
-
if (controller) {
|
|
34
|
-
try {
|
|
35
|
-
controller.close();
|
|
36
|
-
} catch (_) {
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
if (reader) {
|
|
40
|
-
try {
|
|
41
|
-
await reader.cancel();
|
|
42
|
-
} catch (_) {
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
return new ReadableStream({
|
|
47
|
-
async pull(controller) {
|
|
48
|
-
var _a, _b, _c, _d, _e, _f;
|
|
49
|
-
const flushFunctionCall = () => {
|
|
50
|
-
let args = null;
|
|
51
|
-
try {
|
|
52
|
-
args = JSON.parse(functionCallArguments);
|
|
53
|
-
} catch (error) {
|
|
54
|
-
cleanup(controller);
|
|
55
|
-
controller.error(error);
|
|
56
|
-
return false;
|
|
57
|
-
}
|
|
58
|
-
controller.enqueue({
|
|
59
|
-
type: "function",
|
|
60
|
-
name: functionCallName,
|
|
61
|
-
arguments: args,
|
|
62
|
-
scope: functionCallScope
|
|
63
|
-
});
|
|
64
|
-
mode = null;
|
|
65
|
-
functionCallName = "";
|
|
66
|
-
functionCallArguments = "";
|
|
67
|
-
return true;
|
|
68
|
-
};
|
|
69
|
-
while (true) {
|
|
70
|
-
try {
|
|
71
|
-
const { done, value } = await reader.read();
|
|
72
|
-
if (done) {
|
|
73
|
-
if ((mode == null ? void 0 : mode.type) === "function") {
|
|
74
|
-
flushFunctionCall();
|
|
75
|
-
}
|
|
76
|
-
await cleanup(controller);
|
|
77
|
-
return;
|
|
78
|
-
}
|
|
79
|
-
if ((mode == null ? void 0 : mode.type) === "function" && (!((_b = (_a = value.choices[0].delta.tool_calls) == null ? void 0 : _a[0]) == null ? void 0 : _b.function) || ((_d = (_c = value.choices[0].delta.tool_calls) == null ? void 0 : _c[0]) == null ? void 0 : _d.function.name))) {
|
|
80
|
-
if (!flushFunctionCall()) {
|
|
81
|
-
return;
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
const maybeFunctionCall = (_f = (_e = value.choices[0].delta.tool_calls) == null ? void 0 : _e[0]) == null ? void 0 : _f.function;
|
|
85
|
-
if (maybeFunctionCall) {
|
|
86
|
-
mode = { type: "function", function: maybeFunctionCall };
|
|
87
|
-
} else {
|
|
88
|
-
mode = { type: "message" };
|
|
89
|
-
}
|
|
90
|
-
if (mode.type === "message") {
|
|
91
|
-
if (value.choices[0].delta.role === "function") {
|
|
92
|
-
controller.enqueue({
|
|
93
|
-
type: "result",
|
|
94
|
-
content: value.choices[0].delta.content,
|
|
95
|
-
name: value.choices[0].delta.name
|
|
96
|
-
});
|
|
97
|
-
} else if (value.choices[0].delta.content) {
|
|
98
|
-
controller.enqueue({
|
|
99
|
-
type: "content",
|
|
100
|
-
content: value.choices[0].delta.content
|
|
101
|
-
});
|
|
102
|
-
}
|
|
103
|
-
continue;
|
|
104
|
-
} else if (mode.type === "function") {
|
|
105
|
-
const maybeFunctionCallName = mode.function.name;
|
|
106
|
-
if (maybeFunctionCallName) {
|
|
107
|
-
functionCallName = maybeFunctionCallName;
|
|
108
|
-
}
|
|
109
|
-
const maybeFunctionCallArguments = mode.function.arguments;
|
|
110
|
-
if (maybeFunctionCallArguments) {
|
|
111
|
-
functionCallArguments += maybeFunctionCallArguments;
|
|
112
|
-
}
|
|
113
|
-
const maybeFunctionCallScope = mode.function.scope;
|
|
114
|
-
if (maybeFunctionCallScope) {
|
|
115
|
-
functionCallScope = maybeFunctionCallScope;
|
|
116
|
-
}
|
|
117
|
-
controller.enqueue({
|
|
118
|
-
type: "partial",
|
|
119
|
-
name: functionCallName,
|
|
120
|
-
arguments: functionCallArguments
|
|
121
|
-
});
|
|
122
|
-
continue;
|
|
123
|
-
}
|
|
124
|
-
} catch (error) {
|
|
125
|
-
controller.error(error);
|
|
126
|
-
await cleanup(controller);
|
|
127
|
-
return;
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
},
|
|
131
|
-
cancel() {
|
|
132
|
-
reader.cancel();
|
|
133
|
-
}
|
|
134
|
-
});
|
|
135
|
-
}
|
|
136
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
137
|
-
0 && (module.exports = {
|
|
138
|
-
decodeChatCompletion
|
|
139
|
-
});
|
|
140
|
-
//# sourceMappingURL=decode-chat-completion.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/utils/decode-chat-completion.ts"],"sourcesContent":["import { ChatCompletionChunk, ToolCallFunctionCall } from \"./parse-chat-completion\";\n\nexport interface ChatCompletionContentEvent {\n type: \"content\";\n content: string;\n}\n\nexport interface ChatCompletionPartialEvent {\n type: \"partial\";\n name: string;\n arguments: string;\n}\n\nexport interface ChatCompletionFunctionEvent {\n type: \"function\";\n name: string;\n arguments: any;\n scope: \"client\" | \"server\";\n}\n\nexport interface ChatCompletionResultEvent {\n type: \"result\";\n content: string;\n name: string;\n}\n\nexport type ChatCompletionEvent =\n | ChatCompletionContentEvent\n | ChatCompletionPartialEvent\n | ChatCompletionFunctionEvent\n | ChatCompletionResultEvent;\n\nexport function decodeChatCompletion(\n stream: ReadableStream<ChatCompletionChunk>,\n): ReadableStream<ChatCompletionEvent> {\n const reader = stream.getReader();\n\n type Mode = { type: \"function\"; function: ToolCallFunctionCall } | { type: \"message\" };\n\n let mode: Mode | null = null;\n let functionCallName: string = \"\";\n let functionCallArguments: string = \"\";\n let functionCallScope: \"client\" | \"server\" = \"client\";\n\n async function cleanup(controller?: ReadableStreamDefaultController<any>) {\n if (controller) {\n try {\n controller.close();\n } catch (_) {}\n }\n if (reader) {\n try {\n await reader.cancel();\n } catch (_) {}\n }\n }\n\n return new ReadableStream<ChatCompletionEvent>({\n async pull(controller) {\n const flushFunctionCall = (): boolean => {\n let args: any = null;\n try {\n args = JSON.parse(functionCallArguments);\n } catch (error) {\n cleanup(controller);\n controller.error(error);\n return false;\n }\n controller.enqueue({\n type: \"function\",\n name: functionCallName,\n arguments: args,\n scope: functionCallScope,\n });\n\n mode = null;\n functionCallName = \"\";\n functionCallArguments = \"\";\n return true;\n };\n\n while (true) {\n try {\n const { done, value } = await reader.read();\n\n if (done) {\n if (mode?.type === \"function\") {\n flushFunctionCall();\n }\n await cleanup(controller);\n return;\n }\n\n // In case we are currently handling a function call but the next message is either\n // - not a function call\n // - or is another function call (indicated by the presence of 'name' field in the next function call object)\n // => flush the current function call.\n if (\n mode?.type === \"function\" &&\n (!value.choices[0].delta.tool_calls?.[0]?.function ||\n value.choices[0].delta.tool_calls?.[0]?.function.name)\n ) {\n if (!flushFunctionCall()) {\n return;\n }\n }\n\n const maybeFunctionCall = value.choices[0].delta.tool_calls?.[0]?.function;\n if (maybeFunctionCall) {\n mode = { type: \"function\", function: maybeFunctionCall };\n } else {\n mode = { type: \"message\" };\n }\n\n // if we get a message, emit the content and continue;\n if (mode.type === \"message\") {\n // if we got a result message, send a result event\n if (value.choices[0].delta.role === \"function\") {\n controller.enqueue({\n type: \"result\",\n content: value.choices[0].delta.content!,\n name: value.choices[0].delta.name!,\n });\n }\n // otherwise, send a content event\n else if (value.choices[0].delta.content) {\n controller.enqueue({\n type: \"content\",\n content: value.choices[0].delta.content,\n });\n }\n continue;\n }\n // if we get a function call, buffer the name and arguments, then emit a partial event.\n else if (mode.type === \"function\") {\n const maybeFunctionCallName = mode.function.name;\n if (maybeFunctionCallName) {\n functionCallName = maybeFunctionCallName;\n }\n\n const maybeFunctionCallArguments = mode.function.arguments;\n if (maybeFunctionCallArguments) {\n functionCallArguments += maybeFunctionCallArguments;\n }\n\n const maybeFunctionCallScope = mode.function.scope;\n if (maybeFunctionCallScope) {\n functionCallScope = maybeFunctionCallScope;\n }\n\n controller.enqueue({\n type: \"partial\",\n name: functionCallName,\n arguments: functionCallArguments,\n });\n continue;\n }\n } catch (error) {\n controller.error(error);\n await cleanup(controller);\n return;\n }\n }\n },\n cancel() {\n reader.cancel();\n },\n });\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAgCO,SAAS,qBACd,QACqC;AACrC,QAAM,SAAS,OAAO,UAAU;AAIhC,MAAI,OAAoB;AACxB,MAAI,mBAA2B;AAC/B,MAAI,wBAAgC;AACpC,MAAI,oBAA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,kBAAkB;AAAA,UACzD,OAAO;AACL,mBAAO,EAAE,MAAM,UAAU;AAAA,UAC3B;AAGA,cAAI,KAAK,SAAS,WAAW;AAE3B,gBAAI,MAAM,QAAQ,CAAC,EAAE,MAAM,SAAS,YAAY;AAC9C,yBAAW,QAAQ;AAAA,gBACjB,MAAM;AAAA,gBACN,SAAS,MAAM,QAAQ,CAAC,EAAE,MAAM;AAAA,gBAChC,MAAM,MAAM,QAAQ,CAAC,EAAE,MAAM;AAAA,cAC/B,CAAC;AAAA,YACH,WAES,MAAM,QAAQ,CAAC,EAAE,MAAM,SAAS;AACvC,yBAAW,QAAQ;AAAA,gBACjB,MAAM;AAAA,gBACN,SAAS,MAAM,QAAQ,CAAC,EAAE,MAAM;AAAA,cAClC,CAAC;AAAA,YACH;AACA;AAAA,UACF,WAES,KAAK,SAAS,YAAY;AACjC,kBAAM,wBAAwB,KAAK,SAAS;AAC5C,gBAAI,uBAAuB;AACzB,iCAAmB;AAAA,YACrB;AAEA,kBAAM,6BAA6B,KAAK,SAAS;AACjD,gBAAI,4BAA4B;AAC9B,uCAAyB;AAAA,YAC3B;AAEA,kBAAM,yBAAyB,KAAK,SAAS;AAC7C,gBAAI,wBAAwB;AAC1B,kCAAoB;AAAA,YACtB;AAEA,uBAAW,QAAQ;AAAA,cACjB,MAAM;AAAA,cACN,MAAM;AAAA,cACN,WAAW;AAAA,YACb,CAAC;AACD;AAAA,UACF;AAAA,QACF,SAAS,OAAP;AACA,qBAAW,MAAM,KAAK;AACtB,gBAAM,QAAQ,UAAU;AACxB;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,IACA,SAAS;AACP,aAAO,OAAO;AAAA,IAChB;AAAA,EACF,CAAC;AACH;","names":[]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { Role } from '../types/openai-assistant.js';
|
|
2
|
-
|
|
3
|
-
interface ToolCallFunctionCall {
|
|
4
|
-
arguments?: string;
|
|
5
|
-
name?: string;
|
|
6
|
-
scope?: "client" | "server";
|
|
7
|
-
}
|
|
8
|
-
interface ToolCallPayload {
|
|
9
|
-
index: number;
|
|
10
|
-
id?: string;
|
|
11
|
-
function: ToolCallFunctionCall;
|
|
12
|
-
}
|
|
13
|
-
interface ChatCompletionChunk {
|
|
14
|
-
choices: {
|
|
15
|
-
delta: {
|
|
16
|
-
id?: string;
|
|
17
|
-
role: Role;
|
|
18
|
-
content?: string | null;
|
|
19
|
-
name?: string;
|
|
20
|
-
function_call?: {
|
|
21
|
-
name?: string;
|
|
22
|
-
arguments?: string;
|
|
23
|
-
};
|
|
24
|
-
tool_calls?: ToolCallPayload[];
|
|
25
|
-
};
|
|
26
|
-
}[];
|
|
27
|
-
}
|
|
28
|
-
declare function parseChatCompletion(stream: ReadableStream<Uint8Array>): ReadableStream<ChatCompletionChunk>;
|
|
29
|
-
|
|
30
|
-
export { ChatCompletionChunk, ToolCallFunctionCall, ToolCallPayload, parseChatCompletion };
|
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/utils/parse-chat-completion.ts
|
|
21
|
-
var parse_chat_completion_exports = {};
|
|
22
|
-
__export(parse_chat_completion_exports, {
|
|
23
|
-
parseChatCompletion: () => parseChatCompletion
|
|
24
|
-
});
|
|
25
|
-
module.exports = __toCommonJS(parse_chat_completion_exports);
|
|
26
|
-
function parseChatCompletion(stream) {
|
|
27
|
-
const reader = stream.getReader();
|
|
28
|
-
let buffer = new Uint8Array();
|
|
29
|
-
async function cleanup(controller) {
|
|
30
|
-
if (controller) {
|
|
31
|
-
try {
|
|
32
|
-
controller.close();
|
|
33
|
-
} catch (_) {
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
if (reader) {
|
|
37
|
-
try {
|
|
38
|
-
await reader.cancel();
|
|
39
|
-
} catch (_) {
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
return new ReadableStream({
|
|
44
|
-
async pull(controller) {
|
|
45
|
-
while (true) {
|
|
46
|
-
try {
|
|
47
|
-
const { done, value } = await reader.read();
|
|
48
|
-
if (done) {
|
|
49
|
-
await cleanup(controller);
|
|
50
|
-
return;
|
|
51
|
-
}
|
|
52
|
-
const newBuffer = new Uint8Array(buffer.length + value.length);
|
|
53
|
-
newBuffer.set(buffer);
|
|
54
|
-
newBuffer.set(value, buffer.length);
|
|
55
|
-
buffer = newBuffer;
|
|
56
|
-
const valueString = new TextDecoder("utf-8").decode(buffer);
|
|
57
|
-
const lines = valueString.split("\n").filter((line) => line.trim() !== "");
|
|
58
|
-
buffer = !valueString.endsWith("\n") ? new TextEncoder().encode(lines.pop() || "") : new Uint8Array();
|
|
59
|
-
for (const line of lines) {
|
|
60
|
-
const cleanedLine = line.replace(/^data: /, "");
|
|
61
|
-
if (cleanedLine === "[DONE]") {
|
|
62
|
-
await cleanup(controller);
|
|
63
|
-
return;
|
|
64
|
-
}
|
|
65
|
-
const json = JSON.parse(cleanedLine);
|
|
66
|
-
controller.enqueue(json);
|
|
67
|
-
}
|
|
68
|
-
} catch (error) {
|
|
69
|
-
controller.error(error);
|
|
70
|
-
await cleanup(controller);
|
|
71
|
-
return;
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
},
|
|
75
|
-
cancel() {
|
|
76
|
-
reader.cancel();
|
|
77
|
-
}
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
81
|
-
0 && (module.exports = {
|
|
82
|
-
parseChatCompletion
|
|
83
|
-
});
|
|
84
|
-
//# sourceMappingURL=parse-chat-completion.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/utils/parse-chat-completion.ts"],"sourcesContent":["import { Role } from \"../types/openai-assistant\";\n\nexport interface ToolCallFunctionCall {\n arguments?: string;\n\n name?: string;\n // TODO:\n // Temporarily add scope to the OpenAI protocol until we\n // have our own protocol.\n // When scope is \"server\", the client will not attempt to\n // execute the function.\n scope?: \"client\" | \"server\";\n}\n\nexport interface ToolCallPayload {\n index: number;\n id?: string;\n function: ToolCallFunctionCall;\n}\n\nexport interface ChatCompletionChunk {\n choices: {\n delta: {\n id?: string;\n role: Role;\n content?: string | null;\n\n // TODO:\n // Temporarily add name to the OpenAI protocol until we\n // have our own protocol.\n // When name is set, we return the result of a server-side\n // function call.\n name?: string;\n\n function_call?: {\n name?: string;\n arguments?: string;\n };\n tool_calls?: ToolCallPayload[];\n };\n }[];\n}\n\n// TODO:\n// it's possible that unicode characters could be split across chunks\n// make sure to properly handle that\nexport function parseChatCompletion(\n stream: ReadableStream<Uint8Array>,\n): ReadableStream<ChatCompletionChunk> {\n const reader = stream.getReader();\n let buffer = new Uint8Array();\n\n async function cleanup(controller?: ReadableStreamDefaultController<any>) {\n if (controller) {\n try {\n controller.close();\n } catch (_) {}\n }\n if (reader) {\n try {\n await reader.cancel();\n } catch (_) {}\n }\n }\n\n return new ReadableStream<ChatCompletionChunk>({\n async pull(controller) {\n while (true) {\n try {\n const { done, value } = await reader.read();\n\n if (done) {\n await cleanup(controller);\n return;\n }\n\n const newBuffer = new Uint8Array(buffer.length + value.length);\n newBuffer.set(buffer);\n newBuffer.set(value, buffer.length);\n buffer = newBuffer;\n\n const valueString = new TextDecoder(\"utf-8\").decode(buffer);\n const lines = valueString.split(\"\\n\").filter((line) => line.trim() !== \"\");\n\n // If the last line isn't complete, keep it in the buffer for next time\n buffer = !valueString.endsWith(\"\\n\")\n ? new TextEncoder().encode(lines.pop() || \"\")\n : new Uint8Array();\n\n for (const line of lines) {\n const cleanedLine = line.replace(/^data: /, \"\");\n\n if (cleanedLine === \"[DONE]\") {\n await cleanup(controller);\n return;\n }\n\n const json = JSON.parse(cleanedLine);\n controller.enqueue(json);\n }\n } catch (error) {\n controller.error(error);\n await cleanup(controller);\n return;\n }\n }\n },\n cancel() {\n reader.cancel();\n },\n });\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AA8CO,SAAS,oBACd,QACqC;AACrC,QAAM,SAAS,OAAO,UAAU;AAChC,MAAI,SAAS,IAAI,WAAW;AAE5B,iBAAe,QAAQ,YAAmD;AACxE,QAAI,YAAY;AACd,UAAI;AACF,mBAAW,MAAM;AAAA,MACnB,SAAS,GAAP;AAAA,MAAW;AAAA,IACf;AACA,QAAI,QAAQ;AACV,UAAI;AACF,cAAM,OAAO,OAAO;AAAA,MACtB,SAAS,GAAP;AAAA,MAAW;AAAA,IACf;AAAA,EACF;AAEA,SAAO,IAAI,eAAoC;AAAA,IAC7C,MAAM,KAAK,YAAY;AACrB,aAAO,MAAM;AACX,YAAI;AACF,gBAAM,EAAE,MAAM,MAAM,IAAI,MAAM,OAAO,KAAK;AAE1C,cAAI,MAAM;AACR,kBAAM,QAAQ,UAAU;AACxB;AAAA,UACF;AAEA,gBAAM,YAAY,IAAI,WAAW,OAAO,SAAS,MAAM,MAAM;AAC7D,oBAAU,IAAI,MAAM;AACpB,oBAAU,IAAI,OAAO,OAAO,MAAM;AAClC,mBAAS;AAET,gBAAM,cAAc,IAAI,YAAY,OAAO,EAAE,OAAO,MAAM;AAC1D,gBAAM,QAAQ,YAAY,MAAM,IAAI,EAAE,OAAO,CAAC,SAAS,KAAK,KAAK,MAAM,EAAE;AAGzE,mBAAS,CAAC,YAAY,SAAS,IAAI,IAC/B,IAAI,YAAY,EAAE,OAAO,MAAM,IAAI,KAAK,EAAE,IAC1C,IAAI,WAAW;AAEnB,qBAAW,QAAQ,OAAO;AACxB,kBAAM,cAAc,KAAK,QAAQ,WAAW,EAAE;AAE9C,gBAAI,gBAAgB,UAAU;AAC5B,oBAAM,QAAQ,UAAU;AACxB;AAAA,YACF;AAEA,kBAAM,OAAO,KAAK,MAAM,WAAW;AACnC,uBAAW,QAAQ,IAAI;AAAA,UACzB;AAAA,QACF,SAAS,OAAP;AACA,qBAAW,MAAM,KAAK;AACtB,gBAAM,QAAQ,UAAU;AACxB;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,IACA,SAAS;AACP,aAAO,OAAO;AAAA,IAChB;AAAA,EACF,CAAC;AACH;","names":[]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
package/dist/utils/utils.d.ts
DELETED
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
import { JSONValue, FunctionCall, AssistantMessage } from '../types/openai-assistant.js';
|
|
2
|
-
|
|
3
|
-
declare function encodeResult(result: string): string;
|
|
4
|
-
declare function decodeResult(result: string): any;
|
|
5
|
-
interface StreamPart<CODE extends string, NAME extends string, TYPE> {
|
|
6
|
-
code: CODE;
|
|
7
|
-
name: NAME;
|
|
8
|
-
parse: (value: JSONValue) => {
|
|
9
|
-
type: NAME;
|
|
10
|
-
value: TYPE;
|
|
11
|
-
};
|
|
12
|
-
}
|
|
13
|
-
declare const textStreamPart: StreamPart<"0", "text", string>;
|
|
14
|
-
/**
|
|
15
|
-
* This is a utility function that helps in parsing the stream parts.
|
|
16
|
-
* It takes a JSONValue as input and returns an object with type and value.
|
|
17
|
-
* The type is a string that represents the type of the stream part.
|
|
18
|
-
* The value is the actual value of the stream part.
|
|
19
|
-
* If the input value is not a string, it throws an error.
|
|
20
|
-
*/
|
|
21
|
-
declare const functionCallStreamPart: StreamPart<"1", "function_call", {
|
|
22
|
-
function_call: FunctionCall;
|
|
23
|
-
}>;
|
|
24
|
-
declare const dataStreamPart: StreamPart<"2", "data", Array<JSONValue>>;
|
|
25
|
-
declare const errorStreamPart: StreamPart<"3", "error", string>;
|
|
26
|
-
declare const assistantMessage: StreamPart<"4", "assistant_message", AssistantMessage>;
|
|
27
|
-
declare const assistantControlData: StreamPart<"5", "assistant_control_data", {
|
|
28
|
-
threadId: string;
|
|
29
|
-
messageId: string;
|
|
30
|
-
}>;
|
|
31
|
-
type StreamParts = typeof textStreamPart | typeof functionCallStreamPart | typeof dataStreamPart | typeof errorStreamPart | typeof assistantMessage | typeof assistantControlData;
|
|
32
|
-
/**
|
|
33
|
-
* Maps the type of a stream part to its value type.
|
|
34
|
-
*/
|
|
35
|
-
type StreamPartValueType = {
|
|
36
|
-
[P in StreamParts as P["name"]]: ReturnType<P["parse"]>["value"];
|
|
37
|
-
};
|
|
38
|
-
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>;
|
|
39
|
-
declare const streamPartsByCode: {
|
|
40
|
-
readonly "0": StreamPart<"0", "text", string>;
|
|
41
|
-
readonly "1": StreamPart<"1", "function_call", {
|
|
42
|
-
function_call: FunctionCall;
|
|
43
|
-
}>;
|
|
44
|
-
readonly "2": StreamPart<"2", "data", JSONValue[]>;
|
|
45
|
-
readonly "3": StreamPart<"3", "error", string>;
|
|
46
|
-
readonly "4": StreamPart<"4", "assistant_message", AssistantMessage>;
|
|
47
|
-
readonly "5": StreamPart<"5", "assistant_control_data", {
|
|
48
|
-
threadId: string;
|
|
49
|
-
messageId: string;
|
|
50
|
-
}>;
|
|
51
|
-
};
|
|
52
|
-
/**
|
|
53
|
-
* The map of prefixes for data in the stream
|
|
54
|
-
*
|
|
55
|
-
* - 0: Text from the LLM response
|
|
56
|
-
* - 1: (OpenAI) function_call responses
|
|
57
|
-
* - 2: custom JSON added by the user using `Data`
|
|
58
|
-
*
|
|
59
|
-
* Example:
|
|
60
|
-
* ```
|
|
61
|
-
* 0:Vercel
|
|
62
|
-
* 0:'s
|
|
63
|
-
* 0: AI
|
|
64
|
-
* 0: AI
|
|
65
|
-
* 0: SDK
|
|
66
|
-
* 0: is great
|
|
67
|
-
* 0:!
|
|
68
|
-
* 2: { "someJson": "value" }
|
|
69
|
-
* 1: {"function_call": {"name": "get_current_weather", "arguments": "{\\n\\"location\\": \\"Charlottesville, Virginia\\",\\n\\"format\\": \\"celsius\\"\\n}"}}
|
|
70
|
-
*```
|
|
71
|
-
*/
|
|
72
|
-
declare const StreamStringPrefixes: {
|
|
73
|
-
readonly text: "0";
|
|
74
|
-
readonly function_call: "1";
|
|
75
|
-
readonly data: "2";
|
|
76
|
-
readonly error: "3";
|
|
77
|
-
readonly assistant_message: "4";
|
|
78
|
-
readonly assistant_control_data: "5";
|
|
79
|
-
};
|
|
80
|
-
declare const validCodes: ("0" | "1" | "2" | "3" | "4" | "5")[];
|
|
81
|
-
/**
|
|
82
|
-
* Parses a stream part from a string.
|
|
83
|
-
*
|
|
84
|
-
* @param line The string to parse.
|
|
85
|
-
* @returns The parsed stream part.
|
|
86
|
-
* @throws An error if the string cannot be parsed.
|
|
87
|
-
*/
|
|
88
|
-
declare const parseStreamPart: (line: string) => StreamPartType;
|
|
89
|
-
/**
|
|
90
|
-
* Prepends a string with a prefix from the `StreamChunkPrefixes`, JSON-ifies it,
|
|
91
|
-
* and appends a new line.
|
|
92
|
-
*
|
|
93
|
-
* It ensures type-safety for the part type and value.
|
|
94
|
-
*/
|
|
95
|
-
declare function formatStreamPart<T extends keyof StreamPartValueType>(type: T, value: StreamPartValueType[T]): StreamString;
|
|
96
|
-
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`;
|
|
97
|
-
type StreamString = `${(typeof StreamStringPrefixes)[keyof typeof StreamStringPrefixes]}:${string}\n`;
|
|
98
|
-
/**
|
|
99
|
-
* 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)
|
|
100
|
-
*/
|
|
101
|
-
declare const COMPLEX_HEADER = "X-Experimental-Stream-Data";
|
|
102
|
-
|
|
103
|
-
export { COMPLEX_HEADER, StreamPart, StreamPartType, StreamString, StreamStringPrefixes, decodeResult, encodeResult, formatStreamPart, isStreamStringEqualToType, parseStreamPart, streamPartsByCode, validCodes };
|
package/dist/utils/utils.js
DELETED
|
@@ -1,189 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/utils/utils.ts
|
|
21
|
-
var utils_exports = {};
|
|
22
|
-
__export(utils_exports, {
|
|
23
|
-
COMPLEX_HEADER: () => COMPLEX_HEADER,
|
|
24
|
-
StreamStringPrefixes: () => StreamStringPrefixes,
|
|
25
|
-
decodeResult: () => decodeResult,
|
|
26
|
-
encodeResult: () => encodeResult,
|
|
27
|
-
formatStreamPart: () => formatStreamPart,
|
|
28
|
-
isStreamStringEqualToType: () => isStreamStringEqualToType,
|
|
29
|
-
parseStreamPart: () => parseStreamPart,
|
|
30
|
-
streamPartsByCode: () => streamPartsByCode,
|
|
31
|
-
validCodes: () => validCodes
|
|
32
|
-
});
|
|
33
|
-
module.exports = __toCommonJS(utils_exports);
|
|
34
|
-
function encodeResult(result) {
|
|
35
|
-
if (result === void 0) {
|
|
36
|
-
return "";
|
|
37
|
-
} else if (typeof result === "string") {
|
|
38
|
-
return result;
|
|
39
|
-
} else {
|
|
40
|
-
return JSON.stringify(result);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
function decodeResult(result) {
|
|
44
|
-
try {
|
|
45
|
-
return JSON.parse(result);
|
|
46
|
-
} catch (e) {
|
|
47
|
-
return result;
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
var textStreamPart = {
|
|
51
|
-
code: "0",
|
|
52
|
-
name: "text",
|
|
53
|
-
parse: (value) => {
|
|
54
|
-
if (typeof value !== "string") {
|
|
55
|
-
throw new Error('"text" parts expect a string value.');
|
|
56
|
-
}
|
|
57
|
-
return { type: "text", value };
|
|
58
|
-
}
|
|
59
|
-
};
|
|
60
|
-
var functionCallStreamPart = {
|
|
61
|
-
code: "1",
|
|
62
|
-
name: "function_call",
|
|
63
|
-
parse: (value) => {
|
|
64
|
-
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") {
|
|
65
|
-
throw new Error('"function_call" parts expect an object with a "function_call" property.');
|
|
66
|
-
}
|
|
67
|
-
return {
|
|
68
|
-
type: "function_call",
|
|
69
|
-
value
|
|
70
|
-
};
|
|
71
|
-
}
|
|
72
|
-
};
|
|
73
|
-
var dataStreamPart = {
|
|
74
|
-
code: "2",
|
|
75
|
-
name: "data",
|
|
76
|
-
parse: (value) => {
|
|
77
|
-
if (!Array.isArray(value)) {
|
|
78
|
-
throw new Error('"data" parts expect an array value.');
|
|
79
|
-
}
|
|
80
|
-
return { type: "data", value };
|
|
81
|
-
}
|
|
82
|
-
};
|
|
83
|
-
var errorStreamPart = {
|
|
84
|
-
code: "3",
|
|
85
|
-
name: "error",
|
|
86
|
-
parse: (value) => {
|
|
87
|
-
if (typeof value !== "string") {
|
|
88
|
-
throw new Error('"error" parts expect a string value.');
|
|
89
|
-
}
|
|
90
|
-
return { type: "error", value };
|
|
91
|
-
}
|
|
92
|
-
};
|
|
93
|
-
var assistantMessage = {
|
|
94
|
-
code: "4",
|
|
95
|
-
name: "assistant_message",
|
|
96
|
-
parse: (value) => {
|
|
97
|
-
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(
|
|
98
|
-
(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"
|
|
99
|
-
)) {
|
|
100
|
-
throw new Error(
|
|
101
|
-
'"assistant_message" parts expect an object with an "id", "role", and "content" property.'
|
|
102
|
-
);
|
|
103
|
-
}
|
|
104
|
-
return {
|
|
105
|
-
type: "assistant_message",
|
|
106
|
-
value
|
|
107
|
-
};
|
|
108
|
-
}
|
|
109
|
-
};
|
|
110
|
-
var assistantControlData = {
|
|
111
|
-
code: "5",
|
|
112
|
-
name: "assistant_control_data",
|
|
113
|
-
parse: (value) => {
|
|
114
|
-
if (value == null || typeof value !== "object" || !("threadId" in value) || !("messageId" in value) || typeof value.threadId !== "string" || typeof value.messageId !== "string") {
|
|
115
|
-
throw new Error(
|
|
116
|
-
'"assistant_control_data" parts expect an object with a "threadId" and "messageId" property.'
|
|
117
|
-
);
|
|
118
|
-
}
|
|
119
|
-
return {
|
|
120
|
-
type: "assistant_control_data",
|
|
121
|
-
value: {
|
|
122
|
-
threadId: value.threadId,
|
|
123
|
-
messageId: value.messageId
|
|
124
|
-
}
|
|
125
|
-
};
|
|
126
|
-
}
|
|
127
|
-
};
|
|
128
|
-
var streamParts = [
|
|
129
|
-
textStreamPart,
|
|
130
|
-
functionCallStreamPart,
|
|
131
|
-
dataStreamPart,
|
|
132
|
-
errorStreamPart,
|
|
133
|
-
assistantMessage,
|
|
134
|
-
assistantControlData
|
|
135
|
-
];
|
|
136
|
-
var streamPartsByCode = {
|
|
137
|
-
[textStreamPart.code]: textStreamPart,
|
|
138
|
-
[functionCallStreamPart.code]: functionCallStreamPart,
|
|
139
|
-
[dataStreamPart.code]: dataStreamPart,
|
|
140
|
-
[errorStreamPart.code]: errorStreamPart,
|
|
141
|
-
[assistantMessage.code]: assistantMessage,
|
|
142
|
-
[assistantControlData.code]: assistantControlData
|
|
143
|
-
};
|
|
144
|
-
var StreamStringPrefixes = {
|
|
145
|
-
[textStreamPart.name]: textStreamPart.code,
|
|
146
|
-
[functionCallStreamPart.name]: functionCallStreamPart.code,
|
|
147
|
-
[dataStreamPart.name]: dataStreamPart.code,
|
|
148
|
-
[errorStreamPart.name]: errorStreamPart.code,
|
|
149
|
-
[assistantMessage.name]: assistantMessage.code,
|
|
150
|
-
[assistantControlData.name]: assistantControlData.code
|
|
151
|
-
};
|
|
152
|
-
var validCodes = streamParts.map((part) => part.code);
|
|
153
|
-
var parseStreamPart = (line) => {
|
|
154
|
-
const firstSeparatorIndex = line.indexOf(":");
|
|
155
|
-
if (firstSeparatorIndex === -1) {
|
|
156
|
-
throw new Error("Failed to parse stream string. No separator found.");
|
|
157
|
-
}
|
|
158
|
-
const prefix = line.slice(0, firstSeparatorIndex);
|
|
159
|
-
if (!validCodes.includes(prefix)) {
|
|
160
|
-
throw new Error(`Failed to parse stream string. Invalid code ${prefix}.`);
|
|
161
|
-
}
|
|
162
|
-
const code = prefix;
|
|
163
|
-
const textValue = line.slice(firstSeparatorIndex + 1);
|
|
164
|
-
const jsonValue = JSON.parse(textValue);
|
|
165
|
-
return streamPartsByCode[code].parse(jsonValue);
|
|
166
|
-
};
|
|
167
|
-
function formatStreamPart(type, value) {
|
|
168
|
-
const streamPart = streamParts.find((part) => part.name === type);
|
|
169
|
-
if (!streamPart) {
|
|
170
|
-
throw new Error(`Invalid stream part type: ${type}`);
|
|
171
|
-
}
|
|
172
|
-
return `${streamPart.code}:${JSON.stringify(value)}
|
|
173
|
-
`;
|
|
174
|
-
}
|
|
175
|
-
var isStreamStringEqualToType = (type, value) => value.startsWith(`${StreamStringPrefixes[type]}:`) && value.endsWith("\n");
|
|
176
|
-
var COMPLEX_HEADER = "X-Experimental-Stream-Data";
|
|
177
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
178
|
-
0 && (module.exports = {
|
|
179
|
-
COMPLEX_HEADER,
|
|
180
|
-
StreamStringPrefixes,
|
|
181
|
-
decodeResult,
|
|
182
|
-
encodeResult,
|
|
183
|
-
formatStreamPart,
|
|
184
|
-
isStreamStringEqualToType,
|
|
185
|
-
parseStreamPart,
|
|
186
|
-
streamPartsByCode,
|
|
187
|
-
validCodes
|
|
188
|
-
});
|
|
189
|
-
//# sourceMappingURL=utils.js.map
|
package/dist/utils/utils.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/utils/utils.ts"],"sourcesContent":["import { AssistantMessage, FunctionCall, JSONValue } from \"../types/openai-assistant\";\n\nexport function encodeResult(result: string): string {\n if (result === undefined) {\n return \"\";\n } else if (typeof result === \"string\") {\n return result;\n } else {\n return JSON.stringify(result);\n }\n}\n\nexport function decodeResult(result: string): any {\n try {\n return JSON.parse(result);\n } catch (e) {\n return result;\n }\n}\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":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAEO,SAAS,aAAa,QAAwB;AACnD,MAAI,WAAW,QAAW;AACxB,WAAO;AAAA,EACT,WAAW,OAAO,WAAW,UAAU;AACrC,WAAO;AAAA,EACT,OAAO;AACL,WAAO,KAAK,UAAU,MAAM;AAAA,EAC9B;AACF;AAEO,SAAS,aAAa,QAAqB;AAChD,MAAI;AACF,WAAO,KAAK,MAAM,MAAM;AAAA,EAC1B,SAAS,GAAP;AACA,WAAO;AAAA,EACT;AACF;AAQA,IAAM,iBAAkD;AAAA,EACtD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eResult,
|
|
6
|
-
formatStreamPart,
|
|
7
|
-
isStreamStringEqualToType,
|
|
8
|
-
parseStreamPart,
|
|
9
|
-
streamPartsByCode,
|
|
10
|
-
validCodes
|
|
11
|
-
} from "../chunk-DBW3BY7E.mjs";
|
|
12
|
-
export {
|
|
13
|
-
COMPLEX_HEADER,
|
|
14
|
-
StreamStringPrefixes,
|
|
15
|
-
decodeResult,
|
|
16
|
-
encodeResult,
|
|
17
|
-
formatStreamPart,
|
|
18
|
-
isStreamStringEqualToType,
|
|
19
|
-
parseStreamPart,
|
|
20
|
-
streamPartsByCode,
|
|
21
|
-
validCodes
|
|
22
|
-
};
|
|
23
|
-
//# sourceMappingURL=utils.mjs.map
|
package/dist/utils/utils.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|