@copilotkit/react-core 0.5.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/.turbo/turbo-build.log +45 -38
  2. package/.turbo/turbo-check-types.log +4 -0
  3. package/CHANGELOG.md +12 -0
  4. package/dist/{chunk-YVXQVXMS.mjs → chunk-3BOHSSKR.mjs} +46 -16
  5. package/dist/chunk-3BOHSSKR.mjs.map +1 -0
  6. package/dist/{chunk-GJUAS6I7.mjs → chunk-FWAPH3V2.mjs} +2 -2
  7. package/dist/{chunk-7IPFT6UY.mjs → chunk-HZM7VSAI.mjs} +11 -8
  8. package/dist/chunk-HZM7VSAI.mjs.map +1 -0
  9. package/dist/{chunk-7TCGEDGA.mjs → chunk-MYPA66N5.mjs} +4 -4
  10. package/dist/chunk-MYPA66N5.mjs.map +1 -0
  11. package/dist/{chunk-OMI7JPW5.mjs → chunk-QC54QNRW.mjs} +2 -2
  12. package/dist/chunk-YOYU25BE.mjs +26 -0
  13. package/dist/chunk-YOYU25BE.mjs.map +1 -0
  14. package/dist/components/copilot-provider.mjs +3 -3
  15. package/dist/components/index.mjs +3 -3
  16. package/dist/context/copilot-context.d.ts +7 -7
  17. package/dist/context/copilot-context.mjs +1 -1
  18. package/dist/context/index.d.ts +3 -3
  19. package/dist/context/index.mjs +1 -1
  20. package/dist/hooks/index.mjs +4 -4
  21. package/dist/hooks/use-copilot-chat.mjs +2 -2
  22. package/dist/hooks/use-make-copilot-actionable.mjs +2 -2
  23. package/dist/hooks/use-make-copilot-readable.d.ts +8 -1
  24. package/dist/hooks/use-make-copilot-readable.mjs +2 -2
  25. package/dist/hooks/use-tree.d.ts +3 -2
  26. package/dist/hooks/use-tree.mjs +1 -1
  27. package/dist/index.d.ts +2 -2
  28. package/dist/index.mjs +6 -6
  29. package/dist/utils/utils.d.ts +1 -0
  30. package/dist/utils/utils.mjs +3 -0
  31. package/dist/utils/utils.mjs.map +1 -0
  32. package/dist/utils/utils.test.d.ts +1 -0
  33. package/dist/utils/utils.test.mjs +8 -0
  34. package/dist/utils/utils.test.mjs.map +1 -0
  35. package/jest.config.js +5 -0
  36. package/package.json +9 -5
  37. package/src/components/copilot-provider.tsx +16 -13
  38. package/src/context/copilot-context.tsx +24 -10
  39. package/src/hooks/use-make-copilot-readable.ts +11 -3
  40. package/src/hooks/use-tree.ts +67 -17
  41. package/src/utils/utils.test.ts +7 -0
  42. package/src/utils/utils.ts +1 -0
  43. package/tsconfig.json +8 -1
  44. package/tsup.config.ts +5 -0
  45. package/LICENSE +0 -21
  46. package/dist/chunk-7IPFT6UY.mjs.map +0 -1
  47. package/dist/chunk-7TCGEDGA.mjs.map +0 -1
  48. package/dist/chunk-UABEP4UT.mjs +0 -22
  49. package/dist/chunk-UABEP4UT.mjs.map +0 -1
  50. package/dist/chunk-YVXQVXMS.mjs.map +0 -1
  51. /package/dist/{chunk-GJUAS6I7.mjs.map → chunk-FWAPH3V2.mjs.map} +0 -0
  52. /package/dist/{chunk-OMI7JPW5.mjs.map → chunk-QC54QNRW.mjs.map} +0 -0
@@ -1,77 +1,84 @@
1
1
 
2
- > @copilotkit/react-core@0.4.0 build /Users/ataibarkai/LocalGit/Recursively.ai/CopilotKit/packages/react-core
2
+ > @copilotkit/react-core@0.6.0 build /Users/ataibarkai/LocalGit/Recursively.ai/CopilotKit/CopilotKit/packages/react-core
3
3
  > tsup --treeshake
4
4
 
5
- CLI Building entry: src/index.tsx, src/components/copilot-provider.tsx, src/components/index.ts, src/context/copilot-context.tsx, src/context/index.ts, src/hooks/index.ts, src/hooks/use-copilot-chat.ts, src/hooks/use-make-copilot-actionable.ts, src/hooks/use-make-copilot-readable.ts, src/hooks/use-tree.ts, src/types/annotated-function.ts, src/types/index.ts
5
+ CLI Building entry: src/index.tsx, src/components/copilot-provider.tsx, src/components/index.ts, src/context/copilot-context.tsx, src/context/index.ts, src/hooks/index.ts, src/hooks/use-copilot-chat.ts, src/hooks/use-make-copilot-actionable.ts, src/hooks/use-make-copilot-readable.ts, src/hooks/use-tree.ts, src/utils/utils.test.ts, src/utils/utils.ts, src/types/annotated-function.ts, src/types/index.ts
6
6
  CLI Using tsconfig: tsconfig.json
7
7
  CLI tsup v6.5.0
8
- CLI Using tsup config: /Users/ataibarkai/LocalGit/Recursively.ai/CopilotKit/packages/react-core/tsup.config.ts
8
+ CLI Using tsup config: /Users/ataibarkai/LocalGit/Recursively.ai/CopilotKit/CopilotKit/packages/react-core/tsup.config.ts
9
9
  CLI Target: es6
10
10
  CLI Cleaning output folder
11
11
  ESM Build start
12
+ Generated an empty chunk: "utils".
12
13
  Generated an empty chunk: "annotated-function".
13
14
  Generated an empty chunk: "chunk-EFZPSZWO".
14
15
  Generated an empty chunk: "chunk-JD7BAH7U".
15
16
  Generated an empty chunk: "chunk-VNRDQJXW".
16
17
  Generated an empty chunk: "chunk-BVQRDAR7".
17
- ESM dist/chunk-JD7BAH7U.mjs 76.00 B
18
+ ESM dist/chunk-EFZPSZWO.mjs 76.00 B
18
19
  ESM dist/hooks/use-make-copilot-readable.mjs 214.00 B
20
+ ESM dist/chunk-BVQRDAR7.mjs 76.00 B
21
+ ESM dist/index.mjs 546.00 B
19
22
  ESM dist/types/index.mjs 98.00 B
23
+ ESM dist/hooks/use-tree.mjs 170.00 B
20
24
  ESM dist/components/copilot-provider.mjs 230.00 B
21
- ESM dist/context/index.mjs 186.00 B
22
- ESM dist/index.mjs 546.00 B
23
- ESM dist/chunk-EFZPSZWO.mjs 76.00 B
24
- ESM dist/chunk-BVQRDAR7.mjs 76.00 B
25
- ESM dist/chunk-7IPFT6UY.mjs 3.56 KB
26
- ESM dist/chunk-VNRDQJXW.mjs 76.00 B
27
- ESM dist/chunk-YVXQVXMS.mjs 3.28 KB
28
- ESM dist/components/index.mjs 251.00 B
29
- ESM dist/chunk-7TCGEDGA.mjs 602.00 B
25
+ ESM dist/chunk-JD7BAH7U.mjs 76.00 B
26
+ ESM dist/chunk-HZM7VSAI.mjs 3.65 KB
30
27
  ESM dist/chunk-MRXNTQOX.mjs 1.92 KB
28
+ ESM dist/components/index.mjs 251.00 B
31
29
  ESM dist/hooks/index.mjs 348.00 B
32
- ESM dist/hooks/use-tree.mjs 170.00 B
33
- ESM dist/chunk-UABEP4UT.mjs 570.00 B
34
- ESM dist/chunk-GJUAS6I7.mjs 2.59 KB
35
- ESM dist/hooks/use-make-copilot-actionable.mjs 218.00 B
30
+ ESM dist/chunk-MYPA66N5.mjs 626.00 B
36
31
  ESM dist/hooks/use-copilot-chat.mjs 219.00 B
37
- ESM dist/chunk-OMI7JPW5.mjs 972.00 B
32
+ ESM dist/chunk-FWAPH3V2.mjs 2.59 KB
33
+ ESM dist/context/index.mjs 186.00 B
38
34
  ESM dist/context/copilot-context.mjs 164.00 B
35
+ ESM dist/hooks/use-make-copilot-actionable.mjs 218.00 B
36
+ ESM dist/chunk-QC54QNRW.mjs 972.00 B
37
+ ESM dist/chunk-3BOHSSKR.mjs 3.95 KB
38
+ ESM dist/chunk-VNRDQJXW.mjs 76.00 B
39
+ ESM dist/chunk-YOYU25BE.mjs 723.00 B
40
+ ESM dist/utils/utils.test.mjs 202.00 B
39
41
  ESM dist/types/annotated-function.mjs 80.00 B
40
- ESM dist/chunk-JD7BAH7U.mjs.map 51.00 B
42
+ ESM dist/utils/utils.mjs 67.00 B
41
43
  ESM dist/hooks/use-make-copilot-readable.mjs.map 51.00 B
42
- ESM dist/components/copilot-provider.mjs.map 51.00 B
44
+ ESM dist/chunk-EFZPSZWO.mjs.map 51.00 B
45
+ ESM dist/hooks/use-tree.mjs.map 51.00 B
43
46
  ESM dist/index.mjs.map 51.00 B
44
- ESM dist/chunk-VNRDQJXW.mjs.map 51.00 B
45
- ESM dist/chunk-7IPFT6UY.mjs.map 7.04 KB
47
+ ESM dist/chunk-JD7BAH7U.mjs.map 51.00 B
46
48
  ESM dist/chunk-BVQRDAR7.mjs.map 51.00 B
