Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

smoke.ts

125 lines5.1 KB

1/**
2 * Live check of @tanstack/ai-computer-sandbox on one sandbox provider.
3 *
4 * pnpm smoke <provider> [--install]
5 *
6 * It creates one sandbox, starts the desktop, opens xterm, clicks, types,
7 * takes two screenshots into ./out, and destroys the sandbox.
8 * `--install` installs the desktop tools with apt-get first, for images
9 * that do not have them.
10 */
11import { mkdir, writeFile } from 'node:fs/promises'
12import { createSandboxComputerDriver } from '@tanstack/ai-computer-sandbox'
13import type { SandboxProvider } from '@tanstack/ai-sandbox'
14​
15try {
16 process.loadEnvFile(new URL('../../.env.local', import.meta.url))
17} catch {
18 // No .env.local. The provider SDKs read process.env.
20​
21/** A desktop image for providers that take one. Build it from ./Dockerfile. */
22const image = process.env.DESKTOP_IMAGE ?? 'tanstack-desktop-smoke'
23​
24// Import only the chosen provider, so one SDK loads.
25const providers: Record<string, () => Promise<SandboxProvider>> = {
26 docker: async () =>
27 (await import('@tanstack/ai-sandbox-docker')).dockerSandbox({ image }),
28 e2b: async () =>
29 (await import('@tanstack/ai-sandbox-e2b')).e2bSandbox({
30 template: 'desktop',
31 }),
32 daytona: async () =>
33 (await import('@tanstack/ai-sandbox-daytona')).daytonaSandbox({
34 snapshot: process.env.DAYTONA_SNAPSHOT,
35 }),
36 blaxel: async () =>
37 (await import('@tanstack/ai-sandbox-blaxel')).blaxelSandbox({
38 image: process.env.DESKTOP_IMAGE,
39 }),
40 boxd: async () => (await import('@tanstack/ai-sandbox-boxd')).boxdSandbox(),
41 railway: async () =>
42 (await import('@tanstack/ai-sandbox-railway')).railwaySandbox(),
43 sprites: async () =>
44 (await import('@tanstack/ai-sandbox-sprites')).spritesSandbox(),
45 'upstash-box': async () =>
46 (await import('@tanstack/ai-sandbox-upstash-box')).upstashBoxSandbox(),
47 vercel: async () =>
48 (await import('@tanstack/ai-sandbox-vercel')).vercelSandbox({
49 teamId: process.env.VERCEL_TEAM_ID ?? process.env.VERCEL_TEAM,
50 }),
52​
53// Debian and Ubuntu: apt-get. Alpine (Blaxel): apk. Amazon Linux (Vercel): dnf has no xdotool or
54// scrot, so build xdotool, and the driver takes screenshots with ImageMagick.
55const INSTALL = `S=; [ "$(id -u)" = 0 ] || S=sudo
56if command -v apt-get >/dev/null; then
57 $S apt-get update -qq && $S env DEBIAN_FRONTEND=noninteractive apt-get install -y -qq --no-install-recommends xvfb xdotool scrot x11-utils xterm
58elif command -v dnf >/dev/null; then
59 $S dnf -y -q install gcc make git libXtst-devel libXinerama-devel libxkbcommon-devel libXi-devel xorg-x11-server-Xvfb xdpyinfo xterm ImageMagick \\
60 && rm -rf /tmp/xdotool && git clone -q --depth 1 https://github.com/jordansissel/xdotool /tmp/xdotool \\
61 && make -s -C /tmp/xdotool WITHOUT_RPATH_FIX=1 && $S make -s -C /tmp/xdotool install PREFIX=/usr WITHOUT_RPATH_FIX=1 && $S ldconfig
62elif command -v apk >/dev/null; then
63 $S apk add -q --no-cache xvfb xdotool scrot xdpyinfo xterm font-misc-misc
64else
65 echo "No apt-get, dnf, or apk" >&2; exit 1
66fi`
67​
68const [name, flag] = process.argv.slice(2)
69const makeProvider = name ? providers[name] : undefined
70if (!name || !makeProvider) {
71 console.error(
72 `Usage: pnpm smoke <${Object.keys(providers).join('|')}> [--install]`,
73 )
74 process.exit(1)
76​
77async function step<T>(label: string, fn: () => Promise<T>) {
78 const start = performance.now()
79 const result = await fn()
80 console.log(`${label.padEnd(28)} ${Math.round(performance.now() - start)} ms`)
81 return result
83​
84const sandbox = await step('create sandbox', async () =>
85 (await makeProvider()).create({}),
87try {
88 if (flag === '--install') {
89 await step('install desktop tools', async () => {
90 const result = await sandbox.process.exec(INSTALL)
91 if (result.exitCode !== 0) throw new Error(result.stderr)
92 })
93 }
94 const driver = await step('start desktop + driver', () =>
95 createSandboxComputerDriver({ sandbox }),
96 )
97 console.log('display', driver.display)
98​
99 // Open a terminal to type into. The E2B template has xfce4-terminal, not xterm.
100 await sandbox.process.spawn(
101 'export DISPLAY=:0; if command -v xterm >/dev/null; then xterm -geometry 80x20+50+50; else xfce4-terminal --geometry=80x20+50+50; fi >/dev/null 2>&1',
102 )
103 await new Promise((resolve) => setTimeout(resolve, 1500))
104​
105 await mkdir(new URL('./out/', import.meta.url), { recursive: true })
106 const before = await step('screenshot', () => driver.screenshot())
107 await writeFile(new URL(`./out/${name}-1.png`, import.meta.url), before)
108​
109 await step('click', () =>
110 driver.click(200, 150, { button: 'left', count: 1 }),
111 )
112 await step('type', () => driver.type('echo hello from tanstack'))
113 await step('press Enter', () => driver.press(['Enter']))
114 await step('scroll', () => driver.scroll(200, 150, 0, 300))
115 const position = await step('cursor position', () => driver.cursorPosition())
116 console.log('cursor', position)
117​
118 const after = await step('screenshot', () => driver.screenshot())
119 await writeFile(new URL(`./out/${name}-2.png`, import.meta.url), after)
120 console.log(
121 `\n${name}: OK. Screenshots in testing/computer-sandbox-smoke/out/`,
122 )
123} finally {
124 await step('destroy sandbox', () => sandbox.destroy())