feat(server): W4-A integration endpoints and contract gaps
- POST /acquisition-plans: manual submit via download.plan -> download.submit
(same authorization/audit/idempotency path as the agent), slotId or entityId
- read-only /media-assets, /asset-locations, /asset-unit-coverage, /path-bindings,
/reconcile-runs, /server-mappings, /artifacts, GET /title-aliases/{id};
unimplemented write endpoints removed from api.ts; registry test now checks both directions
- candidate evaluation upsert per (slot, source_release) + migration 0022 dedupe/unique index
- organization records expose downloadTaskId (+ list filter); inventory adds
subscribed/candidateReasons; feed sources expose derived possibleGap
- GET /events?after=&limit= JSON replay page for API key callers (same sse_event table)
- preference tag revision history from audit_event; subscriptions/identities/aliases
lists carry mediaEntityTitle; subscription q/mediaKind search; slot subscriptionId
filter + currentAsset summary; policy preview samples carry title/reasons
- DecisionSummary, IdentityRevisionList, ReleaseIdentityResult, PolicyPreviewSummary exported from contracts
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
ea4c384664
commit
d3ab54f2bd
34 files changed
+1083
-123
No files matched your search
@@ -4,6 +4,7 @@ import {
|
||||
Api,
|
||||
CAPABILITY_CATALOG,
|
||||
type CapabilityDescriptor,
|
||||
type DownloadPlanOutput,
|
||||
type KnownCapabilityId,
|
||||
} from "@umt/contracts";
|
||||
import { type AgentRunRow, sql } from "@umt/db";
|
||||
@@ -341,23 +342,106 @@ export function registerAgentRoutes(app: ModuleApp, ctx: ServerContext) {
|
||||
planId: typeof o.planId === "string" ? o.planId : typeof o.plan?.id === "string" ? o.plan.id : null,
|
||||
};
|
||||
} catch (err) {
|
||||
if (!isCapabilityError(err)) throw err;
|
||||
const code =
|
||||
err.code === "unauthorized"
|
||||
? "forbidden"
|
||||
: err.code === "not_found"
|
||||
? "not_found"
|
||||
: err.code === "conflict"
|
||||
? "conflict"
|
||||
: err.code === "precondition" || err.code === "budget"
|
||||
? "precondition_failed"
|
||||
: err.code === "invalid_input"
|
||||
? "validation_failed"
|
||||
: "internal";
|
||||
throw new ApiException(code, err.message);
|
||||
capabilityHttpError(err);
|
||||
}
|
||||
});
|
||||
|
||||
// ── 手动提交下载:同一条 download.plan → download.submit 能力路径(授权/审计/幂等与 Agent 一致)──
|
||||
route(app, Api.acquisitionPlans.create, async ({ body, actorId, c }) => {
|
||||
const rel = await db()
|
||||
.selectFrom("source_release")
|
||||
.select(["id", "media_entity_id"])
|
||||
.where("id", "=", body.sourceReleaseId)
|
||||
.executeTakeFirst();
|
||||
if (!rel) throw new ApiException("not_found", "来源资源不存在");
|
||||
let slotIds: string[];
|
||||
if (body.slotId) {
|
||||
const s = await db().selectFrom("acquisition_slot").select("id").where("id", "=", body.slotId).executeTakeFirst();
|
||||
if (!s) throw new ApiException("not_found", "收录目标(Slot)不存在");
|
||||
slotIds = [s.id];
|
||||
} else {
|
||||
const entityId = body.entityId ?? rel.media_entity_id;
|
||||
if (!entityId)
|
||||
throw new ApiException("precondition_failed", "来源资源尚未识别到本地作品,请先在“身份与元数据”指定作品");
|
||||
slotIds = (
|
||||
await db()
|
||||
.selectFrom("acquisition_slot")
|
||||
.select("id")
|
||||
.where("media_entity_id", "=", entityId)
|
||||
.orderBy("created_at")
|
||||
.execute()
|
||||
).map((r) => r.id);
|
||||
}
|
||||
const rejected: { slotId: string | null; code: string; message: string }[] = [];
|
||||
if (slotIds.length === 0)
|
||||
rejected.push({ slotId: null, code: "no_slot", message: "该作品没有版本目标(Slot),请先在订阅里建目标" });
|
||||
const invokeCtx = {
|
||||
actorId,
|
||||
taskId: `http:${c.get("requestId") ?? "req"}`,
|
||||
scopeIds: [],
|
||||
signal: c.req.raw.signal,
|
||||
source: "web" as const,
|
||||
};
|
||||
try {
|
||||
for (const slotId of slotIds) {
|
||||
const out = (await ctx.capabilities.invoke(
|
||||
"download.plan",
|
||||
{ slotId, sourceReleaseId: rel.id },
|
||||
invokeCtx,
|
||||
)) as DownloadPlanOutput;
|
||||
if (!out.plan) {
|
||||
for (const r of out.rejected) rejected.push({ slotId, ...r });
|
||||
continue;
|
||||
}
|
||||
const sub = (await ctx.capabilities.invoke("download.submit", { planId: out.plan.id }, invokeCtx)) as {
|
||||
taskId: string;
|
||||
};
|
||||
const plan = await db()
|
||||
.selectFrom("acquisition_plan")
|
||||
.selectAll()
|
||||
.where("id", "=", out.plan.id)
|
||||
.executeTakeFirstOrThrow();
|
||||
await audit(ctx, actorId, "acquisition_plan.manual_submit", plan.id, {
|
||||
sourceReleaseId: rel.id,
|
||||
slotId,
|
||||
taskId: sub.taskId,
|
||||
note: body.note ?? null,
|
||||
});
|
||||
return { submitted: true, plan: camel(plan), slotId, taskId: sub.taskId, rejected };
|
||||
}
|
||||
} catch (err) {
|
||||
capabilityHttpError(err);
|
||||
}
|
||||
return { submitted: false, plan: null, slotId: null, taskId: null, rejected };
|
||||
});
|
||||
|
||||
// ── 产物(整理计划 / 汇报 / 证据):只读 ──
|
||||
route(app, Api.artifacts.list, async ({ query: raw }) => {
|
||||
const query = raw as ListQuery & { agentRunId?: string; taskId?: string };
|
||||
let q = db().selectFrom("artifact").selectAll();
|
||||
if (query.agentRunId) q = q.where("agent_run_id", "=", query.agentRunId);
|
||||
if (query.taskId) q = q.where("task_id", "=", query.taskId);
|
||||
if (query.kind) q = q.where("kind", "=", query.kind as never);
|
||||
const cur = decode(query.cursor);
|
||||
if (cur) q = q.where(sql<boolean>`(created_at, id) < (${cur.at}::timestamptz, ${cur.id}::uuid)`);
|
||||
const rows = await q
|
||||
.orderBy("created_at", "desc")
|
||||
.orderBy("id", "desc")
|
||||
.limit(query.limit + 1)
|
||||
.execute();
|
||||
const items = rows.slice(0, query.limit);
|
||||
const last = items.at(-1);
|
||||
return {
|
||||
items: items.map(camel),
|
||||
nextCursor: rows.length > query.limit && last ? encode(last.created_at, last.id) : null,
|
||||
};
|
||||
});
|
||||
route(app, Api.artifacts.get, async ({ params }) => {
|
||||
const r = await db().selectFrom("artifact").selectAll().where("id", "=", params.id).executeTakeFirst();
|
||||
if (!r) throw new ApiException("not_found", "产物不存在");
|
||||
return camel(r);
|
||||
});
|
||||
|
||||
// ── 业务任务 ──
|
||||
route(app, Api.tasks.list, async ({ query: raw }) => {
|
||||
const query = raw as ListQuery;
|
||||
@@ -413,6 +497,24 @@ export function registerAgentRoutes(app: ModuleApp, ctx: ServerContext) {
|
||||
});
|
||||
}
|
||||
|
||||
/** 能力错误 → HTTP 错误码 */
|
||||
function capabilityHttpError(err: unknown): never {
|
||||
if (!isCapabilityError(err)) throw err;
|
||||
const code =
|
||||
err.code === "unauthorized"
|
||||
? "forbidden"
|
||||
: err.code === "not_found"
|
||||
? "not_found"
|
||||
: err.code === "conflict"
|
||||
? "conflict"
|
||||
: err.code === "precondition" || err.code === "budget"
|
||||
? "precondition_failed"
|
||||
: err.code === "invalid_input"
|
||||
? "validation_failed"
|
||||
: "internal";
|
||||
throw new ApiException(code, err.message);
|
||||
}
|
||||
|
||||
async function audit(ctx: ServerContext, actorId: string, action: string, targetId: string, payload: object) {
|
||||
await ctx.db
|
||||
.insertInto("audit_event")
|
||||
|
||||
Reference in new issue
Block a user