@sleep2agi/commhub-server 0.9.0-preview.235 → 0.9.0-preview.237

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.235",
3
+ "version": "0.9.0-preview.237",
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",
@@ -136,6 +136,27 @@ describe("GET /api/skills", () => {
136
136
  const owner = await get(`/api/providers?network_id=${ownerNet}`, ownerToken);
137
137
  expect(owner.body.state).toBe("configured");
138
138
  expect("hint" in owner.body).toBe(false);
139
+
140
+ // 2026-10-31 —— **闭集不许有死成员**:客户端 union 里写着 all_disabled,
141
+ // 而这个端点原来只产出前两个 ⇒ 那一格永远不会出现、界面分支永远跑不到。
142
+ // 本地 hub 实测:建了 enabled:true 的行 ⇒ configured;把 enabled 改成 false ⇒ **仍是 configured**
143
+ // ⇒ 读的人会把「全都关了」看成「配置好了」。现在三态都必须可产出。
144
+ db.run(`UPDATE hub_provider_configs SET config_json = json_set(config_json, '$.enabled', json('false'))
145
+ WHERE network_id = ?1`, [ownerNet]);
146
+ const disabled = await get(`/api/providers?network_id=${ownerNet}`, ownerToken);
147
+ expect(disabled.body.state).toBe("all_disabled");
148
+ expect(disabled.body.counts.providers_enabled).toBe(0);
149
+ expect(disabled.body.counts.providers_disabled).toBeGreaterThan(0);
150
+ expect(disabled.body.counts.models_enabled).toBe(0);
151
+ expect(disabled.body.hint).toContain("全部被禁用");
152
+ expect(disabled.body.hint).toContain("不是「还没配置」");
153
+
154
+ // 还原:否则后面的用例会看到一个全禁用的网络
155
+ db.run(`UPDATE hub_provider_configs SET config_json = json_set(config_json, '$.enabled', json('true'))
156
+ WHERE network_id = ?1`, [ownerNet]);
157
+ const back = await get(`/api/providers?network_id=${ownerNet}`, ownerToken);
158
+ expect(back.body.state).toBe("configured");
159
+ expect(back.body.counts.models_enabled).toBeGreaterThan(0);
139
160
  });
140
161
 
