@copilotkit/react-core 0.14.0 → 0.15.0-alpha.1

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 (113) hide show
  1. package/.turbo/turbo-build.log +141 -88
  2. package/CHANGELOG.md +22 -0
  3. package/dist/components/copilot-provider/copilot-provider-props.d.ts +50 -0
  4. package/dist/components/copilot-provider/copilot-provider-props.mjs +2 -0
  5. package/dist/components/copilot-provider/copilot-provider-props.mjs.map +1 -0
  6. package/dist/components/copilot-provider/copilot-provider.d.ts +45 -0
  7. package/dist/components/copilot-provider/copilot-provider.mjs +469 -0
  8. package/dist/components/copilot-provider/copilot-provider.mjs.map +1 -0
  9. package/dist/components/copilot-provider/index.d.ts +9 -0
  10. package/dist/components/copilot-provider/index.mjs +468 -0
  11. package/dist/components/copilot-provider/index.mjs.map +1 -0
  12. package/dist/components/copilot-provider/standard-cpilot-api-config.d.ts +24 -0
  13. package/dist/components/copilot-provider/standard-cpilot-api-config.mjs +13 -0
  14. package/dist/components/copilot-provider/standard-cpilot-api-config.mjs.map +1 -0
  15. package/dist/components/index.d.ts +8 -1
  16. package/dist/components/index.mjs +467 -7
  17. package/dist/components/index.mjs.map +1 -1
  18. package/dist/context/copilot-context.d.ts +5 -0
  19. package/dist/context/copilot-context.mjs +81 -3
  20. package/dist/context/copilot-context.mjs.map +1 -1
  21. package/dist/context/index.mjs +80 -4
  22. package/dist/context/index.mjs.map +1 -1
  23. package/dist/hooks/index.mjs +249 -12
  24. package/dist/hooks/index.mjs.map +1 -1
  25. package/dist/hooks/use-copilot-chat.mjs +194 -8
  26. package/dist/hooks/use-copilot-chat.mjs.map +1 -1
  27. package/dist/hooks/use-flat-category-store.mjs +68 -3
  28. package/dist/hooks/use-flat-category-store.mjs.map +1 -1
  29. package/dist/hooks/use-make-copilot-actionable.mjs +95 -4
  30. package/dist/hooks/use-make-copilot-actionable.mjs.map +1 -1
  31. package/dist/hooks/use-make-copilot-document-readable.mjs +87 -4
  32. package/dist/hooks/use-make-copilot-document-readable.mjs.map +1 -1
  33. package/dist/hooks/use-make-copilot-readable.mjs +87 -4
  34. package/dist/hooks/use-make-copilot-readable.mjs.map +1 -1
  35. package/dist/hooks/use-tree.mjs +153 -3
  36. package/dist/hooks/use-tree.mjs.map +1 -1
  37. package/dist/index.d.ts +5 -1
  38. package/dist/index.mjs +755 -14
  39. package/dist/index.mjs.map +1 -1
  40. package/dist/openai-assistants/hooks/index.d.ts +2 -0
  41. package/dist/openai-assistants/hooks/index.mjs +246 -0
  42. package/dist/openai-assistants/hooks/index.mjs.map +1 -0
  43. package/dist/openai-assistants/hooks/use-assistants.d.ts +17 -0
  44. package/dist/openai-assistants/hooks/use-assistants.mjs +131 -0
  45. package/dist/openai-assistants/hooks/use-assistants.mjs.map +1 -0
  46. package/dist/openai-assistants/hooks/use-copilot-chat-v2.d.ts +44 -0
  47. package/dist/openai-assistants/hooks/use-copilot-chat-v2.mjs +247 -0
  48. package/dist/openai-assistants/hooks/use-copilot-chat-v2.mjs.map +1 -0
  49. package/dist/openai-assistants/index.d.ts +3 -0
  50. package/dist/openai-assistants/index.mjs +247 -0
  51. package/dist/openai-assistants/index.mjs.map +1 -0
  52. package/dist/openai-assistants/utils/index.d.ts +1 -0
  53. package/dist/openai-assistants/utils/index.mjs +47 -0
  54. package/dist/openai-assistants/utils/index.mjs.map +1 -0
  55. package/dist/openai-assistants/utils/process-message-stream.d.ts +3 -0
  56. package/dist/openai-assistants/utils/process-message-stream.mjs +47 -0
  57. package/dist/openai-assistants/utils/process-message-stream.mjs.map +1 -0
  58. package/dist/types/annotated-function.mjs +0 -2
  59. package/dist/types/annotated-function.mjs.map +1 -1
  60. package/dist/types/document-pointer.mjs +0 -2
  61. package/dist/types/document-pointer.mjs.map +1 -1
  62. package/dist/types/index.mjs +0 -2
  63. package/dist/types/index.mjs.map +1 -1
  64. package/dist/utils/utils.d.ts +1 -0
  65. package/dist/utils/utils.mjs +0 -2
  66. package/dist/utils/utils.mjs.map +1 -1
  67. package/dist/utils/utils.test.d.ts +1 -0
  68. package/dist/utils/utils.test.mjs +0 -1
  69. package/dist/utils/utils.test.mjs.map +1 -1
  70. package/package.json +8 -7
  71. package/src/components/copilot-provider/copilot-provider-props.tsx +50 -0
  72. package/src/components/{copilot-provider.tsx → copilot-provider/copilot-provider.tsx} +60 -23
  73. package/src/components/copilot-provider/index.ts +7 -0
  74. package/src/components/copilot-provider/standard-cpilot-api-config.tsx +28 -0
  75. package/src/components/index.ts +1 -1
  76. package/src/context/copilot-context.tsx +13 -0
  77. package/src/hooks/use-copilot-chat.ts +1 -1
  78. package/src/index.tsx +1 -0
  79. package/src/openai-assistants/hooks/index.ts +9 -0
  80. package/src/openai-assistants/hooks/use-assistants.ts +114 -0
  81. package/src/openai-assistants/hooks/use-copilot-chat-v2.ts +186 -0
  82. package/src/openai-assistants/index.ts +2 -0
  83. package/src/openai-assistants/utils/index.ts +1 -0
  84. package/src/openai-assistants/utils/process-message-stream.ts +25 -0
  85. package/dist/chunk-C4KF43WB.mjs +0 -135
  86. package/dist/chunk-C4KF43WB.mjs.map +0 -1
  87. package/dist/chunk-EFZPSZWO.mjs +0 -3
  88. package/dist/chunk-EFZPSZWO.mjs.map +0 -1
  89. package/dist/chunk-FVU5RFVX.mjs +0 -80
  90. package/dist/chunk-FVU5RFVX.mjs.map +0 -1
  91. package/dist/chunk-HZDMKMAV.mjs +0 -50
  92. package/dist/chunk-HZDMKMAV.mjs.map +0 -1
  93. package/dist/chunk-JD7BAH7U.mjs +0 -3
  94. package/dist/chunk-JD7BAH7U.mjs.map +0 -1
  95. package/dist/chunk-MRXNTQOX.mjs +0 -55
  96. package/dist/chunk-MRXNTQOX.mjs.map +0 -1
  97. package/dist/chunk-QACD2U6P.mjs +0 -3
  98. package/dist/chunk-QACD2U6P.mjs.map +0 -1
  99. package/dist/chunk-RW25IF4S.mjs +0 -19
  100. package/dist/chunk-RW25IF4S.mjs.map +0 -1
  101. package/dist/chunk-UBLDQLZM.mjs +0 -19
  102. package/dist/chunk-UBLDQLZM.mjs.map +0 -1
  103. package/dist/chunk-XFSUVTIQ.mjs +0 -27
  104. package/dist/chunk-XFSUVTIQ.mjs.map +0 -1
  105. package/dist/chunk-YPSGKPDA.mjs +0 -3
  106. package/dist/chunk-YPSGKPDA.mjs.map +0 -1
  107. package/dist/chunk-YULKJPY3.mjs +0 -70
  108. package/dist/chunk-YULKJPY3.mjs.map +0 -1
  109. package/dist/chunk-ZIFCJ774.mjs +0 -158
  110. package/dist/chunk-ZIFCJ774.mjs.map +0 -1
  111. package/dist/components/copilot-provider.d.ts +0 -11
  112. package/dist/components/copilot-provider.mjs +0 -7
  113. package/dist/components/copilot-provider.mjs.map +0 -1
