@copilotkit/react-core 0.2.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 (80) hide show
  1. package/.turbo/turbo-build.log +70 -12
  2. package/CHANGELOG.md +13 -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 -1
  54. package/dist/index.mjs +13 -0
  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 +17 -14
  63. package/src/components/copilot-provider.tsx +172 -0
  64. package/src/components/index.ts +1 -0
  65. package/src/context/copilot-context.tsx +33 -0
  66. package/src/context/index.ts +2 -0
  67. package/src/hooks/index.ts +6 -0
  68. package/src/hooks/use-copilot-chat.ts +107 -0
  69. package/src/hooks/use-make-copilot-actionable.ts +35 -0
  70. package/src/hooks/use-make-copilot-readable.ts +23 -0
  71. package/src/hooks/use-tree.ts +170 -0
  72. package/src/index.tsx +4 -2
  73. package/src/types/annotated-function.ts +27 -0
  74. package/src/types/index.ts +2 -0
  75. package/tsup.config.ts +3 -4
  76. package/dist/chunk-KFCOT2YX.js +0 -26
  77. package/dist/hook.d.ts +0 -3
  78. package/dist/hook.js +0 -10
  79. package/dist/index.js +0 -10
  80. package/src/hook.tsx +0 -5
@@ -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 +1,10 @@
1
- export { useTurbo } from './hook.js';
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 ADDED
@@ -0,0 +1,13 @@
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,15 +4,12 @@
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
- "version": "0.2.0",
8
- "sideEffects": [
9
- "**/*.css"
10
- ],
7
+ "version": "0.4.0",
8
+ "sideEffects": false,
9
+ "main": "./dist/index.js",
10
+ "module": "./dist/index.mjs",
11
+ "exports": "./dist/index.mjs",
11
12
  "types": "./dist/index.d.ts",
12
- "exports": {
13
- ".": "./dist",
14
- "./styles.css": "./dist/index.css"
15
- },
16
13
  "license": "MIT",
17
14
  "peerDependencies": {
18
15
  "react": "^18.2.0"
@@ -21,14 +18,20 @@
21
18
  "@types/react": "^18.2.5",
22
19
  "eslint": "^7.32.0",
23
20
  "react": "^18.2.0",
24
- "tsup": "^6.1.3",
21
+ "tsup": "^6.5.0",
25
22
  "typescript": "^4.9.4",
26
- "eslint-config-custom": "0.0.0",
27
- "tsconfig": "0.0.0"
23
+ "tsconfig": "0.2.0",
24
+ "eslint-config-custom": "0.1.0"
25
+ },
26
+ "dependencies": {
27
+ "ai": "^2.1.22",
28
+ "nanoid": "^4.0.2",
29
+ "openai-edge": "^1.2.0"
28
30
  },
29
31
  "scripts": {
30
- "build": "tsup --format cjs --dts",
31
- "dev": "tsup --watch",
32
- "check-types": "tsc --noEmit"
32
+ "build": "tsup --treeshake",
33
+ "dev": "tsup --watch --no-splitting",
34
+ "check-types": "tsc --noEmit",
35
+ "clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist && rm -rf .next"
33
36
  }
34
37
  }
