@njuptlzf/agent-chat-server 0.1.2 → 0.2.1
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 +16 -10
- package/scripts/agentchat-integrate.mjs +852 -607
package/dist/server.d.ts
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { type IncomingMessage, type Server, type ServerResponse } from "node:http";
|
|
2
|
+
import type { ModelRuntime } from "@earendil-works/pi-coding-agent";
|
|
3
|
+
import { type BillingUsageReport, type ErrorCode, type ProductManifest } from "@njuptlzf/agent-chat-protocol";
|
|
4
|
+
import { type AuthzLike } from "./authz.ts";
|
|
5
|
+
import { type LoadedProduct, type ToolContext } from "./manifest.ts";
|
|
6
|
+
import { type PersonaConfigStore } from "./persona-config.ts";
|
|
7
|
+
import { type TrialInput, type TrialResult } from "./persona-trial.ts";
|
|
8
|
+
import { type CooldownRegistry } from "./model-failover.ts";
|
|
9
|
+
import { type ModelConfigStore } from "./model-config.ts";
|
|
10
|
+
import { type McpConfigStore } from "./mcp-config.ts";
|
|
11
|
+
import type { AccountStateStore, AuditEvent, AuditStore, Authenticator, BillingStore, HistoryStore, MembershipStore, MemberPoolStore, QuotaGate, RunLogStore } from "./stores.ts";
|
|
12
|
+
/**
|
|
13
|
+
* 产品业务路由接缝。路由体住在 apps/<product>/server/routes.ts(manifest
|
|
14
|
+
* `server.routes` 声明、main.ts 动态 import),core 只认这两个形状——
|
|
15
|
+
* 命中 = handler 已写响应(sendJson/sendError 都以 end 收尾),落空返回 false。
|
|
16
|
+
*/
|
|
17
|
+
export interface RouteCall {
|
|
18
|
+
deps: ServerDeps;
|
|
19
|
+
req: IncomingMessage;
|
|
20
|
+
res: ServerResponse;
|
|
21
|
+
url: URL;
|
|
22
|
+
}
|
|
23
|
+
export type ProductRoute = (call: RouteCall) => Promise<boolean | void>;
|
|
24
|
+
export interface ProductAdminRoute {
|
|
25
|
+
match(pathname: string): boolean;
|
|
26
|
+
/** adminGate 的闸名(= casbin object),与内建组同一命名空间(如 "activities")。 */
|
|
27
|
+
group: string;
|
|
28
|
+
handle(deps: ServerDeps, req: IncomingMessage, res: ServerResponse, url: URL, acting: string): Promise<void>;
|
|
29
|
+
}
|
|
30
|
+
export interface ServerDeps {
|
|
31
|
+
product: LoadedProduct;
|
|
32
|
+
modelRuntime: ModelRuntime;
|
|
33
|
+
/**
|
|
34
|
+
* Optional 运行时模型配置。omitted ⇒
|
|
35
|
+
* 档位一律按 manifest 的 `agent.model` 解析;主链 main.ts 恒装配。
|
|
36
|
+
*/
|
|
37
|
+
modelConfig?: ModelConfigStore;
|
|
38
|
+
/**
|
|
39
|
+
* Optional 业务接入配置。omitted ⇒ 会话
|
|
40
|
+
* 回落 manifest 的 `mcp:` 种子;主链 main.ts 恒装配。
|
|
41
|
+
*/
|
|
42
|
+
mcpConfig?: McpConfigStore;
|
|
43
|
+
/**
|
|
44
|
+
* Optional 运行时人格配置。omitted ⇒
|
|
45
|
+
* 人格一律按 manifest 编译;主链 main.ts 恒装配。
|
|
46
|
+
*/
|
|
47
|
+
personaConfig?: PersonaConfigStore;
|
|
48
|
+
/** 产品业务路由(用户面);产品 admin 路由(闸仍走 adminGate)。缺省 = 纯 core 部署。 */
|
|
49
|
+
productRoutes?: ProductRoute[];
|
|
50
|
+
productAdminRoutes?: ProductAdminRoute[];
|
|
51
|
+
/**
|
|
52
|
+
* 试跑执行器。omitted ⇒ 真建会话 + 真调模型(`runPersonaTrial`);
|
|
53
|
+
* 测试注入替身,避免为了验证"旁路"而真的花钱。
|
|
54
|
+
*/
|
|
55
|
+
trial?: (input: TrialInput) => Promise<TrialResult>;
|
|
56
|
+
/**
|
|
57
|
+
* Optional 候选冷却表。omitted ⇒ 进程级默认实例(重启清空、不进 DB);
|
|
58
|
+
* 测试注入自己的实例以隔离。
|
|
59
|
+
*/
|
|
60
|
+
modelCooldown?: CooldownRegistry;
|
|
61
|
+
toolFactory: (ctx: ToolContext) => unknown[];
|
|
62
|
+
history: HistoryStore;
|
|
63
|
+
quota: QuotaGate;
|
|
64
|
+
authenticate: Authenticator;
|
|
65
|
+
dataDir: string;
|
|
66
|
+
/** Optional per-run log backing GET /api/usage (-11). */
|
|
67
|
+
runLog?: RunLogStore;
|
|
68
|
+
/** Optional operator authenticator for GET /api/usage/report. */
|
|
69
|
+
adminAuth?: Authenticator;
|
|
70
|
+
/** Optional balance ledger; billing is enforced only when the manifest also declares `billing` (-8). */
|
|
71
|
+
billing?: BillingStore;
|
|
72
|
+
/** Optional thumbs up/down sink backing POST /api/feedback (phase-2 A7). */
|
|
73
|
+
feedback?: unknown;
|
|
74
|
+
/** 打卡活动(序列 5):GET /api/activities + POST /api/activities/:id/join。 */
|
|
75
|
+
activities?: unknown;
|
|
76
|
+
/** Optional graded-answer sink backing POST /api/attempts (业务1 产品页). */
|
|
77
|
+
attempts?: unknown;
|
|
78
|
+
/** Optional draft sink backing GET/POST /api/essays (业务2 论文编辑器). */
|
|
79
|
+
essays?: unknown;
|
|
80
|
+
/**
|
|
81
|
+
* Optional 收藏 sink backing GET/POST /api/marks (学习记录 , V8).
|
|
82
|
+
*/
|
|
83
|
+
bankMarks?: unknown;
|
|
84
|
+
/**
|
|
85
|
+
* Optional 标签标注 sink backing GET/POST/DELETE /api/tags (标签轴). Omitted
|
|
86
|
+
* ⇒ 业务数据只有基线标签(`taxonomy.json` 派生),用户的「我的标签」加不上去。
|
|
87
|
+
*/
|
|
88
|
+
tags?: unknown;
|
|
89
|
+
/**
|
|
90
|
+
* Optional 整卷成绩 sink backing GET /api/papers/runs (刷内容 V6b). When
|
|
91
|
+
* omitted, grading still works and simply keeps no run — a deployment that
|
|
92
|
+
* has not migrated the table must not lose the ability to grade a paper.
|
|
93
|
+
*/
|
|
94
|
+
paperRuns?: unknown;
|
|
95
|
+
/**
|
|
96
|
+
* Optional spend rollup backing GET /api/billing/usage. A function rather than
|
|
97
|
+
* a store because it is a pure read over two tables that only the SQLite
|
|
98
|
+
* layer can join; the memory deployment simply omits it.
|
|
99
|
+
*/
|
|
100
|
+
billingUsage?: (userId: string, days: number, tzOffsetMinutes?: number) => Promise<BillingUsageReport>;
|
|
101
|
+
/**
|
|
102
|
+
* Optional 授权内核(权限与会员方案 Phase A)。omitted ⇒ 全部闸不启用,
|
|
103
|
+
* 业务数据保持 公开口径(内存/dev/测试部署的现状);主链 main.ts 恒装配。
|
|
104
|
+
* ready()=false(casbin 加载失败)时按:业务数据 fail-closed(503),其余闸放行。
|
|
105
|
+
*/
|
|
106
|
+
authz?: AuthzLike;
|
|
107
|
+
/** Optional 会员有效期。whoami 展示用;member 角色由 authz 现算。 */
|
|
108
|
+
membership?: MembershipStore;
|
|
109
|
+
/** Optional 封号状态。omitted ⇒ 封号闸不存在。 */
|
|
110
|
+
accountState?: AccountStateStore;
|
|
111
|
+
/** Optional 会员月度池。omitted ⇒ 池不存在,token 全走余额(现状)。 */
|
|
112
|
+
memberPool?: MemberPoolStore;
|
|
113
|
+
/**
|
|
114
|
+
* Optional 审计哈希链。先落 seam+装配,五写点接线在 ;
|
|
115
|
+
* omitted ⇒ 端点整组 404 隐藏式、写点 `?.` 短路(降级 1)。
|
|
116
|
+
*/
|
|
117
|
+
audit?: AuditStore;
|
|
118
|
+
}
|
|
119
|
+
/** 只记不 await 的访问被拒行(洪水面,慢一秒都是帮攻击者)。meta 键集固定 {path, method}。 */
|
|
120
|
+
export declare function auditDeny(deps: ServerDeps, req: IncomingMessage, reason: "no_auth" | "forbidden" | "account_banned" | "audit_revoked", actor: string | null): void;
|
|
121
|
+
export declare function deny401(deps: ServerDeps, req: IncomingMessage, res: ServerResponse, msg?: string): void;
|
|
122
|
+
/**
|
|
123
|
+
* 管理/资金写动作的事务外纹身上报:run 先执行,成功落 ok 行(append 抛错=500,fail-closed);
|
|
124
|
+
* run 失败也记一行 error(错误行自身写不进才 stderr——链断在事故现场也要留案底)。
|
|
125
|
+
* ponytail: ok 行的 append 若抛错会走全局 500,此时动作已生效但未留痕——按口径这就是事故,
|
|
126
|
+
* 不做补偿写(补偿会重复计 ok/error 双行)。升级路径:同库事务内双写。
|
|
127
|
+
*/
|
|
128
|
+
export declare function auditedWrite<T>(deps: ServerDeps, ev: Omit<AuditEvent, "result">, run: () => Promise<T>): Promise<T>;
|
|
129
|
+
export declare const SESSION_ID: RegExp;
|
|
130
|
+
/** Draft id = the editor's topic key ("arch-style", "custom", …). */
|
|
131
|
+
export declare const DRAFT_ID: RegExp;
|
|
132
|
+
/**
|
|
133
|
+
* Per-field cap for a saved 论文草稿. Two fields of 10k CJK chars stay inside the
|
|
134
|
+
* 64KB JSON body cap; the recommended paper is 3000, so this only stops abuse.
|
|
135
|
+
*/
|
|
136
|
+
export declare const MAX_ESSAY_CHARS = 10000;
|
|
137
|
+
type BillingConfig = NonNullable<ProductManifest["billing"]>;
|
|
138
|
+
/**
|
|
139
|
+
* Cost of one run in cents: token price (rounded up) plus per-call tool
|
|
140
|
+
* surcharges (`prefix*` patterns match). `multiplier` is the tier's price
|
|
141
|
+
* factor, so the number the UI showed is the number charged.
|
|
142
|
+
*/
|
|
143
|
+
export declare function computeSpendCents(usageTotal: number, toolCalls: Record<string, number>, cfg: BillingConfig, multiplier?: number): number;
|
|
144
|
+
/**
|
|
145
|
+
* 处理一条工单——改状态、写回复、发会员天数。
|
|
146
|
+
* 这是 唯一动钱的地方,执行顺序与并发闸见设计稿 (别按直觉改成「先发钱后写状态」):
|
|
147
|
+
* 校验 → 条件 UPDATE 占位(数据库当裁判)→ 真发钱 → 失败回滚占位。
|
|
148
|
+
*/
|
|
149
|
+
/** FeedbackEntry → 用户可见视图:**剥掉 userId**(公开板与「我的工单」都不带身份)。 */
|
|
150
|
+
/**
|
|
151
|
+
* `?tzOffset=`:调用方 UTC 偏移(**东为正**,UTC+8 = 480,±840 校验)。
|
|
152
|
+
* 缺省 0 = UTC 日界;非法返回 undefined(调用方回 400)。「今天」按谁的表算全走它。
|
|
153
|
+
*/
|
|
154
|
+
/**
|
|
155
|
+
* 连续天数:从今天或昨天起连数(今天还没做不算断)。
|
|
156
|
+
* `days` 必须已经按调用方时区分好桶(store 负责),这里只做日历加减。
|
|
157
|
+
*/
|
|
158
|
+
export declare function sendJson(res: ServerResponse, status: number, body: unknown): void;
|
|
159
|
+
export declare function sendError(res: ServerResponse, status: number, code: ErrorCode, message: string): void;
|
|
160
|
+
export declare function readJson(req: IncomingMessage, maxBytes?: number): Promise<unknown>;
|
|
161
|
+
export declare function createChatServer(deps: ServerDeps): Server;
|
|
162
|
+
export {};
|