@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
package/dist/index.mjs CHANGED
@@ -4,42 +4,45 @@ import "./chunk-JD7BAH7U.mjs";
4
4
  import "./chunk-MRFF7GSQ.mjs";
5
5
  import {
6
6
  CopilotSidebar
7
- } from "./chunk-3JNDMHS4.mjs";
7
+ } from "./chunk-T7DQWLTN.mjs";
8
8
  import "./chunk-WB3YULQ4.mjs";
9
9
  import {
10
10
  CopilotPopup
11
- } from "./chunk-AX7CAT7V.mjs";
12
- import "./chunk-3EYL2DRG.mjs";
11
+ } from "./chunk-PIK3RYPJ.mjs";
12
+ import "./chunk-DTI5DZSK.mjs";
13
13
  import "./chunk-YAGE7RCE.mjs";
14
14
  import "./chunk-VEC45H6Q.mjs";
15
15
  import "./chunk-RQNJNK2W.mjs";
16
16
  import {
17
17
  CopilotChat
18
- } from "./chunk-NLIDKNNS.mjs";
18
+ } from "./chunk-UYT3USIS.mjs";
19
19
  import "./chunk-V7W6IM2V.mjs";
20
20
  import "./chunk-OMTPWC7T.mjs";
21
21
  import "./chunk-ZABXREBH.mjs";
22
22
  import "./chunk-6INMITFA.mjs";
23
23
  import "./chunk-RU73BEZM.mjs";
24
+ import "./chunk-3XAXY2Z3.mjs";
24
25
  import "./chunk-RJCZRKTV.mjs";
25
- import "./chunk-RS6UPR5N.mjs";
26
- import "./chunk-4FIGRRFS.mjs";
26
+ import "./chunk-W3TDOLUY.mjs";
27
27
  import "./chunk-OTPAZXVR.mjs";
28
+ import "./chunk-4FIGRRFS.mjs";
28
29
  import "./chunk-4LUMV4YO.mjs";
29
30
  import "./chunk-YQFVRDNC.mjs";
30
31
  import "./chunk-YQ3D5IQV.mjs";
32
+ import "./chunk-UPTB2MVO.mjs";
31
33
  import "./chunk-CPMIEVKK.mjs";
32
- import "./chunk-3XAXY2Z3.mjs";
33
34
  import {
34
35
  useChatContext
35
36
  } from "./chunk-CBBFRI3Q.mjs";
36
- import "./chunk-UPTB2MVO.mjs";
37
37
  import "./chunk-FZC7X5PK.mjs";
38
38
  import "./chunk-IU3WTXLQ.mjs";
39
- import "./chunk-T26KLXLH.mjs";
39
+ import "./chunk-RJ54B6VE.mjs";
40
40
  import {
41
41
  useCopilotChatSuggestions
42
- } from "./chunk-Z2UZSN3K.mjs";
42
+ } from "./chunk-HGQFDT5I.mjs";
43
+ import {
44
+ useCopilotSuggestions
45
+ } from "./chunk-K5JLXNIN.mjs";
43
46
  import "./chunk-54JAUBUJ.mjs";
44
47
  import "./chunk-S5MBUNGN.mjs";
45
48
  import "./chunk-MRXNTQOX.mjs";
@@ -48,6 +51,7 @@ export {
48
51
  CopilotPopup,
49
52
  CopilotSidebar,
50
53
  useChatContext,
51
- useCopilotChatSuggestions
54
+ useCopilotChatSuggestions,
55
+ useCopilotSuggestions
52
56
  };
53
57
  //# sourceMappingURL=index.mjs.map
package/package.json CHANGED
@@ -9,7 +9,7 @@
9
9
  "publishConfig": {
10
10
  "access": "public"
11
11
  },
12
- "version": "1.3.15",
12
+ "version": "1.3.16-mme-copilot-suggestions.0",
13
13
  "sideEffects": [
14
14
  "**/*.css"
15
15
  ],
@@ -40,9 +40,9 @@
40
40
  "ts-jest": "^29.1.1",
41
41
  "tsup": "^6.7.0",
42
42
  "typescript": "^5.2.3",
