feat(server): W2-D agent assembly, session API, runAgent/resolveDecision and backend e2e
- Capabilities: all 14 registered (AO: inventory.query, download.plan/submit, organize.plan/execute,
policy.preview/apply; agent: task.inspect/retry/cancel); startup asserts count == 14.
Registry injects server-side authorize (automation_grant by action/scope/policy version/budget,
one-shot approval bound to plan id + payload hash + revision + expiry; model claims ignored;
automation switch), audit_event, server idempotency keys and a capability_invocation op ledger
(migration 0021) used for crash reconciliation. download.plan creates draft plans only;
download.submit promotes draft -> reserved after authorization.
- Agent session API: POST/GET /agent/runs, GET /agent/runs/{id} (events by seq, messages, report),
POST .../messages (wake), POST .../abort; automation-grants, approvals, capabilities invoke, tasks.
- runAgent task: Pi or fake provider from settings (apiKey via secrets), events persisted to
agent_run_event then published, resume + ledger reconciliation when pendingOperations > 0,
waits for goal-linked downloads/imports and reports from business tables (submitted != imported).
- Background triggers: decision.created -> bounded plan_only investigation (dedupe per decision);
decision.updated(resolved) -> resolveDecision executes chosen option.
- W2 closure: all OpenAPI routes registered in contracts api.ts (+ subset test); TMDB key from
settings secret_ref with env fallback; PUT /sites/{id}/credentials documented as alias of
/site-accounts; completion events documented as existing status events.
- e2e (real PG + OpenWorkflow + testkit mocks): RSS->library, agent delegation with grant/deny,
background continuation, worker kill -9 recovery (single qB add), auto investigation dedupe.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
0dec0854df
commit
6280d52ec3
46 files changed
+5133
-111
No files matched your search
@@ -0,0 +1,85 @@
|
||||
import type { FakeScript, FakeStep, FakeToolResult } from "@umt/agent";
|
||||
import { z } from "zod";
|
||||
|
||||
// fake 模式脚本(pi.provider = "fake"):测试直接注入 FakeScript;设置里的 agent.fakeScript 是可序列化子集(JSON):
|
||||
// { "match": "收进来" | { "regex": "..." }, "steps": [{ "text"?, "toolCalls": [{ "capability", "input" }] }], "finalText": "..." }
|
||||
// 或它们的数组。input 里的字符串可以写 "{{download.plan.plan.id}}":取该能力最近一次成功结果里的路径值。
|
||||
|
||||
const JsonStep = z.object({
|
||||
thinking: z.string().optional(),
|
||||
text: z.string().optional(),
|
||||
toolCalls: z
|
||||
.array(z.object({ capability: z.string().min(3), input: z.record(z.string(), z.unknown()).default({}) }))
|
||||
.optional(),
|
||||
});
|
||||
const JsonScript = z.object({
|
||||
name: z.string().optional(),
|
||||
match: z.union([z.string(), z.object({ regex: z.string() })]),
|
||||
steps: z.array(JsonStep),
|
||||
finalText: z.string(),
|
||||
});
|
||||
const JsonScripts = z.union([JsonScript, z.array(JsonScript)]);
|
||||
|
||||
const TEMPLATE = /^\{\{\s*([a-z][a-z0-9]*\.[a-z][a-z0-9_]*)((?:\.[A-Za-z0-9_]+)*)\s*\}\}$/;
|
||||
|
||||
function lookup(results: FakeToolResult[], capability: string, path: string[]): unknown {
|
||||
const hit = [...results].reverse().find((r) => r.capabilityId === capability && !r.isError);
|
||||
let cur: unknown = hit?.json;
|
||||
for (const k of path) {
|
||||
if (cur === null || cur === undefined) return undefined;
|
||||
cur = Array.isArray(cur) ? cur[Number(k)] : (cur as Record<string, unknown>)[k];
|
||||
}
|
||||
return cur;
|
||||
}
|
||||
|
||||
function render(v: unknown, results: FakeToolResult[]): unknown {
|
||||
if (typeof v === "string") {
|
||||
const m = TEMPLATE.exec(v);
|
||||
if (!m) return v;
|
||||
return lookup(results, m[1]!, (m[2] ?? "").split(".").filter(Boolean)) ?? null;
|
||||
}
|
||||
if (Array.isArray(v)) return v.map((x) => render(x, results));
|
||||
if (v && typeof v === "object") {
|
||||
return Object.fromEntries(Object.entries(v as Record<string, unknown>).map(([k, x]) => [k, render(x, results)]));
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
/** 解析 settings.agent.fakeScript;不合法时抛错(运行失败并给出原因) */
|
||||
export function parseFakeScripts(json: string): FakeScript[] {
|
||||
const parsed = JsonScripts.parse(JSON.parse(json));
|
||||
const list = Array.isArray(parsed) ? parsed : [parsed];
|
||||
return list.map((s) => ({
|
||||
...(s.name ? { name: s.name } : {}),
|
||||
match: typeof s.match === "string" ? s.match : new RegExp(s.match.regex),
|
||||
steps: s.steps.map(
|
||||
(st): FakeStep => ({
|
||||
...(st.thinking ? { thinking: st.thinking } : {}),
|
||||
...(st.text ? { text: st.text } : {}),
|
||||
...(st.toolCalls
|
||||
? {
|
||||
toolCalls: st.toolCalls.map((c) => ({
|
||||
capability: c.capability,
|
||||
input: (results: FakeToolResult[]) => render(c.input, results) as never,
|
||||
})),
|
||||
}
|
||||
: {}),
|
||||
}),
|
||||
),
|
||||
finalText: s.finalText,
|
||||
}));
|
||||
}
|
||||
|
||||
/** 内置演示脚本:任何目标都先查库存再如实汇报(不做写操作) */
|
||||
export const DEMO_SCRIPTS: FakeScript[] = [
|
||||
{
|
||||
name: "demo",
|
||||
match: () => true,
|
||||
steps: [{ text: "[fake] 先查本地库存。", toolCalls: [{ capability: "inventory.query", input: { limit: 5 } }] }],
|
||||
finalText: (results) => {
|
||||
const r = results.find((x) => x.capabilityId === "inventory.query");
|
||||
const n = (r?.json as { items?: unknown[] } | undefined)?.items?.length ?? 0;
|
||||
return `[fake] 演示模式:库存查询返回 ${n} 个作品。演示模型不会提交下载;需要真实执行请在设置里配置 Pi 上游。`;
|
||||
},
|
||||
},
|
||||
];
|
||||
Reference in new issue
Block a user