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;" />

163 lines (161 loc) 6.51 kB
import { areStatesEquals, getEffectiveRunId, isPlaceholderMessageId, isPlaceholderMessageName, readCachedMessageEntry, resolveClaim, selectSnapshot } from "./chunk-7GIBHX6X.mjs"; import { __spreadValues } from "./chunk-SKC7AJIV.mjs"; // src/hooks/use-coagent-state-render-registry.ts import { useEffect } from "react"; var LAST_SNAPSHOTS_BY_RENDER_AND_RUN = "__lastSnapshotsByStateRenderIdAndRun"; var LAST_SNAPSHOTS_BY_MESSAGE = "__lastSnapshotsByMessageId"; function getClaimsStore(claimsRef) { return claimsRef.current; } function getSnapshotCaches(claimsRef) { var _a, _b; const store = getClaimsStore(claimsRef); return { byStateRenderAndRun: (_a = store[LAST_SNAPSHOTS_BY_RENDER_AND_RUN]) != null ? _a : {}, byMessageId: (_b = store[LAST_SNAPSHOTS_BY_MESSAGE]) != null ? _b : {} }; } function useStateRenderRegistry({ agentId, stateRenderId, message, messageIndex, stateSnapshot, agentState, agentMessages, claimsRef }) { var _a, _b, _c, _d, _e, _f; const store = getClaimsStore(claimsRef); const runId = message.runId; const cachedMessageEntry = (_a = store[LAST_SNAPSHOTS_BY_MESSAGE]) == null ? void 0 : _a[message.id]; const { runId: cachedMessageRunId } = readCachedMessageEntry(cachedMessageEntry); const existingClaimRunId = (_b = claimsRef.current[message.id]) == null ? void 0 : _b.runId; const effectiveRunId = getEffectiveRunId({ existingClaimRunId, cachedMessageRunId, runId }); useEffect(() => { return () => { var _a2, _b2, _c2, _d2; const existingClaim2 = claimsRef.current[message.id]; if ((existingClaim2 == null ? void 0 : existingClaim2.stateSnapshot) && Object.keys(existingClaim2.stateSnapshot).length > 0) { const snapshotCache = __spreadValues({}, (_a2 = store[LAST_SNAPSHOTS_BY_RENDER_AND_RUN]) != null ? _a2 : {}); const cacheKey = `${existingClaim2.stateRenderId}::${(_b2 = existingClaim2.runId) != null ? _b2 : "pending"}`; snapshotCache[cacheKey] = existingClaim2.stateSnapshot; snapshotCache[`${existingClaim2.stateRenderId}::latest`] = existingClaim2.stateSnapshot; store[LAST_SNAPSHOTS_BY_RENDER_AND_RUN] = snapshotCache; const messageCache = __spreadValues({}, (_c2 = store[LAST_SNAPSHOTS_BY_MESSAGE]) != null ? _c2 : {}); messageCache[message.id] = { snapshot: existingClaim2.stateSnapshot, runId: (_d2 = existingClaim2.runId) != null ? _d2 : effectiveRunId }; store[LAST_SNAPSHOTS_BY_MESSAGE] = messageCache; } delete claimsRef.current[message.id]; }; }, [claimsRef, effectiveRunId, message.id]); if (!stateRenderId) { return { canRender: false }; } const caches = getSnapshotCaches(claimsRef); const existingClaim = claimsRef.current[message.id]; const { snapshot, hasSnapshotKeys, allowEmptySnapshot, snapshotForClaim } = selectSnapshot({ messageId: message.id, messageName: message.name, allowLiveState: isPlaceholderMessageName(message.name) || isPlaceholderMessageId(message.id), skipLatestCache: isPlaceholderMessageName(message.name) || isPlaceholderMessageId(message.id), stateRenderId, effectiveRunId, stateSnapshotProp: stateSnapshot, agentState, agentMessages, existingClaim, caches }); const resolution = resolveClaim({ claims: claimsRef.current, context: { agentId, messageId: message.id, stateRenderId, runId: effectiveRunId, messageIndex }, stateSnapshot: snapshotForClaim }); if (resolution.action === "block" /* Block */) { return { canRender: false }; } if (resolution.updateRunId && claimsRef.current[message.id]) { claimsRef.current[message.id].runId = resolution.updateRunId; } if (resolution.nextClaim) { claimsRef.current[message.id] = resolution.nextClaim; } if (resolution.lockOthers) { Object.entries(claimsRef.current).forEach(([id, claim]) => { if (id !== message.id && claim.stateRenderId === stateRenderId) { claim.locked = true; } }); } if (existingClaim && !existingClaim.locked && (agentMessages == null ? void 0 : agentMessages.length)) { const indexInAgentMessages = agentMessages.findIndex( (msg) => msg.id === message.id ); if (indexInAgentMessages >= 0 && indexInAgentMessages < agentMessages.length - 1) { existingClaim.locked = true; } } const existingSnapshot = claimsRef.current[message.id].stateSnapshot; const snapshotChanged = stateSnapshot && existingSnapshot !== void 0 && !areStatesEquals(existingSnapshot, snapshot); if (snapshot && (stateSnapshot || hasSnapshotKeys || allowEmptySnapshot) && (!claimsRef.current[message.id].locked || snapshotChanged)) { if (!claimsRef.current[message.id].locked || snapshotChanged) { claimsRef.current[message.id].stateSnapshot = snapshot; const snapshotCache = __spreadValues({}, (_c = store[LAST_SNAPSHOTS_BY_RENDER_AND_RUN]) != null ? _c : {}); const cacheKey = `${stateRenderId}::${effectiveRunId}`; snapshotCache[cacheKey] = snapshot; snapshotCache[`${stateRenderId}::latest`] = snapshot; store[LAST_SNAPSHOTS_BY_RENDER_AND_RUN] = snapshotCache; const messageCache = __spreadValues({}, (_d = store[LAST_SNAPSHOTS_BY_MESSAGE]) != null ? _d : {}); messageCache[message.id] = { snapshot, runId: effectiveRunId }; store[LAST_SNAPSHOTS_BY_MESSAGE] = messageCache; if (stateSnapshot) { claimsRef.current[message.id].locked = true; } } } else if (snapshotForClaim) { const existingSnapshot2 = claimsRef.current[message.id].stateSnapshot; if (!existingSnapshot2) { claimsRef.current[message.id].stateSnapshot = snapshotForClaim; const snapshotCache = __spreadValues({}, (_e = store[LAST_SNAPSHOTS_BY_RENDER_AND_RUN]) != null ? _e : {}); const cacheKey = `${stateRenderId}::${effectiveRunId}`; snapshotCache[cacheKey] = snapshotForClaim; snapshotCache[`${stateRenderId}::latest`] = snapshotForClaim; store[LAST_SNAPSHOTS_BY_RENDER_AND_RUN] = snapshotCache; const messageCache = __spreadValues({}, (_f = store[LAST_SNAPSHOTS_BY_MESSAGE]) != null ? _f : {}); messageCache[message.id] = { snapshot: snapshotForClaim, runId: effectiveRunId }; store[LAST_SNAPSHOTS_BY_MESSAGE] = messageCache; } } return { canRender: true }; } export { useStateRenderRegistry }; //# sourceMappingURL=chunk-QXZTCGF4.mjs.map