@qontinui/ui-bridge-native 0.5.1 → 0.6.1

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.js CHANGED
@@ -256,7 +256,18 @@ var NativeUIBridgeRegistry = class {
256
256
  return Array.from(this.elements.values());
257
257
  }
258
258
  /**
259
- * Get all registered elements that are visible and have a layout
259
+ * Get all registered elements that are visible AND have a measured layout.
260
+ *
261
+ * This is the *strict* visibility filter: requires `state.visible === true`
262
+ * AND `state.layout !== null`. Use this when you need both signals
263
+ * (e.g. coord-based tap, where you need an actual rect to hit-test against).
264
+ *
265
+ * Callers driving an LLM agent typically want
266
+ * {@link getMountedVisibleElements} instead — that variant accepts
267
+ * elements that *should* be on screen (visible:true) but whose first
268
+ * `onLayout` has not yet fired. Without it, a snapshot taken in the gap
269
+ * between mount and the first layout pass returns 0 elements on a fully
270
+ * populated screen.
260
271
  */
261
272
  getVisibleElements() {
262
273
  return this.getAllElements().filter((e) => {
@@ -264,6 +275,26 @@ var NativeUIBridgeRegistry = class {
264
275
  return state.visible && state.layout !== null;
265
276
  });
266
277
  }
278
+ /**
279
+ * Get all registered elements marked as visible (regardless of layout).
280
+ *
281
+ * Looser than {@link getVisibleElements}: includes elements whose
282
+ * `onLayout` callback has not yet fired (so `layout === null`). React
283
+ * Native's `onLayout` is async — there's a one-tick window after mount
284
+ * where elements are in the tree, the user *sees* them, but the
285
+ * registry hasn't received their measurements yet. Excluding those
286
+ * elements makes `getSnapshot?visibleOnly=true` return 0 on a populated
287
+ * screen, which is a worse failure mode than including a few elements
288
+ * we can't yet locate spatially.
289
+ *
290
+ * Used by `createSnapshot`'s `visibleOnly` filter and by the
291
+ * `getSnapshot` / `getElements` handlers. Each emitted element carries a
292
+ * `visibility` discriminator (`"visible"` / `"likely-visible"`) so
293
+ * agents that *do* require a known rect can branch downstream.
294
+ */
295
+ getMountedVisibleElements() {
296
+ return this.getAllElements().filter((e) => e.getState().visible);
297
+ }
267
298
  /**
268
299
  * Get elements registered on a specific route (for page-scoped filtering)
269
300
  */
@@ -538,7 +569,7 @@ var NativeUIBridgeRegistry = class {
538
569
  }
539
570
  return { currentRoute: null, segments: void 0 };
540
571
  })();
