@copilotkit/react-core 0.3.0 → 0.4.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 (76) hide show
  1. package/.turbo/turbo-build.log +69 -11
  2. package/CHANGELOG.md +6 -0
  3. package/LICENSE +21 -0
  4. package/dist/chunk-7IPFT6UY.mjs +115 -0
  5. package/dist/chunk-7IPFT6UY.mjs.map +1 -0
  6. package/dist/chunk-7TCGEDGA.mjs +19 -0
  7. package/dist/chunk-7TCGEDGA.mjs.map +1 -0
  8. package/dist/chunk-BVQRDAR7.mjs +3 -0
  9. package/dist/chunk-BVQRDAR7.mjs.map +1 -0
  10. package/dist/chunk-EFZPSZWO.mjs +3 -0
  11. package/dist/chunk-EFZPSZWO.mjs.map +1 -0
  12. package/dist/chunk-GJUAS6I7.mjs +80 -0
  13. package/dist/chunk-GJUAS6I7.mjs.map +1 -0
  14. package/dist/chunk-JD7BAH7U.mjs +3 -0
  15. package/dist/chunk-JD7BAH7U.mjs.map +1 -0
  16. package/dist/chunk-MRXNTQOX.mjs +55 -0
  17. package/dist/chunk-MRXNTQOX.mjs.map +1 -0
  18. package/dist/chunk-OMI7JPW5.mjs +30 -0
  19. package/dist/chunk-OMI7JPW5.mjs.map +1 -0
  20. package/dist/chunk-UABEP4UT.mjs +22 -0
  21. package/dist/chunk-UABEP4UT.mjs.map +1 -0
  22. package/dist/chunk-VNRDQJXW.mjs +3 -0
  23. package/dist/chunk-VNRDQJXW.mjs.map +1 -0
  24. package/dist/chunk-YVXQVXMS.mjs +111 -0
  25. package/dist/chunk-YVXQVXMS.mjs.map +1 -0
  26. package/dist/components/copilot-provider.d.ts +7 -0
  27. package/dist/components/copilot-provider.mjs +6 -0
  28. package/dist/components/copilot-provider.mjs.map +1 -0
  29. package/dist/components/index.d.ts +2 -0
  30. package/dist/components/index.mjs +7 -0
  31. package/dist/components/index.mjs.map +1 -0
  32. package/dist/context/copilot-context.d.ts +19 -0
  33. package/dist/context/copilot-context.mjs +4 -0
  34. package/dist/context/copilot-context.mjs.map +1 -0
  35. package/dist/context/index.d.ts +6 -0
  36. package/dist/context/index.mjs +5 -0
  37. package/dist/context/index.mjs.map +1 -0
  38. package/dist/hooks/index.d.ts +5 -0
  39. package/dist/hooks/index.mjs +8 -0
  40. package/dist/hooks/index.mjs.map +1 -0
  41. package/dist/hooks/use-copilot-chat.d.ts +18 -0
  42. package/dist/hooks/use-copilot-chat.mjs +5 -0
  43. package/dist/hooks/use-copilot-chat.mjs.map +1 -0
  44. package/dist/hooks/use-make-copilot-actionable.d.ts +5 -0
  45. package/dist/hooks/use-make-copilot-actionable.mjs +5 -0
  46. package/dist/hooks/use-make-copilot-actionable.mjs.map +1 -0
  47. package/dist/hooks/use-make-copilot-readable.d.ts +3 -0
  48. package/dist/hooks/use-make-copilot-readable.mjs +5 -0
  49. package/dist/hooks/use-make-copilot-readable.mjs.map +1 -0
  50. package/dist/hooks/use-tree.d.ts +17 -0
  51. package/dist/hooks/use-tree.mjs +4 -0
  52. package/dist/hooks/use-tree.mjs.map +1 -0
  53. package/dist/index.d.ts +10 -55
  54. package/dist/index.mjs +13 -26
  55. package/dist/index.mjs.map +1 -0
  56. package/dist/types/annotated-function.d.ts +24 -0
  57. package/dist/types/annotated-function.mjs +3 -0
  58. package/dist/types/annotated-function.mjs.map +1 -0
  59. package/dist/types/index.d.ts +1 -0
  60. package/dist/types/index.mjs +3 -0
  61. package/dist/types/index.mjs.map +1 -0
  62. package/package.json +8 -8
  63. package/src/components/copilot-provider.tsx +60 -55
  64. package/src/components/index.ts +1 -1
  65. package/src/context/copilot-context.tsx +20 -20
  66. package/src/context/index.ts +2 -2
  67. package/src/hooks/index.ts +5 -5
  68. package/src/hooks/use-copilot-chat.ts +43 -39
  69. package/src/hooks/use-make-copilot-actionable.ts +13 -13
  70. package/src/hooks/use-make-copilot-readable.ts +11 -11
  71. package/src/hooks/use-tree.ts +87 -76
  72. package/src/index.tsx +4 -4
  73. package/src/types/annotated-function.ts +23 -10
  74. package/src/types/index.ts +2 -2
  75. package/tsup.config.ts +3 -5
  76. package/dist/index.js +0 -32
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/hooks/use-tree.ts"],"names":[],"mappings":"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","sourcesContent":["import { nanoid } 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"]}
@@ -0,0 +1,7 @@
1
+ import { ReactNode } from 'react';
2
+
3
+ declare function CopilotProvider({ children, }: {
4
+ children: ReactNode;
5
+ }): JSX.Element;
6
+
7
+ export { CopilotProvider };
@@ -0,0 +1,6 @@
1
+ export { CopilotProvider } from '../chunk-7IPFT6UY.mjs';
2
+ import '../chunk-YVXQVXMS.mjs';
3
+ import '../chunk-UABEP4UT.mjs';
4
+ import '../chunk-MRXNTQOX.mjs';
5
+ //# sourceMappingURL=out.js.map
6
+ //# sourceMappingURL=copilot-provider.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ export { CopilotProvider } from './copilot-provider.js';
2
+ import 'react';
@@ -0,0 +1,7 @@
1
+ import '../chunk-JD7BAH7U.mjs';
2
+ export { CopilotProvider } from '../chunk-7IPFT6UY.mjs';
3
+ import '../chunk-YVXQVXMS.mjs';
4
+ import '../chunk-UABEP4UT.mjs';
5
+ import '../chunk-MRXNTQOX.mjs';
6
+ //# sourceMappingURL=out.js.map
7
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"names":[],"mappings":""}
@@ -0,0 +1,19 @@
1
+ import React from 'react';
2
+ import { AnnotatedFunction } from '../types/annotated-function.js';
3
+ import { TreeNodeId } from '../hooks/use-tree.js';
4
+ import { ChatCompletionFunctions } from 'openai-edge/types/api';
5
+ import { FunctionCallHandler } from 'ai';
6
+
7
+ interface CopilotContextParams {
8
+ entryPoints: Record<string, AnnotatedFunction<any[]>>;
9
+ getChatCompletionFunctionDescriptions: () => ChatCompletionFunctions[];
10
+ getFunctionCallHandler: () => FunctionCallHandler;
11
+ setEntryPoint: (id: string, entryPoint: AnnotatedFunction<any[]>) => void;
12
+ removeEntryPoint: (id: string) => void;
13
+ getContextString: () => string;
14
+ addContext: (context: string, parentId?: string) => TreeNodeId;
15
+ removeContext: (id: TreeNodeId) => void;
16
+ }
17
+ declare const CopilotContext: React.Context<CopilotContextParams>;
18
+
19
+ export { CopilotContext, CopilotContextParams };
@@ -0,0 +1,4 @@
1
+ export { CopilotContext } from '../chunk-UABEP4UT.mjs';
2
+ import '../chunk-MRXNTQOX.mjs';
3
+ //# sourceMappingURL=out.js.map
4
+ //# sourceMappingURL=copilot-context.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"names":[],"mappings":""}
@@ -0,0 +1,6 @@
1
+ export { CopilotContext, CopilotContextParams } from './copilot-context.js';
2
+ import 'react';
3
+ import '../types/annotated-function.js';
4
+ import '../hooks/use-tree.js';
5
+ import 'openai-edge/types/api';
6
+ import 'ai';
@@ -0,0 +1,5 @@
1
+ import '../chunk-VNRDQJXW.mjs';
2
+ export { CopilotContext } from '../chunk-UABEP4UT.mjs';
3
+ import '../chunk-MRXNTQOX.mjs';
4
+ //# sourceMappingURL=out.js.map
5
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"names":[],"mappings":""}
@@ -0,0 +1,5 @@
1
+ export { UseCopilotChatOptions, UseCopilotChatReturn, useCopilotChat } from './use-copilot-chat.js';
2
+ export { useMakeCopilotActionable } from './use-make-copilot-actionable.js';
3
+ export { useMakeCopilotReadable } from './use-make-copilot-readable.js';
4
+ import 'ai';
5
+ import '../types/annotated-function.js';
@@ -0,0 +1,8 @@
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';
6
+ import '../chunk-MRXNTQOX.mjs';
7
+ //# sourceMappingURL=out.js.map
8
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"names":[],"mappings":""}
@@ -0,0 +1,18 @@
1
+ import { UseChatOptions, Message, CreateMessage, ChatRequestOptions } from 'ai';
2
+
3
+ interface UseCopilotChatOptions extends UseChatOptions {
4
+ makeSystemMessage?: (contextString: string) => string;
5
+ }
6
+ interface UseCopilotChatReturn {
7
+ visibleMessages: Message[];
8
+ append: (message: Message | CreateMessage, chatRequestOptions?: ChatRequestOptions) => Promise<string | null | undefined>;
9
+ reload: (chatRequestOptions?: ChatRequestOptions) => Promise<string | null | undefined>;
10
+ stop: () => void;
11
+ isLoading: boolean;
12
+ input: string;
13
+ setInput: React.Dispatch<React.SetStateAction<string>>;
14
+ }
15
+ declare function useCopilotChat({ makeSystemMessage, ...options }: UseCopilotChatOptions): UseCopilotChatReturn;
16
+ declare function defaultSystemMessage(contextString: string): string;
17
+
18
+ export { UseCopilotChatOptions, UseCopilotChatReturn, defaultSystemMessage, useCopilotChat };
@@ -0,0 +1,5 @@
1
+ export { defaultSystemMessage, useCopilotChat } from '../chunk-GJUAS6I7.mjs';
2
+ import '../chunk-UABEP4UT.mjs';
3
+ import '../chunk-MRXNTQOX.mjs';
4
+ //# sourceMappingURL=out.js.map
5
+ //# sourceMappingURL=use-copilot-chat.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"names":[],"mappings":""}
@@ -0,0 +1,5 @@
1
+ import { AnnotatedFunction } from '../types/annotated-function.js';
2
+
3
+ declare function useMakeCopilotActionable<ActionInput extends any[]>(annotatedFunction: AnnotatedFunction<ActionInput>, dependencies: any[]): void;
4
+
5
+ export { useMakeCopilotActionable };
@@ -0,0 +1,5 @@
1
+ export { useMakeCopilotActionable } from '../chunk-OMI7JPW5.mjs';
2
+ import '../chunk-UABEP4UT.mjs';
3
+ import '../chunk-MRXNTQOX.mjs';
4
+ //# sourceMappingURL=out.js.map
5
+ //# sourceMappingURL=use-make-copilot-actionable.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"names":[],"mappings":""}
@@ -0,0 +1,3 @@
1
+ declare function useMakeCopilotReadable(information: string, parentId?: string): string | undefined;
2
+
3
+ export { useMakeCopilotReadable };
@@ -0,0 +1,5 @@
1
+ export { useMakeCopilotReadable } from '../chunk-7TCGEDGA.mjs';
2
+ import '../chunk-UABEP4UT.mjs';
3
+ import '../chunk-MRXNTQOX.mjs';
4
+ //# sourceMappingURL=out.js.map
5
+ //# sourceMappingURL=use-make-copilot-readable.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"names":[],"mappings":""}
@@ -0,0 +1,17 @@
1
+ type TreeNodeId = string;
2
+ interface TreeNode {
3
+ id: TreeNodeId;
4
+ value: string;
5
+ children: TreeNode[];
6
+ parentId?: TreeNodeId;
7
+ }
8
+ type Tree = TreeNode[];
9
+ interface UseTreeReturn {
10
+ tree: Tree;
11
+ addElement: (value: string, parentId?: TreeNodeId) => TreeNodeId;
12
+ printTree: () => string;
13
+ removeElement: (id: TreeNodeId) => void;
14
+ }
15
+ declare const useTree: () => UseTreeReturn;
16
+
17
+ export { Tree, TreeNode, TreeNodeId, UseTreeReturn, useTree as default };
@@ -0,0 +1,4 @@
1
+ export { use_tree_default as default } from '../chunk-YVXQVXMS.mjs';
2
+ import '../chunk-MRXNTQOX.mjs';
3
+ //# sourceMappingURL=out.js.map
4
+ //# sourceMappingURL=use-tree.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"names":[],"mappings":""}
package/dist/index.d.ts CHANGED
@@ -1,55 +1,10 @@
1
- import React$1, { ReactNode } from 'react';
2
- import { ChatCompletionFunctions } from 'openai-edge/types/api';
3
- import { FunctionCallHandler, UseChatOptions, Message, CreateMessage, ChatRequestOptions } from 'ai';
4
-
5
- declare function CopilotProvider({ children }: {
6
- children: ReactNode;
7
- }): JSX.Element;
8
-
9
- interface AnnotatedFunctionArgument {
10
- name: string;
11
- type: string;
12
- description: string;
13
- allowedValues?: any[];
14
- required: boolean;
15
- }
16
- interface AnnotatedFunction<Inputs extends any[]> {
17
- name: string;
18
- description: string;
19
- argumentAnnotations: AnnotatedFunctionArgument[];
20
- implementation: (...args: Inputs) => Promise<void>;
21
- }
22
-
23
- type TreeNodeId = string;
24
-
25
- interface CopilotContextParams {
26
- entryPoints: Record<string, AnnotatedFunction<any[]>>;
27
- getChatCompletionFunctions: () => ChatCompletionFunctions[];
28
- getFunctionCallHandler: () => FunctionCallHandler;
29
- setEntryPoint: (id: string, entryPoint: AnnotatedFunction<any[]>) => void;
30
- removeEntryPoint: (id: string) => void;
31
- getContextString: () => string;
32
- addContext: (context: string, parentId?: string) => TreeNodeId;
33
- removeContext: (id: TreeNodeId) => void;
34
- }
35
- declare const CopilotContext: React$1.Context<CopilotContextParams>;
36
-
37
- interface UseCopilotChatOptions extends UseChatOptions {
38
- makeSystemMessage?: (contextString: string) => string;
39
- }
40
- interface UseCopilotChatReturn {
41
- visibleMessages: Message[];
42
- append: (message: Message | CreateMessage, chatRequestOptions?: ChatRequestOptions) => Promise<string | null | undefined>;
43
- reload: (chatRequestOptions?: ChatRequestOptions) => Promise<string | null | undefined>;
44
- stop: () => void;
45
- isLoading: boolean;
46
- input: string;
47
- setInput: React.Dispatch<React.SetStateAction<string>>;
48
- }
49
- declare function useCopilotChat({ makeSystemMessage, ...options }: UseCopilotChatOptions): UseCopilotChatReturn;
50
-
51
- declare function useMakeCopilotActionable<ActionInput extends any[]>(annotatedFunction: AnnotatedFunction<ActionInput>, dependencies: any[]): void;
52
-
53
- declare function useMakeCopilotReadable(information: string, parentId?: string): string | undefined;
54
-
55
- export { AnnotatedFunction, AnnotatedFunctionArgument, CopilotContext, CopilotContextParams, CopilotProvider, UseCopilotChatOptions, UseCopilotChatReturn, useCopilotChat, useMakeCopilotActionable, useMakeCopilotReadable };
1
+ export { CopilotProvider } from './components/copilot-provider.js';
2
+ export { CopilotContext, CopilotContextParams } from './context/copilot-context.js';
3
+ export { UseCopilotChatOptions, UseCopilotChatReturn, useCopilotChat } from './hooks/use-copilot-chat.js';
4
+ export { useMakeCopilotActionable } from './hooks/use-make-copilot-actionable.js';
5
+ export { useMakeCopilotReadable } from './hooks/use-make-copilot-readable.js';
6
+ export { AnnotatedFunction, AnnotatedFunctionArgument } from './types/annotated-function.js';
7
+ import 'react';
8
+ import './hooks/use-tree.js';
9
+ import 'openai-edge/types/api';
10
+ import 'ai';
package/dist/index.mjs CHANGED
@@ -1,26 +1,13 @@
1
- import V, { useState, useCallback, useContext, useMemo, useRef, useEffect, useReducer } from 'react';
2
- import { nanoid } from 'nanoid';
3
- import { jsx } from 'react/jsx-runtime';
4
- import { useChat } from 'ai/react';
5
-
6
- var k=Object.defineProperty,q=Object.defineProperties;var U=Object.getOwnPropertyDescriptors;var x=Object.getOwnPropertySymbols;var P=Object.prototype.hasOwnProperty,A=Object.prototype.propertyIsEnumerable;var N=(e,t,n)=>t in e?k(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n,h=(e,t)=>{for(var n in t||(t={}))P.call(t,n)&&N(e,n,t[n]);if(x)for(var n of x(t))A.call(t,n)&&N(e,n,t[n]);return e},T=(e,t)=>q(e,U(t));var E=(e,t)=>{var n={};for(var o in e)P.call(e,o)&&t.indexOf(o)<0&&(n[o]=e[o]);if(e!=null&&x)for(var o of x(e))t.indexOf(o)<0&&A.call(e,o)&&(n[o]=e[o]);return n};var v=(e,t,n)=>new Promise((o,r)=>{var i=u=>{try{a(n.next(u));}catch(c){r(c);}},s=u=>{try{a(n.throw(u));}catch(c){r(c);}},a=u=>u.done?o(u.value):Promise.resolve(u.value).then(i,s);a((n=n.apply(e,t)).next());});var I=(e,t)=>{for(let n of e){if(n.id===t)return n;let o=I(n.children,t);if(o)return o}},M=(e,t)=>e.reduce((n,o)=>{if(o.id!==t){let r=T(h({},o),{children:M(o.children,t)});n.push(r);}return n},[]),b=(e,t)=>{if(t===0)return (e+1).toString();if(t===1)return String.fromCharCode(65+e);if(t===2)return String.fromCharCode(97+e);throw new Error("Indentation level not supported")},O=(e,t="",n=0)=>{let o=" ".repeat(3).repeat(n),r=t.length+o.length,i=" ".repeat(r),s=e.value.split(`
7
- `),a=`${o}${t}${s[0]}`,u=s.slice(1).map(l=>`${i}${l}`).join(`
8
- `),c=`${a}
9
- `;return u&&(c+=`${u}
10
- `),e.children.forEach((l,g)=>c+=O(l,`${t}${b(g,n+1)}. `,n+1)),c};function j(e,t){switch(t.type){case"ADD_NODE":{let{value:n,parentId:o,id:r}=t,i={id:r,value:n,children:[]};if(o){let s=I(e,o);if(s)i.parentId=o,s.children.push(i);else throw new Error(`Parent with id ${o} not found`)}else return [...e,i];return e}case"REMOVE_NODE":return M(e,t.id);default:return e}}var L=()=>{let[e,t]=useReducer(j,[]),n=useCallback((i,s)=>{let a=nanoid();return t({type:"ADD_NODE",value:i,parentId:s,id:a}),a},[]),o=useCallback(i=>{t({type:"REMOVE_NODE",id:i});},[]);return {tree:e,addElement:n,printTree:()=>{let i="";return e.forEach((s,a)=>i+=O(s,`${b(a,0)}. `)),i},removeElement:o}},w=L;var J={entryPoints:{},getChatCompletionFunctions:()=>[],getFunctionCallHandler:()=>()=>v(void 0,null,function*(){}),setEntryPoint:()=>{},removeEntryPoint:()=>{},getContextString:()=>"",addContext:()=>"",removeContext:()=>{}},d=V.createContext(J);function B({children:e}){let[t,n]=useState({}),{addElement:o,removeElement:r,printTree:i}=w(),s=useCallback((p,m)=>{n(C=>T(h({},C),{[p]:m}));},[]),a=useCallback(p=>{n(m=>{let C=h({},m);return delete C[p],C});},[]),u=useCallback(()=>i(),[i]),c=useCallback((p,m)=>o(p,m),[o]),l=useCallback(p=>{r(p);},[r]),g=useCallback(()=>X(Object.values(t)),[t]),F=useCallback(()=>W(Object.values(t)),[t]);return jsx(d.Provider,{value:{entryPoints:t,getChatCompletionFunctions:g,getFunctionCallHandler:F,setEntryPoint:s,removeEntryPoint:a,getContextString:u,addContext:c,removeContext:l},children:e})}function W(e){return (t,n)=>v(this,null,function*(){let o={};for(let i of e)o[i.name]=i;let r=o[n.name||""];if(r){let i=[];n.arguments&&(i=JSON.parse(n.arguments));let s=[];for(let a of r.argumentAnnotations)s.push(i[a.name]);yield r.implementation(...s);}})}function X(e){return e.map(Y)}function Y(e){let t={};for(let r of e.argumentAnnotations)t[r.name]={type:r.type,description:r.description};let n=[];for(let r of e.argumentAnnotations)r.required&&n.push(r.name);return {name:e.name,description:e.description,parameters:{type:"object",properties:t,required:n}}}function Z(n){var o=n,{makeSystemMessage:e}=o,t=E(o,["makeSystemMessage"]);let{getContextString:r,getChatCompletionFunctions:i,getFunctionCallHandler:s}=useContext(d),u=[useMemo(()=>{let y=e||te,$=r();return {id:"system",content:y($),role:"system"}},[r,e])].concat(t.initialMessages||[]),c=useMemo(()=>i(),[i]),{messages:l,append:g,reload:F,stop:p,isLoading:m,input:C,setInput:D}=useChat({id:t.id,initialMessages:u,experimental_onFunctionCall:s(),body:{id:t.id,previewToken:ee,functions:c}});return {visibleMessages:l.filter(y=>y.role==="user"||y.role==="assistant"),append:g,reload:F,stop:p,isLoading:m,input:C,setInput:D}}var ee="TODO123";function te(e){return `
11
- Please act as a efficient, competent, and conscientious professional assistant.
12
- You 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.
13
- Always be polite and respectful, and prefer brevity over verbosity.
14
-
15
- The user has provided you with the following context:
16
- \`\`\`
17
- ${e}
18
- \`\`\`
19
-
20
- 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.
21
-
22
- Please assist them as best you can.
23
- If you are not sure how to proceed to best fulfill their requests, please ask them for more information.
24
- `}function ae(e,t){let n=useRef(nanoid()),{setEntryPoint:o,removeEntryPoint:r}=useContext(d),i=useMemo(()=>({name:e.name,description:e.description,argumentAnnotations:e.argumentAnnotations,implementation:e.implementation}),t);useEffect(()=>(o(n.current,i),()=>{r(n.current);}),[i,o,r]);}function de(e,t){let{addContext:n,removeContext:o}=useContext(d),r=useRef();return useEffect(()=>{let i=n(e,t);return r.current=i,()=>{o(i);}},[e,t,n,o]),r.current}
25
-
26
- export { d as CopilotContext, B as CopilotProvider, Z as useCopilotChat, ae as useMakeCopilotActionable, de as useMakeCopilotReadable };
1
+ import './chunk-EFZPSZWO.mjs';
2
+ import './chunk-JD7BAH7U.mjs';
3
+ export { CopilotProvider } from './chunk-7IPFT6UY.mjs';
4
+ import './chunk-YVXQVXMS.mjs';
5
+ import './chunk-VNRDQJXW.mjs';
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';
11
+ import './chunk-MRXNTQOX.mjs';
12
+ //# sourceMappingURL=out.js.map
13
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"names":[],"mappings":""}
@@ -0,0 +1,24 @@
1
+ interface AnnotatedFunctionSimpleArgument {
2
+ name: string;
3
+ type: "string" | "number" | "boolean" | "object";
4
+ description: string;
5
+ required: boolean;
6
+ }
7
+ interface AnnotatedFunctionArrayArgument {
8
+ name: string;
9
+ type: "array";
10
+ items: {
11
+ type: string;
12
+ };
13
+ description: string;
14
+ required: boolean;
15
+ }
16
+ type AnnotatedFunctionArgument = AnnotatedFunctionSimpleArgument | AnnotatedFunctionArrayArgument;
17
+ interface AnnotatedFunction<Inputs extends any[]> {
18
+ name: string;
19
+ description: string;
20
+ argumentAnnotations: AnnotatedFunctionArgument[];
21
+ implementation: (...args: Inputs) => Promise<void>;
22
+ }
23
+
24
+ export { AnnotatedFunction, AnnotatedFunctionArgument, AnnotatedFunctionArrayArgument, AnnotatedFunctionSimpleArgument };
@@ -0,0 +1,3 @@
1
+
2
+ //# sourceMappingURL=out.js.map
3
+ //# sourceMappingURL=annotated-function.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"names":[],"mappings":""}
@@ -0,0 +1 @@
1
+ export { AnnotatedFunction, AnnotatedFunctionArgument } from './annotated-function.js';
@@ -0,0 +1,3 @@
1
+ import '../chunk-EFZPSZWO.mjs';
2
+ //# sourceMappingURL=out.js.map
3
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"names":[],"mappings":""}
package/package.json CHANGED
@@ -4,9 +4,9 @@
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
- "version": "0.3.0",
7
+ "version": "0.4.0",
8
8
  "sideEffects": false,
