@copilotkit/react-core 0.9.0 → 0.10.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 (37) hide show
  1. package/.turbo/turbo-build.log +18 -18
  2. package/CHANGELOG.md +6 -0
  3. package/dist/{chunk-Q6E6JSLL.mjs → chunk-F4BQVCQY.mjs} +11 -4
  4. package/dist/chunk-F4BQVCQY.mjs.map +1 -0
  5. package/dist/{chunk-K7TVQCXG.mjs → chunk-GGSWSZ2C.mjs} +2 -2
  6. package/dist/chunk-GGSWSZ2C.mjs.map +1 -0
  7. package/dist/{chunk-YTIZQV7H.mjs → chunk-HRUQ2NR4.mjs} +2 -2
  8. package/dist/{chunk-3O374DDN.mjs → chunk-KL7FKGUB.mjs} +2 -2
  9. package/dist/{chunk-6RRG7ME3.mjs → chunk-WR5WT6YJ.mjs} +4 -4
  10. package/dist/chunk-WR5WT6YJ.mjs.map +1 -0
  11. package/dist/{chunk-UNFDVVCF.mjs → chunk-XMY4ZXI7.mjs} +3 -3
  12. package/dist/chunk-XMY4ZXI7.mjs.map +1 -0
  13. package/dist/components/copilot-provider.mjs +2 -2
  14. package/dist/components/index.mjs +2 -2
  15. package/dist/context/copilot-context.d.ts +1 -1
  16. package/dist/context/copilot-context.mjs +1 -1
  17. package/dist/context/index.mjs +1 -1
  18. package/dist/hooks/index.mjs +5 -5
  19. package/dist/hooks/use-copilot-chat.mjs +2 -2
  20. package/dist/hooks/use-make-copilot-actionable.mjs +2 -2
  21. package/dist/hooks/use-make-copilot-document-readable.d.ts +1 -1
  22. package/dist/hooks/use-make-copilot-document-readable.mjs +2 -2
  23. package/dist/hooks/use-make-copilot-readable.mjs +2 -2
  24. package/dist/index.mjs +6 -6
  25. package/dist/types/document-pointer.d.ts +1 -1
  26. package/package.json +1 -1
  27. package/src/components/copilot-provider.tsx +12 -2
  28. package/src/context/copilot-context.tsx +5 -2
  29. package/src/hooks/use-copilot-chat.ts +1 -1
  30. package/src/hooks/use-make-copilot-document-readable.ts +3 -2
  31. package/src/types/document-pointer.ts +1 -1
  32. package/dist/chunk-6RRG7ME3.mjs.map +0 -1
  33. package/dist/chunk-K7TVQCXG.mjs.map +0 -1
  34. package/dist/chunk-Q6E6JSLL.mjs.map +0 -1
  35. package/dist/chunk-UNFDVVCF.mjs.map +0 -1
  36. /package/dist/{chunk-YTIZQV7H.mjs.map → chunk-HRUQ2NR4.mjs.map} +0 -0
  37. /package/dist/{chunk-3O374DDN.mjs.map → chunk-KL7FKGUB.mjs.map} +0 -0
@@ -1,5 +1,5 @@
1
1
 
2
- > @copilotkit/react-core@0.9.0 build /home/runner/work/CopilotKit/CopilotKit/CopilotKit/packages/react-core
2
+ > @copilotkit/react-core@0.10.0 build /home/runner/work/CopilotKit/CopilotKit/CopilotKit/packages/react-core
3
3
  > tsup --treeshake
4
4
 
5
5
  CLI Building entry: src/index.tsx, src/components/copilot-provider.tsx, src/components/index.ts, src/context/copilot-context.tsx, src/context/index.ts, src/hooks/index.ts, src/hooks/use-copilot-chat.ts, src/hooks/use-flat-category-store.ts, src/hooks/use-make-copilot-actionable.ts, src/hooks/use-make-copilot-document-readable.ts, src/hooks/use-make-copilot-readable.ts, src/hooks/use-tree.ts, src/types/annotated-function.ts, src/types/document-pointer.ts, src/types/index.ts, src/utils/utils.test.ts, src/utils/utils.ts
@@ -30,16 +30,16 @@ Generated an empty chunk: "chunk-QACD2U6P".
30
30
  ESM dist/hooks/use-tree.mjs 170.00 B
31
31
  ESM dist/index.mjs 678.00 B
32
32
  ESM dist/components/index.mjs 283.00 B
33
- ESM dist/chunk-Q6E6JSLL.mjs 4.47 KB
33
+ ESM dist/chunk-F4BQVCQY.mjs 4.73 KB
34
34
  ESM dist/chunk-3BOHSSKR.mjs 3.95 KB
35
35
  ESM dist/context/index.mjs 216.00 B
36
36
  ESM dist/hooks/index.mjs 420.00 B
37
- ESM dist/chunk-YTIZQV7H.mjs 972.00 B
38
- ESM dist/chunk-6RRG7ME3.mjs 663.00 B
39
- ESM dist/chunk-3O374DDN.mjs 626.00 B
37
+ ESM dist/chunk-HRUQ2NR4.mjs 972.00 B
38
+ ESM dist/chunk-WR5WT6YJ.mjs 689.00 B
39
+ ESM dist/chunk-KL7FKGUB.mjs 626.00 B
40
40
  ESM dist/hooks/use-copilot-chat.mjs 219.00 B
41
- ESM dist/chunk-UNFDVVCF.mjs 2.62 KB
42
- ESM dist/chunk-K7TVQCXG.mjs 1.21 KB
41
+ ESM dist/chunk-XMY4ZXI7.mjs 2.62 KB
42
+ ESM dist/chunk-GGSWSZ2C.mjs 1.21 KB
43
43
  ESM dist/hooks/use-flat-category-store.mjs 200.00 B
44
44
  ESM dist/chunk-63ILXW5V.mjs 1.92 KB
45
45
  ESM dist/utils/utils.mjs 67.00 B
@@ -60,34 +60,34 @@ Generated an empty chunk: "chunk-QACD2U6P".
60
60
  ESM dist/hooks/use-tree.mjs.map 51.00 B
61
61
  ESM dist/index.mjs.map 51.00 B
62
62
  ESM dist/components/index.mjs.map 51.00 B
63
- ESM dist/chunk-Q6E6JSLL.mjs.map 8.75 KB
63
+ ESM dist/chunk-F4BQVCQY.mjs.map 9.26 KB
64
64
  ESM dist/chunk-3BOHSSKR.mjs.map 8.39 KB
