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

app-studio.ts

100 lines3.6 KB

1import { toolDefinition } from '@tanstack/ai'
2import {
3 DEFAULT_GROK_ACP_PORT,
4 GROK_CLI_INSTALL_COMMAND,
5 grokBuildText,
6} from '@tanstack/ai-grok-build'
7import {
8 createSecrets,
9 defineSandbox,
10 defineWorkspace,
11} from '@tanstack/ai-sandbox'
12import { dockerSandbox } from '@tanstack/ai-sandbox-docker'
13import { z } from 'zod'
14import type { AnyTextAdapter } from '@tanstack/ai'
15import type {
16 SandboxDefinition,
17 SandboxEnsureContext,
18} from '@tanstack/ai-sandbox'
19​
20export const PREVIEW_PORT = 5173
21​
22const SCAFFOLD =
23 'Scaffold with the TanStack CLI via npx. Run it exactly like this: `npx --yes @tanstack/cli create my-app --framework react --no-examples --intent -y`. Do not guess other package names.'
24​
25const APP =
26 'Turn it into a self-contained interactive app. No external APIs, no env vars, no keys. Keep state in the browser (localStorage). Make it look polished.'
27​
28const RUN = `Add \`server: { host: true, allowedHosts: true }\` to vite.config.ts. Start the dev server on port ${PREVIEW_PORT}: \`pnpm dev --host 0.0.0.0 --port ${PREVIEW_PORT}\`. When it is listening, call exposePreview with { "port": ${PREVIEW_PORT} } and share the URL.`
29​
30export function missingAppStudioEnv(): Array<string> {
31 return process.env.XAI_API_KEY ? [] : ['XAI_API_KEY']
33​
34export function buildAppStudioAdapter(): AnyTextAdapter {
35 return grokBuildText('grok-build')
37​
38export function buildAppStudioSandbox(): SandboxDefinition {
39 const key = process.env.XAI_API_KEY
40 return defineSandbox({
41 id: 'app-studio',
42 provider: dockerSandbox({
43 image: process.env.SANDBOX_IMAGE ?? 'node:22',
44 publishPorts: [PREVIEW_PORT, DEFAULT_GROK_ACP_PORT],
45 }),
46 workspace: defineWorkspace({
47 source: { type: 'none' },
48 setup: ({ serial }) => serial(GROK_CLI_INSTALL_COMMAND),
49 secrets: createSecrets(key ? { XAI_API_KEY: key } : {}),
50 }),
51 lifecycle: { reuse: 'thread' },
52 })
54​
55export const tanstackStartRecipe = toolDefinition({
56 name: 'tanstackStartRecipe',
57 description:
58 'The recipe for a self-contained TanStack Start app in this sandbox. Call this before you scaffold.',
59 inputSchema: z.object({
60 section: z
61 .enum(['scaffold', 'app', 'run', 'all'])
62 .describe('Which part of the recipe you need. Use all first.'),
63 }),
64}).server(({ section }) => {
65 const recipe = { scaffold: SCAFFOLD, app: APP, run: RUN }
66 return section === 'all' ? recipe : { [section]: recipe[section] }
67})
68​
69export function makeExposePreviewTool(
70 definition: SandboxDefinition,
71 threadId: string,
72 bookkeeping?: Pick<SandboxEnsureContext, 'store' | 'locks'>,
73) {
74 return toolDefinition({
75 name: 'exposePreview',
76 description: `Expose the sandbox port the dev server is listening on and return a preview URL. Call this after the server is up on port ${PREVIEW_PORT}.`,
77 inputSchema: z.object({
78 port: z.number().int().min(1024).max(65535),
79 }),
80 }).server(async ({ port }) => {
81 const handle = await definition.ensure({
82 threadId,
83 runId: 'expose-preview',
84 ...bookkeeping,
85 })
86 const channel = await handle.ports.connect(port)
87 return { url: channel.url }
88 })
90​
91export const APP_STUDIO_SYSTEM_PROMPT = [
92 'You work in this sandbox.',
93 'If the workspace is empty, call tanstackStartRecipe with section all, then scaffold, build the app, start the preview, and call exposePreview.',
94 'If the workspace already has an app, do not scaffold again.',
95 'Install dependencies if node_modules is missing.',
96 'Apply the requested change.',
97 `Restart the preview on port ${PREVIEW_PORT}.`,
98 'Then call exposePreview.',
99 `The preview port must be ${PREVIEW_PORT}.`,
100].join(' ')