9
- "main": "./dist/index.mjs",
9
+ "main": "./dist/index.js",
10
10
  "module": "./dist/index.mjs",
11
11
  "exports": "./dist/index.mjs",
12
12
  "types": "./dist/index.d.ts",
@@ -18,19 +18,19 @@
18
18
  "@types/react": "^18.2.5",
19
19
  "eslint": "^7.32.0",
20
20
  "react": "^18.2.0",
21
- "tsup": "^6.1.3",
21
+ "tsup": "^6.5.0",
22
22
  "typescript": "^4.9.4",
23
- "eslint-config-custom": "0.1.0",
24
- "tsconfig": "0.1.0"
23
+ "tsconfig": "0.2.0",
24
+ "eslint-config-custom": "0.1.0"
25
25
  },
26
26
  "dependencies": {
27
- "ai": "^2.1.19",
27
+ "ai": "^2.1.22",
28
28
  "nanoid": "^4.0.2",
29
29
  "openai-edge": "^1.2.0"
30
30
  },
31
31
  "scripts": {
32
- "build": "tsup src/index.tsx --format esm,cjs --dts --external react",
33
- "dev": "tsup src/index.tsx --format esm,cjs --dts --external react --watch",
32
+ "build": "tsup --treeshake",
33
+ "dev": "tsup --watch --no-splitting",
34
34
  "check-types": "tsc --noEmit",
35
35
  "clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist && rm -rf .next"
36
36
  }
