@copilotkit/react-core 0.8.0-alpha.4 → 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 (77) hide show
  1. package/.turbo/turbo-build.log +85 -71
  2. package/CHANGELOG.md +12 -0
  3. package/dist/components/copilot-provider.d.ts +1 -0
  4. package/dist/components/copilot-provider.mjs +442 -5
  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 +442 -6
  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 +67 -3
  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 +67 -4
  14. package/dist/context/index.mjs.map +1 -1
  15. package/dist/hooks/index.d.ts +2 -0
  16. package/dist/hooks/index.mjs +220 -7
  17. package/dist/hooks/index.mjs.map +1 -1
  18. package/dist/hooks/use-copilot-chat.mjs +159 -4
  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 +88 -4
  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 +77 -4
  29. package/dist/hooks/use-make-copilot-readable.mjs.map +1 -1
  30. package/dist/hooks/use-tree.mjs +159 -3
  31. package/dist/hooks/use-tree.mjs.map +1 -1
  32. package/dist/index.d.ts +2 -0
  33. package/dist/index.mjs +589 -12
  34. package/dist/index.mjs.map +1 -1
  35. package/dist/types/annotated-function.mjs +0 -2
  36. package/dist/types/annotated-function.mjs.map +1 -1
  37. package/dist/types/document-pointer.d.ts +9 -0
  38. package/dist/types/document-pointer.mjs +1 -0
  39. package/dist/types/document-pointer.mjs.map +1 -0
  40. package/dist/types/index.d.ts +1 -0
  41. package/dist/types/index.mjs +0 -2
  42. package/dist/types/index.mjs.map +1 -1
  43. package/dist/utils/utils.mjs +0 -2
  44. package/dist/utils/utils.mjs.map +1 -1
  45. package/dist/utils/utils.test.mjs +0 -1
  46. package/dist/utils/utils.test.mjs.map +1 -1
  47. package/package.json +3 -3
  48. package/src/components/copilot-provider.tsx +31 -1
  49. package/src/context/copilot-context.tsx +13 -0
  50. package/src/hooks/index.ts +1 -0
  51. package/src/hooks/use-copilot-chat.ts +0 -3
  52. package/src/hooks/use-flat-category-store.ts +114 -0
  53. package/src/hooks/use-make-copilot-document-readable.ts +32 -0
  54. package/src/types/document-pointer.ts +7 -0
  55. package/src/types/index.ts +1 -0
  56. package/dist/chunk-3BOHSSKR.mjs +0 -141
  57. package/dist/chunk-3BOHSSKR.mjs.map +0 -1
  58. package/dist/chunk-6VD2ABXH.mjs +0 -82
  59. package/dist/chunk-6VD2ABXH.mjs.map +0 -1
  60. package/dist/chunk-BVQRDAR7.mjs +0 -3
  61. package/dist/chunk-BVQRDAR7.mjs.map +0 -1
  62. package/dist/chunk-DFQJR3H5.mjs +0 -120
  63. package/dist/chunk-DFQJR3H5.mjs.map +0 -1
  64. package/dist/chunk-EFZPSZWO.mjs +0 -3
  65. package/dist/chunk-EFZPSZWO.mjs.map +0 -1
  66. package/dist/chunk-JD7BAH7U.mjs +0 -3
  67. package/dist/chunk-JD7BAH7U.mjs.map +0 -1
  68. package/dist/chunk-JLQULZT4.mjs +0 -19
  69. package/dist/chunk-JLQULZT4.mjs.map +0 -1
  70. package/dist/chunk-MRXNTQOX.mjs +0 -55
  71. package/dist/chunk-MRXNTQOX.mjs.map +0 -1
  72. package/dist/chunk-W6NBODX5.mjs +0 -30
  73. package/dist/chunk-W6NBODX5.mjs.map +0 -1
  74. package/dist/chunk-YPSGKPDA.mjs +0 -3
  75. package/dist/chunk-YPSGKPDA.mjs.map +0 -1
  76. package/dist/chunk-ZKO7HCZS.mjs +0 -40
  77. package/dist/chunk-ZKO7HCZS.mjs.map +0 -1
