feat(contracts): site center schemas, endpoints and migration 0031

Site health/proxy, SiteAccount login state (challenge, lastLoginAt, lastError,
userData), SiteUserData history table, SiteCredentialsBody by kind,
refresh-login/health-check/user-data/delete endpoints, SitesSettings defaults.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
rootandClaude Opus 5.5 committed 2026-09-28 14:40:07 +08:00
1 parent 381791cd64
commit e2d1e21968
7 files changed
+458 -31

No files matched your search

+44 -7
View File
@@ -1,5 +1,6 @@
import { z } from "zod";
import { Id, Timestamp } from "./common.ts";
import { SiteRateLimit } from "./sources.ts";
// 全局设置:存于 settings 表(key → jsonb)。每个顶层分组对应一个 settings.key。
// schema 本身不带 .default()(zod 4 在 partial 里仍会套默认值,会让 PATCH 覆盖现值);
@@ -152,6 +153,29 @@ export const MetadataSettings = z.object({
});
export type MetadataSettings = z.infer<typeof MetadataSettings>;
/** 固定桌面 Chrome UA(账号未设 UA 时使用) */
export const DEFAULT_SITE_USER_AGENT =
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36";
/** 站点默认(site-center §8):站点行的限流 / 快照周期为空时继承;站点和凭证在站点页管理 */
export const SitesSettings = z.object({
/** 站点 rate_limit 为空时使用 */
defaultRateLimit: SiteRateLimit,
/** 站点未设快照周期时使用(秒) */
defaultSnapshotIntervalSec: z
.number()
.int()
.min(300)
.max(7 * 24 * 3600),
/** 账号未设 UA 时使用 */
userAgent: z.string().min(1).max(500),
/** 站点开"使用代理"时生效;null = 不用代理 */
proxyUrl: z.url().nullable(),
/** 用户数据做种分页上限 */
userDataMaxSeedingPages: z.number().int().min(1).max(50),
});
export type SitesSettings = z.infer<typeof SitesSettings>;
/** 每日预算与并发(REQ §6.4、§10.5) */
export const BudgetSettings = z.object({
maxNewTitlesPerDay: z.number().int().min(0),
@@ -171,6 +195,7 @@ export const GlobalSettings = z.object({
budget: BudgetSettings,
agent: AgentRuntimeSettings,
metadata: MetadataSettings,
sites: SitesSettings,
});
export type GlobalSettings = z.infer<typeof GlobalSettings>;
@@ -219,6 +244,13 @@ export const DEFAULT_SETTINGS: GlobalSettings = {
sessionDir: null,
},
metadata: { tmdbApiKeySecretRef: null, tmdbBaseUrl: null },
sites: {
defaultRateLimit: { requestsPerMinute: 20, concurrency: 1 },
defaultSnapshotIntervalSec: 3600,
userAgent: DEFAULT_SITE_USER_AGENT,
proxyUrl: null,
userDataMaxSeedingPages: 10,
},
};
/** PATCH /settings:按分组部分更新;服务端与现值合并后用 GlobalSettings 整体再校验 */
@@ -254,6 +286,7 @@ export const GlobalSettingsUpdate = z.object({
tmdbApiKey: z.string().min(1).max(4096).nullable().optional(),
})
.optional(),
sites: SitesSettings.partial().optional(),
});
export type GlobalSettingsUpdate = z.infer<typeof GlobalSettingsUpdate>;
@@ -270,6 +303,7 @@ export function mergeSettings(current: GlobalSettings, patch: GlobalSettingsUpda
budget: { ...current.budget, ...patch.budget },
agent: { ...current.agent, ...patch.agent },
metadata: { ...current.metadata, ...metadata },
sites: { ...(current.sites ?? DEFAULT_SETTINGS.sites), ...patch.sites },
};
}
@@ -368,15 +402,18 @@ export const SETTINGS_SECTIONS: readonly SettingsSectionDef[] = [
},
{
id: "sites",
label: "站点与账号",
description: "站点启停、快照周期、限流、账号凭证",
label: "站点默认",
description: "新站点的限流、快照周期、UA 和代理;站点和凭证在站点页管理",
legacyTabs: [{ tab: "site-credentials" }],
anchors: [
{ id: "sites", label: "站点" },
{ id: "accounts", label: "站点账号" },
anchors: [{ id: "defaults", label: "站点默认" }],
fields: [
"sites.defaultRateLimit",
"sites.defaultSnapshotIntervalSec",
"sites.userAgent",
"sites.proxyUrl",
"sites.userDataMaxSeedingPages",
],
fields: [],
resources: ["sites", "site-accounts"],
resources: [],
},
{
id: "downloaders",