@copilotkit/react-core 1.9.3-next.4 → 1.10.0-next.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 (144) hide show
  1. package/CHANGELOG.md +38 -0
  2. package/dist/chunk-2IDV5OHF.mjs +11 -0
  3. package/dist/chunk-2IDV5OHF.mjs.map +1 -0
  4. package/dist/{chunk-36MGCCPZ.mjs → chunk-4CFY3CON.mjs} +2 -2
  5. package/dist/{chunk-BVK7PLK6.mjs → chunk-DF4YG4PF.mjs} +2 -2
  6. package/dist/{chunk-ELU3VLPJ.mjs → chunk-G35QHAQ6.mjs} +8 -8
  7. package/dist/chunk-G35QHAQ6.mjs.map +1 -0
  8. package/dist/{chunk-DNHCRVE6.mjs → chunk-LIGLZ53V.mjs} +98 -26
  9. package/dist/chunk-LIGLZ53V.mjs.map +1 -0
  10. package/dist/{chunk-5BSUSFHM.mjs → chunk-LNAQ7JG3.mjs} +2 -2
  11. package/dist/{chunk-NJA5ZLAZ.mjs → chunk-LU5JPZ24.mjs} +2 -2
  12. package/dist/{chunk-GIMSRCVW.mjs → chunk-NAM55D5I.mjs} +153 -14
  13. package/dist/chunk-NAM55D5I.mjs.map +1 -0
  14. package/dist/{chunk-CUAFWKTQ.mjs → chunk-O5KV32S5.mjs} +4 -4
  15. package/dist/{chunk-FN3UA2ZE.mjs → chunk-PZXTDFMX.mjs} +3 -3
  16. package/dist/{chunk-VDADWRS3.mjs → chunk-Q6FZZJ5A.mjs} +2 -2
  17. package/dist/{chunk-KIXKBJUV.mjs → chunk-RGKZCCPA.mjs} +2 -2
  18. package/dist/{chunk-SGF6C7I6.mjs → chunk-SJJNFYGQ.mjs} +4 -4
  19. package/dist/{chunk-DKZTPL66.mjs → chunk-XGRBCWK6.mjs} +5 -2
  20. package/dist/chunk-XGRBCWK6.mjs.map +1 -0
  21. package/dist/{chunk-BSAVFYRQ.mjs → chunk-YBDRISKO.mjs} +5 -5
  22. package/dist/{chunk-QPX2JKOR.mjs → chunk-YSQ5I2AE.mjs} +6 -6
  23. package/dist/chunk-YSQ5I2AE.mjs.map +1 -0
  24. package/dist/{chunk-5B3NSTQK.mjs → chunk-YVBDAURK.mjs} +3 -3
  25. package/dist/components/copilot-provider/copilot-messages.js +4 -1
  26. package/dist/components/copilot-provider/copilot-messages.js.map +1 -1
  27. package/dist/components/copilot-provider/copilot-messages.mjs +3 -3
  28. package/dist/components/copilot-provider/copilotkit-props.d.ts +2 -1
  29. package/dist/components/copilot-provider/copilotkit.d.ts +2 -1
  30. package/dist/components/copilot-provider/copilotkit.js +8 -2
  31. package/dist/components/copilot-provider/copilotkit.js.map +1 -1
  32. package/dist/components/copilot-provider/copilotkit.mjs +10 -9
  33. package/dist/components/copilot-provider/index.d.ts +2 -1
  34. package/dist/components/copilot-provider/index.js +8 -2
  35. package/dist/components/copilot-provider/index.js.map +1 -1
  36. package/dist/components/copilot-provider/index.mjs +10 -9
  37. package/dist/components/error-boundary/error-boundary.mjs +2 -2
  38. package/dist/components/index.d.ts +2 -1
  39. package/dist/components/index.js +8 -2
  40. package/dist/components/index.js.map +1 -1
  41. package/dist/components/index.mjs +10 -9
  42. package/dist/context/copilot-context.d.ts +2 -1
  43. package/dist/context/copilot-context.js +4 -1
  44. package/dist/context/copilot-context.js.map +1 -1
  45. package/dist/context/copilot-context.mjs +1 -1
  46. package/dist/context/index.d.ts +1 -1
  47. package/dist/context/index.js +4 -1
  48. package/dist/context/index.js.map +1 -1
  49. package/dist/context/index.mjs +4 -4
  50. package/dist/{copilot-context-3ab4fdf5.d.ts → copilot-context-bd88d30d.d.ts} +21 -1
  51. package/dist/hooks/index.d.ts +5 -5
  52. package/dist/hooks/index.js +493 -154
  53. package/dist/hooks/index.js.map +1 -1
  54. package/dist/hooks/index.mjs +32 -31
  55. package/dist/hooks/use-chat.d.ts +6 -1
  56. package/dist/hooks/use-chat.js +5 -2
  57. package/dist/hooks/use-chat.js.map +1 -1
  58. package/dist/hooks/use-chat.mjs +4 -4
  59. package/dist/hooks/use-coagent-state-render.js +4 -1
  60. package/dist/hooks/use-coagent-state-render.js.map +1 -1
  61. package/dist/hooks/use-coagent-state-render.mjs +2 -2
  62. package/dist/hooks/use-coagent.d.ts +3 -3
  63. package/dist/hooks/use-coagent.js +427 -45
  64. package/dist/hooks/use-coagent.js.map +1 -1
  65. package/dist/hooks/use-coagent.mjs +16 -14
  66. package/dist/hooks/use-copilot-action.js +4 -1
  67. package/dist/hooks/use-copilot-action.js.map +1 -1
  68. package/dist/hooks/use-copilot-action.mjs +2 -2
  69. package/dist/hooks/use-copilot-additional-instructions.js +4 -1
  70. package/dist/hooks/use-copilot-additional-instructions.js.map +1 -1
  71. package/dist/hooks/use-copilot-additional-instructions.mjs +2 -2
  72. package/dist/hooks/use-copilot-authenticated-action.js +4 -1
  73. package/dist/hooks/use-copilot-authenticated-action.js.map +1 -1
  74. package/dist/hooks/use-copilot-authenticated-action.mjs +3 -3
  75. package/dist/hooks/use-copilot-chat.d.ts +43 -6
  76. package/dist/hooks/use-copilot-chat.js +443 -65
  77. package/dist/hooks/use-copilot-chat.js.map +1 -1
  78. package/dist/hooks/use-copilot-chat.mjs +15 -13
  79. package/dist/hooks/use-copilot-readable.js +4 -1
  80. package/dist/hooks/use-copilot-readable.js.map +1 -1
  81. package/dist/hooks/use-copilot-readable.mjs +2 -2
  82. package/dist/hooks/use-langgraph-interrupt-render.js +4 -1
  83. package/dist/hooks/use-langgraph-interrupt-render.js.map +1 -1
  84. package/dist/hooks/use-langgraph-interrupt-render.mjs +2 -2
  85. package/dist/hooks/use-langgraph-interrupt.d.ts +4 -3
  86. package/dist/hooks/use-langgraph-interrupt.js +452 -72
  87. package/dist/hooks/use-langgraph-interrupt.js.map +1 -1
  88. package/dist/hooks/use-langgraph-interrupt.mjs +16 -14
  89. package/dist/hooks/use-make-copilot-document-readable.js +4 -1
  90. package/dist/hooks/use-make-copilot-document-readable.js.map +1 -1
  91. package/dist/hooks/use-make-copilot-document-readable.mjs +2 -2
  92. package/dist/index.d.ts +2 -1
  93. package/dist/index.js +422 -201
  94. package/dist/index.js.map +1 -1
  95. package/dist/index.mjs +42 -36
  96. package/dist/lib/copilot-task.d.ts +2 -1
  97. package/dist/lib/copilot-task.js.map +1 -1
  98. package/dist/lib/copilot-task.mjs +12 -11
  99. package/dist/lib/index.d.ts +2 -1
  100. package/dist/lib/index.js.map +1 -1
  101. package/dist/lib/index.mjs +12 -11
  102. package/dist/types/interrupt-action.d.ts +3 -2
  103. package/dist/utils/extract.d.ts +1 -1
  104. package/dist/utils/extract.js.map +1 -1
  105. package/dist/utils/extract.mjs +10 -9
  106. package/dist/utils/index.d.ts +4 -3
  107. package/dist/utils/index.js +144 -0
  108. package/dist/utils/index.js.map +1 -1
  109. package/dist/utils/index.mjs +16 -10
  110. package/dist/utils/suggestions-constants.d.ts +9 -0
  111. package/dist/utils/suggestions-constants.js +35 -0
  112. package/dist/utils/suggestions-constants.js.map +1 -0
  113. package/dist/utils/suggestions-constants.mjs +8 -0
  114. package/dist/utils/suggestions-constants.mjs.map +1 -0
  115. package/dist/utils/suggestions.d.ts +11 -0
  116. package/dist/utils/suggestions.js +343 -0
  117. package/dist/utils/suggestions.js.map +1 -0
  118. package/dist/utils/suggestions.mjs +22 -0
  119. package/dist/utils/suggestions.mjs.map +1 -0
  120. package/package.json +3 -3
  121. package/src/components/copilot-provider/copilotkit.tsx +4 -0
  122. package/src/context/copilot-context.tsx +6 -0
  123. package/src/hooks/use-chat.ts +6 -2
  124. package/src/hooks/use-coagent.ts +1 -1
  125. package/src/hooks/use-copilot-chat.ts +197 -21
  126. package/src/utils/index.ts +3 -0
  127. package/src/utils/suggestions-constants.ts +8 -0
  128. package/src/utils/suggestions.ts +208 -0
  129. package/dist/chunk-DKZTPL66.mjs.map +0 -1
  130. package/dist/chunk-DNHCRVE6.mjs.map +0 -1
  131. package/dist/chunk-ELU3VLPJ.mjs.map +0 -1
  132. package/dist/chunk-GIMSRCVW.mjs.map +0 -1
  133. package/dist/chunk-QPX2JKOR.mjs.map +0 -1
  134. /package/dist/{chunk-36MGCCPZ.mjs.map → chunk-4CFY3CON.mjs.map} +0 -0
  135. /package/dist/{chunk-BVK7PLK6.mjs.map → chunk-DF4YG4PF.mjs.map} +0 -0
  136. /package/dist/{chunk-5BSUSFHM.mjs.map → chunk-LNAQ7JG3.mjs.map} +0 -0
  137. /package/dist/{chunk-NJA5ZLAZ.mjs.map → chunk-LU5JPZ24.mjs.map} +0 -0
  138. /package/dist/{chunk-CUAFWKTQ.mjs.map → chunk-O5KV32S5.mjs.map} +0 -0
  139. /package/dist/{chunk-FN3UA2ZE.mjs.map → chunk-PZXTDFMX.mjs.map} +0 -0
  140. /package/dist/{chunk-VDADWRS3.mjs.map → chunk-Q6FZZJ5A.mjs.map} +0 -0
  141. /package/dist/{chunk-KIXKBJUV.mjs.map → chunk-RGKZCCPA.mjs.map} +0 -0
  142. /package/dist/{chunk-SGF6C7I6.mjs.map → chunk-SJJNFYGQ.mjs.map} +0 -0
  143. /package/dist/{chunk-BSAVFYRQ.mjs.map → chunk-YBDRISKO.mjs.map} +0 -0
  144. /package/dist/{chunk-5B3NSTQK.mjs.map → chunk-YVBDAURK.mjs.map} +0 -0
