@copilotkit/react-core 0.18.0-alpha.1 → 0.18.0-alpha.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 (158) hide show
  1. package/.turbo/turbo-build.log +184 -122
  2. package/CHANGELOG.md +14 -1
  3. package/dist/{chunk-6A4PCNMZ.mjs → chunk-4CFJ6A6A.mjs} +4 -10
  4. package/dist/chunk-4CFJ6A6A.mjs.map +1 -0
  5. package/dist/{chunk-FY7XNRJP.mjs → chunk-5GS7FQPW.mjs} +4 -4
  6. package/dist/chunk-5GS7FQPW.mjs.map +1 -0
  7. package/dist/{chunk-FP4EXCGS.mjs → chunk-DGUVNY3V.mjs} +2 -2
  8. package/dist/chunk-DGUVNY3V.mjs.map +1 -0
  9. package/dist/{chunk-D4NQCVHE.mjs → chunk-GBZZPQ5X.mjs} +11 -38
  10. package/dist/chunk-GBZZPQ5X.mjs.map +1 -0
  11. package/dist/chunk-IQEWY7P4.mjs +102 -0
  12. package/dist/chunk-IQEWY7P4.mjs.map +1 -0
  13. package/dist/{chunk-7P4OYNP3.mjs → chunk-IQH5AQQC.mjs} +2 -2
  14. package/dist/chunk-UAKLDQMP.mjs +61 -0
  15. package/dist/chunk-UAKLDQMP.mjs.map +1 -0
  16. package/dist/{chunk-P34OLW2N.mjs → chunk-VP2EU4US.mjs} +3 -3
  17. package/dist/{chunk-HW5IH6PV.mjs → chunk-YOXWT7I6.mjs} +2 -2
  18. package/dist/components/copilot-provider/copilotkit-props.d.ts +39 -0
  19. package/dist/components/copilot-provider/copilotkit-props.js +4 -0
  20. package/dist/components/copilot-provider/copilotkit-props.mjs +3 -0
  21. package/dist/components/copilot-provider/{copilot-provider.d.ts → copilotkit.d.ts} +9 -15
  22. package/dist/components/copilot-provider/copilotkit.js +426 -0
  23. package/dist/components/copilot-provider/copilotkit.js.map +1 -0
  24. package/dist/components/copilot-provider/copilotkit.mjs +8 -0
  25. package/dist/components/copilot-provider/index.d.ts +2 -8
  26. package/dist/components/copilot-provider/index.js +426 -0
  27. package/dist/components/copilot-provider/index.js.map +1 -0
  28. package/dist/components/copilot-provider/index.mjs +2 -2
  29. package/dist/components/copilot-provider/standard-copilot-api-config.d.ts +2 -4
  30. package/dist/components/copilot-provider/standard-copilot-api-config.js +15 -0
  31. package/dist/components/copilot-provider/standard-copilot-api-config.js.map +1 -0
  32. package/dist/components/index.d.ts +2 -8
  33. package/dist/components/index.js +426 -0
  34. package/dist/components/index.js.map +1 -0
  35. package/dist/components/index.mjs +2 -2
  36. package/dist/context/copilot-context.d.ts +3 -5
  37. package/dist/context/copilot-context.js +47 -0
  38. package/dist/context/copilot-context.js.map +1 -0
  39. package/dist/context/copilot-context.mjs +1 -1
  40. package/dist/context/index.d.ts +2 -4
  41. package/dist/context/index.js +47 -0
  42. package/dist/context/index.js.map +1 -0
  43. package/dist/context/index.mjs +1 -1
  44. package/dist/hooks/index.d.ts +2 -4
  45. package/dist/hooks/index.js +358 -0
  46. package/dist/hooks/index.js.map +1 -0
  47. package/dist/hooks/index.mjs +8 -9
  48. package/dist/hooks/use-chat.d.ts +2 -4
  49. package/dist/hooks/use-chat.js +186 -0
  50. package/dist/hooks/use-chat.js.map +1 -0
  51. package/dist/hooks/use-chat.mjs +2 -3
  52. package/dist/hooks/use-copilot-chat.d.ts +2 -4
  53. package/dist/hooks/use-copilot-chat.js +313 -0
  54. package/dist/hooks/use-copilot-chat.js.map +1 -0
  55. package/dist/hooks/use-copilot-chat.mjs +5 -6
  56. package/dist/hooks/use-flat-category-store.js +72 -0
  57. package/dist/hooks/use-flat-category-store.js.map +1 -0
  58. package/dist/hooks/use-make-copilot-actionable.d.ts +1 -1
  59. package/dist/hooks/use-make-copilot-actionable.js +67 -0
  60. package/dist/hooks/use-make-copilot-actionable.js.map +1 -0
  61. package/dist/hooks/use-make-copilot-actionable.mjs +2 -2
  62. package/dist/hooks/use-make-copilot-document-readable.js +61 -0
  63. package/dist/hooks/use-make-copilot-document-readable.js.map +1 -0
  64. package/dist/hooks/use-make-copilot-document-readable.mjs +2 -2
  65. package/dist/hooks/use-make-copilot-readable.js +61 -0
  66. package/dist/hooks/use-make-copilot-readable.js.map +1 -0
  67. package/dist/hooks/use-make-copilot-readable.mjs +2 -2
  68. package/dist/hooks/use-tree.js +155 -0
  69. package/dist/hooks/use-tree.js.map +1 -0
  70. package/dist/index.d.ts +4 -9
  71. package/dist/index.js +838 -0
  72. package/dist/index.js.map +1 -0
  73. package/dist/index.mjs +10 -13
  74. package/dist/openai-assistants/hooks/index.js +225 -0
  75. package/dist/openai-assistants/hooks/index.js.map +1 -0
  76. package/dist/openai-assistants/hooks/index.mjs +3 -3
  77. package/dist/openai-assistants/hooks/use-assistants.js +129 -0
  78. package/dist/openai-assistants/hooks/use-assistants.js.map +1 -0
  79. package/dist/openai-assistants/hooks/use-copilot-chat-v2.js +226 -0
  80. package/dist/openai-assistants/hooks/use-copilot-chat-v2.js.map +1 -0
  81. package/dist/openai-assistants/hooks/use-copilot-chat-v2.mjs +3 -3
  82. package/dist/openai-assistants/index.js +226 -0
  83. package/dist/openai-assistants/index.js.map +1 -0
  84. package/dist/openai-assistants/index.mjs +3 -3
  85. package/dist/openai-assistants/utils/index.js +49 -0
  86. package/dist/openai-assistants/utils/index.js.map +1 -0
  87. package/dist/openai-assistants/utils/process-message-stream.js +49 -0
  88. package/dist/openai-assistants/utils/process-message-stream.js.map +1 -0
  89. package/dist/types/document-pointer.js +4 -0
  90. package/dist/types/index.d.ts +0 -2
  91. package/dist/types/index.js +4 -0
  92. package/dist/types/system-message.js +4 -0
  93. package/dist/utils/fetch-chat-completion.d.ts +28 -0
  94. package/dist/utils/fetch-chat-completion.js +103 -0
  95. package/dist/utils/fetch-chat-completion.js.map +1 -0
  96. package/dist/utils/fetch-chat-completion.mjs +4 -0
  97. package/dist/utils/utils.js +4 -0
  98. package/dist/utils/utils.test.js +10 -0
  99. package/dist/utils/utils.test.js.map +1 -0
  100. package/package.json +8 -8
  101. package/src/components/copilot-provider/copilotkit-props.tsx +42 -0
  102. package/src/components/copilot-provider/{copilot-provider.tsx → copilotkit.tsx} +22 -57
  103. package/src/components/copilot-provider/index.ts +2 -6
  104. package/src/context/copilot-context.tsx +5 -13
  105. package/src/hooks/use-chat.ts +54 -72
  106. package/src/hooks/use-copilot-chat.ts +4 -4
  107. package/src/hooks/use-make-copilot-actionable.ts +1 -1
  108. package/src/index.tsx +6 -1
  109. package/src/types/index.ts +0 -3
  110. package/src/utils/fetch-chat-completion.ts +95 -0
  111. package/tsup.config.ts +1 -2
  112. package/dist/chunk-6A4PCNMZ.mjs.map +0 -1
  113. package/dist/chunk-6SOQYBNX.mjs +0 -44
  114. package/dist/chunk-6SOQYBNX.mjs.map +0 -1
  115. package/dist/chunk-D4NQCVHE.mjs.map +0 -1
  116. package/dist/chunk-FP4EXCGS.mjs.map +0 -1
  117. package/dist/chunk-FY7XNRJP.mjs.map +0 -1
  118. package/dist/chunk-LMZN4NSM.mjs +0 -113
  119. package/dist/chunk-LMZN4NSM.mjs.map +0 -1
  120. package/dist/chunk-PUFR5OAI.mjs +0 -3
  121. package/dist/chunk-TKXPMMND.mjs +0 -177
  122. package/dist/chunk-TKXPMMND.mjs.map +0 -1
  123. package/dist/chunk-VK25FIML.mjs +0 -144
  124. package/dist/chunk-VK25FIML.mjs.map +0 -1
  125. package/dist/components/copilot-provider/copilot-provider-props.d.ts +0 -50
  126. package/dist/components/copilot-provider/copilot-provider-props.mjs +0 -3
  127. package/dist/components/copilot-provider/copilot-provider.mjs +0 -8
  128. package/dist/openai/chat-completion-client.d.ts +0 -52
  129. package/dist/openai/chat-completion-client.mjs +0 -5
  130. package/dist/openai/chat-completion-stream.d.ts +0 -21
  131. package/dist/openai/chat-completion-stream.mjs +0 -5
  132. package/dist/openai/chat-completion-transport.d.ts +0 -38
  133. package/dist/openai/chat-completion-transport.mjs +0 -4
  134. package/dist/openai/index.d.ts +0 -10
  135. package/dist/openai/index.mjs +0 -6
  136. package/dist/types/annotated-function.d.ts +0 -24
  137. package/dist/types/annotated-function.mjs +0 -3
  138. package/dist/types/base.d.ts +0 -56
  139. package/dist/types/base.mjs +0 -3
  140. package/dist/types/base.mjs.map +0 -1
  141. package/src/components/copilot-provider/copilot-provider-props.tsx +0 -50
  142. package/src/openai/chat-completion-client.ts +0 -229
  143. package/src/openai/chat-completion-stream.ts +0 -54
  144. package/src/openai/chat-completion-transport.ts +0 -186
  145. package/src/openai/index.tsx +0 -5
  146. package/src/types/annotated-function.ts +0 -27
  147. package/src/types/base.ts +0 -61
  148. /package/dist/{chunk-7P4OYNP3.mjs.map → chunk-IQH5AQQC.mjs.map} +0 -0
  149. /package/dist/{chunk-P34OLW2N.mjs.map → chunk-VP2EU4US.mjs.map} +0 -0
  150. /package/dist/{chunk-HW5IH6PV.mjs.map → chunk-YOXWT7I6.mjs.map} +0 -0
  151. /package/dist/{chunk-PUFR5OAI.mjs.map → components/copilot-provider/copilotkit-props.js.map} +0 -0
  152. /package/dist/components/copilot-provider/{copilot-provider-props.mjs.map → copilotkit-props.mjs.map} +0 -0
  153. /package/dist/components/copilot-provider/{copilot-provider.mjs.map → copilotkit.mjs.map} +0 -0
  154. /package/dist/{openai/chat-completion-client.mjs.map → types/document-pointer.js.map} +0 -0
  155. /package/dist/{openai/chat-completion-stream.mjs.map → types/index.js.map} +0 -0
  156. /package/dist/{openai/chat-completion-transport.mjs.map → types/system-message.js.map} +0 -0
  157. /package/dist/{openai/index.mjs.map → utils/fetch-chat-completion.mjs.map} +0 -0
  158. /package/dist/{types/annotated-function.mjs.map → utils/utils.js.map} +0 -0