65
65
  ESM dist/context/index.mjs.map 51.00 B
66
66
  ESM dist/hooks/index.mjs.map 51.00 B
67
- ESM dist/chunk-YTIZQV7H.mjs.map 1.70 KB
68
- ESM dist/chunk-6RRG7ME3.mjs.map 1.56 KB
69
- ESM dist/chunk-3O374DDN.mjs.map 1.47 KB
67
+ ESM dist/chunk-HRUQ2NR4.mjs.map 1.70 KB
68
+ ESM dist/chunk-WR5WT6YJ.mjs.map 1.61 KB
69
+ ESM dist/chunk-KL7FKGUB.mjs.map 1.47 KB
70
70
  ESM dist/hooks/use-copilot-chat.mjs.map 51.00 B
71
- ESM dist/chunk-UNFDVVCF.mjs.map 4.48 KB
72
- ESM dist/chunk-K7TVQCXG.mjs.map 3.38 KB
71
+ ESM dist/chunk-XMY4ZXI7.mjs.map 4.49 KB
72
+ ESM dist/chunk-GGSWSZ2C.mjs.map 3.46 KB
73
73
  ESM dist/hooks/use-flat-category-store.mjs.map 51.00 B
74
74
  ESM dist/chunk-63ILXW5V.mjs.map 4.31 KB
75
75
  ESM dist/utils/utils.mjs.map 51.00 B
76
76
  ESM dist/types/annotated-function.mjs.map 51.00 B
77
77
  ESM dist/types/document-pointer.mjs.map 51.00 B
78
78
  ESM dist/utils/utils.test.mjs.map 351.00 B
79
- ESM ⚡️ Build success in 491ms
79
+ ESM ⚡️ Build success in 281ms
80
80
  DTS Build start
81
- DTS ⚡️ Build success in 2330ms
81
+ DTS ⚡️ Build success in 1570ms
82
82
  DTS dist/index.d.ts 814.00 B
83
83
  DTS dist/components/copilot-provider.d.ts 203.00 B
84
- DTS dist/context/copilot-context.d.ts 1.45 KB
84
+ DTS dist/context/copilot-context.d.ts 1.47 KB
85
85
  DTS dist/hooks/use-copilot-chat.d.ts 890.00 B
86
86
  DTS dist/hooks/use-make-copilot-actionable.d.ts 255.00 B
87
87
  DTS dist/hooks/use-make-copilot-readable.d.ts 598.00 B
88
- DTS dist/hooks/use-make-copilot-document-readable.d.ts 666.00 B
88
+ DTS dist/hooks/use-make-copilot-document-readable.d.ts 688.00 B
89
89
  DTS dist/types/annotated-function.d.ts 767.00 B
90
- DTS dist/types/document-pointer.d.ts 190.00 B
90
+ DTS dist/types/document-pointer.d.ts 181.00 B
91
91
  DTS dist/hooks/use-tree.d.ts 543.00 B
92
92
  DTS dist/components/index.d.ts 73.00 B
93
93
  DTS dist/context/index.d.ts 297.00 B
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # ui
2
2
 
3
+ ## 0.10.0
4
+
5
+ ### Minor Changes
6
+
7
+ - document contents funneled to prompt context
8
+
3
9
  ## 0.9.0
4
10
 
5
11
  ### Minor Changes
@@ -1,5 +1,5 @@
1
1
  import { use_tree_default } from './chunk-3BOHSSKR.mjs';
2
- import { CopilotContext } from './chunk-K7TVQCXG.mjs';
2
+ import { CopilotContext } from './chunk-GGSWSZ2C.mjs';
3
3
  import { use_flat_category_store_default } from './chunk-63ILXW5V.mjs';
4
4
  import { __spreadProps, __spreadValues, __async, __objRest } from './chunk-MRXNTQOX.mjs';
5
5
  import { useState, useCallback } from 'react';
@@ -34,8 +34,15 @@ function CopilotProvider({
34
34
  });
35
35
  }, []);
