@njuptlzf/agent-chat-server 0.7.0 → 0.9.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/history-archive.d.ts +116 -0
- package/dist/history-archive.js +352 -0
- package/dist/main.js +15 -1
- package/dist/server.d.ts +3 -0
- package/dist/server.js +35 -0
- package/dist/session.d.ts +31 -1
- package/dist/session.js +78 -2
- package/dist/sqlite-stores.d.ts +25 -2
- package/dist/sqlite-stores.js +263 -24
- package/package.json +65 -65
- package/scripts/agentchat-integrate.mjs +892 -892
- package/src/history-archive.ts +437 -0
- package/src/main.ts +17 -1
- package/src/server.ts +33 -0
- package/src/session.ts +100 -3
- package/src/sqlite-stores.ts +283 -27
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import type { FileEntry } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
/**
|
|
3
|
+
* 会话历史冷热分层阶段 1–2 的内部接缝(不进公开导出)。
|
|
4
|
+
*
|
|
5
|
+
* 对象存储只放 `history.entries` 正文;SQLite 仍是索引与元数据的唯一真相源。
|
|
6
|
+
* 这里刻意不引入云 SDK:B5 的硬约束是本地零云依赖,且各家 S3 兼容实现
|
|
7
|
+
* 的差异(virtual-host、拒 aws-chunked)用 SDK 反而要额外关开关。Node 内置
|
|
8
|
+
* fetch + crypto 的 SigV4 足够覆盖 put/get 两个动作,依赖面一动不动。
|
|
9
|
+
*/
|
|
10
|
+
/** 正文对象的最小读写面。测试注入内存实现,生产走 S3 兼容 HTTP。 */
|
|
11
|
+
export interface HistoryArchiveStore {
|
|
12
|
+
put(key: string, body: string): Promise<void>;
|
|
13
|
+
get(key: string): Promise<string | null>;
|
|
14
|
+
}
|
|
15
|
+
export interface ArchiveSweepOptions {
|
|
16
|
+
idleDays: number;
|
|
17
|
+
batchSize: number;
|
|
18
|
+
}
|
|
19
|
+
export interface ArchiveSweepReport {
|
|
20
|
+
scanned: number;
|
|
21
|
+
archived: number;
|
|
22
|
+
skipped: number;
|
|
23
|
+
failed: number;
|
|
24
|
+
}
|
|
25
|
+
/** 真正做归档/恢复的 store 能力;ServerDeps 上的可选字段用这个类型收口。 */
|
|
26
|
+
export interface HistoryArchiveCapable {
|
|
27
|
+
archiveIdle(options: ArchiveSweepOptions): Promise<ArchiveSweepReport>;
|
|
28
|
+
restore(userId: string, scope: string, sessionId: string): Promise<FileEntry[] | undefined>;
|
|
29
|
+
}
|
|
30
|
+
export interface HistoryArchiveConfig {
|
|
31
|
+
store: HistoryArchiveStore;
|
|
32
|
+
idleDays: number;
|
|
33
|
+
batchSize: number;
|
|
34
|
+
intervalMs: number;
|
|
35
|
+
/** 阶段 2:热模式开关;关闭时 save/load 与阶段 1 完全一致。 */
|
|
36
|
+
hot: boolean;
|
|
37
|
+
cacheEntries: number;
|
|
38
|
+
cacheBytes: number;
|
|
39
|
+
}
|
|
40
|
+
/** 对象存储读不到/校验不过:只有已归档会话会踩到,本地会话不受影响。 */
|
|
41
|
+
export declare class HistoryArchiveUnavailableError extends Error {
|
|
42
|
+
constructor(message: string);
|
|
43
|
+
}
|
|
44
|
+
/** 内容寻址:key 就是正文字节的 sha256,重跑天然得到同一个 key。 */
|
|
45
|
+
export declare function archiveKeyFor(body: string): string;
|
|
46
|
+
/** 取回时按 key 里的 hash 验正文,损坏对象不会被写回 SQLite。 */
|
|
47
|
+
export declare function verifyArchiveObject(key: string, body: string): boolean;
|
|
48
|
+
/** 缓存按会话索引,避免两条会话正文相同时共享一个缓存项导致失效串台。 */
|
|
49
|
+
export declare function sessionCacheKey(userId: string, scope: string, sessionId: string): string;
|
|
50
|
+
export interface HistoryBodyCacheStats {
|
|
51
|
+
entries: number;
|
|
52
|
+
bytes: number;
|
|
53
|
+
hits: number;
|
|
54
|
+
misses: number;
|
|
55
|
+
evictions: number;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* 进程内 LRU。条数与字节双上限;get 命中把项移到队尾,set 先删旧项再插入。
|
|
59
|
+
* 这不持久化:进程重启 / 缓存全丢后仍从对象存储验 hash 恢复(阶段 2 验收)。
|
|
60
|
+
*/
|
|
61
|
+
export declare class HistoryBodyCache {
|
|
62
|
+
private readonly maxEntries;
|
|
63
|
+
private readonly maxBytes;
|
|
64
|
+
private readonly onEvict?;
|
|
65
|
+
private readonly map;
|
|
66
|
+
private bytes;
|
|
67
|
+
private hits;
|
|
68
|
+
private misses;
|
|
69
|
+
private evictions;
|
|
70
|
+
constructor(maxEntries?: number, maxBytes?: number, onEvict?: ((sessionKey: string, reason: "count" | "bytes" | "replace") => void) | undefined);
|
|
71
|
+
get(sessionKey: string, archiveKey: string): string | undefined;
|
|
72
|
+
set(sessionKey: string, archiveKey: string, body: string): void;
|
|
73
|
+
delete(sessionKey: string): void;
|
|
74
|
+
clear(): void;
|
|
75
|
+
stats(): HistoryBodyCacheStats;
|
|
76
|
+
private remove;
|
|
77
|
+
}
|
|
78
|
+
/** key 布局:path-style 走 endpoint/bucket/key,virtual-host 走 bucket.endpoint/key。 */
|
|
79
|
+
export declare function archiveObjectUrl(endpoint: URL, bucket: string, key: string, pathStyle: boolean): URL;
|
|
80
|
+
/** 测试替身:对象留在进程内,不碰网络。 */
|
|
81
|
+
export declare function createInMemoryHistoryArchiveStore(): HistoryArchiveStore & {
|
|
82
|
+
objects: Map<string, string>;
|
|
83
|
+
};
|
|
84
|
+
/** S3 兼容客户端:只实现 put/get,SigV4 手工签名,无第三方依赖。 */
|
|
85
|
+
export declare class S3HistoryArchiveStore implements HistoryArchiveStore {
|
|
86
|
+
private readonly endpoint;
|
|
87
|
+
private readonly credential;
|
|
88
|
+
constructor(opts: {
|
|
89
|
+
endpoint: URL;
|
|
90
|
+
bucket: string;
|
|
91
|
+
accessKeyId: string;
|
|
92
|
+
secretAccessKey: string;
|
|
93
|
+
region: string;
|
|
94
|
+
pathStyle: boolean;
|
|
95
|
+
});
|
|
96
|
+
private readonly bucket;
|
|
97
|
+
private readonly pathStyle;
|
|
98
|
+
private url;
|
|
99
|
+
private headers;
|
|
100
|
+
private withRetry;
|
|
101
|
+
/** 自检桶:存在即放行,不存在则创建。409(已被本账号占用)也视为就绪。 */
|
|
102
|
+
ensureBucket(): Promise<void>;
|
|
103
|
+
put(key: string, body: string): Promise<void>;
|
|
104
|
+
get(key: string): Promise<string | null>;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* 启动时集中校验。全部未设置 = 归档功能关闭(本地零云依赖的默认形态);
|
|
108
|
+
* 只要出现任一归档变量,必需项缺失就 fail-fast,不静默半开。
|
|
109
|
+
*/
|
|
110
|
+
export declare function loadHistoryArchiveConfig(env?: NodeJS.ProcessEnv): HistoryArchiveConfig | undefined;
|
|
111
|
+
export interface HistoryArchiver {
|
|
112
|
+
sweep(): Promise<ArchiveSweepReport>;
|
|
113
|
+
stop(): void;
|
|
114
|
+
}
|
|
115
|
+
/** 后台定时器:只在配置了归档时创建;unref 避免它成为测试/CLI 的 open handle。 */
|
|
116
|
+
export declare function startHistoryArchiver(archive: HistoryArchiveCapable, config: Pick<HistoryArchiveConfig, "idleDays" | "batchSize" | "intervalMs">): HistoryArchiver;
|
|
@@ -0,0 +1,352 @@
|
|
|
1
|
+
import { createHash, createHmac } from "node:crypto";
|
|
2
|
+
import { isIP } from "node:net";
|
|
3
|
+
/** 对象存储读不到/校验不过:只有已归档会话会踩到,本地会话不受影响。 */
|
|
4
|
+
export class HistoryArchiveUnavailableError extends Error {
|
|
5
|
+
constructor(message) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.name = "HistoryArchiveUnavailableError";
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
const EMPTY_SHA256 = createHash("sha256").update("").digest("hex");
|
|
11
|
+
const REQUEST_TIMEOUT_MS = 10_000;
|
|
12
|
+
const MAX_ATTEMPTS = 3;
|
|
13
|
+
function sha256Hex(data) {
|
|
14
|
+
return createHash("sha256").update(data).digest("hex");
|
|
15
|
+
}
|
|
16
|
+
function errorText(err) {
|
|
17
|
+
return err instanceof Error ? err.message : String(err);
|
|
18
|
+
}
|
|
19
|
+
/** 内容寻址:key 就是正文字节的 sha256,重跑天然得到同一个 key。 */
|
|
20
|
+
export function archiveKeyFor(body) {
|
|
21
|
+
return `archive/${sha256Hex(body)}.json`;
|
|
22
|
+
}
|
|
23
|
+
/** 取回时按 key 里的 hash 验正文,损坏对象不会被写回 SQLite。 */
|
|
24
|
+
export function verifyArchiveObject(key, body) {
|
|
25
|
+
const match = /^archive\/([a-f0-9]{64})\.json$/.exec(key);
|
|
26
|
+
return match !== null && match[1] === sha256Hex(body);
|
|
27
|
+
}
|
|
28
|
+
/** 缓存按会话索引,避免两条会话正文相同时共享一个缓存项导致失效串台。 */
|
|
29
|
+
export function sessionCacheKey(userId, scope, sessionId) {
|
|
30
|
+
return `${userId}\u0000${scope}\u0000${sessionId}`;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* 进程内 LRU。条数与字节双上限;get 命中把项移到队尾,set 先删旧项再插入。
|
|
34
|
+
* 这不持久化:进程重启 / 缓存全丢后仍从对象存储验 hash 恢复(阶段 2 验收)。
|
|
35
|
+
*/
|
|
36
|
+
export class HistoryBodyCache {
|
|
37
|
+
maxEntries;
|
|
38
|
+
maxBytes;
|
|
39
|
+
onEvict;
|
|
40
|
+
map = new Map();
|
|
41
|
+
bytes = 0;
|
|
42
|
+
hits = 0;
|
|
43
|
+
misses = 0;
|
|
44
|
+
evictions = 0;
|
|
45
|
+
constructor(maxEntries = 500, maxBytes = 64 * 1024 * 1024, onEvict) {
|
|
46
|
+
this.maxEntries = maxEntries;
|
|
47
|
+
this.maxBytes = maxBytes;
|
|
48
|
+
this.onEvict = onEvict;
|
|
49
|
+
}
|
|
50
|
+
get(sessionKey, archiveKey) {
|
|
51
|
+
const found = this.map.get(sessionKey);
|
|
52
|
+
if (!found || found.key !== archiveKey) {
|
|
53
|
+
if (found)
|
|
54
|
+
this.remove(sessionKey);
|
|
55
|
+
this.misses++;
|
|
56
|
+
return undefined;
|
|
57
|
+
}
|
|
58
|
+
this.map.delete(sessionKey);
|
|
59
|
+
this.map.set(sessionKey, found);
|
|
60
|
+
this.hits++;
|
|
61
|
+
return found.body;
|
|
62
|
+
}
|
|
63
|
+
set(sessionKey, archiveKey, body) {
|
|
64
|
+
const replaced = this.remove(sessionKey);
|
|
65
|
+
if (replaced)
|
|
66
|
+
this.onEvict?.(sessionKey, "replace");
|
|
67
|
+
const bytes = Buffer.byteLength(body, "utf8");
|
|
68
|
+
this.map.set(sessionKey, { key: archiveKey, body, bytes });
|
|
69
|
+
this.bytes += bytes;
|
|
70
|
+
while (this.map.size > this.maxEntries || this.bytes > this.maxBytes) {
|
|
71
|
+
const oldest = this.map.keys().next();
|
|
72
|
+
if (oldest.done)
|
|
73
|
+
break;
|
|
74
|
+
const reason = this.map.size > this.maxEntries ? "count" : "bytes";
|
|
75
|
+
this.remove(oldest.value);
|
|
76
|
+
this.evictions++;
|
|
77
|
+
this.onEvict?.(oldest.value, reason);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
delete(sessionKey) {
|
|
81
|
+
this.remove(sessionKey);
|
|
82
|
+
}
|
|
83
|
+
clear() {
|
|
84
|
+
this.map.clear();
|
|
85
|
+
this.bytes = 0;
|
|
86
|
+
}
|
|
87
|
+
stats() {
|
|
88
|
+
return { entries: this.map.size, bytes: this.bytes, hits: this.hits, misses: this.misses, evictions: this.evictions };
|
|
89
|
+
}
|
|
90
|
+
remove(sessionKey) {
|
|
91
|
+
const found = this.map.get(sessionKey);
|
|
92
|
+
if (!found)
|
|
93
|
+
return undefined;
|
|
94
|
+
this.map.delete(sessionKey);
|
|
95
|
+
this.bytes -= found.bytes;
|
|
96
|
+
return found;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
/** key 布局:path-style 走 endpoint/bucket/key,virtual-host 走 bucket.endpoint/key。 */
|
|
100
|
+
export function archiveObjectUrl(endpoint, bucket, key, pathStyle) {
|
|
101
|
+
const url = new URL(endpoint.href);
|
|
102
|
+
const encoded = key.split("/").map((part) => encodeURIComponent(part)).join("/");
|
|
103
|
+
const base = url.pathname.replace(/\/+$/, "");
|
|
104
|
+
if (pathStyle) {
|
|
105
|
+
url.pathname = `${base}/${bucket}/${encoded}`;
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
url.hostname = `${bucket}.${url.hostname}`;
|
|
109
|
+
url.pathname = `${base}/${encoded}`;
|
|
110
|
+
}
|
|
111
|
+
return url;
|
|
112
|
+
}
|
|
113
|
+
/** 测试替身:对象留在进程内,不碰网络。 */
|
|
114
|
+
export function createInMemoryHistoryArchiveStore() {
|
|
115
|
+
const objects = new Map();
|
|
116
|
+
return {
|
|
117
|
+
objects,
|
|
118
|
+
async put(key, body) {
|
|
119
|
+
objects.set(key, body);
|
|
120
|
+
},
|
|
121
|
+
async get(key) {
|
|
122
|
+
return objects.get(key) ?? null;
|
|
123
|
+
},
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
/** S3 兼容客户端:只实现 put/get,SigV4 手工签名,无第三方依赖。 */
|
|
127
|
+
export class S3HistoryArchiveStore {
|
|
128
|
+
endpoint;
|
|
129
|
+
credential;
|
|
130
|
+
constructor(opts) {
|
|
131
|
+
this.endpoint = opts.endpoint;
|
|
132
|
+
this.credential = {
|
|
133
|
+
accessKeyId: opts.accessKeyId,
|
|
134
|
+
secretAccessKey: opts.secretAccessKey,
|
|
135
|
+
region: opts.region,
|
|
136
|
+
};
|
|
137
|
+
this.bucket = opts.bucket;
|
|
138
|
+
this.pathStyle = opts.pathStyle;
|
|
139
|
+
}
|
|
140
|
+
bucket;
|
|
141
|
+
pathStyle;
|
|
142
|
+
url(key) {
|
|
143
|
+
return archiveObjectUrl(this.endpoint, this.bucket, key, this.pathStyle);
|
|
144
|
+
}
|
|
145
|
+
headers(method, url, payloadHash) {
|
|
146
|
+
const amzDate = new Date().toISOString().replace(/[:-]|\.\d{3}/g, "");
|
|
147
|
+
const dateStamp = amzDate.slice(0, 8);
|
|
148
|
+
const headers = {
|
|
149
|
+
host: url.host,
|
|
150
|
+
"x-amz-content-sha256": payloadHash,
|
|
151
|
+
"x-amz-date": amzDate,
|
|
152
|
+
};
|
|
153
|
+
const signed = Object.keys(headers).sort();
|
|
154
|
+
const canonicalHeaders = signed.map((name) => `${name}:${headers[name].trim()}\n`).join("");
|
|
155
|
+
const canonicalRequest = [
|
|
156
|
+
method,
|
|
157
|
+
url.pathname,
|
|
158
|
+
url.search.replace(/^\?/, ""),
|
|
159
|
+
canonicalHeaders,
|
|
160
|
+
signed.join(";"),
|
|
161
|
+
payloadHash,
|
|
162
|
+
].join("\n");
|
|
163
|
+
const scope = `${dateStamp}/${this.credential.region}/s3/aws4_request`;
|
|
164
|
+
const stringToSign = ["AWS4-HMAC-SHA256", amzDate, scope, sha256Hex(canonicalRequest)].join("\n");
|
|
165
|
+
const kDate = createHmac("sha256", `AWS4${this.credential.secretAccessKey}`).update(dateStamp).digest();
|
|
166
|
+
const kRegion = createHmac("sha256", kDate).update(this.credential.region).digest();
|
|
167
|
+
const kService = createHmac("sha256", kRegion).update("s3").digest();
|
|
168
|
+
const kSigning = createHmac("sha256", kService).update("aws4_request").digest();
|
|
169
|
+
const signature = createHmac("sha256", kSigning).update(stringToSign).digest("hex");
|
|
170
|
+
headers.authorization =
|
|
171
|
+
`AWS4-HMAC-SHA256 Credential=${this.credential.accessKeyId}/${scope}, ` +
|
|
172
|
+
`SignedHeaders=${signed.join(";")}, Signature=${signature}`;
|
|
173
|
+
return headers;
|
|
174
|
+
}
|
|
175
|
+
async withRetry(label, fn) {
|
|
176
|
+
let last;
|
|
177
|
+
for (let attempt = 1; attempt <= MAX_ATTEMPTS; attempt++) {
|
|
178
|
+
try {
|
|
179
|
+
return await fn();
|
|
180
|
+
}
|
|
181
|
+
catch (err) {
|
|
182
|
+
last = err;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
throw new HistoryArchiveUnavailableError(`${label} failed after ${MAX_ATTEMPTS} attempts: ${errorText(last)}`);
|
|
186
|
+
}
|
|
187
|
+
/** 自检桶:存在即放行,不存在则创建。409(已被本账号占用)也视为就绪。 */
|
|
188
|
+
async ensureBucket() {
|
|
189
|
+
const url = new URL(this.endpoint.href);
|
|
190
|
+
const base = url.pathname.replace(/\/+$/, "");
|
|
191
|
+
// virtual-host 必须把桶名并入 hostname(该布局下 endpoint 根域会拒 path-style 请求),与 archiveObjectUrl 同一口径。
|
|
192
|
+
if (this.pathStyle) {
|
|
193
|
+
url.pathname = `${base}/${this.bucket}`;
|
|
194
|
+
}
|
|
195
|
+
else {
|
|
196
|
+
url.hostname = `${this.bucket}.${url.hostname}`;
|
|
197
|
+
url.pathname = base || "/";
|
|
198
|
+
}
|
|
199
|
+
const probe = await fetch(url, {
|
|
200
|
+
method: "HEAD",
|
|
201
|
+
headers: this.headers("HEAD", url, EMPTY_SHA256),
|
|
202
|
+
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
|
|
203
|
+
});
|
|
204
|
+
if (probe.status === 200)
|
|
205
|
+
return;
|
|
206
|
+
if (probe.status !== 404)
|
|
207
|
+
throw new Error(`bucket check failed: HTTP ${probe.status}`);
|
|
208
|
+
const created = await fetch(url, {
|
|
209
|
+
method: "PUT",
|
|
210
|
+
headers: this.headers("PUT", url, EMPTY_SHA256),
|
|
211
|
+
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
|
|
212
|
+
});
|
|
213
|
+
if (![200, 201, 204, 409].includes(created.status)) {
|
|
214
|
+
throw new Error(`bucket creation failed: HTTP ${created.status}`);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
async put(key, body) {
|
|
218
|
+
const bytes = Buffer.from(body, "utf8");
|
|
219
|
+
await this.withRetry(`archive put ${key}`, async () => {
|
|
220
|
+
const url = this.url(key);
|
|
221
|
+
const res = await fetch(url, {
|
|
222
|
+
method: "PUT",
|
|
223
|
+
headers: this.headers("PUT", url, sha256Hex(bytes)),
|
|
224
|
+
body: bytes,
|
|
225
|
+
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
|
|
226
|
+
});
|
|
227
|
+
if (!res.ok)
|
|
228
|
+
throw new Error(`HTTP ${res.status}: ${(await res.text()).slice(0, 200)}`);
|
|
229
|
+
await res.arrayBuffer();
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
async get(key) {
|
|
233
|
+
return this.withRetry(`archive get ${key}`, async () => {
|
|
234
|
+
const url = this.url(key);
|
|
235
|
+
const res = await fetch(url, {
|
|
236
|
+
method: "GET",
|
|
237
|
+
headers: this.headers("GET", url, EMPTY_SHA256),
|
|
238
|
+
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
|
|
239
|
+
});
|
|
240
|
+
if (res.status === 404)
|
|
241
|
+
return null;
|
|
242
|
+
if (!res.ok)
|
|
243
|
+
throw new Error(`HTTP ${res.status}: ${(await res.text()).slice(0, 200)}`);
|
|
244
|
+
return Buffer.from(await res.arrayBuffer()).toString("utf8");
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
const ARCHIVE_ENV_KEYS = [
|
|
249
|
+
"HISTORY_ARCHIVE_ENDPOINT",
|
|
250
|
+
"HISTORY_ARCHIVE_REGION",
|
|
251
|
+
"HISTORY_ARCHIVE_BUCKET",
|
|
252
|
+
"HISTORY_ARCHIVE_ACCESS_KEY_ID",
|
|
253
|
+
"HISTORY_ARCHIVE_SECRET_ACCESS_KEY",
|
|
254
|
+
"HISTORY_ARCHIVE_PATH_STYLE",
|
|
255
|
+
"HISTORY_ARCHIVE_IDLE_DAYS",
|
|
256
|
+
"HISTORY_ARCHIVE_BATCH_SIZE",
|
|
257
|
+
"HISTORY_ARCHIVE_INTERVAL_MS",
|
|
258
|
+
"HISTORY_ARCHIVE_HOT_ENABLED",
|
|
259
|
+
"HISTORY_ARCHIVE_HOT_CACHE_ENTRIES",
|
|
260
|
+
"HISTORY_ARCHIVE_HOT_CACHE_BYTES",
|
|
261
|
+
];
|
|
262
|
+
function positiveInt(env, name, fallback, min = 1) {
|
|
263
|
+
const raw = env[name];
|
|
264
|
+
if (raw === undefined || raw === "")
|
|
265
|
+
return fallback;
|
|
266
|
+
const value = Number(raw);
|
|
267
|
+
if (!Number.isInteger(value) || value < min) {
|
|
268
|
+
throw new Error(`[history-archive] ${name} must be an integer >= ${min}, got "${raw}"`);
|
|
269
|
+
}
|
|
270
|
+
return value;
|
|
271
|
+
}
|
|
272
|
+
function booleanEnv(env, name, fallback) {
|
|
273
|
+
const raw = env[name];
|
|
274
|
+
if (raw === undefined || raw === "")
|
|
275
|
+
return fallback;
|
|
276
|
+
if (raw === "true")
|
|
277
|
+
return true;
|
|
278
|
+
if (raw === "false")
|
|
279
|
+
return false;
|
|
280
|
+
throw new Error(`[history-archive] ${name} must be "true" or "false", got "${raw}"`);
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* 启动时集中校验。全部未设置 = 归档功能关闭(本地零云依赖的默认形态);
|
|
284
|
+
* 只要出现任一归档变量,必需项缺失就 fail-fast,不静默半开。
|
|
285
|
+
*/
|
|
286
|
+
export function loadHistoryArchiveConfig(env = process.env) {
|
|
287
|
+
const enabled = ARCHIVE_ENV_KEYS.some((key) => env[key] !== undefined && env[key] !== "");
|
|
288
|
+
if (!enabled)
|
|
289
|
+
return undefined;
|
|
290
|
+
const required = [
|
|
291
|
+
"HISTORY_ARCHIVE_ENDPOINT",
|
|
292
|
+
"HISTORY_ARCHIVE_BUCKET",
|
|
293
|
+
"HISTORY_ARCHIVE_ACCESS_KEY_ID",
|
|
294
|
+
"HISTORY_ARCHIVE_SECRET_ACCESS_KEY",
|
|
295
|
+
];
|
|
296
|
+
const missing = required.filter((key) => !env[key]);
|
|
297
|
+
if (missing.length > 0) {
|
|
298
|
+
throw new Error(`[history-archive] enabled but missing: ${missing.join(", ")}`);
|
|
299
|
+
}
|
|
300
|
+
let endpoint;
|
|
301
|
+
try {
|
|
302
|
+
endpoint = new URL(env.HISTORY_ARCHIVE_ENDPOINT);
|
|
303
|
+
}
|
|
304
|
+
catch {
|
|
305
|
+
throw new Error(`[history-archive] HISTORY_ARCHIVE_ENDPOINT must be an absolute URL`);
|
|
306
|
+
}
|
|
307
|
+
if (!["http:", "https:"].includes(endpoint.protocol) || endpoint.username || endpoint.password || endpoint.search || endpoint.hash) {
|
|
308
|
+
throw new Error(`[history-archive] HISTORY_ARCHIVE_ENDPOINT must be a bare http(s) URL without credentials/query/hash`);
|
|
309
|
+
}
|
|
310
|
+
const bucket = env.HISTORY_ARCHIVE_BUCKET;
|
|
311
|
+
if (bucket.includes("/"))
|
|
312
|
+
throw new Error(`[history-archive] HISTORY_ARCHIVE_BUCKET must not contain "/"`);
|
|
313
|
+
const pathStyle = booleanEnv(env, "HISTORY_ARCHIVE_PATH_STYLE", false);
|
|
314
|
+
// URL 的 hostname 不接受 `bucket.127.0.0.1` 这种伪域名;IP 端点必须显式走 path-style。
|
|
315
|
+
if (!pathStyle && isIP(endpoint.hostname) > 0) {
|
|
316
|
+
throw new Error(`[history-archive] virtual-host needs a hostname; set HISTORY_ARCHIVE_PATH_STYLE=true for IP endpoints`);
|
|
317
|
+
}
|
|
318
|
+
return {
|
|
319
|
+
store: new S3HistoryArchiveStore({
|
|
320
|
+
endpoint,
|
|
321
|
+
bucket,
|
|
322
|
+
accessKeyId: env.HISTORY_ARCHIVE_ACCESS_KEY_ID,
|
|
323
|
+
secretAccessKey: env.HISTORY_ARCHIVE_SECRET_ACCESS_KEY,
|
|
324
|
+
region: env.HISTORY_ARCHIVE_REGION || "us-east-1",
|
|
325
|
+
pathStyle,
|
|
326
|
+
}),
|
|
327
|
+
idleDays: positiveInt(env, "HISTORY_ARCHIVE_IDLE_DAYS", 30),
|
|
328
|
+
batchSize: positiveInt(env, "HISTORY_ARCHIVE_BATCH_SIZE", 50),
|
|
329
|
+
intervalMs: positiveInt(env, "HISTORY_ARCHIVE_INTERVAL_MS", 3_600_000, 1_000),
|
|
330
|
+
hot: booleanEnv(env, "HISTORY_ARCHIVE_HOT_ENABLED", false),
|
|
331
|
+
cacheEntries: positiveInt(env, "HISTORY_ARCHIVE_HOT_CACHE_ENTRIES", 500),
|
|
332
|
+
cacheBytes: positiveInt(env, "HISTORY_ARCHIVE_HOT_CACHE_BYTES", 64 * 1024 * 1024),
|
|
333
|
+
};
|
|
334
|
+
}
|
|
335
|
+
/** 后台定时器:只在配置了归档时创建;unref 避免它成为测试/CLI 的 open handle。 */
|
|
336
|
+
export function startHistoryArchiver(archive, config) {
|
|
337
|
+
let stopped = false;
|
|
338
|
+
const sweep = () => archive.archiveIdle({ idleDays: config.idleDays, batchSize: config.batchSize });
|
|
339
|
+
const timer = setInterval(() => {
|
|
340
|
+
if (stopped)
|
|
341
|
+
return;
|
|
342
|
+
void sweep().catch((err) => console.warn("[history-archive] sweep failed:", errorText(err)));
|
|
343
|
+
}, config.intervalMs);
|
|
344
|
+
timer.unref?.();
|
|
345
|
+
return {
|
|
346
|
+
sweep,
|
|
347
|
+
stop() {
|
|
348
|
+
stopped = true;
|
|
349
|
+
clearInterval(timer);
|
|
350
|
+
},
|
|
351
|
+
};
|
|
352
|
+
}
|
package/dist/main.js
CHANGED
|
@@ -12,6 +12,7 @@ import { describeComponents } from "./components.js";
|
|
|
12
12
|
import { createModelRuntime } from "./session.js";
|
|
13
13
|
import { createAdminAuth, createJwtAuth, insecureHeaderAuth } from "./stores.js";
|
|
14
14
|
import { openDatabase, SqliteAccountState, SqliteAudit, SqliteBilling, SqliteCheckpointStore, SqliteHistoryStore, SqliteMembership, SqliteMemberPool, SqliteLedger, SqliteRunLog } from "./sqlite-stores.js";
|
|
15
|
+
import { HistoryBodyCache, loadHistoryArchiveConfig, startHistoryArchiver } from "./history-archive.js";
|
|
15
16
|
import { billingUsage } from "./usage-report.js";
|
|
16
17
|
const productDir = process.env.PRODUCT_DIR;
|
|
17
18
|
if (!productDir) {
|
|
@@ -39,6 +40,16 @@ console.log(`[agent-chat] persona config: ${initialPersonaConfig.source === "fil
|
|
|
39
40
|
const toolFactory = await loadToolFactory(product);
|
|
40
41
|
// Pre-scope rows belong to the qualification this deployment used to serve.
|
|
41
42
|
const db = openDatabase(path.join(dataDir, "agent-chat.db"), product.defaultQual, product.defaultPersona);
|
|
43
|
+
// 阶段 1:归档配置全缺 = 功能关闭;出现任一变量则 load 内部 fail-fast。
|
|
44
|
+
const archiveConfig = loadHistoryArchiveConfig();
|
|
45
|
+
if (archiveConfig) {
|
|
46
|
+
console.log(`[agent-chat] history archive: idle ${archiveConfig.idleDays}d · batch ${archiveConfig.batchSize} · every ${archiveConfig.intervalMs}ms · hot ${archiveConfig.hot ? "on" : "off"}`);
|
|
47
|
+
}
|
|
48
|
+
const historyCache = new HistoryBodyCache(archiveConfig?.cacheEntries, archiveConfig?.cacheBytes);
|
|
49
|
+
const history = new SqliteHistoryStore(db, archiveConfig?.store, {
|
|
50
|
+
hot: archiveConfig?.hot ?? false,
|
|
51
|
+
cache: historyCache,
|
|
52
|
+
});
|
|
42
53
|
// S4:产品 Store 由产品仓自己装配(manifest server.assembly 动态 import,形状不对即启动失败)。
|
|
43
54
|
const productAssembly = await loadProductAssembly(product);
|
|
44
55
|
const jwtSecret = process.env.AUTH_JWT_SECRET;
|
|
@@ -101,7 +112,8 @@ const deps = {
|
|
|
101
112
|
mcpConfig,
|
|
102
113
|
personaConfig,
|
|
103
114
|
toolFactory,
|
|
104
|
-
history
|
|
115
|
+
history,
|
|
116
|
+
historyArchive: archiveConfig ? history : undefined,
|
|
105
117
|
ledger: new SqliteLedger(db, product.manifest.agent.limits?.dailyTokenQuota),
|
|
106
118
|
// 统一成本模型:启动期已校验 models.json 声明,这里装配成本表供 settle 折算 CU。
|
|
107
119
|
// 全局倍率兜底(无逐模型声明时用)来自 manifest,默认 1 CU/千 token。
|
|
@@ -127,6 +139,8 @@ const deps = {
|
|
|
127
139
|
// 坏卷单/坏 taxonomy ⇒ 这里就抛(启动失败),而不是首个请求 500。
|
|
128
140
|
await initProductRoutes(productRoutes, deps);
|
|
129
141
|
const server = createChatServer(deps);
|
|
142
|
+
const historyArchiver = archiveConfig ? startHistoryArchiver(history, archiveConfig) : undefined;
|
|
143
|
+
server.on("close", () => historyArchiver?.stop());
|
|
130
144
|
const port = Number(process.env.PORT ?? 8787);
|
|
131
145
|
server.listen(port, () => {
|
|
132
146
|
console.log(`[agent-chat] ${product.manifest.id} listening on http://localhost:${port}`);
|
package/dist/server.d.ts
CHANGED
|
@@ -14,6 +14,7 @@ import type { AccountStateStore, AuditEvent, AuditStore, Authenticator, BillingS
|
|
|
14
14
|
import type { Ledger } from "./ledger.ts";
|
|
15
15
|
import type { Engine } from "./orchestrator/engine.ts";
|
|
16
16
|
import type { CheckpointStore } from "./checkpoint-store.ts";
|
|
17
|
+
import type { HistoryArchiveCapable } from "./history-archive.ts";
|
|
17
18
|
/**
|
|
18
19
|
* 产品业务路由接缝。路由体住在 apps/<product>/server/routes.ts(manifest
|
|
19
20
|
* `server.routes` 声明、main.ts 动态 import),core 只认这两个形状——
|
|
@@ -70,6 +71,8 @@ export interface ServerDeps {
|
|
|
70
71
|
modelCooldown?: CooldownRegistry;
|
|
71
72
|
toolFactory: (ctx: ToolContext) => unknown[];
|
|
72
73
|
history: HistoryStore;
|
|
74
|
+
/** 对象存储归档能力。omitted ⇒ 恢复端点 503;主链只在配置归档时装配。 */
|
|
75
|
+
historyArchive?: HistoryArchiveCapable;
|
|
73
76
|
/**
|
|
74
77
|
* 预留-结算额度账本(M-14 W3)。替换了旧的 `QuotaGate.check/record`:
|
|
75
78
|
* 先占位再开跑,并发下不会超发(旧模型实测超发 140%)。
|
package/dist/server.js
CHANGED
|
@@ -2240,6 +2240,41 @@ export function createChatServer(deps) {
|
|
|
2240
2240
|
await deps.history.save(auth.userId, scope, body.sessionId, kept);
|
|
2241
2241
|
return sendJson(res, 200, { sessionId: body.sessionId, messages: toHistoryMessages(kept) });
|
|
2242
2242
|
}
|
|
2243
|
+
if (req.method === "POST" && url.pathname === "/api/history/restore") {
|
|
2244
|
+
const auth = await deps.authenticate(req);
|
|
2245
|
+
if (!auth)
|
|
2246
|
+
return deny401(deps, req, res);
|
|
2247
|
+
if (!deps.historyArchive) {
|
|
2248
|
+
return sendError(res, 503, "internal", "history archive is not configured");
|
|
2249
|
+
}
|
|
2250
|
+
let body;
|
|
2251
|
+
try {
|
|
2252
|
+
body = (await readJson(req));
|
|
2253
|
+
}
|
|
2254
|
+
catch {
|
|
2255
|
+
return sendError(res, 400, "bad_request", "body must be valid JSON under 64KB");
|
|
2256
|
+
}
|
|
2257
|
+
if (typeof body.sessionId !== "string" || !SESSION_ID.test(body.sessionId)) {
|
|
2258
|
+
return sendError(res, 400, "bad_request", "expected {sessionId}");
|
|
2259
|
+
}
|
|
2260
|
+
// 与编辑路径同一条并发闸:run 末尾会整块写回历史,恢复不能插在中间。
|
|
2261
|
+
if (running.has(`${auth.userId}\u0000${body.sessionId}`)) {
|
|
2262
|
+
return sendError(res, 409, "bad_request", "this session already has a run in progress");
|
|
2263
|
+
}
|
|
2264
|
+
const scope = await deps.history.scopeOf(auth.userId, body.sessionId);
|
|
2265
|
+
if (scope === undefined)
|
|
2266
|
+
return sendError(res, 404, "not_found", "session not found");
|
|
2267
|
+
try {
|
|
2268
|
+
const entries = await deps.historyArchive.restore(auth.userId, scope, body.sessionId);
|
|
2269
|
+
if (!entries)
|
|
2270
|
+
return sendError(res, 404, "not_found", "session not found");
|
|
2271
|
+
return sendJson(res, 200, { sessionId: body.sessionId, messages: toHistoryMessages(entries) });
|
|
2272
|
+
}
|
|
2273
|
+
catch (err) {
|
|
2274
|
+
// 对象存储不可达/校验失败只让这条恢复请求失败,不影响其它会话的在线读写。
|
|
2275
|
+
return sendError(res, 503, "internal", err instanceof Error ? err.message : "history restore failed");
|
|
2276
|
+
}
|
|
2277
|
+
}
|
|
2243
2278
|
if (req.method === "POST" && url.pathname === "/api/chat")
|
|
2244
2279
|
return await handleChat(req, res);
|
|
2245
2280
|
if (req.method === "POST" && url.pathname === "/api/suggestions") {
|
package/dist/session.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ModelRuntime, type AgentSession, type FileEntry, type McpServerConfig, type Skill } from "@earendil-works/pi-coding-agent";
|
|
2
|
-
import type { McpServerDecl } from "@njuptlzf/agent-chat-protocol";
|
|
2
|
+
import type { McpServerDecl, MemoryCompletion } from "@njuptlzf/agent-chat-protocol";
|
|
3
3
|
import { type McpConfigStore } from "./mcp-config.ts";
|
|
4
4
|
import type { LoadedProduct, ResolvedTier, ToolContext } from "./manifest.ts";
|
|
5
5
|
import { type PersonaConfigStore, type PersonaOverride } from "./persona-config.ts";
|
|
@@ -203,11 +203,41 @@ export declare function compileSystemPrompt(product: LoadedProduct, qual: string
|
|
|
203
203
|
allowTools?: string[];
|
|
204
204
|
disabledTools: string[];
|
|
205
205
|
};
|
|
206
|
+
/**
|
|
207
|
+
* 把宿主的 `modelRuntime` 适配成注入 provider 的 `MemoryCompletion`。
|
|
208
|
+
*
|
|
209
|
+
* 复用当前会话已解析的 model(与聊天同一物理模型),但**不进用户会话**:
|
|
210
|
+
* 一次 `completeSimple` 调用,独立历史,失败以 `ok:false` 结构化返回,
|
|
211
|
+
* 不参与聊天主链的 failover,也不向 provider 抛异常。超时双保险:signal
|
|
212
|
+
* 传给 provider 请求,Promise.race 兜底不遵守 signal 的底层实现。
|
|
213
|
+
*/
|
|
214
|
+
export declare function createMemoryCompletion(modelRuntime: ModelRuntime, model: NonNullable<ReturnType<ModelRuntime["getModel"]>>): MemoryCompletion;
|
|
206
215
|
/**
|
|
207
216
|
* 产品依赖跨会话画像,但选中的 provider 没声明该能力时给宿主的可读告警(C6)。
|
|
208
217
|
* 导出为纯函数,便于契约测试钉住文案;返回 null 表示无需告警。
|
|
209
218
|
*/
|
|
210
219
|
export declare function sharedProfileMismatchWarning(requiresSharedProfile: boolean, providerSupports: boolean): string | null;
|
|
220
|
+
/**
|
|
221
|
+
* The single source of truth for memory root paths. Pure: no fs, no env, no cwd.
|
|
222
|
+
*
|
|
223
|
+
* Two disjoint roots per tenant:
|
|
224
|
+
* - `storageRoot` — one per (user, scope), dies with the qualification/session;
|
|
225
|
+
* - `sharedRoot` — one per user, dies only on account deletion.
|
|
226
|
+
* Siblings, never nested: deleting a qualification must not delete user identity.
|
|
227
|
+
* The scope key falls back through memoryScopeKey → qual → "default"; callers
|
|
228
|
+
* must not re-implement that order (job-scoped sessions pass an explicit key).
|
|
229
|
+
* `qual` is the second fallback so callers pass both and own no precedence logic.
|
|
230
|
+
*
|
|
231
|
+
* Path layers, so nobody has to count the two directory names:
|
|
232
|
+
* - `tenants/` is the HOST layer — one root per (user, scope), plus one per user
|
|
233
|
+
* for the shared profile. The host allocates and erases it.
|
|
234
|
+
* - `memory/` (under a tenant root) is the PROVIDER's internal layout, which
|
|
235
|
+
* this host neither knows nor asserts.
|
|
236
|
+
*/
|
|
237
|
+
export declare function resolveMemoryRoots(dataDir: string, userId: string, memoryScopeKey: string | undefined, qual: string | undefined): {
|
|
238
|
+
storageRoot: string;
|
|
239
|
+
sharedRoot: string;
|
|
240
|
+
};
|
|
211
241
|
/**
|
|
212
242
|
* Build a pi session locked down for multi-user serving:
|
|
213
243
|
* - no built-in tools (no bash / read / write / edit), verified by test
|