import { findAvailablePort, formatPortDecision, mapDevPortsToPortRange, normalizePort, reserveLinuxDevPortRange, } from '../../../scripts/dev-stack-port-utils.mjs'; const agcDevHost = '127.0.0.1'; const legacyAgcDevPort = 3080; const agcVitePortEnvKey = 'GENARRATIVE_AGC_VITE_PORT'; function readConfiguredAgcDevPort(env = process.env) { const rawPort = String(env[agcVitePortEnvKey] ?? '').trim(); if (!rawPort) { return null; } const port = normalizePort(rawPort, -1); if (port < 1024) { throw new Error(`${agcVitePortEnvKey} 必须是 1024-65535 的有效端口`); } return port; } function createAgcDevEndpoint(port, portRange = null) { const url = `http://${agcDevHost}:${port}/`; return { host: agcDevHost, port, url, markerUrl: `${url}__agc_dev_server.json`, portRange, }; } function readAgcDevEndpoint(env = process.env) { return createAgcDevEndpoint( readConfiguredAgcDevPort(env) ?? legacyAgcDevPort, ); } async function resolveAgcDevEndpoint({ env = process.env, platform = process.platform, strictConfigured = false, reservePortRange = reserveLinuxDevPortRange, findPort = findAvailablePort, } = {}) { const configuredPort = readConfiguredAgcDevPort(env); let portRange = null; let preferredPort = configuredPort ?? legacyAgcDevPort; if (platform === 'linux') { const allocation = await reservePortRange({ env }); if (!allocation?.range) { throw new Error('无法取得当前 Linux 用户的 dev 端口段'); } portRange = allocation.range; const mappedAgcVitePort = mapDevPortsToPortRange(portRange)?.agcVitePort; if (!Number.isInteger(mappedAgcVitePort)) { throw new Error( `当前 Linux dev 端口段 ${portRange.label} 缺少 AGC Vite 槽位;请先迁移为至少 6 个端口且不与其它用户重叠的端口段`, ); } if (configuredPort != null && configuredPort < mappedAgcVitePort) { throw new Error( `${agcVitePortEnvKey} ${configuredPort} 占用了 Linux dev 端口段 ${portRange.label} 的前五个服务槽位`, ); } preferredPort = configuredPort ?? mappedAgcVitePort; } const port = await findPort({ host: agcDevHost, preferredPort, portRange, strict: strictConfigured && configuredPort != null, }); console.log( formatPortDecision({ name: 'ai-game-creator-shell', host: agcDevHost, preferredPort, resolvedPort: port, }), ); if (portRange) { console.log(`[ai-game-creator-shell] dev port-range: ${portRange.label}`); } return createAgcDevEndpoint(port, portRange); } function withAgcDevEndpointEnv(endpoint, env = process.env) { return { ...env, [agcVitePortEnvKey]: String(endpoint.port), }; } export { agcDevHost, agcVitePortEnvKey, createAgcDevEndpoint, legacyAgcDevPort, readAgcDevEndpoint, readConfiguredAgcDevPort, resolveAgcDevEndpoint, withAgcDevEndpointEnv, };