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

135 lines (131 loc) 4.79 kB
"use client"; Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); let react = require("react"); let _copilotkit_core = require("@copilotkit/core"); //#region src/v2/lib/react-core.ts var CopilotKitCoreReact = class extends _copilotkit_core.CopilotKitCore { constructor(config) { super(config); this._renderToolCalls = []; this._hookRenderToolCalls = /* @__PURE__ */ new Map(); this._cachedMergedRenderToolCalls = null; this._renderCustomMessages = []; this._renderActivityMessages = []; this._interruptElement = null; this._renderToolCalls = config.renderToolCalls ?? []; this._renderCustomMessages = config.renderCustomMessages ?? []; this._renderActivityMessages = config.renderActivityMessages ?? []; } get renderCustomMessages() { return this._renderCustomMessages; } get renderActivityMessages() { return this._renderActivityMessages; } get renderToolCalls() { if (this._hookRenderToolCalls.size === 0) return this._renderToolCalls; if (this._cachedMergedRenderToolCalls) return this._cachedMergedRenderToolCalls; const merged = /* @__PURE__ */ new Map(); for (const rc of this._renderToolCalls) merged.set(`${rc.agentId ?? ""}:${rc.name}`, rc); for (const [key, rc] of this._hookRenderToolCalls) merged.set(key, rc); this._cachedMergedRenderToolCalls = Array.from(merged.values()); return this._cachedMergedRenderToolCalls; } setRenderActivityMessages(renderers) { this._renderActivityMessages = renderers; } setRenderCustomMessages(renderers) { this._renderCustomMessages = renderers; } setRenderToolCalls(renderToolCalls) { this._renderToolCalls = renderToolCalls; this._cachedMergedRenderToolCalls = null; this._notifyRenderToolCallsChanged(); } addHookRenderToolCall(entry) { const key = `${entry.agentId ?? ""}:${entry.name}`; this._hookRenderToolCalls.set(key, entry); this._cachedMergedRenderToolCalls = null; this._notifyRenderToolCallsChanged(); } removeHookRenderToolCall(name, agentId) { const key = `${agentId ?? ""}:${name}`; if (this._hookRenderToolCalls.delete(key)) { this._cachedMergedRenderToolCalls = null; this._notifyRenderToolCallsChanged(); } } _notifyRenderToolCallsChanged() { this.notifySubscribers((subscriber) => { const reactSubscriber = subscriber; if (reactSubscriber.onRenderToolCallsChanged) reactSubscriber.onRenderToolCallsChanged({ copilotkit: this, renderToolCalls: this.renderToolCalls }); }, "Subscriber onRenderToolCallsChanged error:"); } get interruptElement() { return this._interruptElement; } setInterruptElement(element) { this._interruptElement = element; this.notifySubscribers((subscriber) => { subscriber.onInterruptElementChanged?.({ copilotkit: this, interruptElement: this._interruptElement }); }, "Subscriber onInterruptElementChanged error:"); } subscribe(subscriber) { return super.subscribe(subscriber); } /** * Wait for pending React state updates before the follow-up agent run. * * When a frontend tool handler calls setState(), React 18 batches the update * and schedules a commit via its internal scheduler (MessageChannel). The * useAgentContext hook registers context via useLayoutEffect, which runs * synchronously after React commits that batch. * * Awaiting a zero-delay timeout yields to the macrotask queue. React's * MessageChannel task runs first, committing the pending state and running * useLayoutEffect (which updates the context store). The follow-up runAgent * call then reads fresh context. */ async waitForPendingFrameworkUpdates() { await new Promise((resolve) => setTimeout(resolve, 0)); } }; //#endregion //#region src/v2/context.ts const EMPTY_SET = /* @__PURE__ */ new Set(); const CopilotKitContext = (0, react.createContext)(null); const useCopilotKit = () => { const context = (0, react.useContext)(CopilotKitContext); const [, forceUpdate] = (0, react.useReducer)((x) => x + 1, 0); if (!context) throw new Error("useCopilotKit must be used within CopilotKitProvider"); (0, react.useEffect)(() => { const subscription = context.copilotkit.subscribe({ onRuntimeConnectionStatusChanged: () => { forceUpdate(); } }); return () => { subscription.unsubscribe(); }; }, []); return context; }; const LicenseContext = (0, react.createContext)({ status: null, license: null, checkFeature: () => true, getLimit: () => null }); const useLicenseContext = () => (0, react.useContext)(LicenseContext); //#endregion exports.CopilotKitContext = CopilotKitContext; exports.CopilotKitCoreReact = CopilotKitCoreReact; exports.EMPTY_SET = EMPTY_SET; exports.LicenseContext = LicenseContext; exports.useCopilotKit = useCopilotKit; exports.useLicenseContext = useLicenseContext; //# sourceMappingURL=context.cjs.map