UNPKG

@copilotkit/react-core

Version:

<img src="https://github.com/user-attachments/assets/0a6b64d9-e193-4940-a3f6-60334ac34084" alt="banner" style="border-radius: 12px; border: 2px solid #d6d4fa;" />

195 lines (193 loc) • 8.16 kB
import { __objRest } from "./chunk-SKC7AJIV.mjs"; // src/hooks/use-coagent-state-render-bridge.helpers.ts import { dataToUUID, parseJson } from "@copilotkit/shared"; var RenderStatus = /* @__PURE__ */ ((RenderStatus2) => { RenderStatus2["InProgress"] = "inProgress"; RenderStatus2["Complete"] = "complete"; return RenderStatus2; })(RenderStatus || {}); var ClaimAction = /* @__PURE__ */ ((ClaimAction2) => { ClaimAction2["Create"] = "create"; ClaimAction2["Override"] = "override"; ClaimAction2["Existing"] = "existing"; ClaimAction2["Block"] = "block"; return ClaimAction2; })(ClaimAction || {}); function getStateWithoutConstantKeys(state) { if (!state) return {}; const _a = state, { messages, tools, copilotkit } = _a, stateWithoutConstantKeys = __objRest(_a, ["messages", "tools", "copilotkit"]); return stateWithoutConstantKeys; } function areStatesEquals(a, b) { if (a && !b || !a && b) return false; const _a = a, { messages, tools, copilotkit } = _a, aWithoutConstantKeys = __objRest(_a, ["messages", "tools", "copilotkit"]); const _b = b, { messages: bMessages, tools: bTools, copilotkit: bCopilotkit } = _b, bWithoutConstantKeys = __objRest(_b, [ "messages", "tools", "copilotkit" ]); return JSON.stringify(aWithoutConstantKeys) === JSON.stringify(bWithoutConstantKeys); } function isPlaceholderMessageId(messageId) { return !!messageId && messageId.startsWith("coagent-state-render-"); } function isPlaceholderMessageName(messageName) { return messageName === "coagent-state-render"; } function readCachedMessageEntry(entry) { if (!entry || typeof entry !== "object") { return { snapshot: entry, runId: void 0 }; } const snapshot = "snapshot" in entry ? entry.snapshot : entry; const runId = "runId" in entry ? entry.runId : void 0; return { snapshot, runId }; } function getEffectiveRunId({ existingClaimRunId, cachedMessageRunId, runId }) { return existingClaimRunId || cachedMessageRunId || runId || "pending"; } function resolveClaim({ claims, context, stateSnapshot }) { const { messageId, stateRenderId, runId, messageIndex } = context; const existing = claims[messageId]; if (existing) { const canRender = existing.stateRenderId === stateRenderId; const shouldUpdateRunId = canRender && runId && (!existing.runId || existing.runId === "pending"); return { canRender, action: canRender ? "existing" /* Existing */ : "block" /* Block */, updateRunId: shouldUpdateRunId ? runId : void 0 }; } const normalizedRunId = runId != null ? runId : "pending"; const renderClaimedByOtherMessageEntry = Object.entries(claims).find( ([, claim]) => { var _a; return claim.stateRenderId === stateRenderId && ((_a = claim.runId) != null ? _a : "pending") === normalizedRunId && dataToUUID(getStateWithoutConstantKeys(claim.stateSnapshot)) === dataToUUID(getStateWithoutConstantKeys(stateSnapshot)); } ); const renderClaimedByOtherMessage = renderClaimedByOtherMessageEntry == null ? void 0 : renderClaimedByOtherMessageEntry[1]; const claimedMessageId = renderClaimedByOtherMessageEntry == null ? void 0 : renderClaimedByOtherMessageEntry[0]; if (renderClaimedByOtherMessage) { if (messageIndex !== void 0 && renderClaimedByOtherMessage.messageIndex !== void 0 && messageIndex > renderClaimedByOtherMessage.messageIndex) { return { canRender: true, action: "override" /* Override */, nextClaim: { stateRenderId, runId, messageIndex }, lockOthers: runId === renderClaimedByOtherMessage.runId || isPlaceholderMessageId(claimedMessageId) }; } if (runId && renderClaimedByOtherMessage.runId && runId !== renderClaimedByOtherMessage.runId) { return { canRender: true, action: "override" /* Override */, nextClaim: { stateRenderId, runId, messageIndex }, lockOthers: isPlaceholderMessageId(claimedMessageId) }; } if (isPlaceholderMessageId(claimedMessageId)) { return { canRender: true, action: "override" /* Override */, nextClaim: { stateRenderId, runId, messageIndex }, lockOthers: true }; } if (stateSnapshot && renderClaimedByOtherMessage.stateSnapshot && !areStatesEquals(renderClaimedByOtherMessage.stateSnapshot, stateSnapshot)) { return { canRender: true, action: "override" /* Override */, nextClaim: { stateRenderId, runId } }; } return { canRender: false, action: "block" /* Block */ }; } if (!runId) { return { canRender: false, action: "block" /* Block */ }; } return { canRender: true, action: "create" /* Create */, nextClaim: { stateRenderId, runId, messageIndex } }; } function selectSnapshot({ messageId, messageName, allowLiveState, skipLatestCache, stateRenderId, effectiveRunId, stateSnapshotProp, agentState, agentMessages, existingClaim, caches }) { var _a, _b, _c, _d, _e, _f; const lastAssistantId = agentMessages ? (_a = [...agentMessages].reverse().find((msg) => msg.role === "assistant")) == null ? void 0 : _a.id : void 0; const latestSnapshot = stateRenderId !== void 0 ? caches.byStateRenderAndRun[`${stateRenderId}::latest`] : void 0; const messageIndex = agentMessages ? agentMessages.findIndex((msg) => msg.id === messageId) : -1; const messageRole = messageIndex >= 0 && agentMessages ? (_b = agentMessages[messageIndex]) == null ? void 0 : _b.role : void 0; let previousUserMessageId; if (messageIndex > 0 && agentMessages) { for (let i = messageIndex - 1; i >= 0; i -= 1) { if (((_c = agentMessages[i]) == null ? void 0 : _c.role) === "user") { previousUserMessageId = (_d = agentMessages[i]) == null ? void 0 : _d.id; break; } } } const liveStateIsStale = stateSnapshotProp === void 0 && latestSnapshot !== void 0 && agentState !== void 0 && areStatesEquals(latestSnapshot, agentState); const shouldUseLiveState = (Boolean(allowLiveState) || !lastAssistantId || messageId === lastAssistantId) && !liveStateIsStale; const snapshot = stateSnapshotProp ? parseJson(stateSnapshotProp, stateSnapshotProp) : shouldUseLiveState ? agentState : void 0; const hasSnapshotKeys = !!(snapshot && Object.keys(snapshot).length > 0); const allowEmptySnapshot = snapshot !== void 0 && !hasSnapshotKeys && (stateSnapshotProp !== void 0 || shouldUseLiveState); const messageCacheEntry = caches.byMessageId[messageId]; const cachedMessageSnapshot = readCachedMessageEntry(messageCacheEntry).snapshot; const cacheKey = stateRenderId !== void 0 ? `${stateRenderId}::${effectiveRunId}` : void 0; let cachedSnapshot = cachedMessageSnapshot != null ? cachedMessageSnapshot : caches.byMessageId[messageId]; if (cachedSnapshot === void 0 && cacheKey && caches.byStateRenderAndRun[cacheKey] !== void 0) { cachedSnapshot = caches.byStateRenderAndRun[cacheKey]; } if (cachedSnapshot === void 0 && stateRenderId && previousUserMessageId && caches.byStateRenderAndRun[`${stateRenderId}::pending:${previousUserMessageId}`] !== void 0) { cachedSnapshot = caches.byStateRenderAndRun[`${stateRenderId}::pending:${previousUserMessageId}`]; } if (cachedSnapshot === void 0 && !skipLatestCache && stateRenderId && messageRole !== "assistant" && (stateSnapshotProp !== void 0 || agentState && Object.keys(agentState).length > 0)) { cachedSnapshot = caches.byStateRenderAndRun[`${stateRenderId}::latest`]; } 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; return { snapshot, hasSnapshotKeys, cachedSnapshot, allowEmptySnapshot, snapshotForClaim }; } export { RenderStatus, ClaimAction, areStatesEquals, isPlaceholderMessageId, isPlaceholderMessageName, readCachedMessageEntry, getEffectiveRunId, resolveClaim, selectSnapshot }; //# sourceMappingURL=chunk-7GIBHX6X.mjs.map