@copilotkit/shared 0.5.0-mme-assistant-api.0 → 0.5.0-mme-function-call-labels.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/.turbo/turbo-build.log +74 -67
  2. package/CHANGELOG.md +14 -1
  3. package/dist/{chunk-XJLZZQTA.mjs → chunk-2C54HQBI.mjs} +9 -1
  4. package/dist/chunk-2C54HQBI.mjs.map +1 -0
  5. package/dist/chunk-DE5K76I2.mjs +1 -0
  6. package/dist/chunk-GI5AWM7D.mjs +112 -0
  7. package/dist/chunk-GI5AWM7D.mjs.map +1 -0
  8. package/dist/chunk-NAFEBKSO.mjs +1 -0
  9. package/dist/chunk-NAFEBKSO.mjs.map +1 -0
  10. package/dist/{chunk-CPRMXQXK.mjs → chunk-NKEDWRKJ.mjs} +1 -1
  11. package/dist/chunk-NKEDWRKJ.mjs.map +1 -0
  12. package/dist/index.d.ts +3 -2
  13. package/dist/index.js +90 -0
  14. package/dist/index.js.map +1 -1
  15. package/dist/index.mjs +12 -7
  16. package/dist/types/action.d.ts +54 -0
  17. package/dist/types/action.js +19 -0
  18. package/dist/types/action.js.map +1 -0
  19. package/dist/types/action.mjs +2 -0
  20. package/dist/types/action.mjs.map +1 -0
  21. package/dist/types/index.d.ts +1 -0
  22. package/dist/types/index.js.map +1 -1
  23. package/dist/types/index.mjs +2 -1
  24. package/dist/types/openai-assistant.d.ts +6 -1
  25. package/dist/types/openai-assistant.js.map +1 -1
  26. package/dist/utils/annotated-function.d.ts +3 -1
  27. package/dist/utils/annotated-function.js +81 -0
  28. package/dist/utils/annotated-function.js.map +1 -1
  29. package/dist/utils/annotated-function.mjs +3 -1
  30. package/dist/utils/decode-chat-completion.js.map +1 -1
  31. package/dist/utils/decode-chat-completion.mjs +1 -1
  32. package/dist/utils/index.d.ts +3 -2
  33. package/dist/utils/index.js +90 -0
  34. package/dist/utils/index.js.map +1 -1
  35. package/dist/utils/index.mjs +10 -6
  36. package/dist/utils/utils.d.ts +8 -7
  37. package/dist/utils/utils.js +9 -0
  38. package/dist/utils/utils.js.map +1 -1
  39. package/dist/utils/utils.mjs +3 -1
  40. package/package.json +5 -5
  41. package/src/types/action.ts +73 -0
  42. package/src/types/index.ts +1 -0
  43. package/src/types/openai-assistant.ts +7 -1
  44. package/src/utils/annotated-function.ts +90 -1
  45. package/src/utils/decode-chat-completion.ts +0 -2
  46. package/src/utils/utils.ts +8 -0
  47. package/dist/chunk-CPRMXQXK.mjs.map +0 -1
  48. package/dist/chunk-FD6FGKYY.mjs +0 -1
  49. package/dist/chunk-NSY3OEEP.mjs +0 -32
  50. package/dist/chunk-NSY3OEEP.mjs.map +0 -1
  51. package/dist/chunk-XJLZZQTA.mjs.map +0 -1
  52. /package/dist/{chunk-FD6FGKYY.mjs.map → chunk-DE5K76I2.mjs.map} +0 -0
@@ -1,8 +1,8 @@
1
1
 
2
- > @copilotkit/shared@0.5.0-mme-assistant-api.0 build /home/runner/work/CopilotKit/CopilotKit/CopilotKit/packages/shared
2
+ > @copilotkit/shared@0.5.0-mme-function-call-labels.2 build /home/runner/work/CopilotKit/CopilotKit/CopilotKit/packages/shared
3
3
  > tsup --clean
4
4
 
5
- CLI Building entry: src/index.ts, src/constants/copilot-protocol.ts, src/constants/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
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
6
  CLI Using tsconfig: tsconfig.json
7
7
  CLI tsup v6.7.0
8
8
  CLI Using tsup config: /home/runner/work/CopilotKit/CopilotKit/CopilotKit/packages/shared/tsup.config.ts
@@ -10,94 +10,101 @@
10
10
  CLI Cleaning output folder
11
11
  ESM Build start
12
12
  CJS Build start
