@copilotkit/react-core 0.11.0-alpha.5 → 0.11.0-alpha.7

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 (38) hide show
  1. package/.turbo/turbo-build.log +38 -78
  2. package/CHANGELOG.md +12 -0
  3. package/package.json +2 -2
  4. package/tsup.config.ts +1 -1
  5. package/dist/components/copilot-provider.js +0 -447
  6. package/dist/components/copilot-provider.js.map +0 -1
  7. package/dist/components/index.js +0 -447
  8. package/dist/components/index.js.map +0 -1
  9. package/dist/context/copilot-context.js +0 -52
  10. package/dist/context/copilot-context.js.map +0 -1
  11. package/dist/context/index.js +0 -52
  12. package/dist/context/index.js.map +0 -1
  13. package/dist/hooks/index.js +0 -205
  14. package/dist/hooks/index.js.map +0 -1
  15. package/dist/hooks/use-copilot-chat.js +0 -156
  16. package/dist/hooks/use-copilot-chat.js.map +0 -1
  17. package/dist/hooks/use-flat-category-store.js +0 -78
  18. package/dist/hooks/use-flat-category-store.js.map +0 -1
  19. package/dist/hooks/use-make-copilot-actionable.js +0 -67
  20. package/dist/hooks/use-make-copilot-actionable.js.map +0 -1
  21. package/dist/hooks/use-make-copilot-document-readable.js +0 -56
  22. package/dist/hooks/use-make-copilot-document-readable.js.map +0 -1
  23. package/dist/hooks/use-make-copilot-readable.js +0 -56
  24. package/dist/hooks/use-make-copilot-readable.js.map +0 -1
  25. package/dist/hooks/use-tree.js +0 -161
  26. package/dist/hooks/use-tree.js.map +0 -1
  27. package/dist/index.js +0 -579
  28. package/dist/index.js.map +0 -1
  29. package/dist/types/annotated-function.js +0 -4
  30. package/dist/types/annotated-function.js.map +0 -1
  31. package/dist/types/document-pointer.js +0 -4
  32. package/dist/types/document-pointer.js.map +0 -1
  33. package/dist/types/index.js +0 -4
  34. package/dist/types/index.js.map +0 -1
  35. package/dist/utils/utils.js +0 -4
  36. package/dist/utils/utils.js.map +0 -1
  37. package/dist/utils/utils.test.js +0 -10
  38. package/dist/utils/utils.test.js.map +0 -1
