@njuptlzf/agent-chat-server 0.2.1 → 0.4.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 (55) hide show
  1. package/dist/branding-ui.d.ts +16 -0
  2. package/dist/branding-ui.js +119 -0
  3. package/dist/checkpoint-store.d.ts +78 -0
  4. package/dist/checkpoint-store.js +52 -0
  5. package/dist/components.d.ts +15 -0
  6. package/dist/components.js +50 -0
  7. package/dist/extension-admission.d.ts +32 -0
  8. package/dist/extension-admission.js +155 -0
  9. package/dist/flows.d.ts +49 -0
  10. package/dist/flows.js +208 -0
  11. package/dist/ledger.d.ts +90 -0
  12. package/dist/ledger.js +115 -0
  13. package/dist/main.js +14 -2
  14. package/dist/manifest.js +27 -1
  15. package/dist/orchestrator/engine.d.ts +132 -0
  16. package/dist/orchestrator/engine.js +1 -0
  17. package/dist/orchestrator/engines/fake.d.ts +14 -0
  18. package/dist/orchestrator/engines/fake.js +282 -0
  19. package/dist/orchestrator/engines/taskflow.d.ts +4 -0
  20. package/dist/orchestrator/engines/taskflow.js +541 -0
  21. package/dist/orchestrator/run-spec.d.ts +154 -0
  22. package/dist/orchestrator/run-spec.js +11 -0
  23. package/dist/persona-config.js +12 -0
  24. package/dist/server.d.ts +27 -2
  25. package/dist/server.js +595 -187
  26. package/dist/session.js +2 -1
  27. package/dist/sqlite-stores.d.ts +46 -6
  28. package/dist/sqlite-stores.js +292 -33
  29. package/dist/stores.d.ts +0 -36
  30. package/dist/stores.js +0 -27
  31. package/dist/suggestions.d.ts +60 -0
  32. package/dist/suggestions.js +107 -0
  33. package/dist/testing.d.ts +5 -4
  34. package/dist/testing.js +5 -4
  35. package/package.json +65 -64
  36. package/scripts/agentchat-integrate.mjs +852 -852
  37. package/src/branding-ui.ts +126 -0
  38. package/src/checkpoint-store.ts +122 -0
  39. package/src/components.ts +66 -0
  40. package/src/extension-admission.ts +131 -0
  41. package/src/flows.ts +244 -0
  42. package/src/ledger.ts +171 -0
  43. package/src/main.ts +16 -2
  44. package/src/manifest.ts +27 -1
  45. package/src/orchestrator/engine.ts +134 -0
  46. package/src/orchestrator/engines/fake.ts +330 -0
  47. package/src/orchestrator/engines/taskflow.ts +626 -0
  48. package/src/orchestrator/run-spec.ts +171 -0
  49. package/src/persona-config.ts +12 -1
  50. package/src/server.ts +611 -183
  51. package/src/session.ts +2 -1
  52. package/src/sqlite-stores.ts +343 -35
  53. package/src/stores.ts +0 -41
  54. package/src/suggestions.ts +146 -0
  55. package/src/testing.ts +4 -4