13
- ESM dist/utils/decode-chat-completion.mjs 150.00 B
14
- ESM dist/utils/index.mjs 843.00 B
15
- ESM dist/utils/parse-chat-completion.mjs 147.00 B
16
- ESM dist/utils/utils.test.mjs 170.00 B
17
- ESM dist/utils/utils.mjs 407.00 B
18
- ESM dist/index.mjs 1.04 KB
13
+ CJS dist/index.js 15.46 KB
14
+ CJS dist/constants/copilot-protocol.js 1.25 KB
15
+ CJS dist/constants/index.js 1.25 KB
16
+ CJS dist/types/action.js 801.00 B
17
+ CJS dist/types/annotated-function.js 849.00 B
18
+ CJS dist/types/index.js 797.00 B
19
+ CJS dist/types/openai-assistant.js 841.00 B
20
+ CJS dist/utils/annotated-function.js 4.33 KB
21
+ CJS dist/utils/decode-chat-completion-as-text.js 1.82 KB
22
+ CJS dist/utils/decode-chat-completion.js 4.42 KB
23
+ CJS dist/utils/index.js 15.26 KB
24
+ CJS dist/utils/parse-chat-completion.js 2.68 KB
25
+ CJS dist/utils/utils.js 6.25 KB
26
+ CJS dist/utils/utils.test.js 184.00 B
27
+ CJS dist/index.js.map 30.29 KB
28
+ CJS dist/constants/copilot-protocol.js.map 261.00 B
29
+ CJS dist/constants/index.js.map 336.00 B
30
+ CJS dist/types/action.js.map 2.94 KB
31
+ CJS dist/types/annotated-function.js.map 871.00 B
32
+ CJS dist/types/index.js.map 234.00 B
33
+ CJS dist/types/openai-assistant.js.map 2.86 KB
34
+ CJS dist/utils/annotated-function.js.map 5.99 KB
35
+ CJS dist/utils/decode-chat-completion-as-text.js.map 1.34 KB
36
+ CJS dist/utils/decode-chat-completion.js.map 6.89 KB
37
+ CJS dist/utils/index.js.map 30.28 KB
38
+ CJS dist/utils/parse-chat-completion.js.map 4.15 KB
39
+ CJS dist/utils/utils.js.map 12.08 KB
40
+ CJS dist/utils/utils.test.js.map 354.00 B
41
+ CJS ⚡️ Build success in 64ms
42
+ ESM dist/index.mjs 1.17 KB
43
+ ESM dist/constants/copilot-protocol.mjs 166.00 B
44
+ ESM dist/types/action.mjs 67.00 B
45
+ ESM dist/utils/annotated-function.mjs 256.00 B
19
46
  ESM dist/chunk-UAPRMZEY.mjs 43.00 B
20
- ESM dist/chunk-CPRMXQXK.mjs 3.36 KB
47
+ ESM dist/chunk-4MTSDAP6.mjs 729.00 B
48
+ ESM dist/chunk-NKEDWRKJ.mjs 3.36 KB
21
49
  ESM dist/chunk-TYZD5BQY.mjs 1.63 KB
22
- ESM dist/chunk-XJLZZQTA.mjs 4.80 KB
23
- ESM dist/constants/copilot-protocol.mjs 166.00 B
24
- ESM dist/constants/index.mjs 187.00 B
50
+ ESM dist/chunk-2C54HQBI.mjs 4.92 KB
25
51
  ESM dist/chunk-2M3YVRFS.mjs 43.00 B
26
52
  ESM dist/chunk-3ZU7SB62.mjs 200.00 B
53
+ ESM dist/constants/index.mjs 187.00 B
27
54
  ESM dist/types/annotated-function.mjs 79.00 B
28
- ESM dist/types/index.mjs 130.00 B
29
- ESM dist/chunk-FD6FGKYY.mjs 43.00 B
55
+ ESM dist/types/index.mjs 162.00 B
56
+ ESM dist/chunk-DE5K76I2.mjs 43.00 B
57
+ ESM dist/chunk-NAFEBKSO.mjs 43.00 B
30
58
  ESM dist/chunk-YBHX4Y25.mjs 43.00 B
31
59
  ESM dist/types/openai-assistant.mjs 77.00 B
32
60
  ESM dist/chunk-IAFBVORQ.mjs 43.00 B
33
- ESM dist/utils/annotated-function.mjs 188.00 B
34
- ESM dist/chunk-NSY3OEEP.mjs 888.00 B
61
+ ESM dist/chunk-GI5AWM7D.mjs 3.18 KB
35
62
  ESM dist/utils/decode-chat-completion-as-text.mjs 170.00 B
