@copilotkit/react-core 0.8.0-alpha.5 → 0.8.0-alpha.6

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 (44) hide show
  1. package/.turbo/turbo-build.log +83 -73
  2. package/CHANGELOG.md +6 -0
  3. package/dist/components/copilot-provider.d.ts +1 -0
  4. package/dist/components/copilot-provider.mjs +110 -8
  5. package/dist/components/copilot-provider.mjs.map +1 -1
  6. package/dist/components/index.d.ts +1 -0
  7. package/dist/components/index.mjs +110 -8
  8. package/dist/components/index.mjs.map +1 -1
  9. package/dist/context/copilot-context.d.ts +4 -0
  10. package/dist/context/copilot-context.mjs +4 -0
  11. package/dist/context/copilot-context.mjs.map +1 -1
  12. package/dist/context/index.d.ts +1 -0
  13. package/dist/context/index.mjs +4 -0
  14. package/dist/context/index.mjs.map +1 -1
  15. package/dist/hooks/index.d.ts +2 -0
  16. package/dist/hooks/index.mjs +21 -0
  17. package/dist/hooks/index.mjs.map +1 -1
  18. package/dist/hooks/use-copilot-chat.mjs +4 -0
  19. package/dist/hooks/use-copilot-chat.mjs.map +1 -1
  20. package/dist/hooks/use-flat-category-store.d.ts +9 -0
  21. package/dist/hooks/use-flat-category-store.mjs +75 -0
  22. package/dist/hooks/use-flat-category-store.mjs.map +1 -0
  23. package/dist/hooks/use-make-copilot-actionable.mjs +4 -0
  24. package/dist/hooks/use-make-copilot-actionable.mjs.map +1 -1
  25. package/dist/hooks/use-make-copilot-document-readable.d.ts +12 -0
  26. package/dist/hooks/use-make-copilot-document-readable.mjs +78 -0
  27. package/dist/hooks/use-make-copilot-document-readable.mjs.map +1 -0
  28. package/dist/hooks/use-make-copilot-readable.mjs +4 -0
  29. package/dist/hooks/use-make-copilot-readable.mjs.map +1 -1
  30. package/dist/index.d.ts +2 -0
  31. package/dist/index.mjs +129 -10
  32. package/dist/index.mjs.map +1 -1
  33. package/dist/types/document-pointer.d.ts +9 -0
  34. package/dist/types/document-pointer.mjs +1 -0
  35. package/dist/types/document-pointer.mjs.map +1 -0
  36. package/dist/types/index.d.ts +1 -0
  37. package/package.json +3 -3
  38. package/src/components/copilot-provider.tsx +31 -1
  39. package/src/context/copilot-context.tsx +13 -0
  40. package/src/hooks/index.ts +1 -0
  41. package/src/hooks/use-flat-category-store.ts +114 -0
  42. package/src/hooks/use-make-copilot-document-readable.ts +32 -0
  43. package/src/types/document-pointer.ts +7 -0
  44. package/src/types/index.ts +1 -0
@@ -5,6 +5,8 @@ import { CopilotContext, CopilotApiConfig } from "../context/copilot-context";
5
5
  import useTree from "../hooks/use-tree";
6
6
  import { AnnotatedFunction } from "../types/annotated-function";
7
7
  import { ChatCompletionCreateParams } from "openai/resources/chat";
8
+ import { DocumentPointer } from "../types";
9
+ import useFlatCategoryStore from "../hooks/use-flat-category-store";
8
10
 