@@ -0,0 +1,49 @@
1
+ 'use strict';
2
+
3
+ var __async = (__this, __arguments, generator) => {
4
+ return new Promise((resolve, reject) => {
5
+ var fulfilled = (value) => {
6
+ try {
7
+ step(generator.next(value));
8
+ } catch (e) {
9
+ reject(e);
10
+ }
11
+ };
12
+ var rejected = (value) => {
13
+ try {
14
+ step(generator.throw(value));
15
+ } catch (e) {
16
+ reject(e);
17
+ }
18
+ };
19
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
20
+ step((generator = generator.apply(__this, __arguments)).next());
21
+ });
22
+ };
23
+
24
+ // src/openai-assistants/utils/process-message-stream.ts
25
+ function processMessageStream(reader, processMessage) {
26
+ return __async(this, null, function* () {
27
+ const decoder = new TextDecoder();
28
+ let buffer = "";
29
+ while (true) {
30
+ const { done, value } = yield reader.read();
31
+ if (done) {
32
+ if (buffer.length > 0) {
33
+ processMessage(buffer);
34
+ }
35
+ break;
36
+ }
37
+ buffer += decoder.decode(value, { stream: true });
38
+ let endIndex;
39
+ while ((endIndex = buffer.indexOf("\n")) !== -1) {
40
+ processMessage(buffer.substring(0, endIndex).trim());
41
+ buffer = buffer.substring(endIndex + 1);
42
+ }
43
+ }
44
+ });
45
+ }
46
+
47
+ exports.processMessageStream = processMessageStream;
48
+ //# sourceMappingURL=out.js.map
49
+ //# sourceMappingURL=process-message-stream.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../src/openai-assistants/utils/process-message-stream.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAAA,SAAsB,qBACpB,QACA,gBACA;AAAA;AACA,UAAM,UAAU,IAAI,YAAY;AAChC,QAAI,SAAS;AACb,WAAO,MAAM;AACX,YAAM,EAAE,MAAM,MAAM,IAAI,MAAM,OAAO,KAAK;AAE1C,UAAI,MAAM;AACR,YAAI,OAAO,SAAS,GAAG;AACrB,yBAAe,MAAM;AAAA,QACvB;AACA;AAAA,MACF;AAEA,gBAAU,QAAQ,OAAO,OAAO,EAAE,QAAQ,KAAK,CAAC;AAEhD,UAAI;AACJ,cAAQ,WAAW,OAAO,QAAQ,IAAI,OAAO,IAAI;AAC/C,uBAAe,OAAO,UAAU,GAAG,QAAQ,EAAE,KAAK,CAAC;AACnD,iBAAS,OAAO,UAAU,WAAW,CAAC;AAAA,MACxC;AAAA,IACF;AAAA,EACF;AAAA","sourcesContent":["export async function processMessageStream(\n reader: ReadableStreamDefaultReader<Uint8Array>,\n processMessage: (message: string) => void | Promise<void>,\n) {\n const decoder = new TextDecoder();\n let buffer = \"\";\n while (true) {\n const { done, value } = await reader.read();\n\n if (done) {\n if (buffer.length > 0) {\n processMessage(buffer);\n }\n break;\n }\n\n buffer += decoder.decode(value, { stream: true });\n\n let endIndex: number;\n while ((endIndex = buffer.indexOf(\"\\n\")) !== -1) {\n processMessage(buffer.substring(0, endIndex).trim());\n buffer = buffer.substring(endIndex + 1); // Remove the processed instruction + delimiter\n }\n }\n}\n"]}
@@ -0,0 +1,4 @@
1
+ 'use strict';
2
+
3
+ //# sourceMappingURL=out.js.map
4
+ //# sourceMappingURL=document-pointer.js.map
@@ -1,4 +1,2 @@
1
- export { AnnotatedFunction, AnnotatedFunctionArgument } from './annotated-function.js';
2
1
  export { DocumentPointer } from './document-pointer.js';
