openai-cua-sample-app

repository·main·Indexed 23 days ago

https://github.com/openai/openai-cua-sample-app

A TypeScript sample application demonstrating browser-focused computer-use (CUA) workflows using GPT-5.4. The project includes a web-based operator console, a backend runner for managing browser sessions, and shared packages for scenario orchestration. It supports two execution modes: 'native' (direct computer tool actions) and 'code' (Playwright JavaScript REPL). Included scenarios for testing agentic capabilities include booking-complete-reservation, kanban-reprioritize-sprint, and paint-draw-poster.

Tokens
14.1K
Snippets
19
Records
101
Agent score
82%

What's inside openai-cua-sample-app

  1. Overview of the Booking Lab Template

    main

    The Booking Lab Template is a run-scoped HTTP commerce lab designed to demonstrate specific agentic workflows. It provides the infrastructure for an agent to perform the following tasks:

    1. Filter inventory: Querying available products or services.
    2. Complete a reservation form: Interacting with commerce interfaces to submit data.
    3. Verify local confirmation: Checking that a reservation record was successfully created locally.

    The primary hero scenario implemented in this template is booking-complete-reservation.

  2. Understand the difference between `code` and `native` modes

    main

    When running scenarios, you can choose between two execution modes. The verification process remains identical for both because it inspects the final state of the lab rather than the agent's transcript.

    • code mode: Uses the browser REPL tool (exec_js) to drive the lab.
    • native mode: Uses the computer tool directly to interact with the environment.
  3. How the runtime flow works

    main

    The execution lifecycle follows this sequence:

    1. Discovery: The operator console (demo-web) requests the public scenario registry from the runner.
    2. Initialization: Starting a run triggers RunnerManager to create a mutable workspace and a replay bundle.
    3. Selection: RunnerManager selects the appropriate scenario executor via executor-registry.ts.
    4. Execution: The executor launches the lab and hands control to responses-loop.ts.
    5. Feedback Loop: The loop emits events, screenshots, and final verification results into the replay bundle.
    6. Observation: The web app reads run details and consumes SSE (Server-Sent Events) updates until the run completes.
  4. Understand the CUA Sample App package boundaries

    main

    The project is a TypeScript monorepo organized into specific functional packages. Understanding these boundaries is essential for knowing where to place new code:

    • packages/replay-schema: The source of truth for shared contracts. Use this to define scenario manifests, run requests/responses, replay bundle metadata, SSE event payloads, and structured runner errors. If an HTTP route or UI state is public, its shape must be defined here.
    • packages/scenario-kit: The public scenario registry. It contains scenario manifests and default prompts for the public labs (kanban, paint, and booking).
    • packages/browser-runtime: A thin Playwright abstraction. It handles browser launching, target resolution, state reading, and screenshot capture. It is intentionally decoupled from scenario prompts, verification, or the Responses API.
    • packages/runner-core: The orchestration engine. It manages mutable run workspaces, the run lifecycle, the Responses API loop, scenario executors, and verification. src/responses-loop.ts is the reference implementation for Responses API integration.
    • apps/runner: A thin Fastify HTTP layer providing the API surface (e.g., /api/runs, /api/runs/:id/events). Business logic should reside in runner-core, not here.
    • apps/demo-web: A Next.js operator console for scenario selection, run control, activity streaming, and screenshot scrubbing. It utilizes the useRunStream hook for UI updates.
  5. How Scenario Verification Works

    main

    Verification for scenarios is performed by comparing the final state of the lab against the requirements defined in the operator prompt. The verification logic is independent of the agent's execution mode (see Notes On Modes).

    Kanban Verification

    • Parses the target board state from the operator prompt.
    • Reads the live board state from the lab.
    • Ensures every card appears exactly once in the requested column and order.

    Paint Verification

    • Compares the saved checksum to the live canvas checksum using the live canvas grid and saved draft record.
    • Validates that the saved painted-cell count matches the live grid and that the result is not blank.

    Booking Verification

    • Parses the operator prompt into a booking request.
    • Checks applied filters in the UI.
    • Verifies the local confirmation record matches the requested hotel, guest, dates, and special requests.
  6. Understand the Execution Modes: native vs code

    main

    The sample app supports two distinct modes for interacting with the browser lab:

    • native: This mode exposes the Responses API computer tool directly. The model performs high-level computer actions like clicks, drags, typing, waits, and taking screenshots against a live browser session.
    • code: This mode exposes a persistent Playwright JavaScript REPL via the exec_js tool. Instead of raw computer actions, the model writes and executes JavaScript scripts to control the browser.

    Both modes utilize the same scenario manifests and the replay pipeline.

  7. Add a new scenario to the sample app

    main

    To add a new scenario, follow these steps across the package boundaries to ensure proper integration with the runner and UI:

    1. Create the lab template in labs/<name>-lab-template.
    2. Define the scenario manifest and default prompt in packages/scenario-kit/src.
    3. Export the manifest via packages/scenario-kit/src/scenarios.ts.
    4. Implement scenario instructions and verification helpers in packages/runner-core/src.
    5. Register the executor in packages/runner-core/src/executor-registry.ts.
    6. Update or add tests covering the manifest, runner behavior, and UI guidance.
  8. Run the services independently

    main

    If you need to view independent logs for the runner and the web console, you can start them in separate terminal sessions:

    • Runner: Use pnpm dev:runner.
    • Web App: Use RUNNER_BASE_URL=http://127.0.0.1:4001 pnpm dev:web (ensure the URL matches your runner's address).
    pnpm dev:runner
    RUNNER_BASE_URL=http://127.0.0.1:4001 pnpm dev:web
  9. Add a lab template

    main

    Lab templates must be self-contained and resettable. Follow these guidelines:

    • Asset Management: Keep all assets local to the specific template folder.
    • Verification: Expose stable browser-side accessors to facilitate verification.
    • Dependencies: Avoid network dependencies unless they are essential to the scenario story.
    • Observability: Ensure the initial state is easy to reason about from screenshots.