141
162
  test("a member of another network is refused", async () => {
@@ -459,6 +480,22 @@ describe("GET /api/providers", () => {
459
480
  // 而语义已经不是你想的那个。」)
460
481
  // ⇒ 读点必须落在**路由文件**里(server.ts),否则等于表有了、用法另有其处。
461
482
  // 位置:读点必须落在**实现或路由**里(不是只落在注释里)
483
+ // 2026-10-31 —— **兜底不许把「我们的缺陷」说成「客户端的错」**:
484
+ // 表里查不到的码 ⇒ 归因指向了错误的行动方(返回 400 会让客户端去改自己的请求)。
485
+ // 判据同「遍历每个抛点断言它不等于 catch-all」:逐个码断言它映射到**自己那一格**,
486
+ // 再断言未登记的码落 500 而不是 400。
487
+ const { PROVIDER_WRITE_ERROR_CODES: CODES, PROVIDER_WRITE_STATUS: STATUS, providerWriteStatus: statusOf } =
488
+ await import("./hub-provider-config.js");
489
+ const CLOSED = new Set([400, 401, 403, 404, 409, 500, 503]);
490
+ for (const code of CODES) {
491
+ expect(statusOf(code)).toBe(STATUS[code]);
492
+ expect(CLOSED.has(statusOf(code))).toBe(true);
493
+ }
494
+ // 镜像(must-fire):一个**不在表里**的码必须落到 500,不能落 400
495
+ const bogus = statusOf("definitely_not_a_listed_code" as any);
496
+ expect(bogus).toBe(500);
497
+ expect(bogus).not.toBe(400);
498
+
462
499
  const readerFiles = new Set(readers.map((r) => String(r.f).split("/").pop()));
463
500
  expect(readerFiles.size).toBeGreaterThan(0);
464
501
  expect([...readerFiles].some((f) => f === "server.ts" || f === "hub-provider-config.ts" || f === "requirements.ts")).toBe(true);
@@ -50,8 +50,19 @@ export const PROVIDER_WRITE_STATUS: Record<ProviderWriteErrorCode, number> = {
50
50
  hub_provider_save_failed: 500,
51
51
  vault_unavailable: 503,
52
52
  };
53
- export function providerWriteStatus(code: string): number {
54
- return (PROVIDER_WRITE_STATUS as Record<string, number>)[code] ?? 400;
53
+ /**
54
+ * 码 → HTTP 状态。
55
+ *
56
+ * 2026-10-31 —— 参数类型从 `string` 收紧到 `ProviderWriteErrorCode`:
57
+ * 之前签名收 `string`,于是「表里加码、忘了加状态」在**调用点**不会被类型拦住
58
+ * (编译器只看见一个字符串)。而 `Record<ProviderWriteErrorCode, number>` 只在
59
+ * 「用类型化的键去索引」时才保证穷尽 —— 签名放宽就等于把穷尽性丢了。
60
+ *
61
+ * 兜底也改了方向:`?? 400` 会告诉客户端「你的请求不对」,而表里查不到的码是**我们**的缺陷
62
+ * ⇒ 归因指向了错误的行动方。现在兜底返回 500(服务端错误),并让测试断言它不被 400 顶替。
63
+ */
64
+ export function providerWriteStatus(code: ProviderWriteErrorCode): number {
65
+ return PROVIDER_WRITE_STATUS[code] ?? 500;
55
66
  }
56
67
  const idValid = (id: unknown) => typeof id === 'string' && /^[A-Za-z0-9][A-Za-z0-9_-]{0,63}$/.test(id);
57
68
  function auth(h: Human, write: boolean, owner = false) {
@@ -95,12 +106,33 @@ export function readHubProviders(h: Human, exposeRefs = false) {
95
106
  const otherSource = db.get<{ cnt: number }>(
96
107
  'SELECT COUNT(*) AS cnt FROM providers WHERE network_id=?1 AND enabled=1', h.networkId!);
97
108
  const otherCount = otherSource?.cnt ?? 0;
98
- const state = rows.length > 0 ? 'configured' as const : 'unconfigured' as const;
109
+ // 2026-10-31 —— **闭集不许有死成员**:客户端 union 里写着
110
+ // "configured" | "unconfigured" | "all_disabled",而这个端点原来只产出前两个
111
+ // ⇒ all_disabled 是一格永远不会出现的状态,界面里那条分支也就永远跑不到
112
+ // (实测:本地 hub 建了一行 enabled:true 的 provider 之后,state 仍是 configured/空表是 unconfigured,
113
+ // 把 enabled 改成 false 依旧是 configured ⇒ 读的人会把「全都关了」看成「配置好了」)。
114
+ // 现在三态都可产出,并把「哪些算可用」用计数说清(客户端不必自己再数一遍)。
115
+ const parsed = rows.map(r => JSON.parse(r.config_json) as { enabled?: boolean; models?: string[] });
116
+ const enabledRows = parsed.filter(c => c.enabled === true);
117
+ const state = rows.length === 0 ? 'unconfigured' as const
118
+ : enabledRows.length === 0 ? 'all_disabled' as const
119
+ : 'configured' as const;
99
120
  return { ok: true as const, network_id: h.networkId!, source: 'hub' as const,
100
121
  // 读的是哪张表 —— 名字直接写出来,避免下一个人拿这个结果去解释另一个接口
101
122
  source_table: 'hub_provider_configs' as const,
102
123
  state,
103
- counts: { providers: rows.length, providers_in_cli_registry: otherCount },
124
+ counts: {
125
+ providers: rows.length,
126
+ providers_enabled: enabledRows.length,
127
+ providers_disabled: rows.length - enabledRows.length,
128
+ models_enabled: enabledRows.reduce((n, c) => n + (Array.isArray(c.models) ? c.models.length : 0), 0),
129
+ providers_without_models: enabledRows.filter(c => !Array.isArray(c.models) || c.models.length === 0).length,
130
+ providers_in_cli_registry: otherCount,
131
+ },
132
+ ...(state === 'all_disabled' ? {
133
+ hint: '这个网络里配置了 ' + rows.length + ' 个 provider,但**全部被禁用** ⇒ 客户端仍然没有可选模型。'
134
+ + '这不是「还没配置」,要做的是启用其中一个(或在确认无用后删除)。',
135
+ } : {}),
104
136
  ...(state === 'unconfigured' ? {
105
137
  hint: otherCount > 0
106
138
  ? '本端点读 hub_provider_configs(网络自有 provider 配置),此处为空;但要给客户端选模型,还需要这里至少一行 —— CLI 注册表里的 ' + otherCount + ' 条不会自动出现在本端点。'