9
11
  export function CopilotProvider({
10
12
  copilotApiConfig,
@@ -18,6 +20,11 @@ export function CopilotProvider({
18
20
  >({});
19
21
 
20
22
  const { addElement, removeElement, printTree } = useTree();
23
+ const {
24
+ addElement: addDocument,
25
+ removeElement: removeDocument,
26
+ allElements: allDocuments,
27
+ } = useFlatCategoryStore<DocumentPointer>();
21
28
 
22
29
  const setEntryPoint = useCallback(
23
30
  (id: string, entryPoint: AnnotatedFunction<any[]>) => {
@@ -68,6 +75,27 @@ export function CopilotProvider({
68
75
  return entryPointsToFunctionCallHandler(Object.values(entryPoints));
69
76
  }, [entryPoints]);
70
77
 
78
+ const getDocumentsContext = useCallback(
79
+ (categories: string[] = ["global"]) => {
80
+ return allDocuments(categories);
81
+ },
82
+ [allDocuments]
83
+ );
84
+
85
+ const addDocumentContext = useCallback(
86
+ (documentPointer: DocumentPointer, categories: string[] = ["global"]) => {
87
+ return addDocument(documentPointer, categories);
88
+ },
89
+ [addDocument]
90
+ );
91
+
92
+ const removeDocumentContext = useCallback(
93
+ (documentId: string) => {
94
+ removeDocument(documentId);
95
+ },
96
+ [removeDocument]
97
+ );
98
+
71
99
  return (
72
100
  <CopilotContext.Provider
73
101
  value={{
@@ -79,7 +107,9 @@ export function CopilotProvider({
79
107
  getContextString,
80
108
  addContext,
81
109
  removeContext,
82
-
110
+ getDocumentsContext,
111
+ addDocumentContext,
112
+ removeDocumentContext,
83
113
  copilotApiConfig,
84
114
  }}
85
115
  >
@@ -5,6 +5,7 @@ import React from "react";
5
5
  import { TreeNodeId } from "../hooks/use-tree";
6
6
  import { AnnotatedFunction } from "../types/annotated-function";
7
7
  import { ChatCompletionCreateParams } from "openai/resources/chat";
8
+ import { DocumentPointer } from "../types";
8
9
 
9
10
  export interface CopilotApiConfig {
10
11
  chatApiEndpoint: string;
@@ -35,6 +36,14 @@ export interface CopilotContextParams {
35
36
  ) => TreeNodeId;
36
37
  removeContext: (id: TreeNodeId) => void;
37
38
 
39
+ // document context
40
+ getDocumentsContext: (categories?: string[]) => DocumentPointer[];
41
+ addDocumentContext: (
42
+ documentPointer: DocumentPointer,
43
+ categories?: string[]
44
+ ) => TreeNodeId;
45
+ removeDocumentContext: (documentId: string) => void;
46
+
38
47
  // api endpoints
39
48
  copilotApiConfig: CopilotApiConfig;
40
49
  }
@@ -50,6 +59,10 @@ const emptyCopilotContext: CopilotContextParams = {
50
59
  addContext: () => "",
51
60
  removeContext: () => {},
52
61
 
62
+ getDocumentsContext: () => returnAndThrowInDebug([]),
63
+ addDocumentContext: () => returnAndThrowInDebug(""),
64
+ removeDocumentContext: () => {},
65
+
53
66
  copilotApiConfig: new (class implements CopilotApiConfig {
54
67
  get chatApiEndpoint(): string {
55
68
  throw new Error(
@@ -4,3 +4,4 @@ export type { UseCopilotChatReturn } from "./use-copilot-chat";
4
4
 
5
5
  export { useMakeCopilotActionable } from "./use-make-copilot-actionable";
6
6
  export { useMakeCopilotReadable } from "./use-make-copilot-readable";
7
+ export { useMakeCopilotDocumentReadable } from "./use-make-copilot-document-readable";
@@ -0,0 +1,114 @@
1
+ import { nanoid } from "nanoid";
2
+ import { useCallback, useReducer } from "react";
3
+
4
+ export type FlatCategoryStoreId = string;
5
+
6
+ export interface UseFlatCategoryStoreReturn<T> {
7
+ addElement: (value: T, categories: string[]) => FlatCategoryStoreId;
8
+ removeElement: (id: FlatCategoryStoreId) => void;
9
+ allElements: (categories: string[]) => T[];
10
+ }
11
+
12
+ interface FlatCategoryStoreElement<T> {
13
+ id: FlatCategoryStoreId;
14
+ value: T;
15
+ categories: Set<string>;
16
+ }
17
+
18
+ const useFlatCategoryStore = <T>(): UseFlatCategoryStoreReturn<T> => {
19
+ const [elements, dispatch] = useReducer<
20
+ React.Reducer<
21
+ Map<FlatCategoryStoreId, FlatCategoryStoreElement<T>>,
22
+ Action<T>
23
+ >
24
+ >(
25
+ flatCategoryStoreReducer,
26
+ new Map<FlatCategoryStoreId, FlatCategoryStoreElement<T>>()
27
+ );
28
+
29
+ const addElement = useCallback(
30
+ (value: T, categories: string[]): FlatCategoryStoreId => {
31
+ const newId = nanoid();
32
+ dispatch({
33
+ type: "ADD_ELEMENT",
34
+ value,
35
+ id: newId,
36
+ categories,
37
+ });
38
+ return newId;
39
+ },
40
+ []
41
+ );
42
+
43
+ const removeElement = useCallback((id: FlatCategoryStoreId): void => {
44
+ dispatch({ type: "REMOVE_ELEMENT", id });
45
+ }, []);
46
+
47
+ const allElements = useCallback(
48
+ (categories: string[]): T[] => {
49
+ const categoriesSet = new Set(categories);
50
+ const result: T[] = [];
51
+ elements.forEach((element) => {
52
+ if (setsHaveIntersection(categoriesSet, element.categories)) {
53
+ result.push(element.value);
54
+ }
55
+ });
56
+ return result;
57
+ },
58
+ [elements]
59
+ );
60
+
61
+ return { addElement, removeElement, allElements };
62
+ };
63
+
64
+ export default useFlatCategoryStore;
65
+
66
+ // Action types
67
+ type Action<T> =
68
+ | {
69
+ type: "ADD_ELEMENT";
70
+ value: T;
71
+ id: FlatCategoryStoreId;
72
+ categories: string[];
73
+ }
74
+ | { type: "REMOVE_ELEMENT"; id: FlatCategoryStoreId };
75
+
76
+ // Reducer
77
+ function flatCategoryStoreReducer<T>(
78
+ state: Map<FlatCategoryStoreId, FlatCategoryStoreElement<T>>,
79
+ action: Action<T>
80
+ ): Map<FlatCategoryStoreId, FlatCategoryStoreElement<T>> {
81
+ switch (action.type) {
82
+ case "ADD_ELEMENT": {
83
+ const { value, id, categories } = action;
84
+ const newElement: FlatCategoryStoreElement<T> = {
85
+ id,
86
+ value,
87
+ categories: new Set(categories),
88
+ };
89
+ const newState = new Map(state);
90
+ newState.set(id, newElement);
91
+ return newState;
92
+ }
93
+ case "REMOVE_ELEMENT": {
94
+ const newState = new Map(state);
95
+ newState.delete(action.id);
96
+ return newState;
97
+ }
98
+ default:
99
+ return state;
100
+ }
101
+ }
102
+
103
+ function setsHaveIntersection<T>(setA: Set<T>, setB: Set<T>): boolean {
104
+ const [smallerSet, largerSet] =
105
+ setA.size <= setB.size ? [setA, setB] : [setB, setA];
106
+
107
+ for (let item of smallerSet) {
108
+ if (largerSet.has(item)) {
109
+ return true;
110
+ }
111
+ }
112
+
113
+ return false;
114
+ }
@@ -0,0 +1,32 @@
1
+ "use client";
2
+
3
+ import { useContext, useEffect, useRef } from "react";
4
+ import { CopilotContext } from "../context/copilot-context";
5
+ import { DocumentPointer } from "../types";
6
+
7
+ /**
8
+ * Adds the given information to the Copilot context to make it readable by Copilot.
9
+ * @param information - The information to be added to the Copilot context.
10
+ * @param parentId - The ID of the parent context, if any.
11
+ * @param categories - An array of categories to control which context are visible where. Particularly useful with CopilotTextarea (see `useMakeAutosuggestionFunction`)
12
+ * @returns The ID of the added context.
13
+ */
14
+ export function useMakeCopilotDocumentReadable(
15
+ document: DocumentPointer,
16
+ categories?: string[]
17
+ ): string | undefined {
18
+ const { addDocumentContext, removeDocumentContext } =
19
+ useContext(CopilotContext);
20
+ const idRef = useRef<string>();
21
+
22
+ useEffect(() => {
23
+ const id = addDocumentContext(document, categories);
24
+ idRef.current = id;
25
+
26
+ return () => {
27
+ removeDocumentContext(id);
28
+ };
29
+ }, [document, categories, addDocumentContext, removeDocumentContext]);
30
+
31
+ return idRef.current;
32
+ }
@@ -0,0 +1,7 @@
1
+ export interface DocumentPointer {
2
+ id: string;
3
+ name: string;
4
+ sourceApplication: string;
5
+ iconImageUri: string;
6
+ getContents: () => Promise<string>;
7
+ }
@@ -1,2 +1,3 @@
1
1
  export type { AnnotatedFunctionArgument } from "./annotated-function";
2
2
  export type { AnnotatedFunction } from "./annotated-function";
3
+ export type { DocumentPointer } from "./document-pointer";