@mebular/mcp 0.2.4 → 0.3.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/bin/mebular.mjs +606 -2
- package/package.json +5 -4
- package/src/admin.mjs +69 -47
- package/src/clients.mjs +699 -33
- package/src/config-apply.mjs +254 -6
- package/src/config-lock.mjs +167 -0
- package/src/config-schema.mjs +7 -0
- package/src/config.mjs +17 -1
- package/src/detail-maintenance.mjs +150 -0
- package/src/fleet-events.mjs +1 -1
- package/src/home-identity.mjs +13 -1
- package/src/memory-capability.mjs +5393 -0
- package/src/memory-runtime-config.mjs +86 -0
- package/src/memory-runtime-diagnostics.mjs +716 -0
- package/src/memory-runtime-supervisor.mjs +2670 -0
- package/src/memory-runtime-token.mjs +193 -0
- package/src/serve.mjs +1534 -16
- package/src/server.mjs +36 -4
- package/src/task-tools.mjs +10 -1
- package/src/version.mjs +1 -1
package/bin/mebular.mjs
CHANGED
|
@@ -6,7 +6,8 @@
|
|
|
6
6
|
import { randomUUID } from 'node:crypto';
|
|
7
7
|
import { existsSync } from 'node:fs';
|
|
8
8
|
import { chmod, mkdir, readFile, writeFile } from 'node:fs/promises';
|
|
9
|
-
import {
|
|
9
|
+
import { acquireConfigFileLock } from '../src/config-lock.mjs';
|
|
10
|
+
import { dirname, join, resolve } from 'node:path';
|
|
10
11
|
import { fileURLToPath } from 'node:url';
|
|
11
12
|
import { homeDir } from '../src/home.mjs';
|
|
12
13
|
import { serviceBuildSha } from '../src/build-sha.mjs';
|
|
@@ -211,6 +212,9 @@ async function runInit(flags) {
|
|
|
211
212
|
const keyFile = join(home, 'user-master-key.json');
|
|
212
213
|
await mkdir(home, { recursive: true });
|
|
213
214
|
|
|
215
|
+
const releaseConfigLock = await acquireConfigFileLock(configFile);
|
|
216
|
+
if (!releaseConfigLock) throw initError('配置文件正在被其他进程写入,请稍后重试', 'CONFIG_BUSY');
|
|
217
|
+
try {
|
|
214
218
|
const config = existsSync(configFile)
|
|
215
219
|
? await readInitJson(configFile, { label: 'config.json', code: 'CONFIG_INVALID' })
|
|
216
220
|
: null;
|
|
@@ -288,7 +292,7 @@ async function runInit(flags) {
|
|
|
288
292
|
}
|
|
289
293
|
|
|
290
294
|
if (config !== null) {
|
|
291
|
-
const migrated = await cleanLegacyConfig({ configPath: configFile, identity });
|
|
295
|
+
const migrated = await cleanLegacyConfig({ configPath: configFile, identity, lock: false });
|
|
292
296
|
console.log(`配置已存在,未覆盖:${configFile}`);
|
|
293
297
|
if (migrated.migrated) {
|
|
294
298
|
console.log(`已备份旧 config 身份字段:${migrated.backupPath},并以身份 ${identity.deviceId} 为准`);
|
|
@@ -308,6 +312,9 @@ async function runInit(flags) {
|
|
|
308
312
|
await writeFile(configFile, JSON.stringify(nextConfig, null, 2), 'utf-8');
|
|
309
313
|
console.log(`已写入配置:${configFile}(deviceId=${identity.deviceId},由身份文件提供)`);
|
|
310
314
|
}
|
|
315
|
+
} finally {
|
|
316
|
+
await releaseConfigLock();
|
|
317
|
+
}
|
|
311
318
|
// Skill 是部署产物:初始化成功后对已检测到的助手自动 ensure;失败只告警,不阻断 init。
|
|
312
319
|
try {
|
|
313
320
|
const { autoEnsureSkills, assistantUserHome } = await import('../src/clients.mjs');
|
|
@@ -1117,6 +1124,292 @@ async function runCloud(action, args, flags) {
|
|
|
1117
1124
|
process.exit(2);
|
|
1118
1125
|
}
|
|
1119
1126
|
|
|
1127
|
+
// ---------- 本地模型:mebular models list/status/install/import/remove/start/stop/test ----------
|
|
1128
|
+
|
|
1129
|
+
function modelsUsage() {
|
|
1130
|
+
return '用法:mebular models <list|status|install|download|select|remove|start|stop> [--json] [--pack P] [--force] [--token T](需要 `mebular serve`;import/test/pause/resume 暂不支持)';
|
|
1131
|
+
}
|
|
1132
|
+
|
|
1133
|
+
async function modelsDaemonBase(home) {
|
|
1134
|
+
const { readRuntimeFile } = await import('../src/runtime-file.mjs');
|
|
1135
|
+
const record = await readRuntimeFile(home).catch(() => null);
|
|
1136
|
+
const host = record?.http?.host;
|
|
1137
|
+
const port = record?.http?.port;
|
|
1138
|
+
if (typeof host !== 'string' || !Number.isInteger(port)) return null;
|
|
1139
|
+
const displayHost = host.includes(':') && !host.startsWith('[') ? `[${host}]` : host;
|
|
1140
|
+
const base = `http://${displayHost}:${port}`;
|
|
1141
|
+
try {
|
|
1142
|
+
const response = await fetch(`${base}/healthz`, { signal: AbortSignal.timeout(1200) });
|
|
1143
|
+
return response.ok ? base : null;
|
|
1144
|
+
} catch {
|
|
1145
|
+
return null;
|
|
1146
|
+
}
|
|
1147
|
+
}
|
|
1148
|
+
|
|
1149
|
+
async function modelsAdminCsrf(base) {
|
|
1150
|
+
const response = await fetch(`${base}/console/`, { signal: AbortSignal.timeout(3000) });
|
|
1151
|
+
const token = response.headers.get('x-mebular-csrf');
|
|
1152
|
+
const cookies = typeof response.headers.getSetCookie === 'function' ? response.headers.getSetCookie() : [];
|
|
1153
|
+
const cookie = cookies.find((value) => value.startsWith('mebular_csrf='))?.split(';')[0] ?? null;
|
|
1154
|
+
return token && cookie ? { token, cookie } : null;
|
|
1155
|
+
}
|
|
1156
|
+
|
|
1157
|
+
async function modelsAdminRequest(base, { method = 'GET', path, body = null, flags = {} } = {}) {
|
|
1158
|
+
const headers = { accept: 'application/json' };
|
|
1159
|
+
const token = typeof flags.token === 'string'
|
|
1160
|
+
? flags.token
|
|
1161
|
+
: process.env.MEBULAR_ADMIN_TOKEN;
|
|
1162
|
+
if (typeof token === 'string' && token.length > 0) headers.authorization = `Bearer ${token}`;
|
|
1163
|
+
if (method !== 'GET') {
|
|
1164
|
+
const csrf = await modelsAdminCsrf(base).catch(() => null);
|
|
1165
|
+
if (!csrf) {
|
|
1166
|
+
return { status: 403, body: { ok: false, error: { code: 'csrf_unavailable', message: '无法从守护获取 CSRF token' } } };
|
|
1167
|
+
}
|
|
1168
|
+
headers['x-mebular-csrf'] = csrf.token;
|
|
1169
|
+
headers.cookie = csrf.cookie;
|
|
1170
|
+
headers['content-type'] = 'application/json';
|
|
1171
|
+
}
|
|
1172
|
+
const response = await fetch(`${base}${path}`, {
|
|
1173
|
+
method,
|
|
1174
|
+
headers,
|
|
1175
|
+
...(body !== null ? { body: JSON.stringify(body) } : {}),
|
|
1176
|
+
signal: AbortSignal.timeout(10 * 60_000),
|
|
1177
|
+
});
|
|
1178
|
+
const data = await response.json().catch(() => null);
|
|
1179
|
+
if (!data || typeof data !== 'object') {
|
|
1180
|
+
return { status: response.status, body: { ok: false, error: { code: 'invalid_response', message: '守护返回了非 JSON 响应' } } };
|
|
1181
|
+
}
|
|
1182
|
+
// 现有 admin 门禁返回 { error: 'unauthorized', message };CLI 统一成结构化 error。
|
|
1183
|
+
if (typeof data.ok !== 'boolean' && typeof data.error === 'string') {
|
|
1184
|
+
return {
|
|
1185
|
+
status: response.status,
|
|
1186
|
+
body: {
|
|
1187
|
+
...data,
|
|
1188
|
+
ok: false,
|
|
1189
|
+
error: { code: data.error, message: typeof data.message === 'string' ? data.message : data.error },
|
|
1190
|
+
code: data.error,
|
|
1191
|
+
},
|
|
1192
|
+
};
|
|
1193
|
+
}
|
|
1194
|
+
return { status: response.status, body: data };
|
|
1195
|
+
}
|
|
1196
|
+
|
|
1197
|
+
function modelCliError(error) {
|
|
1198
|
+
if (error && typeof error.toBody === 'function') return error.toBody();
|
|
1199
|
+
if (error?.code && typeof error.code === 'string') {
|
|
1200
|
+
return {
|
|
1201
|
+
ok: false,
|
|
1202
|
+
error: { code: error.code, message: String(error.message ?? error), retryable: false },
|
|
1203
|
+
code: error.code,
|
|
1204
|
+
message: String(error.message ?? error),
|
|
1205
|
+
};
|
|
1206
|
+
}
|
|
1207
|
+
return {
|
|
1208
|
+
ok: false,
|
|
1209
|
+
error: { code: 'models_failed', message: String(error?.message ?? error), retryable: false },
|
|
1210
|
+
code: 'models_failed',
|
|
1211
|
+
message: String(error?.message ?? error),
|
|
1212
|
+
};
|
|
1213
|
+
}
|
|
1214
|
+
|
|
1215
|
+
function printModelsResult(command, body, { json = false } = {}) {
|
|
1216
|
+
const payload = body && typeof body === 'object' ? { ...body, command } : { ok: false, command, error: { code: 'models_failed', message: '空响应' } };
|
|
1217
|
+
if (json) {
|
|
1218
|
+
console.log(JSON.stringify(payload, null, 2));
|
|
1219
|
+
return;
|
|
1220
|
+
}
|
|
1221
|
+
if (payload.ok !== true) {
|
|
1222
|
+
console.error(`✗ ${payload.error?.code ?? 'models_failed'}:${payload.error?.message ?? payload.message ?? '失败'}`);
|
|
1223
|
+
return;
|
|
1224
|
+
}
|
|
1225
|
+
if (command === 'models.list' || command === 'models.status') {
|
|
1226
|
+
const packs = Array.isArray(payload.packs) ? payload.packs : [];
|
|
1227
|
+
console.log(`selected: ${payload.effectivePack ?? payload.selectedPack ?? '(none)'}`);
|
|
1228
|
+
for (const pack of packs) {
|
|
1229
|
+
console.log(`${pack.packId}\t${pack.state}${pack.selected ? '\tselected' : ''}`);
|
|
1230
|
+
}
|
|
1231
|
+
return;
|
|
1232
|
+
}
|
|
1233
|
+
console.log(JSON.stringify(payload, null, 2));
|
|
1234
|
+
}
|
|
1235
|
+
|
|
1236
|
+
const MODELS_UNSUPPORTED = new Set(['import', 'test', 'pause', 'resume']);
|
|
1237
|
+
const MODELS_SUPPORTED = new Set(['list', 'status', 'install', 'download', 'select', 'remove', 'uninstall', 'start', 'stop']);
|
|
1238
|
+
|
|
1239
|
+
function daemonUnavailable(command) {
|
|
1240
|
+
return {
|
|
1241
|
+
ok: false,
|
|
1242
|
+
command,
|
|
1243
|
+
error: {
|
|
1244
|
+
code: 'daemon_unavailable',
|
|
1245
|
+
message: '模型目录/安装/引擎由 Memory Runtime sidecar 拥有;请先启动 `mebular serve`(沉浸式)',
|
|
1246
|
+
retryable: false,
|
|
1247
|
+
},
|
|
1248
|
+
code: 'daemon_unavailable',
|
|
1249
|
+
message: 'Memory Runtime sidecar 未运行',
|
|
1250
|
+
};
|
|
1251
|
+
}
|
|
1252
|
+
|
|
1253
|
+
function unsupportedModelCommand(command, sub) {
|
|
1254
|
+
return {
|
|
1255
|
+
ok: false,
|
|
1256
|
+
command,
|
|
1257
|
+
error: {
|
|
1258
|
+
code: 'unsupported',
|
|
1259
|
+
message: `models ${sub} 暂不支持:sidecar 当前只提供 list/status/install/select/remove/start/stop`,
|
|
1260
|
+
retryable: false,
|
|
1261
|
+
},
|
|
1262
|
+
code: 'unsupported',
|
|
1263
|
+
message: `models ${sub} 暂不支持`,
|
|
1264
|
+
};
|
|
1265
|
+
}
|
|
1266
|
+
|
|
1267
|
+
async function waitForModelInstall(daemon, jobId, flags) {
|
|
1268
|
+
const deadline = Date.now() + 10 * 60_000;
|
|
1269
|
+
for (;;) {
|
|
1270
|
+
const status = await modelsAdminRequest(daemon, { path: '/admin/api/models/status', flags });
|
|
1271
|
+
const job = status.body?.install;
|
|
1272
|
+
if (job && typeof job === 'object' && job.jobId === jobId) {
|
|
1273
|
+
if (job.state === 'completed') {
|
|
1274
|
+
return {
|
|
1275
|
+
status: 200,
|
|
1276
|
+
body: {
|
|
1277
|
+
ok: true,
|
|
1278
|
+
pack: job.packId,
|
|
1279
|
+
state: 'installed',
|
|
1280
|
+
job,
|
|
1281
|
+
warnings: Array.isArray(job.warnings) ? job.warnings : [],
|
|
1282
|
+
},
|
|
1283
|
+
};
|
|
1284
|
+
}
|
|
1285
|
+
if (['failed', 'cancelled', 'interrupted'].includes(job.state)) {
|
|
1286
|
+
return {
|
|
1287
|
+
status: 409,
|
|
1288
|
+
body: {
|
|
1289
|
+
ok: false,
|
|
1290
|
+
pack: job.packId,
|
|
1291
|
+
state: job.state,
|
|
1292
|
+
job,
|
|
1293
|
+
error: job.error ?? {
|
|
1294
|
+
code: `install_${job.state}`,
|
|
1295
|
+
message: `模型安装${job.state}`,
|
|
1296
|
+
retryable: job.state === 'interrupted',
|
|
1297
|
+
},
|
|
1298
|
+
},
|
|
1299
|
+
};
|
|
1300
|
+
}
|
|
1301
|
+
}
|
|
1302
|
+
if (Date.now() >= deadline) {
|
|
1303
|
+
return {
|
|
1304
|
+
status: 504,
|
|
1305
|
+
body: {
|
|
1306
|
+
ok: false,
|
|
1307
|
+
pack: null,
|
|
1308
|
+
jobId,
|
|
1309
|
+
error: { code: 'install_timeout', message: '等待模型安装超时', retryable: true },
|
|
1310
|
+
},
|
|
1311
|
+
};
|
|
1312
|
+
}
|
|
1313
|
+
await new Promise((resolvePromise) => setTimeout(resolvePromise, 500));
|
|
1314
|
+
}
|
|
1315
|
+
}
|
|
1316
|
+
|
|
1317
|
+
async function runModels(args, flags) {
|
|
1318
|
+
const sub = typeof args[0] === 'string' ? args[0] : 'list';
|
|
1319
|
+
const json = flags.json === true;
|
|
1320
|
+
const home = homeDir();
|
|
1321
|
+
const command = `models.${sub}`;
|
|
1322
|
+
try {
|
|
1323
|
+
if (MODELS_UNSUPPORTED.has(sub)) {
|
|
1324
|
+
printModelsResult(command, unsupportedModelCommand(command, sub), { json });
|
|
1325
|
+
process.exit(2);
|
|
1326
|
+
}
|
|
1327
|
+
if (!MODELS_SUPPORTED.has(sub)) {
|
|
1328
|
+
throw Object.assign(new Error(modelsUsage()), { code: 'USAGE' });
|
|
1329
|
+
}
|
|
1330
|
+
|
|
1331
|
+
// 模型目录/下载/安装/引擎只由 sidecar 拥有;CLI 没有 daemon 时不再本地兜底。
|
|
1332
|
+
const daemon = await modelsDaemonBase(home);
|
|
1333
|
+
if (!daemon) {
|
|
1334
|
+
printModelsResult(command, daemonUnavailable(command), { json });
|
|
1335
|
+
process.exit(2);
|
|
1336
|
+
}
|
|
1337
|
+
|
|
1338
|
+
if (sub === 'list' || sub === 'status') {
|
|
1339
|
+
const result = await modelsAdminRequest(daemon, {
|
|
1340
|
+
path: sub === 'status' ? '/admin/api/models/status' : '/admin/api/models',
|
|
1341
|
+
flags,
|
|
1342
|
+
});
|
|
1343
|
+
printModelsResult(command, result.body, { json });
|
|
1344
|
+
if (result.status >= 400) process.exit(2);
|
|
1345
|
+
return;
|
|
1346
|
+
}
|
|
1347
|
+
|
|
1348
|
+
if (sub === 'install' || sub === 'download') {
|
|
1349
|
+
const pack = args[1] ?? flags.pack;
|
|
1350
|
+
const result = await modelsAdminRequest(daemon, {
|
|
1351
|
+
method: 'POST',
|
|
1352
|
+
path: '/admin/api/models/install',
|
|
1353
|
+
body: {
|
|
1354
|
+
pack,
|
|
1355
|
+
...(flags.force === true ? { force: true } : {}),
|
|
1356
|
+
},
|
|
1357
|
+
flags,
|
|
1358
|
+
});
|
|
1359
|
+
if (result.status >= 400 || result.body?.ok === false) {
|
|
1360
|
+
printModelsResult(command, result.body, { json });
|
|
1361
|
+
process.exit(2);
|
|
1362
|
+
}
|
|
1363
|
+
const jobId = typeof result.body?.job?.jobId === 'string' ? result.body.job.jobId : null;
|
|
1364
|
+
const final = jobId ? await waitForModelInstall(daemon, jobId, flags) : result;
|
|
1365
|
+
printModelsResult(command, final.body, { json });
|
|
1366
|
+
if (final.status >= 400 || final.body?.ok === false) process.exit(2);
|
|
1367
|
+
return;
|
|
1368
|
+
}
|
|
1369
|
+
|
|
1370
|
+
if (sub === 'select') {
|
|
1371
|
+
const result = await modelsAdminRequest(daemon, {
|
|
1372
|
+
method: 'POST',
|
|
1373
|
+
path: '/admin/api/models/select',
|
|
1374
|
+
body: { pack: args[1] ?? flags.pack },
|
|
1375
|
+
flags,
|
|
1376
|
+
});
|
|
1377
|
+
printModelsResult(command, result.body, { json });
|
|
1378
|
+
if (result.status >= 400 || result.body?.ok === false) process.exit(2);
|
|
1379
|
+
return;
|
|
1380
|
+
}
|
|
1381
|
+
|
|
1382
|
+
if (sub === 'remove' || sub === 'uninstall') {
|
|
1383
|
+
const result = await modelsAdminRequest(daemon, {
|
|
1384
|
+
method: 'POST',
|
|
1385
|
+
path: '/admin/api/models/remove',
|
|
1386
|
+
body: { pack: args[1] ?? flags.pack },
|
|
1387
|
+
flags,
|
|
1388
|
+
});
|
|
1389
|
+
printModelsResult(command, result.body, { json });
|
|
1390
|
+
if (result.status >= 400 || result.body?.ok === false) process.exit(2);
|
|
1391
|
+
return;
|
|
1392
|
+
}
|
|
1393
|
+
|
|
1394
|
+
if (sub === 'start' || sub === 'stop') {
|
|
1395
|
+
const result = await modelsAdminRequest(daemon, {
|
|
1396
|
+
method: 'POST',
|
|
1397
|
+
path: `/admin/api/models/${sub}`,
|
|
1398
|
+
body: { pack: args[1] ?? flags.pack },
|
|
1399
|
+
flags,
|
|
1400
|
+
});
|
|
1401
|
+
printModelsResult(command, result.body, { json });
|
|
1402
|
+
if (result.status >= 400 || result.body?.ok === false) process.exit(2);
|
|
1403
|
+
return;
|
|
1404
|
+
}
|
|
1405
|
+
|
|
1406
|
+
throw Object.assign(new Error(modelsUsage()), { code: 'USAGE' });
|
|
1407
|
+
} catch (error) {
|
|
1408
|
+
printModelsResult(command, modelCliError(error), { json });
|
|
1409
|
+
process.exit(2);
|
|
1410
|
+
}
|
|
1411
|
+
}
|
|
1412
|
+
|
|
1120
1413
|
// ---------- 共享 runtime:resolve / ensure / status + provider 注册表 ----------
|
|
1121
1414
|
|
|
1122
1415
|
function runtimeCliError(error) {
|
|
@@ -1503,6 +1796,303 @@ async function runRuntimeUpgrade(flags) {
|
|
|
1503
1796
|
printRuntimeJson(result, result.ok ? 0 : 2);
|
|
1504
1797
|
}
|
|
1505
1798
|
|
|
1799
|
+
// ---------- Memory Runtime sidecar(`mebular memory-runtime ...`) ----------
|
|
1800
|
+
//
|
|
1801
|
+
// 与 `mebular runtime ...`(MCP daemon)严格分离:这里只管理 `mebular-memory-runtime serve`。
|
|
1802
|
+
// 输出稳定 JSON,不返回 host token / capability token / tokenFile。
|
|
1803
|
+
|
|
1804
|
+
async function readMemoryRuntimeConfig(home) {
|
|
1805
|
+
try {
|
|
1806
|
+
const file = process.env.MEBULAR_CONFIG ?? join(home, 'config.json');
|
|
1807
|
+
return JSON.parse(await readFile(file, 'utf-8'));
|
|
1808
|
+
} catch {
|
|
1809
|
+
return {};
|
|
1810
|
+
}
|
|
1811
|
+
}
|
|
1812
|
+
|
|
1813
|
+
function memoryRuntimeHome(flags) {
|
|
1814
|
+
const explicit = typeof flags.home === 'string' && flags.home.length > 0 ? flags.home : null;
|
|
1815
|
+
return explicit ? resolve(explicit) : resolve(homeDir());
|
|
1816
|
+
}
|
|
1817
|
+
|
|
1818
|
+
function memoryRuntimeTimeout(flags) {
|
|
1819
|
+
if (flags.timeout === undefined) return 5000;
|
|
1820
|
+
const value = Number(flags.timeout);
|
|
1821
|
+
if (!Number.isInteger(value) || value < 0 || value > 120_000) {
|
|
1822
|
+
const error = new Error('--timeout 必须是 0–120000 的整数(毫秒)');
|
|
1823
|
+
error.code = 'USAGE';
|
|
1824
|
+
throw error;
|
|
1825
|
+
}
|
|
1826
|
+
return value;
|
|
1827
|
+
}
|
|
1828
|
+
|
|
1829
|
+
function printMemoryRuntime(value, exitCode = 0) {
|
|
1830
|
+
console.log(JSON.stringify(value, null, 2));
|
|
1831
|
+
process.exit(exitCode);
|
|
1832
|
+
}
|
|
1833
|
+
|
|
1834
|
+
async function runMemoryRuntime(action, args, flags) {
|
|
1835
|
+
const usage = '用法:mebular memory-runtime <status|start|stop|restart|logs|doctor|outbox> [--json] [--home DIR] [--timeout MS] [logs --limit N|--tail N]';
|
|
1836
|
+
try {
|
|
1837
|
+
const actions = ['status', 'start', 'stop', 'restart', 'logs', 'doctor', 'outbox'];
|
|
1838
|
+
if (!actions.includes(action)) {
|
|
1839
|
+
const error = new Error(usage);
|
|
1840
|
+
error.code = 'USAGE';
|
|
1841
|
+
throw error;
|
|
1842
|
+
}
|
|
1843
|
+
const home = memoryRuntimeHome(flags);
|
|
1844
|
+
const config = await readMemoryRuntimeConfig(home);
|
|
1845
|
+
const timeoutMs = memoryRuntimeTimeout(flags);
|
|
1846
|
+
const {
|
|
1847
|
+
MemoryRuntimeSupervisor,
|
|
1848
|
+
inspectMemoryRuntimeStatus,
|
|
1849
|
+
memoryRuntimeStatusExitCode,
|
|
1850
|
+
resolveMemoryRuntimePaths,
|
|
1851
|
+
} = await import('../src/memory-runtime-supervisor.mjs');
|
|
1852
|
+
const {
|
|
1853
|
+
assertSafeMemoryRuntimeDataDir,
|
|
1854
|
+
inspectMemoryRuntimeOutbox,
|
|
1855
|
+
inspectMemoryRuntimeLocalCounts,
|
|
1856
|
+
memoryRuntimeDiskUsage,
|
|
1857
|
+
memoryRuntimeLogStatus,
|
|
1858
|
+
readMemoryRuntimeLogs,
|
|
1859
|
+
rebuildMemoryRuntimeOutboxItem,
|
|
1860
|
+
purgeMemoryRuntimeOutboxItem,
|
|
1861
|
+
sanitizeMemoryRuntimeEvent,
|
|
1862
|
+
} = await import('../src/memory-runtime-diagnostics.mjs');
|
|
1863
|
+
const dataDir = assertSafeMemoryRuntimeDataDir(
|
|
1864
|
+
resolveMemoryRuntimePaths({ home, env: process.env }).dataDir,
|
|
1865
|
+
home,
|
|
1866
|
+
);
|
|
1867
|
+
|
|
1868
|
+
if (action === 'logs') {
|
|
1869
|
+
const limitFlag = flags.limit === undefined ? flags.tail : flags.limit;
|
|
1870
|
+
const limitRaw = limitFlag === undefined ? 50 : Number(limitFlag);
|
|
1871
|
+
const limit = Number.isInteger(limitRaw) && limitRaw > 0 && limitRaw <= 500 ? limitRaw : 50;
|
|
1872
|
+
const [parsedEntries, status] = await Promise.all([
|
|
1873
|
+
readMemoryRuntimeLogs(dataDir, { limit }),
|
|
1874
|
+
memoryRuntimeLogStatus(dataDir),
|
|
1875
|
+
]);
|
|
1876
|
+
const entries = parsedEntries.map((entry) => sanitizeMemoryRuntimeEvent(entry));
|
|
1877
|
+
printMemoryRuntime({
|
|
1878
|
+
ok: true,
|
|
1879
|
+
kind: 'memory-runtime',
|
|
1880
|
+
action,
|
|
1881
|
+
count: entries.length,
|
|
1882
|
+
truncated: entries.length >= limit,
|
|
1883
|
+
log: status,
|
|
1884
|
+
entries,
|
|
1885
|
+
}, 0);
|
|
1886
|
+
return;
|
|
1887
|
+
}
|
|
1888
|
+
|
|
1889
|
+
if (action === 'doctor') {
|
|
1890
|
+
const supervisor = new MemoryRuntimeSupervisor({ home, config, env: process.env });
|
|
1891
|
+
const [status, runtime, outbox, localCounts, disk, logStatus, logs] = await Promise.all([
|
|
1892
|
+
inspectMemoryRuntimeStatus({ home, config, env: process.env }),
|
|
1893
|
+
supervisor.readStatus().catch(() => null),
|
|
1894
|
+
inspectMemoryRuntimeOutbox(dataDir, { now: Date.now() }),
|
|
1895
|
+
inspectMemoryRuntimeLocalCounts(dataDir),
|
|
1896
|
+
memoryRuntimeDiskUsage(dataDir),
|
|
1897
|
+
memoryRuntimeLogStatus(dataDir),
|
|
1898
|
+
readMemoryRuntimeLogs(dataDir, { limit: 100 }),
|
|
1899
|
+
]);
|
|
1900
|
+
const recentErrors = logs
|
|
1901
|
+
.filter((entry) => entry.level === 'error' || entry.level === 'warn')
|
|
1902
|
+
.map((entry) => ({
|
|
1903
|
+
ts: entry.ts,
|
|
1904
|
+
level: entry.level,
|
|
1905
|
+
event: entry.event,
|
|
1906
|
+
...(typeof entry.code === 'string' ? { code: entry.code } : {}),
|
|
1907
|
+
...(typeof entry.reason === 'string' ? { reason: entry.reason } : {}),
|
|
1908
|
+
...(typeof entry.stage === 'string' ? { stage: entry.stage } : {}),
|
|
1909
|
+
...(entry.possibleCommit === true ? { possibleCommit: true } : {}),
|
|
1910
|
+
}));
|
|
1911
|
+
for (const [source, error] of [['sidecar', status.error], ['runtime', runtime?.error]]) {
|
|
1912
|
+
if (!error?.code) continue;
|
|
1913
|
+
recentErrors.push({
|
|
1914
|
+
ts: Date.now(),
|
|
1915
|
+
level: 'error',
|
|
1916
|
+
event: `${source}_status`,
|
|
1917
|
+
code: error.code,
|
|
1918
|
+
});
|
|
1919
|
+
}
|
|
1920
|
+
recentErrors.splice(0, Math.max(0, recentErrors.length - 10));
|
|
1921
|
+
const restartEvents = logs.filter((entry) => entry.event === 'supervisor_restart');
|
|
1922
|
+
const lastRestart = restartEvents[restartEvents.length - 1]
|
|
1923
|
+
?? [...logs].reverse().find((entry) => entry.event === 'runtime_start' || entry.event === 'runtime_resume');
|
|
1924
|
+
const restartReason = typeof lastRestart?.reason === 'string'
|
|
1925
|
+
? lastRestart.reason
|
|
1926
|
+
: (status.reason ?? null);
|
|
1927
|
+
const next = [];
|
|
1928
|
+
if (status.error?.code) next.push(`sidecar 错误:${status.error.code}`);
|
|
1929
|
+
if (outbox.dead > 0) next.push(`${outbox.dead} 个 dead 操作:先看 deadReasons/lastConflict,再显式 outbox rebuild/purge`);
|
|
1930
|
+
if (outbox.lastConflict) next.push(`最近冲突 ${outbox.lastConflict.code}(requestId=${outbox.lastConflict.requestId})`);
|
|
1931
|
+
if (outbox.ambiguous.length > 0) next.push('存在超时/部分写入:结果未知,需按 committedObjects 做权威回读对账,不能当作已提交');
|
|
1932
|
+
if (runtime?.degraded) next.push(`运行时降级:${runtime.error?.code ?? 'degraded'}`);
|
|
1933
|
+
if (recentErrors.length === 0) next.push('最近没有日志错误');
|
|
1934
|
+
printMemoryRuntime({
|
|
1935
|
+
ok: true,
|
|
1936
|
+
kind: 'memory-runtime',
|
|
1937
|
+
action,
|
|
1938
|
+
enabled: status.enabled,
|
|
1939
|
+
available: status.available,
|
|
1940
|
+
state: status.state,
|
|
1941
|
+
degraded: status.degraded === true || outbox.dead > 0 || outbox.ambiguous.length > 0 || runtime?.degraded === true,
|
|
1942
|
+
installed: status.installed,
|
|
1943
|
+
restartReason,
|
|
1944
|
+
capability: runtime?.capability ?? null,
|
|
1945
|
+
model: runtime?.model ?? null,
|
|
1946
|
+
counts: {
|
|
1947
|
+
staging: localCounts.stagingChunks,
|
|
1948
|
+
traces: localCounts.traces,
|
|
1949
|
+
cache: localCounts.cacheEntries,
|
|
1950
|
+
detailCache: localCounts.detailCacheEntries,
|
|
1951
|
+
outbox: outbox.active,
|
|
1952
|
+
dead: outbox.dead,
|
|
1953
|
+
},
|
|
1954
|
+
outbox: {
|
|
1955
|
+
active: outbox.active,
|
|
1956
|
+
dead: outbox.dead,
|
|
1957
|
+
oldestAgeMs: outbox.oldestAgeMs,
|
|
1958
|
+
deadOldestAgeMs: outbox.deadOldestAgeMs,
|
|
1959
|
+
byOperation: outbox.byOperation,
|
|
1960
|
+
byEndpoint: outbox.byEndpoint,
|
|
1961
|
+
deadReasons: outbox.deadReasons,
|
|
1962
|
+
lastConflict: outbox.lastConflict,
|
|
1963
|
+
ambiguous: outbox.ambiguous,
|
|
1964
|
+
},
|
|
1965
|
+
logs: logStatus,
|
|
1966
|
+
disk,
|
|
1967
|
+
recentErrors,
|
|
1968
|
+
next,
|
|
1969
|
+
}, 0);
|
|
1970
|
+
return;
|
|
1971
|
+
}
|
|
1972
|
+
|
|
1973
|
+
if (action === 'outbox') {
|
|
1974
|
+
const sub = args[0] ?? 'list';
|
|
1975
|
+
if (sub === 'list') {
|
|
1976
|
+
const summary = await inspectMemoryRuntimeOutbox(dataDir, { now: Date.now(), includeItems: true });
|
|
1977
|
+
printMemoryRuntime({ ok: true, kind: 'memory-runtime', action, sub, ...summary }, 0);
|
|
1978
|
+
return;
|
|
1979
|
+
}
|
|
1980
|
+
const id = args[1];
|
|
1981
|
+
if (typeof id !== 'string' || id.length === 0) {
|
|
1982
|
+
const error = new Error('outbox 命令需要 id:list|show|rebuild|purge');
|
|
1983
|
+
error.code = 'USAGE';
|
|
1984
|
+
throw error;
|
|
1985
|
+
}
|
|
1986
|
+
if (sub === 'show') {
|
|
1987
|
+
const summary = await inspectMemoryRuntimeOutbox(dataDir, { now: Date.now(), includeItems: true });
|
|
1988
|
+
const item = summary.items.find((entry) => entry.id === id || entry.requestId === id);
|
|
1989
|
+
if (!item) {
|
|
1990
|
+
printMemoryRuntime({
|
|
1991
|
+
ok: false,
|
|
1992
|
+
kind: 'memory-runtime',
|
|
1993
|
+
action,
|
|
1994
|
+
sub,
|
|
1995
|
+
error: { code: 'outbox_not_found', message: 'outbox 项不存在' },
|
|
1996
|
+
}, 1);
|
|
1997
|
+
return;
|
|
1998
|
+
}
|
|
1999
|
+
printMemoryRuntime({ ok: true, kind: 'memory-runtime', action, sub, item }, 0);
|
|
2000
|
+
return;
|
|
2001
|
+
}
|
|
2002
|
+
if (sub === 'rebuild') {
|
|
2003
|
+
let expectedRevision;
|
|
2004
|
+
const revisionFlag = flags['expected-revision'];
|
|
2005
|
+
if (revisionFlag !== undefined) {
|
|
2006
|
+
if (typeof revisionFlag !== 'string' || !/^(?:0|[1-9]\d*)$/.test(revisionFlag)) {
|
|
2007
|
+
const error = new Error('--expected-revision 必须显式提供非负整数');
|
|
2008
|
+
error.code = 'usage';
|
|
2009
|
+
throw error;
|
|
2010
|
+
}
|
|
2011
|
+
expectedRevision = Number(revisionFlag);
|
|
2012
|
+
if (!Number.isSafeInteger(expectedRevision)) {
|
|
2013
|
+
const error = new Error('--expected-revision 超出安全整数范围');
|
|
2014
|
+
error.code = 'usage';
|
|
2015
|
+
throw error;
|
|
2016
|
+
}
|
|
2017
|
+
}
|
|
2018
|
+
const item = await rebuildMemoryRuntimeOutboxItem(dataDir, id, {
|
|
2019
|
+
...(expectedRevision !== undefined ? { expectedRevision } : {}),
|
|
2020
|
+
now: Date.now(),
|
|
2021
|
+
});
|
|
2022
|
+
printMemoryRuntime({
|
|
2023
|
+
ok: true,
|
|
2024
|
+
kind: 'memory-runtime',
|
|
2025
|
+
action,
|
|
2026
|
+
sub,
|
|
2027
|
+
item,
|
|
2028
|
+
note: expectedRevision === undefined
|
|
2029
|
+
? '已按原 revision 重建;不会自动 rebase 到新 revision'
|
|
2030
|
+
: '已按显式 --expected-revision 重建(人工覆盖,非自动)',
|
|
2031
|
+
}, 0);
|
|
2032
|
+
return;
|
|
2033
|
+
}
|
|
2034
|
+
if (sub === 'purge') {
|
|
2035
|
+
const result = await purgeMemoryRuntimeOutboxItem(dataDir, id);
|
|
2036
|
+
printMemoryRuntime({ ok: true, kind: 'memory-runtime', action, sub, ...result }, 0);
|
|
2037
|
+
return;
|
|
2038
|
+
}
|
|
2039
|
+
const error = new Error('outbox 子命令:list|show|rebuild|purge');
|
|
2040
|
+
error.code = 'USAGE';
|
|
2041
|
+
throw error;
|
|
2042
|
+
}
|
|
2043
|
+
|
|
2044
|
+
if (action === 'status') {
|
|
2045
|
+
const status = await inspectMemoryRuntimeStatus({ home, config, env: process.env });
|
|
2046
|
+
printMemoryRuntime({
|
|
2047
|
+
ok: true,
|
|
2048
|
+
kind: 'memory-runtime',
|
|
2049
|
+
action,
|
|
2050
|
+
...status,
|
|
2051
|
+
}, 0);
|
|
2052
|
+
return;
|
|
2053
|
+
}
|
|
2054
|
+
|
|
2055
|
+
const supervisor = new MemoryRuntimeSupervisor({
|
|
2056
|
+
home,
|
|
2057
|
+
config,
|
|
2058
|
+
env: process.env,
|
|
2059
|
+
});
|
|
2060
|
+
|
|
2061
|
+
if (action === 'stop') {
|
|
2062
|
+
const status = await supervisor.stop({ includeAttached: true, timeoutMs });
|
|
2063
|
+
printMemoryRuntime({
|
|
2064
|
+
ok: true,
|
|
2065
|
+
kind: 'memory-runtime',
|
|
2066
|
+
action,
|
|
2067
|
+
...status,
|
|
2068
|
+
}, 0);
|
|
2069
|
+
return;
|
|
2070
|
+
}
|
|
2071
|
+
|
|
2072
|
+
const status = action === 'restart'
|
|
2073
|
+
? await supervisor.restart({ includeAttached: true, timeoutMs, manual: true })
|
|
2074
|
+
: await supervisor.start({ force: true, manual: true });
|
|
2075
|
+
const ok = status.state === 'ready' || status.state === 'degraded';
|
|
2076
|
+
printMemoryRuntime({
|
|
2077
|
+
ok,
|
|
2078
|
+
kind: 'memory-runtime',
|
|
2079
|
+
action,
|
|
2080
|
+
...status,
|
|
2081
|
+
}, ok ? 0 : memoryRuntimeStatusExitCode(status));
|
|
2082
|
+
} catch (error) {
|
|
2083
|
+
const code = error?.code === 'USAGE' || error?.code === 'usage' ? 'usage' : (error?.code ?? 'error');
|
|
2084
|
+
printMemoryRuntime({
|
|
2085
|
+
ok: false,
|
|
2086
|
+
kind: 'memory-runtime',
|
|
2087
|
+
action: action ?? null,
|
|
2088
|
+
error: {
|
|
2089
|
+
code,
|
|
2090
|
+
message: error?.message ?? String(error),
|
|
2091
|
+
},
|
|
2092
|
+
}, code === 'usage' ? 2 : 1);
|
|
2093
|
+
}
|
|
2094
|
+
}
|
|
2095
|
+
|
|
1506
2096
|
async function runRuntime(action, args, flags) {
|
|
1507
2097
|
try {
|
|
1508
2098
|
if (action === 'provider') {
|
|
@@ -1670,6 +2260,14 @@ async function main() {
|
|
|
1670
2260
|
await runRuntime(argv[1], argv.slice(2), flags);
|
|
1671
2261
|
return;
|
|
1672
2262
|
}
|
|
2263
|
+
case 'memory-runtime': {
|
|
2264
|
+
await runMemoryRuntime(argv[1], argv.slice(2), flags);
|
|
2265
|
+
return;
|
|
2266
|
+
}
|
|
2267
|
+
case 'models': {
|
|
2268
|
+
await runModels(argv.slice(1), flags);
|
|
2269
|
+
return;
|
|
2270
|
+
}
|
|
1673
2271
|
case 'token': {
|
|
1674
2272
|
await runToken(argv[1], flags, argv[2]);
|
|
1675
2273
|
return;
|
|
@@ -1698,6 +2296,8 @@ async function main() {
|
|
|
1698
2296
|
' runtime current show|set|clear [--version V --runtime-path P --cli C --provider shared --owner O --installed-at ISO] current 指针(非敏感)',
|
|
1699
2297
|
' runtime service install|uninstall|restart|status [--json] 共享 mebular-serve 所有权/生命周期',
|
|
1700
2298
|
' runtime provider list|add|remove [--kind app|cli|shared --path P --version V --capabilities a,b --id ID] runtime provider 注册表',
|
|
2299
|
+
' memory-runtime status|start|stop|restart|logs|doctor|outbox [--json] [--home DIR] [logs --limit|--tail N] 记忆运行时 sidecar(独立于 MCP daemon runtime)',
|
|
2300
|
+
' models list|status|install|download|select|remove|start|stop [--json] [--pack P] [--force] sidecar 模型(需 serve;不同步)',
|
|
1701
2301
|
' token grant|list|revoke [--scope a,b] [--id x] [--tokens-file p] bearer 令牌管理',
|
|
1702
2302
|
' token client add|list|remove [--redirect uri] [--scope a,b] [--id x] OAuth 客户端预注册',
|
|
1703
2303
|
' token consent [--scope a,b] [--ttl sec] 生成一次性本地同意码(/authorize 用)',
|
|
@@ -1738,6 +2338,10 @@ async function main() {
|
|
|
1738
2338
|
}
|
|
1739
2339
|
return;
|
|
1740
2340
|
case 'doctor':
|
|
2341
|
+
if (argv[1] === 'memory-runtime' || flags['memory-runtime'] === true || flags['memory-runtime'] === 'true') {
|
|
2342
|
+
await runMemoryRuntime('doctor', argv.slice(2), flags);
|
|
2343
|
+
return;
|
|
2344
|
+
}
|
|
1741
2345
|
if (flags.net === true || flags.net === 'true') {
|
|
1742
2346
|
await runNetDoctor();
|
|
1743
2347
|
return;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mebular/mcp",
|
|
3
|
-
"version": "0.2
|
|
4
|
-
"description": "Mebular MCP server(stdio + Streamable HTTP):把 Mebular
|
|
3
|
+
"version": "0.3.2",
|
|
4
|
+
"description": "Mebular MCP server (stdio + Streamable HTTP): exposes Mebular memory and task capabilities to any MCP client. | Mebular MCP server(stdio + Streamable HTTP):把 Mebular 的记忆与任务能力暴露给任意 MCP client。",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": {
|
|
7
7
|
"name": "Sobert Li (Windsander)",
|
|
@@ -29,9 +29,10 @@
|
|
|
29
29
|
"access": "public"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@mebular/core": "^0.2
|
|
32
|
+
"@mebular/core": "^0.3.2",
|
|
33
|
+
"@mebular/memory-runtime": "^0.3.2",
|
|
33
34
|
"@modelcontextprotocol/server": "^2.3.1",
|
|
34
35
|
"zod": "^4.6.7",
|
|
35
|
-
"@mebular/fleet": "^0.2
|
|
36
|
+
"@mebular/fleet": "^0.3.2"
|
|
36
37
|
}
|
|
37
38
|
}
|