36
- ESM dist/chunk-4MTSDAP6.mjs 729.00 B
37
- ESM dist/utils/index.mjs.map 71.00 B
38
- ESM dist/utils/decode-chat-completion.mjs.map 71.00 B
39
- ESM dist/utils/parse-chat-completion.mjs.map 71.00 B
40
- ESM dist/utils/utils.test.mjs.map 352.00 B
41
- ESM dist/utils/utils.mjs.map 71.00 B
42
- ESM dist/chunk-UAPRMZEY.mjs.map 71.00 B
63
+ ESM dist/utils/decode-chat-completion.mjs 150.00 B
64
+ ESM dist/utils/index.mjs 943.00 B
65
+ ESM dist/utils/utils.test.mjs 170.00 B
66
+ ESM dist/utils/parse-chat-completion.mjs 147.00 B
67
+ ESM dist/utils/utils.mjs 439.00 B
43
68
  ESM dist/index.mjs.map 71.00 B
44
- ESM dist/chunk-CPRMXQXK.mjs.map 6.93 KB
45
- ESM dist/chunk-TYZD5BQY.mjs.map 4.10 KB
46
- ESM dist/chunk-XJLZZQTA.mjs.map 11.76 KB
47
- ESM dist/constants/index.mjs.map 71.00 B
48
69
  ESM dist/constants/copilot-protocol.mjs.map 71.00 B
70
+ ESM dist/types/action.mjs.map 71.00 B
71
+ ESM dist/utils/annotated-function.mjs.map 71.00 B
72
+ ESM dist/chunk-UAPRMZEY.mjs.map 71.00 B
73
+ ESM dist/chunk-4MTSDAP6.mjs.map 1.30 KB
74
+ ESM dist/chunk-NKEDWRKJ.mjs.map 6.85 KB
75
+ ESM dist/chunk-TYZD5BQY.mjs.map 4.10 KB
76
+ ESM dist/chunk-2C54HQBI.mjs.map 12.00 KB
49
77
  ESM dist/chunk-2M3YVRFS.mjs.map 71.00 B
50
78
  ESM dist/chunk-3ZU7SB62.mjs.map 214.00 B
79
+ ESM dist/constants/index.mjs.map 71.00 B
51
80
  ESM dist/types/annotated-function.mjs.map 71.00 B
52
81
  ESM dist/types/index.mjs.map 71.00 B
53
- ESM dist/chunk-FD6FGKYY.mjs.map 71.00 B
82
+ ESM dist/chunk-DE5K76I2.mjs.map 71.00 B
83
+ ESM dist/chunk-NAFEBKSO.mjs.map 71.00 B
54
84
  ESM dist/chunk-YBHX4Y25.mjs.map 71.00 B
55
- ESM dist/chunk-IAFBVORQ.mjs.map 71.00 B
56
85
  ESM dist/types/openai-assistant.mjs.map 71.00 B
57
- ESM dist/utils/annotated-function.mjs.map 71.00 B
58
- ESM dist/chunk-NSY3OEEP.mjs.map 1.69 KB
86
+ ESM dist/chunk-IAFBVORQ.mjs.map 71.00 B
87
+ ESM dist/chunk-GI5AWM7D.mjs.map 5.94 KB
59
88
  ESM dist/utils/decode-chat-completion-as-text.mjs.map 71.00 B
60
- ESM dist/chunk-4MTSDAP6.mjs.map 1.30 KB
61
- ESM ⚡️ Build success in 61ms
62
- CJS dist/index.js 12.92 KB
63
- CJS dist/constants/index.js 1.25 KB
64
- CJS dist/types/annotated-function.js 849.00 B
65
- CJS dist/utils/annotated-function.js 1.95 KB
66
- CJS dist/types/index.js 797.00 B
67
- CJS dist/types/openai-assistant.js 841.00 B
68
- CJS dist/utils/decode-chat-completion-as-text.js 1.82 KB
69
- CJS dist/utils/decode-chat-completion.js 4.42 KB
70
- CJS dist/utils/index.js 12.72 KB
71
- CJS dist/utils/parse-chat-completion.js 2.68 KB
72
- CJS dist/utils/utils.test.js 184.00 B
73
- CJS dist/utils/utils.js 6.09 KB
74
- CJS dist/constants/copilot-protocol.js 1.25 KB
75
- CJS dist/index.js.map 25.88 KB
76
- CJS dist/constants/index.js.map 336.00 B
77
- CJS dist/types/annotated-function.js.map 871.00 B
78
- CJS dist/utils/annotated-function.js.map 1.74 KB
79
- CJS dist/types/openai-assistant.js.map 2.65 KB
80
- CJS dist/types/index.js.map 205.00 B
81
- CJS dist/utils/decode-chat-completion-as-text.js.map 1.34 KB
82
- CJS dist/utils/decode-chat-completion.js.map 6.97 KB
83
- CJS dist/utils/parse-chat-completion.js.map 4.15 KB
84
- CJS dist/utils/index.js.map 25.87 KB
85
- CJS dist/utils/utils.test.js.map 354.00 B
86
- CJS dist/utils/utils.js.map 11.84 KB
87
- CJS dist/constants/copilot-protocol.js.map 261.00 B
88
- CJS ⚡️ Build success in 64ms
89
+ ESM dist/utils/decode-chat-completion.mjs.map 71.00 B
90
+ ESM dist/utils/index.mjs.map 71.00 B
91
+ ESM dist/utils/utils.test.mjs.map 352.00 B
92
+ ESM dist/utils/parse-chat-completion.mjs.map 71.00 B
93
+ ESM dist/utils/utils.mjs.map 71.00 B
94
+ ESM ⚡️ Build success in 70ms
89
95
  DTS Build start
