@copilotkit/react-ui 1.3.15 → 1.3.16-mme-copilot-suggestions.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/dist/{chunk-3EYL2DRG.mjs → chunk-DTI5DZSK.mjs} +5 -5
  3. package/dist/{chunk-Z2UZSN3K.mjs → chunk-HGQFDT5I.mjs} +1 -1
  4. package/dist/chunk-HGQFDT5I.mjs.map +1 -0
  5. package/dist/chunk-K5JLXNIN.mjs +108 -0
  6. package/dist/chunk-K5JLXNIN.mjs.map +1 -0
  7. package/dist/{chunk-AX7CAT7V.mjs → chunk-PIK3RYPJ.mjs} +2 -2
  8. package/dist/chunk-RJ54B6VE.mjs +1 -0
  9. package/dist/{chunk-3JNDMHS4.mjs → chunk-T7DQWLTN.mjs} +2 -2
  10. package/dist/{chunk-NLIDKNNS.mjs → chunk-UYT3USIS.mjs} +5 -5
  11. package/dist/{chunk-RS6UPR5N.mjs → chunk-W3TDOLUY.mjs} +8 -8
  12. package/dist/components/chat/Chat.mjs +5 -5
  13. package/dist/components/chat/Modal.mjs +6 -6
  14. package/dist/components/chat/Popup.mjs +7 -7
  15. package/dist/components/chat/Sidebar.mjs +7 -7
  16. package/dist/components/chat/index.mjs +8 -8
  17. package/dist/components/chat/messages/RenderTextMessage.mjs +1 -1
  18. package/dist/components/dev-console/console.mjs +2 -2
  19. package/dist/components/dev-console/index.mjs +2 -2
  20. package/dist/components/index.mjs +8 -8
  21. package/dist/hooks/index.d.ts +2 -0
  22. package/dist/hooks/index.js +135 -2
  23. package/dist/hooks/index.js.map +1 -1
  24. package/dist/hooks/index.mjs +7 -3
  25. package/dist/hooks/use-copilot-chat-suggestions.d.ts +1 -3
  26. package/dist/hooks/use-copilot-chat-suggestions.js.map +1 -1
  27. package/dist/hooks/use-copilot-chat-suggestions.mjs +1 -1
  28. package/dist/hooks/use-copilot-suggestions.d.ts +79 -0
  29. package/dist/hooks/use-copilot-suggestions.js +159 -0
  30. package/dist/hooks/use-copilot-suggestions.js.map +1 -0
  31. package/dist/hooks/use-copilot-suggestions.mjs +8 -0
  32. package/dist/hooks/use-copilot-suggestions.mjs.map +1 -0
  33. package/dist/index.d.ts +2 -0
  34. package/dist/index.js +98 -2
  35. package/dist/index.js.map +1 -1
  36. package/dist/index.mjs +15 -11
  37. package/package.json +7 -7
  38. package/src/hooks/index.ts +1 -0
  39. package/src/hooks/use-copilot-chat-suggestions.tsx +1 -3
  40. package/src/hooks/use-copilot-suggestions.ts +189 -0
  41. package/dist/chunk-T26KLXLH.mjs +0 -1
  42. package/dist/chunk-Z2UZSN3K.mjs.map +0 -1
  43. /package/dist/{chunk-3EYL2DRG.mjs.map → chunk-DTI5DZSK.mjs.map} +0 -0
  44. /package/dist/{chunk-AX7CAT7V.mjs.map → chunk-PIK3RYPJ.mjs.map} +0 -0
  45. /package/dist/{chunk-T26KLXLH.mjs.map → chunk-RJ54B6VE.mjs.map} +0 -0
  46. /package/dist/{chunk-3JNDMHS4.mjs.map → chunk-T7DQWLTN.mjs.map} +0 -0
  47. /package/dist/{chunk-NLIDKNNS.mjs.map → chunk-UYT3USIS.mjs.map} +0 -0
  48. /package/dist/{chunk-RS6UPR5N.mjs.map → chunk-W3TDOLUY.mjs.map} +0 -0
@@ -1,8 +1,25 @@
1
1
  "use strict";
2
2
  var __defProp = Object.defineProperty;
3
+ var __defProps = Object.defineProperties;
3
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
4
6
  var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
8
  var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
10
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
11
+ var __spreadValues = (a, b) => {
12
+ for (var prop in b || (b = {}))
13
+ if (__hasOwnProp.call(b, prop))
14
+ __defNormalProp(a, prop, b[prop]);
15
+ if (__getOwnPropSymbols)
16
+ for (var prop of __getOwnPropSymbols(b)) {
17
+ if (__propIsEnum.call(b, prop))
18
+ __defNormalProp(a, prop, b[prop]);
19
+ }
20
+ return a;
21
+ };
22
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
6
23
  var __export = (target, all) => {
7
24
  for (var name in all)
8
25
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -16,11 +33,32 @@ var __copyProps = (to, from, except, desc) => {
16
33
  return to;
17
34
  };
18
35
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
36
+ var __async = (__this, __arguments, generator) => {
37
+ return new Promise((resolve, reject) => {
38
+ var fulfilled = (value) => {
39
+ try {
40
+ step(generator.next(value));
41
+ } catch (e) {
42
+ reject(e);
43
+ }
44
+ };
45
+ var rejected = (value) => {
46
+ try {
47
+ step(generator.throw(value));
48
+ } catch (e) {
49
+ reject(e);
50
+ }
51
+ };
52
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
53
+ step((generator = generator.apply(__this, __arguments)).next());
54
+ });
55
+ };
19
56
 
20
57
  // src/hooks/index.ts
21
58
  var hooks_exports = {};
22
59
  __export(hooks_exports, {
23
- useCopilotChatSuggestions: () => useCopilotChatSuggestions
60
+ useCopilotChatSuggestions: () => useCopilotChatSuggestions,
61
+ useCopilotSuggestions: () => useCopilotSuggestions
24
62
  });
25
63
  module.exports = __toCommonJS(hooks_exports);
26
64
 
@@ -48,8 +86,103 @@ function useCopilotChatSuggestions({
48
86
  };
49
87
  }, [...dependencies, instructions, minSuggestions, maxSuggestions, className]);
50
88
  }
