import { z } from "zod"; import { Id, Timestamp } from "./common"; // 全局设置:存于 settings 表(key → jsonb)。每个顶层分组对应一个 settings.key。 // schema 本身不带 .default()(zod 4 在 partial 里仍会套默认值,会让 PATCH 覆盖现值); // 缺省值统一放在 DEFAULT_SETTINGS,服务端读取时 `{ ...DEFAULT_SETTINGS[key], ...stored }` 后再 parse。 const Percent = z.number().min(1).max(100); export const AutomationSettings = z.object({ /** 自动化总开关:关闭后所有自动收录/下载/整理停止,只读查询与手动操作可用 */ enabled: z.boolean(), /** 恢复后的只读对账模式(REQ §8.5):开启时禁止自动写入 */ readOnlyReconcile: z.boolean(), }); export type AutomationSettings = z.infer; const CacheShape = z.object({ /** 缓存卷使用率硬阈值:达到即停止新增下载并暂停纳管下载中任务 */ hardThresholdPercent: Percent, /** 恢复水位:降到此值以下恢复因阈值暂停的任务 */ resumePercent: Percent, checkIntervalSec: z.number().int().min(10).max(3600), /** 缓存清理的目标使用率(低水位) */ cleanupTargetPercent: Percent, }); /** 下载缓存卷硬阈值与恢复水位(REQ §8.4.1) */ export const CacheSettings = CacheShape.refine((v) => v.resumePercent < v.hardThresholdPercent, { message: "恢复水位必须低于硬阈值", }).refine((v) => v.cleanupTargetPercent <= v.resumePercent, { message: "清理目标不得高于恢复水位" }); export type CacheSettings = z.infer; export const OrganizeMode = z.enum(["copy", "move"]); export type OrganizeMode = z.infer; export const OrganizeSettings = z.object({ defaultMode: OrganizeMode, /** 跨卷 Move 采用 Copy→核验→删源;EXDEV 不静默降级 */ allowCrossVolumeMove: z.boolean(), }); export type OrganizeSettings = z.infer; export const RetentionSettings = z.object({ /** Copy 入库后下载源在缓存中至少保留的天数(与 H&R 要求分开判断) */ seedRetentionDays: z.number().int().min(0).max(365), /** 观察/日志类数据保留天数 */ logRetentionDays: z.number().int().min(1).max(3650), }); export type RetentionSettings = z.infer; export const ThinkingLevel = z.enum(["off", "minimal", "low", "medium", "high", "xhigh"]); export type ThinkingLevel = z.infer; /** Pi Agent 设置(REQ §10.5):上游/模型/思考档位/上下文预算/压缩/子代理;密钥只存 secret 引用 */ export const PiSettings = z.object({ provider: z.string().min(1), baseUrl: z.url().nullable(), model: z.string().min(1).nullable(), thinkingLevel: ThinkingLevel, /** 上下文预算(token) */ contextBudget: z.number().int().min(4096).max(2_000_000), compaction: z.object({ enabled: z.boolean(), /** 达到上下文预算的该比例时触发压缩 */ thresholdRatio: z.number().min(0.3).max(0.95), }), subagentsEnabled: z.boolean(), apiKeySecretRef: Id.nullable(), }); export type PiSettings = z.infer; /** 每日预算与并发(REQ §6.4、§10.5) */ export const BudgetSettings = z.object({ maxNewTitlesPerDay: z.number().int().min(0), maxDownloadBytesPerDay: z.number().int().min(0), maxConcurrentDownloads: z.number().int().min(1).max(100), agentMaxRunsPerDay: z.number().int().min(0), agentMaxCostUsdPerDay: z.number().min(0), }); export type BudgetSettings = z.infer; export const GlobalSettings = z.object({ automation: AutomationSettings, cache: CacheSettings, organize: OrganizeSettings, retention: RetentionSettings, pi: PiSettings, budget: BudgetSettings, }); export type GlobalSettings = z.infer; /** settings 表的合法 key */ export const SettingsKey = GlobalSettings.keyof(); export type SettingsKey = z.infer; /** 缺省设置(settings 表缺 key 或缺字段时使用) */ export const DEFAULT_SETTINGS: GlobalSettings = { automation: { enabled: true, readOnlyReconcile: false }, cache: { hardThresholdPercent: 90, resumePercent: 85, checkIntervalSec: 60, cleanupTargetPercent: 80 }, organize: { defaultMode: "copy", allowCrossVolumeMove: true }, retention: { seedRetentionDays: 7, logRetentionDays: 180 }, pi: { provider: "anthropic", baseUrl: null, model: null, thinkingLevel: "medium", contextBudget: 128_000, compaction: { enabled: true, thresholdRatio: 0.8 }, subagentsEnabled: false, apiKeySecretRef: null, }, budget: { maxNewTitlesPerDay: 20, maxDownloadBytesPerDay: 500 * 1024 ** 3, maxConcurrentDownloads: 5, agentMaxRunsPerDay: 50, agentMaxCostUsdPerDay: 5, }, }; /** PATCH /settings:按分组部分更新;服务端与现值合并后用 GlobalSettings 整体再校验 */ export const GlobalSettingsUpdate = z.object({ automation: AutomationSettings.partial().optional(), cache: CacheShape.partial().optional(), organize: OrganizeSettings.partial().optional(), retention: RetentionSettings.partial().optional(), pi: PiSettings.omit({ apiKeySecretRef: true }) .partial() .extend({ compaction: PiSettings.shape.compaction.partial().optional(), /** 只写:明文上游密钥,服务端存为 secret 并回填 apiKeySecretRef;传 null 表示清除 */ apiKey: z.string().min(1).max(4096).nullable().optional(), }) .optional(), budget: BudgetSettings.partial().optional(), }); export type GlobalSettingsUpdate = z.infer; /** 合并部分更新(纯函数,web 乐观更新与 server 共用) */ export function mergeSettings(current: GlobalSettings, patch: GlobalSettingsUpdate): GlobalSettings { const { apiKey: _apiKey, compaction, ...pi } = patch.pi ?? {}; return { automation: { ...current.automation, ...patch.automation }, cache: { ...current.cache, ...patch.cache }, organize: { ...current.organize, ...patch.organize }, retention: { ...current.retention, ...patch.retention }, pi: { ...current.pi, ...pi, compaction: { ...current.pi.compaction, ...compaction } }, budget: { ...current.budget, ...patch.budget }, }; } /** settings 表行 */ export const SettingEntry = z.object({ key: SettingsKey, value: z.unknown(), updatedAt: Timestamp, }); export type SettingEntry = z.infer;