重构基线
This commit is contained in:
@@ -6,6 +6,9 @@
|
||||
* - ENABLE_BACKEND:设为 "1" or "true" 时启用默认 FastAPI 后端
|
||||
* - BACKEND_CMD:覆盖 FastAPI 启动命令;设置后即视为显式启用后端
|
||||
* - SKIP_BACKEND:设为 "1" or "true" 可强制跳过 FastAPI 后端
|
||||
* - ENABLE_OPENCODE:设为 "1" or "true" 时启用 opencode serve
|
||||
* - OPENCODE_CMD:覆盖 opencode 启动命令;设置后即视为显式启用 opencode
|
||||
* - SKIP_OPENCODE:设为 "1" or "true" 可强制跳过 opencode
|
||||
* - PYTHON_BIN:只在 BACKEND_CMD 未覆盖时,设置 Python 可执行文件,默认 "python"
|
||||
*/
|
||||
|
||||
@@ -45,6 +48,7 @@ function resolveBackendExecutable(envName, fallbackName) {
|
||||
|
||||
const pythonBin = resolveBackendExecutable("PYTHON_BIN", "python");
|
||||
const backendPortFromEnv = Number(process.env.BACKEND_PORT || 8000);
|
||||
const opencodePortFromEnv = Number(process.env.OPENCODE_PORT || 4096);
|
||||
|
||||
function hasCommand(command) {
|
||||
try {
|
||||
@@ -68,6 +72,10 @@ function buildDefaultBackendCommand(port) {
|
||||
return `${pythonBin} -m uvicorn app.main:app --reload --port ${port}`;
|
||||
}
|
||||
|
||||
function buildDefaultOpencodeCommand(port) {
|
||||
return `while true; do script -qfec "opencode serve --hostname=127.0.0.1 --port ${port} --print-logs" /dev/null; sleep 1; done`;
|
||||
}
|
||||
|
||||
function isEnabledEnv(value) {
|
||||
const normalized = String(value || "").toLowerCase();
|
||||
return normalized === "1" || normalized === "true";
|
||||
@@ -79,6 +87,12 @@ function shouldStartBackend(env = process.env) {
|
||||
return isEnabledEnv(env.ENABLE_BACKEND);
|
||||
}
|
||||
|
||||
function shouldStartOpencode(env = process.env) {
|
||||
if (isEnabledEnv(env.SKIP_OPENCODE)) return false;
|
||||
if (String(env.OPENCODE_CMD || "").trim()) return true;
|
||||
return true;
|
||||
}
|
||||
|
||||
function resolveRuntimePlan(env = process.env) {
|
||||
const frontendPort = Number(env.FRONTEND_PORT || 3000);
|
||||
const skipGateway = false;
|
||||
@@ -92,6 +106,7 @@ function resolveRuntimePlan(env = process.env) {
|
||||
mnoteWebEnv: {
|
||||
MNOTE_WEB_BIND: env.MNOTE_WEB_BIND || `0.0.0.0:${publicPort}`,
|
||||
MNOTE_WEB_PUBLIC_BIND: env.MNOTE_WEB_PUBLIC_BIND || `127.0.0.1:${publicPort}`,
|
||||
MNOTE_OPENCODE_BASE_URL: env.MNOTE_OPENCODE_BASE_URL || `http://127.0.0.1:${opencodePortFromEnv}`,
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -122,6 +137,17 @@ const tasks = [
|
||||
},
|
||||
]
|
||||
: []),
|
||||
...(shouldStartOpencode(process.env)
|
||||
? [
|
||||
{
|
||||
name: "opencode",
|
||||
command:
|
||||
process.env.OPENCODE_CMD ||
|
||||
buildDefaultOpencodeCommand(opencodePortFromEnv),
|
||||
cwd: rootDir,
|
||||
},
|
||||
]
|
||||
: []),
|
||||
];
|
||||
|
||||
function findTask(name) {
|
||||
@@ -475,6 +501,24 @@ async function main() {
|
||||
|
||||
}
|
||||
|
||||
const desiredOpencodePort = opencodePortFromEnv;
|
||||
if (shouldStartOpencode(process.env) && !process.env.OPENCODE_CMD) {
|
||||
const opencodePortOk = await ensurePortFree(desiredOpencodePort, "opencode");
|
||||
if (!opencodePortOk) {
|
||||
console.error(`opencode 端口 ${desiredOpencodePort} 无法释放,已中止启动。`);
|
||||
process.exit(1);
|
||||
}
|
||||
const opencodeTask = findTask("opencode");
|
||||
if (!opencodeTask) {
|
||||
throw new Error("缺少 opencode 任务配置");
|
||||
}
|
||||
opencodeTask.command = buildDefaultOpencodeCommand(desiredOpencodePort);
|
||||
} else if (isEnabledEnv(process.env.SKIP_OPENCODE)) {
|
||||
logPrefix("opencode", "已跳过 opencode(SKIP_OPENCODE=1)。");
|
||||
} else if (!shouldStartOpencode(process.env)) {
|
||||
|
||||
}
|
||||
|
||||
if (tasks.length === 0) {
|
||||
console.error("未配置任何可运行的任务,检查环境变量设置。");
|
||||
process.exit(1);
|
||||
|
||||
Reference in New Issue
Block a user