@copilotkit/shared 0.8.0-mme-esm-error.2 → 0.9.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 +107 -107
- package/CHANGELOG.md +15 -0
- package/dist/{chunk-NKEDWRKJ.mjs → chunk-CBF2BIA7.mjs} +20 -12
- package/dist/chunk-CBF2BIA7.mjs.map +1 -0
- package/dist/{chunk-TYZD5BQY.mjs → chunk-HW4V75UQ.mjs} +1 -1
- package/dist/chunk-HW4V75UQ.mjs.map +1 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +19 -11
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +2 -2
- package/dist/utils/decode-chat-completion.js +19 -11
- package/dist/utils/decode-chat-completion.js.map +1 -1
- package/dist/utils/decode-chat-completion.mjs +1 -1
- package/dist/utils/index.d.ts +1 -1
- package/dist/utils/index.js +19 -11
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/index.mjs +2 -2
- package/dist/utils/parse-chat-completion.d.ts +12 -10
- package/dist/utils/parse-chat-completion.js.map +1 -1
- package/dist/utils/parse-chat-completion.mjs +1 -1
- package/package.json +3 -3
- package/src/utils/decode-chat-completion.ts +29 -16
- package/src/utils/parse-chat-completion.ts +19 -15
- package/dist/chunk-NKEDWRKJ.mjs.map +0 -1
- package/dist/chunk-TYZD5BQY.mjs.map +0 -1
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,110 +1,110 @@
|
|
|
1
1
|
|
|
2
|
-
> @copilotkit/shared@0.8.0
|
|
2
|
+
> @copilotkit/shared@0.8.0 build /Users/ataibarkai/LocalGit/Recursively.ai/CopilotKit/CopilotKit/packages/shared
|
|
3
3
|
> tsup --clean
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
5
|
+
CLI Building entry: src/index.ts, src/constants/copilot-protocol.ts, src/constants/index.ts, src/types/action.ts, src/types/annotated-function.ts, src/types/index.ts, src/types/openai-assistant.ts, src/utils/annotated-function.ts, src/utils/decode-chat-completion-as-text.ts, src/utils/decode-chat-completion.ts, src/utils/index.ts, src/utils/parse-chat-completion.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
|
+
CJS Build start
|
|
13
|
+
ESM dist/constants/index.mjs 187.00 B
|
|
14
|
+
ESM dist/types/annotated-function.mjs 79.00 B
|
|
15
|
+
ESM dist/utils/index.mjs 1001.00 B
|
|
16
|
+
ESM dist/utils/decode-chat-completion-as-text.mjs 170.00 B
|
|
17
|
+
ESM dist/utils/utils.test.mjs 170.00 B
|
|
18
|
+
ESM dist/utils/parse-chat-completion.mjs 147.00 B
|
|
19
|
+
ESM dist/chunk-UAPRMZEY.mjs 43.00 B
|
|
20
|
+
ESM dist/utils/utils.mjs 439.00 B
|
|
21
|
+
ESM dist/chunk-4MTSDAP6.mjs 729.00 B
|
|
22
|
+
ESM dist/chunk-2C54HQBI.mjs 4.92 KB
|
|
23
|
+
ESM dist/chunk-CBF2BIA7.mjs 3.62 KB
|
|
24
|
+
ESM dist/index.mjs 1.23 KB
|
|
25
|
+
ESM dist/chunk-HW4V75UQ.mjs 1.63 KB
|
|
26
|
+
ESM dist/types/action.mjs 67.00 B
|
|
27
|
+
ESM dist/utils/decode-chat-completion.mjs 150.00 B
|
|
28
|
+
ESM dist/chunk-3ZU7SB62.mjs 200.00 B
|
|
29
|
+
ESM dist/chunk-DE5K76I2.mjs 43.00 B
|
|
30
|
+
ESM dist/chunk-2M3YVRFS.mjs 43.00 B
|
|
31
|
+
ESM dist/chunk-YBHX4Y25.mjs 43.00 B
|
|
32
|
+
ESM dist/chunk-NAFEBKSO.mjs 43.00 B
|
|
33
|
+
ESM dist/types/openai-assistant.mjs 77.00 B
|
|
34
|
+
ESM dist/chunk-IAFBVORQ.mjs 43.00 B
|
|
35
|
+
ESM dist/chunk-K7TZQSAZ.mjs 4.59 KB
|
|
36
|
+
ESM dist/types/index.mjs 162.00 B
|
|
37
|
+
ESM dist/constants/copilot-protocol.mjs 166.00 B
|
|
38
|
+
ESM dist/utils/annotated-function.mjs 314.00 B
|
|
39
|
+
ESM dist/constants/index.mjs.map 71.00 B
|
|
40
|
+
ESM dist/utils/index.mjs.map 71.00 B
|
|
41
|
+
ESM dist/types/annotated-function.mjs.map 71.00 B
|
|
42
|
+
ESM dist/utils/decode-chat-completion-as-text.mjs.map 71.00 B
|
|
43
|
+
ESM dist/utils/utils.test.mjs.map 352.00 B
|
|
44
|
+
ESM dist/chunk-UAPRMZEY.mjs.map 71.00 B
|
|
45
|
+
ESM dist/utils/parse-chat-completion.mjs.map 71.00 B
|
|
46
|
+
ESM dist/chunk-4MTSDAP6.mjs.map 1.30 KB
|
|
47
|
+
ESM dist/chunk-HW4V75UQ.mjs.map 4.12 KB
|
|
48
|
+
ESM dist/chunk-CBF2BIA7.mjs.map 7.21 KB
|
|
49
|
+
ESM dist/utils/utils.mjs.map 71.00 B
|
|
50
|
+
ESM dist/chunk-3ZU7SB62.mjs.map 214.00 B
|
|
51
|
+
ESM dist/index.mjs.map 71.00 B
|
|
52
|
+
ESM dist/types/action.mjs.map 71.00 B
|
|
53
|
+
ESM dist/chunk-NAFEBKSO.mjs.map 71.00 B
|
|
54
|
+
ESM dist/chunk-YBHX4Y25.mjs.map 71.00 B
|
|
55
|
+
ESM dist/types/openai-assistant.mjs.map 71.00 B
|
|
56
|
+
ESM dist/chunk-2C54HQBI.mjs.map 12.00 KB
|
|
57
|
+
ESM dist/chunk-DE5K76I2.mjs.map 71.00 B
|
|
58
|
+
ESM dist/utils/decode-chat-completion.mjs.map 71.00 B
|
|
59
|
+
ESM dist/chunk-2M3YVRFS.mjs.map 71.00 B
|
|
60
|
+
ESM dist/types/index.mjs.map 71.00 B
|
|
61
|
+
ESM dist/chunk-K7TZQSAZ.mjs.map 8.35 KB
|
|
62
|
+
ESM dist/chunk-IAFBVORQ.mjs.map 71.00 B
|
|
63
|
+
ESM dist/constants/copilot-protocol.mjs.map 71.00 B
|
|
64
|
+
ESM dist/utils/annotated-function.mjs.map 71.00 B
|
|
65
|
+
ESM ⚡️ Build success in 21ms
|
|
66
|
+
CJS dist/index.js 17.20 KB
|
|
67
|
+
CJS dist/utils/annotated-function.js 5.81 KB
|
|
68
|
+
CJS dist/utils/utils.js 6.25 KB
|
|
69
|
+
CJS dist/utils/utils.test.js 184.00 B
|
|
70
|
+
CJS dist/utils/parse-chat-completion.js 2.68 KB
|
|
71
|
+
CJS dist/utils/decode-chat-completion.js 4.68 KB
|
|
72
|
+
CJS dist/utils/index.js 16.99 KB
|
|
73
|
+
CJS dist/utils/decode-chat-completion-as-text.js 1.82 KB
|
|
74
|
+
CJS dist/constants/copilot-protocol.js 1.25 KB
|
|
75
|
+
CJS dist/types/annotated-function.js 849.00 B
|
|
76
|
+
CJS dist/types/index.js 797.00 B
|
|
77
|
+
CJS dist/constants/index.js 1.25 KB
|
|
78
|
+
CJS dist/types/openai-assistant.js 841.00 B
|
|
79
|
+
CJS dist/types/action.js 801.00 B
|
|
80
|
+
CJS dist/index.js.map 33.09 KB
|
|
81
|
+
CJS dist/utils/annotated-function.js.map 8.41 KB
|
|
82
|
+
CJS dist/utils/utils.js.map 12.08 KB
|
|
83
|
+
CJS dist/utils/utils.test.js.map 354.00 B
|
|
84
|
+
CJS dist/utils/parse-chat-completion.js.map 4.17 KB
|
|
85
|
+
CJS dist/utils/decode-chat-completion.js.map 7.26 KB
|
|
86
|
+
CJS dist/utils/index.js.map 33.08 KB
|
|
87
|
+
CJS dist/utils/decode-chat-completion-as-text.js.map 1.34 KB
|
|
88
|
+
CJS dist/constants/copilot-protocol.js.map 261.00 B
|
|
89
|
+
CJS dist/constants/index.js.map 336.00 B
|
|
90
|
+
CJS dist/types/annotated-function.js.map 871.00 B
|
|
91
|
+
CJS dist/types/index.js.map 234.00 B
|
|
92
|
+
CJS dist/types/openai-assistant.js.map 2.86 KB
|
|
93
|
+
CJS dist/types/action.js.map 4.59 KB
|
|
94
|
+
CJS ⚡️ Build success in 22ms
|
|
95
|
+
DTS Build start
|
|
96
|
+
DTS ⚡️ Build success in 788ms
|
|
97
|
+
DTS dist/index.d.ts 1.31 KB
|
|
98
|
+
DTS dist/types/openai-assistant.d.ts 2.79 KB
|
|
99
|
+
DTS dist/types/annotated-function.d.ts 766.00 B
|
|
100
|
+
DTS dist/types/action.d.ts 2.09 KB
|
|
101
|
+
DTS dist/utils/utils.d.ts 4.32 KB
|
|
102
|
+
DTS dist/utils/parse-chat-completion.d.ts 826.00 B
|
|
103
|
+
DTS dist/utils/decode-chat-completion.d.ts 955.00 B
|
|
104
|
+
DTS dist/utils/decode-chat-completion-as-text.d.ts 298.00 B
|
|
105
|
+
DTS dist/utils/annotated-function.d.ts 600.00 B
|
|
106
|
+
DTS dist/constants/copilot-protocol.d.ts 128.00 B
|
|
107
|
+
DTS dist/constants/index.d.ts 73.00 B
|
|
108
|
+
DTS dist/types/index.d.ts 428.00 B
|
|
109
|
+
DTS dist/utils/index.d.ts 892.00 B
|
|
110
|
+
DTS dist/utils/utils.test.d.ts 12.00 B
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,20 @@
|
|
|
1
1
|
# @copilotkit/shared
|
|
2
2
|
|
|
3
|
+
## 0.9.0-alpha.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- gpt-4-turbo-april-2024
|
|
8
|
+
|
|
9
|
+
## 0.8.0
|
|
10
|
+
|
|
11
|
+
### Minor Changes
|
|
12
|
+
|
|
13
|
+
- 1f06d29: declare esm/cjs/types in export
|
|
14
|
+
- fix esm error
|
|
15
|
+
- 5a0b2cf: Inline codeblock style to avoid ESM error
|
|
16
|
+
- e12b921: ESM by default
|
|
17
|
+
|
|
3
18
|
## 0.8.0-mme-esm-error.2
|
|
4
19
|
|
|
5
20
|
### Minor Changes
|
|
@@ -46,19 +46,24 @@ function decodeChatCompletion(stream) {
|
|
|
46
46
|
try {
|
|
47
47
|
const { done, value } = await reader.read();
|
|
48
48
|
if (done) {
|
|
49
|
-
if (mode === "function") {
|
|
49
|
+
if ((mode == null ? void 0 : mode.type) === "function") {
|
|
50
50
|
flushFunctionCall();
|
|
51
51
|
}
|
|
52
52
|
await cleanup(controller);
|
|
53
53
|
return;
|
|
54
54
|
}
|
|
55
|
-
if (mode === "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))) {
|
|
55
|
+
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))) {
|
|
56
56
|
if (!flushFunctionCall()) {
|
|
57
57
|
return;
|
|
58
58
|
}
|
|
59
59
|
}
|
|
60
|
-
|
|
61
|
-
if (
|
|
60
|
+
const maybeFunctionCall = (_f = (_e = value.choices[0].delta.tool_calls) == null ? void 0 : _e[0]) == null ? void 0 : _f.function;
|
|
61
|
+
if (maybeFunctionCall) {
|
|
62
|
+
mode = { type: "function", function: maybeFunctionCall };
|
|
63
|
+
} else {
|
|
64
|
+
mode = { type: "message" };
|
|
65
|
+
}
|
|
66
|
+
if (mode.type === "message") {
|
|
62
67
|
if (value.choices[0].delta.role === "function") {
|
|
63
68
|
controller.enqueue({
|
|
64
69
|
type: "result",
|
|
@@ -72,15 +77,18 @@ function decodeChatCompletion(stream) {
|
|
|
72
77
|
});
|
|
73
78
|
}
|
|
74
79
|
continue;
|
|
75
|
-
} else if (mode === "function") {
|
|
76
|
-
|
|
77
|
-
|
|
80
|
+
} else if (mode.type === "function") {
|
|
81
|
+
const maybeFunctionCallName = mode.function.name;
|
|
82
|
+
if (maybeFunctionCallName) {
|
|
83
|
+
functionCallName = maybeFunctionCallName;
|
|
78
84
|
}
|
|
79
|
-
|
|
80
|
-
|
|
85
|
+
const maybeFunctionCallArguments = mode.function.arguments;
|
|
86
|
+
if (maybeFunctionCallArguments) {
|
|
87
|
+
functionCallArguments += maybeFunctionCallArguments;
|
|
81
88
|
}
|
|
82
|
-
|
|
83
|
-
|
|
89
|
+
const maybeFunctionCallScope = mode.function.scope;
|
|
90
|
+
if (maybeFunctionCallScope) {
|
|
91
|
+
functionCallScope = maybeFunctionCallScope;
|
|
84
92
|
}
|
|
85
93
|
controller.enqueue({
|
|
86
94
|
type: "partial",
|
|
@@ -105,4 +113,4 @@ function decodeChatCompletion(stream) {
|
|
|
105
113
|
export {
|
|
106
114
|
decodeChatCompletion
|
|
107
115
|
};
|
|
108
|
-
//# sourceMappingURL=chunk-
|
|
116
|
+
//# sourceMappingURL=chunk-CBF2BIA7.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
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":"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","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
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":";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":[]}
|
package/dist/index.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ export { AssistantMessage, FunctionCall, FunctionCallHandler, FunctionDefinition
|
|
|
2
2
|
export { AnnotatedFunction, AnnotatedFunctionArgument, AnnotatedFunctionArrayArgument, AnnotatedFunctionSimpleArgument } from './types/annotated-function.js';
|
|
3
3
|
export { Action, BaseParameter, MappedParameterTypes, ObjectParameter, Parameter, StringParameter } from './types/action.js';
|
|
4
4
|
export { COMPLEX_HEADER, StreamPart, StreamPartType, StreamString, StreamStringPrefixes, decodeResult, encodeResult, formatStreamPart, isStreamStringEqualToType, parseStreamPart, streamPartsByCode, validCodes } from './utils/utils.js';
|
|
5
|
-
export { ChatCompletionChunk, parseChatCompletion } from './utils/parse-chat-completion.js';
|
|
5
|
+
export { ChatCompletionChunk, ToolCallFunctionCall, ToolCallPayload, parseChatCompletion } from './utils/parse-chat-completion.js';
|
|
6
6
|
export { ChatCompletionContentEvent, ChatCompletionEvent, ChatCompletionFunctionEvent, ChatCompletionPartialEvent, ChatCompletionResultEvent, decodeChatCompletion } from './utils/decode-chat-completion.js';
|
|
7
7
|
export { decodeChatCompletionAsText } from './utils/decode-chat-completion-as-text.js';
|
|
8
8
|
export { actionToChatCompletionFunction, annotatedFunctionToAction, annotatedFunctionToChatCompletionFunction } from './utils/annotated-function.js';
|
package/dist/index.js
CHANGED
|
@@ -288,19 +288,24 @@ function decodeChatCompletion(stream) {
|
|
|
288
288
|
try {
|
|
289
289
|
const { done, value } = await reader.read();
|
|
290
290
|
if (done) {
|
|
291
|
-
if (mode === "function") {
|
|
291
|
+
if ((mode == null ? void 0 : mode.type) === "function") {
|
|
292
292
|
flushFunctionCall();
|
|
293
293
|
}
|
|
294
294
|
await cleanup(controller);
|
|
295
295
|
return;
|
|
296
296
|
}
|
|
297
|
-
if (mode === "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))) {
|
|
297
|
+
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))) {
|
|
298
298
|
if (!flushFunctionCall()) {
|
|
299
299
|
return;
|
|
300
300
|
}
|
|
301
301
|
}
|
|
302
|
-
|
|
303
|
-
if (
|
|
302
|
+
const maybeFunctionCall = (_f = (_e = value.choices[0].delta.tool_calls) == null ? void 0 : _e[0]) == null ? void 0 : _f.function;
|
|
303
|
+
if (maybeFunctionCall) {
|
|
304
|
+
mode = { type: "function", function: maybeFunctionCall };
|
|
305
|
+
} else {
|
|
306
|
+
mode = { type: "message" };
|
|
307
|
+
}
|
|
308
|
+
if (mode.type === "message") {
|
|
304
309
|
if (value.choices[0].delta.role === "function") {
|
|
305
310
|
controller.enqueue({
|
|
306
311
|
type: "result",
|
|
@@ -314,15 +319,18 @@ function decodeChatCompletion(stream) {
|
|
|
314
319
|
});
|
|
315
320
|
}
|
|
316
321
|
continue;
|
|
317
|
-
} else if (mode === "function") {
|
|
318
|
-
|
|
319
|
-
|
|
322
|
+
} else if (mode.type === "function") {
|
|
323
|
+
const maybeFunctionCallName = mode.function.name;
|
|
324
|
+
if (maybeFunctionCallName) {
|
|
325
|
+
functionCallName = maybeFunctionCallName;
|
|
320
326
|
}
|
|
321
|
-
|
|
322
|
-
|
|
327
|
+
const maybeFunctionCallArguments = mode.function.arguments;
|
|
328
|
+
if (maybeFunctionCallArguments) {
|
|
329
|
+
functionCallArguments += maybeFunctionCallArguments;
|
|
323
330
|
}
|
|
324
|
-
|
|
325
|
-
|
|
331
|
+
const maybeFunctionCallScope = mode.function.scope;
|
|
332
|
+
if (maybeFunctionCallScope) {
|
|
333
|
+
functionCallScope = maybeFunctionCallScope;
|
|
326
334
|
}
|
|
327
335
|
controller.enqueue({
|
|
328
336
|
type: "partial",
|