90
- DTS ⚡️ Build success in 1951ms
91
- DTS dist/index.d.ts 1.08 KB
92
- DTS dist/types/openai-assistant.d.ts 2.58 KB
96
+ DTS ⚡️ Build success in 2150ms
97
+ DTS dist/index.d.ts 1.25 KB
98
+ DTS dist/types/openai-assistant.d.ts 2.79 KB
93
99
  DTS dist/types/annotated-function.d.ts 766.00 B
94
- DTS dist/utils/utils.d.ts 4.24 KB
100
+ DTS dist/types/action.d.ts 1.86 KB
101
+ DTS dist/utils/utils.d.ts 4.32 KB
95
102
  DTS dist/utils/parse-chat-completion.d.ts 805.00 B
96
103
  DTS dist/utils/decode-chat-completion.d.ts 955.00 B
97
104
  DTS dist/utils/decode-chat-completion-as-text.d.ts 298.00 B
98
- DTS dist/utils/annotated-function.d.ts 308.00 B
105
+ DTS dist/utils/annotated-function.d.ts 471.00 B
99
106
  DTS dist/constants/copilot-protocol.d.ts 128.00 B
100
107
  DTS dist/constants/index.d.ts 73.00 B
101
- DTS dist/types/index.d.ts 308.00 B
102
- DTS dist/utils/index.d.ts 751.00 B
108
+ DTS dist/types/index.d.ts 428.00 B
109
+ DTS dist/utils/index.d.ts 826.00 B
103
110
  DTS dist/utils/utils.test.d.ts 12.00 B
package/CHANGELOG.md CHANGED
@@ -1,11 +1,24 @@
1
1
  # @copilotkit/shared
2
2
 
3
- ## 0.5.0-mme-assistant-api.0
3
+ ## 0.5.0-mme-function-call-labels.2
4
+
5
+ ### Minor Changes
6
+
7
+ - be00d61: Alpha
8
+ - QA
9
+
10
+ ## 0.5.0-mme-experimental-actions.1
4
11
 
5
12
  ### Minor Changes
6
13
 
7
14
  - Alpha
8
15
 
16
+ ## 0.5.0-mme-experimental-actions.0
17
+
18
+ ### Minor Changes
19
+
20
+ - QA new action type
21
+
9
22
  ## 0.4.1
10
23
 
11
24
  ### Patch Changes
@@ -8,6 +8,13 @@ function encodeResult(result) {
8
8
  return JSON.stringify(result);
9
9
  }
10
10
  }
