@copilotkitnext/react 0.0.0-0.0.0-max-changeset-10101010101010-20260109194224 → 0.0.0-fix-runtime-openai-api-key-passthrough-20260115003142

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/index.mjs CHANGED
@@ -1,7 +1,6 @@
1
1
  "use client";
2
2
 
3
3
  // src/index.ts
4
- export * from "@ag-ui/core";
5
4
  export * from "@ag-ui/client";
6
5
 
7
6
  // src/components/chat/CopilotChatInput.tsx
@@ -1386,7 +1385,9 @@ import { createContext as createContext2, useContext as useContext2, useMemo as
1386
1385
  import { z as z2 } from "zod";
1387
1386
 
1388
1387
  // src/lib/react-core.ts
1389
- import { CopilotKitCore } from "@copilotkitnext/core";
1388
+ import {
1389
+ CopilotKitCore
1390
+ } from "@copilotkitnext/core";
1390
1391
  var CopilotKitCoreReact = class extends CopilotKitCore {
1391
1392
  _renderToolCalls = [];
1392
1393
  _renderCustomMessages = [];
@@ -1460,6 +1461,104 @@ import { useEffect as useEffect4, useRef as useRef3, useState as useState4, useC
1460
1461
  import { z } from "zod";
1461
1462
  import { jsx as jsx8, jsxs as jsxs4 } from "react/jsx-runtime";
1462
1463
  var PROTOCOL_VERSION = "2025-06-18";
1464
+ var SANDBOX_HTML = `<!doctype html>
1465
+ <html>
1466
+ <head>
1467
+ <meta charset="utf-8" />
1468
+ <meta http-equiv="Content-Security-Policy" content="default-src 'self'; img-src * data: blob: 'unsafe-inline'; media-src * blob: data:; font-src * blob: data:; script-src 'self' 'wasm-unsafe-eval' 'unsafe-inline' 'unsafe-eval' blob: data: http://localhost:* https://localhost:*; style-src * blob: data: 'unsafe-inline'; connect-src *; frame-src * blob: data: http://localhost:* https://localhost:*; base-uri 'self';" />
1469
+ <style>html,body{margin:0;padding:0;height:100%;width:100%;overflow:hidden}*{box-sizing:border-box}iframe{background-color:transparent;border:none;padding:0;overflow:hidden;width:100%;height:100%}</style>
1470
+ </head>
1471
+ <body>
1472
+ <script>
1473
+ if(window.self===window.top){throw new Error("This file must be used in an iframe.")}
1474
+ const inner=document.createElement("iframe");
1475
+ inner.style="width:100%;height:100%;border:none;";
1476
+ inner.setAttribute("sandbox","allow-scripts allow-same-origin allow-forms");
1477
+ document.body.appendChild(inner);
1478
+ window.addEventListener("message",async(event)=>{
1479
+ if(event.source===window.parent){
1480
+ if(event.data&&event.data.method==="ui/notifications/sandbox-resource-ready"){
1481
+ const{html,sandbox}=event.data.params;
1482
+ if(typeof sandbox==="string")inner.setAttribute("sandbox",sandbox);
1483
+ if(typeof html==="string")inner.srcdoc=html;
1484
+ }else if(inner&&inner.contentWindow){
1485
+ inner.contentWindow.postMessage(event.data,"*");
1486
+ }
1487
+ }else if(event.source===inner.contentWindow){
1488
+ window.parent.postMessage(event.data,"*");
1489
+ }
1490
+ });
1491
+ window.parent.postMessage({jsonrpc:"2.0",method:"ui/notifications/sandbox-proxy-ready",params:{}},"*");
1492
+ </script>
1493
+ </body>
1494
+ </html>`;
1495
+ var MCPAppsRequestQueue = class {
1496
+ queues = /* @__PURE__ */ new Map();
1497
+ processing = /* @__PURE__ */ new Map();
1498
+ /**
1499
+ * Add a request to the queue for a specific agent thread.
1500
+ * Returns a promise that resolves when the request completes.
1501
+ */
1502
+ async enqueue(agent, request) {
1503
+ const threadId = agent.threadId || "default";
1504
+ return new Promise((resolve, reject) => {
1505
+ let queue = this.queues.get(threadId);
1506
+ if (!queue) {
1507
+ queue = [];
1508
+ this.queues.set(threadId, queue);
1509
+ }
1510
+ queue.push({ execute: request, resolve, reject });
1511
+ this.processQueue(threadId, agent);
1512
+ });
1513
+ }
1514
+ async processQueue(threadId, agent) {
1515
+ if (this.processing.get(threadId)) {
1516
+ return;
1517
+ }
1518
+ this.processing.set(threadId, true);
1519
+ try {
1520
+ const queue = this.queues.get(threadId);
1521
+ if (!queue) return;
1522
+ while (queue.length > 0) {
1523
+ const item = queue[0];
1524
+ try {
1525
+ await this.waitForAgentIdle(agent);
1526
+ const result = await item.execute();
1527
+ item.resolve(result);
1528
+ } catch (error) {
1529
+ item.reject(error instanceof Error ? error : new Error(String(error)));
1530
+ }
1531
+ queue.shift();
1532
+ }
1533
+ } finally {
1534
+ this.processing.set(threadId, false);
1535
+ }
1536
+ }
1537
+ waitForAgentIdle(agent) {
1538
+ return new Promise((resolve) => {
1539
+ if (!agent.isRunning) {
1540
+ resolve();
1541
+ return;
1542
+ }
1543
+ let done = false;
1544
+ const finish = () => {
1545
+ if (done) return;
1546
+ done = true;
1547
+ clearInterval(checkInterval);
1548
+ sub.unsubscribe();
1549
+ resolve();
1550
+ };
1551
+ const sub = agent.subscribe({
1552
+ onRunFinalized: finish,
1553
+ onRunFailed: finish
1554
+ });
1555
+ const checkInterval = setInterval(() => {
1556
+ if (!agent.isRunning) finish();
1557
+ }, 500);
1558
+ });
1559
+ }
1560
+ };
1561
+ var mcpAppsRequestQueue = new MCPAppsRequestQueue();
1463
1562
  var MCPAppsActivityType = "mcp-apps";
1464
1563
  var MCPAppsActivityContentSchema = z.object({
1465
1564
  result: z.object({
@@ -1467,13 +1566,11 @@ var MCPAppsActivityContentSchema = z.object({
1467
1566
  structuredContent: z.any().optional(),
1468
1567
  isError: z.boolean().optional()
1469
1568
  }),
1470
- resource: z.object({
1471
- uri: z.string(),
1472
- mimeType: z.string().optional(),
1473
- text: z.string().optional(),
1474
- blob: z.string().optional()
1475
- }),
1476
- // Server ID for proxying requests (MD5 hash of server config)
1569
+ // Resource URI to fetch (e.g., "ui://server/dashboard")
1570
+ resourceUri: z.string(),
1571
+ // MD5 hash of server config (renamed from serverId in 0.0.1)
1572
+ serverHash: z.string(),
1573
+ // Optional stable server ID from config (takes precedence over serverHash)
1477
1574
  serverId: z.string().optional(),
1478
1575
  // Original tool input arguments
1479
1576
  toolInput: z.record(z.unknown()).optional()
@@ -1484,25 +1581,19 @@ function isRequest(msg) {
1484
1581
  function isNotification(msg) {
1485
1582
  return !("id" in msg) && "method" in msg;
1486
1583
  }
1487
- function extractHtmlFromResource(resource) {
1488
- if (resource.text) {
1489
- return resource.text;
1490
- }
1491
- if (resource.blob) {
1492
- return atob(resource.blob);
1493
- }
1494
- throw new Error("Resource has no text or blob content");
1495
- }
1496
- var MCPAppsActivityRenderer = ({ content, agent }) => {
1584
+ var MCPAppsActivityRenderer = function MCPAppsActivityRenderer2({ content, agent }) {
1497
1585
  const containerRef = useRef3(null);
1498
1586
  const iframeRef = useRef3(null);
1499
1587
  const [iframeReady, setIframeReady] = useState4(false);
1500
1588
  const [error, setError] = useState4(null);
1589
+ const [isLoading, setIsLoading] = useState4(true);
1501
1590
  const [iframeSize, setIframeSize] = useState4({});
1591
+ const [fetchedResource, setFetchedResource] = useState4(null);
1502
1592
  const contentRef = useRef3(content);
1503
1593
  contentRef.current = content;
1504
1594
  const agentRef = useRef3(agent);
1505
1595
  agentRef.current = agent;
1596
+ const fetchStateRef = useRef3({ inProgress: false, promise: null, resourceUri: null });
1506
1597
  const sendToIframe = useCallback2((msg) => {
1507
1598
  if (iframeRef.current?.contentWindow) {
1508
1599
  console.log("[MCPAppsRenderer] Sending to iframe:", msg);
@@ -1531,31 +1622,112 @@ var MCPAppsActivityRenderer = ({ content, agent }) => {
1531
1622
  });
1532
1623
  }, [sendToIframe]);
1533
1624
  useEffect4(() => {
1534
- if (!containerRef.current) return;
1625
+ const { resourceUri, serverHash, serverId } = content;
1626
+ if (fetchStateRef.current.inProgress && fetchStateRef.current.resourceUri === resourceUri) {
1627
+ fetchStateRef.current.promise?.then((resource) => {
1628
+ if (resource) {
1629
+ setFetchedResource(resource);
1630
+ setIsLoading(false);
1631
+ }
1632
+ }).catch((err) => {
1633
+ setError(err instanceof Error ? err : new Error(String(err)));
1634
+ setIsLoading(false);
1635
+ });
1636
+ return;
1637
+ }
1638
+ if (!agent) {
1639
+ setError(new Error("No agent available to fetch resource"));
1640
+ setIsLoading(false);
1641
+ return;
1642
+ }
1643
+ fetchStateRef.current.inProgress = true;
1644
+ fetchStateRef.current.resourceUri = resourceUri;
1645
+ const fetchPromise = (async () => {
1646
+ try {
1647
+ const runResult = await mcpAppsRequestQueue.enqueue(
1648
+ agent,
1649
+ () => agent.runAgent({
1650
+ forwardedProps: {
1651
+ __proxiedMCPRequest: {
1652
+ serverHash,
1653
+ serverId,
1654
+ // optional, takes precedence if provided
1655
+ method: "resources/read",
1656
+ params: { uri: resourceUri }
1657
+ }
1658
+ }
1659
+ })
1660
+ );
1661
+ const resultData = runResult.result;
1662
+ const resource = resultData?.contents?.[0];
1663
+ if (!resource) {
1664
+ throw new Error("No resource content in response");
1665
+ }
1666
+ return resource;
1667
+ } catch (err) {
1668
+ console.error("[MCPAppsRenderer] Failed to fetch resource:", err);
1669
+ throw err;
1670
+ } finally {
1671
+ fetchStateRef.current.inProgress = false;
1672
+ }
1673
+ })();
1674
+ fetchStateRef.current.promise = fetchPromise;
1675
+ fetchPromise.then((resource) => {
1676
+ if (resource) {
1677
+ setFetchedResource(resource);
1678
+ setIsLoading(false);
1679
+ }
1680
+ }).catch((err) => {
1681
+ setError(err instanceof Error ? err : new Error(String(err)));
1682
+ setIsLoading(false);
1683
+ });
1684
+ }, [agent, content]);
1685
+ useEffect4(() => {
1686
+ if (isLoading || !fetchedResource) {
1687
+ return;
1688
+ }
1689
+ const container = containerRef.current;
1690
+ if (!container) {
1691
+ return;
1692
+ }
1535
1693
  let mounted = true;
1536
1694
  let messageHandler = null;
1695
+ let initialListener = null;
1696
+ let createdIframe = null;
1537
1697
  const setup = async () => {
1538
1698
  try {
1539
1699
  const iframe = document.createElement("iframe");
1700
+ createdIframe = iframe;
1540
1701
  iframe.style.width = "100%";
1541
- iframe.style.height = "300px";
1702
+ iframe.style.height = "100px";
1542
1703
  iframe.style.border = "none";
1543
1704
  iframe.style.backgroundColor = "transparent";
1705
+ iframe.style.display = "block";
1544
1706
  iframe.setAttribute("sandbox", "allow-scripts allow-same-origin allow-forms");
1545
1707
  const sandboxReady = new Promise((resolve) => {
1546
- const initialListener = (event) => {
1708
+ initialListener = (event) => {
1547
1709
  if (event.source === iframe.contentWindow) {
1548
1710
  if (event.data?.method === "ui/notifications/sandbox-proxy-ready") {
1549
- window.removeEventListener("message", initialListener);
1711
+ if (initialListener) {
1712
+ window.removeEventListener("message", initialListener);
1713
+ initialListener = null;
1714
+ }
1550
1715
  resolve();
1551
1716
  }
1552
1717
  }
1553
1718
  };
1554
1719
  window.addEventListener("message", initialListener);
1555
1720
  });
1556
- iframe.src = "/sandbox.html";
1721
+ if (!mounted) {
1722
+ if (initialListener) {
1723
+ window.removeEventListener("message", initialListener);
1724
+ initialListener = null;
1725
+ }
1726
+ return;
1727
+ }
1728
+ iframe.srcdoc = SANDBOX_HTML;
1557
1729
  iframeRef.current = iframe;
1558
- containerRef.current?.appendChild(iframe);
1730
+ container.appendChild(iframe);
1559
1731
  await sandboxReady;
1560
1732
  if (!mounted) return;
1561
1733
  console.log("[MCPAppsRenderer] Sandbox proxy ready");
@@ -1585,8 +1757,27 @@ var MCPAppsActivityRenderer = ({ content, agent }) => {
1585
1757
  break;
1586
1758
  }
1587
1759
  case "ui/message": {
1588
- console.log("[MCPAppsRenderer] ui/message request:", msg.params);
1589
- sendResponse(msg.id, { isError: false });
1760
+ const currentAgent = agentRef.current;
1761
+ if (!currentAgent) {
1762
+ console.warn("[MCPAppsRenderer] ui/message: No agent available");
1763
+ sendResponse(msg.id, { isError: false });
1764
+ break;
1765
+ }
1766
+ try {
1767
+ const params = msg.params;
1768
+ const textContent = params.content?.filter((c) => c.type === "text" && c.text).map((c) => c.text).join("\n") || "";
1769
+ if (textContent) {
1770
+ currentAgent.addMessage({
1771
+ id: crypto.randomUUID(),
1772
+ role: params.role || "user",
1773
+ content: textContent
1774
+ });
1775
+ }
1776
+ sendResponse(msg.id, { isError: false });
1777
+ } catch (err) {
1778
+ console.error("[MCPAppsRenderer] ui/message error:", err);
1779
+ sendResponse(msg.id, { isError: true });
1780
+ }
1590
1781
  break;
1591
1782
  }
1592
1783
  case "ui/open-link": {
@@ -1600,10 +1791,10 @@ var MCPAppsActivityRenderer = ({ content, agent }) => {
1600
1791
  break;
1601
1792
  }
1602
1793
  case "tools/call": {
1603
- const { serverId } = contentRef.current;
1794
+ const { serverHash, serverId } = contentRef.current;
1604
1795
  const currentAgent = agentRef.current;
1605
- if (!serverId) {
1606
- sendErrorResponse(msg.id, -32603, "No server ID available for proxying");
1796
+ if (!serverHash) {
1797
+ sendErrorResponse(msg.id, -32603, "No server hash available for proxying");
1607
1798
  break;
1608
1799
  }
1609
1800
  if (!currentAgent) {
@@ -1611,15 +1802,20 @@ var MCPAppsActivityRenderer = ({ content, agent }) => {
1611
1802
  break;
1612
1803
  }
1613
1804
  try {
1614
- const runResult = await currentAgent.runAgent({
1615
- forwardedProps: {
1616
- __proxiedMCPRequest: {
1617
- serverId,
1618
- method: "tools/call",
1619
- params: msg.params
1805
+ const runResult = await mcpAppsRequestQueue.enqueue(
1806
+ currentAgent,
1807
+ () => currentAgent.runAgent({
1808
+ forwardedProps: {
1809
+ __proxiedMCPRequest: {
1810
+ serverHash,
1811
+ serverId,
1812
+ // optional, takes precedence if provided
1813
+ method: "tools/call",
1814
+ params: msg.params
1815
+ }
1620
1816
  }
1621
- }
1622
- });
1817
+ })
1818
+ );
1623
1819
  sendResponse(msg.id, runResult.result || {});
1624
1820
  } catch (err) {
1625
1821
  console.error("[MCPAppsRenderer] tools/call error:", err);
@@ -1640,7 +1836,7 @@ var MCPAppsActivityRenderer = ({ content, agent }) => {
1640
1836
  }
1641
1837
  break;
1642
1838
  }
1643
- case "ui/notifications/size-change": {
1839
+ case "ui/notifications/size-changed": {
1644
1840
  const { width, height } = msg.params || {};
1645
1841
  console.log("[MCPAppsRenderer] Size change:", { width, height });
1646
1842
  if (mounted) {
@@ -1659,7 +1855,14 @@ var MCPAppsActivityRenderer = ({ content, agent }) => {
1659
1855
  }
1660
1856
  };
1661
1857
  window.addEventListener("message", messageHandler);
1662
- const html = extractHtmlFromResource(content.resource);
1858
+ let html;
1859
+ if (fetchedResource.text) {
1860
+ html = fetchedResource.text;
1861
+ } else if (fetchedResource.blob) {
1862
+ html = atob(fetchedResource.blob);
1863
+ } else {
1864
+ throw new Error("Resource has no text or blob content");
1865
+ }
1663
1866
  sendNotification("ui/notifications/sandbox-resource-ready", { html });
1664
1867
  } catch (err) {
1665
1868
  console.error("[MCPAppsRenderer] Setup error:", err);
@@ -1671,19 +1874,25 @@ var MCPAppsActivityRenderer = ({ content, agent }) => {
1671
1874
  setup();
1672
1875
  return () => {
1673
1876
  mounted = false;
1877
+ if (initialListener) {
1878
+ window.removeEventListener("message", initialListener);
1879
+ initialListener = null;
1880
+ }
1674
1881
  if (messageHandler) {
1675
1882
  window.removeEventListener("message", messageHandler);
1676
1883
  }
1677
- if (iframeRef.current && containerRef.current?.contains(iframeRef.current)) {
1678
- containerRef.current.removeChild(iframeRef.current);
1884
+ if (createdIframe) {
1885
+ createdIframe.remove();
1886
+ createdIframe = null;
1679
1887
  }
1680
1888
  iframeRef.current = null;
1681
1889
  };
1682
- }, [content.resource, sendNotification, sendResponse, sendErrorResponse]);
1890
+ }, [isLoading, fetchedResource, sendNotification, sendResponse, sendErrorResponse]);
1683
1891
  useEffect4(() => {
1684
1892
  if (iframeRef.current) {
1685
1893
  if (iframeSize.width !== void 0) {
1686
- iframeRef.current.style.width = `${iframeSize.width}px`;
1894
+ iframeRef.current.style.minWidth = `min(${iframeSize.width}px, 100%)`;
1895
+ iframeRef.current.style.width = "100%";
1687
1896
  }
1688
1897
  if (iframeSize.height !== void 0) {
1689
1898
  iframeRef.current.style.height = `${iframeSize.height}px`;
@@ -1704,22 +1913,31 @@ var MCPAppsActivityRenderer = ({ content, agent }) => {
1704
1913
  sendNotification("ui/notifications/tool-result", content.result);
1705
1914
  }
1706
1915
  }, [iframeReady, content.result, sendNotification]);
1707
- return /* @__PURE__ */ jsx8(
1916
+ const prefersBorder = fetchedResource?._meta?.ui?.prefersBorder;
1917
+ const borderStyle = prefersBorder === true ? {
1918
+ borderRadius: "8px",
1919
+ backgroundColor: "#f9f9f9",
1920
+ border: "1px solid #e0e0e0"
1921
+ } : {};
1922
+ return /* @__PURE__ */ jsxs4(
1708
1923
  "div",
1709
1924
  {
1710
1925
  ref: containerRef,
1711
1926
  style: {
1712
1927
  width: "100%",
1928
+ height: iframeSize.height ? `${iframeSize.height}px` : "auto",
1713
1929
  minHeight: "100px",
1714
- borderRadius: "8px",
1715
1930
  overflow: "hidden",
1716
- backgroundColor: "#f9f9f9",
1717
- border: "1px solid #e0e0e0"
1931
+ position: "relative",
1932
+ ...borderStyle
1718
1933
  },
1719
- children: error && /* @__PURE__ */ jsxs4("div", { style: { color: "red", padding: "1rem" }, children: [
1720
- "Error: ",
1721
- error.message
1722
- ] })
1934
+ children: [
1935
+ isLoading && /* @__PURE__ */ jsx8("div", { style: { padding: "1rem", color: "#666" }, children: "Loading..." }),
1936
+ error && /* @__PURE__ */ jsxs4("div", { style: { color: "red", padding: "1rem" }, children: [
1937
+ "Error: ",
1938
+ error.message
1939
+ ] })
1940
+ ]
1723
1941
  }
1724
1942
  );
1725
1943
  };
@@ -2141,22 +2359,28 @@ function useRenderCustomMessages() {
2141
2359
 
2142
2360
  // src/hooks/use-render-activity-message.tsx
2143
2361
  import { DEFAULT_AGENT_ID as DEFAULT_AGENT_ID3 } from "@copilotkitnext/shared";
2144
- import { useCallback as useCallback4 } from "react";
2362
+ import { useCallback as useCallback4, useMemo as useMemo5 } from "react";
2145
2363
  import { jsx as jsx12 } from "react/jsx-runtime";
2146
2364
  function useRenderActivityMessage() {
2147
2365
  const { copilotkit } = useCopilotKit();
2148
2366
  const config = useCopilotChatConfiguration();
2149
2367
  const agentId = config?.agentId ?? DEFAULT_AGENT_ID3;
2150
2368
  const renderers = copilotkit.renderActivityMessages;
2151
- return useCallback4(
2152
- (message) => {
2369
+ const findRenderer = useCallback4(
2370
+ (activityType) => {
2153
2371
  if (!renderers.length) {
2154
2372
  return null;
2155
2373
  }
2156
2374
  const matches = renderers.filter(
2157
- (renderer2) => renderer2.activityType === message.activityType
2375
+ (renderer) => renderer.activityType === activityType
2158
2376
  );
2159
- const renderer = matches.find((candidate) => candidate.agentId === agentId) ?? matches.find((candidate) => candidate.agentId === void 0) ?? renderers.find((candidate) => candidate.activityType === "*");
2377
+ return matches.find((candidate) => candidate.agentId === agentId) ?? matches.find((candidate) => candidate.agentId === void 0) ?? renderers.find((candidate) => candidate.activityType === "*") ?? null;
2378
+ },
2379
+ [agentId, renderers]
2380
+ );
2381
+ const renderActivityMessage = useCallback4(
2382
+ (message) => {
2383
+ const renderer = findRenderer(message.activityType);
2160
2384
  if (!renderer) {
2161
2385
  return null;
2162
2386
  }
@@ -2181,7 +2405,11 @@ function useRenderActivityMessage() {
2181
2405
  message.id
2182
2406
  );
2183
2407
  },
2184
- [agentId, copilotkit, renderers]
2408
+ [agentId, copilotkit, findRenderer]
2409
+ );
2410
+ return useMemo5(
2411
+ () => ({ renderActivityMessage, findRenderer }),
2412
+ [renderActivityMessage, findRenderer]
2185
2413
  );
2186
2414
  }
2187
2415
 
@@ -2290,7 +2518,7 @@ function useHumanInTheLoop(tool, deps) {
2290
2518
  }
2291
2519
 
2292
2520
  // src/hooks/use-agent.tsx
2293
- import { useMemo as useMemo5, useEffect as useEffect8, useReducer as useReducer2 } from "react";
2521
+ import { useMemo as useMemo6, useEffect as useEffect8, useReducer as useReducer2 } from "react";
2294
2522
  import { DEFAULT_AGENT_ID as DEFAULT_AGENT_ID4 } from "@copilotkitnext/shared";
2295
2523
  import { ProxiedCopilotRuntimeAgent, CopilotKitCoreRuntimeConnectionStatus } from "@copilotkitnext/core";
2296
2524
  var UseAgentUpdate = /* @__PURE__ */ ((UseAgentUpdate2) => {
@@ -2308,11 +2536,11 @@ function useAgent({ agentId, updates } = {}) {
2308
2536
  agentId ??= DEFAULT_AGENT_ID4;
2309
2537
  const { copilotkit } = useCopilotKit();
2310
2538
  const [, forceUpdate] = useReducer2((x) => x + 1, 0);
2311
- const updateFlags = useMemo5(
2539
+ const updateFlags = useMemo6(
2312
2540
  () => updates ?? ALL_UPDATES,
2313
2541
  [JSON.stringify(updates)]
2314
2542
  );
2315
- const agent = useMemo5(() => {
2543
+ const agent = useMemo6(() => {
2316
2544
  const existing = copilotkit.getAgent(agentId);
2317
2545
  if (existing) {
2318
2546
  return existing;
@@ -2353,20 +2581,12 @@ function useAgent({ agentId, updates } = {}) {
2353
2581
  };
2354
2582
  }
2355
2583
  if (updateFlags.includes("OnStateChanged" /* OnStateChanged */)) {
2356
- handlers.onStateChanged = () => {
2357
- forceUpdate();
2358
- };
2584
+ handlers.onStateChanged = forceUpdate;
2359
2585
  }
2360
2586
  if (updateFlags.includes("OnRunStatusChanged" /* OnRunStatusChanged */)) {
2361
- handlers.onRunInitialized = () => {
2362
- forceUpdate();
2363
- };
2364
- handlers.onRunFinalized = () => {
2365
- forceUpdate();
2366
- };
2367
- handlers.onRunFailed = () => {
2368
- forceUpdate();
2369
- };
2587
+ handlers.onRunInitialized = forceUpdate;
2588
+ handlers.onRunFinalized = forceUpdate;
2589
+ handlers.onRunFailed = forceUpdate;
2370
2590
  }
2371
2591
  const subscription = agent.subscribe(handlers);
2372
2592
  return () => subscription.unsubscribe();
@@ -2377,11 +2597,11 @@ function useAgent({ agentId, updates } = {}) {
2377
2597
  }
2378
2598
 
2379
2599
  // src/hooks/use-agent-context.tsx
2380
- import { useEffect as useEffect9, useMemo as useMemo6 } from "react";
2600
+ import { useEffect as useEffect9, useMemo as useMemo7 } from "react";
2381
2601
  function useAgentContext(context) {
2382
2602
  const { description, value } = context;
2383
2603
  const { copilotkit } = useCopilotKit();
2384
- const stringValue = useMemo6(() => {
2604
+ const stringValue = useMemo7(() => {
2385
2605
  if (typeof value === "string") {
2386
2606
  return value;
2387
2607
  }
@@ -2397,12 +2617,12 @@ function useAgentContext(context) {
2397
2617
  }
2398
2618
 
2399
2619
  // src/hooks/use-suggestions.tsx
2400
- import { useCallback as useCallback6, useEffect as useEffect10, useMemo as useMemo7, useState as useState6 } from "react";
2620
+ import { useCallback as useCallback6, useEffect as useEffect10, useMemo as useMemo8, useState as useState6 } from "react";
2401
2621
  import { DEFAULT_AGENT_ID as DEFAULT_AGENT_ID5 } from "@copilotkitnext/shared";
2402
2622
  function useSuggestions({ agentId } = {}) {
2403
2623
  const { copilotkit } = useCopilotKit();
2404
2624
  const config = useCopilotChatConfiguration();
2405
- const resolvedAgentId = useMemo7(() => agentId ?? config?.agentId ?? DEFAULT_AGENT_ID5, [agentId, config?.agentId]);
2625
+ const resolvedAgentId = useMemo8(() => agentId ?? config?.agentId ?? DEFAULT_AGENT_ID5, [agentId, config?.agentId]);
2406
2626
  const [suggestions, setSuggestions] = useState6(() => {
2407
2627
  const result = copilotkit.getSuggestions(resolvedAgentId);
2408
2628
  return result.suggestions;
@@ -2461,19 +2681,19 @@ function useSuggestions({ agentId } = {}) {
2461
2681
  }
2462
2682
 
2463
2683
  // src/hooks/use-configure-suggestions.tsx
2464
- import { useCallback as useCallback7, useEffect as useEffect11, useMemo as useMemo8, useRef as useRef6 } from "react";
2684
+ import { useCallback as useCallback7, useEffect as useEffect11, useMemo as useMemo9, useRef as useRef6 } from "react";
2465
2685
  import { DEFAULT_AGENT_ID as DEFAULT_AGENT_ID6 } from "@copilotkitnext/shared";
2466
2686
  function useConfigureSuggestions(config, deps) {
2467
2687
  const { copilotkit } = useCopilotKit();
2468
2688
  const chatConfig = useCopilotChatConfiguration();
2469
2689
  const extraDeps = deps ?? [];
2470
- const resolvedConsumerAgentId = useMemo8(() => chatConfig?.agentId ?? DEFAULT_AGENT_ID6, [chatConfig?.agentId]);
2471
- const rawConsumerAgentId = useMemo8(() => config ? config.consumerAgentId : void 0, [config]);
2690
+ const resolvedConsumerAgentId = useMemo9(() => chatConfig?.agentId ?? DEFAULT_AGENT_ID6, [chatConfig?.agentId]);
2691
+ const rawConsumerAgentId = useMemo9(() => config ? config.consumerAgentId : void 0, [config]);
2472
2692
  const normalizationCacheRef = useRef6({
2473
2693
  serialized: null,
2474
2694
  config: null
2475
2695
  });
2476
- const { normalizedConfig, serializedConfig } = useMemo8(() => {
2696
+ const { normalizedConfig, serializedConfig } = useMemo9(() => {
2477
2697
  if (!config) {
2478
2698
  normalizationCacheRef.current = { serialized: null, config: null };
2479
2699
  return { normalizedConfig: null, serializedConfig: null };
@@ -2506,7 +2726,7 @@ function useConfigureSuggestions(config, deps) {
2506
2726
  const latestConfigRef = useRef6(null);
2507
2727
  latestConfigRef.current = normalizedConfig;
2508
2728
  const previousSerializedConfigRef = useRef6(null);
2509
- const targetAgentId = useMemo8(() => {
2729
+ const targetAgentId = useMemo9(() => {
2510
2730
  if (!normalizedConfig) {
2511
2731
  return resolvedConsumerAgentId;
2512
2732
  }
@@ -2850,7 +3070,7 @@ CopilotChatAssistantMessage.RegenerateButton.displayName = "CopilotChatAssistant
2850
3070
  var CopilotChatAssistantMessage_default = CopilotChatAssistantMessage;
2851
3071
 
2852
3072
  // src/components/chat/CopilotChatUserMessage.tsx
2853
- import { useMemo as useMemo9, useState as useState8 } from "react";
3073
+ import { useMemo as useMemo10, useState as useState8 } from "react";
2854
3074
  import { Copy as Copy2, Check as Check3, Edit, ChevronLeft, ChevronRight } from "lucide-react";
2855
3075
  import { twMerge as twMerge5 } from "tailwind-merge";
2856
3076
  import { Fragment as Fragment4, jsx as jsx15, jsxs as jsxs7 } from "react/jsx-runtime";
@@ -2884,7 +3104,7 @@ function CopilotChatUserMessage({
2884
3104
  className,
2885
3105
  ...props
2886
3106
  }) {
2887
- const flattenedContent = useMemo9(
3107
+ const flattenedContent = useMemo10(
2888
3108
  () => flattenUserMessageContent(message.content),
2889
3109
  [message.content]
2890
3110
  );
@@ -3325,7 +3545,7 @@ function CopilotChatMessageView({
3325
3545
  ...props
3326
3546
  }) {
3327
3547
  const renderCustomMessage = useRenderCustomMessages();
3328
- const renderActivityMessage = useRenderActivityMessage();
3548
+ const { renderActivityMessage } = useRenderActivityMessage();
3329
3549
  const { copilotkit } = useCopilotKit();
3330
3550
  const config = useCopilotChatConfiguration();
3331
3551
  const [, forceUpdate] = useReducer3((x) => x + 1, 0);
@@ -3334,10 +3554,10 @@ function CopilotChatMessageView({
3334
3554
  const agent = copilotkit.getAgent(config.agentId);
3335
3555
  if (!agent) return;
3336
3556
  const subscription = agent.subscribe({
3337
- onStateChanged: () => forceUpdate()
3557
+ onStateChanged: forceUpdate
3338
3558
  });
3339
3559
  return () => subscription.unsubscribe();
3340
- }, [config?.agentId, copilotkit]);
3560
+ }, [config?.agentId, copilotkit, forceUpdate]);
3341
3561
  const getStateSnapshotForMessage = (messageId) => {
3342
3562
  if (!config) return void 0;
3343
3563
  const runId = copilotkit.getRunIdForMessage(config.agentId, config.threadId, messageId);
@@ -3388,11 +3608,12 @@ function CopilotChatMessageView({
3388
3608
  )
3389
3609
  );
3390
3610
  } else if (message.role === "activity") {
3611
+ const activityMsg = message;
3391
3612
  elements.push(
3392
3613
  /* @__PURE__ */ jsx18(
3393
3614
  MemoizedActivityMessage,
3394
3615
  {
3395
- message,
3616
+ message: activityMsg,
3396
3617
  renderActivityMessage
3397
3618
  },
3398
3619
  message.id
@@ -3754,14 +3975,14 @@ var CopilotChatView_default = CopilotChatView;
3754
3975
 
3755
3976
  // src/components/chat/CopilotChat.tsx
3756
3977
  import { DEFAULT_AGENT_ID as DEFAULT_AGENT_ID7, randomUUID as randomUUID2 } from "@copilotkitnext/shared";
3757
- import { useCallback as useCallback8, useEffect as useEffect15, useMemo as useMemo10 } from "react";
3978
+ import { useCallback as useCallback8, useEffect as useEffect15, useMemo as useMemo11 } from "react";
3758
3979
  import { merge } from "ts-deepmerge";
3759
3980
  import { AGUIConnectNotImplementedError } from "@ag-ui/client";
3760
3981
  import { jsx as jsx20 } from "react/jsx-runtime";
3761
3982
  function CopilotChat({ agentId, threadId, labels, chatView, isModalDefaultOpen, ...props }) {
3762
3983
  const existingConfig = useCopilotChatConfiguration();
3763
3984
  const resolvedAgentId = agentId ?? existingConfig?.agentId ?? DEFAULT_AGENT_ID7;
3764
- const resolvedThreadId = useMemo10(
3985
+ const resolvedThreadId = useMemo11(
3765
3986
  () => threadId ?? existingConfig?.threadId ?? randomUUID2(),
3766
3987
  [threadId, existingConfig?.threadId]
3767
3988
  );
@@ -3855,8 +4076,9 @@ function CopilotChat({ agentId, threadId, labels, chatView, isModalDefaultOpen,
3855
4076
  isRunning: agent.isRunning
3856
4077
  };
3857
4078
  finalInputProps.mode = agent.isRunning ? "processing" : finalInputProps.mode ?? "input";
4079
+ const messages = useMemo11(() => [...agent.messages], [JSON.stringify(agent.messages)]);
3858
4080
  const finalProps = merge(mergedProps, {
3859
- messages: agent.messages,
4081
+ messages,
3860
4082
  inputProps: finalInputProps
3861
4083
  });
3862
4084
  const RenderedChatView = renderSlot(chatView, CopilotChatView, finalProps);
@@ -4176,7 +4398,7 @@ function CopilotSidebarView({ header, width, ...props }) {
4176
4398
  CopilotSidebarView.displayName = "CopilotSidebarView";
4177
4399
 
4178
4400
  // src/components/chat/CopilotPopupView.tsx
4179
- import { useEffect as useEffect17, useMemo as useMemo11, useRef as useRef9, useState as useState13 } from "react";
4401
+ import { useEffect as useEffect17, useMemo as useMemo12, useRef as useRef9, useState as useState13 } from "react";
4180
4402
  import { Fragment as Fragment8, jsx as jsx24, jsxs as jsxs15 } from "react/jsx-runtime";
4181
4403
  var DEFAULT_POPUP_WIDTH = 420;
4182
4404
  var DEFAULT_POPUP_HEIGHT = 560;
@@ -4270,10 +4492,10 @@ function CopilotPopupView({
4270
4492
  document.addEventListener("pointerdown", handlePointerDown);
4271
4493
  return () => document.removeEventListener("pointerdown", handlePointerDown);
4272
4494
  }, [isPopupOpen, clickOutsideToClose, setModalOpen]);
4273
- const headerElement = useMemo11(() => renderSlot(header, CopilotModalHeader, {}), [header]);
4495
+ const headerElement = useMemo12(() => renderSlot(header, CopilotModalHeader, {}), [header]);
4274
4496
  const resolvedWidth = dimensionToCss(width, DEFAULT_POPUP_WIDTH);
4275
4497
  const resolvedHeight = dimensionToCss(height, DEFAULT_POPUP_HEIGHT);
4276
- const popupStyle = useMemo11(
4498
+ const popupStyle = useMemo12(
4277
4499
  () => ({
4278
4500
  "--copilot-popup-width": resolvedWidth,
4279
4501
  "--copilot-popup-height": resolvedHeight,
@@ -4335,10 +4557,10 @@ function CopilotPopupView({
4335
4557
  CopilotPopupView.displayName = "CopilotPopupView";
4336
4558
 
4337
4559
  // src/components/chat/CopilotSidebar.tsx
4338
- import { useMemo as useMemo12 } from "react";
4560
+ import { useMemo as useMemo13 } from "react";
4339
4561
  import { jsx as jsx25 } from "react/jsx-runtime";
4340
4562
  function CopilotSidebar({ header, defaultOpen, width, ...chatProps }) {
4341
- const SidebarViewOverride = useMemo12(() => {
4563
+ const SidebarViewOverride = useMemo13(() => {
4342
4564
  const Component = (viewProps) => {
4343
4565
  const { header: viewHeader, width: viewWidth, ...restProps } = viewProps;
4344
4566
  return /* @__PURE__ */ jsx25(
@@ -4364,7 +4586,7 @@ function CopilotSidebar({ header, defaultOpen, width, ...chatProps }) {
4364
4586
  CopilotSidebar.displayName = "CopilotSidebar";
4365
4587
 
4366
4588
  // src/components/chat/CopilotPopup.tsx
4367
- import { useMemo as useMemo13 } from "react";
4589
+ import { useMemo as useMemo14 } from "react";
4368
4590
  import { jsx as jsx26 } from "react/jsx-runtime";
4369
4591
  function CopilotPopup({
4370
4592
  header,
@@ -4374,7 +4596,7 @@ function CopilotPopup({
4374
4596
  clickOutsideToClose,
4375
4597
  ...chatProps
4376
4598
  }) {
4377
- const PopupViewOverride = useMemo13(() => {
4599
+ const PopupViewOverride = useMemo14(() => {
4378
4600
  const Component = (viewProps) => {
4379
4601
  const {
4380
4602
  header: viewHeader,