@copilotkit/react-core 0.8.0-alpha.6 → 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.
Files changed (63) hide show
  1. package/.turbo/turbo-build.log +88 -88
  2. package/CHANGELOG.md +16 -0
  3. package/dist/chunk-3BOHSSKR.mjs +141 -0
  4. package/dist/chunk-3BOHSSKR.mjs.map +1 -0
  5. package/dist/chunk-3O374DDN.mjs +19 -0
  6. package/dist/chunk-3O374DDN.mjs.map +1 -0
  7. package/dist/chunk-63ILXW5V.mjs +76 -0
  8. package/dist/chunk-63ILXW5V.mjs.map +1 -0
  9. package/dist/chunk-6RRG7ME3.mjs +19 -0
  10. package/dist/chunk-6RRG7ME3.mjs.map +1 -0
  11. package/dist/chunk-EFZPSZWO.mjs +3 -0
  12. package/dist/chunk-EFZPSZWO.mjs.map +1 -0
  13. package/dist/chunk-JD7BAH7U.mjs +3 -0
  14. package/dist/chunk-JD7BAH7U.mjs.map +1 -0
  15. package/dist/chunk-K7TVQCXG.mjs +44 -0
  16. package/dist/chunk-K7TVQCXG.mjs.map +1 -0
  17. package/dist/chunk-MRXNTQOX.mjs +55 -0
  18. package/dist/chunk-MRXNTQOX.mjs.map +1 -0
  19. package/dist/chunk-QACD2U6P.mjs +3 -0
  20. package/dist/chunk-QACD2U6P.mjs.map +1 -0
  21. package/dist/chunk-QFCPUIF7.mjs +147 -0
  22. package/dist/chunk-QFCPUIF7.mjs.map +1 -0
  23. package/dist/chunk-UNFDVVCF.mjs +80 -0
  24. package/dist/chunk-UNFDVVCF.mjs.map +1 -0
  25. package/dist/chunk-YPSGKPDA.mjs +3 -0
  26. package/dist/chunk-YPSGKPDA.mjs.map +1 -0
  27. package/dist/chunk-YTIZQV7H.mjs +30 -0
  28. package/dist/chunk-YTIZQV7H.mjs.map +1 -0
  29. package/dist/components/copilot-provider.mjs +6 -442
  30. package/dist/components/copilot-provider.mjs.map +1 -1
  31. package/dist/components/index.mjs +7 -442
  32. package/dist/components/index.mjs.map +1 -1
  33. package/dist/context/copilot-context.mjs +3 -67
  34. package/dist/context/copilot-context.mjs.map +1 -1
  35. package/dist/context/index.mjs +4 -67
  36. package/dist/context/index.mjs.map +1 -1
  37. package/dist/hooks/index.mjs +8 -220
  38. package/dist/hooks/index.mjs.map +1 -1
  39. package/dist/hooks/use-copilot-chat.mjs +4 -159
  40. package/dist/hooks/use-copilot-chat.mjs.map +1 -1
  41. package/dist/hooks/use-flat-category-store.mjs +3 -74
  42. package/dist/hooks/use-flat-category-store.mjs.map +1 -1
  43. package/dist/hooks/use-make-copilot-actionable.mjs +4 -88
  44. package/dist/hooks/use-make-copilot-actionable.mjs.map +1 -1
  45. package/dist/hooks/use-make-copilot-document-readable.mjs +4 -77
  46. package/dist/hooks/use-make-copilot-document-readable.mjs.map +1 -1
  47. package/dist/hooks/use-make-copilot-readable.mjs +4 -77
  48. package/dist/hooks/use-make-copilot-readable.mjs.map +1 -1
  49. package/dist/hooks/use-tree.mjs +3 -159
  50. package/dist/hooks/use-tree.mjs.map +1 -1
  51. package/dist/index.mjs +14 -589
  52. package/dist/index.mjs.map +1 -1
  53. package/dist/types/annotated-function.mjs +2 -0
  54. package/dist/types/annotated-function.mjs.map +1 -1
  55. package/dist/types/document-pointer.mjs +2 -0
  56. package/dist/types/document-pointer.mjs.map +1 -1
  57. package/dist/types/index.mjs +2 -0
  58. package/dist/types/index.mjs.map +1 -1
  59. package/dist/utils/utils.mjs +2 -0
  60. package/dist/utils/utils.mjs.map +1 -1
  61. package/dist/utils/utils.test.mjs +1 -0
  62. package/dist/utils/utils.test.mjs.map +1 -1
  63. package/package.json +2 -2
