Skip to content

Commit 6c3cfe1

Browse files
committed
feat(preview): run the browser on the environment server
Standalone servers (`t3 serve`, `npx t3`) now host preview tabs in headless Chromium, so phones, web clients, and agents can use the browser while no desktop app is connected. The desktop app's own environment keeps Electron tabs. - server: ServerBrowser owns tabs, serves the agent preview tools as the preferred automation host, and streams JPEG screencasts over /api/preview-stream with paced acks, motion-mode downscaling, and settle stills. Recording encodes in-browser to mp4. A pinned Chromium and font pack install on first use. - web: server tabs render through ServerBrowserSurface with touch input and a server-probed soft keyboard, float over chat, and pop into system picture-in-picture. - mobile: a browser screen, floating player, and iOS picture-in-picture built on the same stream. - fix: the details card no longer loops with a tall floating preview.
1 parent 8283b48 commit 6c3cfe1

52 files changed

Lines changed: 5721 additions & 200 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎apps/mobile/app.config.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,9 @@ const config: ExpoConfig = {
270270
NSLocalNetworkUsageDescription:
271271
"Allow T3 Code to connect to T3 Code servers on your local network or tailnet.",
272272
NSPhotoLibraryAddUsageDescription: "Allow T3 Code to save images to your photo library.",
273+
// "Audio, AirPlay, and Picture in Picture": the browser screen's system
274+
// picture in picture needs it to start and to stay up outside the app.
275+
UIBackgroundModes: ["audio"],
273276
ITSAppUsesNonExemptEncryption: false,
274277
// The App Store screenshot harness rotates the iPad interface from
275278
// inside the app (CI denies osascript the Accessibility access that

‎apps/mobile/metro.config.js‎

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const licenseGeneratorSource = path.join(
1818
const escapedWorkspaceRoot = workspaceRoot.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
1919
const mobileShikiRoot = path.dirname(require.resolve("shiki/package.json", { paths: [__dirname] }));
2020
const generatedDeviceStreamRoot = path.join(__dirname, ".generated", "device-stream");
21+
const generatedPreviewStreamRoot = path.join(__dirname, ".generated", "preview-stream");
2122
const resolveShikiDependencyRoot = (packageName) => {
2223
const entryPath = require.resolve(packageName, { paths: [mobileShikiRoot] });
2324
let currentDir = path.dirname(entryPath);
@@ -48,6 +49,7 @@ config.resolver = {
4849
...config.resolver?.extraNodeModules,
4950
"@t3tools/mobile-third-party-licenses": generatedLicenseModuleRoot,
5051
"@t3tools/mobile-device-stream": generatedDeviceStreamRoot,
52+
"@t3tools/mobile-preview-stream": generatedPreviewStreamRoot,
5153
shiki: mobileShikiRoot,
5254
"@shikijs/core": resolveShikiDependencyRoot("@shikijs/core"),
5355
"@shikijs/engine-javascript": resolveShikiDependencyRoot("@shikijs/engine-javascript"),
@@ -95,35 +97,57 @@ async function generateMobileThirdPartyLicenses() {
9597
]);
9698
}
9799

98-
async function prepareDeviceStream() {
99-
const { generateDeviceStreamScript } = await import(
100+
async function prepareStreamScripts() {
101+
const { generateDeviceStreamScript, generatePreviewStreamScript } = await import(
100102
pathToFileURL(path.join(__dirname, "scripts", "generate-device-stream.mts")).href
101103
);
102-
await generateDeviceStreamScript();
104+
const generateAll = () =>
105+
Promise.all([generateDeviceStreamScript(), generatePreviewStreamScript()]);
106+
await generateAll();
103107
if (process.env.NODE_ENV !== "production") {
104108
let rebuild = Promise.resolve();
105-
for (const [directory, files] of [
106-
[path.join(__dirname, "src/features/devices"), ["device-stream.browser.ts"]],
109+
for (const [directory, files, generate] of [
110+
[
111+
path.join(__dirname, "src/features/devices"),
112+
["device-stream.browser.ts"],
113+
generateDeviceStreamScript,
114+
],
115+
[
116+
path.join(__dirname, "src/features/browser"),
117+
["preview-stream.browser.ts"],
118+
generatePreviewStreamScript,
119+
],
120+
// The preview transport also imports `hubAccess.ts`.
107121
[
108122
path.join(workspaceRoot, "packages/client-runtime/src/device"),
109123
["stream.ts", "hubAccess.ts"],
124+
generateAll,
125+
],
126+
[
127+
path.join(workspaceRoot, "packages/client-runtime/src/preview"),
128+
["serverBrowserStream.ts"],
129+
generatePreviewStreamScript,
110130
],
111131
]) {
112-
// The generated module participates in Metro's normal Fast Refresh.
132+
// The generated modules participate in Metro's normal Fast Refresh.
113133
fs.watch(directory, { persistent: false }, (_event, filename) => {
114134
if (filename && !files.includes(String(filename))) return;
115-
rebuild = rebuild.then(generateDeviceStreamScript).catch((error) => {
116-
console.error("Could not rebuild the device stream:", error);
117-
});
135+
rebuild = rebuild
136+
.then(generate)
137+
.then(() => undefined)
138+
.catch((error) => {
139+
console.error("Could not rebuild a WebView stream script:", error);
140+
});
118141
});
119142
}
120143
}
121144
}
122145

123-
module.exports = Promise.all([generateMobileThirdPartyLicenses(), prepareDeviceStream()]).then(() =>
124-
withUniwindConfig(config, {
125-
cssEntryFile: "./global.css",
126-
extraThemes,
127-
polyfills: { rem: 14 },
128-
}),
146+
module.exports = Promise.all([generateMobileThirdPartyLicenses(), prepareStreamScripts()]).then(
147+
() =>
148+
withUniwindConfig(config, {
149+
cssEntryFile: "./global.css",
150+
extraThemes,
151+
polyfills: { rem: 14 },
152+
}),
129153
);

‎apps/mobile/scripts/generate-device-stream.mts‎

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import { build } from "vite-plus";
55

66
const mobileRoot = NodePath.resolve(NodePath.dirname(NodeURL.fileURLToPath(import.meta.url)), "..");
77

8-
/** Metro embeds the shared browser transport as a small script in the native WebView. */
9-
export async function generateDeviceStreamScript() {
8+
/** Metro embeds each shared browser transport as a small script in a native WebView. */
9+
async function generateWebViewScript(entry: string, name: string, output: string) {
1010
const result = await build({
1111
configFile: false,
1212
logLevel: "silent",
@@ -15,8 +15,8 @@ export async function generateDeviceStreamScript() {
1515
target: "es2022",
1616
minify: true,
1717
lib: {
18-
entry: NodePath.join(mobileRoot, "src/features/devices/device-stream.browser.ts"),
19-
name: "T3DeviceStream",
18+
entry: NodePath.join(mobileRoot, entry),
19+
name,
2020
formats: ["iife"],
2121
},
2222
},
@@ -25,15 +25,29 @@ export async function generateDeviceStreamScript() {
2525
const chunk = bundles
2626
.flatMap((bundle) => ("output" in bundle ? bundle.output : []))
2727
.find((output) => output.type === "chunk");
28-
if (!chunk) throw new Error("Device stream build did not emit a script.");
29-
const root = NodePath.join(mobileRoot, ".generated/device-stream");
28+
if (!chunk) throw new Error(`${name} build did not emit a script.`);
29+
const root = NodePath.join(mobileRoot, output);
3030
await NodeFSP.mkdir(root, { recursive: true });
31-
for (const [name, contents] of [
31+
for (const [file, contents] of [
3232
["index.js", `module.exports = ${JSON.stringify(chunk.code)};\n`],
3333
["package.json", '{"main":"index.js"}\n'],
3434
] as const) {
35-
const destination = NodePath.join(root, name);
35+
const destination = NodePath.join(root, file);
3636
const previous = await NodeFSP.readFile(destination, "utf8").catch(() => null);
3737
if (previous !== contents) await NodeFSP.writeFile(destination, contents);
3838
}
3939
}
40+
41+
export const generateDeviceStreamScript = () =>
42+
generateWebViewScript(
43+
"src/features/devices/device-stream.browser.ts",
44+
"T3DeviceStream",
45+
".generated/device-stream",
46+
);
47+
48+
export const generatePreviewStreamScript = () =>
49+
generateWebViewScript(
50+
"src/features/browser/preview-stream.browser.ts",
51+
"T3PreviewStream",
52+
".generated/preview-stream",
53+
);

‎apps/mobile/src/Stack.tsx‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import { ReviewCommentComposerSheet } from "./features/review/ReviewCommentCompo
4343
import { ReviewSheet } from "./features/review/ReviewSheet";
4444
import { ThreadTerminalRouteScreen } from "./features/terminal/ThreadTerminalRouteScreen";
4545
import { DevicePreviewRouteScreen } from "./features/devices/DevicePreviewRouteScreen";
46+
import { BrowserPreviewRouteScreen } from "./features/browser/BrowserPreviewRouteScreen";
4647
import { GitBranchesSheet } from "./features/threads/git/GitBranchesSheet";
4748
import { GitCommitSheet } from "./features/threads/git/GitCommitSheet";
4849
import { GitConfirmSheet } from "./features/threads/git/GitConfirmSheet";
@@ -548,6 +549,7 @@ const WORKSPACE_OVERLAY_ROUTES = new Set([
548549
"ThreadQueue",
549550
"ThreadReviewComment",
550551
"ThreadDevicePreview",
552+
"ThreadBrowserPreview",
551553
"ThreadSettingsSheet",
552554
]);
553555

@@ -703,6 +705,15 @@ const RootStackConfig = createNativeStackNavigator({
703705
gestureEnabled: false,
704706
},
705707
}),
708+
ThreadBrowserPreview: createNativeStackScreen({
709+
screen: BrowserPreviewRouteScreen,
710+
linking: `${THREAD_LINKING_PREFIX}/browser`,
711+
options: {
712+
presentation: "fullScreenModal",
713+
headerShown: false,
714+
gestureEnabled: false,
715+
},
716+
}),
706717
ThreadReview: createNativeStackScreen({
707718
screen: ReviewSheet,
708719
linking: `${THREAD_LINKING_PREFIX}/review`,

0 commit comments

Comments
 (0)