feat(short-drama-agent-front): 接入审核人工确认与延期验证
This commit is contained in:
1 parent
5836b90934
commit
c6ef595a8e
23 files changed
+3923
-67
No files matched your search
@@ -0,0 +1,447 @@
|
||||
import { computed, onScopeDispose, ref, watch, type Ref } from 'vue'
|
||||
import { useQuery } from '../../../composables/useQuery'
|
||||
import { ApiError, errorMessage } from '../../../lib/http'
|
||||
import { useActionMessage } from '../../../components/ui/useActionMessage'
|
||||
import { getOperation, runOperation } from '../../workflows/operations'
|
||||
import { dramaReviewApi, loadDramaReview } from './api'
|
||||
import {
|
||||
deferredDraftError,
|
||||
newDeferredDraft,
|
||||
suggestionForUncertainty,
|
||||
indexedPendingIssues,
|
||||
indexedUncertainties,
|
||||
manualIssueError,
|
||||
reviewBlockReason,
|
||||
sameReviewSource,
|
||||
uncertaintyAcceptable,
|
||||
type IndexedReviewIssue
|
||||
} from './model'
|
||||
import type {
|
||||
ConfirmDramaIssuesInput,
|
||||
ConfirmDramaIssuesResult,
|
||||
DramaReviewIssue,
|
||||
ManualReviewDraft,
|
||||
DeferredUncertaintyDraft,
|
||||
ReviewDraftBackup,
|
||||
UncertaintyDisposition
|
||||
} from './types'
|
||||
|
||||
/**
|
||||
* 审核问题处理只保存人工裁决;当前权限与终态始终读取服务端。
|
||||
* @param projectId 正式项目编号
|
||||
* @param changed 写入后刷新项目、保存记录及阶段状态
|
||||
* @param disabled 外部项目读取或操作限制
|
||||
* @returns 查询、原始索引选择与明确提交入口
|
||||
*/
|
||||
export function useDramaReview(
|
||||
projectId: Ref<string>,
|
||||
changed: () => Promise<void>,
|
||||
disabled: Ref<boolean> = ref(false)
|
||||
) {
|
||||
/** 正式审核与当前预检,不自动轮询。 */
|
||||
const query = useQuery(projectId, (id, signal) => loadDramaReview(id, signal))
|
||||
/** 复杂原文核对与人工补充使用同一个全屏抽屉。 */
|
||||
const editorOpen = ref(false)
|
||||
/** 本次人工确认请求状态,普通读取不锁定关闭。 */
|
||||
const writing = ref(false)
|
||||
/** 用户对本次处理的明确确认。 */
|
||||
const acknowledged = ref(false)
|
||||
/** 当前已尝试提交表单,首次打开不显示字段错误。 */
|
||||
const attempted = ref(false)
|
||||
/** 当前操作的可读错误,不改变原始业务错误。 */
|
||||
const actionError = ref('')
|
||||
/** 服务端本次真实回执,不在页面推算 finalized。 */
|
||||
const receipt = ref<ConfirmDramaIssuesResult | null>(null)
|
||||
/** 原始待确认列表中的错误索引。 */
|
||||
const selectedErrors = ref<number[]>([])
|
||||
/** 原始待确认列表中的警告索引。 */
|
||||
const selectedWarnings = ref<number[]>([])
|
||||
/** 原始不确定项数组索引。 */
|
||||
const selectedUncertainty = ref<number[]>([])
|
||||
/** 用户主动选择的延期草稿;读取建议不会新增任何条目。 */
|
||||
const deferredDrafts = ref<Record<number, DeferredUncertaintyDraft>>({})
|
||||
/** 来源变化后保留的只读草稿,不能直接套用到新审核。 */
|
||||
const preservedDraft = ref<ReviewDraftBackup | null>(null)
|
||||
/** 字段验证反馈复用应用 NMessage,不重复发送后台错误通知。 */
|
||||
const message = useActionMessage()
|
||||
/** 用户补充的原文证据草稿。 */
|
||||
const manualDrafts = ref<ManualReviewDraft[]>([])
|
||||
/** 项目级活动锁与其它生成入口保持一致。 */
|
||||
const operation = computed(() => getOperation(projectId.value))
|
||||
/** 剩余问题保留原始索引,分组不会影响请求。 */
|
||||
const pendingRows = computed(() => indexedPendingIssues(query.data.value?.preview ?? null))
|
||||
/** 剩余不确定项保留原始索引。 */
|
||||
const uncertaintyRows = computed(() => indexedUncertainties(query.data.value?.preview ?? null))
|
||||
/** 服务端明确给出的不可处理原因。 */
|
||||
const blockedReason = computed(() => query.error.value || reviewBlockReason(query.data.value))
|
||||
/** 普通读取、来源不完整或后台运行时只读查看。 */
|
||||
const locked = computed(
|
||||
() => disabled.value || writing.value || query.loading.value || !!blockedReason.value || operation.value.pending
|
||||
)
|
||||
/** 用户实际选择的处理数量,不混算服务端阻断分类。 */
|
||||
const selectionCount = computed(
|
||||
() =>
|
||||
selectedErrors.value.length +
|
||||
selectedWarnings.value.length +
|
||||
selectedUncertainty.value.length +
|
||||
Object.keys(deferredDrafts.value).length +
|
||||
manualDrafts.value.length
|
||||
)
|
||||
/** 只有用户明确勾选后才允许执行表单提交。 */
|
||||
const canConfirm = computed(
|
||||
() => editorOpen.value && acknowledged.value && selectionCount.value > 0 && !locked.value
|
||||
)
|
||||
/** 提交后才逐行展示人工补充表单错误。 */
|
||||
const manualErrors = computed(() =>
|
||||
attempted.value
|
||||
? manualDrafts.value.map(draft => manualIssueError(draft, query.data.value?.preview?.episodes ?? []))
|
||||
: []
|
||||
)
|
||||
/** 用户尝试提交后才显示延期字段错误。 */
|
||||
const deferredErrors = computed<Record<number, string>>(() =>
|
||||
attempted.value
|
||||
? Object.fromEntries(
|
||||
Object.entries(deferredDrafts.value).map(([key, draft]) => [
|
||||
Number(key),
|
||||
draft.index !== Number(key) ||
|
||||
!uncertaintyRows.value.some(row => row.originalIndex === draft.index)
|
||||
? '原审核项已变化,请重新读取。'
|
||||
: deferredDraftError(draft, query.data.value?.preview?.episodes ?? [])
|
||||
])
|
||||
)
|
||||
: {}
|
||||
)
|
||||
/** 草稿行编号只用于界面,不作为业务标识提交。 */
|
||||
let nextDraftId = 0
|
||||
/** 项目版本,防止旧项目预检或回执回填。 */
|
||||
let revision = 0
|
||||
/** 提交前只读核对的取消控制器。 */
|
||||
let controller: AbortController | undefined
|
||||
onScopeDispose(() => {
|
||||
revision++
|
||||
controller?.abort()
|
||||
})
|
||||
|
||||
/** 清除当前选择与证据草稿,来源变化时要求重新核对。 */
|
||||
function resetChoices() {
|
||||
selectedErrors.value = []
|
||||
selectedWarnings.value = []
|
||||
selectedUncertainty.value = []
|
||||
deferredDrafts.value = {}
|
||||
manualDrafts.value = []
|
||||
acknowledged.value = false
|
||||
attempted.value = false
|
||||
}
|
||||
watch(projectId, () => {
|
||||
revision++
|
||||
controller?.abort()
|
||||
editorOpen.value = false
|
||||
resetChoices()
|
||||
receipt.value = null
|
||||
preservedDraft.value = null
|
||||
actionError.value = ''
|
||||
})
|
||||
watch(editorOpen, () => {
|
||||
acknowledged.value = false
|
||||
attempted.value = false
|
||||
})
|
||||
watch(
|
||||
[selectedErrors, selectedWarnings, selectedUncertainty, deferredDrafts, manualDrafts],
|
||||
() => {
|
||||
acknowledged.value = false
|
||||
attempted.value = false
|
||||
if (!writing.value && selectionCount.value > 0) actionError.value = ''
|
||||
},
|
||||
{ deep: true }
|
||||
)
|
||||
watch(
|
||||
() => query.data.value,
|
||||
(current, previous) => {
|
||||
const before = previous?.preview
|
||||
const after = current?.preview
|
||||
const sourceChanged =
|
||||
(before && (!after || !sameReviewSource(before, after))) ||
|
||||
(!!previous?.latest && previous.latest.id !== current?.latest?.id)
|
||||
if (sourceChanged) {
|
||||
const hadChoices = selectionCount.value > 0
|
||||
if (!writing.value) receipt.value = null
|
||||
if (before && hadChoices) preserveChoices(before)
|
||||
resetChoices()
|
||||
if (hadChoices && !writing.value)
|
||||
actionError.value = '审核来源或建议已变化,旧选择已保留为草稿,请按当前版本重新核对。'
|
||||
}
|
||||
},
|
||||
{ flush: 'sync' }
|
||||
)
|
||||
|
||||
/** 打开处理界面时重新核对当前版本,不自动提交。 */
|
||||
async function openEditor() {
|
||||
editorOpen.value = true
|
||||
actionError.value = ''
|
||||
await query.refresh()
|
||||
}
|
||||
|
||||
/**
|
||||
* 已有结构化问题可带入补充表单,纯文本疑点不能自动升级为错误。
|
||||
* @param issue 用户选择补充证据的原问题
|
||||
*/
|
||||
function addManual(issue?: DramaReviewIssue) {
|
||||
if (locked.value || issue?.policyException) return
|
||||
manualDrafts.value.push({
|
||||
id: `manual-${++nextDraftId}`,
|
||||
episode: issue?.episode ?? query.data.value?.preview?.episodes[0]?.episode ?? null,
|
||||
category: issue?.category ?? 'action_continuity',
|
||||
code: issue?.code ?? '',
|
||||
sourceQuote: issue?.sourceQuote ?? '',
|
||||
message: issue?.message ?? '',
|
||||
suggestion: issue?.suggestion ?? '',
|
||||
subjectRef: issue?.subjectRef ?? ''
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除尚未保存的证据行,不修改正式审核。
|
||||
* @param id 草稿行编号
|
||||
*/
|
||||
function removeManual(id: string) {
|
||||
manualDrafts.value = manualDrafts.value.filter(draft => draft.id !== id)
|
||||
actionError.value = ''
|
||||
attempted.value = false
|
||||
}
|
||||
|
||||
/**
|
||||
* 界面仅开放接口明确支持的操作,最终证据与权限仍由服务端复核。
|
||||
* @param row 原始问题及索引
|
||||
* @returns 可选择的确认或接受方式,无法核实时为空
|
||||
*/
|
||||
function issueAction(row: IndexedReviewIssue): 'error' | 'warning' | null {
|
||||
const issue = row.issue
|
||||
const content = query.data.value?.preview?.episodes.find(item => item.episode === issue.episode)?.content
|
||||
if (issue.policyException || !content?.includes(issue.sourceQuote)) return null
|
||||
if (issue.severity === 'warning') return 'warning'
|
||||
return issue.evidenceConfidence === 'explicit' ? 'error' : null
|
||||
}
|
||||
|
||||
/**
|
||||
* 来源变化时保留用户输入供核对,不自动复用旧索引。
|
||||
* @param source 原草稿绑定的预检
|
||||
*/
|
||||
function preserveChoices(source: NonNullable<typeof query.data.value>['preview']) {
|
||||
if (!source || selectionCount.value === 0) return
|
||||
preservedDraft.value = JSON.parse(
|
||||
JSON.stringify({
|
||||
reviewId: source.reviewId,
|
||||
checkpointId: source.checkpointId,
|
||||
issueIndexes: selectedErrors.value,
|
||||
acceptedWarningIndexes: selectedWarnings.value,
|
||||
acceptedUncertaintyIndexes: selectedUncertainty.value,
|
||||
descriptions: source.uncertainty,
|
||||
deferredUncertainties: Object.values(deferredDrafts.value),
|
||||
manualIssues: manualDrafts.value
|
||||
})
|
||||
) as ReviewDraftBackup
|
||||
}
|
||||
|
||||
/**
|
||||
* 人工选择是唯一创建延期草稿的入口,接受与延期始终互斥。
|
||||
* @param index 原始不确定项索引
|
||||
* @param action 用户明确选择的处理方式
|
||||
*/
|
||||
function setUncertaintyAction(index: number, action: UncertaintyDisposition) {
|
||||
if (locked.value) return
|
||||
const row = uncertaintyRows.value.find(item => item.originalIndex === index)
|
||||
if (!row || (action !== 'pending' && !uncertaintyAcceptable(row.text))) return
|
||||
selectedUncertainty.value = selectedUncertainty.value.filter(value => value !== index)
|
||||
if (action === 'accept') {
|
||||
delete deferredDrafts.value[index]
|
||||
selectedUncertainty.value.push(index)
|
||||
} else if (action === 'defer') {
|
||||
if (!deferredDrafts.value[index])
|
||||
deferredDrafts.value[index] = newDeferredDraft(
|
||||
index,
|
||||
suggestionForUncertainty(query.data.value?.preview ?? null, index),
|
||||
query.data.value?.preview?.episodes ?? []
|
||||
)
|
||||
} else delete deferredDrafts.value[index]
|
||||
acknowledged.value = false
|
||||
attempted.value = false
|
||||
actionError.value = ''
|
||||
}
|
||||
|
||||
/**
|
||||
* 不确定说明只能带入尚未核实的表单草稿,原文与建议仍需人工填写。
|
||||
* @param index 原始不确定项索引
|
||||
*/
|
||||
function addUncertaintyEvidence(index: number) {
|
||||
const row = uncertaintyRows.value.find(item => item.originalIndex === index)
|
||||
if (locked.value || !row) return
|
||||
addManual()
|
||||
const draft = manualDrafts.value.at(-1)
|
||||
const suggestion = suggestionForUncertainty(query.data.value?.preview ?? null, index)
|
||||
if (draft) {
|
||||
draft.episode =
|
||||
suggestion?.confidence === 'high' && !suggestion.requiresEpisodeSelection ? suggestion.episode : null
|
||||
draft.message = row.text
|
||||
}
|
||||
}
|
||||
|
||||
/** 在明确确认后重新预检并保存裁决,成功或冲突后重读真实结果,不自动改写。 */
|
||||
async function confirm() {
|
||||
if (!canConfirm.value || !query.data.value?.preview) return
|
||||
attempted.value = true
|
||||
const overlap = Object.values(deferredDrafts.value).some(draft =>
|
||||
selectedUncertainty.value.includes(draft.index)
|
||||
)
|
||||
if (overlap) {
|
||||
actionError.value = '同一问题不能同时接受和延期验证。'
|
||||
message?.warning(actionError.value)
|
||||
return
|
||||
}
|
||||
const unresolved = [
|
||||
...selectedUncertainty.value,
|
||||
...Object.values(deferredDrafts.value).map(draft => draft.index)
|
||||
].some(index => {
|
||||
const row = uncertaintyRows.value.find(item => item.originalIndex === index)
|
||||
return !row || !uncertaintyAcceptable(row.text)
|
||||
})
|
||||
if (unresolved) {
|
||||
actionError.value = '所选问题已变化或需要补充原文证据,请重新核对。'
|
||||
message?.warning(actionError.value)
|
||||
return
|
||||
}
|
||||
if (Object.values(deferredErrors.value).some(Boolean)) {
|
||||
actionError.value = '请先完善延期验证的剧集、阶段和验证标准。'
|
||||
message?.warning(actionError.value)
|
||||
return
|
||||
}
|
||||
if (manualErrors.value.some(Boolean)) {
|
||||
actionError.value = '请先修正人工补充中的原文与必填内容。'
|
||||
message?.warning(actionError.value)
|
||||
return
|
||||
}
|
||||
const expected = query.data.value.preview
|
||||
const id = projectId.value
|
||||
const version = revision
|
||||
const input: ConfirmDramaIssuesInput = {
|
||||
confirmed: true,
|
||||
reviewId: expected.reviewId,
|
||||
issueIndexes: [...selectedErrors.value],
|
||||
acceptedWarningIndexes: [...selectedWarnings.value],
|
||||
acceptedUncertaintyIndexes: [...selectedUncertainty.value],
|
||||
...(Object.keys(deferredDrafts.value).length
|
||||
? {
|
||||
deferredUncertainties: Object.values(deferredDrafts.value)
|
||||
.toSorted((a, b) => a.index - b.index)
|
||||
.map(draft => ({
|
||||
index: draft.index,
|
||||
episode: draft.episode!,
|
||||
verificationTarget: draft.verificationTarget!,
|
||||
verificationCriteria: draft.verificationCriteria.trim()
|
||||
}))
|
||||
}
|
||||
: {}),
|
||||
...(manualDrafts.value.length
|
||||
? {
|
||||
manualIssues: manualDrafts.value.map(draft => ({
|
||||
episode: draft.episode!,
|
||||
category: draft.category,
|
||||
code: draft.code.trim(),
|
||||
severity: 'error' as const,
|
||||
sourceQuote: draft.sourceQuote.trim(),
|
||||
message: draft.message.trim(),
|
||||
suggestion: draft.suggestion.trim(),
|
||||
evidenceConfidence: 'explicit' as const,
|
||||
policyException: false,
|
||||
...(draft.subjectRef.trim() ? { subjectRef: draft.subjectRef.trim() } : {})
|
||||
}))
|
||||
}
|
||||
: {})
|
||||
}
|
||||
writing.value = true
|
||||
actionError.value = ''
|
||||
controller = new AbortController()
|
||||
try {
|
||||
const succeeded = await runOperation(
|
||||
id,
|
||||
'确认审核问题',
|
||||
async () => {
|
||||
const current = await loadDramaReview(id, controller!.signal)
|
||||
if (disabled.value || version !== revision || projectId.value !== id)
|
||||
throw new Error('查看的项目已变化,请重新核对。')
|
||||
if (!current.preview || !sameReviewSource(expected, current.preview)) {
|
||||
preserveChoices(expected)
|
||||
query.data.value = current
|
||||
resetChoices()
|
||||
throw new Error('审核记录或建议来源已变化,旧选择已保留为草稿,请重新核对并选择问题。')
|
||||
}
|
||||
const block = reviewBlockReason(current)
|
||||
if (block) {
|
||||
query.data.value = current
|
||||
throw new Error(block)
|
||||
}
|
||||
const result = await dramaReviewApi.confirm(id, input)
|
||||
if (
|
||||
result.projectId !== id ||
|
||||
result.reviewId !== expected.reviewId ||
|
||||
!result.checkpointId ||
|
||||
typeof result.finalized !== 'boolean' ||
|
||||
!Array.isArray(result.confirmedEpisodes)
|
||||
)
|
||||
throw new Error('处理回执来源尚未确认,请刷新审核记录,不要重复提交。')
|
||||
if (version === revision && projectId.value === id) receipt.value = result
|
||||
return result
|
||||
},
|
||||
result => ({
|
||||
type: result.finalized ? 'success' : 'info',
|
||||
content: result.finalized
|
||||
? '人工处理已完成,当前审核已通过。'
|
||||
: '本次处理已保存,请继续核对剩余问题与改写范围。'
|
||||
})
|
||||
)
|
||||
if (version !== revision || projectId.value !== id) return
|
||||
acknowledged.value = false
|
||||
if (succeeded) {
|
||||
resetChoices()
|
||||
await Promise.all([query.refresh(), changed()])
|
||||
} else {
|
||||
actionError.value = getOperation(id).error
|
||||
if (getOperation(id).cause instanceof ApiError && (getOperation(id).cause as ApiError).status === 409)
|
||||
await query.refresh()
|
||||
}
|
||||
} catch (cause) {
|
||||
if (version === revision && projectId.value === id) actionError.value = errorMessage(cause)
|
||||
} finally {
|
||||
writing.value = false
|
||||
}
|
||||
}
|
||||
return {
|
||||
query,
|
||||
editorOpen,
|
||||
writing,
|
||||
acknowledged,
|
||||
actionError,
|
||||
receipt,
|
||||
selectedErrors,
|
||||
selectedWarnings,
|
||||
selectedUncertainty,
|
||||
deferredDrafts,
|
||||
deferredErrors,
|
||||
preservedDraft,
|
||||
setUncertaintyAction,
|
||||
addUncertaintyEvidence,
|
||||
manualDrafts,
|
||||
manualErrors,
|
||||
pendingRows,
|
||||
uncertaintyRows,
|
||||
blockedReason,
|
||||
locked,
|
||||
selectionCount,
|
||||
canConfirm,
|
||||
openEditor,
|
||||
addManual,
|
||||
removeManual,
|
||||
issueAction,
|
||||
uncertaintyAcceptable,
|
||||
confirm
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user