@@ -1,113 +1,116 @@
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
- import { FunctionCallHandler } from 'ai'
7
- import { ChatCompletionFunctions } from 'openai-edge/types/api'
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
+ import { FunctionCallHandler } from "ai";
7
+ import { ChatCompletionFunctions } from "openai-edge/types/api";
8
8
 
9
9
  export function CopilotProvider({
10
- children
10
+ children,
11
11
  }: {
12
- children: ReactNode
12
+ children: ReactNode;
13
13
  }): JSX.Element {
14
14
  const [entryPoints, setEntryPoints] = useState<
15
15
  Record<string, AnnotatedFunction<any[]>>
16
- >({})
16
+ >({});
17
17
 
18
- const { addElement, removeElement, printTree } = useTree()
18
+ const { addElement, removeElement, printTree } = useTree();
19
19
 
20
20
  const setEntryPoint = useCallback(
21
21
  (id: string, entryPoint: AnnotatedFunction<any[]>) => {
22
- setEntryPoints(prevPoints => {
22
+ setEntryPoints((prevPoints) => {
23
23
  return {
24
24
  ...prevPoints,
25
- [id]: entryPoint
26
- }
27
- })
25
+ [id]: entryPoint,
26
+ };
27
+ });
28
28
  },
29
29
  []
30
- )
30
+ );
31
31
 
