mioku-service-webui 4.11.0 → 4.11.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/config-page-loader.ts +44 -18
- package/package.json +1 -1
- package/routes/adapter-config.ts +2 -2
- package/routes/config.ts +0 -1
- package/system.ts +1 -52
- package/utils.ts +8 -1
package/config-page-loader.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
1
2
|
import * as fs from "node:fs";
|
|
2
3
|
import * as path from "node:path";
|
|
3
4
|
import matter from "gray-matter";
|
|
4
|
-
import { logger } from "mioku";
|
|
5
|
+
import { BOT_CWD, logger } from "mioku";
|
|
6
|
+
import { projectRoot } from "./utils";
|
|
5
7
|
|
|
6
8
|
export interface ConfigField {
|
|
7
9
|
key: string;
|
|
@@ -27,39 +29,63 @@ export interface ConfigPageManifest {
|
|
|
27
29
|
}
|
|
28
30
|
|
|
29
31
|
export function loadPluginConfigPage(pluginName: string): ConfigPageManifest | null {
|
|
30
|
-
const
|
|
31
|
-
|
|
32
|
-
path.join(
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
return loadConfigPage(pluginName, possiblePaths);
|
|
32
|
+
const root = projectRoot();
|
|
33
|
+
return loadConfigPage(pluginName, `mioku-plugin-${pluginName}`, [
|
|
34
|
+
path.join(root, "plugins", pluginName),
|
|
35
|
+
path.join(root, "node_modules", `mioku-plugin-${pluginName}`),
|
|
36
|
+
]);
|
|
36
37
|
}
|
|
37
38
|
|
|
38
39
|
export function loadServiceConfigPage(serviceName: string): ConfigPageManifest | null {
|
|
39
|
-
const
|
|
40
|
-
|
|
41
|
-
path.join(
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
return loadConfigPage(serviceName, possiblePaths);
|
|
40
|
+
const root = projectRoot();
|
|
41
|
+
return loadConfigPage(serviceName, `mioku-service-${serviceName}`, [
|
|
42
|
+
path.join(root, "services", serviceName),
|
|
43
|
+
path.join(root, "node_modules", `mioku-service-${serviceName}`),
|
|
44
|
+
]);
|
|
45
45
|
}
|
|
46
46
|
|
|
47
47
|
export function loadAdapterConfigPage(adapterName: string): ConfigPageManifest | null {
|
|
48
|
-
|
|
49
|
-
path.join(
|
|
50
|
-
];
|
|
48
|
+
return loadConfigPage(adapterName, `mioku-adapter-${adapterName}`, [
|
|
49
|
+
path.join(projectRoot(), "node_modules", `mioku-adapter-${adapterName}`),
|
|
50
|
+
]);
|
|
51
|
+
}
|
|
51
52
|
|
|
52
|
-
|
|
53
|
+
/** 从机器人根目录按 node_modules 依赖解析包真实目录(pnpm / 提升安装等布局) */
|
|
54
|
+
function resolvePackageDir(root: string, packageName: string): string | null {
|
|
55
|
+
const require = createRequire(path.join(root, "package.json"));
|
|
56
|
+
try {
|
|
57
|
+
return path.dirname(require.resolve(`${packageName}/package.json`));
|
|
58
|
+
} catch {
|
|
59
|
+
// exports 未暴露 package.json 时,从入口文件向上找
|
|
60
|
+
}
|
|
61
|
+
try {
|
|
62
|
+
let dir = path.dirname(require.resolve(packageName));
|
|
63
|
+
for (let i = 0; i < 5; i++) {
|
|
64
|
+
if (fs.existsSync(path.join(dir, "package.json"))) return dir;
|
|
65
|
+
const parent = path.dirname(dir);
|
|
66
|
+
if (parent === dir) break;
|
|
67
|
+
dir = parent;
|
|
68
|
+
}
|
|
69
|
+
} catch {
|
|
70
|
+
// 未安装该依赖
|
|
71
|
+
}
|
|
72
|
+
return null;
|
|
53
73
|
}
|
|
54
74
|
|
|
55
75
|
function loadConfigPage(
|
|
56
76
|
packageName: string,
|
|
77
|
+
npmName: string,
|
|
57
78
|
possiblePaths: string[],
|
|
58
79
|
): ConfigPageManifest | null {
|
|
80
|
+
const resolved = resolvePackageDir(projectRoot(), npmName);
|
|
81
|
+
const candidates =
|
|
82
|
+
resolved && !possiblePaths.includes(resolved)
|
|
83
|
+
? [...possiblePaths, resolved]
|
|
84
|
+
: possiblePaths;
|
|
59
85
|
let configMdPath: string | null = null;
|
|
60
86
|
let packageDir: string | null = null;
|
|
61
87
|
|
|
62
|
-
for (const dir of
|
|
88
|
+
for (const dir of candidates) {
|
|
63
89
|
const candidate = path.join(dir, "config.md");
|
|
64
90
|
if (fs.existsSync(candidate)) {
|
|
65
91
|
configMdPath = candidate;
|
package/package.json
CHANGED
package/routes/adapter-config.ts
CHANGED
|
@@ -3,10 +3,10 @@ import * as fs from "node:fs";
|
|
|
3
3
|
import * as path from "node:path";
|
|
4
4
|
import { loadAdapterConfigPage } from "../config-page-loader";
|
|
5
5
|
import { getAdapterConfigs, updateAdapterConfig } from "../system";
|
|
6
|
-
import { isPackageDir } from "../utils";
|
|
6
|
+
import { isPackageDir, projectRoot } from "../utils";
|
|
7
7
|
|
|
8
8
|
function listAdaptersFromNodeModules(): string[] {
|
|
9
|
-
const modulesPath = path.join(
|
|
9
|
+
const modulesPath = path.join(projectRoot(), "node_modules");
|
|
10
10
|
const adapters: string[] = [];
|
|
11
11
|
if (!fs.existsSync(modulesPath)) return adapters;
|
|
12
12
|
|
package/routes/config.ts
CHANGED
|
@@ -23,7 +23,6 @@ export function createConfigRoutes() {
|
|
|
23
23
|
logger.info(`[webui-action] config.mioku.update`, {
|
|
24
24
|
owners: body?.owners?.length,
|
|
25
25
|
admins: body?.admins?.length,
|
|
26
|
-
onebotInstances: body?.onebotInstances?.length,
|
|
27
26
|
plugins: body?.plugins?.length,
|
|
28
27
|
coreConfigured: Boolean(body?.core),
|
|
29
28
|
});
|
package/system.ts
CHANGED
|
@@ -75,15 +75,6 @@ function normalizeRepositoryUrl(repository: any): string {
|
|
|
75
75
|
return url.replace(/\.git$/, "");
|
|
76
76
|
}
|
|
77
77
|
|
|
78
|
-
export interface OneBotInstanceConfig {
|
|
79
|
-
protocol?: string;
|
|
80
|
-
host?: string;
|
|
81
|
-
port?: number;
|
|
82
|
-
token?: string;
|
|
83
|
-
reconnect?: boolean;
|
|
84
|
-
[key: string]: unknown;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
78
|
const SYSTEM_PLUGIN_NAMES = new Set(["chat", "help"]);
|
|
88
79
|
const SYSTEM_SERVICE_NAMES = new Set(["ai", "config", "help", "screenshot"]);
|
|
89
80
|
const CORE_CONFIG_PATH = path.join(
|
|
@@ -2431,7 +2422,6 @@ export interface MiokuConfig {
|
|
|
2431
2422
|
owners: string[];
|
|
2432
2423
|
admins: string[];
|
|
2433
2424
|
plugins: string[];
|
|
2434
|
-
onebotInstances: OneBotInstanceConfig[];
|
|
2435
2425
|
core: CoreSystemConfig;
|
|
2436
2426
|
}
|
|
2437
2427
|
|
|
@@ -2439,33 +2429,6 @@ function toStringArray(input: unknown): string[] {
|
|
|
2439
2429
|
return Array.isArray(input) ? input.map((v) => String(v)) : [];
|
|
2440
2430
|
}
|
|
2441
2431
|
|
|
2442
|
-
function normalizeOneBotInstances(input: unknown): OneBotInstanceConfig[] {
|
|
2443
|
-
if (!Array.isArray(input)) return [];
|
|
2444
|
-
return input
|
|
2445
|
-
.filter((item) => item && typeof item === "object")
|
|
2446
|
-
.map((item: any) => ({
|
|
2447
|
-
...item,
|
|
2448
|
-
protocol: item.protocol === "wss" ? "wss" : "ws",
|
|
2449
|
-
host: typeof item.host === "string" && item.host ? item.host : "localhost",
|
|
2450
|
-
port:
|
|
2451
|
-
typeof item.port === "number" && Number.isFinite(item.port)
|
|
2452
|
-
? item.port
|
|
2453
|
-
: 3001,
|
|
2454
|
-
token: typeof item.token === "string" ? item.token : "",
|
|
2455
|
-
reconnect: item.reconnect !== false,
|
|
2456
|
-
}));
|
|
2457
|
-
}
|
|
2458
|
-
|
|
2459
|
-
function getOneBotInstancesFromPkg(miokuConfig: any): OneBotInstanceConfig[] {
|
|
2460
|
-
const adapters =
|
|
2461
|
-
miokuConfig && typeof miokuConfig === "object" ? miokuConfig.adapters : null;
|
|
2462
|
-
const onebotv11 =
|
|
2463
|
-
adapters && typeof adapters === "object" ? adapters.onebotv11 : null;
|
|
2464
|
-
const instances =
|
|
2465
|
-
onebotv11 && typeof onebotv11 === "object" ? onebotv11.instances : null;
|
|
2466
|
-
return normalizeOneBotInstances(instances);
|
|
2467
|
-
}
|
|
2468
|
-
|
|
2469
2432
|
export function getMiokuConfig(): MiokuConfig {
|
|
2470
2433
|
const rootPkg = readRootPackageJsonSafe();
|
|
2471
2434
|
const miokuConfig =
|
|
@@ -2477,7 +2440,6 @@ export function getMiokuConfig(): MiokuConfig {
|
|
|
2477
2440
|
owners: toStringArray(miokuConfig.owners),
|
|
2478
2441
|
admins: toStringArray(miokuConfig.admins),
|
|
2479
2442
|
plugins: toStringArray(miokuConfig.plugins),
|
|
2480
|
-
onebotInstances: getOneBotInstancesFromPkg(miokuConfig),
|
|
2481
2443
|
core: getCoreSystemConfig(),
|
|
2482
2444
|
};
|
|
2483
2445
|
}
|
|
@@ -2494,12 +2456,10 @@ export function updateMiokuConfig(config: Partial<MiokuConfig>): MiokuConfig {
|
|
|
2494
2456
|
plugins: Array.isArray(config.plugins)
|
|
2495
2457
|
? config.plugins.map((v) => String(v))
|
|
2496
2458
|
: current.plugins,
|
|
2497
|
-
onebotInstances: Array.isArray(config.onebotInstances)
|
|
2498
|
-
? normalizeOneBotInstances(config.onebotInstances)
|
|
2499
|
-
: current.onebotInstances,
|
|
2500
2459
|
core: config.core ? updateCoreSystemConfig(config.core) : current.core,
|
|
2501
2460
|
};
|
|
2502
2461
|
|
|
2462
|
+
// 适配器配置(mioku.adapters)由适配器配置页维护,这里不再改写
|
|
2503
2463
|
const rootPkg = readRootPackageJson();
|
|
2504
2464
|
const miokuConfig =
|
|
2505
2465
|
typeof rootPkg.mioku === "object" && rootPkg.mioku !== null
|
|
@@ -2510,17 +2470,6 @@ export function updateMiokuConfig(config: Partial<MiokuConfig>): MiokuConfig {
|
|
|
2510
2470
|
miokuConfig.admins = updated.admins;
|
|
2511
2471
|
miokuConfig.plugins = updated.plugins;
|
|
2512
2472
|
|
|
2513
|
-
const adapters =
|
|
2514
|
-
typeof miokuConfig.adapters === "object" && miokuConfig.adapters !== null
|
|
2515
|
-
? miokuConfig.adapters
|
|
2516
|
-
: {};
|
|
2517
|
-
const onebotv11 =
|
|
2518
|
-
typeof adapters.onebotv11 === "object" && adapters.onebotv11 !== null
|
|
2519
|
-
? adapters.onebotv11
|
|
2520
|
-
: {};
|
|
2521
|
-
adapters.onebotv11 = { ...onebotv11, instances: updated.onebotInstances };
|
|
2522
|
-
miokuConfig.adapters = adapters;
|
|
2523
|
-
|
|
2524
2473
|
rootPkg.mioku = miokuConfig;
|
|
2525
2474
|
writeRootPackageJson(rootPkg);
|
|
2526
2475
|
|
package/utils.ts
CHANGED
|
@@ -3,6 +3,7 @@ import * as path from "node:path";
|
|
|
3
3
|
import { fileURLToPath } from "node:url";
|
|
4
4
|
import type { PackageManager, WebUISettings } from "./types";
|
|
5
5
|
import {
|
|
6
|
+
BOT_CWD,
|
|
6
7
|
getServiceConfigDir,
|
|
7
8
|
getServiceDataDir,
|
|
8
9
|
getConfigDir,
|
|
@@ -19,7 +20,13 @@ export const SETTINGS_PATH = path.join(
|
|
|
19
20
|
"settings.json",
|
|
20
21
|
);
|
|
21
22
|
export const AUTH_PATH = path.join(getServiceConfigDir("webui"), "auth.json");
|
|
22
|
-
|
|
23
|
+
|
|
24
|
+
/** 机器人根目录:进程 cwd 可能与 start({ cwd }) 不一致,以框架记录为准 */
|
|
25
|
+
export function projectRoot(): string {
|
|
26
|
+
return BOT_CWD.value || process.cwd();
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export const ROOT_PACKAGE_PATH = path.join(projectRoot(), "package.json");
|
|
23
30
|
|
|
24
31
|
export const WEBUI_DIST = path.join(__dirname, "dist");
|
|
25
32
|
export const CONFIG_DIR = getConfigDir();
|