89
+
90
+ // src/hooks/use-copilot-suggestions.ts
91
+ var import_react2 = require("react");
92
+ var import_react_core2 = require("@copilotkit/react-core");
93
+ var import_runtime_client_gql = require("@copilotkit/runtime-client-gql");
94
+ function useCopilotSuggestions({
95
+ instructions,
96
+ parameters,
97
+ value,
98
+ enabled = true,
99
+ debounceTime = 1e3
100
+ }, dependencies = []) {
101
+ const suggestionsAbortControllerRef = (0, import_react2.useRef)(null);
102
+ const debounceTimerRef = (0, import_react2.useRef)();
103
+ const [suggestions, setSuggestions] = (0, import_react2.useState)({
104
+ suggestions: void 0,
105
+ isAvailable: false,
106
+ isLoading: false
107
+ });
108
+ const isFirstRunRef = (0, import_react2.useRef)(true);
109
+ const abortSuggestions = (0, import_react2.useCallback)(() => {
110
+ var _a;
111
+ (_a = suggestionsAbortControllerRef.current) == null ? void 0 : _a.abort();
112
+ suggestionsAbortControllerRef.current = null;
113
+ }, []);
114
+ const generalContext = (0, import_react_core2.useCopilotContext)();
115
+ const messagesContext = (0, import_react_core2.useCopilotMessagesContext)();
116
+ const context = __spreadValues(__spreadValues({}, generalContext), messagesContext);
117
+ (0, import_react2.useEffect)(() => {
118
+ abortSuggestions();
119
+ if (!enabled) {
120
+ setSuggestions({ suggestions: void 0, isAvailable: false, isLoading: false });
121
+ return;
122
+ }
123
+ if (JSON.stringify(value) === JSON.stringify(suggestions.suggestions)) {
124
+ return;
125
+ }
126
+ debounceTimerRef.current = setTimeout(
127
+ () => __async(this, null, function* () {
128
+ isFirstRunRef.current = false;
129
+ suggestionsAbortControllerRef.current = new AbortController();
130
+ setSuggestions(__spreadProps(__spreadValues({}, suggestions), { isLoading: true }));
131
+ yield reloadSuggestions(
132
+ context,
133
+ instructions,
134
+ parameters,
135
+ value,
136
+ suggestionsAbortControllerRef,
137
+ setSuggestions
138
+ );
139
+ }),
140
+ isFirstRunRef.current === true ? 0 : debounceTime
141
+ );
142
+ return () => {
143
+ clearTimeout(debounceTimerRef.current);
144
+ };
145
+ }, [
146
+ instructions,
147
+ JSON.stringify(parameters),
148
+ JSON.stringify(value),
149
+ enabled,
150
+ debounceTime,
151
+ ...dependencies
152
+ ]);
153
+ return suggestions;
154
+ }
155
+ function reloadSuggestions(context, instructions, parameters, value, abortControllerRef, setSuggestions) {
156
+ return __async(this, null, function* () {
157
+ const abortController = abortControllerRef.current;
158
+ let fullInstructions = `It's your task to generate suggestions based on the application context.`;
159
+ if (instructions) {
160
+ fullInstructions += `
161
+
162
+ In addition, follow these specific instructions: ${instructions}`;
163
+ }
164
+ if (value) {
165
+ fullInstructions += `
166
+
167
+ The current value of the parameter is: ${JSON.stringify(value)}.If it makes sense to complete the existing data, i.e. expand string values, add new elements to arrays, etc., do so. Otherwise, generate a new value.`;
168
+ }
169
+ yield (0, import_react_core2.extract)({
170
+ context,
171
+ instructions: fullInstructions,
172
+ parameters,
173
+ abortSignal: abortController == null ? void 0 : abortController.signal,
174
+ requestType: import_runtime_client_gql.CopilotRequestType.Task,
175
+ stream({ args, status }) {
176
+ if (status === "complete") {
177
+ setSuggestions({ suggestions: args, isAvailable: true, isLoading: false });
178
+ }
179
+ }
180
+ });
181
+ });
182
+ }
51
183
  // Annotate the CommonJS export names for ESM import in node:
52
184
  0 && (module.exports = {
53
- useCopilotChatSuggestions
185
+ useCopilotChatSuggestions,
186
+ useCopilotSuggestions
54
187
  });