32
32
  const removeEntryPoint = useCallback((id: string) => {
33
- setEntryPoints(prevPoints => {
34
- const newPoints = { ...prevPoints }
35
- delete newPoints[id]
36
- return newPoints
37
- })
38
- }, [])
33
+ setEntryPoints((prevPoints) => {
34
+ const newPoints = { ...prevPoints };
35
+ delete newPoints[id];
36
+ return newPoints;
37
+ });
38
+ }, []);
39
39
 
40
40
  const getContextString = useCallback(() => {
41
- return printTree()
42
- }, [printTree])
41
+ return printTree();
42
+ }, [printTree]);
43
43
 
44
44
  const addContext = useCallback(
45
45
  (context: string, parentId?: string) => {
46
- return addElement(context, parentId)
46
+ return addElement(context, parentId);
47
47
  },
48
48
  [addElement]
49
- )
49
+ );
50
50
 
51
51
  const removeContext = useCallback(
52
52
  (id: string) => {
53
- removeElement(id)
53
+ removeElement(id);
54
54
  },
55
55
  [removeElement]
56
- )
56
+ );
57
57
 
58
- const getChatCompletionFunctions = useCallback(() => {
59
- return entryPointsToChatCompletionFunctions(Object.values(entryPoints))
60
- }, [entryPoints])
58
+ const getChatCompletionFunctionDescriptions = useCallback(() => {
59
+ return entryPointsToChatCompletionFunctions(Object.values(entryPoints));
60
+ }, [entryPoints]);
61
61
 
