@njuptlzf/agent-chat-server 0.3.0 → 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.
- package/dist/checkpoint-store.d.ts +78 -0
- package/dist/checkpoint-store.js +52 -0
- package/dist/components.d.ts +15 -0
- package/dist/components.js +50 -0
- package/dist/extension-admission.d.ts +32 -0
- package/dist/extension-admission.js +155 -0
- package/dist/flows.d.ts +49 -0
- package/dist/flows.js +208 -0
- package/dist/ledger.d.ts +90 -0
- package/dist/ledger.js +115 -0
- package/dist/main.js +14 -2
- package/dist/manifest.js +14 -1
- package/dist/orchestrator/engine.d.ts +132 -0
- package/dist/orchestrator/engine.js +1 -0
- package/dist/orchestrator/engines/fake.d.ts +14 -0
- package/dist/orchestrator/engines/fake.js +282 -0
- package/dist/orchestrator/engines/taskflow.d.ts +4 -0
- package/dist/orchestrator/engines/taskflow.js +541 -0
- package/dist/orchestrator/run-spec.d.ts +154 -0
- package/dist/orchestrator/run-spec.js +11 -0
- package/dist/server.d.ts +21 -2
- package/dist/server.js +543 -187
- package/dist/sqlite-stores.d.ts +46 -6
- package/dist/sqlite-stores.js +292 -33
- package/dist/stores.d.ts +0 -36
- package/dist/stores.js +0 -27
- package/dist/testing.d.ts +5 -4
- package/dist/testing.js +5 -4
- package/package.json +7 -6
- package/scripts/agentchat-integrate.mjs +853 -853
- package/src/checkpoint-store.ts +122 -0
- package/src/components.ts +66 -0
- package/src/extension-admission.ts +131 -0
- package/src/flows.ts +244 -0
- package/src/ledger.ts +171 -0
- package/src/main.ts +16 -2
- package/src/manifest.ts +14 -1
- package/src/orchestrator/engine.ts +134 -0
- package/src/orchestrator/engines/fake.ts +330 -0
- package/src/orchestrator/engines/taskflow.ts +626 -0
- package/src/orchestrator/run-spec.ts +171 -0
- package/src/server.ts +553 -183
- package/src/session.ts +689 -689
- package/src/sqlite-stores.ts +343 -35
- package/src/stores.ts +0 -41
- package/src/testing.ts +4 -4
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 假引擎:`Engine` 端口的测试替身。
|
|
3
|
+
*
|
|
4
|
+
* 为什么它必须存在:本仓的接口惯例是「**恰好两个实现**」——
|
|
5
|
+
* `HistoryStore` / `QuotaBill` / `BillingStore`… 全是 Memory 版 + Sqlite 版,
|
|
6
|
+
* 没有一个是「单实现 + 为将来预留」。`Engine` 的第二个实现就是它:
|
|
7
|
+
* 跑 Run 通路测试时注入它,**不建 pi 会话、不调模型**,
|
|
8
|
+
* 与本仓 `ServerDeps.trial` / `suggestions` 的替身模式同源。
|
|
9
|
+
*
|
|
10
|
+
* 它同时让 `engine-contract.test.ts` 从第一天就跑 ≥2 个实现,
|
|
11
|
+
* 而不是等将来真有第二引擎时才发现端口设计有问题。
|
|
12
|
+
*
|
|
13
|
+
* ## 它做什么 / 不做什么
|
|
14
|
+
*
|
|
15
|
+
* **做**:按拓扑层(`dependsOn` 分层)顺序调度、按 `branches` 展开 parallel、
|
|
16
|
+
* 按 `from` 聚合 reduce 的输入、认 `resumeFrom` 跳过已完成 phase、发成员事件。
|
|
17
|
+
* 这些都是 `Engine` 端口契约的一部分,替身必须同样满足,否则契约测试只验了
|
|
18
|
+
* 「fake 自己的语义」而不是「端口的语义」。
|
|
19
|
+
*
|
|
20
|
+
* **不做**:真实的 token 计量、真实并发、真实 abort 传播时序。它跑得是「逻辑」,
|
|
21
|
+
* 不是「性能」。并发相关的不变量(I4)由 `ledger.test.ts` 双实现验。
|
|
22
|
+
*/
|
|
23
|
+
import type { TokenUsage } from "@njuptlzf/agent-chat-protocol";
|
|
24
|
+
import type { Engine, EngineDeps, JobRunResult } from "../engine.ts";
|
|
25
|
+
import type { JobOutcome, JobSpec, PhaseCheckpoint, PhaseSpec, RunOutcome, RunSpec, ValidationReport } from "../run-spec.ts";
|
|
26
|
+
|
|
27
|
+
function emptyUsage(): TokenUsage {
|
|
28
|
+
return { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0, costUsd: 0 };
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function addUsage(a: TokenUsage, b: TokenUsage): TokenUsage {
|
|
32
|
+
return {
|
|
33
|
+
input: a.input + b.input,
|
|
34
|
+
output: a.output + b.output,
|
|
35
|
+
cacheRead: a.cacheRead + b.cacheRead,
|
|
36
|
+
cacheWrite: a.cacheWrite + b.cacheWrite,
|
|
37
|
+
total: a.total + b.total,
|
|
38
|
+
costUsd: a.costUsd + b.costUsd,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** I1 的环检测:三色 DFS。任何引擎都必须做到,不能只靠「库自带」。 */
|
|
43
|
+
function hasCycle(spec: RunSpec): boolean {
|
|
44
|
+
const deps = new Map<string, readonly string[]>();
|
|
45
|
+
for (const phase of spec.phases) deps.set(phase.id, phase.dependsOn ?? []);
|
|
46
|
+
const WHITE = 0;
|
|
47
|
+
const GRAY = 1;
|
|
48
|
+
const BLACK = 2;
|
|
49
|
+
const color = new Map<string, number>();
|
|
50
|
+
for (const id of deps.keys()) color.set(id, WHITE);
|
|
51
|
+
|
|
52
|
+
const visit = (id: string): boolean => {
|
|
53
|
+
color.set(id, GRAY);
|
|
54
|
+
for (const next of deps.get(id) ?? []) {
|
|
55
|
+
const c = color.get(next);
|
|
56
|
+
if (c === GRAY) return true;
|
|
57
|
+
if (c === WHITE && visit(next)) return true;
|
|
58
|
+
}
|
|
59
|
+
color.set(id, BLACK);
|
|
60
|
+
return false;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
for (const id of deps.keys()) {
|
|
64
|
+
if (color.get(id) === WHITE && visit(id)) return true;
|
|
65
|
+
}
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** 拓扑层:同层之间无依赖,可并行(fake 串行跑,但顺序仍按层)。 */
|
|
70
|
+
function topoLayers(spec: RunSpec): PhaseSpec[][] {
|
|
71
|
+
const remaining = new Map(spec.phases.map((p) => [p.id, p]));
|
|
72
|
+
const done = new Set<string>();
|
|
73
|
+
const layers: PhaseSpec[][] = [];
|
|
74
|
+
while (remaining.size > 0) {
|
|
75
|
+
const ready = [...remaining.values()].filter((p) => (p.dependsOn ?? []).every((d) => done.has(d)));
|
|
76
|
+
if (ready.length === 0) break; // 环,validate 已拒;防御性退出
|
|
77
|
+
layers.push(ready);
|
|
78
|
+
for (const p of ready) {
|
|
79
|
+
remaining.delete(p.id);
|
|
80
|
+
done.add(p.id);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return layers;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** 一个 phase 要派发的 job 列表(parallel 展开多份)。 */
|
|
87
|
+
function jobsOf(phase: PhaseSpec): JobSpec[] {
|
|
88
|
+
if (phase.kind === "parallel") return [...(phase.branches ?? [])];
|
|
89
|
+
return phase.job ? [phase.job] : [];
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** 注入的 `runJob` 决定结局;假引擎只负责把它转成 `JobOutcome`。 */
|
|
93
|
+
export interface FakeEngineOptions {
|
|
94
|
+
/**
|
|
95
|
+
* 覆写某个 phase 的 `runJob` 行为。缺省 = 成功、输出 `<phaseId>`、
|
|
96
|
+
* usage `{input:1,output:1,total:2}`。
|
|
97
|
+
*/
|
|
98
|
+
readonly runJob?: (jobId: string, deps: EngineDeps) => Promise<JobRunResult>;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export interface FakeEngine extends Engine {
|
|
102
|
+
/** 调用次序(`<runId>:<phaseId>`),供断言「谁被调过、调几次」。 */
|
|
103
|
+
readonly calls: readonly string[];
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function createFakeEngine(options?: FakeEngineOptions): FakeEngine {
|
|
107
|
+
const calls: string[] = [];
|
|
108
|
+
|
|
109
|
+
const engine: FakeEngine = {
|
|
110
|
+
name: "fake",
|
|
111
|
+
calls,
|
|
112
|
+
|
|
113
|
+
validate(spec: RunSpec, parentTools?: readonly string[]): ValidationReport {
|
|
114
|
+
const errors: string[] = [];
|
|
115
|
+
const ids = new Set<string>();
|
|
116
|
+
for (const phase of spec.phases) {
|
|
117
|
+
if (ids.has(phase.id)) errors.push(`duplicate phase id: ${phase.id}`);
|
|
118
|
+
ids.add(phase.id);
|
|
119
|
+
}
|
|
120
|
+
for (const phase of spec.phases) {
|
|
121
|
+
for (const dep of phase.dependsOn ?? []) {
|
|
122
|
+
if (!ids.has(dep)) errors.push(`phase "${phase.id}": dependsOn unknown phase "${dep}"`);
|
|
123
|
+
}
|
|
124
|
+
// 与 taskflow 实现同一套形状检查——端口契约不为某个实现放宽。
|
|
125
|
+
switch (phase.kind) {
|
|
126
|
+
case "agent":
|
|
127
|
+
if (!phase.job) errors.push(`phase "${phase.id}": agent phase requires job`);
|
|
128
|
+
break;
|
|
129
|
+
case "parallel":
|
|
130
|
+
if (!phase.branches || phase.branches.length === 0) {
|
|
131
|
+
errors.push(`phase "${phase.id}": parallel phase requires at least one branch`);
|
|
132
|
+
}
|
|
133
|
+
break;
|
|
134
|
+
case "reduce":
|
|
135
|
+
if (!phase.job) errors.push(`phase "${phase.id}": reduce phase requires job (who summarizes)`);
|
|
136
|
+
if (!phase.from || phase.from.length === 0) {
|
|
137
|
+
errors.push(`phase "${phase.id}": reduce phase requires from[] (inputs to aggregate)`);
|
|
138
|
+
}
|
|
139
|
+
break;
|
|
140
|
+
case "gate":
|
|
141
|
+
errors.push(`phase "${phase.id}": gate phase needs a verdict rule, not modeled yet`);
|
|
142
|
+
break;
|
|
143
|
+
}
|
|
144
|
+
for (const job of jobsOf(phase)) {
|
|
145
|
+
if (parentTools === undefined || job.tools === undefined) continue;
|
|
146
|
+
const allow = new Set(parentTools);
|
|
147
|
+
for (const tool of job.tools) {
|
|
148
|
+
if (!allow.has(tool)) {
|
|
149
|
+
errors.push(`phase "${phase.id}": tool "${tool}" exceeds parent allowTools`);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
for (const phase of spec.phases) {
|
|
155
|
+
if (phase.kind !== "reduce" || !phase.from) continue;
|
|
156
|
+
for (const src of phase.from) {
|
|
157
|
+
if (!ids.has(src)) errors.push(`phase "${phase.id}": from unknown phase "${src}"`);
|
|
158
|
+
if (src === phase.id) errors.push(`phase "${phase.id}": from cannot reference itself`);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
if (spec.resumeFrom) {
|
|
162
|
+
for (const key of Object.keys(spec.resumeFrom)) {
|
|
163
|
+
if (!ids.has(key)) errors.push(`resumeFrom references unknown phase "${key}"`);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
if (spec.phases.length === 0) errors.push("run has no phases");
|
|
167
|
+
if (errors.length === 0 && hasCycle(spec)) errors.push("dependency cycle detected");
|
|
168
|
+
return { ok: errors.length === 0, errors };
|
|
169
|
+
},
|
|
170
|
+
|
|
171
|
+
async execute(spec: RunSpec, deps: EngineDeps): Promise<RunOutcome> {
|
|
172
|
+
const report = engine.validate(spec);
|
|
173
|
+
if (!report.ok) {
|
|
174
|
+
// I1:验证不过 → 零 runJob 调用
|
|
175
|
+
return {
|
|
176
|
+
runId: spec.runId,
|
|
177
|
+
state: "failed",
|
|
178
|
+
output: "",
|
|
179
|
+
usage: emptyUsage(),
|
|
180
|
+
jobs: [],
|
|
181
|
+
error: report.errors.join("; "),
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// 单 phase 与多 phase 的事件策略与生产实现一致:单 phase 零 emit(I8)。
|
|
186
|
+
const multiPhase = spec.phases.length > 1;
|
|
187
|
+
const emit = multiPhase ? deps.emit : undefined;
|
|
188
|
+
|
|
189
|
+
const jobs: JobOutcome[] = [];
|
|
190
|
+
const checkpoints: PhaseCheckpoint[] = [];
|
|
191
|
+
const outputs = new Map<string, string>();
|
|
192
|
+
let usage = emptyUsage();
|
|
193
|
+
let failed: JobOutcome | undefined;
|
|
194
|
+
const resumed = spec.resumeFrom;
|
|
195
|
+
const now = deps.now ?? Date.now;
|
|
196
|
+
|
|
197
|
+
for (const layer of topoLayers(spec)) {
|
|
198
|
+
for (const phase of layer) {
|
|
199
|
+
if (failed) break;
|
|
200
|
+
|
|
201
|
+
// 断点命中:不调 runJob,直接采信产物(I7)。
|
|
202
|
+
const cp = resumed?.[phase.id];
|
|
203
|
+
if (cp) {
|
|
204
|
+
outputs.set(phase.id, cp.output);
|
|
205
|
+
usage = addUsage(usage, cp.usage);
|
|
206
|
+
checkpoints.push(cp);
|
|
207
|
+
for (const [i, job] of jobsOf(phase).entries()) {
|
|
208
|
+
jobs.push({
|
|
209
|
+
jobId: `${spec.runId}:${phase.id}${jobsOf(phase).length > 1 ? `~${i}` : ""}`,
|
|
210
|
+
phaseId: phase.id,
|
|
211
|
+
state: "completed",
|
|
212
|
+
output: cp.output,
|
|
213
|
+
usage: emptyUsage(),
|
|
214
|
+
stopReason: "resumed",
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
continue;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
const phaseJobs = jobsOf(phase);
|
|
221
|
+
// reduce 的输入:把 from[] 的产物聚合成一段文本喂给汇总者。
|
|
222
|
+
// 聚合格式与生产实现(taskflow)保持一致:单份→原文,多份→带标题分段。
|
|
223
|
+
const reduceInput = phase.kind === "reduce" ? aggregate(phase.from ?? [], outputs) : undefined;
|
|
224
|
+
|
|
225
|
+
let phaseUsage = emptyUsage();
|
|
226
|
+
let phaseOutput = "";
|
|
227
|
+
|
|
228
|
+
for (const [i, job] of phaseJobs.entries()) {
|
|
229
|
+
const jobId = phaseJobs.length > 1 ? `${spec.runId}:${phase.id}~${i}` : `${spec.runId}:${phase.id}`;
|
|
230
|
+
calls.push(jobId);
|
|
231
|
+
const task = reduceInput === undefined ? job.task : `${job.task}\n\n${reduceInput}`;
|
|
232
|
+
const memberId = job.persona;
|
|
233
|
+
|
|
234
|
+
emit?.({ type: "phase_started", phaseId: phase.id, jobId });
|
|
235
|
+
emit?.({ type: "member_started", phaseId: phase.id, memberId, jobId, task });
|
|
236
|
+
|
|
237
|
+
const result = options?.runJob
|
|
238
|
+
? await options.runJob(jobId, deps)
|
|
239
|
+
: await deps.runJob(
|
|
240
|
+
{ persona: job.persona, task, ...(job.tools !== undefined ? { tools: job.tools } : {}) },
|
|
241
|
+
{
|
|
242
|
+
signal: deps.signal,
|
|
243
|
+
jobId,
|
|
244
|
+
phaseId: phase.id,
|
|
245
|
+
memberId,
|
|
246
|
+
...(multiPhase ? { onDelta: (delta: string) => deps.emit?.({ type: "member_delta", phaseId: phase.id, memberId, delta }) } : {}),
|
|
247
|
+
},
|
|
248
|
+
);
|
|
249
|
+
|
|
250
|
+
const jobUsage: TokenUsage = {
|
|
251
|
+
input: result.usage?.input ?? 0,
|
|
252
|
+
output: result.usage?.output ?? 0,
|
|
253
|
+
cacheRead: result.usage?.cacheRead ?? 0,
|
|
254
|
+
cacheWrite: result.usage?.cacheWrite ?? 0,
|
|
255
|
+
total: result.usage?.total ?? 0,
|
|
256
|
+
costUsd: result.usage?.costUsd ?? 0,
|
|
257
|
+
};
|
|
258
|
+
phaseUsage = addUsage(phaseUsage, jobUsage);
|
|
259
|
+
// parallel 的各分支产物拼起来作为 phase 输出(多份时带分隔)。
|
|
260
|
+
phaseOutput = phaseOutput === "" ? result.output : `${phaseOutput}\n\n${result.output}`;
|
|
261
|
+
|
|
262
|
+
const state = result.ok ? "completed" : "failed";
|
|
263
|
+
emit?.({
|
|
264
|
+
type: "member_done",
|
|
265
|
+
phaseId: phase.id,
|
|
266
|
+
memberId,
|
|
267
|
+
jobId,
|
|
268
|
+
state,
|
|
269
|
+
usage: {
|
|
270
|
+
input: jobUsage.input,
|
|
271
|
+
output: jobUsage.output,
|
|
272
|
+
cacheRead: jobUsage.cacheRead,
|
|
273
|
+
cacheWrite: jobUsage.cacheWrite,
|
|
274
|
+
total: jobUsage.total,
|
|
275
|
+
costUsd: jobUsage.costUsd,
|
|
276
|
+
},
|
|
277
|
+
});
|
|
278
|
+
emit?.({ type: "phase_settled", phaseId: phase.id, jobId, state });
|
|
279
|
+
|
|
280
|
+
const outcome: JobOutcome = {
|
|
281
|
+
jobId,
|
|
282
|
+
phaseId: phase.id,
|
|
283
|
+
state: result.ok ? "completed" : "failed",
|
|
284
|
+
output: result.output,
|
|
285
|
+
usage: jobUsage,
|
|
286
|
+
stopReason: result.stopReason ?? (result.ok ? "end_turn" : "error"),
|
|
287
|
+
...(result.attempts !== undefined ? { attempts: result.attempts } : {}),
|
|
288
|
+
...(result.error !== undefined ? { error: result.error } : {}),
|
|
289
|
+
};
|
|
290
|
+
jobs.push(outcome);
|
|
291
|
+
if (!result.ok) {
|
|
292
|
+
failed = outcome;
|
|
293
|
+
break;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
outputs.set(phase.id, phaseOutput);
|
|
298
|
+
usage = addUsage(usage, phaseUsage);
|
|
299
|
+
if (!failed) {
|
|
300
|
+
checkpoints.push({ phaseId: phase.id, output: phaseOutput, usage: phaseUsage, at: now() });
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
if (failed) break;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
// 最终输出:取 final 标记的 phase,否则最后一个有产物的。
|
|
307
|
+
const finalPhase = spec.phases.filter((p) => p.final === true).at(-1) ?? spec.phases.at(-1);
|
|
308
|
+
const outcome: RunOutcome = {
|
|
309
|
+
runId: spec.runId,
|
|
310
|
+
state: failed ? "failed" : "completed",
|
|
311
|
+
output: finalPhase ? (outputs.get(finalPhase.id) ?? "") : "",
|
|
312
|
+
usage,
|
|
313
|
+
jobs,
|
|
314
|
+
checkpoints,
|
|
315
|
+
...(failed?.error !== undefined ? { error: failed.error } : {}),
|
|
316
|
+
};
|
|
317
|
+
deps.persist?.(outcome);
|
|
318
|
+
return outcome;
|
|
319
|
+
},
|
|
320
|
+
};
|
|
321
|
+
|
|
322
|
+
return engine;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/** reduce 的输入聚合:单份→原文,多份→`### <id>` 分段(与生产实现同格式)。 */
|
|
326
|
+
function aggregate(from: readonly string[], outputs: ReadonlyMap<string, string>): string {
|
|
327
|
+
const parts = from.map((id) => ({ id, text: outputs.get(id) ?? "" }));
|
|
328
|
+
if (parts.length === 1) return parts[0]!.text;
|
|
329
|
+
return parts.map((p, i) => `### [${i + 1}/${parts.length}] ${p.id}\n\n${p.text}`).join("\n\n---\n\n");
|
|
330
|
+
}
|