{"version":3,"file":"context.mjs","names":[],"sources":["../../src/v2/lib/react-core.ts","../../src/v2/context.ts"],"sourcesContent":["import React from \"react\";\nimport { ReactActivityMessageRenderer, ReactToolCallRenderer } from \"../types\";\nimport { ReactCustomMessageRenderer } from \"../types/react-custom-message-renderer\";\nimport {\n  CopilotKitCore,\n  type CopilotKitCoreConfig,\n  type CopilotKitCoreSubscriber,\n  type CopilotKitCoreSubscription,\n} from \"@copilotkit/core\";\n\nexport interface CopilotKitCoreReactConfig extends CopilotKitCoreConfig {\n  // Add any additional configuration properties specific to the React implementation\n  renderToolCalls?: ReactToolCallRenderer<any>[];\n  renderActivityMessages?: ReactActivityMessageRenderer<any>[];\n\n  // Add custom message renderers\n  renderCustomMessages?: ReactCustomMessageRenderer[];\n}\n\nexport interface CopilotKitCoreReactSubscriber extends CopilotKitCoreSubscriber {\n  onRenderToolCallsChanged?: (event: {\n    copilotkit: CopilotKitCore;\n    renderToolCalls: ReactToolCallRenderer<any>[];\n  }) => void | Promise<void>;\n  onInterruptElementChanged?: (event: {\n    copilotkit: CopilotKitCore;\n    interruptElement: React.ReactElement | null;\n  }) => void | Promise<void>;\n}\n\nexport class CopilotKitCoreReact extends CopilotKitCore {\n  private _renderToolCalls: ReactToolCallRenderer<any>[] = [];\n  private _hookRenderToolCalls: Map<string, ReactToolCallRenderer<any>> =\n    new Map();\n  private _cachedMergedRenderToolCalls: ReactToolCallRenderer<any>[] | null =\n    null;\n  private _renderCustomMessages: ReactCustomMessageRenderer[] = [];\n  private _renderActivityMessages: ReactActivityMessageRenderer<any>[] = [];\n  private _interruptElement: React.ReactElement | null = null;\n\n  constructor(config: CopilotKitCoreReactConfig) {\n    super(config);\n    this._renderToolCalls = config.renderToolCalls ?? [];\n    this._renderCustomMessages = config.renderCustomMessages ?? [];\n    this._renderActivityMessages = config.renderActivityMessages ?? [];\n  }\n\n  get renderCustomMessages(): Readonly<ReactCustomMessageRenderer[]> {\n    return this._renderCustomMessages;\n  }\n\n  get renderActivityMessages(): Readonly<ReactActivityMessageRenderer<any>>[] {\n    return this._renderActivityMessages;\n  }\n\n  get renderToolCalls(): Readonly<ReactToolCallRenderer<any>>[] {\n    if (this._hookRenderToolCalls.size === 0) {\n      return this._renderToolCalls;\n    }\n    if (this._cachedMergedRenderToolCalls) {\n      return this._cachedMergedRenderToolCalls;\n    }\n    // Merge: hook entries override prop entries with the same key\n    const merged = new Map<string, ReactToolCallRenderer<any>>();\n    for (const rc of this._renderToolCalls) {\n      merged.set(`${rc.agentId ?? \"\"}:${rc.name}`, rc);\n    }\n    for (const [key, rc] of this._hookRenderToolCalls) {\n      merged.set(key, rc);\n    }\n    this._cachedMergedRenderToolCalls = Array.from(merged.values());\n    return this._cachedMergedRenderToolCalls;\n  }\n\n  setRenderActivityMessages(\n    renderers: ReactActivityMessageRenderer<any>[],\n  ): void {\n    this._renderActivityMessages = renderers;\n  }\n\n  setRenderCustomMessages(renderers: ReactCustomMessageRenderer[]): void {\n    this._renderCustomMessages = renderers;\n  }\n\n  setRenderToolCalls(renderToolCalls: ReactToolCallRenderer<any>[]): void {\n    this._renderToolCalls = renderToolCalls;\n    this._cachedMergedRenderToolCalls = null;\n    this._notifyRenderToolCallsChanged();\n  }\n\n  addHookRenderToolCall(entry: ReactToolCallRenderer<any>): void {\n    const key = `${entry.agentId ?? \"\"}:${entry.name}`;\n    this._hookRenderToolCalls.set(key, entry);\n    this._cachedMergedRenderToolCalls = null;\n    this._notifyRenderToolCallsChanged();\n  }\n\n  removeHookRenderToolCall(name: string, agentId?: string): void {\n    const key = `${agentId ?? \"\"}:${name}`;\n    if (this._hookRenderToolCalls.delete(key)) {\n      this._cachedMergedRenderToolCalls = null;\n      this._notifyRenderToolCallsChanged();\n    }\n  }\n\n  private _notifyRenderToolCallsChanged(): void {\n    void this.notifySubscribers((subscriber) => {\n      const reactSubscriber = subscriber as CopilotKitCoreReactSubscriber;\n      if (reactSubscriber.onRenderToolCallsChanged) {\n        reactSubscriber.onRenderToolCallsChanged({\n          copilotkit: this,\n          renderToolCalls: this.renderToolCalls,\n        });\n      }\n    }, \"Subscriber onRenderToolCallsChanged error:\");\n  }\n\n  get interruptElement(): React.ReactElement | null {\n    return this._interruptElement;\n  }\n\n  setInterruptElement(element: React.ReactElement | null): void {\n    this._interruptElement = element;\n    void this.notifySubscribers((subscriber) => {\n      const reactSubscriber = subscriber as CopilotKitCoreReactSubscriber;\n      reactSubscriber.onInterruptElementChanged?.({\n        copilotkit: this,\n        interruptElement: this._interruptElement,\n      });\n    }, \"Subscriber onInterruptElementChanged error:\");\n  }\n\n  // Override to accept React-specific subscriber type\n  subscribe(\n    subscriber: CopilotKitCoreReactSubscriber,\n  ): CopilotKitCoreSubscription {\n    return super.subscribe(subscriber);\n  }\n\n  /**\n   * Wait for pending React state updates before the follow-up agent run.\n   *\n   * When a frontend tool handler calls setState(), React 18 batches the update\n   * and schedules a commit via its internal scheduler (MessageChannel). The\n   * useAgentContext hook registers context via useLayoutEffect, which runs\n   * synchronously after React commits that batch.\n   *\n   * Awaiting a zero-delay timeout yields to the macrotask queue. React's\n   * MessageChannel task runs first, committing the pending state and running\n   * useLayoutEffect (which updates the context store). The follow-up runAgent\n   * call then reads fresh context.\n   */\n  async waitForPendingFrameworkUpdates(): Promise<void> {\n    await new Promise<void>((resolve) => setTimeout(resolve, 0));\n  }\n}\n","\"use client\";\n\nimport { createContext, useContext, useEffect, useReducer } from \"react\";\nimport { CopilotKitCoreReact } from \"./lib/react-core\";\nimport type { CopilotKitCoreReactConfig } from \"./lib/react-core\";\nimport type { LicenseContextValue } from \"@copilotkit/shared\";\n\n// Re-export so headless.ts (and consumers) reference the same type declaration.\nexport { CopilotKitCoreReact };\nexport type { CopilotKitCoreReactConfig };\n\nexport interface CopilotKitContextValue {\n  copilotkit: CopilotKitCoreReact;\n  /**\n   * Set of tool call IDs currently being executed.\n   * This is tracked at the provider level to ensure tool execution events\n   * are captured even before child components mount.\n   */\n  executingToolCallIds: ReadonlySet<string>;\n}\n\nexport const EMPTY_SET: ReadonlySet<string> = new Set();\n\nexport const CopilotKitContext = createContext<CopilotKitContextValue | null>(\n  null,\n);\n\nexport const useCopilotKit = (): CopilotKitContextValue => {\n  const context = useContext(CopilotKitContext);\n  const [, forceUpdate] = useReducer((x: number) => x + 1, 0);\n\n  if (!context) {\n    throw new Error(\"useCopilotKit must be used within CopilotKitProvider\");\n  }\n  useEffect(() => {\n    const subscription = context.copilotkit.subscribe({\n      onRuntimeConnectionStatusChanged: () => {\n        forceUpdate();\n      },\n    });\n    return () => {\n      subscription.unsubscribe();\n    };\n    // eslint-disable-next-line react-hooks/exhaustive-deps\n  }, []);\n\n  return context;\n};\n\n// License context — shared between web and RN providers.\n// Default is permissive (all features allowed) — providers override via createLicenseContextValue.\n// Inlined here to avoid a runtime import from @copilotkit/shared, which pulls in\n// Node-only deps (jose) that break React Native's Metro bundler.\nexport const LicenseContext = createContext<LicenseContextValue>({\n  status: null,\n  license: null,\n  checkFeature: () => true,\n  getLimit: () => null,\n} as LicenseContextValue);\n\nexport const useLicenseContext = (): LicenseContextValue =>\n  useContext(LicenseContext);\n"],"mappings":";;;;;;AA8BA,IAAa,sBAAb,cAAyC,eAAe;CAUtD,YAAY,QAAmC;AAC7C,QAAM,OAAO;0BAV0C,EAAE;8CAEzD,IAAI,KAAK;sCAET;+BAC4D,EAAE;iCACO,EAAE;2BAClB;AAIrD,OAAK,mBAAmB,OAAO,mBAAmB,EAAE;AACpD,OAAK,wBAAwB,OAAO,wBAAwB,EAAE;AAC9D,OAAK,0BAA0B,OAAO,0BAA0B,EAAE;;CAGpE,IAAI,uBAA+D;AACjE,SAAO,KAAK;;CAGd,IAAI,yBAAwE;AAC1E,SAAO,KAAK;;CAGd,IAAI,kBAA0D;AAC5D,MAAI,KAAK,qBAAqB,SAAS,EACrC,QAAO,KAAK;AAEd,MAAI,KAAK,6BACP,QAAO,KAAK;EAGd,MAAM,yBAAS,IAAI,KAAyC;AAC5D,OAAK,MAAM,MAAM,KAAK,iBACpB,QA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