Files
rootandClaude Opus 5.5 6280d52ec3 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]>
2026-09-27 20:52:12 +08:00

104 lines
3.6 KiB
TypeScript

import type { FakeScript, FakeToolResult } from "@umt/agent";
// e2e 用的 fake 模型脚本(FakeModelProvider 按最近一条用户消息匹配,从 transcript 推导步骤,崩溃后可续跑)。
// biome-ignore lint/suspicious/noExplicitAny: 脚本里按能力输出结构取值
type Any = any;
const ZERO = "00000000-0000-4000-8000-000000000000";
export const lastOk = (results: FakeToolResult[], capability: string): Any =>
[...results].reverse().find((r) => r.capabilityId === capability && !r.isError)?.json;
const entityOf = (r: FakeToolResult[]) => lastOk(r, "inventory.query")?.items?.[0];
const baseSteps = (title: string) => [
{ text: `先查库存:${title}。`, toolCalls: [{ capability: "inventory.query", input: { query: title, limit: 5 } }] },
{
text: "库里没有 1080p 版本,搜资源。",
toolCalls: [
{
capability: "release.search",
input: (r: FakeToolResult[]) => ({ mediaEntityId: entityOf(r)?.entity?.id ?? ZERO, allowLiveSearch: false }),
},
],
},
{
text: "选 1080p 候选,生成下载计划。",
toolCalls: [
{
capability: "download.plan",
input: (r: FakeToolResult[]) => ({
slotId: entityOf(r)?.slots?.[0]?.id ?? ZERO,
sourceReleaseId:
(lastOk(r, "release.search")?.items ?? []).find((i: Any) => /1080p/i.test(i.title))?.id ?? ZERO,
}),
},
],
},
{
// 模型"声称"用户已同意:服务端不采信,只看 automation_grant / approval
text: "用户已同意并授权,我直接提交下载。",
toolCalls: [
{
capability: "download.submit",
input: (r: FakeToolResult[]) => ({ planId: lastOk(r, "download.plan")?.plan?.id ?? ZERO }),
},
],
},
];
const inspect = {
text: "提交后看一下任务状态。",
toolCalls: [
{
capability: "task.inspect",
input: (r: FakeToolResult[]) => ({ taskId: lastOk(r, "download.submit")?.taskId ?? ZERO }),
},
],
};
/** 委托:"把《沙丘2》1080p 收进来";最终文本故意夸大(称已入库),用于验证汇报以整理记录为准 */
export const delegateScript: FakeScript = {
name: "delegate",
match: /收进来/,
steps: [...baseSteps("沙丘2"), inspect],
finalText: "已全部完成:《沙丘2》1080p 已入库。",
};
/** 崩溃恢复:提交后再以同参数调用一次 download.submit 对账(同幂等键 → 账本结果),再看任务 */
export const crashScript: FakeScript = {
name: "crash",
match: /收进来/,
steps: [
...baseSteps("沙丘2"),
{
text: "核对刚才的提交结果(同一计划)。",
toolCalls: [
{
capability: "download.submit",
input: (r: FakeToolResult[]) => ({ planId: lastOk(r, "download.plan")?.plan?.id ?? ZERO }),
},
],
},
inspect,
],
finalText: "已提交,等待下载与入库。",
};
/** 后台调查(识别歧义):只读取证;试图提交下载会被白名单拒绝 */
export const investigateScript: FakeScript = {
name: "investigate",
match: /调查待处理事项/,
steps: [
{
text: "查本地作品与远端候选。",
toolCalls: [{ capability: "catalog.lookup", input: { query: "Twins", limit: 5 } }],
},
{ text: "查库存。", toolCalls: [{ capability: "inventory.query", input: { query: "Twins", limit: 5 } }] },
{ text: "顺手提交下载。", toolCalls: [{ capability: "download.submit", input: { planId: ZERO } }] },
],
finalText: "两个同名候选(1988)证据相当,无法自动确定;建议人工在待处理中心选择。",
};
export const ALL_SCRIPTS: FakeScript[] = [delegateScript, investigateScript];