3
- export { Function, FunctionCall, FunctionCallHandler, Message, Role } from './base.js';
4
2
  export { SystemMessageFunction } from './system-message.js';
@@ -0,0 +1,4 @@
1
+ 'use strict';
2
+
3
+ //# sourceMappingURL=out.js.map
4
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,4 @@
1
+ 'use strict';
2
+
3
+ //# sourceMappingURL=out.js.map
4
+ //# sourceMappingURL=system-message.js.map
@@ -0,0 +1,28 @@
1
+ import { Message, Function, ChatCompletionEvent } from '@copilotkit/shared';
2
+ import { CopilotApiConfig } from '../context/copilot-context.js';
3
+ import 'react';
4
+ import '../hooks/use-tree.js';
5
+ import '../types/document-pointer.js';
6
+
7
+ interface FetchChatCompletionParams {
8
+ copilotConfig: CopilotApiConfig;
9
+ model?: string;
10
+ messages: Message[];
11
+ functions?: Function[];
12
+ temperature?: number;
13
+ maxTokens?: number;
14
+ headers?: Record<string, string> | Headers;
15
+ body?: object;
16
+ signal?: AbortSignal;
17
+ }
18
+ declare function fetchChatCompletion({ copilotConfig, model, messages, functions, temperature, headers, body, signal, }: FetchChatCompletionParams): Promise<Response>;
19
+ interface DecodedChatCompletionResponse extends Response {
20
+ events: ReadableStream<ChatCompletionEvent> | null;
21
+ }
22
+ declare function fetchAndDecodeChatCompletion(params: FetchChatCompletionParams): Promise<DecodedChatCompletionResponse>;
23
+ interface DecodedChatCompletionResponseAsText extends Response {
24
+ events: ReadableStream<string> | null;
25
+ }
26
+ declare function fetchAndDecodeChatCompletionAsText(params: FetchChatCompletionParams): Promise<DecodedChatCompletionResponseAsText>;
27
+
28
+ export { DecodedChatCompletionResponse, DecodedChatCompletionResponseAsText, FetchChatCompletionParams, fetchAndDecodeChatCompletion, fetchAndDecodeChatCompletionAsText, fetchChatCompletion };
@@ -0,0 +1,103 @@
1
+ 'use strict';
2
+
3
+ var shared = require('@copilotkit/shared');
4
+
5
+ var __defProp = Object.defineProperty;
6
+ var __defProps = Object.defineProperties;
7
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
8
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
9
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
10
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
11
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
12
+ var __spreadValues = (a, b) => {
13
+ for (var prop in b || (b = {}))
14
+ if (__hasOwnProp.call(b, prop))
15
+ __defNormalProp(a, prop, b[prop]);
16
+ if (__getOwnPropSymbols)
17
+ for (var prop of __getOwnPropSymbols(b)) {
18
+ if (__propIsEnum.call(b, prop))
19
+ __defNormalProp(a, prop, b[prop]);
20
+ }
21
+ return a;
22
+ };
23
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
24
+ var __async = (__this, __arguments, generator) => {
25
+ return new Promise((resolve, reject) => {
26
+ var fulfilled = (value) => {
27
+ try {
28
+ step(generator.next(value));
29
+ } catch (e) {
30
+ reject(e);
31
+ }
32
+ };
33
+ var rejected = (value) => {
34
+ try {
35
+ step(generator.throw(value));
36
+ } catch (e) {
37
+ reject(e);
38
+ }
39
+ };
40
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
41
+ step((generator = generator.apply(__this, __arguments)).next());
42
+ });
43
+ };
44
+ function fetchChatCompletion(_0) {
45
+ return __async(this, arguments, function* ({
46
+ copilotConfig,
47
+ model,
48
+ messages,
49
+ functions,
50
+ temperature,
51
+ headers,
52
+ body,
53
+ signal
54
+ }) {
55
+ temperature || (temperature = 0.5);
56
+ functions || (functions = []);
57
+ const cleanedMessages = messages.map((message) => {
58
+ const { content, role, name, function_call } = message;
59
+ return { content, role, name, function_call };
60
+ });
61
+ const response = yield fetch(copilotConfig.chatApiEndpoint, {
62
+ method: "POST",
63
+ headers: __spreadValues(__spreadValues({
64
+ "Content-Type": "application/json"
65
+ }, copilotConfig.headers), headers ? __spreadValues({}, headers) : {}),
66
+ body: JSON.stringify(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues({
67
+ model,
68
+ messages: cleanedMessages,
69
+ stream: true
70
+ }, functions.length ? { functions } : {}), temperature ? { temperature } : {}), functions.length != 0 ? { function_call: "auto" } : {}), copilotConfig.body), body ? __spreadValues({}, body) : {})),
71
+ signal
72
+ });
73
+ return response;
74
+ });
75
+ }
76
+ function fetchAndDecodeChatCompletion(params) {
77
+ return __async(this, null, function* () {
78
+ const response = yield fetchChatCompletion(params);
79
+ if (!response.ok || !response.body) {
80
+ return __spreadProps(__spreadValues({}, response), { events: null });
81
+ }
82
+ const events = yield shared.decodeChatCompletion(shared.parseChatCompletion(response.body));
83
+ return __spreadProps(__spreadValues({}, response), { events });
84
+ });
85
+ }
86
+ function fetchAndDecodeChatCompletionAsText(params) {
87
+ return __async(this, null, function* () {
88
+ const response = yield fetchChatCompletion(params);
89
+ if (!response.ok || !response.body) {
90
+ return __spreadProps(__spreadValues({}, response), { events: null });
91
+ }
92
+ const events = yield shared.decodeChatCompletionAsText(
93
+ shared.decodeChatCompletion(shared.parseChatCompletion(response.body))
94
+ );
95
+ return __spreadProps(__spreadValues({}, response), { events });
96
+ });
97
+ }
98
+
99
+ exports.fetchAndDecodeChatCompletion = fetchAndDecodeChatCompletion;
100
+ exports.fetchAndDecodeChatCompletionAsText = fetchAndDecodeChatCompletionAsText;
101
+ exports.fetchChatCompletion = fetchChatCompletion;
102
+ //# sourceMappingURL=out.js.map
103
+ //# sourceMappingURL=fetch-chat-completion.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/utils/fetch-chat-completion.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA,EAIE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAeP,SAAsB,oBAAoB,IASO;AAAA,6CATP;AAAA,IACxC;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,GAAiD;AAC/C,kCAAgB;AAChB,8BAAc,CAAC;AAGf,UAAM,kBAAkB,SAAS,IAAI,CAAC,YAAY;AAChD,YAAM,EAAE,SAAS,MAAM,MAAM,cAAc,IAAI;AAC/C,aAAO,EAAE,SAAS,MAAM,MAAM,cAAc;AAAA,IAC9C,CAAC;AAED,UAAM,WAAW,MAAM,MAAM,cAAc,iBAAiB;AAAA,MAC1D,QAAQ;AAAA,MACR,SAAS;AAAA,QACP,gBAAgB;AAAA,SACb,cAAc,UACb,UAAU,mBAAK,WAAY,CAAC;AAAA,MAElC,MAAM,KAAK,UAAU;AAAA,QACnB;AAAA,QACA,UAAU;AAAA,QACV,QAAQ;AAAA,SACJ,UAAU,SAAS,EAAE,UAAU,IAAI,CAAC,IACpC,cAAc,EAAE,YAAY,IAAI,CAAC,IACjC,UAAU,UAAU,IAAI,EAAE,eAAe,OAAO,IAAI,CAAC,IACtD,cAAc,OACb,OAAO,mBAAK,QAAS,CAAC,EAC3B;AAAA,MACD;AAAA,IACF,CAAC;AAED,WAAO;AAAA,EACT;AAAA;AAMA,SAAsB,6BACpB,QACwC;AAAA;AACxC,UAAM,WAAW,MAAM,oBAAoB,MAAM;AACjD,QAAI,CAAC,SAAS,MAAM,CAAC,SAAS,MAAM;AAClC,aAAO,iCAAK,WAAL,EAAe,QAAQ,KAAK;AAAA,IACrC;AACA,UAAM,SAAS,MAAM,qBAAqB,oBAAoB,SAAS,IAAI,CAAC;AAC5E,WAAO,iCAAK,WAAL,EAAe,OAAO;AAAA,EAC/B;AAAA;AAMA,SAAsB,mCACpB,QAC8C;AAAA;AAC9C,UAAM,WAAW,MAAM,oBAAoB,MAAM;AACjD,QAAI,CAAC,SAAS,MAAM,CAAC,SAAS,MAAM;AAClC,aAAO,iCAAK,WAAL,EAAe,QAAQ,KAAK;AAAA,IACrC;AACA,UAAM,SAAS,MAAM;AAAA,MACnB,qBAAqB,oBAAoB,SAAS,IAAI,CAAC;AAAA,IACzD;AACA,WAAO,iCAAK,WAAL,EAAe,OAAO;AAAA,EAC/B;AAAA","sourcesContent":["import {\n Message,\n Function,\n ChatCompletionEvent,\n decodeChatCompletion,\n parseChatCompletion,\n decodeChatCompletionAsText,\n} from \"@copilotkit/shared\";\nimport { CopilotApiConfig } from \"../context\";\n\nexport interface FetchChatCompletionParams {\n copilotConfig: CopilotApiConfig;\n model?: string;\n messages: Message[];\n functions?: Function[];\n temperature?: number;\n maxTokens?: number;\n headers?: Record<string, string> | Headers;\n body?: object;\n signal?: AbortSignal;\n}\n\nexport async function fetchChatCompletion({\n copilotConfig,\n model,\n messages,\n functions,\n temperature,\n headers,\n body,\n signal,\n}: FetchChatCompletionParams): Promise<Response> {\n temperature ||= 0.5;\n functions ||= [];\n\n // clean up any extra properties from messages\n const cleanedMessages = messages.map((message) => {\n const { content, role, name, function_call } = message;\n return { content, role, name, function_call };\n });\n\n const response = await fetch(copilotConfig.chatApiEndpoint, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n ...copilotConfig.headers,\n ...(headers ? { ...headers } : {}),\n },\n body: JSON.stringify({\n model,\n messages: cleanedMessages,\n stream: true,\n ...(functions.length ? { functions } : {}),\n ...(temperature ? { temperature } : {}),\n ...(functions.length != 0 ? { function_call: \"auto\" } : {}),\n ...copilotConfig.body,\n ...(body ? { ...body } : {}),\n }),\n signal,\n });\n\n return response;\n}\n\nexport interface DecodedChatCompletionResponse extends Response {\n events: ReadableStream<ChatCompletionEvent> | null;\n}\n\nexport async function fetchAndDecodeChatCompletion(\n params: FetchChatCompletionParams,\n): Promise<DecodedChatCompletionResponse> {\n const response = await fetchChatCompletion(params);\n if (!response.ok || !response.body) {\n return { ...response, events: null };\n }\n const events = await decodeChatCompletion(parseChatCompletion(response.body));\n return { ...response, events };\n}\n\nexport interface DecodedChatCompletionResponseAsText extends Response {\n events: ReadableStream<string> | null;\n}\n\nexport async function fetchAndDecodeChatCompletionAsText(\n params: FetchChatCompletionParams,\n): Promise<DecodedChatCompletionResponseAsText> {\n const response = await fetchChatCompletion(params);\n if (!response.ok || !response.body) {\n return { ...response, events: null };\n }\n const events = await decodeChatCompletionAsText(\n decodeChatCompletion(parseChatCompletion(response.body)),\n );\n return { ...response, events };\n}\n"]}
@@ -0,0 +1,4 @@
1
+ export { fetchAndDecodeChatCompletion, fetchAndDecodeChatCompletionAsText, fetchChatCompletion } from '../chunk-UAKLDQMP.mjs';
2
+ import '../chunk-MRXNTQOX.mjs';
3
+ //# sourceMappingURL=out.js.map
4
+ //# sourceMappingURL=fetch-chat-completion.mjs.map
@@ -0,0 +1,4 @@
1
+ 'use strict';
2
+
3
+ //# sourceMappingURL=out.js.map
4
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1,10 @@
1
+ 'use strict';
2
+
3
+ // src/utils/utils.test.ts
4
+ describe("emptyTest", () => {
5
+ it("should be truthy", () => {
6
+ expect(true).toBeTruthy();
7
+ });
8
+ });
9
+ //# sourceMappingURL=out.js.map
10
+ //# sourceMappingURL=utils.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/utils/utils.test.ts"],"names":[],"mappings":";AAEA,SAAS,aAAa,MAAM;AAC1B,KAAG,oBAAoB,MAAM;AAC3B,WAAO,IAAI,EAAE,WAAW;AAAA,EAC1B,CAAC;AACH,CAAC","sourcesContent":["import * as utils from \"./utils\";\n\ndescribe(\"emptyTest\", () => {\n it(\"should be truthy\", () => {\n expect(true).toBeTruthy();\n });\n});\n"]}
package/package.json CHANGED
@@ -4,11 +4,13 @@
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
- "version": "0.18.0-alpha.1",
7
+ "version": "0.18.0-alpha.2",
8
8
  "sideEffects": false,
