@copilotkit/mcp-apps-renderer 2.0.0-rc.18 → 2.0.0-rc.19
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.
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"request-queue.mjs","names":[],"sources":["../src/request-queue.ts"],"sourcesContent":["import type { AbstractAgent, RunAgentResult } from \"@ag-ui/client\";\n\n/**\n * How long a queued request waits for a busy agent before it is rejected.\n * A stuck run would otherwise hold the whole queue (and the widget's loading\n * state) forever. The Vue host enforced this before the shared extraction; it\n * now applies to every frontend.\n */\nexport const MCP_APPS_QUEUE_IDLE_TIMEOUT_MS = 30_000;\n\n/**\n * Raised when queued work is dropped because the agent moved to another thread\n * between enqueue and execution. Distinguishable so callers can answer the\n * requester explicitly instead of leaving it waiting.\n */\nexport class MCPAppsQueueThreadChangedError extends Error {\n constructor(\n readonly originThreadId: string,\n readonly currentThreadId: string,\n ) {\n super(\n `[CopilotKit] MCP app request dropped: the agent moved from thread \"${originThreadId}\" to \"${currentThreadId}\" while it was queued.`,\n );\n this.name = \"MCPAppsQueueThreadChangedError\";\n }\n}\n\n/** Options accepted by {@link MCPAppsRequestQueue.enqueue}. */\nexport interface MCPAppsEnqueueOptions {\n /**\n * Identifies the caller that owns this request, so `cancelOwner` can drop the\n * work of a single widget without touching requests queued by other widgets\n * (the pre-extraction `cancel(threadId)` was too broad: it rejected everything\n * sharing a thread).\n */\n owner?: object;\n /** Override the busy-agent wait timeout. Defaults to 30s. */\n timeoutMs?: number;\n /** Also release an executing request's wait on owner cancellation (does not stop its run). */\n cancelRunningWait?: boolean;\n /**\n * Drop the request instead of running it when the agent has switched threads\n * since it was enqueued.\n *\n * The queue is keyed by agent AND thread, but that only decides which FIFO the\n * work joins: a single shared agent object can have its `threadId` mutated in\n * place while the work waits, so executing later would run a thread-1 request\n * against thread-2 - and stream into it (issue #5819's failure mode, applied\n * to the proxy paths).\n */\n dropAfterThreadSwitch?: boolean;\n}\n\ninterface QueueItem {\n execute: (signal: AbortSignal) => Promise<RunAgentResult>;\n cancelRunningWait?: boolean;\n resolve: (result: RunAgentResult) => void;\n reject: (error: Error) => void;\n owner?: object;\n timeoutMs: number;\n /** Aborted by `cancelOwner`, so an active idle wait stops immediately. */\n controller: AbortController;\n /** Thread the agent was on when this work was enqueued. */\n originThreadId: string;\n dropAfterThreadSwitch?: boolean;\n /**\n * Set once `execute()` has actually been invoked. The run itself can no longer\n * be cancelled (runAgent exposes no abort), but the caller's WAIT still can:\n * with `cancelRunningWait`, `cancelOwner` aborts the signal and settles the\n * requester instead of leaving it pending.\n */\n executing?: boolean;\n /** Set by `cancelOwner` for work that must not run. */\n cancelled?: boolean;\n}\n\n/** Stable per-agent id, so two agents sharing a threadId keep separate queues. */\nconst agentKeys = new WeakMap<AbstractAgent, string>();\nlet nextAgentKey = 0;\nfunction keyFor(agent: AbstractAgent): string {\n let key = agentKeys.get(agent);\n if (!key) {\n key = `agent-${nextAgentKey++}`;\n agentKeys.set(agent, key);\n }\n return `${key}::${agent.threadId || \"default\"}`;\n}\n\n/**\n * Queue for serializing MCP app requests to an agent.\n * Ensures requests wait for the agent to stop running and are processed one at a time.\n */\nexport class MCPAppsRequestQueue {\n private queues = new Map<string, QueueItem[]>();\n private processing = new Map<string, boolean>();\n\n /**\n * Add a request to the queue for a specific agent thread.\n * Returns a promise that resolves when the request completes.\n */\n async enqueue(\n agent: AbstractAgent,\n request: (signal: AbortSignal) => Promise<RunAgentResult>,\n options?: MCPAppsEnqueueOptions,\n ): Promise<RunAgentResult> {\n // Keyed by agent AND thread: a shared thread id must not serialize (or\n // cancel) work belonging to a different agent.\n const key = keyFor(agent);\n\n return new Promise((resolve, reject) => {\n let queue = this.queues.get(key);\n if (!queue) {\n queue = [];\n this.queues.set(key, queue);\n }\n\n queue.push({\n execute: request,\n cancelRunningWait: options?.cancelRunningWait,\n resolve,\n reject,\n owner: options?.owner,\n timeoutMs: options?.timeoutMs ?? MCP_APPS_QUEUE_IDLE_TIMEOUT_MS,\n controller: new AbortController(),\n originThreadId: agent.threadId || \"default\",\n dropAfterThreadSwitch: options?.dropAfterThreadSwitch,\n });\n\n // Start processing if not already running\n this.processQueue(key, agent);\n });\n }\n\n /**\n * Drop every request queued by `owner` that has not started executing yet,\n * rejecting its promise. Used by a widget teardown so work that only exists to\n * feed a now-unmounted iframe does not run against the agent later.\n *\n * By default this cancels waiting work only. `cancelRunningWait` also\n * releases an executing caller's wait and signals its callback; it does not\n * itself stop a runAgent call. The next item still waits for agent idle.\n */\n cancelOwner(owner: object): void {\n for (const queue of this.queues.values()) {\n for (const item of queue) {\n if (\n item.owner !== owner ||\n (item.executing && !item.cancelRunningWait) ||\n item.cancelled\n )\n continue;\n item.cancelled = true;\n // Abort first: an item already waiting on a busy agent must release its\n // timer and agent subscription now, not when the agent finally frees up.\n item.controller.abort();\n item.reject(\n new Error(\"[CopilotKit] MCP app request cancelled on teardown\"),\n );\n }\n }\n }\n\n /**\n * Drain a queue one request at a time, waiting for the agent to go idle before\n * each. Re-entrant-safe (a single processor per key) and drops the entries once\n * fully drained to keep the shared queue bounded.\n */\n private async processQueue(key: string, agent: AbstractAgent): Promise<void> {\n // If already processing this queue, return\n if (this.processing.get(key)) {\n return;\n }\n\n this.processing.set(key, true);\n\n try {\n const queue = this.queues.get(key);\n if (!queue) return;\n\n while (queue.length > 0) {\n const item = queue[0]!;\n\n // Cancelled before it got its turn: its promise is already rejected.\n if (item.cancelled) {\n queue.shift();\n continue;\n }\n\n try {\n // Wait for any active run to complete before processing\n await this.waitForAgentIdle(\n agent,\n item.timeoutMs,\n item.controller.signal,\n );\n\n // The owner may have torn down while we waited for the agent.\n if (item.cancelled) {\n queue.shift();\n continue;\n }\n\n // Thread guard: the agent object is shared and its `threadId` can be\n // mutated in place while work waits, so re-check right before running.\n // The queue key only decided which FIFO this joined, it does not pin\n // the agent's current thread.\n const currentThreadId = agent.threadId || \"default\";\n if (\n item.dropAfterThreadSwitch &&\n currentThreadId !== item.originThreadId\n ) {\n item.reject(\n new MCPAppsQueueThreadChangedError(\n item.originThreadId,\n currentThreadId,\n ),\n );\n queue.shift();\n continue;\n }\n\n // Execution itself is not aborted; callers may opt into releasing\n // their wait on teardown (the legacy Angular contract).\n item.executing = true;\n const execution = item.execute(item.controller.signal);\n const result = item.cancelRunningWait\n ? await waitWithAbort(execution, item.controller.signal)\n : await execution;\n item.resolve(result);\n } catch (error) {\n item.reject(\n error instanceof Error ? error : new Error(String(error)),\n );\n }\n\n // Remove processed item\n queue.shift();\n }\n } finally {\n // Drop the drained entries from both maps. `mcpAppsRequestQueue` is shared\n // for the page lifetime, so retaining an entry per agent/thread would grow\n // unbounded as threads come and go.\n const queue = this.queues.get(key);\n if (!queue || queue.length === 0) {\n this.queues.delete(key);\n this.processing.delete(key);\n } else {\n this.processing.set(key, false);\n }\n }\n }\n\n /**\n * Resolve once the agent is not running. Subscribes to run-finalized/failed and\n * also polls as a fallback for reconnect scenarios where events do not fire.\n * Rejects after `timeoutMs` so a stuck run cannot block the queue forever.\n */\n private waitForAgentIdle(\n agent: AbstractAgent,\n timeoutMs: number,\n signal?: AbortSignal,\n ): Promise<void> {\n return new Promise((resolve, reject) => {\n if (!agent.isRunning) {\n resolve();\n return;\n }\n if (signal?.aborted) {\n reject(new Error(\"[CopilotKit] MCP app request cancelled on teardown\"));\n return;\n }\n\n let done = false;\n const settle = (fn: () => void) => {\n if (done) return;\n done = true;\n clearTimeout(timer);\n clearInterval(checkInterval);\n sub.unsubscribe();\n signal?.removeEventListener(\"abort\", onAbort);\n fn();\n };\n const onAbort = () =>\n settle(() =>\n reject(\n new Error(\"[CopilotKit] MCP app request cancelled on teardown\"),\n ),\n );\n signal?.addEventListener(\"abort\", onAbort);\n const finish = () => settle(resolve);\n\n const sub = agent.subscribe({\n onRunFinalized: finish,\n onRunFailed: finish,\n });\n\n // Fallback for reconnect scenarios where events don't fire\n const checkInterval = setInterval(() => {\n if (!agent.isRunning) finish();\n }, 500);\n\n const timer = setTimeout(() => {\n settle(() =>\n reject(\n new Error(\n \"[CopilotKit] Timed out waiting for agent to become idle\",\n ),\n ),\n );\n }, timeoutMs);\n });\n }\n}\n\n// Shared per-thread queue instance for all MCP app requests.\nexport const mcpAppsRequestQueue = new MCPAppsRequestQueue();\n\n/** Release only the caller's wait; the underlying run may still be active. */\nfunction waitWithAbort<T>(\n promise: Promise<T>,\n signal: AbortSignal,\n): Promise<T> {\n return new Promise((resolve, reject) => {\n const cancel = () =>\n reject(new Error(\"[CopilotKit] MCP app request cancelled on teardown\"));\n signal.addEventListener(\"abort\", cancel, { once: true });\n promise\n .then(resolve, reject)\n .finally(() => signal.removeEventListener(\"abort\", cancel));\n if (signal.aborted) cancel();\n });\n}\n"],"mappings":";;;;;;;AAQA,MAAa,iCAAiC;;;;;;AAO9C,IAAa,iCAAb,cAAoD,MAAM;CACxD,YACE,AAAS,gBACT,AAAS,iBACT;AACA,QACE,sEAAsE,eAAe,QAAQ,gBAAgB,wBAC9G;EALQ;EACA;AAKT,OAAK,OAAO;;;;AAsDhB,MAAM,4BAAY,IAAI,SAAgC;AACtD,IAAI,eAAe;AACnB,SAAS,OAAO,OAA8B;CAC5C,IAAI,MAAM,UAAU,IAAI,MAAM;AAC9B,KAAI,CAAC,KAAK;AACR,QAAM,SAAS;AACf,YAAU,IAAI,OAAO,IAAI;;AAE3B,QAAO,GAAG,IAAI,IAAI,MAAM,YAAY;;;;;;AAOtC,IAAa,sBAAb,MAAiC;;gCACd,IAAI,KAA0B;oCAC1B,IAAI,KAAsB;;;;;;CAM/C,MAAM,QACJ,OACA,SACA,SACyB;EAGzB,MAAM,MAAM,OAAO,MAAM;AAEzB,SAAO,IAAI,SAAS,SAAS,WAAW;GACtC,IAAI,QAAQ,KAAK,OAAO,IAAI,IAAI;AAChC,OAAI,CAAC,OAAO;AACV,YAAQ,EAAE;AACV,SAAK,OAAO,IAAI,KAAK,MAAM;;AAG7B,SAAM,KAAK;IACT,SAAS;IACT,mBA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AAS,OAAO,CACrB,cAAc,OAAO,oBAAoB,SAAS,OAAO,CAAC;AAC7D,MAAI,OAAO,QAAS,SAAQ;GAC5B"}
|
|
1
|
+
{"version":3,"file":"request-queue.mjs","names":[],"sources":["../src/request-queue.ts"],"sourcesContent":["import type { AbstractAgent, RunAgentResult } from \"@ag-ui/client\";\n\n/**\n * How long a queued request waits for a busy agent before it is rejected.\n * A stuck run would otherwise hold the whole queue (and the widget's loading\n * state) forever. The Vue host enforced this before the shared extraction; it\n * now applies to every frontend.\n */\nexport const MCP_APPS_QUEUE_IDLE_TIMEOUT_MS = 30_000;\n\n/**\n * Raised when queued work is dropped because the agent moved to another thread\n * between enqueue and execution. Distinguishable so callers can answer the\n * requester explicitly instead of leaving it waiting.\n */\nexport class MCPAppsQueueThreadChangedError extends Error {\n constructor(\n readonly originThreadId: string,\n readonly currentThreadId: string,\n ) {\n super(\n `[CopilotKit] MCP app request dropped: the agent moved from thread \"${originThreadId}\" to \"${currentThreadId}\" while it was queued.`,\n );\n this.name = \"MCPAppsQueueThreadChangedError\";\n }\n}\n\n/** Options accepted by {@link MCPAppsRequestQueue.enqueue}. */\nexport interface MCPAppsEnqueueOptions {\n /**\n * Identifies the caller that owns this request, so `cancelOwner` can drop the\n * work of a single widget without touching requests queued by other widgets\n * (the pre-extraction `cancel(threadId)` was too broad: it rejected everything\n * sharing a thread).\n */\n owner?: object;\n /** Override the busy-agent wait timeout. Defaults to 30s. */\n timeoutMs?: number;\n /** Also release an executing request's wait on owner cancellation (does not stop its run). */\n cancelRunningWait?: boolean;\n /**\n * Drop the request instead of running it when the agent has switched threads\n * since it was enqueued.\n *\n * The queue is keyed by agent AND thread, but that only decides which FIFO the\n * work joins: a single shared agent object can have its `threadId` mutated in\n * place while the work waits, so executing later would run a thread-1 request\n * against thread-2 - and stream into it (issue #5819's failure mode, applied\n * to the proxy paths).\n */\n dropAfterThreadSwitch?: boolean;\n}\n\ninterface QueueItem {\n execute: (signal: AbortSignal) => Promise<RunAgentResult>;\n cancelRunningWait?: boolean;\n resolve: (result: RunAgentResult) => void;\n reject: (error: Error) => void;\n owner?: object;\n timeoutMs: number;\n /** Aborted by `cancelOwner`, so an active idle wait stops immediately. */\n controller: AbortController;\n /** Thread the agent was on when this work was enqueued. */\n originThreadId: string;\n dropAfterThreadSwitch?: boolean;\n /**\n * Set once `execute()` has actually been invoked. The run itself can no longer\n * be cancelled (runAgent exposes no abort), but the caller's WAIT still can:\n * with `cancelRunningWait`, `cancelOwner` aborts the signal and settles the\n * requester instead of leaving it pending.\n */\n executing?: boolean;\n /** Set by `cancelOwner` for work that must not run. */\n cancelled?: boolean;\n}\n\n/** Stable per-agent id, so two agents sharing a threadId keep separate queues. */\nconst agentKeys = new WeakMap<AbstractAgent, string>();\nlet nextAgentKey = 0;\nfunction keyFor(agent: AbstractAgent): string {\n let key = agentKeys.get(agent);\n if (!key) {\n key = `agent-${nextAgentKey++}`;\n agentKeys.set(agent, key);\n }\n return `${key}::${agent.threadId || \"default\"}`;\n}\n\n/**\n * Queue for serializing MCP app requests to an agent.\n * Ensures requests wait for the agent to stop running and are processed one at a time.\n */\nexport class MCPAppsRequestQueue {\n private queues = new Map<string, QueueItem[]>();\n private processing = new Map<string, boolean>();\n\n /**\n * Add a request to the queue for a specific agent thread.\n * Returns a promise that resolves when the request completes.\n */\n async enqueue(\n agent: AbstractAgent,\n request: (signal: AbortSignal) => Promise<RunAgentResult>,\n options?: MCPAppsEnqueueOptions,\n ): Promise<RunAgentResult> {\n // Keyed by agent AND thread: a shared thread id must not serialize (or\n // cancel) work belonging to a different agent.\n const key = keyFor(agent);\n\n return new Promise((resolve, reject) => {\n let queue = this.queues.get(key);\n if (!queue) {\n queue = [];\n this.queues.set(key, queue);\n }\n\n queue.push({\n execute: request,\n cancelRunningWait: options?.cancelRunningWait,\n resolve,\n reject,\n owner: options?.owner,\n timeoutMs: options?.timeoutMs ?? MCP_APPS_QUEUE_IDLE_TIMEOUT_MS,\n controller: new AbortController(),\n originThreadId: agent.threadId || \"default\",\n dropAfterThreadSwitch: options?.dropAfterThreadSwitch,\n });\n\n // Start processing if not already running\n void this.processQueue(key, agent);\n });\n }\n\n /**\n * Drop every request queued by `owner` that has not started executing yet,\n * rejecting its promise. Used by a widget teardown so work that only exists to\n * feed a now-unmounted iframe does not run against the agent later.\n *\n * By default this cancels waiting work only. `cancelRunningWait` also\n * releases an executing caller's wait and signals its callback; it does not\n * itself stop a runAgent call. The next item still waits for agent idle.\n */\n cancelOwner(owner: object): void {\n for (const queue of this.queues.values()) {\n for (const item of queue) {\n if (\n item.owner !== owner ||\n (item.executing && !item.cancelRunningWait) ||\n item.cancelled\n )\n continue;\n item.cancelled = true;\n // Abort first: an item already waiting on a busy agent must release its\n // timer and agent subscription now, not when the agent finally frees up.\n item.controller.abort();\n item.reject(\n new Error(\"[CopilotKit] MCP app request cancelled on teardown\"),\n );\n }\n }\n }\n\n /**\n * Drain a queue one request at a time, waiting for the agent to go idle before\n * each. Re-entrant-safe (a single processor per key) and drops the entries once\n * fully drained to keep the shared queue bounded.\n */\n private async processQueue(key: string, agent: AbstractAgent): Promise<void> {\n // If already processing this queue, return\n if (this.processing.get(key)) {\n return;\n }\n\n this.processing.set(key, true);\n\n try {\n const queue = this.queues.get(key);\n if (!queue) return;\n\n while (queue.length > 0) {\n const item = queue[0]!;\n\n // Cancelled before it got its turn: its promise is already rejected.\n if (item.cancelled) {\n queue.shift();\n continue;\n }\n\n try {\n // Wait for any active run to complete before processing\n await this.waitForAgentIdle(\n agent,\n item.timeoutMs,\n item.controller.signal,\n );\n\n // The owner may have torn down while we waited for the agent.\n if (item.cancelled) {\n queue.shift();\n continue;\n }\n\n // Thread guard: the agent object is shared and its `threadId` can be\n // mutated in place while work waits, so re-check right before running.\n // The queue key only decided which FIFO this joined, it does not pin\n // the agent's current thread.\n const currentThreadId = agent.threadId || \"default\";\n if (\n item.dropAfterThreadSwitch &&\n currentThreadId !== item.originThreadId\n ) {\n item.reject(\n new MCPAppsQueueThreadChangedError(\n item.originThreadId,\n currentThreadId,\n ),\n );\n queue.shift();\n continue;\n }\n\n // Execution itself is not aborted; callers may opt into releasing\n // their wait on teardown (the legacy Angular contract).\n item.executing = true;\n const execution = item.execute(item.controller.signal);\n const result = item.cancelRunningWait\n ? await waitWithAbort(execution, item.controller.signal)\n : await execution;\n item.resolve(result);\n } catch (error) {\n item.reject(\n error instanceof Error ? error : new Error(String(error)),\n );\n }\n\n // Remove processed item\n queue.shift();\n }\n } finally {\n // Drop the drained entries from both maps. `mcpAppsRequestQueue` is shared\n // for the page lifetime, so retaining an entry per agent/thread would grow\n // unbounded as threads come and go.\n const queue = this.queues.get(key);\n if (!queue || queue.length === 0) {\n this.queues.delete(key);\n this.processing.delete(key);\n } else {\n this.processing.set(key, false);\n }\n }\n }\n\n /**\n * Resolve once the agent is not running. Subscribes to run-finalized/failed and\n * also polls as a fallback for reconnect scenarios where events do not fire.\n * Rejects after `timeoutMs` so a stuck run cannot block the queue forever.\n */\n private waitForAgentIdle(\n agent: AbstractAgent,\n timeoutMs: number,\n signal?: AbortSignal,\n ): Promise<void> {\n return new Promise((resolve, reject) => {\n if (!agent.isRunning) {\n resolve();\n return;\n }\n if (signal?.aborted) {\n reject(new Error(\"[CopilotKit] MCP app request cancelled on teardown\"));\n return;\n }\n\n let done = false;\n const settle = (fn: () => void) => {\n if (done) return;\n done = true;\n clearTimeout(timer);\n clearInterval(checkInterval);\n sub.unsubscribe();\n signal?.removeEventListener(\"abort\", onAbort);\n fn();\n };\n const onAbort = () =>\n settle(() =>\n reject(\n new Error(\"[CopilotKit] MCP app request cancelled on teardown\"),\n ),\n );\n signal?.addEventListener(\"abort\", onAbort);\n const finish = () => settle(resolve);\n\n const sub = agent.subscribe({\n onRunFinalized: finish,\n onRunFailed: finish,\n });\n\n // Fallback for reconnect scenarios where events don't fire\n const checkInterval = setInterval(() => {\n if (!agent.isRunning) finish();\n }, 500);\n\n const timer = setTimeout(() => {\n settle(() =>\n reject(\n new Error(\n \"[CopilotKit] Timed out waiting for agent to become idle\",\n ),\n ),\n );\n }, timeoutMs);\n });\n }\n}\n\n// Shared per-thread queue instance for all MCP app requests.\nexport const mcpAppsRequestQueue = new MCPAppsRequestQueue();\n\n/** Release only the caller's wait; the underlying run may still be active. */\nfunction waitWithAbort<T>(\n promise: Promise<T>,\n signal: AbortSignal,\n): Promise<T> {\n return new Promise((resolve, reject) => {\n const cancel = () =>\n reject(new Error(\"[CopilotKit] MCP app request cancelled on teardown\"));\n signal.addEventListener(\"abort\", cancel, { once: true });\n promise\n .then(resolve, reject)\n .finally(() => signal.removeEventListener(\"abort\", cancel));\n if (signal.aborted) cancel();\n });\n}\n"],"mappings":";;;;;;;AAQA,MAAa,iCAAiC;;;;;;AAO9C,IAAa,iCAAb,cAAoD,MAAM;CACxD,YACE,AAAS,gBACT,AAAS,iBACT;AACA,QACE,sEAAsE,eAAe,QAAQ,gBAAgB,wBAC9G;EALQ;EACA;AAKT,OAAK,OAAO;;;;AAsDhB,MAAM,4BAAY,IAAI,SAAgC;AACtD,IAAI,eAAe;AACnB,SAAS,OAAO,OAA8B;CAC5C,IAAI,MAAM,UAAU,IAAI,MAAM;AAC9B,KAAI,CAAC,KAAK;AACR,QAAM,SAAS;AACf,YAAU,IAAI,OAAO,IAAI;;AAE3B,QAAO,GAAG,IAAI,IAAI,MAAM,YAAY;;;;;;AAOtC,IAAa,sBAAb,MAAiC;;gCACd,IAAI,KAA0B;oCAC1B,IAAI,KAAsB;;;;;;CAM/C,MAAM,QACJ,OACA,SACA,SACyB;EAGzB,MAAM,MAAM,OAAO,MAAM;AAEzB,SAAO,IAAI,SAAS,SAAS,WAAW;GACtC,IAAI,QAAQ,KAAK,OAAO,IAAI,IAAI;AAChC,OAAI,CAAC,OAAO;AACV,YAAQ,EAAE;AACV,SAAK,OAAO,IAAI,KAAK,MAAM;;AAG7B,SAAM,KAAK;IACT,SAAS;IACT,mBAAmB,SAAS;IAC5B;IACA;IACA,OAAO,SAAS;IAChB,WAAW,SA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CAAC;AAC7D,MAAI,OAAO,QAAS,SAAQ;GAC5B"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@copilotkit/mcp-apps-renderer",
|
|
3
|
-
"version": "2.0.0-rc.
|
|
3
|
+
"version": "2.0.0-rc.19",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Framework-agnostic MCP Apps host for CopilotKit - the app<->host protocol (ext-apps AppBridge), sandbox proxy, and shared renderer consumed by the React, Vue, and Angular frontends",
|
|
6
6
|
"keywords": [
|
|
@@ -51,7 +51,7 @@
|
|
|
51
51
|
"access": "public"
|
|
52
52
|
},
|
|
53
53
|
"dependencies": {
|
|
54
|
-
"@copilotkit/shared": "2.0.0-rc.
|
|
54
|
+
"@copilotkit/shared": "2.0.0-rc.19",
|
|
55
55
|
"@modelcontextprotocol/ext-apps": "^1.7.5",
|
|
56
56
|
"@modelcontextprotocol/sdk": "^1.29.0",
|
|
57
57
|
"zod": "^4.2.0"
|