55
188
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/hooks/index.ts","../../src/hooks/use-copilot-chat-suggestions.tsx"],"sourcesContent":["export * from \"./use-copilot-chat-suggestions\";\n","/**\n * <Callout type=\"warning\">\n * useCopilotChatSuggestions is experimental. The interface is not final and\n * can change without notice.\n * </Callout>\n *\n * `useCopilotReadable` is a React hook that provides app-state and other information\n * to the Copilot. Optionally, the hook can also handle hierarchical state within your\n * application, passing these parent-child relationships to the Copilot.\n *\n * <br/>\n * <img src=\"/images/use-copilot-chat-suggestions/use-copilot-chat-suggestions.gif\" width=\"500\" />\n *\n * ## Usage\n *\n * ### Install Dependencies\n *\n * This component is part of the [@copilotkit/react-ui](https://npmjs.com/package/@copilotkit/react-ui) package.\n *\n * ```shell npm2yarn \\\"@copilotkit/react-ui\"\\\n * npm install @copilotkit/react-core @copilotkit/react-ui\n * ```\n *\n * ### Simple Usage\n *\n * ```tsx\n * import { useCopilotChatSuggestions } from \"@copilotkit/react-ui\";\n *\n * export function MyComponent() {\n * const [employees, setEmployees] = useState([]);\n *\n * useCopilotChatSuggestions({\n * instructions: `The following employees are on duty: ${JSON.stringify(employees)}`,\n * });\n * }\n * ```\n *\n * ### Dependency Management\n *\n * ```tsx\n * import { useCopilotChatSuggestions } from \"@copilotkit/react-ui\";\n *\n * export function MyComponent() {\n * useCopilotChatSuggestions(\n * {\n * instructions: \"Suggest the most relevant next actions.\",\n * },\n * [appState],\n * );\n * }\n * ```\n *\n * In the example above, the suggestions are generated based on the given instructions.\n * The hook monitors `appState`, and updates suggestions accordingly whenever it changes.\n *\n * ### Behavior and Lifecycle\n *\n * The hook registers the configuration with the chat context upon component mount and\n * removes it on unmount, ensuring a clean and efficient lifecycle management.\n */\n\nimport { useEffect } from \"react\";\nimport { useCopilotContext } from \"@copilotkit/react-core\";\nimport { randomId } from \"@copilotkit/shared\";\n\ninterface UseCopilotChatSuggestionsConfiguration {\n /**\n * A prompt or instructions for the GPT to generate suggestions.\n */\n instructions: string;\n /**\n * The minimum number of suggestions to generate. Defaults to `1`.\n * @default 1\n */\n minSuggestions?: number;\n /**\n * The maximum number of suggestions to generate. Defaults to `3`.\n * @default 1\n */\n maxSuggestions?: number;\n /**\n * An optional class name to apply to the suggestions.\n */\n className?: string;\n}\n\nexport function useCopilotChatSuggestions(\n {\n instructions,\n className,\n minSuggestions = 1,\n maxSuggestions = 3,\n }: UseCopilotChatSuggestionsConfiguration,\n dependencies: any[] = [],\n) {\n const context = useCopilotContext();\n\n useEffect(() => {\n const id = randomId();\n\n context.addChatSuggestionConfiguration(id, {\n instructions,\n minSuggestions,\n maxSuggestions,\n className,\n });\n\n return () => {\n context.removeChatSuggestionConfiguration(id);\n };\n }, [...dependencies, instructions, minSuggestions, maxSuggestions, className]);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;AC6DA,mBAA0B;AAC1B,wBAAkC;AAClC,oBAAyB;AAuBlB,SAAS,0BACd;AAAA,EACE;AAAA,EACA;AAAA,EACA,iBAAiB;AAAA,EACjB,iBAAiB;AACnB,GACA,eAAsB,CAAC,GACvB;AACA,QAAM,cAAU,qCAAkB;AAElC,8BAAU,MAAM;AACd,UAAM,SAAK,wBAAS;AAEpB,YAAQ,+BAA+B,IAAI;AAAA,MACzC;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACF,CAAC;AAED,WAAO,MAAM;AACX,cAAQ,kCAAkC,EAAE;AAAA,IAC9C;AAAA,EACF,GAAG,CAAC,GAAG,cAAc,cAAc,gBAAgB,gBAAgB,SAAS,CAAC;AAC/E;","names":[]}
1
+ {"version":3,"sources":["../../src/hooks/index.ts","../../src/hooks/use-copilot-chat-suggestions.tsx","../../src/hooks/use-copilot-suggestions.ts"],"sourcesContent":["export * from \"./use-copilot-chat-suggestions\";\nexport * from \"./use-copilot-suggestions\";\n","/**\n * <Callout type=\"warning\">\n * useCopilotChatSuggestions is experimental. The interface is not final and\n * can change without notice.\n * </Callout>\n *\n * `useCopilotChatSuggestions` is a React hook that provides auto-suggestions in the Copilot chat.\n *\n * <br/>\n * <img src=\"/images/use-copilot-chat-suggestions/use-copilot-chat-suggestions.gif\" width=\"500\" />\n *\n * ## Usage\n *\n * ### Install Dependencies\n *\n * This component is part of the [@copilotkit/react-ui](https://npmjs.com/package/@copilotkit/react-ui) package.\n *\n * ```shell npm2yarn \\\"@copilotkit/react-ui\"\\\n * npm install @copilotkit/react-core @copilotkit/react-ui\n * ```\n *\n * ### Simple Usage\n *\n * ```tsx\n * import { useCopilotChatSuggestions } from \"@copilotkit/react-ui\";\n *\n * export function MyComponent() {\n * const [employees, setEmployees] = useState([]);\n *\n * useCopilotChatSuggestions({\n * instructions: `The following employees are on duty: ${JSON.stringify(employees)}`,\n * });\n * }\n * ```\n *\n * ### Dependency Management\n *\n * ```tsx\n * import { useCopilotChatSuggestions } from \"@copilotkit/react-ui\";\n *\n * export function MyComponent() {\n * useCopilotChatSuggestions(\n * {\n * instructions: \"Suggest the most relevant next actions.\",\n * },\n * [appState],\n * );\n * }\n * ```\n *\n * In the example above, the suggestions are generated based on the given instructions.\n * The hook monitors `appState`, and updates suggestions accordingly whenever it changes.\n *\n * ### Behavior and Lifecycle\n *\n * The hook registers the configuration with the chat context upon component mount and\n * removes it on unmount, ensuring a clean and efficient lifecycle management.\n */\n\nimport { useEffect } from \"react\";\nimport { useCopilotContext } from \"@copilotkit/react-core\";\nimport { randomId } from \"@copilotkit/shared\";\n\ninterface UseCopilotChatSuggestionsConfiguration {\n /**\n * A prompt or instructions for the GPT to generate suggestions.\n */\n instructions: string;\n /**\n * The minimum number of suggestions to generate. Defaults to `1`.\n * @default 1\n */\n minSuggestions?: number;\n /**\n * The maximum number of suggestions to generate. Defaults to `3`.\n * @default 1\n */\n maxSuggestions?: number;\n /**\n * An optional class name to apply to the suggestions.\n */\n className?: string;\n}\n\nexport function useCopilotChatSuggestions(\n {\n instructions,\n className,\n minSuggestions = 1,\n maxSuggestions = 3,\n }: UseCopilotChatSuggestionsConfiguration,\n dependencies: any[] = [],\n) {\n const context = useCopilotContext();\n\n useEffect(() => {\n const id = randomId();\n\n context.addChatSuggestionConfiguration(id, {\n instructions,\n minSuggestions,\n maxSuggestions,\n className,\n });\n\n return () => {\n context.removeChatSuggestionConfiguration(id);\n };\n }, [...dependencies, instructions, minSuggestions, maxSuggestions, className]);\n}\n","/**\n * <Callout type=\"warning\">\n * useCopilotSuggestions is experimental. The interface is not final and\n * can change without notice.\n * </Callout>\n *\n * `useCopilotSuggestions` is a React hook that provides auto-suggestions that can be added to any\n * UI component.\n *\n * ## Usage\n *\n * ### Install Dependencies\n *\n * This component is part of the [@copilotkit/react-ui](https://npmjs.com/package/@copilotkit/react-ui) package.\n *\n * ```shell npm2yarn \\\"@copilotkit/react-ui\"\\\n * npm install @copilotkit/react-core @copilotkit/react-ui\n * ```\n *\n * ### Simple Usage\n *\n * ```tsx\n * TODO\n * ```\n *\n * ### Dependency Management\n *\n * ```tsx\n * TODO\n * }\n * ```\n *\n * In the example above, the suggestions are generated based on the given instructions.\n * The hook monitors `appState`, and updates suggestions accordingly whenever it changes.\n *\n * ### Behavior and Lifecycle\n *\n * The hook registers the configuration with the chat context upon component mount and\n * removes it on unmount, ensuring a clean and efficient lifecycle management.\n */\n\nimport { useEffect, useState, useRef, useCallback } from \"react\";\nimport {\n CopilotContextParams,\n CopilotMessagesContextParams,\n extract,\n useCopilotContext,\n useCopilotMessagesContext,\n} from \"@copilotkit/react-core\";\nimport { MappedParameterTypes, Parameter, randomId } from \"@copilotkit/shared\";\nimport { CopilotRequestType } from \"@copilotkit/runtime-client-gql\";\n\nexport interface UseCopilotSuggestionsConfiguration<T extends Parameter[] = []> {\n /**\n * A prompt or instructions for the GPT to generate suggestions.\n */\n instructions?: string;\n\n /**\n * The data to pass to the suggestions.\n */\n parameters: T;\n\n /**\n * The current value of the parameter.\n */\n value?: Partial<MappedParameterTypes<T>>;\n\n /**\n * Whether the suggestions are enabled.\n * @default true\n */\n enabled?: boolean;\n\n /**\n * The debounce time in milliseconds.\n * @default 1000\n */\n debounceTime?: number;\n}\nexport type SuggestionsResult<T extends Parameter[]> =\n | { suggestions: undefined; isAvailable: false; isLoading: boolean }\n | { suggestions: MappedParameterTypes<T>; isAvailable: true; isLoading: boolean };\n\nexport function useCopilotSuggestions<const T extends Parameter[]>(\n {\n instructions,\n parameters,\n value,\n enabled = true,\n debounceTime = 1000,\n }: UseCopilotSuggestionsConfiguration<T>,\n dependencies: any[] = [],\n): SuggestionsResult<T> {\n const suggestionsAbortControllerRef = useRef<AbortController | null>(null);\n const debounceTimerRef = useRef<any>();\n const [suggestions, setSuggestions] = useState<SuggestionsResult<T>>({\n suggestions: undefined,\n isAvailable: false,\n isLoading: false,\n });\n const isFirstRunRef = useRef(true);\n\n const abortSuggestions = useCallback(() => {\n suggestionsAbortControllerRef.current?.abort();\n suggestionsAbortControllerRef.current = null;\n }, []);\n\n const generalContext = useCopilotContext();\n const messagesContext = useCopilotMessagesContext();\n const context = { ...generalContext, ...messagesContext };\n\n useEffect(() => {\n abortSuggestions();\n if (!enabled) {\n setSuggestions({ suggestions: undefined, isAvailable: false, isLoading: false });\n return;\n }\n\n // if value is the same as the last suggestions, don't reload\n if (JSON.stringify(value) === JSON.stringify(suggestions.suggestions)) {\n return;\n }\n\n debounceTimerRef.current = setTimeout(\n async () => {\n isFirstRunRef.current = false;\n suggestionsAbortControllerRef.current = new AbortController();\n setSuggestions({ ...suggestions, isLoading: true });\n await reloadSuggestions(\n context,\n instructions,\n parameters,\n value,\n suggestionsAbortControllerRef,\n setSuggestions,\n );\n },\n isFirstRunRef.current === true ? 0 : debounceTime,\n );\n\n return () => {\n clearTimeout(debounceTimerRef.current);\n };\n }, [\n instructions,\n JSON.stringify(parameters),\n JSON.stringify(value),\n enabled,\n debounceTime,\n ...dependencies,\n ]);\n\n return suggestions;\n}\n\nasync function reloadSuggestions(\n context: CopilotContextParams & CopilotMessagesContextParams,\n instructions: string | undefined,\n parameters: Parameter[],\n value: any,\n abortControllerRef: React.MutableRefObject<AbortController | null>,\n setSuggestions: (suggestions: any) => void,\n) {\n const abortController = abortControllerRef.current;\n let fullInstructions = `It's your task to generate suggestions based on the application context.`;\n if (instructions) {\n fullInstructions += `\\n\\nIn addition, follow these specific instructions: ${instructions}`;\n }\n if (value) {\n fullInstructions +=\n `\\n\\nThe current value of the parameter is: ${JSON.stringify(value)}.` +\n `If it makes sense to complete the existing data, i.e. expand string values, add new elements to arrays, etc., do so. ` +\n `Otherwise, generate a new value.`;\n }\n\n await extract({\n context,\n instructions: fullInstructions,\n parameters,\n abortSignal: abortController?.signal,\n requestType: CopilotRequestType.Task,\n stream({ args, status }) {\n if (status === \"complete\") {\n setSuggestions({ suggestions: args, isAvailable: true, isLoading: false });\n }\n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react_core"]}
@@ -1,9 +1,13 @@
1
- import "../chunk-T26KLXLH.mjs";
1
+ import "../chunk-RJ54B6VE.mjs";
2
2
  import {
3
3
  useCopilotChatSuggestions
4
- } from "../chunk-Z2UZSN3K.mjs";
4
+ } from "../chunk-HGQFDT5I.mjs";
5
+ import {
6
+ useCopilotSuggestions
7
+ } from "../chunk-K5JLXNIN.mjs";
5
8
  import "../chunk-MRXNTQOX.mjs";