@@ -1,52 +0,0 @@
1
- 'use strict';
2
-
3
- var React = require('react');
4
-
5
- function copilotApiConfigExtrapolator(config) {
6
- return {
7
- get chatApiEndpoint() {
8
- return `${config.chatApiEndpoint}`;
9
- }
10
- };
11
- }
12
- var emptyCopilotContext = {
13
- entryPoints: {},
14
- setEntryPoint: () => {
15
- },
16
- removeEntryPoint: () => {
17
- },
18
- getChatCompletionFunctionDescriptions: () => returnAndThrowInDebug(),
19
- getFunctionCallHandler: () => returnAndThrowInDebug(),
20
- getContextString: (documents) => returnAndThrowInDebug(),
21
- addContext: () => "",
22
- removeContext: () => {
23
- },
24
- getDocumentsContext: () => returnAndThrowInDebug(),
25
- addDocumentContext: () => returnAndThrowInDebug(),
26
- removeDocumentContext: () => {
27
- },
28
- copilotApiConfig: new class {
29
- get chatApiEndpoint() {
30
- throw new Error(
31
- "Remember to wrap your app in a `<CopilotProvider> {...} </CopilotProvider>` !!!"
32
- );
33
- }
34
- get headers() {
35
- return {};
36
- }
37
- get body() {
38
- return {};
39
- }
40
- }()
41
- };
42
- var CopilotContext = React.createContext(emptyCopilotContext);
43
- function returnAndThrowInDebug(value) {
44
- throw new Error(
45
- "Remember to wrap your app in a `<CopilotProvider> {...} </CopilotProvider>` !!!"
46
- );
47
- }
48
-
49
- exports.CopilotContext = CopilotContext;
50
- exports.copilotApiConfigExtrapolator = copilotApiConfigExtrapolator;
51
- //# sourceMappingURL=out.js.map
52
- //# sourceMappingURL=index.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../src/context/copilot-context.tsx"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAGA,OAAO,WAAW;AAHlB;AA2CO,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,IACA,IAAI,UAAkC;AACpC,aAAO,CAAC;AAAA,IACV;AAAA,IACA,IAAI,OAA4B;AAC9B,aAAO,CAAC;AAAA,IACV;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\n/**\n * Interface for the configuration of the Copilot API.\n */\nexport interface CopilotApiConfig {\n /**\n * The endpoint for the chat API.\n */\n chatApiEndpoint: string;\n\n /**\n * additional headers to be sent with the request\n * @default {}\n * @example\n * ```\n * {\n * 'Authorization': 'Bearer your_token_here'\n * }\n * ```\n */\n headers: Record<string, string>;\n\n /**\n * Additional body params to be sent with the request\n * @default {}\n * @example\n * ```\n * {\n * 'message': 'Hello, world!'\n * }\n * ```\n */\n body: Record<string, any>;\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 get headers(): Record<string, string> {\n return {};\n }\n get body(): Record<string, any> {\n return {};\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,205 +0,0 @@
1
- 'use strict';
2
-
3
- var React = require('react');
4
- var react = require('ai/react');
5
- var nanoid = require('nanoid');
6
-
7
- var __defProp = Object.defineProperty;
8
- var __defProps = Object.defineProperties;
9
- var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
10
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
11
- var __hasOwnProp = Object.prototype.hasOwnProperty;
12
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
13
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
14
- var __spreadValues = (a, b) => {
15
- for (var prop in b || (b = {}))
16
- if (__hasOwnProp.call(b, prop))
17
- __defNormalProp(a, prop, b[prop]);
18
- if (__getOwnPropSymbols)
19
- for (var prop of __getOwnPropSymbols(b)) {
20
- if (__propIsEnum.call(b, prop))
21
- __defNormalProp(a, prop, b[prop]);
22
- }
23
- return a;
24
- };
25
- var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
26
- var __objRest = (source, exclude) => {
27
- var target = {};
28
- for (var prop in source)
29
- if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
30
- target[prop] = source[prop];
31
- if (source != null && __getOwnPropSymbols)
32
- for (var prop of __getOwnPropSymbols(source)) {
33
- if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
34
- target[prop] = source[prop];
35
- }
36
- return target;
37
- };
38
- function copilotApiConfigExtrapolator(config) {
39
- return {
40
- get chatApiEndpoint() {
41
- return `${config.chatApiEndpoint}`;
42
- }
43
- };
44
- }
45
- var emptyCopilotContext = {
46
- entryPoints: {},
47
- setEntryPoint: () => {
48
- },
49
- removeEntryPoint: () => {
50
- },
51
- getChatCompletionFunctionDescriptions: () => returnAndThrowInDebug(),
52
- getFunctionCallHandler: () => returnAndThrowInDebug(),
53
- getContextString: (documents) => returnAndThrowInDebug(),
54
- addContext: () => "",
55
- removeContext: () => {
56
- },
57
- getDocumentsContext: () => returnAndThrowInDebug(),
58
- addDocumentContext: () => returnAndThrowInDebug(),
59
- removeDocumentContext: () => {
60
- },
61
- copilotApiConfig: new class {
62
- get chatApiEndpoint() {
63
- throw new Error(
64
- "Remember to wrap your app in a `<CopilotProvider> {...} </CopilotProvider>` !!!"
65
- );
66
- }
67
- get headers() {
68
- return {};
69
- }
70
- get body() {
71
- return {};
72
- }
73
- }()
74
- };
75
- var CopilotContext = React.createContext(emptyCopilotContext);
76
- function returnAndThrowInDebug(value) {
77
- throw new Error(
78
- "Remember to wrap your app in a `<CopilotProvider> {...} </CopilotProvider>` !!!"
79
- );
80
- }
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
- } = React.useContext(CopilotContext);
93
- const systemMessage = React.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 = React.useMemo(() => {
106
- return getChatCompletionFunctionDescriptions();
107
- }, [getChatCompletionFunctionDescriptions]);
108
- const { messages, append, reload, stop, isLoading, input, setInput } = react.useChat(__spreadProps(__spreadValues({}, options), {
109
- api: copilotApiConfigExtrapolator(copilotApiConfig).chatApiEndpoint,
110
- id: options.id,
111
- initialMessages: initialMessagesWithContext,
112
- experimental_onFunctionCall: getFunctionCallHandler(),
113
- headers: __spreadValues(__spreadValues({}, copilotApiConfig.headers), options.headers),
114
- body: __spreadValues(__spreadValues({
115
- id: options.id,
116
- functions: functionDescriptions
117
- }, copilotApiConfig.body), options.body)
118
- }));
119
- const visibleMessages = messages.filter(
120
- (message) => message.role === "user" || message.role === "assistant"
121
- );
122
- return {
123
- visibleMessages,
124
- append,
125
- reload,
126
- stop,
127
- isLoading,
128
- input,
129
- setInput
130
- };
131
- }
132
- function defaultSystemMessage(contextString) {
133
- return `
134
- Please act as an efficient, competent, conscientious, and industrious professional assistant.
135
-
136
- 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.
137
- Always be polite and respectful, and prefer brevity over verbosity.
138
-
139
- The user has provided you with the following context:
140
- \`\`\`
141
- ${contextString}
142
- \`\`\`
143
-
144
- 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.
145
-
146
- Please assist them as best you can.
147
-
148
- 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.
149
-
150
- If you would like to call a function, call it without saying anything else.
151
- `;
152
- }
153
- function useMakeCopilotActionable(annotatedFunction, dependencies) {
154
- const idRef = React.useRef(nanoid.nanoid());
155
- const { setEntryPoint, removeEntryPoint } = React.useContext(CopilotContext);
156
- const memoizedAnnotatedFunction = React.useMemo(
157
- () => ({
158
- name: annotatedFunction.name,
159
- description: annotatedFunction.description,
160
- argumentAnnotations: annotatedFunction.argumentAnnotations,
161
- implementation: annotatedFunction.implementation
162
- }),
163
- dependencies
164
- );
165
- React.useEffect(() => {
166
- setEntryPoint(
167
- idRef.current,
168
- memoizedAnnotatedFunction
169
- );
170
- return () => {
171
- removeEntryPoint(idRef.current);
172
- };
173
- }, [memoizedAnnotatedFunction, setEntryPoint, removeEntryPoint]);
174
- }
175
- function useMakeCopilotReadable(information, parentId, categories) {
176
- const { addContext, removeContext } = React.useContext(CopilotContext);
177
- const idRef = React.useRef();
178
- React.useEffect(() => {
179
- const id = addContext(information, parentId, categories);
180
- idRef.current = id;
181
- return () => {
182
- removeContext(id);
183
- };
184
- }, [information, parentId, addContext, removeContext]);
185
- return idRef.current;
186
- }
187
- function useMakeCopilotDocumentReadable(document, categories, dependencies = []) {
188
- const { addDocumentContext, removeDocumentContext } = React.useContext(CopilotContext);
189
- const idRef = React.useRef();
190
- React.useEffect(() => {
191
- const id = addDocumentContext(document, categories);
192
- idRef.current = id;
193
- return () => {
194
- removeDocumentContext(id);
195
- };
196
- }, [categories, addDocumentContext, removeDocumentContext, ...dependencies]);
197
- return idRef.current;
198
- }
199
-
200
- exports.useCopilotChat = useCopilotChat;
201
- exports.useMakeCopilotActionable = useMakeCopilotActionable;
202
- exports.useMakeCopilotDocumentReadable = useMakeCopilotDocumentReadable;
203
- exports.useMakeCopilotReadable = useMakeCopilotReadable;
204
- //# sourceMappingURL=out.js.map
205
- //# sourceMappingURL=index.js.map
@@ -1 +0,0 @@
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","../../src/hooks/use-make-copilot-document-readable.ts"],"names":["useContext","useMemo","useEffect","useRef"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,SAAS,SAAS,kBAAkB;;;ACGpC,OAAO,WAAW;AAHlB;AA2CO,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,eA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AAE,YAAW,UAAAC,eAAc;AAF9C;AAYO,SAAS,uBACd,aACA,UACA,YACoB;AACpB,QAAM,EAAE,YAAY,cAAc,IAAIH,YAAW,cAAc;AAC/D,QAAM,QAAQG,QAAe;AAE7B,EAAAD,WAAU,MAAM;AACd,UAAM,KAAK,WAAW,aAAa,UAAU,UAAU;AACvD,UAAM,UAAU;AAEhB,WAAO,MAAM;AACX,oBAAc,EAAE;AAAA,IAClB;AAAA,EACF,GAAG,CAAC,aAAa,UAAU,YAAY,aAAa,CAAC;AAErD,SAAO,MAAM;AACf;;;AC5BA,SAAS,cAAAF,aAAY,aAAAE,YAAW,UAAAC,eAAc;AAF9C;AAaO,SAAS,+BACd,UACA,YACA,eAAsB,CAAC,GACH;AACpB,QAAM,EAAE,oBAAoB,sBAAsB,IAChDH,YAAW,cAAc;AAC3B,QAAM,QAAQG,QAAe;AAE7B,EAAAD,WAAU,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":["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 ...options,\n api: copilotApiConfigExtrapolator(copilotApiConfig).chatApiEndpoint,\n id: options.id,\n initialMessages: initialMessagesWithContext,\n experimental_onFunctionCall: getFunctionCallHandler(),\n headers: { ...copilotApiConfig.headers, ...options.headers },\n body: {\n id: options.id,\n functions: functionDescriptions,\n ...copilotApiConfig.body,\n ...options.body,\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\";\nimport { DocumentPointer } from \"../types\";\n\n/**\n * Interface for the configuration of the Copilot API.\n */\nexport interface CopilotApiConfig {\n /**\n * The endpoint for the chat API.\n */\n chatApiEndpoint: string;\n\n /**\n * additional headers to be sent with the request\n * @default {}\n * @example\n * ```\n * {\n * 'Authorization': 'Bearer your_token_here'\n * }\n * ```\n */\n headers: Record<string, string>;\n\n /**\n * Additional body params to be sent with the request\n * @default {}\n * @example\n * ```\n * {\n * 'message': 'Hello, world!'\n * }\n * ```\n */\n body: Record<string, any>;\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 get headers(): Record<string, string> {\n return {};\n }\n get body(): Record<string, any> {\n return {};\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","\"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,156 +0,0 @@
1
- 'use strict';
2
-
3
- var React = require('react');
4
- var react = require('ai/react');
5
-
6
- var __defProp = Object.defineProperty;
7
- var __defProps = Object.defineProperties;
8
- var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
9
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
10
- var __hasOwnProp = Object.prototype.hasOwnProperty;
11
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
12
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
13
- var __spreadValues = (a, b) => {
14
- for (var prop in b || (b = {}))
15
- if (__hasOwnProp.call(b, prop))
16
- __defNormalProp(a, prop, b[prop]);
17
- if (__getOwnPropSymbols)
18
- for (var prop of __getOwnPropSymbols(b)) {
19
- if (__propIsEnum.call(b, prop))
20
- __defNormalProp(a, prop, b[prop]);
21
- }
22
- return a;
23
- };
24
- var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
25
- var __objRest = (source, exclude) => {
26
- var target = {};
27
- for (var prop in source)
28
- if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
29
- target[prop] = source[prop];
30
- if (source != null && __getOwnPropSymbols)
31
- for (var prop of __getOwnPropSymbols(source)) {
32
- if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
33
- target[prop] = source[prop];
34
- }
35
- return target;
36
- };
37
- function copilotApiConfigExtrapolator(config) {
38
- return {
39
- get chatApiEndpoint() {
40
- return `${config.chatApiEndpoint}`;
41
- }
42
- };
43
- }
44
- var emptyCopilotContext = {
45
- entryPoints: {},
46
- setEntryPoint: () => {
47
- },
48
- removeEntryPoint: () => {
49
- },
50
- getChatCompletionFunctionDescriptions: () => returnAndThrowInDebug(),
51
- getFunctionCallHandler: () => returnAndThrowInDebug(),
52
- getContextString: (documents) => returnAndThrowInDebug(),
53
- addContext: () => "",
54
- removeContext: () => {
55
- },
56
- getDocumentsContext: () => returnAndThrowInDebug(),
57
- addDocumentContext: () => returnAndThrowInDebug(),
58
- removeDocumentContext: () => {
59
- },
60
- copilotApiConfig: new class {
61
- get chatApiEndpoint() {
62
- throw new Error(
63
- "Remember to wrap your app in a `<CopilotProvider> {...} </CopilotProvider>` !!!"
64
- );
65
- }
66
- get headers() {
67
- return {};
68
- }
69
- get body() {
70
- return {};
71
- }
72
- }()
73
- };
74
- var CopilotContext = React.createContext(emptyCopilotContext);
75
- function returnAndThrowInDebug(value) {
76
- throw new Error(
77
- "Remember to wrap your app in a `<CopilotProvider> {...} </CopilotProvider>` !!!"
78
- );
79
- }
80
- function useCopilotChat(_a) {
81
- var _b = _a, {
82
- makeSystemMessage
83
- } = _b, options = __objRest(_b, [
84
- "makeSystemMessage"
85
- ]);
86
- const {
87
- getContextString,
88
- getChatCompletionFunctionDescriptions,
89
- getFunctionCallHandler,
90
- copilotApiConfig
91
- } = React.useContext(CopilotContext);
92
- const systemMessage = React.useMemo(() => {
93
- const systemMessageMaker = makeSystemMessage || defaultSystemMessage;
94
- const contextString = getContextString([]);
95
- return {
96
- id: "system",
97
- content: systemMessageMaker(contextString),
98
- role: "system"
99
- };
100
- }, [getContextString, makeSystemMessage]);
101
- const initialMessagesWithContext = [systemMessage].concat(
102
- options.initialMessages || []
103
- );
104
- const functionDescriptions = React.useMemo(() => {
105
- return getChatCompletionFunctionDescriptions();
106
- }, [getChatCompletionFunctionDescriptions]);
107
- const { messages, append, reload, stop, isLoading, input, setInput } = react.useChat(__spreadProps(__spreadValues({}, options), {
108
- api: copilotApiConfigExtrapolator(copilotApiConfig).chatApiEndpoint,
109
- id: options.id,
110
- initialMessages: initialMessagesWithContext,
111
- experimental_onFunctionCall: getFunctionCallHandler(),
112
- headers: __spreadValues(__spreadValues({}, copilotApiConfig.headers), options.headers),
113
- body: __spreadValues(__spreadValues({
114
- id: options.id,
115
- functions: functionDescriptions
116
- }, copilotApiConfig.body), options.body)
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
- exports.defaultSystemMessage = defaultSystemMessage;
154
- exports.useCopilotChat = useCopilotChat;
155
- //# sourceMappingURL=out.js.map
156
- //# sourceMappingURL=use-copilot-chat.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../src/hooks/use-copilot-chat.ts","../../src/context/copilot-context.tsx"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,SAAS,SAAS,kBAAkB;;;ACGpC,OAAO,WAAW;AAHlB;AA2CO,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;AA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;AAAA,EAEf,EAAC;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 ...options,\n api: copilotApiConfigExtrapolator(copilotApiConfig).chatApiEndpoint,\n id: options.id,\n initialMessages: initialMessagesWithContext,\n experimental_onFunctionCall: getFunctionCallHandler(),\n headers: { ...copilotApiConfig.headers, ...options.headers },\n body: {\n id: options.id,\n functions: functionDescriptions,\n ...copilotApiConfig.body,\n ...options.body,\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\";\nimport { DocumentPointer } from \"../types\";\n\n/**\n * Interface for the configuration of the Copilot API.\n */\nexport interface CopilotApiConfig {\n /**\n * The endpoint for the chat API.\n */\n chatApiEndpoint: string;\n\n /**\n * additional headers to be sent with the request\n * @default {}\n * @example\n * ```\n * {\n * 'Authorization': 'Bearer your_token_here'\n * }\n * ```\n */\n headers: Record<string, string>;\n\n /**\n * Additional body params to be sent with the request\n * @default {}\n * @example\n * ```\n * {\n * 'message': 'Hello, world!'\n * }\n * ```\n */\n body: Record<string, any>;\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 get headers(): Record<string, string> {\n return {};\n }\n get body(): Record<string, any> {\n return {};\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,78 +0,0 @@
1
- 'use strict';
2
-
3
- var nanoid = require('nanoid');
4
- var react = require('react');
5
-
6
- // src/hooks/use-flat-category-store.ts
7
- var useFlatCategoryStore = () => {
8
- const [elements, dispatch] = react.useReducer(
9
- flatCategoryStoreReducer,
10
- /* @__PURE__ */ new Map()
11
- );
12
- const addElement = react.useCallback(
13
- (value, categories) => {
14
- const newId = nanoid.nanoid();
15
- dispatch({
16
- type: "ADD_ELEMENT",
17
- value,
18
- id: newId,
19
- categories
20
- });
21
- return newId;
22
- },
23
- []
24
- );
25
- const removeElement = react.useCallback((id) => {
26
- dispatch({ type: "REMOVE_ELEMENT", id });
27
- }, []);
28
- const allElements = react.useCallback(
29
- (categories) => {
30
- const categoriesSet = new Set(categories);
31
- const result = [];
32
- elements.forEach((element) => {
33
- if (setsHaveIntersection(categoriesSet, element.categories)) {
34
- result.push(element.value);
35
- }
36
- });
37
- return result;
38
- },
39
- [elements]
40
- );
41
- return { addElement, removeElement, allElements };
42
- };
43
- var use_flat_category_store_default = useFlatCategoryStore;
44
- function flatCategoryStoreReducer(state, action) {
45
- switch (action.type) {
46
- case "ADD_ELEMENT": {
47
- const { value, id, categories } = action;
48
- const newElement = {
49
- id,
50
- value,
51
- categories: new Set(categories)
52
- };
53
- const newState = new Map(state);
54
- newState.set(id, newElement);
55
- return newState;
56
- }
57
- case "REMOVE_ELEMENT": {
58
- const newState = new Map(state);
59
- newState.delete(action.id);
60
- return newState;
61
- }
62
- default:
63
- return state;
64
- }
65
- }
66
- function setsHaveIntersection(setA, setB) {
67
- const [smallerSet, largerSet] = setA.size <= setB.size ? [setA, setB] : [setB, setA];
68
- for (let item of smallerSet) {
69
- if (largerSet.has(item)) {
70
- return true;
71
- }
72
- }
73
- return false;
74
- }
75
-
76
- module.exports = use_flat_category_store_default;
77
- //# sourceMappingURL=out.js.map
78
- //# sourceMappingURL=use-flat-category-store.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../src/hooks/use-flat-category-store.ts"],"names":[],"mappings":";AAAA,SAAS,cAAc;AACvB,SAAS,aAAa,kBAAkB;AAgBxC,IAAM,uBAAuB,MAAwC;AACnE,QAAM,CAAC,UAAU,QAAQ,IAAI;AAAA,IAM3B;AAAA,IACA,oBAAI,IAAsD;AAAA,EAC5D;AAEA,QAAM,aAAa;AAAA,IACjB,CAAC,OAAU,eAA8C;AACvD,YAAM,QAAQ,OAAO;AACrB,eAAS;AAAA,QACP,MAAM;AAAA,QACN;AAAA,QACA,IAAI;AAAA,QACJ;AAAA,MACF,CAAC;AACD,aAAO;AAAA,IACT;AAAA,IACA,CAAC;AAAA,EACH;AAEA,QAAM,gBAAgB,YAAY,CAAC,OAAkC;AACnE,aAAS,EAAE,MAAM,kBAAkB,GAAG,CAAC;AAAA,EACzC,GAAG,CAAC,CAAC;AAEL,QAAM,cAAc;AAAA,IAClB,CAAC,eAA8B;AAC7B,YAAM,gBAAgB,IAAI,IAAI,UAAU;AACxC,YAAM,SAAc,CAAC;AACrB,eAAS,QAAQ,CAAC,YAAY;AAC5B,YAAI,qBAAqB,eAAe,QAAQ,UAAU,GAAG;AAC3D,iBAAO,KAAK,QAAQ,KAAK;AAAA,QAC3B;AAAA,MACF,CAAC;AACD,aAAO;AAAA,IACT;AAAA,IACA,CAAC,QAAQ;AAAA,EACX;AAEA,SAAO,EAAE,YAAY,eAAe,YAAY;AAClD;AAEA,IAAO,kCAAQ;AAaf,SAAS,yBACP,OACA,QACuD;AACvD,UAAQ,OAAO,MAAM;AAAA,IACnB,KAAK,eAAe;AAClB,YAAM,EAAE,OAAO,IAAI,WAAW,IAAI;AAClC,YAAM,aAA0C;AAAA,QAC9C;AAAA,QACA;AAAA,QACA,YAAY,IAAI,IAAI,UAAU;AAAA,MAChC;AACA,YAAM,WAAW,IAAI,IAAI,KAAK;AAC9B,eAAS,IAAI,IAAI,UAAU;AAC3B,aAAO;AAAA,IACT;AAAA,IACA,KAAK,kBAAkB;AACrB,YAAM,WAAW,IAAI,IAAI,KAAK;AAC9B,eAAS,OAAO,OAAO,EAAE;AACzB,aAAO;AAAA,IACT;AAAA,IACA;AACE,aAAO;AAAA,EACX;AACF;AAEA,SAAS,qBAAwB,MAAc,MAAuB;AACpE,QAAM,CAAC,YAAY,SAAS,IAC1B,KAAK,QAAQ,KAAK,OAAO,CAAC,MAAM,IAAI,IAAI,CAAC,MAAM,IAAI;AAErD,WAAS,QAAQ,YAAY;AAC3B,QAAI,UAAU,IAAI,IAAI,GAAG;AACvB,aAAO;AAAA,IACT;AAAA,EACF;AAEA,SAAO;AACT","sourcesContent":["import { nanoid } from \"nanoid\";\nimport { useCallback, useReducer } from \"react\";\n\nexport type FlatCategoryStoreId = string;\n\nexport interface UseFlatCategoryStoreReturn<T> {\n addElement: (value: T, categories: string[]) => FlatCategoryStoreId;\n removeElement: (id: FlatCategoryStoreId) => void;\n allElements: (categories: string[]) => T[];\n}\n\ninterface FlatCategoryStoreElement<T> {\n id: FlatCategoryStoreId;\n value: T;\n categories: Set<string>;\n}\n\nconst useFlatCategoryStore = <T>(): UseFlatCategoryStoreReturn<T> => {\n const [elements, dispatch] = useReducer<\n React.Reducer<\n Map<FlatCategoryStoreId, FlatCategoryStoreElement<T>>,\n Action<T>\n >\n >(\n flatCategoryStoreReducer,\n new Map<FlatCategoryStoreId, FlatCategoryStoreElement<T>>()\n );\n\n const addElement = useCallback(\n (value: T, categories: string[]): FlatCategoryStoreId => {\n const newId = nanoid();\n dispatch({\n type: \"ADD_ELEMENT\",\n value,\n id: newId,\n categories,\n });\n return newId;\n },\n []\n );\n\n const removeElement = useCallback((id: FlatCategoryStoreId): void => {\n dispatch({ type: \"REMOVE_ELEMENT\", id });\n }, []);\n\n const allElements = useCallback(\n (categories: string[]): T[] => {\n const categoriesSet = new Set(categories);\n const result: T[] = [];\n elements.forEach((element) => {\n if (setsHaveIntersection(categoriesSet, element.categories)) {\n result.push(element.value);\n }\n });\n return result;\n },\n [elements]\n );\n\n return { addElement, removeElement, allElements };\n};\n\nexport default useFlatCategoryStore;\n\n// Action types\ntype Action<T> =\n | {\n type: \"ADD_ELEMENT\";\n value: T;\n id: FlatCategoryStoreId;\n categories: string[];\n }\n | { type: \"REMOVE_ELEMENT\"; id: FlatCategoryStoreId };\n\n// Reducer\nfunction flatCategoryStoreReducer<T>(\n state: Map<FlatCategoryStoreId, FlatCategoryStoreElement<T>>,\n action: Action<T>\n): Map<FlatCategoryStoreId, FlatCategoryStoreElement<T>> {\n switch (action.type) {\n case \"ADD_ELEMENT\": {\n const { value, id, categories } = action;\n const newElement: FlatCategoryStoreElement<T> = {\n id,\n value,\n categories: new Set(categories),\n };\n const newState = new Map(state);\n newState.set(id, newElement);\n return newState;\n }\n case \"REMOVE_ELEMENT\": {\n const newState = new Map(state);\n newState.delete(action.id);\n return newState;\n }\n default:\n return state;\n }\n}\n\nfunction setsHaveIntersection<T>(setA: Set<T>, setB: Set<T>): boolean {\n const [smallerSet, largerSet] =\n setA.size <= setB.size ? [setA, setB] : [setB, setA];\n\n for (let item of smallerSet) {\n if (largerSet.has(item)) {\n return true;\n }\n }\n\n return false;\n}\n"]}
@@ -1,67 +0,0 @@
1
- 'use strict';
2
-
3
- var React = require('react');
4
- var nanoid = require('nanoid');
5
-
6
- var emptyCopilotContext = {
7
- entryPoints: {},
8
- setEntryPoint: () => {
9
- },
10
- removeEntryPoint: () => {
11
- },
12
- getChatCompletionFunctionDescriptions: () => returnAndThrowInDebug(),
13
- getFunctionCallHandler: () => returnAndThrowInDebug(),
14
- getContextString: (documents) => returnAndThrowInDebug(),
15
- addContext: () => "",
16
- removeContext: () => {
17
- },
18
- getDocumentsContext: () => returnAndThrowInDebug(),
19
- addDocumentContext: () => returnAndThrowInDebug(),
20
- removeDocumentContext: () => {
21
- },
22
- copilotApiConfig: new class {
23
- get chatApiEndpoint() {
24
- throw new Error(
25
- "Remember to wrap your app in a `<CopilotProvider> {...} </CopilotProvider>` !!!"
26
- );
27
- }
28
- get headers() {
29
- return {};
30
- }
31
- get body() {
32
- return {};
33
- }
34
- }()
35
- };
36
- var CopilotContext = React.createContext(emptyCopilotContext);
37
- function returnAndThrowInDebug(value) {
38
- throw new Error(
39
- "Remember to wrap your app in a `<CopilotProvider> {...} </CopilotProvider>` !!!"
40
- );
41
- }
42
- function useMakeCopilotActionable(annotatedFunction, dependencies) {
43
- const idRef = React.useRef(nanoid.nanoid());
44
- const { setEntryPoint, removeEntryPoint } = React.useContext(CopilotContext);
45
- const memoizedAnnotatedFunction = React.useMemo(
46
- () => ({
47
- name: annotatedFunction.name,
48
- description: annotatedFunction.description,
49
- argumentAnnotations: annotatedFunction.argumentAnnotations,
50
- implementation: annotatedFunction.implementation
51
- }),
52
- dependencies
53
- );
54
- React.useEffect(() => {
55
- setEntryPoint(
56
- idRef.current,
57
- memoizedAnnotatedFunction
58
- );
59
- return () => {
60
- removeEntryPoint(idRef.current);
61
- };
62
- }, [memoizedAnnotatedFunction, setEntryPoint, removeEntryPoint]);
63
- }
64
-
65
- exports.useMakeCopilotActionable = useMakeCopilotActionable;
66
- //# sourceMappingURL=out.js.map
67
- //# sourceMappingURL=use-make-copilot-actionable.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../src/hooks/use-make-copilot-actionable.ts","../../src/context/copilot-context.tsx"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAEA,SAAS,QAAQ,YAAY,WAAW,eAAe;;;ACCvD,OAAO,WAAW;AAHlB;AAmFA,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,IACA,IAAI,UAAkC;AACpC,aAAO,CAAC;AAAA,IACV;AAAA,IACA,IAAI,OAA4B;AAC9B,aAAO,CAAC;AAAA,IACV;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;;;ADpHA,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","\"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\n/**\n * Interface for the configuration of the Copilot API.\n */\nexport interface CopilotApiConfig {\n /**\n * The endpoint for the chat API.\n */\n chatApiEndpoint: string;\n\n /**\n * additional headers to be sent with the request\n * @default {}\n * @example\n * ```\n * {\n * 'Authorization': 'Bearer your_token_here'\n * }\n * ```\n */\n headers: Record<string, string>;\n\n /**\n * Additional body params to be sent with the request\n * @default {}\n * @example\n * ```\n * {\n * 'message': 'Hello, world!'\n * }\n * ```\n */\n body: Record<string, any>;\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 get headers(): Record<string, string> {\n return {};\n }\n get body(): Record<string, any> {\n return {};\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"]}