useReportStudio.ts
4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import { computed, ref, shallowRef } from 'vue'
import type { ReportStatusPayload, ReportTask } from '@/types'
import { fetchJson, postJson } from '@/utils/http'
import { bindPersistentState, loadStoredValue } from './usePersistentState'
interface ReportStatusResponse extends ReportStatusPayload {
success: boolean
}
interface GenerateResponse {
success: boolean
task_id: string
message: string
task: ReportTask
}
const TEMPLATE_STORAGE_KEY = 'bettafish.reportTemplate.v1'
const REPORT_STATE_STORAGE_KEY = 'bettafish.reportState.v2'
const RESEARCH_STATE_STORAGE_KEY = 'bettafish.frontendState.v2'
const TERMINAL_STATUSES = new Set(['completed', 'error', 'cancelled'])
export function useReportStudio() {
const storedTemplate = loadStoredValue(TEMPLATE_STORAGE_KEY, {
content: '',
})
const storedState = loadStoredValue(REPORT_STATE_STORAGE_KEY, {
selectedTaskId: '',
})
const loading = shallowRef(false)
const generating = shallowRef(false)
const selectedTaskId = shallowRef(String(storedState.selectedTaskId || ''))
const customTemplate = shallowRef(String(storedTemplate.content || ''))
const status = ref<ReportStatusPayload>({
initialized: false,
engines_ready: false,
files_found: [],
missing_files: [],
current_task: null,
tasks: [],
})
let progressTimer: number | null = null
const selectedTask = computed(() => (
status.value.tasks.find((item) => item.task_id === selectedTaskId.value)
|| status.value.current_task
|| null
))
const previewUrl = computed(() => {
const taskId = selectedTask.value?.task_id
return taskId ? `/api/report/result/${taskId}` : ''
})
async function refreshStatus() {
loading.value = true
try {
const payload = await fetchJson<ReportStatusResponse>('/api/report/status')
status.value = {
initialized: payload.initialized,
engines_ready: payload.engines_ready,
files_found: payload.files_found,
missing_files: payload.missing_files,
current_task: payload.current_task,
tasks: payload.tasks,
}
if (!selectedTaskId.value && payload.tasks.length > 0) {
selectedTaskId.value = payload.tasks[0].task_id
}
} finally {
loading.value = false
}
}
async function refreshTasks() {
const payload = await fetchJson<ReportStatusResponse>('/api/report/tasks')
status.value = {
...status.value,
current_task: payload.current_task,
tasks: payload.tasks,
}
if (!selectedTaskId.value && payload.tasks.length > 0) {
selectedTaskId.value = payload.tasks[0].task_id
}
}
function stopPolling() {
if (progressTimer !== null) {
window.clearInterval(progressTimer)
progressTimer = null
}
}
function startPolling(taskId: string) {
stopPolling()
progressTimer = window.setInterval(async () => {
await refreshStatus()
const task = status.value.tasks.find((item) => item.task_id === taskId)
if (!task || TERMINAL_STATUSES.has(task.status)) {
generating.value = false
stopPolling()
}
}, 2000)
}
async function generate(query: string, researchTaskId = '') {
generating.value = true
try {
const persistedResearchState = loadStoredValue(RESEARCH_STATE_STORAGE_KEY, {
selectedResearchTaskId: '',
})
const resolvedResearchTaskId = String(
researchTaskId || persistedResearchState.selectedResearchTaskId || '',
).trim()
const payload = await postJson<GenerateResponse>('/api/report/generate', {
query,
custom_template: customTemplate.value,
research_task_id: resolvedResearchTaskId,
})
selectedTaskId.value = payload.task_id
await refreshStatus()
startPolling(payload.task_id)
return payload
} catch (error) {
generating.value = false
throw error
}
}
function selectTask(taskId: string) {
selectedTaskId.value = taskId
}
function download(kind: 'html' | 'md' | 'pdf', taskId: string) {
const target = {
html: `/api/report/download/${taskId}`,
md: `/api/report/export/md/${taskId}`,
pdf: `/api/report/export/pdf/${taskId}`,
}[kind]
window.open(target, '_blank', 'noopener')
}
bindPersistentState(
TEMPLATE_STORAGE_KEY,
computed(() => ({
content: customTemplate.value,
})),
)
bindPersistentState(
REPORT_STATE_STORAGE_KEY,
computed(() => ({
selectedTaskId: selectedTaskId.value,
})),
)
return {
loading,
generating,
status,
selectedTask,
selectedTaskId,
customTemplate,
previewUrl,
refreshStatus,
refreshTasks,
generate,
selectTask,
download,
stopPolling,
}
}