@kuntur/a2a-carbon-chat-adapter 0.1.7 → 0.1.9

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.cjs CHANGED
@@ -1728,6 +1728,7 @@ function A2AChat({
1728
1728
  agentUrl,
1729
1729
  apiKey,
1730
1730
  // Display options
1731
+ embedded = false,
1731
1732
  layout = "fullscreen",
1732
1733
  allowLayoutSwitch = false,
1733
1734
  defaultOpen = true,
@@ -1784,6 +1785,13 @@ function A2AChat({
1784
1785
  const instanceRef = react.useRef(null);
1785
1786
  const translatorRef = react.useRef(null);
1786
1787
  const abortControllerRef = react.useRef(null);
1788
+ const [isViewOpen, setIsViewOpen] = react.useState(true);
1789
+ const [showExternalLauncher, setShowExternalLauncher] = react.useState(false);
1790
+ const [sidebarClosing, setSidebarClosing] = react.useState(false);
1791
+ const layoutRef = react.useRef(layout);
1792
+ react.useEffect(() => {
1793
+ layoutRef.current = layout;
1794
+ }, [layout]);
1787
1795
  react.useEffect(() => {
1788
1796
  if (typeof window !== "undefined") {
1789
1797
  import('@carbon/ai-chat').then((mod) => {
@@ -1800,6 +1808,50 @@ function A2AChat({
1800
1808
  react.useEffect(() => {
1801
1809
  onConnectionChange?.(connectionState);
1802
1810
  }, [connectionState, onConnectionChange]);
1811
+ const SIDEBAR_ANIMATION_MS = 250;
1812
+ const onViewChange = react.useCallback(
1813
+ (event) => {
1814
+ const currentLayout = layoutRef.current;
1815
+ if (embedded) {
1816
+ if (!event.newViewState.mainWindow) {
1817
+ onClose?.();
1818
+ }
1819
+ return;
1820
+ }
1821
+ setIsViewOpen(event.newViewState.mainWindow);
1822
+ if (currentLayout === "sidebar") {
1823
+ if (event.newViewState.mainWindow) {
1824
+ setShowExternalLauncher(false);
1825
+ setSidebarClosing(false);
1826
+ onOpen?.();
1827
+ } else {
1828
+ setSidebarClosing(false);
1829
+ setShowExternalLauncher(true);
1830
+ onClose?.();
1831
+ }
1832
+ }
1833
+ },
1834
+ [embedded, onOpen, onClose]
1835
+ );
1836
+ const onViewPreChange = react.useCallback(
1837
+ async (event) => {
1838
+ if (embedded) {
1839
+ return;
1840
+ }
1841
+ if (layoutRef.current === "sidebar" && !event.newViewState.mainWindow) {
1842
+ setSidebarClosing(true);
1843
+ await new Promise((resolve) => setTimeout(resolve, SIDEBAR_ANIMATION_MS));
1844
+ }
1845
+ },
1846
+ [embedded]
1847
+ );
1848
+ const handleOpenSidebar = react.useCallback(() => {
1849
+ if (embedded) return;
1850
+ const instance = instanceRef.current;
1851
+ if (instance?.actions?.changeView) {
1852
+ instance.actions.changeView("MAIN_WINDOW");
1853
+ }
1854
+ }, [embedded]);
1803
1855
  const handleSendMessage = react.useCallback(
1804
1856
  async (message) => {
1805
1857
  if (!agent) {
@@ -1952,6 +2004,24 @@ function A2AChat({
1952
2004
  instanceRef.current = instance;
1953
2005
  console.log("[A2AChat] Chat instance ready");
1954
2006
  }, []);
2007
+ const elementClassName = react.useMemo(() => {
2008
+ const classes = ["a2a-chat__element"];
2009
+ if (layout === "sidebar") {
2010
+ classes.push("a2a-chat__element--sidebar");
2011
+ if (sidebarClosing && !embedded) {
2012
+ classes.push("a2a-chat__element--sidebar-closing");
2013
+ }
2014
+ } else if (layout === "fullscreen") {
2015
+ classes.push("a2a-chat__element--fullscreen");
2016
+ }
2017
+ if (embedded && (layout === "sidebar" || layout === "fullscreen")) {
2018
+ classes.push("a2a-chat__element--embedded");
2019
+ }
2020
+ if (!isViewOpen && !embedded) {
2021
+ classes.push("cds-aichat--hidden");
2022
+ }
2023
+ return classes.join(" ");
2024
+ }, [layout, embedded, isViewOpen, sidebarClosing]);
1955
2025
  if (!agent) {
1956
2026
  return /* @__PURE__ */ jsxRuntime.jsx("div", { className: `a2a-chat a2a-chat--error ${className}`, children: /* @__PURE__ */ jsxRuntime.jsx("p", { children: "No agent configured. Provide `agent`, `agentId`, or `agentUrl` prop." }) });
1957
2027
  }
@@ -2005,7 +2075,7 @@ function A2AChat({
2005
2075
  ChatCustomElement,
2006
2076
  {
2007
2077
  ...{
2008
- className: "a2a-chat__element",
2078
+ className: elementClassName,
2009
2079
  debug: false,
2010
2080
  aiEnabled: true,
2011
2081
  openChatByDefault: true,
@@ -2013,15 +2083,22 @@ function A2AChat({
2013
2083
  },
2014
2084
  header: {
2015
2085
  title: agent?.name ?? "AI Assistant",
2016
- showMinimize: layout !== "fullscreen"
2086
+ // EMBEDDED MODE: Always show minimize - it's the close mechanism
2087
+ // STANDALONE: Show for sidebar only (fullscreen doesn't need it)
2088
+ showMinimize: embedded || layout === "sidebar"
2017
2089
  },
2018
2090
  launcher: {
2019
2091
  isOn: layout === "float"
2092
+ // Only show built-in launcher for float
2020
2093
  },
2021
2094
  layout: {
2022
2095
  showFrame: layout === "float",
2023
- showCloseAndRestartButton: layout !== "fullscreen"
2096
+ // EMBEDDED MODE: Hide close/restart since parent controls lifecycle
2097
+ // STANDALONE: Show for non-fullscreen layouts
2098
+ showCloseAndRestartButton: !embedded && layout !== "fullscreen"
2024
2099
  },
2100
+ onViewChange,
2101
+ onViewPreChange,
2025
2102
  onAfterRender: handleAfterRender,
2026
2103
  renderUserDefinedResponse: renderCustomResponse,
2027
2104
  messaging: {
@@ -2036,6 +2113,29 @@ function A2AChat({
2036
2113
  }
2037
2114
  }
2038
2115
  ),
2116
+ layout === "sidebar" && showExternalLauncher && !embedded && /* @__PURE__ */ jsxRuntime.jsx(
2117
+ "button",
2118
+ {
2119
+ className: "a2a-chat__external-launcher",
2120
+ onClick: handleOpenSidebar,
2121
+ "aria-label": "Open chat",
2122
+ title: "Open chat",
2123
+ children: /* @__PURE__ */ jsxRuntime.jsxs(
2124
+ "svg",
2125
+ {
2126
+ width: "24",
2127
+ height: "24",
2128
+ viewBox: "0 0 32 32",
2129
+ fill: "currentColor",
2130
+ "aria-hidden": "true",
2131
+ children: [
2132
+ /* @__PURE__ */ jsxRuntime.jsx("path", { d: "M17.74 30L16 29l4-7h6a2 2 0 0 0 2-2V8a2 2 0 0 0-2-2H6a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h9v2H6a4 4 0 0 1-4-4V8a4 4 0 0 1 4-4h20a4 4 0 0 1 4 4v12a4 4 0 0 1-4 4h-4.84Z" }),
2133
+ /* @__PURE__ */ jsxRuntime.jsx("path", { d: "M8 10h16v2H8zM8 16h10v2H8z" })
2134
+ ]
2135
+ }
2136
+ )
2137
+ }
2138
+ ),
2039
2139
  formOverlay
2040
2140
  ] });
2041
2141
  }