@@ -0,0 +1,107 @@
1
+ import { resolveTier } from "./manifest.js";
2
+ /**
3
+ * 缺省值与硬上限。前端 `followUpConfigOf` 各持一份同名缺省(两个包不共享模块,
4
+ * 单测钉住两边一致)——改这里要同步那边。
5
+ */
6
+ export const SUGGESTIONS_DEFAULTS = {
7
+ count: 3,
8
+ maxMessages: 10,
9
+ timeoutMs: 8000,
10
+ maxTokens: 200,
11
+ /** 单条消息文本上限(超出截断)。 */
12
+ messageChars: 500,
13
+ /** 整段对话字符上限(从最新往回收)。 */
14
+ totalChars: 4000,
15
+ /** 单条建议字符上限。 */
16
+ suggestionChars: 80,
17
+ };
18
+ /** 内置生成指令:中性、产品无关;`followUp.systemPrompt` 可整体覆盖。 */
19
+ function defaultSystemPrompt(count) {
20
+ return (`根据下面这段用户与助手的对话,推测用户接下来最可能发出的 ${count} 条简短追问。` +
21
+ "每行一条:不要编号、不要任何解释或前后缀,不要复述对话内容,每条不超过 30 个字。");
22
+ }
23
+ const NUMBERED = /^\s*(?:\d{1,3}[.)、]|[•*·-])\s*/;
24
+ /** 模型回复 → 建议列表:按行拆、去编号前缀、去空行、截到 count 条。 */
25
+ export function parseSuggestions(text, count) {
26
+ const cap = Math.max(1, Math.min(count, 20));
27
+ const out = [];
28
+ for (const line of text.split(/\r?\n/)) {
29
+ const s = line.replace(NUMBERED, "").trim();
30
+ if (!s)
31
+ continue;
32
+ out.push(s.length > SUGGESTIONS_DEFAULTS.suggestionChars ? s.slice(0, SUGGESTIONS_DEFAULTS.suggestionChars) : s);
33
+ if (out.length >= cap)
34
+ break;
35
+ }
36
+ return out;
37
+ }
38
+ /**
39
+ * 服务端强制的输入裁剪——前端已裁一道,这里是权威层:
40
+ * 只留 user/assistant 的非空文本、单条截断、取最近 maxMessages 条、整段不超 totalChars。
41
+ */
42
+ export function clipMessages(raw, maxMessages) {
43
+ if (!Array.isArray(raw))
44
+ return [];
45
+ const kept = [];
46
+ for (const entry of raw) {
47
+ if (!entry || typeof entry !== "object")
48
+ continue;
49
+ const role = entry.role;
50
+ const text = entry.text;
51
+ if ((role !== "user" && role !== "assistant") || typeof text !== "string")
52
+ continue;
53
+ const trimmed = text.trim();
54
+ if (!trimmed)
55
+ continue;
56
+ kept.push({ role, text: trimmed.slice(0, SUGGESTIONS_DEFAULTS.messageChars) });
57
+ }
58
+ const window = kept.slice(-Math.max(1, maxMessages));
59
+ let total = 0;
60
+ const out = [];
61
+ for (let i = window.length - 1; i >= 0; i--) {
62
+ total += window[i].text.length;
63
+ if (total > SUGGESTIONS_DEFAULTS.totalChars)
64
+ break;
65
+ out.unshift(window[i]);
66
+ }
67
+ return out;
68
+ }
69
+ /** 真调模型。任何失败都抛给路由(统一 catch 后回落 static)。 */
70
+ export async function runSuggestions(env, input) {
71
+ // 模型来源与建会话同源(manifest `agent.model`);model-config 的档位链是聊天主链
72
+ // 的东西,这条旁路不参与——失败静默,不值得为它引一整套 failover。
73
+ const tier = resolveTier(env.product);
74
+ if (!tier)
75
+ throw new Error("product has no default model");
76
+ const model = env.modelRuntime.getModel(tier.provider, tier.model);
77
+ if (!model)
78
+ throw new Error(`unknown model ${tier.provider}/${tier.model}`);
79
+ const dialogue = input.messages.map((m) => `${m.role}: ${m.text}`).join("\n");
80
+ const systemPrompt = input.systemPrompt?.trim() || defaultSystemPrompt(input.count);
81
+ const controller = new AbortController();
82
+ let timer;
83
+ try {
84
+ // 双保险:signal 传进 provider 请求;不遵守 signal 的底层由 Promise.race 兜底超时。
85
+ const timeout = new Promise((_, reject) => {
86
+ timer = setTimeout(() => {
87
+ controller.abort();
88
+ reject(new Error("suggestions timeout"));
89
+ }, Math.max(1, input.timeoutMs));
90
+ });
91
+ // 只构造一条 UserMessage 把对话拍平——AssistantMessage 必填字段太多,别手工造。
92
+ const reply = await Promise.race([
93
+ env.modelRuntime.completeSimple(model, { systemPrompt, messages: [{ role: "user", content: `最近一段对话:\n${dialogue}`, timestamp: Date.now() }] }, { signal: controller.signal, maxTokens: SUGGESTIONS_DEFAULTS.maxTokens }),
94
+ timeout,
95
+ ]);
96
+ const parts = [];
97
+ for (const c of reply.content) {
98
+ if (c.type === "text")
99
+ parts.push(c.text);
100
+ }
101
+ return parseSuggestions(parts.join("\n"), input.count);
102
+ }
103
+ finally {
104
+ if (timer !== undefined)
105
+ clearTimeout(timer);
106
+ }
107
+ }
package/dist/testing.d.ts CHANGED
@@ -19,8 +19,8 @@
19
19
  * ## 为什么不做成"产品自造替身"
20
20
  *
21
21
  * 曾考虑让产品自己写内存 store。否决理由:`createChatServer` 只认平台接口