62
62
  const getFunctionCallHandler = useCallback(() => {
63
- return entryPointsToFunctionCallHandler(Object.values(entryPoints))
64
- }, [entryPoints])
63
+ return entryPointsToFunctionCallHandler(Object.values(entryPoints));
64
+ }, [entryPoints]);
65
65
 
66
66
  return (
67
67
  <CopilotContext.Provider
68
68
  value={{
69
69
  entryPoints,
70
- getChatCompletionFunctions,
70
+ getChatCompletionFunctionDescriptions,
71
71
  getFunctionCallHandler,
72
72
  setEntryPoint,
73
73
  removeEntryPoint,
74
74
  getContextString,
75
75
  addContext,
76
- removeContext
76
+ removeContext,
77
77
  }}
78
78
  >
79
79
  {children}
80
80
  </CopilotContext.Provider>
81
- )
81
+ );
82
82
  }
83
83
 
84
84
  function entryPointsToFunctionCallHandler(
85
85
  entryPoints: AnnotatedFunction<any[]>[]
86
86
  ): FunctionCallHandler {
87
87
  return async (chatMessages, functionCall) => {
88
- let entrypointsByFunctionName: Record<string, AnnotatedFunction<any[]>> = {}
88
+ let entrypointsByFunctionName: Record<
89
+ string,
90
+ AnnotatedFunction<any[]>
91
+ > = {};
89
92
  for (let entryPoint of entryPoints) {
90
- entrypointsByFunctionName[entryPoint.name] = entryPoint
93
+ entrypointsByFunctionName[entryPoint.name] = entryPoint;
91
94
  }
92
95
 
93
96
  const entryPointFunction =
94
- entrypointsByFunctionName[functionCall.name || '']
97
+ entrypointsByFunctionName[functionCall.name || ""];
95
98
  if (entryPointFunction) {
96
- let parsedFunctionCallArguments: Record<string, any>[] = []
99
+ let parsedFunctionCallArguments: Record<string, any>[] = [];
97
100
  if (functionCall.arguments) {
98
- parsedFunctionCallArguments = JSON.parse(functionCall.arguments)
101
+ parsedFunctionCallArguments = JSON.parse(functionCall.arguments);
99
102
  }
100
103
 
101
- const paramsInCorrectOrder: any[] = []
104
+ const paramsInCorrectOrder: any[] = [];
102
105
  for (let arg of entryPointFunction.argumentAnnotations) {
103
106
  paramsInCorrectOrder.push(
104
107
  parsedFunctionCallArguments[
105
108
  arg.name as keyof typeof parsedFunctionCallArguments
106
109
  ]
107
- )
110
+ );
108
111
  }
109
112
 
110
- await entryPointFunction.implementation(...paramsInCorrectOrder)
113
+ await entryPointFunction.implementation(...paramsInCorrectOrder);
111
114
 
112
115
  // commented out becasue for now we don't want to return anything
113
116
  // const result = await entryPointFunction.implementation(
@@ -127,28 +130,30 @@ function entryPointsToFunctionCallHandler(
127
130
 
128
131
  // return functionResponse;
129
132
  }
130
- }
133
+ };
131
134
  }