@@ -1 +1 @@
1
- {"version":3,"sources":[],"names":[],"mappings":""}
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
@@ -0,0 +1,9 @@
1
+ interface DocumentPointer {
2
+ id: string;
3
+ name: string;
4
+ sourceApplication: string;
5
+ iconImageUri: string;
6
+ getContents: () => Promise<string>;
7
+ }
8
+
9
+ export { DocumentPointer };
@@ -0,0 +1 @@
1
+ //# sourceMappingURL=document-pointer.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
@@ -1 +1,2 @@
1
1
  export { AnnotatedFunction, AnnotatedFunctionArgument } from './annotated-function.js';
2
+ export { DocumentPointer } from './document-pointer.js';
@@ -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,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":[]}
@@ -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.8.0-alpha.4",
7
+ "version": "0.8.0-alpha.6",
8
8
  "sideEffects": false,
9
9
  "main": "./dist/index.js",
10
10
  "module": "./dist/index.mjs",
@@ -23,8 +23,8 @@
23
23
  "ts-jest": "^29.1.1",
24
24
  "tsup": "^6.5.0",
25
25
  "typescript": "^4.9.4",
26
- "tsconfig": "0.4.0-alpha.2",
27
- "eslint-config-custom": "0.2.0"
26
+ "eslint-config-custom": "0.2.0",
27
+ "tsconfig": "0.4.0-alpha.4"
28
28
  },
