@copilotkit/react-core 0.8.0-alpha.5 → 0.8.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.
- package/.turbo/turbo-build.log +90 -80
- package/CHANGELOG.md +22 -0
- package/dist/chunk-3BOHSSKR.mjs +141 -0
- package/dist/chunk-3BOHSSKR.mjs.map +1 -0
- package/dist/chunk-3O374DDN.mjs +19 -0
- package/dist/chunk-3O374DDN.mjs.map +1 -0
- package/dist/chunk-63ILXW5V.mjs +76 -0
- package/dist/chunk-63ILXW5V.mjs.map +1 -0
- package/dist/chunk-6RRG7ME3.mjs +19 -0
- package/dist/chunk-6RRG7ME3.mjs.map +1 -0
- package/dist/chunk-EFZPSZWO.mjs +3 -0
- package/dist/chunk-EFZPSZWO.mjs.map +1 -0
- package/dist/chunk-JD7BAH7U.mjs +3 -0
- package/dist/chunk-JD7BAH7U.mjs.map +1 -0
- package/dist/chunk-K7TVQCXG.mjs +44 -0
- package/dist/chunk-K7TVQCXG.mjs.map +1 -0
- package/dist/chunk-MRXNTQOX.mjs +55 -0
- package/dist/chunk-MRXNTQOX.mjs.map +1 -0
- package/dist/chunk-QACD2U6P.mjs +3 -0
- package/dist/chunk-QACD2U6P.mjs.map +1 -0
- package/dist/chunk-QFCPUIF7.mjs +147 -0
- package/dist/chunk-QFCPUIF7.mjs.map +1 -0
- package/dist/chunk-UNFDVVCF.mjs +80 -0
- package/dist/chunk-UNFDVVCF.mjs.map +1 -0
- package/dist/chunk-YPSGKPDA.mjs +3 -0
- package/dist/chunk-YPSGKPDA.mjs.map +1 -0
- package/dist/chunk-YTIZQV7H.mjs +30 -0
- package/dist/chunk-YTIZQV7H.mjs.map +1 -0
- package/dist/components/copilot-provider.d.ts +1 -0
- package/dist/components/copilot-provider.mjs +6 -340
- package/dist/components/copilot-provider.mjs.map +1 -1
- package/dist/components/index.d.ts +1 -0
- package/dist/components/index.mjs +7 -340
- package/dist/components/index.mjs.map +1 -1
- package/dist/context/copilot-context.d.ts +4 -0
- package/dist/context/copilot-context.mjs +3 -63
- package/dist/context/copilot-context.mjs.map +1 -1
- package/dist/context/index.d.ts +1 -0
- package/dist/context/index.mjs +4 -63
- package/dist/context/index.mjs.map +1 -1
- package/dist/hooks/index.d.ts +2 -0
- package/dist/hooks/index.mjs +8 -199
- package/dist/hooks/index.mjs.map +1 -1
- package/dist/hooks/use-copilot-chat.mjs +4 -155
- package/dist/hooks/use-copilot-chat.mjs.map +1 -1
- package/dist/hooks/use-flat-category-store.d.ts +9 -0
- package/dist/hooks/use-flat-category-store.mjs +4 -0
- package/dist/hooks/use-flat-category-store.mjs.map +1 -0
- package/dist/hooks/use-make-copilot-actionable.mjs +4 -84
- package/dist/hooks/use-make-copilot-actionable.mjs.map +1 -1
- package/dist/hooks/use-make-copilot-document-readable.d.ts +12 -0
- package/dist/hooks/use-make-copilot-document-readable.mjs +5 -0
- package/dist/hooks/use-make-copilot-document-readable.mjs.map +1 -0
- package/dist/hooks/use-make-copilot-readable.mjs +4 -73
- package/dist/hooks/use-make-copilot-readable.mjs.map +1 -1
- package/dist/hooks/use-tree.mjs +3 -159
- package/dist/hooks/use-tree.mjs.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.mjs +14 -470
- package/dist/index.mjs.map +1 -1
- package/dist/types/annotated-function.mjs +2 -0
- package/dist/types/annotated-function.mjs.map +1 -1
- package/dist/types/document-pointer.d.ts +9 -0
- package/dist/types/document-pointer.mjs +3 -0
- package/dist/types/document-pointer.mjs.map +1 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.mjs +2 -0
- package/dist/types/index.mjs.map +1 -1
- package/dist/utils/utils.mjs +2 -0
- package/dist/utils/utils.mjs.map +1 -1
- package/dist/utils/utils.test.mjs +1 -0
- package/dist/utils/utils.test.mjs.map +1 -1
- package/package.json +3 -3
- package/src/components/copilot-provider.tsx +31 -1
- package/src/context/copilot-context.tsx +13 -0
- package/src/hooks/index.ts +1 -0
- package/src/hooks/use-flat-category-store.ts +114 -0
- package/src/hooks/use-make-copilot-document-readable.ts +32 -0
- package/src/types/document-pointer.ts +7 -0
- package/src/types/index.ts +1 -0
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/components/copilot-provider.tsx","../src/context/copilot-context.tsx","../src/hooks/use-tree.ts","../src/hooks/use-copilot-chat.ts","../src/hooks/use-make-copilot-actionable.ts","../src/hooks/use-make-copilot-readable.ts"],"sourcesContent":["\"use client\";\nimport { FunctionCallHandler } from \"ai\";\nimport { ReactNode, useCallback, useState } from \"react\";\nimport { CopilotContext, CopilotApiConfig } from \"../context/copilot-context\";\nimport useTree from \"../hooks/use-tree\";\nimport { AnnotatedFunction } from \"../types/annotated-function\";\nimport { ChatCompletionCreateParams } from \"openai/resources/chat\";\n\nexport function CopilotProvider({\n copilotApiConfig,\n children,\n}: {\n copilotApiConfig: CopilotApiConfig;\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 copilotApiConfig,\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): ChatCompletionCreateParams.Function[] {\n return entryPoints.map(annotatedFunctionToChatCompletionFunction);\n}\n\nfunction annotatedFunctionToChatCompletionFunction(\n annotatedFunction: AnnotatedFunction<any[]>\n): ChatCompletionCreateParams.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: ChatCompletionCreateParams.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","\"use client\";\n\nimport { FunctionCallHandler } from \"ai\";\nimport React from \"react\";\nimport { TreeNodeId } from \"../hooks/use-tree\";\nimport { AnnotatedFunction } from \"../types/annotated-function\";\nimport { ChatCompletionCreateParams } from \"openai/resources/chat\";\n\nexport interface CopilotApiConfig {\n chatApiEndpoint: string;\n}\n\nexport function copilotApiConfigExtrapolator(config: CopilotApiConfig) {\n return {\n get chatApiEndpoint(): string {\n return `${config.chatApiEndpoint}`;\n },\n };\n}\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: () => ChatCompletionCreateParams.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 // api endpoints\n copilotApiConfig: CopilotApiConfig;\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 copilotApiConfig: new (class implements CopilotApiConfig {\n get chatApiEndpoint(): string {\n throw new Error(\n \"Remember to wrap your app in a `<CopilotProvider> {...} </CopilotProvider>` !!!\"\n );\n }\n })(),\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","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","import { useMemo, useContext } from \"react\";\nimport {\n CopilotContext,\n CopilotContextParams,\n copilotApiConfigExtrapolator,\n} from \"../context/copilot-context\";\nimport { useChat } from \"ai/react\";\nimport { ChatRequestOptions, CreateMessage, Message } from \"ai\";\nimport { UseChatOptions } from \"ai\";\n\nexport interface UseCopilotChatOptions extends UseChatOptions {\n makeSystemMessage?: (contextString: string) => string;\n}\n\nexport interface UseCopilotChatReturn {\n visibleMessages: Message[];\n append: (\n message: Message | CreateMessage,\n chatRequestOptions?: ChatRequestOptions\n ) => Promise<string | null | undefined>;\n reload: (\n chatRequestOptions?: ChatRequestOptions\n ) => Promise<string | null | undefined>;\n stop: () => void;\n isLoading: boolean;\n input: string;\n setInput: React.Dispatch<React.SetStateAction<string>>;\n}\n\nexport function useCopilotChat({\n makeSystemMessage,\n ...options\n}: UseCopilotChatOptions): UseCopilotChatReturn {\n const {\n getContextString,\n getChatCompletionFunctionDescriptions,\n getFunctionCallHandler,\n copilotApiConfig,\n } = useContext(CopilotContext);\n\n const systemMessage: Message = useMemo(() => {\n const systemMessageMaker = makeSystemMessage || defaultSystemMessage;\n const contextString = getContextString();\n\n return {\n id: \"system\",\n content: systemMessageMaker(contextString),\n role: \"system\",\n };\n }, [getContextString, makeSystemMessage]);\n\n const initialMessagesWithContext = [systemMessage].concat(\n options.initialMessages || []\n );\n\n const functionDescriptions = useMemo(() => {\n return getChatCompletionFunctionDescriptions();\n }, [getChatCompletionFunctionDescriptions]);\n\n const { messages, append, reload, stop, isLoading, input, setInput } =\n useChat({\n api: copilotApiConfigExtrapolator(copilotApiConfig).chatApiEndpoint,\n id: options.id,\n initialMessages: initialMessagesWithContext,\n experimental_onFunctionCall: getFunctionCallHandler(),\n body: {\n id: options.id,\n functions: functionDescriptions,\n },\n });\n\n const visibleMessages = messages.filter(\n (message) => message.role === \"user\" || message.role === \"assistant\"\n );\n\n return {\n visibleMessages,\n append,\n reload,\n stop,\n isLoading,\n input,\n setInput,\n };\n}\n\nexport function defaultSystemMessage(contextString: string): string {\n return `\nPlease act as an efficient, competent, conscientious, and industrious professional assistant.\n\nHelp the user achieve their goals, and you do so in a way that is as efficient as possible, without unnecessary fluff, but also without sacrificing professionalism.\nAlways be polite and respectful, and prefer brevity over verbosity.\n\nThe user has provided you with the following context:\n\\`\\`\\`\n${contextString}\n\\`\\`\\`\n\nThey have also provided you with functions you can call to initiate actions on their behalf, or functions you can call to receive more information.\n\nPlease assist them as best you can.\n\nYou can ask them for clarifying questions if needed, but don't be annoying about it. If you can reasonably 'fill in the blanks' yourself, do so.\n\nIf you would like to call a function, call it without saying anything else.\n`;\n}\n","\"use client\";\n\nimport { useRef, useContext, useEffect, useMemo } from \"react\";\nimport { CopilotContext } from \"../context/copilot-context\";\nimport { AnnotatedFunction } from \"../types/annotated-function\";\nimport { nanoid } from \"nanoid\";\n\nexport function useMakeCopilotActionable<ActionInput extends any[]>(\n annotatedFunction: AnnotatedFunction<ActionInput>,\n dependencies: any[]\n) {\n const idRef = useRef(nanoid()); // generate a unique id\n const { setEntryPoint, removeEntryPoint } = useContext(CopilotContext);\n\n const memoizedAnnotatedFunction: AnnotatedFunction<ActionInput> = useMemo(\n () => ({\n name: annotatedFunction.name,\n description: annotatedFunction.description,\n argumentAnnotations: annotatedFunction.argumentAnnotations,\n implementation: annotatedFunction.implementation,\n }),\n dependencies\n );\n\n useEffect(() => {\n setEntryPoint(\n idRef.current,\n memoizedAnnotatedFunction as AnnotatedFunction<any[]>\n );\n\n return () => {\n removeEntryPoint(idRef.current);\n };\n }, [memoizedAnnotatedFunction, setEntryPoint, removeEntryPoint]);\n}\n","\"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"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEA,SAAoB,eAAAA,cAAa,gBAAgB;;;ACCjD,OAAO,WAAW;AAHlB;AAYO,SAAS,6BAA6B,QAA0B;AACrE,SAAO;AAAA,IACL,IAAI,kBAA0B;AAC5B,aAAO,GAAG,OAAO;AAAA,IACnB;AAAA,EACF;AACF;AAuBA,IAAM,sBAA4C;AAAA,EAChD,aAAa,CAAC;AAAA,EACd,eAAe,MAAM;AAAA,EAAC;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IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;AAEO,SAAS,qBAAqB,eAA+B;AAClE,SAAO;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQP;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAWF;;;ACxGA,SAAS,QAAQ,cAAAC,aAAY,WAAW,WAAAC,gBAAe;AAGvD,SAAS,UAAAC,eAAc;AALvB;AAOO,SAAS,yBACd,mBACA,cACA;AACA,QAAM,QAAQ,OAAOA,QAAO,CAAC;AAC7B,QAAM,EAAE,eAAe,iBAAiB,IAAIC,YAAW,cAAc;AAErE,QAAM,4BAA4DC;AAAA,IAChE,OAAO;AAAA,MACL,MAAM,kBAAkB;AAAA,MACxB,aAAa,kBAAkB;AAAA,MAC/B,qBAAqB,kBAAkB;AAAA,MACvC,gBAAgB,kBAAkB;AAAA,IACpC;AAAA,IACA;AAAA,EACF;AAEA,YAAU,MAAM;AACd;AAAA,MACE,MAAM;AAAA,MACN;AAAA,IACF;AAEA,WAAO,MAAM;AACX,uBAAiB,MAAM,OAAO;AAAA,IAChC;AAAA,EACF,GAAG,CAAC,2BAA2B,eAAe,gBAAgB,CAAC;AACjE;;;AChCA,SAAS,cAAAC,aAAY,aAAAC,YAAW,UAAAC,eAAc;AAF9C;AAYO,SAAS,uBACd,aACA,UACA,YACoB;AACpB,QAAM,EAAE,YAAY,cAAc,IAAIC,YAAW,cAAc;AAC/D,QAAM,QAAQC,QAAe;AAE7B,EAAAC,WAAU,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;","names":["useCallback","useCallback","useContext","useMemo","nanoid","useContext","useMemo","useContext","useEffect","useRef","useContext","useRef","useEffect"]}
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":""}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":[],"
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":""}
|
package/dist/types/index.d.ts
CHANGED
package/dist/types/index.mjs
CHANGED
package/dist/types/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":[],"
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":""}
|
package/dist/utils/utils.mjs
CHANGED
package/dist/utils/utils.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":[],"
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":""}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/utils/utils.test.ts"],"sourcesContent":["import * as utils from \"./utils\";\n\ndescribe(\"emptyTest\", () => {\n it(\"should be truthy\", () => {\n expect(true).toBeTruthy();\n });\n});\n"]
|
|
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/package.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
7
|
-
"version": "0.8.0
|
|
7
|
+
"version": "0.8.0",
|
|
8
8
|
"sideEffects": false,
|
|
9
9
|
"main": "./dist/index.js",
|
|
10
10
|
"module": "./dist/index.mjs",
|
|
@@ -23,8 +23,8 @@
|
|
|
23
23
|
"ts-jest": "^29.1.1",
|
|
24
24
|
"tsup": "^6.5.0",
|
|
25
25
|
"typescript": "^4.9.4",
|
|
26
|
-
"
|
|
27
|
-
"
|
|
26
|
+
"eslint-config-custom": "0.2.0",
|
|
27
|
+
"tsconfig": "0.4.0"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
30
|
"ai": "^2.2.12",
|
|
@@ -5,6 +5,8 @@ import { CopilotContext, CopilotApiConfig } from "../context/copilot-context";
|
|
|
5
5
|
import useTree from "../hooks/use-tree";
|
|
6
6
|
import { AnnotatedFunction } from "../types/annotated-function";
|
|
7
7
|
import { ChatCompletionCreateParams } from "openai/resources/chat";
|
|
8
|
+
import { DocumentPointer } from "../types";
|
|
9
|
+
import useFlatCategoryStore from "../hooks/use-flat-category-store";
|
|
8
10
|
|
|
9
11
|
export function CopilotProvider({
|
|
10
12
|
copilotApiConfig,
|
|
@@ -18,6 +20,11 @@ export function CopilotProvider({
|
|
|
18
20
|
>({});
|
|
19
21
|
|
|
20
22
|
const { addElement, removeElement, printTree } = useTree();
|
|
23
|
+
const {
|
|
24
|
+
addElement: addDocument,
|
|
25
|
+
removeElement: removeDocument,
|
|
26
|
+
allElements: allDocuments,
|
|
27
|
+
} = useFlatCategoryStore<DocumentPointer>();
|
|
21
28
|
|
|
22
29
|
const setEntryPoint = useCallback(
|
|
23
30
|
(id: string, entryPoint: AnnotatedFunction<any[]>) => {
|
|
@@ -68,6 +75,27 @@ export function CopilotProvider({
|
|
|
68
75
|
return entryPointsToFunctionCallHandler(Object.values(entryPoints));
|
|
69
76
|
}, [entryPoints]);
|
|
70
77
|
|
|
78
|
+
const getDocumentsContext = useCallback(
|
|
79
|
+
(categories: string[] = ["global"]) => {
|
|
80
|
+
return allDocuments(categories);
|
|
81
|
+
},
|
|
82
|
+
[allDocuments]
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
const addDocumentContext = useCallback(
|
|
86
|
+
(documentPointer: DocumentPointer, categories: string[] = ["global"]) => {
|
|
87
|
+
return addDocument(documentPointer, categories);
|
|
88
|
+
},
|
|
89
|
+
[addDocument]
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
const removeDocumentContext = useCallback(
|
|
93
|
+
(documentId: string) => {
|
|
94
|
+
removeDocument(documentId);
|
|
95
|
+
},
|
|
96
|
+
[removeDocument]
|
|
97
|
+
);
|
|
98
|
+
|
|
71
99
|
return (
|
|
72
100
|
<CopilotContext.Provider
|
|
73
101
|
value={{
|
|
@@ -79,7 +107,9 @@ export function CopilotProvider({
|
|
|
79
107
|
getContextString,
|
|
80
108
|
addContext,
|
|
81
109
|
removeContext,
|
|
82
|
-
|
|
110
|
+
getDocumentsContext,
|
|
111
|
+
addDocumentContext,
|
|
112
|
+
removeDocumentContext,
|
|
83
113
|
copilotApiConfig,
|
|
84
114
|
}}
|
|
85
115
|
>
|
|
@@ -5,6 +5,7 @@ import React from "react";
|
|
|
5
5
|
import { TreeNodeId } from "../hooks/use-tree";
|
|
6
6
|
import { AnnotatedFunction } from "../types/annotated-function";
|
|
7
7
|
import { ChatCompletionCreateParams } from "openai/resources/chat";
|
|
8
|
+
import { DocumentPointer } from "../types";
|
|
8
9
|
|
|
9
10
|
export interface CopilotApiConfig {
|
|
10
11
|
chatApiEndpoint: string;
|
|
@@ -35,6 +36,14 @@ export interface CopilotContextParams {
|
|
|
35
36
|
) => TreeNodeId;
|
|
36
37
|
removeContext: (id: TreeNodeId) => void;
|
|
37
38
|
|
|
39
|
+
// document context
|
|
40
|
+
getDocumentsContext: (categories?: string[]) => DocumentPointer[];
|
|
41
|
+
addDocumentContext: (
|
|
42
|
+
documentPointer: DocumentPointer,
|
|
43
|
+
categories?: string[]
|
|
44
|
+
) => TreeNodeId;
|
|
45
|
+
removeDocumentContext: (documentId: string) => void;
|
|
46
|
+
|
|
38
47
|
// api endpoints
|
|
39
48
|
copilotApiConfig: CopilotApiConfig;
|
|
40
49
|
}
|
|
@@ -50,6 +59,10 @@ const emptyCopilotContext: CopilotContextParams = {
|
|
|
50
59
|
addContext: () => "",
|
|
51
60
|
removeContext: () => {},
|
|
52
61
|
|
|
62
|
+
getDocumentsContext: () => returnAndThrowInDebug([]),
|
|
63
|
+
addDocumentContext: () => returnAndThrowInDebug(""),
|
|
64
|
+
removeDocumentContext: () => {},
|
|
65
|
+
|
|
53
66
|
copilotApiConfig: new (class implements CopilotApiConfig {
|
|
54
67
|
get chatApiEndpoint(): string {
|
|
55
68
|
throw new Error(
|
package/src/hooks/index.ts
CHANGED
|
@@ -4,3 +4,4 @@ export type { UseCopilotChatReturn } from "./use-copilot-chat";
|
|
|
4
4
|
|
|
5
5
|
export { useMakeCopilotActionable } from "./use-make-copilot-actionable";
|
|
6
6
|
export { useMakeCopilotReadable } from "./use-make-copilot-readable";
|
|
7
|
+
export { useMakeCopilotDocumentReadable } from "./use-make-copilot-document-readable";
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { nanoid } from "nanoid";
|
|
2
|
+
import { useCallback, useReducer } from "react";
|
|
3
|
+
|
|
4
|
+
export type FlatCategoryStoreId = string;
|
|
5
|
+
|
|
6
|
+
export interface UseFlatCategoryStoreReturn<T> {
|
|
7
|
+
addElement: (value: T, categories: string[]) => FlatCategoryStoreId;
|
|
8
|
+
removeElement: (id: FlatCategoryStoreId) => void;
|
|
9
|
+
allElements: (categories: string[]) => T[];
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
interface FlatCategoryStoreElement<T> {
|
|
13
|
+
id: FlatCategoryStoreId;
|
|
14
|
+
value: T;
|
|
15
|
+
categories: Set<string>;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const useFlatCategoryStore = <T>(): UseFlatCategoryStoreReturn<T> => {
|
|
19
|
+
const [elements, dispatch] = useReducer<
|
|
20
|
+
React.Reducer<
|
|
21
|
+
Map<FlatCategoryStoreId, FlatCategoryStoreElement<T>>,
|
|
22
|
+
Action<T>
|
|
23
|
+
>
|
|
24
|
+
>(
|
|
25
|
+
flatCategoryStoreReducer,
|
|
26
|
+
new Map<FlatCategoryStoreId, FlatCategoryStoreElement<T>>()
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
const addElement = useCallback(
|
|
30
|
+
(value: T, categories: string[]): FlatCategoryStoreId => {
|
|
31
|
+
const newId = nanoid();
|
|
32
|
+
dispatch({
|
|
33
|
+
type: "ADD_ELEMENT",
|
|
34
|
+
value,
|
|
35
|
+
id: newId,
|
|
36
|
+
categories,
|
|
37
|
+
});
|
|
38
|
+
return newId;
|
|
39
|
+
},
|
|
40
|
+
[]
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
const removeElement = useCallback((id: FlatCategoryStoreId): void => {
|
|
44
|
+
dispatch({ type: "REMOVE_ELEMENT", id });
|
|
45
|
+
}, []);
|
|
46
|
+
|
|
47
|
+
const allElements = useCallback(
|
|
48
|
+
(categories: string[]): T[] => {
|
|
49
|
+
const categoriesSet = new Set(categories);
|
|
50
|
+
const result: T[] = [];
|
|
51
|
+
elements.forEach((element) => {
|
|
52
|
+
if (setsHaveIntersection(categoriesSet, element.categories)) {
|
|
53
|
+
result.push(element.value);
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
return result;
|
|
57
|
+
},
|
|
58
|
+
[elements]
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
return { addElement, removeElement, allElements };
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
export default useFlatCategoryStore;
|
|
65
|
+
|
|
66
|
+
// Action types
|
|
67
|
+
type Action<T> =
|
|
68
|
+
| {
|
|
69
|
+
type: "ADD_ELEMENT";
|
|
70
|
+
value: T;
|
|
71
|
+
id: FlatCategoryStoreId;
|
|
72
|
+
categories: string[];
|
|
73
|
+
}
|
|
74
|
+
| { type: "REMOVE_ELEMENT"; id: FlatCategoryStoreId };
|
|
75
|
+
|
|
76
|
+
// Reducer
|
|
77
|
+
function flatCategoryStoreReducer<T>(
|
|
78
|
+
state: Map<FlatCategoryStoreId, FlatCategoryStoreElement<T>>,
|
|
79
|
+
action: Action<T>
|
|
80
|
+
): Map<FlatCategoryStoreId, FlatCategoryStoreElement<T>> {
|
|
81
|
+
switch (action.type) {
|
|
82
|
+
case "ADD_ELEMENT": {
|
|
83
|
+
const { value, id, categories } = action;
|
|
84
|
+
const newElement: FlatCategoryStoreElement<T> = {
|
|
85
|
+
id,
|
|
86
|
+
value,
|
|
87
|
+
categories: new Set(categories),
|
|
88
|
+
};
|
|
89
|
+
const newState = new Map(state);
|
|
90
|
+
newState.set(id, newElement);
|
|
91
|
+
return newState;
|
|
92
|
+
}
|
|
93
|
+
case "REMOVE_ELEMENT": {
|
|
94
|
+
const newState = new Map(state);
|
|
95
|
+
newState.delete(action.id);
|
|
96
|
+
return newState;
|
|
97
|
+
}
|
|
98
|
+
default:
|
|
99
|
+
return state;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function setsHaveIntersection<T>(setA: Set<T>, setB: Set<T>): boolean {
|
|
104
|
+
const [smallerSet, largerSet] =
|
|
105
|
+
setA.size <= setB.size ? [setA, setB] : [setB, setA];
|
|
106
|
+
|
|
107
|
+
for (let item of smallerSet) {
|
|
108
|
+
if (largerSet.has(item)) {
|
|
109
|
+
return true;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useContext, useEffect, useRef } from "react";
|
|
4
|
+
import { CopilotContext } from "../context/copilot-context";
|
|
5
|
+
import { DocumentPointer } from "../types";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Adds the given information to the Copilot context to make it readable by Copilot.
|
|
9
|
+
* @param information - The information to be added to the Copilot context.
|
|
10
|
+
* @param parentId - The ID of the parent context, if any.
|
|
11
|
+
* @param categories - An array of categories to control which context are visible where. Particularly useful with CopilotTextarea (see `useMakeAutosuggestionFunction`)
|
|
12
|
+
* @returns The ID of the added context.
|
|
13
|
+
*/
|
|
14
|
+
export function useMakeCopilotDocumentReadable(
|
|
15
|
+
document: DocumentPointer,
|
|
16
|
+
categories?: string[]
|
|
17
|
+
): string | undefined {
|
|
18
|
+
const { addDocumentContext, removeDocumentContext } =
|
|
19
|
+
useContext(CopilotContext);
|
|
20
|
+
const idRef = useRef<string>();
|
|
21
|
+
|
|
22
|
+
useEffect(() => {
|
|
23
|
+
const id = addDocumentContext(document, categories);
|
|
24
|
+
idRef.current = id;
|
|
25
|
+
|
|
26
|
+
return () => {
|
|
27
|
+
removeDocumentContext(id);
|
|
28
|
+
};
|
|
29
|
+
}, [document, categories, addDocumentContext, removeDocumentContext]);
|
|
30
|
+
|
|
31
|
+
return idRef.current;
|
|
32
|
+
}
|
package/src/types/index.ts
CHANGED