@copilotkit/shared 0.2.0-alpha.2 → 0.2.0-alpha.4
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 +86 -33
- package/CHANGELOG.md +36 -3
- package/dist/chunk-4MTSDAP6.mjs +31 -0
- package/dist/chunk-4MTSDAP6.mjs.map +1 -0
- package/dist/chunk-FD6FGKYY.mjs +3 -0
- package/dist/chunk-GFCJUF6J.mjs +95 -0
- package/dist/chunk-GFCJUF6J.mjs.map +1 -0
- package/dist/chunk-L46AW3ET.mjs +28 -0
- package/dist/chunk-L46AW3ET.mjs.map +1 -0
- package/dist/chunk-UAPRMZEY.mjs +3 -0
- package/dist/chunk-W6NBBCWB.mjs +59 -0
- package/dist/chunk-W6NBBCWB.mjs.map +1 -0
- package/dist/chunk-YBHX4Y25.mjs +3 -0
- package/dist/chunk-YBHX4Y25.mjs.map +1 -0
- package/dist/index.d.ts +6 -1
- package/dist/index.js +345 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +7 -2
- package/dist/types/annotated-function.d.ts +24 -0
- package/dist/types/annotated-function.js +4 -0
- package/dist/types/annotated-function.js.map +1 -0
- package/dist/types/annotated-function.mjs +3 -0
- package/dist/types/annotated-function.mjs.map +1 -0
- package/dist/types/index.d.ts +2 -1
- package/dist/types/index.js +4 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/index.mjs +2 -1
- package/dist/types/openai-assistant.d.ts +26 -2
- package/dist/types/openai-assistant.js +4 -0
- package/dist/types/openai-assistant.js.map +1 -0
- package/dist/utils/annotated-function.d.ts +6 -0
- package/dist/utils/annotated-function.js +30 -0
- package/dist/utils/annotated-function.js.map +1 -0
- package/dist/utils/annotated-function.mjs +3 -0
- package/dist/utils/annotated-function.mjs.map +1 -0
- package/dist/utils/decode-chat-completion-as-text.d.ts +7 -0
- package/dist/utils/decode-chat-completion-as-text.js +33 -0
- package/dist/utils/decode-chat-completion-as-text.js.map +1 -0
- package/dist/utils/decode-chat-completion-as-text.mjs +3 -0
- package/dist/utils/decode-chat-completion-as-text.mjs.map +1 -0
- package/dist/utils/decode-chat-completion.d.ts +21 -0
- package/dist/utils/decode-chat-completion.js +97 -0
- package/dist/utils/decode-chat-completion.js.map +1 -0
- package/dist/utils/decode-chat-completion.mjs +3 -0
- package/dist/utils/decode-chat-completion.mjs.map +1 -0
- package/dist/utils/index.d.ts +5 -0
- package/dist/utils/index.js +345 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/index.mjs +5 -1
- package/dist/utils/parse-chat-completion.d.ts +17 -0
- package/dist/utils/parse-chat-completion.js +61 -0
- package/dist/utils/parse-chat-completion.js.map +1 -0
- package/dist/utils/parse-chat-completion.mjs +3 -0
- package/dist/utils/parse-chat-completion.mjs.map +1 -0
- package/dist/utils/utils.js +140 -0
- package/dist/utils/utils.js.map +1 -0
- package/dist/utils/utils.test.js +10 -0
- package/dist/utils/utils.test.js.map +1 -0
- package/package.json +8 -6
- package/src/types/annotated-function.ts +27 -0
- package/src/types/index.ts +1 -0
- package/src/types/openai-assistant.ts +31 -1
- package/src/utils/annotated-function.ts +33 -0
- package/src/utils/decode-chat-completion-as-text.ts +33 -0
- package/src/utils/decode-chat-completion.ts +127 -0
- package/src/utils/index.ts +4 -0
- package/src/utils/parse-chat-completion.ts +84 -0
- package/tsup.config.ts +1 -2
- package/dist/chunk-YJLRG5U3.mjs +0 -3
- package/dist/chunk-YPBKY4KY.mjs +0 -3
- /package/dist/{chunk-YJLRG5U3.mjs.map → chunk-FD6FGKYY.mjs.map} +0 -0
- /package/dist/{chunk-YPBKY4KY.mjs.map → chunk-UAPRMZEY.mjs.map} +0 -0
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,43 +1,96 @@
|
|
|
1
1
|
|
|
2
|
-
> @copilotkit/shared@0.1.
|
|
3
|
-
> tsup --treeshake
|
|
2
|
+
> @copilotkit/shared@0.1.1 build /Users/ataibarkai/LocalGit/Recursively.ai/CopilotKit/CopilotKit/packages/shared
|
|
3
|
+
> tsup --treeshake --clean
|
|
4
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
|
|
5
|
+
CLI Building entry: src/index.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
6
|
CLI Using tsconfig: tsconfig.json
|
|
7
7
|
CLI tsup v6.7.0
|
|
8
8
|
CLI Using tsup config: /Users/ataibarkai/LocalGit/Recursively.ai/CopilotKit/CopilotKit/packages/shared/tsup.config.ts
|
|
9
9
|
CLI Target: node14
|
|
10
10
|
CLI Cleaning output folder
|
|
11
11
|
ESM Build start
|
|
12
|
-
|
|
12
|
+
CJS Build start
|
|
13
|
+
Generated an empty chunk: "annotated-function".
|
|
14
|
+
Generated an empty chunk: "index".
|
|
15
|
+
Generated an empty chunk: "openai-assistant".
|
|
16
|
+
Generated an empty chunk: "chunk-FD6FGKYY".
|
|
17
|
+
Generated an empty chunk: "chunk-YBHX4Y25".
|
|
13
18
|
Generated an empty chunk: "chunk-IAFBVORQ".
|
|
14
|
-
Generated an empty chunk: "chunk-
|
|
15
|
-
ESM dist/chunk-
|
|
16
|
-
ESM dist/
|
|
17
|
-
ESM dist/
|
|
18
|
-
ESM dist/
|
|
19
|
-
ESM dist/chunk-
|
|
20
|
-
ESM dist/chunk-
|
|
21
|
-
ESM dist/
|
|
22
|
-
ESM dist/utils/utils.mjs
|
|
23
|
-
ESM dist/
|
|
24
|
-
ESM dist/
|
|
25
|
-
ESM dist/
|
|
26
|
-
ESM dist/index.mjs.
|
|
27
|
-
ESM dist/
|
|
28
|
-
ESM dist/
|
|
29
|
-
ESM dist/
|
|
30
|
-
ESM dist/utils/
|
|
31
|
-
ESM dist/
|
|
32
|
-
ESM dist/utils/
|
|
33
|
-
ESM dist/chunk-
|
|
34
|
-
ESM dist/
|
|
35
|
-
ESM
|
|
19
|
+
Generated an empty chunk: "chunk-UAPRMZEY".
|
|
20
|
+
ESM dist/chunk-FD6FGKYY.mjs 76.00 B
|
|
21
|
+
ESM dist/utils/utils.test.mjs 202.00 B
|
|
22
|
+
ESM dist/chunk-IAFBVORQ.mjs 76.00 B
|
|
23
|
+
ESM dist/chunk-UAPRMZEY.mjs 76.00 B
|
|
24
|
+
ESM dist/chunk-YBHX4Y25.mjs 76.00 B
|
|
25
|
+
ESM dist/chunk-GFCJUF6J.mjs 2.58 KB
|
|
26
|
+
ESM dist/chunk-L46AW3ET.mjs 860.00 B
|
|
27
|
+
ESM dist/utils/utils.mjs 237.00 B
|
|
28
|
+
ESM dist/utils/parse-chat-completion.mjs 143.00 B
|
|
29
|
+
ESM dist/chunk-NCTOECKL.mjs 4.62 KB
|
|
30
|
+
ESM dist/utils/annotated-function.mjs 162.00 B
|
|
31
|
+
ESM dist/index.mjs 630.00 B
|
|
32
|
+
ESM dist/types/openai-assistant.mjs 109.00 B
|
|
33
|
+
ESM dist/types/annotated-function.mjs 111.00 B
|
|
34
|
+
ESM dist/types/index.mjs 162.00 B
|
|
35
|
+
ESM dist/utils/decode-chat-completion.mjs 145.00 B
|
|
36
|
+
ESM dist/chunk-4MTSDAP6.mjs 759.00 B
|
|
37
|
+
ESM dist/utils/index.mjs 543.00 B
|
|
38
|
+
ESM dist/chunk-W6NBBCWB.mjs 1.66 KB
|
|
39
|
+
ESM dist/utils/decode-chat-completion-as-text.mjs 159.00 B
|
|
40
|
+
ESM dist/chunk-FD6FGKYY.mjs.map 51.00 B
|
|
41
|
+
ESM dist/chunk-IAFBVORQ.mjs.map 51.00 B
|
|
42
|
+
ESM dist/chunk-YBHX4Y25.mjs.map 51.00 B
|
|
43
|
+
ESM dist/chunk-UAPRMZEY.mjs.map 51.00 B
|
|
44
|
+
ESM dist/chunk-GFCJUF6J.mjs.map 5.24 KB
|
|
45
|
+
ESM dist/utils/parse-chat-completion.mjs.map 51.00 B
|
|
46
|
+
ESM dist/utils/utils.test.mjs.map 351.00 B
|
|
47
|
+
ESM dist/types/annotated-function.mjs.map 51.00 B
|
|
48
|
+
ESM dist/types/openai-assistant.mjs.map 51.00 B
|
|
49
|
+
ESM dist/index.mjs.map 51.00 B
|
|
50
|
+
ESM dist/types/index.mjs.map 51.00 B
|
|
51
|
+
ESM dist/utils/annotated-function.mjs.map 51.00 B
|
|
52
|
+
ESM dist/chunk-L46AW3ET.mjs.map 1.55 KB
|
|
53
|
+
ESM dist/utils/decode-chat-completion.mjs.map 51.00 B
|
|
54
|
+
ESM dist/chunk-4MTSDAP6.mjs.map 1.30 KB
|
|
55
|
+
ESM dist/utils/index.mjs.map 51.00 B
|
|
56
|
+
ESM dist/utils/utils.mjs.map 51.00 B
|
|
57
|
+
ESM dist/chunk-NCTOECKL.mjs.map 11.39 KB
|
|
58
|
+
ESM dist/chunk-W6NBBCWB.mjs.map 3.42 KB
|
|
59
|
+
ESM dist/utils/decode-chat-completion-as-text.mjs.map 51.00 B
|
|
60
|
+
ESM ⚡️ Build success in 112ms
|
|
61
|
+
CJS dist/index.js 10.43 KB
|
|
62
|
+
CJS dist/types/index.js 80.00 B
|
|
63
|
+
CJS dist/utils/annotated-function.js 919.00 B
|
|
64
|
+
CJS dist/utils/decode-chat-completion-as-text.js 815.00 B
|
|
65
|
+
CJS dist/utils/index.js 10.43 KB
|
|
66
|
+
CJS dist/utils/utils.test.js 216.00 B
|
|
67
|
+
CJS dist/utils/parse-chat-completion.js 1.70 KB
|
|
68
|
+
CJS dist/utils/utils.js 4.80 KB
|
|
69
|
+
CJS dist/utils/decode-chat-completion.js 2.62 KB
|
|
70
|
+
CJS dist/types/openai-assistant.js 91.00 B
|
|
71
|
+
CJS dist/types/annotated-function.js 93.00 B
|
|
72
|
+
CJS dist/index.js.map 22.64 KB
|
|
73
|
+
CJS dist/types/index.js.map 51.00 B
|
|
74
|
+
CJS dist/utils/annotated-function.js.map 1.56 KB
|
|
75
|
+
CJS dist/utils/index.js.map 22.66 KB
|
|
76
|
+
CJS dist/utils/decode-chat-completion-as-text.js.map 1.30 KB
|
|
77
|
+
CJS dist/utils/parse-chat-completion.js.map 3.43 KB
|
|
78
|
+
CJS dist/utils/utils.js.map 11.39 KB
|
|
79
|
+
CJS dist/utils/utils.test.js.map 351.00 B
|
|
80
|
+
CJS dist/utils/decode-chat-completion.js.map 5.24 KB
|
|
81
|
+
CJS dist/types/openai-assistant.js.map 51.00 B
|
|
82
|
+
CJS dist/types/annotated-function.js.map 51.00 B
|
|
83
|
+
CJS ⚡️ Build success in 113ms
|
|
36
84
|
DTS Build start
|
|
37
|
-
DTS ⚡️ Build success in
|
|
38
|
-
DTS dist/index.d.ts
|
|
39
|
-
DTS dist/types/openai-assistant.d.ts
|
|
40
|
-
DTS dist/
|
|
41
|
-
DTS dist/
|
|
42
|
-
DTS dist/utils/
|
|
43
|
-
DTS dist/utils/
|
|
85
|
+
DTS ⚡️ Build success in 916ms
|
|
86
|
+
DTS dist/index.d.ts 954.00 B
|
|
87
|
+
DTS dist/types/openai-assistant.d.ts 2.36 KB
|
|
88
|
+
DTS dist/types/annotated-function.d.ts 767.00 B
|
|
89
|
+
DTS dist/utils/utils.d.ts 4.17 KB
|
|
90
|
+
DTS dist/utils/parse-chat-completion.d.ts 478.00 B
|
|
91
|
+
DTS dist/utils/decode-chat-completion.d.ts 769.00 B
|
|
92
|
+
DTS dist/utils/decode-chat-completion-as-text.d.ts 298.00 B
|
|
93
|
+
DTS dist/utils/annotated-function.d.ts 296.00 B
|
|
94
|
+
DTS dist/types/index.d.ts 282.00 B
|
|
95
|
+
DTS dist/utils/index.d.ts 710.00 B
|
|
96
|
+
DTS dist/utils/utils.test.d.ts 12.00 B
|
package/CHANGELOG.md
CHANGED
|
@@ -1,22 +1,55 @@
|
|
|
1
1
|
# @copilotkit/shared
|
|
2
2
|
|
|
3
|
+
## 0.2.0-alpha.4
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- .5
|
|
8
|
+
|
|
9
|
+
## 0.2.0-alpha.3
|
|
10
|
+
|
|
11
|
+
### Minor Changes
|
|
12
|
+
|
|
13
|
+
- .4
|
|
14
|
+
|
|
3
15
|
## 0.2.0-alpha.2
|
|
4
16
|
|
|
5
17
|
### Minor Changes
|
|
6
18
|
|
|
7
|
-
-
|
|
19
|
+
- .3
|
|
8
20
|
|
|
9
21
|
## 0.2.0-alpha.1
|
|
10
22
|
|
|
11
23
|
### Minor Changes
|
|
12
24
|
|
|
13
|
-
-
|
|
25
|
+
- .2
|
|
26
|
+
- .3
|
|
14
27
|
|
|
15
28
|
## 0.2.0-alpha.0
|
|
16
29
|
|
|
17
30
|
### Minor Changes
|
|
18
31
|
|
|
19
|
-
-
|
|
32
|
+
- build naming refactor
|
|
33
|
+
|
|
34
|
+
## 0.1.1
|
|
35
|
+
|
|
36
|
+
### Patch Changes
|
|
37
|
+
|
|
38
|
+
- stop generating button working
|
|
39
|
+
- aa6bc5a: fix stop generate
|
|
40
|
+
- cf0bde6: change order of operations on stop cleanup
|
|
41
|
+
|
|
42
|
+
## 0.1.1-alpha.1
|
|
43
|
+
|
|
44
|
+
### Patch Changes
|
|
45
|
+
|
|
46
|
+
- change order of operations on stop cleanup
|
|
47
|
+
|
|
48
|
+
## 0.1.1-alpha.0
|
|
49
|
+
|
|
50
|
+
### Patch Changes
|
|
51
|
+
|
|
52
|
+
- fix stop generate
|
|
20
53
|
|
|
21
54
|
## 0.1.0
|
|
22
55
|
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// src/utils/decode-chat-completion-as-text.ts
|
|
2
|
+
function decodeChatCompletionAsText(stream) {
|
|
3
|
+
const reader = stream.getReader();
|
|
4
|
+
return new ReadableStream({
|
|
5
|
+
async pull(controller) {
|
|
6
|
+
while (true) {
|
|
7
|
+
try {
|
|
8
|
+
const { done, value } = await reader.read();
|
|
9
|
+
if (done) {
|
|
10
|
+
controller.close();
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
if (value.type === "content") {
|
|
14
|
+
controller.enqueue(value.content);
|
|
15
|
+
continue;
|
|
16
|
+
}
|
|
17
|
+
} catch (error) {
|
|
18
|
+
controller.error(error);
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
cancel() {
|
|
24
|
+
reader.cancel();
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export { decodeChatCompletionAsText };
|
|
30
|
+
//# sourceMappingURL=out.js.map
|
|
31
|
+
//# sourceMappingURL=chunk-4MTSDAP6.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/utils/decode-chat-completion-as-text.ts"],"names":[],"mappings":";AAEO,SAAS,2BACd,QACwB;AACxB,QAAM,SAAS,OAAO,UAAU;AAEhC,SAAO,IAAI,eAAuB;AAAA,IAChC,MAAM,KAAK,YAAY;AACrB,aAAO,MAAM;AACX,YAAI;AACF,gBAAM,EAAE,MAAM,MAAM,IAAI,MAAM,OAAO,KAAK;AAE1C,cAAI,MAAM;AACR,uBAAW,MAAM;AACjB;AAAA,UACF;AAEA,cAAI,MAAM,SAAS,WAAW;AAC5B,uBAAW,QAAQ,MAAM,OAAO;AAChC;AAAA,UACF;AAAA,QACF,SAAS,OAAP;AACA,qBAAW,MAAM,KAAK;AACtB;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,IACA,SAAS;AACP,aAAO,OAAO;AAAA,IAChB;AAAA,EACF,CAAC;AACH","sourcesContent":["import { ChatCompletionEvent } from \"./decode-chat-completion\";\n\nexport function decodeChatCompletionAsText(\n stream: ReadableStream<ChatCompletionEvent>,\n): ReadableStream<string> {\n const reader = stream.getReader();\n\n return new ReadableStream<string>({\n async pull(controller) {\n while (true) {\n try {\n const { done, value } = await reader.read();\n\n if (done) {\n controller.close();\n return;\n }\n\n if (value.type === \"content\") {\n controller.enqueue(value.content);\n continue;\n }\n } catch (error) {\n controller.error(error);\n return;\n }\n }\n },\n cancel() {\n reader.cancel();\n },\n });\n}\n"]}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
// src/utils/decode-chat-completion.ts
|
|
2
|
+
function decodeChatCompletion(stream) {
|
|
3
|
+
const reader = stream.getReader();
|
|
4
|
+
let mode = null;
|
|
5
|
+
let functionCallName = "";
|
|
6
|
+
let functionCallArguments = "";
|
|
7
|
+
async function cleanup(controller) {
|
|
8
|
+
if (controller) {
|
|
9
|
+
try {
|
|
10
|
+
controller.close();
|
|
11
|
+
} catch (_) {
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
if (reader) {
|
|
15
|
+
try {
|
|
16
|
+
await reader.cancel();
|
|
17
|
+
} catch (_) {
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return new ReadableStream({
|
|
22
|
+
async pull(controller) {
|
|
23
|
+
const flushFunctionCall = () => {
|
|
24
|
+
let args = null;
|
|
25
|
+
try {
|
|
26
|
+
args = JSON.parse(functionCallArguments);
|
|
27
|
+
} catch (error) {
|
|
28
|
+
cleanup(controller);
|
|
29
|
+
controller.error(error);
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
controller.enqueue({
|
|
33
|
+
type: "function",
|
|
34
|
+
name: functionCallName,
|
|
35
|
+
arguments: args
|
|
36
|
+
});
|
|
37
|
+
mode = null;
|
|
38
|
+
functionCallName = "";
|
|
39
|
+
functionCallArguments = "";
|
|
40
|
+
return true;
|
|
41
|
+
};
|
|
42
|
+
while (true) {
|
|
43
|
+
try {
|
|
44
|
+
const { done, value } = await reader.read();
|
|
45
|
+
if (done) {
|
|
46
|
+
if (mode === "function") {
|
|
47
|
+
flushFunctionCall();
|
|
48
|
+
}
|
|
49
|
+
await cleanup(controller);
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
if (mode === "function" && !value.choices[0].delta.function_call) {
|
|
53
|
+
if (!flushFunctionCall()) {
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
mode = value.choices[0].delta.function_call ? "function" : "message";
|
|
58
|
+
if (mode === "message") {
|
|
59
|
+
if (value.choices[0].delta.content) {
|
|
60
|
+
controller.enqueue({
|
|
61
|
+
type: "content",
|
|
62
|
+
content: value.choices[0].delta.content
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
continue;
|
|
66
|
+
} else if (mode === "function") {
|
|
67
|
+
if (value.choices[0].delta.function_call.name) {
|
|
68
|
+
functionCallName = value.choices[0].delta.function_call.name;
|
|
69
|
+
}
|
|
70
|
+
if (value.choices[0].delta.function_call.arguments) {
|
|
71
|
+
functionCallArguments += value.choices[0].delta.function_call.arguments;
|
|
72
|
+
}
|
|
73
|
+
controller.enqueue({
|
|
74
|
+
type: "partial",
|
|
75
|
+
name: functionCallName,
|
|
76
|
+
arguments: functionCallArguments
|
|
77
|
+
});
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
} catch (error) {
|
|
81
|
+
controller.error(error);
|
|
82
|
+
await cleanup(controller);
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
},
|
|
87
|
+
cancel() {
|
|
88
|
+
reader.cancel();
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export { decodeChatCompletion };
|
|
94
|
+
//# sourceMappingURL=out.js.map
|
|
95
|
+
//# sourceMappingURL=chunk-GFCJUF6J.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/utils/decode-chat-completion.ts"],"names":[],"mappings":";AAwBO,SAAS,qBACd,QACqC;AACrC,QAAM,SAAS,OAAO,UAAU;AAEhC,MAAI,OAAsC;AAC1C,MAAI,mBAA2B;AAC/B,MAAI,wBAAgC;AAEpC,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,YAAM,oBAAoB,MAAe;AACvC,YAAI,OAAY;AAChB,YAAI;AACF,iBAAO,KAAK,MAAM,qBAAqB;AAAA,QACzC,SAAS,OAAP;AACA,kBAAQ,UAAU;AAClB,qBAAW,MAAM,KAAK;AACtB,iBAAO;AAAA,QACT;AACA,mBAAW,QAAQ;AAAA,UACjB,MAAM;AAAA,UACN,MAAM;AAAA,UACN,WAAW;AAAA,QACb,CAAC;AAED,eAAO;AACP,2BAAmB;AACnB,gCAAwB;AACxB,eAAO;AAAA,MACT;AAEA,aAAO,MAAM;AACX,YAAI;AACF,gBAAM,EAAE,MAAM,MAAM,IAAI,MAAM,OAAO,KAAK;AAE1C,cAAI,MAAM;AACR,gBAAI,SAAS,YAAY;AACvB,gCAAkB;AAAA,YACpB;AACA,kBAAM,QAAQ,UAAU;AACxB;AAAA,UACF;AAGA,cAAI,SAAS,cAAc,CAAC,MAAM,QAAQ,CAAC,EAAE,MAAM,eAAe;AAChE,gBAAI,CAAC,kBAAkB,GAAG;AACxB;AAAA,YACF;AAAA,UACF;AAEA,iBAAO,MAAM,QAAQ,CAAC,EAAE,MAAM,gBAAgB,aAAa;AAG3D,cAAI,SAAS,WAAW;AACtB,gBAAI,MAAM,QAAQ,CAAC,EAAE,MAAM,SAAS;AAClC,yBAAW,QAAQ;AAAA,gBACjB,MAAM;AAAA,gBACN,SAAS,MAAM,QAAQ,CAAC,EAAE,MAAM;AAAA,cAClC,CAAC;AAAA,YACH;AACA;AAAA,UACF,WAES,SAAS,YAAY;AAC5B,gBAAI,MAAM,QAAQ,CAAC,EAAE,MAAM,cAAe,MAAM;AAC9C,iCAAmB,MAAM,QAAQ,CAAC,EAAE,MAAM,cAAe;AAAA,YAC3D;AACA,gBAAI,MAAM,QAAQ,CAAC,EAAE,MAAM,cAAe,WAAW;AACnD,uCAAyB,MAAM,QAAQ,CAAC,EAAE,MAAM,cAAe;AAAA,YACjE;AACA,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","sourcesContent":["import { ChatCompletionChunk } 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}\n\nexport type ChatCompletionEvent =\n | ChatCompletionContentEvent\n | ChatCompletionPartialEvent\n | ChatCompletionFunctionEvent;\n\nexport function decodeChatCompletion(\n stream: ReadableStream<ChatCompletionChunk>,\n): ReadableStream<ChatCompletionEvent> {\n const reader = stream.getReader();\n\n let mode: \"function\" | \"message\" | null = null;\n let functionCallName: string = \"\";\n let functionCallArguments: string = \"\";\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 });\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 === \"function\") {\n flushFunctionCall();\n }\n await cleanup(controller);\n return;\n }\n\n // In case we are in a function call but the next message is not a function call, flush it.\n if (mode === \"function\" && !value.choices[0].delta.function_call) {\n if (!flushFunctionCall()) {\n return;\n }\n }\n\n mode = value.choices[0].delta.function_call ? \"function\" : \"message\";\n\n // if we get a message, emit the content and continue;\n if (mode === \"message\") {\n 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 === \"function\") {\n if (value.choices[0].delta.function_call!.name) {\n functionCallName = value.choices[0].delta.function_call!.name!;\n }\n if (value.choices[0].delta.function_call!.arguments) {\n functionCallArguments += value.choices[0].delta.function_call!.arguments!;\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"]}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// src/utils/annotated-function.ts
|
|
2
|
+
function annotatedFunctionToChatCompletionFunction(annotatedFunction) {
|
|
3
|
+
let parameters = {};
|
|
4
|
+
for (let arg of annotatedFunction.argumentAnnotations) {
|
|
5
|
+
let { name, required, ...forwardedArgs } = arg;
|
|
6
|
+
parameters[arg.name] = forwardedArgs;
|
|
7
|
+
}
|
|
8
|
+
let requiredParameterNames = [];
|
|
9
|
+
for (let arg of annotatedFunction.argumentAnnotations) {
|
|
10
|
+
if (arg.required) {
|
|
11
|
+
requiredParameterNames.push(arg.name);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
let chatCompletionFunction = {
|
|
15
|
+
name: annotatedFunction.name,
|
|
16
|
+
description: annotatedFunction.description,
|
|
17
|
+
parameters: {
|
|
18
|
+
type: "object",
|
|
19
|
+
properties: parameters,
|
|
20
|
+
required: requiredParameterNames
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
return chatCompletionFunction;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export { annotatedFunctionToChatCompletionFunction };
|
|
27
|
+
//# sourceMappingURL=out.js.map
|
|
28
|
+
//# sourceMappingURL=chunk-L46AW3ET.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/utils/annotated-function.ts"],"names":[],"mappings":";AAEO,SAAS,0CACd,mBACU;AAEV,MAAI,aAAqC,CAAC;AAC1C,WAAS,OAAO,kBAAkB,qBAAqB;AAErD,QAAI,EAAE,MAAM,UAAU,GAAG,cAAc,IAAI;AAC3C,eAAW,IAAI,IAAI,IAAI;AAAA,EACzB;AAEA,MAAI,yBAAmC,CAAC;AACxC,WAAS,OAAO,kBAAkB,qBAAqB;AACrD,QAAI,IAAI,UAAU;AAChB,6BAAuB,KAAK,IAAI,IAAI;AAAA,IACtC;AAAA,EACF;AAGA,MAAI,yBAAmC;AAAA,IACrC,MAAM,kBAAkB;AAAA,IACxB,aAAa,kBAAkB;AAAA,IAC/B,YAAY;AAAA,MACV,MAAM;AAAA,MACN,YAAY;AAAA,MACZ,UAAU;AAAA,IACZ;AAAA,EACF;AAEA,SAAO;AACT","sourcesContent":["import { AnnotatedFunction, Function } from \"../types\";\n\nexport function annotatedFunctionToChatCompletionFunction(\n annotatedFunction: AnnotatedFunction<any[]>,\n): Function {\n // Create the parameters object based on the argumentAnnotations\n let parameters: { [key: string]: any } = {};\n for (let arg of annotatedFunction.argumentAnnotations) {\n // isolate the args we should forward inline\n let { name, required, ...forwardedArgs } = arg;\n parameters[arg.name] = forwardedArgs;\n }\n\n let requiredParameterNames: string[] = [];\n for (let arg of annotatedFunction.argumentAnnotations) {\n if (arg.required) {\n requiredParameterNames.push(arg.name);\n }\n }\n\n // Create the ChatCompletionFunctions object\n let chatCompletionFunction: Function = {\n name: annotatedFunction.name,\n description: annotatedFunction.description,\n parameters: {\n type: \"object\",\n properties: parameters,\n required: requiredParameterNames,\n },\n };\n\n return chatCompletionFunction;\n}\n"]}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
// src/utils/parse-chat-completion.ts
|
|
2
|
+
function parseChatCompletion(stream) {
|
|
3
|
+
const reader = stream.getReader();
|
|
4
|
+
let buffer = new Uint8Array();
|
|
5
|
+
async function cleanup(controller) {
|
|
6
|
+
if (controller) {
|
|
7
|
+
try {
|
|
8
|
+
controller.close();
|
|
9
|
+
} catch (_) {
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
if (reader) {
|
|
13
|
+
try {
|
|
14
|
+
await reader.cancel();
|
|
15
|
+
} catch (_) {
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return new ReadableStream({
|
|
20
|
+
async pull(controller) {
|
|
21
|
+
while (true) {
|
|
22
|
+
try {
|
|
23
|
+
const { done, value } = await reader.read();
|
|
24
|
+
if (done) {
|
|
25
|
+
await cleanup(controller);
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
const newBuffer = new Uint8Array(buffer.length + value.length);
|
|
29
|
+
newBuffer.set(buffer);
|
|
30
|
+
newBuffer.set(value, buffer.length);
|
|
31
|
+
buffer = newBuffer;
|
|
32
|
+
const valueString = new TextDecoder("utf-8").decode(buffer);
|
|
33
|
+
const lines = valueString.split("\n").filter((line) => line.trim() !== "");
|
|
34
|
+
buffer = !valueString.endsWith("\n") ? new TextEncoder().encode(lines.pop() || "") : new Uint8Array();
|
|
35
|
+
for (const line of lines) {
|
|
36
|
+
const cleanedLine = line.replace(/^data: /, "");
|
|
37
|
+
if (cleanedLine === "[DONE]") {
|
|
38
|
+
await cleanup(controller);
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
const json = JSON.parse(cleanedLine);
|
|
42
|
+
controller.enqueue(json);
|
|
43
|
+
}
|
|
44
|
+
} catch (error) {
|
|
45
|
+
controller.error(error);
|
|
46
|
+
await cleanup(controller);
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
cancel() {
|
|
52
|
+
reader.cancel();
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export { parseChatCompletion };
|
|
58
|
+
//# sourceMappingURL=out.js.map
|
|
59
|
+
//# sourceMappingURL=chunk-W6NBBCWB.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/utils/parse-chat-completion.ts"],"names":[],"mappings":";AAkBO,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","sourcesContent":["import { Role } from \"../types/openai-assistant\";\n\nexport interface ChatCompletionChunk {\n choices: {\n delta: {\n role: Role;\n content?: string | null;\n function_call?: {\n name?: string;\n arguments?: string;\n };\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"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":""}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,7 @@
|
|
|
1
|
-
export { AssistantMessage, FunctionCall, JSONValue, Message } from './types/openai-assistant.js';
|
|
1
|
+
export { AssistantMessage, Function, FunctionCall, FunctionCallHandler, JSONValue, Message, Role } from './types/openai-assistant.js';
|
|
2
|
+
export { AnnotatedFunction, AnnotatedFunctionArgument, AnnotatedFunctionArrayArgument, AnnotatedFunctionSimpleArgument } from './types/annotated-function.js';
|
|
2
3
|
export { COMPLEX_HEADER, StreamPart, StreamPartType, StreamString, StreamStringPrefixes, formatStreamPart, isStreamStringEqualToType, parseStreamPart, streamPartsByCode, validCodes } from './utils/utils.js';
|
|
4
|
+
export { ChatCompletionChunk, parseChatCompletion } from './utils/parse-chat-completion.js';
|
|
5
|
+
export { ChatCompletionContentEvent, ChatCompletionEvent, ChatCompletionFunctionEvent, ChatCompletionPartialEvent, decodeChatCompletion } from './utils/decode-chat-completion.js';
|
|
6
|
+
export { decodeChatCompletionAsText } from './utils/decode-chat-completion-as-text.js';
|
|
7
|
+
export { annotatedFunctionToChatCompletionFunction } from './utils/annotated-function.js';
|