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

servers.mjs

176 lines4.3 KB

1import { execFileSync } from 'node:child_process'
2import { mkdirSync, writeFileSync } from 'node:fs'
3import path from 'node:path'
4import { fileURLToPath } from 'node:url'
5​
6const exampleDir = path.resolve(fileURLToPath(new URL('..', import.meta.url)))
7const outputPath = path.join(exampleDir, 'public', 'servers.json')
8​
9/** @type {const} */
10export const SERVER_CATALOG = [
11 {
12 id: 'go',
13 label: 'Go',
14 port: 8001,
15 detect: [['go', 'version']],
16 devScript: 'dev:go',
17 setup: {
18 summary: 'Install Go 1.22+ and ensure `go` is on PATH.',
19 installUrl: 'https://go.dev/dl/',
20 verify: 'go version',
21 run: 'pnpm dev:go',
22 },
23 },
24 {
25 id: 'rust',
26 label: 'Rust',
27 port: 8002,
28 detect: [['cargo', '--version']],
29 devScript: 'dev:rust',
30 setup: {
31 summary: 'Install Rust stable via rustup and ensure `cargo` is on PATH.',
32 installUrl: 'https://rustup.rs/',
33 verify: 'cargo --version',
34 run: 'pnpm dev:rust',
35 },
36 },
37 {
38 id: 'php',
39 label: 'PHP',
40 port: 8003,
41 detect: [['php', '-v']],
42 devScript: 'dev:php',
43 setup: {
44 summary: 'Install PHP 8.2+ CLI and ensure `php` is on PATH.',
45 installUrl: 'https://www.php.net/downloads',
46 verify: 'php -v',
47 run: 'pnpm dev:php',
48 },
49 },
50 {
51 id: 'zig',
52 label: 'Zig',
53 port: 8004,
54 detect: [['zig', 'version']],
55 devScript: 'dev:zig',
56 setup: {
57 summary: 'Install Zig and ensure `zig` is on PATH.',
58 installUrl: 'https://ziglang.org/download/',
59 verify: 'zig version',
60 run: 'pnpm dev:zig',
61 },
62 },
63 {
64 id: 'bash',
65 label: 'Bash',
66 port: 8005,
67 detect: [
68 ['bash', '-c', '(( BASH_VERSINFO[0] >= 4 ))'],
69 ['curl', '--version'],
70 ['jq', '--version'],
71 ['socat', '-V'],
72 ],
73 devScript: 'dev:bash',
74 setup: {
75 summary:
76 'Install Bash 4+, curl, jq, and socat and ensure they are on PATH.',
77 installUrl: 'https://brew.sh/',
78 verify: 'bash --version && curl --version && jq --version && socat -V',
79 run: 'pnpm dev:bash',
80 },
81 },
82 {
83 id: 'python',
84 label: 'Python',
85 port: 8006,
86 detect: [['python3', '--version']],
87 devScript: 'dev:python',
88 setup: {
89 summary: 'Install Python 3.9+ and ensure `python3` is on PATH.',
90 installUrl: 'https://www.python.org/downloads/',
91 verify: 'python3 --version',
92 run: 'pnpm dev:python',
93 },
94 },
96​
97function parseDisabledServers(env) {
98 const raw = env.AGUI_DISABLE_SERVERS ?? ''
99 return new Set(
100 raw
101 .split(',')
102 .map((item) => item.trim().toLowerCase())
103 .filter(Boolean),
104 )
106​
107function hasToolchain(detectCommands) {
108 return detectCommands.every(([command, ...args]) => {
109 try {
110 execFileSync(command, args, { stdio: 'ignore' })
111 return true
112 } catch {
113 return false
114 }
115 })
117​
118export function detectServers(env = process.env) {
119 const disabled = parseDisabledServers(env)
120​
121 return SERVER_CATALOG.map((server) => {
122 const disabledByEnv = disabled.has(server.id)
123 const toolchainPresent = hasToolchain(server.detect)
124 const available = toolchainPresent && !disabledByEnv
125​
126 return {
127 id: server.id,
128 label: server.label,
129 port: server.port,
130 available,
131 disabledByEnv,
132 toolchainPresent,
133 devScript: server.devScript,
134 setup: server.setup,
135 }
136 })
138​
139export function writeServersJson(env = process.env) {
140 const servers = detectServers(env).map(
141 ({ devScript: _devScript, toolchainPresent: _toolchainPresent, ...rest }) =>
142 rest,
143 )
144​
145 const payload = {
146 generatedAt: new Date().toISOString(),
147 servers,
148 }
149​
150 mkdirSync(path.dirname(outputPath), { recursive: true })
151 writeFileSync(outputPath, `${JSON.stringify(payload, null, 2)}\n`, 'utf8')
152​
153 return { payload, outputPath, servers }
155​
156export function logDetectionSummary(servers) {
157 for (const server of servers) {
158 if (server.available) {
159 console.log(`[ag-ui] ${server.label} available on :${server.port}`)
160 continue
161 }
162​
163 if (server.disabledByEnv) {
164 console.log(
165 `[ag-ui] ${server.label} disabled via AGUI_DISABLE_SERVERS (toolchain ${server.toolchainPresent ? 'present' : 'missing'})`,
166 )
167 continue
168 }
169​
170 console.log(
171 `[ag-ui] ${server.label} unavailable — ${server.setup.verify} not found`,
172 )
173 }
175​
176export { exampleDir, outputPath }