How evanbacon/serve-sim works
Purpose and Entry Points
serve-sim hosts booted Apple Simulators for browser- and agent-driven interaction. Its primary CLI starts per-device streaming helpers and an optional preview server; command modes also issue direct simulator actions, query event logs, and manage camera injection. Primary entry points: packages/serve-sim/src/index.ts, packages/serve-sim/src/runtime.ts, packages/serve-sim/src/middleware.ts.
Process Topology and Device Lifecycle
The TypeScript runtime discovers/targets booted simulators, allocates ports, starts or connects to one helper per device, and persists helper state under $TMPDIR/serve-sim/. This state is the contract between detached helpers, CLI invocations, middleware, and UI. Multi-device session/device coordination is centered in packages/serve-sim/src/device-session.ts, packages/serve-sim/src/device.ts, packages/serve-sim/src/state.ts.
Native Streaming Pipeline
A bundled arm64 macOS Swift helper captures simulator frames through xcrun simctl io, transforms pixel buffers, encodes H.264 or JPEG/MJPEG, and exposes stream/control endpoints. The native module boundary is Swift/macOS frameworks rather than app instrumentation: packages/serve-sim/Sources/SimNative/sim-module.swift, packages/serve-sim/Sources/SimNative/CaptureEngine.swift, packages/serve-sim/Sources/SimNative/H264Encoder.swift. Runtime requirements are macOS, Apple Silicon, Xcode command-line tools/simctl, and Node 20+.
Browser Preview and Interaction
The React client renders simulator chrome, device frame, video, controls, tool panels, and multi-device layout. It consumes MJPEG or AVCC/H.264 streams and sends input over WebSockets. Key presentation/stream boundaries are packages/serve-sim/src/client/client.tsx, packages/serve-sim/src/client/simulator/SimulatorView.tsx, packages/serve-sim/src/client/simulator/useSimStream.ts. Browser actions are translated to HID/gesture, keyboard, rotation, and simulator commands through packages/serve-sim/src/native.ts, packages/serve-sim/src/text-to-keys.ts, packages/serve-sim/src/exec-ws.ts.
HTTP Middleware and Remote Exposure
simMiddleware embeds the same preview into Connect-compatible servers. It reads helper state and either directs clients to helper ports directly or proxies streams, control WebSockets, and DevTools through a single origin. This makes it suitable for Metro, Vite, Next, Express, or tunneled remote hosts. The server boundary is implemented in packages/serve-sim/src/middleware.ts, with port/state resolution in packages/serve-sim/src/ports.ts and packages/serve-sim/src/state.ts.
Accessibility, Logs, and DevTools
The system surfaces simulator accessibility snapshots, recent action/event logs, and WebKit inspection targets to browser tools and agents. Native accessibility extraction crosses into the TypeScript/UI layer; DevTools is exposed as a separately routed endpoint. Relevant components: packages/serve-sim/Sources/SimNative/AccessibilityBridge.swift, packages/serve-sim/src/ax.ts, packages/serve-sim/src/devtools-targets.ts.
Synthetic Camera Injection
Camera support is a separate host-helper/injected-dylib architecture. A macOS helper captures or generates BGRA frames from placeholder, file, or webcam sources into POSIX shared memory; a dylib injected with DYLD_INSERT_LIBRARIES swizzles AVFoundation in a selected simulator app to consume those frames. Its control socket permits source and mirror updates without relaunching the app. Core paths: packages/serve-sim/Sources/SimCameraHelper/main.m, packages/serve-sim/Sources/SimCameraInjector/SimCameraInjector.m, packages/serve-sim/Sources/SimCameraInjector/SimCamFrameSource.m.
Packaging and Agent Integration
The npm package builds TypeScript/browser bundles plus native artifacts, with development orchestration in packages/serve-sim/build.ts and packages/serve-sim/dev.ts. Agent-facing operating guidance ships as an Open Agent Skill rather than a runtime service: skills/serve-sim/SKILL.md, skills/serve-sim/README.md.
Main entry points: sim-module.swift Streaming helper · middleware.ts simMiddleware · runtime.ts Runtime orchestration · client.tsx React preview client
Components 22
TypeScript control plane
- CLI entry point · Starts helpers and runs direct simulator commands.
packages/serve-sim/src/index.ts - Runtime orchestration · Discovers booted devices, allocates ports, and manages helpers.
packages/serve-sim/src/runtime.ts - Device sessions · Coordinates multi-device sessions and targets.
packages/serve-sim/src/device-session.ts - Simulator device
packages/serve-sim/src/device.ts - Helper state · Persisted under $TMPDIR/serve-sim for detached process coordination.
packages/serve-sim/src/state.ts - simMiddleware · Embeds preview and proxies helper services behind one origin.
packages/serve-sim/src/middleware.ts - Port resolution
packages/serve-sim/src/ports.ts - Native actions · Translates browser actions into simulator commands.
packages/serve-sim/src/native.ts - Accessibility API
packages/serve-sim/src/ax.ts - DevTools targets · Exposes WebKit inspection targets.
packages/serve-sim/src/devtools-targets.ts
Native streaming
- Capture engine · Captures simulator frames through xcrun simctl io.
packages/serve-sim/Sources/SimNative/CaptureEngine.swift - Video encoder · Produces H.264 or JPEG/MJPEG streams.
packages/serve-sim/Sources/SimNative/H264Encoder.swift - Streaming helper · Exposes stream and control endpoints per simulator.
packages/serve-sim/Sources/SimNative/sim-module.swift - Accessibility bridge · Extracts simulator accessibility snapshots.
packages/serve-sim/Sources/SimNative/AccessibilityBridge.swift
Browser preview
- React preview client · Renders chrome, controls, tools, and multi-device layout.
packages/serve-sim/src/client/client.tsx - Simulator view
packages/serve-sim/src/client/simulator/SimulatorView.tsx - Stream client · Consumes MJPEG or AVCC/H.264 video.
packages/serve-sim/src/client/simulator/useSimStream.ts
Synthetic camera
- Camera host helper · Writes generated, file, or webcam BGRA frames to shared memory.
packages/serve-sim/Sources/SimCameraHelper/main.m - Injected camera dylib · Swizzles AVFoundation in the selected simulator app.
packages/serve-sim/Sources/SimCameraInjector/SimCameraInjector.m - Shared frame source
packages/serve-sim/Sources/SimCameraInjector/SimCamFrameSource.m
Other components
- Package build · Builds TypeScript, browser bundles, and native artifacts.
packages/serve-sim/build.ts - Agent skill guidance · Operating guidance for agents.
skills/serve-sim/SKILL.md
Connections 24
- CLI entry point → Runtime orchestration: starts
- Runtime orchestration → Device sessions: coordinates
- Device sessions → Simulator device: targets
- Runtime orchestration → Helper state: persists helper records
- Runtime orchestration → Streaming helper: starts or connects
- Streaming helper → Simulator device: captures and controls
- Capture engine → Video encoder: pixel buffers
- Video encoder → Streaming helper: video stream
- Accessibility bridge → Streaming helper: AX data
- simMiddleware → Helper state: reads
- simMiddleware → Port resolution: resolves
- simMiddleware → Streaming helper: proxies streams and control
- simMiddleware → DevTools targets: routes
- React preview client → Simulator view: renders
- Simulator view → Stream client: uses
- Stream client → Streaming helper: consumes video
- React preview client → Native actions: sends input
- Native actions → Streaming helper: control WebSocket
- Accessibility API → Accessibility bridge: reads snapshots
- Camera host helper → Shared frame source: writes shared frames
- Shared frame source → Injected camera dylib: supplies BGRA frames
- Injected camera dylib → Simulator device: injected into app
- Package build → Streaming helper: builds native artifact
- Package build → React preview client: builds bundle