@gravity-platform/unoverse-react 0.0.2 → 0.0.4

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
@@ -6889,6 +6889,7 @@ var require_dist = __commonJS({
6889
6889
  // src/index.ts
6890
6890
  var index_exports = {};
6891
6891
  __export(index_exports, {
6892
+ IsolatedRoot: () => IsolatedRoot,
6892
6893
  StreamedUnoverseComponent: () => StreamedUnoverseComponent,
6893
6894
  StreamedUnoverseTemplate: () => StreamedUnoverseTemplate,
6894
6895
  UnoverseComponent: () => UnoverseComponent,
@@ -6903,7 +6904,7 @@ __export(index_exports, {
6903
6904
  module.exports = __toCommonJS(index_exports);
6904
6905
 
6905
6906
  // src/UnoverseComponent.tsx
6906
- var import_react3 = require("react");
6907
+ var import_react4 = require("react");
6907
6908
 
6908
6909
  // src/primitives.tsx
6909
6910
  var import_react = require("react");
@@ -7210,14 +7211,43 @@ function useUnoverseTheme(client, name = "light") {
7210
7211
  return theme;
7211
7212
  }
7212
7213
 
7213
- // src/UnoverseComponent.tsx
7214
+ // src/isolate.tsx
7215
+ var import_react3 = require("react");
7216
+ var import_react_dom = require("react-dom");
7214
7217
  var import_jsx_runtime3 = require("react/jsx-runtime");
7215
- function UnoverseComponent({ client, uri, data, onAction, theme }) {
7216
- const [def, setDef] = (0, import_react3.useState)(null);
7217
- const [error2, setError] = (0, import_react3.useState)(null);
7218
+ var RESET_CSS = `
7219
+ :host { all: initial; display: contents; }
7220
+ .uno-isolate-root { box-sizing: border-box; }
7221
+ .uno-isolate-root *, .uno-isolate-root *::before, .uno-isolate-root *::after { box-sizing: border-box; }
7222
+ .uno-isolate-root button, .uno-isolate-root input, .uno-isolate-root textarea, .uno-isolate-root select {
7223
+ font: inherit; letter-spacing: inherit; color: inherit; margin: 0;
7224
+ }
7225
+ `;
7226
+ function IsolatedRoot({ children }) {
7227
+ const hostRef = (0, import_react3.useRef)(null);
7228
+ const [shadow, setShadow] = (0, import_react3.useState)(null);
7229
+ (0, import_react3.useEffect)(() => {
7230
+ const host = hostRef.current;
7231
+ if (!host) return;
7232
+ setShadow(host.shadowRoot ?? host.attachShadow({ mode: "open" }));
7233
+ }, []);
7234
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { ref: hostRef, style: { display: "contents" }, children: shadow && (0, import_react_dom.createPortal)(
7235
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(import_jsx_runtime3.Fragment, { children: [
7236
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("style", { children: RESET_CSS }),
7237
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { className: "uno-isolate-root", children })
7238
+ ] }),
7239
+ shadow
7240
+ ) });
7241
+ }
7242
+
7243
+ // src/UnoverseComponent.tsx
7244
+ var import_jsx_runtime4 = require("react/jsx-runtime");
7245
+ function UnoverseComponent({ client, uri, data, onAction, theme, isolate = true }) {
7246
+ const [def, setDef] = (0, import_react4.useState)(null);
7247
+ const [error2, setError] = (0, import_react4.useState)(null);
7218
7248
  const fetched = useUnoverseTheme(client, "light");
7219
7249
  const activeTheme = theme ?? fetched;
7220
- (0, import_react3.useEffect)(() => {
7250
+ (0, import_react4.useEffect)(() => {
7221
7251
  let alive = true;
7222
7252
  setDef(null);
7223
7253
  setError(null);
@@ -7226,11 +7256,11 @@ function UnoverseComponent({ client, uri, data, onAction, theme }) {
7226
7256
  alive = false;
7227
7257
  };
7228
7258
  }, [client, uri]);
7229
- if (error2) return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("div", { children: [
7259
+ if (error2) return /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("div", { children: [
7230
7260
  "Unoverse error: ",
7231
7261
  error2
7232
7262
  ] });
7233
- if (!def || !activeTheme) return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("div", { children: [
7263
+ if (!def || !activeTheme) return /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("div", { children: [
7234
7264
  "Loading ",
7235
7265
  uri,
7236
7266
  "\u2026"
@@ -7238,26 +7268,27 @@ function UnoverseComponent({ client, uri, data, onAction, theme }) {
7238
7268
  const propDefaults2 = Object.fromEntries(
7239
7269
  Object.entries(def.props ?? {}).map(([k, v]) => [k, v?.default])
7240
7270
  );
7241
- const merged = { ...propDefaults2, ...data ?? {} };
7242
- return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(import_jsx_runtime3.Fragment, { children: [
7243
- /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("style", { children: keyframesCss(activeTheme) }),
7244
- /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { style: { display: "contents", ...activeTheme.root }, children: renderNode(def.root, merged, onAction, activeTheme) })
7271
+ const merged = data ?? propDefaults2;
7272
+ const body = /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(import_jsx_runtime4.Fragment, { children: [
7273
+ /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("style", { children: keyframesCss(activeTheme) }),
7274
+ /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("div", { style: { display: "contents", ...activeTheme.root }, children: renderNode(def.root, merged, onAction, activeTheme) })
7245
7275
  ] });
7276
+ return isolate ? /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(IsolatedRoot, { children: body }) : body;
7246
7277
  }
7247
7278
 
7248
7279
  // src/streamed.tsx
7249
- var import_react4 = require("react");
7280
+ var import_react5 = require("react");
7250
7281
  var import_unoverse_core2 = require("@gravity-platform/unoverse-core");
7251
- var import_jsx_runtime4 = require("react/jsx-runtime");
7282
+ var import_jsx_runtime5 = require("react/jsx-runtime");
7252
7283
  function useUnoverseInstance(store, chatId, nodeId) {
7253
- const subscribe = (0, import_react4.useCallback)((cb) => store.subscribe(cb), [store]);
7254
- const getVersion = (0, import_react4.useCallback)(() => store.getVersion(), [store]);
7255
- (0, import_react4.useSyncExternalStore)(subscribe, getVersion);
7284
+ const subscribe = (0, import_react5.useCallback)((cb) => store.subscribe(cb), [store]);
7285
+ const getVersion = (0, import_react5.useCallback)(() => store.getVersion(), [store]);
7286
+ (0, import_react5.useSyncExternalStore)(subscribe, getVersion);
7256
7287
  return { type: store.getType(chatId, nodeId), data: store.get(chatId, nodeId) };
7257
7288
  }
7258
7289
  function StreamedUnoverseComponent({ client, store, chatId, nodeId, onAction, theme, extraData }) {
7259
7290
  const { type, data } = useUnoverseInstance(store, chatId, nodeId);
7260
- const handleAction = (0, import_react4.useMemo)(
7291
+ const handleAction = (0, import_react5.useMemo)(
7261
7292
  () => (action, scope) => (0, import_unoverse_core2.dispatchAction)(action, scope, {
7262
7293
  store,
7263
7294
  chatId,
@@ -7266,14 +7297,14 @@ function StreamedUnoverseComponent({ client, store, chatId, nodeId, onAction, th
7266
7297
  }),
7267
7298
  [store, chatId, nodeId, onAction]
7268
7299
  );
7269
- if (!type) return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("div", { children: "waiting for COMPONENT_INIT\u2026" });
7300
+ if (!type) return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("div", { children: "waiting for COMPONENT_INIT\u2026" });
7270
7301
  const merged = extraData ? { ...data, ...extraData } : data;
7271
- return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(UnoverseComponent, { client, uri: `unoverse://components/${type}`, data: merged, onAction: handleAction, theme });
7302
+ return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(UnoverseComponent, { client, uri: `unoverse://components/${type}`, data: merged, onAction: handleAction, theme });
7272
7303
  }
7273
7304
 
7274
7305
  // src/template.tsx
7275
- var import_react5 = require("react");
7276
- var import_jsx_runtime5 = require("react/jsx-runtime");
7306
+ var import_react6 = require("react");
7307
+ var import_jsx_runtime6 = require("react/jsx-runtime");
7277
7308
  function propDefaults(props) {
7278
7309
  const out = {};
7279
7310
  for (const [k, v] of Object.entries(props ?? {})) {
@@ -7304,16 +7335,16 @@ function selectPointers(store, node) {
7304
7335
  if (sel.limit != null) pointers = pointers.slice(0, sel.limit);
7305
7336
  return pointers;
7306
7337
  }
7307
- function StreamedUnoverseTemplate({ client, store, uri, onAction, theme: themeProp, props }) {
7308
- const [def, setDef] = (0, import_react5.useState)(null);
7309
- const [error2, setError] = (0, import_react5.useState)(null);
7338
+ function StreamedUnoverseTemplate({ client, store, uri, onAction, theme: themeProp, props, isolate = true }) {
7339
+ const [def, setDef] = (0, import_react6.useState)(null);
7340
+ const [error2, setError] = (0, import_react6.useState)(null);
7310
7341
  const fetched = useUnoverseTheme(client, "light");
7311
7342
  const theme = themeProp ?? fetched;
7312
- (0, import_react5.useSyncExternalStore)(
7343
+ (0, import_react6.useSyncExternalStore)(
7313
7344
  (cb) => store.subscribe(cb),
7314
7345
  () => store.getVersion()
7315
7346
  );
7316
- (0, import_react5.useEffect)(() => {
7347
+ (0, import_react6.useEffect)(() => {
7317
7348
  let alive = true;
7318
7349
  setDef(null);
7319
7350
  setError(null);
@@ -7322,18 +7353,18 @@ function StreamedUnoverseTemplate({ client, store, uri, onAction, theme: themePr
7322
7353
  alive = false;
7323
7354
  };
7324
7355
  }, [client, uri]);
7325
- if (error2) return /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("div", { children: [
7356
+ if (error2) return /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)("div", { children: [
7326
7357
  "Unoverse error: ",
7327
7358
  error2
7328
7359
  ] });
7329
- if (!def || !theme) return /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("div", { children: [
7360
+ if (!def || !theme) return /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)("div", { children: [
7330
7361
  "Loading ",
7331
7362
  uri,
7332
7363
  "\u2026"
7333
7364
  ] });
7334
7365
  const leaf = (pointer, extraData) => {
7335
7366
  const { chatId, nodeId } = store.split(pointer);
7336
- return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(StreamedUnoverseComponent, { client, store, chatId, nodeId, onAction, theme, extraData }, pointer);
7367
+ return /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(StreamedUnoverseComponent, { client, store, chatId, nodeId, onAction, theme, extraData }, pointer);
7337
7368
  };
7338
7369
  const resolveSlot = (node, key) => {
7339
7370
  const pointers = selectPointers(store, node);
@@ -7343,7 +7374,7 @@ function StreamedUnoverseTemplate({ client, store, uri, onAction, theme: themePr
7343
7374
  return pointers.map((p) => leaf(p));
7344
7375
  };
7345
7376
  const resolveTimeline = (node, key) => {
7346
- return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("div", { style: styleToCss(node.style, theme), children: store.getTimeline().map((turn) => {
7377
+ return /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("div", { style: styleToCss(node.style, theme), children: store.getTimeline().map((turn) => {
7347
7378
  const sub = turn.role === "user" ? node.user : node.assistant;
7348
7379
  if (!sub) return null;
7349
7380
  const turnSlot = (slotNode, k) => {
@@ -7382,14 +7413,15 @@ function StreamedUnoverseTemplate({ client, store, uri, onAction, theme: themePr
7382
7413
  // button submits it. Lives in the store (shared state), not locally in Input.
7383
7414
  draft: store.getDraft()
7384
7415
  };
7385
- return /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)(import_jsx_runtime5.Fragment, { children: [
7386
- /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("style", { children: keyframesCss(theme) }),
7387
- /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("div", { style: { display: "contents", ...theme.root }, children: renderNode(def.root, rootData, onAction, theme, void 0, resolve) })
7416
+ const body = /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)(import_jsx_runtime6.Fragment, { children: [
7417
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("style", { children: keyframesCss(theme) }),
7418
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("div", { style: { display: "contents", ...theme.root }, children: renderNode(def.root, rootData, onAction, theme, void 0, resolve) })
7388
7419
  ] });
7420
+ return isolate ? /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(IsolatedRoot, { children: body }) : body;
7389
7421
  }
7390
7422
 
7391
7423
  // src/connection.tsx
7392
- var import_react6 = require("react");
7424
+ var import_react7 = require("react");
7393
7425
  var import_unoverse_core3 = require("@gravity-platform/unoverse-core");
7394
7426
 
7395
7427
  // ../node_modules/zod/v4/core/core.js
@@ -17614,11 +17646,11 @@ function useUnoverseConnection(config2, session, store, options = {}) {
17614
17646
  const { apiUrl, streamUrl, getAccessToken } = config2;
17615
17647
  const { workflowId, targetTriggerNode, userId, conversationId } = session;
17616
17648
  const chatId = session.chatId ?? conversationId;
17617
- const [isConnected, setIsConnected] = (0, import_react6.useState)(false);
17618
- const [isReady, setIsReady] = (0, import_react6.useState)(false);
17619
- const clientRef = (0, import_react6.useRef)(null);
17620
- const seenRef = (0, import_react6.useRef)(/* @__PURE__ */ new Set());
17621
- (0, import_react6.useEffect)(() => {
17649
+ const [isConnected, setIsConnected] = (0, import_react7.useState)(false);
17650
+ const [isReady, setIsReady] = (0, import_react7.useState)(false);
17651
+ const clientRef = (0, import_react7.useRef)(null);
17652
+ const seenRef = (0, import_react7.useRef)(/* @__PURE__ */ new Set());
17653
+ (0, import_react7.useEffect)(() => {
17622
17654
  if (!enabled) return;
17623
17655
  if (!workflowId || !userId || !conversationId) {
17624
17656
  console.warn("[unoverse] connection missing workflowId/userId/conversationId");
@@ -17673,7 +17705,7 @@ function useUnoverseConnection(config2, session, store, options = {}) {
17673
17705
  clientRef.current = null;
17674
17706
  };
17675
17707
  }, [enabled, apiUrl, streamUrl, workflowId, targetTriggerNode, userId, conversationId, chatId, getAccessToken, store]);
17676
- const trigger = (0, import_react6.useCallback)(
17708
+ const trigger = (0, import_react7.useCallback)(
17677
17709
  (input) => {
17678
17710
  (async () => {
17679
17711
  try {
@@ -17696,7 +17728,7 @@ function useUnoverseConnection(config2, session, store, options = {}) {
17696
17728
  },
17697
17729
  [apiUrl, workflowId, targetTriggerNode, userId, conversationId, chatId, getAccessToken]
17698
17730
  );
17699
- const sendMessage = (0, import_react6.useCallback)(
17731
+ const sendMessage = (0, import_react7.useCallback)(
17700
17732
  (text) => {
17701
17733
  const message = text.trim();
17702
17734
  if (!message) return;
@@ -17705,13 +17737,13 @@ function useUnoverseConnection(config2, session, store, options = {}) {
17705
17737
  },
17706
17738
  [chatId, store, trigger]
17707
17739
  );
17708
- const sendAction = (0, import_react6.useCallback)(
17740
+ const sendAction = (0, import_react7.useCallback)(
17709
17741
  (action, data) => {
17710
17742
  void clientRef.current?.callTool({ name: "user_action", arguments: { action, data, userId, conversationId, chatId } }).catch((e) => console.error("[unoverse] user_action failed", e));
17711
17743
  },
17712
17744
  [userId, conversationId, chatId]
17713
17745
  );
17714
- const activeTemplate = (0, import_react6.useSyncExternalStore)(
17746
+ const activeTemplate = (0, import_react7.useSyncExternalStore)(
17715
17747
  (cb) => store.subscribe(cb),
17716
17748
  () => store.getActiveTemplate().name
17717
17749
  );
@@ -17719,6 +17751,7 @@ function useUnoverseConnection(config2, session, store, options = {}) {
17719
17751
  }
17720
17752
  // Annotate the CommonJS export names for ESM import in node:
17721
17753
  0 && (module.exports = {
17754
+ IsolatedRoot,
17722
17755
  StreamedUnoverseComponent,
17723
17756
  StreamedUnoverseTemplate,
17724
17757
  UnoverseComponent,