@njuptlzf/agent-chat-server 0.1.2 → 0.2.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.
Files changed (65) hide show
  1. package/dist/audit.d.ts +54 -0
  2. package/dist/audit.js +133 -0
  3. package/dist/authz.d.ts +134 -0
  4. package/dist/authz.js +238 -0
  5. package/dist/events.d.ts +33 -0
  6. package/dist/events.js +169 -0
  7. package/dist/index.d.ts +16 -0
  8. package/dist/index.js +4 -0
  9. package/dist/main.d.ts +1 -0
  10. package/dist/main.js +126 -0
  11. package/dist/manifest.d.ts +147 -0
  12. package/dist/manifest.js +491 -0
  13. package/dist/mcp-config.d.ts +87 -0
  14. package/dist/mcp-config.js +353 -0
  15. package/dist/model-config.d.ts +123 -0
  16. package/dist/model-config.js +371 -0
  17. package/dist/model-failover.d.ts +42 -0
  18. package/dist/model-failover.js +82 -0
  19. package/dist/models-api.d.ts +98 -0
  20. package/dist/models-api.js +326 -0
  21. package/dist/persona-config.d.ts +112 -0
  22. package/dist/persona-config.js +368 -0
  23. package/dist/persona-trial.d.ts +50 -0
  24. package/dist/persona-trial.js +77 -0
  25. package/dist/product-routes.d.ts +35 -0
  26. package/dist/product-routes.js +92 -0
  27. package/dist/server.d.ts +162 -0
  28. package/dist/server.js +2572 -0
  29. package/dist/session.d.ts +218 -0
  30. package/dist/session.js +545 -0
  31. package/dist/sqlite-stores.d.ts +173 -0
  32. package/dist/sqlite-stores.js +921 -0
  33. package/dist/stores.d.ts +506 -0
  34. package/dist/stores.js +614 -0
  35. package/dist/testing.d.ts +42 -0
  36. package/dist/testing.js +47 -0
  37. package/dist/trial-grant.d.ts +40 -0
  38. package/dist/trial-grant.js +24 -0
  39. package/dist/usage-report.d.ts +23 -0
  40. package/dist/usage-report.js +68 -0
  41. package/dist/util.d.ts +3 -0
  42. package/dist/util.js +5 -0
  43. package/package.json +15 -9
  44. package/scripts/agentchat-integrate.mjs +607 -607
  45. package/src/audit.ts +169 -169
  46. package/src/authz.ts +307 -307
  47. package/src/events.ts +206 -206
  48. package/src/index.ts +26 -26
  49. package/src/main.ts +142 -142
  50. package/src/manifest.ts +562 -562
  51. package/src/mcp-config.ts +385 -385
  52. package/src/model-config.ts +424 -424
  53. package/src/model-failover.ts +95 -95
  54. package/src/models-api.ts +360 -360
  55. package/src/persona-config.ts +436 -436
  56. package/src/persona-trial.ts +121 -121
  57. package/src/product-routes.ts +102 -102
  58. package/src/server.ts +2647 -2647
  59. package/src/session.ts +688 -688
  60. package/src/sqlite-stores.ts +1040 -1040
  61. package/src/stores.ts +899 -899
  62. package/src/testing.ts +80 -80
  63. package/src/trial-grant.ts +62 -62
  64. package/src/usage-report.ts +86 -86
  65. package/src/util.ts +6 -6
