82 lines
3.7 KiB
TypeScript
82 lines
3.7 KiB
TypeScript
import { hubCall } from './hub'
|
|
import { uuid } from '@/utils/uuid'
|
|
export interface ParseParams {
|
|
files: File[]
|
|
parser_resource_id: string
|
|
output_dir: string
|
|
lang_list: string
|
|
backend: string
|
|
parse_method: string
|
|
formula_enable: boolean
|
|
table_enable: boolean
|
|
server_url?: string
|
|
start_page_id: number
|
|
end_page_id: number
|
|
return_md: boolean
|
|
return_middle_json: boolean
|
|
response_format_zip: boolean
|
|
}
|
|
|
|
export interface ParseResult {
|
|
backend: string
|
|
version: string
|
|
results: Record<string, {
|
|
md_content?: string
|
|
middle_json?: string
|
|
model_output?: string
|
|
content_list?: string
|
|
images?: Record<string, string>
|
|
}>
|
|
}
|
|
|
|
export interface ParseProgress {
|
|
progress: number
|
|
stage: string
|
|
status: 'pending' | 'processing' | 'completed' | 'failed'
|
|
error: string | null
|
|
file_names: string
|
|
}
|
|
|
|
export interface MindmapOrganizeProgress extends ParseProgress {
|
|
result_md?: string | null
|
|
}
|
|
|
|
|
|
const ids = new Map<string, string>()
|
|
const states: Record<string, ParseProgress['status']> = { PENDING: 'pending', RUNNING: 'processing', SUCCESS: 'completed', FAILED: 'failed' }
|
|
async function progress(taskId: string): Promise<MindmapOrganizeProgress> {
|
|
const id = ids.get(taskId)
|
|
if (!id) return { progress: 0, status: 'pending', stage: '准备中', error: null, file_names: '' }
|
|
const task = await hubCall('tasks', 'get', undefined, id)
|
|
return { progress: task.status === 'SUCCESS' ? 100 : Math.floor((task.steps || []).filter((s: any) => s.status === 'SUCCESS').length * 100 / Math.max(1, task.snapshot?.nodes?.length || 1)), status: states[task.status], stage: task.status === 'PENDING' ? '等待处理' : task.status === 'RUNNING' ? '处理中' : task.status === 'SUCCESS' ? '完成' : '失败', error: task.error, file_names: task.name, result_md: task.result?.markdown }
|
|
}
|
|
export const documentApi = {
|
|
async parseDocument(params: ParseParams, taskId: string = uuid()): Promise<ParseResult> {
|
|
if (!params.parser_resource_id) throw new Error('请选择解析服务')
|
|
const results: ParseResult['results'] = {}
|
|
for (const file of params.files) {
|
|
const task = await hubCall('tasks', 'post', { name: file.name.slice(0, 128), nodes: [{ type: 'parse', resource_id: params.parser_resource_id }, { type: 'render' }] }, undefined, file)
|
|
ids.set(taskId, task.id)
|
|
const until = Date.now() + 30 * 60 * 1000
|
|
let completed = false
|
|
while (Date.now() < until) {
|
|
const current = await hubCall('tasks', 'get', undefined, task.id)
|
|
if (current.status === 'FAILED') throw new Error(current.error || '解析失败')
|
|
if (current.status === 'SUCCESS') { results[file.name] = { md_content: current.result.markdown, middle_json: JSON.stringify(current.result.source) }; completed = true; break }
|
|
await new Promise(resolve => setTimeout(resolve, 1500))
|
|
}
|
|
if (!completed) throw new Error('任务仍在处理中,请到管理页查看')
|
|
}
|
|
return { backend: 'shared', version: '1', results }
|
|
},
|
|
async createParseTask(_taskId: string): Promise<ParseProgress> { return { progress: 0, stage: '准备中', status: 'pending', error: null, file_names: '' } },
|
|
getParseProgress: progress,
|
|
async createMindmapTask(taskId: string, markdown: string, _mode = 'smart', prompt?: string, config?: { model_id: string; temperature: number }): Promise<MindmapOrganizeProgress> {
|
|
if (!config?.model_id) throw new Error('请选择整理模型')
|
|
const task = await hubCall('tasks', 'post', { name: '智能整理', markdown, nodes: [{ type: 'organize', model_id: config.model_id, prompt, temperature: config.temperature }, { type: 'render' }] })
|
|
ids.set(taskId, task.id)
|
|
return progress(taskId)
|
|
},
|
|
getMindmapProgress: progress
|
|
}
|