feat(episode-mapping): 映射规则接口与模拟预览、待处理确认 / 忽略、作品页集数映射分区与订阅摘要

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Cxwgnn2pbzQ7rXv3rq7fei
This commit is contained in:
rootandClaude Opus 5.5 committed 2026-10-03 19:28:09 +08:00
1 parent fe597187dd
commit 45a114263b
26 files changed
+2770 -9

No files matched your search

@@ -3,6 +3,7 @@ import type { ServerContext, ServerModule } from "../types.ts";
import { registerSourcesCapabilities } from "./capabilities.ts";
import { decisionRoutes } from "./routes/decisions.ts";
import { enrichRoutes } from "./routes/enrich.ts";
import { episodeMappingRoutes } from "./routes/episode-mapping.ts";
import { identityRoutes } from "./routes/identity.ts";
import { sourcesRoutes } from "./routes/sources.ts";
import { workbenchRoutes } from "./routes/workbench.ts";
@@ -40,6 +41,7 @@ export function createSourcesIdentityModule(overrides: RuntimeOverrides = {}): S
identityRoutes(app, rt);
enrichRoutes(app, rt);
decisionRoutes(app, rt);
episodeMappingRoutes(app, rt);
},
registerTasks(registry, ctx) {
registerSourcesTasks(registry, () => runtime(ctx));
@@ -10,6 +10,8 @@ import { route } from "../http.ts";
import { decodeCursor, jsonb, page, toApi, toApiList } from "../rows.ts";
import type { SourcesRuntime } from "../runtime.ts";
import { audit } from "../service/common.ts";
import { resolveEpisodeDecision } from "../service/episode-mapping-admin.ts";
import { EPISODE_DECISION_CONFIRM, EPISODE_DECISION_IGNORE_RULE } from "../service/episode-resolution.ts";
import { assignReleaseIdentity, dismissPoolEntry } from "../service/identity-admin.ts";
import { groupMemberIds } from "../service/identity-decisions.ts";
@@ -161,6 +163,14 @@ export function decisionRoutes(app: ModuleApp, rt: () => SourcesRuntime) {
const opt = body.optionKey ? options.find((o) => o.key === body.optionKey) : undefined;
if (body.optionKey && !opt) throw new ApiException("bad_request", `未知选项 ${body.optionKey}`);
// 集数待确认:确认建议规则 / 忽略并建忽略规则 = 建规则 + 关闭事项 + 按作品重算(同一事务);单纯忽略走下面的通用关闭
if (
d.kind === "episode_mapping" &&
(opt?.key === EPISODE_DECISION_CONFIRM || opt?.key === EPISODE_DECISION_IGNORE_RULE)
) {
return toApi(await resolveEpisodeDecision(rt().ctx, d, opt.key, body.note ?? null, actor));
}
// 身份类:直接执行(选候选 / 手动指定 TMDB id / 选本地作品 = 人工指定并加锁;忽略 = 待识别池 dismissed)
const identityKind = d.kind === "identity_ambiguous" || d.kind === "identity_unresolved";
if (identityKind && d.subject_type === "source_release" && d.subject_id) {
@@ -0,0 +1,26 @@
import { Api } from "@umt/contracts";
import type { ModuleApp } from "../../types.ts";
import { route } from "../http.ts";
import type { SourcesRuntime } from "../runtime.ts";
import {
createRule,
deleteRule,
listRules,
previewEpisodeMapping,
updateRule,
} from "../service/episode-mapping-admin.ts";
// 集数映射(docs/plan/episode-resolution.md 批次 4):规则增删改(写入后按作品重算)、模拟预览(纯读)。
const T = ["episode-mapping"];
const E = Api.episodeMapping;
export function episodeMappingRoutes(app: ModuleApp, rt: () => SourcesRuntime) {
const ctx = () => rt().ctx;
route(app, { ...E.listRules, tags: T }, async ({ params }) => ({ items: await listRules(ctx().db, params.id) }));
route(app, { ...E.createRule, tags: T }, ({ params, body, actor }) => createRule(ctx(), params.id, body, actor));
route(app, { ...E.updateRule, tags: T }, ({ params, body, actor }) => updateRule(ctx(), params.id, body, actor));
route(app, { ...E.deleteRule, tags: T }, ({ params, actor }) => deleteRule(ctx(), params.id, actor));
route(app, { ...E.preview, tags: T }, ({ params, body }) => previewEpisodeMapping(ctx().db, params.id, body));
}
@@ -0,0 +1,521 @@
import type {
EpisodeMappingPreview,
EpisodeMappingPreviewGroup,
EpisodeMappingPreviewInput,
EpisodeMappingPreviewItem,
EpisodeMappingRuleCreate,
EpisodeMappingRuleRow,
EpisodeMappingRuleSpec,
EpisodeMappingRuleUpdate,
EpisodeMappingRuleWriteResult,
EpisodeResolutionMethod,
EpisodeSuggestion,
EpisodeValidationState,
} from "@umt/contracts";
import type { DecisionRow, EpisodeMappingRuleRow as RuleDbRow } from "@umt/db";
import {
type EpisodeMappingRule,
type EpisodeResolution,
evalEpisodeOffset,
normalizeOffsetExpression,
parseRelease,
resolveEpisodes,
type SeasonTable,
type SuggestSample,
suggestRule,
} from "@umt/domain";
import { ApiException } from "../../../http/errors.ts";
import type { ServerContext } from "../../types.ts";
import { jsonb, toApi } from "../rows.ts";
import { type Actor, audit, type Exec } from "./common.ts";
import {
EPISODE_DECISION_CONFIRM,
EPISODE_DECISION_IGNORE_RULE,
type EpisodeDecisionEvidence,
loadRules,
loadSeasonTable,
publishedAtOf,
reresolveEntityEpisodes,
sameResolution,
} from "./episode-resolution.ts";
// 集数映射的接口层(docs/plan/episode-resolution.md 批次 4):规则写入校验、写入后按作品重算、模拟预览、
// 待处理 episode_mapping 的「确认建议规则 / 忽略并建忽略规则」。判定语义全在 domain,这里只装配与校验。
const EPISODE_KINDS = new Set(["series", "variety"]);
const PREVIEW_LIMIT = 100;
/** 有季 / 组过滤时多取一些再在内存里筛(季与组要按原标题重解析才知道) */
const PREVIEW_SCAN = 1000;
interface NormalizedRule {
season: number | null;
release_group: string | null;
kind: EpisodeMappingRuleSpec["kind"];
expression: string | null;
target_season: number | null;
table_entries: NonNullable<EpisodeMappingRuleSpec["tableEntries"]> | null;
special: NonNullable<EpisodeMappingRuleSpec["special"]> | null;
}
const bad = (msg: string) => new ApiException("bad_request", msg);
/** 校验并规范化规则内容:按 kind 只保留对应字段;表达式要能解析,表格区间要合法且不重叠 */
export function normalizeRuleSpec(spec: EpisodeMappingRuleSpec): NormalizedRule {
const group = spec.releaseGroup?.trim() || null;
const out: NormalizedRule = {
season: spec.season ?? null,
release_group: group,
kind: spec.kind,
expression: null,
target_season: null,
table_entries: null,
special: null,
};
if (spec.kind === "expression") {
const raw = spec.expression?.trim();
if (!raw) throw bad("偏移表达式不能为空");
let expr: string;
try {
expr = normalizeOffsetExpression(raw);
evalEpisodeOffset(expr, 1);
} catch (e) {
throw bad(`偏移表达式无法解析:${(e as Error).message}`);
}
out.expression = expr;
out.target_season = spec.targetSeason ?? null;
} else if (spec.kind === "table") {
const entries = spec.tableEntries ?? [];
if (!entries.length) throw bad("表格规则至少要有一行");
entries.forEach((e, i) => {
const row = `第 ${i + 1} 行`;
if (e.from.start < 0 || e.from.end < e.from.start) throw bad(`表格${row}:来源集号区间不合法`);
if (e.to.season < 0 || e.to.episode < 1) throw bad(`表格${row}:目标季集不合法`);
});
const sorted = [...entries].sort((a, b) => a.from.start - b.from.start);
for (let i = 1; i < sorted.length; i++) {
if (sorted[i]!.from.start <= sorted[i - 1]!.from.end)
throw bad(`表格里的来源区间 ${sorted[i - 1]!.from.start}-${sorted[i - 1]!.from.end} 与后一行重叠`);
}
out.table_entries = entries;
} else {
if (!spec.special) throw bad("要选映射成特别篇、花絮还是忽略");
out.special = spec.special;
}
return out;
}
const scopeText = (season: number | null, group: string | null) =>
`${season === null ? "任意季" : `第 ${season} 季`} · ${group ?? "任意发布组"}`;
/** 同作品 + 季 + 发布组(忽略大小写)已有启用的规则 → conflict */
async function assertScopeFree(
exec: Exec,
entityId: string,
season: number | null,
group: string | null,
exceptId: string | null,
): Promise<void> {
let q = exec
.selectFrom("episode_mapping_rule")
.select(["id", "release_group"])
.where("media_entity_id", "=", entityId)
.where("status", "=", "active");
q = season === null ? q.where("season", "is", null) : q.where("season", "=", season);
if (exceptId) q = q.where("id", "<>", exceptId);
const rows = await q.execute();
const hit = rows.find((r) => (r.release_group?.trim().toLowerCase() || null) === (group?.toLowerCase() ?? null));
if (hit) throw new ApiException("conflict", `「${scopeText(season, group)}」已有一条启用的规则,先停用或修改那一条`);
}
const isUniqueViolation = (e: unknown) => (e as { code?: string }).code === "23505";
async function loadEpisodicEntity(exec: Exec, entityId: string) {
const e = await exec
.selectFrom("media_entity")
.select(["id", "kind", "status", "merged_into_id"])
.where("id", "=", entityId)
.executeTakeFirst();
if (!e) throw new ApiException("not_found", "作品不存在");
return e;
}
export async function listRules(exec: Exec, entityId: string): Promise<EpisodeMappingRuleRow[]> {
await loadEpisodicEntity(exec, entityId);
const rows = await exec
.selectFrom("episode_mapping_rule")
.selectAll()
.where("media_entity_id", "=", entityId)
.orderBy("status")
.orderBy("created_at", "desc")
.execute();
return rows.map((r) => toApi<EpisodeMappingRuleRow>(r));
}
/** 写入一条规则(不重算,调用方在同一事务里接着重算) */
async function insertRule(
exec: Exec,
entityId: string,
n: NormalizedRule,
actor: Actor,
evidence: Record<string, unknown>,
): Promise<RuleDbRow> {
await assertScopeFree(exec, entityId, n.season, n.release_group, null);
const row = await exec
.insertInto("episode_mapping_rule")
.values({
media_entity_id: entityId,
...n,
table_entries: n.table_entries ? jsonb(n.table_entries) : null,
evidence: jsonb(evidence),
evidence_level: "manual",
created_by_type: actor.type,
created_by_id: actor.id,
})
.returningAll()
.executeTakeFirstOrThrow();
await audit(
exec,
actor,
"episode_mapping_rule.created",
{ type: "episode_mapping_rule", id: row.id },
{
mediaEntityId: entityId,
...n,
},
);
return row;
}
async function withConflict<T>(fn: () => Promise<T>): Promise<T> {
try {
return await fn();
} catch (e) {
if (isUniqueViolation(e)) throw new ApiException("conflict", "同一季和发布组已有一条启用的规则");
throw e;
}
}
export async function createRule(
ctx: ServerContext,
entityId: string,
body: EpisodeMappingRuleCreate,
actor: Actor,
): Promise<EpisodeMappingRuleWriteResult> {
const entity = await loadEpisodicEntity(ctx.db, entityId);
if (!EPISODE_KINDS.has(entity.kind)) throw bad("只有剧集和综艺能设置集数映射");
const n = normalizeRuleSpec(body);
return withConflict(() =>
ctx.db.transaction().execute(async (trx) => {
const row = await insertRule(trx, entityId, n, actor, body.evidence ?? {});
const reresolve = await reresolveEntityEpisodes(ctx, trx, entityId);
return { rule: toApi<EpisodeMappingRuleRow>(await reloadRule(trx, row.id)), reresolve };
}),
);
}
const reloadRule = (exec: Exec, id: string) =>
exec.selectFrom("episode_mapping_rule").selectAll().where("id", "=", id).executeTakeFirstOrThrow();
function specOfRow(r: RuleDbRow): EpisodeMappingRuleSpec {
return {
season: r.season,
releaseGroup: r.release_group,
kind: r.kind,
expression: r.expression,
targetSeason: r.target_season,
tableEntries: (r.table_entries as EpisodeMappingRuleSpec["tableEntries"]) ?? null,
special: r.special,
};
}
export async function updateRule(
ctx: ServerContext,
id: string,
body: EpisodeMappingRuleUpdate,
actor: Actor,
): Promise<EpisodeMappingRuleWriteResult> {
const cur = await ctx.db.selectFrom("episode_mapping_rule").selectAll().where("id", "=", id).executeTakeFirst();
if (!cur) throw new ApiException("not_found", "映射规则不存在");
const { status, ...patch } = body;
const merged: EpisodeMappingRuleSpec = { ...specOfRow(cur) };
for (const [k, v] of Object.entries(patch)) if (v !== undefined) (merged as Record<string, unknown>)[k] = v;
const n = normalizeRuleSpec(merged);
const nextStatus = status ?? cur.status;
return withConflict(() =>
ctx.db.transaction().execute(async (trx) => {
if (nextStatus === "active") await assertScopeFree(trx, cur.media_entity_id, n.season, n.release_group, id);
const disabledReason =
nextStatus === "active"
? null
: status === "disabled" && cur.status === "active"
? JSON.stringify({ manual: true, by: `${actor.type}:${actor.id ?? ""}`, at: new Date().toISOString() })
: cur.disabled_reason;
await trx
.updateTable("episode_mapping_rule")
.set({
...n,
table_entries: n.table_entries ? jsonb(n.table_entries) : null,
status: nextStatus,
disabled_reason: disabledReason,
updated_at: new Date(),
})
.where("id", "=", id)
.execute();
await audit(
trx,
actor,
"episode_mapping_rule.updated",
{ type: "episode_mapping_rule", id },
{
mediaEntityId: cur.media_entity_id,
status: nextStatus,
...n,
},
);
const reresolve = await reresolveEntityEpisodes(ctx, trx, cur.media_entity_id);
return { rule: toApi<EpisodeMappingRuleRow>(await reloadRule(trx, id)), reresolve };
}),
);
}
export async function deleteRule(ctx: ServerContext, id: string, actor: Actor): Promise<EpisodeMappingRuleWriteResult> {
return ctx.db.transaction().execute(async (trx) => {
const cur = await trx.selectFrom("episode_mapping_rule").selectAll().where("id", "=", id).executeTakeFirst();
if (!cur) throw new ApiException("not_found", "映射规则不存在");
await trx.deleteFrom("episode_mapping_rule").where("id", "=", id).execute();
await audit(
trx,
actor,
"episode_mapping_rule.deleted",
{ type: "episode_mapping_rule", id },
{
mediaEntityId: cur.media_entity_id,
...specOfRow(cur),
},
);
const reresolve = await reresolveEntityEpisodes(ctx, trx, cur.media_entity_id);
return { rule: toApi<EpisodeMappingRuleRow>(cur), reresolve };
});
}
// ── 模拟预览 ──
const DRAFT_ID = "draft";
function domainRule(n: NormalizedRule, id: string): EpisodeMappingRule {
return {
id,
season: n.season,
releaseGroup: n.release_group,
kind: n.kind,
...(n.expression !== null ? { expression: n.expression } : {}),
...(n.target_season !== null ? { targetSeason: n.target_season } : {}),
...(n.table_entries ? { table: n.table_entries } : {}),
...(n.special !== null ? { special: n.special } : {}),
};
}
const sameScope = (a: EpisodeMappingRule, b: EpisodeMappingRule) =>
a.season === b.season &&
(a.releaseGroup?.trim().toLowerCase() ?? null) === (b.releaseGroup?.trim().toLowerCase() ?? null);
export function validationOf(r: EpisodeResolution): EpisodeValidationState {
if (r.special) return "special";
if (r.method === "skipped") return "skipped";
if (r.method === "unresolved") return "fail";
if (r.method === "unverified") return "unverified";
return "pass";
}
const sourceSeason = (r: EpisodeResolution) => r.source.season ?? (r.source.episodes ? 1 : null);
function seasonSummary(table: SeasonTable): EpisodeMappingPreview["seasons"] {
return {
seasons: table.seasons.map((s) => ({
season: s.season,
total: s.total,
aired: (s.episodes ?? []).filter((e) => e.airDate).length,
})),
latestSeason: table.latestSeason,
ended: table.ended,
};
}
/** 同组待确认样本推导建议规则:取样本最多的来源季,按发布时间对播出日期 */
function groupSuggestion(
items: { preview: EpisodeResolution; publishedAt: string }[],
table: SeasonTable,
releaseGroup: string | null,
): EpisodeSuggestion | null {
const pending = items.filter((i) => i.preview.method === "unresolved" && i.preview.source.episodes);
if (!pending.length) return null;
const bySeason = new Map<number | null, typeof pending>();
for (const p of pending) {
const s = sourceSeason(p.preview);
bySeason.set(s, [...(bySeason.get(s) ?? []), p]);
}
const [season, list] = [...bySeason.entries()].sort((a, b) => b[1].length - a[1].length)[0]!;
const samples: SuggestSample[] = list
.map((p) => ({ episodes: p.preview.source.episodes!, publishedAt: p.publishedAt }))
.sort((a, b) => a.publishedAt.localeCompare(b.publishedAt));
const s = suggestRule(samples, table);
return s ? { season, releaseGroup, ...s } : null;
}
export async function previewEpisodeMapping(
exec: Exec,
entityId: string,
input: EpisodeMappingPreviewInput,
): Promise<EpisodeMappingPreview> {
const entity = await loadEpisodicEntity(exec, entityId);
const table = await loadSeasonTable(exec, entityId);
const seasons = seasonSummary(table);
if (!EPISODE_KINDS.has(entity.kind)) return { applicable: false, seasons, groups: [], items: [], changed: 0 };
let rules = await loadRules(exec, entityId);
if (input.replaceRuleId) rules = rules.filter((r) => r.id !== input.replaceRuleId);
if (input.draft) {
const draft = domainRule(normalizeRuleSpec(input.draft), DRAFT_ID);
rules = [...rules.filter((r) => !sameScope(r, draft)), draft];
}
const limit = input.limit ?? PREVIEW_LIMIT;
const filtered = input.season != null || !!input.releaseGroup?.trim();
const rows = await exec
.selectFrom("source_release")
.select(["id", "title", "subtitle", "source_published_at", "first_seen_at", "episode_resolution"])
.where("media_entity_id", "=", entityId)
.where("resolution_status", "=", "resolved")
.where("resolution_locked", "=", false)
.orderBy((eb) => eb.fn.coalesce("source_published_at", "first_seen_at"), "desc")
.orderBy("id")
.limit(filtered ? PREVIEW_SCAN : limit)
.execute();
const wantGroup = input.releaseGroup?.trim().toLowerCase() || null;
const items: (EpisodeMappingPreviewItem & { preview: EpisodeResolution })[] = [];
for (const row of rows) {
if (items.length >= limit) break;
const raw = parseRelease(row.title, row.subtitle ? { subtitle: row.subtitle } : {});
const group = raw.releaseGroup?.name ?? null;
if (wantGroup && group?.toLowerCase() !== wantGroup) continue;
const publishedAt = publishedAtOf(row).toISOString();
const preview = resolveEpisodes({ parsed: raw, kind: entity.kind, table, rules, publishedAt });
const current = (row.episode_resolution as EpisodeResolution | null) ?? null;
if (input.season != null) {
const s = input.season;
const hit = sourceSeason(preview) === s || preview.canonical?.season === s || current?.canonical?.season === s;
if (!hit) continue;
}
items.push({
sourceReleaseId: row.id,
title: row.title,
publishedAt,
releaseGroup: group,
current,
preview,
validation: validationOf(preview),
changed: !sameResolution(current, preview),
});
}
const byGroup = new Map<string, { name: string | null; items: typeof items }>();
for (const it of items) {
const key = it.releaseGroup?.toLowerCase() ?? "";
const g = byGroup.get(key) ?? { name: it.releaseGroup, items: [] };
g.items.push(it);
byGroup.set(key, g);
}
const groups: EpisodeMappingPreviewGroup[] = [...byGroup.values()]
.map((g) => {
const counts = new Map<EpisodeResolutionMethod, number>();
for (const it of g.items) counts.set(it.preview.method, (counts.get(it.preview.method) ?? 0) + 1);
return {
releaseGroup: g.name,
total: g.items.length,
methods: [...counts.entries()].map(([method, count]) => ({ method, count })),
pending: g.items.filter((i) => i.preview.method === "unresolved").length,
suggestion: groupSuggestion(g.items, table, g.name),
};
})
.sort((a, b) => b.total - a.total);
return { applicable: true, seasons, groups, items, changed: items.filter((i) => i.changed).length };
}
// ── 待处理 episode_mapping ──
/**
* 处理 episode_mapping 事项:确认建议规则(建 expression 规则)/ 忽略并建 special=ignore 规则。
* 同一事务里:建规则 → 按用户的选择关闭事项 → 按作品重算(资源解决后不会再挂到已关闭的事项上;
* 仍待确认的资源会另开一条新事项)。
*/
export async function resolveEpisodeDecision(
ctx: ServerContext,
d: DecisionRow,
optionKey: string,
note: string | null,
actor: Actor,
): Promise<DecisionRow> {
const ev = (d.evidence ?? {}) as EpisodeDecisionEvidence;
const entityId = ev.mediaEntityId ?? (d.subject_type === "media_entity" ? d.subject_id : null);
if (!entityId) throw bad("事项缺少作品");
const scope = { season: ev.season ?? null, releaseGroup: ev.releaseGroup ?? null };
let spec: EpisodeMappingRuleSpec;
if (optionKey === EPISODE_DECISION_CONFIRM) {
if (!ev.suggestion) throw new ApiException("conflict", "这条事项没有建议规则,到作品页的集数映射里手动建");
spec = {
...scope,
kind: "expression",
expression: ev.suggestion.expression,
targetSeason: ev.suggestion.targetSeason,
};
} else if (optionKey === EPISODE_DECISION_IGNORE_RULE) {
spec = { ...scope, kind: "special", special: "ignore" };
} else {
throw bad(`未知选项 ${optionKey}`);
}
const n = normalizeRuleSpec(spec);
const by = `${actor.type}:${actor.id ?? ""}`;
const status = optionKey === EPISODE_DECISION_IGNORE_RULE ? "dismissed" : "resolved";
return withConflict(() =>
ctx.db.transaction().execute(async (trx) => {
const cur = await trx
.selectFrom("decision")
.selectAll()
.where("id", "=", d.id)
.forUpdate()
.executeTakeFirstOrThrow();
if (!["open", "investigating", "awaiting_user"].includes(cur.status))
throw new ApiException("conflict", `事项已是 ${cur.status}`);
const rule = await insertRule(trx, entityId, n, actor, {
decisionId: d.id,
...(optionKey === EPISODE_DECISION_CONFIRM ? { suggestion: ev.suggestion } : {}),
});
await trx
.updateTable("decision")
.set({
status,
resolution: jsonb({ optionKey, note, by, execution: { ruleId: rule.id } }),
resolved_by: by,
resolved_at: new Date(),
})
.where("id", "=", d.id)
.execute();
await ctx.outbox.emit(
trx,
"decision.updated",
{ decisionId: d.id, status },
{ idempotencyKey: `decision.updated:${d.id}:${status}` },
);
await audit(trx, actor, "decision.resolved", { type: "decision", id: d.id }, { optionKey, status });
const reresolve = await reresolveEntityEpisodes(ctx, trx, entityId);
return trx
.updateTable("decision")
.set({ resolution: jsonb({ optionKey, note, by, execution: { ruleId: rule.id, reresolve } }) })
.where("id", "=", d.id)
.returningAll()
.executeTakeFirstOrThrow();
}),
);
}
@@ -1,4 +1,4 @@
import type { EvidenceLevel, MediaKind } from "@umt/contracts";
import type { DecisionOption, EvidenceLevel, MediaKind } from "@umt/contracts";
import { type SourceReleaseRow, sql } from "@umt/db";
import {
type EpisodeMappingRule,
@@ -343,9 +343,42 @@ async function writeEpisodeDecision(ctx: ServerContext, exec: Exec, s: EpisodeDe
...(s.failedRules.length ? { failedRules: s.failedRules } : {}),
suggestion,
},
options: episodeDecisionOptions({ season: s.season, releaseGroup: s.releaseGroup, suggestion }),
});
}
/** 待处理 episode_mapping 的处理选项键(批次 4,处理逻辑在 episode-mapping-admin.ts) */
export const EPISODE_DECISION_CONFIRM = "confirm_suggestion";
export const EPISODE_DECISION_IGNORE_RULE = "ignore_rule";
export interface EpisodeDecisionEvidence {
mediaEntityId?: string;
season?: number | null;
releaseGroup?: string | null;
suggestion?: { expression: string; targetSeason: number; samples?: unknown[] } | null;
}
/** 开 / 刷新 episode_mapping 待处理时附的选项(批次 2 的 writeEpisodeDecision 调用) */
export function episodeDecisionOptions(ev: EpisodeDecisionEvidence): DecisionOption[] {
const scope = { season: ev.season ?? null, releaseGroup: ev.releaseGroup ?? null };
const out: DecisionOption[] = [];
if (ev.suggestion) {
out.push({
key: EPISODE_DECISION_CONFIRM,
label: "确认建议规则",
description: `按 ${ev.suggestion.expression} 换算到第 ${ev.suggestion.targetSeason} 季,建规则后重算这部作品的资源`,
input: { ...scope, expression: ev.suggestion.expression, targetSeason: ev.suggestion.targetSeason },
});
}
out.push({
key: EPISODE_DECISION_IGNORE_RULE,
label: "忽略并建忽略规则",
description: "这个发布组这一季的资源以后都不参与集数解析,也不再提醒",
input: scope,
});
return out;
}
function sourceText(res: EpisodeResolution | null): string {
if (!res) return "";
const { season, episodes, airDate } = res.source;
@@ -411,6 +444,18 @@ export async function clearReleaseEpisodes(ctx: ServerContext, exec: Exec, sourc
await detachFromEpisodeDecisions(ctx, exec, sourceReleaseId, null);
}
const canonicalKey = (c: EpisodeResolution["canonical"] | undefined) =>
c ? `${c.season}:${c.episodes.start}-${c.episodes.end}` : "-";
/** 方式、规范季集、特别篇都相同(jsonb 读回的键序与写入时不同,不能直接比 JSON 串) */
export function sameResolution(
a: EpisodeResolution | null | undefined,
b: EpisodeResolution | null | undefined,
): boolean {
if (!a || !b) return !a && !b;
return a.method === b.method && canonicalKey(a.canonical) === canonicalKey(b.canonical) && a.special === b.special;
}
export interface ReresolveResult {
/** 重算的资源数 */
releases: number;
@@ -463,10 +508,7 @@ export async function reresolveEntityEpisodes(
// 规则刚被停用:后面的资源不再用它
if (r.resolution.ruleFailed) rules = rules.filter((x) => x.id !== r.resolution.ruleFailed);
const before = row.episode_resolution as EpisodeResolution | null;
const changed =
JSON.stringify(before?.canonical ?? null) !== JSON.stringify(r.resolution.canonical) ||
before?.method !== r.resolution.method ||
before?.special !== r.resolution.special;
const changed = !sameResolution(before, r.resolution);
if (changed) out.changed++;
if (before?.method === "unresolved" && r.resolution.method !== "unresolved") out.resolved++;
const cols = toParsedColumns(r.parsed);