@copilotkit/react-core 0.8.0-alpha.3 → 0.8.0-alpha.5
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 +49 -45
- package/CHANGELOG.md +12 -0
- package/dist/components/copilot-provider.d.ts +7 -1
- package/dist/components/copilot-provider.mjs +340 -5
- package/dist/components/copilot-provider.mjs.map +1 -1
- package/dist/components/index.d.ts +5 -0
- package/dist/components/index.mjs +340 -6
- package/dist/components/index.mjs.map +1 -1
- package/dist/context/copilot-context.d.ts +8 -1
- package/dist/context/copilot-context.mjs +63 -3
- package/dist/context/copilot-context.mjs.map +1 -1
- package/dist/context/index.d.ts +1 -1
- package/dist/context/index.mjs +63 -4
- package/dist/context/index.mjs.map +1 -1
- package/dist/hooks/index.mjs +199 -7
- package/dist/hooks/index.mjs.map +1 -1
- package/dist/hooks/use-copilot-chat.mjs +155 -4
- package/dist/hooks/use-copilot-chat.mjs.map +1 -1
- package/dist/hooks/use-make-copilot-actionable.mjs +84 -4
- package/dist/hooks/use-make-copilot-actionable.mjs.map +1 -1
- package/dist/hooks/use-make-copilot-readable.mjs +73 -4
- package/dist/hooks/use-make-copilot-readable.mjs.map +1 -1
- package/dist/hooks/use-tree.mjs +159 -3
- package/dist/hooks/use-tree.mjs.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.mjs +470 -12
- package/dist/index.mjs.map +1 -1
- package/dist/types/annotated-function.mjs +0 -2
- package/dist/types/annotated-function.mjs.map +1 -1
- package/dist/types/index.mjs +0 -2
- package/dist/types/index.mjs.map +1 -1
- package/dist/utils/utils.mjs +0 -2
- package/dist/utils/utils.mjs.map +1 -1
- package/dist/utils/utils.test.mjs +0 -1
- package/dist/utils/utils.test.mjs.map +1 -1
- package/package.json +3 -3
- package/src/components/copilot-provider.tsx +5 -1
- package/src/context/copilot-context.tsx +23 -0
- package/src/context/index.ts +2 -0
- package/src/hooks/use-copilot-chat.ts +4 -4
- package/dist/chunk-3BOHSSKR.mjs +0 -141
- package/dist/chunk-3BOHSSKR.mjs.map +0 -1
- package/dist/chunk-547TMALJ.mjs +0 -80
- package/dist/chunk-547TMALJ.mjs.map +0 -1
- package/dist/chunk-6CBAEPMO.mjs +0 -26
- package/dist/chunk-6CBAEPMO.mjs.map +0 -1
- package/dist/chunk-BVQRDAR7.mjs +0 -3
- package/dist/chunk-BVQRDAR7.mjs.map +0 -1
- package/dist/chunk-EFZPSZWO.mjs +0 -3
- package/dist/chunk-EFZPSZWO.mjs.map +0 -1
- package/dist/chunk-FCFGH4M4.mjs +0 -118
- package/dist/chunk-FCFGH4M4.mjs.map +0 -1
- package/dist/chunk-H23NBUJ7.mjs +0 -30
- package/dist/chunk-H23NBUJ7.mjs.map +0 -1
- package/dist/chunk-JD7BAH7U.mjs +0 -3
- package/dist/chunk-JD7BAH7U.mjs.map +0 -1
- package/dist/chunk-KQACBBGH.mjs +0 -19
- package/dist/chunk-KQACBBGH.mjs.map +0 -1
- package/dist/chunk-MRXNTQOX.mjs +0 -55
- package/dist/chunk-MRXNTQOX.mjs.map +0 -1
- package/dist/chunk-VNRDQJXW.mjs +0 -3
- package/dist/chunk-VNRDQJXW.mjs.map +0 -1
package/dist/hooks/index.mjs
CHANGED
|
@@ -1,8 +1,200 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
2
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
3
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
4
|
+
var __objRest = (source, exclude) => {
|
|
5
|
+
var target = {};
|
|
6
|
+
for (var prop in source)
|
|
7
|
+
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
8
|
+
target[prop] = source[prop];
|
|
9
|
+
if (source != null && __getOwnPropSymbols)
|
|
10
|
+
for (var prop of __getOwnPropSymbols(source)) {
|
|
11
|
+
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
|
12
|
+
target[prop] = source[prop];
|
|
13
|
+
}
|
|
14
|
+
return target;
|
|
15
|
+
};
|
|
16
|
+
var __async = (__this, __arguments, generator) => {
|
|
17
|
+
return new Promise((resolve, reject) => {
|
|
18
|
+
var fulfilled = (value) => {
|
|
19
|
+
try {
|
|
20
|
+
step(generator.next(value));
|
|
21
|
+
} catch (e) {
|
|
22
|
+
reject(e);
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
var rejected = (value) => {
|
|
26
|
+
try {
|
|
27
|
+
step(generator.throw(value));
|
|
28
|
+
} catch (e) {
|
|
29
|
+
reject(e);
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
33
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
34
|
+
});
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
// src/hooks/use-copilot-chat.ts
|
|
38
|
+
import { useMemo, useContext } from "react";
|
|
39
|
+
|
|
40
|
+
// src/context/copilot-context.tsx
|
|
41
|
+
import React from "react";
|
|
42
|
+
"use client";
|
|
43
|
+
function copilotApiConfigExtrapolator(config) {
|
|
44
|
+
return {
|
|
45
|
+
get chatApiEndpoint() {
|
|
46
|
+
return `${config.chatApiEndpoint}`;
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
var emptyCopilotContext = {
|
|
51
|
+
entryPoints: {},
|
|
52
|
+
setEntryPoint: () => {
|
|
53
|
+
},
|
|
54
|
+
removeEntryPoint: () => {
|
|
55
|
+
},
|
|
56
|
+
getChatCompletionFunctionDescriptions: () => returnAndThrowInDebug([]),
|
|
57
|
+
getFunctionCallHandler: () => returnAndThrowInDebug(() => __async(void 0, null, function* () {
|
|
58
|
+
})),
|
|
59
|
+
getContextString: () => returnAndThrowInDebug(""),
|
|
60
|
+
addContext: () => "",
|
|
61
|
+
removeContext: () => {
|
|
62
|
+
},
|
|
63
|
+
copilotApiConfig: new class {
|
|
64
|
+
get chatApiEndpoint() {
|
|
65
|
+
throw new Error(
|
|
66
|
+
"Remember to wrap your app in a `<CopilotProvider> {...} </CopilotProvider>` !!!"
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
}()
|
|
70
|
+
};
|
|
71
|
+
var CopilotContext = React.createContext(emptyCopilotContext);
|
|
72
|
+
function returnAndThrowInDebug(value) {
|
|
73
|
+
throw new Error(
|
|
74
|
+
"Remember to wrap your app in a `<CopilotProvider> {...} </CopilotProvider>` !!!"
|
|
75
|
+
);
|
|
76
|
+
return value;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// src/hooks/use-copilot-chat.ts
|
|
80
|
+
import { useChat } from "ai/react";
|
|
81
|
+
function useCopilotChat(_a) {
|
|
82
|
+
var _b = _a, {
|
|
83
|
+
makeSystemMessage
|
|
84
|
+
} = _b, options = __objRest(_b, [
|
|
85
|
+
"makeSystemMessage"
|
|
86
|
+
]);
|
|
87
|
+
const {
|
|
88
|
+
getContextString,
|
|
89
|
+
getChatCompletionFunctionDescriptions,
|
|
90
|
+
getFunctionCallHandler,
|
|
91
|
+
copilotApiConfig
|
|
92
|
+
} = useContext(CopilotContext);
|
|
93
|
+
const systemMessage = useMemo(() => {
|
|
94
|
+
const systemMessageMaker = makeSystemMessage || defaultSystemMessage;
|
|
95
|
+
const contextString = getContextString();
|
|
96
|
+
return {
|
|
97
|
+
id: "system",
|
|
98
|
+
content: systemMessageMaker(contextString),
|
|
99
|
+
role: "system"
|
|
100
|
+
};
|
|
101
|
+
}, [getContextString, makeSystemMessage]);
|
|
102
|
+
const initialMessagesWithContext = [systemMessage].concat(
|
|
103
|
+
options.initialMessages || []
|
|
104
|
+
);
|
|
105
|
+
const functionDescriptions = useMemo(() => {
|
|
106
|
+
return getChatCompletionFunctionDescriptions();
|
|
107
|
+
}, [getChatCompletionFunctionDescriptions]);
|
|
108
|
+
const { messages, append, reload, stop, isLoading, input, setInput } = useChat({
|
|
109
|
+
api: copilotApiConfigExtrapolator(copilotApiConfig).chatApiEndpoint,
|
|
110
|
+
id: options.id,
|
|
111
|
+
initialMessages: initialMessagesWithContext,
|
|
112
|
+
experimental_onFunctionCall: getFunctionCallHandler(),
|
|
113
|
+
body: {
|
|
114
|
+
id: options.id,
|
|
115
|
+
functions: functionDescriptions
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
const visibleMessages = messages.filter(
|
|
119
|
+
(message) => message.role === "user" || message.role === "assistant"
|
|
120
|
+
);
|
|
121
|
+
return {
|
|
122
|
+
visibleMessages,
|
|
123
|
+
append,
|
|
124
|
+
reload,
|
|
125
|
+
stop,
|
|
126
|
+
isLoading,
|
|
127
|
+
input,
|
|
128
|
+
setInput
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
function defaultSystemMessage(contextString) {
|
|
132
|
+
return `
|
|
133
|
+
Please act as an efficient, competent, conscientious, and industrious professional assistant.
|
|
134
|
+
|
|
135
|
+
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.
|
|
136
|
+
Always be polite and respectful, and prefer brevity over verbosity.
|
|
137
|
+
|
|
138
|
+
The user has provided you with the following context:
|
|
139
|
+
\`\`\`
|
|
140
|
+
${contextString}
|
|
141
|
+
\`\`\`
|
|
142
|
+
|
|
143
|
+
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.
|
|
144
|
+
|
|
145
|
+
Please assist them as best you can.
|
|
146
|
+
|
|
147
|
+
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.
|
|
148
|
+
|
|
149
|
+
If you would like to call a function, call it without saying anything else.
|
|
150
|
+
`;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// src/hooks/use-make-copilot-actionable.ts
|
|
154
|
+
import { useRef, useContext as useContext2, useEffect, useMemo as useMemo2 } from "react";
|
|
155
|
+
import { nanoid } from "nanoid";
|
|
156
|
+
"use client";
|
|
157
|
+
function useMakeCopilotActionable(annotatedFunction, dependencies) {
|
|
158
|
+
const idRef = useRef(nanoid());
|
|
159
|
+
const { setEntryPoint, removeEntryPoint } = useContext2(CopilotContext);
|
|
160
|
+
const memoizedAnnotatedFunction = useMemo2(
|
|
161
|
+
() => ({
|
|
162
|
+
name: annotatedFunction.name,
|
|
163
|
+
description: annotatedFunction.description,
|
|
164
|
+
argumentAnnotations: annotatedFunction.argumentAnnotations,
|
|
165
|
+
implementation: annotatedFunction.implementation
|
|
166
|
+
}),
|
|
167
|
+
dependencies
|
|
168
|
+
);
|
|
169
|
+
useEffect(() => {
|
|
170
|
+
setEntryPoint(
|
|
171
|
+
idRef.current,
|
|
172
|
+
memoizedAnnotatedFunction
|
|
173
|
+
);
|
|
174
|
+
return () => {
|
|
175
|
+
removeEntryPoint(idRef.current);
|
|
176
|
+
};
|
|
177
|
+
}, [memoizedAnnotatedFunction, setEntryPoint, removeEntryPoint]);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// src/hooks/use-make-copilot-readable.ts
|
|
181
|
+
import { useContext as useContext3, useEffect as useEffect2, useRef as useRef2 } from "react";
|
|
182
|
+
"use client";
|
|
183
|
+
function useMakeCopilotReadable(information, parentId, categories) {
|
|
184
|
+
const { addContext, removeContext } = useContext3(CopilotContext);
|
|
185
|
+
const idRef = useRef2();
|
|
186
|
+
useEffect2(() => {
|
|
187
|
+
const id = addContext(information, parentId, categories);
|
|
188
|
+
idRef.current = id;
|
|
189
|
+
return () => {
|
|
190
|
+
removeContext(id);
|
|
191
|
+
};
|
|
192
|
+
}, [information, parentId, addContext, removeContext]);
|
|
193
|
+
return idRef.current;
|
|
194
|
+
}
|
|
195
|
+
export {
|
|
196
|
+
useCopilotChat,
|
|
197
|
+
useMakeCopilotActionable,
|
|
198
|
+
useMakeCopilotReadable
|
|
199
|
+
};
|
|
8
200
|
//# sourceMappingURL=index.mjs.map
|
package/dist/hooks/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":[],"names":[],"mappings":""}
|
|
1
|
+
{"version":3,"sources":["../../src/hooks/use-copilot-chat.ts","../../src/context/copilot-context.tsx","../../src/hooks/use-make-copilot-actionable.ts","../../src/hooks/use-make-copilot-readable.ts"],"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","\"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","\"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":"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","names":["useContext","useMemo","useContext","useMemo","useContext","useEffect","useRef","useContext","useRef","useEffect"]}
|
|
@@ -1,5 +1,156 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
2
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
3
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
4
|
+
var __objRest = (source, exclude) => {
|
|
5
|
+
var target = {};
|
|
6
|
+
for (var prop in source)
|
|
7
|
+
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
8
|
+
target[prop] = source[prop];
|
|
9
|
+
if (source != null && __getOwnPropSymbols)
|
|
10
|
+
for (var prop of __getOwnPropSymbols(source)) {
|
|
11
|
+
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
|
12
|
+
target[prop] = source[prop];
|
|
13
|
+
}
|
|
14
|
+
return target;
|
|
15
|
+
};
|
|
16
|
+
var __async = (__this, __arguments, generator) => {
|
|
17
|
+
return new Promise((resolve, reject) => {
|
|
18
|
+
var fulfilled = (value) => {
|
|
19
|
+
try {
|
|
20
|
+
step(generator.next(value));
|
|
21
|
+
} catch (e) {
|
|
22
|
+
reject(e);
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
var rejected = (value) => {
|
|
26
|
+
try {
|
|
27
|
+
step(generator.throw(value));
|
|
28
|
+
} catch (e) {
|
|
29
|
+
reject(e);
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
33
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
34
|
+
});
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
// src/hooks/use-copilot-chat.ts
|
|
38
|
+
import { useMemo, useContext } from "react";
|
|
39
|
+
|
|
40
|
+
// src/context/copilot-context.tsx
|
|
41
|
+
import React from "react";
|
|
42
|
+
"use client";
|
|
43
|
+
function copilotApiConfigExtrapolator(config) {
|
|
44
|
+
return {
|
|
45
|
+
get chatApiEndpoint() {
|
|
46
|
+
return `${config.chatApiEndpoint}`;
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
var emptyCopilotContext = {
|
|
51
|
+
entryPoints: {},
|
|
52
|
+
setEntryPoint: () => {
|
|
53
|
+
},
|
|
54
|
+
removeEntryPoint: () => {
|
|
55
|
+
},
|
|
56
|
+
getChatCompletionFunctionDescriptions: () => returnAndThrowInDebug([]),
|
|
57
|
+
getFunctionCallHandler: () => returnAndThrowInDebug(() => __async(void 0, null, function* () {
|
|
58
|
+
})),
|
|
59
|
+
getContextString: () => returnAndThrowInDebug(""),
|
|
60
|
+
addContext: () => "",
|
|
61
|
+
removeContext: () => {
|
|
62
|
+
},
|
|
63
|
+
copilotApiConfig: new class {
|
|
64
|
+
get chatApiEndpoint() {
|
|
65
|
+
throw new Error(
|
|
66
|
+
"Remember to wrap your app in a `<CopilotProvider> {...} </CopilotProvider>` !!!"
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
}()
|
|
70
|
+
};
|
|
71
|
+
var CopilotContext = React.createContext(emptyCopilotContext);
|
|
72
|
+
function returnAndThrowInDebug(value) {
|
|
73
|
+
throw new Error(
|
|
74
|
+
"Remember to wrap your app in a `<CopilotProvider> {...} </CopilotProvider>` !!!"
|
|
75
|
+
);
|
|
76
|
+
return value;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// src/hooks/use-copilot-chat.ts
|
|
80
|
+
import { useChat } from "ai/react";
|
|
81
|
+
function useCopilotChat(_a) {
|
|
82
|
+
var _b = _a, {
|
|
83
|
+
makeSystemMessage
|
|
84
|
+
} = _b, options = __objRest(_b, [
|
|
85
|
+
"makeSystemMessage"
|
|
86
|
+
]);
|
|
87
|
+
const {
|
|
88
|
+
getContextString,
|
|
89
|
+
getChatCompletionFunctionDescriptions,
|
|
90
|
+
getFunctionCallHandler,
|
|
91
|
+
copilotApiConfig
|
|
92
|
+
} = useContext(CopilotContext);
|
|
93
|
+
const systemMessage = useMemo(() => {
|
|
94
|
+
const systemMessageMaker = makeSystemMessage || defaultSystemMessage;
|
|
95
|
+
const contextString = getContextString();
|
|
96
|
+
return {
|
|
97
|
+
id: "system",
|
|
98
|
+
content: systemMessageMaker(contextString),
|
|
99
|
+
role: "system"
|
|
100
|
+
};
|
|
101
|
+
}, [getContextString, makeSystemMessage]);
|
|
102
|
+
const initialMessagesWithContext = [systemMessage].concat(
|
|
103
|
+
options.initialMessages || []
|
|
104
|
+
);
|
|
105
|
+
const functionDescriptions = useMemo(() => {
|
|
106
|
+
return getChatCompletionFunctionDescriptions();
|
|
107
|
+
}, [getChatCompletionFunctionDescriptions]);
|
|
108
|
+
const { messages, append, reload, stop, isLoading, input, setInput } = useChat({
|
|
109
|
+
api: copilotApiConfigExtrapolator(copilotApiConfig).chatApiEndpoint,
|
|
110
|
+
id: options.id,
|
|
111
|
+
initialMessages: initialMessagesWithContext,
|
|
112
|
+
experimental_onFunctionCall: getFunctionCallHandler(),
|
|
113
|
+
body: {
|
|
114
|
+
id: options.id,
|
|
115
|
+
functions: functionDescriptions
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
const visibleMessages = messages.filter(
|
|
119
|
+
(message) => message.role === "user" || message.role === "assistant"
|
|
120
|
+
);
|
|
121
|
+
return {
|
|
122
|
+
visibleMessages,
|
|
123
|
+
append,
|
|
124
|
+
reload,
|
|
125
|
+
stop,
|
|
126
|
+
isLoading,
|
|
127
|
+
input,
|
|
128
|
+
setInput
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
function defaultSystemMessage(contextString) {
|
|
132
|
+
return `
|
|
133
|
+
Please act as an efficient, competent, conscientious, and industrious professional assistant.
|
|
134
|
+
|
|
135
|
+
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.
|
|
136
|
+
Always be polite and respectful, and prefer brevity over verbosity.
|
|
137
|
+
|
|
138
|
+
The user has provided you with the following context:
|
|
139
|
+
\`\`\`
|
|
140
|
+
${contextString}
|
|
141
|
+
\`\`\`
|
|
142
|
+
|
|
143
|
+
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.
|
|
144
|
+
|
|
145
|
+
Please assist them as best you can.
|
|
146
|
+
|
|
147
|
+
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.
|
|
148
|
+
|
|
149
|
+
If you would like to call a function, call it without saying anything else.
|
|
150
|
+
`;
|
|
151
|
+
}
|
|
152
|
+
export {
|
|
153
|
+
defaultSystemMessage,
|
|
154
|
+
useCopilotChat
|
|
155
|
+
};
|
|
5
156
|
//# sourceMappingURL=use-copilot-chat.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":[],"
|
|
1
|
+
{"version":3,"sources":["../../src/hooks/use-copilot-chat.ts","../../src/context/copilot-context.tsx"],"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","\"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"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,SAAS,SAAS,kBAAkB;;;ACGpC,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;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,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;;;AD/DA,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;","names":[]}
|
|
@@ -1,5 +1,85 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
var __async = (__this, __arguments, generator) => {
|
|
2
|
+
return new Promise((resolve, reject) => {
|
|
3
|
+
var fulfilled = (value) => {
|
|
4
|
+
try {
|
|
5
|
+
step(generator.next(value));
|
|
6
|
+
} catch (e) {
|
|
7
|
+
reject(e);
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
var rejected = (value) => {
|
|
11
|
+
try {
|
|
12
|
+
step(generator.throw(value));
|
|
13
|
+
} catch (e) {
|
|
14
|
+
reject(e);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
18
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
// src/hooks/use-make-copilot-actionable.ts
|
|
23
|
+
import { useRef, useContext, useEffect, useMemo } from "react";
|
|
24
|
+
|
|
25
|
+
// src/context/copilot-context.tsx
|
|
26
|
+
import React from "react";
|
|
27
|
+
"use client";
|
|
28
|
+
var emptyCopilotContext = {
|
|
29
|
+
entryPoints: {},
|
|
30
|
+
setEntryPoint: () => {
|
|
31
|
+
},
|
|
32
|
+
removeEntryPoint: () => {
|
|
33
|
+
},
|
|
34
|
+
getChatCompletionFunctionDescriptions: () => returnAndThrowInDebug([]),
|
|
35
|
+
getFunctionCallHandler: () => returnAndThrowInDebug(() => __async(void 0, null, function* () {
|
|
36
|
+
})),
|
|
37
|
+
getContextString: () => returnAndThrowInDebug(""),
|
|
38
|
+
addContext: () => "",
|
|
39
|
+
removeContext: () => {
|
|
40
|
+
},
|
|
41
|
+
copilotApiConfig: new class {
|
|
42
|
+
get chatApiEndpoint() {
|
|
43
|
+
throw new Error(
|
|
44
|
+
"Remember to wrap your app in a `<CopilotProvider> {...} </CopilotProvider>` !!!"
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
}()
|
|
48
|
+
};
|
|
49
|
+
var CopilotContext = React.createContext(emptyCopilotContext);
|
|
50
|
+
function returnAndThrowInDebug(value) {
|
|
51
|
+
throw new Error(
|
|
52
|
+
"Remember to wrap your app in a `<CopilotProvider> {...} </CopilotProvider>` !!!"
|
|
53
|
+
);
|
|
54
|
+
return value;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// src/hooks/use-make-copilot-actionable.ts
|
|
58
|
+
import { nanoid } from "nanoid";
|
|
59
|
+
"use client";
|
|
60
|
+
function useMakeCopilotActionable(annotatedFunction, dependencies) {
|
|
61
|
+
const idRef = useRef(nanoid());
|
|
62
|
+
const { setEntryPoint, removeEntryPoint } = useContext(CopilotContext);
|
|
63
|
+
const memoizedAnnotatedFunction = useMemo(
|
|
64
|
+
() => ({
|
|
65
|
+
name: annotatedFunction.name,
|
|
66
|
+
description: annotatedFunction.description,
|
|
67
|
+
argumentAnnotations: annotatedFunction.argumentAnnotations,
|
|
68
|
+
implementation: annotatedFunction.implementation
|
|
69
|
+
}),
|
|
70
|
+
dependencies
|
|
71
|
+
);
|
|
72
|
+
useEffect(() => {
|
|
73
|
+
setEntryPoint(
|
|
74
|
+
idRef.current,
|
|
75
|
+
memoizedAnnotatedFunction
|
|
76
|
+
);
|
|
77
|
+
return () => {
|
|
78
|
+
removeEntryPoint(idRef.current);
|
|
79
|
+
};
|
|
80
|
+
}, [memoizedAnnotatedFunction, setEntryPoint, removeEntryPoint]);
|
|
81
|
+
}
|
|
82
|
+
export {
|
|
83
|
+
useMakeCopilotActionable
|
|
84
|
+
};
|
|
5
85
|
//# sourceMappingURL=use-make-copilot-actionable.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":[],"
|
|
1
|
+
{"version":3,"sources":["../../src/hooks/use-make-copilot-actionable.ts","../../src/context/copilot-context.tsx"],"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","\"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"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAEA,SAAS,QAAQ,YAAY,WAAW,eAAe;;;ACCvD,OAAO,WAAW;AAHlB;AAyCA,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,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;;;ADhEA,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;","names":[]}
|
|
@@ -1,5 +1,74 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
var __async = (__this, __arguments, generator) => {
|
|
2
|
+
return new Promise((resolve, reject) => {
|
|
3
|
+
var fulfilled = (value) => {
|
|
4
|
+
try {
|
|
5
|
+
step(generator.next(value));
|
|
6
|
+
} catch (e) {
|
|
7
|
+
reject(e);
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
var rejected = (value) => {
|
|
11
|
+
try {
|
|
12
|
+
step(generator.throw(value));
|
|
13
|
+
} catch (e) {
|
|
14
|
+
reject(e);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
18
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
// src/hooks/use-make-copilot-readable.ts
|
|
23
|
+
import { useContext, useEffect, useRef } from "react";
|
|
24
|
+
|
|
25
|
+
// src/context/copilot-context.tsx
|
|
26
|
+
import React from "react";
|
|
27
|
+
"use client";
|
|
28
|
+
var emptyCopilotContext = {
|
|
29
|
+
entryPoints: {},
|
|
30
|
+
setEntryPoint: () => {
|
|
31
|
+
},
|
|
32
|
+
removeEntryPoint: () => {
|
|
33
|
+
},
|
|
34
|
+
getChatCompletionFunctionDescriptions: () => returnAndThrowInDebug([]),
|
|
35
|
+
getFunctionCallHandler: () => returnAndThrowInDebug(() => __async(void 0, null, function* () {
|
|
36
|
+
})),
|
|
37
|
+
getContextString: () => returnAndThrowInDebug(""),
|
|
38
|
+
addContext: () => "",
|
|
39
|
+
removeContext: () => {
|
|
40
|
+
},
|
|
41
|
+
copilotApiConfig: new class {
|
|
42
|
+
get chatApiEndpoint() {
|
|
43
|
+
throw new Error(
|
|
44
|
+
"Remember to wrap your app in a `<CopilotProvider> {...} </CopilotProvider>` !!!"
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
}()
|
|
48
|
+
};
|
|
49
|
+
var CopilotContext = React.createContext(emptyCopilotContext);
|
|
50
|
+
function returnAndThrowInDebug(value) {
|
|
51
|
+
throw new Error(
|
|
52
|
+
"Remember to wrap your app in a `<CopilotProvider> {...} </CopilotProvider>` !!!"
|
|
53
|
+
);
|
|
54
|
+
return value;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// src/hooks/use-make-copilot-readable.ts
|
|
58
|
+
"use client";
|
|
59
|
+
function useMakeCopilotReadable(information, parentId, categories) {
|
|
60
|
+
const { addContext, removeContext } = useContext(CopilotContext);
|
|
61
|
+
const idRef = useRef();
|
|
62
|
+
useEffect(() => {
|
|
63
|
+
const id = addContext(information, parentId, categories);
|
|
64
|
+
idRef.current = id;
|
|
65
|
+
return () => {
|
|
66
|
+
removeContext(id);
|
|
67
|
+
};
|
|
68
|
+
}, [information, parentId, addContext, removeContext]);
|
|
69
|
+
return idRef.current;
|
|
70
|
+
}
|
|
71
|
+
export {
|
|
72
|
+
useMakeCopilotReadable
|
|
73
|
+
};
|
|
5
74
|
//# sourceMappingURL=use-make-copilot-readable.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":[],"
|
|
1
|
+
{"version":3,"sources":["../../src/hooks/use-make-copilot-readable.ts","../../src/context/copilot-context.tsx"],"sourcesContent":["\"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","\"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"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAEA,SAAS,YAAY,WAAW,cAAc;;;ACC9C,OAAO,WAAW;AAHlB;AAyCA,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,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;;;ADrEA;AAYO,SAAS,uBACd,aACA,UACA,YACoB;AACpB,QAAM,EAAE,YAAY,cAAc,IAAI,WAAW,cAAc;AAC/D,QAAM,QAAQ,OAAe;AAE7B,YAAU,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":[]}
|