@copilotkit/react-core 1.6.0-next.5 → 1.6.0-next.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -2460,9 +2460,6 @@ function useCoAgent(options) {
2460
2460
  const { addToast } = useToast();
2461
2461
  const lastLoadedThreadId = (0, import_react17.useRef)();
2462
2462
  const lastLoadedState = (0, import_react17.useRef)();
2463
- const isExternalStateManagement = (options2) => {
2464
- return "state" in options2 && "setState" in options2;
2465
- };
2466
2463
  const { name } = options;
2467
2464
  (0, import_react17.useEffect)(() => {
2468
2465
  if ((availableAgents == null ? void 0 : availableAgents.length) && !availableAgents.some((a) => a.name === name)) {
@@ -2471,44 +2468,27 @@ function useCoAgent(options) {
2471
2468
  addToast({ type: "warning", message });
2472
2469
  }
2473
2470
  }, [availableAgents]);
2474
- const isInternalStateManagementWithInitial = (options2) => {
2475
- return "initialState" in options2;
2476
- };
2477
2471
  const messagesContext = useCopilotMessagesContext();
2478
2472
  const context = __spreadValues(__spreadValues({}, generalContext), messagesContext);
2479
2473
  const { coagentStates, coagentStatesRef, setCoagentStatesWithRef, threadId, copilotApiConfig } = context;
2480
2474
  const { appendMessage, runChatCompletion } = useCopilotChat();
2481
- const getCoagentState = (coagentStates2, name2) => {
2482
- var _a;
2483
- if (coagentStates2[name2]) {
2484
- return coagentStates2[name2];
2485
- } else {
2486
- return {
2487
- name: name2,
2488
- state: isInternalStateManagementWithInitial(options) ? options.initialState : {},
2489
- configurable: (_a = options.configurable) != null ? _a : {},
2490
- running: false,
2491
- active: false,
2492
- threadId: void 0,
2493
- nodeName: void 0,
2494
- runId: void 0
2495
- };
2496
- }
2497
- };
2498
2475
  const runtimeClient = useCopilotRuntimeClient({
2499
2476
  url: copilotApiConfig.chatApiEndpoint,
2500
2477
  publicApiKey: copilotApiConfig.publicApiKey,
2501
2478
  credentials: copilotApiConfig.credentials
2502
2479
  });
2503
- const setState = (newState) => {
2504
- let coagentState2 = getCoagentState(coagentStatesRef.current || {}, name);
2505
- const updatedState = typeof newState === "function" ? newState(coagentState2.state) : newState;
2506
- setCoagentStatesWithRef(__spreadProps(__spreadValues({}, coagentStatesRef.current), {
2507
- [name]: __spreadProps(__spreadValues({}, coagentState2), {
2508
- state: updatedState
2509
- })
2510
- }));
2511
- };
2480
+ const setState = (0, import_react17.useCallback)(
2481
+ (newState) => {
2482
+ let coagentState = getCoagentState({ coagentStates, name, options });
2483
+ const updatedState = typeof newState === "function" ? newState(coagentState.state) : newState;
2484
+ setCoagentStatesWithRef(__spreadProps(__spreadValues({}, coagentStatesRef.current), {
2485
+ [name]: __spreadProps(__spreadValues({}, coagentState), {
2486
+ state: updatedState
2487
+ })
2488
+ }));
2489
+ },
2490
+ [coagentStates, name]
2491
+ );
2512
2492
  (0, import_react17.useEffect)(() => {
2513
2493
  const fetchAgentState = () => __async(this, null, function* () {
2514
2494
  var _a, _b, _c, _d;
@@ -2530,7 +2510,6 @@ function useCoAgent(options) {
2530
2510
  });
2531
2511
  void fetchAgentState();
2532
2512
  }, [threadId]);
2533
- const coagentState = getCoagentState(coagentStates, name);
2534
2513
  (0, import_react17.useEffect)(() => {
2535
2514
  if (isExternalStateManagement(options)) {
2536
2515
  setState(options.state);
@@ -2548,17 +2527,20 @@ function useCoAgent(options) {
2548
2527
  }),
2549
2528
  [name, context, appendMessage, runChatCompletion]
2550
2529
  );
2551
- return {
2552
- name,
2553
- nodeName: coagentState.nodeName,
2554
- threadId: coagentState.threadId,
2555
- running: coagentState.running,
2556
- state: coagentState.state,
2557
- setState: isExternalStateManagement(options) ? options.setState : setState,
2558
- start: () => startAgent(name, context),
2559
- stop: () => stopAgent(name, context),
2560
- run: runAgentCallback
2561
- };
2530
+ return (0, import_react17.useMemo)(() => {
2531
+ const coagentState = getCoagentState({ coagentStates, name, options });
2532
+ return {
2533
+ name,
2534
+ nodeName: coagentState.nodeName,
2535
+ threadId: coagentState.threadId,
2536
+ running: coagentState.running,
2537
+ state: coagentState.state,
2538
+ setState: isExternalStateManagement(options) ? options.setState : setState,
2539
+ start: () => startAgent(name, context),
2540
+ stop: () => stopAgent(name, context),
2541
+ run: runAgentCallback
2542
+ };
2543
+ }, [name, coagentStates, options, setState, runAgentCallback]);
2562
2544
  }
2563
2545
  function startAgent(name, context) {
2564
2546
  const { setAgentSession } = context;
@@ -2614,6 +2596,33 @@ function runAgent(name, context, appendMessage, runChatCompletion, hint) {
2614
2596
  }
2615
2597
  });
2616
2598
  }
2599
+ var isExternalStateManagement = (options) => {
2600
+ return "state" in options && "setState" in options;
2601
+ };
2602
+ var isInternalStateManagementWithInitial = (options) => {
2603
+ return "initialState" in options;
2604
+ };
2605
+ var getCoagentState = ({
2606
+ coagentStates,
2607
+ name,
2608
+ options
2609
+ }) => {
2610
+ var _a;
2611
+ if (coagentStates[name]) {
2612
+ return coagentStates[name];
2613
+ } else {
2614
+ return {
2615
+ name,
2616
+ state: isInternalStateManagementWithInitial(options) ? options.initialState : {},
2617
+ configurable: (_a = options.configurable) != null ? _a : {},
2618
+ running: false,
2619
+ active: false,
2620
+ threadId: void 0,
2621
+ nodeName: void 0,
2622
+ runId: void 0
2623
+ };
2624
+ }
2625
+ };
2617
2626
 
2618
2627
  // src/hooks/use-copilot-authenticated-action.ts
2619
2628
  var import_react18 = require("react");