返回 oh-my-ppt
sessionMetadata.ts
根目录 / src / renderer / src / lib / sessionMetadata.ts
1 type SessionLike = {
2 status?: string | null
3 page_count?: number | null
4 generated_count?: number | null
5 generatedCount?: number | null
6 failed_count?: number | null
7 failedCount?: number | null
8 metadata?: string | null
9 }
10
11 type SessionMetadata = {
12 source?: unknown
13 }
14
15 export interface EditorGate {
16 canEdit: boolean
17 generatedCount: number
18 failedCount: number
19 totalCount: number
20 requiredCount: number
21 }
22
23 export const parseSessionMetadata = (metadata: string | null | undefined): SessionMetadata => {
24 if (!metadata) return {}
25 try {
26 const parsed = JSON.parse(metadata) as SessionMetadata
27 return parsed && typeof parsed === 'object' ? parsed : {}
28 } catch {
29 return {}
30 }
31 }
32
33 export const getEditorGate = (session: SessionLike | null | undefined, threshold = 0.5): EditorGate => {
34 const explicitGenerated = Number(session?.generated_count ?? session?.generatedCount)
35 const explicitFailed = Number(session?.failed_count ?? session?.failedCount)
36 const generatedCount = Number.isFinite(explicitGenerated)
37 ? Math.max(0, Math.floor(explicitGenerated))
38 : 0
39 const failedCount = Number.isFinite(explicitFailed)
40 ? Math.max(0, Math.floor(explicitFailed))
41 : 0
42 const explicitTotal = Number(session?.page_count ?? 0)
43 const totalCount = Math.max(
44 Number.isFinite(explicitTotal) ? Math.floor(explicitTotal) : 0,
45 generatedCount + failedCount,
46 generatedCount
47 )
48 const requiredCount = Math.max(1, Math.ceil(Math.max(1, totalCount) * threshold))
49 const canEdit = generatedCount > 0 && (session?.status === 'completed' || generatedCount >= requiredCount)
50
51 return {
52 canEdit,
53 generatedCount,
54 failedCount,
55 totalCount: Math.max(1, totalCount),
56 requiredCount,
57 }
58 }
59
59 lines TYPESCRIPT