@sleep2agi/commhub-server 0.9.0-preview.224 → 0.9.0-preview.225

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sleep2agi/commhub-server",
3
- "version": "0.9.0-preview.224",
3
+ "version": "0.9.0-preview.225",
4
4
  "description": "CommHub Server — AI Agent communication hub with MCP protocol, multi-network isolation, user auth, and MCP tools (17 collaboration-core + node/provider ops tools; authoritative list: docs-site/docs/api/mcp-tools.md).",
5
5
  "type": "module",
6
6
  "main": "src/index.ts",
@@ -280,4 +280,53 @@ describe("GET /api/providers", () => {
280
280
  const missing = [...PROVIDER_WRITE_ERROR_CODES].filter((c) => !src.includes(`'${c}'`) && !src.includes(`"${c}"`));
281
281
  if (missing.length) throw new Error(`码清单里这些码在源码中找不到产出点:${missing.join(", ")}`);
282
282
  });
283
+
284
+ // 2026-10-31 —— 节点当前 provider/模型的三态读口(#3145 的数据面)。
285
+ // 关键:**从未切换过 / 正在切 / 切失败** 必须能分开 —— 界面给的下一步完全不同。
286
+ test("GET /api/nodes/:id/provider-selection distinguishes never_switched / pending / applied / failed", async () => {
287
+ const { db } = await import("./db.js");
288
+ const nodeId = "ps-probe-node";
289
+ db.run(`INSERT OR REPLACE INTO nodes (node_id, node_name, network_id, alias, created_at, updated_at)
290
+ VALUES (?1, 'ps-probe', ?2, 'ps-probe', ?3, ?3)`, [nodeId, ownerNet, Date.now()]);
291
+ const read = async () => {
292
+ const r = await fetch(`${base}/api/nodes/${nodeId}/provider-selection`, { headers: { Authorization: `Bearer ${ownerToken}` } });
293
+ return { status: r.status, body: await r.json() as any };
294
+ };
295
+ try {
296
+ // ① 从未切换过:带 hint,selection 为 null(不假装是 Hub 的读数)
297
+ const a = await read();
298
+ expect(a.status).toBe(200);
299
+ expect(a.body.state).toBe("never_switched");
300
+ expect(a.body.selection).toBeNull();
301
+ expect(String(a.body.hint)).toContain("本地默认配置");
302
+
303
+ // ② 正在切:pending 时不带 hint(hint 只属于「没有读数」那一档)
304
+ const q = (requestId: string, status: string, payload: string | null, error: string | null) => db.run(
305
+ `INSERT OR REPLACE INTO daemon_provider_snapshots (request_id, network_id, daemon_node_id, requested_by, status, created_at, expires_at, observed_at, snapshot_json, error)
306
+ VALUES (?1, ?2, ?3, 'test', ?4, ?5, ?6, ?5, ?7, ?8)`,
307
+ [requestId, ownerNet, nodeId, status, Date.now(), Date.now() + 60_000, payload, error],
308
+ );
309
+ q("dpa_probe_1", "pending", null, null);
310
+ const b = await read();
311
+ expect(b.body.state).toBe("pending");
312
+ expect("hint" in b.body).toBe(false);
313
+
314
+ // ③ 已生效:selection 原样带回,并给出 revision
315
+ q("dpa_probe_2", "completed", JSON.stringify({ expected_revision: 7, provider_id: "main", model: "m1" }), null);
316
+ const c = await read();
317
+ expect(c.body.state).toBe("applied");
318
+ expect(c.body.selection).toMatchObject({ provider_id: "main", model: "m1" });
319
+ expect(c.body.revision).toBe(7);
320
+ expect(typeof c.body.observed_at).toBe("string");
321
+
322
+ // ④ 失败:必须带 error(否则界面只能说「失败了」)
323
+ q("dpa_probe_3", "failed", null, "daemon_rejected_selection");
324
+ const d = await read();
325
+ expect(d.body.state).toBe("failed");
326
+ expect(d.body.error).toBe("daemon_rejected_selection");
327
+ } finally {
328
+ db.run(`DELETE FROM daemon_provider_snapshots WHERE daemon_node_id = ?1`, [nodeId]);
329
+ db.run(`DELETE FROM nodes WHERE node_id = ?1`, [nodeId]);
330
+ }
331
+ });
283
332
  });