9
9
  "main": "./dist/index.js",
10
10
  "module": "./dist/index.mjs",
11
- "exports": "./dist/index.mjs",
11
+ "exports": {
12
+ ".": "./dist/index.js"
13
+ },
12
14
  "types": "./dist/index.d.ts",
13
15
  "license": "MIT",
14
16
  "peerDependencies": {
@@ -23,17 +25,15 @@
23
25
  "ts-jest": "^29.1.1",
24
26
  "tsup": "^6.7.0",
25
27
  "typescript": "^5.1.3",
26
- "tsconfig": "0.7.0-alpha.0",
27
- "eslint-config-custom": "0.3.0-alpha.0"
28
+ "tsconfig": "0.7.0-alpha.1",
29
+ "eslint-config-custom": "0.3.0-alpha.1"
28
30
  },
29
31
  "dependencies": {
30
- "eventemitter3": "^5.0.1",
31
32
  "nanoid": "^4.0.2",
32
- "openai": "^4.6.0",
33
- "@copilotkit/shared": "0.2.0-alpha.0"
33
+ "@copilotkit/shared": "0.2.0-alpha.1"
34
34
  },
35
35
  "scripts": {
36
- "build": "tsup --treeshake",
36
+ "build": "tsup --treeshake --clean",
37
37
  "dev": "tsup --watch --no-splitting",
38
38
  "test": "jest",
39
39
  "check-types": "tsc --noEmit",
@@ -0,0 +1,42 @@
1
+ "use client";
2
+ import { ReactNode } from "react";
3
+
4
+ /**
5
+ * Props for CopilotKit.
6
+ */
7
+
8
+ export interface CopilotKitProps {
9
+ /**
10
+ * The endpoint for the chat API.
11
+ */
12
+ url: string;
13
+
14
+ /**
15
+ * additional headers to be sent with the request
16
+ * @default {}
17
+ * @example
18
+ * ```
19
+ * {
20
+ * 'Authorization': 'Bearer your_token_here'
21
+ * }
22
+ * ```
23
+ */
24
+ headers?: Record<string, string>;
25
+
26
+ /**
27
+ * Additional body params to be sent with the request
28
+ * @default {}
29
+ * @example
30
+ * ```
31
+ * {
32
+ * 'message': 'Hello, world!'
33
+ * }
34
+ * ```
35
+ */
36
+ body?: Record<string, any>;
37
+
38
+ /**
39
+ * The children to be rendered within the CopilotKit.
40
+ */
41
+ children: ReactNode;
42
+ }
@@ -1,17 +1,21 @@
1
1
  "use client";
2
2
 
3
+ import { Function } from "@copilotkit/shared";
3
4
  import { useCallback, useState } from "react";
4
5
  import { CopilotContext, CopilotApiConfig } from "../../context/copilot-context";
5
6
  import useTree from "../../hooks/use-tree";
6
- import { AnnotatedFunction } from "../../types/annotated-function";
7
- import { ChatCompletionCreateParams } from "openai/resources/chat";
8
- import { DocumentPointer, FunctionCallHandler } from "../../types";
7
+ import { DocumentPointer } from "../../types";
8
+ import {
9
+ FunctionCallHandler,
10
+ AnnotatedFunction,
11
+ annotatedFunctionToChatCompletionFunction,
12
+ } from "@copilotkit/shared";
9
13
  import useFlatCategoryStore from "../../hooks/use-flat-category-store";
10
14
  import { StandardCopilotApiConfig } from "./standard-copilot-api-config";
11
- import { CopilotProviderProps } from "./copilot-provider-props";
15
+ import { CopilotKitProps } from "./copilotkit-props";
12
16
 
13
17
  /**
14
- * The CopilotProvider component.
18
+ * The CopilotKit component.
15
19
  * This component provides the Copilot context to its children.
16
20
  * It can be configured either with a chat API endpoint or a CopilotApiConfig.
17
21
  *
@@ -22,9 +26,9 @@ import { CopilotProviderProps } from "./copilot-provider-props";
22
26
  *
23
27
  * Example usage:
24
28
  * ```
25
- * <CopilotProvider chatApiEndpoint="https://your.copilotkit.api">
29
+ * <CopilotKit url="https://your.copilotkit.api">
26
30
  * <App />
27
- * </CopilotProvider>
31
+ * </CopilotKit>
28
32
  * ```
29
33
  *
30
34
  * or
@@ -39,15 +43,15 @@ import { CopilotProviderProps } from "./copilot-provider-props";
39
43
  *
40
44
  * // ...
41
45
  *
42
- * <CopilotProvider chatApiConfig={copilotApiConfig}>
46
+ * <CopilotKit chatApiConfig={copilotApiConfig}>
43
47
  * <App />
44
- * </CopilotProvider>
48
+ * </CopilotKit>
45
49
  * ```
46
50
  *
47
51
  * @param props - The props for the component.
48
- * @returns The CopilotProvider component.
52
+ * @returns The CopilotKit component.
49
53
  */
50
- export function CopilotProvider({ children, ...props }: CopilotProviderProps): JSX.Element {
54
+ export function CopilotKit({ children, ...props }: CopilotKitProps) {
51
55
  // Compute all the functions and properties that we need to pass
52
56
  // to the CopilotContext.
53
57
 
@@ -141,17 +145,12 @@ export function CopilotProvider({ children, ...props }: CopilotProviderProps): J
141
145
  );
142
146
 
143
147
  // get the appropriate CopilotApiConfig from the props
144
- let copilotApiConfig: CopilotApiConfig;
145
- if ("chatApiEndpoint" in props) {
146
- copilotApiConfig = new StandardCopilotApiConfig(
147
- props.chatApiEndpoint,
148
- props.chatApiEndpointV2 || `${props.chatApiEndpoint}/v2`,
149
- {},
150
- {},
151
- );
152
- } else {
153
- copilotApiConfig = props.chatApiConfig;
154
- }
148
+ const copilotApiConfig: CopilotApiConfig = new StandardCopilotApiConfig(
149
+ props.url,
150
+ `${props.url}/v2`,
151
+ props.headers || {},
152
+ props.body || {},
153
+ );
155
154
 
156
155
  return (
157
156
  <CopilotContext.Provider
@@ -223,40 +222,6 @@ function entryPointsToFunctionCallHandler(
223
222
  };
224
223
  }
225
224
 
226
- function entryPointsToChatCompletionFunctions(
227
- entryPoints: AnnotatedFunction<any[]>[],
228
- ): ChatCompletionCreateParams.Function[] {
225
+ function entryPointsToChatCompletionFunctions(entryPoints: AnnotatedFunction<any[]>[]): Function[] {
229
226
  return entryPoints.map(annotatedFunctionToChatCompletionFunction);
230
227
  }
231
-
232
- function annotatedFunctionToChatCompletionFunction(
233
- annotatedFunction: AnnotatedFunction<any[]>,
234
- ): ChatCompletionCreateParams.Function {
235
- // Create the parameters object based on the argumentAnnotations
236
- let parameters: { [key: string]: any } = {};
237
- for (let arg of annotatedFunction.argumentAnnotations) {
238
- // isolate the args we should forward inline
239
- let { name, required, ...forwardedArgs } = arg;
240
- parameters[arg.name] = forwardedArgs;
241
- }
242
-
243
- let requiredParameterNames: string[] = [];
244
- for (let arg of annotatedFunction.argumentAnnotations) {
245
- if (arg.required) {
246
- requiredParameterNames.push(arg.name);
247
- }
248
- }
249
-
250
- // Create the ChatCompletionFunctions object
251
- let chatCompletionFunction: ChatCompletionCreateParams.Function = {
252
- name: annotatedFunction.name,
253
- description: annotatedFunction.description,
254
- parameters: {
255
- type: "object",
256
- properties: parameters,
257
- required: requiredParameterNames,
258
- },
259
- };
260
-
261
- return chatCompletionFunction;
262
- }
@@ -1,7 +1,3 @@
1
- export { CopilotProvider, defaultCopilotContextCategories } from "./copilot-provider";
1
+ export { CopilotKit, defaultCopilotContextCategories } from "./copilotkit";
2
2
 
3
- export type {
4
- CopilotProviderApiEndpointProps,
5
- CopilotProviderApiConfigProps,
6
- CopilotProviderProps,
7
- } from "./copilot-provider-props";
3
+ export type { CopilotKitProps } from "./copilotkit-props";
@@ -1,10 +1,8 @@
1
1
  "use client";
2
2
 
3
- import { FunctionCallHandler } from "../types";
3
+ import { FunctionCallHandler, AnnotatedFunction, Function } from "@copilotkit/shared";
4
4
  import React from "react";
5
5
  import { TreeNodeId } from "../hooks/use-tree";
6
- import { AnnotatedFunction } from "../types/annotated-function";
7
- import { ChatCompletionCreateParams } from "openai/resources/chat";
8
6
  import { DocumentPointer } from "../types";
9
7
 
10
8
  /**
@@ -51,7 +49,7 @@ export interface CopilotContextParams {
51
49
  entryPoints: Record<string, AnnotatedFunction<any[]>>;
52
50
  setEntryPoint: (id: string, entryPoint: AnnotatedFunction<any[]>) => void;
53
51
  removeEntryPoint: (id: string) => void;
54
- getChatCompletionFunctionDescriptions: () => ChatCompletionCreateParams.Function[];
52
+ getChatCompletionFunctionDescriptions: () => Function[];
55
53
  getFunctionCallHandler: () => FunctionCallHandler;
56
54
 
57
55
  // text context
@@ -86,14 +84,10 @@ const emptyCopilotContext: CopilotContextParams = {
86
84
 
87
85
  copilotApiConfig: new (class implements CopilotApiConfig {
88
86
  get chatApiEndpoint(): string {
89
- throw new Error(
90
- "Remember to wrap your app in a `<CopilotProvider> {...} </CopilotProvider>` !!!",
91
- );
87
+ throw new Error("Remember to wrap your app in a `<CopilotKit> {...} </CopilotKit>` !!!");
92
88
  }
93
89
  get chatApiEndpointV2(): string {
94
- throw new Error(
95
- "Remember to wrap your app in a `<CopilotProvider> {...} </CopilotProvider>` !!!",
96
- );
90
+ throw new Error("Remember to wrap your app in a `<CopilotKit> {...} </CopilotKit>` !!!");
97
91
  }
98
92
  get headers(): Record<string, string> {
99
93
  return {};
@@ -107,8 +101,6 @@ const emptyCopilotContext: CopilotContextParams = {
107
101
  export const CopilotContext = React.createContext<CopilotContextParams>(emptyCopilotContext);
108
102
 
109
103
  function returnAndThrowInDebug<T>(value: T): T {
110
- throw new Error(
111
- "Remember to wrap your app in a `<CopilotProvider> {...} </CopilotProvider>` !!!",
112
- );
104
+ throw new Error("Remember to wrap your app in a `<CopilotKit> {...} </CopilotKit>` !!!");
113
105
  return value;
114
106
  }
@@ -1,7 +1,7 @@
1
1
  import { useRef, useState } from "react";
2
- import { Message, Function, FunctionCallHandler } from "../types";
2
+ import { Message, Function, FunctionCallHandler, FunctionCall } from "@copilotkit/shared";
3
3
  import { nanoid } from "nanoid";
4
- import { ChatCompletionClient } from "../openai/chat-completion-client";
4
+ import { fetchAndDecodeChatCompletion } from "../utils/fetch-chat-completion";
5
5
  import { CopilotApiConfig } from "../context";
6
6
 
7
7
  export type UseChatOptions = {
@@ -87,84 +87,66 @@ export function useChat(options: UseChatOptionsWithCopilotConfig): UseChatHelper
87
87
  const abortControllerRef = useRef<AbortController>();
88
88
 
89
89
  const runChatCompletion = async (messages: Message[]): Promise<Message> => {
90
- return new Promise<Message>((resolve, reject) => {
91
- setIsLoading(true);
92
-
93
- const abortController = new AbortController();
94
- abortControllerRef.current = abortController;
95
-
96
- const assistantMessage: Message = {
97
- id: nanoid(),
98
- createdAt: new Date(),
99
- content: "",
100
- role: "assistant",
101
- };
102
-
103
- // Assistant messages are always copied when using setState
104
- setMessages([...messages, { ...assistantMessage }]);
105
-
106
- const messagesWithContext = [...(options.initialMessages || []), ...messages];
107
-
108
- const client = new ChatCompletionClient({});
109
-
110
- const cleanup = () => {
111
- client.off("content");
112
- client.off("end");
113
- client.off("error");
114
- client.off("function");
115
-
116
- abortControllerRef.current = undefined;
117
- };
118
-
119
- abortController.signal.addEventListener("abort", () => {
120
- setIsLoading(false);
121
- cleanup();
122
- reject(new DOMException("Aborted", "AbortError"));
123
- });
124
-
125
- client.on("content", (content) => {
126
- assistantMessage.content += content;
127
- setMessages([...messages, { ...assistantMessage }]);
128
- });
90
+ setIsLoading(true);
91
+ const assistantMessage: Message = {
92
+ id: nanoid(),
93
+ createdAt: new Date(),
94
+ content: "",
95
+ role: "assistant",
96
+ };
97
+ const abortController = new AbortController();
98
+ abortControllerRef.current = abortController;
99
+
100
+ setMessages([...messages, { ...assistantMessage }]);
101
+
102
+ const messagesWithContext = [...(options.initialMessages || []), ...messages];
103
+ const response = await fetchAndDecodeChatCompletion({
104
+ copilotConfig: options.copilotConfig,
105
+ messages: messagesWithContext,
106
+ functions: options.functions,
107
+ headers: options.headers,
108
+ signal: abortController.signal,
109
+ });
129
110
 
130
- client.on("end", () => {
131
- setIsLoading(false);
132
- cleanup();
133
- resolve({ ...assistantMessage });
134
- });
111
+ if (!response.events) {
112
+ throw new Error("Failed to fetch chat completion");
113
+ }
135
114
 
136
- client.on("error", (error) => {
137
- setIsLoading(false);
138
- cleanup();
139
- reject(error);
140
- });
141
-
142
- client.on("function", async (functionCall) => {
143
- assistantMessage.function_call = {
144
- name: functionCall.name,
145
- arguments: JSON.stringify(functionCall.arguments),
146
- };
147
- setMessages([...messages, { ...assistantMessage }]);
148
- // quit early if we get a function call
115
+ const reader = response.events.getReader();
116
+
117
+ while (true) {
118
+ try {
119
+ const { done, value } = await reader.read();
120
+
121
+ if (done) {
122
+ setIsLoading(false);
123
+ return { ...assistantMessage };
124
+ }
125
+
126
+ if (value.type === "content") {
127
+ assistantMessage.content += value.content;
128
+ setMessages([...messages, { ...assistantMessage }]);
129
+ } else if (value.type === "function") {
130
+ assistantMessage.function_call = {
131
+ name: value.name,
132
+ arguments: JSON.stringify(value.arguments),
133
+ };
134
+ setMessages([...messages, { ...assistantMessage }]);
135
+ // quit early if we get a function call
136
+ setIsLoading(false);
137
+ return { ...assistantMessage };
138
+ }
139
+ } catch (error) {
149
140
  setIsLoading(false);
150
- cleanup();
151
- resolve({ ...assistantMessage });
152
- });
153
-
154
- client.fetch({
155
- copilotConfig: options.copilotConfig,
156
- messages: messagesWithContext,
157
- functions: options.functions,
158
- headers: options.headers,
159
- signal: abortController.signal,
160
- });
161
- });
141
+ throw error;
142
+ }
143
+ }
162
144
  };
163
145
 
164
146
  const runChatCompletionAndHandleFunctionCall = async (messages: Message[]): Promise<void> => {
165
147
  const message = await runChatCompletion(messages);
166
148
  if (message.function_call && options.onFunctionCall) {
167
- await options.onFunctionCall(messages, message.function_call);
149
+ await options.onFunctionCall(messages, message.function_call as FunctionCall);
168
150
  }
169
151
  };
170
152