@copilotkit/react-core 1.1.1-feat-runtime-remote-actions.4 → 1.1.1-feat-runtime-remote-actions.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (129) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/dist/{chunk-2MQX7BJL.mjs → chunk-6GOM3DN7.mjs} +2 -2
  3. package/dist/{chunk-IF6P5ZXR.mjs → chunk-6UNB66M3.mjs} +38 -10
  4. package/dist/chunk-6UNB66M3.mjs.map +1 -0
  5. package/dist/{chunk-73KBO5JG.mjs → chunk-73YBF52D.mjs} +2 -2
  6. package/dist/chunk-BWYAGPEF.mjs +1 -0
  7. package/dist/{chunk-DZH3HSXW.mjs → chunk-CHOQ5C7K.mjs} +35 -8
  8. package/dist/chunk-CHOQ5C7K.mjs.map +1 -0
  9. package/dist/{chunk-33ERMMNX.mjs → chunk-DU7PVINC.mjs} +2 -2
  10. package/dist/chunk-IQH77T4S.mjs +44 -0
  11. package/dist/chunk-IQH77T4S.mjs.map +1 -0
  12. package/dist/{chunk-DB4VWZ5Q.mjs → chunk-LTA2O5WH.mjs} +11 -6
  13. package/dist/chunk-LTA2O5WH.mjs.map +1 -0
  14. package/dist/chunk-MTOHRBPJ.mjs +87 -0
  15. package/dist/chunk-MTOHRBPJ.mjs.map +1 -0
  16. package/dist/{chunk-52M7642J.mjs → chunk-PPYZB3IY.mjs} +52 -31
  17. package/dist/chunk-PPYZB3IY.mjs.map +1 -0
  18. package/dist/{chunk-H4XENJME.mjs → chunk-SYJIZLOI.mjs} +2 -2
  19. package/dist/{chunk-3K7JAFGM.mjs → chunk-TFSMXQYS.mjs} +2 -2
  20. package/dist/components/copilot-provider/copilotkit-props.d.ts +4 -0
  21. package/dist/components/copilot-provider/copilotkit-props.js.map +1 -1
  22. package/dist/components/copilot-provider/copilotkit.js +46 -13
  23. package/dist/components/copilot-provider/copilotkit.js.map +1 -1
  24. package/dist/components/copilot-provider/copilotkit.mjs +2 -2
  25. package/dist/components/copilot-provider/index.js +46 -13
  26. package/dist/components/copilot-provider/index.js.map +1 -1
  27. package/dist/components/copilot-provider/index.mjs +2 -2
  28. package/dist/components/index.js +46 -13
  29. package/dist/components/index.js.map +1 -1
  30. package/dist/components/index.mjs +2 -2
  31. package/dist/context/copilot-context.d.ts +18 -8
  32. package/dist/context/copilot-context.js +10 -5
  33. package/dist/context/copilot-context.js.map +1 -1
  34. package/dist/context/copilot-context.mjs +1 -1
  35. package/dist/context/index.d.ts +2 -1
  36. package/dist/context/index.js +10 -5
  37. package/dist/context/index.js.map +1 -1
  38. package/dist/context/index.mjs +1 -1
  39. package/dist/hooks/index.d.ts +4 -2
  40. package/dist/hooks/index.js +205 -89
  41. package/dist/hooks/index.js.map +1 -1
  42. package/dist/hooks/index.mjs +17 -17
  43. package/dist/hooks/use-chat.d.ts +24 -7
  44. package/dist/hooks/use-chat.js +51 -30
  45. package/dist/hooks/use-chat.js.map +1 -1
  46. package/dist/hooks/use-chat.mjs +1 -1
  47. package/dist/hooks/use-coagent-action.d.ts +5 -0
  48. package/dist/hooks/{use-copilot-chat-ui.js → use-coagent-action.js} +48 -22
  49. package/dist/hooks/use-coagent-action.js.map +1 -0
  50. package/dist/hooks/use-coagent-action.mjs +9 -0
  51. package/dist/hooks/use-coagent.d.ts +14 -18
  52. package/dist/hooks/use-coagent.js +184 -17
  53. package/dist/hooks/use-coagent.js.map +1 -1
  54. package/dist/hooks/use-coagent.mjs +6 -20
  55. package/dist/hooks/use-coagent.mjs.map +1 -1
  56. package/dist/hooks/use-copilot-action.js +10 -5
  57. package/dist/hooks/use-copilot-action.js.map +1 -1
  58. package/dist/hooks/use-copilot-action.mjs +2 -2
  59. package/dist/hooks/use-copilot-chat.js +89 -37
  60. package/dist/hooks/use-copilot-chat.js.map +1 -1
  61. package/dist/hooks/use-copilot-chat.mjs +4 -4
  62. package/dist/hooks/use-copilot-readable.js +10 -5
  63. package/dist/hooks/use-copilot-readable.js.map +1 -1
  64. package/dist/hooks/use-copilot-readable.mjs +2 -2
  65. package/dist/hooks/use-make-copilot-document-readable.js +10 -5
  66. package/dist/hooks/use-make-copilot-document-readable.js.map +1 -1
  67. package/dist/hooks/use-make-copilot-document-readable.mjs +2 -2
  68. package/dist/index.d.ts +4 -2
  69. package/dist/index.js +263 -119
  70. package/dist/index.js.map +1 -1
  71. package/dist/index.mjs +21 -21
  72. package/dist/lib/copilot-task.d.ts +2 -1
  73. package/dist/lib/copilot-task.js.map +1 -1
  74. package/dist/lib/copilot-task.mjs +3 -3
  75. package/dist/lib/index.d.ts +2 -1
  76. package/dist/lib/index.js.map +1 -1
  77. package/dist/lib/index.mjs +3 -3
  78. package/dist/types/coagent-action.d.ts +17 -0
  79. package/dist/types/coagent-action.js +19 -0
  80. package/dist/types/coagent-action.js.map +1 -0
  81. package/dist/types/coagent-action.mjs +1 -0
  82. package/dist/types/coagent-state.d.ts +11 -0
  83. package/dist/types/coagent-state.js +19 -0
  84. package/dist/types/coagent-state.js.map +1 -0
  85. package/dist/types/coagent-state.mjs +1 -0
  86. package/dist/types/coagent-state.mjs.map +1 -0
  87. package/dist/utils/extract.d.ts +2 -1
  88. package/dist/utils/extract.js.map +1 -1
  89. package/dist/utils/extract.mjs +3 -3
  90. package/dist/utils/index.d.ts +2 -1
  91. package/dist/utils/index.js.map +1 -1
  92. package/dist/utils/index.mjs +3 -3
  93. package/package.json +5 -5
  94. package/src/components/copilot-provider/copilotkit-props.tsx +5 -0
  95. package/src/components/copilot-provider/copilotkit.tsx +41 -9
  96. package/src/context/copilot-context.tsx +34 -15
  97. package/src/hooks/index.ts +2 -2
  98. package/src/hooks/use-chat.ts +88 -38
  99. package/src/hooks/use-coagent-action.ts +44 -0
  100. package/src/hooks/use-coagent.ts +111 -38
  101. package/src/hooks/use-copilot-chat.ts +29 -2
  102. package/src/types/coagent-action.ts +17 -0
  103. package/src/types/coagent-state.ts +9 -0
  104. package/dist/chunk-52M7642J.mjs.map +0 -1
  105. package/dist/chunk-6YOQY4WD.mjs +0 -20
  106. package/dist/chunk-6YOQY4WD.mjs.map +0 -1
  107. package/dist/chunk-A47L32JN.mjs +0 -52
  108. package/dist/chunk-A47L32JN.mjs.map +0 -1
  109. package/dist/chunk-AIWDXM7L.mjs +0 -1
  110. package/dist/chunk-DB4VWZ5Q.mjs.map +0 -1
  111. package/dist/chunk-DZH3HSXW.mjs.map +0 -1
  112. package/dist/chunk-IF6P5ZXR.mjs.map +0 -1
  113. package/dist/hooks/use-agent-state.d.ts +0 -12
  114. package/dist/hooks/use-agent-state.js +0 -499
  115. package/dist/hooks/use-agent-state.js.map +0 -1
  116. package/dist/hooks/use-agent-state.mjs +0 -19
  117. package/dist/hooks/use-copilot-chat-ui.d.ts +0 -13
  118. package/dist/hooks/use-copilot-chat-ui.js.map +0 -1
  119. package/dist/hooks/use-copilot-chat-ui.mjs +0 -10
  120. package/src/hooks/use-agent-state.ts +0 -63
  121. package/src/hooks/use-copilot-chat-ui.ts +0 -26
  122. /package/dist/{chunk-2MQX7BJL.mjs.map → chunk-6GOM3DN7.mjs.map} +0 -0
  123. /package/dist/{chunk-73KBO5JG.mjs.map → chunk-73YBF52D.mjs.map} +0 -0
  124. /package/dist/{chunk-AIWDXM7L.mjs.map → chunk-BWYAGPEF.mjs.map} +0 -0
  125. /package/dist/{chunk-33ERMMNX.mjs.map → chunk-DU7PVINC.mjs.map} +0 -0
  126. /package/dist/{chunk-H4XENJME.mjs.map → chunk-SYJIZLOI.mjs.map} +0 -0
  127. /package/dist/{chunk-3K7JAFGM.mjs.map → chunk-TFSMXQYS.mjs.map} +0 -0
  128. /package/dist/hooks/{use-agent-state.mjs.map → use-coagent-action.mjs.map} +0 -0
  129. /package/dist/{hooks/use-copilot-chat-ui.mjs.map → types/coagent-action.mjs.map} +0 -0