36
36
  const getContextString = useCallback(
37
- (categories = ["global"]) => {
38
- return printTree(categories);
37
+ (documents, categories = ["global"]) => {
38
+ const documentsString = documents.map((document) => {
39
+ return `${document.name} (${document.sourceApplication}):
40
+ ${document.getContents()}`;
41
+ }).join("\n\n");
42
+ const nonDocumentStrings = printTree(categories);
43
+ return `${documentsString}
44
+
45
+ ${nonDocumentStrings}`;
39
46
  },
40
47
  [printTree]
41
48
  );
@@ -146,4 +153,4 @@ function annotatedFunctionToChatCompletionFunction(annotatedFunction) {
146
153
 
147
154
  export { CopilotProvider };
148
155
  //# sourceMappingURL=out.js.map
149
- //# sourceMappingURL=chunk-Q6E6JSLL.mjs.map
156
+ //# sourceMappingURL=chunk-F4BQVCQY.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/components/copilot-provider.tsx"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAEA,SAAoB,aAAa,gBAAgB;AA2G7C;AA7GJ;AAUO,SAAS,gBAAgB;AAAA,EAC9B;AAAA,EACA;AACF,GAGgB;AACd,QAAM,CAAC,aAAa,cAAc,IAAI,SAEpC,CAAC,CAAC;AAEJ,QAAM,EAAE,YAAY,eAAe,UAAU,IAAI,iBAAQ;AACzD,QAAM;AAAA,IACJ,YAAY;AAAA,IACZ,eAAe;AAAA,IACf,aAAa;AAAA,EACf,IAAI,gCAAsC;AAE1C,QAAM,gBAAgB;AAAA,IACpB,CAAC,IAAY,eAAyC;AACpD,qBAAe,CAAC,eAAe;AAC7B,eAAO,iCACF,aADE;AAAA,UAEL,CAAC,KAAK;AAAA,QACR;AAAA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B;AAEA,MAAI,yBAAmC,CAAC;AACxC,WAAS,OAAO,kBAAkB,qBAAqB;AACrD,QAAI,IAAI,UAAU;AAChB,6BAAuB,KAAK,IAAI,IAAI;AAAA,IACtC;AAAA,EACF;AAGA,MAAI,yBAA8D;AAAA,IAChE,MAAM,kBAAkB;AAAA,IACxB,aAAa,kBAAkB;AAAA,IAC/B,YAAY;AAAA,MACV,MAAM;AAAA,MACN,YAAY;AAAA,MACZ,UAAU;AAAA,IACZ;AAAA,EACF;AAEA,SAAO;AACT","sourcesContent":["\"use client\";\nimport { FunctionCallHandler } from \"ai\";\nimport { 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\";\nimport { DocumentPointer } from \"../types\";\nimport useFlatCategoryStore from \"../hooks/use-flat-category-store\";\n\nexport function CopilotProvider({\n chatApiEndpoint,\n children,\n}: {\n chatApiEndpoint: string;\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 const {\n addElement: addDocument,\n removeElement: removeDocument,\n allElements: allDocuments,\n } = useFlatCategoryStore<DocumentPointer>();\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 (documents: DocumentPointer[], categories: string[] = [\"global\"]) => {\n const documentsString = documents\n .map((document) => {\n return `${document.name} (${\n document.sourceApplication\n }):\\n${document.getContents()}`;\n })\n .join(\"\\n\\n\");\n\n const nonDocumentStrings = printTree(categories);\n\n return `${documentsString}\\n\\n${nonDocumentStrings}`;\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 const getDocumentsContext = useCallback(\n (categories: string[] = [\"global\"]) => {\n return allDocuments(categories);\n },\n [allDocuments]\n );\n\n const addDocumentContext = useCallback(\n (documentPointer: DocumentPointer, categories: string[] = [\"global\"]) => {\n return addDocument(documentPointer, categories);\n },\n [addDocument]\n );\n\n const removeDocumentContext = useCallback(\n (documentId: string) => {\n removeDocument(documentId);\n },\n [removeDocument]\n );\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 getDocumentsContext,\n addDocumentContext,\n removeDocumentContext,\n copilotApiConfig: {\n chatApiEndpoint,\n },\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"]}
@@ -16,7 +16,7 @@ var emptyCopilotContext = {
16
16
  },
17
17
  getChatCompletionFunctionDescriptions: () => returnAndThrowInDebug(),
18
18
  getFunctionCallHandler: () => returnAndThrowInDebug(),
19
- getContextString: () => returnAndThrowInDebug(),
19
+ getContextString: (documents) => returnAndThrowInDebug(),
20
20
  addContext: () => "",
21
21
  removeContext: () => {
22
22
  },
@@ -41,4 +41,4 @@ function returnAndThrowInDebug(value) {
41
41
 
42
42
  export { CopilotContext, copilotApiConfigExtrapolator };
43
43
  //# sourceMappingURL=out.js.map
44
- //# sourceMappingURL=chunk-K7TVQCXG.mjs.map
44
+ //# sourceMappingURL=chunk-GGSWSZ2C.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/context/copilot-context.tsx"],"names":[],"mappings":";;;;;AAGA,OAAO,WAAW;AAHlB;AAaO,SAAS,6BAA6B,QAA0B;AACrE,SAAO;AAAA,IACL,IAAI,kBAA0B;AAC5B,aAAO,GAAG,OAAO;AAAA,IACnB;AAAA,EACF;AACF;AAkCA,IAAM,sBAA4C;AAAA,EAChD,aAAa,CAAC;AAAA,EACd,eAAe,MAAM;AAAA,EAAC;AAAA,EACtB,kBAAkB,MAAM;AAAA,EAAC;AAAA,EACzB,uCAAuC,MAAM,sBAAsB,CAAC,CAAC;AAAA,EACrE,wBAAwB,MAAM,sBAAsB,MAAY;AAAA,EAAC,EAAC;AAAA,EAElE,kBAAkB,CAAC,cAAiC,sBAAsB,EAAE;AAAA,EAC5E,YAAY,MAAM;AAAA,EAClB,eAAe,MAAM;AAAA,EAAC;AAAA,EAEtB,qBAAqB,MAAM,sBAAsB,CAAC,CAAC;AAAA,EACnD,oBAAoB,MAAM,sBAAsB,EAAE;AAAA,EAClD,uBAAuB,MAAM;AAAA,EAAC;AAAA,EAE9B,kBAAkB,IAAK,MAAkC;AAAA,IACvD,IAAI,kBAA0B;AAC5B,YAAM,IAAI;AAAA,QACR;AAAA,MACF;AAAA,IACF;AAAA,EACF,EAAG;AACL;AAEO,IAAM,iBACX,MAAM,cAAoC,mBAAmB;AAE/D,SAAS,sBAAyB,OAAa;AAC7C,QAAM,IAAI;AAAA,IACR;AAAA,EACF;AACA,SAAO;AACT","sourcesContent":["\"use client\";\n\nimport { FunctionCallHandler } from \"ai\";\nimport React from \"react\";\nimport { TreeNodeId } from \"../hooks/use-tree\";\nimport { AnnotatedFunction } from \"../types/annotated-function\";\nimport { ChatCompletionCreateParams } from \"openai/resources/chat\";\nimport { DocumentPointer } from \"../types\";\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: (\n documents: DocumentPointer[],\n categories?: string[]\n ) => string;\n addContext: (\n context: string,\n parentId?: string,\n categories?: string[]\n ) => TreeNodeId;\n removeContext: (id: TreeNodeId) => void;\n\n // document context\n getDocumentsContext: (categories?: string[]) => DocumentPointer[];\n addDocumentContext: (\n documentPointer: DocumentPointer,\n categories?: string[]\n ) => TreeNodeId;\n removeDocumentContext: (documentId: string) => 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: (documents: DocumentPointer[]) => returnAndThrowInDebug(\"\"),\n addContext: () => \"\",\n removeContext: () => {},\n\n getDocumentsContext: () => returnAndThrowInDebug([]),\n addDocumentContext: () => returnAndThrowInDebug(\"\"),\n removeDocumentContext: () => {},\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"]}
@@ -1,4 +1,4 @@
1
- import { CopilotContext } from './chunk-K7TVQCXG.mjs';
1
+ import { CopilotContext } from './chunk-GGSWSZ2C.mjs';
2
2
  import { useRef, useContext, useMemo, useEffect } from 'react';
3
3
  import { nanoid } from 'nanoid';
4
4
 
@@ -27,4 +27,4 @@ function useMakeCopilotActionable(annotatedFunction, dependencies) {
27
27
 
28
28
  export { useMakeCopilotActionable };
29
29
  //# sourceMappingURL=out.js.map
30
- //# sourceMappingURL=chunk-YTIZQV7H.mjs.map
30
+ //# sourceMappingURL=chunk-HRUQ2NR4.mjs.map
@@ -1,4 +1,4 @@
1
- import { CopilotContext } from './chunk-K7TVQCXG.mjs';
1
+ import { CopilotContext } from './chunk-GGSWSZ2C.mjs';
2
2
  import { useContext, useRef, useEffect } from 'react';
3
3
 
4
4
  function useMakeCopilotReadable(information, parentId, categories) {
@@ -16,4 +16,4 @@ function useMakeCopilotReadable(information, parentId, categories) {
16
16
 
17
17
  export { useMakeCopilotReadable };
18
18
  //# sourceMappingURL=out.js.map
19
- //# sourceMappingURL=chunk-3O374DDN.mjs.map
19
+ //# sourceMappingURL=chunk-KL7FKGUB.mjs.map
@@ -1,7 +1,7 @@
1
- import { CopilotContext } from './chunk-K7TVQCXG.mjs';
1
+ import { CopilotContext } from './chunk-GGSWSZ2C.mjs';
2
2
  import { useContext, useRef, useEffect } from 'react';
3
3
 
4
- function useMakeCopilotDocumentReadable(document, categories) {
4
+ function useMakeCopilotDocumentReadable(document, categories, dependencies = []) {
5
5
  const { addDocumentContext, removeDocumentContext } = useContext(CopilotContext);
6
6
  const idRef = useRef();
7
7
  useEffect(() => {
@@ -10,10 +10,10 @@ function useMakeCopilotDocumentReadable(document, categories) {
10
10
  return () => {
11
11
  removeDocumentContext(id);
12
12
  };
13
- }, [document, categories, addDocumentContext, removeDocumentContext]);
13
+ }, [categories, addDocumentContext, removeDocumentContext, ...dependencies]);
14
14
  return idRef.current;
15
15
  }
16
16
 
17
17
  export { useMakeCopilotDocumentReadable };
18
18
  //# sourceMappingURL=out.js.map
19
- //# sourceMappingURL=chunk-6RRG7ME3.mjs.map
19
+ //# sourceMappingURL=chunk-WR5WT6YJ.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/hooks/use-make-copilot-document-readable.ts"],"names":[],"mappings":";;;;;AAEA,SAAS,YAAY,WAAW,cAAc;AAF9C;AAaO,SAAS,+BACd,UACA,YACA,eAAsB,CAAC,GACH;AACpB,QAAM,EAAE,oBAAoB,sBAAsB,IAChD,WAAW,cAAc;AAC3B,QAAM,QAAQ,OAAe;AAE7B,YAAU,MAAM;AACd,UAAM,KAAK,mBAAmB,UAAU,UAAU;AAClD,UAAM,UAAU;AAEhB,WAAO,MAAM;AACX,4BAAsB,EAAE;AAAA,IAC1B;AAAA,EACF,GAAG,CAAC,YAAY,oBAAoB,uBAAuB,GAAG,YAAY,CAAC;AAE3E,SAAO,MAAM;AACf","sourcesContent":["\"use client\";\n\nimport { useContext, useEffect, useRef } from \"react\";\nimport { CopilotContext } from \"../context/copilot-context\";\nimport { DocumentPointer } from \"../types\";\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 useMakeCopilotDocumentReadable(\n document: DocumentPointer,\n categories?: string[],\n dependencies: any[] = []\n): string | undefined {\n const { addDocumentContext, removeDocumentContext } =\n useContext(CopilotContext);\n const idRef = useRef<string>();\n\n useEffect(() => {\n const id = addDocumentContext(document, categories);\n idRef.current = id;\n\n return () => {\n removeDocumentContext(id);\n };\n }, [categories, addDocumentContext, removeDocumentContext, ...dependencies]);\n\n return idRef.current;\n}\n"]}
@@ -1,4 +1,4 @@
1
- import { CopilotContext, copilotApiConfigExtrapolator } from './chunk-K7TVQCXG.mjs';
1
+ import { CopilotContext, copilotApiConfigExtrapolator } from './chunk-GGSWSZ2C.mjs';
2
2
  import { __objRest } from './chunk-MRXNTQOX.mjs';
3
3
  import { useContext, useMemo } from 'react';
4
4
  import { useChat } from 'ai/react';
@@ -17,7 +17,7 @@ function useCopilotChat(_a) {
17
17
  } = useContext(CopilotContext);
18
18
  const systemMessage = useMemo(() => {
19
19
  const systemMessageMaker = makeSystemMessage || defaultSystemMessage;
20
- const contextString = getContextString();
20
+ const contextString = getContextString([]);
21
21
  return {
22
22
  id: "system",
23
23
  content: systemMessageMaker(contextString),
@@ -77,4 +77,4 @@ If you would like to call a function, call it without saying anything else.
77
77
 
78
78
  export { defaultSystemMessage, useCopilotChat };
79
79
  //# sourceMappingURL=out.js.map
80
- //# sourceMappingURL=chunk-UNFDVVCF.mjs.map
80
+ //# sourceMappingURL=chunk-XMY4ZXI7.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/hooks/use-copilot-chat.ts"],"names":[],"mappings":";;;;;;;;;AAAA,SAAS,SAAS,kBAAkB;AAMpC,SAAS,eAAe;AAuBjB,SAAS,eAAe,IAGiB;AAHjB,eAC7B;AAAA;AAAA,EA9BF,IA6B+B,IAE1B,oBAF0B,IAE1B;AAAA,IADH;AAAA;AAGA,QAAM;AAAA,IACJ;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,IAAI,WAAW,cAAc;AAE7B,QAAM,gBAAyB,QAAQ,MAAM;AAC3C,UAAM,qBAAqB,qBAAqB;AAChD,UAAM,gBAAgB,iBAAiB,CAAC,CAAC;AAEzC,WAAO;AAAA,MACL,IAAI;AAAA,MACJ,SAAS,mBAAmB,aAAa;AAAA,MACzC,MAAM;AAAA,IACR;AAAA,EACF,GAAG,CAAC,kBAAkB,iBAAiB,CAAC;AAExC,QAAM,6BAA6B,CAAC,aAAa,EAAE;AAAA,IACjD,QAAQ,mBAAmB,CAAC;AAAA,EAC9B;AAEA,QAAM,uBAAuB,QAAQ,MAAM;AACzC,WAAO,sCAAsC;AAAA,EAC/C,GAAG,CAAC,qCAAqC,CAAC;AAE1C,QAAM,EAAE,UAAU,QAAQ,QAAQ,MAAM,WAAW,OAAO,SAAS,IACjE,QAAQ;AAAA,IACN,KAAK,6BAA6B,gBAAgB,EAAE;AAAA,IACpD,IAAI,QAAQ;AAAA,IACZ,iBAAiB;AAAA,IACjB,6BAA6B,uBAAuB;AAAA,IACpD,MAAM;AAAA,MACJ,IAAI,QAAQ;AAAA,MACZ,WAAW;AAAA,IACb;AAAA,EACF,CAAC;AAEH,QAAM,kBAAkB,SAAS;AAAA,IAC/B,CAAC,YAAY,QAAQ,SAAS,UAAU,QAAQ,SAAS;AAAA,EAC3D;AAEA,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;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","sourcesContent":["import { useMemo, useContext } from \"react\";\nimport {\n CopilotContext,\n CopilotContextParams,\n copilotApiConfigExtrapolator,\n} from \"../context/copilot-context\";\nimport { useChat } from \"ai/react\";\nimport { ChatRequestOptions, CreateMessage, Message } from \"ai\";\nimport { UseChatOptions } from \"ai\";\n\nexport interface UseCopilotChatOptions extends UseChatOptions {\n makeSystemMessage?: (contextString: string) => string;\n}\n\nexport interface UseCopilotChatReturn {\n visibleMessages: Message[];\n append: (\n message: Message | CreateMessage,\n chatRequestOptions?: ChatRequestOptions\n ) => Promise<string | null | undefined>;\n reload: (\n chatRequestOptions?: ChatRequestOptions\n ) => Promise<string | null | undefined>;\n stop: () => void;\n isLoading: boolean;\n input: string;\n setInput: React.Dispatch<React.SetStateAction<string>>;\n}\n\nexport function useCopilotChat({\n makeSystemMessage,\n ...options\n}: UseCopilotChatOptions): UseCopilotChatReturn {\n const {\n getContextString,\n getChatCompletionFunctionDescriptions,\n getFunctionCallHandler,\n copilotApiConfig,\n } = useContext(CopilotContext);\n\n const systemMessage: Message = useMemo(() => {\n const systemMessageMaker = makeSystemMessage || defaultSystemMessage;\n const contextString = getContextString([]);\n\n return {\n id: \"system\",\n content: systemMessageMaker(contextString),\n role: \"system\",\n };\n }, [getContextString, makeSystemMessage]);\n\n const initialMessagesWithContext = [systemMessage].concat(\n options.initialMessages || []\n );\n\n const functionDescriptions = useMemo(() => {\n return getChatCompletionFunctionDescriptions();\n }, [getChatCompletionFunctionDescriptions]);\n\n const { messages, append, reload, stop, isLoading, input, setInput } =\n useChat({\n api: copilotApiConfigExtrapolator(copilotApiConfig).chatApiEndpoint,\n id: options.id,\n initialMessages: initialMessagesWithContext,\n experimental_onFunctionCall: getFunctionCallHandler(),\n body: {\n id: options.id,\n 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"]}
@@ -1,6 +1,6 @@
1
- export { CopilotProvider } from '../chunk-Q6E6JSLL.mjs';
1
+ export { CopilotProvider } from '../chunk-F4BQVCQY.mjs';
2
2
  import '../chunk-3BOHSSKR.mjs';
3
- import '../chunk-K7TVQCXG.mjs';
3
+ import '../chunk-GGSWSZ2C.mjs';
4
4
  import '../chunk-63ILXW5V.mjs';
5
5
  import '../chunk-MRXNTQOX.mjs';
6
6
  //# sourceMappingURL=out.js.map
@@ -1,7 +1,7 @@
1
1
  import '../chunk-JD7BAH7U.mjs';
2
- export { CopilotProvider } from '../chunk-Q6E6JSLL.mjs';
2
+ export { CopilotProvider } from '../chunk-F4BQVCQY.mjs';
3
3
  import '../chunk-3BOHSSKR.mjs';
4
- import '../chunk-K7TVQCXG.mjs';
4
+ import '../chunk-GGSWSZ2C.mjs';
5
5
  import '../chunk-63ILXW5V.mjs';
6
6
  import '../chunk-MRXNTQOX.mjs';
7
7
  //# sourceMappingURL=out.js.map
@@ -17,7 +17,7 @@ interface CopilotContextParams {
17
17
  removeEntryPoint: (id: string) => void;
18
18
  getChatCompletionFunctionDescriptions: () => ChatCompletionCreateParams.Function[];
19
19
  getFunctionCallHandler: () => FunctionCallHandler;
20
- getContextString: (categories?: string[]) => string;
20
+ getContextString: (documents: DocumentPointer[], categories?: string[]) => string;
21
21
  addContext: (context: string, parentId?: string, categories?: string[]) => TreeNodeId;
22
22
  removeContext: (id: TreeNodeId) => void;
23
23
  getDocumentsContext: (categories?: string[]) => DocumentPointer[];
@@ -1,4 +1,4 @@
1
- export { CopilotContext, copilotApiConfigExtrapolator } from '../chunk-K7TVQCXG.mjs';
1
+ export { CopilotContext, copilotApiConfigExtrapolator } from '../chunk-GGSWSZ2C.mjs';
2
2
  import '../chunk-MRXNTQOX.mjs';
3
3
  //# sourceMappingURL=out.js.map
4
4
  //# sourceMappingURL=copilot-context.mjs.map
@@ -1,5 +1,5 @@
1
1
  import '../chunk-YPSGKPDA.mjs';
2
- export { CopilotContext, copilotApiConfigExtrapolator } from '../chunk-K7TVQCXG.mjs';
2
+ export { CopilotContext, copilotApiConfigExtrapolator } from '../chunk-GGSWSZ2C.mjs';
3
3
  import '../chunk-MRXNTQOX.mjs';
4
4
  //# sourceMappingURL=out.js.map
5
5
  //# sourceMappingURL=index.mjs.map
@@ -1,9 +1,9 @@
1
1
  import '../chunk-QACD2U6P.mjs';
2
- export { useMakeCopilotActionable } from '../chunk-YTIZQV7H.mjs';
3
- export { useMakeCopilotDocumentReadable } from '../chunk-6RRG7ME3.mjs';
4
- export { useMakeCopilotReadable } from '../chunk-3O374DDN.mjs';
5
- export { useCopilotChat } from '../chunk-UNFDVVCF.mjs';
6
- import '../chunk-K7TVQCXG.mjs';
2
+ export { useMakeCopilotActionable } from '../chunk-HRUQ2NR4.mjs';
3
+ export { useMakeCopilotDocumentReadable } from '../chunk-WR5WT6YJ.mjs';
4
+ export { useMakeCopilotReadable } from '../chunk-KL7FKGUB.mjs';
5
+ export { useCopilotChat } from '../chunk-XMY4ZXI7.mjs';
6
+ import '../chunk-GGSWSZ2C.mjs';
7
7
  import '../chunk-MRXNTQOX.mjs';
8
8
  //# sourceMappingURL=out.js.map
9
9
  //# sourceMappingURL=index.mjs.map
@@ -1,5 +1,5 @@
1
- export { defaultSystemMessage, useCopilotChat } from '../chunk-UNFDVVCF.mjs';
2
- import '../chunk-K7TVQCXG.mjs';
1
+ export { defaultSystemMessage, useCopilotChat } from '../chunk-XMY4ZXI7.mjs';
2
+ import '../chunk-GGSWSZ2C.mjs';
3
3
  import '../chunk-MRXNTQOX.mjs';
4
4
  //# sourceMappingURL=out.js.map
5
5
  //# sourceMappingURL=use-copilot-chat.mjs.map
@@ -1,5 +1,5 @@
1
- export { useMakeCopilotActionable } from '../chunk-YTIZQV7H.mjs';
2
- import '../chunk-K7TVQCXG.mjs';
1
+ export { useMakeCopilotActionable } from '../chunk-HRUQ2NR4.mjs';
2
+ import '../chunk-GGSWSZ2C.mjs';
3
3
  import '../chunk-MRXNTQOX.mjs';
4
4
  //# sourceMappingURL=out.js.map
5
5
  //# sourceMappingURL=use-make-copilot-actionable.mjs.map
@@ -7,6 +7,6 @@ import { DocumentPointer } from '../types/document-pointer.js';
7
7
  * @param categories - An array of categories to control which context are visible where. Particularly useful with CopilotTextarea (see `useMakeAutosuggestionFunction`)
8
8
  * @returns The ID of the added context.
9
9
  */
10
- declare function useMakeCopilotDocumentReadable(document: DocumentPointer, categories?: string[]): string | undefined;
10
+ declare function useMakeCopilotDocumentReadable(document: DocumentPointer, categories?: string[], dependencies?: any[]): string | undefined;
11
11
 
12
12
  export { useMakeCopilotDocumentReadable };
@@ -1,5 +1,5 @@
1
- export { useMakeCopilotDocumentReadable } from '../chunk-6RRG7ME3.mjs';
2
- import '../chunk-K7TVQCXG.mjs';
1
+ export { useMakeCopilotDocumentReadable } from '../chunk-WR5WT6YJ.mjs';
2
+ import '../chunk-GGSWSZ2C.mjs';
3
3
  import '../chunk-MRXNTQOX.mjs';
4
4
  //# sourceMappingURL=out.js.map
5
5
  //# sourceMappingURL=use-make-copilot-document-readable.mjs.map
@@ -1,5 +1,5 @@
1
- export { useMakeCopilotReadable } from '../chunk-3O374DDN.mjs';
2
- import '../chunk-K7TVQCXG.mjs';
1
+ export { useMakeCopilotReadable } from '../chunk-KL7FKGUB.mjs';
2
+ import '../chunk-GGSWSZ2C.mjs';
3
3
  import '../chunk-MRXNTQOX.mjs';
4
4
  //# sourceMappingURL=out.js.map
5
5
  //# sourceMappingURL=use-make-copilot-readable.mjs.map
package/dist/index.mjs CHANGED
@@ -1,14 +1,14 @@
1
1
  import './chunk-EFZPSZWO.mjs';
2
2
  import './chunk-JD7BAH7U.mjs';
3
- export { CopilotProvider } from './chunk-Q6E6JSLL.mjs';
3
+ export { CopilotProvider } from './chunk-F4BQVCQY.mjs';
4
4
  import './chunk-3BOHSSKR.mjs';
5
5
  import './chunk-YPSGKPDA.mjs';
6
6
  import './chunk-QACD2U6P.mjs';
7
- export { useMakeCopilotActionable } from './chunk-YTIZQV7H.mjs';
8
- export { useMakeCopilotDocumentReadable } from './chunk-6RRG7ME3.mjs';
9
- export { useMakeCopilotReadable } from './chunk-3O374DDN.mjs';
10
- export { useCopilotChat } from './chunk-UNFDVVCF.mjs';
11
- export { CopilotContext, copilotApiConfigExtrapolator } from './chunk-K7TVQCXG.mjs';
7
+ export { useMakeCopilotActionable } from './chunk-HRUQ2NR4.mjs';
8
+ export { useMakeCopilotDocumentReadable } from './chunk-WR5WT6YJ.mjs';
9
+ export { useMakeCopilotReadable } from './chunk-KL7FKGUB.mjs';
10
+ export { useCopilotChat } from './chunk-XMY4ZXI7.mjs';
11
+ export { CopilotContext, copilotApiConfigExtrapolator } from './chunk-GGSWSZ2C.mjs';
12
12
  import './chunk-63ILXW5V.mjs';
13
13
  import './chunk-MRXNTQOX.mjs';
14
14
  //# sourceMappingURL=out.js.map
@@ -3,7 +3,7 @@ interface DocumentPointer {
3
3
  name: string;
4
4
  sourceApplication: string;
5
5
  iconImageUri: string;
6
- getContents: () => Promise<string>;
6
+ getContents: () => string;
7
7
  }
8
8
 
9
9
  export { DocumentPointer };
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
- "version": "0.9.0",
7
+ "version": "0.10.0",
8
8
  "sideEffects": false,
9
9
  "main": "./dist/index.js",
10
10
  "module": "./dist/index.mjs",
@@ -47,8 +47,18 @@ export function CopilotProvider({
47
47
  }, []);
48
48
 
49
49
  const getContextString = useCallback(
50
- (categories: string[] = ["global"]) => {
51
- return printTree(categories);
50
+ (documents: DocumentPointer[], categories: string[] = ["global"]) => {
51
+ const documentsString = documents
52
+ .map((document) => {
53
+ return `${document.name} (${
54
+ document.sourceApplication
55
+ }):\n${document.getContents()}`;
56
+ })
57
+ .join("\n\n");
58
+
59
+ const nonDocumentStrings = printTree(categories);
60
+
61
+ return `${documentsString}\n\n${nonDocumentStrings}`;
52
62
  },
53
63
  [printTree]
54
64
  );
@@ -28,7 +28,10 @@ export interface CopilotContextParams {
28
28
  getFunctionCallHandler: () => FunctionCallHandler;
29
29
 
30
30
  // text context
31
- getContextString: (categories?: string[]) => string;
31
+ getContextString: (
32
+ documents: DocumentPointer[],
33
+ categories?: string[]
34
+ ) => string;
32
35
  addContext: (
33
36
  context: string,
34
37
  parentId?: string,
@@ -55,7 +58,7 @@ const emptyCopilotContext: CopilotContextParams = {
55
58
  getChatCompletionFunctionDescriptions: () => returnAndThrowInDebug([]),
56
59
  getFunctionCallHandler: () => returnAndThrowInDebug(async () => {}),
57
60
 
58
- getContextString: () => returnAndThrowInDebug(""),
61
+ getContextString: (documents: DocumentPointer[]) => returnAndThrowInDebug(""),
59
62
  addContext: () => "",
60
63
  removeContext: () => {},
61
64
 
@@ -40,7 +40,7 @@ export function useCopilotChat({
40
40
 
41
41
  const systemMessage: Message = useMemo(() => {
42
42
  const systemMessageMaker = makeSystemMessage || defaultSystemMessage;
43
- const contextString = getContextString();
43
+ const contextString = getContextString([]);
44
44
 
45
45
  return {
46
46
  id: "system",
@@ -13,7 +13,8 @@ import { DocumentPointer } from "../types";
13
13
  */
14
14
  export function useMakeCopilotDocumentReadable(
15
15
  document: DocumentPointer,
16
- categories?: string[]
16
+ categories?: string[],
17
+ dependencies: any[] = []
17
18
  ): string | undefined {
18
19
  const { addDocumentContext, removeDocumentContext } =
19
20
  useContext(CopilotContext);
@@ -26,7 +27,7 @@ export function useMakeCopilotDocumentReadable(
26
27
  return () => {
27
28
  removeDocumentContext(id);
28
29
  };
29
- }, [document, categories, addDocumentContext, removeDocumentContext]);
30
+ }, [categories, addDocumentContext, removeDocumentContext, ...dependencies]);
30
31
 
31
32
  return idRef.current;
32
33
  }
@@ -3,5 +3,5 @@ export interface DocumentPointer {
3
3
  name: string;
4
4
  sourceApplication: string;
5
5
  iconImageUri: string;
6
- getContents: () => Promise<string>;
6
+ getContents: () => string;
7
7
  }
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/hooks/use-make-copilot-document-readable.ts"],"names":[],"mappings":";;;;;AAEA,SAAS,YAAY,WAAW,cAAc;AAF9C;AAaO,SAAS,+BACd,UACA,YACoB;AACpB,QAAM,EAAE,oBAAoB,sBAAsB,IAChD,WAAW,cAAc;AAC3B,QAAM,QAAQ,OAAe;AAE7B,YAAU,MAAM;AACd,UAAM,KAAK,mBAAmB,UAAU,UAAU;AAClD,UAAM,UAAU;AAEhB,WAAO,MAAM;AACX,4BAAsB,EAAE;AAAA,IAC1B;AAAA,EACF,GAAG,CAAC,UAAU,YAAY,oBAAoB,qBAAqB,CAAC;AAEpE,SAAO,MAAM;AACf","sourcesContent":["\"use client\";\n\nimport { useContext, useEffect, useRef } from \"react\";\nimport { CopilotContext } from \"../context/copilot-context\";\nimport { DocumentPointer } from \"../types\";\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 useMakeCopilotDocumentReadable(\n document: DocumentPointer,\n categories?: string[]\n): string | undefined {\n const { addDocumentContext, removeDocumentContext } =\n useContext(CopilotContext);\n const idRef = useRef<string>();\n\n useEffect(() => {\n const id = addDocumentContext(document, categories);\n idRef.current = id;\n\n return () => {\n removeDocumentContext(id);\n };\n }, [document, categories, addDocumentContext, removeDocumentContext]);\n\n return idRef.current;\n}\n"]}
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/context/copilot-context.tsx"],"names":[],"mappings":";;;;;AAGA,OAAO,WAAW;AAHlB;AAaO,SAAS,6BAA6B,QAA0B;AACrE,SAAO;AAAA,IACL,IAAI,kBAA0B;AAC5B,aAAO,GAAG,OAAO;AAAA,IACnB;AAAA,EACF;AACF;AA+BA,IAAM,sBAA4C;AAAA,EAChD,aAAa,CAAC;AAAA,EACd,eAAe,MAAM;AAAA,EAAC;AAAA,EACtB,kBAAkB,MAAM;AAAA,EAAC;AAAA,EACzB,uCAAuC,MAAM,sBAAsB,CAAC,CAAC;AAAA,EACrE,wBAAwB,MAAM,sBAAsB,MAAY;AAAA,EAAC,EAAC;AAAA,EAElE,kBAAkB,MAAM,sBAAsB,EAAE;AAAA,EAChD,YAAY,MAAM;AAAA,EAClB,eAAe,MAAM;AAAA,EAAC;AAAA,EAEtB,qBAAqB,MAAM,sBAAsB,CAAC,CAAC;AAAA,EACnD,oBAAoB,MAAM,sBAAsB,EAAE;AAAA,EAClD,uBAAuB,MAAM;AAAA,EAAC;AAAA,EAE9B,kBAAkB,IAAK,MAAkC;AAAA,IACvD,IAAI,kBAA0B;AAC5B,YAAM,IAAI;AAAA,QACR;AAAA,MACF;AAAA,IACF;AAAA,EACF,EAAG;AACL;AAEO,IAAM,iBACX,MAAM,cAAoC,mBAAmB;AAE/D,SAAS,sBAAyB,OAAa;AAC7C,QAAM,IAAI;AAAA,IACR;AAAA,EACF;AACA,SAAO;AACT","sourcesContent":["\"use client\";\n\nimport { FunctionCallHandler } from \"ai\";\nimport React from \"react\";\nimport { TreeNodeId } from \"../hooks/use-tree\";\nimport { AnnotatedFunction } from \"../types/annotated-function\";\nimport { ChatCompletionCreateParams } from \"openai/resources/chat\";\nimport { DocumentPointer } from \"../types\";\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 // document context\n getDocumentsContext: (categories?: string[]) => DocumentPointer[];\n addDocumentContext: (\n documentPointer: DocumentPointer,\n categories?: string[]\n ) => TreeNodeId;\n removeDocumentContext: (documentId: string) => 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 getDocumentsContext: () => returnAndThrowInDebug([]),\n addDocumentContext: () => returnAndThrowInDebug(\"\"),\n removeDocumentContext: () => {},\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"]}
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/components/copilot-provider.tsx"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAEA,SAAoB,aAAa,gBAAgB;AAiG7C;AAnGJ;AAUO,SAAS,gBAAgB;AAAA,EAC9B;AAAA,EACA;AACF,GAGgB;AACd,QAAM,CAAC,aAAa,cAAc,IAAI,SAEpC,CAAC,CAAC;AAEJ,QAAM,EAAE,YAAY,eAAe,UAAU,IAAI,iBAAQ;AACzD,QAAM;AAAA,IACJ,YAAY;AAAA,IACZ,eAAe;AAAA,IACf,aAAa;AAAA,EACf,IAAI,gCAAsC;AAE1C,QAAM,gBAAgB;AAAA,IACpB,CAAC,IAAY,eAAyC;AACpD,qBAA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IAAI;AAAA,IACtC;AAAA,EACF;AAGA,MAAI,yBAA8D;AAAA,IAChE,MAAM,kBAAkB;AAAA,IACxB,aAAa,kBAAkB;AAAA,IAC/B,YAAY;AAAA,MACV,MAAM;AAAA,MACN,YAAY;AAAA,MACZ,UAAU;AAAA,IACZ;AAAA,EACF;AAEA,SAAO;AACT","sourcesContent":["\"use client\";\nimport { FunctionCallHandler } from \"ai\";\nimport { 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\";\nimport { DocumentPointer } from \"../types\";\nimport useFlatCategoryStore from \"../hooks/use-flat-category-store\";\n\nexport function CopilotProvider({\n chatApiEndpoint,\n children,\n}: {\n chatApiEndpoint: string;\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 const {\n addElement: addDocument,\n removeElement: removeDocument,\n allElements: allDocuments,\n } = useFlatCategoryStore<DocumentPointer>();\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 const getDocumentsContext = useCallback(\n (categories: string[] = [\"global\"]) => {\n return allDocuments(categories);\n },\n [allDocuments]\n );\n\n const addDocumentContext = useCallback(\n (documentPointer: DocumentPointer, categories: string[] = [\"global\"]) => {\n return addDocument(documentPointer, categories);\n },\n [addDocument]\n );\n\n const removeDocumentContext = useCallback(\n (documentId: string) => {\n removeDocument(documentId);\n },\n [removeDocument]\n );\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 getDocumentsContext,\n addDocumentContext,\n removeDocumentContext,\n copilotApiConfig: {\n chatApiEndpoint,\n },\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"]}
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/hooks/use-copilot-chat.ts"],"names":[],"mappings":";;;;;;;;;AAAA,SAAS,SAAS,kBAAkB;AAMpC,SAAS,eAAe;AAuBjB,SAAS,eAAe,IAGiB;AAHjB,eAC7B;AAAA;AAAA,EA9BF,IA6B+B,IAE1B,oBAF0B,IAE1B;AAAA,IADH;AAAA;AAGA,QAAM;AAAA,IACJ;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,IAAI,WAAW,cAAc;AAE7B,QAAM,gBAAyB,QAAQ,MAAM;AAC3C,UAAM,qBAAqB,qBAAqB;AAChD,UAAM,gBAAgB,iBAAiB;AAEvC,WAAO;AAAA,MACL,IAAI;AAAA,MACJ,SAAS,mBAAmB,aAAa;AAAA,MACzC,MAAM;AAAA,IACR;AAAA,EACF,GAAG,CAAC,kBAAkB,iBAAiB,CAAC;AAExC,QAAM,6BAA6B,CAAC,aAAa,EAAE;AAAA,IACjD,QAAQ,mBAAmB,CAAC;AAAA,EAC9B;AAEA,QAAM,uBAAuB,QAAQ,MAAM;AACzC,WAAO,sCAAsC;AAAA,EAC/C,GAAG,CAAC,qCAAqC,CAAC;AAE1C,QAAM,EAAE,UAAU,QAAQ,QAAQ,MAAM,WAAW,OAAO,SAAS,IACjE,QAAQ;AAAA,IACN,KAAK,6BAA6B,gBAAgB,EAAE;AAAA,IACpD,IAAI,QAAQ;AAAA,IACZ,iBAAiB;AAAA,IACjB,6BAA6B,uBAAuB;AAAA,IACpD,MAAM;AAAA,MACJ,IAAI,QAAQ;AAAA,MACZ,WAAW;AAAA,IACb;AAAA,EACF,CAAC;AAEH,QAAM,kBAAkB,SAAS;AAAA,IAC/B,CAAC,YAAY,QAAQ,SAAS,UAAU,QAAQ,SAAS;AAAA,EAC3D;AAEA,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;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","sourcesContent":["import { useMemo, useContext } from \"react\";\nimport {\n CopilotContext,\n CopilotContextParams,\n copilotApiConfigExtrapolator,\n} from \"../context/copilot-context\";\nimport { useChat } from \"ai/react\";\nimport { ChatRequestOptions, CreateMessage, Message } from \"ai\";\nimport { UseChatOptions } from \"ai\";\n\nexport interface UseCopilotChatOptions extends UseChatOptions {\n makeSystemMessage?: (contextString: string) => string;\n}\n\nexport interface UseCopilotChatReturn {\n visibleMessages: Message[];\n append: (\n message: Message | CreateMessage,\n chatRequestOptions?: ChatRequestOptions\n ) => Promise<string | null | undefined>;\n reload: (\n chatRequestOptions?: ChatRequestOptions\n ) => Promise<string | null | undefined>;\n stop: () => void;\n isLoading: boolean;\n input: string;\n setInput: React.Dispatch<React.SetStateAction<string>>;\n}\n\nexport function useCopilotChat({\n makeSystemMessage,\n ...options\n}: UseCopilotChatOptions): UseCopilotChatReturn {\n const {\n getContextString,\n getChatCompletionFunctionDescriptions,\n getFunctionCallHandler,\n copilotApiConfig,\n } = useContext(CopilotContext);\n\n const systemMessage: Message = useMemo(() => {\n const systemMessageMaker = makeSystemMessage || defaultSystemMessage;\n const contextString = getContextString();\n\n return {\n id: \"system\",\n content: systemMessageMaker(contextString),\n role: \"system\",\n };\n }, [getContextString, makeSystemMessage]);\n\n const initialMessagesWithContext = [systemMessage].concat(\n options.initialMessages || []\n );\n\n const functionDescriptions = useMemo(() => {\n return getChatCompletionFunctionDescriptions();\n }, [getChatCompletionFunctionDescriptions]);\n\n const { messages, append, reload, stop, isLoading, input, setInput } =\n useChat({\n api: copilotApiConfigExtrapolator(copilotApiConfig).chatApiEndpoint,\n id: options.id,\n initialMessages: initialMessagesWithContext,\n experimental_onFunctionCall: getFunctionCallHandler(),\n body: {\n id: options.id,\n 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"]}