@@ -0,0 +1,47 @@
1
+ var __async = (__this, __arguments, generator) => {
2
+ return new Promise((resolve, reject) => {
3
+ var fulfilled = (value) => {
4
+ try {
5
+ step(generator.next(value));
6
+ } catch (e) {
7
+ reject(e);
8
+ }
9
+ };
10
+ var rejected = (value) => {
11
+ try {
12
+ step(generator.throw(value));
13
+ } catch (e) {
14
+ reject(e);
15
+ }
16
+ };
17
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
18
+ step((generator = generator.apply(__this, __arguments)).next());
19
+ });
20
+ };
21
+
22
+ // src/openai-assistants/utils/process-message-stream.ts
23
+ function processMessageStream(reader, processMessage) {
24
+ return __async(this, null, function* () {
25
+ const decoder = new TextDecoder();
26
+ let buffer = "";
27
+ while (true) {
28
+ const { done, value } = yield reader.read();
29
+ if (done) {
30
+ if (buffer.length > 0) {
31
+ processMessage(buffer);
32
+ }
33
+ break;
34
+ }
35
+ buffer += decoder.decode(value, { stream: true });
36
+ let endIndex;
37
+ while ((endIndex = buffer.indexOf("\n")) !== -1) {
38
+ processMessage(buffer.substring(0, endIndex).trim());
39
+ buffer = buffer.substring(endIndex + 1);
40
+ }
41
+ }
42
+ });
43
+ }
44
+ export {
45
+ processMessageStream
46
+ };
47
+ //# sourceMappingURL=process-message-stream.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../src/openai-assistants/utils/process-message-stream.ts"],"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"],"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;","names":[]}
@@ -1,3 +1 @@
1
-
2
- //# sourceMappingURL=out.js.map
3
1
  //# sourceMappingURL=annotated-function.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":[],"names":[],"mappings":""}
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
@@ -1,3 +1 @@
1
-
2
- //# sourceMappingURL=out.js.map
3
1
  //# sourceMappingURL=document-pointer.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":[],"names":[],"mappings":""}
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
@@ -1,3 +1 @@
1
- import '../chunk-EFZPSZWO.mjs';
2
- //# sourceMappingURL=out.js.map
3
1
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":[],"names":[],"mappings":""}
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
@@ -1 +1,2 @@
1
1
 