@@ -83,7 +83,7 @@ __export(use_copilot_chat_exports, {
83
83
  useCopilotChat: () => useCopilotChat
84
84
  });
85
85
  module.exports = __toCommonJS(use_copilot_chat_exports);
86
- var import_react8 = require("react");
86
+ var import_react9 = require("react");
87
87
 
88
88
  // src/context/copilot-context.tsx
89
89
  var import_react = __toESM(require("react"));
@@ -159,7 +159,10 @@ var emptyCopilotContext = {
159
159
  langGraphInterruptAction: null,
160
160
  setLangGraphInterruptAction: () => null,
161
161
  removeLangGraphInterruptAction: () => null,
162
- onError: void 0
162
+ onError: void 0,
163
+ suggestions: [],
164
+ setSuggestions: () => {
165
+ }
163
166
  };
164
167
  var CopilotContext = import_react.default.createContext(emptyCopilotContext);
165
168
  function useCopilotContext() {
@@ -173,11 +176,25 @@ function returnAndThrowInDebug(_value) {
173
176
  throw new Error("Remember to wrap your app in a `<CopilotKit> {...} </CopilotKit>` !!!");
174
177
  }
175
178
 
176
- // src/hooks/use-copilot-chat.ts
177
- var import_runtime_client_gql4 = require("@copilotkit/runtime-client-gql");
179
+ // src/context/copilot-messages-context.tsx
180
+ var import_react2 = __toESM(require("react"));
181
+ var emptyCopilotContext2 = {
182
+ messages: [],
183
+ setMessages: () => []
184
+ };
185
+ var CopilotMessagesContext = import_react2.default.createContext(emptyCopilotContext2);
186
+ function useCopilotMessagesContext() {
187
+ const context = import_react2.default.useContext(CopilotMessagesContext);
188
+ if (context === emptyCopilotContext2) {
189
+ throw new Error(
190
+ "A messages consuming component was not wrapped with `<CopilotMessages> {...} </CopilotMessages>`"
191
+ );
192
+ }
193
+ return context;
194
+ }
178
195
 
179
196
  // src/hooks/use-chat.ts
180
- var import_react5 = require("react");
197
+ var import_react6 = require("react");
181
198
  var import_react_dom = require("react-dom");
182
199
  var import_shared4 = require("@copilotkit/shared");
183
200
  var import_runtime_client_gql3 = require("@copilotkit/runtime-client-gql");
@@ -211,12 +228,12 @@ function processActionsForRuntimeRequest(actions) {
211
228
  var import_runtime_client_gql2 = require("@copilotkit/runtime-client-gql");
212
229
 
213
230
  // src/components/toast/toast-provider.tsx
214
- var import_react2 = require("react");
231
+ var import_react3 = require("react");
215
232
  var import_shared2 = require("@copilotkit/shared");
216
233
  var import_jsx_runtime = require("react/jsx-runtime");
217
- var ToastContext = (0, import_react2.createContext)(void 0);
234
+ var ToastContext = (0, import_react3.createContext)(void 0);
218
235
  function useToast() {
219
- const context = (0, import_react2.useContext)(ToastContext);
236
+ const context = (0, import_react3.useContext)(ToastContext);
220
237
  if (!context) {
221
238
  throw new Error("useToast must be used within a ToastProvider");
222
239
  }
@@ -224,7 +241,7 @@ function useToast() {
224
241
  }
225
242
 
226
243
  // src/hooks/use-copilot-runtime-client.ts
227
- var import_react3 = require("react");
244
+ var import_react4 = require("react");
228
245
  var import_shared3 = require("@copilotkit/shared");
229
246
 
230
247
  // src/utils/dev-console.ts
@@ -236,7 +253,7 @@ function shouldShowDevConsole(showDevConsole) {
236
253
  var useCopilotRuntimeClient = (options) => {
237
254
  const { setBannerError } = useToast();
238
255
  const _a = options, { showDevConsole, onError } = _a, runtimeOptions = __objRest(_a, ["showDevConsole", "onError"]);
239
- const lastStructuredErrorRef = (0, import_react3.useRef)(null);
256
+ const lastStructuredErrorRef = (0, import_react4.useRef)(null);
240
257
  const traceUIError = (error, originalError) => __async(void 0, null, function* () {
241
258
  if (!onError || !runtimeOptions.publicApiKey)
242
259
  return;
@@ -264,7 +281,7 @@ var useCopilotRuntimeClient = (options) => {
264
281
  console.error("Error in onError handler:", error2);
265
282
  }
266
283
  });
267
- const runtimeClient = (0, import_react3.useMemo)(() => {
284
+ const runtimeClient = (0, import_react4.useMemo)(() => {
268
285
  return new import_runtime_client_gql2.CopilotRuntimeClient(__spreadProps(__spreadValues({}, runtimeOptions), {
269
286
  handleGQLErrors: (error) => {
270
287
  var _a2;
@@ -353,7 +370,7 @@ function createStructuredError(gqlError) {
353
370
  }
354
371
 
355
372
  // src/components/error-boundary/error-utils.tsx
356
- var import_react4 = require("react");
373
+ var import_react5 = require("react");
357
374
 
358
375
  // src/components/toast/exclamation-mark-icon.tsx
359
376
  var import_jsx_runtime2 = require("react/jsx-runtime");
@@ -436,7 +453,7 @@ function ErrorToast({ errors }) {
436
453
  }
437
454
  function useErrorToast() {
438
455
  const { addToast } = useToast();
439
- return (0, import_react4.useCallback)(
456
+ return (0, import_react5.useCallback)(
440
457
  (error) => {
441
458
  const errorId = error.map((err) => {
442
459
  var _a, _b;
@@ -456,7 +473,7 @@ function useErrorToast() {
456
473
  }
457
474
  function useAsyncCallback(callback, deps) {
458
475
  const addErrorToast = useErrorToast();
459
- return (0, import_react4.useCallback)((...args) => __async(this, null, function* () {
476
+ return (0, import_react5.useCallback)((...args) => __async(this, null, function* () {
460
477
  try {
461
478
  return yield callback(...args);
462
479
  } catch (error) {
@@ -495,7 +512,7 @@ function useChat(options) {
495
512
  langGraphInterruptAction,
496
513
  setLangGraphInterruptAction
497
514
  } = options;
498
- const runChatCompletionRef = (0, import_react5.useRef)();
515
+ const runChatCompletionRef = (0, import_react6.useRef)();
499
516
  const addErrorToast = useErrorToast();
500
517
  const { setBannerError } = useToast();
501
518
  const { onError } = useCopilotContext();
@@ -526,11 +543,11 @@ function useChat(options) {
526
543
  console.error("Error in use-chat onError handler:", traceError);
527
544
  }
528
545
  });
529
- const agentSessionRef = (0, import_react5.useRef)(agentSession);
546
+ const agentSessionRef = (0, import_react6.useRef)(agentSession);
530
547
  agentSessionRef.current = agentSession;
531
- const runIdRef = (0, import_react5.useRef)(runId);
548
+ const runIdRef = (0, import_react6.useRef)(runId);
532
549
  runIdRef.current = runId;
533
- const extensionsRef = (0, import_react5.useRef)(extensions);
550
+ const extensionsRef = (0, import_react6.useRef)(extensions);
534
551
  extensionsRef.current = extensions;
535
552
  const publicApiKey = copilotConfig.publicApiKey;
536
553
  const headers = __spreadValues(__spreadValues({}, copilotConfig.headers || {}), publicApiKey ? { [import_shared4.COPILOT_CLOUD_PUBLIC_API_KEY_HEADER]: publicApiKey } : {});
@@ -542,7 +559,7 @@ function useChat(options) {
542
559
  credentials: copilotConfig.credentials,
543
560
  showDevConsole
544
561
  });
545
- const pendingAppendsRef = (0, import_react5.useRef)([]);
562
+ const pendingAppendsRef = (0, import_react6.useRef)([]);
546
563
  const runChatCompletion = useAsyncCallback(
547
564
  (previousMessages) => __async(this, null, function* () {
548
565
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t;
@@ -838,7 +855,7 @@ function useChat(options) {
838
855
  const action = actions.find(
839
856
  (action2) => action2.name === message.name
840
857
  );
841
- if (action && action.available === "remote") {
858
+ if (action && action.available === "frontend") {
842
859
  continue;
843
860
  }
844
861
  const currentResultMessagePairedFeAction = message.isResultMessage() ? getPairedFeAction(actions, message) : null;
@@ -936,7 +953,7 @@ function useChat(options) {
936
953
  }),
937
954
  [messages]
938
955
  );
939
- (0, import_react5.useEffect)(() => {
956
+ (0, import_react6.useEffect)(() => {
940
957
  if (!isLoading && pendingAppendsRef.current.length > 0) {
941
958
  const pending = pendingAppendsRef.current.splice(0);
942
959
  const followUp = pending.some((p) => p.followUp);
@@ -947,7 +964,7 @@ function useChat(options) {
947
964
  }
948
965
  }
949
966
  }, [isLoading, messages, setMessages, runChatCompletionAndHandleFunctionCall]);
950
- const composeAndFlushMetaEventsInput = (0, import_react5.useCallback)(
967
+ const composeAndFlushMetaEventsInput = (0, import_react6.useCallback)(
951
968
  (metaEvents) => {
952
969
  return metaEvents.reduce((acc, event) => {
953
970
  if (!event)
@@ -1127,36 +1144,326 @@ function getPairedFeAction(actions, message) {
1127
1144
  // src/components/copilot-provider/copilotkit.tsx
1128
1145
  var import_react7 = require("react");
1129
1146
  var import_react_dom2 = require("react-dom");
1147
+ var import_shared7 = require("@copilotkit/shared");
1148
+
1149
+ // src/utils/extract.ts
1130
1150
  var import_shared5 = require("@copilotkit/shared");
1151
+ var import_runtime_client_gql4 = require("@copilotkit/runtime-client-gql");
1152
+ var import_runtime_client_gql5 = require("@copilotkit/runtime-client-gql");
1153
+ function extract(_0) {
1154
+ return __async(this, arguments, function* ({
1155
+ context,
1156
+ instructions,
1157
+ parameters,
1158
+ include,
1159
+ data,
1160
+ abortSignal,
1161
+ stream,
1162
+ requestType = import_runtime_client_gql4.CopilotRequestType.Task,
1163
+ forwardedParameters
1164
+ }) {
1165
+ var _a, _b;
1166
+ const { messages } = context;
1167
+ const action = {
1168
+ name: "extract",
1169
+ description: instructions,
1170
+ parameters,
1171
+ handler: (args) => {
1172
+ }
1173
+ };
1174
+ const includeReadable = (_a = include == null ? void 0 : include.readable) != null ? _a : false;
1175
+ const includeMessages = (_b = include == null ? void 0 : include.messages) != null ? _b : false;
1176
+ let contextString = "";
1177
+ if (data) {
1178
+ contextString = (typeof data === "string" ? data : JSON.stringify(data)) + "\n\n";
1179
+ }
1180
+ if (includeReadable) {
1181
+ contextString += context.getContextString([], defaultCopilotContextCategories);
1182
+ }
1183
+ const systemMessage = new import_runtime_client_gql4.TextMessage({
1184
+ content: makeSystemMessage(contextString, instructions),
1185
+ role: import_runtime_client_gql4.Role.System
1186
+ });
1187
+ const instructionsMessage = new import_runtime_client_gql4.TextMessage({
1188
+ content: makeInstructionsMessage(instructions),
1189
+ role: import_runtime_client_gql4.Role.User
1190
+ });
1191
+ const response = context.runtimeClient.asStream(
1192
+ context.runtimeClient.generateCopilotResponse({
1193
+ data: {
1194
+ frontend: {
1195
+ actions: [
1196
+ {
1197
+ name: action.name,
1198
+ description: action.description || "",
1199
+ jsonSchema: JSON.stringify((0, import_shared5.actionParametersToJsonSchema)(action.parameters || []))
1200
+ }
1201
+ ],
1202
+ url: window.location.href
1203
+ },
1204
+ messages: (0, import_runtime_client_gql5.convertMessagesToGqlInput)(
1205
+ includeMessages ? [systemMessage, instructionsMessage, ...(0, import_runtime_client_gql5.filterAgentStateMessages)(messages)] : [systemMessage, instructionsMessage]
1206
+ ),
1207
+ metadata: {
1208
+ requestType
1209
+ },
1210
+ forwardedParameters: __spreadProps(__spreadValues({}, forwardedParameters != null ? forwardedParameters : {}), {
1211
+ toolChoice: "function",
1212
+ toolChoiceFunctionName: action.name
1213
+ })
1214
+ },
1215
+ properties: context.copilotApiConfig.properties,
1216
+ signal: abortSignal
1217
+ })
1218
+ );
1219
+ const reader = response.getReader();
1220
+ let isInitial = true;
1221
+ let actionExecutionMessage = void 0;
1222
+ while (true) {
1223
+ const { done, value } = yield reader.read();
1224
+ if (done) {
1225
+ break;
1226
+ }
1227
+ if (abortSignal == null ? void 0 : abortSignal.aborted) {
1228
+ throw new Error("Aborted");
1229
+ }
1230
+ actionExecutionMessage = (0, import_runtime_client_gql4.convertGqlOutputToMessages)(
1231
+ value.generateCopilotResponse.messages
1232
+ ).find((msg) => msg.isActionExecutionMessage());
1233
+ if (!actionExecutionMessage) {
1234
+ continue;
1235
+ }
1236
+ stream == null ? void 0 : stream({
1237
+ status: isInitial ? "initial" : "inProgress",
1238
+ args: actionExecutionMessage.arguments
1239
+ });
1240
+ isInitial = false;
1241
+ }
1242
+ if (!actionExecutionMessage) {
1243
+ throw new Error("extract() failed: No function call occurred");
1244
+ }
1245
+ stream == null ? void 0 : stream({
1246
+ status: "complete",
1247
+ args: actionExecutionMessage.arguments
1248
+ });
1249
+ return actionExecutionMessage.arguments;
1250
+ });
1251
+ }
1252
+ function makeInstructionsMessage(instructions) {
1253
+ return `
1254
+ The user has given you the following task to complete:
1131
1255
 
1132
- // src/context/copilot-messages-context.tsx
1133
- var import_react6 = __toESM(require("react"));
1134
- var emptyCopilotContext2 = {
1135
- messages: [],
1136
- setMessages: () => []
1137
- };
1138
- var CopilotMessagesContext = import_react6.default.createContext(emptyCopilotContext2);
1139
- function useCopilotMessagesContext() {
1140
- const context = import_react6.default.useContext(CopilotMessagesContext);
1141
- if (context === emptyCopilotContext2) {
1142
- throw new Error(
1143
- "A messages consuming component was not wrapped with `<CopilotMessages> {...} </CopilotMessages>`"
1256
+ \`\`\`
1257
+ ${instructions}
1258
+ \`\`\`
1259
+
1260
+ Any additional messages provided are for providing context only and should not be used to ask questions or engage in conversation.
1261
+ `;
1262
+ }
1263
+ function makeSystemMessage(contextString, instructions) {
1264
+ return `
1265
+ Please act as an efficient, competent, conscientious, and industrious professional assistant.
1266
+
1267
+ 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.
1268
+ Always be polite and respectful, and prefer brevity over verbosity.
1269
+
1270
+ The user has provided you with the following context:
1271
+ \`\`\`
1272
+ ${contextString}
1273
+ \`\`\`
1274
+
1275
+ They have also provided you with a function called extract you MUST call to initiate actions on their behalf.
1276
+
1277
+ Please assist them as best you can.
1278
+
1279
+ This is not a conversation, so please do not ask questions. Just call the function without saying anything else.
1280
+ `;
1281
+ }
1282
+
1283
+ // src/utils/suggestions.ts
1284
+ var import_shared6 = require("@copilotkit/shared");
1285
+ var import_runtime_client_gql6 = require("@copilotkit/runtime-client-gql");
1286
+ var reloadSuggestions = (context, chatSuggestionConfiguration, setCurrentSuggestions, abortControllerRef) => __async(void 0, null, function* () {
1287
+ const abortController = abortControllerRef.current;
1288
+ if (abortController == null ? void 0 : abortController.signal.aborted) {
1289
+ return;
1290
+ }
1291
+ const setSuggestionsIfNotAborted = (suggestions) => {
1292
+ if (!(abortController == null ? void 0 : abortController.signal.aborted) && abortControllerRef.current === abortController) {
1293
+ setCurrentSuggestions(suggestions);
1294
+ }
1295
+ };
1296
+ try {
1297
+ const tools = JSON.stringify(
1298
+ Object.values(context.actions).map((action) => ({
1299
+ name: action.name,
1300
+ description: action.description,
1301
+ jsonSchema: JSON.stringify((0, import_shared6.actionParametersToJsonSchema)(action.parameters))
1302
+ }))
1144
1303
  );
1304
+ const allSuggestions = [];
1305
+ let hasSuccessfulSuggestions = false;
1306
+ let hasErrors = false;
1307
+ let lastError = null;
1308
+ const enabledConfigs = Object.values(chatSuggestionConfiguration).filter(
1309
+ (config) => config.instructions && config.instructions.trim().length > 0
1310
+ );
1311
+ if (enabledConfigs.length === 0) {
1312
+ return;
1313
+ }
1314
+ setSuggestionsIfNotAborted([]);
1315
+ for (const config of enabledConfigs) {
1316
+ if (abortController == null ? void 0 : abortController.signal.aborted) {
1317
+ setSuggestionsIfNotAborted([]);
1318
+ return;
1319
+ }
1320
+ try {
1321
+ const result = yield extract({
1322
+ context,
1323
+ instructions: "Suggest what the user could say next. Provide clear, highly relevant suggestions. Do not literally suggest function calls. ",
1324
+ data: `${config.instructions}
1325
+
1326
+ Available tools: ${tools}
1327
+
1328
+ `,
1329
+ requestType: import_runtime_client_gql6.CopilotRequestType.Task,
1330
+ parameters: [
1331
+ {
1332
+ name: "suggestions",
1333
+ type: "object[]",
1334
+ attributes: [
1335
+ {
1336
+ name: "title",
1337
+ description: "The title of the suggestion. This is shown as a button and should be short.",
1338
+ type: "string"
1339
+ },
1340
+ {
1341
+ name: "message",
1342
+ description: "The message to send when the suggestion is clicked. This should be a clear, complete sentence and will be sent as an instruction to the AI.",
1343
+ type: "string"
1344
+ }
1345
+ ]
1346
+ }
1347
+ ],
1348
+ include: {
1349
+ messages: true,
1350
+ readable: true
1351
+ },
1352
+ abortSignal: abortController == null ? void 0 : abortController.signal,
1353
+ stream: ({ status, args }) => {
1354
+ if (abortController == null ? void 0 : abortController.signal.aborted) {
1355
+ return;
1356
+ }
1357
+ const suggestions = args.suggestions || [];
1358
+ const newSuggestions = [];
1359
+ for (let i = 0; i < suggestions.length; i++) {
1360
+ if (config.maxSuggestions !== void 0 && i >= config.maxSuggestions) {
1361
+ break;
1362
+ }
1363
+ const suggestion = suggestions[i];
1364
+ if (!suggestion || typeof suggestion !== "object") {
1365
+ continue;
1366
+ }
1367
+ const { title, message } = suggestion;
1368
+ const hasValidTitle = title && typeof title === "string" && title.trim().length > 0;
1369
+ const hasValidMessage = message && typeof message === "string" && message.trim().length > 0;
1370
+ if (!hasValidTitle) {
1371
+ continue;
1372
+ }
1373
+ const partial = i === suggestions.length - 1 && status !== "complete";
1374
+ newSuggestions.push({
1375
+ title: title.trim(),
1376
+ message: hasValidMessage ? message.trim() : "",
1377
+ // Use title as fallback
1378
+ partial,
1379
+ className: config.className
1380
+ });
1381
+ }
1382
+ setSuggestionsIfNotAborted([...allSuggestions, ...newSuggestions]);
1383
+ }
1384
+ });
1385
+ if ((result == null ? void 0 : result.suggestions) && Array.isArray(result.suggestions)) {
1386
+ const validSuggestions = result.suggestions.filter(
1387
+ (suggestion) => suggestion && typeof suggestion.title === "string" && suggestion.title.trim().length > 0
1388
+ ).map((suggestion) => ({
1389
+ title: suggestion.title.trim(),
1390
+ message: suggestion.message && typeof suggestion.message === "string" && suggestion.message.trim() ? suggestion.message.trim() : suggestion.title.trim()
1391
+ }));
1392
+ if (validSuggestions.length > 0) {
1393
+ allSuggestions.push(...validSuggestions);
1394
+ hasSuccessfulSuggestions = true;
1395
+ }
1396
+ }
1397
+ } catch (error) {
1398
+ hasErrors = true;
1399
+ lastError = error instanceof Error ? error : new Error(String(error));
1400
+ }
1401
+ }
1402
+ if (hasSuccessfulSuggestions && allSuggestions.length > 0) {
1403
+ const uniqueSuggestions = allSuggestions.filter(
1404
+ (suggestion, index, self) => index === self.findIndex((s) => s.message === suggestion.message)
1405
+ );
1406
+ setSuggestionsIfNotAborted(uniqueSuggestions);
1407
+ } else if (hasErrors) {
1408
+ const errorMessage = lastError ? lastError.message : "Failed to generate suggestions due to API errors";
1409
+ throw new Error(errorMessage);
1410
+ }
1411
+ } catch (error) {
1412
+ throw error;
1145
1413
  }
1146
- return context;
1147
- }
1414
+ });
1148
1415
 
1149
1416
  // src/components/copilot-provider/copilotkit.tsx
1150
1417
  var import_jsx_runtime4 = require("react/jsx-runtime");
1151
1418
  var defaultCopilotContextCategories = ["global"];
1152
1419
 
1153
1420
  // src/hooks/use-copilot-chat.ts
1154
- function useCopilotChat(_a = {}) {
1155
- var _b = _a, {
1156
- makeSystemMessage
1157
- } = _b, options = __objRest(_b, [
1158
- "makeSystemMessage"
1159
- ]);
1421
+ var import_runtime_client_gql7 = require("@copilotkit/runtime-client-gql");
1422
+
1423
+ // src/hooks/use-langgraph-interrupt-render.ts
1424
+ var import_react8 = __toESM(require("react"));
1425
+ var InterruptRenderer = ({ event, result, render, resolve }) => {
1426
+ return render({ event, result, resolve });
1427
+ };
1428
+ function useLangGraphInterruptRender() {
1429
+ const { langGraphInterruptAction, setLangGraphInterruptAction, agentSession } = useCopilotContext();
1430
+ const responseRef = import_react8.default.useRef();
1431
+ const resolveInterrupt = (0, import_react8.useCallback)(
1432
+ (response) => {
1433
+ responseRef.current = response;
1434
+ setTimeout(() => {
1435
+ setLangGraphInterruptAction({ event: { response } });
1436
+ }, 0);
1437
+ },
1438
+ [setLangGraphInterruptAction]
1439
+ );
1440
+ if (!langGraphInterruptAction || !langGraphInterruptAction.event || !langGraphInterruptAction.render)
1441
+ return null;
1442
+ const { render, handler, event, enabled } = langGraphInterruptAction;
1443
+ const conditionsMet = !agentSession || !enabled ? true : enabled({ eventValue: event.value, agentMetadata: agentSession });
1444
+ if (!conditionsMet) {
1445
+ return null;
1446
+ }
1447
+ let result = null;
1448
+ if (handler) {
1449
+ result = handler({
1450
+ event,
1451
+ resolve: resolveInterrupt
1452
+ });
1453
+ }
1454
+ return import_react8.default.createElement(InterruptRenderer, {
1455
+ event,
1456
+ result,
1457
+ render,
1458
+ resolve: resolveInterrupt
1459
+ });
1460
+ }
1461
+
1462
+ // src/hooks/use-copilot-chat.ts
1463
+ var globalSuggestionPromise = null;
1464
+ function useCopilotChat(options = {}) {
1465
+ var _a;
1466
+ const makeSystemMessage2 = (_a = options.makeSystemMessage) != null ? _a : defaultSystemMessage;
1160
1467
  const {
1161
1468
  getContextString,
1162
1469
  getFunctionCallHandler,
@@ -1180,11 +1487,72 @@ function useCopilotChat(_a = {}) {
1180
1487
  extensions,
1181
1488
  setExtensions,
1182
1489
  langGraphInterruptAction,
1183
- setLangGraphInterruptAction
1490
+ setLangGraphInterruptAction,
1491
+ chatSuggestionConfiguration,
1492
+ suggestions,
1493
+ setSuggestions,
1494
+ runtimeClient
1184
1495
  } = useCopilotContext();
1185
1496
  const { messages, setMessages } = useCopilotMessagesContext();
1186
- const [mcpServers, setLocalMcpServers] = (0, import_react8.useState)([]);
1187
- (0, import_react8.useEffect)(() => {
1497
+ const [mcpServers, setLocalMcpServers] = (0, import_react9.useState)([]);
1498
+ const suggestionsAbortControllerRef = (0, import_react9.useRef)(null);
1499
+ const isLoadingSuggestionsRef = (0, import_react9.useRef)(false);
1500
+ const abortSuggestions = (0, import_react9.useCallback)(
1501
+ (clear = true) => {
1502
+ var _a2;
1503
+ (_a2 = suggestionsAbortControllerRef.current) == null ? void 0 : _a2.abort("suggestions aborted by user");
1504
+ suggestionsAbortControllerRef.current = null;
1505
+ if (clear) {
1506
+ setSuggestions([]);
1507
+ }
1508
+ },
1509
+ [setSuggestions]
1510
+ );
1511
+ const stableContext = (0, import_react9.useMemo)(() => {
1512
+ return {
1513
+ actions,
1514
+ copilotApiConfig,
1515
+ chatSuggestionConfiguration,
1516
+ messages,
1517
+ setMessages,
1518
+ getContextString,
1519
+ runtimeClient
1520
+ };
1521
+ }, [
1522
+ JSON.stringify(Object.keys(actions)),
1523
+ copilotApiConfig.chatApiEndpoint,
1524
+ messages.length,
1525
+ Object.keys(chatSuggestionConfiguration).length
1526
+ ]);
1527
+ const generateSuggestionsFunc = (0, import_react9.useCallback)(() => __async(this, null, function* () {
1528
+ if (globalSuggestionPromise) {
1529
+ return globalSuggestionPromise;
1530
+ }
1531
+ globalSuggestionPromise = (() => __async(this, null, function* () {
1532
+ try {
1533
+ abortSuggestions();
1534
+ isLoadingSuggestionsRef.current = true;
1535
+ suggestionsAbortControllerRef.current = new AbortController();
1536
+ setSuggestions([]);
1537
+ yield reloadSuggestions(
1538
+ stableContext,
1539
+ chatSuggestionConfiguration,
1540
+ setSuggestions,
1541
+ suggestionsAbortControllerRef
1542
+ );
1543
+ } catch (error) {
1544
+ throw error;
1545
+ } finally {
1546
+ isLoadingSuggestionsRef.current = false;
1547
+ globalSuggestionPromise = null;
1548
+ }
1549
+ }))();
1550
+ return globalSuggestionPromise;
1551
+ }), [stableContext, chatSuggestionConfiguration, setSuggestions, abortSuggestions]);
1552
+ const resetSuggestions = (0, import_react9.useCallback)(() => {
1553
+ setSuggestions([]);
1554
+ }, [setSuggestions]);
1555
+ (0, import_react9.useEffect)(() => {
1188
1556
  if (mcpServers.length > 0) {
1189
1557
  const serversCopy = [...mcpServers];
1190
1558
  copilotApiConfig.mcpServers = serversCopy;
@@ -1194,7 +1562,7 @@ function useCopilotChat(_a = {}) {
1194
1562
  copilotApiConfig.properties.mcpServers = serversCopy;
1195
1563
  }
1196
1564
  }, [mcpServers, copilotApiConfig]);
1197
- const setMcpServers = (0, import_react8.useCallback)((servers) => {
1565
+ const setMcpServers = (0, import_react9.useCallback)((servers) => {
1198
1566
  setLocalMcpServers(servers);
1199
1567
  }, []);
1200
1568
  const onCoAgentStateRender = useAsyncCallback(
@@ -1215,15 +1583,15 @@ function useCopilotChat(_a = {}) {
1215
1583
  }),
1216
1584
  [coAgentStateRenders]
1217
1585
  );
1218
- const makeSystemMessageCallback = (0, import_react8.useCallback)(() => {
1219
- const systemMessageMaker = makeSystemMessage || defaultSystemMessage;
1586
+ const makeSystemMessageCallback = (0, import_react9.useCallback)(() => {
1587
+ const systemMessageMaker = makeSystemMessage2 || defaultSystemMessage;
1220
1588
  const contextString = getContextString([], defaultCopilotContextCategories);
1221
- return new import_runtime_client_gql4.TextMessage({
1589
+ return new import_runtime_client_gql7.TextMessage({
1222
1590
  content: systemMessageMaker(contextString, chatInstructions),
1223
- role: import_runtime_client_gql4.Role.System
1591
+ role: import_runtime_client_gql7.Role.System
1224
1592
  });
1225
- }, [getContextString, makeSystemMessage, chatInstructions]);
1226
- const deleteMessage = (0, import_react8.useCallback)(
1593
+ }, [getContextString, makeSystemMessage2, chatInstructions]);
1594
+ const deleteMessage = (0, import_react9.useCallback)(
1227
1595
  (messageId) => {
1228
1596
  setMessages((prev) => prev.filter((message) => message.id !== messageId));
1229
1597
  },
@@ -1232,7 +1600,7 @@ function useCopilotChat(_a = {}) {
1232
1600
  const { append, reload, stop, runChatCompletion } = useChat(__spreadProps(__spreadValues({}, options), {
1233
1601
  actions: Object.values(actions),
1234
1602
  copilotConfig: copilotApiConfig,
1235
- initialMessages: options.initialMessages || [],
1603
+ initialMessages: (0, import_runtime_client_gql7.aguiToGQL)(options.initialMessages || []),
1236
1604
  onFunctionCall: getFunctionCallHandler(),
1237
1605
  onCoAgentStateRender,
1238
1606
  messages,
@@ -1259,7 +1627,8 @@ function useCopilotChat(_a = {}) {
1259
1627
  const latestAppend = useUpdatedRef(append);
1260
1628
  const latestAppendFunc = useAsyncCallback(
1261
1629
  (message, options2) => __async(this, null, function* () {
1262
- return yield latestAppend.current(message, options2);
1630
+ abortSuggestions(options2 == null ? void 0 : options2.clearSuggestions);
1631
+ return yield latestAppend.current((0, import_runtime_client_gql7.aguiToGQL)([message])[0], options2);
1263
1632
  }),
1264
1633
  [latestAppend]
1265
1634
  );
@@ -1271,20 +1640,20 @@ function useCopilotChat(_a = {}) {
1271
1640
  [latestReload]
1272
1641
  );
1273
1642
  const latestStop = useUpdatedRef(stop);
1274
- const latestStopFunc = (0, import_react8.useCallback)(() => {
1643
+ const latestStopFunc = (0, import_react9.useCallback)(() => {
1275
1644
  return latestStop.current();
1276
1645
  }, [latestStop]);
1277
1646
  const latestDelete = useUpdatedRef(deleteMessage);
1278
- const latestDeleteFunc = (0, import_react8.useCallback)(
1647
+ const latestDeleteFunc = (0, import_react9.useCallback)(
1279
1648
  (messageId) => {
1280
1649
  return latestDelete.current(messageId);
1281
1650
  },
1282
1651
  [latestDelete]
1283
1652
  );
1284
1653
  const latestSetMessages = useUpdatedRef(setMessages);
1285
- const latestSetMessagesFunc = (0, import_react8.useCallback)(
1654
+ const latestSetMessagesFunc = (0, import_react9.useCallback)(
1286
1655
  (messages2) => {
1287
- return latestSetMessages.current(messages2);
1656
+ return latestSetMessages.current((0, import_runtime_client_gql7.aguiToGQL)(messages2));
1288
1657
  },
1289
1658
  [latestSetMessages]
1290
1659
  );
@@ -1292,7 +1661,7 @@ function useCopilotChat(_a = {}) {
1292
1661
  const latestRunChatCompletionFunc = useAsyncCallback(() => __async(this, null, function* () {
1293
1662
  return yield latestRunChatCompletion.current();
1294
1663
  }), [latestRunChatCompletion]);
1295
- const reset = (0, import_react8.useCallback)(() => {
1664
+ const reset = (0, import_react9.useCallback)(() => {
1296
1665
  latestStopFunc();
1297
1666
  setMessages([]);
1298
1667
  setRunId(null);
@@ -1304,20 +1673,23 @@ function useCopilotChat(_a = {}) {
1304
1673
  };
1305
1674
  }
1306
1675
  setAgentSession(initialAgentSession);
1676
+ resetSuggestions();
1307
1677
  }, [
1308
1678
  latestStopFunc,
1309
1679
  setMessages,
1310
1680
  setThreadId,
1311
1681
  setCoagentStatesWithRef,
1312
1682
  setAgentSession,
1313
- agentLock
1683
+ agentLock,
1684
+ resetSuggestions
1314
1685
  ]);
1315
1686
  const latestReset = useUpdatedRef(reset);
1316
- const latestResetFunc = (0, import_react8.useCallback)(() => {
1687
+ const latestResetFunc = (0, import_react9.useCallback)(() => {
1317
1688
  return latestReset.current();
1318
1689
  }, [latestReset]);
1690
+ const interrupt = useLangGraphInterruptRender();
1319
1691
  return {
1320
- visibleMessages: messages,
1692
+ visibleMessages: (0, import_runtime_client_gql7.gqlToAGUI)(messages, actions, coAgentStateRenders),
1321
1693
  appendMessage: latestAppendFunc,
1322
1694
  setMessages: latestSetMessagesFunc,
1323
1695
  reloadMessages: latestReloadFunc,
@@ -1327,12 +1699,18 @@ function useCopilotChat(_a = {}) {
1327
1699
  runChatCompletion: latestRunChatCompletionFunc,
1328
1700
  isLoading,
1329
1701
  mcpServers,
1330
- setMcpServers
1702
+ setMcpServers,
1703
+ suggestions,
1704
+ setSuggestions,
1705
+ generateSuggestions: generateSuggestionsFunc,
1706
+ resetSuggestions,
1707
+ isLoadingSuggestions: isLoadingSuggestionsRef.current,
1708
+ interrupt
1331
1709
  };
1332
1710
  }
1333
1711
  function useUpdatedRef(value) {
1334
- const ref = (0, import_react8.useRef)(value);
1335
- (0, import_react8.useEffect)(() => {
1712
+ const ref = (0, import_react9.useRef)(value);
1713
+ (0, import_react9.useEffect)(() => {
1336
1714
  ref.current = value;
1337
1715
  }, [value]);
1338
1716
  return ref;