package/src/server.ts CHANGED
@@ -3881,6 +3881,45 @@ return Bun.serve({
3881
3881
  logAudit(restAuth.userId, restAuth.username, "node_permission_mode_changed", "node", node.node_id, JSON.stringify({ from: previous, to: mode }), getClientIP(req), node.network_id);
3882
3882
  return withCors(req, Response.json({ ok: true, node_id: node.node_id, permission_mode: mode, previous }));
3883
3883
  }
3884
+ // 2026-10-31 —— **读侧:这个节点当前用的是哪个 provider / 模型**(#3145 的数据面)。
3885
+ // 写侧(切哪个)已有 requestProviderApplication(MCP),但客户端要"显示当前",需要一个 REST 读口,
3886
+ // 否则界面只能显示上次操作的结果——而"上次操作"与"现在生效"不是同一件事。
3887
+ const providerSelMatch = url.pathname.match(/^\/api\/nodes\/([^/]+)\/provider-selection$/);
3888
+ if (providerSelMatch && req.method === "GET") {
3889
+ if (!restAuth) return withCors(req, Response.json({ ok: false, error: "auth required" }, { status: 401 }));
3890
+ if (restAuth.networkId) return userTokenRequired(req);
3891
+ const nodeRef = decodeURIComponent(providerSelMatch[1]);
3892
+ const node = db.get<{ node_id: string; network_id: string | null }>(
3893
+ "SELECT node_id, network_id FROM nodes WHERE node_id = ?1", nodeRef);
3894
+ const role = node?.network_id ? getUserNetworkRole(restAuth.userId, node.network_id) : null;
3895
+ if (!node || !node.network_id || (!role && !isAdmin)) {
3896
+ return withCors(req, Response.json({ ok: false, error: "node_not_found" }, { status: 404 }));
3897
+ }
3898
+ const row = db.get<{ request_id: string; status: string; observed_at: number | null; snapshot_json: string | null; error: string | null }>(
3899
+ `SELECT request_id, status, observed_at, snapshot_json, error FROM daemon_provider_snapshots
3900
+ WHERE network_id = ?1 AND daemon_node_id = ?2 AND request_id LIKE 'dpa\_%' ESCAPE '\\'
3901
+ ORDER BY created_at DESC LIMIT 1`,
3902
+ node.network_id, node.node_id);
3903
+ // 三态而不是「有/没有」:从未切换过 / 正在切 / 切失败,界面要给的下一步完全不同。
3904
+ const state = !row ? "never_switched"
3905
+ : row.status === "pending" ? "pending"
3906
+ : row.status === "failed" || row.status === "expired" ? "failed"
3907
+ : "applied";
3908
+ let selection: unknown = null;
3909
+ if (row?.snapshot_json) { try { selection = JSON.parse(row.snapshot_json); } catch { selection = null; } }
3910
+ return withCors(req, Response.json({
3911
+ ok: true,
3912
+ node_id: node.node_id,
3913
+ state,
3914
+ selection,
3915
+ revision: (selection as any)?.expected_revision ?? null,
3916
+ observed_at: row?.observed_at ? new Date(row.observed_at).toISOString() : null,
3917
+ ...(state === "failed" ? { error: row?.error ?? null } : {}),
3918
+ ...(state === "never_switched"
3919
+ ? { hint: "这个节点还没有被切换过 provider;显示的是它的本地默认配置(不是 Hub 的读数)" }
3920
+ : {}),
3921
+ }));
3922
+ }
3884
3923
  // #2969 后半 —— 接口白名单:PUT /api/nodes/:id/tool-allowlist { tools: string[] | null }
3885
3924
  // 与 peer-allowlist 同一套授权口径。
3886
3925
  const toolAllowMatch = url.pathname.match(/^\/api\/nodes\/([^/]+)\/tool-allowlist$/);