@copilotkit/react-core 2.0.0-rc.1 → 2.0.0-rc.10
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/dist/{MarkdownRendererContext-B4Us6mwL.d.cts → MarkdownRendererContext-CS35epe1.d.mts} +17 -33
- package/dist/MarkdownRendererContext-CS35epe1.d.mts.map +1 -0
- package/dist/{MarkdownRendererContext-CRKFzsEB.d.mts → MarkdownRendererContext-CfTh92vk.d.cts} +17 -33
- package/dist/MarkdownRendererContext-CfTh92vk.d.cts.map +1 -0
- package/dist/{MarkdownSurfaces-ChSlVYOk.mjs → MarkdownSurfaces-CimUSR3Y.mjs} +153 -76
- package/dist/MarkdownSurfaces-CimUSR3Y.mjs.map +1 -0
- package/dist/{MarkdownSurfaces-C2XZISKn.cjs → MarkdownSurfaces-DGDeUhbX.cjs} +175 -85
- package/dist/MarkdownSurfaces-DGDeUhbX.cjs.map +1 -0
- package/dist/context.cjs +20 -17
- package/dist/context.cjs.map +1 -1
- package/dist/context.d.cts +38 -98
- package/dist/context.d.cts.map +1 -1
- package/dist/context.d.mts +38 -98
- package/dist/context.d.mts.map +1 -1
- package/dist/context.mjs +20 -17
- package/dist/context.mjs.map +1 -1
- package/dist/generative-ui.cjs +564 -564
- package/dist/generative-ui.cjs.map +1 -1
- package/dist/generative-ui.d.cts +2137 -4178
- package/dist/generative-ui.d.cts.map +1 -1
- package/dist/generative-ui.d.mts +2137 -4178
- package/dist/generative-ui.d.mts.map +1 -1
- package/dist/generative-ui.mjs +69 -70
- package/dist/generative-ui.mjs.map +1 -1
- package/dist/headless.cjs +50 -76
- package/dist/headless.cjs.map +1 -1
- package/dist/headless.d.cts +78 -253
- package/dist/headless.d.cts.map +1 -1
- package/dist/headless.d.mts +56 -202
- package/dist/headless.d.mts.map +1 -1
- package/dist/headless.mjs +43 -74
- package/dist/headless.mjs.map +1 -1
- package/dist/index.cjs +1383 -1958
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +213 -538
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +212 -537
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +1089 -1920
- package/dist/index.mjs.map +1 -1
- package/dist/index.umd.js +1549 -2039
- package/dist/index.umd.js.map +1 -1
- package/package.json +20 -35
- package/dist/MarkdownRendererContext-B4Us6mwL.d.cts.map +0 -1
- package/dist/MarkdownRendererContext-CRKFzsEB.d.mts.map +0 -1
- package/dist/MarkdownSurfaces-C2XZISKn.cjs.map +0 -1
- package/dist/MarkdownSurfaces-ChSlVYOk.mjs.map +0 -1
- package/dist/index.css +0 -2
package/dist/context.cjs
CHANGED
|
@@ -5,9 +5,28 @@ let react = require("react");
|
|
|
5
5
|
let _copilotkit_core = require("@copilotkit/core");
|
|
6
6
|
|
|
7
7
|
//#region src/lib/react-core.ts
|
|
8
|
+
/**
|
|
9
|
+
* Wait for pending React state updates before the follow-up agent run.
|
|
10
|
+
*
|
|
11
|
+
* When a frontend tool handler calls setState(), React 18 batches the update
|
|
12
|
+
* and schedules a commit via its internal scheduler (MessageChannel). The
|
|
13
|
+
* useAgentContext hook registers context via useLayoutEffect, which runs
|
|
14
|
+
* synchronously after React commits that batch.
|
|
15
|
+
*
|
|
16
|
+
* Awaiting a zero-delay timeout yields to the macrotask queue. React's
|
|
17
|
+
* MessageChannel task runs first, committing the pending state and running
|
|
18
|
+
* useLayoutEffect (which updates the context store). The follow-up runAgent
|
|
19
|
+
* call then reads fresh context.
|
|
20
|
+
*/
|
|
21
|
+
function waitForReactCommit() {
|
|
22
|
+
return new Promise((resolve) => setTimeout(resolve, 0));
|
|
23
|
+
}
|
|
8
24
|
var CopilotKitCoreReact = class extends _copilotkit_core.CopilotKitCore {
|
|
9
25
|
constructor(config) {
|
|
10
|
-
super(
|
|
26
|
+
super({
|
|
27
|
+
...config,
|
|
28
|
+
ɵwaitForFrameworkUpdates: waitForReactCommit
|
|
29
|
+
});
|
|
11
30
|
this._renderToolCalls = [];
|
|
12
31
|
this._hookRenderToolCalls = /* @__PURE__ */ new Map();
|
|
13
32
|
this._cachedMergedRenderToolCalls = null;
|
|
@@ -81,22 +100,6 @@ var CopilotKitCoreReact = class extends _copilotkit_core.CopilotKitCore {
|
|
|
81
100
|
subscribe(subscriber) {
|
|
82
101
|
return super.subscribe(subscriber);
|
|
83
102
|
}
|
|
84
|
-
/**
|
|
85
|
-
* Wait for pending React state updates before the follow-up agent run.
|
|
86
|
-
*
|
|
87
|
-
* When a frontend tool handler calls setState(), React 18 batches the update
|
|
88
|
-
* and schedules a commit via its internal scheduler (MessageChannel). The
|
|
89
|
-
* useAgentContext hook registers context via useLayoutEffect, which runs
|
|
90
|
-
* synchronously after React commits that batch.
|
|
91
|
-
*
|
|
92
|
-
* Awaiting a zero-delay timeout yields to the macrotask queue. React's
|
|
93
|
-
* MessageChannel task runs first, committing the pending state and running
|
|
94
|
-
* useLayoutEffect (which updates the context store). The follow-up runAgent
|
|
95
|
-
* call then reads fresh context.
|
|
96
|
-
*/
|
|
97
|
-
async waitForPendingFrameworkUpdates() {
|
|
98
|
-
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
99
|
-
}
|
|
100
103
|
};
|
|
101
104
|
|
|
102
105
|
//#endregion
|
package/dist/context.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"context.cjs","names":["CopilotKitCore"],"sources":["../src/lib/react-core.ts","../src/context.ts"],"sourcesContent":["import React from \"react\";\nimport type {\n ReactActivityMessageRenderer,\n ReactToolCallRenderer,\n} from \"../types\";\nimport type { ReactCustomMessageRenderer } from \"../types/react-custom-message-renderer\";\nimport { CopilotKitCore } from \"@copilotkit/core\";\nimport type {\n CopilotKitCoreConfig,\n CopilotKitCoreSubscriber,\n 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 * Whether CopilotKit should automatically mount Intelligence indicators.\n * Defaults to true when omitted for compatibility with custom providers.\n */\n showIntelligenceIndicator?: boolean;\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 onHeadersChanged: () => {\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\n// Provider-level default agent id, published by `<CopilotKitProvider agentId>`.\n//\n// This is a context of its own rather than a `CopilotChatConfigurationProvider`\n// rendered at the root, because that provider also owns a thread: it resolves a\n// threadId (minting a UUID when none is given) and the top-most one owns the\n// imperative active-thread override. Rendering it around the whole application\n// would hand every `<CopilotChat>` the same inherited threadId, so two sibling\n// chats would share one transcript. A bare string context carries the agent\n// default and nothing else.\n//\n// It is the LAST fallback before `DEFAULT_AGENT_ID`, so an explicit `agentId`\n// argument, a `<CopilotChat agentId>`, and a `<CopilotChatConfigurationProvider\n// agentId>` all still win.\nexport const CopilotKitAgentIdContext = createContext<string | undefined>(\n undefined,\n);\n\nexport const useDefaultAgentId = (): string | undefined =>\n useContext(CopilotKitAgentIdContext);\n"],"mappings":";;;;;;;AAiCA,IAAa,sBAAb,cAAyCA,gCAAe;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,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,EAAE,CAAC;;;;;;AClIhE,MAAa,4BAAiC,IAAI,KAAK;AAEvD,MAAa,6CACX,KACD;AAED,MAAa,sBAA8C;CACzD,MAAM,gCAAqB,kBAAkB;CAC7C,MAAM,GAAG,sCAA2B,MAAc,IAAI,GAAG,EAAE;AAE3D,KAAI,CAAC,QACH,OAAM,IAAI,MAAM,uDAAuD;AAEzE,4BAAgB;EACd,MAAM,eAAe,QAAQ,WAAW,UAAU;GAChD,wCAAwC;AACtC,iBAAa;;GAEf,wBAAwB;AACtB,iBAAa;;GAEhB,CAAC;AACF,eAAa;AACX,gBAAa,aAAa;;IAG3B,EAAE,CAAC;AAEN,QAAO;;AAOT,MAAa,0CAAoD;CAC/D,QAAQ;CACR,SAAS;CACT,oBAAoB;CACpB,gBAAgB;CACjB,CAAwB;AAEzB,MAAa,gDACA,eAAe;AAe5B,MAAa,oDACX,OACD;AAED,MAAa,gDACA,yBAAyB"}
|
|
1
|
+
{"version":3,"file":"context.cjs","names":["CopilotKitCore"],"sources":["../src/lib/react-core.ts","../src/context.ts"],"sourcesContent":["import React from \"react\";\nimport type {\n ReactActivityMessageRenderer,\n ReactToolCallRenderer,\n} from \"../types\";\nimport type { ReactCustomMessageRenderer } from \"../types/react-custom-message-renderer\";\nimport { CopilotKitCore } from \"@copilotkit/core\";\nimport type {\n CopilotKitCoreConfig,\n CopilotKitCoreSubscriber,\n 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\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 */\nfunction waitForReactCommit() {\n return new Promise<void>((resolve) => setTimeout(resolve, 0));\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({\n ...config,\n ɵwaitForFrameworkUpdates: waitForReactCommit,\n });\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","\"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 * Whether CopilotKit should automatically mount Intelligence indicators.\n * Defaults to true when omitted for compatibility with custom providers.\n */\n showIntelligenceIndicator?: boolean;\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 onHeadersChanged: () => {\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\n// Provider-level default agent id, published by `<CopilotKitProvider agentId>`.\n//\n// This is a context of its own rather than a `CopilotChatConfigurationProvider`\n// rendered at the root, because that provider also owns a thread: it resolves a\n// threadId (minting a UUID when none is given) and the top-most one owns the\n// imperative active-thread override. Rendering it around the whole application\n// would hand every `<CopilotChat>` the same inherited threadId, so two sibling\n// chats would share one transcript. A bare string context carries the agent\n// default and nothing else.\n//\n// It is the LAST fallback before `DEFAULT_AGENT_ID`, so an explicit `agentId`\n// argument, a `<CopilotChat agentId>`, and a `<CopilotChatConfigurationProvider\n// agentId>` all still win.\nexport const CopilotKitAgentIdContext = createContext<string | undefined>(\n undefined,\n);\n\nexport const useDefaultAgentId = (): string | undefined =>\n useContext(CopilotKitAgentIdContext);\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AA8CA,SAAS,qBAAqB;AAC5B,QAAO,IAAI,SAAe,YAAY,WAAW,SAAS,EAAE,CAAC;;AAG/D,IAAa,sBAAb,cAAyCA,gCAAe;CAUtD,YAAY,QAAmC;AAC7C,QAAM;GACJ,GAAG;GACH,0BAA0B;GAC3B,CAAC;0BAbqD,EAAE;8CAEzD,IAAI,KAAK;sCAET;+BAC4D,EAAE;iCACO,EAAE;2BAClB;AAOrD,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,QAAO,IAAI,GAAG,GAAG,WAAW,GAAG,GAAG,GAAG,QAAQ,GAAG;A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;AAE3D,KAAI,CAAC,QACH,OAAM,IAAI,MAAM,uDAAuD;AAEzE,4BAAgB;EACd,MAAM,eAAe,QAAQ,WAAW,UAAU;GAChD,wCAAwC;AACtC,iBAAa;;GAEf,wBAAwB;AACtB,iBAAa;;GAEhB,CAAC;AACF,eAAa;AACX,gBAAa,aAAa;;IAG3B,EAAE,CAAC;AAEN,QAAO;;AAOT,MAAa,0CAAoD;CAC/D,QAAQ;CACR,SAAS;CACT,oBAAoB;CACpB,gBAAgB;CACjB,CAAwB;AAEzB,MAAa,gDACA,eAAe;AAe5B,MAAa,oDACX,OACD;AAED,MAAa,gDACA,yBAAyB"}
|
package/dist/context.d.cts
CHANGED
|
@@ -1,91 +1,45 @@
|
|
|
1
1
|
import * as react from "react";
|
|
2
|
-
import React
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import { ActivityMessage, Message } from "@ag-ui/core";
|
|
6
|
-
import { AbstractAgent } from "@ag-ui/client";
|
|
2
|
+
import React from "react";
|
|
3
|
+
import { ActivityMessageRenderProps, ActivityMessageRendererDef, CopilotKitCore, CopilotKitCoreConfig, CopilotKitCoreSubscriber, CopilotKitCoreSubscription, CustomMessageRenderProps, CustomMessageRendererDef, ToolCallRenderProps, ToolCallRendererDef } from "@copilotkit/core";
|
|
4
|
+
import { LicenseContextValue } from "@copilotkit/shared";
|
|
7
5
|
|
|
8
6
|
//#region src/types/react-tool-call-renderer.d.ts
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
*/
|
|
20
|
-
agentId?: string;
|
|
21
|
-
/**
|
|
22
|
-
* Whether calls this renderer draws join the chat's tool-call groups (see
|
|
23
|
-
* `toolCallDisplay` on `CopilotChatMessageView`). CopilotKit's built-in card
|
|
24
|
-
* sets it; a renderer that leaves it unset always shows its calls on their
|
|
25
|
-
* own, since the chat can't tell what it draws (it may even draw nothing).
|
|
26
|
-
*/
|
|
27
|
-
group?: boolean;
|
|
28
|
-
render: React.ComponentType<{
|
|
29
|
-
name: string;
|
|
30
|
-
toolCallId: string;
|
|
31
|
-
args: Partial<T>;
|
|
32
|
-
status: ToolCallStatus.InProgress;
|
|
33
|
-
result: undefined;
|
|
34
|
-
} | {
|
|
35
|
-
name: string;
|
|
36
|
-
toolCallId: string;
|
|
37
|
-
args: T;
|
|
38
|
-
status: ToolCallStatus.Executing;
|
|
39
|
-
result: undefined;
|
|
40
|
-
} | {
|
|
41
|
-
name: string;
|
|
42
|
-
toolCallId: string;
|
|
43
|
-
args: T;
|
|
44
|
-
status: ToolCallStatus.Complete;
|
|
45
|
-
result: string;
|
|
46
|
-
}>;
|
|
47
|
-
}
|
|
7
|
+
/**
|
|
8
|
+
* A tool call renderer for React: a React component that draws one tool call
|
|
9
|
+
* in the chat. The component receives {@link ToolCallRenderProps}.
|
|
10
|
+
*
|
|
11
|
+
* Pass a list of these to `renderToolCalls` on `CopilotKitProvider`, or build
|
|
12
|
+
* one with `defineToolCallRenderer`.
|
|
13
|
+
*
|
|
14
|
+
* @typeParam T - The shape of the tool arguments.
|
|
15
|
+
*/
|
|
16
|
+
type ReactToolCallRenderer<T = unknown> = ToolCallRendererDef<T, React.ComponentType<ToolCallRenderProps<T>>>;
|
|
48
17
|
//#endregion
|
|
49
18
|
//#region src/types/react-activity-message-renderer.d.ts
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
agentId?: string;
|
|
59
|
-
/**
|
|
60
|
-
* Schema describing the activity content payload.
|
|
61
|
-
*/
|
|
62
|
-
content: StandardSchemaV1<any, TActivityContent>;
|
|
63
|
-
/**
|
|
64
|
-
* React component invoked to render the activity message.
|
|
65
|
-
*/
|
|
66
|
-
render: React.ComponentType<{
|
|
67
|
-
activityType: string;
|
|
68
|
-
content: TActivityContent;
|
|
69
|
-
message: ActivityMessage;
|
|
70
|
-
agent: AbstractAgent | undefined;
|
|
71
|
-
}>;
|
|
72
|
-
}
|
|
19
|
+
/**
|
|
20
|
+
* An activity message renderer for React: a React component that draws
|
|
21
|
+
* activity messages of one `activityType` (or `"*"` for any type). The
|
|
22
|
+
* component receives {@link ActivityMessageRenderProps}.
|
|
23
|
+
*
|
|
24
|
+
* @typeParam TActivityContent - The shape of the activity content.
|
|
25
|
+
*/
|
|
26
|
+
type ReactActivityMessageRenderer<TActivityContent> = ActivityMessageRendererDef<TActivityContent, React.ComponentType<ActivityMessageRenderProps<TActivityContent>>>;
|
|
73
27
|
//#endregion
|
|
74
28
|
//#region src/types/react-custom-message-renderer.d.ts
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
29
|
+
/**
|
|
30
|
+
* Props a React custom message renderer receives: the same as
|
|
31
|
+
* {@link CustomMessageRenderProps}, with `stateSnapshot` typed `any` so a
|
|
32
|
+
* component can read the agent state without a cast.
|
|
33
|
+
*/
|
|
34
|
+
type ReactCustomMessageRenderProps = Omit<CustomMessageRenderProps, "stateSnapshot"> & {
|
|
35
|
+
stateSnapshot: any;
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* A custom message renderer for React: a component that draws before or
|
|
39
|
+
* after each chat message. Return `null` from the component to draw nothing
|
|
40
|
+
* for a message. A `null` render is skipped.
|
|
41
|
+
*/
|
|
42
|
+
type ReactCustomMessageRenderer = CustomMessageRendererDef<React.ComponentType<ReactCustomMessageRenderProps> | null>;
|
|
89
43
|
//#endregion
|
|
90
44
|
//#region src/lib/react-core.d.ts
|
|
91
45
|
interface CopilotKitCoreReactConfig extends CopilotKitCoreConfig {
|
|
@@ -100,7 +54,7 @@ interface CopilotKitCoreReactSubscriber extends CopilotKitCoreSubscriber {
|
|
|
100
54
|
}) => void | Promise<void>;
|
|
101
55
|
onInterruptElementChanged?: (event: {
|
|
102
56
|
copilotkit: CopilotKitCore;
|
|
103
|
-
interruptElement: React
|
|
57
|
+
interruptElement: React.ReactElement | null;
|
|
104
58
|
}) => void | Promise<void>;
|
|
105
59
|
}
|
|
106
60
|
declare class CopilotKitCoreReact extends CopilotKitCore {
|
|
@@ -120,23 +74,9 @@ declare class CopilotKitCoreReact extends CopilotKitCore {
|
|
|
120
74
|
addHookRenderToolCall(entry: ReactToolCallRenderer<any>): void;
|
|
121
75
|
removeHookRenderToolCall(name: string, agentId?: string): void;
|
|
122
76
|
private _notifyRenderToolCallsChanged;
|
|
123
|
-
get interruptElement(): React
|
|
124
|
-
setInterruptElement(element: React
|
|
77
|
+
get interruptElement(): React.ReactElement | null;
|
|
78
|
+
setInterruptElement(element: React.ReactElement | null): void;
|
|
125
79
|
subscribe(subscriber: CopilotKitCoreReactSubscriber): CopilotKitCoreSubscription;
|
|
126
|
-
/**
|
|
127
|
-
* Wait for pending React state updates before the follow-up agent run.
|
|
128
|
-
*
|
|
129
|
-
* When a frontend tool handler calls setState(), React 18 batches the update
|
|
130
|
-
* and schedules a commit via its internal scheduler (MessageChannel). The
|
|
131
|
-
* useAgentContext hook registers context via useLayoutEffect, which runs
|
|
132
|
-
* synchronously after React commits that batch.
|
|
133
|
-
*
|
|
134
|
-
* Awaiting a zero-delay timeout yields to the macrotask queue. React's
|
|
135
|
-
* MessageChannel task runs first, committing the pending state and running
|
|
136
|
-
* useLayoutEffect (which updates the context store). The follow-up runAgent
|
|
137
|
-
* call then reads fresh context.
|
|
138
|
-
*/
|
|
139
|
-
waitForPendingFrameworkUpdates(): Promise<void>;
|
|
140
80
|
}
|
|
141
81
|
//#endregion
|
|
142
82
|
//#region src/context.d.ts
|
package/dist/context.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"context.d.cts","names":[],"sources":["../src/types/react-tool-call-renderer.ts","../src/types/react-activity-message-renderer.ts","../src/types/react-custom-message-renderer.ts","../src/lib/react-core.ts","../src/context.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"context.d.cts","names":[],"sources":["../src/types/react-tool-call-renderer.ts","../src/types/react-activity-message-renderer.ts","../src/types/react-custom-message-renderer.ts","../src/lib/react-core.ts","../src/context.ts"],"mappings":";;;;;;;;;;AAeA;;;;;KAAY,qBAAA,gBAAqC,mBAAA,CAC/C,CAAA,EACA,KAAA,CAAM,aAAA,CAAc,mBAAA,CAAoB,CAAA;;;;;;;AAF1C;;;KCFY,4BAAA,qBACV,0BAAA,CACE,gBAAA,EACA,KAAA,CAAM,aAAA,CAAc,0BAAA,CAA2B,gBAAA;;;;;ADDnD;;;KEAK,6BAAA,GAAgC,IAAA,CACnC,wBAAA;EAGA,aAAA;AAAA;;;;;;KAQU,0BAAA,GACV,wBAAA,CAAyB,KAAA,CAAM,aAAA,CAAc,6BAAA;;;UCf9B,yBAAA,SAAkC,oBAAA;EAEjD,eAAA,GAAkB,qBAAA;EAClB,sBAAA,GAAyB,4BAAA;EAGzB,oBAAA,GAAuB,0BAAA;AAAA;AAAA,UAGR,6BAAA,SAAsC,wBAAA;EACrD,wBAAA,IAA4B,KAAA;IAC1B,UAAA,EAAY,cAAA;IACZ,eAAA,EAAiB,qBAAA;EAAA,aACN,OAAA;EACb,yBAAA,IAA6B,KAAA;IAC3B,UAAA,EAAY,cAAA;IACZ,gBAAA,EAAkB,KAAA,CAAM,YAAA;EAAA,aACb,OAAA;AAAA;AAAA,cAoBF,mBAAA,SAA4B,cAAA;EAAA,QAC/B,gBAAA;EAAA,QACA,oBAAA;EAAA,QAEA,4BAAA;EAAA,QAEA,qBAAA;EAAA,QACA,uBAAA;EAAA,QACA,iBAAA;cAEI,MAAA,EAAQ,yBAAA;EAAA,IAUhB,oBAAA,CAAA,GAAwB,QAAA,CAAS,0BAAA;EAAA,IAIjC,sBAAA,CAAA,GAA0B,QAAA,CAAS,4BAAA;EAAA,IAInC,eAAA,CAAA,GAAmB,QAAA,CAAS,qBAAA;EAmBhC,yBAAA,CACE,SAAA,EAAW,4BAAA;EAKb,uBAAA,CAAwB,SAAA,EAAW,0BAAA;EAInC,kBAAA,CAAmB,eAAA,EAAiB,qBAAA;EAMpC,qBAAA,CAAsB,KAAA,EAAO,qBAAA;EAO7B,wBAAA,CAAyB,IAAA,UAAc,OAAA;EAAA,QAQ/B,6BAAA;EAAA,IAYJ,gBAAA,CAAA,GAAoB,KAAA,CAAM,YAAA;EAI9B,mBAAA,CAAoB,OAAA,EAAS,KAAA,CAAM,YAAA;EAYnC,SAAA,CACE,UAAA,EAAY,6BAAA,GACX,0BAAA;AAAA;;;UCnJY,sBAAA;EACf,UAAA,EAAY,mBAAA;EJGmB;;;;EIE/B,yBAAA;EJAA;;;;;EIMA,oBAAA,EAAsB,WAAA;AAAA;AAAA,cAGX,SAAA,EAAW,WAAA;AAAA,cAEX,iBAAA,EAAiB,KAAA,CAAA,OAAA,CAAA,sBAAA;AAAA,cAIjB,aAAA,QAAoB,sBAAA;AAAA,cA6BpB,cAAA,EAAc,KAAA,CAAA,OAAA,CAAA,mBAAA;AAAA,cAOd,iBAAA,QAAwB,mBAAA;AAAA,cAgBxB,wBAAA,EAEZ,KAAA,CAFoC,OAAA;AAAA,cAIxB,iBAAA"}
|
package/dist/context.d.mts
CHANGED
|
@@ -1,91 +1,45 @@
|
|
|
1
1
|
import * as react from "react";
|
|
2
|
-
import React
|
|
3
|
-
import { CopilotKitCore, CopilotKitCoreConfig, CopilotKitCoreSubscriber, CopilotKitCoreSubscription,
|
|
4
|
-
import { LicenseContextValue
|
|
5
|
-
import { ActivityMessage, Message } from "@ag-ui/core";
|
|
6
|
-
import { AbstractAgent } from "@ag-ui/client";
|
|
2
|
+
import React from "react";
|
|
3
|
+
import { ActivityMessageRenderProps, ActivityMessageRendererDef, CopilotKitCore, CopilotKitCoreConfig, CopilotKitCoreSubscriber, CopilotKitCoreSubscription, CustomMessageRenderProps, CustomMessageRendererDef, ToolCallRenderProps, ToolCallRendererDef } from "@copilotkit/core";
|
|
4
|
+
import { LicenseContextValue } from "@copilotkit/shared";
|
|
7
5
|
|
|
8
6
|
//#region src/types/react-tool-call-renderer.d.ts
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
*/
|
|
20
|
-
agentId?: string;
|
|
21
|
-
/**
|
|
22
|
-
* Whether calls this renderer draws join the chat's tool-call groups (see
|
|
23
|
-
* `toolCallDisplay` on `CopilotChatMessageView`). CopilotKit's built-in card
|
|
24
|
-
* sets it; a renderer that leaves it unset always shows its calls on their
|
|
25
|
-
* own, since the chat can't tell what it draws (it may even draw nothing).
|
|
26
|
-
*/
|
|
27
|
-
group?: boolean;
|
|
28
|
-
render: React.ComponentType<{
|
|
29
|
-
name: string;
|
|
30
|
-
toolCallId: string;
|
|
31
|
-
args: Partial<T>;
|
|
32
|
-
status: ToolCallStatus.InProgress;
|
|
33
|
-
result: undefined;
|
|
34
|
-
} | {
|
|
35
|
-
name: string;
|
|
36
|
-
toolCallId: string;
|
|
37
|
-
args: T;
|
|
38
|
-
status: ToolCallStatus.Executing;
|
|
39
|
-
result: undefined;
|
|
40
|
-
} | {
|
|
41
|
-
name: string;
|
|
42
|
-
toolCallId: string;
|
|
43
|
-
args: T;
|
|
44
|
-
status: ToolCallStatus.Complete;
|
|
45
|
-
result: string;
|
|
46
|
-
}>;
|
|
47
|
-
}
|
|
7
|
+
/**
|
|
8
|
+
* A tool call renderer for React: a React component that draws one tool call
|
|
9
|
+
* in the chat. The component receives {@link ToolCallRenderProps}.
|
|
10
|
+
*
|
|
11
|
+
* Pass a list of these to `renderToolCalls` on `CopilotKitProvider`, or build
|
|
12
|
+
* one with `defineToolCallRenderer`.
|
|
13
|
+
*
|
|
14
|
+
* @typeParam T - The shape of the tool arguments.
|
|
15
|
+
*/
|
|
16
|
+
type ReactToolCallRenderer<T = unknown> = ToolCallRendererDef<T, React.ComponentType<ToolCallRenderProps<T>>>;
|
|
48
17
|
//#endregion
|
|
49
18
|
//#region src/types/react-activity-message-renderer.d.ts
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
agentId?: string;
|
|
59
|
-
/**
|
|
60
|
-
* Schema describing the activity content payload.
|
|
61
|
-
*/
|
|
62
|
-
content: StandardSchemaV1<any, TActivityContent>;
|
|
63
|
-
/**
|
|
64
|
-
* React component invoked to render the activity message.
|
|
65
|
-
*/
|
|
66
|
-
render: React.ComponentType<{
|
|
67
|
-
activityType: string;
|
|
68
|
-
content: TActivityContent;
|
|
69
|
-
message: ActivityMessage;
|
|
70
|
-
agent: AbstractAgent | undefined;
|
|
71
|
-
}>;
|
|
72
|
-
}
|
|
19
|
+
/**
|
|
20
|
+
* An activity message renderer for React: a React component that draws
|
|
21
|
+
* activity messages of one `activityType` (or `"*"` for any type). The
|
|
22
|
+
* component receives {@link ActivityMessageRenderProps}.
|
|
23
|
+
*
|
|
24
|
+
* @typeParam TActivityContent - The shape of the activity content.
|
|
25
|
+
*/
|
|
26
|
+
type ReactActivityMessageRenderer<TActivityContent> = ActivityMessageRendererDef<TActivityContent, React.ComponentType<ActivityMessageRenderProps<TActivityContent>>>;
|
|
73
27
|
//#endregion
|
|
74
28
|
//#region src/types/react-custom-message-renderer.d.ts
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
29
|
+
/**
|
|
30
|
+
* Props a React custom message renderer receives: the same as
|
|
31
|
+
* {@link CustomMessageRenderProps}, with `stateSnapshot` typed `any` so a
|
|
32
|
+
* component can read the agent state without a cast.
|
|
33
|
+
*/
|
|
34
|
+
type ReactCustomMessageRenderProps = Omit<CustomMessageRenderProps, "stateSnapshot"> & {
|
|
35
|
+
stateSnapshot: any;
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* A custom message renderer for React: a component that draws before or
|
|
39
|
+
* after each chat message. Return `null` from the component to draw nothing
|
|
40
|
+
* for a message. A `null` render is skipped.
|
|
41
|
+
*/
|
|
42
|
+
type ReactCustomMessageRenderer = CustomMessageRendererDef<React.ComponentType<ReactCustomMessageRenderProps> | null>;
|
|
89
43
|
//#endregion
|
|
90
44
|
//#region src/lib/react-core.d.ts
|
|
91
45
|
interface CopilotKitCoreReactConfig extends CopilotKitCoreConfig {
|
|
@@ -100,7 +54,7 @@ interface CopilotKitCoreReactSubscriber extends CopilotKitCoreSubscriber {
|
|
|
100
54
|
}) => void | Promise<void>;
|
|
101
55
|
onInterruptElementChanged?: (event: {
|
|
102
56
|
copilotkit: CopilotKitCore;
|
|
103
|
-
interruptElement: React
|
|
57
|
+
interruptElement: React.ReactElement | null;
|
|
104
58
|
}) => void | Promise<void>;
|
|
105
59
|
}
|
|
106
60
|
declare class CopilotKitCoreReact extends CopilotKitCore {
|
|
@@ -120,23 +74,9 @@ declare class CopilotKitCoreReact extends CopilotKitCore {
|
|
|
120
74
|
addHookRenderToolCall(entry: ReactToolCallRenderer<any>): void;
|
|
121
75
|
removeHookRenderToolCall(name: string, agentId?: string): void;
|
|
122
76
|
private _notifyRenderToolCallsChanged;
|
|
123
|
-
get interruptElement(): React
|
|
124
|
-
setInterruptElement(element: React
|
|
77
|
+
get interruptElement(): React.ReactElement | null;
|
|
78
|
+
setInterruptElement(element: React.ReactElement | null): void;
|
|
125
79
|
subscribe(subscriber: CopilotKitCoreReactSubscriber): CopilotKitCoreSubscription;
|
|
126
|
-
/**
|
|
127
|
-
* Wait for pending React state updates before the follow-up agent run.
|
|
128
|
-
*
|
|
129
|
-
* When a frontend tool handler calls setState(), React 18 batches the update
|
|
130
|
-
* and schedules a commit via its internal scheduler (MessageChannel). The
|
|
131
|
-
* useAgentContext hook registers context via useLayoutEffect, which runs
|
|
132
|
-
* synchronously after React commits that batch.
|
|
133
|
-
*
|
|
134
|
-
* Awaiting a zero-delay timeout yields to the macrotask queue. React's
|
|
135
|
-
* MessageChannel task runs first, committing the pending state and running
|
|
136
|
-
* useLayoutEffect (which updates the context store). The follow-up runAgent
|
|
137
|
-
* call then reads fresh context.
|
|
138
|
-
*/
|
|
139
|
-
waitForPendingFrameworkUpdates(): Promise<void>;
|
|
140
80
|
}
|
|
141
81
|
//#endregion
|
|
142
82
|
//#region src/context.d.ts
|
package/dist/context.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"context.d.mts","names":[],"sources":["../src/types/react-tool-call-renderer.ts","../src/types/react-activity-message-renderer.ts","../src/types/react-custom-message-renderer.ts","../src/lib/react-core.ts","../src/context.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"context.d.mts","names":[],"sources":["../src/types/react-tool-call-renderer.ts","../src/types/react-activity-message-renderer.ts","../src/types/react-custom-message-renderer.ts","../src/lib/react-core.ts","../src/context.ts"],"mappings":";;;;;;;;;;AAeA;;;;;KAAY,qBAAA,gBAAqC,mBAAA,CAC/C,CAAA,EACA,KAAA,CAAM,aAAA,CAAc,mBAAA,CAAoB,CAAA;;;;;;;AAF1C;;;KCFY,4BAAA,qBACV,0BAAA,CACE,gBAAA,EACA,KAAA,CAAM,aAAA,CAAc,0BAAA,CAA2B,gBAAA;;;;;ADDnD;;;KEAK,6BAAA,GAAgC,IAAA,CACnC,wBAAA;EAGA,aAAA;AAAA;;;;;;KAQU,0BAAA,GACV,wBAAA,CAAyB,KAAA,CAAM,aAAA,CAAc,6BAAA;;;UCf9B,yBAAA,SAAkC,oBAAA;EAEjD,eAAA,GAAkB,qBAAA;EAClB,sBAAA,GAAyB,4BAAA;EAGzB,oBAAA,GAA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CAAA,OAAA,CAAA,mBAAA;AAAA,cAOd,iBAAA,QAAwB,mBAAA;AAAA,cAgBxB,wBAAA,EAEZ,KAAA,CAFoC,OAAA;AAAA,cAIxB,iBAAA"}
|
package/dist/context.mjs
CHANGED
|
@@ -4,9 +4,28 @@ import { createContext, useContext, useEffect, useReducer } from "react";
|
|
|
4
4
|
import { CopilotKitCore } from "@copilotkit/core";
|
|
5
5
|
|
|
6
6
|
//#region src/lib/react-core.ts
|
|
7
|
+
/**
|
|
8
|
+
* Wait for pending React state updates before the follow-up agent run.
|
|
9
|
+
*
|
|
10
|
+
* When a frontend tool handler calls setState(), React 18 batches the update
|
|
11
|
+
* and schedules a commit via its internal scheduler (MessageChannel). The
|
|
12
|
+
* useAgentContext hook registers context via useLayoutEffect, which runs
|
|
13
|
+
* synchronously after React commits that batch.
|
|
14
|
+
*
|
|
15
|
+
* Awaiting a zero-delay timeout yields to the macrotask queue. React's
|
|
16
|
+
* MessageChannel task runs first, committing the pending state and running
|
|
17
|
+
* useLayoutEffect (which updates the context store). The follow-up runAgent
|
|
18
|
+
* call then reads fresh context.
|
|
19
|
+
*/
|
|
20
|
+
function waitForReactCommit() {
|
|
21
|
+
return new Promise((resolve) => setTimeout(resolve, 0));
|
|
22
|
+
}
|
|
7
23
|
var CopilotKitCoreReact = class extends CopilotKitCore {
|
|
8
24
|
constructor(config) {
|
|
9
|
-
super(
|
|
25
|
+
super({
|
|
26
|
+
...config,
|
|
27
|
+
ɵwaitForFrameworkUpdates: waitForReactCommit
|
|
28
|
+
});
|
|
10
29
|
this._renderToolCalls = [];
|
|
11
30
|
this._hookRenderToolCalls = /* @__PURE__ */ new Map();
|
|
12
31
|
this._cachedMergedRenderToolCalls = null;
|
|
@@ -80,22 +99,6 @@ var CopilotKitCoreReact = class extends CopilotKitCore {
|
|
|
80
99
|
subscribe(subscriber) {
|
|
81
100
|
return super.subscribe(subscriber);
|
|
82
101
|
}
|
|
83
|
-
/**
|
|
84
|
-
* Wait for pending React state updates before the follow-up agent run.
|
|
85
|
-
*
|
|
86
|
-
* When a frontend tool handler calls setState(), React 18 batches the update
|
|
87
|
-
* and schedules a commit via its internal scheduler (MessageChannel). The
|
|
88
|
-
* useAgentContext hook registers context via useLayoutEffect, which runs
|
|
89
|
-
* synchronously after React commits that batch.
|
|
90
|
-
*
|
|
91
|
-
* Awaiting a zero-delay timeout yields to the macrotask queue. React's
|
|
92
|
-
* MessageChannel task runs first, committing the pending state and running
|
|
93
|
-
* useLayoutEffect (which updates the context store). The follow-up runAgent
|
|
94
|
-
* call then reads fresh context.
|
|
95
|
-
*/
|
|
96
|
-
async waitForPendingFrameworkUpdates() {
|
|
97
|
-
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
98
|
-
}
|
|
99
102
|
};
|
|
100
103
|
|
|
101
104
|
//#endregion
|
package/dist/context.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"context.mjs","names":[],"sources":["../src/lib/react-core.ts","../src/context.ts"],"sourcesContent":["import React from \"react\";\nimport type {\n ReactActivityMessageRenderer,\n ReactToolCallRenderer,\n} from \"../types\";\nimport type { ReactCustomMessageRenderer } from \"../types/react-custom-message-renderer\";\nimport { CopilotKitCore } from \"@copilotkit/core\";\nimport type {\n CopilotKitCoreConfig,\n CopilotKitCoreSubscriber,\n 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 * Whether CopilotKit should automatically mount Intelligence indicators.\n * Defaults to true when omitted for compatibility with custom providers.\n */\n showIntelligenceIndicator?: boolean;\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 onHeadersChanged: () => {\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\n// Provider-level default agent id, published by `<CopilotKitProvider agentId>`.\n//\n// This is a context of its own rather than a `CopilotChatConfigurationProvider`\n// rendered at the root, because that provider also owns a thread: it resolves a\n// threadId (minting a UUID when none is given) and the top-most one owns the\n// imperative active-thread override. Rendering it around the whole application\n// would hand every `<CopilotChat>` the same inherited threadId, so two sibling\n// chats would share one transcript. A bare string context carries the agent\n// default and nothing else.\n//\n// It is the LAST fallback before `DEFAULT_AGENT_ID`, so an explicit `agentId`\n// argument, a `<CopilotChat agentId>`, and a `<CopilotChatConfigurationProvider\n// agentId>` all still win.\nexport const CopilotKitAgentIdContext = createContext<string | undefined>(\n undefined,\n);\n\nexport const useDefaultAgentId = (): string | undefined =>\n useContext(CopilotKitAgentIdContext);\n"],"mappings":";;;;;;AAiCA,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,QAAO,IAAI,GAAG,GAAG,WAAW,GAAG,GAAG,GAAG,QAAQ,GAAG;AAElD,OAAK,MAAM,CAAC,KAAK,OAAO,KAAK,qBAC3B,QAAO,IAAI,KAAK,GAAG;AAErB,OAAK,+BAA+B,MAAM,KAAK,OAAO,QAAQ,CAAC;AAC/D,SAAO,KAAK;;CAGd,0BACE,WACM;AACN,OAAK,0BAA0B;;CAGjC,wBA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,QAAQ,WAAW,UAAU;GAChD,wCAAwC;AACtC,iBAAa;;GAEf,wBAAwB;AACtB,iBAAa;;GAEhB,CAAC;AACF,eAAa;AACX,gBAAa,aAAa;;IAG3B,EAAE,CAAC;AAEN,QAAO;;AAOT,MAAa,iBAAiB,cAAmC;CAC/D,QAAQ;CACR,SAAS;CACT,oBAAoB;CACpB,gBAAgB;CACjB,CAAwB;AAEzB,MAAa,0BACX,WAAW,eAAe;AAe5B,MAAa,2BAA2B,cACtC,OACD;AAED,MAAa,0BACX,WAAW,yBAAyB"}
|
|
1
|
+
{"version":3,"file":"context.mjs","names":[],"sources":["../src/lib/react-core.ts","../src/context.ts"],"sourcesContent":["import React from \"react\";\nimport type {\n ReactActivityMessageRenderer,\n ReactToolCallRenderer,\n} from \"../types\";\nimport type { ReactCustomMessageRenderer } from \"../types/react-custom-message-renderer\";\nimport { CopilotKitCore } from \"@copilotkit/core\";\nimport type {\n CopilotKitCoreConfig,\n CopilotKitCoreSubscriber,\n 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\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 */\nfunction waitForReactCommit() {\n return new Promise<void>((resolve) => setTimeout(resolve, 0));\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({\n ...config,\n ɵwaitForFrameworkUpdates: waitForReactCommit,\n });\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","\"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 * Whether CopilotKit should automatically mount Intelligence indicators.\n * Defaults to true when omitted for compatibility with custom providers.\n */\n showIntelligenceIndicator?: boolean;\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 onHeadersChanged: () => {\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\n// Provider-level default agent id, published by `<CopilotKitProvider agentId>`.\n//\n// This is a context of its own rather than a `CopilotChatConfigurationProvider`\n// rendered at the root, because that provider also owns a thread: it resolves a\n// threadId (minting a UUID when none is given) and the top-most one owns the\n// imperative active-thread override. Rendering it around the whole application\n// would hand every `<CopilotChat>` the same inherited threadId, so two sibling\n// chats would share one transcript. A bare string context carries the agent\n// default and nothing else.\n//\n// It is the LAST fallback before `DEFAULT_AGENT_ID`, so an explicit `agentId`\n// argument, a `<CopilotChat agentId>`, and a `<CopilotChatConfigurationProvider\n// agentId>` all still win.\nexport const CopilotKitAgentIdContext = createContext<string | undefined>(\n undefined,\n);\n\nexport const useDefaultAgentId = (): string | undefined =>\n useContext(CopilotKitAgentIdContext);\n"],"mappings":";;;;;;;;;;;;;;;;;;;AA8CA,SAAS,qBAAqB;AAC5B,QAAO,IAAI,SAAe,YAAY,WAAW,SAAS,EAAE,CAAC;;AAG/D,IAAa,sBAAb,cAAyC,eAAe;CAUtD,YAAY,QAAmC;AAC7C,QAAM;GACJ,GAAG;GACH,0BAA0B;GAC3B,CAAC;0BAbqD,EAAE;8CAEzD,IAAI,KAAK;sCAET;+BAC4D,EAAE;iCACO,EAAE;2BAClB;AAOrD,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,QAAO,IAAI,GAAG,GAAG,WAAW,GAAG,GAAG,GAAG,QAAQ,GAAG;AAElD,OAAK,MAAM,CAAC,KAAK,OAAO,KAAK,qBAC3B,QAAO,IAAI,KAAK,GAAG;AAErB,OAAK,+BAA+B,MAAM,KAAK,OAAO,QAAQ,CAAC;AAC/D,SAAO,KAAK;;CAGd,0BACE,WACM;AACN,OAAK,0BAA0B;;CAGjC,wBAAwB,WAA+C;AACrE,OAAK,wBAAwB;;CAG/B,mBAAmB,iB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