Skip to content

Automate browser based workflows with AI

3.7 KB
Raw Blame
import {
  EXTERNAL_BROWSER_DIAGNOSTIC,
  StreamStatusPanel,
} from "@/routes/streaming/StreamDiagnostics";
import type { StreamStateChangeHandler } from "@/routes/streaming/streamState";
import type { Status, WorkflowRunRetryFields } from "@/api/types";
import {
  getRunAttemptKey,
  runIsRetryWaiting,
} from "../../workflowRun/runRetryState";
import { useEffect, useState } from "react";

import { BrowserStream } from "@/components/BrowserStream";
import { useStreamTransport } from "@/hooks/useRuntimeConfig";
import { BrowserSessionStream } from "@/routes/browserSessions/BrowserSessionStream";

import { WorkflowRunStream } from "../../workflowRun/WorkflowRunStream";

type RunLiveStreamProps = {
  workflowRunId: string;
  run?: { workflow_run_id: string; status: Status } & WorkflowRunRetryFields;
  browserSessionId: string | null;
  interactive: boolean;
  // Live page URL from the CDP frames; the VNC path doesn't surface one yet.
  onUrlChange?: (url: string) => void;
  onStreamStateChange?: StreamStateChangeHandler;
};

/**
 * Live browser for a workflow run, mirroring WorkflowRunOverview: VNC keyed by the
 * browser session, with the session's CDP screencast when VNC is wrong or closes early.
 */
export function RunLiveStream(props: RunLiveStreamProps) {
  if (props.run && runIsRetryWaiting(props.run)) {
    return (
      <StreamStatusPanel
        diagnostic={{
          title: "Retry pending",
          detail: "The browser reconnects when the next attempt starts.",
          pending: true,
        }}
      />
    );
  }
  return (
    <RunLiveStreamTransport
      key={props.run ? getRunAttemptKey(props.run) : props.workflowRunId}
      {...props}
    />
  );
}

function RunLiveStreamTransport({
  workflowRunId,
  browserSessionId,
  interactive,
  onUrlChange,
  onStreamStateChange,
}: RunLiveStreamProps) {
  const { streamTransport, isExternal } = useStreamTransport(browserSessionId);
  const [vncFailed, setVncFailed] = useState(false);

  useEffect(() => {
    setVncFailed(false);
  }, [browserSessionId]);

  useEffect(() => {
    if (browserSessionId && isExternal) {
      onStreamStateChange?.("stopped", browserSessionId);
    }
  }, [browserSessionId, isExternal, onStreamStateChange]);

  if (browserSessionId) {
    if (!streamTransport) {
      // Defaulting to VNC here opens a socket an externally hosted session cannot serve, and the
      // failure is what swaps the transport — a visible drop rather than a first paint.
      return null;
    }

    if (isExternal) {
      return <StreamStatusPanel diagnostic={EXTERNAL_BROWSER_DIAGNOSTIC} />;
    }

    if (streamTransport !== "cdp" && !vncFailed) {
      return (
        <BrowserStream
          key={browserSessionId}
          browserSessionId={browserSessionId}
          interactive={interactive}
          showControlButtons={interactive}
          onClose={() => setVncFailed(true)}
          onStreamStateChange={onStreamStateChange}
        />
      );
    }

    // The per-run stream is fed only by display capture on the worker, which stops as soon as a
    // run has a browser_session_id — so a session-backed run must stream the session itself.
    return (
      <BrowserSessionStream
        key={browserSessionId}
        browserSessionId={browserSessionId}
        interactive={interactive}
        showControlButtons={interactive}
        onUrlChange={onUrlChange}
        onStreamStateChange={onStreamStateChange}
        centered
      />
    );
  }

  return (
    <WorkflowRunStream
      workflowRunId={workflowRunId}
      alwaysShowStream
      interactive={interactive}
      showControlButtons={interactive}
      onUrlChange={onUrlChange}
      onStreamStateChange={onStreamStateChange}
      centered
    />
  );
}