47
- ESM dist/chunk-YVXQVXMS.mjs.map 6.82 KB
48
- ESM dist/types/index.mjs.map 51.00 B
49
- ESM dist/chunk-7TCGEDGA.mjs.map 1015.00 B
49
+ ESM dist/chunk-HZM7VSAI.mjs.map 7.23 KB
50
50
  ESM dist/chunk-MRXNTQOX.mjs.map 71.00 B
51
- ESM dist/chunk-UABEP4UT.mjs.map 1.59 KB
52
- ESM dist/chunk-EFZPSZWO.mjs.map 51.00 B
53
- ESM dist/hooks/use-tree.mjs.map 51.00 B
54
- ESM dist/chunk-GJUAS6I7.mjs.map 4.43 KB
51
+ ESM dist/chunk-MYPA66N5.mjs.map 1.47 KB
52
+ ESM dist/hooks/use-copilot-chat.mjs.map 51.00 B
53
+ ESM dist/types/index.mjs.map 51.00 B
55
54
  ESM dist/hooks/index.mjs.map 51.00 B
55
+ ESM dist/chunk-FWAPH3V2.mjs.map 4.43 KB
56
+ ESM dist/components/copilot-provider.mjs.map 51.00 B
57
+ ESM dist/chunk-QC54QNRW.mjs.map 1.70 KB
58
+ ESM dist/context/copilot-context.mjs.map 51.00 B
59
+ ESM dist/chunk-3BOHSSKR.mjs.map 8.39 KB
56
60
  ESM dist/context/index.mjs.map 51.00 B
57
- ESM dist/hooks/use-make-copilot-actionable.mjs.map 51.00 B
58
- ESM dist/hooks/use-copilot-chat.mjs.map 51.00 B
59
- ESM dist/chunk-OMI7JPW5.mjs.map 1.70 KB
61
+ ESM dist/chunk-VNRDQJXW.mjs.map 51.00 B
60
62
  ESM dist/components/index.mjs.map 51.00 B
61
- ESM dist/context/copilot-context.mjs.map 51.00 B
63
+ ESM dist/chunk-YOYU25BE.mjs.map 2.06 KB
64
+ ESM dist/hooks/use-make-copilot-actionable.mjs.map 51.00 B
65
+ ESM dist/utils/utils.test.mjs.map 351.00 B
66
+ ESM dist/utils/utils.mjs.map 51.00 B
62
67
  ESM dist/types/annotated-function.mjs.map 51.00 B
63
- ESM ⚡️ Build success in 82ms
68
+ ESM ⚡️ Build success in 89ms
64
69
  DTS Build start
65
- DTS ⚡️ Build success in 487ms
70
+ DTS ⚡️ Build success in 824ms
66
71
  DTS dist/index.d.ts 607.00 B
67
72
  DTS dist/components/copilot-provider.d.ts 157.00 B
68
- DTS dist/context/copilot-context.d.ts 868.00 B
73
+ DTS dist/context/copilot-context.d.ts 919.00 B
69
74
  DTS dist/hooks/use-copilot-chat.d.ts 890.00 B
70
75
  DTS dist/hooks/use-make-copilot-actionable.d.ts 255.00 B
71
- DTS dist/hooks/use-make-copilot-readable.d.ts 137.00 B
76
+ DTS dist/hooks/use-make-copilot-readable.d.ts 598.00 B
72
77
  DTS dist/types/annotated-function.d.ts 767.00 B
73
- DTS dist/hooks/use-tree.d.ts 472.00 B
78
+ DTS dist/hooks/use-tree.d.ts 543.00 B
74
79
  DTS dist/components/index.d.ts 73.00 B
75
80
  DTS dist/context/index.d.ts 210.00 B
76
81
  DTS dist/hooks/index.d.ts 305.00 B
82
+ DTS dist/utils/utils.test.d.ts 1.00 B
83
+ DTS dist/utils/utils.d.ts 1.00 B
77
84
  DTS dist/types/index.d.ts 88.00 B
@@ -0,0 +1,4 @@
1
+
2
+ > @copilotkit/react-core@0.6.0 check-types /Users/ataibarkai/LocalGit/Recursively.ai/CopilotKit/CopilotKit/packages/react-core
3
+ > tsc --noEmit
4
+
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # ui
2
2
 
3
+ ## 0.7.0
4
+
5
+ ### Minor Changes
6
+
7
+ - ce193f7: Dependency fix
8
+
9
+ ## 0.6.0
10
+
11
+ ### Minor Changes
12
+
13
+ - Introduced CopilotTextarea
14
+
3
15
  ## 0.5.0
4
16
 
5
17
  ### Minor Changes
@@ -32,7 +32,7 @@ var treeIndentationRepresentation = (index, indentLevel) => {
32
32
  } else if (indentLevel === 2) {
33
33
  return String.fromCharCode(97 + index);
34
34
  } else {
35
- throw new Error("Indentation level not supported");
35
+ return "-";
36
36
  }
37
37
  };
