UnisKB/ui/src/views/mindmap/workbench/api/document.ts

200 lines
6.9 KiB
TypeScript

import { hubCall } from './hub'
import { uuid } from '@/views/mindmap/workbench/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 {
task_id?: string
backend: string
version: string
results: Record<
string,
{
md_content?: string
middle_json?: string
model_output?: string
content_list?: string
images?: Record<string, string>
original_md?: string
heading_structure?: any
}
>
}
export interface ParseProgress {
progress: number | null
stage: string
status: 'pending' | 'processing' | 'completed' | 'failed'
error: string | null
file_names: string
}
export interface MindmapOrganizeProgress extends ParseProgress {
plan?: Record<string, any>
task_id?: string
result_md?: string | null
heading_structure?: any
original_md?: string
sources?: any[]
warnings?: string[]
summary_input?: { markdown?: string; heading_structure?: any }
summary_node?: Record<string, any>
}
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) || (/^[a-f0-9-]{36}$/i.test(taskId) ? taskId : '')
if (!id)
return { progress: null, status: 'pending', stage: '准备中', error: null, file_names: '' }
const task = await hubCall('tasks', 'get', undefined, id)
return {
progress: task.status === 'SUCCESS' ? 100 : (task.progress?.percent ?? null),
status: states[task.status],
stage:
task.progress?.stage ||
(task.status === 'PENDING'
? '等待处理'
: task.status === 'RUNNING'
? '处理中'
: task.status === 'SUCCESS'
? '完成'
: '失败'),
error: task.error,
file_names: task.name,
task_id: task.id,
result_md: task.result?.markdown,
heading_structure: task.result?.source?.heading_structure,
original_md: task.result?.original,
plan: task.progress?.plan || task.result?.source?.organization || task.result?.summary_state?.metrics,
sources: task.result?.source?.summary_sources || [],
warnings: task.result?.source?.summary_warnings || [],
summary_input: task.summary_input,
summary_node: task.snapshot?.nodes?.find((node: any) => node.type === 'organize'),
}
}
export interface SummaryOptions {
context_tokens: number | null
max_input_tokens: number | null
input_tokens: number | null
max_output_tokens: number | null
output_tokens: number | null
safety_tokens: number | null
top_p: number | null
timeout: number
retries: number
concurrency: number
max_depth: number
max_children: number
node_max_chars: number
detail: 'brief' | 'standard' | 'detailed'
structure_mode: 'preserve' | 'reorganize'
parallel: boolean
auto_split: boolean
}
export const documentApi = {
async previewSummary(markdown: string, node: Record<string, unknown>) {
return hubCall('summary_preview', 'post', { markdown, nodes: [node, { type: 'render' }] })
},
async parseDocument(params: ParseParams, taskId: string = uuid()): Promise<ParseResult> {
const results: ParseResult['results'] = {}
for (const file of params.files) {
const native = ['doc', 'docx', 'xls', 'xlsx', 'csv', 'html', 'htm', 'md', 'txt'].includes(
file.name.split('.').pop()?.toLowerCase() || '',
)
if (
params.parser_resource_id === 'builtin' &&
!native &&
!file.name.toLowerCase().endsWith('.pdf')
)
throw new Error('内置解析不支持图片,请选择 MinerU 解析服务')
if (!native && !params.parser_resource_id) throw new Error('请选择 PDF/图片解析服务')
const node =
native || params.parser_resource_id === 'builtin'
? { type: 'parse', processor: 'builtin' }
: { type: 'parse', resource_id: params.parser_resource_id }
const task = await hubCall(
'tasks',
'post',
{ name: file.name.slice(0, 128), nodes: [node, { 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),
original_md: current.result.original,
heading_structure: current.result.source?.heading_structure,
}
completed = true
break
}
await new Promise((resolve) => setTimeout(resolve, 1500))
}
if (!completed) throw new Error('任务仍在处理中,请到管理页查看')
}
return { backend: 'shared', version: '1', results, task_id: ids.get(taskId) }
},
async createParseTask(_taskId: string): Promise<ParseProgress> {
return { progress: null, 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 | null; options?: SummaryOptions },
context?: { heading_structure?: any; original?: string },
): Promise<MindmapOrganizeProgress> {
if (!config?.model_id) throw new Error('请选择总结模型')
const node: Record<string, unknown> = { type: 'organize', model_id: config.model_id, prompt }
if (config.temperature !== null && config.temperature !== undefined)
node.temperature = config.temperature
Object.entries(config.options || {}).forEach(([key, value]) => {
if (value !== null && value !== undefined) node[key] = value
})
const task = await hubCall('tasks', 'post', {
name: '智能总结',
markdown,
nodes: [node, { type: 'render' }],
...context,
})
ids.set(taskId, task.id)
return { progress: null, status: 'pending', stage: '等待任务处理', error: null,
file_names: task.name || '智能总结', task_id: task.id }
},
getMindmapProgress: progress,
async retryMindmapTask(taskId: string) {
const id = ids.get(taskId) || (/^[a-f0-9-]{36}$/i.test(taskId) ? taskId : '')
if (!id) throw new Error('任务已失效,请重新总结')
return hubCall('tasks', 'post', {}, id)
},
}