@@ -79,10 +79,10 @@ var __async = (__this, __arguments, generator) => {
79
79
  // src/hooks/index.ts
80
80
  var hooks_exports = {};
81
81
  __export(hooks_exports, {
82
- useAgentState: () => useAgentState,
82
+ useCoagent: () => useCoagent,
83
+ useCoagentAction: () => useCoagentAction,
83
84
  useCopilotAction: () => useCopilotAction,
84
85
  useCopilotChat: () => useCopilotChat,
85
- useCopilotChatUI: () => useCopilotChatUI,
86
86
  useCopilotReadable: () => useCopilotReadable,
87
87
  useMakeCopilotDocumentReadable: () => useMakeCopilotDocumentReadable
88
88
  });
@@ -99,7 +99,12 @@ var emptyCopilotContext = {
99
99
  },
100
100
  removeAction: () => {
101
101
  },
102
- chatComponentsCache: { current: { actions: {}, chatUI: {} } },
102
+ coagentActions: {},
103
+ setCoagentAction: () => {
104
+ },
105
+ removeCoagentAction: () => {
106
+ },
107
+ chatComponentsCache: { current: { actions: {}, coagentActions: {} } },
103
108
  getContextString: (documents, categories) => returnAndThrowInDebug(""),
104
109
  addContext: () => "",
105
110
  removeContext: () => {
@@ -133,11 +138,11 @@ var emptyCopilotContext = {
133
138
  removeChatSuggestionConfiguration: () => {
134
139
  },
135
140
  showDevConsole: "auto",
136
- agentStates: {},
137
- setAgentStates: () => {
141
+ coagentStates: {},
142
+ setCoagentStates: () => {
138
143
  },
139
- chatUI: [],
140
- setChatUI: () => {
144
+ agentSession: null,
145
+ setAgentSession: () => {
141
146
  }
142
147
  };
143
148
  var CopilotContext = import_react.default.createContext(emptyCopilotContext);
@@ -167,12 +172,15 @@ function useChat(options) {
167
172
  isLoading,
168
173
  actions,
169
174
  onFunctionCall,
170
- setAgentStates
175
+ onCoagentAction,
176
+ setCoagentStates,
177
+ coagentStates,
178
+ agentSession,
179
+ setAgentSession
171
180
  } = options;
172
181
  const abortControllerRef = (0, import_react2.useRef)();
173
182
  const threadIdRef = (0, import_react2.useRef)(null);
174
183
  const runIdRef = (0, import_react2.useRef)(null);
175
- const agentSessionRef = (0, import_react2.useRef)(null);
176
184
  const publicApiKey = copilotConfig.publicApiKey;
177
185
  const headers = __spreadValues(__spreadValues({}, copilotConfig.headers || {}), publicApiKey ? { [import_shared.COPILOT_CLOUD_PUBLIC_API_KEY_HEADER]: publicApiKey } : {});
178
186
  const runtimeClient = new import_runtime_client_gql.CopilotRuntimeClient({
@@ -197,7 +205,7 @@ function useChat(options) {
197
205
  const messagesWithContext = [systemMessage, ...initialMessages || [], ...previousMessages];
198
206
  const stream = import_runtime_client_gql.CopilotRuntimeClient.asStream(
199
207
  runtimeClient.generateCopilotResponse({
200
- data: __spreadValues(__spreadProps(__spreadValues({
208
+ data: __spreadProps(__spreadValues(__spreadProps(__spreadValues({
201
209
  frontend: {
202
210
  actions: actions.map((action) => ({
203
211
  name: action.name,
@@ -221,20 +229,22 @@ function useChat(options) {
221
229
  metadata: {
222
230
  requestType: import_runtime_client_gql.CopilotRequestType.Chat
223
231
  }
224
- }), agentSessionRef.current ? {
225
- agentSession: {
226
- threadId: agentSessionRef.current.threadId,
227
- agentName: agentSessionRef.current.agentName,
228
- nodeName: agentSessionRef.current.nodeName
229
- }
230
- } : {}),
232
+ }), agentSession ? {
233
+ agentSession
234
+ } : {}), {
235
+ agentStates: Object.values(coagentStates).map((state) => ({
236
+ agentName: state.name,
237
+ state: JSON.stringify(state.state)
238
+ }))
239
+ }),
231
240
  properties: copilotConfig.properties,
232
241
  signal: (_d = abortControllerRef.current) == null ? void 0 : _d.signal
233
242
  })
234
243
  );
235
244
  const guardrailsEnabled = ((_g = (_f = (_e = copilotConfig.cloud) == null ? void 0 : _e.guardrails) == null ? void 0 : _f.input) == null ? void 0 : _g.restrictToTopic.enabled) || false;
236
245
  const reader = stream.getReader();
237
- let results = {};
246
+ let actionResults = {};
247
+ let executedCoagentActions = [];
238
248
  try {
239
249
  while (true) {
240
250
  const { done, value } = yield reader.read();
@@ -263,25 +273,28 @@ function useChat(options) {
263
273
  newMessages.push(message);
264
274
  if (message instanceof import_runtime_client_gql.AgentStateMessage) {
265
275
  if (message.running) {
266
- setAgentStates((prevAgentStates) => __spreadProps(__spreadValues({}, prevAgentStates), {
267
- [message.agentName]: message
276
+ setCoagentStates((prevAgentStates) => __spreadProps(__spreadValues({}, prevAgentStates), {
277
+ [message.agentName]: {
278
+ name: message.agentName,
279
+ state: message.state,
280
+ running: message.running,
281
+ active: message.active,
282
+ threadId: message.threadId,
283
+ nodeName: message.nodeName,
284
+ runId: message.runId
285
+ }
268
286
  }));
269
- agentSessionRef.current = {
287
+ setAgentSession({
270
288
  threadId: message.threadId,
271
289
  agentName: message.agentName,
272
290
  nodeName: message.nodeName
273
- };
274
- } else {
275
- setAgentStates((prevAgentStates) => {
276
- const newAgentStates = __spreadValues({}, prevAgentStates);
277
- delete newAgentStates[message.agentName];
278
- return newAgentStates;
279
291
  });
280
- agentSessionRef.current = null;
292
+ } else {
293
+ setAgentSession(null);
281
294
  }
282
295
  }
283
296
  if (message instanceof import_runtime_client_gql.ActionExecutionMessage && message.status.code !== import_runtime_client_gql.MessageStatusCode.Pending && message.scope === "client" && onFunctionCall) {
284
- if (!(message.id in results)) {
297
+ if (!(message.id in actionResults)) {
285
298
  if (guardrailsEnabled && value.generateCopilotResponse.status === void 0) {
286
299
  break;
287
300
  }
@@ -290,26 +303,39 @@ function useChat(options) {
290
303
  name: message.name,
291
304
  args: message.arguments
292
305
  });
293
- results[message.id] = result;
306
+ actionResults[message.id] = result;
294
307
  }
295
308
  newMessages.push(
296
309
  new import_runtime_client_gql.ResultMessage({
297
- result: import_runtime_client_gql.ResultMessage.encodeResult(results[message.id]),
310
+ result: import_runtime_client_gql.ResultMessage.encodeResult(actionResults[message.id]),
298
311
  actionExecutionId: message.id,
299
312
  actionName: message.name
300
313
  })
301
314
  );
302
315
  }
316
+ if (message instanceof import_runtime_client_gql.AgentStateMessage && !message.active && !executedCoagentActions.includes(message.id) && onCoagentAction) {
317
+ if (guardrailsEnabled && value.generateCopilotResponse.status === void 0) {
318
+ break;
319
+ }
320
+ yield onCoagentAction({
321
+ name: message.agentName,
322
+ nodeName: message.nodeName,
323
+ state: message.state
324
+ });
325
+ executedCoagentActions.push(message.id);
326
+ }
303
327
  }
304
328
  }
305
329
  if (newMessages.length > 0) {
306
330
  const filteredMessages = [...previousMessages, ...newMessages].reduce(
307
331
  (acc, message) => {
308
- if (message instanceof import_runtime_client_gql.AgentStateMessage && // Check if the current message is an AgentStateMessage
309
- acc.length > 0 && // Ensure there is at least one message in the accumulator
310
- acc[acc.length - 1] instanceof import_runtime_client_gql.AgentStateMessage && // Check if the last message in the accumulator is also an AgentStateMessage
311
- acc[acc.length - 1].agentName === message.agentName && // Check if the agentName is the same
312
- acc[acc.length - 1].nodeName === message.nodeName) {
332
+ if (
333
+ // If the current message is an AgentStateMessage
334
+ message instanceof import_runtime_client_gql.AgentStateMessage && // And there is at least one message in the accumulator
335
+ acc.length > 0 && // And the last message in the accumulator is also an AgentStateMessage
336
+ acc[acc.length - 1] instanceof import_runtime_client_gql.AgentStateMessage && // And the agentName, nodeName, and runId are the same
337
+ acc[acc.length - 1].agentName === message.agentName && acc[acc.length - 1].nodeName === message.nodeName && acc[acc.length - 1].runId === message.runId
338
+ ) {
313
339
  acc[acc.length - 1] = message;
314
340
  } else {
315
341
  acc.push(message);
@@ -323,7 +349,7 @@ function useChat(options) {
323
349
  }
324
350
  if (
325
351
  // if we have client side results
326
- Object.values(results).length || // or the last message we received is a result
352
+ Object.values(actionResults).length || // or the last message we received is a result
327
353
  newMessages.length && newMessages[newMessages.length - 1] instanceof import_runtime_client_gql.ResultMessage
328
354
  ) {
329
355
  yield new Promise((resolve) => setTimeout(resolve, 10));
@@ -393,7 +419,11 @@ function useCopilotChat(_a = {}) {
393
419
  setIsLoading,
394
420
  chatInstructions,
395
421
  actions,
396
- setAgentStates
422
+ coagentStates,
423
+ setCoagentStates,
424
+ coagentActions,
425
+ agentSession,
426
+ setAgentSession
397
427
  } = (0, import_react4.useContext)(CopilotContext);
398
428
  const latestGetContextString = useUpdatedRef(getContextString);
399
429
  const deleteMessage = (0, import_react4.useCallback)(
@@ -410,17 +440,39 @@ function useCopilotChat(_a = {}) {
410
440
  role: import_runtime_client_gql2.Role.System
411
441
  });
412
442
  }, [getContextString, makeSystemMessage, chatInstructions]);
443
+ const onCoagentAction = (0, import_react4.useCallback)(
444
+ (args) => __async(this, null, function* () {
445
+ var _a2;
446
+ const { name, nodeName, state } = args;
447
+ let action = Object.values(coagentActions).find(
448
+ (action2) => action2.name === name && action2.nodeName === nodeName
449
+ );
450
+ if (!action) {
451
+ action = Object.values(coagentActions).find(
452
+ (action2) => action2.name === name && !action2.nodeName
453
+ );
454
+ }
455
+ if (action) {
456
+ yield (_a2 = action.handler) == null ? void 0 : _a2.call(action, { state, nodeName });
457
+ }
458
+ }),
459
+ [coagentActions]
460
+ );
413
461
  const { append, reload, stop } = useChat(__spreadProps(__spreadValues({}, options), {
414
462
  actions: Object.values(actions),
415
463
  copilotConfig: copilotApiConfig,
416
464
  initialMessages: options.initialMessages || [],
417
465
  onFunctionCall: getFunctionCallHandler(),
466
+ onCoagentAction,
418
467
  messages,
419
468
  setMessages,
420
469
  makeSystemMessageCallback,
421
470
  isLoading,
422
471
  setIsLoading,
423
- setAgentStates
472
+ coagentStates,
473
+ setCoagentStates,
474
+ agentSession,
475
+ setAgentSession
424
476
  }));
425
477
  return {
426
478
  visibleMessages: messages,
@@ -502,12 +554,48 @@ function useCopilotAction(action, dependencies) {
502
554
  ]);
503
555
  }
504
556
 
505
- // src/hooks/use-make-copilot-document-readable.ts
557
+ // src/hooks/use-coagent-action.ts
506
558
  var import_react6 = require("react");
507
- function useMakeCopilotDocumentReadable(document, categories, dependencies = []) {
508
- const { addDocumentContext, removeDocumentContext } = (0, import_react6.useContext)(CopilotContext);
509
- const idRef = (0, import_react6.useRef)();
559
+ var import_shared4 = require("@copilotkit/shared");
560
+ function useCoagentAction(action, dependencies) {
561
+ const { setCoagentAction, removeCoagentAction, coagentActions, chatComponentsCache } = (0, import_react6.useContext)(CopilotContext);
562
+ const idRef = (0, import_react6.useRef)((0, import_shared4.randomId)());
563
+ const key = `${action.name}-${action.nodeName || "global"}`;
564
+ if (dependencies === void 0) {
565
+ if (coagentActions[idRef.current]) {
566
+ coagentActions[idRef.current].handler = action.handler;
567
+ if (typeof action.render === "function") {
568
+ if (chatComponentsCache.current !== null) {
569
+ chatComponentsCache.current.coagentActions[key] = action.render;
570
+ }
571
+ }
572
+ }
573
+ }
510
574
  (0, import_react6.useEffect)(() => {
575
+ setCoagentAction(idRef.current, action);
576
+ if (chatComponentsCache.current !== null && action.render !== void 0) {
577
+ chatComponentsCache.current.coagentActions[key] = action.render;
578
+ }
579
+ return () => {
580
+ removeCoagentAction(idRef.current);
581
+ };
582
+ }, [
583
+ setCoagentAction,
584
+ removeCoagentAction,
585
+ action.name,
586
+ // include render only if it's a string
587
+ typeof action.render === "string" ? action.render : void 0,
588
+ // dependencies set by the developer
589
+ ...dependencies || []
590
+ ]);
591
+ }
592
+
593
+ // src/hooks/use-make-copilot-document-readable.ts
594
+ var import_react7 = require("react");
595
+ function useMakeCopilotDocumentReadable(document, categories, dependencies = []) {
596
+ const { addDocumentContext, removeDocumentContext } = (0, import_react7.useContext)(CopilotContext);
597
+ const idRef = (0, import_react7.useRef)();
598
+ (0, import_react7.useEffect)(() => {
511
599
  const id = addDocumentContext(document, categories);
512
600
  idRef.current = id;
513
601
  return () => {
@@ -518,16 +606,16 @@ function useMakeCopilotDocumentReadable(document, categories, dependencies = [])
518
606
  }
519
607
 
520
608
  // src/hooks/use-copilot-readable.ts
521
- var import_react7 = require("react");
609
+ var import_react8 = require("react");
522
610
  function convertToJSON(description, value) {
523
611
  return `${description}: ${typeof value === "string" ? value : JSON.stringify(value)}`;
524
612
  }
525
613
  function useCopilotReadable({ description, value, parentId, categories, convert }, dependencies) {
526
- const { addContext, removeContext } = (0, import_react7.useContext)(CopilotContext);
527
- const idRef = (0, import_react7.useRef)();
614
+ const { addContext, removeContext } = (0, import_react8.useContext)(CopilotContext);
615
+ const idRef = (0, import_react8.useRef)();
528
616
  convert = convert || convertToJSON;
529
617
  const information = convert(description, value);
530
- (0, import_react7.useEffect)(() => {
618
+ (0, import_react8.useEffect)(() => {
531
619
  const id = addContext(information, parentId, categories);
532
620
  idRef.current = id;
533
621
  return () => {
@@ -537,58 +625,86 @@ function useCopilotReadable({ description, value, parentId, categories, convert
537
625
  return idRef.current;
538
626
  }
539
627
 
540
- // src/hooks/use-agent-state.ts
541
- var import_runtime_client_gql3 = require("@copilotkit/runtime-client-gql");
542
- function useAgentState(agentName) {
543
- const { agentStates, setAgentStates } = useCopilotContext();
544
- const { visibleMessages, setMessages } = useCopilotChat();
545
- const setSpecificAgentState = (newState) => {
546
- setAgentStates((prevAgentStates) => {
547
- const currentState2 = prevAgentStates[agentName];
548
- if (!currentState2) {
549
- throw new Error(`Agent state ${agentName} not found`);
550
- }
551
- const updatedState = typeof newState === "function" ? newState(currentState2) : newState;
552
- const lastAgentStateMessage = [...visibleMessages].reverse().find((message) => message instanceof import_runtime_client_gql3.AgentStateMessage);
553
- const agentStateMessage = new import_runtime_client_gql3.AgentStateMessage({
554
- agentName,
555
- nodeName: lastAgentStateMessage.nodeName,
556
- state: updatedState,
557
- running: lastAgentStateMessage.running,
558
- threadId: lastAgentStateMessage.threadId,
559
- role: lastAgentStateMessage.role
560
- });
561
- setMessages([...visibleMessages, agentStateMessage]);
628
+ // src/hooks/use-coagent.ts
629
+ var import_react9 = require("react");
630
+ function useCoagent(options) {
631
+ const isExternalStateManagement = (options2) => {
632
+ return "state" in options2 && "setState" in options2;
633
+ };
634
+ const { name } = options;
635
+ const isInternalStateManagementWithInitial = (options2) => {
636
+ return "initialState" in options2;
637
+ };
638
+ const { coagentStates, setCoagentStates } = useCopilotContext();
639
+ const getCoagentState = (coagentStates2, name2) => {
640
+ if (coagentStates2[name2]) {
641
+ return coagentStates2[name2];
642
+ } else {
643
+ return {
644
+ name: name2,
645
+ state: isInternalStateManagementWithInitial(options) ? options.initialState : {},
646
+ running: false,
647
+ active: false,
648
+ threadId: void 0,
649
+ nodeName: void 0,
650
+ runId: void 0
651
+ };
652
+ }
653
+ };
654
+ const setState = (newState) => {
655
+ setCoagentStates((prevAgentStates) => {
656
+ let coagentState2 = getCoagentState(prevAgentStates, name);
657
+ const updatedState = typeof newState === "function" ? newState(coagentState2.state) : newState;
562
658
  return __spreadProps(__spreadValues({}, prevAgentStates), {
563
- [agentName]: agentStateMessage
659
+ [name]: __spreadProps(__spreadValues({}, coagentState2), {
660
+ state: updatedState
661
+ })
564
662
  });
565
663
  });
566
664
  };
567
- const currentState = agentStates[agentName];
568
- return [
569
- currentState == null ? void 0 : currentState.state,
570
- setSpecificAgentState,
571
- currentState ? { nodeName: currentState.nodeName } : void 0
572
- ];
665
+ const coagentState = getCoagentState(coagentStates, name);
666
+ const state = isExternalStateManagement(options) ? options.state : coagentState.state;
667
+ (0, import_react9.useEffect)(() => {
668
+ if (isExternalStateManagement(options)) {
669
+ setState(options.state);
670
+ } else if (coagentStates[name] === void 0) {
671
+ setState(options.initialState === void 0 ? {} : options.initialState);
672
+ }
673
+ }, [isExternalStateManagement(options) ? JSON.stringify(options.state) : void 0]);
674
+ return {
675
+ name,
676
+ nodeName: coagentState.nodeName,
677
+ state,
678
+ setState,
679
+ running: coagentState.running,
680
+ start: () => {
681
+ startAgent(name);
682
+ },
683
+ stop: () => {
684
+ stopAgent(name);
685
+ }
686
+ };
573
687
  }
574
-
575
- // src/hooks/use-copilot-chat-ui.ts
576
- var import_react8 = require("react");
577
- function useCopilotChatUI(chatUI, dependencies) {
578
- const { chatUI: chatUIFromContext, setChatUI } = useCopilotContext();
579
- (0, import_react8.useEffect)(() => {
580
- setChatUI([...chatUIFromContext, chatUI]);
581
- return () => {
582
- setChatUI(chatUIFromContext.filter((ui) => ui !== chatUI));
583
- };
584
- }, [chatUI.agentName, chatUI.nodeName, setChatUI]);
688
+ function startAgent(name) {
689
+ const { setAgentSession } = useCopilotContext();
690
+ setAgentSession({
691
+ agentName: name
692
+ });
693
+ }
694
+ function stopAgent(name) {
695
+ const { agentSession, setAgentSession } = useCopilotContext();
696
+ if (agentSession && agentSession.agentName === name) {
697
+ setAgentSession(null);
698
+ } else {
699
+ console.warn(`No agent session found for ${name}`);
700
+ }
585
701
  }
586
702
  // Annotate the CommonJS export names for ESM import in node:
587
703
  0 && (module.exports = {
588
- useAgentState,
704
+ useCoagent,
705
+ useCoagentAction,
589
706
  useCopilotAction,
590
707
  useCopilotChat,
591
- useCopilotChatUI,
592
708
  useCopilotReadable,
593
709
  useMakeCopilotDocumentReadable
594
710
  });