Phase 1 of the 4-package split called out in RoadMap #3. Moves the "infrastructure" leaves — files with zero business dependencies that @snowluma/bridge, @snowluma/core, and @snowluma/onebot all consume — into a dedicated workspace package. Mirrors what #4 (proto-defs) did for wire-protobuf definitions. Why --- - Cleaner layering: these were never bridge- or onebot-specific. Logger / hex codec / runtime config sit at the bottom of every module's import graph; giving them their own package makes that graph readable and prevents future leaks the other direction. - Sets up #3's bridge + onebot extractions: those need a clean leaf to depend on. Without this, every cross-package import would have to re-export through core, which defeats the split. - Mirrors @snowluma/proto-defs layout — per-file subpath exports (`@snowluma/common/logger`, `@snowluma/common/hex`, …) keep import sites unambiguous and let bundlers tree-shake aggressively. Layout ------ Moved (via `git mv` — history preserved): packages/core/src/utils/logger.ts → @snowluma/common/logger packages/core/src/utils/hex.ts → @snowluma/common/hex packages/core/src/utils/log-file-transport.ts → @snowluma/common/log-file-transport packages/core/src/utils/log-summary.ts → @snowluma/common/log-summary packages/core/src/protocol/packet-sender.ts → @snowluma/common/packet-sender packages/core/src/protocol/types.ts → @snowluma/common/protocol-types packages/core/src/common/runtime.ts → @snowluma/common/runtime Not moved (deferred to Phase 2): `packages/core/src/utils/event-format.ts` — it has reverse imports on `bridge/events`, `bridge/identity-service`, `onebot/message-store`, `onebot/types`. It moves into @snowluma/bridge in Phase 2, with the MessageStore parameter decoupled via a structural type so bridge doesn't end up depending on onebot. Changes ------- - New package `packages/common/` (package.json, tsconfig.json — no src/index.ts barrel, callers use the subpath exports directly to avoid pulling in `node:fs` when only the hex codec is needed). - 7 `.ts` files moved via `git mv`. - 50+ import sites rewritten via sed across `packages/core/src` and `packages/core/tests`. Old shapes (`../utils/logger`, `../../protocol/types`, `./common/runtime`, `../src/utils/hex`) all collapse to `@snowluma/common/<sub>`. - `@snowluma/core` depends on `@snowluma/common` (workspace:*). - `packages/core/src/{protocol,common}/` directories removed (empty). Verification ------------ - core typecheck: clean - common typecheck: clean - Full workspace `pnpm -r typecheck`: all 9 projects clean - core test suite: 552 passed / 552 total (pre-existing websocket-darwin-arm64.node addon failure unchanged — same baseline as the previous refactor commits) - Vite build with stubbed native binary: clean (143 modules, 2.84 MB bundle, 224 KB gzip — same size envelope as before) Note: `utils/event-format.ts` is the only file remaining under the old `utils/` namespace. It moves into @snowluma/bridge alongside the events / identity-service / packet-pipeline trio in Phase 2.
114 lines
3.5 KiB
TypeScript
114 lines
3.5 KiB
TypeScript
import fs from 'fs';
|
|
import path from 'path';
|
|
|
|
export interface RuntimeConfig {
|
|
webuiPort?: number;
|
|
/** When true, every newly-discovered QQ process gets auto-injected by
|
|
* the HookManager. Also overridable at runtime via SNOWLUMA_HOOK_AUTOLOAD.
|
|
* Defaults to false; the Docker image flips it on in supervisord.conf. */
|
|
hookAutoLoad?: boolean;
|
|
}
|
|
|
|
const CONFIG_DIR = 'config';
|
|
const RUNTIME_CONFIG_PATH = path.join(CONFIG_DIR, 'runtime.json');
|
|
|
|
const DEFAULT_RUNTIME_CONFIG: RuntimeConfig = {
|
|
webuiPort: 5099,
|
|
hookAutoLoad: false,
|
|
};
|
|
|
|
export function loadRuntimeConfig(): RuntimeConfig {
|
|
fs.mkdirSync(CONFIG_DIR, { recursive: true });
|
|
|
|
const loaded = tryLoadRuntimeConfig();
|
|
let normalized: RuntimeConfig;
|
|
if (!loaded) {
|
|
saveRuntimeConfig(DEFAULT_RUNTIME_CONFIG);
|
|
normalized = { ...DEFAULT_RUNTIME_CONFIG };
|
|
} else {
|
|
normalized = {
|
|
webuiPort: normalizePort(loaded.webuiPort ?? 5099, 5099),
|
|
hookAutoLoad: normalizeBool(loaded.hookAutoLoad, false),
|
|
};
|
|
if (
|
|
normalized.webuiPort !== loaded.webuiPort
|
|
|| normalized.hookAutoLoad !== loaded.hookAutoLoad
|
|
) {
|
|
saveRuntimeConfig(normalized);
|
|
}
|
|
}
|
|
|
|
// SNOWLUMA_WEBUI_PORT overrides the on-disk value at runtime without
|
|
// rewriting runtime.json. Lets a trusted launcher (e.g. SnowLumaDesktop)
|
|
// pick a free port per-launch while CLI/Docker users continue to drive
|
|
// it from runtime.json.
|
|
const envPort = envPortOverride();
|
|
if (envPort !== undefined) {
|
|
normalized = { ...normalized, webuiPort: envPort };
|
|
}
|
|
|
|
return normalized;
|
|
}
|
|
|
|
function envPortOverride(): number | undefined {
|
|
const raw = process.env.SNOWLUMA_WEBUI_PORT;
|
|
if (!raw || typeof raw !== 'string') return undefined;
|
|
const n = Number(raw.trim());
|
|
if (!Number.isFinite(n)) return undefined;
|
|
const port = Math.trunc(n);
|
|
if (port <= 0 || port > 65535) return undefined;
|
|
return port;
|
|
}
|
|
|
|
function tryLoadRuntimeConfig(): RuntimeConfig | null {
|
|
if (!fs.existsSync(RUNTIME_CONFIG_PATH)) return null;
|
|
|
|
try {
|
|
const raw = fs.readFileSync(RUNTIME_CONFIG_PATH, 'utf8');
|
|
const parsed = JSON.parse(raw) as unknown;
|
|
if (!isObject(parsed)) return null;
|
|
|
|
return {
|
|
webuiPort: normalizePort(parsed.webuiPort ?? 5099, 5099),
|
|
hookAutoLoad: normalizeBool(parsed.hookAutoLoad, false),
|
|
};
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
function saveRuntimeConfig(config: RuntimeConfig): void {
|
|
fs.writeFileSync(RUNTIME_CONFIG_PATH, JSON.stringify(config, null, 2), 'utf8');
|
|
}
|
|
|
|
function normalizePort(value: unknown, fallback: number): number {
|
|
if (typeof value === 'number' && Number.isFinite(value)) {
|
|
const n = Math.trunc(value);
|
|
if (n > 0 && n <= 65535) return n;
|
|
return fallback;
|
|
}
|
|
if (typeof value === 'string' && value.trim()) {
|
|
const n = Number(value);
|
|
if (Number.isFinite(n)) {
|
|
const port = Math.trunc(n);
|
|
if (port > 0 && port <= 65535) return port;
|
|
}
|
|
}
|
|
return fallback;
|
|
}
|
|
|
|
function normalizeBool(value: unknown, fallback: boolean): boolean {
|
|
if (typeof value === 'boolean') return value;
|
|
if (typeof value === 'number') return value !== 0;
|
|
if (typeof value === 'string') {
|
|
const v = value.trim().toLowerCase();
|
|
if (v === 'true' || v === '1' || v === 'yes' || v === 'on') return true;
|
|
if (v === 'false' || v === '0' || v === 'no' || v === 'off' || v === '') return false;
|
|
}
|
|
return fallback;
|
|
}
|
|
|
|
function isObject(value: unknown): value is Record<string, unknown> {
|
|
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
}
|