22
- * (`HistoryStore`/`QuotaGate`/…),产品手写的替身必须逐字段对齐这些接口的
23
- * 行为——尤其是 `SqliteQuotaGate` 的"今日计数"、`SqliteMembership` 的幂等
22
+ * (`HistoryStore`/`Ledger`/…),产品手写的替身必须逐字段对齐这些接口的
23
+ * 行为——尤其是 `SqliteLedger` 的"今日计数"、`SqliteMembership` 的幂等
24
24
  * 写入这类带语义的。两侧各写一份,行为漂移时测试全绿而生产出问题。
25
25
  * 复用平台的实现,测试才验的是**真代码路径**。
26
26
  *
@@ -30,9 +30,10 @@
30
30
  * "产品生产代码依赖了平台内部"和"产品测试依赖了平台测试基建"两类事实
31
31
  * 就被同一个数字掩盖了——前者是边界事故,后者是正常需求。
32
32
  */
33
- export { DailyTokenQuota, MemoryAccountState, MemoryAudit, MemoryBilling, MemoryHistoryStore, MemoryMembership, createAdminAuth, createJwtAuth, insecureHeaderAuth, } from "./stores.ts";
33
+ export { MemoryLedger } from "./ledger.ts";
34
+ export { MemoryAccountState, MemoryAudit, MemoryBilling, MemoryHistoryStore, MemoryMembership, createAdminAuth, createJwtAuth, insecureHeaderAuth, } from "./stores.ts";
34
35
  export type { AccountStateStore, MembershipStore, } from "./stores.ts";
35
- export { SqliteBilling, SqliteHistoryStore, SqliteMemberPool, SqliteMembership, SqliteQuotaGate, } from "./sqlite-stores.ts";
36
+ export { SqliteBilling, SqliteHistoryStore, SqliteMemberPool, SqliteMembership, SqliteLedger, } from "./sqlite-stores.ts";
36
37
  export { Authz, UnavailableAuthz } from "./authz.ts";
37
38
  export { capsForPersona, capsForRoles, limitsForPersona } from "./authz.ts";
38
39
  export { agentFor, loadProduct, loadToolFactory } from "./manifest.ts";
package/dist/testing.js CHANGED
@@ -19,8 +19,8 @@
19
19
  * ## 为什么不做成"产品自造替身"
20
20
  *
21
21
  * 曾考虑让产品自己写内存 store。否决理由:`createChatServer` 只认平台接口
22
- * (`HistoryStore`/`QuotaGate`/…),产品手写的替身必须逐字段对齐这些接口的
23
- * 行为——尤其是 `SqliteQuotaGate` 的"今日计数"、`SqliteMembership` 的幂等
22
+ * (`HistoryStore`/`Ledger`/…),产品手写的替身必须逐字段对齐这些接口的
23
+ * 行为——尤其是 `SqliteLedger` 的"今日计数"、`SqliteMembership` 的幂等
24
24
  * 写入这类带语义的。两侧各写一份,行为漂移时测试全绿而生产出问题。
25
25
  * 复用平台的实现,测试才验的是**真代码路径**。
26
26
  *
@@ -31,9 +31,10 @@
31
31
  * 就被同一个数字掩盖了——前者是边界事故,后者是正常需求。
32
32
  */
33
33
  // ── store 实现(内存版:测试首选,无 IO) ──────────────────────────────
34
- export { DailyTokenQuota, MemoryAccountState, MemoryAudit, MemoryBilling, MemoryHistoryStore, MemoryMembership, createAdminAuth, createJwtAuth, insecureHeaderAuth, } from "./stores.js";
34
+ export { MemoryLedger } from "./ledger.js";
35
+ export { MemoryAccountState, MemoryAudit, MemoryBilling, MemoryHistoryStore, MemoryMembership, createAdminAuth, createJwtAuth, insecureHeaderAuth, } from "./stores.js";
35
36
  // ── store 实现(SQLite 版:验真实迁移/约束/事务时才用) ────────────────
36
- export { SqliteBilling, SqliteHistoryStore, SqliteMemberPool, SqliteMembership, SqliteQuotaGate, } from "./sqlite-stores.js";
37
+ export { SqliteBilling, SqliteHistoryStore, SqliteMemberPool, SqliteMembership, SqliteLedger, } from "./sqlite-stores.js";
37
38
  // ── 授权 ────────────────────────────────────────────────────────────────
38
39
  export { Authz, UnavailableAuthz } from "./authz.js";
39
40
  export { capsForPersona, capsForRoles, limitsForPersona } from "./authz.js";