43
- "eslint-config-custom": "1.3.15",
44
- "tailwind-config": "1.3.15",
45
- "tsconfig": "1.3.15"
43
+ "eslint-config-custom": "1.3.16-mme-copilot-suggestions.0",
44
+ "tailwind-config": "1.3.16-mme-copilot-suggestions.0",
45
+ "tsconfig": "1.3.16-mme-copilot-suggestions.0"
46
46
  },
47
47
  "dependencies": {
48
48
  "@headlessui/react": "^2.1.3",
@@ -50,9 +50,9 @@
50
50
  "react-syntax-highlighter": "^15.5.0",
51
51
  "remark-gfm": "^3.0.1",
52
52
  "remark-math": "^5.1.1",
53
- "@copilotkit/react-core": "1.3.15",
54
- "@copilotkit/runtime-client-gql": "1.3.15",
55
- "@copilotkit/shared": "1.3.15"
53
+ "@copilotkit/react-core": "1.3.16-mme-copilot-suggestions.0",
54
+ "@copilotkit/runtime-client-gql": "1.3.16-mme-copilot-suggestions.0",
55
+ "@copilotkit/shared": "1.3.16-mme-copilot-suggestions.0"
56
56
  },
57
57
  "keywords": [
58
58
  "copilotkit",
@@ -1 +1,2 @@
1
1
  export * from "./use-copilot-chat-suggestions";
2
+ export * from "./use-copilot-suggestions";
@@ -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" />
@@ -0,0 +1,189 @@
1
+ /**
2
+ * <Callout type="warning">
3
+ * useCopilotSuggestions is experimental. The interface is not final and
4
+ * can change without notice.
5
+ * </Callout>
6
+ *
7
+ * `useCopilotSuggestions` is a React hook that provides auto-suggestions that can be added to any
8
+ * UI component.
9
+ *
10
+ * ## Usage
11
+ *
12
+ * ### Install Dependencies
13
+ *
14
+ * This component is part of the [@copilotkit/react-ui](https://npmjs.com/package/@copilotkit/react-ui) package.
15
+ *
16
+ * ```shell npm2yarn \"@copilotkit/react-ui"\
17
+ * npm install @copilotkit/react-core @copilotkit/react-ui
18
+ * ```
19
+ *
20
+ * ### Simple Usage
21
+ *
22
+ * ```tsx
23
+ * TODO
24
+ * ```
25
+ *
26
+ * ### Dependency Management
27
+ *
28
+ * ```tsx
29
+ * TODO
30
+ * }
31
+ * ```
32
+ *
33
+ * In the example above, the suggestions are generated based on the given instructions.
34
+ * The hook monitors `appState`, and updates suggestions accordingly whenever it changes.
35
+ *
36
+ * ### Behavior and Lifecycle
37
+ *
38
+ * The hook registers the configuration with the chat context upon component mount and
39
+ * removes it on unmount, ensuring a clean and efficient lifecycle management.
40
+ */
41
+
42
+ import { useEffect, useState, useRef, useCallback } from "react";
43
+ import {
44
+ CopilotContextParams,
45
+ CopilotMessagesContextParams,
46
+ extract,
47
+ useCopilotContext,
48
+ useCopilotMessagesContext,
49
+ } from "@copilotkit/react-core";
50
+ import { MappedParameterTypes, Parameter, randomId } from "@copilotkit/shared";
51
+ import { CopilotRequestType } from "@copilotkit/runtime-client-gql";
52
+
53
+ export interface UseCopilotSuggestionsConfiguration<T extends Parameter[] = []> {
54
+ /**
55
+ * A prompt or instructions for the GPT to generate suggestions.
56
+ */
57
+ instructions?: string;
58
+
59
+ /**
60
+ * The data to pass to the suggestions.
61
+ */
62
+ parameters: T;
63
+
64
+ /**
65
+ * The current value of the parameter.
66
+ */
67
+ value?: Partial<MappedParameterTypes<T>>;
68
+
69
+ /**
70
+ * Whether the suggestions are enabled.
71
+ * @default true
72
+ */
73
+ enabled?: boolean;
74
+
75
+ /**
76
+ * The debounce time in milliseconds.
77
+ * @default 1000
78
+ */
79
+ debounceTime?: number;
80
+ }
81
+ export type SuggestionsResult<T extends Parameter[]> =
82
+ | { suggestions: undefined; isAvailable: false; isLoading: boolean }
83
+ | { suggestions: MappedParameterTypes<T>; isAvailable: true; isLoading: boolean };
84
+
85
+ export function useCopilotSuggestions<const T extends Parameter[]>(
86
+ {
87
+ instructions,
88
+ parameters,
89
+ value,
90
+ enabled = true,
91
+ debounceTime = 1000,
92
+ }: UseCopilotSuggestionsConfiguration<T>,
93
+ dependencies: any[] = [],
94
+ ): SuggestionsResult<T> {
95
+ const suggestionsAbortControllerRef = useRef<AbortController | null>(null);
96
+ const debounceTimerRef = useRef<any>();
97
+ const [suggestions, setSuggestions] = useState<SuggestionsResult<T>>({
98
+ suggestions: undefined,
99
+ isAvailable: false,
100
+ isLoading: false,
101
+ });
102
+ const isFirstRunRef = useRef(true);
103
+
104
+ const abortSuggestions = useCallback(() => {
105
+ suggestionsAbortControllerRef.current?.abort();
106
+ suggestionsAbortControllerRef.current = null;
107
+ }, []);
108
+
109
+ const generalContext = useCopilotContext();
110
+ const messagesContext = useCopilotMessagesContext();
111
+ const context = { ...generalContext, ...messagesContext };
112
+
113
+ useEffect(() => {
114
+ abortSuggestions();
115
+ if (!enabled) {
116
+ setSuggestions({ suggestions: undefined, isAvailable: false, isLoading: false });
117
+ return;
118
+ }
119
+
120
+ // if value is the same as the last suggestions, don't reload
121
+ if (JSON.stringify(value) === JSON.stringify(suggestions.suggestions)) {
122
+ return;
123
+ }
124
+
125
+ debounceTimerRef.current = setTimeout(
126
+ async () => {
127
+ isFirstRunRef.current = false;
128
+ suggestionsAbortControllerRef.current = new AbortController();
129
+ setSuggestions({ ...suggestions, isLoading: true });
130
+ await reloadSuggestions(
131
+ context,
132
+ instructions,
133
+ parameters,
134
+ value,
135
+ suggestionsAbortControllerRef,
136
+ setSuggestions,
137
+ );
138
+ },
139
+ isFirstRunRef.current === true ? 0 : debounceTime,
140
+ );
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
+
154
+ return suggestions;
155
+ }
156
+
157
+ async function reloadSuggestions(
158
+ context: CopilotContextParams & CopilotMessagesContextParams,
159
+ instructions: string | undefined,
160
+ parameters: Parameter[],
161
+ value: any,
162
+ abortControllerRef: React.MutableRefObject<AbortController | null>,
163
+ setSuggestions: (suggestions: any) => void,
164
+ ) {
165
+ const abortController = abortControllerRef.current;
166
+ let fullInstructions = `It's your task to generate suggestions based on the application context.`;
167
+ if (instructions) {
168
+ fullInstructions += `\n\nIn addition, follow these specific instructions: ${instructions}`;
169
+ }
170
+ if (value) {
171
+ fullInstructions +=
172
+ `\n\nThe current value of the parameter is: ${JSON.stringify(value)}.` +
173
+ `If it makes sense to complete the existing data, i.e. expand string values, add new elements to arrays, etc., do so. ` +
174
+ `Otherwise, generate a new value.`;
175
+ }
176
+
177
+ await extract({
178
+ context,
179
+ instructions: fullInstructions,
180
+ parameters,
181
+ abortSignal: abortController?.signal,
182
+ requestType: CopilotRequestType.Task,
183
+ stream({ args, status }) {
184
+ if (status === "complete") {
185
+ setSuggestions({ suggestions: args, isAvailable: true, isLoading: false });
186
+ }
187
+ },
188
+ });
189
+ }
@@ -1 +0,0 @@
1
- //# sourceMappingURL=chunk-T26KLXLH.mjs.map
@@ -1 +0,0 @@
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":";AA6DA,SAAS,iBAAiB;AAC1B,SAAS,yBAAyB;AAClC,SAAS,gBAAgB;AAuBlB,SAAS,0BACd;AAAA,EACE;AAAA,EACA;AAAA,EACA,iBAAiB;AAAA,EACjB,iBAAiB;AACnB,GACA,eAAsB,CAAC,GACvB;AACA,QAAM,UAAU,kBAAkB;AAElC,YAAU,MAAM;AACd,UAAM,KAAK,SAAS;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":[]}