@mebular/mcp 0.2.3 → 0.3.0
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 +411 -1
- package/package.json +4 -3
- package/src/admin.mjs +60 -44
- package/src/clients.mjs +1172 -68
- package/src/config-schema.mjs +7 -0
- package/src/detail-maintenance.mjs +134 -0
- package/src/fleet-events.mjs +1 -1
- package/src/memory-capability.mjs +4734 -0
- package/src/memory-runtime-config.mjs +86 -0
- package/src/memory-runtime-supervisor.mjs +1844 -0
- package/src/memory-runtime-token.mjs +193 -0
- package/src/serve.mjs +848 -6
- package/src/server.mjs +37 -2
- package/src/task-tools.mjs +10 -1
- package/src/tools.mjs +20 -17
- package/src/version.mjs +1 -1
package/bin/mebular.mjs
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
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 { dirname, join } from 'node:path';
|
|
9
|
+
import { dirname, join, resolve } from 'node:path';
|
|
10
10
|
import { fileURLToPath } from 'node:url';
|
|
11
11
|
import { homeDir } from '../src/home.mjs';
|
|
12
12
|
import { serviceBuildSha } from '../src/build-sha.mjs';
|
|
@@ -308,6 +308,16 @@ async function runInit(flags) {
|
|
|
308
308
|
await writeFile(configFile, JSON.stringify(nextConfig, null, 2), 'utf-8');
|
|
309
309
|
console.log(`已写入配置:${configFile}(deviceId=${identity.deviceId},由身份文件提供)`);
|
|
310
310
|
}
|
|
311
|
+
// Skill 是部署产物:初始化成功后对已检测到的助手自动 ensure;失败只告警,不阻断 init。
|
|
312
|
+
try {
|
|
313
|
+
const { autoEnsureSkills, assistantUserHome } = await import('../src/clients.mjs');
|
|
314
|
+
const skills = await autoEnsureSkills({ home, userHome: assistantUserHome(), cwd: process.cwd() });
|
|
315
|
+
if (!skills.skipped && skills.summary?.total > 0) {
|
|
316
|
+
console.log(`Skill:安装 ${skills.summary.installed} · 更新 ${skills.summary.updated} · 保留 ${skills.summary.kept} · 失败 ${skills.summary.failed}`);
|
|
317
|
+
}
|
|
318
|
+
} catch (error) {
|
|
319
|
+
console.error(`warning: Skill 自动安装失败(不阻断初始化):${error?.message ?? error}`);
|
|
320
|
+
}
|
|
311
321
|
console.log(
|
|
312
322
|
[
|
|
313
323
|
'',
|
|
@@ -1107,6 +1117,292 @@ async function runCloud(action, args, flags) {
|
|
|
1107
1117
|
process.exit(2);
|
|
1108
1118
|
}
|
|
1109
1119
|
|
|
1120
|
+
// ---------- 本地模型:mebular models list/status/install/import/remove/start/stop/test ----------
|
|
1121
|
+
|
|
1122
|
+
function modelsUsage() {
|
|
1123
|
+
return '用法:mebular models <list|status|install|download|select|remove|start|stop> [--json] [--pack P] [--force] [--token T](需要 `mebular serve`;import/test/pause/resume 暂不支持)';
|
|
1124
|
+
}
|
|
1125
|
+
|
|
1126
|
+
async function modelsDaemonBase(home) {
|
|
1127
|
+
const { readRuntimeFile } = await import('../src/runtime-file.mjs');
|
|
1128
|
+
const record = await readRuntimeFile(home).catch(() => null);
|
|
1129
|
+
const host = record?.http?.host;
|
|
1130
|
+
const port = record?.http?.port;
|
|
1131
|
+
if (typeof host !== 'string' || !Number.isInteger(port)) return null;
|
|
1132
|
+
const displayHost = host.includes(':') && !host.startsWith('[') ? `[${host}]` : host;
|
|
1133
|
+
const base = `http://${displayHost}:${port}`;
|
|
1134
|
+
try {
|
|
1135
|
+
const response = await fetch(`${base}/healthz`, { signal: AbortSignal.timeout(1200) });
|
|
1136
|
+
return response.ok ? base : null;
|
|
1137
|
+
} catch {
|
|
1138
|
+
return null;
|
|
1139
|
+
}
|
|
1140
|
+
}
|
|
1141
|
+
|
|
1142
|
+
async function modelsAdminCsrf(base) {
|
|
1143
|
+
const response = await fetch(`${base}/console/`, { signal: AbortSignal.timeout(3000) });
|
|
1144
|
+
const token = response.headers.get('x-mebular-csrf');
|
|
1145
|
+
const cookies = typeof response.headers.getSetCookie === 'function' ? response.headers.getSetCookie() : [];
|
|
1146
|
+
const cookie = cookies.find((value) => value.startsWith('mebular_csrf='))?.split(';')[0] ?? null;
|
|
1147
|
+
return token && cookie ? { token, cookie } : null;
|
|
1148
|
+
}
|
|
1149
|
+
|
|
1150
|
+
async function modelsAdminRequest(base, { method = 'GET', path, body = null, flags = {} } = {}) {
|
|
1151
|
+
const headers = { accept: 'application/json' };
|
|
1152
|
+
const token = typeof flags.token === 'string'
|
|
1153
|
+
? flags.token
|
|
1154
|
+
: process.env.MEBULAR_ADMIN_TOKEN;
|
|
1155
|
+
if (typeof token === 'string' && token.length > 0) headers.authorization = `Bearer ${token}`;
|
|
1156
|
+
if (method !== 'GET') {
|
|
1157
|
+
const csrf = await modelsAdminCsrf(base).catch(() => null);
|
|
1158
|
+
if (!csrf) {
|
|
1159
|
+
return { status: 403, body: { ok: false, error: { code: 'csrf_unavailable', message: '无法从守护获取 CSRF token' } } };
|
|
1160
|
+
}
|
|
1161
|
+
headers['x-mebular-csrf'] = csrf.token;
|
|
1162
|
+
headers.cookie = csrf.cookie;
|
|
1163
|
+
headers['content-type'] = 'application/json';
|
|
1164
|
+
}
|
|
1165
|
+
const response = await fetch(`${base}${path}`, {
|
|
1166
|
+
method,
|
|
1167
|
+
headers,
|
|
1168
|
+
...(body !== null ? { body: JSON.stringify(body) } : {}),
|
|
1169
|
+
signal: AbortSignal.timeout(10 * 60_000),
|
|
1170
|
+
});
|
|
1171
|
+
const data = await response.json().catch(() => null);
|
|
1172
|
+
if (!data || typeof data !== 'object') {
|
|
1173
|
+
return { status: response.status, body: { ok: false, error: { code: 'invalid_response', message: '守护返回了非 JSON 响应' } } };
|
|
1174
|
+
}
|
|
1175
|
+
// 现有 admin 门禁返回 { error: 'unauthorized', message };CLI 统一成结构化 error。
|
|
1176
|
+
if (typeof data.ok !== 'boolean' && typeof data.error === 'string') {
|
|
1177
|
+
return {
|
|
1178
|
+
status: response.status,
|
|
1179
|
+
body: {
|
|
1180
|
+
...data,
|
|
1181
|
+
ok: false,
|
|
1182
|
+
error: { code: data.error, message: typeof data.message === 'string' ? data.message : data.error },
|
|
1183
|
+
code: data.error,
|
|
1184
|
+
},
|
|
1185
|
+
};
|
|
1186
|
+
}
|
|
1187
|
+
return { status: response.status, body: data };
|
|
1188
|
+
}
|
|
1189
|
+
|
|
1190
|
+
function modelCliError(error) {
|
|
1191
|
+
if (error && typeof error.toBody === 'function') return error.toBody();
|
|
1192
|
+
if (error?.code && typeof error.code === 'string') {
|
|
1193
|
+
return {
|
|
1194
|
+
ok: false,
|
|
1195
|
+
error: { code: error.code, message: String(error.message ?? error), retryable: false },
|
|
1196
|
+
code: error.code,
|
|
1197
|
+
message: String(error.message ?? error),
|
|
1198
|
+
};
|
|
1199
|
+
}
|
|
1200
|
+
return {
|
|
1201
|
+
ok: false,
|
|
1202
|
+
error: { code: 'models_failed', message: String(error?.message ?? error), retryable: false },
|
|
1203
|
+
code: 'models_failed',
|
|
1204
|
+
message: String(error?.message ?? error),
|
|
1205
|
+
};
|
|
1206
|
+
}
|
|
1207
|
+
|
|
1208
|
+
function printModelsResult(command, body, { json = false } = {}) {
|
|
1209
|
+
const payload = body && typeof body === 'object' ? { ...body, command } : { ok: false, command, error: { code: 'models_failed', message: '空响应' } };
|
|
1210
|
+
if (json) {
|
|
1211
|
+
console.log(JSON.stringify(payload, null, 2));
|
|
1212
|
+
return;
|
|
1213
|
+
}
|
|
1214
|
+
if (payload.ok !== true) {
|
|
1215
|
+
console.error(`✗ ${payload.error?.code ?? 'models_failed'}:${payload.error?.message ?? payload.message ?? '失败'}`);
|
|
1216
|
+
return;
|
|
1217
|
+
}
|
|
1218
|
+
if (command === 'models.list' || command === 'models.status') {
|
|
1219
|
+
const packs = Array.isArray(payload.packs) ? payload.packs : [];
|
|
1220
|
+
console.log(`selected: ${payload.effectivePack ?? payload.selectedPack ?? '(none)'}`);
|
|
1221
|
+
for (const pack of packs) {
|
|
1222
|
+
console.log(`${pack.packId}\t${pack.state}${pack.selected ? '\tselected' : ''}`);
|
|
1223
|
+
}
|
|
1224
|
+
return;
|
|
1225
|
+
}
|
|
1226
|
+
console.log(JSON.stringify(payload, null, 2));
|
|
1227
|
+
}
|
|
1228
|
+
|
|
1229
|
+
const MODELS_UNSUPPORTED = new Set(['import', 'test', 'pause', 'resume']);
|
|
1230
|
+
const MODELS_SUPPORTED = new Set(['list', 'status', 'install', 'download', 'select', 'remove', 'uninstall', 'start', 'stop']);
|
|
1231
|
+
|
|
1232
|
+
function daemonUnavailable(command) {
|
|
1233
|
+
return {
|
|
1234
|
+
ok: false,
|
|
1235
|
+
command,
|
|
1236
|
+
error: {
|
|
1237
|
+
code: 'daemon_unavailable',
|
|
1238
|
+
message: '模型目录/安装/引擎由 Memory Runtime sidecar 拥有;请先启动 `mebular serve`(沉浸式)',
|
|
1239
|
+
retryable: false,
|
|
1240
|
+
},
|
|
1241
|
+
code: 'daemon_unavailable',
|
|
1242
|
+
message: 'Memory Runtime sidecar 未运行',
|
|
1243
|
+
};
|
|
1244
|
+
}
|
|
1245
|
+
|
|
1246
|
+
function unsupportedModelCommand(command, sub) {
|
|
1247
|
+
return {
|
|
1248
|
+
ok: false,
|
|
1249
|
+
command,
|
|
1250
|
+
error: {
|
|
1251
|
+
code: 'unsupported',
|
|
1252
|
+
message: `models ${sub} 暂不支持:sidecar 当前只提供 list/status/install/select/remove/start/stop`,
|
|
1253
|
+
retryable: false,
|
|
1254
|
+
},
|
|
1255
|
+
code: 'unsupported',
|
|
1256
|
+
message: `models ${sub} 暂不支持`,
|
|
1257
|
+
};
|
|
1258
|
+
}
|
|
1259
|
+
|
|
1260
|
+
async function waitForModelInstall(daemon, jobId, flags) {
|
|
1261
|
+
const deadline = Date.now() + 10 * 60_000;
|
|
1262
|
+
for (;;) {
|
|
1263
|
+
const status = await modelsAdminRequest(daemon, { path: '/admin/api/models/status', flags });
|
|
1264
|
+
const job = status.body?.install;
|
|
1265
|
+
if (job && typeof job === 'object' && job.jobId === jobId) {
|
|
1266
|
+
if (job.state === 'completed') {
|
|
1267
|
+
return {
|
|
1268
|
+
status: 200,
|
|
1269
|
+
body: {
|
|
1270
|
+
ok: true,
|
|
1271
|
+
pack: job.packId,
|
|
1272
|
+
state: 'installed',
|
|
1273
|
+
job,
|
|
1274
|
+
warnings: Array.isArray(job.warnings) ? job.warnings : [],
|
|
1275
|
+
},
|
|
1276
|
+
};
|
|
1277
|
+
}
|
|
1278
|
+
if (['failed', 'cancelled', 'interrupted'].includes(job.state)) {
|
|
1279
|
+
return {
|
|
1280
|
+
status: 409,
|
|
1281
|
+
body: {
|
|
1282
|
+
ok: false,
|
|
1283
|
+
pack: job.packId,
|
|
1284
|
+
state: job.state,
|
|
1285
|
+
job,
|
|
1286
|
+
error: job.error ?? {
|
|
1287
|
+
code: `install_${job.state}`,
|
|
1288
|
+
message: `模型安装${job.state}`,
|
|
1289
|
+
retryable: job.state === 'interrupted',
|
|
1290
|
+
},
|
|
1291
|
+
},
|
|
1292
|
+
};
|
|
1293
|
+
}
|
|
1294
|
+
}
|
|
1295
|
+
if (Date.now() >= deadline) {
|
|
1296
|
+
return {
|
|
1297
|
+
status: 504,
|
|
1298
|
+
body: {
|
|
1299
|
+
ok: false,
|
|
1300
|
+
pack: null,
|
|
1301
|
+
jobId,
|
|
1302
|
+
error: { code: 'install_timeout', message: '等待模型安装超时', retryable: true },
|
|
1303
|
+
},
|
|
1304
|
+
};
|
|
1305
|
+
}
|
|
1306
|
+
await new Promise((resolvePromise) => setTimeout(resolvePromise, 500));
|
|
1307
|
+
}
|
|
1308
|
+
}
|
|
1309
|
+
|
|
1310
|
+
async function runModels(args, flags) {
|
|
1311
|
+
const sub = typeof args[0] === 'string' ? args[0] : 'list';
|
|
1312
|
+
const json = flags.json === true;
|
|
1313
|
+
const home = homeDir();
|
|
1314
|
+
const command = `models.${sub}`;
|
|
1315
|
+
try {
|
|
1316
|
+
if (MODELS_UNSUPPORTED.has(sub)) {
|
|
1317
|
+
printModelsResult(command, unsupportedModelCommand(command, sub), { json });
|
|
1318
|
+
process.exit(2);
|
|
1319
|
+
}
|
|
1320
|
+
if (!MODELS_SUPPORTED.has(sub)) {
|
|
1321
|
+
throw Object.assign(new Error(modelsUsage()), { code: 'USAGE' });
|
|
1322
|
+
}
|
|
1323
|
+
|
|
1324
|
+
// 模型目录/下载/安装/引擎只由 sidecar 拥有;CLI 没有 daemon 时不再本地兜底。
|
|
1325
|
+
const daemon = await modelsDaemonBase(home);
|
|
1326
|
+
if (!daemon) {
|
|
1327
|
+
printModelsResult(command, daemonUnavailable(command), { json });
|
|
1328
|
+
process.exit(2);
|
|
1329
|
+
}
|
|
1330
|
+
|
|
1331
|
+
if (sub === 'list' || sub === 'status') {
|
|
1332
|
+
const result = await modelsAdminRequest(daemon, {
|
|
1333
|
+
path: sub === 'status' ? '/admin/api/models/status' : '/admin/api/models',
|
|
1334
|
+
flags,
|
|
1335
|
+
});
|
|
1336
|
+
printModelsResult(command, result.body, { json });
|
|
1337
|
+
if (result.status >= 400) process.exit(2);
|
|
1338
|
+
return;
|
|
1339
|
+
}
|
|
1340
|
+
|
|
1341
|
+
if (sub === 'install' || sub === 'download') {
|
|
1342
|
+
const pack = args[1] ?? flags.pack;
|
|
1343
|
+
const result = await modelsAdminRequest(daemon, {
|
|
1344
|
+
method: 'POST',
|
|
1345
|
+
path: '/admin/api/models/install',
|
|
1346
|
+
body: {
|
|
1347
|
+
pack,
|
|
1348
|
+
...(flags.force === true ? { force: true } : {}),
|
|
1349
|
+
},
|
|
1350
|
+
flags,
|
|
1351
|
+
});
|
|
1352
|
+
if (result.status >= 400 || result.body?.ok === false) {
|
|
1353
|
+
printModelsResult(command, result.body, { json });
|
|
1354
|
+
process.exit(2);
|
|
1355
|
+
}
|
|
1356
|
+
const jobId = typeof result.body?.job?.jobId === 'string' ? result.body.job.jobId : null;
|
|
1357
|
+
const final = jobId ? await waitForModelInstall(daemon, jobId, flags) : result;
|
|
1358
|
+
printModelsResult(command, final.body, { json });
|
|
1359
|
+
if (final.status >= 400 || final.body?.ok === false) process.exit(2);
|
|
1360
|
+
return;
|
|
1361
|
+
}
|
|
1362
|
+
|
|
1363
|
+
if (sub === 'select') {
|
|
1364
|
+
const result = await modelsAdminRequest(daemon, {
|
|
1365
|
+
method: 'POST',
|
|
1366
|
+
path: '/admin/api/models/select',
|
|
1367
|
+
body: { pack: args[1] ?? flags.pack },
|
|
1368
|
+
flags,
|
|
1369
|
+
});
|
|
1370
|
+
printModelsResult(command, result.body, { json });
|
|
1371
|
+
if (result.status >= 400 || result.body?.ok === false) process.exit(2);
|
|
1372
|
+
return;
|
|
1373
|
+
}
|
|
1374
|
+
|
|
1375
|
+
if (sub === 'remove' || sub === 'uninstall') {
|
|
1376
|
+
const result = await modelsAdminRequest(daemon, {
|
|
1377
|
+
method: 'POST',
|
|
1378
|
+
path: '/admin/api/models/remove',
|
|
1379
|
+
body: { pack: args[1] ?? flags.pack },
|
|
1380
|
+
flags,
|
|
1381
|
+
});
|
|
1382
|
+
printModelsResult(command, result.body, { json });
|
|
1383
|
+
if (result.status >= 400 || result.body?.ok === false) process.exit(2);
|
|
1384
|
+
return;
|
|
1385
|
+
}
|
|
1386
|
+
|
|
1387
|
+
if (sub === 'start' || sub === 'stop') {
|
|
1388
|
+
const result = await modelsAdminRequest(daemon, {
|
|
1389
|
+
method: 'POST',
|
|
1390
|
+
path: `/admin/api/models/${sub}`,
|
|
1391
|
+
body: { pack: args[1] ?? flags.pack },
|
|
1392
|
+
flags,
|
|
1393
|
+
});
|
|
1394
|
+
printModelsResult(command, result.body, { json });
|
|
1395
|
+
if (result.status >= 400 || result.body?.ok === false) process.exit(2);
|
|
1396
|
+
return;
|
|
1397
|
+
}
|
|
1398
|
+
|
|
1399
|
+
throw Object.assign(new Error(modelsUsage()), { code: 'USAGE' });
|
|
1400
|
+
} catch (error) {
|
|
1401
|
+
printModelsResult(command, modelCliError(error), { json });
|
|
1402
|
+
process.exit(2);
|
|
1403
|
+
}
|
|
1404
|
+
}
|
|
1405
|
+
|
|
1110
1406
|
// ---------- 共享 runtime:resolve / ensure / status + provider 注册表 ----------
|
|
1111
1407
|
|
|
1112
1408
|
function runtimeCliError(error) {
|
|
@@ -1493,6 +1789,110 @@ async function runRuntimeUpgrade(flags) {
|
|
|
1493
1789
|
printRuntimeJson(result, result.ok ? 0 : 2);
|
|
1494
1790
|
}
|
|
1495
1791
|
|
|
1792
|
+
// ---------- Memory Runtime sidecar(`mebular memory-runtime ...`) ----------
|
|
1793
|
+
//
|
|
1794
|
+
// 与 `mebular runtime ...`(MCP daemon)严格分离:这里只管理 `mebular-memory-runtime serve`。
|
|
1795
|
+
// 输出稳定 JSON,不返回 host token / capability token / tokenFile。
|
|
1796
|
+
|
|
1797
|
+
async function readMemoryRuntimeConfig(home) {
|
|
1798
|
+
try {
|
|
1799
|
+
const file = process.env.MEBULAR_CONFIG ?? join(home, 'config.json');
|
|
1800
|
+
return JSON.parse(await readFile(file, 'utf-8'));
|
|
1801
|
+
} catch {
|
|
1802
|
+
return {};
|
|
1803
|
+
}
|
|
1804
|
+
}
|
|
1805
|
+
|
|
1806
|
+
function memoryRuntimeHome(flags) {
|
|
1807
|
+
const explicit = typeof flags.home === 'string' && flags.home.length > 0 ? flags.home : null;
|
|
1808
|
+
return explicit ? resolve(explicit) : resolve(homeDir());
|
|
1809
|
+
}
|
|
1810
|
+
|
|
1811
|
+
function memoryRuntimeTimeout(flags) {
|
|
1812
|
+
if (flags.timeout === undefined) return 5000;
|
|
1813
|
+
const value = Number(flags.timeout);
|
|
1814
|
+
if (!Number.isInteger(value) || value < 0 || value > 120_000) {
|
|
1815
|
+
const error = new Error('--timeout 必须是 0–120000 的整数(毫秒)');
|
|
1816
|
+
error.code = 'USAGE';
|
|
1817
|
+
throw error;
|
|
1818
|
+
}
|
|
1819
|
+
return value;
|
|
1820
|
+
}
|
|
1821
|
+
|
|
1822
|
+
function printMemoryRuntime(value, exitCode = 0) {
|
|
1823
|
+
console.log(JSON.stringify(value, null, 2));
|
|
1824
|
+
process.exit(exitCode);
|
|
1825
|
+
}
|
|
1826
|
+
|
|
1827
|
+
async function runMemoryRuntime(action, flags) {
|
|
1828
|
+
const usage = '用法:mebular memory-runtime <status|start|stop|restart> [--json] [--home DIR] [--timeout MS]';
|
|
1829
|
+
try {
|
|
1830
|
+
if (!['status', 'start', 'stop', 'restart'].includes(action)) {
|
|
1831
|
+
const error = new Error(usage);
|
|
1832
|
+
error.code = 'USAGE';
|
|
1833
|
+
throw error;
|
|
1834
|
+
}
|
|
1835
|
+
const home = memoryRuntimeHome(flags);
|
|
1836
|
+
const config = await readMemoryRuntimeConfig(home);
|
|
1837
|
+
const timeoutMs = memoryRuntimeTimeout(flags);
|
|
1838
|
+
const {
|
|
1839
|
+
MemoryRuntimeSupervisor,
|
|
1840
|
+
inspectMemoryRuntimeStatus,
|
|
1841
|
+
memoryRuntimeStatusExitCode,
|
|
1842
|
+
} = await import('../src/memory-runtime-supervisor.mjs');
|
|
1843
|
+
|
|
1844
|
+
if (action === 'status') {
|
|
1845
|
+
const status = await inspectMemoryRuntimeStatus({ home, config, env: process.env });
|
|
1846
|
+
printMemoryRuntime({
|
|
1847
|
+
ok: true,
|
|
1848
|
+
kind: 'memory-runtime',
|
|
1849
|
+
action,
|
|
1850
|
+
...status,
|
|
1851
|
+
}, 0);
|
|
1852
|
+
return;
|
|
1853
|
+
}
|
|
1854
|
+
|
|
1855
|
+
const supervisor = new MemoryRuntimeSupervisor({
|
|
1856
|
+
home,
|
|
1857
|
+
config,
|
|
1858
|
+
env: process.env,
|
|
1859
|
+
});
|
|
1860
|
+
|
|
1861
|
+
if (action === 'stop') {
|
|
1862
|
+
const status = await supervisor.stop({ includeAttached: true, timeoutMs });
|
|
1863
|
+
printMemoryRuntime({
|
|
1864
|
+
ok: true,
|
|
1865
|
+
kind: 'memory-runtime',
|
|
1866
|
+
action,
|
|
1867
|
+
...status,
|
|
1868
|
+
}, 0);
|
|
1869
|
+
return;
|
|
1870
|
+
}
|
|
1871
|
+
|
|
1872
|
+
const status = action === 'restart'
|
|
1873
|
+
? await supervisor.restart({ includeAttached: true, timeoutMs, manual: true })
|
|
1874
|
+
: await supervisor.start({ force: true, manual: true });
|
|
1875
|
+
const ok = status.state === 'ready' || status.state === 'degraded';
|
|
1876
|
+
printMemoryRuntime({
|
|
1877
|
+
ok,
|
|
1878
|
+
kind: 'memory-runtime',
|
|
1879
|
+
action,
|
|
1880
|
+
...status,
|
|
1881
|
+
}, ok ? 0 : memoryRuntimeStatusExitCode(status));
|
|
1882
|
+
} catch (error) {
|
|
1883
|
+
const code = error?.code === 'USAGE' ? 'usage' : (error?.code ?? 'error');
|
|
1884
|
+
printMemoryRuntime({
|
|
1885
|
+
ok: false,
|
|
1886
|
+
kind: 'memory-runtime',
|
|
1887
|
+
action: action ?? null,
|
|
1888
|
+
error: {
|
|
1889
|
+
code,
|
|
1890
|
+
message: error?.message ?? String(error),
|
|
1891
|
+
},
|
|
1892
|
+
}, code === 'usage' ? 2 : 1);
|
|
1893
|
+
}
|
|
1894
|
+
}
|
|
1895
|
+
|
|
1496
1896
|
async function runRuntime(action, args, flags) {
|
|
1497
1897
|
try {
|
|
1498
1898
|
if (action === 'provider') {
|
|
@@ -1660,6 +2060,14 @@ async function main() {
|
|
|
1660
2060
|
await runRuntime(argv[1], argv.slice(2), flags);
|
|
1661
2061
|
return;
|
|
1662
2062
|
}
|
|
2063
|
+
case 'memory-runtime': {
|
|
2064
|
+
await runMemoryRuntime(argv[1], flags);
|
|
2065
|
+
return;
|
|
2066
|
+
}
|
|
2067
|
+
case 'models': {
|
|
2068
|
+
await runModels(argv.slice(1), flags);
|
|
2069
|
+
return;
|
|
2070
|
+
}
|
|
1663
2071
|
case 'token': {
|
|
1664
2072
|
await runToken(argv[1], flags, argv[2]);
|
|
1665
2073
|
return;
|
|
@@ -1688,6 +2096,8 @@ async function main() {
|
|
|
1688
2096
|
' runtime current show|set|clear [--version V --runtime-path P --cli C --provider shared --owner O --installed-at ISO] current 指针(非敏感)',
|
|
1689
2097
|
' runtime service install|uninstall|restart|status [--json] 共享 mebular-serve 所有权/生命周期',
|
|
1690
2098
|
' runtime provider list|add|remove [--kind app|cli|shared --path P --version V --capabilities a,b --id ID] runtime provider 注册表',
|
|
2099
|
+
' memory-runtime status|start|stop|restart [--json] [--home DIR] 记忆运行时 sidecar(独立于 MCP daemon runtime)',
|
|
2100
|
+
' models list|status|install|download|select|remove|start|stop [--json] [--pack P] [--force] sidecar 模型(需 serve;不同步)',
|
|
1691
2101
|
' token grant|list|revoke [--scope a,b] [--id x] [--tokens-file p] bearer 令牌管理',
|
|
1692
2102
|
' token client add|list|remove [--redirect uri] [--scope a,b] [--id x] OAuth 客户端预注册',
|
|
1693
2103
|
' token consent [--scope a,b] [--ttl sec] 生成一次性本地同意码(/authorize 用)',
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mebular/mcp",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Mebular MCP server(stdio + Streamable HTTP):把 Mebular 记忆能力暴露给任意 MCP client",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": {
|
|
@@ -29,9 +29,10 @@
|
|
|
29
29
|
"access": "public"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@mebular/core": "^0.
|
|
32
|
+
"@mebular/core": "^0.3.0",
|
|
33
|
+
"@mebular/memory-runtime": "^0.3.0",
|
|
33
34
|
"@modelcontextprotocol/server": "^2.3.1",
|
|
34
35
|
"zod": "^4.6.7",
|
|
35
|
-
"@mebular/fleet": "^0.
|
|
36
|
+
"@mebular/fleet": "^0.3.0"
|
|
36
37
|
}
|
|
37
38
|
}
|
package/src/admin.mjs
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
// 控制台只读 API 的数据组装(D1)
|
|
2
2
|
//
|
|
3
|
-
// 只使用 @mebular/core 的公共 API:eventLog /
|
|
4
|
-
//
|
|
5
|
-
//
|
|
3
|
+
// 只使用 @mebular/core 的公共 API:eventLog / node / sync / 门面只读入口
|
|
4
|
+
// (getSubscriptions / getBlockedDevices / 域公告)与 MemoryService 统一读面
|
|
5
|
+
// (status / listRecords / namespaceStats)。这里不做任何写入,也不替其他设备
|
|
6
|
+
// 做决定——只回答「本机给谁看什么」「与谁连接」。
|
|
6
7
|
//
|
|
7
8
|
// 契约(见 docs/console/interaction-draft.md):
|
|
8
9
|
// GET /admin/api/overview { device, status, onlinePeers[], revokedCount }
|
|
@@ -31,6 +32,9 @@ import {
|
|
|
31
32
|
|
|
32
33
|
export const POLICY_NS = POLICY_NAMESPACE;
|
|
33
34
|
|
|
35
|
+
// 记忆列表/域统计的过滤与聚合统一在 MemoryService.listRecords()/namespaceStats();
|
|
36
|
+
// 这里只做 Console 展示映射,不再识别内部类型或自行统计。
|
|
37
|
+
|
|
34
38
|
/** hex → Uint8Array(证书 devicePublicKey 为 hex 编码) */
|
|
35
39
|
export function hexToBytes(hex) {
|
|
36
40
|
if (typeof hex !== 'string' || hex.length % 2 !== 0) return new Uint8Array();
|
|
@@ -709,10 +713,17 @@ export async function buildPolicy({ app }) {
|
|
|
709
713
|
return policy.timeline;
|
|
710
714
|
}
|
|
711
715
|
|
|
712
|
-
/**
|
|
716
|
+
/**
|
|
717
|
+
* GET /admin/api/namespaces:目录 = 域公告 ∪ 本机域统计(未订阅也显示)。
|
|
718
|
+
*
|
|
719
|
+
* 计数/最近更新时间/记忆记录/内部对象统一来自 MemoryService.namespaceStats(),
|
|
720
|
+
* 与 stats()/capability 共用同一聚合规则。显式 includeReserved 保留 Console
|
|
721
|
+
* 旧行为:本机 `__*` 内部域仍然列出(不静默丢域);公共 stats/capability 默认
|
|
722
|
+
* 仍排除它们。
|
|
723
|
+
*/
|
|
713
724
|
export async function buildNamespaces({ app, service, config }) {
|
|
714
|
-
const [
|
|
715
|
-
|
|
725
|
+
const [domains, status, policy] = await Promise.all([
|
|
726
|
+
service.namespaceStats({ includeReserved: true }),
|
|
716
727
|
service.status(),
|
|
717
728
|
collectPolicy(app),
|
|
718
729
|
]);
|
|
@@ -721,26 +732,30 @@ export async function buildNamespaces({ app, service, config }) {
|
|
|
721
732
|
byNamespace.set(declaration.namespace, {
|
|
722
733
|
namespace: declaration.namespace,
|
|
723
734
|
count: 0,
|
|
735
|
+
memoryRecords: 0,
|
|
736
|
+
internalObjects: 0,
|
|
724
737
|
lastUpdatedAt: 0,
|
|
725
738
|
declared: true,
|
|
726
739
|
declaredAt: declaration.at,
|
|
727
740
|
declaredBy: declaration.issuer,
|
|
728
741
|
});
|
|
729
742
|
}
|
|
730
|
-
for (const
|
|
731
|
-
const
|
|
732
|
-
|
|
733
|
-
namespace,
|
|
743
|
+
for (const domain of domains) {
|
|
744
|
+
const entry = byNamespace.get(domain.domain) ?? {
|
|
745
|
+
namespace: domain.domain,
|
|
734
746
|
count: 0,
|
|
747
|
+
memoryRecords: 0,
|
|
748
|
+
internalObjects: 0,
|
|
735
749
|
lastUpdatedAt: 0,
|
|
736
750
|
declared: false,
|
|
737
751
|
declaredAt: null,
|
|
738
752
|
declaredBy: null,
|
|
739
753
|
};
|
|
740
|
-
entry.count
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
754
|
+
entry.count = domain.count;
|
|
755
|
+
entry.memoryRecords = domain.memoryRecords;
|
|
756
|
+
entry.internalObjects = domain.internalObjects;
|
|
757
|
+
entry.lastUpdatedAt = domain.lastUpdatedAt;
|
|
758
|
+
byNamespace.set(domain.domain, entry);
|
|
744
759
|
}
|
|
745
760
|
const hashes = status.stateHashByNamespace ?? {};
|
|
746
761
|
const subscription = app.sync?.getSubscriptionNamespaces?.()
|
|
@@ -1151,31 +1166,34 @@ export async function buildConfigView({ config, home }) {
|
|
|
1151
1166
|
|
|
1152
1167
|
/**
|
|
1153
1168
|
* G-ML-1:记忆列表(动作式 GUI 的读面)。
|
|
1154
|
-
*
|
|
1155
|
-
*
|
|
1169
|
+
* 默认**过滤已删除(墓碑)、已归档与 Runtime 内部对象**;对应 include* 参数可切换查看。
|
|
1170
|
+
* 过滤/排序/分页统一由 MemoryService.listRecords() 执行;这里只做脱敏展示映射
|
|
1171
|
+
* (只给预览,不回显完整 metadata)。
|
|
1156
1172
|
*/
|
|
1157
|
-
export async function buildMemories({
|
|
1173
|
+
export async function buildMemories({ service, url }) {
|
|
1158
1174
|
const params = url?.searchParams ?? new URLSearchParams();
|
|
1159
1175
|
const flag = (name) => params.get(name) === '1' || params.get(name) === 'true';
|
|
1160
1176
|
const includeArchived = flag('includeArchived');
|
|
1161
1177
|
const includeDeleted = flag('includeDeleted');
|
|
1178
|
+
// 0.3:Runtime 内部对象默认隐藏;includeInternal=1 仅管理面调试使用。
|
|
1179
|
+
const includeInternal = flag('includeInternal');
|
|
1162
1180
|
const namespace = params.get('namespace') ?? undefined;
|
|
1163
1181
|
const limitRaw = Number(params.get('limit') ?? '50');
|
|
1164
1182
|
const limit = Number.isFinite(limitRaw) ? Math.min(Math.max(Math.trunc(limitRaw), 1), 200) : 50;
|
|
1165
1183
|
|
|
1166
1184
|
const archivedOf = (node) => typeof node?.metadata?.archivedAt === 'number';
|
|
1167
|
-
let
|
|
1185
|
+
let records = { total: 0, nodes: [] };
|
|
1168
1186
|
try {
|
|
1169
|
-
|
|
1187
|
+
records = await service.listRecords({
|
|
1188
|
+
includeDeleted,
|
|
1189
|
+
includeArchived,
|
|
1190
|
+
includeInternal,
|
|
1191
|
+
...(namespace !== undefined ? { namespace } : {}),
|
|
1192
|
+
limit,
|
|
1193
|
+
});
|
|
1170
1194
|
} catch {
|
|
1171
|
-
|
|
1195
|
+
records = { total: 0, nodes: [] };
|
|
1172
1196
|
}
|
|
1173
|
-
const filtered = nodes.filter((node) => {
|
|
1174
|
-
if (!includeDeleted && node.deletedAt) return false;
|
|
1175
|
-
if (!includeArchived && archivedOf(node)) return false;
|
|
1176
|
-
if (namespace !== undefined && (node.namespace ?? 'default') !== namespace) return false;
|
|
1177
|
-
return true;
|
|
1178
|
-
});
|
|
1179
1197
|
const previewOf = (node) => {
|
|
1180
1198
|
const content = node?.content ?? {};
|
|
1181
1199
|
if (typeof content === 'string') return content.slice(0, 160);
|
|
@@ -1188,28 +1206,25 @@ export async function buildMemories({ app, url }) {
|
|
|
1188
1206
|
const text = content.content ?? content.name ?? content.description ?? content.value ?? '';
|
|
1189
1207
|
return String(text).slice(0, 160);
|
|
1190
1208
|
};
|
|
1191
|
-
const memories =
|
|
1192
|
-
.
|
|
1193
|
-
.
|
|
1194
|
-
.
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
deleted: Boolean(node.deletedAt),
|
|
1205
|
-
deletedAt: node.deletedAt ?? null,
|
|
1206
|
-
tags: Array.isArray(node.tags) ? node.tags : [],
|
|
1207
|
-
}));
|
|
1209
|
+
const memories = records.nodes.map((node) => ({
|
|
1210
|
+
id: node.id,
|
|
1211
|
+
type: node.type,
|
|
1212
|
+
namespace: node.namespace ?? 'default',
|
|
1213
|
+
preview: previewOf(node),
|
|
1214
|
+
createdAt: node.createdAt ?? null,
|
|
1215
|
+
updatedAt: node.updatedAt ?? null,
|
|
1216
|
+
archived: archivedOf(node),
|
|
1217
|
+
archivedAt: node.metadata?.archivedAt ?? null,
|
|
1218
|
+
deleted: Boolean(node.deletedAt),
|
|
1219
|
+
deletedAt: node.deletedAt ?? null,
|
|
1220
|
+
tags: Array.isArray(node.tags) ? node.tags : [],
|
|
1221
|
+
}));
|
|
1208
1222
|
return {
|
|
1209
|
-
total:
|
|
1223
|
+
total: records.total,
|
|
1210
1224
|
limit,
|
|
1211
1225
|
includeArchived,
|
|
1212
1226
|
includeDeleted,
|
|
1227
|
+
includeInternal,
|
|
1213
1228
|
...(namespace !== undefined ? { namespace } : {}),
|
|
1214
1229
|
memories,
|
|
1215
1230
|
};
|
|
@@ -1338,4 +1353,5 @@ export const READ_ROUTES = {
|
|
|
1338
1353
|
'/admin/api/net': buildNetStatusView,
|
|
1339
1354
|
'/admin/api/service': buildServiceView,
|
|
1340
1355
|
'/admin/api/clients': buildClientsView,
|
|
1356
|
+
// 0.3 本地模型/推理由 serve.mjs 的 sidecar 薄代理处理,这里不再直读设备 store。
|
|
1341
1357
|
};
|