132
135
 
133
136
  function entryPointsToChatCompletionFunctions(
134
137
  entryPoints: AnnotatedFunction<any[]>[]
135
138
  ): ChatCompletionFunctions[] {
136
- return entryPoints.map(annotatedFunctionToChatCompletionFunction)
139
+ return entryPoints.map(annotatedFunctionToChatCompletionFunction);
137
140
  }
138
141
 
139
142
  function annotatedFunctionToChatCompletionFunction(
140
143
  annotatedFunction: AnnotatedFunction<any[]>
141
144
  ): ChatCompletionFunctions {
142
145
  // Create the parameters object based on the argumentAnnotations
143
- let parameters: { [key: string]: any } = {}
146
+ let parameters: { [key: string]: any } = {};
144
147
  for (let arg of annotatedFunction.argumentAnnotations) {
145
- parameters[arg.name] = { type: arg.type, description: arg.description }
148
+ // isolate the args we should forward inline
149
+ let { name, required, ...forwardedArgs } = arg;
150
+ parameters[arg.name] = forwardedArgs;
146
151
  }
147
152
 
148
- let requiredParameterNames: string[] = []
153
+ let requiredParameterNames: string[] = [];
149
154
  for (let arg of annotatedFunction.argumentAnnotations) {
150
155
  if (arg.required) {
151
- requiredParameterNames.push(arg.name)
156
+ requiredParameterNames.push(arg.name);
152
157
  }
153
158
  }
154
159
 
@@ -157,11 +162,11 @@ function annotatedFunctionToChatCompletionFunction(
157
162
  name: annotatedFunction.name,
158
163
  description: annotatedFunction.description,
159
164
  parameters: {
160
- type: 'object',
165
+ type: "object",
161
166
  properties: parameters,
162
- required: requiredParameterNames
163
- }
164
- }
167
+ required: requiredParameterNames,
168
+ },
169
+ };
165
170
 
166
- return chatCompletionFunction
171
+ return chatCompletionFunction;
167
172
  }
@@ -1 +1 @@
1
- export { CopilotProvider } from './copilot-provider'
1
+ export { CopilotProvider } from "./copilot-provider";