6
9
  export {
7
- useCopilotChatSuggestions
10
+ useCopilotChatSuggestions,
11
+ useCopilotSuggestions
8
12
  };
9
13
  //# sourceMappingURL=index.mjs.map
@@ -4,9 +4,7 @@
4
4
  * can change without notice.
5
5
  * </Callout>
6
6
  *
7
- * `useCopilotReadable` is a React hook that provides app-state and other information
8
- * to the Copilot. Optionally, the hook can also handle hierarchical state within your
9
- * application, passing these parent-child relationships to the Copilot.
7
+ * `useCopilotChatSuggestions` is a React hook that provides auto-suggestions in the Copilot chat.
10
8
  *
11
9
  * <br/>
12
10
  * <img src="/images/use-copilot-chat-suggestions/use-copilot-chat-suggestions.gif" width="500" />
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/hooks/use-copilot-chat-suggestions.tsx"],"sourcesContent":["/**\n * <Callout type=\"warning\">\n * useCopilotChatSuggestions is experimental. The interface is not final and\n * can change without notice.\n * </Callout>\n *\n * `useCopilotReadable` is a React hook that provides app-state and other information\n * to the Copilot. Optionally, the hook can also handle hierarchical state within your\n * application, passing these parent-child relationships to the Copilot.\n *\n * <br/>\n * <img src=\"/images/use-copilot-chat-suggestions/use-copilot-chat-suggestions.gif\" width=\"500\" />\n *\n * ## Usage\n *\n * ### Install Dependencies\n *\n * This component is part of the [@copilotkit/react-ui](https://npmjs.com/package/@copilotkit/react-ui) package.\n *\n * ```shell npm2yarn \\\"@copilotkit/react-ui\"\\\n * npm install @copilotkit/react-core @copilotkit/react-ui\n * ```\n *\n * ### Simple Usage\n *\n * ```tsx\n * import { useCopilotChatSuggestions } from \"@copilotkit/react-ui\";\n *\n * export function MyComponent() {\n * const [employees, setEmployees] = useState([]);\n *\n * useCopilotChatSuggestions({\n * instructions: `The following employees are on duty: ${JSON.stringify(employees)}`,\n * });\n * }\n * ```\n *\n * ### Dependency Management\n *\n * ```tsx\n * import { useCopilotChatSuggestions } from \"@copilotkit/react-ui\";\n *\n * export function MyComponent() {\n * useCopilotChatSuggestions(\n * {\n * instructions: \"Suggest the most relevant next actions.\",\n * },\n * [appState],\n * );\n * }\n * ```\n *\n * In the example above, the suggestions are generated based on the given instructions.\n * The hook monitors `appState`, and updates suggestions accordingly whenever it changes.\n *\n * ### Behavior and Lifecycle\n *\n * The hook registers the configuration with the chat context upon component mount and\n * removes it on unmount, ensuring a clean and efficient lifecycle management.\n */\n\nimport { useEffect } from \"react\";\nimport { useCopilotContext } from \"@copilotkit/react-core\";\nimport { randomId } from \"@copilotkit/shared\";\n\ninterface UseCopilotChatSuggestionsConfiguration {\n /**\n * A prompt or instructions for the GPT to generate suggestions.\n */\n instructions: string;\n /**\n * The minimum number of suggestions to generate. Defaults to `1`.\n * @default 1\n */\n minSuggestions?: number;\n /**\n * The maximum number of suggestions to generate. Defaults to `3`.\n * @default 1\n */\n maxSuggestions?: number;\n /**\n * An optional class name to apply to the suggestions.\n */\n className?: string;\n}\n\nexport function useCopilotChatSuggestions(\n {\n instructions,\n className,\n minSuggestions = 1,\n maxSuggestions = 3,\n }: UseCopilotChatSuggestionsConfiguration,\n dependencies: any[] = [],\n) {\n const context = useCopilotContext();\n\n useEffect(() => {\n const id = randomId();\n\n context.addChatSuggestionConfiguration(id, {\n instructions,\n minSuggestions,\n maxSuggestions,\n className,\n });\n\n return () => {\n context.removeChatSuggestionConfiguration(id);\n };\n }, [...dependencies, instructions, minSuggestions, maxSuggestions, className]);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AA6DA,mBAA0B;AAC1B,wBAAkC;AAClC,oBAAyB;AAuBlB,SAAS,0BACd;AAAA,EACE;AAAA,EACA;AAAA,EACA,iBAAiB;AAAA,EACjB,iBAAiB;AACnB,GACA,eAAsB,CAAC,GACvB;AACA,QAAM,cAAU,qCAAkB;AAElC,8BAAU,MAAM;AACd,UAAM,SAAK,wBAAS;AAEpB,YAAQ,+BAA+B,IAAI;AAAA,MACzC;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACF,CAAC;AAED,WAAO,MAAM;AACX,cAAQ,kCAAkC,EAAE;AAAA,IAC9C;AAAA,EACF,GAAG,CAAC,GAAG,cAAc,cAAc,gBAAgB,gBAAgB,SAAS,CAAC;AAC/E;","names":[]}
1
+ {"version":3,"sources":["../../src/hooks/use-copilot-chat-suggestions.tsx"],"sourcesContent":["/**\n * <Callout type=\"warning\">\n * useCopilotChatSuggestions is experimental. The interface is not final and\n * can change without notice.\n * </Callout>\n *\n * `useCopilotChatSuggestions` is a React hook that provides auto-suggestions in the Copilot chat.\n *\n * <br/>\n * <img src=\"/images/use-copilot-chat-suggestions/use-copilot-chat-suggestions.gif\" width=\"500\" />\n *\n * ## Usage\n *\n * ### Install Dependencies\n *\n * This component is part of the [@copilotkit/react-ui](https://npmjs.com/package/@copilotkit/react-ui) package.\n *\n * ```shell npm2yarn \\\"@copilotkit/react-ui\"\\\n * npm install @copilotkit/react-core @copilotkit/react-ui\n * ```\n *\n * ### Simple Usage\n *\n * ```tsx\n * import { useCopilotChatSuggestions } from \"@copilotkit/react-ui\";\n *\n * export function MyComponent() {\n * const [employees, setEmployees] = useState([]);\n *\n * useCopilotChatSuggestions({\n * instructions: `The following employees are on duty: ${JSON.stringify(employees)}`,\n * });\n * }\n * ```\n *\n * ### Dependency Management\n *\n * ```tsx\n * import { useCopilotChatSuggestions } from \"@copilotkit/react-ui\";\n *\n * export function MyComponent() {\n * useCopilotChatSuggestions(\n * {\n * instructions: \"Suggest the most relevant next actions.\",\n * },\n * [appState],\n * );\n * }\n * ```\n *\n * In the example above, the suggestions are generated based on the given instructions.\n * The hook monitors `appState`, and updates suggestions accordingly whenever it changes.\n *\n * ### Behavior and Lifecycle\n *\n * The hook registers the configuration with the chat context upon component mount and\n * removes it on unmount, ensuring a clean and efficient lifecycle management.\n */\n\nimport { useEffect } from \"react\";\nimport { useCopilotContext } from \"@copilotkit/react-core\";\nimport { randomId } from \"@copilotkit/shared\";\n\ninterface UseCopilotChatSuggestionsConfiguration {\n /**\n * A prompt or instructions for the GPT to generate suggestions.\n */\n instructions: string;\n /**\n * The minimum number of suggestions to generate. Defaults to `1`.\n * @default 1\n */\n minSuggestions?: number;\n /**\n * The maximum number of suggestions to generate. Defaults to `3`.\n * @default 1\n */\n maxSuggestions?: number;\n /**\n * An optional class name to apply to the suggestions.\n */\n className?: string;\n}\n\nexport function useCopilotChatSuggestions(\n {\n instructions,\n className,\n minSuggestions = 1,\n maxSuggestions = 3,\n }: UseCopilotChatSuggestionsConfiguration,\n dependencies: any[] = [],\n) {\n const context = useCopilotContext();\n\n useEffect(() => {\n const id = randomId();\n\n context.addChatSuggestionConfiguration(id, {\n instructions,\n minSuggestions,\n maxSuggestions,\n className,\n });\n\n return () => {\n context.removeChatSuggestionConfiguration(id);\n };\n }, [...dependencies, instructions, minSuggestions, maxSuggestions, className]);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AA2DA,mBAA0B;AAC1B,wBAAkC;AAClC,oBAAyB;AAuBlB,SAAS,0BACd;AAAA,EACE;AAAA,EACA;AAAA,EACA,iBAAiB;AAAA,EACjB,iBAAiB;AACnB,GACA,eAAsB,CAAC,GACvB;AACA,QAAM,cAAU,qCAAkB;AAElC,8BAAU,MAAM;AACd,UAAM,SAAK,wBAAS;AAEpB,YAAQ,+BAA+B,IAAI;AAAA,MACzC;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACF,CAAC;AAED,WAAO,MAAM;AACX,cAAQ,kCAAkC,EAAE;AAAA,IAC9C;AAAA,EACF,GAAG,CAAC,GAAG,cAAc,cAAc,gBAAgB,gBAAgB,SAAS,CAAC;AAC/E;","names":[]}
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  useCopilotChatSuggestions
3
- } from "../chunk-Z2UZSN3K.mjs";
3
+ } from "../chunk-HGQFDT5I.mjs";
4
4
  import "../chunk-MRXNTQOX.mjs";
