Files
Genarrative/apps/ai-game-creator-shell/scripts/start-dev-server.mjs
T
lhk229 65d1489bdd 新增 Anthropic 链路,LLM 请求收口为 LlmRunRequest/LlmApiKind,新增Agent流程图绘图脚本 (#70)
新增 Anthropic 链路,LLM 请求收口为 LlmRunRequest/LlmApiKind,为未来创建更多字段适配高级功能准备

Reviewed-on: https://git.genarrative.world/git/GenarrativeAI/Genarrative/pulls/70
Co-authored-by: Linghong <ink29535@proton.me>
Co-committed-by: Linghong <ink29535@proton.me>
2026-06-30 19:33:37 +08:00

112 lines
2.7 KiB
JavaScript

import { spawn } from 'node:child_process';
import http from 'node:http';
import net from 'node:net';
import { fileURLToPath } from 'node:url';
const appRoot = fileURLToPath(new URL('..', import.meta.url));
const host = '127.0.0.1';
const port = 3080;
const devUrl = `http://${host}:${port}/`;
function readExistingServer() {
return new Promise((resolve) => {
const request = http.get(
{
host,
port,
path: '/',
timeout: 1000,
},
(response) => {
let body = '';
response.setEncoding('utf8');
response.on('data', (chunk) => {
if (body.length < 4096) {
body += chunk;
}
});
response.on('end', () => {
resolve({
statusCode: response.statusCode ?? 0,
body,
});
});
},
);
request.on('timeout', () => {
request.destroy();
resolve(null);
});
request.on('error', () => resolve(null));
});
}
function isAiGameCreatorServer(response) {
return (
response &&
response.statusCode >= 200 &&
response.statusCode < 500 &&
response.body.includes('<title>AI 游戏创作</title>') &&
response.body.includes('/src/main.tsx')
);
}
function isPortListening() {
return new Promise((resolve) => {
const socket = net.connect({ host, port });
socket.once('connect', () => {
socket.destroy();
resolve(true);
});
socket.once('error', () => resolve(false));
socket.setTimeout(1000, () => {
socket.destroy();
resolve(true);
});
});
}
const existing = await readExistingServer();
if (existing) {
if (isAiGameCreatorServer(existing)) {
console.log(`[ai-game-creator-shell] reuse existing Vite dev server ${devUrl}`);
process.exit(0);
}
console.error(
`[ai-game-creator-shell] ${devUrl} is already in use by another server. Stop that process before starting Tauri dev.`,
);
process.exit(1);
}
if (await isPortListening()) {
console.error(
`[ai-game-creator-shell] ${devUrl} is already in use by a non-HTTP or unrecognized server. Stop that process before starting Tauri dev.`,
);
process.exit(1);
}
const npm = process.platform === 'win32' ? 'npm.cmd' : 'npm';
const child = spawn(
npm,
['--prefix', '../..', 'exec', 'vite', '--', '--config', 'vite.config.ts'],
{
cwd: appRoot,
stdio: 'inherit',
// Node 18.20+/20+/24 on Windows rejects spawning .cmd (npm.cmd) without a shell (EINVAL).
shell: true,
},
);
for (const signal of ['SIGINT', 'SIGTERM']) {
process.on(signal, () => {
child.kill(signal);
});
}
child.on('exit', (code, signal) => {
if (signal) {
process.exit(1);
}
process.exit(code ?? 0);
});