@@ -0,0 +1,172 @@
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
+
9
+ export function CopilotProvider({
10
+ children,
11
+ }: {
12
+ children: ReactNode;
13
+ }): JSX.Element {
14
+ const [entryPoints, setEntryPoints] = useState<
15
+ Record<string, AnnotatedFunction<any[]>>
16
+ >({});
17
+
18
+ const { addElement, removeElement, printTree } = useTree();
19
+
20
+ const setEntryPoint = useCallback(
21
+ (id: string, entryPoint: AnnotatedFunction<any[]>) => {
22
+ setEntryPoints((prevPoints) => {
23
+ return {
24
+ ...prevPoints,
25
+ [id]: entryPoint,
26
+ };
27
+ });
28
+ },
29
+ []
30
+ );
31
+
32
+ const removeEntryPoint = useCallback((id: string) => {
33
+ setEntryPoints((prevPoints) => {
34
+ const newPoints = { ...prevPoints };
35
+ delete newPoints[id];
36
+ return newPoints;
37
+ });
38
+ }, []);
39
+
40
+ const getContextString = useCallback(() => {
41
+ return printTree();
42
+ }, [printTree]);
43
+
44
+ const addContext = useCallback(
45
+ (context: string, parentId?: string) => {
46
+ return addElement(context, parentId);
47
+ },
48
+ [addElement]
49
+ );
50
+
51
+ const removeContext = useCallback(
52
+ (id: string) => {
53
+ removeElement(id);
54
+ },
55
+ [removeElement]
56
+ );
57
+
58
+ const getChatCompletionFunctionDescriptions = useCallback(() => {
59
+ return entryPointsToChatCompletionFunctions(Object.values(entryPoints));
60
+ }, [entryPoints]);
61
+
62
+ const getFunctionCallHandler = useCallback(() => {
63
+ return entryPointsToFunctionCallHandler(Object.values(entryPoints));
64
+ }, [entryPoints]);
65
+
66
+ return (
67
+ <CopilotContext.Provider
68
+ value={{
69
+ entryPoints,
70
+ getChatCompletionFunctionDescriptions,
71
+ getFunctionCallHandler,
72
+ setEntryPoint,
73
+ removeEntryPoint,
74
+ getContextString,
75
+ addContext,
76
+ removeContext,
77
+ }}
78
+ >
79
+ {children}
80
+ </CopilotContext.Provider>
81
+ );
82
+ }
83
+
84
+ function entryPointsToFunctionCallHandler(
85
+ entryPoints: AnnotatedFunction<any[]>[]
86
+ ): FunctionCallHandler {
87
+ return async (chatMessages, functionCall) => {
88
+ let entrypointsByFunctionName: Record<
89
+ string,
90
+ AnnotatedFunction<any[]>
91
+ > = {};
92
+ for (let entryPoint of entryPoints) {
93
+ entrypointsByFunctionName[entryPoint.name] = entryPoint;
94
+ }
95
+
96
+ const entryPointFunction =
97
+ entrypointsByFunctionName[functionCall.name || ""];
98
+ if (entryPointFunction) {
99
+ let parsedFunctionCallArguments: Record<string, any>[] = [];
100
+ if (functionCall.arguments) {
101
+ parsedFunctionCallArguments = JSON.parse(functionCall.arguments);
102
+ }
103
+
104
+ const paramsInCorrectOrder: any[] = [];
105
+ for (let arg of entryPointFunction.argumentAnnotations) {
106
+ paramsInCorrectOrder.push(
107
+ parsedFunctionCallArguments[
108
+ arg.name as keyof typeof parsedFunctionCallArguments
109
+ ]
110
+ );
111
+ }
112
+
113
+ await entryPointFunction.implementation(...paramsInCorrectOrder);
114
+
115
+ // commented out becasue for now we don't want to return anything
116
+ // const result = await entryPointFunction.implementation(
117
+ // ...parsedFunctionCallArguments
118
+ // );
119
+ // const functionResponse: ChatRequest = {
120
+ // messages: [
121
+ // ...chatMessages,
122
+ // {
123
+ // id: nanoid(),
124
+ // name: functionCall.name,
125
+ // role: 'function' as const,
126
+ // content: JSON.stringify(result),
127
+ // },
128
+ // ],
129
+ // };
130
+
131
+ // return functionResponse;
132
+ }
133
+ };
134
+ }
135
+
136
+ function entryPointsToChatCompletionFunctions(
137
+ entryPoints: AnnotatedFunction<any[]>[]
138
+ ): ChatCompletionFunctions[] {
139
+ return entryPoints.map(annotatedFunctionToChatCompletionFunction);
140
+ }
141
+
142
+ function annotatedFunctionToChatCompletionFunction(
143
+ annotatedFunction: AnnotatedFunction<any[]>
144
+ ): ChatCompletionFunctions {
145
+ // Create the parameters object based on the argumentAnnotations
146
+ let parameters: { [key: string]: any } = {};
147
+ for (let arg of annotatedFunction.argumentAnnotations) {
148
+ // isolate the args we should forward inline
149
+ let { name, required, ...forwardedArgs } = arg;
150
+ parameters[arg.name] = forwardedArgs;
151
+ }
152
+
153
+ let requiredParameterNames: string[] = [];
154
+ for (let arg of annotatedFunction.argumentAnnotations) {
155
+ if (arg.required) {
156
+ requiredParameterNames.push(arg.name);
157
+ }
158
+ }
159
+
160
+ // Create the ChatCompletionFunctions object
161
+ let chatCompletionFunction: ChatCompletionFunctions = {
162
+ name: annotatedFunction.name,
163
+ description: annotatedFunction.description,
164
+ parameters: {
165
+ type: "object",
166
+ properties: parameters,
167
+ required: requiredParameterNames,
168
+ },
169
+ };
170
+
171
+ return chatCompletionFunction;
172
+ }
@@ -0,0 +1 @@
1
+ export { CopilotProvider } from "./copilot-provider";
@@ -0,0 +1,33 @@
1
+ "use client";
2
+
3
+ import React from "react";
4
+ import { AnnotatedFunction } from "../types/annotated-function";
5
+ import { TreeNodeId } from "../hooks/use-tree";
6
+ import { ChatCompletionFunctions } from "openai-edge/types/api";
7
+ import { FunctionCallHandler } from "ai";
8
+
9
+ export interface CopilotContextParams {
10
+ entryPoints: Record<string, AnnotatedFunction<any[]>>;
11
+ getChatCompletionFunctionDescriptions: () => ChatCompletionFunctions[];
12
+ getFunctionCallHandler: () => FunctionCallHandler;
13
+ setEntryPoint: (id: string, entryPoint: AnnotatedFunction<any[]>) => void;
14
+ removeEntryPoint: (id: string) => void;
15
+
16
+ getContextString: () => string;
17
+ addContext: (context: string, parentId?: string) => TreeNodeId;
18
+ removeContext: (id: TreeNodeId) => void;
19
+ }
20
+
21
+ const emptyCopilotContext: CopilotContextParams = {
22
+ entryPoints: {},
23
+ getChatCompletionFunctionDescriptions: () => [],
24
+ getFunctionCallHandler: () => async () => {},
25
+ setEntryPoint: () => {},
26
+ removeEntryPoint: () => {},
27
+ getContextString: () => "",
28
+ addContext: () => "",
29
+ removeContext: () => {},
30
+ };
31
+
32
+ export const CopilotContext =
33
+ React.createContext<CopilotContextParams>(emptyCopilotContext);
@@ -0,0 +1,2 @@
1
+ export { CopilotContext } from "./copilot-context";
2
+ export type { CopilotContextParams } from "./copilot-context";
@@ -0,0 +1,6 @@
1
+ export { useCopilotChat } from "./use-copilot-chat";
2
+ export type { UseCopilotChatOptions } from "./use-copilot-chat";
3
+ export type { UseCopilotChatReturn } from "./use-copilot-chat";
4
+
5
+ export { useMakeCopilotActionable } from "./use-make-copilot-actionable";
6
+ export { useMakeCopilotReadable } from "./use-make-copilot-readable";