feat(short-drama-agent-front): 完善质量规则确认与中文交互

This commit is contained in:
GJ committed 2026-10-17 22:09:28 +08:00
1 parent 190333d4f9
commit c4b43b9725
85 files changed
+2059 -480

No files matched your search

@@ -0,0 +1,93 @@
import { policyIssue } from './quality-policy'
import type { ProjectQualityPolicy } from './types'
/** 同一页面中主动保存规则后,也撤销无法持久化的临时确认。 */
const invalidations = new Map<string, number>()
/**
* 只比较质量规则的规范化内容,不把模型参数或对象属性顺序作为变更。
* @param policy 后端读取或用户编辑的完整规则
* @returns 可精确比较的内容签名,不是后端授权令牌
*/
export function qualityPolicySignature(policy: ProjectQualityPolicy): string {
if (!policy || !Array.isArray(policy.customRules) ||
(policy.actionExceptions !== undefined && !Array.isArray(policy.actionExceptions)))
throw new Error('质量规则响应不完整,请重新读取。')
const issue = policyIssue(policy)
if (issue) throw new Error(issue)
return JSON.stringify({
version: policy.version,
physicsMode: policy.physicsMode,
allowExaggeratedMotion: policy.allowExaggeratedMotion,
allowNonPhysicalActions: policy.allowNonPhysicalActions,
allowSupernaturalAbilities: policy.allowSupernaturalAbilities,
customRules: policy.customRules.map(rule => rule.trim()),
actionExceptions: (policy.actionExceptions ?? []).map(item => ({
code: item.code,
ownerRef: item.ownerRef.trim(),
resource: item.resource,
reason: item.reason.trim()
}))
})
}
/**
* 隔离项目及本地确认格式版本。
* @param projectId 当前项目
* @returns 本地 UX 记忆键
*/
function confirmationKey(projectId: string): string {
return `short-drama:breakdown-quality-confirmation:v1:${projectId}`
}
/**
* 读取当前浏览器明确确认过的规则,不将默认值当作确认。
* @param projectId 当前项目
* @returns 已确认内容或空值,损坏与不可用存储均不视为确认
*/
export function readQualityPolicyConfirmation(projectId: string): string | null {
try {
const raw = localStorage.getItem(confirmationKey(projectId))
if (!raw) return null
const parsed: unknown = JSON.parse(raw)
if (!parsed || typeof parsed !== 'object' || !('signature' in parsed) ||
!('formatVersion' in parsed) || parsed.formatVersion !== 1 ||
typeof parsed.signature !== 'string') return null
return parsed.signature
} catch {
return null
}
}
/**
* 仅在用户明确确认后记住当前浏览器的规则内容,不能代替后端门禁。
* @param projectId 当前项目
* @param signature 已核对的规则内容
* @returns 是否成功持久化,失败时调用方只保留本次会话确认
*/
export function rememberQualityPolicyConfirmation(projectId: string, signature: string): boolean {
try {
localStorage.setItem(confirmationKey(projectId), JSON.stringify({ formatVersion: 1, signature }))
return true
} catch {
return false
}
}
/**
* 规则有实质变化时撤销本地确认,无变化保存不调用此方法。
* @param projectId 规则所属项目
*/
export function invalidateQualityPolicyConfirmation(projectId: string) {
invalidations.set(projectId, qualityPolicyConfirmationEpoch(projectId) + 1)
try { localStorage.removeItem(confirmationKey(projectId)) } catch { /* 存储受限时仍撤销页面内确认。 */ }
}
/**
* 当前页面内的确认失效代次,用于存储受限的降级场景。
* @param projectId 当前项目
* @returns 规则失效代次
*/
export function qualityPolicyConfirmationEpoch(projectId: string): number {
return invalidations.get(projectId) ?? 0
}