38
38
  var printNode = (node, prefix = "", indentLevel = 0) => {
@@ -48,10 +48,14 @@ var printNode = (node, prefix = "", indentLevel = 0) => {
48
48
  output += `${outputSubsequentLines}
49
49
  `;
50
50
  }
51
+ const childPrePrefix = " ".repeat(prefix.length);
51
52
  node.children.forEach(
52
53
  (child, index) => output += printNode(
53
54
  child,
54
- `${prefix}${treeIndentationRepresentation(index, indentLevel + 1)}. `,
55
+ `${childPrePrefix}${treeIndentationRepresentation(
56
+ index,
57
+ indentLevel + 1
58
+ )}. `,
55
59
  indentLevel + 1
56
60
  )
57
61
  );
@@ -64,7 +68,8 @@ function treeReducer(state, action) {
64
68
  const newNode = {
65
69
  id: newNodeId,
66
70
  value,
67
- children: []
71
+ children: [],
72
+ categories: new Set(action.categories)
68
73
  };
69
74
  try {
70
75
  return addNode(state, newNode, parentId);
@@ -82,9 +87,15 @@ function treeReducer(state, action) {
82
87
  var useTree = () => {
83
88
  const [tree, dispatch] = useReducer(treeReducer, []);
84
89
  const addElement = useCallback(
85
- (value, parentId) => {
90
+ (value, categories, parentId) => {
86
91
  const newNodeId = nanoid();
87
- dispatch({ type: "ADD_NODE", value, parentId, id: newNodeId });
92
+ dispatch({
93
+ type: "ADD_NODE",
94
+ value,
95
+ parentId,
96
+ id: newNodeId,
97
+ categories
98
+ });
88
99
  return newNodeId;
89
100
  },
90
101
  []
@@ -92,20 +103,39 @@ var useTree = () => {
92
103
  const removeElement = useCallback((id) => {
93
104
  dispatch({ type: "REMOVE_NODE", id });
94
105
  }, []);
95
- const printTree = () => {
96
- let output = "";
97
- tree.forEach(
98
- (node, index) => output += printNode(
99
- node,
100
- `${treeIndentationRepresentation(index, 0)}. `
101
- )
102
- );
103
- return output;
104
- };
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
+ );
105
126
  return { tree, addElement, printTree, removeElement };
106
127
  };
107
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
+ }
108
138
 
109
139
  export { use_tree_default };
110
140
  //# sourceMappingURL=out.js.map
111
- //# sourceMappingURL=chunk-YVXQVXMS.mjs.map
141
+ //# sourceMappingURL=chunk-3BOHSSKR.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/hooks/use-tree.ts"],"names":[],"mappings":";;;;;;AAAA,SAAS,cAAc;AACvB,SAAS,aAAa,kBAAkB;AAsCxC,IAAM,aAAa,CAAC,OAAa,OAAyB;AACxD,SAAO,MAAM,OAAO,CAAC,QAAc,SAAS;AAC1C,QAAI,KAAK,OAAO,IAAI;AAClB,YAAM,UAAU,iCAAK,OAAL,EAAW,UAAU,WAAW,KAAK,UAAU,EAAE,EAAE;AACnE,aAAO,KAAK,OAAO;AAAA,IACrB;AACA,WAAO;AAAA,EACT,GAAG,CAAC,CAAC;AACP;AAEA,IAAM,UAAU,CACd,OACA,SACA,aACS;AACT,MAAI,CAAC,UAAU;AACb,WAAO,CAAC,GAAG,OAAO,OAAO;AAAA,EAC3B;AACA,SAAO,MAAM,IAAI,CAAC,SAAS;AACzB,QAAI,KAAK,OAAO,UAAU;AACxB,aAAO,iCAAK,OAAL,EAAW,UAAU,CAAC,GAAG,KAAK,UAAU,OAAO,EAAE;AAAA,IAC1D,WAAW,KAAK,SAAS,QAAQ;AAC/B,aAAO,iCAAK,OAAL,EAAW,UAAU,QAAQ,KAAK,UAAU,SAAS,QAAQ,EAAE;AAAA,IACxE;AACA,WAAO;AAAA,EACT,CAAC;AACH;AAEA,IAAM,gCAAgC,CACpC,OACA,gBACW;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AI;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,4 +1,4 @@
1
- import { CopilotContext } from './chunk-UABEP4UT.mjs';
1
+ import { CopilotContext } from './chunk-YOYU25BE.mjs';
2
2
  import { __objRest } from './chunk-MRXNTQOX.mjs';
3
3
  import { useContext, useMemo } from 'react';
4
4
  import { useChat } from 'ai/react';
@@ -77,4 +77,4 @@ If you would like to call a function, call it without saying anything else.
77
77
 
78
78
  export { defaultSystemMessage, useCopilotChat };
79
79
  //# sourceMappingURL=out.js.map
80
- //# sourceMappingURL=chunk-GJUAS6I7.mjs.map
80
+ //# sourceMappingURL=chunk-FWAPH3V2.mjs.map
@@ -1,5 +1,5 @@
1
- import { use_tree_default } from './chunk-YVXQVXMS.mjs';
2
- import { CopilotContext } from './chunk-UABEP4UT.mjs';
1
+ import { use_tree_default } from './chunk-3BOHSSKR.mjs';
2
+ import { CopilotContext } from './chunk-YOYU25BE.mjs';
3
3
  import { __spreadProps, __spreadValues, __async, __objRest } from './chunk-MRXNTQOX.mjs';
4
4
  import { useState, useCallback } from 'react';
5
5
  import { jsx } from 'react/jsx-runtime';
@@ -26,12 +26,15 @@ function CopilotProvider({
26
26
  return newPoints;
27
27
  });
28
28
  }, []);
29
- const getContextString = useCallback(() => {
30
- return printTree();
31
- }, [printTree]);
29
+ const getContextString = useCallback(
30
+ (categories = ["global"]) => {
31
+ return printTree(categories);
32
+ },
33
+ [printTree]
34
+ );
32
35
  const addContext = useCallback(
33
- (context, parentId) => {
34
- return addElement(context, parentId);
36
+ (context, parentId, categories = ["global"]) => {
37
+ return addElement(context, categories, parentId);
35
38
  },
36
39
  [addElement]
37
40
  );
@@ -112,4 +115,4 @@ function annotatedFunctionToChatCompletionFunction(annotatedFunction) {
112
115
 
113
116
  export { CopilotProvider };
114
117
  //# sourceMappingURL=out.js.map
115
- //# sourceMappingURL=chunk-7IPFT6UY.mjs.map
118
+ //# sourceMappingURL=chunk-HZM7VSAI.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/components/copilot-provider.tsx"],"names":[],"mappings":";;;;;;;;;;;;;;AAGA,SAAoB,aAAa,gBAAgB;AAkE7C;AArEJ;AAQO,SAAS,gBAAgB;AAAA,EAC9B;AACF,GAEgB;AACd,QAAM,CAAC,aAAa,cAAc,IAAI,SAEpC,CAAC,CAAC;AAEJ,QAAM,EAAE,YAAY,eAAe,UAAU,IAAI,iBAAQ;AAEzD,QAAM,gBAAgB;AAAA,IACpB,CAAC,IAAY,eAAyC;AACpD,qBAAe,CAAC,eAAe;AAC7B,eAAO,iCACF,aADE;AAAA,UAEL,CAAC,KAAK;AAAA,QACR;AAAA,MACF,CAAC;AAAA,IACH;AAAA,IACA,CAAC;AAAA,EACH;AAEA,QAAM,mBAAmB,YAAY,CAAC,OAAe;AACnD,mBAAe,CAAC,eAAe;AAC7B,YAAM,YAAY,mBAAK;AACvB,aAAO,UAAU;AACjB,aAAO;AAAA,IACT,CAAC;AAAA,EACH,GAAG,CAAC,CAAC;AAEL,QAAM,mBAAmB;AAAA,IACvB,CAAC,aAAuB,CAAC,QAAQ,MAAM;AACrC,aAAO,UAAU,UAAU;AAAA,IAC7B;AAAA,IACA,CAAC,SAAS;AAAA,EACZ;AAEA,QAAM,aAAa;AAAA,IACjB,CAAC,SAAiB,UAAmB,aAAuB,CA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;AAC1C,WAAS,OAAO,kBAAkB,qBAAqB;AAErD,QAA2C,UAArC,QAAM,SAvJhB,IAuJ+C,IAAlB,0BAAkB,IAAlB,CAAnB,QAAM;AACZ,eAAW,IAAI,QAAQ;AAAA,EACzB;AAEA,MAAI,yBAAmC,CAAC;AACxC,WAAS,OAAO,kBAAkB,qBAAqB;AACrD,QAAI,IAAI,UAAU;AAChB,6BAAuB,KAAK,IAAI,IAAI;AAAA,IACtC;AAAA,EACF;AAGA,MAAI,yBAA0D;AAAA,IAC5D,MAAM,kBAAkB;AAAA,IACxB,aAAa,kBAAkB;AAAA,IAC/B,YAAY;AAAA,MACV,MAAM;AAAA,MACN,YAAY;AAAA,MACZ,UAAU;AAAA,IACZ;AAAA,EACF;AAEA,SAAO;AACT","sourcesContent":["\"use client\";\nimport { FunctionCallHandler } from \"ai\";\nimport { CompletionCreateParams } from \"openai/resources/chat\";\nimport { ReactNode, useCallback, useState } from \"react\";\nimport { CopilotContext } from \"../context/copilot-context\";\nimport useTree from \"../hooks/use-tree\";\nimport { AnnotatedFunction } from \"../types/annotated-function\";\n\nexport function CopilotProvider({\n children,\n}: {\n children: ReactNode;\n}): JSX.Element {\n const [entryPoints, setEntryPoints] = useState<\n Record<string, AnnotatedFunction<any[]>>\n >({});\n\n const { addElement, removeElement, printTree } = useTree();\n\n const setEntryPoint = useCallback(\n (id: string, entryPoint: AnnotatedFunction<any[]>) => {\n setEntryPoints((prevPoints) => {\n return {\n ...prevPoints,\n [id]: entryPoint,\n };\n });\n },\n []\n );\n\n const removeEntryPoint = useCallback((id: string) => {\n setEntryPoints((prevPoints) => {\n const newPoints = { ...prevPoints };\n delete newPoints[id];\n return newPoints;\n });\n }, []);\n\n const getContextString = useCallback(\n (categories: string[] = [\"global\"]) => {\n return printTree(categories);\n },\n [printTree]\n );\n\n const addContext = useCallback(\n (context: string, parentId?: string, categories: string[] = [\"global\"]) => {\n return addElement(context, categories, parentId);\n },\n [addElement]\n );\n\n const removeContext = useCallback(\n (id: string) => {\n removeElement(id);\n },\n [removeElement]\n );\n\n const getChatCompletionFunctionDescriptions = useCallback(() => {\n return entryPointsToChatCompletionFunctions(Object.values(entryPoints));\n }, [entryPoints]);\n\n const getFunctionCallHandler = useCallback(() => {\n return entryPointsToFunctionCallHandler(Object.values(entryPoints));\n }, [entryPoints]);\n\n return (\n <CopilotContext.Provider\n value={{\n entryPoints,\n getChatCompletionFunctionDescriptions,\n getFunctionCallHandler,\n setEntryPoint,\n removeEntryPoint,\n getContextString,\n addContext,\n removeContext,\n }}\n >\n {children}\n </CopilotContext.Provider>\n );\n}\n\nfunction entryPointsToFunctionCallHandler(\n entryPoints: AnnotatedFunction<any[]>[]\n): FunctionCallHandler {\n return async (chatMessages, functionCall) => {\n let entrypointsByFunctionName: Record<\n string,\n AnnotatedFunction<any[]>\n > = {};\n for (let entryPoint of entryPoints) {\n entrypointsByFunctionName[entryPoint.name] = entryPoint;\n }\n\n const entryPointFunction =\n entrypointsByFunctionName[functionCall.name || \"\"];\n if (entryPointFunction) {\n let parsedFunctionCallArguments: Record<string, any>[] = [];\n if (functionCall.arguments) {\n parsedFunctionCallArguments = JSON.parse(functionCall.arguments);\n }\n\n const paramsInCorrectOrder: any[] = [];\n for (let arg of entryPointFunction.argumentAnnotations) {\n paramsInCorrectOrder.push(\n parsedFunctionCallArguments[\n arg.name as keyof typeof parsedFunctionCallArguments\n ]\n );\n }\n\n await entryPointFunction.implementation(...paramsInCorrectOrder);\n\n // commented out becasue for now we don't want to return anything\n // const result = await entryPointFunction.implementation(\n // ...parsedFunctionCallArguments\n // );\n // const functionResponse: ChatRequest = {\n // messages: [\n // ...chatMessages,\n // {\n // id: nanoid(),\n // name: functionCall.name,\n // role: 'function' as const,\n // content: JSON.stringify(result),\n // },\n // ],\n // };\n\n // return functionResponse;\n }\n };\n}\n\nfunction entryPointsToChatCompletionFunctions(\n entryPoints: AnnotatedFunction<any[]>[]\n): CompletionCreateParams.Function[] {\n return entryPoints.map(annotatedFunctionToChatCompletionFunction);\n}\n\nfunction annotatedFunctionToChatCompletionFunction(\n annotatedFunction: AnnotatedFunction<any[]>\n): CompletionCreateParams.Function {\n // Create the parameters object based on the argumentAnnotations\n let parameters: { [key: string]: any } = {};\n for (let arg of annotatedFunction.argumentAnnotations) {\n // isolate the args we should forward inline\n let { name, required, ...forwardedArgs } = arg;\n parameters[arg.name] = forwardedArgs;\n }\n\n let requiredParameterNames: string[] = [];\n for (let arg of annotatedFunction.argumentAnnotations) {\n if (arg.required) {\n requiredParameterNames.push(arg.name);\n }\n }\n\n // Create the ChatCompletionFunctions object\n let chatCompletionFunction: CompletionCreateParams.Function = {\n name: annotatedFunction.name,\n description: annotatedFunction.description,\n parameters: {\n type: \"object\",\n properties: parameters,\n required: requiredParameterNames,\n },\n };\n\n return chatCompletionFunction;\n}\n"]}
@@ -1,11 +1,11 @@
1
- import { CopilotContext } from './chunk-UABEP4UT.mjs';
1
+ import { CopilotContext } from './chunk-YOYU25BE.mjs';
2
2
  import { useContext, useRef, useEffect } from 'react';
3
3
 
4
- function useMakeCopilotReadable(information, parentId) {
4
+ function useMakeCopilotReadable(information, parentId, categories) {
5
5
  const { addContext, removeContext } = useContext(CopilotContext);
6
6
  const idRef = useRef();
7
7
  useEffect(() => {
8
- const id = addContext(information, parentId);
8
+ const id = addContext(information, parentId, categories);
9
9
  idRef.current = id;
10
10
  return () => {
11
11
  removeContext(id);
@@ -16,4 +16,4 @@ function useMakeCopilotReadable(information, parentId) {
16
16
 
17
17
  export { useMakeCopilotReadable };
18
18
  //# sourceMappingURL=out.js.map
19
- //# sourceMappingURL=chunk-7TCGEDGA.mjs.map
19
+ //# sourceMappingURL=chunk-MYPA66N5.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/hooks/use-make-copilot-readable.ts"],"names":[],"mappings":";;;;;AAEA,SAAS,YAAY,WAAW,cAAc;AAF9C;AAYO,SAAS,uBACd,aACA,UACA,YACoB;AACpB,QAAM,EAAE,YAAY,cAAc,IAAI,WAAW,cAAc;AAC/D,QAAM,QAAQ,OAAe;AAE7B,YAAU,MAAM;AACd,UAAM,KAAK,WAAW,aAAa,UAAU,UAAU;AACvD,UAAM,UAAU;AAEhB,WAAO,MAAM;AACX,oBAAc,EAAE;AAAA,IAClB;AAAA,EACF,GAAG,CAAC,aAAa,UAAU,YAAY,aAAa,CAAC;AAErD,SAAO,MAAM;AACf","sourcesContent":["\"use client\";\n\nimport { useContext, useEffect, useRef } from \"react\";\nimport { CopilotContext } from \"../context/copilot-context\";\n\n/**\n * Adds the given information to the Copilot context to make it readable by Copilot.\n * @param information - The information to be added to the Copilot context.\n * @param parentId - The ID of the parent context, if any.\n * @param categories - An array of categories to control which context are visible where. Particularly useful with CopilotTextarea (see `useMakeAutosuggestionFunction`)\n * @returns The ID of the added context.\n */\nexport function useMakeCopilotReadable(\n information: string,\n parentId?: string,\n categories?: string[]\n): string | undefined {\n const { addContext, removeContext } = useContext(CopilotContext);\n const idRef = useRef<string>();\n\n useEffect(() => {\n const id = addContext(information, parentId, categories);\n idRef.current = id;\n\n return () => {\n removeContext(id);\n };\n }, [information, parentId, addContext, removeContext]);\n\n return idRef.current;\n}\n"]}
@@ -1,4 +1,4 @@
1
- import { CopilotContext } from './chunk-UABEP4UT.mjs';
1
+ import { CopilotContext } from './chunk-YOYU25BE.mjs';
2
2
  import { useRef, useContext, useMemo, useEffect } from 'react';
3
3
  import { nanoid } from 'nanoid';
4
4
 
@@ -27,4 +27,4 @@ function useMakeCopilotActionable(annotatedFunction, dependencies) {
27
27
 
28
28
  export { useMakeCopilotActionable };
29
29
  //# sourceMappingURL=out.js.map
30
- //# sourceMappingURL=chunk-OMI7JPW5.mjs.map
30
+ //# sourceMappingURL=chunk-QC54QNRW.mjs.map
@@ -0,0 +1,26 @@
1
+ import './chunk-MRXNTQOX.mjs';
2
+ import React from 'react';
3
+
4
+ var emptyCopilotContext = {
5
+ entryPoints: {},
6
+ setEntryPoint: () => {
7
+ },
8
+ removeEntryPoint: () => {
9
+ },
10
+ getChatCompletionFunctionDescriptions: () => returnAndThrowInDebug(),
11
+ getFunctionCallHandler: () => returnAndThrowInDebug(),
12
+ getContextString: () => returnAndThrowInDebug(),
13
+ addContext: () => "",
14
+ removeContext: () => {
15
+ }
16
+ };
17
+ var CopilotContext = React.createContext(emptyCopilotContext);
18
+ function returnAndThrowInDebug(value) {
19
+ throw new Error(
20
+ "Remember to wrap your app in a `<CopilotProvider> {...} </CopilotProvider>` !!!"
21
+ );
22
+ }
23
+
24
+ export { CopilotContext };
25
+ //# sourceMappingURL=out.js.map
26
+ //# sourceMappingURL=chunk-YOYU25BE.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/context/copilot-context.tsx"],"names":[],"mappings":";;;;;AAIA,OAAO,WAAW;AAJlB;AA0BA,IAAM,sBAA4C;AAAA,EAChD,aAAa,CAAC;AAAA,EACd,eAAe,MAAM;AAAA,EAAC;AAAA,EACtB,kBAAkB,MAAM;AAAA,EAAC;AAAA,EACzB,uCAAuC,MAAM,sBAAsB,CAAC,CAAC;AAAA,EACrE,wBAAwB,MAAM,sBAAsB,MAAY;AAAA,EAAC,EAAC;AAAA,EAElE,kBAAkB,MAAM,sBAAsB,EAAE;AAAA,EAChD,YAAY,MAAM;AAAA,EAClB,eAAe,MAAM;AAAA,EAAC;AACxB;AAEO,IAAM,iBACX,MAAM,cAAoC,mBAAmB;AAE/D,SAAS,sBAAyB,OAAa;AAC7C,QAAM,IAAI;AAAA,IACR;AAAA,EACF;AACA,SAAO;AACT","sourcesContent":["\"use client\";\n\nimport { FunctionCallHandler } from \"ai\";\nimport { CompletionCreateParams } from \"openai/resources/chat\";\nimport React from \"react\";\nimport { TreeNodeId } from \"../hooks/use-tree\";\nimport { AnnotatedFunction } from \"../types/annotated-function\";\n\nexport interface CopilotContextParams {\n // function-calling\n entryPoints: Record<string, AnnotatedFunction<any[]>>;\n setEntryPoint: (id: string, entryPoint: AnnotatedFunction<any[]>) => void;\n removeEntryPoint: (id: string) => void;\n getChatCompletionFunctionDescriptions: () => CompletionCreateParams.Function[];\n getFunctionCallHandler: () => FunctionCallHandler;\n\n // text context\n getContextString: (categories?: string[]) => string;\n addContext: (\n context: string,\n parentId?: string,\n categories?: string[]\n ) => TreeNodeId;\n removeContext: (id: TreeNodeId) => void;\n}\n\nconst emptyCopilotContext: CopilotContextParams = {\n entryPoints: {},\n setEntryPoint: () => {},\n removeEntryPoint: () => {},\n getChatCompletionFunctionDescriptions: () => returnAndThrowInDebug([]),\n getFunctionCallHandler: () => returnAndThrowInDebug(async () => {}),\n\n getContextString: () => returnAndThrowInDebug(\"\"),\n addContext: () => \"\",\n removeContext: () => {},\n};\n\nexport const CopilotContext =\n React.createContext<CopilotContextParams>(emptyCopilotContext);\n\nfunction returnAndThrowInDebug<T>(value: T): T {\n throw new Error(\n \"Remember to wrap your app in a `<CopilotProvider> {...} </CopilotProvider>` !!!\"\n );\n return value;\n}\n"]}
@@ -1,6 +1,6 @@
1
- export { CopilotProvider } from '../chunk-7IPFT6UY.mjs';
2
- import '../chunk-YVXQVXMS.mjs';
3
- import '../chunk-UABEP4UT.mjs';
1
+ export { CopilotProvider } from '../chunk-HZM7VSAI.mjs';
2
+ import '../chunk-3BOHSSKR.mjs';
3
+ import '../chunk-YOYU25BE.mjs';
4
4
  import '../chunk-MRXNTQOX.mjs';
5
5
  //# sourceMappingURL=out.js.map
6
6
  //# sourceMappingURL=copilot-provider.mjs.map
@@ -1,7 +1,7 @@
1
1
  import '../chunk-JD7BAH7U.mjs';
2
- export { CopilotProvider } from '../chunk-7IPFT6UY.mjs';
3
- import '../chunk-YVXQVXMS.mjs';
4
- import '../chunk-UABEP4UT.mjs';
2
+ export { CopilotProvider } from '../chunk-HZM7VSAI.mjs';
3
+ import '../chunk-3BOHSSKR.mjs';
4
+ import '../chunk-YOYU25BE.mjs';
5
5
  import '../chunk-MRXNTQOX.mjs';
6
6
  //# sourceMappingURL=out.js.map
7
7
  //# sourceMappingURL=index.mjs.map
@@ -1,17 +1,17 @@
1
+ import { FunctionCallHandler } from 'ai';
2
+ import { CompletionCreateParams } from 'openai/resources/chat';
1
3
  import React from 'react';
2
- import { AnnotatedFunction } from '../types/annotated-function.js';
3
4
  import { TreeNodeId } from '../hooks/use-tree.js';
4
- import { ChatCompletionFunctions } from 'openai-edge/types/api';
5
- import { FunctionCallHandler } from 'ai';
5
+ import { AnnotatedFunction } from '../types/annotated-function.js';
6
6
 
7
7
  interface CopilotContextParams {
8
8
  entryPoints: Record<string, AnnotatedFunction<any[]>>;
9
- getChatCompletionFunctionDescriptions: () => ChatCompletionFunctions[];
10
- getFunctionCallHandler: () => FunctionCallHandler;
11
9
  setEntryPoint: (id: string, entryPoint: AnnotatedFunction<any[]>) => void;
12
10
  removeEntryPoint: (id: string) => void;
13
- getContextString: () => string;
14
- addContext: (context: string, parentId?: string) => TreeNodeId;
11
+ getChatCompletionFunctionDescriptions: () => CompletionCreateParams.Function[];
12
+ getFunctionCallHandler: () => FunctionCallHandler;
13
+ getContextString: (categories?: string[]) => string;
14
+ addContext: (context: string, parentId?: string, categories?: string[]) => TreeNodeId;
15
15
  removeContext: (id: TreeNodeId) => void;
16
16
  }
17
17
  declare const CopilotContext: React.Context<CopilotContextParams>;
@@ -1,4 +1,4 @@
1
- export { CopilotContext } from '../chunk-UABEP4UT.mjs';
1
+ export { CopilotContext } from '../chunk-YOYU25BE.mjs';
2
2
  import '../chunk-MRXNTQOX.mjs';
3
3
  //# sourceMappingURL=out.js.map
4
4
  //# sourceMappingURL=copilot-context.mjs.map
@@ -1,6 +1,6 @@
1
1
  export { CopilotContext, CopilotContextParams } from './copilot-context.js';
2
+ import 'ai';
3
+ import 'openai/resources/chat';
2
4
  import 'react';
3
- import '../types/annotated-function.js';
4
5
  import '../hooks/use-tree.js';
5
- import 'openai-edge/types/api';
6
- import 'ai';
6
+ import '../types/annotated-function.js';
@@ -1,5 +1,5 @@
1
1
  import '../chunk-VNRDQJXW.mjs';
2
- export { CopilotContext } from '../chunk-UABEP4UT.mjs';
2
+ export { CopilotContext } from '../chunk-YOYU25BE.mjs';
3
3
  import '../chunk-MRXNTQOX.mjs';
4
4
  //# sourceMappingURL=out.js.map
5
5
  //# sourceMappingURL=index.mjs.map
@@ -1,8 +1,8 @@
1
1
  import '../chunk-BVQRDAR7.mjs';
2
- export { useMakeCopilotReadable } from '../chunk-7TCGEDGA.mjs';
3
- export { useCopilotChat } from '../chunk-GJUAS6I7.mjs';
4
- export { useMakeCopilotActionable } from '../chunk-OMI7JPW5.mjs';
5
- import '../chunk-UABEP4UT.mjs';
2
+ export { useMakeCopilotReadable } from '../chunk-MYPA66N5.mjs';
3
+ export { useCopilotChat } from '../chunk-FWAPH3V2.mjs';
4
+ export { useMakeCopilotActionable } from '../chunk-QC54QNRW.mjs';
5
+ import '../chunk-YOYU25BE.mjs';
6
6
  import '../chunk-MRXNTQOX.mjs';
7
7
  //# sourceMappingURL=out.js.map
8
8
  //# sourceMappingURL=index.mjs.map
@@ -1,5 +1,5 @@
1
- export { defaultSystemMessage, useCopilotChat } from '../chunk-GJUAS6I7.mjs';
2
- import '../chunk-UABEP4UT.mjs';
1
+ export { defaultSystemMessage, useCopilotChat } from '../chunk-FWAPH3V2.mjs';
2
+ import '../chunk-YOYU25BE.mjs';
3
3
  import '../chunk-MRXNTQOX.mjs';
4
4
  //# sourceMappingURL=out.js.map
5
5
  //# sourceMappingURL=use-copilot-chat.mjs.map
@@ -1,5 +1,5 @@
1
- export { useMakeCopilotActionable } from '../chunk-OMI7JPW5.mjs';
2
- import '../chunk-UABEP4UT.mjs';
1
+ export { useMakeCopilotActionable } from '../chunk-QC54QNRW.mjs';
2
+ import '../chunk-YOYU25BE.mjs';
3
3
  import '../chunk-MRXNTQOX.mjs';
4
4
  //# sourceMappingURL=out.js.map
5
5
  //# sourceMappingURL=use-make-copilot-actionable.mjs.map
@@ -1,3 +1,10 @@
1
- declare function useMakeCopilotReadable(information: string, parentId?: string): string | undefined;
1
+ /**
2
+ * Adds the given information to the Copilot context to make it readable by Copilot.
3
+ * @param information - The information to be added to the Copilot context.
4
+ * @param parentId - The ID of the parent context, if any.
5
+ * @param categories - An array of categories to control which context are visible where. Particularly useful with CopilotTextarea (see `useMakeAutosuggestionFunction`)
6
+ * @returns The ID of the added context.
7
+ */
8
+ declare function useMakeCopilotReadable(information: string, parentId?: string, categories?: string[]): string | undefined;
2
9
 
3
10
  export { useMakeCopilotReadable };
@@ -1,5 +1,5 @@
1
- export { useMakeCopilotReadable } from '../chunk-7TCGEDGA.mjs';
2
- import '../chunk-UABEP4UT.mjs';
1
+ export { useMakeCopilotReadable } from '../chunk-MYPA66N5.mjs';
2
+ import '../chunk-YOYU25BE.mjs';
3
3
  import '../chunk-MRXNTQOX.mjs';
4
4
  //# sourceMappingURL=out.js.map
5
5
  //# sourceMappingURL=use-make-copilot-readable.mjs.map
@@ -4,12 +4,13 @@ interface TreeNode {
4
4
  value: string;
5
5
  children: TreeNode[];
6
6
  parentId?: TreeNodeId;
7
+ categories: Set<string>;
7
8
  }
8
9
  type Tree = TreeNode[];
9
10
  interface UseTreeReturn {
10
11
  tree: Tree;
11
- addElement: (value: string, parentId?: TreeNodeId) => TreeNodeId;
12
- printTree: () => string;
12
+ addElement: (value: string, categories: string[], parentId?: TreeNodeId) => TreeNodeId;
13
+ printTree: (categories: string[]) => string;
13
14
  removeElement: (id: TreeNodeId) => void;
14
15
  }
15
16
  declare const useTree: () => UseTreeReturn;
@@ -1,4 +1,4 @@
1
- export { use_tree_default as default } from '../chunk-YVXQVXMS.mjs';
1
+ export { use_tree_default as default } from '../chunk-3BOHSSKR.mjs';
2
2
  import '../chunk-MRXNTQOX.mjs';
3
3
  //# sourceMappingURL=out.js.map
4
4
  //# sourceMappingURL=use-tree.mjs.map
package/dist/index.d.ts CHANGED
@@ -5,6 +5,6 @@ export { useMakeCopilotActionable } from './hooks/use-make-copilot-actionable.js
5
5
  export { useMakeCopilotReadable } from './hooks/use-make-copilot-readable.js';
6
6
  export { AnnotatedFunction, AnnotatedFunctionArgument } from './types/annotated-function.js';
7
7
  import 'react';
8
- import './hooks/use-tree.js';
9
- import 'openai-edge/types/api';
10
8
  import 'ai';
9
+ import 'openai/resources/chat';
10
+ import './hooks/use-tree.js';
package/dist/index.mjs CHANGED
@@ -1,13 +1,13 @@
1
1
  import './chunk-EFZPSZWO.mjs';
2
2
  import './chunk-JD7BAH7U.mjs';
3
- export { CopilotProvider } from './chunk-7IPFT6UY.mjs';
4
- import './chunk-YVXQVXMS.mjs';
3
+ export { CopilotProvider } from './chunk-HZM7VSAI.mjs';
4
+ import './chunk-3BOHSSKR.mjs';
5
5
  import './chunk-VNRDQJXW.mjs';
6
6
  import './chunk-BVQRDAR7.mjs';
7
- export { useMakeCopilotReadable } from './chunk-7TCGEDGA.mjs';
8
- export { useCopilotChat } from './chunk-GJUAS6I7.mjs';
9
- export { useMakeCopilotActionable } from './chunk-OMI7JPW5.mjs';
10
- export { CopilotContext } from './chunk-UABEP4UT.mjs';
7
+ export { useMakeCopilotReadable } from './chunk-MYPA66N5.mjs';
8
+ export { useCopilotChat } from './chunk-FWAPH3V2.mjs';
9
+ export { useMakeCopilotActionable } from './chunk-QC54QNRW.mjs';
10
+ export { CopilotContext } from './chunk-YOYU25BE.mjs';
11
11
  import './chunk-MRXNTQOX.mjs';
12
12
  //# sourceMappingURL=out.js.map
13
13
  //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+
@@ -0,0 +1,3 @@
1
+
2
+ //# sourceMappingURL=out.js.map
3
+ //# sourceMappingURL=utils.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"names":[],"mappings":""}
@@ -0,0 +1 @@
1
+
@@ -0,0 +1,8 @@
1
+ // src/utils/utils.test.ts
2
+ describe("emptyTest", () => {
3
+ it("should be truthy", () => {
4
+ expect(true).toBeTruthy();
5
+ });
6
+ });
7
+ //# sourceMappingURL=out.js.map
8
+ //# sourceMappingURL=utils.test.mjs.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/jest.config.js ADDED
@@ -0,0 +1,5 @@
1
+ /** @type {import('ts-jest').JestConfigWithTsJest} */
2
+ module.exports = {
3
+ preset: 'ts-jest',
4
+ testEnvironment: 'node',
5
+ };
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
- "version": "0.5.0",
7
+ "version": "0.7.0",
8
8
  "sideEffects": false,
9
9
  "main": "./dist/index.js",
10
10
  "module": "./dist/index.mjs",
@@ -15,22 +15,26 @@
15
15
  "react": "^18.2.0"
16
16
  },
17
17
  "devDependencies": {
18
+ "@types/jest": "^29.5.4",
18
19
  "@types/react": "^18.2.5",
19
20
  "eslint": "^7.32.0",
21
+ "jest": "^29.6.4",
20
22
  "react": "^18.2.0",
23
+ "ts-jest": "^29.1.1",
21
24
  "tsup": "^6.5.0",
22
25
  "typescript": "^4.9.4",
23
- "tsconfig": "0.3.0",
24
- "eslint-config-custom": "0.2.0"
26
+ "eslint-config-custom": "0.2.0",
27
+ "tsconfig": "0.3.0"
25
28
  },
26
29
  "dependencies": {
27
- "ai": "^2.1.22",
30
+ "ai": "^2.2.6",
28
31
  "nanoid": "^4.0.2",
29
- "openai-edge": "^1.2.0"
32
+ "openai": "^4.0.0"
30
33
  },
31
34
  "scripts": {
32
35
  "build": "tsup --treeshake",
33
36
  "dev": "tsup --watch --no-splitting",
37
+ "test": "jest",
34
38
  "check-types": "tsc --noEmit",
35
39
  "clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist && rm -rf .next"
36
40
  }
@@ -1,10 +1,10 @@
1
1
  "use client";
2
- import React, { useState, ReactNode, useCallback } from "react";
3
- import { AnnotatedFunction } from "../types/annotated-function";
4
- import useTree from "../hooks/use-tree";
5
- import { CopilotContext } from "../context/copilot-context";
6
2
  import { FunctionCallHandler } from "ai";
7
- import { ChatCompletionFunctions } from "openai-edge/types/api";
3
+ import { CompletionCreateParams } from "openai/resources/chat";
4
+ import { ReactNode, useCallback, useState } from "react";
5
+ import { CopilotContext } from "../context/copilot-context";
6
+ import useTree from "../hooks/use-tree";
7
+ import { AnnotatedFunction } from "../types/annotated-function";
8
8
 
9
9
  export function CopilotProvider({
10
10
  children,
@@ -37,13 +37,16 @@ export function CopilotProvider({
37
37
  });
38
38
  }, []);
39
39
 
40
- const getContextString = useCallback(() => {
41
- return printTree();
42
- }, [printTree]);
40
+ const getContextString = useCallback(
41
+ (categories: string[] = ["global"]) => {
42
+ return printTree(categories);
43
+ },
44
+ [printTree]
45
+ );
43
46
 
44
47
  const addContext = useCallback(
45
- (context: string, parentId?: string) => {
46
- return addElement(context, parentId);
48
+ (context: string, parentId?: string, categories: string[] = ["global"]) => {
49
+ return addElement(context, categories, parentId);
47
50
  },
48
51
  [addElement]
49
52
  );
@@ -135,13 +138,13 @@ function entryPointsToFunctionCallHandler(
135
138
 
136
139
  function entryPointsToChatCompletionFunctions(
137
140
  entryPoints: AnnotatedFunction<any[]>[]
138
- ): ChatCompletionFunctions[] {
141
+ ): CompletionCreateParams.Function[] {
139
142
  return entryPoints.map(annotatedFunctionToChatCompletionFunction);
140
143
  }
141
144
 
142
145
  function annotatedFunctionToChatCompletionFunction(
143
146
  annotatedFunction: AnnotatedFunction<any[]>
144
- ): ChatCompletionFunctions {
147
+ ): CompletionCreateParams.Function {
145
148
  // Create the parameters object based on the argumentAnnotations
146
149
  let parameters: { [key: string]: any } = {};
147
150
  for (let arg of annotatedFunction.argumentAnnotations) {
@@ -158,7 +161,7 @@ function annotatedFunctionToChatCompletionFunction(
158
161
  }
159
162
 
160
163
  // Create the ChatCompletionFunctions object
161
- let chatCompletionFunction: ChatCompletionFunctions = {
164
+ let chatCompletionFunction: CompletionCreateParams.Function = {
162
165
  name: annotatedFunction.name,
163
166
  description: annotatedFunction.description,
164
167
  parameters: {
@@ -1,33 +1,47 @@
1
1
  "use client";
2
2
 
3
+ import { FunctionCallHandler } from "ai";
4
+ import { CompletionCreateParams } from "openai/resources/chat";
3
5
  import React from "react";
4
- import { AnnotatedFunction } from "../types/annotated-function";
5
6
  import { TreeNodeId } from "../hooks/use-tree";
6
- import { ChatCompletionFunctions } from "openai-edge/types/api";
7
- import { FunctionCallHandler } from "ai";
7
+ import { AnnotatedFunction } from "../types/annotated-function";
8
8
 
9
9
  export interface CopilotContextParams {
10
+ // function-calling
10
11
  entryPoints: Record<string, AnnotatedFunction<any[]>>;
11
- getChatCompletionFunctionDescriptions: () => ChatCompletionFunctions[];
12
- getFunctionCallHandler: () => FunctionCallHandler;
13
12
  setEntryPoint: (id: string, entryPoint: AnnotatedFunction<any[]>) => void;
14
13
  removeEntryPoint: (id: string) => void;
14
+ getChatCompletionFunctionDescriptions: () => CompletionCreateParams.Function[];
15
+ getFunctionCallHandler: () => FunctionCallHandler;
15
16
 
16
- getContextString: () => string;
17
- addContext: (context: string, parentId?: string) => TreeNodeId;
17
+ // text context
18
+ getContextString: (categories?: string[]) => string;
19
+ addContext: (
20
+ context: string,
21
+ parentId?: string,
22
+ categories?: string[]
23
+ ) => TreeNodeId;
18
24
  removeContext: (id: TreeNodeId) => void;
19
25
  }
20
26
 
21
27
  const emptyCopilotContext: CopilotContextParams = {
22
28
  entryPoints: {},
23
- getChatCompletionFunctionDescriptions: () => [],
24
- getFunctionCallHandler: () => async () => {},
25
29
  setEntryPoint: () => {},
26
30
  removeEntryPoint: () => {},
27
- getContextString: () => "",
31
+ getChatCompletionFunctionDescriptions: () => returnAndThrowInDebug([]),
32
+ getFunctionCallHandler: () => returnAndThrowInDebug(async () => {}),
33
+
34
+ getContextString: () => returnAndThrowInDebug(""),
28
35
  addContext: () => "",
29
36
  removeContext: () => {},
30
37
  };
31
38
 
32
39
  export const CopilotContext =
33
40
  React.createContext<CopilotContextParams>(emptyCopilotContext);
41
+
42
+ function returnAndThrowInDebug<T>(value: T): T {
43
+ throw new Error(
44
+ "Remember to wrap your app in a `<CopilotProvider> {...} </CopilotProvider>` !!!"
45
+ );
46
+ return value;
47
+ }
@@ -1,17 +1,25 @@
1
1
  "use client";
2
2
 
3
- import { useRef, useContext, useEffect } from "react";
3
+ import { useContext, useEffect, useRef } from "react";
4
4
  import { CopilotContext } from "../context/copilot-context";
5
5
 
6
+ /**
7
+ * Adds the given information to the Copilot context to make it readable by Copilot.
8
+ * @param information - The information to be added to the Copilot context.
9
+ * @param parentId - The ID of the parent context, if any.
10
+ * @param categories - An array of categories to control which context are visible where. Particularly useful with CopilotTextarea (see `useMakeAutosuggestionFunction`)
11
+ * @returns The ID of the added context.
12
+ */
6
13
  export function useMakeCopilotReadable(
7
14
  information: string,
8
- parentId?: string
15
+ parentId?: string,
16
+ categories?: string[]
9
17
  ): string | undefined {
10
18
  const { addContext, removeContext } = useContext(CopilotContext);
11
19
  const idRef = useRef<string>();
12
20
 
13
21
  useEffect(() => {
14
- const id = addContext(information, parentId);
22
+ const id = addContext(information, parentId, categories);
15
23
  idRef.current = id;
16
24
 
17
25
  return () => {
@@ -1,5 +1,5 @@
1
1
  import { nanoid } from "nanoid";
2
- import { useReducer, useCallback } from "react";
2
+ import { useCallback, useReducer } from "react";
3
3
 
4
4
  export type TreeNodeId = string;
5
5
 
@@ -8,14 +8,19 @@ export interface TreeNode {
8
8
  value: string;
9
9
  children: TreeNode[];
10
10
  parentId?: TreeNodeId;
11
+ categories: Set<string>;
11
12
  }
12
13
 
13
14
  export type Tree = TreeNode[];
14
15
 
15
16
  export interface UseTreeReturn {
16
17
  tree: Tree;
17
- addElement: (value: string, parentId?: TreeNodeId) => TreeNodeId;
18
- printTree: () => string;
18
+ addElement: (
19
+ value: string,
20
+ categories: string[],
21
+ parentId?: TreeNodeId
22
+ ) => TreeNodeId;
23
+ printTree: (categories: string[]) => string;
19
24
  removeElement: (id: TreeNodeId) => void;
20
25
  }
21
26
 
@@ -71,7 +76,7 @@ const treeIndentationRepresentation = (
71
76
  } else if (indentLevel === 2) {
72
77
  return String.fromCharCode(97 + index); // 97 is the ASCII value for 'a'
73
78
  } else {
74
- throw new Error("Indentation level not supported");
79
+ return "-";
75
80
  }
76
81
  };
77
82
 
@@ -94,11 +99,16 @@ const printNode = (node: TreeNode, prefix = "", indentLevel = 0): string => {
94
99
  output += `${outputSubsequentLines}\n`;
95
100
  }
96
101
 
102
+ const childPrePrefix = " ".repeat(prefix.length);
103
+
97
104
  node.children.forEach(
98
105
  (child, index) =>
99
106
  (output += printNode(
100
107
  child,
101
- `${prefix}${treeIndentationRepresentation(index, indentLevel + 1)}. `,
108
+ `${childPrePrefix}${treeIndentationRepresentation(
109
+ index,
110
+ indentLevel + 1
111
+ )}. `,
102
112
  indentLevel + 1
103
113
  ))
104
114
  );
@@ -107,7 +117,13 @@ const printNode = (node: TreeNode, prefix = "", indentLevel = 0): string => {
107
117
 
108
118
  // Action types
109
119
  type Action =
110
- | { type: "ADD_NODE"; value: string; parentId?: string; id: string }
120
+ | {
121
+ type: "ADD_NODE";
122
+ value: string;
123
+ parentId?: string;
124
+ id: string;
125
+ categories: string[];
126
+ }
111
127
  | { type: "REMOVE_NODE"; id: string };
112
128
 
113
129
  // Reducer function
@@ -119,6 +135,7 @@ function treeReducer(state: Tree, action: Action): Tree {
119
135
  id: newNodeId,
120
136
  value,
121
137
  children: [],
138
+ categories: new Set(action.categories),
122
139
  };
123
140
 
124
141
  try {
@@ -140,9 +157,15 @@ const useTree = (): UseTreeReturn => {
140
157
  const [tree, dispatch] = useReducer(treeReducer, []);
141
158
 
142
159
  const addElement = useCallback(
143
- (value: string, parentId?: string): TreeNodeId => {
160
+ (value: string, categories: string[], parentId?: string): TreeNodeId => {
144
161
  const newNodeId = nanoid(); // Generate new ID outside of dispatch
145
- dispatch({ type: "ADD_NODE", value, parentId, id: newNodeId });
162
+ dispatch({
163
+ type: "ADD_NODE",
164
+ value,
165
+ parentId,
166
+ id: newNodeId,
167
+ categories: categories,
168
+ });
146
169
  return newNodeId; // Return the new ID
147
170
  },
148
171
  []
@@ -152,19 +175,46 @@ const useTree = (): UseTreeReturn => {
152
175
  dispatch({ type: "REMOVE_NODE", id });
153
176
  }, []);
154
177
 
155
- const printTree = (): string => {
156
- let output = "";
157
- tree.forEach(
158
- (node, index) =>
159
- (output += printNode(
178
+ const printTree = useCallback(
179
+ (categories: string[]): string => {
180
+ const categoriesSet = new Set(categories);
181
+
182
+ let output = "";
183
+ tree.forEach((node, index) => {
184
+ // if the node does not have any of the desired categories, continue to the next node
185
+ if (!setsHaveIntersection(categoriesSet, node.categories)) {
186
+ return;
187
+ }
188
+
189
+ // add a new line before each node except the first one
190
+ if (index !== 0) {
191
+ output += "\n";
192
+ }
193
+
194
+ output += printNode(
160
195
  node,
161
196
  `${treeIndentationRepresentation(index, 0)}. `
162
- ))
163
- );
164
- return output;
165
- };
197
+ );
198
+ });
199
+ return output;
200
+ },
201
+ [tree]
202
+ );
166
203
 
167
204
  return { tree, addElement, printTree, removeElement };
168
205
  };
169
206
 
170
207
  export default useTree;
208
+
209
+ function setsHaveIntersection<T>(setA: Set<T>, setB: Set<T>): boolean {
210
+ const [smallerSet, largerSet] =
211
+ setA.size <= setB.size ? [setA, setB] : [setB, setA];
212
+
213
+ for (let item of smallerSet) {
214
+ if (largerSet.has(item)) {
215
+ return true;
216
+ }
217
+ }
218
+
219
+ return false;
220
+ }
@@ -0,0 +1,7 @@
1
+ import * as utils from "./utils";
2
+
3
+ describe("emptyTest", () => {
4
+ it("should be truthy", () => {
5
+ expect(true).toBeTruthy();
6
+ });
7
+ });
@@ -0,0 +1 @@
1
+ export {};
package/tsconfig.json CHANGED
@@ -1,5 +1,12 @@
1
1
  {
2
2
  "extends": "tsconfig/react-library.json",
3
3
  "include": ["."],
4
- "exclude": ["dist", "build", "node_modules"]
4
+ "exclude": [
5
+ "dist",
6
+ "build",
7
+ "node_modules",
8
+ "**/*.test.ts",
9
+ "**/*.test.tsx",
10
+ "**/__tests__/*"
11
+ ]
5
12
  }
package/tsup.config.ts CHANGED
@@ -8,5 +8,10 @@ export default defineConfig((options: Options) => ({
8
8
  clean: true,
9
9
  external: ["react"],
10
10
  sourcemap: true,
11
+ exclude: [
12
+ "**/*.test.ts", // Exclude TypeScript test files
13
+ "**/*.test.tsx", // Exclude TypeScript React test files
14
+ "**/__tests__/*", // Exclude any files inside a __tests__ directory
15
+ ],
11
16
  ...options,
12
17
  }));
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- The MIT License
2
-
3
- Copyright (c) Atai Barkai
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in
13
- all copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- THE SOFTWARE.
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/components/copilot-provider.tsx"],"names":[],"mappings":";;;;;;;;;;;;;;AACA,SAAgB,UAAqB,mBAAmB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,IAEC;AAAA,GACH;AAEJ;AAEA,SAAS,iCACP,aACqB;AACrB,SAAO,CAAO,cAAc,iBAAiB;AAC3C,QAAI,4BAGA,CAAC;AACL,aAAS,cAAc,aAAa;AAClC,gCAA0B,WAAW,QAAQ;AAAA,IAC/C;AAEA,UAAM,qBACJ,0BAA0B,aAAa,QAAQ;AACjD,QAAI,oBAAoB;AACtB,UAAI,8BAAqD,CAAC;AAC1D,UAAI,aAAa,WAAW;AAC1B,sCAA8B,KAAK,MAAM,aAAa,SAAS;AAAA,MACjE;AAEA,YAAM,uBAA8B,CAAC;AACrC,eAAS,OAAO,mBAAmB,qBAAqB;AACtD,6BAAqB;AAAA,UACnB,4BACE,IAAI;AAAA,QAER;AAAA,MACF;AAEA,YAAM,mBAAmB,eAAe,GAAG,oBAAoB;AAAA,IAmBjE;AAAA,EACF;AACF;AAEA,SAAS,qCACP,aAC2B;AAC3B,SAAO,YAAY,IAAI,yCAAyC;AAClE;AAEA,SAAS,0CACP,mBACyB;AAEzB,MAAI,aAAqC,CAAC;AAC1C,WAAS,OAAO,kBAAkB,qBAAqB;AAErD,QAA2C,UAArC,QAAM,SApJhB,IAoJ+C,IAAlB,0BAAkB,IAAlB,CAAnB,QAAM;AACZ,eAAW,IAAI,QAAQ;AAAA,EACzB;AAEA,MAAI,yBAAmC,CAAC;AACxC,WAAS,OAAO,kBAAkB,qBAAqB;AACrD,QAAI,IAAI,UAAU;AAChB,6BAAuB,KAAK,IAAI,IAAI;AAAA,IACtC;AAAA,EACF;AAGA,MAAI,yBAAkD;AAAA,IACpD,MAAM,kBAAkB;AAAA,IACxB,aAAa,kBAAkB;AAAA,IAC/B,YAAY;AAAA,MACV,MAAM;AAAA,MACN,YAAY;AAAA,MACZ,UAAU;AAAA,IACZ;AAAA,EACF;AAEA,SAAO;AACT","sourcesContent":["\"use client\";\nimport React, { useState, ReactNode, useCallback } from \"react\";\nimport { AnnotatedFunction } from \"../types/annotated-function\";\nimport useTree from \"../hooks/use-tree\";\nimport { CopilotContext } from \"../context/copilot-context\";\nimport { FunctionCallHandler } from \"ai\";\nimport { ChatCompletionFunctions } from \"openai-edge/types/api\";\n\nexport function CopilotProvider({\n children,\n}: {\n children: ReactNode;\n}): JSX.Element {\n const [entryPoints, setEntryPoints] = useState<\n Record<string, AnnotatedFunction<any[]>>\n >({});\n\n const { addElement, removeElement, printTree } = useTree();\n\n const setEntryPoint = useCallback(\n (id: string, entryPoint: AnnotatedFunction<any[]>) => {\n setEntryPoints((prevPoints) => {\n return {\n ...prevPoints,\n [id]: entryPoint,\n };\n });\n },\n []\n );\n\n const removeEntryPoint = useCallback((id: string) => {\n setEntryPoints((prevPoints) => {\n const newPoints = { ...prevPoints };\n delete newPoints[id];\n return newPoints;\n });\n }, []);\n\n const getContextString = useCallback(() => {\n return printTree();\n }, [printTree]);\n\n const addContext = useCallback(\n (context: string, parentId?: string) => {\n return addElement(context, parentId);\n },\n [addElement]\n );\n\n const removeContext = useCallback(\n (id: string) => {\n removeElement(id);\n },\n [removeElement]\n );\n\n const getChatCompletionFunctionDescriptions = useCallback(() => {\n return entryPointsToChatCompletionFunctions(Object.values(entryPoints));\n }, [entryPoints]);\n\n const getFunctionCallHandler = useCallback(() => {\n return entryPointsToFunctionCallHandler(Object.values(entryPoints));\n }, [entryPoints]);\n\n return (\n <CopilotContext.Provider\n value={{\n entryPoints,\n getChatCompletionFunctionDescriptions,\n getFunctionCallHandler,\n setEntryPoint,\n removeEntryPoint,\n getContextString,\n addContext,\n removeContext,\n }}\n >\n {children}\n </CopilotContext.Provider>\n );\n}\n\nfunction entryPointsToFunctionCallHandler(\n entryPoints: AnnotatedFunction<any[]>[]\n): FunctionCallHandler {\n return async (chatMessages, functionCall) => {\n let entrypointsByFunctionName: Record<\n string,\n AnnotatedFunction<any[]>\n > = {};\n for (let entryPoint of entryPoints) {\n entrypointsByFunctionName[entryPoint.name] = entryPoint;\n }\n\n const entryPointFunction =\n entrypointsByFunctionName[functionCall.name || \"\"];\n if (entryPointFunction) {\n let parsedFunctionCallArguments: Record<string, any>[] = [];\n if (functionCall.arguments) {\n parsedFunctionCallArguments = JSON.parse(functionCall.arguments);\n }\n\n const paramsInCorrectOrder: any[] = [];\n for (let arg of entryPointFunction.argumentAnnotations) {\n paramsInCorrectOrder.push(\n parsedFunctionCallArguments[\n arg.name as keyof typeof parsedFunctionCallArguments\n ]\n );\n }\n\n await entryPointFunction.implementation(...paramsInCorrectOrder);\n\n // commented out becasue for now we don't want to return anything\n // const result = await entryPointFunction.implementation(\n // ...parsedFunctionCallArguments\n // );\n // const functionResponse: ChatRequest = {\n // messages: [\n // ...chatMessages,\n // {\n // id: nanoid(),\n // name: functionCall.name,\n // role: 'function' as const,\n // content: JSON.stringify(result),\n // },\n // ],\n // };\n\n // return functionResponse;\n }\n };\n}\n\nfunction entryPointsToChatCompletionFunctions(\n entryPoints: AnnotatedFunction<any[]>[]\n): ChatCompletionFunctions[] {\n return entryPoints.map(annotatedFunctionToChatCompletionFunction);\n}\n\nfunction annotatedFunctionToChatCompletionFunction(\n annotatedFunction: AnnotatedFunction<any[]>\n): ChatCompletionFunctions {\n // Create the parameters object based on the argumentAnnotations\n let parameters: { [key: string]: any } = {};\n for (let arg of annotatedFunction.argumentAnnotations) {\n // isolate the args we should forward inline\n let { name, required, ...forwardedArgs } = arg;\n parameters[arg.name] = forwardedArgs;\n }\n\n let requiredParameterNames: string[] = [];\n for (let arg of annotatedFunction.argumentAnnotations) {\n if (arg.required) {\n requiredParameterNames.push(arg.name);\n }\n }\n\n // Create the ChatCompletionFunctions object\n let chatCompletionFunction: ChatCompletionFunctions = {\n name: annotatedFunction.name,\n description: annotatedFunction.description,\n parameters: {\n type: \"object\",\n properties: parameters,\n required: requiredParameterNames,\n },\n };\n\n return chatCompletionFunction;\n}\n"]}
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/hooks/use-make-copilot-readable.ts"],"names":[],"mappings":";;;;;AAEA,SAAS,QAAQ,YAAY,iBAAiB;AAF9C;AAKO,SAAS,uBACd,aACA,UACoB;AACpB,QAAM,EAAE,YAAY,cAAc,IAAI,WAAW,cAAc;AAC/D,QAAM,QAAQ,OAAe;AAE7B,YAAU,MAAM;AACd,UAAM,KAAK,WAAW,aAAa,QAAQ;AAC3C,UAAM,UAAU;AAEhB,WAAO,MAAM;AACX,oBAAc,EAAE;AAAA,IAClB;AAAA,EACF,GAAG,CAAC,aAAa,UAAU,YAAY,aAAa,CAAC;AAErD,SAAO,MAAM;AACf","sourcesContent":["\"use client\";\n\nimport { useRef, useContext, useEffect } from \"react\";\nimport { CopilotContext } from \"../context/copilot-context\";\n\nexport function useMakeCopilotReadable(\n information: string,\n parentId?: string\n): string | undefined {\n const { addContext, removeContext } = useContext(CopilotContext);\n const idRef = useRef<string>();\n\n useEffect(() => {\n const id = addContext(information, parentId);\n idRef.current = id;\n\n return () => {\n removeContext(id);\n };\n }, [information, parentId, addContext, removeContext]);\n\n return idRef.current;\n}\n"]}
@@ -1,22 +0,0 @@
1
- import { __async } from './chunk-MRXNTQOX.mjs';
2
- import React from 'react';
3
-
4
- var emptyCopilotContext = {
5
- entryPoints: {},
6
- getChatCompletionFunctionDescriptions: () => [],
7
- getFunctionCallHandler: () => () => __async(void 0, null, function* () {
8
- }),
9
- setEntryPoint: () => {
10
- },
11
- removeEntryPoint: () => {
12
- },
13
- getContextString: () => "",
14
- addContext: () => "",
15
- removeContext: () => {
16
- }
17
- };
18
- var CopilotContext = React.createContext(emptyCopilotContext);
19
-
20
- export { CopilotContext };
21
- //# sourceMappingURL=out.js.map
22
- //# sourceMappingURL=chunk-UABEP4UT.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/context/copilot-context.tsx"],"names":[],"mappings":";;;;;AAEA,OAAO,WAAW;AAFlB;AAoBA,IAAM,sBAA4C;AAAA,EAChD,aAAa,CAAC;AAAA,EACd,uCAAuC,MAAM,CAAC;AAAA,EAC9C,wBAAwB,MAAM,MAAY;AAAA,EAAC;AAAA,EAC3C,eAAe,MAAM;AAAA,EAAC;AAAA,EACtB,kBAAkB,MAAM;AAAA,EAAC;AAAA,EACzB,kBAAkB,MAAM;AAAA,EACxB,YAAY,MAAM;AAAA,EAClB,eAAe,MAAM;AAAA,EAAC;AACxB;AAEO,IAAM,iBACX,MAAM,cAAoC,mBAAmB","sourcesContent":["\"use client\";\n\nimport React from \"react\";\nimport { AnnotatedFunction } from \"../types/annotated-function\";\nimport { TreeNodeId } from \"../hooks/use-tree\";\nimport { ChatCompletionFunctions } from \"openai-edge/types/api\";\nimport { FunctionCallHandler } from \"ai\";\n\nexport interface CopilotContextParams {\n entryPoints: Record<string, AnnotatedFunction<any[]>>;\n getChatCompletionFunctionDescriptions: () => ChatCompletionFunctions[];\n getFunctionCallHandler: () => FunctionCallHandler;\n setEntryPoint: (id: string, entryPoint: AnnotatedFunction<any[]>) => void;\n removeEntryPoint: (id: string) => void;\n\n getContextString: () => string;\n addContext: (context: string, parentId?: string) => TreeNodeId;\n removeContext: (id: TreeNodeId) => void;\n}\n\nconst emptyCopilotContext: CopilotContextParams = {\n entryPoints: {},\n getChatCompletionFunctionDescriptions: () => [],\n getFunctionCallHandler: () => async () => {},\n setEntryPoint: () => {},\n removeEntryPoint: () => {},\n getContextString: () => \"\",\n addContext: () => \"\",\n removeContext: () => {},\n};\n\nexport const CopilotContext =\n React.createContext<CopilotContextParams>(emptyCopilotContext);\n"]}
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/hooks/use-tree.ts"],"names":[],"mappings":";;;;;;AAAA,SAAS,cAAc;AACvB,SAAS,YAAY,mBAAmB;AAiCxC,IAAM,aAAa,CAAC,OAAa,OAAyB;AACxD,SAAO,MAAM,OAAO,CAAC,QAAc,SAAS;AAC1C,QAAI,KAAK,OAAO,IAAI;AAClB,YAAM,UAAU,iCAAK,OAAL,EAAW,UAAU,WAAW,KAAK,UAAU,EAAE,EAAE;AACnE,aAAO,KAAK,OAAO;AAAA,IACrB;AACA,WAAO;AAAA,EACT,GAAG,CAAC,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from \"nanoid\";\nimport { useReducer, useCallback } from \"react\";\n\nexport type TreeNodeId = string;\n\nexport interface TreeNode {\n id: TreeNodeId;\n value: string;\n children: TreeNode[];\n parentId?: TreeNodeId;\n}\n\nexport type Tree = TreeNode[];\n\nexport interface UseTreeReturn {\n tree: Tree;\n addElement: (value: string, parentId?: TreeNodeId) => TreeNodeId;\n printTree: () => 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 throw new Error(\"Indentation level not supported\");\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 node.children.forEach(\n (child, index) =>\n (output += printNode(\n child,\n `${prefix}${treeIndentationRepresentation(index, indentLevel + 1)}. `,\n indentLevel + 1\n ))\n );\n return output;\n};\n\n// Action types\ntype Action =\n | { type: \"ADD_NODE\"; value: string; parentId?: string; id: string }\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 };\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, parentId?: string): TreeNodeId => {\n const newNodeId = nanoid(); // Generate new ID outside of dispatch\n dispatch({ type: \"ADD_NODE\", value, parentId, id: newNodeId });\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 = (): string => {\n let output = \"\";\n tree.forEach(\n (node, index) =>\n (output += printNode(\n node,\n `${treeIndentationRepresentation(index, 0)}. `\n ))\n );\n return output;\n };\n\n return { tree, addElement, printTree, removeElement };\n};\n\nexport default useTree;\n"]}