541
- let elements = options?.visibleOnly ? this.getVisibleElements() : this.getAllElements();
572
+ let elements = options?.visibleOnly ? this.getMountedVisibleElements() : this.getAllElements();
542
573
  if (options?.currentRouteOnly && resolvedRoute.currentRoute) {
543
574
  const currentRoute = resolvedRoute.currentRoute;
544
575
  elements = elements.filter((e) => e.registrationRoute === currentRoute);
@@ -547,16 +578,19 @@ var NativeUIBridgeRegistry = class {
547
578
  timestamp: Date.now(),
548
579
  elements: elements.map((e) => {
549
580
  const handlers = extractHandlerNames(e.props);
581
+ const state = e.getState();
582
+ const visibility = !state.visible ? "hidden" : state.layout !== null ? "visible" : "likely-visible";
550
583
  return {
551
584
  id: e.id,
552
585
  type: e.type,
553
586
  label: e.label,
554
587
  identifier: e.getIdentifier(),
555
- state: e.getState(),
588
+ state,
556
589
  actions: e.actions,
557
590
  customActions: e.customActions ? Object.keys(e.customActions) : void 0,
558
591
  registeredHandlers: handlers.length > 0 ? handlers : void 0,
559
- registrationRoute: e.registrationRoute
592
+ registrationRoute: e.registrationRoute,
593
+ visibility
560
594
  };
561
595
  }),
562
596
  components: this.getAllComponents().map((c) => ({
@@ -1950,22 +1984,25 @@ function createServerHandlers(registry, executor, config) {
1950
1984
  getElements: async (ctx) => {
1951
1985
  const visibleOnly = ctx.query?.visibleOnly === "true" || ctx.body?.visibleOnly === true;
1952
1986
  const forRoute = ctx.query?.route || ctx.body?.route;
1953
- let allElements = visibleOnly ? registry.getVisibleElements() : registry.getAllElements();
1987
+ let allElements = visibleOnly ? registry.getMountedVisibleElements() : registry.getAllElements();
1954
1988
  if (forRoute) {
1955
1989
  allElements = allElements.filter((e) => e.registrationRoute === forRoute);
1956
1990
  }
1957
1991
  const elements = allElements.map((e) => {
1958
1992
  const handlers = extractHandlerNames(e.props);
1993
+ const state = e.getState();
1994
+ const visibility = !state.visible ? "hidden" : state.layout !== null ? "visible" : "likely-visible";
1959
1995
  return {
1960
1996
  id: e.id,
1961
1997
  type: e.type,
1962
1998
  label: e.label,
1963
1999
  identifier: e.getIdentifier(),
1964
- state: e.getState(),
2000
+ state,
1965
2001
  actions: e.actions,
1966
2002
  customActions: e.customActions ? Object.keys(e.customActions) : void 0,
1967
2003
  registeredHandlers: handlers.length > 0 ? handlers : void 0,
1968
- registrationRoute: e.registrationRoute
2004
+ registrationRoute: e.registrationRoute,
2005
+ visibility
1969
2006
  };
1970
2007
  });
1971
2008
  return success2({ elements });
@@ -2228,6 +2265,37 @@ function createServerHandlers(registry, executor, config) {
2228
2265
  "NOT_SUPPORTED"
2229
2266
  );
2230
2267
  },
2268
+ // AI helpers — runner-only endpoints that have no React Native analog.
2269
+ //
2270
+ // The mobile bridge mounts explicit NOT_SUPPORTED stubs at the runner's
2271
+ // cheatsheet paths so callers (and the /manual-test skill in particular)
2272
+ // get a structured envelope explaining the gap rather than a confusing
2273
+ // HTTP 404. Each error message names an in-tree mobile replacement so
2274
+ // operators aren't left guessing what to use instead.
2275
+ aiForms: async () => {
2276
+ return error2(
2277
+ 'ai/forms (form discovery) is runner-only; mobile React Native has no DOM to walk. Use GET /ui-bridge/control/snapshot and filter for type === "input" instead.',
2278
+ "NOT_SUPPORTED"
2279
+ );
2280
+ },
2281
+ aiIdleStatus: async () => {
2282
+ return error2(
2283
+ 'ai/idle-status (page-idle signal) is runner-only; mobile React Native has no DOM-mutation / network-idle signals. Use the registry event stream (WS subscribe to "element:registered"/"element:stateChanged") or poll /control/snapshot for stability.',
2284
+ "NOT_SUPPORTED"
2285
+ );
2286
+ },
2287
+ aiChangeBufferEnable: async () => {
2288
+ return error2(
2289
+ 'ai/change-buffer (DOM mutation diff buffer) is runner-only; mobile React Native has no DOM. Use the WS event bridge (subscribe to "element:registered"/"element:unregistered"/"element:stateChanged") for change notifications.',
2290
+ "NOT_SUPPORTED"
2291
+ );
2292
+ },
2293
+ aiWaitForElement: async () => {
2294
+ return error2(
2295
+ 'POST /ai/wait-for-element is runner-only on mobile. Use the WS "waitForElement" JSON-RPC method or "waitForCondition" instead \u2014 same semantics, no HTTP polling overhead.',
2296
+ "NOT_SUPPORTED"
2297
+ );
2298
+ },
2231
2299
  // Meta / Introspection (stub — server constructor overrides with real route table)
2232
2300
  getMethods: async () => {
2233
2301
  return error2("Methods introspection not configured", "NOT_SUPPORTED");
@@ -2883,6 +2951,24 @@ var WS_ROUTES = [
2883
2951
  { pattern: "meta/methods", handler: "getMethods", httpMethods: ["GET"] },
2884
2952
  // AI helpers — mobile parity with the runner's `/ui-bridge/ai/*` family.
2885
2953
  { pattern: "ai/fill-form", handler: "fillForm", httpMethods: ["POST"] },
2954
+ // AI helpers — NOT_SUPPORTED stubs. These endpoints exist on the runner-side
2955
+ // UI Bridge surface (and the /manual-test skill's cheatsheet documents them
2956
+ // as if they're universally available). Mobile React Native has no DOM, so
2957
+ // the mobile bridge mounts explicit stubs so callers get a structured
2958
+ // NOT_SUPPORTED envelope (with a hint pointing at the mobile-native
2959
+ // replacement) instead of a confusing HTTP 404.
2960
+ { pattern: "ai/forms", handler: "aiForms", httpMethods: ["GET"] },
2961
+ { pattern: "ai/idle-status", handler: "aiIdleStatus", httpMethods: ["GET"] },
2962
+ {
2963
+ pattern: "ai/change-buffer/enable",
2964
+ handler: "aiChangeBufferEnable",
2965
+ httpMethods: ["POST"]
2966
+ },
2967
+ {
2968
+ pattern: "ai/wait-for-element",
2969
+ handler: "aiWaitForElement",
2970
+ httpMethods: ["POST"]
2971
+ },
2886
2972
  // Coord-based tap — synthesizes a press at the given screen coords by
2887
2973
  // matching against registered elements' layout rects.
2888
2974
  { pattern: "control/tap", handler: "tapAt", httpMethods: ["POST"] },
@@ -2955,6 +3041,10 @@ var HANDLER_DESCRIPTIONS = {
2955
3041
  diffBaseline: "Diff current UI against a saved baseline",
2956
3042
  getMethods: "List every handler name (legacy introspection)",
2957
3043
  fillForm: "Fill multiple inputs in one call: body { fields: [{elementId, value}] } \u2014 each field dispatches `type` + `clear:true`",
3044
+ aiForms: 'STUB \u2014 runner-only; mobile returns NOT_SUPPORTED. Use /control/snapshot + filter type==="input" instead.',
3045
+ aiIdleStatus: "STUB \u2014 runner-only; mobile returns NOT_SUPPORTED. Subscribe to registry events over WS for change signals.",
3046
+ aiChangeBufferEnable: "STUB \u2014 runner-only; mobile returns NOT_SUPPORTED. Use WS event subscriptions for change notifications.",
3047
+ aiWaitForElement: "STUB \u2014 runner-only; mobile returns NOT_SUPPORTED. Use the WS waitForElement / waitForCondition JSON-RPC methods.",
2958
3048
  tapAt: "Synthesize a press at given screen coords by matching registered element layout rects: body { x, y, action? }",
2959
3049
  getConsoleErrors: "TEST HOOK \u2014 last-N console.error/console.warn entries; ?since=<ms>&limit=<n>",
2960
3050
  getNetworkRequests: "TEST HOOK \u2014 last-N fetch/XHR entries with status + duration; ?since=<ms>&limit=<n>",
@@ -5075,6 +5165,24 @@ function flattenStateStyles(stateStyles) {
5075
5165
  if (stateStyles.disabled != null) result.disabled = flattenStyle(stateStyles.disabled);
5076
5166
  return Object.keys(result).length > 0 ? result : void 0;
5077
5167
  }
5168
+ var warnedDeadButtonIds = /* @__PURE__ */ new Set();
5169
+ function scheduleDeadButtonWarning(bridge, id, type) {
5170
+ if (type !== "button" && type !== "pressable") return;
5171
+ if (warnedDeadButtonIds.has(id)) return;
5172
+ if (!bridge) return;
5173
+ setTimeout(() => {
5174
+ if (warnedDeadButtonIds.has(id)) return;
5175
+ const element = bridge.registry.getElement(id);
5176
+ if (!element) return;
5177
+ const props = element.props ?? {};
5178
+ const hasPress = typeof props.onPress === "function" || typeof props.onPressIn === "function" || typeof props.onResponderRelease === "function";
5179
+ if (hasPress) return;
5180
+ warnedDeadButtonIds.add(id);
5181
+ console.warn(
5182
+ `[UI Bridge] Registered \`${type}\` element "${id}" without an \`onPress\` handler \u2014 UI Bridge \`press\` actions on this element will fail with "No press handler found on element". Use \`useUIElementWithProps\` and \`captureProps({onPress})\` instead.`
5183
+ );
5184
+ }, 0);
5185
+ }
5078
5186
  function useUIElement(options) {
5079
5187
  const bridge = useUIBridgeNativeOptional();
5080
5188
  const ref = react.useRef(null);
@@ -5121,6 +5229,7 @@ function useUIElement(options) {
5121
5229
  registeredRef.current = true;
5122
5230
  registeredIdRef.current = id;
5123
5231
  setRegistered(true);
5232
+ scheduleDeadButtonWarning(bridge, id, type);
5124
5233
  }, [bridge, id, type, label, actions, customActions, treePath, style, stateStylesProp]);
5125
5234
  const unregister = react.useCallback(() => {
5126
5235
  if (!bridge || !registeredRef.current) return;
@@ -5180,30 +5289,42 @@ function useUIElement(options) {
5180
5289
  const node = ref.current;
5181
5290
  if (!node || typeof node.measureInWindow !== "function") return;
5182
5291
  let cancelled = false;
5183
- node.measureInWindow((pageX, pageY, w, h) => {
5184
- if (cancelled) return;
5185
- if (w <= 0 || h <= 0) return;
5186
- const newLayout = {
5187
- x: pageX,
5188
- y: pageY,
5189
- width: w,
5190
- height: h,
5191
- pageX,
5192
- pageY
5193
- };
5194
- setLayout(newLayout);
5195
- const newState = {
5196
- mounted: true,
5197
- visible: true,
5198
- enabled: true,
5199
- focused: false,
5200
- layout: newLayout
5201
- };
5202
- bridge.registry.updateElementState(id, newState);
5203
- onStateChangeRef.current?.(newState);
5204
- });
5292
+ let pendingTimer = null;
5293
+ const backoffMs = [50, 200, 500, 1e3, 2500];
5294
+ let attempt = 0;
5295
+ const tryMeasure = () => {
5296
+ if (cancelled || !node.measureInWindow) return;
5297
+ node.measureInWindow((pageX, pageY, w, h) => {
5298
+ if (cancelled) return;
5299
+ if (w > 0 && h > 0) {
5300
+ const newLayout = {
5301
+ x: pageX,
5302
+ y: pageY,
5303
+ width: w,
5304
+ height: h,
5305
+ pageX,
5306
+ pageY
5307
+ };
5308
+ setLayout(newLayout);
5309
+ const newState = {
5310
+ mounted: true,
5311
+ visible: true,
5312
+ enabled: true,
5313
+ focused: false,
5314
+ layout: newLayout
5315
+ };
5316
+ bridge.registry.updateElementState(id, newState);
5317
+ onStateChangeRef.current?.(newState);
5318
+ return;
5319
+ }
5320
+ if (attempt >= backoffMs.length) return;
5321
+ pendingTimer = setTimeout(tryMeasure, backoffMs[attempt++]);
5322
+ });
5323
+ };
5324
+ tryMeasure();
5205
5325
  return () => {
5206
5326
  cancelled = true;
5327
+ if (pendingTimer != null) clearTimeout(pendingTimer);
5207
5328
  };
5208
5329
  }, [bridge, registered, id]);
5209
5330
  const registeredHandlerKeysRef = react.useRef([]);
@@ -5696,6 +5817,31 @@ var UI_BRIDGE_NATIVE_ROUTES = {
5696
5817
  path: "/ui-bridge/ai/fill-form",
5697
5818
  description: "Fill multiple input elements in a single call"
5698
5819
  },
5820
+ // AI helpers (NOT_SUPPORTED stubs) — these endpoints exist on the runner-side
5821
+ // UI Bridge surface but have no analog on mobile React Native. The mobile
5822
+ // bridge registers explicit stubs so callers get a structured NOT_SUPPORTED
5823
+ // envelope instead of a confusing HTTP 404. The mobile bridge's snapshot +
5824
+ // `/control/find` already cover what an operator would reach for these for.
5825
+ AI_FORMS: {
5826
+ method: "GET",
5827
+ path: "/ui-bridge/ai/forms",
5828
+ description: "Discover forms (runner-only; mobile returns NOT_SUPPORTED)"
5829
+ },
5830
+ AI_IDLE_STATUS: {
5831
+ method: "GET",
5832
+ path: "/ui-bridge/ai/idle-status",
5833
+ description: "Get page-idle signal (runner-only; mobile returns NOT_SUPPORTED)"
5834
+ },
5835
+ AI_CHANGE_BUFFER_ENABLE: {
5836
+ method: "POST",
5837
+ path: "/ui-bridge/ai/change-buffer/enable",
5838
+ description: "Enable DOM change buffer (runner-only; mobile returns NOT_SUPPORTED)"
5839
+ },
5840
+ AI_WAIT_FOR_ELEMENT: {
5841
+ method: "POST",
5842
+ path: "/ui-bridge/ai/wait-for-element",
5843
+ description: "Wait for element predicate via HTTP (runner-only; mobile returns NOT_SUPPORTED \u2014 use WS waitForElement instead)"
5844
+ },
5699
5845
  // Test hooks — drive ModalDetector state from outside the React tree
5700
5846
  // (gated by `features.testHooks`). Mirrors `pushModal` / `dismissModal`
5701
5847
  // calls that components normally make in-process.