@copilotkit/react-core 1.51.3-next.6 → 1.51.3-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/CHANGELOG.md +10 -0
- package/dist/chunk-77IVITG3.mjs +158 -0
- package/dist/chunk-77IVITG3.mjs.map +1 -0
- package/dist/chunk-BKMJ4LC7.mjs +119 -0
- package/dist/chunk-BKMJ4LC7.mjs.map +1 -0
- package/dist/chunk-C3YJYDK4.mjs +189 -0
- package/dist/chunk-C3YJYDK4.mjs.map +1 -0
- package/dist/{chunk-GIU66J37.mjs → chunk-DQXCQWSG.mjs} +47 -5
- package/dist/chunk-DQXCQWSG.mjs.map +1 -0
- package/dist/{chunk-HBMPXNW2.mjs → chunk-LO4RRITI.mjs} +71 -18
- package/dist/chunk-LO4RRITI.mjs.map +1 -0
- package/dist/{chunk-3G4VFRVV.mjs → chunk-NXHQDCZF.mjs} +2 -2
- package/dist/{chunk-FDOMAPJY.mjs → chunk-QD7EID4N.mjs} +1 -1
- package/dist/chunk-QD7EID4N.mjs.map +1 -0
- package/dist/{chunk-YTQHRJUA.mjs → chunk-VKNLTZJE.mjs} +2 -2
- package/dist/{chunk-4RRUJHCI.mjs → chunk-VP43SLSZ.mjs} +2 -2
- package/dist/{chunk-MF2ZSLBV.mjs → chunk-XZFIJ7XF.mjs} +2 -2
- package/dist/components/copilot-provider/copilotkit.js +437 -150
- package/dist/components/copilot-provider/copilotkit.js.map +1 -1
- package/dist/components/copilot-provider/copilotkit.mjs +5 -3
- package/dist/components/copilot-provider/index.js +437 -150
- package/dist/components/copilot-provider/index.js.map +1 -1
- package/dist/components/copilot-provider/index.mjs +5 -3
- package/dist/components/index.js +437 -150
- package/dist/components/index.js.map +1 -1
- package/dist/components/index.mjs +5 -3
- package/dist/context/coagent-state-renders-context.d.ts +1 -0
- package/dist/context/coagent-state-renders-context.js.map +1 -1
- package/dist/context/coagent-state-renders-context.mjs +1 -1
- package/dist/context/index.js.map +1 -1
- package/dist/context/index.mjs +1 -1
- package/dist/hooks/index.js +512 -212
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/index.mjs +19 -17
- package/dist/hooks/use-coagent-state-render-bridge.helpers.d.ts +92 -0
- package/dist/hooks/use-coagent-state-render-bridge.helpers.js +231 -0
- package/dist/hooks/use-coagent-state-render-bridge.helpers.js.map +1 -0
- package/dist/hooks/use-coagent-state-render-bridge.helpers.mjs +24 -0
- package/dist/hooks/use-coagent-state-render-bridge.helpers.mjs.map +1 -0
- package/dist/hooks/use-coagent-state-render-bridge.js +334 -72
- package/dist/hooks/use-coagent-state-render-bridge.js.map +1 -1
- package/dist/hooks/use-coagent-state-render-bridge.mjs +4 -2
- package/dist/hooks/use-coagent-state-render-registry.d.ts +25 -0
- package/dist/hooks/use-coagent-state-render-registry.js +358 -0
- package/dist/hooks/use-coagent-state-render-registry.js.map +1 -0
- package/dist/hooks/use-coagent-state-render-registry.mjs +9 -0
- package/dist/hooks/use-coagent-state-render-registry.mjs.map +1 -0
- package/dist/hooks/use-coagent-state-render.js.map +1 -1
- package/dist/hooks/use-coagent-state-render.mjs +2 -2
- package/dist/hooks/use-copilot-chat-headless_c.js +414 -114
- package/dist/hooks/use-copilot-chat-headless_c.js.map +1 -1
- package/dist/hooks/use-copilot-chat-headless_c.mjs +7 -5
- package/dist/hooks/use-copilot-chat.js +406 -106
- package/dist/hooks/use-copilot-chat.js.map +1 -1
- package/dist/hooks/use-copilot-chat.mjs +7 -5
- package/dist/hooks/use-copilot-chat_internal.js +406 -106
- package/dist/hooks/use-copilot-chat_internal.js.map +1 -1
- package/dist/hooks/use-copilot-chat_internal.mjs +6 -4
- package/dist/hooks/use-langgraph-interrupt-render.mjs +1 -1
- package/dist/index.js +651 -311
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +22 -20
- package/dist/lib/copilot-task.js.map +1 -1
- package/dist/lib/copilot-task.mjs +6 -4
- package/dist/lib/index.js.map +1 -1
- package/dist/lib/index.mjs +6 -4
- package/dist/setupTests.js +1 -0
- package/dist/setupTests.js.map +1 -1
- package/dist/setupTests.mjs +1 -0
- package/dist/setupTests.mjs.map +1 -1
- package/dist/test-helpers/copilot-context.d.ts +14 -0
- package/dist/test-helpers/copilot-context.js +128 -0
- package/dist/test-helpers/copilot-context.js.map +1 -0
- package/dist/test-helpers/copilot-context.mjs +74 -0
- package/dist/test-helpers/copilot-context.mjs.map +1 -0
- package/dist/types/index.mjs +1 -1
- package/package.json +5 -5
- package/src/components/copilot-provider/copilotkit.tsx +56 -0
- package/src/context/coagent-state-renders-context.tsx +1 -0
- package/src/hooks/__tests__/use-coagent-state-render-bridge.helpers.test.ts +100 -0
- package/src/hooks/__tests__/use-coagent-state-render.e2e.test.tsx +892 -37
- package/src/hooks/__tests__/use-coagent-state-render.test.tsx +334 -0
- package/src/hooks/use-coagent-state-render-bridge.helpers.ts +311 -0
- package/src/hooks/use-coagent-state-render-bridge.tsx +25 -120
- package/src/hooks/use-coagent-state-render-registry.ts +215 -0
- package/src/hooks/use-copilot-chat_internal.ts +93 -34
- package/src/setupTests.ts +1 -0
- package/src/test-helpers/copilot-context.ts +91 -0
- package/dist/chunk-3X3I7OJV.mjs +0 -172
- package/dist/chunk-3X3I7OJV.mjs.map +0 -1
- package/dist/chunk-FDOMAPJY.mjs.map +0 -1
- package/dist/chunk-GIU66J37.mjs.map +0 -1
- package/dist/chunk-HBMPXNW2.mjs.map +0 -1
- /package/dist/{chunk-3G4VFRVV.mjs.map → chunk-NXHQDCZF.mjs.map} +0 -0
- /package/dist/{chunk-YTQHRJUA.mjs.map → chunk-VKNLTZJE.mjs.map} +0 -0
- /package/dist/{chunk-4RRUJHCI.mjs.map → chunk-VP43SLSZ.mjs.map} +0 -0
- /package/dist/{chunk-MF2ZSLBV.mjs.map → chunk-XZFIJ7XF.mjs.map} +0 -0
package/dist/hooks/index.js
CHANGED
|
@@ -102,7 +102,7 @@ __export(hooks_exports, {
|
|
|
102
102
|
module.exports = __toCommonJS(hooks_exports);
|
|
103
103
|
|
|
104
104
|
// src/hooks/use-copilot-chat_internal.ts
|
|
105
|
-
var
|
|
105
|
+
var import_react14 = require("react");
|
|
106
106
|
|
|
107
107
|
// src/context/copilot-context.tsx
|
|
108
108
|
var import_react = __toESM(require("react"));
|
|
@@ -491,7 +491,7 @@ function useLangGraphInterruptRender(agent) {
|
|
|
491
491
|
}
|
|
492
492
|
|
|
493
493
|
// src/hooks/use-copilot-chat_internal.ts
|
|
494
|
-
var
|
|
494
|
+
var import_react15 = require("@copilotkitnext/react");
|
|
495
495
|
|
|
496
496
|
// src/hooks/use-lazy-tool-renderer.tsx
|
|
497
497
|
var import_react9 = require("@copilotkitnext/react");
|
|
@@ -522,8 +522,11 @@ function useLazyToolRenderer() {
|
|
|
522
522
|
var import_client = require("@ag-ui/client");
|
|
523
523
|
|
|
524
524
|
// src/hooks/use-coagent-state-render-bridge.tsx
|
|
525
|
-
var
|
|
526
|
-
var
|
|
525
|
+
var import_react12 = require("@copilotkitnext/react");
|
|
526
|
+
var import_react13 = require("react");
|
|
527
|
+
var import_shared4 = require("@copilotkit/shared");
|
|
528
|
+
|
|
529
|
+
// src/hooks/use-coagent-state-render-bridge.helpers.ts
|
|
527
530
|
var import_shared3 = require("@copilotkit/shared");
|
|
528
531
|
function getStateWithoutConstantKeys(state) {
|
|
529
532
|
if (!state)
|
|
@@ -546,16 +549,294 @@ function areStatesEquals(a, b) {
|
|
|
546
549
|
]);
|
|
547
550
|
return JSON.stringify(aWithoutConstantKeys) === JSON.stringify(bWithoutConstantKeys);
|
|
548
551
|
}
|
|
552
|
+
function isPlaceholderMessageId(messageId) {
|
|
553
|
+
return !!messageId && messageId.startsWith("coagent-state-render-");
|
|
554
|
+
}
|
|
555
|
+
function isPlaceholderMessageName(messageName) {
|
|
556
|
+
return messageName === "coagent-state-render";
|
|
557
|
+
}
|
|
558
|
+
function readCachedMessageEntry(entry) {
|
|
559
|
+
if (!entry || typeof entry !== "object") {
|
|
560
|
+
return { snapshot: entry, runId: void 0 };
|
|
561
|
+
}
|
|
562
|
+
const snapshot = "snapshot" in entry ? entry.snapshot : entry;
|
|
563
|
+
const runId = "runId" in entry ? entry.runId : void 0;
|
|
564
|
+
return { snapshot, runId };
|
|
565
|
+
}
|
|
566
|
+
function getEffectiveRunId({
|
|
567
|
+
existingClaimRunId,
|
|
568
|
+
cachedMessageRunId,
|
|
569
|
+
runId
|
|
570
|
+
}) {
|
|
571
|
+
return existingClaimRunId || cachedMessageRunId || runId || "pending";
|
|
572
|
+
}
|
|
573
|
+
function resolveClaim({
|
|
574
|
+
claims,
|
|
575
|
+
context,
|
|
576
|
+
stateSnapshot
|
|
577
|
+
}) {
|
|
578
|
+
const { messageId, stateRenderId, runId, messageIndex } = context;
|
|
579
|
+
const existing = claims[messageId];
|
|
580
|
+
if (existing) {
|
|
581
|
+
const canRender = existing.stateRenderId === stateRenderId;
|
|
582
|
+
const shouldUpdateRunId = canRender && runId && (!existing.runId || existing.runId === "pending");
|
|
583
|
+
return {
|
|
584
|
+
canRender,
|
|
585
|
+
action: canRender ? "existing" /* Existing */ : "block" /* Block */,
|
|
586
|
+
updateRunId: shouldUpdateRunId ? runId : void 0
|
|
587
|
+
};
|
|
588
|
+
}
|
|
589
|
+
const normalizedRunId = runId != null ? runId : "pending";
|
|
590
|
+
const renderClaimedByOtherMessageEntry = Object.entries(claims).find(
|
|
591
|
+
([, claim]) => {
|
|
592
|
+
var _a;
|
|
593
|
+
return claim.stateRenderId === stateRenderId && ((_a = claim.runId) != null ? _a : "pending") === normalizedRunId && (0, import_shared3.dataToUUID)(getStateWithoutConstantKeys(claim.stateSnapshot)) === (0, import_shared3.dataToUUID)(getStateWithoutConstantKeys(stateSnapshot));
|
|
594
|
+
}
|
|
595
|
+
);
|
|
596
|
+
const renderClaimedByOtherMessage = renderClaimedByOtherMessageEntry == null ? void 0 : renderClaimedByOtherMessageEntry[1];
|
|
597
|
+
const claimedMessageId = renderClaimedByOtherMessageEntry == null ? void 0 : renderClaimedByOtherMessageEntry[0];
|
|
598
|
+
if (renderClaimedByOtherMessage) {
|
|
599
|
+
if (messageIndex !== void 0 && renderClaimedByOtherMessage.messageIndex !== void 0 && messageIndex > renderClaimedByOtherMessage.messageIndex) {
|
|
600
|
+
return {
|
|
601
|
+
canRender: true,
|
|
602
|
+
action: "override" /* Override */,
|
|
603
|
+
nextClaim: { stateRenderId, runId, messageIndex },
|
|
604
|
+
lockOthers: runId === renderClaimedByOtherMessage.runId || isPlaceholderMessageId(claimedMessageId)
|
|
605
|
+
};
|
|
606
|
+
}
|
|
607
|
+
if (runId && renderClaimedByOtherMessage.runId && runId !== renderClaimedByOtherMessage.runId) {
|
|
608
|
+
return {
|
|
609
|
+
canRender: true,
|
|
610
|
+
action: "override" /* Override */,
|
|
611
|
+
nextClaim: { stateRenderId, runId, messageIndex },
|
|
612
|
+
lockOthers: isPlaceholderMessageId(claimedMessageId)
|
|
613
|
+
};
|
|
614
|
+
}
|
|
615
|
+
if (isPlaceholderMessageId(claimedMessageId)) {
|
|
616
|
+
return {
|
|
617
|
+
canRender: true,
|
|
618
|
+
action: "override" /* Override */,
|
|
619
|
+
nextClaim: { stateRenderId, runId, messageIndex },
|
|
620
|
+
lockOthers: true
|
|
621
|
+
};
|
|
622
|
+
}
|
|
623
|
+
if (stateSnapshot && renderClaimedByOtherMessage.stateSnapshot && !areStatesEquals(renderClaimedByOtherMessage.stateSnapshot, stateSnapshot)) {
|
|
624
|
+
return {
|
|
625
|
+
canRender: true,
|
|
626
|
+
action: "override" /* Override */,
|
|
627
|
+
nextClaim: { stateRenderId, runId }
|
|
628
|
+
};
|
|
629
|
+
}
|
|
630
|
+
return { canRender: false, action: "block" /* Block */ };
|
|
631
|
+
}
|
|
632
|
+
if (!runId) {
|
|
633
|
+
return { canRender: false, action: "block" /* Block */ };
|
|
634
|
+
}
|
|
635
|
+
return {
|
|
636
|
+
canRender: true,
|
|
637
|
+
action: "create" /* Create */,
|
|
638
|
+
nextClaim: { stateRenderId, runId, messageIndex }
|
|
639
|
+
};
|
|
640
|
+
}
|
|
641
|
+
function selectSnapshot({
|
|
642
|
+
messageId,
|
|
643
|
+
messageName,
|
|
644
|
+
allowLiveState,
|
|
645
|
+
skipLatestCache,
|
|
646
|
+
stateRenderId,
|
|
647
|
+
effectiveRunId,
|
|
648
|
+
stateSnapshotProp,
|
|
649
|
+
agentState,
|
|
650
|
+
agentMessages,
|
|
651
|
+
existingClaim,
|
|
652
|
+
caches
|
|
653
|
+
}) {
|
|
654
|
+
var _a, _b, _c, _d, _e, _f;
|
|
655
|
+
const lastAssistantId = agentMessages ? (_a = [...agentMessages].reverse().find((msg) => msg.role === "assistant")) == null ? void 0 : _a.id : void 0;
|
|
656
|
+
const latestSnapshot = stateRenderId !== void 0 ? caches.byStateRenderAndRun[`${stateRenderId}::latest`] : void 0;
|
|
657
|
+
const messageIndex = agentMessages ? agentMessages.findIndex((msg) => msg.id === messageId) : -1;
|
|
658
|
+
const messageRole = messageIndex >= 0 && agentMessages ? (_b = agentMessages[messageIndex]) == null ? void 0 : _b.role : void 0;
|
|
659
|
+
let previousUserMessageId;
|
|
660
|
+
if (messageIndex > 0 && agentMessages) {
|
|
661
|
+
for (let i = messageIndex - 1; i >= 0; i -= 1) {
|
|
662
|
+
if (((_c = agentMessages[i]) == null ? void 0 : _c.role) === "user") {
|
|
663
|
+
previousUserMessageId = (_d = agentMessages[i]) == null ? void 0 : _d.id;
|
|
664
|
+
break;
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
const liveStateIsStale = stateSnapshotProp === void 0 && latestSnapshot !== void 0 && agentState !== void 0 && areStatesEquals(latestSnapshot, agentState);
|
|
669
|
+
const shouldUseLiveState = (Boolean(allowLiveState) || !lastAssistantId || messageId === lastAssistantId) && !liveStateIsStale;
|
|
670
|
+
const snapshot = stateSnapshotProp ? (0, import_shared3.parseJson)(stateSnapshotProp, stateSnapshotProp) : shouldUseLiveState ? agentState : void 0;
|
|
671
|
+
const hasSnapshotKeys = !!(snapshot && Object.keys(snapshot).length > 0);
|
|
672
|
+
const allowEmptySnapshot = snapshot !== void 0 && !hasSnapshotKeys && (stateSnapshotProp !== void 0 || shouldUseLiveState);
|
|
673
|
+
const messageCacheEntry = caches.byMessageId[messageId];
|
|
674
|
+
const cachedMessageSnapshot = readCachedMessageEntry(messageCacheEntry).snapshot;
|
|
675
|
+
const cacheKey = stateRenderId !== void 0 ? `${stateRenderId}::${effectiveRunId}` : void 0;
|
|
676
|
+
let cachedSnapshot = cachedMessageSnapshot != null ? cachedMessageSnapshot : caches.byMessageId[messageId];
|
|
677
|
+
if (cachedSnapshot === void 0 && cacheKey && caches.byStateRenderAndRun[cacheKey] !== void 0) {
|
|
678
|
+
cachedSnapshot = caches.byStateRenderAndRun[cacheKey];
|
|
679
|
+
}
|
|
680
|
+
if (cachedSnapshot === void 0 && stateRenderId && previousUserMessageId && caches.byStateRenderAndRun[`${stateRenderId}::pending:${previousUserMessageId}`] !== void 0) {
|
|
681
|
+
cachedSnapshot = caches.byStateRenderAndRun[`${stateRenderId}::pending:${previousUserMessageId}`];
|
|
682
|
+
}
|
|
683
|
+
if (cachedSnapshot === void 0 && !skipLatestCache && stateRenderId && messageRole !== "assistant" && (stateSnapshotProp !== void 0 || agentState && Object.keys(agentState).length > 0)) {
|
|
684
|
+
cachedSnapshot = caches.byStateRenderAndRun[`${stateRenderId}::latest`];
|
|
685
|
+
}
|
|
686
|
+
const snapshotForClaim = (existingClaim == null ? void 0 : existingClaim.locked) ? (_e = existingClaim.stateSnapshot) != null ? _e : cachedSnapshot : hasSnapshotKeys ? snapshot : (_f = existingClaim == null ? void 0 : existingClaim.stateSnapshot) != null ? _f : cachedSnapshot;
|
|
687
|
+
return { snapshot, hasSnapshotKeys, cachedSnapshot, allowEmptySnapshot, snapshotForClaim };
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
// src/hooks/use-coagent-state-render-registry.ts
|
|
691
|
+
var import_react11 = require("react");
|
|
692
|
+
var LAST_SNAPSHOTS_BY_RENDER_AND_RUN = "__lastSnapshotsByStateRenderIdAndRun";
|
|
693
|
+
var LAST_SNAPSHOTS_BY_MESSAGE = "__lastSnapshotsByMessageId";
|
|
694
|
+
function getClaimsStore(claimsRef) {
|
|
695
|
+
return claimsRef.current;
|
|
696
|
+
}
|
|
697
|
+
function getSnapshotCaches(claimsRef) {
|
|
698
|
+
var _a, _b;
|
|
699
|
+
const store = getClaimsStore(claimsRef);
|
|
700
|
+
return {
|
|
701
|
+
byStateRenderAndRun: (_a = store[LAST_SNAPSHOTS_BY_RENDER_AND_RUN]) != null ? _a : {},
|
|
702
|
+
byMessageId: (_b = store[LAST_SNAPSHOTS_BY_MESSAGE]) != null ? _b : {}
|
|
703
|
+
};
|
|
704
|
+
}
|
|
705
|
+
function useStateRenderRegistry({
|
|
706
|
+
agentId,
|
|
707
|
+
stateRenderId,
|
|
708
|
+
message,
|
|
709
|
+
messageIndex,
|
|
710
|
+
stateSnapshot,
|
|
711
|
+
agentState,
|
|
712
|
+
agentMessages,
|
|
713
|
+
claimsRef
|
|
714
|
+
}) {
|
|
715
|
+
var _a, _b, _c, _d, _e, _f;
|
|
716
|
+
const store = getClaimsStore(claimsRef);
|
|
717
|
+
const runId = message.runId;
|
|
718
|
+
const cachedMessageEntry = (_a = store[LAST_SNAPSHOTS_BY_MESSAGE]) == null ? void 0 : _a[message.id];
|
|
719
|
+
const { runId: cachedMessageRunId } = readCachedMessageEntry(cachedMessageEntry);
|
|
720
|
+
const existingClaimRunId = (_b = claimsRef.current[message.id]) == null ? void 0 : _b.runId;
|
|
721
|
+
const effectiveRunId = getEffectiveRunId({
|
|
722
|
+
existingClaimRunId,
|
|
723
|
+
cachedMessageRunId,
|
|
724
|
+
runId
|
|
725
|
+
});
|
|
726
|
+
(0, import_react11.useEffect)(() => {
|
|
727
|
+
return () => {
|
|
728
|
+
var _a2, _b2, _c2, _d2;
|
|
729
|
+
const existingClaim2 = claimsRef.current[message.id];
|
|
730
|
+
if ((existingClaim2 == null ? void 0 : existingClaim2.stateSnapshot) && Object.keys(existingClaim2.stateSnapshot).length > 0) {
|
|
731
|
+
const snapshotCache = __spreadValues({}, (_a2 = store[LAST_SNAPSHOTS_BY_RENDER_AND_RUN]) != null ? _a2 : {});
|
|
732
|
+
const cacheKey = `${existingClaim2.stateRenderId}::${(_b2 = existingClaim2.runId) != null ? _b2 : "pending"}`;
|
|
733
|
+
snapshotCache[cacheKey] = existingClaim2.stateSnapshot;
|
|
734
|
+
snapshotCache[`${existingClaim2.stateRenderId}::latest`] = existingClaim2.stateSnapshot;
|
|
735
|
+
store[LAST_SNAPSHOTS_BY_RENDER_AND_RUN] = snapshotCache;
|
|
736
|
+
const messageCache = __spreadValues({}, (_c2 = store[LAST_SNAPSHOTS_BY_MESSAGE]) != null ? _c2 : {});
|
|
737
|
+
messageCache[message.id] = {
|
|
738
|
+
snapshot: existingClaim2.stateSnapshot,
|
|
739
|
+
runId: (_d2 = existingClaim2.runId) != null ? _d2 : effectiveRunId
|
|
740
|
+
};
|
|
741
|
+
store[LAST_SNAPSHOTS_BY_MESSAGE] = messageCache;
|
|
742
|
+
}
|
|
743
|
+
delete claimsRef.current[message.id];
|
|
744
|
+
};
|
|
745
|
+
}, [claimsRef, effectiveRunId, message.id]);
|
|
746
|
+
if (!stateRenderId) {
|
|
747
|
+
return { canRender: false };
|
|
748
|
+
}
|
|
749
|
+
const caches = getSnapshotCaches(claimsRef);
|
|
750
|
+
const existingClaim = claimsRef.current[message.id];
|
|
751
|
+
const { snapshot, hasSnapshotKeys, allowEmptySnapshot, snapshotForClaim } = selectSnapshot({
|
|
752
|
+
messageId: message.id,
|
|
753
|
+
messageName: message.name,
|
|
754
|
+
allowLiveState: isPlaceholderMessageName(message.name) || isPlaceholderMessageId(message.id),
|
|
755
|
+
skipLatestCache: isPlaceholderMessageName(message.name) || isPlaceholderMessageId(message.id),
|
|
756
|
+
stateRenderId,
|
|
757
|
+
effectiveRunId,
|
|
758
|
+
stateSnapshotProp: stateSnapshot,
|
|
759
|
+
agentState,
|
|
760
|
+
agentMessages,
|
|
761
|
+
existingClaim,
|
|
762
|
+
caches
|
|
763
|
+
});
|
|
764
|
+
const resolution = resolveClaim({
|
|
765
|
+
claims: claimsRef.current,
|
|
766
|
+
context: {
|
|
767
|
+
agentId,
|
|
768
|
+
messageId: message.id,
|
|
769
|
+
stateRenderId,
|
|
770
|
+
runId: effectiveRunId,
|
|
771
|
+
messageIndex
|
|
772
|
+
},
|
|
773
|
+
stateSnapshot: snapshotForClaim
|
|
774
|
+
});
|
|
775
|
+
if (resolution.action === "block" /* Block */) {
|
|
776
|
+
return { canRender: false };
|
|
777
|
+
}
|
|
778
|
+
if (resolution.updateRunId && claimsRef.current[message.id]) {
|
|
779
|
+
claimsRef.current[message.id].runId = resolution.updateRunId;
|
|
780
|
+
}
|
|
781
|
+
if (resolution.nextClaim) {
|
|
782
|
+
claimsRef.current[message.id] = resolution.nextClaim;
|
|
783
|
+
}
|
|
784
|
+
if (resolution.lockOthers) {
|
|
785
|
+
Object.entries(claimsRef.current).forEach(([id, claim]) => {
|
|
786
|
+
if (id !== message.id && claim.stateRenderId === stateRenderId) {
|
|
787
|
+
claim.locked = true;
|
|
788
|
+
}
|
|
789
|
+
});
|
|
790
|
+
}
|
|
791
|
+
if (existingClaim && !existingClaim.locked && (agentMessages == null ? void 0 : agentMessages.length)) {
|
|
792
|
+
const indexInAgentMessages = agentMessages.findIndex((msg) => msg.id === message.id);
|
|
793
|
+
if (indexInAgentMessages >= 0 && indexInAgentMessages < agentMessages.length - 1) {
|
|
794
|
+
existingClaim.locked = true;
|
|
795
|
+
}
|
|
796
|
+
}
|
|
797
|
+
const existingSnapshot = claimsRef.current[message.id].stateSnapshot;
|
|
798
|
+
const snapshotChanged = stateSnapshot && existingSnapshot !== void 0 && !areStatesEquals(existingSnapshot, snapshot);
|
|
799
|
+
if (snapshot && (stateSnapshot || hasSnapshotKeys || allowEmptySnapshot) && (!claimsRef.current[message.id].locked || snapshotChanged)) {
|
|
800
|
+
if (!claimsRef.current[message.id].locked || snapshotChanged) {
|
|
801
|
+
claimsRef.current[message.id].stateSnapshot = snapshot;
|
|
802
|
+
const snapshotCache = __spreadValues({}, (_c = store[LAST_SNAPSHOTS_BY_RENDER_AND_RUN]) != null ? _c : {});
|
|
803
|
+
const cacheKey = `${stateRenderId}::${effectiveRunId}`;
|
|
804
|
+
snapshotCache[cacheKey] = snapshot;
|
|
805
|
+
snapshotCache[`${stateRenderId}::latest`] = snapshot;
|
|
806
|
+
store[LAST_SNAPSHOTS_BY_RENDER_AND_RUN] = snapshotCache;
|
|
807
|
+
const messageCache = __spreadValues({}, (_d = store[LAST_SNAPSHOTS_BY_MESSAGE]) != null ? _d : {});
|
|
808
|
+
messageCache[message.id] = { snapshot, runId: effectiveRunId };
|
|
809
|
+
store[LAST_SNAPSHOTS_BY_MESSAGE] = messageCache;
|
|
810
|
+
if (stateSnapshot) {
|
|
811
|
+
claimsRef.current[message.id].locked = true;
|
|
812
|
+
}
|
|
813
|
+
}
|
|
814
|
+
} else if (snapshotForClaim) {
|
|
815
|
+
const existingSnapshot2 = claimsRef.current[message.id].stateSnapshot;
|
|
816
|
+
if (!existingSnapshot2) {
|
|
817
|
+
claimsRef.current[message.id].stateSnapshot = snapshotForClaim;
|
|
818
|
+
const snapshotCache = __spreadValues({}, (_e = store[LAST_SNAPSHOTS_BY_RENDER_AND_RUN]) != null ? _e : {});
|
|
819
|
+
const cacheKey = `${stateRenderId}::${effectiveRunId}`;
|
|
820
|
+
snapshotCache[cacheKey] = snapshotForClaim;
|
|
821
|
+
snapshotCache[`${stateRenderId}::latest`] = snapshotForClaim;
|
|
822
|
+
store[LAST_SNAPSHOTS_BY_RENDER_AND_RUN] = snapshotCache;
|
|
823
|
+
const messageCache = __spreadValues({}, (_f = store[LAST_SNAPSHOTS_BY_MESSAGE]) != null ? _f : {});
|
|
824
|
+
messageCache[message.id] = { snapshot: snapshotForClaim, runId: effectiveRunId };
|
|
825
|
+
store[LAST_SNAPSHOTS_BY_MESSAGE] = messageCache;
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
return { canRender: true };
|
|
829
|
+
}
|
|
830
|
+
|
|
831
|
+
// src/hooks/use-coagent-state-render-bridge.tsx
|
|
549
832
|
function useCoagentStateRenderBridge(agentId, props) {
|
|
550
833
|
var _a;
|
|
551
|
-
const { stateSnapshot,
|
|
834
|
+
const { stateSnapshot, message } = props;
|
|
552
835
|
const { coAgentStateRenders, claimsRef } = useCoAgentStateRenders();
|
|
553
|
-
const { agent } = (0,
|
|
554
|
-
const [nodeName, setNodeName] = (0,
|
|
555
|
-
const [, forceUpdate] = (0,
|
|
556
|
-
|
|
557
|
-
const effectiveRunId = runId || "pending";
|
|
558
|
-
(0, import_react12.useEffect)(() => {
|
|
836
|
+
const { agent } = (0, import_react12.useAgent)({ agentId });
|
|
837
|
+
const [nodeName, setNodeName] = (0, import_react13.useState)(void 0);
|
|
838
|
+
const [, forceUpdate] = (0, import_react13.useState)(0);
|
|
839
|
+
(0, import_react13.useEffect)(() => {
|
|
559
840
|
if (!agent)
|
|
560
841
|
return;
|
|
561
842
|
const subscriber = {
|
|
@@ -578,103 +859,69 @@ function useCoagentStateRenderBridge(agentId, props) {
|
|
|
578
859
|
unsubscribe();
|
|
579
860
|
};
|
|
580
861
|
}, [agentId, nodeName]);
|
|
581
|
-
const getStateRender = (0,
|
|
862
|
+
const getStateRender = (0, import_react13.useCallback)(
|
|
582
863
|
(messageId) => {
|
|
583
|
-
return Object.entries(coAgentStateRenders).find(([
|
|
864
|
+
return Object.entries(coAgentStateRenders).find(([stateRenderId2, stateRender2]) => {
|
|
584
865
|
if (claimsRef.current[messageId]) {
|
|
585
|
-
return
|
|
866
|
+
return stateRenderId2 === claimsRef.current[messageId].stateRenderId;
|
|
586
867
|
}
|
|
587
|
-
const matchingAgentName =
|
|
588
|
-
const matchesNodeContext =
|
|
868
|
+
const matchingAgentName = stateRender2.name === agentId;
|
|
869
|
+
const matchesNodeContext = stateRender2.nodeName ? stateRender2.nodeName === nodeName : true;
|
|
589
870
|
return matchingAgentName && matchesNodeContext;
|
|
590
871
|
});
|
|
591
872
|
},
|
|
592
873
|
[coAgentStateRenders, nodeName, agentId]
|
|
593
874
|
);
|
|
594
|
-
const
|
|
875
|
+
const stateRenderEntry = (0, import_react13.useMemo)(() => getStateRender(message.id), [getStateRender, message.id]);
|
|
876
|
+
const stateRenderId = stateRenderEntry == null ? void 0 : stateRenderEntry[0];
|
|
877
|
+
const stateRender = stateRenderEntry == null ? void 0 : stateRenderEntry[1];
|
|
878
|
+
const registryMessage = __spreadProps(__spreadValues({}, message), {
|
|
879
|
+
runId: (_a = props.runId) != null ? _a : message.runId
|
|
880
|
+
});
|
|
881
|
+
const { canRender } = useStateRenderRegistry({
|
|
882
|
+
agentId,
|
|
595
883
|
stateRenderId,
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
stateSnapshot
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
return canRender;
|
|
606
|
-
}
|
|
607
|
-
const renderClaimedByOtherMessage = Object.values(claimsRef.current).find(
|
|
608
|
-
(c) => c.stateRenderId === stateRenderId && (0, import_shared3.dataToUUID)(getStateWithoutConstantKeys(c.stateSnapshot)) === (0, import_shared3.dataToUUID)(getStateWithoutConstantKeys(renderSnapshot))
|
|
609
|
-
);
|
|
610
|
-
if (renderClaimedByOtherMessage) {
|
|
611
|
-
if (renderSnapshot && renderClaimedByOtherMessage.stateSnapshot && !areStatesEquals(renderClaimedByOtherMessage.stateSnapshot, renderSnapshot)) {
|
|
612
|
-
claimsRef.current[messageId] = { stateRenderId, runId: runId2 };
|
|
613
|
-
return true;
|
|
614
|
-
}
|
|
615
|
-
return false;
|
|
616
|
-
}
|
|
617
|
-
if (!runId2) {
|
|
618
|
-
return false;
|
|
619
|
-
}
|
|
620
|
-
claimsRef.current[messageId] = { stateRenderId, runId: runId2 };
|
|
621
|
-
return true;
|
|
622
|
-
};
|
|
623
|
-
return (0, import_react12.useMemo)(() => {
|
|
624
|
-
var _a2, _b, _c;
|
|
625
|
-
if (messageIndexInRun !== 0) {
|
|
626
|
-
return null;
|
|
627
|
-
}
|
|
628
|
-
const [stateRenderId, stateRender] = (_a2 = getStateRender(message.id)) != null ? _a2 : [];
|
|
884
|
+
message: registryMessage,
|
|
885
|
+
messageIndex: props.messageIndex,
|
|
886
|
+
stateSnapshot,
|
|
887
|
+
agentState: agent == null ? void 0 : agent.state,
|
|
888
|
+
agentMessages: agent == null ? void 0 : agent.messages,
|
|
889
|
+
claimsRef
|
|
890
|
+
});
|
|
891
|
+
return (0, import_react13.useMemo)(() => {
|
|
892
|
+
var _a2, _b;
|
|
629
893
|
if (!stateRender || !stateRenderId) {
|
|
630
894
|
return null;
|
|
631
895
|
}
|
|
632
|
-
const snapshot = stateSnapshot ? (0, import_shared3.parseJson)(stateSnapshot, stateSnapshot) : agent == null ? void 0 : agent.state;
|
|
633
|
-
const canRender = handleRenderRequest({
|
|
634
|
-
stateRenderId,
|
|
635
|
-
messageId: message.id,
|
|
636
|
-
runId: effectiveRunId,
|
|
637
|
-
stateSnapshot: snapshot
|
|
638
|
-
});
|
|
639
896
|
if (!canRender) {
|
|
640
897
|
return null;
|
|
641
898
|
}
|
|
642
|
-
if (snapshot) {
|
|
643
|
-
const existingSnapshot = claimsRef.current[message.id].stateSnapshot;
|
|
644
|
-
const snapshotChanged = stateSnapshot && existingSnapshot !== void 0 && !areStatesEquals(existingSnapshot, snapshot);
|
|
645
|
-
if (!claimsRef.current[message.id].locked || snapshotChanged) {
|
|
646
|
-
claimsRef.current[message.id].stateSnapshot = snapshot;
|
|
647
|
-
if (stateSnapshot) {
|
|
648
|
-
claimsRef.current[message.id].locked = true;
|
|
649
|
-
}
|
|
650
|
-
}
|
|
651
|
-
}
|
|
652
899
|
if (stateRender.handler) {
|
|
653
900
|
stateRender.handler({
|
|
654
|
-
state: stateSnapshot ? (0,
|
|
901
|
+
state: stateSnapshot ? (0, import_shared4.parseJson)(stateSnapshot, stateSnapshot) : (_a2 = agent == null ? void 0 : agent.state) != null ? _a2 : {},
|
|
655
902
|
nodeName: nodeName != null ? nodeName : ""
|
|
656
903
|
});
|
|
657
904
|
}
|
|
658
905
|
if (stateRender.render) {
|
|
659
|
-
const status = (agent == null ? void 0 : agent.isRunning) ? "inProgress" : "complete"
|
|
906
|
+
const status = (agent == null ? void 0 : agent.isRunning) ? "inProgress" /* InProgress */ : "complete" /* Complete */;
|
|
660
907
|
if (typeof stateRender.render === "string")
|
|
661
908
|
return stateRender.render;
|
|
662
909
|
return stateRender.render({
|
|
663
910
|
status,
|
|
664
911
|
// Always use state from claim, to make sure the state does not seem "wiped" for a fraction of a second
|
|
665
|
-
state: (
|
|
912
|
+
state: (_b = claimsRef.current[message.id].stateSnapshot) != null ? _b : {},
|
|
666
913
|
nodeName: nodeName != null ? nodeName : ""
|
|
667
914
|
});
|
|
668
915
|
}
|
|
669
916
|
}, [
|
|
670
|
-
|
|
671
|
-
|
|
917
|
+
stateRender,
|
|
918
|
+
stateRenderId,
|
|
672
919
|
agent == null ? void 0 : agent.state,
|
|
673
920
|
agent == null ? void 0 : agent.isRunning,
|
|
674
921
|
nodeName,
|
|
675
|
-
effectiveRunId,
|
|
676
922
|
message.id,
|
|
677
|
-
|
|
923
|
+
stateSnapshot,
|
|
924
|
+
canRender
|
|
678
925
|
]);
|
|
679
926
|
}
|
|
680
927
|
function CoAgentStateRenderBridge(props) {
|
|
@@ -690,13 +937,13 @@ function useCopilotChatInternal({
|
|
|
690
937
|
onReloadMessages
|
|
691
938
|
} = {}) {
|
|
692
939
|
var _a, _b, _c;
|
|
693
|
-
const { copilotkit } = (0,
|
|
940
|
+
const { copilotkit } = (0, import_react15.useCopilotKit)();
|
|
694
941
|
const { threadId, agentSession } = useCopilotContext();
|
|
695
|
-
const existingConfig = (0,
|
|
696
|
-
const [agentAvailable, setAgentAvailable] = (0,
|
|
942
|
+
const existingConfig = (0, import_react15.useCopilotChatConfiguration)();
|
|
943
|
+
const [agentAvailable, setAgentAvailable] = (0, import_react14.useState)(false);
|
|
697
944
|
const resolvedAgentId = (_a = existingConfig == null ? void 0 : existingConfig.agentId) != null ? _a : "default";
|
|
698
|
-
const { agent } = (0,
|
|
699
|
-
(0,
|
|
945
|
+
const { agent } = (0, import_react15.useAgent)({ agentId: resolvedAgentId });
|
|
946
|
+
(0, import_react14.useEffect)(() => {
|
|
700
947
|
const connect = (agent2) => __async(this, null, function* () {
|
|
701
948
|
setAgentAvailable(false);
|
|
702
949
|
try {
|
|
@@ -705,7 +952,7 @@ function useCopilotChatInternal({
|
|
|
705
952
|
} catch (error) {
|
|
706
953
|
if (error instanceof import_client.AGUIConnectNotImplementedError) {
|
|
707
954
|
} else {
|
|
708
|
-
|
|
955
|
+
console.error("CopilotChat: connectAgent failed", error);
|
|
709
956
|
}
|
|
710
957
|
}
|
|
711
958
|
});
|
|
@@ -716,7 +963,7 @@ function useCopilotChatInternal({
|
|
|
716
963
|
return () => {
|
|
717
964
|
};
|
|
718
965
|
}, [existingConfig == null ? void 0 : existingConfig.threadId, agent, copilotkit, resolvedAgentId]);
|
|
719
|
-
(0,
|
|
966
|
+
(0, import_react14.useEffect)(() => {
|
|
720
967
|
onInProgress == null ? void 0 : onInProgress(Boolean(agent == null ? void 0 : agent.isRunning));
|
|
721
968
|
}, [agent == null ? void 0 : agent.isRunning, onInProgress]);
|
|
722
969
|
const interrupt = useLangGraphInterruptRender(agent);
|
|
@@ -724,7 +971,7 @@ function useCopilotChatInternal({
|
|
|
724
971
|
agent == null ? void 0 : agent.setMessages([]);
|
|
725
972
|
agent == null ? void 0 : agent.setState(null);
|
|
726
973
|
};
|
|
727
|
-
const deleteMessage = (0,
|
|
974
|
+
const deleteMessage = (0, import_react14.useCallback)(
|
|
728
975
|
(messageId) => {
|
|
729
976
|
var _a2;
|
|
730
977
|
const filteredMessages = ((_a2 = agent == null ? void 0 : agent.messages) != null ? _a2 : []).filter(
|
|
@@ -735,13 +982,13 @@ function useCopilotChatInternal({
|
|
|
735
982
|
[agent == null ? void 0 : agent.setMessages, agent == null ? void 0 : agent.messages]
|
|
736
983
|
);
|
|
737
984
|
const latestDelete = useUpdatedRef(deleteMessage);
|
|
738
|
-
const latestDeleteFunc = (0,
|
|
985
|
+
const latestDeleteFunc = (0, import_react14.useCallback)(
|
|
739
986
|
(messageId) => {
|
|
740
987
|
return latestDelete.current(messageId);
|
|
741
988
|
},
|
|
742
989
|
[latestDelete]
|
|
743
990
|
);
|
|
744
|
-
const currentSuggestions = (0,
|
|
991
|
+
const currentSuggestions = (0, import_react15.useSuggestions)({ agentId: resolvedAgentId });
|
|
745
992
|
const reload = useAsyncCallback(
|
|
746
993
|
(reloadMessageId) => __async(this, null, function* () {
|
|
747
994
|
var _a2;
|
|
@@ -776,7 +1023,11 @@ function useCopilotChatInternal({
|
|
|
776
1023
|
}
|
|
777
1024
|
agent == null ? void 0 : agent.setMessages(historyCutoff);
|
|
778
1025
|
if (agent) {
|
|
779
|
-
|
|
1026
|
+
try {
|
|
1027
|
+
yield copilotkit.runAgent({ agent });
|
|
1028
|
+
} catch (error) {
|
|
1029
|
+
console.error("CopilotChat: runAgent failed during reload", error);
|
|
1030
|
+
}
|
|
780
1031
|
}
|
|
781
1032
|
return;
|
|
782
1033
|
}),
|
|
@@ -816,7 +1067,7 @@ function useCopilotChatInternal({
|
|
|
816
1067
|
}),
|
|
817
1068
|
[latestSendMessageFunc]
|
|
818
1069
|
);
|
|
819
|
-
const latestSetMessagesFunc = (0,
|
|
1070
|
+
const latestSetMessagesFunc = (0, import_react14.useCallback)(
|
|
820
1071
|
(messages) => {
|
|
821
1072
|
var _a2, _b2;
|
|
822
1073
|
if (messages.every((message) => message instanceof import_runtime_client_gql2.Message)) {
|
|
@@ -839,7 +1090,7 @@ function useCopilotChatInternal({
|
|
|
839
1090
|
}),
|
|
840
1091
|
[latestReload, agent, onReloadMessages]
|
|
841
1092
|
);
|
|
842
|
-
const latestStopFunc = (0,
|
|
1093
|
+
const latestStopFunc = (0, import_react14.useCallback)(() => {
|
|
843
1094
|
var _a2, _b2;
|
|
844
1095
|
onStopGeneration == null ? void 0 : onStopGeneration({
|
|
845
1096
|
currentAgentName: agent == null ? void 0 : agent.agentId,
|
|
@@ -848,11 +1099,11 @@ function useCopilotChatInternal({
|
|
|
848
1099
|
return (_b2 = agent == null ? void 0 : agent.abortRun) == null ? void 0 : _b2.call(agent);
|
|
849
1100
|
}, [onStopGeneration, agent]);
|
|
850
1101
|
const latestReset = useUpdatedRef(reset);
|
|
851
|
-
const latestResetFunc = (0,
|
|
1102
|
+
const latestResetFunc = (0, import_react14.useCallback)(() => {
|
|
852
1103
|
return latestReset.current();
|
|
853
1104
|
}, [latestReset]);
|
|
854
1105
|
const lazyToolRendered = useLazyToolRenderer();
|
|
855
|
-
const renderCustomMessage = (0,
|
|
1106
|
+
const renderCustomMessage = (0, import_react15.useRenderCustomMessages)();
|
|
856
1107
|
const legacyCustomMessageRenderer = useLegacyCoagentRenderer({
|
|
857
1108
|
copilotkit,
|
|
858
1109
|
agent,
|
|
@@ -860,7 +1111,8 @@ function useCopilotChatInternal({
|
|
|
860
1111
|
threadId: (_b = existingConfig == null ? void 0 : existingConfig.threadId) != null ? _b : threadId
|
|
861
1112
|
});
|
|
862
1113
|
const allMessages = (_c = agent == null ? void 0 : agent.messages) != null ? _c : [];
|
|
863
|
-
const resolvedMessages = (0,
|
|
1114
|
+
const resolvedMessages = (0, import_react14.useMemo)(() => {
|
|
1115
|
+
var _a2, _b2;
|
|
864
1116
|
let processedMessages = allMessages.map((message) => {
|
|
865
1117
|
if (message.role !== "assistant") {
|
|
866
1118
|
return message;
|
|
@@ -873,31 +1125,78 @@ function useCopilotChatInternal({
|
|
|
873
1125
|
}
|
|
874
1126
|
}
|
|
875
1127
|
const bridgeRenderer = legacyCustomMessageRenderer || renderCustomMessage ? () => {
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
position: "before"
|
|
879
|
-
}
|
|
880
|
-
|
|
881
|
-
return
|
|
1128
|
+
var _a3;
|
|
1129
|
+
if (legacyCustomMessageRenderer) {
|
|
1130
|
+
return legacyCustomMessageRenderer({ message, position: "before" });
|
|
1131
|
+
}
|
|
1132
|
+
try {
|
|
1133
|
+
return (_a3 = renderCustomMessage == null ? void 0 : renderCustomMessage({ message, position: "before" })) != null ? _a3 : null;
|
|
1134
|
+
} catch (error) {
|
|
1135
|
+
console.warn(
|
|
1136
|
+
"[CopilotKit] renderCustomMessages failed, falling back to legacy renderer",
|
|
1137
|
+
error
|
|
1138
|
+
);
|
|
1139
|
+
return null;
|
|
882
1140
|
}
|
|
883
|
-
return legacyCustomMessageRenderer == null ? void 0 : legacyCustomMessageRenderer({ message, position: "before" });
|
|
884
1141
|
} : null;
|
|
885
1142
|
if (bridgeRenderer) {
|
|
886
|
-
return __spreadProps(__spreadValues({}, message), {
|
|
1143
|
+
return __spreadProps(__spreadValues({}, message), {
|
|
1144
|
+
generativeUI: bridgeRenderer,
|
|
1145
|
+
generativeUIPosition: "before"
|
|
1146
|
+
});
|
|
887
1147
|
}
|
|
888
1148
|
return message;
|
|
889
1149
|
});
|
|
890
1150
|
const hasAssistantMessages = processedMessages.some((msg) => msg.role === "assistant");
|
|
1151
|
+
const canUseCustomRenderer = Boolean(
|
|
1152
|
+
renderCustomMessage && ((_a2 = copilotkit == null ? void 0 : copilotkit.getAgent) == null ? void 0 : _a2.call(copilotkit, resolvedAgentId))
|
|
1153
|
+
);
|
|
1154
|
+
const placeholderRenderer = legacyCustomMessageRenderer ? legacyCustomMessageRenderer : canUseCustomRenderer ? renderCustomMessage : null;
|
|
1155
|
+
const shouldRenderPlaceholder = Boolean(agent == null ? void 0 : agent.isRunning) || Boolean((agent == null ? void 0 : agent.state) && Object.keys(agent.state).length);
|
|
1156
|
+
const effectiveThreadId = (_b2 = threadId != null ? threadId : agent == null ? void 0 : agent.threadId) != null ? _b2 : "default";
|
|
1157
|
+
let latestUserIndex = -1;
|
|
1158
|
+
for (let i = processedMessages.length - 1; i >= 0; i -= 1) {
|
|
1159
|
+
if (processedMessages[i].role === "user") {
|
|
1160
|
+
latestUserIndex = i;
|
|
1161
|
+
break;
|
|
1162
|
+
}
|
|
1163
|
+
}
|
|
1164
|
+
const latestUserMessageId = latestUserIndex >= 0 ? processedMessages[latestUserIndex].id : void 0;
|
|
1165
|
+
const currentRunId = latestUserMessageId ? copilotkit.getRunIdForMessage(resolvedAgentId, effectiveThreadId, latestUserMessageId) || `pending:${latestUserMessageId}` : void 0;
|
|
1166
|
+
const hasAssistantForCurrentRun = latestUserIndex >= 0 ? processedMessages.slice(latestUserIndex + 1).some((msg) => msg.role === "assistant") : hasAssistantMessages;
|
|
1167
|
+
if (placeholderRenderer && shouldRenderPlaceholder && !hasAssistantForCurrentRun) {
|
|
1168
|
+
const placeholderId = currentRunId ? `coagent-state-render-${resolvedAgentId}-${currentRunId}` : `coagent-state-render-${resolvedAgentId}`;
|
|
1169
|
+
const placeholderMessage = {
|
|
1170
|
+
id: placeholderId,
|
|
1171
|
+
role: "assistant",
|
|
1172
|
+
content: "",
|
|
1173
|
+
name: "coagent-state-render",
|
|
1174
|
+
runId: currentRunId
|
|
1175
|
+
};
|
|
1176
|
+
processedMessages = [
|
|
1177
|
+
...processedMessages,
|
|
1178
|
+
__spreadProps(__spreadValues({}, placeholderMessage), {
|
|
1179
|
+
generativeUIPosition: "before",
|
|
1180
|
+
generativeUI: () => placeholderRenderer({
|
|
1181
|
+
message: placeholderMessage,
|
|
1182
|
+
position: "before"
|
|
1183
|
+
})
|
|
1184
|
+
})
|
|
1185
|
+
];
|
|
1186
|
+
}
|
|
891
1187
|
return processedMessages;
|
|
892
1188
|
}, [
|
|
893
1189
|
agent == null ? void 0 : agent.messages,
|
|
894
1190
|
lazyToolRendered,
|
|
895
1191
|
allMessages,
|
|
896
1192
|
renderCustomMessage,
|
|
897
|
-
|
|
898
|
-
resolvedAgentId
|
|
1193
|
+
legacyCustomMessageRenderer,
|
|
1194
|
+
resolvedAgentId,
|
|
1195
|
+
copilotkit,
|
|
1196
|
+
agent == null ? void 0 : agent.isRunning,
|
|
1197
|
+
agent == null ? void 0 : agent.state
|
|
899
1198
|
]);
|
|
900
|
-
const renderedSuggestions = (0,
|
|
1199
|
+
const renderedSuggestions = (0, import_react14.useMemo)(() => {
|
|
901
1200
|
if (Array.isArray(suggestions)) {
|
|
902
1201
|
return {
|
|
903
1202
|
suggestions: suggestions.map((s) => __spreadProps(__spreadValues({}, s), { isLoading: false })),
|
|
@@ -932,8 +1231,8 @@ function useCopilotChatInternal({
|
|
|
932
1231
|
};
|
|
933
1232
|
}
|
|
934
1233
|
function useUpdatedRef(value) {
|
|
935
|
-
const ref = (0,
|
|
936
|
-
(0,
|
|
1234
|
+
const ref = (0, import_react14.useRef)(value);
|
|
1235
|
+
(0, import_react14.useEffect)(() => {
|
|
937
1236
|
ref.current = value;
|
|
938
1237
|
}, [value]);
|
|
939
1238
|
return ref;
|
|
@@ -944,14 +1243,15 @@ function useLegacyCoagentRenderer({
|
|
|
944
1243
|
agentId,
|
|
945
1244
|
threadId
|
|
946
1245
|
}) {
|
|
947
|
-
return (0,
|
|
1246
|
+
return (0, import_react14.useMemo)(() => {
|
|
948
1247
|
if (!copilotkit || !agent) {
|
|
949
1248
|
return null;
|
|
950
1249
|
}
|
|
951
1250
|
return ({ message, position }) => {
|
|
952
1251
|
var _a;
|
|
953
1252
|
const effectiveThreadId = (_a = threadId != null ? threadId : agent.threadId) != null ? _a : "default";
|
|
954
|
-
const
|
|
1253
|
+
const providedRunId = message.runId;
|
|
1254
|
+
const existingRunId = providedRunId ? providedRunId : copilotkit.getRunIdForMessage(agentId, effectiveThreadId, message.id);
|
|
955
1255
|
const runId = existingRunId || `pending:${message.id}`;
|
|
956
1256
|
const messageIndex = Math.max(
|
|
957
1257
|
agent.messages.findIndex((msg) => msg.id === message.id),
|
|
@@ -967,7 +1267,7 @@ function useLegacyCoagentRenderer({
|
|
|
967
1267
|
agentId,
|
|
968
1268
|
stateSnapshot: message.state
|
|
969
1269
|
};
|
|
970
|
-
return (0,
|
|
1270
|
+
return (0, import_react14.createElement)(CoAgentStateRenderBridge, bridgeProps);
|
|
971
1271
|
};
|
|
972
1272
|
}, [agent, agentId, copilotkit, threadId]);
|
|
973
1273
|
}
|
|
@@ -1001,8 +1301,8 @@ function useCopilotChat(options = {}) {
|
|
|
1001
1301
|
}
|
|
1002
1302
|
|
|
1003
1303
|
// src/hooks/use-copilot-chat-headless_c.ts
|
|
1004
|
-
var
|
|
1005
|
-
var
|
|
1304
|
+
var import_react16 = require("react");
|
|
1305
|
+
var import_shared5 = require("@copilotkit/shared");
|
|
1006
1306
|
var createNonFunctionalReturn = () => ({
|
|
1007
1307
|
visibleMessages: [],
|
|
1008
1308
|
messages: [],
|
|
@@ -1042,20 +1342,20 @@ function useCopilotChatHeadless_c(options = {}) {
|
|
|
1042
1342
|
const { copilotApiConfig, setBannerError } = useCopilotContext();
|
|
1043
1343
|
const hasPublicApiKey = Boolean(copilotApiConfig.publicApiKey);
|
|
1044
1344
|
const internalResult = useCopilotChatInternal(options);
|
|
1045
|
-
(0,
|
|
1345
|
+
(0, import_react16.useEffect)(() => {
|
|
1046
1346
|
if (!hasPublicApiKey) {
|
|
1047
1347
|
setBannerError(
|
|
1048
|
-
new
|
|
1348
|
+
new import_shared5.CopilotKitError({
|
|
1049
1349
|
message: (
|
|
1050
1350
|
// add link to documentation here
|
|
1051
1351
|
"You're using useCopilotChatHeadless_c, a premium-only feature, which offers extensive headless chat capabilities. To continue, you'll need to provide a free public license key."
|
|
1052
1352
|
),
|
|
1053
|
-
code:
|
|
1054
|
-
severity:
|
|
1055
|
-
visibility:
|
|
1353
|
+
code: import_shared5.CopilotKitErrorCode.MISSING_PUBLIC_API_KEY_ERROR,
|
|
1354
|
+
severity: import_shared5.Severity.WARNING,
|
|
1355
|
+
visibility: import_shared5.ErrorVisibility.BANNER
|
|
1056
1356
|
})
|
|
1057
1357
|
);
|
|
1058
|
-
|
|
1358
|
+
import_shared5.styledConsole.logCopilotKitPlatformMessage();
|
|
1059
1359
|
} else {
|
|
1060
1360
|
setBannerError(null);
|
|
1061
1361
|
}
|
|
@@ -1067,48 +1367,48 @@ function useCopilotChatHeadless_c(options = {}) {
|
|
|
1067
1367
|
}
|
|
1068
1368
|
|
|
1069
1369
|
// src/hooks/use-copilot-action.ts
|
|
1070
|
-
var
|
|
1370
|
+
var import_react23 = require("react");
|
|
1071
1371
|
|
|
1072
1372
|
// src/hooks/use-frontend-tool.ts
|
|
1073
|
-
var
|
|
1074
|
-
var import_shared5 = require("@copilotkit/shared");
|
|
1373
|
+
var import_react17 = __toESM(require("react"));
|
|
1075
1374
|
var import_shared6 = require("@copilotkit/shared");
|
|
1076
|
-
var
|
|
1375
|
+
var import_shared7 = require("@copilotkit/shared");
|
|
1376
|
+
var import_react18 = require("@copilotkitnext/react");
|
|
1077
1377
|
function useFrontendTool(tool, dependencies) {
|
|
1078
1378
|
const { name, description, parameters, render, followUp } = tool;
|
|
1079
|
-
const zodParameters = (0,
|
|
1379
|
+
const zodParameters = (0, import_shared6.getZodParameters)(parameters);
|
|
1080
1380
|
const normalizedRender = (() => {
|
|
1081
1381
|
if (typeof render === "undefined") {
|
|
1082
1382
|
return void 0;
|
|
1083
1383
|
}
|
|
1084
1384
|
if (typeof render === "string") {
|
|
1085
1385
|
const staticRender = render;
|
|
1086
|
-
return () =>
|
|
1087
|
-
|
|
1386
|
+
return () => import_react17.default.createElement(
|
|
1387
|
+
import_react17.default.Fragment,
|
|
1088
1388
|
null,
|
|
1089
1389
|
staticRender
|
|
1090
1390
|
);
|
|
1091
1391
|
}
|
|
1092
1392
|
return (args) => {
|
|
1093
1393
|
const renderArgs = __spreadProps(__spreadValues({}, args), {
|
|
1094
|
-
result: typeof args.result === "string" ? (0,
|
|
1394
|
+
result: typeof args.result === "string" ? (0, import_shared7.parseJson)(args.result, args.result) : args.result
|
|
1095
1395
|
});
|
|
1096
1396
|
const rendered = render(renderArgs);
|
|
1097
1397
|
if (typeof rendered === "string") {
|
|
1098
|
-
return
|
|
1398
|
+
return import_react17.default.createElement(import_react17.default.Fragment, null, rendered);
|
|
1099
1399
|
}
|
|
1100
1400
|
return rendered != null ? rendered : null;
|
|
1101
1401
|
};
|
|
1102
1402
|
})();
|
|
1103
|
-
const handlerRef = (0,
|
|
1104
|
-
(0,
|
|
1403
|
+
const handlerRef = (0, import_react17.useRef)(tool.handler);
|
|
1404
|
+
(0, import_react17.useEffect)(() => {
|
|
1105
1405
|
handlerRef.current = tool.handler;
|
|
1106
1406
|
}, [tool.handler, ...dependencies != null ? dependencies : []]);
|
|
1107
1407
|
const normalizedHandler = tool.handler ? (args) => {
|
|
1108
1408
|
var _a;
|
|
1109
1409
|
return (_a = handlerRef.current) == null ? void 0 : _a.call(handlerRef, args);
|
|
1110
1410
|
} : void 0;
|
|
1111
|
-
(0,
|
|
1411
|
+
(0, import_react18.useFrontendTool)({
|
|
1112
1412
|
name,
|
|
1113
1413
|
description,
|
|
1114
1414
|
parameters: zodParameters,
|
|
@@ -1119,29 +1419,29 @@ function useFrontendTool(tool, dependencies) {
|
|
|
1119
1419
|
}
|
|
1120
1420
|
|
|
1121
1421
|
// src/hooks/use-render-tool-call.ts
|
|
1122
|
-
var import_shared7 = require("@copilotkit/shared");
|
|
1123
|
-
var import_react18 = require("react");
|
|
1124
|
-
var import_react19 = require("@copilotkitnext/react");
|
|
1125
1422
|
var import_shared8 = require("@copilotkit/shared");
|
|
1423
|
+
var import_react19 = require("react");
|
|
1424
|
+
var import_react20 = require("@copilotkitnext/react");
|
|
1425
|
+
var import_shared9 = require("@copilotkit/shared");
|
|
1126
1426
|
function useRenderToolCall2(tool, dependencies) {
|
|
1127
|
-
const { copilotkit } = (0,
|
|
1128
|
-
const hasAddedRef = (0,
|
|
1129
|
-
(0,
|
|
1427
|
+
const { copilotkit } = (0, import_react20.useCopilotKit)();
|
|
1428
|
+
const hasAddedRef = (0, import_react19.useRef)(false);
|
|
1429
|
+
(0, import_react19.useEffect)(() => {
|
|
1130
1430
|
const { name, parameters, render } = tool;
|
|
1131
|
-
const zodParameters = (0,
|
|
1132
|
-
const renderToolCall = name === "*" ? (0,
|
|
1431
|
+
const zodParameters = (0, import_shared8.getZodParameters)(parameters);
|
|
1432
|
+
const renderToolCall = name === "*" ? (0, import_react20.defineToolCallRenderer)({
|
|
1133
1433
|
name: "*",
|
|
1134
1434
|
render: (args) => {
|
|
1135
1435
|
return render(__spreadProps(__spreadValues({}, args), {
|
|
1136
|
-
result: args.result ? (0,
|
|
1436
|
+
result: args.result ? (0, import_shared9.parseJson)(args.result, args.result) : args.result
|
|
1137
1437
|
}));
|
|
1138
1438
|
}
|
|
1139
|
-
}) : (0,
|
|
1439
|
+
}) : (0, import_react20.defineToolCallRenderer)({
|
|
1140
1440
|
name,
|
|
1141
1441
|
args: zodParameters,
|
|
1142
1442
|
render: (args) => {
|
|
1143
1443
|
return render(__spreadProps(__spreadValues({}, args), {
|
|
1144
|
-
result: args.result ? (0,
|
|
1444
|
+
result: args.result ? (0, import_shared9.parseJson)(args.result, args.result) : args.result
|
|
1145
1445
|
}));
|
|
1146
1446
|
}
|
|
1147
1447
|
});
|
|
@@ -1164,19 +1464,19 @@ function useRenderToolCall2(tool, dependencies) {
|
|
|
1164
1464
|
}
|
|
1165
1465
|
|
|
1166
1466
|
// src/hooks/use-human-in-the-loop.ts
|
|
1167
|
-
var
|
|
1168
|
-
var
|
|
1467
|
+
var import_shared10 = require("@copilotkit/shared");
|
|
1468
|
+
var import_react21 = require("@copilotkitnext/react");
|
|
1169
1469
|
var import_core = require("@copilotkitnext/core");
|
|
1170
|
-
var
|
|
1470
|
+
var import_react22 = __toESM(require("react"));
|
|
1171
1471
|
function useHumanInTheLoop(tool, dependencies) {
|
|
1172
1472
|
const _a = tool, { render } = _a, toolRest = __objRest(_a, ["render"]);
|
|
1173
1473
|
const { name, description, parameters, followUp } = toolRest;
|
|
1174
|
-
const zodParameters = (0,
|
|
1175
|
-
const renderRef = (0,
|
|
1176
|
-
(0,
|
|
1474
|
+
const zodParameters = (0, import_shared10.getZodParameters)(parameters);
|
|
1475
|
+
const renderRef = (0, import_react22.useRef)(null);
|
|
1476
|
+
(0, import_react22.useEffect)(() => {
|
|
1177
1477
|
renderRef.current = (args) => {
|
|
1178
1478
|
if (typeof render === "string") {
|
|
1179
|
-
return
|
|
1479
|
+
return import_react22.default.createElement(import_react22.default.Fragment, null, render);
|
|
1180
1480
|
}
|
|
1181
1481
|
if (!render) {
|
|
1182
1482
|
return null;
|
|
@@ -1204,24 +1504,24 @@ function useHumanInTheLoop(tool, dependencies) {
|
|
|
1204
1504
|
args: mappedArgs,
|
|
1205
1505
|
respond: args.respond,
|
|
1206
1506
|
status: args.status,
|
|
1207
|
-
result: args.result ? (0,
|
|
1507
|
+
result: args.result ? (0, import_shared10.parseJson)(args.result, args.result) : args.result,
|
|
1208
1508
|
handler: void 0
|
|
1209
1509
|
};
|
|
1210
1510
|
default:
|
|
1211
|
-
throw new
|
|
1212
|
-
code:
|
|
1511
|
+
throw new import_shared10.CopilotKitError({
|
|
1512
|
+
code: import_shared10.CopilotKitErrorCode.UNKNOWN,
|
|
1213
1513
|
message: `Invalid tool call status: ${args.status}`
|
|
1214
1514
|
});
|
|
1215
1515
|
}
|
|
1216
1516
|
})();
|
|
1217
1517
|
const rendered = render(renderProps);
|
|
1218
1518
|
if (typeof rendered === "string") {
|
|
1219
|
-
return
|
|
1519
|
+
return import_react22.default.createElement(import_react22.default.Fragment, null, rendered);
|
|
1220
1520
|
}
|
|
1221
1521
|
return rendered != null ? rendered : null;
|
|
1222
1522
|
};
|
|
1223
1523
|
}, [render, ...dependencies != null ? dependencies : []]);
|
|
1224
|
-
(0,
|
|
1524
|
+
(0, import_react21.useHumanInTheLoop)({
|
|
1225
1525
|
name,
|
|
1226
1526
|
description,
|
|
1227
1527
|
followUp,
|
|
@@ -1277,7 +1577,7 @@ function getActionConfig(action) {
|
|
|
1277
1577
|
throw new Error("Invalid action configuration");
|
|
1278
1578
|
}
|
|
1279
1579
|
function useCopilotAction(action, dependencies) {
|
|
1280
|
-
const [initialActionConfig] = (0,
|
|
1580
|
+
const [initialActionConfig] = (0, import_react23.useState)(getActionConfig(action));
|
|
1281
1581
|
const currentActionConfig = getActionConfig(action);
|
|
1282
1582
|
if (initialActionConfig.type !== currentActionConfig.type) {
|
|
1283
1583
|
throw new Error("Action configuration changed between renders");
|
|
@@ -1295,17 +1595,17 @@ function useCopilotAction(action, dependencies) {
|
|
|
1295
1595
|
}
|
|
1296
1596
|
|
|
1297
1597
|
// src/hooks/use-coagent-state-render.ts
|
|
1298
|
-
var
|
|
1299
|
-
var
|
|
1598
|
+
var import_react24 = require("react");
|
|
1599
|
+
var import_shared11 = require("@copilotkit/shared");
|
|
1300
1600
|
function useCoAgentStateRender(action, dependencies) {
|
|
1301
|
-
const { chatComponentsCache, availableAgents } = (0,
|
|
1601
|
+
const { chatComponentsCache, availableAgents } = (0, import_react24.useContext)(CopilotContext);
|
|
1302
1602
|
const { setCoAgentStateRender, removeCoAgentStateRender, coAgentStateRenders } = useCoAgentStateRenders();
|
|
1303
|
-
const idRef = (0,
|
|
1603
|
+
const idRef = (0, import_react24.useRef)((0, import_shared11.randomId)());
|
|
1304
1604
|
const { setBannerError, addToast } = useToast();
|
|
1305
|
-
(0,
|
|
1605
|
+
(0, import_react24.useEffect)(() => {
|
|
1306
1606
|
if ((availableAgents == null ? void 0 : availableAgents.length) && !availableAgents.some((a) => a.name === action.name)) {
|
|
1307
1607
|
const message = `(useCoAgentStateRender): Agent "${action.name}" not found. Make sure the agent exists and is properly configured.`;
|
|
1308
|
-
const agentError = new
|
|
1608
|
+
const agentError = new import_shared11.CopilotKitAgentDiscoveryError({
|
|
1309
1609
|
agentName: action.name,
|
|
1310
1610
|
availableAgents: availableAgents.map((a) => ({ name: a.name, id: a.id }))
|
|
1311
1611
|
});
|
|
@@ -1323,7 +1623,7 @@ function useCoAgentStateRender(action, dependencies) {
|
|
|
1323
1623
|
}
|
|
1324
1624
|
}
|
|
1325
1625
|
}
|
|
1326
|
-
(0,
|
|
1626
|
+
(0, import_react24.useEffect)(() => {
|
|
1327
1627
|
const currentId = idRef.current;
|
|
1328
1628
|
const hasDuplicate = Object.entries(coAgentStateRenders).some(([id, otherAction]) => {
|
|
1329
1629
|
if (id === currentId)
|
|
@@ -1347,7 +1647,7 @@ function useCoAgentStateRender(action, dependencies) {
|
|
|
1347
1647
|
});
|
|
1348
1648
|
}
|
|
1349
1649
|
}, [coAgentStateRenders]);
|
|
1350
|
-
(0,
|
|
1650
|
+
(0, import_react24.useEffect)(() => {
|
|
1351
1651
|
setCoAgentStateRender(idRef.current, action);
|
|
1352
1652
|
if (chatComponentsCache.current !== null && action.render !== void 0) {
|
|
1353
1653
|
chatComponentsCache.current.coAgentStateRenders[key] = action.render;
|
|
@@ -1367,11 +1667,11 @@ function useCoAgentStateRender(action, dependencies) {
|
|
|
1367
1667
|
}
|
|
1368
1668
|
|
|
1369
1669
|
// src/hooks/use-make-copilot-document-readable.ts
|
|
1370
|
-
var
|
|
1670
|
+
var import_react25 = require("react");
|
|
1371
1671
|
function useMakeCopilotDocumentReadable(document, categories, dependencies = []) {
|
|
1372
1672
|
const { addDocumentContext, removeDocumentContext } = useCopilotContext();
|
|
1373
|
-
const idRef = (0,
|
|
1374
|
-
(0,
|
|
1673
|
+
const idRef = (0, import_react25.useRef)(void 0);
|
|
1674
|
+
(0, import_react25.useEffect)(() => {
|
|
1375
1675
|
const id = addDocumentContext(document, categories);
|
|
1376
1676
|
idRef.current = id;
|
|
1377
1677
|
return () => {
|
|
@@ -1382,12 +1682,12 @@ function useMakeCopilotDocumentReadable(document, categories, dependencies = [])
|
|
|
1382
1682
|
}
|
|
1383
1683
|
|
|
1384
1684
|
// src/hooks/use-copilot-readable.ts
|
|
1385
|
-
var
|
|
1386
|
-
var
|
|
1685
|
+
var import_react26 = require("@copilotkitnext/react");
|
|
1686
|
+
var import_react27 = require("react");
|
|
1387
1687
|
function useCopilotReadable({ description, value, convert, available }, dependencies) {
|
|
1388
|
-
const { copilotkit } = (0,
|
|
1389
|
-
const ctxIdRef = (0,
|
|
1390
|
-
(0,
|
|
1688
|
+
const { copilotkit } = (0, import_react26.useCopilotKit)();
|
|
1689
|
+
const ctxIdRef = (0, import_react27.useRef)(void 0);
|
|
1690
|
+
(0, import_react27.useEffect)(() => {
|
|
1391
1691
|
if (!copilotkit)
|
|
1392
1692
|
return;
|
|
1393
1693
|
const found = Object.entries(copilotkit.context).find(([id, ctxItem]) => {
|
|
@@ -1415,13 +1715,13 @@ function useCopilotReadable({ description, value, convert, available }, dependen
|
|
|
1415
1715
|
}
|
|
1416
1716
|
|
|
1417
1717
|
// src/hooks/use-coagent.ts
|
|
1418
|
-
var
|
|
1419
|
-
var
|
|
1718
|
+
var import_react28 = require("react");
|
|
1719
|
+
var import_react29 = require("@copilotkitnext/react");
|
|
1420
1720
|
function useCoAgent(options) {
|
|
1421
|
-
const { agent } = (0,
|
|
1422
|
-
const { copilotkit } = (0,
|
|
1721
|
+
const { agent } = (0, import_react29.useAgent)({ agentId: options.name });
|
|
1722
|
+
const { copilotkit } = (0, import_react29.useCopilotKit)();
|
|
1423
1723
|
const nodeName = useAgentNodeName(options.name);
|
|
1424
|
-
const handleStateUpdate = (0,
|
|
1724
|
+
const handleStateUpdate = (0, import_react28.useCallback)(
|
|
1425
1725
|
(newState) => {
|
|
1426
1726
|
if (!agent)
|
|
1427
1727
|
return;
|
|
@@ -1434,7 +1734,7 @@ function useCoAgent(options) {
|
|
|
1434
1734
|
},
|
|
1435
1735
|
[agent == null ? void 0 : agent.state, agent == null ? void 0 : agent.setState]
|
|
1436
1736
|
);
|
|
1437
|
-
(0,
|
|
1737
|
+
(0, import_react28.useEffect)(() => {
|
|
1438
1738
|
var _a;
|
|
1439
1739
|
if (!options.config && !options.configurable)
|
|
1440
1740
|
return;
|
|
@@ -1446,22 +1746,22 @@ function useCoAgent(options) {
|
|
|
1446
1746
|
}
|
|
1447
1747
|
copilotkit.setProperties(config);
|
|
1448
1748
|
}, [options.config, options.configurable]);
|
|
1449
|
-
const externalStateStr = (0,
|
|
1749
|
+
const externalStateStr = (0, import_react28.useMemo)(
|
|
1450
1750
|
() => isExternalStateManagement(options) ? JSON.stringify(options.state) : void 0,
|
|
1451
1751
|
[isExternalStateManagement(options) ? JSON.stringify(options.state) : void 0]
|
|
1452
1752
|
);
|
|
1453
|
-
(0,
|
|
1753
|
+
(0, import_react28.useEffect)(() => {
|
|
1454
1754
|
if ((agent == null ? void 0 : agent.state) && isExternalStateManagement(options) && JSON.stringify(options.state) !== JSON.stringify(agent.state)) {
|
|
1455
1755
|
handleStateUpdate(options.state);
|
|
1456
1756
|
}
|
|
1457
1757
|
}, [agent, externalStateStr, handleStateUpdate]);
|
|
1458
|
-
const hasStateValues = (0,
|
|
1758
|
+
const hasStateValues = (0, import_react28.useCallback)((value) => {
|
|
1459
1759
|
return Boolean(value && Object.keys(value).length);
|
|
1460
1760
|
}, []);
|
|
1461
|
-
const initialStateRef = (0,
|
|
1761
|
+
const initialStateRef = (0, import_react28.useRef)(
|
|
1462
1762
|
isExternalStateManagement(options) ? options.state : "initialState" in options ? options.initialState : void 0
|
|
1463
1763
|
);
|
|
1464
|
-
(0,
|
|
1764
|
+
(0, import_react28.useEffect)(() => {
|
|
1465
1765
|
if (isExternalStateManagement(options)) {
|
|
1466
1766
|
initialStateRef.current = options.state;
|
|
1467
1767
|
} else if ("initialState" in options) {
|
|
@@ -1470,7 +1770,7 @@ function useCoAgent(options) {
|
|
|
1470
1770
|
}, [
|
|
1471
1771
|
isExternalStateManagement(options) ? JSON.stringify(options.state) : "initialState" in options ? JSON.stringify(options.initialState) : void 0
|
|
1472
1772
|
]);
|
|
1473
|
-
(0,
|
|
1773
|
+
(0, import_react28.useEffect)(() => {
|
|
1474
1774
|
if (!agent)
|
|
1475
1775
|
return;
|
|
1476
1776
|
const subscriber = {
|
|
@@ -1498,7 +1798,7 @@ function useCoAgent(options) {
|
|
|
1498
1798
|
subscription.unsubscribe();
|
|
1499
1799
|
};
|
|
1500
1800
|
}, [agent, handleStateUpdate, hasStateValues]);
|
|
1501
|
-
return (0,
|
|
1801
|
+
return (0, import_react28.useMemo)(() => {
|
|
1502
1802
|
var _a, _b, _c;
|
|
1503
1803
|
if (!agent) {
|
|
1504
1804
|
const noop = () => {
|
|
@@ -1554,12 +1854,12 @@ var isExternalStateManagement = (options) => {
|
|
|
1554
1854
|
|
|
1555
1855
|
// src/hooks/use-copilot-runtime-client.ts
|
|
1556
1856
|
var import_runtime_client_gql3 = require("@copilotkit/runtime-client-gql");
|
|
1557
|
-
var
|
|
1558
|
-
var
|
|
1857
|
+
var import_react30 = require("react");
|
|
1858
|
+
var import_shared12 = require("@copilotkit/shared");
|
|
1559
1859
|
var useCopilotRuntimeClient = (options) => {
|
|
1560
1860
|
const { setBannerError } = useToast();
|
|
1561
1861
|
const _a = options, { showDevConsole, onError } = _a, runtimeOptions = __objRest(_a, ["showDevConsole", "onError"]);
|
|
1562
|
-
const lastStructuredErrorRef = (0,
|
|
1862
|
+
const lastStructuredErrorRef = (0, import_react30.useRef)(null);
|
|
1563
1863
|
const traceUIError = (error, originalError) => __async(void 0, null, function* () {
|
|
1564
1864
|
try {
|
|
1565
1865
|
const errorEvent = {
|
|
@@ -1585,7 +1885,7 @@ var useCopilotRuntimeClient = (options) => {
|
|
|
1585
1885
|
console.error("Error in onError handler:", error2);
|
|
1586
1886
|
}
|
|
1587
1887
|
});
|
|
1588
|
-
const runtimeClient = (0,
|
|
1888
|
+
const runtimeClient = (0, import_react30.useMemo)(() => {
|
|
1589
1889
|
return new import_runtime_client_gql3.CopilotRuntimeClient(__spreadProps(__spreadValues({}, runtimeOptions), {
|
|
1590
1890
|
handleGQLErrors: (error) => {
|
|
1591
1891
|
var _a2;
|
|
@@ -1594,7 +1894,7 @@ var useCopilotRuntimeClient = (options) => {
|
|
|
1594
1894
|
const routeError = (gqlError) => {
|
|
1595
1895
|
const extensions = gqlError.extensions;
|
|
1596
1896
|
const visibility = extensions == null ? void 0 : extensions.visibility;
|
|
1597
|
-
if (visibility ===
|
|
1897
|
+
if (visibility === import_shared12.ErrorVisibility.SILENT) {
|
|
1598
1898
|
console.error("CopilotKit Silent Error:", gqlError.message);
|
|
1599
1899
|
return;
|
|
1600
1900
|
}
|
|
@@ -1609,9 +1909,9 @@ var useCopilotRuntimeClient = (options) => {
|
|
|
1609
1909
|
setBannerError(ckError);
|
|
1610
1910
|
traceUIError(ckError, gqlError);
|
|
1611
1911
|
} else {
|
|
1612
|
-
const fallbackError = new
|
|
1912
|
+
const fallbackError = new import_shared12.CopilotKitError({
|
|
1613
1913
|
message: gqlError.message,
|
|
1614
|
-
code:
|
|
1914
|
+
code: import_shared12.CopilotKitErrorCode.UNKNOWN
|
|
1615
1915
|
});
|
|
1616
1916
|
setBannerError(fallbackError);
|
|
1617
1917
|
traceUIError(fallbackError, gqlError);
|
|
@@ -1619,9 +1919,9 @@ var useCopilotRuntimeClient = (options) => {
|
|
|
1619
1919
|
};
|
|
1620
1920
|
graphQLErrors.forEach(routeError);
|
|
1621
1921
|
} else {
|
|
1622
|
-
const fallbackError = new
|
|
1922
|
+
const fallbackError = new import_shared12.CopilotKitError({
|
|
1623
1923
|
message: (error == null ? void 0 : error.message) || String(error),
|
|
1624
|
-
code:
|
|
1924
|
+
code: import_shared12.CopilotKitErrorCode.UNKNOWN
|
|
1625
1925
|
});
|
|
1626
1926
|
setBannerError(fallbackError);
|
|
1627
1927
|
traceUIError(fallbackError, error);
|
|
@@ -1629,9 +1929,9 @@ var useCopilotRuntimeClient = (options) => {
|
|
|
1629
1929
|
},
|
|
1630
1930
|
handleGQLWarning: (message) => {
|
|
1631
1931
|
console.warn(message);
|
|
1632
|
-
const warningError = new
|
|
1932
|
+
const warningError = new import_shared12.CopilotKitError({
|
|
1633
1933
|
message,
|
|
1634
|
-
code:
|
|
1934
|
+
code: import_shared12.CopilotKitErrorCode.UNKNOWN
|
|
1635
1935
|
});
|
|
1636
1936
|
setBannerError(warningError);
|
|
1637
1937
|
}
|
|
@@ -1646,16 +1946,16 @@ function createStructuredError(gqlError) {
|
|
|
1646
1946
|
const message = (originalError == null ? void 0 : originalError.message) || gqlError.message;
|
|
1647
1947
|
const code = extensions == null ? void 0 : extensions.code;
|
|
1648
1948
|
if (code) {
|
|
1649
|
-
return new
|
|
1949
|
+
return new import_shared12.CopilotKitError({ message, code });
|
|
1650
1950
|
}
|
|
1651
1951
|
if ((_a = originalError == null ? void 0 : originalError.stack) == null ? void 0 : _a.includes("CopilotApiDiscoveryError")) {
|
|
1652
|
-
return new
|
|
1952
|
+
return new import_shared12.CopilotKitApiDiscoveryError({ message });
|
|
1653
1953
|
}
|
|
1654
1954
|
if ((_b = originalError == null ? void 0 : originalError.stack) == null ? void 0 : _b.includes("CopilotKitRemoteEndpointDiscoveryError")) {
|
|
1655
|
-
return new
|
|
1955
|
+
return new import_shared12.CopilotKitRemoteEndpointDiscoveryError({ message });
|
|
1656
1956
|
}
|
|
1657
1957
|
if ((_c = originalError == null ? void 0 : originalError.stack) == null ? void 0 : _c.includes("CopilotKitAgentDiscoveryError")) {
|
|
1658
|
-
return new
|
|
1958
|
+
return new import_shared12.CopilotKitAgentDiscoveryError({
|
|
1659
1959
|
agentName: "",
|
|
1660
1960
|
availableAgents: []
|
|
1661
1961
|
});
|
|
@@ -1664,28 +1964,28 @@ function createStructuredError(gqlError) {
|
|
|
1664
1964
|
}
|
|
1665
1965
|
|
|
1666
1966
|
// src/hooks/use-copilot-authenticated-action.ts
|
|
1667
|
-
var
|
|
1668
|
-
var
|
|
1967
|
+
var import_react31 = require("react");
|
|
1968
|
+
var import_react32 = __toESM(require("react"));
|
|
1669
1969
|
function useCopilotAuthenticatedAction_c(action, dependencies) {
|
|
1670
1970
|
const { authConfig_c, authStates_c, setAuthStates_c } = useCopilotContext();
|
|
1671
|
-
const pendingActionRef = (0,
|
|
1672
|
-
const executeAction = (0,
|
|
1971
|
+
const pendingActionRef = (0, import_react31.useRef)(null);
|
|
1972
|
+
const executeAction = (0, import_react31.useCallback)(
|
|
1673
1973
|
(props) => {
|
|
1674
1974
|
if (typeof action.render === "function") {
|
|
1675
1975
|
return action.render(props);
|
|
1676
1976
|
}
|
|
1677
|
-
return action.render ||
|
|
1977
|
+
return action.render || import_react32.default.createElement(import_react31.Fragment);
|
|
1678
1978
|
},
|
|
1679
1979
|
[action]
|
|
1680
1980
|
);
|
|
1681
|
-
const wrappedRender = (0,
|
|
1981
|
+
const wrappedRender = (0, import_react31.useCallback)(
|
|
1682
1982
|
(props) => {
|
|
1683
1983
|
const isAuthenticated = Object.values(authStates_c || {}).some(
|
|
1684
1984
|
(state) => state.status === "authenticated"
|
|
1685
1985
|
);
|
|
1686
1986
|
if (!isAuthenticated) {
|
|
1687
1987
|
pendingActionRef.current = props;
|
|
1688
|
-
return (authConfig_c == null ? void 0 : authConfig_c.SignInComponent) ?
|
|
1988
|
+
return (authConfig_c == null ? void 0 : authConfig_c.SignInComponent) ? import_react32.default.createElement(authConfig_c.SignInComponent, {
|
|
1689
1989
|
onSignInComplete: (authState) => {
|
|
1690
1990
|
setAuthStates_c == null ? void 0 : setAuthStates_c((prev) => __spreadProps(__spreadValues({}, prev), { [action.name]: authState }));
|
|
1691
1991
|
if (pendingActionRef.current) {
|
|
@@ -1693,7 +1993,7 @@ function useCopilotAuthenticatedAction_c(action, dependencies) {
|
|
|
1693
1993
|
pendingActionRef.current = null;
|
|
1694
1994
|
}
|
|
1695
1995
|
}
|
|
1696
|
-
}) :
|
|
1996
|
+
}) : import_react32.default.createElement(import_react31.Fragment);
|
|
1697
1997
|
}
|
|
1698
1998
|
return executeAction(props);
|
|
1699
1999
|
},
|
|
@@ -1708,13 +2008,13 @@ function useCopilotAuthenticatedAction_c(action, dependencies) {
|
|
|
1708
2008
|
}
|
|
1709
2009
|
|
|
1710
2010
|
// src/hooks/use-langgraph-interrupt.ts
|
|
1711
|
-
var
|
|
1712
|
-
var
|
|
2011
|
+
var import_react33 = require("react");
|
|
2012
|
+
var import_shared13 = require("@copilotkit/shared");
|
|
1713
2013
|
function useLangGraphInterrupt(action, dependencies) {
|
|
1714
|
-
const { setInterruptAction, removeInterruptAction, interruptActions, threadId } = (0,
|
|
2014
|
+
const { setInterruptAction, removeInterruptAction, interruptActions, threadId } = (0, import_react33.useContext)(CopilotContext);
|
|
1715
2015
|
const { addToast } = useToast();
|
|
1716
|
-
const actionId = (0,
|
|
1717
|
-
(0,
|
|
2016
|
+
const actionId = (0, import_shared13.dataToUUID)(action, "lgAction");
|
|
2017
|
+
(0, import_react33.useEffect)(() => {
|
|
1718
2018
|
if (!action)
|
|
1719
2019
|
return;
|
|
1720
2020
|
setInterruptAction(__spreadProps(__spreadValues({}, action), { id: actionId }));
|
|
@@ -1725,10 +2025,10 @@ function useLangGraphInterrupt(action, dependencies) {
|
|
|
1725
2025
|
}
|
|
1726
2026
|
|
|
1727
2027
|
// src/hooks/use-copilot-additional-instructions.ts
|
|
1728
|
-
var
|
|
2028
|
+
var import_react34 = require("react");
|
|
1729
2029
|
function useCopilotAdditionalInstructions({ instructions, available = "enabled" }, dependencies) {
|
|
1730
2030
|
const { setAdditionalInstructions } = useCopilotContext();
|
|
1731
|
-
(0,
|
|
2031
|
+
(0, import_react34.useEffect)(() => {
|
|
1732
2032
|
if (available === "disabled")
|
|
1733
2033
|
return;
|
|
1734
2034
|
setAdditionalInstructions((prevInstructions) => [...prevInstructions || [], instructions]);
|
|
@@ -1746,10 +2046,10 @@ function useDefaultTool(tool, dependencies) {
|
|
|
1746
2046
|
}
|
|
1747
2047
|
|
|
1748
2048
|
// src/hooks/use-copilot-chat-suggestions.tsx
|
|
1749
|
-
var
|
|
2049
|
+
var import_react35 = require("@copilotkitnext/react");
|
|
1750
2050
|
function useCopilotChatSuggestions(config, dependencies = []) {
|
|
1751
2051
|
var _a, _b;
|
|
1752
|
-
const existingConfig = (0,
|
|
2052
|
+
const existingConfig = (0, import_react35.useCopilotChatConfiguration)();
|
|
1753
2053
|
const resolvedAgentId = (_a = existingConfig == null ? void 0 : existingConfig.agentId) != null ? _a : "default";
|
|
1754
2054
|
const available = (_b = config.available === "enabled" ? "always" : config.available) != null ? _b : "before-first-message";
|
|
1755
2055
|
const finalSuggestionConfig = __spreadProps(__spreadValues({}, config), {
|
|
@@ -1757,7 +2057,7 @@ function useCopilotChatSuggestions(config, dependencies = []) {
|
|
|
1757
2057
|
consumerAgentId: resolvedAgentId
|
|
1758
2058
|
// Use chatConfig.agentId here
|
|
1759
2059
|
});
|
|
1760
|
-
(0,
|
|
2060
|
+
(0, import_react35.useConfigureSuggestions)(finalSuggestionConfig, dependencies);
|
|
1761
2061
|
}
|
|
1762
2062
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1763
2063
|
0 && (module.exports = {
|