2
+ export { }
@@ -1,3 +1 @@
1
-
2
- //# sourceMappingURL=out.js.map
3
1
  //# sourceMappingURL=utils.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":[],"names":[],"mappings":""}
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
@@ -1 +1,2 @@
1
1
 
2
+ export { }
@@ -4,5 +4,4 @@ describe("emptyTest", () => {
4
4
  expect(true).toBeTruthy();
5
5
  });
6
6
  });
7
- //# sourceMappingURL=out.js.map
8
7
  //# sourceMappingURL=utils.test.mjs.map
@@ -1 +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"]}
1
+ {"version":3,"sources":["../../src/utils/utils.test.ts"],"sourcesContent":["import * as utils from \"./utils\";\n\ndescribe(\"emptyTest\", () => {\n it(\"should be truthy\", () => {\n expect(true).toBeTruthy();\n });\n});\n"],"mappings":";AAEA,SAAS,aAAa,MAAM;AAC1B,KAAG,oBAAoB,MAAM;AAC3B,WAAO,IAAI,EAAE,WAAW;AAAA,EAC1B,CAAC;AACH,CAAC;","names":[]}
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
- "version": "0.14.0",
7
+ "version": "0.15.0-alpha.1",
8
8
  "sideEffects": false,
9
9
  "main": "./dist/index.js",
10
10
  "module": "./dist/index.mjs",
@@ -21,15 +21,16 @@
21
21
  "jest": "^29.6.4",
22
22
  "react": "^18.2.0",
23
23
  "ts-jest": "^29.1.1",
24
- "tsup": "^6.5.0",
25
- "typescript": "^4.9.4",
26
- "eslint-config-custom": "0.2.0",
27
- "tsconfig": "0.5.0"
24
+ "tsup": "^6.7.0",
25
+ "typescript": "^5.1.3",
26
+ "tsconfig": "0.6.0-alpha.1",
27
+ "eslint-config-custom": "0.2.0"
28
28
  },
29
29
  "dependencies": {
30
- "ai": "^2.2.12",
30
+ "ai": "^2.2.23",
31
31
  "nanoid": "^4.0.2",
32
- "openai": "^4.6.0"
32
+ "openai": "^4.6.0",
33
+ "@copilotkit/shared": "0.1.0-alpha.1"
33
34
  },