5
5
  export {
6
6
  useCopilotChatSuggestions
@@ -0,0 +1,79 @@
1
+ import { Parameter, MappedParameterTypes } from '@copilotkit/shared';
2
+
3
+ /**
4
+ * <Callout type="warning">
5
+ * useCopilotSuggestions is experimental. The interface is not final and
6
+ * can change without notice.
7
+ * </Callout>
8
+ *
9
+ * `useCopilotSuggestions` is a React hook that provides auto-suggestions that can be added to any
10
+ * UI component.
11
+ *
12
+ * ## Usage
13
+ *
14
+ * ### Install Dependencies
15
+ *
16
+ * This component is part of the [@copilotkit/react-ui](https://npmjs.com/package/@copilotkit/react-ui) package.
17
+ *
18
+ * ```shell npm2yarn \"@copilotkit/react-ui"\
19
+ * npm install @copilotkit/react-core @copilotkit/react-ui
20
+ * ```
21
+ *
22
+ * ### Simple Usage
23
+ *
24
+ * ```tsx
25
+ * TODO
26
+ * ```
27
+ *
28
+ * ### Dependency Management
29
+ *
30
+ * ```tsx
31
+ * TODO
32
+ * }
33
+ * ```
34
+ *
35
+ * In the example above, the suggestions are generated based on the given instructions.
36
+ * The hook monitors `appState`, and updates suggestions accordingly whenever it changes.
37
+ *
38
+ * ### Behavior and Lifecycle
39
+ *
40
+ * The hook registers the configuration with the chat context upon component mount and
41
+ * removes it on unmount, ensuring a clean and efficient lifecycle management.
42
+ */
43
+
44
+ interface UseCopilotSuggestionsConfiguration<T extends Parameter[] = []> {
45
+ /**
46
+ * A prompt or instructions for the GPT to generate suggestions.
47
+ */
48
+ instructions?: string;
49
+ /**
50
+ * The data to pass to the suggestions.
51
+ */
52
+ parameters: T;
53
+ /**
54
+ * The current value of the parameter.
55
+ */
56
+ value?: Partial<MappedParameterTypes<T>>;
57
+ /**
58
+ * Whether the suggestions are enabled.
59
+ * @default true
60
+ */
61
+ enabled?: boolean;
62
+ /**
63
+ * The debounce time in milliseconds.
64
+ * @default 1000
65
+ */
66
+ debounceTime?: number;
67
+ }
68
+ type SuggestionsResult<T extends Parameter[]> = {
69
+ suggestions: undefined;
70
+ isAvailable: false;
71
+ isLoading: boolean;
72
+ } | {
73
+ suggestions: MappedParameterTypes<T>;
74
+ isAvailable: true;
75
+ isLoading: boolean;
76
+ };
77
+ declare function useCopilotSuggestions<const T extends Parameter[]>({ instructions, parameters, value, enabled, debounceTime, }: UseCopilotSuggestionsConfiguration<T>, dependencies?: any[]): SuggestionsResult<T>;
78
+
79
+ export { SuggestionsResult, UseCopilotSuggestionsConfiguration, useCopilotSuggestions };
@@ -0,0 +1,159 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __defProps = Object.defineProperties;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
8
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
10
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
11
+ var __spreadValues = (a, b) => {
12
+ for (var prop in b || (b = {}))
13
+ if (__hasOwnProp.call(b, prop))
14
+ __defNormalProp(a, prop, b[prop]);
15
+ if (__getOwnPropSymbols)
16
+ for (var prop of __getOwnPropSymbols(b)) {
17
+ if (__propIsEnum.call(b, prop))
18
+ __defNormalProp(a, prop, b[prop]);
19
+ }
20
+ return a;
21
+ };
22
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
23
+ var __export = (target, all) => {
24
+ for (var name in all)
25
+ __defProp(target, name, { get: all[name], enumerable: true });
26
+ };
27
+ var __copyProps = (to, from, except, desc) => {
28
+ if (from && typeof from === "object" || typeof from === "function") {
29
+ for (let key of __getOwnPropNames(from))
30
+ if (!__hasOwnProp.call(to, key) && key !== except)
31
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
32
+ }
33
+ return to;
34
+ };
35
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
36
+ var __async = (__this, __arguments, generator) => {
37
+ return new Promise((resolve, reject) => {
38
+ var fulfilled = (value) => {
39
+ try {
40
+ step(generator.next(value));
41
+ } catch (e) {
42
+ reject(e);
43
+ }
44
+ };
45
+ var rejected = (value) => {
46
+ try {
47
+ step(generator.throw(value));
48
+ } catch (e) {
49
+ reject(e);
50
+ }
51
+ };
52
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
53
+ step((generator = generator.apply(__this, __arguments)).next());
54
+ });
55
+ };
56
+
57
+ // src/hooks/use-copilot-suggestions.ts
58
+ var use_copilot_suggestions_exports = {};
59
+ __export(use_copilot_suggestions_exports, {
60
+ useCopilotSuggestions: () => useCopilotSuggestions
61
+ });
62
+ module.exports = __toCommonJS(use_copilot_suggestions_exports);
63
+ var import_react = require("react");
64
+ var import_react_core = require("@copilotkit/react-core");
65
+ var import_runtime_client_gql = require("@copilotkit/runtime-client-gql");
66
+ function useCopilotSuggestions({
67
+ instructions,
68
+ parameters,
69
+ value,
70
+ enabled = true,
71
+ debounceTime = 1e3
72
+ }, dependencies = []) {
73
+ const suggestionsAbortControllerRef = (0, import_react.useRef)(null);
74
+ const debounceTimerRef = (0, import_react.useRef)();
75
+ const [suggestions, setSuggestions] = (0, import_react.useState)({
76
+ suggestions: void 0,
77
+ isAvailable: false,
78
+ isLoading: false
79
+ });
80
+ const isFirstRunRef = (0, import_react.useRef)(true);
81
+ const abortSuggestions = (0, import_react.useCallback)(() => {
82
+ var _a;
83
+ (_a = suggestionsAbortControllerRef.current) == null ? void 0 : _a.abort();
84
+ suggestionsAbortControllerRef.current = null;
85
+ }, []);
86
+ const generalContext = (0, import_react_core.useCopilotContext)();
87
+ const messagesContext = (0, import_react_core.useCopilotMessagesContext)();
88
+ const context = __spreadValues(__spreadValues({}, generalContext), messagesContext);
89
+ (0, import_react.useEffect)(() => {
90
+ abortSuggestions();
91
+ if (!enabled) {
92
+ setSuggestions({ suggestions: void 0, isAvailable: false, isLoading: false });
93
+ return;
94
+ }
95
+ if (JSON.stringify(value) === JSON.stringify(suggestions.suggestions)) {
96
+ return;
97
+ }
98
+ debounceTimerRef.current = setTimeout(
99
+ () => __async(this, null, function* () {
100
+ isFirstRunRef.current = false;
101
+ suggestionsAbortControllerRef.current = new AbortController();
102
+ setSuggestions(__spreadProps(__spreadValues({}, suggestions), { isLoading: true }));
103
+ yield reloadSuggestions(
104
+ context,
105
+ instructions,
106
+ parameters,
107
+ value,
108
+ suggestionsAbortControllerRef,
109
+ setSuggestions
110
+ );
111
+ }),
112
+ isFirstRunRef.current === true ? 0 : debounceTime
113
+ );
114
+ return () => {
115
+ clearTimeout(debounceTimerRef.current);
116
+ };
117
+ }, [
118
+ instructions,
119
+ JSON.stringify(parameters),
120
+ JSON.stringify(value),
121
+ enabled,
122
+ debounceTime,
123
+ ...dependencies
124
+ ]);
125
+ return suggestions;
126
+ }
127
+ function reloadSuggestions(context, instructions, parameters, value, abortControllerRef, setSuggestions) {
128
+ return __async(this, null, function* () {
129
+ const abortController = abortControllerRef.current;
130
+ let fullInstructions = `It's your task to generate suggestions based on the application context.`;
131
+ if (instructions) {
132
+ fullInstructions += `
133
+
134
+ In addition, follow these specific instructions: ${instructions}`;
135
+ }
136
+ if (value) {
137
+ fullInstructions += `
138
+
139
+ The current value of the parameter is: ${JSON.stringify(value)}.If it makes sense to complete the existing data, i.e. expand string values, add new elements to arrays, etc., do so. Otherwise, generate a new value.`;
140
+ }
141
+ yield (0, import_react_core.extract)({
142
+ context,
143
+ instructions: fullInstructions,
144
+ parameters,
145
+ abortSignal: abortController == null ? void 0 : abortController.signal,
146
+ requestType: import_runtime_client_gql.CopilotRequestType.Task,
147
+ stream({ args, status }) {
148
+ if (status === "complete") {
149
+ setSuggestions({ suggestions: args, isAvailable: true, isLoading: false });
150
+ }
151
+ }
152
+ });
153
+ });
154
+ }
155
+ // Annotate the CommonJS export names for ESM import in node:
156
+ 0 && (module.exports = {
157
+ useCopilotSuggestions
158
+ });
159
+ //# sourceMappingURL=use-copilot-suggestions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/hooks/use-copilot-suggestions.ts"],"sourcesContent":["/**\n * <Callout type=\"warning\">\n * useCopilotSuggestions is experimental. The interface is not final and\n * can change without notice.\n * </Callout>\n *\n * `useCopilotSuggestions` is a React hook that provides auto-suggestions that can be added to any\n * UI component.\n *\n * ## Usage\n *\n * ### Install Dependencies\n *\n * This component is part of the [@copilotkit/react-ui](https://npmjs.com/package/@copilotkit/react-ui) package.\n *\n * ```shell npm2yarn \\\"@copilotkit/react-ui\"\\\n * npm install @copilotkit/react-core @copilotkit/react-ui\n * ```\n *\n * ### Simple Usage\n *\n * ```tsx\n * TODO\n * ```\n *\n * ### Dependency Management\n *\n * ```tsx\n * TODO\n * }\n * ```\n *\n * In the example above, the suggestions are generated based on the given instructions.\n * The hook monitors `appState`, and updates suggestions accordingly whenever it changes.\n *\n * ### Behavior and Lifecycle\n *\n * The hook registers the configuration with the chat context upon component mount and\n * removes it on unmount, ensuring a clean and efficient lifecycle management.\n */\n\nimport { useEffect, useState, useRef, useCallback } from \"react\";\nimport {\n CopilotContextParams,\n CopilotMessagesContextParams,\n extract,\n useCopilotContext,\n useCopilotMessagesContext,\n} from \"@copilotkit/react-core\";\nimport { MappedParameterTypes, Parameter, randomId } from \"@copilotkit/shared\";\nimport { CopilotRequestType } from \"@copilotkit/runtime-client-gql\";\n\nexport interface UseCopilotSuggestionsConfiguration<T extends Parameter[] = []> {\n /**\n * A prompt or instructions for the GPT to generate suggestions.\n */\n instructions?: string;\n\n /**\n * The data to pass to the suggestions.\n */\n parameters: T;\n\n /**\n * The current value of the parameter.\n */\n value?: Partial<MappedParameterTypes<T>>;\n\n /**\n * Whether the suggestions are enabled.\n * @default true\n */\n enabled?: boolean;\n\n /**\n * The debounce time in milliseconds.\n * @default 1000\n */\n debounceTime?: number;\n}\nexport type SuggestionsResult<T extends Parameter[]> =\n | { suggestions: undefined; isAvailable: false; isLoading: boolean }\n | { suggestions: MappedParameterTypes<T>; isAvailable: true; isLoading: boolean };\n\nexport function useCopilotSuggestions<const T extends Parameter[]>(\n {\n instructions,\n parameters,\n value,\n enabled = true,\n debounceTime = 1000,\n }: UseCopilotSuggestionsConfiguration<T>,\n dependencies: any[] = [],\n): SuggestionsResult<T> {\n const suggestionsAbortControllerRef = useRef<AbortController | null>(null);\n const debounceTimerRef = useRef<any>();\n const [suggestions, setSuggestions] = useState<SuggestionsResult<T>>({\n suggestions: undefined,\n isAvailable: false,\n isLoading: false,\n });\n const isFirstRunRef = useRef(true);\n\n const abortSuggestions = useCallback(() => {\n suggestionsAbortControllerRef.current?.abort();\n suggestionsAbortControllerRef.current = null;\n }, []);\n\n const generalContext = useCopilotContext();\n const messagesContext = useCopilotMessagesContext();\n const context = { ...generalContext, ...messagesContext };\n\n useEffect(() => {\n abortSuggestions();\n if (!enabled) {\n setSuggestions({ suggestions: undefined, isAvailable: false, isLoading: false });\n return;\n }\n\n // if value is the same as the last suggestions, don't reload\n if (JSON.stringify(value) === JSON.stringify(suggestions.suggestions)) {\n return;\n }\n\n debounceTimerRef.current = setTimeout(\n async () => {\n isFirstRunRef.current = false;\n suggestionsAbortControllerRef.current = new AbortController();\n setSuggestions({ ...suggestions, isLoading: true });\n await reloadSuggestions(\n context,\n instructions,\n parameters,\n value,\n suggestionsAbortControllerRef,\n setSuggestions,\n );\n },\n isFirstRunRef.current === true ? 0 : debounceTime,\n );\n\n return () => {\n clearTimeout(debounceTimerRef.current);\n };\n }, [\n instructions,\n JSON.stringify(parameters),\n JSON.stringify(value),\n enabled,\n debounceTime,\n ...dependencies,\n ]);\n\n return suggestions;\n}\n\nasync function reloadSuggestions(\n context: CopilotContextParams & CopilotMessagesContextParams,\n instructions: string | undefined,\n parameters: Parameter[],\n value: any,\n abortControllerRef: React.MutableRefObject<AbortController | null>,\n setSuggestions: (suggestions: any) => void,\n) {\n const abortController = abortControllerRef.current;\n let fullInstructions = `It's your task to generate suggestions based on the application context.`;\n if (instructions) {\n fullInstructions += `\\n\\nIn addition, follow these specific instructions: ${instructions}`;\n }\n if (value) {\n fullInstructions +=\n `\\n\\nThe current value of the parameter is: ${JSON.stringify(value)}.` +\n `If it makes sense to complete the existing data, i.e. expand string values, add new elements to arrays, etc., do so. ` +\n `Otherwise, generate a new value.`;\n }\n\n await extract({\n context,\n instructions: fullInstructions,\n parameters,\n abortSignal: abortController?.signal,\n requestType: CopilotRequestType.Task,\n stream({ args, status }) {\n if (status === \"complete\") {\n setSuggestions({ suggestions: args, isAvailable: true, isLoading: false });\n }\n },\n });\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAyCA,mBAAyD;AACzD,wBAMO;AAEP,gCAAmC;AAkC5B,SAAS,sBACd;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA,UAAU;AAAA,EACV,eAAe;AACjB,GACA,eAAsB,CAAC,GACD;AACtB,QAAM,oCAAgC,qBAA+B,IAAI;AACzE,QAAM,uBAAmB,qBAAY;AACrC,QAAM,CAAC,aAAa,cAAc,QAAI,uBAA+B;AAAA,IACnE,aAAa;AAAA,IACb,aAAa;AAAA,IACb,WAAW;AAAA,EACb,CAAC;AACD,QAAM,oBAAgB,qBAAO,IAAI;AAEjC,QAAM,uBAAmB,0BAAY,MAAM;AAvG7C;AAwGI,wCAA8B,YAA9B,mBAAuC;AACvC,kCAA8B,UAAU;AAAA,EAC1C,GAAG,CAAC,CAAC;AAEL,QAAM,qBA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ACzB,yBAAe,EAAE,aAAa,MAAM,aAAa,MAAM,WAAW,MAAM,CAAC;AAAA,QAC3E;AAAA,MACF;AAAA,IACF,CAAC;AAAA,EACH;AAAA;","names":[]}
@@ -0,0 +1,8 @@
1
+ import {
2
+ useCopilotSuggestions
3
+ } from "../chunk-K5JLXNIN.mjs";
4
+ import "../chunk-MRXNTQOX.mjs";
5
+ export {
6
+ useCopilotSuggestions
7
+ };
8
+ //# sourceMappingURL=use-copilot-suggestions.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
package/dist/index.d.ts CHANGED
@@ -4,6 +4,7 @@ export { CopilotSidebar } from './components/chat/Sidebar.js';
4
4
  export { CopilotChat } from './components/chat/Chat.js';
5
5
  export { useChatContext } from './components/chat/ChatContext.js';
6
6
  export { useCopilotChatSuggestions } from './hooks/use-copilot-chat-suggestions.js';
7
+ export { SuggestionsResult, UseCopilotSuggestionsConfiguration, useCopilotSuggestions } from './hooks/use-copilot-suggestions.js';
7
8
  export { CopilotKitCSSProperties } from './types/css.js';
8
9
  import '@copilotkit/runtime-client-gql';
9
10
  import 'react/jsx-runtime';
@@ -11,3 +12,4 @@ import './components/chat/Modal.js';
11
12
  import 'react';
12
13
  import '@copilotkit/react-core';
13
14
  import './types/suggestions.js';
15
+ import '@copilotkit/shared';
package/dist/index.js CHANGED
@@ -84,7 +84,8 @@ __export(src_exports, {
84
84
  CopilotPopup: () => CopilotPopup,
85
85
  CopilotSidebar: () => CopilotSidebar,
86
86
  useChatContext: () => useChatContext,
87
- useCopilotChatSuggestions: () => useCopilotChatSuggestions
87
+ useCopilotChatSuggestions: () => useCopilotChatSuggestions,
88
+ useCopilotSuggestions: () => useCopilotSuggestions
88
89
  });
89
90
  module.exports = __toCommonJS(src_exports);
90
91
 
@@ -2309,12 +2310,107 @@ function useCopilotChatSuggestions({
2309
2310
  };
2310
2311
  }, [...dependencies, instructions, minSuggestions, maxSuggestions, className]);