@@ -0,0 +1,55 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __defProps = Object.defineProperties;
3
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
4
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
7
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
+ var __spreadValues = (a, b) => {
9
+ for (var prop in b || (b = {}))
10
+ if (__hasOwnProp.call(b, prop))
11
+ __defNormalProp(a, prop, b[prop]);
12
+ if (__getOwnPropSymbols)
13
+ for (var prop of __getOwnPropSymbols(b)) {
14
+ if (__propIsEnum.call(b, prop))
15
+ __defNormalProp(a, prop, b[prop]);
16
+ }
17
+ return a;
18
+ };
19
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
+ var __objRest = (source, exclude) => {
21
+ var target = {};
22
+ for (var prop in source)
23
+ if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
24
+ target[prop] = source[prop];
25
+ if (source != null && __getOwnPropSymbols)
26
+ for (var prop of __getOwnPropSymbols(source)) {
27
+ if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
28
+ target[prop] = source[prop];
29
+ }
30
+ return target;
31
+ };
32
+ var __async = (__this, __arguments, generator) => {
33
+ return new Promise((resolve, reject) => {
34
+ var fulfilled = (value) => {
35
+ try {
36
+ step(generator.next(value));
37
+ } catch (e) {
38
+ reject(e);
39
+ }
40
+ };
41
+ var rejected = (value) => {
42
+ try {
43
+ step(generator.throw(value));
44
+ } catch (e) {
45
+ reject(e);
46
+ }
47
+ };
48
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
49
+ step((generator = generator.apply(__this, __arguments)).next());
50
+ });
51
+ };
52
+
53
+ export { __async, __objRest, __spreadProps, __spreadValues };
54
+ //# sourceMappingURL=out.js.map
55
+ //# sourceMappingURL=chunk-MRXNTQOX.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"names":[],"mappings":"","sourcesContent":[]}
@@ -0,0 +1,3 @@
1
+
2
+ //# sourceMappingURL=out.js.map
3
+ //# sourceMappingURL=chunk-QACD2U6P.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"names":[],"mappings":""}
@@ -0,0 +1,147 @@
1
+ import { use_tree_default } from './chunk-3BOHSSKR.mjs';
2
+ import { CopilotContext } from './chunk-K7TVQCXG.mjs';
3
+ import { use_flat_category_store_default } from './chunk-63ILXW5V.mjs';
4
+ import { __spreadProps, __spreadValues, __async, __objRest } from './chunk-MRXNTQOX.mjs';
5
+ import { useState, useCallback } from 'react';
6
+ import { jsx } from 'react/jsx-runtime';
7
+
8
+ function CopilotProvider({
9
+ copilotApiConfig,
10
+ children
11
+ }) {
12
+ const [entryPoints, setEntryPoints] = useState({});
13
+ const { addElement, removeElement, printTree } = use_tree_default();
14
+ const {
15
+ addElement: addDocument,
16
+ removeElement: removeDocument,
17
+ allElements: allDocuments
18
+ } = use_flat_category_store_default();
19
+ const setEntryPoint = useCallback(
20
+ (id, entryPoint) => {
21
+ setEntryPoints((prevPoints) => {
22
+ return __spreadProps(__spreadValues({}, prevPoints), {
23
+ [id]: entryPoint
24
+ });
25
+ });
26
+ },
27
+ []
28
+ );
29
+ const removeEntryPoint = useCallback((id) => {
30
+ setEntryPoints((prevPoints) => {
31
+ const newPoints = __spreadValues({}, prevPoints);
32
+ delete newPoints[id];
33
+ return newPoints;
34
+ });
35
+ }, []);
36
+ const getContextString = useCallback(
37
+ (categories = ["global"]) => {
38
+ return printTree(categories);
39
+ },
40
+ [printTree]
41
+ );
42
+ const addContext = useCallback(
43
+ (context, parentId, categories = ["global"]) => {
44
+ return addElement(context, categories, parentId);
45
+ },
46
+ [addElement]
47
+ );
48
+ const removeContext = useCallback(
49
+ (id) => {
50
+ removeElement(id);
51
+ },
52
+ [removeElement]
53
+ );
54
+ const getChatCompletionFunctionDescriptions = useCallback(() => {
55
+ return entryPointsToChatCompletionFunctions(Object.values(entryPoints));
56
+ }, [entryPoints]);
57
+ const getFunctionCallHandler = useCallback(() => {
58
+ return entryPointsToFunctionCallHandler(Object.values(entryPoints));
59
+ }, [entryPoints]);
60
+ const getDocumentsContext = useCallback(
61
+ (categories = ["global"]) => {
62
+ return allDocuments(categories);
63
+ },
64
+ [allDocuments]
65
+ );
66
+ const addDocumentContext = useCallback(
67
+ (documentPointer, categories = ["global"]) => {
68
+ return addDocument(documentPointer, categories);
69
+ },
70
+ [addDocument]
71
+ );
72
+ const removeDocumentContext = useCallback(
73
+ (documentId) => {
74
+ removeDocument(documentId);
75
+ },
76
+ [removeDocument]
77
+ );
78
+ return /* @__PURE__ */ jsx(CopilotContext.Provider, {
79
+ value: {
80
+ entryPoints,
81
+ getChatCompletionFunctionDescriptions,
82
+ getFunctionCallHandler,
83
+ setEntryPoint,
84
+ removeEntryPoint,
85
+ getContextString,
86
+ addContext,
87
+ removeContext,
88
+ getDocumentsContext,
89
+ addDocumentContext,
90
+ removeDocumentContext,
91
+ copilotApiConfig
92
+ },
93
+ children
94
+ });
95
+ }
96
+ function entryPointsToFunctionCallHandler(entryPoints) {
97
+ return (chatMessages, functionCall) => __async(this, null, function* () {
98
+ let entrypointsByFunctionName = {};
99
+ for (let entryPoint of entryPoints) {
100
+ entrypointsByFunctionName[entryPoint.name] = entryPoint;
101
+ }
102
+ const entryPointFunction = entrypointsByFunctionName[functionCall.name || ""];
103
+ if (entryPointFunction) {
104
+ let parsedFunctionCallArguments = [];
105
+ if (functionCall.arguments) {
106
+ parsedFunctionCallArguments = JSON.parse(functionCall.arguments);
107
+ }
108
+ const paramsInCorrectOrder = [];
109
+ for (let arg of entryPointFunction.argumentAnnotations) {
110
+ paramsInCorrectOrder.push(
111
+ parsedFunctionCallArguments[arg.name]
112
+ );
113
+ }
114
+ yield entryPointFunction.implementation(...paramsInCorrectOrder);
115
+ }
116
+ });
117
+ }
118
+ function entryPointsToChatCompletionFunctions(entryPoints) {
119
+ return entryPoints.map(annotatedFunctionToChatCompletionFunction);
120
+ }
121
+ function annotatedFunctionToChatCompletionFunction(annotatedFunction) {
122
+ let parameters = {};
123
+ for (let arg of annotatedFunction.argumentAnnotations) {
124
+ let _a = arg, forwardedArgs = __objRest(_a, ["name", "required"]);
125
+ parameters[arg.name] = forwardedArgs;
126
+ }
127
+ let requiredParameterNames = [];
128
+ for (let arg of annotatedFunction.argumentAnnotations) {
129
+ if (arg.required) {
130
+ requiredParameterNames.push(arg.name);
131
+ }
132
+ }
133
+ let chatCompletionFunction = {
134
+ name: annotatedFunction.name,
135
+ description: annotatedFunction.description,
136
+ parameters: {
137
+ type: "object",
138
+ properties: parameters,
139
+ required: requiredParameterNames
140
+ }
141
+ };
142
+ return chatCompletionFunction;
143
+ }
144
+
145
+ export { CopilotProvider };
146
+ //# sourceMappingURL=out.js.map
147
+ //# sourceMappingURL=chunk-QFCPUIF7.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/components/copilot-provider.tsx"],"names":[],"mappings":"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","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 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 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 }}\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"]}
@@ -0,0 +1,80 @@
1
+ import { CopilotContext, copilotApiConfigExtrapolator } from './chunk-K7TVQCXG.mjs';
2
+ import { __objRest } from './chunk-MRXNTQOX.mjs';
3
+ import { useContext, useMemo } from 'react';
4
+ import { useChat } from 'ai/react';
5
+
6
+ function useCopilotChat(_a) {
7
+ var _b = _a, {
8
+ makeSystemMessage
9
+ } = _b, options = __objRest(_b, [
10
+ "makeSystemMessage"
11
+ ]);
12
+ const {
13
+ getContextString,
14
+ getChatCompletionFunctionDescriptions,
15
+ getFunctionCallHandler,
16
+ copilotApiConfig
17
+ } = useContext(CopilotContext);
18
+ const systemMessage = useMemo(() => {
19
+ const systemMessageMaker = makeSystemMessage || defaultSystemMessage;
20
+ const contextString = getContextString();
21
+ return {
22
+ id: "system",
23
+ content: systemMessageMaker(contextString),
24
+ role: "system"
25
+ };
26
+ }, [getContextString, makeSystemMessage]);
27
+ const initialMessagesWithContext = [systemMessage].concat(
28
+ options.initialMessages || []
29
+ );
30
+ const functionDescriptions = useMemo(() => {
31
+ return getChatCompletionFunctionDescriptions();
32
+ }, [getChatCompletionFunctionDescriptions]);
33
+ const { messages, append, reload, stop, isLoading, input, setInput } = useChat({
34
+ api: copilotApiConfigExtrapolator(copilotApiConfig).chatApiEndpoint,
35
+ id: options.id,
36
+ initialMessages: initialMessagesWithContext,
37
+ experimental_onFunctionCall: getFunctionCallHandler(),
38
+ body: {
39
+ id: options.id,
40
+ functions: functionDescriptions
41
+ }
42
+ });
43
+ const visibleMessages = messages.filter(
44
+ (message) => message.role === "user" || message.role === "assistant"
45
+ );
46
+ return {
47
+ visibleMessages,
48
+ append,
49
+ reload,
50
+ stop,
51
+ isLoading,
52
+ input,
53
+ setInput
54
+ };
55
+ }
56
+ function defaultSystemMessage(contextString) {
57
+ return `
58
+ Please act as an efficient, competent, conscientious, and industrious professional assistant.
59
+
60
+ Help the user achieve their goals, and you do so in a way that is as efficient as possible, without unnecessary fluff, but also without sacrificing professionalism.
61
+ Always be polite and respectful, and prefer brevity over verbosity.
62
+
63
+ The user has provided you with the following context:
64
+ \`\`\`
65
+ ${contextString}
66
+ \`\`\`
67
+
68
+ They have also provided you with functions you can call to initiate actions on their behalf, or functions you can call to receive more information.
69
+
70
+ Please assist them as best you can.
71
+
72
+ You 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.
73
+
74
+ If you would like to call a function, call it without saying anything else.
75
+ `;
76
+ }
77
+
78
+ export { defaultSystemMessage, useCopilotChat };
79
+ //# sourceMappingURL=out.js.map
80
+ //# sourceMappingURL=chunk-UNFDVVCF.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;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"]}
@@ -0,0 +1,3 @@
1
+
2
+ //# sourceMappingURL=out.js.map
3
+ //# sourceMappingURL=chunk-YPSGKPDA.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"names":[],"mappings":""}
@@ -0,0 +1,30 @@
1
+ import { CopilotContext } from './chunk-K7TVQCXG.mjs';
2
+ import { useRef, useContext, useMemo, useEffect } from 'react';
3
+ import { nanoid } from 'nanoid';
4
+
5
+ function useMakeCopilotActionable(annotatedFunction, dependencies) {
6
+ const idRef = useRef(nanoid());
7
+ const { setEntryPoint, removeEntryPoint } = useContext(CopilotContext);
8
+ const memoizedAnnotatedFunction = useMemo(
9
+ () => ({
10
+ name: annotatedFunction.name,
11
+ description: annotatedFunction.description,
12
+ argumentAnnotations: annotatedFunction.argumentAnnotations,
13
+ implementation: annotatedFunction.implementation
14
+ }),
15
+ dependencies
16
+ );
17
+ useEffect(() => {
18
+ setEntryPoint(
19
+ idRef.current,
20
+ memoizedAnnotatedFunction
21
+ );
22
+ return () => {
23
+ removeEntryPoint(idRef.current);
24
+ };
25
+ }, [memoizedAnnotatedFunction, setEntryPoint, removeEntryPoint]);
26
+ }
27
+
28
+ export { useMakeCopilotActionable };
29
+ //# sourceMappingURL=out.js.map
30
+ //# sourceMappingURL=chunk-YTIZQV7H.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/hooks/use-make-copilot-actionable.ts"],"names":[],"mappings":";;;;;AAEA,SAAS,QAAQ,YAAY,WAAW,eAAe;AAGvD,SAAS,cAAc;AALvB;AAOO,SAAS,yBACd,mBACA,cACA;AACA,QAAM,QAAQ,OAAO,OAAO,CAAC;AAC7B,QAAM,EAAE,eAAe,iBAAiB,IAAI,WAAW,cAAc;AAErE,QAAM,4BAA4D;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","sourcesContent":["\"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"]}