Skip to content

The agent that grows with you

1.5 KB
Raw Blame
/**
 * OVERLAY NUDGE — the one way to say a stage to an overlay that is ALREADY on
 * the page.
 *
 * The act pipeline (preview-act.ts) injects the whole engine because it has to:
 * it needs to resolve a ref, measure a node and read the page back. The stages
 * that only narrate — the idle pulse, the read wipe — need none of that. They
 * are one function call against something the last action already left on the
 * page, so re-shipping the engine to make it would put the payload on the wire
 * on every turn boundary.
 *
 * A page the agent has never acted on has no overlay, and the nudge is a no-op
 * there. That is the right answer rather than a gap: chrome on a page the agent
 * never touched would be a lie about what it did.
 */

import type { WatchStage } from '@/lib/preview-act/watch-in-page'
import type { PreviewOwner } from '@/store/preview-ownership'

import { activePreviewScriptRunner } from './preview-script-runner'

/** Run one stage against the overlay of the active pane `owner` (omitted =
 *  the focused session) sees, if it has one. */
export function nudgeOverlay(stage: WatchStage, owner?: PreviewOwner): void {
  const run = activePreviewScriptRunner(owner)

  if (!run) {
    return
  }

  void run(`(function () {
  var w = window;
  var fn = w.__hermesWatch_fn;
  if (!fn) { return 'cold'; }
  try { fn(document, w.__hermesActHolder || {}, ${JSON.stringify(stage)}); } catch (err) {}
  return 'ok';
})()`).catch(() => {
    // The page navigated out from under us. The next action re-injects.
  })
}