package/package.json CHANGED
@@ -1,64 +1,65 @@
1
- {
2
- "name": "@njuptlzf/agent-chat-server",
3
- "version": "0.2.1",
4
- "description": "agentChat platform kernel: sessions, authz, billing, audit, model/MCP/persona config planes, and the product-directory loader.",
5
- "type": "module",
6
- "license": "Apache-2.0",
7
- "main": "./dist/index.js",
8
- "types": "./dist/index.d.ts",
9
- "bin": {
10
- "agentchat-integrate": "./scripts/agentchat-integrate.mjs"
11
- },
12
- "exports": {
13
- ".": {
14
- "agent-chat-source": "./src/index.ts",
15
- "types": "./dist/index.d.ts",
16
- "default": "./dist/index.js"
17
- },
18
- "./main": {
19
- "agent-chat-source": "./src/main.ts",
20
- "default": "./dist/main.js"
21
- },
22
- "./testing": {
23
- "agent-chat-source": "./src/testing.ts",
24
- "types": "./dist/testing.d.ts",
25
- "default": "./dist/testing.js"
26
- },
27
- "./package.json": "./package.json"
28
- },
29
- "files": [
30
- "dist",
31
- "src",
32
- "scripts/agentchat-integrate.mjs"
33
- ],
34
- "repository": {
35
- "type": "git",
36
- "url": "git+https://github.com/njuptlzf/agent-chat.git",
37
- "directory": "packages/server"
38
- },
39
- "bugs": {
40
- "url": "https://github.com/njuptlzf/agent-chat/issues"
41
- },
42
- "homepage": "https://github.com/njuptlzf/agent-chat#readme",
43
- "keywords": [
44
- "agent-chat",
45
- "chat-server",
46
- "authz",
47
- "billing",
48
- "audit"
49
- ],
50
- "engines": {
51
- "node": ">=22.13"
52
- },
53
- "publishConfig": {
54
- "access": "public"
55
- },
56
- "dependencies": {
57
- "@njuptlzf/agent-chat-protocol": "^0.2.0",
58
- "@earendil-works/pi-coding-agent": "1.2.0",
59
- "casbin": "^5.51.1",
60
- "typebox": "^1.3.27",
61
- "yaml": "^2.8.0",
62
- "pi-scoped-memory": "^0.3.0"
63
- }
64
- }
1
+ {
2
+ "name": "@njuptlzf/agent-chat-server",
3
+ "version": "0.4.0",
4
+ "description": "agentChat platform kernel: sessions, authz, billing, audit, model/MCP/persona config planes, and the product-directory loader.",
5
+ "type": "module",
6
+ "license": "Apache-2.0",
7
+ "main": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "bin": {
10
+ "agentchat-integrate": "./scripts/agentchat-integrate.mjs"
11
+ },
12
+ "exports": {
13
+ ".": {
14
+ "agent-chat-source": "./src/index.ts",
15
+ "types": "./dist/index.d.ts",
16
+ "default": "./dist/index.js"
17
+ },
18
+ "./main": {
19
+ "agent-chat-source": "./src/main.ts",
20
+ "default": "./dist/main.js"
21
+ },
22
+ "./testing": {
23
+ "agent-chat-source": "./src/testing.ts",
24
+ "types": "./dist/testing.d.ts",
25
+ "default": "./dist/testing.js"
26
+ },
27
+ "./package.json": "./package.json"
28
+ },
29
+ "files": [
30
+ "dist",
31
+ "src",
32
+ "scripts/agentchat-integrate.mjs"
33
+ ],
34
+ "repository": {
35
+ "type": "git",
36
+ "url": "git+https://github.com/njuptlzf/agent-chat.git",
37
+ "directory": "packages/server"
38
+ },
39
+ "bugs": {
40
+ "url": "https://github.com/njuptlzf/agent-chat/issues"
41
+ },
42
+ "homepage": "https://github.com/njuptlzf/agent-chat#readme",
43
+ "keywords": [
44
+ "agent-chat",
45
+ "chat-server",
46
+ "authz",
47
+ "billing",
48
+ "audit"
49
+ ],
50
+ "engines": {
51
+ "node": ">=22.15"
52
+ },
53
+ "publishConfig": {
54
+ "access": "public"
55
+ },
56
+ "dependencies": {
57
+ "@earendil-works/pi-coding-agent": "1.5.2",
58
+ "@njuptlzf/agent-chat-protocol": "^0.4.0",
59
+ "casbin": "^5.51.1",
60
+ "pi-scoped-memory": "^0.3.0",
61
+ "taskflow-core": "1.0.0",
62
+ "typebox": "^1.3.27",
63
+ "yaml": "^2.8.0"
64
+ }
65
+ }