11
+ function decodeResult(result) {
12
+ try {
13
+ return JSON.parse(result);
14
+ } catch (e) {
15
+ return result;
16
+ }
17
+ }
11
18
  var textStreamPart = {
12
19
  code: "0",
13
20
  name: "text",
@@ -138,6 +145,7 @@ var COMPLEX_HEADER = "X-Experimental-Stream-Data";
138
145
 
139
146
  export {
140
147
  encodeResult,
148
+ decodeResult,
141
149
  streamPartsByCode,
142
150
  StreamStringPrefixes,
143
151
  validCodes,
@@ -146,4 +154,4 @@ export {
146
154
  isStreamStringEqualToType,
147
155
  COMPLEX_HEADER
148
156
  };
149
- //# sourceMappingURL=chunk-XJLZZQTA.mjs.map
157
+ //# sourceMappingURL=chunk-2C54HQBI.mjs.map
@@ -0,0 +1 @@
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":";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,MAAM;AAAA,EACN,MAAM;AAAA,EACN,OAAO,CAAC,UAAqB;AAC3B,QAAI,OAAO,UAAU,UAAU;AAC7B,YAAM,IAAI,MAAM,qCAAqC;AAAA,IACvD;AACA,WAAO,EAAE,MAAM,QAAQ,MAAM;AAAA,EAC/B;AACF;AASA,IAAM,yBAA4F;AAAA,EAChG,MAAM;AAAA,EACN,MAAM;AAAA,EACN,OAAO,CAAC,UAAqB;AAC3B,QACE,SAAS,QACT,OAAO,UAAU,YACjB,EAAE,mBAAmB,UACrB,OAAO,MAAM,kBAAkB,YAC/B,MAAM,iBAAiB,QACvB,EAAE,UAAU,MAAM,kBAClB,EAAE,eAAe,MAAM,kBACvB,OAAO,MAAM,cAAc,SAAS,YACpC,OAAO,MAAM,cAAc,cAAc,UACzC;AACA,YAAM,IAAI,MAAM,yEAAyE;AAAA,IAC3F;AAEA,WAAO;AAAA,MACL,MAAM;AAAA,MACN;AAAA,IACF;AAAA,EACF;AACF;AAEA,IAAM,iBAA4D;AA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AAsB,CAAC;AACpD,QAAM,YAAuB,KAAK,MAAM,SAAS;AAEjD,SAAO,kBAAkB,IAAI,EAAE,MAAM,SAAS;AAChD;AAQO,SAAS,iBACd,MACA,OACc;AACd,QAAM,aAAa,YAAY,KAAK,CAAC,SAAS,KAAK,SAAS,IAAI;AAEhE,MAAI,CAAC,YAAY;AACf,UAAM,IAAI,MAAM,6BAA6B,MAAM;AAAA,EACrD;AAEA,SAAO,GAAG,WAAW,QAAQ,KAAK,UAAU,KAAK;AAAA;AACnD;AAEO,IAAM,4BAA4B,CACvC,MACA,UAEA,MAAM,WAAW,GAAG,qBAAqB,IAAI,IAAI,KAAK,MAAM,SAAS,IAAI;AAQpE,IAAM,iBAAiB;","names":[]}
@@ -0,0 +1 @@
1
+ //# sourceMappingURL=chunk-DE5K76I2.mjs.map
@@ -0,0 +1,112 @@
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
+ type: "function",
16
+ function: {
17
+ name: annotatedFunction.name,
18
+ description: annotatedFunction.description,
19
+ parameters: {
20
+ type: "object",
21
+ properties: parameters,
22
+ required: requiredParameterNames
23
+ }
24
+ }
25
+ };
26
+ return chatCompletionFunction;
27
+ }
28
+ function convertAttribute(attribute) {
29
+ var _a, _b, _c;
30
+ switch (attribute.type) {
31
+ case "string":
32
+ return {
33
+ type: "string",
34
+ description: attribute.description,
35
+ ...attribute.enum && { enum: attribute.enum }
36
+ };
37
+ case "number":
38
+ case "boolean":
39
+ return {
40
+ type: attribute.type,
41
+ description: attribute.description
42
+ };
43
+ case "object":
44
+ case "object[]":
45
+ const properties = (_a = attribute.attributes) == null ? void 0 : _a.reduce((acc, attr) => {
46
+ acc[attr.name] = convertAttribute(attr);
47
+ return acc;
48
+ }, {});
49
+ const required = (_b = attribute.attributes) == null ? void 0 : _b.filter((attr) => attr.required !== false).map((attr) => attr.name);
50
+ if (attribute.type === "object[]") {
51
+ return {
52
+ type: "array",
53
+ items: {
54
+ type: "object",
55
+ ...properties && { properties },
56
+ ...required && required.length > 0 && { required }
57
+ },
58
+ description: attribute.description
59
+ };
60
+ }
61
+ return {
62
+ type: "object",
63
+ description: attribute.description,
64
+ ...properties && { properties },
65
+ ...required && required.length > 0 && { required }
66
+ };
67
+ default:
68
+ if ((_c = attribute.type) == null ? void 0 : _c.endsWith("[]")) {
69
+ const itemType = attribute.type.slice(0, -2);
70
+ return {
71
+ type: "array",
72
+ items: { type: itemType },
73
+ description: attribute.description
74
+ };
75
+ }
76
+ return {
77
+ type: "string",
78
+ description: attribute.description
79
+ };
80
+ }
81
+ }
82
+ function actionToChatCompletionFunction(action) {
83
+ let parameters = {};
84
+ for (let parameter of action.parameters || []) {
85
+ parameters[parameter.name] = convertAttribute(parameter);
86
+ }
87
+ let requiredParameterNames = [];
88
+ for (let arg of action.parameters || []) {
89
+ if (arg.required !== false) {
90
+ requiredParameterNames.push(arg.name);
91
+ }
92
+ }
93
+ let chatCompletionFunction = {
94
+ type: "function",
95
+ function: {
96
+ name: action.name,
97
+ ...action.description && { description: action.description },
98
+ parameters: {
99
+ type: "object",
100
+ properties: parameters,
101
+ required: requiredParameterNames
102
+ }
103
+ }
104
+ };
105
+ return chatCompletionFunction;
106
+ }
107
+
108
+ export {
109
+ annotatedFunctionToChatCompletionFunction,
110
+ actionToChatCompletionFunction
111
+ };
112
+ //# sourceMappingURL=chunk-GI5AWM7D.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/utils/annotated-function.ts"],"sourcesContent":["import { Action, AnnotatedFunction, ToolDefinition, Parameter } from \"../types\";\n\nexport function annotatedFunctionToChatCompletionFunction(\n annotatedFunction: AnnotatedFunction<any[]>,\n): ToolDefinition {\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: ToolDefinition = {\n type: \"function\",\n function: {\n name: annotatedFunction.name,\n description: annotatedFunction.description,\n parameters: {\n type: \"object\",\n properties: parameters,\n required: requiredParameterNames,\n },\n },\n };\n\n return chatCompletionFunction;\n}\n\nfunction convertAttribute(attribute: Parameter): any {\n switch (attribute.type) {\n case \"string\":\n return {\n type: \"string\",\n description: attribute.description,\n ...(attribute.enum && { enum: attribute.enum }),\n };\n case \"number\":\n case \"boolean\":\n return {\n type: attribute.type,\n description: attribute.description,\n };\n case \"object\":\n case \"object[]\":\n const properties = attribute.attributes?.reduce((acc, attr) => {\n acc[attr.name] = convertAttribute(attr);\n return acc;\n }, {} as Record<string, any>);\n const required = attribute.attributes\n ?.filter((attr) => attr.required !== false)\n .map((attr) => attr.name);\n if (attribute.type === \"object[]\") {\n return {\n type: \"array\",\n items: {\n type: \"object\",\n ...(properties && { properties }),\n ...(required && required.length > 0 && { required }),\n },\n description: attribute.description,\n };\n }\n return {\n type: \"object\",\n description: attribute.description,\n ...(properties && { properties }),\n ...(required && required.length > 0 && { required }),\n };\n default:\n // Handle arrays of primitive types and undefined attribute.type\n if (attribute.type?.endsWith(\"[]\")) {\n const itemType = attribute.type.slice(0, -2);\n return {\n type: \"array\",\n items: { type: itemType },\n description: attribute.description,\n };\n }\n // Fallback for undefined type or any other unexpected type\n return {\n type: \"string\",\n description: attribute.description,\n };\n }\n}\n\nexport function actionToChatCompletionFunction(action: Action<any>): ToolDefinition {\n // Create the parameters object based on the argumentAnnotations\n let parameters: { [key: string]: any } = {};\n for (let parameter of action.parameters || []) {\n parameters[parameter.name] = convertAttribute(parameter);\n }\n\n let requiredParameterNames: string[] = [];\n for (let arg of action.parameters || []) {\n if (arg.required !== false) {\n requiredParameterNames.push(arg.name);\n }\n }\n\n // Create the ChatCompletionFunctions object\n let chatCompletionFunction: ToolDefinition = {\n type: \"function\",\n function: {\n name: action.name,\n ...(action.description && { description: action.description }),\n parameters: {\n type: \"object\",\n properties: parameters,\n required: requiredParameterNames,\n },\n },\n };\n\n return chatCompletionFunction;\n}\n"],"mappings":";AAEO,SAAS,0CACd,mBACgB;AAEhB,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,yBAAyC;AAAA,IAC3C,MAAM;AAAA,IACN,UAAU;AAAA,MACR,MAAM,kBAAkB;AAAA,MACxB,aAAa,kBAAkB;AAAA,MAC/B,YAAY;AAAA,QACV,MAAM;AAAA,QACN,YAAY;AAAA,QACZ,UAAU;AAAA,MACZ;AAAA,IACF;AAAA,EACF;AAEA,SAAO;AACT;AAEA,SAAS,iBAAiB,WAA2B;AArCrD;AAsCE,UAAQ,UAAU,MAAM;AAAA,IACtB,KAAK;AACH,aAAO;AAAA,QACL,MAAM;AAAA,QACN,aAAa,UAAU;AAAA,QACvB,GAAI,UAAU,QAAQ,EAAE,MAAM,UAAU,KAAK;AAAA,MAC/C;AAAA,IACF,KAAK;AAAA,IACL,KAAK;AACH,aAAO;AAAA,QACL,MAAM,UAAU;AAAA,QAChB,aAAa,UA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AC1B,6BAAuB,KAAK,IAAI,IAAI;AAAA,IACtC;AAAA,EACF;AAGA,MAAI,yBAAyC;AAAA,IAC3C,MAAM;AAAA,IACN,UAAU;AAAA,MACR,MAAM,OAAO;AAAA,MACb,GAAI,OAAO,eAAe,EAAE,aAAa,OAAO,YAAY;AAAA,MAC5D,YAAY;AAAA,QACV,MAAM;AAAA,QACN,YAAY;AAAA,QACZ,UAAU;AAAA,MACZ;AAAA,IACF;AAAA,EACF;AAEA,SAAO;AACT;","names":[]}
@@ -0,0 +1 @@
1
+ //# sourceMappingURL=chunk-NAFEBKSO.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
@@ -105,4 +105,4 @@ function decodeChatCompletion(stream) {
105
105
  export {
106
106
  decodeChatCompletion
107
107
  };
108
- //# sourceMappingURL=chunk-CPRMXQXK.mjs.map
108
+ //# sourceMappingURL=chunk-NKEDWRKJ.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/utils/decode-chat-completion.ts"],"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 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 let mode: \"function\" | \"message\" | 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 === \"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\n // - not a function call\n // - another function call (when name is present)\n // => flush it.\n if (\n mode === \"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 mode = value.choices[0].delta.tool_calls?.[0]?.function ? \"function\" : \"message\";\n\n // if we get a message, emit the content and continue;\n if (mode === \"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 === \"function\") {\n if (value.choices[0].delta.tool_calls![0].function.name) {\n functionCallName = value.choices[0].delta.tool_calls![0].function.name!;\n }\n if (value.choices[0].delta.tool_calls![0].function.arguments) {\n functionCallArguments += value.choices[0].delta.tool_calls![0].function.arguments!;\n }\n if (value.choices[0].delta.tool_calls![0].function.scope) {\n functionCallScope = value.choices[0].delta.tool_calls![0].function.scope!;\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":";AAgCO,SAAS,qBACd,QACqC;AACrC,QAAM,SAAS,OAAO,UAAU;AAEhC,MAAI,OAAsC;AAC1C,MAAI,mBAA2B;AAC/B,MAAI,wBAAgC;AACpC,MAAI,oBAAyC;AAE7C,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;AAxD3B;AAyDM,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,UACX,OAAO;AAAA,QACT,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;AAMA,cACE,SAAS,eACR,GAAC,iBAAM,QAAQ,CAAC,EAAE,MAAM,eAAvB,mBAAoC,OAApC,mBAA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AAC;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":[]}
package/dist/index.d.ts CHANGED
@@ -1,8 +1,9 @@
1
1
  export { AssistantMessage, FunctionCall, FunctionCallHandler, FunctionDefinition, JSONValue, Message, Role, ToolDefinition } from './types/openai-assistant.js';
2
2
  export { AnnotatedFunction, AnnotatedFunctionArgument, AnnotatedFunctionArrayArgument, AnnotatedFunctionSimpleArgument } from './types/annotated-function.js';
3
- export { COMPLEX_HEADER, StreamPart, StreamPartType, StreamString, StreamStringPrefixes, encodeResult, formatStreamPart, isStreamStringEqualToType, parseStreamPart, streamPartsByCode, validCodes } from './utils/utils.js';
3
+ export { Action, BaseParameter, MappedParameterTypes, ObjectParameter, Parameter, StringParameter } from './types/action.js';
4
+ export { COMPLEX_HEADER, StreamPart, StreamPartType, StreamString, StreamStringPrefixes, decodeResult, encodeResult, formatStreamPart, isStreamStringEqualToType, parseStreamPart, streamPartsByCode, validCodes } from './utils/utils.js';
4
5
  export { ChatCompletionChunk, parseChatCompletion } from './utils/parse-chat-completion.js';
5
6
  export { ChatCompletionContentEvent, ChatCompletionEvent, ChatCompletionFunctionEvent, ChatCompletionPartialEvent, ChatCompletionResultEvent, decodeChatCompletion } from './utils/decode-chat-completion.js';
6
7
  export { decodeChatCompletionAsText } from './utils/decode-chat-completion-as-text.js';
7
- export { annotatedFunctionToChatCompletionFunction } from './utils/annotated-function.js';
8
+ export { actionToChatCompletionFunction, annotatedFunctionToChatCompletionFunction } from './utils/annotated-function.js';
8
9
  export { EXCLUDE_FROM_FORWARD_PROPS_KEYS } from './constants/copilot-protocol.js';
package/dist/index.js CHANGED
@@ -23,9 +23,11 @@ __export(src_exports, {
23
23
  COMPLEX_HEADER: () => COMPLEX_HEADER,
24
24
  EXCLUDE_FROM_FORWARD_PROPS_KEYS: () => EXCLUDE_FROM_FORWARD_PROPS_KEYS,
25
25
  StreamStringPrefixes: () => StreamStringPrefixes,
26
+ actionToChatCompletionFunction: () => actionToChatCompletionFunction,
26
27
  annotatedFunctionToChatCompletionFunction: () => annotatedFunctionToChatCompletionFunction,
27
28
  decodeChatCompletion: () => decodeChatCompletion,
28
29
  decodeChatCompletionAsText: () => decodeChatCompletionAsText,
30
+ decodeResult: () => decodeResult,
29
31
  encodeResult: () => encodeResult,
30
32
  formatStreamPart: () => formatStreamPart,
31
33
  isStreamStringEqualToType: () => isStreamStringEqualToType,
@@ -46,6 +48,13 @@ function encodeResult(result) {
46
48
  return JSON.stringify(result);
47
49
  }
48
50
  }
51
+ function decodeResult(result) {
52
+ try {
53
+ return JSON.parse(result);
54
+ } catch (e) {
55
+ return result;
56
+ }
57
+ }
49
58
  var textStreamPart = {
50
59
  code: "0",
51
60
  name: "text",
@@ -389,6 +398,85 @@ function annotatedFunctionToChatCompletionFunction(annotatedFunction) {
389
398
  };
390
399
  return chatCompletionFunction;
391
400
  }
401
+ function convertAttribute(attribute) {
402
+ var _a, _b, _c;
403
+ switch (attribute.type) {
404
+ case "string":
405
+ return {
406
+ type: "string",
407
+ description: attribute.description,
408
+ ...attribute.enum && { enum: attribute.enum }
409
+ };
410
+ case "number":
411
+ case "boolean":
412
+ return {
413
+ type: attribute.type,
414
+ description: attribute.description
415
+ };
416
+ case "object":
417
+ case "object[]":
418
+ const properties = (_a = attribute.attributes) == null ? void 0 : _a.reduce((acc, attr) => {
419
+ acc[attr.name] = convertAttribute(attr);
420
+ return acc;
421
+ }, {});
422
+ const required = (_b = attribute.attributes) == null ? void 0 : _b.filter((attr) => attr.required !== false).map((attr) => attr.name);
423
+ if (attribute.type === "object[]") {
424
+ return {
425
+ type: "array",
426
+ items: {
427
+ type: "object",
428
+ ...properties && { properties },
429
+ ...required && required.length > 0 && { required }
430
+ },
431
+ description: attribute.description
432
+ };
433
+ }
434
+ return {
435
+ type: "object",
436
+ description: attribute.description,
437
+ ...properties && { properties },
438
+ ...required && required.length > 0 && { required }
439
+ };
440
+ default:
441
+ if ((_c = attribute.type) == null ? void 0 : _c.endsWith("[]")) {
442
+ const itemType = attribute.type.slice(0, -2);
443
+ return {
444
+ type: "array",
445
+ items: { type: itemType },
446
+ description: attribute.description
447
+ };
448
+ }
449
+ return {
450
+ type: "string",
451
+ description: attribute.description
452
+ };
453
+ }
454
+ }
455
+ function actionToChatCompletionFunction(action) {
456
+ let parameters = {};
457
+ for (let parameter of action.parameters || []) {
458
+ parameters[parameter.name] = convertAttribute(parameter);
459
+ }
460
+ let requiredParameterNames = [];
461
+ for (let arg of action.parameters || []) {
462
+ if (arg.required !== false) {
463
+ requiredParameterNames.push(arg.name);
464
+ }
465
+ }
466
+ let chatCompletionFunction = {
467
+ type: "function",
468
+ function: {
469
+ name: action.name,
470
+ ...action.description && { description: action.description },
471
+ parameters: {
472
+ type: "object",
473
+ properties: parameters,
474
+ required: requiredParameterNames
475
+ }
476
+ }
477
+ };
478
+ return chatCompletionFunction;
479
+ }
392
480
 
393
481
  // src/constants/copilot-protocol.ts
394
482
  var EXCLUDE_FROM_FORWARD_PROPS_KEYS = "exclude_from_forward_props_keys";
@@ -397,9 +485,11 @@ var EXCLUDE_FROM_FORWARD_PROPS_KEYS = "exclude_from_forward_props_keys";
397
485
  COMPLEX_HEADER,
398
486
  EXCLUDE_FROM_FORWARD_PROPS_KEYS,
399
487
  StreamStringPrefixes,
488
+ actionToChatCompletionFunction,
400
489
  annotatedFunctionToChatCompletionFunction,
401
490
  decodeChatCompletion,
402
491
  decodeChatCompletionAsText,
492
+ decodeResult,
403
493
  encodeResult,
404
494
  formatStreamPart,
405
495
  isStreamStringEqualToType,