@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.
- package/dist/branding-ui.d.ts +16 -0
- package/dist/branding-ui.js +119 -0
- 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 +27 -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/persona-config.js +12 -0
- package/dist/server.d.ts +27 -2
- package/dist/server.js +595 -187
- package/dist/session.js +2 -1
- 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/suggestions.d.ts +60 -0
- package/dist/suggestions.js +107 -0
- package/dist/testing.d.ts +5 -4
- package/dist/testing.js +5 -4
- package/package.json +65 -64
- package/scripts/agentchat-integrate.mjs +852 -852
- package/src/branding-ui.ts +126 -0
- 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 +27 -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/persona-config.ts +12 -1
- package/src/server.ts +611 -183
- package/src/session.ts +2 -1
- package/src/sqlite-stores.ts +343 -35
- package/src/stores.ts +0 -41
- package/src/suggestions.ts +146 -0
- package/src/testing.ts +4 -4
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
function emptyUsage() {
|
|
2
|
+
return { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0, costUsd: 0 };
|
|
3
|
+
}
|
|
4
|
+
function addUsage(a, b) {
|
|
5
|
+
return {
|
|
6
|
+
input: a.input + b.input,
|
|
7
|
+
output: a.output + b.output,
|
|
8
|
+
cacheRead: a.cacheRead + b.cacheRead,
|
|
9
|
+
cacheWrite: a.cacheWrite + b.cacheWrite,
|
|
10
|
+
total: a.total + b.total,
|
|
11
|
+
costUsd: a.costUsd + b.costUsd,
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
/** I1 的环检测:三色 DFS。任何引擎都必须做到,不能只靠「库自带」。 */
|
|
15
|
+
function hasCycle(spec) {
|
|
16
|
+
const deps = new Map();
|
|
17
|
+
for (const phase of spec.phases)
|
|
18
|
+
deps.set(phase.id, phase.dependsOn ?? []);
|
|
19
|
+
const WHITE = 0;
|
|
20
|
+
const GRAY = 1;
|
|
21
|
+
const BLACK = 2;
|
|
22
|
+
const color = new Map();
|
|
23
|
+
for (const id of deps.keys())
|
|
24
|
+
color.set(id, WHITE);
|
|
25
|
+
const visit = (id) => {
|
|
26
|
+
color.set(id, GRAY);
|
|
27
|
+
for (const next of deps.get(id) ?? []) {
|
|
28
|
+
const c = color.get(next);
|
|
29
|
+
if (c === GRAY)
|
|
30
|
+
return true;
|
|
31
|
+
if (c === WHITE && visit(next))
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
color.set(id, BLACK);
|
|
35
|
+
return false;
|
|
36
|
+
};
|
|
37
|
+
for (const id of deps.keys()) {
|
|
38
|
+
if (color.get(id) === WHITE && visit(id))
|
|
39
|
+
return true;
|
|
40
|
+
}
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
/** 拓扑层:同层之间无依赖,可并行(fake 串行跑,但顺序仍按层)。 */
|
|
44
|
+
function topoLayers(spec) {
|
|
45
|
+
const remaining = new Map(spec.phases.map((p) => [p.id, p]));
|
|
46
|
+
const done = new Set();
|
|
47
|
+
const layers = [];
|
|
48
|
+
while (remaining.size > 0) {
|
|
49
|
+
const ready = [...remaining.values()].filter((p) => (p.dependsOn ?? []).every((d) => done.has(d)));
|
|
50
|
+
if (ready.length === 0)
|
|
51
|
+
break; // 环,validate 已拒;防御性退出
|
|
52
|
+
layers.push(ready);
|
|
53
|
+
for (const p of ready) {
|
|
54
|
+
remaining.delete(p.id);
|
|
55
|
+
done.add(p.id);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return layers;
|
|
59
|
+
}
|
|
60
|
+
/** 一个 phase 要派发的 job 列表(parallel 展开多份)。 */
|
|
61
|
+
function jobsOf(phase) {
|
|
62
|
+
if (phase.kind === "parallel")
|
|
63
|
+
return [...(phase.branches ?? [])];
|
|
64
|
+
return phase.job ? [phase.job] : [];
|
|
65
|
+
}
|
|
66
|
+
export function createFakeEngine(options) {
|
|
67
|
+
const calls = [];
|
|
68
|
+
const engine = {
|
|
69
|
+
name: "fake",
|
|
70
|
+
calls,
|
|
71
|
+
validate(spec, parentTools) {
|
|
72
|
+
const errors = [];
|
|
73
|
+
const ids = new Set();
|
|
74
|
+
for (const phase of spec.phases) {
|
|
75
|
+
if (ids.has(phase.id))
|
|
76
|
+
errors.push(`duplicate phase id: ${phase.id}`);
|
|
77
|
+
ids.add(phase.id);
|
|
78
|
+
}
|
|
79
|
+
for (const phase of spec.phases) {
|
|
80
|
+
for (const dep of phase.dependsOn ?? []) {
|
|
81
|
+
if (!ids.has(dep))
|
|
82
|
+
errors.push(`phase "${phase.id}": dependsOn unknown phase "${dep}"`);
|
|
83
|
+
}
|
|
84
|
+
// 与 taskflow 实现同一套形状检查——端口契约不为某个实现放宽。
|
|
85
|
+
switch (phase.kind) {
|
|
86
|
+
case "agent":
|
|
87
|
+
if (!phase.job)
|
|
88
|
+
errors.push(`phase "${phase.id}": agent phase requires job`);
|
|
89
|
+
break;
|
|
90
|
+
case "parallel":
|
|
91
|
+
if (!phase.branches || phase.branches.length === 0) {
|
|
92
|
+
errors.push(`phase "${phase.id}": parallel phase requires at least one branch`);
|
|
93
|
+
}
|
|
94
|
+
break;
|
|
95
|
+
case "reduce":
|
|
96
|
+
if (!phase.job)
|
|
97
|
+
errors.push(`phase "${phase.id}": reduce phase requires job (who summarizes)`);
|
|
98
|
+
if (!phase.from || phase.from.length === 0) {
|
|
99
|
+
errors.push(`phase "${phase.id}": reduce phase requires from[] (inputs to aggregate)`);
|
|
100
|
+
}
|
|
101
|
+
break;
|
|
102
|
+
case "gate":
|
|
103
|
+
errors.push(`phase "${phase.id}": gate phase needs a verdict rule, not modeled yet`);
|
|
104
|
+
break;
|
|
105
|
+
}
|
|
106
|
+
for (const job of jobsOf(phase)) {
|
|
107
|
+
if (parentTools === undefined || job.tools === undefined)
|
|
108
|
+
continue;
|
|
109
|
+
const allow = new Set(parentTools);
|
|
110
|
+
for (const tool of job.tools) {
|
|
111
|
+
if (!allow.has(tool)) {
|
|
112
|
+
errors.push(`phase "${phase.id}": tool "${tool}" exceeds parent allowTools`);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
for (const phase of spec.phases) {
|
|
118
|
+
if (phase.kind !== "reduce" || !phase.from)
|
|
119
|
+
continue;
|
|
120
|
+
for (const src of phase.from) {
|
|
121
|
+
if (!ids.has(src))
|
|
122
|
+
errors.push(`phase "${phase.id}": from unknown phase "${src}"`);
|
|
123
|
+
if (src === phase.id)
|
|
124
|
+
errors.push(`phase "${phase.id}": from cannot reference itself`);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
if (spec.resumeFrom) {
|
|
128
|
+
for (const key of Object.keys(spec.resumeFrom)) {
|
|
129
|
+
if (!ids.has(key))
|
|
130
|
+
errors.push(`resumeFrom references unknown phase "${key}"`);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
if (spec.phases.length === 0)
|
|
134
|
+
errors.push("run has no phases");
|
|
135
|
+
if (errors.length === 0 && hasCycle(spec))
|
|
136
|
+
errors.push("dependency cycle detected");
|
|
137
|
+
return { ok: errors.length === 0, errors };
|
|
138
|
+
},
|
|
139
|
+
async execute(spec, deps) {
|
|
140
|
+
const report = engine.validate(spec);
|
|
141
|
+
if (!report.ok) {
|
|
142
|
+
// I1:验证不过 → 零 runJob 调用
|
|
143
|
+
return {
|
|
144
|
+
runId: spec.runId,
|
|
145
|
+
state: "failed",
|
|
146
|
+
output: "",
|
|
147
|
+
usage: emptyUsage(),
|
|
148
|
+
jobs: [],
|
|
149
|
+
error: report.errors.join("; "),
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
// 单 phase 与多 phase 的事件策略与生产实现一致:单 phase 零 emit(I8)。
|
|
153
|
+
const multiPhase = spec.phases.length > 1;
|
|
154
|
+
const emit = multiPhase ? deps.emit : undefined;
|
|
155
|
+
const jobs = [];
|
|
156
|
+
const checkpoints = [];
|
|
157
|
+
const outputs = new Map();
|
|
158
|
+
let usage = emptyUsage();
|
|
159
|
+
let failed;
|
|
160
|
+
const resumed = spec.resumeFrom;
|
|
161
|
+
const now = deps.now ?? Date.now;
|
|
162
|
+
for (const layer of topoLayers(spec)) {
|
|
163
|
+
for (const phase of layer) {
|
|
164
|
+
if (failed)
|
|
165
|
+
break;
|
|
166
|
+
// 断点命中:不调 runJob,直接采信产物(I7)。
|
|
167
|
+
const cp = resumed?.[phase.id];
|
|
168
|
+
if (cp) {
|
|
169
|
+
outputs.set(phase.id, cp.output);
|
|
170
|
+
usage = addUsage(usage, cp.usage);
|
|
171
|
+
checkpoints.push(cp);
|
|
172
|
+
for (const [i, job] of jobsOf(phase).entries()) {
|
|
173
|
+
jobs.push({
|
|
174
|
+
jobId: `${spec.runId}:${phase.id}${jobsOf(phase).length > 1 ? `~${i}` : ""}`,
|
|
175
|
+
phaseId: phase.id,
|
|
176
|
+
state: "completed",
|
|
177
|
+
output: cp.output,
|
|
178
|
+
usage: emptyUsage(),
|
|
179
|
+
stopReason: "resumed",
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
continue;
|
|
183
|
+
}
|
|
184
|
+
const phaseJobs = jobsOf(phase);
|
|
185
|
+
// reduce 的输入:把 from[] 的产物聚合成一段文本喂给汇总者。
|
|
186
|
+
// 聚合格式与生产实现(taskflow)保持一致:单份→原文,多份→带标题分段。
|
|
187
|
+
const reduceInput = phase.kind === "reduce" ? aggregate(phase.from ?? [], outputs) : undefined;
|
|
188
|
+
let phaseUsage = emptyUsage();
|
|
189
|
+
let phaseOutput = "";
|
|
190
|
+
for (const [i, job] of phaseJobs.entries()) {
|
|
191
|
+
const jobId = phaseJobs.length > 1 ? `${spec.runId}:${phase.id}~${i}` : `${spec.runId}:${phase.id}`;
|
|
192
|
+
calls.push(jobId);
|
|
193
|
+
const task = reduceInput === undefined ? job.task : `${job.task}\n\n${reduceInput}`;
|
|
194
|
+
const memberId = job.persona;
|
|
195
|
+
emit?.({ type: "phase_started", phaseId: phase.id, jobId });
|
|
196
|
+
emit?.({ type: "member_started", phaseId: phase.id, memberId, jobId, task });
|
|
197
|
+
const result = options?.runJob
|
|
198
|
+
? await options.runJob(jobId, deps)
|
|
199
|
+
: await deps.runJob({ persona: job.persona, task, ...(job.tools !== undefined ? { tools: job.tools } : {}) }, {
|
|
200
|
+
signal: deps.signal,
|
|
201
|
+
jobId,
|
|
202
|
+
phaseId: phase.id,
|
|
203
|
+
memberId,
|
|
204
|
+
...(multiPhase ? { onDelta: (delta) => deps.emit?.({ type: "member_delta", phaseId: phase.id, memberId, delta }) } : {}),
|
|
205
|
+
});
|
|
206
|
+
const jobUsage = {
|
|
207
|
+
input: result.usage?.input ?? 0,
|
|
208
|
+
output: result.usage?.output ?? 0,
|
|
209
|
+
cacheRead: result.usage?.cacheRead ?? 0,
|
|
210
|
+
cacheWrite: result.usage?.cacheWrite ?? 0,
|
|
211
|
+
total: result.usage?.total ?? 0,
|
|
212
|
+
costUsd: result.usage?.costUsd ?? 0,
|
|
213
|
+
};
|
|
214
|
+
phaseUsage = addUsage(phaseUsage, jobUsage);
|
|
215
|
+
// parallel 的各分支产物拼起来作为 phase 输出(多份时带分隔)。
|
|
216
|
+
phaseOutput = phaseOutput === "" ? result.output : `${phaseOutput}\n\n${result.output}`;
|
|
217
|
+
const state = result.ok ? "completed" : "failed";
|
|
218
|
+
emit?.({
|
|
219
|
+
type: "member_done",
|
|
220
|
+
phaseId: phase.id,
|
|
221
|
+
memberId,
|
|
222
|
+
jobId,
|
|
223
|
+
state,
|
|
224
|
+
usage: {
|
|
225
|
+
input: jobUsage.input,
|
|
226
|
+
output: jobUsage.output,
|
|
227
|
+
cacheRead: jobUsage.cacheRead,
|
|
228
|
+
cacheWrite: jobUsage.cacheWrite,
|
|
229
|
+
total: jobUsage.total,
|
|
230
|
+
costUsd: jobUsage.costUsd,
|
|
231
|
+
},
|
|
232
|
+
});
|
|
233
|
+
emit?.({ type: "phase_settled", phaseId: phase.id, jobId, state });
|
|
234
|
+
const outcome = {
|
|
235
|
+
jobId,
|
|
236
|
+
phaseId: phase.id,
|
|
237
|
+
state: result.ok ? "completed" : "failed",
|
|
238
|
+
output: result.output,
|
|
239
|
+
usage: jobUsage,
|
|
240
|
+
stopReason: result.stopReason ?? (result.ok ? "end_turn" : "error"),
|
|
241
|
+
...(result.attempts !== undefined ? { attempts: result.attempts } : {}),
|
|
242
|
+
...(result.error !== undefined ? { error: result.error } : {}),
|
|
243
|
+
};
|
|
244
|
+
jobs.push(outcome);
|
|
245
|
+
if (!result.ok) {
|
|
246
|
+
failed = outcome;
|
|
247
|
+
break;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
outputs.set(phase.id, phaseOutput);
|
|
251
|
+
usage = addUsage(usage, phaseUsage);
|
|
252
|
+
if (!failed) {
|
|
253
|
+
checkpoints.push({ phaseId: phase.id, output: phaseOutput, usage: phaseUsage, at: now() });
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
if (failed)
|
|
257
|
+
break;
|
|
258
|
+
}
|
|
259
|
+
// 最终输出:取 final 标记的 phase,否则最后一个有产物的。
|
|
260
|
+
const finalPhase = spec.phases.filter((p) => p.final === true).at(-1) ?? spec.phases.at(-1);
|
|
261
|
+
const outcome = {
|
|
262
|
+
runId: spec.runId,
|
|
263
|
+
state: failed ? "failed" : "completed",
|
|
264
|
+
output: finalPhase ? (outputs.get(finalPhase.id) ?? "") : "",
|
|
265
|
+
usage,
|
|
266
|
+
jobs,
|
|
267
|
+
checkpoints,
|
|
268
|
+
...(failed?.error !== undefined ? { error: failed.error } : {}),
|
|
269
|
+
};
|
|
270
|
+
deps.persist?.(outcome);
|
|
271
|
+
return outcome;
|
|
272
|
+
},
|
|
273
|
+
};
|
|
274
|
+
return engine;
|
|
275
|
+
}
|
|
276
|
+
/** reduce 的输入聚合:单份→原文,多份→`### <id>` 分段(与生产实现同格式)。 */
|
|
277
|
+
function aggregate(from, outputs) {
|
|
278
|
+
const parts = from.map((id) => ({ id, text: outputs.get(id) ?? "" }));
|
|
279
|
+
if (parts.length === 1)
|
|
280
|
+
return parts[0].text;
|
|
281
|
+
return parts.map((p, i) => `### [${i + 1}/${parts.length}] ${p.id}\n\n${p.text}`).join("\n\n---\n\n");
|
|
282
|
+
}
|