2311
2312
  }
2313
+
2314
+ // src/hooks/use-copilot-suggestions.ts
2315
+ var import_react15 = require("react");
2316
+ var import_react_core10 = require("@copilotkit/react-core");
2317
+ var import_runtime_client_gql5 = require("@copilotkit/runtime-client-gql");
2318
+ function useCopilotSuggestions({
2319
+ instructions,
2320
+ parameters,
2321
+ value,
2322
+ enabled = true,
2323
+ debounceTime = 1e3
2324
+ }, dependencies = []) {
2325
+ const suggestionsAbortControllerRef = (0, import_react15.useRef)(null);
2326
+ const debounceTimerRef = (0, import_react15.useRef)();
2327
+ const [suggestions, setSuggestions] = (0, import_react15.useState)({
2328
+ suggestions: void 0,
2329
+ isAvailable: false,
2330
+ isLoading: false
2331
+ });
2332
+ const isFirstRunRef = (0, import_react15.useRef)(true);
2333
+ const abortSuggestions = (0, import_react15.useCallback)(() => {
2334
+ var _a;
2335
+ (_a = suggestionsAbortControllerRef.current) == null ? void 0 : _a.abort();
2336
+ suggestionsAbortControllerRef.current = null;
2337
+ }, []);
2338
+ const generalContext = (0, import_react_core10.useCopilotContext)();
2339
+ const messagesContext = (0, import_react_core10.useCopilotMessagesContext)();
2340
+ const context = __spreadValues(__spreadValues({}, generalContext), messagesContext);
2341
+ (0, import_react15.useEffect)(() => {
2342
+ abortSuggestions();
2343
+ if (!enabled) {
2344
+ setSuggestions({ suggestions: void 0, isAvailable: false, isLoading: false });
2345
+ return;
2346
+ }
2347
+ if (JSON.stringify(value) === JSON.stringify(suggestions.suggestions)) {
2348
+ return;
2349
+ }
2350
+ debounceTimerRef.current = setTimeout(
2351
+ () => __async(this, null, function* () {
2352
+ isFirstRunRef.current = false;
2353
+ suggestionsAbortControllerRef.current = new AbortController();
2354
+ setSuggestions(__spreadProps(__spreadValues({}, suggestions), { isLoading: true }));
2355
+ yield reloadSuggestions2(
2356
+ context,
2357
+ instructions,
2358
+ parameters,
2359
+ value,
2360
+ suggestionsAbortControllerRef,
2361
+ setSuggestions
2362
+ );
2363
+ }),
2364
+ isFirstRunRef.current === true ? 0 : debounceTime
2365
+ );
2366
+ return () => {
2367
+ clearTimeout(debounceTimerRef.current);
2368
+ };
2369
+ }, [
2370
+ instructions,
2371
+ JSON.stringify(parameters),
2372
+ JSON.stringify(value),
2373
+ enabled,
2374
+ debounceTime,
2375
+ ...dependencies
2376
+ ]);
2377
+ return suggestions;
2378
+ }
2379
+ function reloadSuggestions2(context, instructions, parameters, value, abortControllerRef, setSuggestions) {
2380
+ return __async(this, null, function* () {
2381
+ const abortController = abortControllerRef.current;
2382
+ let fullInstructions = `It's your task to generate suggestions based on the application context.`;
2383
+ if (instructions) {
2384
+ fullInstructions += `
2385
+
2386
+ In addition, follow these specific instructions: ${instructions}`;
2387
+ }
2388
+ if (value) {
2389
+ fullInstructions += `
2390
+
2391
+ The current value of the parameter is: ${JSON.stringify(value)}.If it makes sense to complete the existing data, i.e. expand string values, add new elements to arrays, etc., do so. Otherwise, generate a new value.`;
2392
+ }
2393
+ yield (0, import_react_core10.extract)({
2394
+ context,
2395
+ instructions: fullInstructions,
2396
+ parameters,
2397
+ abortSignal: abortController == null ? void 0 : abortController.signal,
2398
+ requestType: import_runtime_client_gql5.CopilotRequestType.Task,
2399
+ stream({ args, status }) {
2400
+ if (status === "complete") {
2401
+ setSuggestions({ suggestions: args, isAvailable: true, isLoading: false });
2402
+ }
2403
+ }
2404
+ });
2405
+ });
2406
+ }
2312
2407
  // Annotate the CommonJS export names for ESM import in node:
2313
2408
  0 && (module.exports = {
2314
2409
  CopilotChat,
2315
2410
  CopilotPopup,
2316
2411
  CopilotSidebar,
2317
2412
  useChatContext,
2318
- useCopilotChatSuggestions
2413
+ useCopilotChatSuggestions,
2414
+ useCopilotSuggestions
2319
2415
  });
2320
2416
  //# sourceMappingURL=index.js.map