29
29
  "dependencies": {
30
30
  "ai": "^2.2.12",
@@ -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";
@@ -65,7 +65,6 @@ export function useCopilotChat({
65
65
  experimental_onFunctionCall: getFunctionCallHandler(),
66
66
  body: {
67
67
  id: options.id,
68
- previewToken,
69
68
  functions: functionDescriptions,
70
69
  },
71
70
  });
@@ -85,8 +84,6 @@ export function useCopilotChat({
85
84
  };
86
85
  }
87
86
 
88
- const previewToken = "TODO123";
89
-
90
87
  export function defaultSystemMessage(contextString: string): string {
91
88
  return `
92
89
  Please act as an efficient, competent, conscientious, and industrious professional assistant.
@@ -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";
@@ -1,141 +0,0 @@
1
- import { __spreadProps, __spreadValues } from './chunk-MRXNTQOX.mjs';
2
- import { nanoid } from 'nanoid';
3
- import { useReducer, useCallback } from 'react';
4
-
5
- var removeNode = (nodes, id) => {
6
- return nodes.reduce((result, node) => {
7
- if (node.id !== id) {
8
- const newNode = __spreadProps(__spreadValues({}, node), { children: removeNode(node.children, id) });
9
- result.push(newNode);
10
- }
11
- return result;
12
- }, []);
13
- };
14
- var addNode = (nodes, newNode, parentId) => {
15
- if (!parentId) {
16
- return [...nodes, newNode];
17
- }
18
- return nodes.map((node) => {
19
- if (node.id === parentId) {
20
- return __spreadProps(__spreadValues({}, node), { children: [...node.children, newNode] });
21
- } else if (node.children.length) {
22
- return __spreadProps(__spreadValues({}, node), { children: addNode(node.children, newNode, parentId) });
23
- }
24
- return node;
25
- });
26
- };
27
- var treeIndentationRepresentation = (index, indentLevel) => {
28
- if (indentLevel === 0) {
29
- return (index + 1).toString();
30
- } else if (indentLevel === 1) {
31
- return String.fromCharCode(65 + index);
32
- } else if (indentLevel === 2) {
33
- return String.fromCharCode(97 + index);
34
- } else {
35
- return "-";
36
- }
37
- };
38
- var printNode = (node, prefix = "", indentLevel = 0) => {
39
- const indent = " ".repeat(3).repeat(indentLevel);
40
- const prefixPlusIndentLength = prefix.length + indent.length;
41
- const subsequentLinesPrefix = " ".repeat(prefixPlusIndentLength);
42
- const valueLines = node.value.split("\n");
43
- const outputFirstLine = `${indent}${prefix}${valueLines[0]}`;
44
- const outputSubsequentLines = valueLines.slice(1).map((line) => `${subsequentLinesPrefix}${line}`).join("\n");
45
- let output = `${outputFirstLine}
46
- `;
47
- if (outputSubsequentLines) {
48
- output += `${outputSubsequentLines}
49
- `;
50
- }
51
- const childPrePrefix = " ".repeat(prefix.length);
52
- node.children.forEach(
53
- (child, index) => output += printNode(
54
- child,
55
- `${childPrePrefix}${treeIndentationRepresentation(
56
- index,
57
- indentLevel + 1
58
- )}. `,
59
- indentLevel + 1
60
- )
61
- );
62
- return output;
63
- };
64
- function treeReducer(state, action) {
65
- switch (action.type) {
66
- case "ADD_NODE": {
67
- const { value, parentId, id: newNodeId } = action;
68
- const newNode = {
69
- id: newNodeId,
70
- value,
71
- children: [],
72
- categories: new Set(action.categories)
73
- };
74
- try {
75
- return addNode(state, newNode, parentId);
76
- } catch (error) {
77
- console.error(`Error while adding node with id ${newNodeId}: ${error}`);
78
- return state;
79
- }
80
- }
81
- case "REMOVE_NODE":
82
- return removeNode(state, action.id);
83
- default:
84
- return state;
85
- }
86
- }
87
- var useTree = () => {
88
- const [tree, dispatch] = useReducer(treeReducer, []);
89
- const addElement = useCallback(
90
- (value, categories, parentId) => {
91
- const newNodeId = nanoid();
92
- dispatch({
93
- type: "ADD_NODE",
94
- value,
95
- parentId,
96
- id: newNodeId,
97
- categories
98
- });
99
- return newNodeId;
100
- },
101
- []
102
- );
103
- const removeElement = useCallback((id) => {
104
- dispatch({ type: "REMOVE_NODE", id });
105
- }, []);
106
- const printTree = useCallback(
107
- (categories) => {
108
- const categoriesSet = new Set(categories);
109
- let output = "";
110
- tree.forEach((node, index) => {
111
- if (!setsHaveIntersection(categoriesSet, node.categories)) {
112
- return;
113
- }
114
- if (index !== 0) {
115
- output += "\n";
116
- }
117
- output += printNode(
118
- node,
119
- `${treeIndentationRepresentation(index, 0)}. `
120
- );
121
- });
122
- return output;
123
- },
124
- [tree]
125
- );
126
- return { tree, addElement, printTree, removeElement };
127
- };
128
- var use_tree_default = useTree;
129
- function setsHaveIntersection(setA, setB) {
130
- const [smallerSet, largerSet] = setA.size <= setB.size ? [setA, setB] : [setB, setA];
131
- for (let item of smallerSet) {
132
- if (largerSet.has(item)) {
133
- return true;
134
- }
135
- }
136
- return false;
137
- }
138
-
139
- export { use_tree_default };
140
- //# sourceMappingURL=out.js.map
141
- //# sourceMappingURL=chunk-3BOHSSKR.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/hooks/use-tree.ts"],"names":[],"mappings":";;;;;;AAAA,SAAS,cAAc;AACvB,SAAS,aAAa,kBAAkB;AAsCxC,IAAM,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,YAAY,CAAC,OAAyB;AAC1D,aAAS,EAAE,MAAM,eAAe,GAAG,CAAC;AAAA,EACtC,GAAG,CAAC,CAAC;AAEL,QAAM,YAAY;AAAA,IAChB,CAAC,eAAiC;AAChC,YAAM,gBAAgB,IAAI,IAAI,UAAU;AAExC,UAAI,SAAS;AACb,WAAK,QAAQ,CAAC,MAAM,UAAU;AAE5B,YAAI,CAAC,qBAAqB,eAAe,KAAK,UAAU,GAAG;AACzD;AAAA,QACF;AAGA,YAAI,UAAU,GAAG;AACf,oBAAU;AAAA,QACZ;AAEA,kBAAU;AAAA,UACR;AAAA,UACA,GAAG,8BAA8B,OAAO,CAAC;AAAA,QAC3C;AAAA,MACF,CAAC;AACD,aAAO;AAAA,IACT;AAAA,IACA,CAAC,IAAI;AAAA,EACP;AAEA,SAAO,EAAE,MAAM,YAAY,WAAW,cAAc;AACtD;AAEA,IAAO,mBAAQ;AAEf,SAAS,qBAAwB,MAAc,MAAuB;AACpE,QAAM,CAAC,YAAY,SAAS,IAC1B,KAAK,QAAQ,KAAK,OAAO,CAAC,MAAM,IAAI,IAAI,CAAC,MAAM,IAAI;AAErD,WAAS,QAAQ,YAAY;AAC3B,QAAI,UAAU,IAAI,IAAI,GAAG;AACvB,aAAO;AAAA,IACT;AAAA,EACF;AAEA,SAAO;AACT","sourcesContent":["import { nanoid } from \"nanoid\";\nimport { useCallback, useReducer } from \"react\";\n\nexport type TreeNodeId = string;\n\nexport interface TreeNode {\n id: TreeNodeId;\n value: string;\n children: TreeNode[];\n parentId?: TreeNodeId;\n categories: Set<string>;\n}\n\nexport type Tree = TreeNode[];\n\nexport interface UseTreeReturn {\n tree: Tree;\n addElement: (\n value: string,\n categories: string[],\n parentId?: TreeNodeId\n ) => TreeNodeId;\n printTree: (categories: string[]) => string;\n removeElement: (id: TreeNodeId) => void;\n}\n\nconst findNode = (nodes: Tree, id: TreeNodeId): TreeNode | undefined => {\n for (const node of nodes) {\n if (node.id === id) {\n return node;\n }\n const result = findNode(node.children, id);\n if (result) {\n return result;\n }\n }\n return undefined;\n};\n\nconst removeNode = (nodes: Tree, id: TreeNodeId): Tree => {\n return nodes.reduce((result: Tree, node) => {\n if (node.id !== id) {\n const newNode = { ...node, children: removeNode(node.children, id) };\n result.push(newNode);\n }\n return result;\n }, []);\n};\n\nconst addNode = (\n nodes: Tree,\n newNode: TreeNode,\n parentId?: TreeNodeId\n): Tree => {\n if (!parentId) {\n return [...nodes, newNode];\n }\n return nodes.map((node) => {\n if (node.id === parentId) {\n return { ...node, children: [...node.children, newNode] };\n } else if (node.children.length) {\n return { ...node, children: addNode(node.children, newNode, parentId) };\n }\n return node;\n });\n};\n\nconst treeIndentationRepresentation = (\n index: number,\n indentLevel: number\n): string => {\n if (indentLevel === 0) {\n return (index + 1).toString();\n } else if (indentLevel === 1) {\n return String.fromCharCode(65 + index); // 65 is the ASCII value for 'A'\n } else if (indentLevel === 2) {\n return String.fromCharCode(97 + index); // 97 is the ASCII value for 'a'\n } else {\n return \"-\";\n }\n};\n\nconst printNode = (node: TreeNode, prefix = \"\", indentLevel = 0): string => {\n const indent = \" \".repeat(3).repeat(indentLevel);\n\n const prefixPlusIndentLength = prefix.length + indent.length;\n const subsequentLinesPrefix = \" \".repeat(prefixPlusIndentLength);\n\n const valueLines = node.value.split(\"\\n\");\n\n const outputFirstLine = `${indent}${prefix}${valueLines[0]}`;\n const outputSubsequentLines = valueLines\n .slice(1)\n .map((line) => `${subsequentLinesPrefix}${line}`)\n .join(\"\\n\");\n\n let output = `${outputFirstLine}\\n`;\n if (outputSubsequentLines) {\n output += `${outputSubsequentLines}\\n`;\n }\n\n const childPrePrefix = \" \".repeat(prefix.length);\n\n node.children.forEach(\n (child, index) =>\n (output += printNode(\n child,\n `${childPrePrefix}${treeIndentationRepresentation(\n index,\n indentLevel + 1\n )}. `,\n indentLevel + 1\n ))\n );\n return output;\n};\n\n// Action types\ntype Action =\n | {\n type: \"ADD_NODE\";\n value: string;\n parentId?: string;\n id: string;\n categories: string[];\n }\n | { type: \"REMOVE_NODE\"; id: string };\n\n// Reducer function\nfunction treeReducer(state: Tree, action: Action): Tree {\n switch (action.type) {\n case \"ADD_NODE\": {\n const { value, parentId, id: newNodeId } = action;\n const newNode: TreeNode = {\n id: newNodeId,\n value,\n children: [],\n categories: new Set(action.categories),\n };\n\n try {\n return addNode(state, newNode, parentId);\n } catch (error) {\n console.error(`Error while adding node with id ${newNodeId}: ${error}`);\n return state;\n }\n }\n case \"REMOVE_NODE\":\n return removeNode(state, action.id);\n default:\n return state;\n }\n}\n\n// useTree hook\nconst useTree = (): UseTreeReturn => {\n const [tree, dispatch] = useReducer(treeReducer, []);\n\n const addElement = useCallback(\n (value: string, categories: string[], parentId?: string): TreeNodeId => {\n const newNodeId = nanoid(); // Generate new ID outside of dispatch\n dispatch({\n type: \"ADD_NODE\",\n value,\n parentId,\n id: newNodeId,\n categories: categories,\n });\n return newNodeId; // Return the new ID\n },\n []\n );\n\n const removeElement = useCallback((id: TreeNodeId): void => {\n dispatch({ type: \"REMOVE_NODE\", id });\n }, []);\n\n const printTree = useCallback(\n (categories: string[]): string => {\n const categoriesSet = new Set(categories);\n\n let output = \"\";\n tree.forEach((node, index) => {\n // if the node does not have any of the desired categories, continue to the next node\n if (!setsHaveIntersection(categoriesSet, node.categories)) {\n return;\n }\n\n // add a new line before each node except the first one\n if (index !== 0) {\n output += \"\\n\";\n }\n\n output += printNode(\n node,\n `${treeIndentationRepresentation(index, 0)}. `\n );\n });\n return output;\n },\n [tree]\n );\n\n return { tree, addElement, printTree, removeElement };\n};\n\nexport default useTree;\n\nfunction setsHaveIntersection<T>(setA: Set<T>, setB: Set<T>): boolean {\n const [smallerSet, largerSet] =\n setA.size <= setB.size ? [setA, setB] : [setB, setA];\n\n for (let item of smallerSet) {\n if (largerSet.has(item)) {\n return true;\n }\n }\n\n return false;\n}\n"]}
@@ -1,82 +0,0 @@
1
- import { CopilotContext, copilotApiConfigExtrapolator } from './chunk-ZKO7HCZS.mjs';
2
- import { __objRest } from './chunk-MRXNTQOX.mjs';
3
- import { useContext, useMemo } from 'react';
4
- import { useChat } from 'ai/react';
5
-
6
- function useCopilotChat(_a) {
7
- var _b = _a, {
8
- makeSystemMessage
9
- } = _b, options = __objRest(_b, [
10
- "makeSystemMessage"
11
- ]);
12
- const {
13
- getContextString,
14
- getChatCompletionFunctionDescriptions,
15
- getFunctionCallHandler,
16
- copilotApiConfig
17
- } = useContext(CopilotContext);
18
- const systemMessage = useMemo(() => {
19
- const systemMessageMaker = makeSystemMessage || defaultSystemMessage;
20
- const contextString = getContextString();
21
- return {
22
- id: "system",
23
- content: systemMessageMaker(contextString),
24
- role: "system"
25
- };
26
- }, [getContextString, makeSystemMessage]);
27
- const initialMessagesWithContext = [systemMessage].concat(
28
- options.initialMessages || []
29
- );
30
- const functionDescriptions = useMemo(() => {
31
- return getChatCompletionFunctionDescriptions();
32
- }, [getChatCompletionFunctionDescriptions]);
33
- const { messages, append, reload, stop, isLoading, input, setInput } = useChat({
34
- api: copilotApiConfigExtrapolator(copilotApiConfig).chatApiEndpoint,
35
- id: options.id,
36
- initialMessages: initialMessagesWithContext,
37
- experimental_onFunctionCall: getFunctionCallHandler(),
38
- body: {
39
- id: options.id,
40
- previewToken,
41
- functions: functionDescriptions
42
- }
43
- });
44
- const visibleMessages = messages.filter(
45
- (message) => message.role === "user" || message.role === "assistant"
46
- );
47
- return {
48
- visibleMessages,
49
- append,
50
- reload,
51
- stop,
52
- isLoading,
53
- input,
54
- setInput
55
- };
56
- }
57
- var previewToken = "TODO123";
58
- function defaultSystemMessage(contextString) {
59
- return `
60
- Please act as an efficient, competent, conscientious, and industrious professional assistant.
61
-
62
- Help the user achieve their goals, and you do so in a way that is as efficient as possible, without unnecessary fluff, but also without sacrificing professionalism.
63
- Always be polite and respectful, and prefer brevity over verbosity.
64
-
65
- The user has provided you with the following context:
66
- \`\`\`
67
- ${contextString}
68
- \`\`\`
69
-
70
- They have also provided you with functions you can call to initiate actions on their behalf, or functions you can call to receive more information.
71
-
72
- Please assist them as best you can.
73
-
74
- You can ask them for clarifying questions if needed, but don't be annoying about it. If you can reasonably 'fill in the blanks' yourself, do so.
75
-
76
- If you would like to call a function, call it without saying anything else.
77
- `;
78
- }
79
-
80
- export { defaultSystemMessage, useCopilotChat };
81
- //# sourceMappingURL=out.js.map
82
- //# sourceMappingURL=chunk-6VD2ABXH.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/hooks/use-copilot-chat.ts"],"names":[],"mappings":";;;;;;;;;AAAA,SAAS,SAAS,kBAAkB;AAMpC,SAAS,eAAe;AAuBjB,SAAS,eAAe,IAGiB;AAHjB,eAC7B;AAAA;AAAA,EA9BF,IA6B+B,IAE1B,oBAF0B,IAE1B;AAAA,IADH;AAAA;AAGA,QAAM;AAAA,IACJ;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,IAAI,WAAW,cAAc;AAE7B,QAAM,gBAAyB,QAAQ,MAAM;AAC3C,UAAM,qBAAqB,qBAAqB;AAChD,UAAM,gBAAgB,iBAAiB;AAEvC,WAAO;AAAA,MACL,IAAI;AAAA,MACJ,SAAS,mBAAmB,aAAa;AAAA,MACzC,MAAM;AAAA,IACR;AAAA,EACF,GAAG,CAAC,kBAAkB,iBAAiB,CAAC;AAExC,QAAM,6BAA6B,CAAC,aAAa,EAAE;AAAA,IACjD,QAAQ,mBAAmB,CAAC;AAAA,EAC9B;AAEA,QAAM,uBAAuB,QAAQ,MAAM;AACzC,WAAO,sCAAsC;AAAA,EAC/C,GAAG,CAAC,qCAAqC,CAAC;AAE1C,QAAM,EAAE,UAAU,QAAQ,QAAQ,MAAM,WAAW,OAAO,SAAS,IACjE,QAAQ;AAAA,IACN,KAAK,6BAA6B,gBAAgB,EAAE;AAAA,IACpD,IAAI,QAAQ;AAAA,IACZ,iBAAiB;AAAA,IACjB,6BAA6B,uBAAuB;AAAA,IACpD,MAAM;AAAA,MACJ,IAAI,QAAQ;AAAA,MACZ;AAAA,MACA,WAAW;AAAA,IACb;AAAA,EACF,CAAC;AAEH,QAAM,kBAAkB,SAAS;AAAA,IAC/B,CAAC,YAAY,QAAQ,SAAS,UAAU,QAAQ,SAAS;AAAA,EAC3D;AAEA,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;AAEA,IAAM,eAAe;AAEd,SAAS,qBAAqB,eAA+B;AAClE,SAAO;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQP;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAWF","sourcesContent":["import { useMemo, useContext } from \"react\";\nimport {\n CopilotContext,\n CopilotContextParams,\n copilotApiConfigExtrapolator,\n} from \"../context/copilot-context\";\nimport { useChat } from \"ai/react\";\nimport { ChatRequestOptions, CreateMessage, Message } from \"ai\";\nimport { UseChatOptions } from \"ai\";\n\nexport interface UseCopilotChatOptions extends UseChatOptions {\n makeSystemMessage?: (contextString: string) => string;\n}\n\nexport interface UseCopilotChatReturn {\n visibleMessages: Message[];\n append: (\n message: Message | CreateMessage,\n chatRequestOptions?: ChatRequestOptions\n ) => Promise<string | null | undefined>;\n reload: (\n chatRequestOptions?: ChatRequestOptions\n ) => Promise<string | null | undefined>;\n stop: () => void;\n isLoading: boolean;\n input: string;\n setInput: React.Dispatch<React.SetStateAction<string>>;\n}\n\nexport function useCopilotChat({\n makeSystemMessage,\n ...options\n}: UseCopilotChatOptions): UseCopilotChatReturn {\n const {\n getContextString,\n getChatCompletionFunctionDescriptions,\n getFunctionCallHandler,\n copilotApiConfig,\n } = useContext(CopilotContext);\n\n const systemMessage: Message = useMemo(() => {\n const systemMessageMaker = makeSystemMessage || defaultSystemMessage;\n const contextString = getContextString();\n\n return {\n id: \"system\",\n content: systemMessageMaker(contextString),\n role: \"system\",\n };\n }, [getContextString, makeSystemMessage]);\n\n const initialMessagesWithContext = [systemMessage].concat(\n options.initialMessages || []\n );\n\n const functionDescriptions = useMemo(() => {\n return getChatCompletionFunctionDescriptions();\n }, [getChatCompletionFunctionDescriptions]);\n\n const { messages, append, reload, stop, isLoading, input, setInput } =\n useChat({\n api: copilotApiConfigExtrapolator(copilotApiConfig).chatApiEndpoint,\n id: options.id,\n initialMessages: initialMessagesWithContext,\n experimental_onFunctionCall: getFunctionCallHandler(),\n body: {\n id: options.id,\n previewToken,\n functions: functionDescriptions,\n },\n });\n\n const visibleMessages = messages.filter(\n (message) => message.role === \"user\" || message.role === \"assistant\"\n );\n\n return {\n visibleMessages,\n append,\n reload,\n stop,\n isLoading,\n input,\n setInput,\n };\n}\n\nconst previewToken = \"TODO123\";\n\nexport function defaultSystemMessage(contextString: string): string {\n return `\nPlease act as an efficient, competent, conscientious, and industrious professional assistant.\n\nHelp the user achieve their goals, and you do so in a way that is as efficient as possible, without unnecessary fluff, but also without sacrificing professionalism.\nAlways be polite and respectful, and prefer brevity over verbosity.\n\nThe user has provided you with the following context:\n\\`\\`\\`\n${contextString}\n\\`\\`\\`\n\nThey have also provided you with functions you can call to initiate actions on their behalf, or functions you can call to receive more information.\n\nPlease assist them as best you can.\n\nYou can ask them for clarifying questions if needed, but don't be annoying about it. If you can reasonably 'fill in the blanks' yourself, do so.\n\nIf you would like to call a function, call it without saying anything else.\n`;\n}\n"]}
@@ -1,3 +0,0 @@
1
-
2
- //# sourceMappingURL=out.js.map
3
- //# sourceMappingURL=chunk-BVQRDAR7.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":[],"names":[],"mappings":""}