The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { createCanvasFrameSink } from "@t3tools/client-runtime/device/frame"; |
| 2 | import type { DuoControlState } from "@t3tools/client-runtime/device/duo-control"; |
| 3 | import { |
| 4 | createDeviceStreamClient, |
| 5 | type DeviceScreenSize, |
| 6 | type DeviceStreamClient, |
| 7 | type DeviceStreamStatus, |
| 8 | } from "@t3tools/client-runtime/device/stream"; |
| 9 | import type { DeviceHubAccess } from "@t3tools/client-runtime/state/deviceHubAccess"; |
| 10 | import type { DevicePlatform, EnvironmentId } from "@t3tools/contracts"; |
| 11 | import { Atom } from "effect/reactivity"; |
| 12 | |
| 13 | import { m } from "~/i18n/messages"; |
| 14 | import { refreshDeviceHubAccess } from "~/state/device"; |
| 15 | |
| 16 | export interface DeviceStreamState { |
| 17 | readonly status: DeviceStreamStatus; |
| 18 | readonly detail: string | undefined; |
| 19 | /** A frame arrived during this session, so a reconnect can keep showing it. */ |
| 20 | readonly hasFrame: boolean; |
| 21 | readonly screen: DeviceScreenSize | null; |
| 22 | readonly mjpegUrl: string | null; |
| 23 | readonly mjpegGeneration: number; |
| 24 | readonly input: { readonly connected: boolean; readonly detail?: string }; |
| 25 | readonly duoControl: DuoControlState; |
| 26 | readonly phoneUnavailable: boolean; |
| 27 | } |
| 28 | |
| 29 | export interface DeviceStreamSession { |
| 30 | readonly stream: DeviceStreamClient; |
| 31 | /** The stream draws here; the view mounts it and the 3D viewers sample it. */ |
| 32 | readonly canvas: HTMLCanvasElement; |
| 33 | readonly setFrameListener: (listener: (() => void) | null) => void; |
| 34 | /** Called when the hub rejects a Duo control, so the 3D viewer drops its optimistic pose. */ |
| 35 | readonly setControlRejectedListener: (listener: (() => void) | null) => void; |
| 36 | readonly markPhoneUnavailable: () => void; |
| 37 | readonly reconnect: () => void; |
| 38 | readonly state: DeviceStreamState; |
| 39 | } |
| 40 | |
| 41 | export interface DeviceStreamKey { |
| 42 | readonly environmentId: EnvironmentId; |
| 43 | readonly hostId: string; |
| 44 | readonly platform: DevicePlatform; |
| 45 | readonly deviceId: string; |
| 46 | readonly access: DeviceHubAccess; |
| 47 | /** See `DeviceStreamTarget.serveSimOnly`. */ |
| 48 | readonly serveSimOnly: boolean; |
| 49 | /** Each mounted view owns its own client: one canvas cannot be in two places. */ |
| 50 | readonly viewId: string; |
| 51 | } |
| 52 | |
| 53 | export const IDLE_DEVICE_STREAM_STATE: DeviceStreamState = { |
| 54 | status: "connecting", |
| 55 | detail: undefined, |
| 56 | hasFrame: false, |
| 57 | screen: null, |
| 58 | mjpegUrl: null, |
| 59 | mjpegGeneration: 0, |
| 60 | input: { connected: false }, |
| 61 | duoControl: { pending: false, requested: null, error: null }, |
| 62 | phoneUnavailable: false, |
| 63 | }; |
| 64 | |
| 65 | export const deviceScreenKey = (environmentId: EnvironmentId, hostId: string, deviceId: string) => |
| 66 | `${environmentId}\u0000${hostId}\u0000${deviceId}`; |
| 67 | |
| 68 | /** |
| 69 | * The screen one view's stream last reported, null while that view streams nothing. |
| 70 | * Keyed by view, not device: two views of one device stream independently, and one |
| 71 | * ending must not clear the other's size. The mini player lays out its frame from it. |
| 72 | */ |
| 73 | export const deviceStreamScreenAtom = Atom.family((viewId: string) => |
| 74 | Atom.make<DeviceScreenSize | null>(null).pipe(Atom.withLabel(`device-stream-screen:${viewId}`)), |
| 75 | ); |
| 76 | |
| 77 | /** A device's video and input connection, live while a view reads it. */ |
| 78 | export const deviceStreamSessionAtom = Atom.family((key: DeviceStreamKey) => |
| 79 | Atom.make((get): DeviceStreamSession => { |
| 80 | const screenAtom = deviceStreamScreenAtom(key.viewId); |
| 81 | const canvas = document.createElement("canvas"); |
| 82 | canvas.style.display = "block"; |
| 83 | canvas.style.width = "100%"; |
| 84 | canvas.style.height = "100%"; |
| 85 | let frameListener: (() => void) | null = null; |
| 86 | let controlRejectedListener: (() => void) | null = null; |
| 87 | let state = IDLE_DEVICE_STREAM_STATE; |
| 88 | let session: DeviceStreamSession; |
| 89 | const update = (patch: Partial<DeviceStreamState>) => { |
| 90 | state = { ...state, ...patch }; |
| 91 | session = { ...session, state }; |
| 92 | get.setSelf(session); |
| 93 | }; |
| 94 | const client = createDeviceStreamClient( |
| 95 | { |
| 96 | platform: key.platform, |
| 97 | deviceId: key.deviceId, |
| 98 | access: key.access, |
| 99 | serveSimOnly: key.serveSimOnly, |
| 100 | }, |
| 101 | createCanvasFrameSink(canvas, () => frameListener?.()), |
| 102 | { |
| 103 | onDuoControl: (duoControl) => { |
| 104 | if (duoControl.error && duoControl.error !== state.duoControl.error) |
| 105 | controlRejectedListener?.(); |
| 106 | update({ duoControl }); |
| 107 | }, |
| 108 | onDuoUnavailable: () => update({ phoneUnavailable: true }), |
| 109 | onStatus: (status, detail) => |
| 110 | update({ status, detail, ...(status === "streaming" ? { hasFrame: true } : {}) }), |
| 111 | onScreen: (screen) => { |
| 112 | get.set(screenAtom, screen); |
| 113 | update({ screen }); |
| 114 | }, |
| 115 | onUnauthorized: () => refreshDeviceHubAccess(key.environmentId), |
| 116 | onMjpegFallback: (mjpegUrl) => |
| 117 | update({ mjpegUrl, mjpegGeneration: state.mjpegGeneration + 1 }), |
| 118 | onCanvasVideo: () => update({ mjpegUrl: null }), |
| 119 | onInputConnected: (connected, detail) => |
| 120 | update({ input: { connected, ...(detail ? { detail } : {}) } }), |
| 121 | }, |
| 122 | m, |
| 123 | ); |
| 124 | session = { |
| 125 | stream: client, |
| 126 | canvas, |
| 127 | setFrameListener: (listener) => { |
| 128 | frameListener = listener; |
| 129 | }, |
| 130 | setControlRejectedListener: (listener) => { |
| 131 | controlRejectedListener = listener; |
| 132 | }, |
| 133 | markPhoneUnavailable: () => update({ phoneUnavailable: true }), |
| 134 | reconnect: () => { |
| 135 | client.stop(); |
| 136 | update({ phoneUnavailable: false }); |
| 137 | client.start(); |
| 138 | }, |
| 139 | state, |
| 140 | }; |
| 141 | get.addFinalizer(() => { |
| 142 | client.stop(); |
| 143 | get.registry.set(screenAtom, null); |
| 144 | }); |
| 145 | client.start(); |
| 146 | return session; |
| 147 | }).pipe(Atom.withLabel(`device-stream:${key.platform}:${key.deviceId}`)), |
| 148 | ); |