@njuptlzf/agent-chat-server 0.1.1 → 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.
- package/dist/audit.d.ts +54 -0
- package/dist/audit.js +133 -0
- package/dist/authz.d.ts +134 -0
- package/dist/authz.js +238 -0
- package/dist/events.d.ts +33 -0
- package/dist/events.js +169 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +4 -0
- package/dist/main.d.ts +1 -0
- package/dist/main.js +126 -0
- package/dist/manifest.d.ts +147 -0
- package/dist/manifest.js +491 -0
- package/dist/mcp-config.d.ts +87 -0
- package/dist/mcp-config.js +353 -0
- package/dist/model-config.d.ts +123 -0
- package/dist/model-config.js +371 -0
- package/dist/model-failover.d.ts +42 -0
- package/dist/model-failover.js +82 -0
- package/dist/models-api.d.ts +98 -0
- package/dist/models-api.js +326 -0
- package/dist/persona-config.d.ts +112 -0
- package/dist/persona-config.js +368 -0
- package/dist/persona-trial.d.ts +50 -0
- package/dist/persona-trial.js +77 -0
- package/dist/product-routes.d.ts +35 -0
- package/dist/product-routes.js +92 -0
- package/dist/server.d.ts +162 -0
- package/dist/server.js +2572 -0
- package/dist/session.d.ts +218 -0
- package/dist/session.js +545 -0
- package/dist/sqlite-stores.d.ts +173 -0
- package/dist/sqlite-stores.js +921 -0
- package/dist/stores.d.ts +506 -0
- package/dist/stores.js +614 -0
- package/dist/testing.d.ts +42 -0
- package/dist/testing.js +47 -0
- package/dist/trial-grant.d.ts +40 -0
- package/dist/trial-grant.js +24 -0
- package/dist/usage-report.d.ts +23 -0
- package/dist/usage-report.js +68 -0
- package/dist/util.d.ts +3 -0
- package/dist/util.js +5 -0
- package/package.json +64 -58
- package/scripts/agentchat-integrate.mjs +607 -601
- package/src/audit.ts +169 -169
- package/src/authz.ts +307 -307
- package/src/events.ts +206 -206
- package/src/index.ts +26 -26
- package/src/main.ts +142 -142
- package/src/manifest.ts +562 -562
- package/src/mcp-config.ts +385 -385
- package/src/model-config.ts +424 -424
- package/src/model-failover.ts +95 -95
- package/src/models-api.ts +360 -360
- package/src/persona-config.ts +436 -436
- package/src/persona-trial.ts +121 -121
- package/src/product-routes.ts +102 -102
- package/src/server.ts +2647 -2647
- package/src/session.ts +688 -688
- package/src/sqlite-stores.ts +1040 -1040
- package/src/stores.ts +899 -899
- package/src/testing.ts +80 -80
- package/src/trial-grant.ts +62 -62
- package/src/usage-report.ts +86 -86
- package/src/util.ts +6 -6
package/dist/testing.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@njuptlzf/agent-chat-server/testing` —— 产品测试用的平台接缝。
|
|
3
|
+
*
|
|
4
|
+
* ## 这个出口和 `.`(主出口)的区别
|
|
5
|
+
*
|
|
6
|
+
* `.` 只导出《[PUBLIC_API](../../../docs/design/PUBLIC_API.md)》登记的那 18 项
|
|
7
|
+
* **生产接缝**:产品在运行期必需的东西,每增一项都要说服守卫。
|
|
8
|
+
*
|
|
9
|
+
* 这个出口导出的是**测试基建**——内存版 store、SQLite 版 store、JWT/Authz
|
|
10
|
+
* 构造器、会话构造器。它们的共同点:产品测试要拿它们搭一个真跑得起来的
|
|
11
|
+
* server,但产品的生产代码一行都不该 import 它们。
|
|
12
|
+
*
|
|
13
|
+
* ## 稳定性承诺:没有
|
|
14
|
+
*
|
|
15
|
+
* 这里的符号**不承诺 API 稳定**。平台重构时可以改名、改签名、删掉——
|
|
16
|
+
* 只要同步改产品仓的测试即可(生产代码不会因为这里的变化编译不过)。
|
|
17
|
+
* 别在产品业务代码里 import 这个出口;守卫会发现(见 §边界)。
|
|
18
|
+
*
|
|
19
|
+
* ## 为什么不做成"产品自造替身"
|
|
20
|
+
*
|
|
21
|
+
* 曾考虑让产品自己写内存 store。否决理由:`createChatServer` 只认平台接口
|
|
22
|
+
* (`HistoryStore`/`QuotaGate`/…),产品手写的替身必须逐字段对齐这些接口的
|
|
23
|
+
* 行为——尤其是 `SqliteQuotaGate` 的"今日计数"、`SqliteMembership` 的幂等
|
|
24
|
+
* 写入这类带语义的。两侧各写一份,行为漂移时测试全绿而生产出问题。
|
|
25
|
+
* 复用平台的实现,测试才验的是**真代码路径**。
|
|
26
|
+
*
|
|
27
|
+
* ## 为什么不并入主出口
|
|
28
|
+
*
|
|
29
|
+
* 主出口的 `public-api.test.ts` 会数条数(18 项)。把测试基建混进去,
|
|
30
|
+
* "产品生产代码依赖了平台内部"和"产品测试依赖了平台测试基建"两类事实
|
|
31
|
+
* 就被同一个数字掩盖了——前者是边界事故,后者是正常需求。
|
|
32
|
+
*/
|
|
33
|
+
// ── store 实现(内存版:测试首选,无 IO) ──────────────────────────────
|
|
34
|
+
export { DailyTokenQuota, MemoryAccountState, MemoryAudit, MemoryBilling, MemoryHistoryStore, MemoryMembership, createAdminAuth, createJwtAuth, insecureHeaderAuth, } from "./stores.js";
|
|
35
|
+
// ── store 实现(SQLite 版:验真实迁移/约束/事务时才用) ────────────────
|
|
36
|
+
export { SqliteBilling, SqliteHistoryStore, SqliteMemberPool, SqliteMembership, SqliteQuotaGate, } from "./sqlite-stores.js";
|
|
37
|
+
// ── 授权 ────────────────────────────────────────────────────────────────
|
|
38
|
+
export { Authz, UnavailableAuthz } from "./authz.js";
|
|
39
|
+
export { capsForPersona, capsForRoles, limitsForPersona } from "./authz.js";
|
|
40
|
+
// ── 产品装配与产品加载 ──────────────────────────────────────────────────
|
|
41
|
+
export { agentFor, loadProduct, loadToolFactory } from "./manifest.js";
|
|
42
|
+
export { loadProductAssembly } from "./product-routes.js";
|
|
43
|
+
export { validatePersona } from "./persona-config.js";
|
|
44
|
+
// ── 会话 ────────────────────────────────────────────────────────────────
|
|
45
|
+
export { buildCommands, createModelRuntime, createProductSession, disposeProductSession, expandCommand, exportHistory, loadProductSkills, } from "./session.js";
|
|
46
|
+
// ── server 构造 ─────────────────────────────────────────────────────────
|
|
47
|
+
export { createChatServer } from "./server.js";
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { DatabaseSync } from "node:sqlite";
|
|
2
|
+
/**
|
|
3
|
+
* 存量会员的「新品类试用」发放(决策①,2026-11-02)。
|
|
4
|
+
*
|
|
5
|
+
* 背景:老会员当年付的钱买的是默认人格那本账,不含后来才新增的人格账本。不补试用会变成
|
|
6
|
+
* 「老会员用新品类要再付一次钱」的体感降级;补一份**统一 30 天**(不按剩余天数等比——
|
|
7
|
+
* 3 天/7 天这种碎值难解释,成本也没上界收益)。
|
|
8
|
+
*
|
|
9
|
+
* 三条语义:
|
|
10
|
+
* - **只给在期会员**(`member_until > now`):已经过期的人不是"存量会员";
|
|
11
|
+
* - **只给还没有目标账本的人**:有行就跳过(哪怕已过期)⇒ 重复执行不会叠加天数;
|
|
12
|
+
* - 默认 **dry-run**,`apply: true` 才写;写走 `extend`(原子续期,和购买/活动同一口径)。
|
|
13
|
+
*
|
|
14
|
+
* core 不认识任何具体人格名:`from` / `to` 都由运维从产品 manifest 取。
|
|
15
|
+
*/
|
|
16
|
+
export interface TrialGrantOptions {
|
|
17
|
+
/** 谁算「老会员」:读哪本账的在期记录。 */
|
|
18
|
+
from: string;
|
|
19
|
+
/** 发给哪本账。 */
|
|
20
|
+
to: string;
|
|
21
|
+
days: number;
|
|
22
|
+
/** 写进 `granted_by`,事后能回答"这份试用哪来的"。 */
|
|
23
|
+
grantedBy: string;
|
|
24
|
+
/** false = 只算不发(默认)。 */
|
|
25
|
+
apply?: boolean;
|
|
26
|
+
}
|
|
27
|
+
export interface TrialGrantReport {
|
|
28
|
+
/** 在期老会员数(from 账本)。 */
|
|
29
|
+
scanned: number;
|
|
30
|
+
/** 其中还没有 to 账本、本次会/已发放的人数。 */
|
|
31
|
+
eligible: number;
|
|
32
|
+
/** 已有 to 账本而跳过的人数(含过期——幂等的关键)。 */
|
|
33
|
+
skipped: number;
|
|
34
|
+
/** apply 时逐人结果;dry-run 为空。 */
|
|
35
|
+
granted: Array<{
|
|
36
|
+
userId: string;
|
|
37
|
+
memberUntil: string;
|
|
38
|
+
}>;
|
|
39
|
+
}
|
|
40
|
+
export declare function grantTrial(db: DatabaseSync, opts: TrialGrantOptions): Promise<TrialGrantReport>;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { SqliteMembership } from "./sqlite-stores.js";
|
|
2
|
+
export async function grantTrial(db, opts) {
|
|
3
|
+
if (!Number.isInteger(opts.days) || opts.days < 1)
|
|
4
|
+
throw new Error(`days must be a positive integer, got ${opts.days}`);
|
|
5
|
+
const now = new Date().toISOString();
|
|
6
|
+
const rows = db
|
|
7
|
+
.prepare("SELECT user_id FROM membership WHERE product = ? AND member_until > ? ORDER BY user_id")
|
|
8
|
+
.all(opts.from, now);
|
|
9
|
+
const hasTarget = db.prepare("SELECT 1 AS ok FROM membership WHERE user_id = ? AND product = ?");
|
|
10
|
+
const membership = new SqliteMembership(db);
|
|
11
|
+
const report = { scanned: rows.length, eligible: 0, skipped: 0, granted: [] };
|
|
12
|
+
for (const row of rows) {
|
|
13
|
+
if (hasTarget.get(row.user_id, opts.to)) {
|
|
14
|
+
report.skipped += 1;
|
|
15
|
+
continue;
|
|
16
|
+
}
|
|
17
|
+
report.eligible += 1;
|
|
18
|
+
if (!opts.apply)
|
|
19
|
+
continue;
|
|
20
|
+
const memberUntil = await membership.extend(row.user_id, opts.to, opts.days, opts.grantedBy);
|
|
21
|
+
report.granted.push({ userId: row.user_id, memberUntil });
|
|
22
|
+
}
|
|
23
|
+
return report;
|
|
24
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { DatabaseSync } from "node:sqlite";
|
|
2
|
+
import type { BillingUsageReport } from "@njuptlzf/agent-chat-protocol";
|
|
3
|
+
/**
|
|
4
|
+
* Spend rolled up by qualification and by **用户的本地日**(B 批次: 消费明细看不出是哪个
|
|
5
|
+
* 选项花的钱)。
|
|
6
|
+
*
|
|
7
|
+
* 日界口径(2026-11-03 统一):与 `/api/progress` 的 `tzOffset` 同源(`localDay`)——
|
|
8
|
+
* 中国用户 00:00–08:00 的消费落在「今天」,不再落进 UTC 的昨天。不传 `tzOffsetMinutes`
|
|
9
|
+
* = 0(UTC 日界),老调用方行为不变。学习记录的进度分桶早就按本地日,两边从此一致。
|
|
10
|
+
*
|
|
11
|
+
* Zero migration: `billing_ledger.ref` holds the chat sessionId for every spend,
|
|
12
|
+
* and `history` has carried a `scope` column since S0. Joining the two recovers
|
|
13
|
+
* the qualification without touching the schema — measured against the dev
|
|
14
|
+
* database, 23/23 spend rows matched. Recharges are excluded on purpose: they
|
|
15
|
+
* are account-level top-ups, not something a qualification consumed.
|
|
16
|
+
*
|
|
17
|
+
* The join is on (user_id, session_id) because a session id is only unique per
|
|
18
|
+
* user; matching on session_id alone would let one user's spend borrow
|
|
19
|
+
* another's scope. A spend whose session row is gone (history wiped) still
|
|
20
|
+
* counts — it lands in the "" bucket instead of vanishing, so the totals always
|
|
21
|
+
* agree with the ledger.
|
|
22
|
+
*/
|
|
23
|
+
export declare function billingUsage(db: DatabaseSync, userId: string, days?: number, tzOffsetMinutes?: number, nowMs?: number): BillingUsageReport;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { localDay } from "./util.js";
|
|
2
|
+
/** Window cap: a year of daily buckets is still a readable chart, 10 years is not. */
|
|
3
|
+
const MAX_DAYS = 365;
|
|
4
|
+
const DEFAULT_DAYS = 30;
|
|
5
|
+
/**
|
|
6
|
+
* Spend rolled up by qualification and by **用户的本地日**(B 批次: 消费明细看不出是哪个
|
|
7
|
+
* 选项花的钱)。
|
|
8
|
+
*
|
|
9
|
+
* 日界口径(2026-11-03 统一):与 `/api/progress` 的 `tzOffset` 同源(`localDay`)——
|
|
10
|
+
* 中国用户 00:00–08:00 的消费落在「今天」,不再落进 UTC 的昨天。不传 `tzOffsetMinutes`
|
|
11
|
+
* = 0(UTC 日界),老调用方行为不变。学习记录的进度分桶早就按本地日,两边从此一致。
|
|
12
|
+
*
|
|
13
|
+
* Zero migration: `billing_ledger.ref` holds the chat sessionId for every spend,
|
|
14
|
+
* and `history` has carried a `scope` column since S0. Joining the two recovers
|
|
15
|
+
* the qualification without touching the schema — measured against the dev
|
|
16
|
+
* database, 23/23 spend rows matched. Recharges are excluded on purpose: they
|
|
17
|
+
* are account-level top-ups, not something a qualification consumed.
|
|
18
|
+
*
|
|
19
|
+
* The join is on (user_id, session_id) because a session id is only unique per
|
|
20
|
+
* user; matching on session_id alone would let one user's spend borrow
|
|
21
|
+
* another's scope. A spend whose session row is gone (history wiped) still
|
|
22
|
+
* counts — it lands in the "" bucket instead of vanishing, so the totals always
|
|
23
|
+
* agree with the ledger.
|
|
24
|
+
*/
|
|
25
|
+
export function billingUsage(db, userId, days = DEFAULT_DAYS, tzOffsetMinutes = 0, nowMs = Date.now()) {
|
|
26
|
+
const window = Math.min(Math.max(Math.trunc(days) || DEFAULT_DAYS, 1), MAX_DAYS);
|
|
27
|
+
// tzOffset 防守与路由同口径:非法值回落 UTC(不 500),这里兜第二道。
|
|
28
|
+
const tz = Number.isInteger(tzOffsetMinutes) && tzOffsetMinutes >= -840 && tzOffsetMinutes <= 840 ? tzOffsetMinutes : 0;
|
|
29
|
+
// 窗口按**本地日**对齐:从「今天(本地)的 0 点」往回数 window-1 天,
|
|
30
|
+
// 而不是 now - window*24h——否则窗口两端各缺半天,图表第一/最后一根柱子永远缺一块。
|
|
31
|
+
const todayLocal = localDay(nowMs, tz);
|
|
32
|
+
const since = new Date(Date.parse(`${todayLocal}T00:00:00.000Z`) - tz * 60_000 - (window - 1) * 86_400_000).toISOString();
|
|
33
|
+
const byScope = db
|
|
34
|
+
.prepare(`SELECT COALESCE(h.scope, '') AS scope,
|
|
35
|
+
COALESCE(SUM(l.amount_cents), 0) AS spend,
|
|
36
|
+
COUNT(*) AS charges
|
|
37
|
+
FROM billing_ledger l
|
|
38
|
+
LEFT JOIN history h ON h.session_id = l.ref AND h.user_id = l.user_id
|
|
39
|
+
WHERE l.user_id = ? AND l.kind = 'spend' AND l.at >= ?
|
|
40
|
+
GROUP BY COALESCE(h.scope, '')
|
|
41
|
+
ORDER BY spend DESC`)
|
|
42
|
+
.all(userId, since);
|
|
43
|
+
// 日键 = localDay(at):SQL 里按偏移平移再切日期,与 progress 的分桶同一算法。
|
|
44
|
+
const dayMod = `${tz >= 0 ? "+" : "-"}${Math.abs(tz)} minutes`;
|
|
45
|
+
const byDay = db
|
|
46
|
+
.prepare(`SELECT substr(datetime(at, ?), 1, 10) AS day,
|
|
47
|
+
COALESCE(SUM(amount_cents), 0) AS spend,
|
|
48
|
+
COUNT(*) AS charges
|
|
49
|
+
FROM billing_ledger
|
|
50
|
+
WHERE user_id = ? AND kind = 'spend' AND at >= ?
|
|
51
|
+
GROUP BY day
|
|
52
|
+
ORDER BY day`)
|
|
53
|
+
.all(dayMod, userId, since);
|
|
54
|
+
const scopes = byScope.map((r) => ({
|
|
55
|
+
scope: r.scope,
|
|
56
|
+
spendCents: r.spend,
|
|
57
|
+
charges: r.charges,
|
|
58
|
+
}));
|
|
59
|
+
const dayList = byDay.map((r) => ({ day: r.day, spendCents: r.spend, charges: r.charges }));
|
|
60
|
+
return {
|
|
61
|
+
days: window,
|
|
62
|
+
// 回显给前端:SpendTrend 的 x 轴标签要用同一套日键补零。
|
|
63
|
+
tzOffset: tz,
|
|
64
|
+
totalSpendCents: scopes.reduce((sum, s) => sum + s.spendCents, 0),
|
|
65
|
+
byScope: scopes,
|
|
66
|
+
byDay: dayList,
|
|
67
|
+
};
|
|
68
|
+
}
|
package/dist/util.d.ts
ADDED
package/dist/util.js
ADDED
package/package.json
CHANGED
|
@@ -1,58 +1,64 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@njuptlzf/agent-chat-server",
|
|
3
|
-
"version": "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": "./
|
|
8
|
-
"types": "./
|
|
9
|
-
"bin": {
|
|
10
|
-
"agentchat-integrate": "./scripts/agentchat-integrate.mjs"
|
|
11
|
-
},
|
|
12
|
-
"exports": {
|
|
13
|
-
".": {
|
|
14
|
-
"
|
|
15
|
-
"
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
"./
|
|
19
|
-
"
|
|
20
|
-
"default": "./
|
|
21
|
-
},
|
|
22
|
-
"./
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
"
|
|
31
|
-
"
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
"
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
"
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
"
|
|
49
|
-
|
|
50
|
-
"
|
|
51
|
-
"
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
"
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@njuptlzf/agent-chat-server",
|
|
3
|
+
"version": "0.2.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.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.2.0"
|
|
63
|
+
}
|
|
64
|
+
}
|