@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 82 lines 3.25 KB
Raw
1 import type { DeviceHubAccess } from "@t3tools/client-runtime/device/hub-access";
2 import type { DevicePlatform } from "@t3tools/contracts";
3 import type { PlatformOSType } from "react-native";
4
5 export interface DeviceStreamConfiguration {
6 readonly access: DeviceHubAccess;
7 readonly platform: DevicePlatform;
8 readonly deviceId: string;
9 /** See `DeviceStreamTarget.serveSimOnly`. */
10 readonly serveSimOnly?: boolean;
11 readonly colors: {
12 readonly background: string;
13 readonly foreground: string;
14 readonly muted: string;
15 readonly buttonBackground: string;
16 readonly buttonForeground: string;
17 readonly buttonBorder: string;
18 };
19 /** Translated in React Native: the WebView cannot load the catalogs. */
20 readonly labels: {
21 readonly screen: string;
22 readonly reconnectingControls: string;
23 };
24 }
25
26 export function deviceStreamBaseUrl(
27 viewerPlatform: PlatformOSType,
28 devicePlatform: DevicePlatform,
29 httpBase: string,
30 ) {
31 if (viewerPlatform !== "android") return "file:///";
32 // Android targets stream through WebCodecs, which needs a secure context. iOS targets
33 // stream an MJPEG image from the environment, which a synthetic https document blocks as
34 // mixed content on plain-http environments even with mixedContentMode="always".
35 if (devicePlatform === "android") return "https://localhost/";
36 return `${new URL(httpBase).origin}/`;
37 }
38
39 export function deviceStreamDocument(
40 configuration: string,
41 script: string,
42 stoppedDetail = "Device viewer stopped unexpectedly.",
43 ) {
44 const safeConfiguration = configuration.replace(/</g, "\\u003c");
45 const safeScript = script.replace(/<\/script/gi, "<\\/script");
46 const safeStoppedDetail = JSON.stringify(stoppedDetail).replace(/</g, "\\u003c");
47 const failure = `window.ReactNativeWebView.postMessage(JSON.stringify({type:"status",status:"error",detail:${safeStoppedDetail}}));`;
48 return `<!doctype html><html><head><meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no"></head><body><script>window.addEventListener("error",function(){${failure}});window.addEventListener("unhandledrejection",function(){${failure}});\n${safeScript}\ntry{T3DeviceStream.start(${safeConfiguration});}catch{${failure}}</script></body></html>`;
49 }
50
51 export function deviceStreamMessage(data: string) {
52 try {
53 const message: unknown = JSON.parse(data);
54 if (typeof message !== "object" || message === null || !("type" in message)) return null;
55 if (message.type === "unauthorized" || message.type === "retry") {
56 return { type: message.type } as const;
57 }
58 if (
59 message.type === "input" &&
60 "connected" in message &&
61 typeof message.connected === "boolean"
62 ) {
63 return { type: message.type, connected: message.connected } as const;
64 }
65 if (
66 message.type === "status" &&
67 "status" in message &&
68 (message.status === "connecting" ||
69 message.status === "streaming" ||
70 message.status === "error") &&
71 (!("detail" in message) || typeof message.detail === "string")
72 ) {
73 return {
74 type: message.type,
75 status: message.status,
76 detail:
77 "detail" in message && typeof message.detail === "string" ? message.detail : undefined,
78 } as const;
79 }
80 } catch {}
81 return null;
82 }