diff --git a/apps/system_manage/ai_hub.py b/apps/system_manage/ai_hub.py index 98c09bf67..b6035da39 100644 --- a/apps/system_manage/ai_hub.py +++ b/apps/system_manage/ai_hub.py @@ -152,12 +152,24 @@ def serialize(obj, detail=False, user=None): from system_manage.ai_hub_progress import task_progress data.update(status=obj.status, steps=obj.steps, error=obj.error, attempt=obj.attempt, started_at=obj.started_at, finished_at=obj.finished_at) + if 'snapshot' not in obj.get_deferred_fields(): + data['artifact_id'] = (obj.snapshot.get('workbench_artifact') or {}).get('id') if detail: data.update(snapshot=obj.snapshot, result=obj.result, progress=task_progress(obj)) if any(n.get('type') == 'organize' for n in obj.snapshot.get('nodes', [])): data['summary_input'] = {key: obj.input_data[key] for key in ('markdown', 'heading_structure') if key in obj.input_data} elif isinstance(obj, AIArtifact): - data.update(revision=obj.revision, layout=obj.layout, task_id=str(obj.task_id) if obj.task_id else None) + data.update(revision=obj.revision, layout=obj.layout, task_id=str(obj.task_id) if obj.task_id else None, + task_status=obj.task.status if obj.task_id else None, task_error=obj.task.error if obj.task_id else '') + meta = next((x for x in reversed(obj.history or []) if isinstance(x, dict) and x.get('_type') == 'workbench_tasks_v1'), {}) + task_ids = [meta.get('parse_task_id'), meta.get('summary_task_id')] + states = {str(pk): status for pk, status in AITask.objects.filter( + id__in=[x for x in task_ids if x]).values_list('id', 'status')} + conflict = bool(obj.task_id and meta.get('conflict_task_id') == str(obj.task_id)) + if conflict: + states[str(obj.task_id)] = 'CONFLICT' + data.update(file_name=meta.get('file_name', ''), parse_status=states.get(task_ids[0]), + summary_status=states.get(task_ids[1]), task_result_conflict=conflict) if detail: from system_manage.heading_structure import artifact_state, version_history, summary_state data.update(original=obj.original, markdown=obj.markdown, history=version_history(obj), heading_structure=artifact_state(obj), summary_state=summary_state(obj)) @@ -401,12 +413,49 @@ def submit(workspace, user, data, upload=None, service=None, request=None): except ValueError as exc: raise ValidationError(str(exc)) from None resource_slots[key] = slots - return AITask.objects.create(workspace_id=workspace, owner_id=user.id, + task = AITask.objects.create(workspace_id=workspace, owner_id=user.id, name=text(data.get('name') or (upload.name if upload else '导图任务'), '名称', limit=128), service=service, snapshot={'nodes': snapshot, 'resources': required, 'resource_slots': resource_slots, 'priority': priority, 'flow_id': str(service.flow_id) if service else data.get('flow_id'), 'flow_revision': service.flow.revision if service else None}, input_data={'markdown': markdown, **heading_input}, file_name=upload.name[:255] if upload else '', file_data=upload.read() if upload else None) + if 'workbench_artifact' in data: + spec = data['workbench_artifact'] + if not isinstance(spec, dict) or set(spec) - {'id', 'revision', 'layout'}: + raise ValidationError('导图任务参数无效') + if spec.get('id'): + from rest_framework.serializers import UUIDField + artifact_id = UUIDField().run_validation(spec['id']) + artifact = AIArtifact.objects.select_for_update().filter( + pk=artifact_id, workspace_id=workspace, owner_id=user.id).first() + if not artifact: + raise ValidationError('导图不存在或无权访问') + if type(spec.get('revision')) is not int or spec['revision'] != artifact.revision: + raise ValidationError('内容已更新,请重新打开') + else: + from users.models import User + from system_manage.artifact_settings import limit + User.objects.select_for_update().get(pk=user.id) + maximum = limit() + if AIArtifact.objects.filter(workspace_id=workspace, owner_id=user.id).count() >= maximum: + raise ValidationError(f'已达到导图保存上限({maximum}条),请删除后再保存') + layout = spec.get('layout', 'tree') + if layout not in ('tree', 'radial', 'horizontal', 'vertical'): + raise ValidationError('布局无效') + artifact = AIArtifact.objects.create(workspace_id=workspace, owner_id=user.id, + name=task.name, layout=layout, original=heading_input.get('original', markdown), markdown=markdown) + artifact.task = task + meta = next((x.copy() for x in reversed(artifact.history or []) if isinstance(x, dict) and x.get('_type') == 'workbench_tasks_v1'), {'_type': 'workbench_tasks_v1'}) + meta.pop('conflict_task_id', None) + is_summary = any(node['type'] == 'organize' for node in snapshot) + meta['summary_task_id' if is_summary else 'parse_task_id'] = str(task.id) + if upload: + meta['file_name'] = upload.name[:255] + artifact.history = [x for x in (artifact.history or []) if not isinstance(x, dict) or x.get('_type') != 'workbench_tasks_v1'] + [meta] + artifact.save(update_fields=['task', 'history', 'updated_at']) + task.snapshot['workbench_artifact'] = {'id': str(artifact.id), 'revision': artifact.revision} + task.save(update_fields=['snapshot', 'updated_at']) + return task @transaction.atomic diff --git a/apps/system_manage/ai_hub_progress.py b/apps/system_manage/ai_hub_progress.py index d12c31ec8..2eee32116 100644 --- a/apps/system_manage/ai_hub_progress.py +++ b/apps/system_manage/ai_hub_progress.py @@ -20,12 +20,16 @@ def parser_progress(task): def task_progress(task): elapsed = max(0, int(((task.finished_at or timezone.now()) - (task.started_at or task.created_at)).total_seconds())) + section_detail = next((step.get('progress', {}) for step in reversed(task.steps or []) + if 'partial_markdown' in (step.get('progress') or {})), {}) + section_fields = {key: section_detail[key] for key in + ('partial_markdown', 'completed_sections', 'total_sections') if key in section_detail} if task.status == 'SUCCESS': - return {'stage': '完成', 'percent': 100, 'elapsed_seconds': elapsed} + return {'stage': '完成', 'percent': 100, 'elapsed_seconds': elapsed, **section_fields} if task.status == 'PENDING': return {'stage': '等待任务处理', 'percent': None, 'elapsed_seconds': elapsed} if task.status == 'FAILED': - return {'stage': '处理失败', 'percent': None, 'elapsed_seconds': elapsed} + return {'stage': '处理失败', 'percent': None, 'elapsed_seconds': elapsed, **section_fields} current = next((step for step in task.steps if step.get('status') == 'RUNNING'), {}) detail = current.get('progress') or {} percent = detail.get('percent') @@ -35,4 +39,4 @@ def task_progress(task): percent = min(99, int((complete_steps * 100 + percent) / total_steps)) return {'stage': detail.get('stage') or LABELS.get(current.get('type'), '处理中'), 'percent': percent, 'elapsed_seconds': elapsed, - **({'plan': detail['plan']} if isinstance(detail.get('plan'), dict) else {})} + **section_fields, **({'plan': detail['plan']} if isinstance(detail.get('plan'), dict) else {})} diff --git a/apps/system_manage/ai_hub_worker.py b/apps/system_manage/ai_hub_worker.py index 8ebad058d..90b23fd5a 100644 --- a/apps/system_manage/ai_hub_worker.py +++ b/apps/system_manage/ai_hub_worker.py @@ -131,6 +131,8 @@ def execute(task): task.refresh_from_db(fields=['result']) task.result.update(original=original, markdown=markdown, source=source, tree=tree) task.save(update_fields=['steps', 'result', 'updated_at']) + if task.snapshot.get('workbench_artifact') and not markdown.strip(): + raise ValueError('处理结果为空,请检查文档内容后重试') task.status = 'SUCCESS' except Exception as exc: if steps: @@ -147,5 +149,45 @@ def execute(task): task.status = 'FAILED' task.steps = steps task.finished_at = timezone.now() - task.save(update_fields=['status', 'error', 'steps', 'finished_at', 'updated_at']) + with transaction.atomic(): + task.save(update_fields=['status', 'error', 'steps', 'finished_at', 'updated_at']) + if task.status == 'SUCCESS': + persist_workbench_result(task) return task + + +def persist_workbench_result(task): + """Publish only to the unchanged artifact that still points at this task.""" + from system_manage.models.ai_hub import AIArtifact + from system_manage.heading_structure import store_artifact_state, store_summary_state + spec = task.snapshot.get('workbench_artifact') or {} + if not spec.get('id'): + return + artifact = AIArtifact.objects.select_for_update().filter( + pk=spec['id'], workspace_id=task.workspace_id, owner_id=task.owner_id, + task_id=task.id).first() + if not artifact: + return + if artifact.revision != spec.get('revision'): + meta = next((x.copy() for x in reversed(artifact.history or []) if isinstance(x, dict) + and x.get('_type') == 'workbench_tasks_v1'), {'_type': 'workbench_tasks_v1'}) + meta['conflict_task_id'] = str(task.id) + artifact.history = [x for x in (artifact.history or []) if not isinstance(x, dict) + or x.get('_type') != 'workbench_tasks_v1'] + [meta] + artifact.save(update_fields=['history', 'updated_at']) + return + result = task.result or {} + markdown = result.get('markdown', '') + if not markdown: + return + is_summary = any(n.get('type') == 'organize' for n in task.snapshot.get('nodes', [])) + if is_summary: + store_summary_state(artifact, {'source_markdown': task.input_data.get('markdown', ''), + 'smart_markdown': markdown, 'mode': 'smart'}) + artifact.original = result.get('original', artifact.original) + artifact.markdown = markdown + structure = (result.get('source') or {}).get('heading_structure') + if structure: + store_artifact_state(artifact, structure) + artifact.revision += 1 + artifact.save(update_fields=['original', 'markdown', 'history', 'revision', 'updated_at']) diff --git a/apps/system_manage/views/ai_hub.py b/apps/system_manage/views/ai_hub.py index 904640641..88e2516ca 100644 --- a/apps/system_manage/views/ai_hub.py +++ b/apps/system_manage/views/ai_hub.py @@ -75,7 +75,8 @@ class HubView(APIView): query = query.filter(Q(snapshot__engine__isnull=True) | Q(snapshot__engine=False)) query = query.defer('file_data', 'input_data', 'snapshot', 'result') elif kind == 'artifacts': - query = query.defer('original', 'markdown', 'history') + query = query.select_related('task').defer('original', 'markdown', 'task__file_data', + 'task__input_data', 'task__snapshot', 'task__result', 'task__steps') if any(key in request.query_params for key in ('mine', 'current_page', 'page_size', 'name')): mine = request.query_params.get('mine', 'false') if mine not in ('true', 'false'): diff --git a/ui/src/views/mindmap/workbench/api/document.ts b/ui/src/views/mindmap/workbench/api/document.ts index b9759616a..6e5e824b4 100644 --- a/ui/src/views/mindmap/workbench/api/document.ts +++ b/ui/src/views/mindmap/workbench/api/document.ts @@ -19,6 +19,7 @@ export interface ParseParams { export interface ParseResult { task_id?: string + artifact_id?: string backend: string version: string results: Record< @@ -44,6 +45,9 @@ export interface ParseProgress { } export interface MindmapOrganizeProgress extends ParseProgress { + partial_markdown?: string + completed_sections?: number + total_sections?: number plan?: Record task_id?: string result_md?: string | null @@ -83,6 +87,9 @@ async function progress(taskId: string): Promise { file_names: task.name, task_id: task.id, result_md: task.result?.markdown, + partial_markdown: task.progress?.partial_markdown, + completed_sections: task.progress?.completed_sections, + total_sections: task.progress?.total_sections, 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, @@ -115,7 +122,7 @@ export const documentApi = { async previewSummary(markdown: string, node: Record) { return hubCall('summary_preview', 'post', { markdown, nodes: [node, { type: 'render' }] }) }, - async parseDocument(params: ParseParams, taskId: string = uuid()): Promise { + async parseDocument(params: ParseParams, taskId: string = uuid(), onCreated?: (task: any) => void, isCancelled?: () => boolean): Promise { const results: ParseResult['results'] = {} for (const file of params.files) { const native = ['doc', 'docx', 'xls', 'xlsx', 'csv', 'html', 'htm', 'md', 'txt'].includes( @@ -135,15 +142,23 @@ export const documentApi = { const task = await hubCall( 'tasks', 'post', - { name: file.name.slice(0, 128), nodes: [node, { type: 'render' }] }, + { name: file.name.slice(0, 128), nodes: [node, { type: 'render' }], workbench_artifact: {} }, undefined, file, ) ids.set(taskId, task.id) - const until = Date.now() + 30 * 60 * 1000 + onCreated?.(task) + if (isCancelled?.()) return { backend: 'shared', version: '1', results } let completed = false - while (Date.now() < until) { - const current = await hubCall('tasks', 'get', undefined, task.id) + while (!isCancelled?.()) { + if (isCancelled?.()) return { backend: 'shared', version: '1', results } + let current + try { current = await hubCall('tasks', 'get', undefined, task.id) } + catch { + await new Promise((resolve) => setTimeout(resolve, 1500)) + continue + } + if (isCancelled?.()) return { backend: 'shared', version: '1', results } if (current.status === 'FAILED') throw new Error(current.error || '解析失败') if (current.status === 'SUCCESS') { results[file.name] = { @@ -171,7 +186,7 @@ export const documentApi = { _mode = 'smart', prompt?: string, config?: { model_id: string; temperature?: number | null; options?: SummaryOptions }, - context?: { heading_structure?: any; original?: string }, + context?: { heading_structure?: any; original?: string; workbench_artifact?: { id: string; revision: number } }, ): Promise { if (!config?.model_id) throw new Error('请选择总结模型') const node: Record = { type: 'organize', model_id: config.model_id, prompt } diff --git a/ui/src/views/mindmap/workbench/api/hub.ts b/ui/src/views/mindmap/workbench/api/hub.ts index f63d294b8..f0e3e257d 100644 --- a/ui/src/views/mindmap/workbench/api/hub.ts +++ b/ui/src/views/mindmap/workbench/api/hub.ts @@ -16,9 +16,14 @@ export async function hubCall(kind: string, method = 'get', data?: any, id?: str export function onOpenArtifact(callback: (artifact: any) => void) { const route = useRoute() - return watch(() => route.query.artifact, async id => { + let generation = 0 + const stop = watch(() => route.query.artifact, async id => { + const current = ++generation if (typeof id !== 'string' || !/^[a-f0-9-]{36}$/i.test(id)) return - try { callback(await hubCall('artifacts', 'get', undefined, id)) } - catch { ElMessage.error('导图不存在或无权访问') } + try { + const artifact = await hubCall('artifacts', 'get', undefined, id) + if (generation === current) callback(artifact) + } catch { if (generation === current) ElMessage.error('导图不存在或无权访问') } }, { immediate: true }) + return () => { generation++; stop() } } diff --git a/ui/src/views/mindmap/workbench/components/ConfigPanel.vue b/ui/src/views/mindmap/workbench/components/ConfigPanel.vue index 087f7165a..fb170923d 100644 --- a/ui/src/views/mindmap/workbench/components/ConfigPanel.vue +++ b/ui/src/views/mindmap/workbench/components/ConfigPanel.vue @@ -26,6 +26,7 @@ > + 长度单位为 Token,留空使用默认值。 diff --git a/ui/src/views/mindmap/workbench/composables/useDocumentProcessor.ts b/ui/src/views/mindmap/workbench/composables/useDocumentProcessor.ts index f22f9a529..ec5bc70d0 100644 --- a/ui/src/views/mindmap/workbench/composables/useDocumentProcessor.ts +++ b/ui/src/views/mindmap/workbench/composables/useDocumentProcessor.ts @@ -327,21 +327,21 @@ export function useDocumentProcessor() { } // 处理文档转换 - const processDocument = async () => { + let processGeneration = 0 + const cancelProcessing = () => { + processGeneration++ + stopProgressPolling() + isProcessing.value = false + processingStage.value = '' + } + const processDocument = async (onCreated?: (task: any, current: boolean) => void) => { + const generation = ++processGeneration error.value = null if (uploadedFiles.value.length === 0) { error.value = '请先上传文件' return } - const firstFileName = uploadedFiles.value[0]?.name.toLowerCase() || '' - if (firstFileName.endsWith('.md')) { - if (!results.value) { - error.value = 'Markdown 文件内容为空,请重新上传' - } - return - } - isProcessing.value = true error.value = null processingStage.value = '准备提交解析任务' @@ -351,6 +351,7 @@ export function useDocumentProcessor() { try { await documentApi.createParseTask(taskId) + if (generation !== processGeneration) return startProgressPolling(taskId) processingStage.value = '提交文档到解析服务' progressStage.value = '提交文档' @@ -376,7 +377,10 @@ export function useDocumentProcessor() { } processingStage.value = '解析文档' - const response = await documentApi.parseDocument(params, taskId) + const response = await documentApi.parseDocument(params, taskId, (task) => { + onCreated?.(task, generation === processGeneration) + }, () => generation !== processGeneration) + if (generation !== processGeneration) return if (response.results) { stopProgressPolling() @@ -399,11 +403,14 @@ export function useDocumentProcessor() { } } catch (err: any) { + if (generation !== processGeneration) return error.value = err.message || '转换失败' } finally { - stopProgressPolling() - processingStage.value = '' - isProcessing.value = false + if (generation === processGeneration) { + stopProgressPolling() + processingStage.value = '' + isProcessing.value = false + } } } @@ -456,6 +463,7 @@ export function useDocumentProcessor() { initializeManualResult, clearAll, processDocument, + cancelProcessing, getFormulaLabel, getFormulaInfo } diff --git a/ui/src/views/mindmap/workbench/views/DocumentProcessor.vue b/ui/src/views/mindmap/workbench/views/DocumentProcessor.vue index 779e14e76..3a5c71254 100644 --- a/ui/src/views/mindmap/workbench/views/DocumentProcessor.vue +++ b/ui/src/views/mindmap/workbench/views/DocumentProcessor.vue @@ -11,9 +11,9 @@

思维导图工作台

- 我的导图 + 我的导图 保存导图 - + 设置
@@ -29,7 +29,7 @@ @dragleave="isDragging = false" @click="triggerUpload" > -
+
文件导入
@@ -38,6 +38,7 @@
+
{{ openedArtifact.file_name }}
{{ file.name }} @@ -77,6 +78,7 @@
+