@@ -1,95 +1,95 @@
1
- import type { ModelCandidate } from "./model-config.ts";
2
-
3
- /**
4
- * 运行时候选链的失败切换。
5
- *
6
- * 这一层只放**纯判定**:错误分类、冷却、链序。真正的「重建会话 + 重发 prompt」在
7
- * server.ts 的 run 循环里(它才知道流到哪一步了)。pi 自己的同 provider 重试
8
- * (408/409/429/5xx backoff,2–3 次)在更下面,先让它重试完,我们再切候选。
9
- */
10
-
11
- /** 文案:用户侧只看到这一句,永远不出现 provider / 模型名。 */
12
- export const FAILOVER_NOTICE = "服务繁忙,已自动切换另一条线路";
13
-
14
- export const candidateKey = (c: ModelCandidate): string => `${c.provider}/${c.model}`;
15
-
16
- export interface CooldownRegistry {
17
- /** 给刚失败的候选打冷却;seconds<=0 等于不冷却。 */
18
- mark(c: ModelCandidate, seconds: number): void;
19
- isCooling(c: ModelCandidate): boolean;
20
- clear(): void;
21
- }
22
-
23
- /**
24
- * 进程内存态(不进 DB,重启清空)。冷却不是「拉黑」——见 orderChain。
25
- */
26
- export function createCooldownRegistry(now: () => number = Date.now): CooldownRegistry {
27
- const until = new Map<string, number>();
28
- return {
29
- mark(c, seconds) {
30
- if (!Number.isFinite(seconds) || seconds <= 0) return;
31
- until.set(candidateKey(c), now() + seconds * 1000);
32
- },
33
- isCooling(c) {
34
- const key = candidateKey(c);
35
- const at = until.get(key);
36
- if (at === undefined) return false;
37
- if (at <= now()) {
38
- until.delete(key);
39
- return false;
40
- }
41
- return true;
42
- },
43
- clear() {
44
- until.clear();
45
- },
46
- };
47
- }
48
-
49
- /** 进程级默认冷却表(main.ts 不装配也能用;测试注入自己的实例以隔离)。 */
50
- export const processCooldown: CooldownRegistry = createCooldownRegistry();
51
-
52
- /**
53
- * 冷却中的候选**排到链尾**,不是删掉: 说的是「不再被选为主选」,不是「不能当备选」。
54
- * 只有一条候选时它仍然会被用——总比整个产品答不出话强。
55
- */
56
- export function orderChain(chain: readonly ModelCandidate[], cooldown: CooldownRegistry): ModelCandidate[] {
57
- const ready: ModelCandidate[] = [];
58
- const cooling: ModelCandidate[] = [];
59
- for (const c of chain) (cooldown.isCooling(c) ? cooling : ready).push(c);
60
- return [...ready, ...cooling];
61
- }
62
-
63
- /**
64
- * 换家也没用的错误:**请求本身**坏了(参数非法、超长、不支持的取值)。这类在哪个
65
- * provider 都会失败,切过去只是把备用线路也烧一次。
66
- *
67
- * 注意 401/403 不在这一档:那是**钥匙**坏了,不是请求坏了。跨 provider 的候选各拿
68
- * 各的 key,备用线路完全可能好使——这正是「备一条线路」的意义(验收原话:
69
- * 「主线路 401 自动切备用出答案」)。同 provider 内 401 会连切到链尾再冒泡,代价有界。
70
- */
71
- const FATAL = /\b(400|422)\b|invalid[ _]request|bad[ _]request|context[ _]length|too many tokens|request too large|unsupported[ _](parameter|value)/i;
72
-
73
- /** 可重试:鉴权/找不到端点(换钥匙、换端点可能就好了)、限流、服务端故障、网络抖动。 */
74
- const RETRYABLE = /\b(401|403|404|405|408|409|429|5\d\d)\b|invalid[ _-]?api[ _-]?key|unauthorized|forbidden|not found|does not exist|rate[ _-]?limit|timeout|timed out|ETIMEDOUT|ECONNRESET|ECONNREFUSED|ECONNABORTED|EAI_AGAIN|ENOTFOUND|socket hang up|fetch failed|network|overloaded|unavailable|bad gateway|gateway timeout/i;
75
-
76
- /**
77
- * 这次失败值不值得换候选。判不出来的一律**不切**(保守):宁可把原文冒给用户,
78
- * 也不要为一条看不懂的错误悄悄换家、让「谁在答」变成谎话。
79
- */
80
- export function isRetryableModelError(message: string): boolean {
81
- if (FATAL.test(message)) return false;
82
- return RETRYABLE.test(message);
83
- }
84
-
85
- /**
86
- * 从 pi 原始事件取模型报错原文(2026-11-04):failover 的正则**只吃原文**——
87
- * 用户看到的那条 error 消息已经由 events.ts 折叠成人话,不能拿它当判定输入。
88
- * 与 events.ts 的 error 事件同源同条件(message_end + stopReason==="error")。
89
- */
90
- export function rawModelError(event: { type: string; message?: unknown }): string {
91
- if (event.type !== "message_end") return "";
92
- // pi 的 message 是多态联合(System/User/Assistant…),这里只关心 stopReason/errorMessage。
93
- const msg = event.message as { stopReason?: string; errorMessage?: string } | undefined;
94
- return msg?.stopReason === "error" ? (msg.errorMessage ?? "") : "";
95
- }
1
+ import type { ModelCandidate } from "./model-config.ts";
2
+
3
+ /**
4
+ * 运行时候选链的失败切换。
5
+ *
6
+ * 这一层只放**纯判定**:错误分类、冷却、链序。真正的「重建会话 + 重发 prompt」在
7
+ * server.ts 的 run 循环里(它才知道流到哪一步了)。pi 自己的同 provider 重试
8
+ * (408/409/429/5xx backoff,2–3 次)在更下面,先让它重试完,我们再切候选。
9
+ */
10
+
11
+ /** 文案:用户侧只看到这一句,永远不出现 provider / 模型名。 */
12
+ export const FAILOVER_NOTICE = "服务繁忙,已自动切换另一条线路";
13
+
14
+ export const candidateKey = (c: ModelCandidate): string => `${c.provider}/${c.model}`;
15
+
16
+ export interface CooldownRegistry {
17
+ /** 给刚失败的候选打冷却;seconds<=0 等于不冷却。 */
18
+ mark(c: ModelCandidate, seconds: number): void;
19
+ isCooling(c: ModelCandidate): boolean;
20
+ clear(): void;
21
+ }
22
+
23
+ /**
24
+ * 进程内存态(不进 DB,重启清空)。冷却不是「拉黑」——见 orderChain。
25
+ */
26
+ export function createCooldownRegistry(now: () => number = Date.now): CooldownRegistry {
27
+ const until = new Map<string, number>();
28
+ return {
29
+ mark(c, seconds) {
30
+ if (!Number.isFinite(seconds) || seconds <= 0) return;
31
+ until.set(candidateKey(c), now() + seconds * 1000);
32
+ },
33
+ isCooling(c) {
34
+ const key = candidateKey(c);
35
+ const at = until.get(key);
36
+ if (at === undefined) return false;
37
+ if (at <= now()) {
38
+ until.delete(key);
39
+ return false;
40
+ }
41
+ return true;
42
+ },
43
+ clear() {
44
+ until.clear();
45
+ },
46
+ };
47
+ }
48
+
49
+ /** 进程级默认冷却表(main.ts 不装配也能用;测试注入自己的实例以隔离)。 */
50
+ export const processCooldown: CooldownRegistry = createCooldownRegistry();
51
+
52
+ /**
53
+ * 冷却中的候选**排到链尾**,不是删掉: 说的是「不再被选为主选」,不是「不能当备选」。
54
+ * 只有一条候选时它仍然会被用——总比整个产品答不出话强。
55
+ */
56
+ export function orderChain(chain: readonly ModelCandidate[], cooldown: CooldownRegistry): ModelCandidate[] {
57
+ const ready: ModelCandidate[] = [];
58
+ const cooling: ModelCandidate[] = [];
59
+ for (const c of chain) (cooldown.isCooling(c) ? cooling : ready).push(c);
60
+ return [...ready, ...cooling];
61
+ }
62
+
63
+ /**
64
+ * 换家也没用的错误:**请求本身**坏了(参数非法、超长、不支持的取值)。这类在哪个
65
+ * provider 都会失败,切过去只是把备用线路也烧一次。
66
+ *
67
+ * 注意 401/403 不在这一档:那是**钥匙**坏了,不是请求坏了。跨 provider 的候选各拿
68
+ * 各的 key,备用线路完全可能好使——这正是「备一条线路」的意义(验收原话:
69
+ * 「主线路 401 自动切备用出答案」)。同 provider 内 401 会连切到链尾再冒泡,代价有界。
70
+ */
71
+ const FATAL = /\b(400|422)\b|invalid[ _]request|bad[ _]request|context[ _]length|too many tokens|request too large|unsupported[ _](parameter|value)/i;
72
+
73
+ /** 可重试:鉴权/找不到端点(换钥匙、换端点可能就好了)、限流、服务端故障、网络抖动。 */
74
+ const RETRYABLE = /\b(401|403|404|405|408|409|429|5\d\d)\b|invalid[ _-]?api[ _-]?key|unauthorized|forbidden|not found|does not exist|rate[ _-]?limit|timeout|timed out|ETIMEDOUT|ECONNRESET|ECONNREFUSED|ECONNABORTED|EAI_AGAIN|ENOTFOUND|socket hang up|fetch failed|network|overloaded|unavailable|bad gateway|gateway timeout/i;
75
+
76
+ /**
77
+ * 这次失败值不值得换候选。判不出来的一律**不切**(保守):宁可把原文冒给用户,
78
+ * 也不要为一条看不懂的错误悄悄换家、让「谁在答」变成谎话。
79
+ */
80
+ export function isRetryableModelError(message: string): boolean {
81
+ if (FATAL.test(message)) return false;
82
+ return RETRYABLE.test(message);
83
+ }
84
+
85
+ /**
86
+ * 从 pi 原始事件取模型报错原文(2026-11-04):failover 的正则**只吃原文**——
87
+ * 用户看到的那条 error 消息已经由 events.ts 折叠成人话,不能拿它当判定输入。
88
+ * 与 events.ts 的 error 事件同源同条件(message_end + stopReason==="error")。
89
+ */
90
+ export function rawModelError(event: { type: string; message?: unknown }): string {
91
+ if (event.type !== "message_end") return "";
92
+ // pi 的 message 是多态联合(System/User/Assistant…),这里只关心 stopReason/errorMessage。
93
+ const msg = event.message as { stopReason?: string; errorMessage?: string } | undefined;
94
+ return msg?.stopReason === "error" ? (msg.errorMessage ?? "") : "";
95
+ }