34
35
  "scripts": {
35
36
  "build": "tsup --treeshake",
@@ -0,0 +1,50 @@
1
+ "use client";
2
+ import { ReactNode } from "react";
3
+ import { CopilotApiConfig } from "../../context/copilot-context";
4
+
5
+ /**
6
+ * Props for the CopilotProvider when using a chat API endpoint.
7
+ */
8
+
9
+ export interface CopilotProviderApiEndpointProps {
10
+ /**
11
+ * The endpoint for the chat API.
12
+ */
13
+ chatApiEndpoint: string;
14
+
15
+ /**
16
+ * The endpoint for the chat API v2.
17
+ * If not provided, defaults to chatApiEndpoint + "/v2".
18
+ * This is used for the chat API v2.
19
+ * If you are not using the chat API v2, you can ignore this.
20
+ * @default chatApiEndpoint + "/v2"
21
+ * @optional
22
+ */
23
+ chatApiEndpointV2?: string;
24
+
25
+ /**
26
+ * The children to be rendered within the CopilotProvider.
27
+ */
28
+ children: ReactNode;
29
+ }
30
+ /**
31
+ * Props for the CopilotProvider when using a CopilotApiConfig.
32
+ */
33
+
34
+ export interface CopilotProviderApiConfigProps {
35
+ /**
36
+ * The configuration for the Copilot API.
37
+ */
38
+ chatApiConfig: CopilotApiConfig;
39
+
40
+ /**
41
+ * The children to be rendered within the CopilotProvider.
42
+ */
43
+ children: ReactNode;
44
+ }
45
+ /**
46
+ * Props for the CopilotProvider component.
47
+ * Can be either CopilotProviderApiEndpointProps or CopilotProviderApiConfigProps.
48
+ */
49
+
50
+ export type CopilotProviderProps = CopilotProviderApiEndpointProps | CopilotProviderApiConfigProps;
@@ -1,24 +1,52 @@
1
1
  "use client";
2
+
2
3
  import { FunctionCallHandler } from "ai";
3
- import { ReactNode, useCallback, useState } from "react";
4
- import { CopilotContext, CopilotApiConfig } from "../context/copilot-context";
5
- import useTree from "../hooks/use-tree";
6
- import { AnnotatedFunction } from "../types/annotated-function";
4
+ import { useCallback, useState } from "react";
5
+ import { CopilotContext, CopilotApiConfig } from "../../context/copilot-context";
6
+ import useTree from "../../hooks/use-tree";
7
+ import { AnnotatedFunction } from "../../types/annotated-function";
7
8
  import { ChatCompletionCreateParams } from "openai/resources/chat";
8
- import { DocumentPointer } from "../types";
9
- import useFlatCategoryStore from "../hooks/use-flat-category-store";
10
-
11
- export function CopilotProvider({
12
- chatApiEndpoint,
13
- headers,
14
- body,
15
- children,
16
- }: {
17
- chatApiEndpoint: string;
18
- headers?: Record<string, string>;
19
- body?: Record<string, any>;
20
- children: ReactNode;
21
- }): JSX.Element {
9
+ import { DocumentPointer } from "../../types";
10
+ import useFlatCategoryStore from "../../hooks/use-flat-category-store";
11
+ import { StandardCopilotApiConfig } from "./standard-cpilot-api-config";
12
+ import { CopilotProviderProps } from "./copilot-provider-props";
13
+
14
+ /**
15
+ * The CopilotProvider component.
16
+ * This component provides the Copilot context to its children.
17
+ * It can be configured either with a chat API endpoint or a CopilotApiConfig.
18
+ *
19
+ * Example usage:
20
+ * ```
21
+ * <CopilotProvider chatApiEndpoint="https://api.copilot.chat">
22
+ * <App />
23
+ * </CopilotProvider>
24
+ * ```
25
+ *
26
+ * or
27
+ *
28
+ * ```
29
+ * const copilotApiConfig = new StandardCopilotApiConfig(
30
+ * "https://api.copilot.chat",
31
+ * "https://api.copilot.chat/v2",
32
+ * {},
33
+ * {}
34
+ * );
35
+ *
36
+ * // ...
37
+ *
38
+ * <CopilotProvider chatApiConfig={copilotApiConfig}>
39
+ * <App />
40
+ * </CopilotProvider>
41
+ * ```
42
+ *
43
+ * @param props - The props for the component.
44
+ * @returns The CopilotProvider component.
45
+ */
46
+ export function CopilotProvider({ children, ...props }: CopilotProviderProps): JSX.Element {
47
+ // Compute all the functions and properties that we need to pass
48
+ // to the CopilotContext.
49
+
22
50
  const [entryPoints, setEntryPoints] = useState<Record<string, AnnotatedFunction<any[]>>>({});
23
51
 
24
52
  const { addElement, removeElement, printTree } = useTree();
@@ -107,6 +135,19 @@ export function CopilotProvider({
107
135
  [removeDocument],
108
136
  );
109
137
 
138
+ // get the appropriate CopilotApiConfig from the props
139
+ let copilotApiConfig: CopilotApiConfig;
140
+ if ("chatApiEndpoint" in props) {
141
+ copilotApiConfig = new StandardCopilotApiConfig(
142
+ props.chatApiEndpoint,
143
+ props.chatApiEndpointV2 || `${props.chatApiEndpoint}/v2`,
144
+ {},
145
+ {},
146
+ );
147
+ } else {
148
+ copilotApiConfig = props.chatApiConfig;
149
+ }
150
+
110
151
  return (
111
152
  <CopilotContext.Provider
112
153
  value={{
@@ -121,11 +162,7 @@ export function CopilotProvider({
121
162
  getDocumentsContext,
122
163
  addDocumentContext,
123
164
  removeDocumentContext,
124
- copilotApiConfig: {
125
- chatApiEndpoint,
126
- headers: headers || {},
127
- body: body || {},
128
- },
165
+ copilotApiConfig: copilotApiConfig,
129
166
  }}
130
167
  >
131
168
  {children}
@@ -0,0 +1,7 @@
1
+ export { CopilotProvider, defaultCopilotContextCategories } from "./copilot-provider";
2
+
3
+ export type {
4
+ CopilotProviderApiEndpointProps,
5
+ CopilotProviderApiConfigProps,
6
+ CopilotProviderProps,
7
+ } from "./copilot-provider-props";
@@ -0,0 +1,28 @@
1
+ import { CopilotApiConfig } from "../../context/copilot-context";
2
+
3
+ /**
4
+ * A standard implementation of the CopilotApiConfig interface.
5
+ *
6
+ * Pass in the base URL of the chat API, the headers to be sent with each request, and the body to be sent with each request.
7
+ * The rest of the CopilotApiConfig interface is implemented by default.
8
+ *
9
+ */
10
+
11
+ export class StandardCopilotApiConfig implements CopilotApiConfig {
12
+ chatApiEndpoint: string;
13
+ chatApiEndpointV2: string;
14
+ headers: Record<string, string>;
15
+ body: Record<string, any>;
16
+
17
+ constructor(
18
+ chatApiEndpoint: string,
19
+ chatApiEndpointV2: string,
20
+ headers: Record<string, string>,
21
+ body: Record<string, any>,
22
+ ) {
23
+ this.chatApiEndpoint = chatApiEndpoint;
24
+ this.chatApiEndpointV2 = chatApiEndpointV2;
25
+ this.headers = headers;
26
+ this.body = body;
27
+ }
28
+ }
@@ -1 +1 @@
1
- export { CopilotProvider, defaultCopilotContextCategories } from "./copilot-provider";
1
+ export * from "./copilot-provider";
@@ -16,6 +16,11 @@ export interface CopilotApiConfig {
16
16
  */
17
17
  chatApiEndpoint: string;
18
18
 
19
+ /**
20
+ * The endpoint for the chat API v2.
21
+ */
22
+ chatApiEndpointV2: string;
23
+
19
24
  /**
20
25
  * additional headers to be sent with the request
21
26
  * @default {}
@@ -46,6 +51,9 @@ export function copilotApiConfigExtrapolator(config: CopilotApiConfig) {
46
51
  get chatApiEndpoint(): string {
47
52
  return `${config.chatApiEndpoint}`;
48
53
  },
54
+ get chatApiEndpointV2(): string {
55
+ return `${config.chatApiEndpointV2}`;
56
+ },
49
57
  };
50
58
  }
51
59
 
@@ -93,6 +101,11 @@ const emptyCopilotContext: CopilotContextParams = {
93
101
  "Remember to wrap your app in a `<CopilotProvider> {...} </CopilotProvider>` !!!",
94
102
  );
95
103
  }
104
+ get chatApiEndpointV2(): string {
105
+ throw new Error(
106
+ "Remember to wrap your app in a `<CopilotProvider> {...} </CopilotProvider>` !!!",
107
+ );
108
+ }
96
109
  get headers(): Record<string, string> {
97
110
  return {};
98
111
  }
@@ -63,7 +63,7 @@ export function useCopilotChat({
63
63
  headers: { ...copilotApiConfig.headers, ...options.headers },
64
64
  body: {
65
65
  id: options.id,
66
- functions: functionDescriptions,
66
+ functions: functionDescriptions.length > 0 ? functionDescriptions : undefined,
67
67
  ...copilotApiConfig.body,
68
68
  ...options.body,
69
69
  },
package/src/index.tsx CHANGED
@@ -2,3 +2,4 @@ export * from "./components";
2
2
  export * from "./context";
3
3
  export * from "./hooks";
4
4
  export * from "./types";
5
+ export * from "./openai-assistants";
@@ -0,0 +1,9 @@
1
+ // export type { AssistantStatus } from "./use-assistants";
2
+ // export { experimental_useAssistant } from "./use-assistants";
3
+ export type {
4
+ AssistantStatus,
5
+ RequestForwardingOptions,
6
+ UseCopilotChatOptionsV2,
7
+ UseCopilotChatV2Result,
8
+ } from "./use-copilot-chat-v2";
9
+ export { useCopilotChatV2 } from "./use-copilot-chat-v2";
@@ -0,0 +1,114 @@
1
+ "use client";
2
+
3
+ import { useState } from "react";
4
+ import { processMessageStream } from "../utils";
5
+ import { Message, parseStreamPart } from "@copilotkit/shared";
6
+
7
+ export type AssistantStatus = "in_progress" | "awaiting_message";
8
+
9
+ export interface UseAssistantResult {
10
+ messages: Message[];
11
+ input: string;
12
+ handleInputChange: (e: any) => void;
13
+ submitMessage: (e: any) => Promise<void>;
14
+ status: AssistantStatus;
15
+ error: unknown;
16
+ }
17
+
18
+ export function experimental_useAssistant({
19
+ api,
20
+ threadId: threadIdParam,
21
+ }: {
22
+ api: string;
23
+ threadId?: string | undefined;
24
+ }): UseAssistantResult {
25
+ const [messages, setMessages] = useState<Message[]>([]);
26
+ const [input, setInput] = useState("");
27
+ const [threadId, setThreadId] = useState<string | undefined>(undefined);
28
+ const [status, setStatus] = useState<AssistantStatus>("awaiting_message");
29
+ const [error, setError] = useState<unknown | undefined>(undefined);
30
+
31
+ const handleInputChange = (e: any) => {
32
+ setInput(e.target.value);
33
+ };
34
+
35
+ const submitMessage = async (e: any) => {
36
+ e.preventDefault();
37
+
38
+ if (input === "") {
39
+ return;
40
+ }
41
+
42
+ setStatus("in_progress");
43
+
44
+ setMessages((messages) => [...messages, { id: "", role: "user", content: input }]);
45
+
46
+ setInput("");
47
+
48
+ const result = await fetch(api, {
49
+ method: "POST",
50
+ headers: { "Content-Type": "application/json" },
51
+ body: JSON.stringify({
52
+ // always use user-provided threadId when available:
53
+ threadId: threadIdParam ?? threadId ?? null,
54
+ message: input,
55
+ }),
56
+ });
57
+
58
+ if (result.body == null) {
59
+ throw new Error("The response body is empty.");
60
+ }
61
+
62
+ await processMessageStream(result.body.getReader(), (message: string) => {
63
+ try {
64
+ const { type, value } = parseStreamPart(message);
65
+
66
+ switch (type) {
67
+ case "assistant_message": {
68
+ // append message:
69
+ setMessages((messages) => [
70
+ ...messages,
71
+ {
72
+ id: value.id,
73
+ role: value.role,
74
+ content: value.content[0].text.value,
75
+ },
76
+ ]);
77
+ break;
78
+ }
79
+
80
+ case "assistant_control_data": {
81
+ setThreadId(value.threadId);
82
+
83
+ // set id of last message:
84
+ setMessages((messages) => {
85
+ const lastMessage = messages[messages.length - 1];
86
+ lastMessage.id = value.messageId;
87
+ return [...messages.slice(0, messages.length - 1), lastMessage];
88
+ });
89
+
90
+ break;
91
+ }
92
+
93
+ case "error": {
94
+ setError(value);
95
+ break;
96
+ }
97
+ }
98
+ } catch (error) {
99
+ setError(error);
100
+ }
101
+ });
102
+
103
+ setStatus("awaiting_message");
104
+ };
105
+
106
+ return {
107
+ messages,
108
+ input,
109
+ handleInputChange,
110
+ submitMessage,
111
+ status,
112
+ error,
113
+ };
114
+ }