feat:优化使用体验
parent
2baba0b19c
commit
5434504f9b
|
|
@ -183,6 +183,10 @@ class BaseChatStep(IChatStep):
|
|||
tool_ids=None,
|
||||
mcp_output_enable=True,
|
||||
**kwargs):
|
||||
from system_manage.mcp_agent import context_for
|
||||
enabled = (model_setting or {}).get('engine_tools', [])
|
||||
self.context['engine_body'] = {**manage.context, 'workspace_id': workspace_id, 'chat_id': str(chat_id)}
|
||||
self.context['engine_tools'] = enabled
|
||||
chat_model = get_model_instance_by_model_workspace_id(model_id, workspace_id,
|
||||
**model_params_setting) if model_id is not None else None
|
||||
if stream:
|
||||
|
|
@ -208,7 +212,9 @@ class BaseChatStep(IChatStep):
|
|||
code_path = f'{executor.sandbox_path}/execute/{tool_id}.py'
|
||||
if os.path.exists(code_path):
|
||||
os.remove(code_path)
|
||||
from system_manage.mcp_agent import call_records
|
||||
return {
|
||||
'engine_calls': call_records(self.context.get('engine_body', {})),
|
||||
'step_type': 'chat_step',
|
||||
'run_time': self.context['run_time'],
|
||||
'model_id': str(manage.context['model_id']),
|
||||
|
|
@ -231,25 +237,15 @@ class BaseChatStep(IChatStep):
|
|||
|
||||
def _handle_mcp_request(self, mcp_enable, tool_enable, mcp_source, mcp_servers, mcp_tool_ids, tool_ids,
|
||||
mcp_output_enable, chat_model, message_list):
|
||||
if not mcp_enable and not tool_enable:
|
||||
if not mcp_enable and not tool_enable and not self.context.get('engine_tools'):
|
||||
return None
|
||||
|
||||
mcp_servers_config = {}
|
||||
|
||||
# 迁移过来mcp_source是None
|
||||
if mcp_source is None:
|
||||
mcp_source = 'custom'
|
||||
if mcp_enable:
|
||||
# 兼容老数据
|
||||
if not mcp_tool_ids:
|
||||
mcp_tool_ids = []
|
||||
if mcp_source == 'custom' and mcp_servers is not None and '"stdio"' not in mcp_servers:
|
||||
mcp_servers_config = json.loads(mcp_servers)
|
||||
elif mcp_tool_ids:
|
||||
mcp_tools = QuerySet(Tool).filter(id__in=mcp_tool_ids).values()
|
||||
for mcp_tool in mcp_tools:
|
||||
if mcp_tool and mcp_tool['is_active']:
|
||||
mcp_servers_config = {**mcp_servers_config, **json.loads(mcp_tool['code'])}
|
||||
from system_manage.mcp_agent import context_for
|
||||
engine_context = context_for(self.context['engine_body'], engine_tools=self.context.get('engine_tools'),
|
||||
tool_ids=mcp_tool_ids if mcp_enable and mcp_source not in (None, 'custom') else [],
|
||||
custom=mcp_servers if mcp_enable and mcp_source in (None, 'custom') else None)
|
||||
|
||||
if tool_enable:
|
||||
if tool_ids and len(tool_ids) > 0: # 如果有工具ID,则将其转换为MCP
|
||||
|
|
@ -269,8 +265,8 @@ class BaseChatStep(IChatStep):
|
|||
self.context['execute_ids'].append(_id)
|
||||
mcp_servers_config[str(tool.id)] = tool_config
|
||||
|
||||
if len(mcp_servers_config) > 0:
|
||||
return mcp_response_generator(chat_model, message_list, json.dumps(mcp_servers_config), mcp_output_enable)
|
||||
if mcp_servers_config or engine_context['tool_ids'] or engine_context['custom'] or engine_context['engine_tools']:
|
||||
return mcp_response_generator(chat_model, message_list, json.dumps(mcp_servers_config), mcp_output_enable, engine_context)
|
||||
|
||||
return None
|
||||
|
||||
|
|
@ -332,6 +328,7 @@ class BaseChatStep(IChatStep):
|
|||
no_references_setting, problem_text, mcp_enable, mcp_tool_ids, mcp_servers, mcp_source, tool_enable, tool_ids,
|
||||
mcp_output_enable)
|
||||
chat_record_id = uuid.uuid7()
|
||||
self.context.get('engine_body', {})['chat_record_id'] = str(chat_record_id)
|
||||
r = StreamingHttpResponse(
|
||||
streaming_content=event_content(chat_result, chat_id, chat_record_id, paragraph_list,
|
||||
post_response_handler, manage, self, chat_model, message_list, problem_text,
|
||||
|
|
@ -375,7 +372,8 @@ class BaseChatStep(IChatStep):
|
|||
chat_model, message_list,
|
||||
)
|
||||
if mcp_result:
|
||||
return mcp_result, True
|
||||
from application.flow.tools import collect_mcp_response
|
||||
return collect_mcp_response(mcp_result), True
|
||||
return chat_model.invoke(message_list), True
|
||||
|
||||
def execute_block(self, message_list: List[BaseMessage],
|
||||
|
|
@ -401,6 +399,7 @@ class BaseChatStep(IChatStep):
|
|||
reasoning = Reasoning(reasoning_content_start,
|
||||
reasoning_content_end)
|
||||
chat_record_id = uuid.uuid7()
|
||||
self.context.get('engine_body', {})['chat_record_id'] = str(chat_record_id)
|
||||
# 调用模型
|
||||
try:
|
||||
chat_result, is_ai_chat = self.get_block_result(message_list, chat_model, paragraph_list,
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@ from .loop_continue_node import BaseLoopContinueNode
|
|||
from .loop_node import *
|
||||
from .loop_start_node import *
|
||||
from .mcp_node import BaseMcpNode
|
||||
from .component_node import BaseComponentNode
|
||||
from .parameter_extraction_node import BaseParameterExtractionNode
|
||||
from .question_node import *
|
||||
from .reranker_node import *
|
||||
|
|
@ -42,7 +43,7 @@ node_list = [BaseStartStepNode, BaseChatNode, BaseSearchKnowledgeNode, BaseSearc
|
|||
BaseToolNodeNode, BaseToolLibNodeNode, BaseRerankerNode, BaseApplicationNode,
|
||||
BaseDocumentExtractNode,
|
||||
BaseImageUnderstandNode, BaseFormNode, BaseSpeechToTextNode, BaseTextToSpeechNode,
|
||||
BaseImageGenerateNode, BaseVariableAssignNode, BaseMcpNode, BaseTextToVideoNode, BaseImageToVideoNode,
|
||||
BaseImageGenerateNode, BaseVariableAssignNode, BaseMcpNode, BaseComponentNode, BaseTextToVideoNode, BaseImageToVideoNode,
|
||||
BaseVideoUnderstandNode,
|
||||
BaseIntentNode, BaseLoopNode, BaseLoopStartStepNode,
|
||||
BaseLoopContinueNode,
|
||||
|
|
|
|||
|
|
@ -206,29 +206,23 @@ class BaseChatNode(IChatNode):
|
|||
|
||||
def _handle_mcp_request(self, mcp_enable, tool_enable, mcp_source, mcp_servers, mcp_tool_id, mcp_tool_ids, tool_ids,
|
||||
mcp_output_enable, chat_model, message_list, history_message, question):
|
||||
if not mcp_enable and not tool_enable:
|
||||
enabled = (self.node_params.get('model_setting') or {}).get('engine_tools', [])
|
||||
if not mcp_enable and not tool_enable and not enabled:
|
||||
return None
|
||||
|
||||
mcp_servers_config = {}
|
||||
|
||||
# 迁移过来mcp_source是None
|
||||
if mcp_source is None:
|
||||
mcp_source = 'custom'
|
||||
if mcp_enable:
|
||||
# 兼容老数据
|
||||
if not mcp_tool_ids:
|
||||
mcp_tool_ids = []
|
||||
if mcp_tool_id:
|
||||
mcp_tool_ids = list(set(mcp_tool_ids + [mcp_tool_id]))
|
||||
if mcp_source == 'custom' and mcp_servers is not None and '"stdio"' not in mcp_servers:
|
||||
mcp_servers_config = json.loads(mcp_servers)
|
||||
mcp_servers_config = self.handle_variables(mcp_servers_config)
|
||||
elif mcp_tool_ids:
|
||||
mcp_tools = QuerySet(Tool).filter(id__in=mcp_tool_ids).values()
|
||||
for mcp_tool in mcp_tools:
|
||||
if mcp_tool and mcp_tool['is_active']:
|
||||
mcp_servers_config = {**mcp_servers_config, **json.loads(mcp_tool['code'])}
|
||||
mcp_servers_config = self.handle_variables(mcp_servers_config)
|
||||
from system_manage.mcp_agent import context_for
|
||||
ids = list(mcp_tool_ids or [])
|
||||
if mcp_tool_id: ids.append(mcp_tool_id)
|
||||
body = dict(self.workflow_manage.get_body())
|
||||
# Explicit AI-node selection is limited to knowledge bases associated with this workflow.
|
||||
from system_manage.mcp_workflows import knowledge_scope
|
||||
body['knowledge_id_list'] = knowledge_scope(self.workflow_manage.flow)
|
||||
engine_context = context_for(body, engine_tools=enabled,
|
||||
tool_ids=ids if mcp_enable and mcp_source not in (None, 'custom') else [],
|
||||
custom=self.handle_variables(json.loads(mcp_servers) if isinstance(mcp_servers, str) else mcp_servers) if mcp_enable and mcp_source in (None, 'custom') and mcp_servers else None,
|
||||
transform=self.handle_variables, node_id=self.id)
|
||||
|
||||
if tool_enable:
|
||||
if tool_ids and len(tool_ids) > 0: # 如果有工具ID,则将其转换为MCP
|
||||
|
|
@ -248,8 +242,8 @@ class BaseChatNode(IChatNode):
|
|||
self.context['execute_ids'].append(_id)
|
||||
mcp_servers_config[str(tool.id)] = tool_config
|
||||
|
||||
if len(mcp_servers_config) > 0:
|
||||
r = mcp_response_generator(chat_model, message_list, json.dumps(mcp_servers_config), mcp_output_enable)
|
||||
if mcp_servers_config or engine_context['tool_ids'] or engine_context['custom'] or enabled:
|
||||
r = mcp_response_generator(chat_model, message_list, json.dumps(mcp_servers_config), mcp_output_enable, engine_context)
|
||||
return NodeResult(
|
||||
{'result': r, 'chat_model': chat_model, 'message_list': message_list,
|
||||
'history_message': [{'content': message.content, 'role': message.type} for message in
|
||||
|
|
@ -315,7 +309,9 @@ class BaseChatNode(IChatNode):
|
|||
code_path = f'{executor.sandbox_path}/execute/{tool_id}.py'
|
||||
if os.path.exists(code_path):
|
||||
os.remove(code_path)
|
||||
from system_manage.mcp_agent import call_records
|
||||
return {
|
||||
'engine_calls': call_records(self.workflow_manage.get_body(), self.id),
|
||||
'name': self.node.properties.get('stepName'),
|
||||
"index": index,
|
||||
'run_time': self.context.get('run_time'),
|
||||
|
|
|
|||
|
|
@ -9,12 +9,16 @@ from application.flow.i_step_node import INode, NodeResult
|
|||
|
||||
|
||||
class McpNodeSerializer(serializers.Serializer):
|
||||
mcp_servers = serializers.JSONField(required=True, label=_("Mcp servers"))
|
||||
mcp_servers = serializers.JSONField(required=False, default=dict, label=_("Mcp servers"))
|
||||
mcp_server = serializers.CharField(required=True, label=_("Mcp server"))
|
||||
mcp_tool = serializers.CharField(required=True, label=_("Mcp tool"))
|
||||
mcp_tool_id = serializers.CharField(required=False, label=_("Mcp tool"), allow_null=True, allow_blank=True)
|
||||
mcp_source = serializers.CharField(required=False, label=_("Mcp source"), allow_blank=True, allow_null=True)
|
||||
tool_params = serializers.DictField(required=True, label=_("Tool parameters"))
|
||||
tool_form_field = serializers.ListField(required=False, default=list)
|
||||
timeout = serializers.IntegerField(default=60, min_value=1, max_value=600)
|
||||
retries = serializers.IntegerField(default=0, min_value=0, max_value=2)
|
||||
priority = serializers.IntegerField(default=5, min_value=0, max_value=9)
|
||||
|
||||
|
||||
class IMcpNode(INode):
|
||||
|
|
|
|||
|
|
@ -1,75 +1,59 @@
|
|||
# coding=utf-8
|
||||
import asyncio
|
||||
import json
|
||||
from copy import deepcopy
|
||||
from typing import List
|
||||
|
||||
from django.db.models import QuerySet
|
||||
from langchain_mcp_adapters.client import MultiServerMCPClient
|
||||
|
||||
from rest_framework.exceptions import ValidationError
|
||||
from application.flow.i_step_node import NodeResult
|
||||
from application.flow.step_node.mcp_node.i_mcp_node import IMcpNode
|
||||
from tools.models import Tool
|
||||
from application.flow.step_node.component_node.base_component_node import workflow_identity
|
||||
from system_manage import mcp_runtime
|
||||
|
||||
|
||||
class BaseMcpNode(IMcpNode):
|
||||
def save_context(self, details, workflow_manage):
|
||||
self.context['result'] = details.get('result')
|
||||
self.context['tool_params'] = details.get('tool_params')
|
||||
self.context['mcp_tool'] = details.get('mcp_tool')
|
||||
self.context.update({key: details.get(key) for key in ('result', 'text', 'structured', 'tool_params', 'mcp_tool', 'mcp_server', 'attempts', 'task_id')})
|
||||
|
||||
def execute(self, mcp_servers, mcp_server, mcp_tool, mcp_tool_id, mcp_source, tool_params,**kwargs) -> NodeResult:
|
||||
if mcp_source == 'referencing':
|
||||
if not mcp_tool_id:
|
||||
raise ValueError("MCP tool ID is required when mcp_source is 'referencing'.")
|
||||
tool = QuerySet(Tool).filter(id=mcp_tool_id).first()
|
||||
if not tool:
|
||||
raise ValueError(f"Tool with ID {mcp_tool_id} not found.")
|
||||
if not tool.is_active:
|
||||
raise ValueError(f"Tool with ID {mcp_tool_id} is inactive.")
|
||||
servers = json.loads(tool.code)
|
||||
servers = self.handle_variables(servers) # 处理servers中的变量
|
||||
params = json.loads(json.dumps(tool_params))
|
||||
params = self.handle_variables(params)
|
||||
else:
|
||||
servers = json.loads(mcp_servers)
|
||||
servers = self.handle_variables(servers) # 处理servers中的变量
|
||||
params = json.loads(json.dumps(tool_params))
|
||||
params = self.handle_variables(params)
|
||||
def execute(self, mcp_servers, mcp_server, mcp_tool, tool_params, mcp_tool_id=None, mcp_source=None,
|
||||
tool_form_field=None, timeout=60, retries=0, priority=5, **kwargs):
|
||||
body, user = workflow_identity(self.workflow_manage)
|
||||
params = self.handle_variables(deepcopy(tool_params), tool_form_field or [])
|
||||
key = 'mcp:' + str(mcp_tool_id) if mcp_source == 'referencing' else 'mcp-custom'
|
||||
result = mcp_runtime.invoke(body['workspace_id'], user, key,
|
||||
{'servers': self.handle_variables(mcp_runtime.connections(mcp_servers)) if key == 'mcp-custom' else None,
|
||||
'server': mcp_server, 'tool': mcp_tool, 'arguments': params},
|
||||
parent=body.get('engine_task_id'), application_id=body['application_id'], chat_id=body.get('chat_id'),
|
||||
scope={**body, 'node_id': self.id, 'source': 'workflow-mcp'}, timeout=timeout, retries=retries, priority=priority)
|
||||
return NodeResult({**result, 'tool_params': params}, {})
|
||||
|
||||
async def call_tool(t, a):
|
||||
client = MultiServerMCPClient(servers)
|
||||
async with client.session(mcp_server) as s:
|
||||
return await s.call_tool(t, a)
|
||||
|
||||
res = asyncio.run(call_tool(mcp_tool, params))
|
||||
return NodeResult(
|
||||
{'result': [content.text for content in res.content], 'tool_params': params, 'mcp_tool': mcp_tool}, {})
|
||||
|
||||
def handle_variables(self, tool_params):
|
||||
# 处理参数中的变量
|
||||
for k, v in tool_params.items():
|
||||
if type(v) == str:
|
||||
tool_params[k] = self.workflow_manage.generate_prompt(tool_params[k])
|
||||
if type(v) == dict:
|
||||
self.handle_variables(v)
|
||||
if (type(v) == list) and (type(v[0]) == str):
|
||||
tool_params[k] = self.get_reference_content(v)
|
||||
return tool_params
|
||||
def handle_variables(self, value, fields=None):
|
||||
if isinstance(value, dict):
|
||||
output = {}
|
||||
for key, item in value.items():
|
||||
field = next((f for f in fields or [] if f.get('field') == key), {})
|
||||
if field.get('source') == 'custom' and field.get('input_type') == 'JsonInput' and isinstance(item, str):
|
||||
try: output[key] = json.loads(self.workflow_manage.generate_prompt(item))
|
||||
except ValueError: raise ValidationError('工具参数必须为有效 JSON') from None
|
||||
elif field.get('source') == 'custom':
|
||||
output[key] = self.workflow_manage.generate_prompt(item) if isinstance(item, str) else item
|
||||
elif field.get('source') == 'referencing':
|
||||
if not isinstance(item, list) or len(item) < 2: raise ValidationError('请选择工具参数的上游变量')
|
||||
output[key] = self.get_reference_content(item)
|
||||
else:
|
||||
output[key] = self.handle_variables(item, fields)
|
||||
return output
|
||||
if isinstance(value, str): return self.workflow_manage.generate_prompt(value)
|
||||
if isinstance(value, list):
|
||||
known = set(self.workflow_manage.flow.node_map) | {'global', 'chat', 'loop'}
|
||||
if len(value) >= 2 and all(isinstance(v, str) for v in value) and value[0] in known:
|
||||
return self.get_reference_content(value)
|
||||
return [self.handle_variables(item, fields) for item in value]
|
||||
return value
|
||||
|
||||
def get_reference_content(self, fields: List[str]):
|
||||
return self.workflow_manage.get_reference_field(
|
||||
fields[0],
|
||||
fields[1:])
|
||||
return self.workflow_manage.get_reference_field(fields[0], fields[1:])
|
||||
|
||||
def get_details(self, index: int, **kwargs):
|
||||
return {
|
||||
'name': self.node.properties.get('stepName'),
|
||||
"index": index,
|
||||
'run_time': self.context.get('run_time'),
|
||||
'status': self.status,
|
||||
'err_message': self.err_message,
|
||||
'type': self.node.type,
|
||||
'mcp_tool': self.context.get('mcp_tool'),
|
||||
'tool_params': self.context.get('tool_params'),
|
||||
'result': self.context.get('result'),
|
||||
}
|
||||
return {'name': self.node.properties.get('stepName'), 'index': index,
|
||||
'run_time': self.context.get('run_time'), 'status': self.status,
|
||||
'err_message': self.err_message, 'type': self.node.type,
|
||||
**{key: self.context.get(key) for key in ('mcp_tool', 'mcp_server', 'tool_params', 'result', 'text', 'structured', 'attempts', 'task_id')}}
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ import threading
|
|||
from typing import Iterator
|
||||
|
||||
from django.http import StreamingHttpResponse
|
||||
from langchain_core.messages import BaseMessageChunk, BaseMessage, ToolMessage, AIMessageChunk
|
||||
from langchain_core.messages import BaseMessageChunk, BaseMessage, ToolMessage, AIMessageChunk, AIMessage, SystemMessage
|
||||
from langchain_mcp_adapters.client import MultiServerMCPClient
|
||||
from langgraph.prebuilt import create_react_agent
|
||||
from application.flow.i_step_node import WorkFlowPostHandler
|
||||
|
|
@ -267,17 +267,23 @@ def get_global_loop():
|
|||
return _global_loop
|
||||
|
||||
|
||||
async def _yield_mcp_response(chat_model, message_list, mcp_servers, mcp_output_enable=True):
|
||||
client = MultiServerMCPClient(json.loads(mcp_servers))
|
||||
tools = await client.get_tools()
|
||||
async def _yield_mcp_response(chat_model, message_list, mcp_servers, mcp_output_enable=True, engine_context=None):
|
||||
servers = json.loads(mcp_servers)
|
||||
tools = await MultiServerMCPClient(servers).get_tools() if servers else []
|
||||
if engine_context:
|
||||
from system_manage.mcp_agent import tools as engine_tools
|
||||
tools += await engine_tools(engine_context)
|
||||
agent = create_react_agent(chat_model, tools)
|
||||
response = agent.astream({"messages": message_list}, stream_mode='messages')
|
||||
messages = message_list
|
||||
if not any(isinstance(message, SystemMessage) for message in message_list):
|
||||
messages = [SystemMessage(content='你是应用助手。根据用户请求选择已授权工具。需要外部数据或生成图谱时实际调用工具,不要编造工具结果。工具执行完成后依据结果回答,保存导图后返回打开链接,避免重复调用。'), *message_list]
|
||||
response = agent.astream({"messages": messages}, stream_mode='messages')
|
||||
|
||||
# 用于存储工具调用信息
|
||||
tool_calls_info = {}
|
||||
|
||||
async for chunk in response:
|
||||
if isinstance(chunk[0], AIMessageChunk):
|
||||
if isinstance(chunk[0], (AIMessageChunk, AIMessage)):
|
||||
tool_calls = chunk[0].additional_kwargs.get('tool_calls', [])
|
||||
for tool_call in tool_calls:
|
||||
tool_id = tool_call.get('id', '')
|
||||
|
|
@ -287,6 +293,10 @@ async def _yield_mcp_response(chat_model, message_list, mcp_servers, mcp_output_
|
|||
'name': tool_call.get('function', {}).get('name', ''),
|
||||
'input': tool_call.get('function', {}).get('arguments', '')
|
||||
}
|
||||
for tool_call in chunk[0].tool_calls:
|
||||
if tool_call.get('id'):
|
||||
tool_calls_info[tool_call['id']] = {'name': tool_call['name'],
|
||||
'input': json.dumps(tool_call.get('args', {}), ensure_ascii=False)}
|
||||
yield chunk[0]
|
||||
|
||||
if mcp_output_enable and isinstance(chunk[0], ToolMessage):
|
||||
|
|
@ -303,14 +313,30 @@ async def _yield_mcp_response(chat_model, message_list, mcp_servers, mcp_output_
|
|||
yield chunk[0]
|
||||
|
||||
|
||||
def mcp_response_generator(chat_model, message_list, mcp_servers, mcp_output_enable=True):
|
||||
def collect_mcp_response(response):
|
||||
"""Use the same tool stream for the non-streaming chat API."""
|
||||
from langchain_core.messages import AIMessage
|
||||
parts, reasoning, metadata = [], [], {}
|
||||
for chunk in response:
|
||||
if isinstance(chunk.content, str):
|
||||
parts.append(chunk.content)
|
||||
else:
|
||||
parts.extend(item.get('text', '') for item in chunk.content if isinstance(item, dict))
|
||||
metadata.update(chunk.response_metadata)
|
||||
value = chunk.additional_kwargs.get('reasoning_content')
|
||||
if value: reasoning.append(value)
|
||||
if reasoning: metadata['reasoning_content'] = ''.join(reasoning)
|
||||
return AIMessage(content=''.join(parts), response_metadata=metadata)
|
||||
|
||||
|
||||
def mcp_response_generator(chat_model, message_list, mcp_servers, mcp_output_enable=True, engine_context=None):
|
||||
"""使用全局事件循环,不创建新实例"""
|
||||
result_queue = queue.Queue()
|
||||
loop = get_global_loop() # 使用共享循环
|
||||
|
||||
async def _run():
|
||||
try:
|
||||
async_gen = _yield_mcp_response(chat_model, message_list, mcp_servers, mcp_output_enable)
|
||||
async_gen = _yield_mcp_response(chat_model, message_list, mcp_servers, mcp_output_enable, engine_context)
|
||||
async for chunk in async_gen:
|
||||
result_queue.put(('data', chunk))
|
||||
except Exception as e:
|
||||
|
|
|
|||
|
|
@ -151,6 +151,8 @@ class ModelKnowledgeAssociation(serializers.Serializer):
|
|||
|
||||
|
||||
class ModelSettingSerializer(serializers.Serializer):
|
||||
engine_tools = serializers.ListField(required=False, child=serializers.ChoiceField(
|
||||
choices=['knowledge-search', 'mindmap-save']), max_length=2)
|
||||
prompt = serializers.CharField(required=False, allow_null=True, allow_blank=True, max_length=102400,
|
||||
label=_("Prompt word"))
|
||||
system = serializers.CharField(required=False, allow_null=True, allow_blank=True, max_length=102400,
|
||||
|
|
@ -677,21 +679,8 @@ class ApplicationOperateSerializer(serializers.Serializer):
|
|||
if with_valid:
|
||||
self.is_valid(raise_exception=True)
|
||||
McpServersSerializer(data=instance).is_valid(raise_exception=True)
|
||||
servers = json.loads(instance.get('mcp_servers'))
|
||||
for server, config in servers.items():
|
||||
if config.get('transport') not in ['sse', 'streamable_http']:
|
||||
raise AppApiException(500, _('Only support transport=sse or transport=streamable_http'))
|
||||
tools = []
|
||||
for server in servers:
|
||||
tools += [
|
||||
{
|
||||
'server': server,
|
||||
'name': tool.name,
|
||||
'description': tool.description,
|
||||
'args_schema': tool.args_schema,
|
||||
}
|
||||
for tool in asyncio.run(get_mcp_tools({server: servers[server]}))]
|
||||
return tools
|
||||
from system_manage.mcp_runtime import discover
|
||||
return discover(instance.get('mcp_servers'))
|
||||
|
||||
def delete(self, with_valid=True):
|
||||
if with_valid:
|
||||
|
|
|
|||
|
|
@ -136,7 +136,7 @@ class OpenView(APIView):
|
|||
enforce(request, 'application', application_id)
|
||||
return result.success(OpenChatSerializers(
|
||||
data={'workspace_id': workspace_id, 'application_id': application_id,
|
||||
'chat_user_id': str(uuid.uuid7()), 'chat_user_type': ChatUserType.ANONYMOUS_USER,
|
||||
'chat_user_id': str(request.user.id), 'chat_user_type': ChatUserType.PLATFORM_USER.value,
|
||||
'debug': True}).open())
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -8,6 +8,9 @@
|
|||
"""
|
||||
|
||||
import os
|
||||
import logging
|
||||
import threading
|
||||
from pathlib import Path
|
||||
|
||||
class MKTokenizer:
|
||||
def __init__(self, tokenizer):
|
||||
|
|
@ -17,14 +20,38 @@ class MKTokenizer:
|
|||
return self.tokenizer.encode(text).ids
|
||||
|
||||
|
||||
class ByteTokenEstimate:
|
||||
"""Conservative token budget when an offline tokenizer is unavailable.
|
||||
|
||||
Provider-reported usage remains authoritative. This only supplies local
|
||||
estimates for prompt budgeting; it never downloads model files.
|
||||
"""
|
||||
def encode(self, text):
|
||||
return list(text.encode('utf-8'))
|
||||
|
||||
|
||||
class TokenizerManage:
|
||||
tokenizer = None
|
||||
_lock = threading.Lock()
|
||||
|
||||
@staticmethod
|
||||
def get_tokenizer():
|
||||
from tokenizers import Tokenizer
|
||||
# 创建Tokenizer
|
||||
model_path = os.path.join("/opt/maxkb-app", "model", "tokenizer", "models--bert-base-cased")
|
||||
with open(f"{model_path}/refs/main", encoding="utf-8") as f: snapshot = f.read()
|
||||
TokenizerManage.tokenizer = Tokenizer.from_file(f"{model_path}/snapshots/{snapshot}/tokenizer.json")
|
||||
return MKTokenizer(TokenizerManage.tokenizer)
|
||||
if TokenizerManage.tokenizer is not None:
|
||||
return TokenizerManage.tokenizer
|
||||
with TokenizerManage._lock:
|
||||
if TokenizerManage.tokenizer is not None:
|
||||
return TokenizerManage.tokenizer
|
||||
from tokenizers import Tokenizer
|
||||
model_path = Path(os.environ.get('MAXKB_TOKENIZER_PATH') or
|
||||
'/opt/maxkb-app/model/tokenizer/models--bert-base-cased')
|
||||
try:
|
||||
path = model_path if model_path.is_file() else model_path / 'tokenizer.json'
|
||||
if not path.is_file():
|
||||
snapshot = (model_path / 'refs/main').read_text(encoding='utf-8').strip()
|
||||
path = model_path / 'snapshots' / snapshot / 'tokenizer.json'
|
||||
TokenizerManage.tokenizer = MKTokenizer(Tokenizer.from_file(str(path)))
|
||||
except Exception:
|
||||
# A missing/corrupt tokenizer must not prevent remote-model chat.
|
||||
logging.getLogger(__name__).warning('本地分词器不可用,使用保守字节估算;实际用量以模型返回为准')
|
||||
TokenizerManage.tokenizer = ByteTokenEstimate()
|
||||
return TokenizerManage.tokenizer
|
||||
|
|
|
|||
|
|
@ -54,7 +54,7 @@ class XlsxSplitHandle(BaseSplitHandle):
|
|||
try:
|
||||
if type(limit) is str:
|
||||
limit = int(limit)
|
||||
workbook = openpyxl.load_workbook(io.BytesIO(buffer))
|
||||
workbook = openpyxl.load_workbook(io.BytesIO(buffer), data_only=True)
|
||||
try:
|
||||
image_dict: dict = xlsx_embed_cells_images(io.BytesIO(buffer))
|
||||
save_image([item for item in image_dict.values()])
|
||||
|
|
|
|||
|
|
@ -37,6 +37,7 @@ def validate_params(params):
|
|||
|
||||
def knowledge_params(knowledge):
|
||||
params = validate_params((knowledge.meta or {}).get('embedding_params') or {})
|
||||
params.setdefault('normalize', True)
|
||||
return {key: value for key, value in params.items()
|
||||
if key in {'batch_size', 'normalize', 'query_prefix', 'document_prefix'}}
|
||||
|
||||
|
|
@ -48,7 +49,7 @@ class ConfiguredEmbedding(Embeddings):
|
|||
self.batch_size = params.get('batch_size') or getattr(model, 'batch_size', 10) or 10
|
||||
|
||||
def _vector(self, vector):
|
||||
if not self.params.get('normalize'):
|
||||
if not self.params.get('normalize', True):
|
||||
return vector
|
||||
norm = math.sqrt(sum(float(v) ** 2 for v in vector))
|
||||
return [float(v) / norm for v in vector] if norm else vector
|
||||
|
|
|
|||
|
|
@ -1057,7 +1057,7 @@ class DocumentSerializers(serializers.Serializer):
|
|||
knowledge = QuerySet(Knowledge).filter(id=self.data.get('knowledge_id')).first()
|
||||
segmentation = (knowledge.meta or {}).get('segmentation') if knowledge is not None else None
|
||||
segmentation = segmentation or {}
|
||||
default_limit = segmentation.get('chunk_size') or 4096
|
||||
default_limit = segmentation.get('chunk_size') or 500
|
||||
default_overlap = segmentation.get('chunk_overlap') or 0
|
||||
limit = instance.get("limit", None) or default_limit
|
||||
overlap = instance.get("overlap", None)
|
||||
|
|
@ -1740,7 +1740,7 @@ class DocumentSerializers(serializers.Serializer):
|
|||
data={'workspace_id': self.data.get('workspace_id'), 'knowledge_id': self.data.get('knowledge_id')}
|
||||
)
|
||||
split_serializer.is_valid(instance={'file': [file]}, raise_exception=True)
|
||||
parsed_documents = split_serializer.file_to_paragraph(file, None, None, 4096)
|
||||
parsed_documents = split_serializer.file_to_paragraph(file, None, None, 500)
|
||||
paragraphs = []
|
||||
for parsed_document in parsed_documents:
|
||||
paragraphs.extend(parsed_document.get('content', []))
|
||||
|
|
|
|||
|
|
@ -346,9 +346,12 @@ class KnowledgeSerializer(serializers.Serializer):
|
|||
)
|
||||
)
|
||||
), with_search_one=True)
|
||||
from knowledge.embedding_guard import embedding_change_reason
|
||||
return {
|
||||
**knowledge_dict,
|
||||
'meta': json.loads(knowledge_dict.get('meta', '{}')),
|
||||
'embedding_change_block_reason': embedding_change_reason(
|
||||
QuerySet(Knowledge).get(id=self.data.get('knowledge_id'))),
|
||||
'application_id_list': list(filter(
|
||||
lambda application_id: all_application_list.__contains__(application_id),
|
||||
[
|
||||
|
|
@ -371,6 +374,23 @@ class KnowledgeSerializer(serializers.Serializer):
|
|||
).exclude(id=knowledge.id).exists():
|
||||
raise AppApiException(500, _('Knowledge base name duplicate!'))
|
||||
KnowledgeEditRequest(data=instance).is_valid(knowledge=knowledge)
|
||||
from knowledge.embedding_params import knowledge_params, validate_params
|
||||
from knowledge.embedding_guard import embedding_change_reason
|
||||
from types import SimpleNamespace
|
||||
current_params = knowledge_params(knowledge)
|
||||
try:
|
||||
params = validate_params(instance['embedding_params']) if 'embedding_params' in instance else None
|
||||
except ValueError as exc:
|
||||
raise AppApiException(500, str(exc))
|
||||
new_params = knowledge_params(SimpleNamespace(meta={'embedding_params': params})) if params is not None else current_params
|
||||
model_changed = ('embedding_model_id' in instance and
|
||||
str(instance['embedding_model_id'] or '') != str(knowledge.embedding_model_id or ''))
|
||||
vector_params_changed = ({k: v for k, v in current_params.items() if k != 'batch_size'} !=
|
||||
{k: v for k, v in new_params.items() if k != 'batch_size'})
|
||||
if model_changed or vector_params_changed:
|
||||
reason = embedding_change_reason(knowledge)
|
||||
if reason:
|
||||
raise AppApiException(500, reason)
|
||||
if 'embedding_model_id' in instance:
|
||||
knowledge.embedding_model_id = instance.get('embedding_model_id')
|
||||
if "name" in instance:
|
||||
|
|
@ -386,11 +406,6 @@ class KnowledgeSerializer(serializers.Serializer):
|
|||
incoming_meta[managed_key] = knowledge.meta[managed_key]
|
||||
knowledge.meta = incoming_meta
|
||||
if 'embedding_params' in instance:
|
||||
from knowledge.embedding_params import validate_params
|
||||
try:
|
||||
params = validate_params(instance['embedding_params'])
|
||||
except ValueError as exc:
|
||||
raise AppApiException(500, str(exc))
|
||||
knowledge.meta = {**(knowledge.meta or {}), 'embedding_params': params}
|
||||
if 'folder_id' in instance:
|
||||
knowledge.folder_id = instance.get('folder_id')
|
||||
|
|
|
|||
|
|
@ -166,7 +166,7 @@ class KnowledgeSegmentationSerializer(serializers.Serializer):
|
|||
保存后新建文档上传时默认使用该规则,上传时可覆盖。
|
||||
"""
|
||||
|
||||
DEFAULTS = {"chunk_size": 4096, "chunk_overlap": 0}
|
||||
DEFAULTS = {"chunk_size": 500, "chunk_overlap": 0}
|
||||
|
||||
@staticmethod
|
||||
def one(workspace_id: str, knowledge_id: str):
|
||||
|
|
|
|||
|
|
@ -54,19 +54,29 @@ class EmbeddingParamsTests(unittest.TestCase):
|
|||
params = {'领域': '电力', 'dimensions': 512, '备注': '', 'batch_size': 2, 'query_prefix': '检索:'}
|
||||
self.assertEqual(validate_params(params), params)
|
||||
active = knowledge_params(SimpleNamespace(meta={'embedding_params': params}))
|
||||
self.assertEqual(active, {'batch_size': 2, 'query_prefix': '检索:'})
|
||||
self.assertEqual(active, {'batch_size': 2, 'query_prefix': '检索:', 'normalize': True})
|
||||
base = RecordingModel()
|
||||
model = ConfiguredEmbedding(base, active)
|
||||
model.embed_query('问题')
|
||||
self.assertEqual(base.calls, [('query', '检索:问题')])
|
||||
|
||||
def test_legacy_and_empty_params_keep_original_behavior(self):
|
||||
self.assertEqual(knowledge_params(SimpleNamespace(meta={})), {})
|
||||
def test_missing_normalization_defaults_to_unit_vectors(self):
|
||||
self.assertEqual(knowledge_params(SimpleNamespace(meta={})), {'normalize': True})
|
||||
self.assertEqual(knowledge_params(SimpleNamespace(meta={'embedding_params': {'normalize': None}})),
|
||||
{'normalize': True})
|
||||
self.assertEqual(validate_params({'batch_size': None, 'query_prefix': ''}), {})
|
||||
base = RecordingModel()
|
||||
self.assertEqual(ConfiguredEmbedding(base, {}).embed_query('x'), [3., 4.])
|
||||
self.assertEqual(ConfiguredEmbedding(base, {}).embed_query('x'), [.6, .8])
|
||||
self.assertEqual(ConfiguredEmbedding(base, {}).embed_documents(['x']), [[.6, .8]])
|
||||
self.assertEqual(ConfiguredEmbedding(base, {}).embed_documents([]), [])
|
||||
|
||||
def test_explicit_false_keeps_original_vectors(self):
|
||||
params = knowledge_params(SimpleNamespace(meta={'embedding_params': {'normalize': False}}))
|
||||
self.assertEqual(params, {'normalize': False})
|
||||
model = ConfiguredEmbedding(RecordingModel(), params)
|
||||
self.assertEqual(model.embed_query('x'), [3., 4.])
|
||||
self.assertEqual(model.embed_documents(['x']), [[3., 4.]])
|
||||
|
||||
def test_zero_vector_is_safe(self):
|
||||
base = RecordingModel()
|
||||
base.embed_query = lambda _: [0., 0.]
|
||||
|
|
|
|||
|
|
@ -152,6 +152,11 @@ class MetadataTests(TestCase):
|
|||
|
||||
def test_upload_inherits_defaults_and_accepts_explicit_override(self):
|
||||
from knowledge.serializers.document import DocumentSerializers
|
||||
self.assertEqual(Rules.one('default', str(self.k.id))['chunk_size'], 500)
|
||||
serializer = DocumentSerializers.Split(data={'knowledge_id':str(self.k.id),'workspace_id':'default'})
|
||||
with patch.object(serializer,'file_to_paragraph',return_value=[]) as split:
|
||||
serializer.parse({'file':[SimpleUploadedFile('default.txt',b'test')]})
|
||||
self.assertEqual(split.call_args.args[-2:], (500,0))
|
||||
Rules.update('default', str(self.k.id), self.user, {'chunk_size':100,'chunk_overlap':20})
|
||||
serializer = DocumentSerializers.Split(data={'knowledge_id':str(self.k.id),'workspace_id':'default'})
|
||||
with patch.object(serializer,'file_to_paragraph',return_value=[]) as split:
|
||||
|
|
|
|||
|
|
@ -97,8 +97,11 @@ class MaxKBBaseModel(ABC):
|
|||
|
||||
@staticmethod
|
||||
def filter_optional_params(model_kwargs):
|
||||
from system_manage.summary_plan import LOCAL_PARAMETERS
|
||||
optional_params = {}
|
||||
for key, value in model_kwargs.items():
|
||||
if key in LOCAL_PARAMETERS and key != 'timeout':
|
||||
continue
|
||||
if key not in ['model_id', 'use_local', 'streaming', 'show_ref_label', 'stream']:
|
||||
if key == 'extra_body' and isinstance(value, dict):
|
||||
optional_params = {**optional_params, **value}
|
||||
|
|
|
|||
|
|
@ -14,6 +14,9 @@ class BaiLianChatModel(MaxKBBaseModel, BaseChatOpenAI):
|
|||
@staticmethod
|
||||
def new_instance(model_type, model_name, model_credential: Dict[str, object], **model_kwargs):
|
||||
optional_params = MaxKBBaseModel.filter_optional_params(model_kwargs)
|
||||
# Client controls belong to the SDK, not the provider's JSON body.
|
||||
client_params = {key: optional_params.pop(key) for key in
|
||||
('temperature', 'top_p', 'max_tokens', 'timeout', 'max_retries') if key in optional_params}
|
||||
# if 'qwen-omni-turbo' in model_name or 'qwq' in model_name:
|
||||
# optional_params['streaming'] = True
|
||||
return BaiLianChatModel(
|
||||
|
|
@ -21,5 +24,7 @@ class BaiLianChatModel(MaxKBBaseModel, BaseChatOpenAI):
|
|||
openai_api_base=model_credential.get('api_base'),
|
||||
openai_api_key=model_credential.get('api_key'),
|
||||
streaming=True,
|
||||
stream_usage=True,
|
||||
**client_params,
|
||||
extra_body=optional_params
|
||||
)
|
||||
|
|
|
|||
|
|
@ -36,6 +36,8 @@ class OllamaChatModel(MaxKBBaseModel, ChatOllama):
|
|||
api_base = model_credential.get('api_base', '')
|
||||
base_url = get_base_url(api_base)
|
||||
optional_params = MaxKBBaseModel.filter_optional_params(model_kwargs)
|
||||
from models_provider.context_policy import model_context
|
||||
optional_params['num_ctx'] = model_context(model_kwargs)
|
||||
|
||||
return OllamaChatModel(model=model_name, base_url=base_url,
|
||||
model_resource_id=str(model_kwargs['model_id']) if model_kwargs.get('model_id') else None,
|
||||
|
|
|
|||
|
|
@ -1,11 +1,13 @@
|
|||
"""Model file assets, independent of runtime/model connection records. Never executes model code."""
|
||||
import json
|
||||
import hashlib
|
||||
import os
|
||||
import shutil
|
||||
import stat
|
||||
import tempfile
|
||||
import uuid
|
||||
import zipfile
|
||||
from contextlib import contextmanager
|
||||
from datetime import datetime, timezone
|
||||
from pathlib import Path, PurePosixPath
|
||||
|
||||
|
|
@ -18,6 +20,42 @@ class ImportError(ValueError):
|
|||
pass
|
||||
|
||||
|
||||
def model_fingerprint(directory):
|
||||
"""Hash relative paths and complete file contents, independent of the archive name."""
|
||||
root = Path(directory)
|
||||
digest = hashlib.sha256()
|
||||
for file in sorted((p for p in root.rglob('*') if p.is_file()), key=lambda p: p.relative_to(root).as_posix()):
|
||||
name = file.relative_to(root).as_posix().encode('utf-8')
|
||||
digest.update(len(name).to_bytes(8, 'big'))
|
||||
digest.update(name)
|
||||
digest.update(file.stat().st_size.to_bytes(8, 'big'))
|
||||
with file.open('rb') as source:
|
||||
for chunk in iter(lambda: source.read(1024 * 1024), b''):
|
||||
digest.update(chunk)
|
||||
return digest.hexdigest()
|
||||
|
||||
|
||||
@contextmanager
|
||||
def import_lock(root):
|
||||
"""Serialize duplicate checks and record creation across Web processes."""
|
||||
with (root / '.import.lock').open('a+b') as lock:
|
||||
if os.name == 'nt':
|
||||
import msvcrt
|
||||
lock.seek(0)
|
||||
msvcrt.locking(lock.fileno(), msvcrt.LK_LOCK, 1)
|
||||
else:
|
||||
import fcntl
|
||||
fcntl.flock(lock.fileno(), fcntl.LOCK_EX)
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
if os.name == 'nt':
|
||||
lock.seek(0)
|
||||
msvcrt.locking(lock.fileno(), msvcrt.LK_UNLCK, 1)
|
||||
else:
|
||||
fcntl.flock(lock.fileno(), fcntl.LOCK_UN)
|
||||
|
||||
|
||||
def inspect_model(directory):
|
||||
root = Path(directory)
|
||||
if not root.is_dir() or root.is_symlink():
|
||||
|
|
@ -98,14 +136,33 @@ class ModelImportStore:
|
|||
self.root = Path(root)
|
||||
self.root.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
def list(self):
|
||||
def _records(self):
|
||||
records = []
|
||||
for path in self.root.glob('*/import.json'):
|
||||
if path.parent.name.startswith('.import-'):
|
||||
continue
|
||||
try:
|
||||
records.append(json.loads(path.read_text(encoding='utf-8')))
|
||||
except (ValueError, OSError):
|
||||
continue
|
||||
return sorted(records, key=lambda r: r['created_at'], reverse=True)[:200]
|
||||
return sorted(records, key=lambda r: r.get('created_at', ''), reverse=True)
|
||||
|
||||
def list(self):
|
||||
records, paths, fingerprints = [], set(), set()
|
||||
for record in self._records():
|
||||
path = record.get('path')
|
||||
canonical = os.path.normcase(os.path.realpath(path)) if path else None
|
||||
fingerprint = record.get('fingerprint') if record.get('source') == 'UPLOAD' else None
|
||||
if (canonical and canonical in paths) or (fingerprint and fingerprint in fingerprints):
|
||||
continue
|
||||
if canonical:
|
||||
paths.add(canonical)
|
||||
if fingerprint:
|
||||
fingerprints.add(fingerprint)
|
||||
records.append(record)
|
||||
if len(records) == 200:
|
||||
break
|
||||
return records
|
||||
|
||||
def create(self, owner_id, path=None, upload=None):
|
||||
if (path is None) == (upload is None):
|
||||
|
|
@ -163,15 +220,37 @@ class ModelImportStore:
|
|||
model_dir = Path(path).resolve(strict=True)
|
||||
info = inspect_model(model_dir)
|
||||
final_path, source_type, display_name = model_dir, 'PATH', model_dir.name
|
||||
record = {
|
||||
'id': identity, 'name': display_name, 'path': str(final_path),
|
||||
'source': source_type, 'owner_id': str(owner_id),
|
||||
'created_at': datetime.now(timezone.utc).isoformat(),
|
||||
'status': 'IMPORTED', **info,
|
||||
}
|
||||
(staging / 'import.json').write_text(json.dumps(record, ensure_ascii=False), encoding='utf-8')
|
||||
staging.rename(self.root / identity)
|
||||
return record
|
||||
with import_lock(self.root):
|
||||
existing = self._records()
|
||||
if source_type == 'PATH':
|
||||
canonical = os.path.normcase(os.path.realpath(model_dir))
|
||||
for record in existing:
|
||||
if record.get('path') and os.path.normcase(os.path.realpath(record['path'])) == canonical:
|
||||
return {**record, 'already_imported': True}
|
||||
fingerprint = model_fingerprint(model_dir)
|
||||
for record in existing:
|
||||
if (record.get('size_bytes') != info['size_bytes'] or
|
||||
record.get('file_count') != info['file_count']):
|
||||
continue
|
||||
previous_path = record.get('path')
|
||||
if not previous_path or not Path(previous_path).is_dir():
|
||||
continue
|
||||
try:
|
||||
inspect_model(previous_path)
|
||||
previous = model_fingerprint(previous_path)
|
||||
except (OSError, ImportError):
|
||||
continue
|
||||
if previous == fingerprint:
|
||||
return {**record, 'already_imported': True}
|
||||
record = {
|
||||
'id': identity, 'name': display_name, 'path': str(final_path),
|
||||
'source': source_type, 'owner_id': str(owner_id),
|
||||
'created_at': datetime.now(timezone.utc).isoformat(),
|
||||
'status': 'IMPORTED', 'fingerprint': fingerprint, **info,
|
||||
}
|
||||
(staging / 'import.json').write_text(json.dumps(record, ensure_ascii=False), encoding='utf-8')
|
||||
staging.rename(self.root / identity)
|
||||
return record
|
||||
except (FileNotFoundError, PermissionError) as exc:
|
||||
raise ImportError('目录不存在或无访问权限') from exc
|
||||
finally:
|
||||
|
|
|
|||
|
|
@ -105,5 +105,54 @@ class ImportTests(unittest.TestCase):
|
|||
for kwargs in ({}, {'path':str(self.model),'upload':Upload([])}):
|
||||
with self.assertRaises(ImportError):self.store.create('owner',**kwargs)
|
||||
|
||||
def test_repeated_path_and_historical_records_are_deduplicated(self):
|
||||
first = self.store.create('owner', path=str(self.model))
|
||||
repeated = self.store.create('owner', path=str(self.model / '..' / 'model'))
|
||||
self.assertEqual(repeated['id'], first['id'])
|
||||
self.assertTrue(repeated['already_imported'])
|
||||
legacy = self.store.root / 'legacy'
|
||||
legacy.mkdir()
|
||||
(legacy / 'import.json').write_text(json.dumps({**first, 'id': 'legacy', 'created_at': '2000'}))
|
||||
self.assertEqual([row['id'] for row in self.store.list()], [first['id']])
|
||||
|
||||
def test_zip_content_deduplicates_and_distinct_content_is_retained(self):
|
||||
files = [('repo/config.json', '{"model_type":"bert"}'), ('repo/model.safetensors', b'weights')]
|
||||
first = self.store.create('owner', upload=Upload(files))
|
||||
second = self.store.create('owner', upload=Upload(list(reversed(files))))
|
||||
self.assertEqual(first['id'], second['id'])
|
||||
self.assertTrue(second['already_imported'])
|
||||
different = self.store.create('owner', upload=Upload([
|
||||
('repo/config.json', '{"model_type":"bert"}'), ('repo/model.safetensors', b'weights2')]))
|
||||
self.assertNotEqual(first['id'], different['id'])
|
||||
self.assertEqual(len(self.store.list()), 2)
|
||||
|
||||
def test_different_directory_and_zip_with_same_files_reuse_record(self):
|
||||
first = self.store.create('owner', path=str(self.model))
|
||||
copy = self.root / 'copy'
|
||||
import shutil
|
||||
shutil.copytree(self.model, copy)
|
||||
second = self.store.create('owner', path=str(copy))
|
||||
self.assertEqual(first['id'], second['id'])
|
||||
zipped = Upload([('repo/config.json', (copy / 'config.json').read_bytes()),
|
||||
('repo/model.safetensors', (copy / 'model.safetensors').read_bytes())])
|
||||
self.assertEqual(self.store.create('owner', upload=zipped)['id'], first['id'])
|
||||
|
||||
def test_concurrent_imports_create_one_record(self):
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
with ThreadPoolExecutor(max_workers=4) as pool:
|
||||
results = list(pool.map(lambda _: self.store.create('owner', path=str(self.model)), range(4)))
|
||||
self.assertEqual(len({row['id'] for row in results}), 1)
|
||||
self.assertEqual(len(self.store._records()), 1)
|
||||
|
||||
def test_invalid_legacy_directory_does_not_block_import(self):
|
||||
first = self.store.create('owner', path=str(self.model))
|
||||
config = self.model / 'config.json'
|
||||
config.write_text('x' * len(config.read_text()))
|
||||
replacement = self.root / 'replacement'
|
||||
replacement.mkdir()
|
||||
(replacement / 'config.json').write_text(json.dumps({'model_type': 'bert', 'transformers_version': '4.45'}))
|
||||
(replacement / 'model.safetensors').write_bytes(b'test fixture, not runtime weights')
|
||||
self.assertNotEqual(self.store.create('owner', path=str(replacement))['id'], first['id'])
|
||||
|
||||
|
||||
if __name__ == '__main__':unittest.main()
|
||||
|
|
|
|||
|
|
@ -131,11 +131,15 @@ def get_model_default_params(model):
|
|||
return value
|
||||
return value
|
||||
|
||||
return {
|
||||
params = {
|
||||
p.get('field'): convert_to_int(p.get('default_value'))
|
||||
for p in model.model_params_form
|
||||
if p.get('default_value') is not None
|
||||
}
|
||||
if model.model_type == 'LLM':
|
||||
from models_provider.context_policy import model_context
|
||||
params['context_tokens'] = model_context(params)
|
||||
return params
|
||||
|
||||
|
||||
def get_model_instance_by_model_workspace_id(model_id, workspace_id, **kwargs):
|
||||
|
|
@ -147,4 +151,8 @@ def get_model_instance_by_model_workspace_id(model_id, workspace_id, **kwargs):
|
|||
"""
|
||||
model = get_model_by_id(model_id, workspace_id)
|
||||
s = get_model_default_params(model)
|
||||
return ModelManage.get_model(model_id, lambda _id: get_model(model, **{**s, **kwargs}))
|
||||
params = {**s, **kwargs}
|
||||
if model.model_type == 'LLM' and any(kwargs.get(key) is not None for key in ('context_tokens', 'num_ctx')):
|
||||
from models_provider.context_policy import model_context
|
||||
params['context_tokens'] = model_context(kwargs)
|
||||
return ModelManage.get_model(model_id, lambda _id: get_model(model, **params))
|
||||
|
|
|
|||
|
|
@ -144,19 +144,23 @@ def serialize(obj, detail=False, user=None):
|
|||
elif isinstance(obj, AIFlow):
|
||||
data.update(revision=obj.revision, nodes=obj.nodes)
|
||||
elif isinstance(obj, AIService):
|
||||
from system_manage.ai_hub_service_api import get_config
|
||||
data.update(flow_id=str(obj.flow_id), enabled=obj.enabled, allowed_users=obj.allowed_users,
|
||||
requests_per_minute=obj.requests_per_minute,
|
||||
key_configured=bool(obj.key_hash), url=f'ai_hub/published/{obj.id}/run')
|
||||
key_configured=bool(obj.key_hash), url=f'ai_hub/published/{obj.id}/run', api_config=get_config(obj.id))
|
||||
elif isinstance(obj, AITask):
|
||||
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 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)
|
||||
if detail:
|
||||
data.update(original=obj.original, markdown=obj.markdown, history=obj.history)
|
||||
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))
|
||||
return data
|
||||
|
||||
|
||||
|
|
@ -251,22 +255,24 @@ def validate_nodes(workspace, user, nodes):
|
|||
raise ValidationError('解析服务已停用')
|
||||
clean['resource_id'] = str(parser.id)
|
||||
if kind == 'organize':
|
||||
if node.get('operation') not in (None, 'heading_repair'):
|
||||
raise ValidationError('整理操作无效')
|
||||
if node.get('operation'):
|
||||
clean['operation'] = node['operation']
|
||||
clean['model_id'] = str(model_for(workspace, user, node.get('model_id')).id)
|
||||
clean['prompt'] = text(node.get('prompt', '保留事实、数值和原文语言,将以下文档整理成分级Markdown标题和要点。'), '提示词', limit=16000)
|
||||
temperature = node.get('temperature')
|
||||
if temperature is not None:
|
||||
if isinstance(temperature, bool) or not isinstance(temperature, (int, float)) or not 0 <= temperature <= 2:
|
||||
raise ValidationError('温度无效')
|
||||
clean['temperature'] = temperature
|
||||
# Absent parameters fall back to the per-model settings in model management.
|
||||
for field, low, high in (
|
||||
('context_tokens', 4096, 262144), ('output_tokens', 256, 32768),
|
||||
('safety_tokens', 256, 8192), ('timeout', 30, 600)):
|
||||
value = node.get(field)
|
||||
if value is not None:
|
||||
clean[field] = integer(value, low, high, '模型参数')
|
||||
if clean.get('output_tokens', 4096) + clean.get('safety_tokens', 1024) >= clean.get('context_tokens', 32768):
|
||||
raise ValidationError('输出与预留长度不能超过模型上下文')
|
||||
from system_manage.summary_plan import validate_options
|
||||
from system_manage.ai_hub_organize import resolve_settings
|
||||
from models_provider.tools import get_model_default_params
|
||||
from system_manage.mcp_business import summary_model_defaults, summary_supported_parameters
|
||||
try:
|
||||
clean.update(validate_options(node))
|
||||
model = model_for(workspace, user, clean['model_id'])
|
||||
resolve_settings(summary_model_defaults(model, get_model_default_params(model)), clean)
|
||||
if clean.get('top_p') is not None and 'top_p' not in summary_supported_parameters(model):
|
||||
raise ValueError('该模型未声明支持 top_p 参数')
|
||||
except ValueError as exc:
|
||||
raise ValidationError(str(exc)) from None
|
||||
result.append(clean)
|
||||
return result
|
||||
|
||||
|
|
@ -286,10 +292,14 @@ def service_create(workspace, user, data):
|
|||
require_admin(user)
|
||||
flow = one('flows', data.get('flow_id'), workspace, user, lock=True)
|
||||
validate_nodes(workspace, user, flow.nodes)
|
||||
return AIService.objects.create(workspace_id=workspace, owner_id=user.id,
|
||||
obj = AIService.objects.create(workspace_id=workspace, owner_id=user.id,
|
||||
name=text(data.get('name'), '名称', limit=128), flow=flow,
|
||||
allowed_users=users(data.get('allowed_users', [])),
|
||||
requests_per_minute=integer(data.get('requests_per_minute', 20), 1, 600, '调用频率'))
|
||||
if 'api_config' in data:
|
||||
from system_manage.ai_hub_service_api import save_config
|
||||
save_config(obj.id, data['api_config'])
|
||||
return obj
|
||||
|
||||
|
||||
def make_snapshot(workspace, user, nodes):
|
||||
|
|
@ -324,6 +334,10 @@ def submit(workspace, user, data, upload=None, service=None, request=None):
|
|||
else:
|
||||
nodes = one('flows', data['flow_id'], workspace, user, lock=True).nodes if data.get('flow_id') else data.get('nodes')
|
||||
snapshot = make_snapshot(workspace, user, nodes)
|
||||
from system_manage.mcp_registry import pipeline_resources, resolve
|
||||
required = pipeline_resources(snapshot)
|
||||
for key in required: resolve(workspace, user, key)
|
||||
priority = integer(data.get('priority', 5), 0, 9, '优先级')
|
||||
from system_manage.access_control import enforce_task
|
||||
enforce_task(request, service.id if service else None, snapshot)
|
||||
markdown = text(data.get('markdown', ''), '文档内容', required=False, limit=MAX_INLINE_TEXT)
|
||||
|
|
@ -338,33 +352,98 @@ def submit(workspace, user, data, upload=None, service=None, request=None):
|
|||
from system_manage.ai_hub_assets import asset_lock, normalize
|
||||
with asset_lock():
|
||||
markdown = text(normalize(markdown, workspace, user), '文档内容', required=False)
|
||||
heading_input = {}
|
||||
if data.get('heading_structure') is not None:
|
||||
from system_manage.heading_structure import analyze, validate_manual
|
||||
if not isinstance(data['heading_structure'], dict):
|
||||
raise ValidationError('层级数据无效')
|
||||
try:
|
||||
_, structure = analyze(markdown, previous=data['heading_structure'], repair=False)
|
||||
structure = validate_manual(structure, data['heading_structure'])
|
||||
except ValueError as exc:
|
||||
raise ValidationError(str(exc)) from None
|
||||
heading_input = {'heading_structure': structure, 'original': text(data.get('original', markdown), '原稿', required=False)}
|
||||
if data.get('_heading_source') is not None:
|
||||
heading_input['heading_source'] = data['_heading_source']
|
||||
# Plan without invoking a model. Reserve only useful internal parallel calls.
|
||||
from system_manage.mcp_registry import settings_row
|
||||
from system_manage.mcp_business import summary_model_defaults, summary_counter
|
||||
from system_manage.summary_plan import token_counter
|
||||
from system_manage.ai_hub_organize import plan_document, resolve_settings
|
||||
from models_provider.tools import get_model_default_params
|
||||
from system_manage.ai_hub_assets import protect_images
|
||||
scheduler_settings = settings_row().meta
|
||||
capacity = scheduler_settings.get('capacity', 4)
|
||||
worker_capacity = max(1, capacity - min(scheduler_settings.get('interactive_reserve', 1), max(0, capacity - 1)))
|
||||
resource_slots = {}
|
||||
for node in snapshot:
|
||||
if node['type'] != 'organize' or node.get('operation') == 'heading_repair':
|
||||
continue
|
||||
key = 'model:' + node['model_id']
|
||||
slots = min(node.get('concurrency', 1) if node.get('parallel', True) else 1,
|
||||
resolve(workspace, user, key)[0]['capacity'], worker_capacity)
|
||||
if snapshot[0]['type'] != 'parse':
|
||||
model = model_for(workspace, user, node['model_id'])
|
||||
defaults = summary_model_defaults(model, get_model_default_params(model))
|
||||
try:
|
||||
options = {**node, 'max_input_tokens': min(int(defaults.get('max_input_tokens', 2000000)), node.get('max_input_tokens', 2000000))}
|
||||
if 'input_tokens' not in options and defaults.get('input_tokens'):
|
||||
options['input_tokens'] = int(defaults['input_tokens'])
|
||||
settings = resolve_settings(defaults, node)
|
||||
count, _ = summary_counter(model, defaults, markdown, workspace, user.id, settings, options)
|
||||
if node.get('structure_mode', 'preserve') in ('preserve', 'reorganize'):
|
||||
from system_manage.summary_sections import build_plan
|
||||
planned = build_plan(protect_images(markdown)[0], node['prompt'], settings, options, count)
|
||||
slots = min(slots, max(1, len(planned['batches'])))
|
||||
else:
|
||||
_, _, _, _, chunks = plan_document(protect_images(markdown)[0], node['prompt'], settings, options, count)
|
||||
slots = min(slots, len(chunks))
|
||||
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,
|
||||
name=text(data.get('name') or (upload.name if upload else '导图任务'), '名称', limit=128), service=service,
|
||||
snapshot={'nodes': snapshot, 'flow_id': str(service.flow_id) if service else data.get('flow_id'),
|
||||
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}, file_name=upload.name[:255] if upload else '',
|
||||
input_data={'markdown': markdown, **heading_input}, file_name=upload.name[:255] if upload else '',
|
||||
file_data=upload.read() if upload else None)
|
||||
|
||||
|
||||
@transaction.atomic
|
||||
def artifact_rename(obj, data):
|
||||
if set(data) != {'action', 'name', 'revision'}:
|
||||
raise ValidationError('重命名参数无效')
|
||||
if isinstance(data['revision'], bool) or not isinstance(data['revision'], int) or data['revision'] != obj.revision:
|
||||
raise ValidationError('内容已更新,请重新打开')
|
||||
obj.name = text(data['name'], '名称', limit=128)
|
||||
obj.save(update_fields=['name', 'updated_at'])
|
||||
return obj
|
||||
|
||||
|
||||
@transaction.atomic
|
||||
def artifact_save(workspace, user, data, obj=None):
|
||||
from system_manage.heading_structure import analyze, artifact_state, store_artifact_state, store_summary_state, summary_state, validate_manual
|
||||
previous_structure = artifact_state(obj) if obj else {}
|
||||
if obj:
|
||||
obj = AIArtifact.objects.select_for_update().get(pk=obj.pk)
|
||||
previous_structure = artifact_state(obj)
|
||||
if data.get('revision') != obj.revision:
|
||||
raise ValidationError('内容已更新,请重新打开')
|
||||
history = list(obj.history)
|
||||
if len(history) >= 30:
|
||||
raise ValidationError('修订已达30次,请另存为新图谱')
|
||||
history.append({'revision': obj.revision, 'markdown': obj.markdown, 'layout': obj.layout,
|
||||
'operator': str(user.id), 'updated_at': obj.updated_at.isoformat()})
|
||||
obj.history = history
|
||||
obj.revision += 1
|
||||
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}条),请删除后再保存')
|
||||
task = one('tasks', data['task_id'], workspace, user, lock=True) if data.get('task_id') else None
|
||||
if task and task.status != 'SUCCESS':
|
||||
raise ValidationError('任务尚未成功')
|
||||
obj = AIArtifact(workspace_id=workspace, owner_id=user.id, task=task,
|
||||
original=task.result.get('original', '') if task else text(data.get('original', ''), '原始内容', False, MAX_INLINE_TEXT))
|
||||
if task:
|
||||
previous_structure = (task.result.get('source') or {}).get('heading_structure', {})
|
||||
obj.name = text(data.get('name'), '名称', limit=128)
|
||||
obj.markdown = text(data.get('markdown'), '修订内容', limit=MAX_INLINE_TEXT)
|
||||
obj.layout = data.get('layout', 'tree')
|
||||
|
|
@ -374,8 +453,34 @@ def artifact_save(workspace, user, data, obj=None):
|
|||
with asset_lock():
|
||||
obj.markdown = text(normalize(obj.markdown, workspace, user), '修订内容')
|
||||
obj.original = text(normalize(obj.original, workspace, user), '原始内容', required=False)
|
||||
if 'summary_state' in data:
|
||||
state = data['summary_state']
|
||||
if not isinstance(state, dict) or set(state) - {'source_markdown', 'smart_markdown', 'mode'}:
|
||||
raise ValidationError('智能总结内容无效')
|
||||
if state.get('mode', 'source') not in ('source', 'smart'):
|
||||
raise ValidationError('智能总结状态无效')
|
||||
clean_state = {'mode': state.get('mode', 'source')}
|
||||
for key in ('source_markdown', 'smart_markdown'):
|
||||
clean_state[key] = text(normalize(text(state.get(key, ''), '总结内容', required=False, limit=MAX_INLINE_TEXT), workspace, user), '总结内容', required=False, limit=MAX_INLINE_TEXT)
|
||||
store_summary_state(obj, clean_state)
|
||||
elif summary_state(obj):
|
||||
state = dict(summary_state(obj))
|
||||
state.pop('_type', None)
|
||||
state['smart_markdown' if state.get('mode') == 'smart' else 'source_markdown'] = obj.markdown
|
||||
store_summary_state(obj, state)
|
||||
for entry in obj.history:
|
||||
entry['markdown'] = normalize(entry.get('markdown', ''), workspace, user)
|
||||
if 'markdown' in entry:
|
||||
entry['markdown'] = normalize(entry['markdown'], workspace, user)
|
||||
incoming = data.get('heading_structure')
|
||||
if incoming is not None or previous_structure:
|
||||
try:
|
||||
_, structure = analyze(obj.markdown, previous=previous_structure or incoming, repair=False)
|
||||
if incoming is not None:
|
||||
structure = validate_manual(structure, incoming)
|
||||
_, structure = analyze(obj.markdown, previous=structure, repair=False)
|
||||
except ValueError as exc:
|
||||
raise ValidationError(str(exc)) from None
|
||||
store_artifact_state(obj, structure)
|
||||
obj.save()
|
||||
return obj
|
||||
|
||||
|
|
@ -407,6 +512,9 @@ def delete_record(kind, pk, workspace, user):
|
|||
from system_manage.models.platform_access import PlatformGrant
|
||||
PlatformGrant.objects.filter(workspace_id=workspace, resource_type=resource_type,
|
||||
target=str(obj.id)).delete()
|
||||
if kind == 'services':
|
||||
from system_manage.ai_hub_service_api import save_config
|
||||
save_config(obj.id)
|
||||
obj.delete()
|
||||
|
||||
|
||||
|
|
@ -419,9 +527,10 @@ def parse_file(parser, name, content, options=None, progress_callback=None):
|
|||
return parse(parser, name, content, opts, headers, progress_callback=progress_callback)
|
||||
|
||||
|
||||
def markdown_tree(markdown):
|
||||
def markdown_tree(markdown, excluded_lines=None):
|
||||
"""Markdown转稳定ID结构树:标题层级、嵌套列表、表格;围栏内容不参与结构。"""
|
||||
import re
|
||||
excluded_lines = set(excluded_lines or [])
|
||||
root = {'id': 'root', 'text': '文档', 'children': []}
|
||||
heading_stack = [(0, root)]
|
||||
list_stack = []
|
||||
|
|
@ -459,6 +568,8 @@ def markdown_tree(markdown):
|
|||
|
||||
list_pattern = re.compile(r'^(\s*)(?:([-*+])|(\d{1,9})[.)])\s+(.+)$')
|
||||
for index, line in enumerate(markdown.splitlines()):
|
||||
if excluded_lines and index in excluded_lines:
|
||||
continue
|
||||
fence_match = re.match(r'^\s{0,3}(`{3,}|~{3,})', line)
|
||||
if fence_match:
|
||||
token = fence_match.group(1)
|
||||
|
|
|
|||
|
|
@ -172,7 +172,7 @@ def protect_images(markdown):
|
|||
return pattern.sub(replace, markdown), images
|
||||
|
||||
|
||||
def restore_images(markdown, images):
|
||||
def restore_images(markdown, images, append_missing=True):
|
||||
# Summarizers may omit or repeat placeholders. The backend owns the image
|
||||
# manifest, so preserve known positions and append omitted originals once.
|
||||
known = {str(index + 1): image for index, image in enumerate(images)}
|
||||
|
|
@ -185,7 +185,7 @@ def restore_images(markdown, images):
|
|||
return known[index]
|
||||
markdown = re.sub(r'AIHUBIMAGE[\s_*`-]*(\d+)[\s_*`-]*TOKEN', replace, markdown, flags=re.I)
|
||||
missing = [image for index, image in known.items() if index not in restored]
|
||||
if missing:
|
||||
if missing and append_missing:
|
||||
markdown = markdown.rstrip() + '\n\n## 原文图片\n\n' + '\n\n'.join(missing)
|
||||
return markdown
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,41 @@
|
|||
"""Bounded hierarchical organization; source and completed chunks survive failures."""
|
||||
import re
|
||||
import time
|
||||
import hashlib
|
||||
import json
|
||||
import threading
|
||||
from models_provider.context_policy import DEFAULT_CONTEXT_TOKENS, summary_defaults
|
||||
from concurrent.futures import ThreadPoolExecutor, as_completed
|
||||
from system_manage.summary_plan import LOCAL_PARAMETERS, prompt_rules, output_estimate, source_language, language_rule, language_matches, boundaries_match
|
||||
|
||||
ORGANIZER_PARAMETERS = LOCAL_PARAMETERS
|
||||
|
||||
|
||||
def resolve_settings(defaults, params):
|
||||
"""Workflow overrides win; model-management fields configure the organizer locally."""
|
||||
defaults = summary_defaults(defaults)
|
||||
def number(name, value, low, high):
|
||||
if isinstance(value, str) and value.isdigit():
|
||||
value = int(value)
|
||||
if type(value) is not int or not low <= value <= high:
|
||||
raise ValueError(f'总结参数 {name} 无效,请填写 {low} 至 {high} 的整数')
|
||||
return value
|
||||
context = number('context_tokens', params.get('context_tokens', defaults['context_tokens']), 1024, 2000000)
|
||||
if context > int(defaults['context_tokens']):
|
||||
raise ValueError('上下文超过模型配置上限,请先在模型管理中配置能力参数')
|
||||
output = number('output_tokens', params.get('max_tokens', params.get('output_tokens',
|
||||
defaults['output_tokens'])), 1, 524288)
|
||||
safety = number('safety_tokens', params.get('safety_tokens', defaults['safety_tokens']), 0, 200000)
|
||||
if output + safety >= context:
|
||||
raise ValueError('总结输出与预留长度不能超过上下文长度')
|
||||
for key, value in [('context_tokens', context), ('max_input_tokens', params.get('max_input_tokens')),
|
||||
('max_output_tokens', max(output, params.get('max_output_tokens') or 0))]:
|
||||
limit = defaults.get(key)
|
||||
if limit is not None and value is not None and int(value) > int(limit):
|
||||
raise ValueError(f'总结参数超过模型配置的 {key} 上限')
|
||||
if params.get('max_output_tokens') is not None and output > params['max_output_tokens']:
|
||||
raise ValueError('本次输出长度超过模型最大输出长度')
|
||||
return {'context_tokens': context, 'output_tokens': output, 'safety_tokens': safety}
|
||||
|
||||
|
||||
def token_bound(text):
|
||||
|
|
@ -8,25 +43,31 @@ def token_bound(text):
|
|||
return len(text.encode('utf-8'))
|
||||
|
||||
|
||||
def split_markdown(text, budget):
|
||||
def split_markdown(text, budget, count=None):
|
||||
count = count or token_bound
|
||||
if budget < 256:
|
||||
raise ValueError('模型上下文预算不足,请调大上下文或减少输出长度')
|
||||
chunks, current = [], ''
|
||||
blocks = re.split(r'(?m)(?=^#{1,6} )', text)
|
||||
# Do not mistake headings inside fenced examples for document sections.
|
||||
protected = list(re.finditer(r'(?ms)^```[^\n]*\n.*?^```[^\n]*(?:\n|$)|^~~~[^\n]*\n.*?^~~~[^\n]*(?:\n|$)', text))
|
||||
boundaries = [m.start() for m in re.finditer(r'(?m)^#{1,6} ', text)
|
||||
if not any(span.start() <= m.start() < span.end() for span in protected)]
|
||||
boundaries = sorted(set([0, *boundaries, len(text)]))
|
||||
blocks = [text[a:b] for a, b in zip(boundaries, boundaries[1:])]
|
||||
for block in blocks:
|
||||
while block:
|
||||
if token_bound(current + block) <= budget:
|
||||
if count(current + block) <= budget:
|
||||
current += block
|
||||
break
|
||||
if current:
|
||||
chunks.append(current); current = ''
|
||||
if token_bound(block) <= budget:
|
||||
if count(block) <= budget:
|
||||
current = block
|
||||
break
|
||||
lo, hi = 1, min(len(block), budget)
|
||||
lo, hi = 1, len(block)
|
||||
while lo < hi:
|
||||
mid = (lo + hi + 1) // 2
|
||||
if token_bound(block[:mid]) <= budget:
|
||||
if count(block[:mid]) <= budget:
|
||||
lo = mid
|
||||
else:
|
||||
hi = mid - 1
|
||||
|
|
@ -34,6 +75,13 @@ def split_markdown(text, budget):
|
|||
newline = block.rfind('\n', 0, end)
|
||||
if newline > end // 2:
|
||||
end = newline + 1
|
||||
# Keep a table, fenced example, or consecutive list together when
|
||||
# the whole block fits; oversized blocks may still be continued.
|
||||
spans = re.finditer(r'(?ms)^```[^\n]*\n.*?^```[^\n]*(?:\n|$)|^~~~[^\n]*\n.*?^~~~[^\n]*(?:\n|$)|(?:^\|[^\n]*\n?)+|(?:^[ \t]*(?:[-*+] |\d+[.)] )[^\n]*\n?)+', block)
|
||||
for span in spans:
|
||||
if span.start() < end < span.end() and count(span[0]) <= budget:
|
||||
end = span.start() if span.start() else span.end()
|
||||
break
|
||||
# Never split an image placeholder between requests.
|
||||
for match in re.finditer(r'AIHUBIMAGE\s*\d+\s*TOKEN', block[:end + 64]):
|
||||
if match.start() < end < match.end():
|
||||
|
|
@ -47,65 +95,301 @@ def split_markdown(text, budget):
|
|||
return chunks
|
||||
|
||||
|
||||
def organize(markdown, prompt, invoke, *, context_tokens=32768, output_tokens=4096,
|
||||
safety_tokens=1024, progress=None, checkpoint=None):
|
||||
budget = context_tokens - output_tokens - safety_tokens - token_bound(prompt) - 512
|
||||
chunks = split_markdown(markdown, budget)
|
||||
def plan_document(markdown, prompt, settings, options, count=token_bound):
|
||||
context_tokens, output_tokens, safety_tokens = (settings[key] for key in ('context_tokens', 'output_tokens', 'safety_tokens'))
|
||||
instruction = prompt_rules(prompt, options, source_language(markdown, prompt))
|
||||
# Includes bounded batch context, retry guidance, and message framing.
|
||||
input_limit = min(context_tokens - output_tokens - safety_tokens,
|
||||
options.get('max_input_tokens') or context_tokens)
|
||||
budget = input_limit - count(instruction) - 2048
|
||||
if budget < 256:
|
||||
raise ValueError('模型上下文预算不足,请减少提示词或输出预留')
|
||||
ratio = max(.05, output_estimate(markdown, count, options) / max(1, count(markdown)))
|
||||
# Output risk can require splitting even when input fits. Never constrain
|
||||
# every map result to a fraction of the final merge's output allowance.
|
||||
output_budget = max(256, int(output_tokens * .8 / ratio))
|
||||
chunk_budget = min(budget, output_budget)
|
||||
chunks = split_markdown(markdown, chunk_budget, count)
|
||||
if len(chunks) > 128:
|
||||
raise ValueError('文档分块过多,请调大模型上下文或拆分文档')
|
||||
calls = 0
|
||||
raise ValueError('文档分块过多,请调大模型预算或拆分文档')
|
||||
if len(chunks) > 1 and not options.get('auto_split', True):
|
||||
raise ValueError('文档超过输入或输出预算,请开启自动拆分')
|
||||
return instruction, input_limit, budget, chunk_budget, chunks
|
||||
|
||||
def call(content, instruction):
|
||||
nonlocal calls
|
||||
calls += 1
|
||||
if calls > 160:
|
||||
raise ValueError('整理批次过多,请拆分文档')
|
||||
if token_bound(content) + token_bound(instruction) > context_tokens - output_tokens - safety_tokens:
|
||||
|
||||
def organize(markdown, prompt, invoke, *, context_tokens=DEFAULT_CONTEXT_TOKENS, output_tokens=None,
|
||||
safety_tokens=None, progress=None, checkpoint=None, metrics=None,
|
||||
options=None, count=None, count_method='conservative_bytes', resume=None,
|
||||
state_checkpoint=None, fingerprint_context=None):
|
||||
options = options or {}
|
||||
count = count or token_bound
|
||||
inherited = summary_defaults({'context_tokens': context_tokens})
|
||||
output_tokens = inherited['output_tokens'] if output_tokens is None else output_tokens
|
||||
safety_tokens = inherited['safety_tokens'] if safety_tokens is None else safety_tokens
|
||||
settings = {'context_tokens': context_tokens, 'output_tokens': output_tokens, 'safety_tokens': safety_tokens}
|
||||
instruction, input_limit, budget, chunk_budget, chunks = plan_document(markdown, prompt, settings, options, count)
|
||||
merge = len(chunks) > 1 and options.get('structure_mode', 'preserve') == 'reorganize'
|
||||
concurrency = min(len(chunks), options.get('concurrency', 1)) if options.get('parallel', True) else 1
|
||||
stats = metrics if metrics is not None else {}
|
||||
stats.update(context_tokens=context_tokens, output_tokens=output_tokens, safety_tokens=safety_tokens,
|
||||
input_bytes=token_bound(markdown), input_tokens_estimate=count(markdown), token_count_method=count_method,
|
||||
input_budget=budget, chunk_budget=chunk_budget, map_chunks=len(chunks), concurrency=concurrency,
|
||||
needs_merge=merge, expected_calls=len(chunks) + int(merge), model_calls=0,
|
||||
compression_retries=0, language_retries=0, structure_retries=0, fact_retries=0, language_splits=0, split_retries=0, reused_chunks=0,
|
||||
estimated_output_tokens=output_estimate(markdown, count, options))
|
||||
fingerprint = hashlib.sha256(json.dumps([markdown, instruction, context_tokens, output_tokens,
|
||||
safety_tokens, chunk_budget, options, fingerprint_context, 'summary-v4'],
|
||||
ensure_ascii=False, sort_keys=True).encode()).hexdigest()
|
||||
previous = (resume or {}).get('outputs', {}) if (resume or {}).get('fingerprint') == fingerprint else {}
|
||||
saved = {key: value for key, value in previous.items() if isinstance(value, str) and value.strip()}
|
||||
lock = threading.RLock()
|
||||
last_saved = None
|
||||
|
||||
def persist():
|
||||
nonlocal last_saved
|
||||
with lock:
|
||||
if checkpoint and saved != last_saved:
|
||||
checkpoint(list(saved.values()))
|
||||
last_saved = dict(saved)
|
||||
if state_checkpoint:
|
||||
state_checkpoint({'fingerprint': fingerprint, 'outputs': dict(saved), 'metrics': dict(stats)})
|
||||
|
||||
def report(stage, percent):
|
||||
if progress:
|
||||
progress({'stage': stage, 'percent': percent, 'plan': dict(stats)})
|
||||
|
||||
def call(content, guidance):
|
||||
if count(content) + count(guidance) + 512 > input_limit:
|
||||
raise ValueError('模型输入超过上下文预算')
|
||||
for attempt in range(3):
|
||||
for attempt in range(options.get('retries', 2) + 1):
|
||||
with lock:
|
||||
stats['model_calls'] += 1
|
||||
if stats['model_calls'] > 160:
|
||||
raise ValueError('总结调用超过限制,已保留完成分块')
|
||||
try:
|
||||
response = invoke(instruction, content)
|
||||
response = invoke(guidance, content)
|
||||
break
|
||||
except Exception as exc:
|
||||
if attempt == 2:
|
||||
if attempt == options.get('retries', 2):
|
||||
raise ValueError('模型调用失败,已保留原稿和完成的分块,请稍后重试') from exc
|
||||
time.sleep(attempt + 1)
|
||||
metadata = getattr(response, 'response_metadata', {})
|
||||
if isinstance(metadata, dict) and metadata.get('finish_reason') in ('length', 'max_tokens'):
|
||||
raise ValueError('模型输出被截断,请到模型管理调大该模型的输出上限,或减少整理内容')
|
||||
result = getattr(response, 'content', response)
|
||||
if isinstance(result, list):
|
||||
result = ''.join(item.get('text', '') for item in result if isinstance(item, dict))
|
||||
if not isinstance(result, str) or not result.strip():
|
||||
metadata = getattr(response, 'response_metadata', {}) or {}
|
||||
truncated = (metadata.get('finish_reason') or metadata.get('done_reason')) in ('length', 'max_tokens', 'max_output_tokens')
|
||||
value = getattr(response, 'content', response)
|
||||
if isinstance(value, list):
|
||||
value = ''.join(item.get('text', '') for item in value if isinstance(item, dict))
|
||||
if not truncated and (not isinstance(value, str) or not value.strip()):
|
||||
raise ValueError('模型未返回有效整理内容')
|
||||
return result.strip()
|
||||
value = value.strip() if isinstance(value, str) else ''
|
||||
if value.startswith('```'):
|
||||
lines = value.splitlines()
|
||||
closing = next((i for i, line in enumerate(lines[1:], 1) if re.fullmatch(r'```\s*', line)), len(lines))
|
||||
value = '\n'.join(lines[1:closing]).strip()
|
||||
elif '```' not in content:
|
||||
closing = re.search(r'(?m)^```\s*$', value)
|
||||
if closing:
|
||||
value = value[:closing.start()].strip()
|
||||
usage = getattr(response, 'usage_metadata', {}) or metadata.get('token_usage', {}) or {}
|
||||
actual_output = usage.get('output_tokens', usage.get('completion_tokens', metadata.get('eval_count')))
|
||||
return value, truncated, actual_output
|
||||
|
||||
outputs = []
|
||||
chunk_prompt = prompt + '\n这是文档的一个分块,保留事实及标题关系,精简重复内容。'
|
||||
for index, chunk in enumerate(chunks, 1):
|
||||
if progress:
|
||||
progress({'stage': f'智能整理分块 {index}/{len(chunks)}', 'percent': (index - 1) * 70 // len(chunks)})
|
||||
outputs.append(call(chunk, prompt if len(chunks) == 1 else chunk_prompt))
|
||||
if checkpoint:
|
||||
checkpoint(list(outputs))
|
||||
if len(outputs) == 1:
|
||||
return outputs[0]
|
||||
merge_prompt = prompt + '\n合并以下局部大纲,统一标题层级,压缩重复内容;保留事实、数字和图片占位符。'
|
||||
previous_size = None
|
||||
for round_index in range(4):
|
||||
joined = '\n\n'.join(outputs)
|
||||
groups = split_markdown(joined, budget)
|
||||
if previous_size is not None and token_bound(joined) >= previous_size and len(groups) > 1:
|
||||
raise ValueError('合并结果未收敛,已保留完成的分块,请缩小文档或调整提示词')
|
||||
previous_size = token_bound(joined)
|
||||
merged = []
|
||||
for index, group in enumerate(groups, 1):
|
||||
if progress:
|
||||
progress({'stage': f'合并大纲 {round_index + 1}:{index}/{len(groups)}', 'percent': 70 + round_index * 6})
|
||||
merged.append(call(group, merge_prompt))
|
||||
if checkpoint:
|
||||
checkpoint(merged)
|
||||
if len(merged) == 1:
|
||||
return merged[0]
|
||||
outputs = merged
|
||||
raise ValueError('合并批次超过限制,已保留完成的分块')
|
||||
def summarize(content, key, guidance, depth=0):
|
||||
with lock:
|
||||
if key in saved:
|
||||
stats['reused_chunks'] += 1
|
||||
return saved[key]
|
||||
original = content
|
||||
language = source_language(original, prompt)
|
||||
language_failed = False
|
||||
outline_source = re.sub(r'(?ms)^```[^\n]*\n.*?^```[^\n]*(?:\n|$)|^~~~[^\n]*\n.*?^~~~[^\n]*(?:\n|$)', '', original)
|
||||
required_headings = re.findall(r'(?m)^(#{1,6}) (.+)$', outline_source) if options.get('structure_mode') == 'preserve' else []
|
||||
outline = '\n'.join(hashes + ' ' + title for hashes, title in required_headings)
|
||||
# An exceptionally large TOC would defeat the body budget; it is already
|
||||
# present in the source. Include only bounded, exact outline guidance.
|
||||
outline_hint = ('\nREQUIRED OUTPUT OUTLINE. Copy these headings EXACTLY and add summary bullets beneath each:\n' + outline
|
||||
if outline and count(outline) <= 400 else '')
|
||||
structure_failed = False
|
||||
facts_failed = False
|
||||
for attempt in range(2):
|
||||
hint = guidance + f'\nOutput at most {output_tokens} tokens. Summarize concisely, do not repeat the full source.'
|
||||
if attempt:
|
||||
hint += '\nPrevious result failed validation. Return a concise, complete summary in the required language.'
|
||||
with lock:
|
||||
stats['language_retries' if language_failed else 'structure_retries' if structure_failed else 'fact_retries' if facts_failed else 'compression_retries'] += 1
|
||||
hint += '\n' + language_rule(language)
|
||||
hint += outline_hint
|
||||
if language_failed or structure_failed:
|
||||
hint += '\nVALIDATION FAILED. Keep ALL original section headings in order. Fill EACH section using its source language.'
|
||||
if facts_failed:
|
||||
hint += '\nNUMERIC CONDITION VALIDATION FAILED. Preserve all explicit comparison operators and conditions EXACTLY. Above/exceeds means >, not >= or reaches. Do not weaken or reverse a threshold.'
|
||||
value, truncated, actual_output = call(content, hint)
|
||||
source_sections = list(re.finditer(r'(?m)^(#{1,6}) (.+)$', outline_source)) if required_headings else []
|
||||
target_sections = list(re.finditer(r'(?m)^(#{1,6}) (.+)$', value))
|
||||
bodies = [outline_source[src.end():source_sections[j+1].start() if j+1 < len(source_sections) else len(outline_source)].strip()
|
||||
for j, src in enumerate(source_sections)]
|
||||
# A heading-only ancestor chain has one unambiguous body owner.
|
||||
# Restore that skeleton in code instead of paying for a relabel call.
|
||||
if (source_sections and sum(bool(body) for body in bodies) == 1 and bodies[-1] and target_sections and
|
||||
all(not value[dst.end():target_sections[j+1].start()].strip() for j, dst in enumerate(target_sections[:-1]))):
|
||||
last_body = value[target_sections[-1].end():].strip()
|
||||
if (len(target_sections) <= len(source_sections) and
|
||||
language_matches(last_body, source_language(bodies[-1], prompt)) and
|
||||
set(re.findall(r'\d+(?:\.\d+)?', bodies[-1])) <= set(re.findall(r'\d+(?:\.\d+)?', last_body))):
|
||||
value = '\n'.join(src[1] + ' ' + src[2] for src in source_sections) + '\n' + last_body
|
||||
target_sections = list(re.finditer(r'(?m)^(#{1,6}) (.+)$', value))
|
||||
aligned = bool(source_sections) and len(source_sections) == len(target_sections)
|
||||
section_language_failed = False
|
||||
if aligned:
|
||||
# Headings are owned by the document. Restore harmless translated
|
||||
# or reformatted labels only when section order, body languages,
|
||||
# and numeric anchors agree; do not ask the model to relabel them.
|
||||
for j, (src, dst) in enumerate(zip(source_sections, target_sections)):
|
||||
body = outline_source[src.end():source_sections[j+1].start() if j+1 < len(source_sections) else len(outline_source)].strip()
|
||||
summary = value[dst.end():target_sections[j+1].start() if j+1 < len(target_sections) else len(value)].strip()
|
||||
anchors = set(re.findall(r'\d+(?:\.\d+)?', body))
|
||||
if body and not language_matches(summary, source_language(body, prompt)):
|
||||
section_language_failed = True
|
||||
aligned = False
|
||||
if not anchors <= set(re.findall(r'\d+(?:\.\d+)?', summary)):
|
||||
aligned = False
|
||||
if aligned:
|
||||
for src, dst in reversed(list(zip(source_sections, target_sections))):
|
||||
value = value[:dst.start()] + src[1] + ' ' + src[2] + value[dst.end():]
|
||||
# With no model tokenizer use the conservative estimator, explicitly
|
||||
# recorded in the plan. Provider finish_reason remains authoritative.
|
||||
language_failed = section_language_failed or not language_matches(value, language)
|
||||
facts_failed = not boundaries_match(original, value)
|
||||
actual_headings = re.findall(r'(?m)^#{1,6} (.+)$', value)
|
||||
remaining = iter(actual_headings)
|
||||
heading_key = lambda title: re.sub(r'[*_`]', '', title).strip()
|
||||
structure_failed = any(not any(heading_key(candidate) == heading_key(title) for candidate in remaining) for _, title in required_headings)
|
||||
# Some small models translate mixed-language documents despite valid
|
||||
# instructions. Retry only language-local sections, with no global
|
||||
# rewrite. Models that succeed initially still use a single call.
|
||||
body_languages = {source_language(body, prompt) for body in bodies if body}
|
||||
if (attempt == 0 and depth < 2 and language == 'mixed' and (language_failed or structure_failed)
|
||||
and len(body_languages) > 1 and body_languages <= {'english', 'chinese'}):
|
||||
with lock:
|
||||
stats['language_splits'] += 1
|
||||
stats['expected_calls'] += sum(bool(body) for body in bodies)
|
||||
parts = []
|
||||
for j, (section, body) in enumerate(zip(source_sections, bodies)):
|
||||
heading = section[1] + ' ' + section[2]
|
||||
if not body:
|
||||
parts.append(heading)
|
||||
continue
|
||||
section_language = source_language(body, prompt)
|
||||
parts.append(summarize(heading + '\n' + body, key + '/language' + str(j),
|
||||
prompt_rules(prompt, options, section_language) +
|
||||
f'\nLanguage-local recovery section {j+1}/{len(source_sections)}. Only summarize this section.', depth+1))
|
||||
value = '\n\n'.join(parts)
|
||||
with lock:
|
||||
saved[key] = value
|
||||
return value
|
||||
if not truncated and not language_failed and not structure_failed and not facts_failed and (actual_output if isinstance(actual_output, int) else count(value)) <= output_tokens:
|
||||
# Restore original levels by ordered occurrence, including duplicate titles.
|
||||
pending_headings = list(required_headings)
|
||||
def restore_level(match):
|
||||
if pending_headings and heading_key(match[2]) == heading_key(pending_headings[0][1]):
|
||||
hashes, title = pending_headings.pop(0)
|
||||
return hashes + ' ' + title
|
||||
return match[0]
|
||||
value = re.sub(r'(?m)^(#{1,6}) (.+)$', restore_level, value)
|
||||
with lock:
|
||||
saved[key] = value
|
||||
return value
|
||||
if language_failed or structure_failed or facts_failed:
|
||||
content = original
|
||||
elif not truncated and count(value) <= budget:
|
||||
content = value
|
||||
if language_failed:
|
||||
raise ValueError('总结未保持原文语言,已保留完成分块;请调整提示词或更换模型后重试')
|
||||
if structure_failed:
|
||||
raise ValueError('总结未保留原文标题,已保留完成分块;请调整提示词或更换模型后重试')
|
||||
if facts_failed:
|
||||
raise ValueError('总结改变了数值边界条件,已保留原稿和完成分块;请调整提示词或更换模型后重试')
|
||||
if depth >= 3 or count(original) <= 512:
|
||||
raise ValueError('模型总结截断或长度校验未通过,已保留完成的分块,请提高输出上限或减少细节')
|
||||
with lock:
|
||||
stats['split_retries'] += 1
|
||||
parts = split_markdown(original, max(256, (count(original) + 1) // 2), count)
|
||||
values = [summarize(part, key + '/' + str(i), guidance +
|
||||
f'\n本批继续拆分为 {len(parts)} 段,当前第 {i+1} 段,只总结当前正文。', depth + 1)
|
||||
for i, part in enumerate(parts)]
|
||||
value = '\n\n'.join(values)
|
||||
with lock:
|
||||
saved[key] = value
|
||||
return value
|
||||
|
||||
report(f'预计 {len(chunks)} 批,并发 {concurrency},' + ('完成后汇总' if merge else '完成后组装'), 0)
|
||||
outputs = [None] * len(chunks)
|
||||
offset = 0
|
||||
jobs = []
|
||||
for i, chunk in enumerate(chunks):
|
||||
prefix = markdown[:offset]
|
||||
path = []
|
||||
for match in re.finditer(r'(?m)^(#{1,6}) (.+)$', prefix):
|
||||
level, title = len(match[1]), match[2]
|
||||
path = [(n, t) for n, t in path if n < level] + [(level, title)]
|
||||
headings = re.findall(r'(?m)^#{1,6} (.+)$', chunk)
|
||||
opening_heading = re.match(r'^(#{1,6}) ', chunk)
|
||||
if opening_heading:
|
||||
path = [(level, title) for level, title in path if level < len(opening_heading[1])]
|
||||
metadata = ('\nStage: full document.' if len(chunks) == 1 else
|
||||
f'\nCONTEXT ONLY, NEVER INCLUDE IN OUTPUT: batch {i+1}/{len(chunks)}; '
|
||||
f'ancestor path: {" > ".join(t for _, t in path)[-80:] or "document start"}; '
|
||||
f'headings: {" > ".join(headings[:3])[:80] or "continued body"}; '
|
||||
f'source character range {offset+1}-{offset+len(chunk)} of {len(markdown)}; '
|
||||
f'previous/next batch: {i or "none"}/{i+2 if i+1 < len(chunks) else "none"}. '
|
||||
'Summarize ONLY the current source, never infer other batches.'
|
||||
+ (f'\nPrevious source excerpt for context only, do not summarize it again: {prefix[-80:]}'
|
||||
if prefix and not opening_heading else ''))
|
||||
jobs.append((chunk, f'part{i}', instruction + metadata))
|
||||
offset += len(chunk)
|
||||
error = None
|
||||
if concurrency == 1:
|
||||
for i, job in enumerate(jobs):
|
||||
try:
|
||||
outputs[i] = summarize(*job)
|
||||
finally:
|
||||
persist()
|
||||
report(f'总结分块 {i+1}/{len(chunks)}', (i+1)*85//len(chunks))
|
||||
else:
|
||||
with ThreadPoolExecutor(max_workers=concurrency) as pool:
|
||||
futures = {pool.submit(summarize, *job): i for i, job in enumerate(jobs)}
|
||||
done = 0
|
||||
for future in as_completed(futures):
|
||||
try:
|
||||
outputs[futures[future]] = future.result()
|
||||
except Exception as exc:
|
||||
error = error or exc
|
||||
for pending in futures:
|
||||
pending.cancel()
|
||||
done += 1
|
||||
persist()
|
||||
report(f'总结分块 {done}/{len(chunks)}', done*85//len(chunks))
|
||||
if error:
|
||||
raise error
|
||||
if merge:
|
||||
for round_index in range(4):
|
||||
joined = '\n\n'.join(outputs)
|
||||
groups = split_markdown(joined, chunk_budget, count)
|
||||
report(f'合并大纲 {round_index+1},共 {len(groups)} 批', 88 + round_index*2)
|
||||
merged = []
|
||||
for i, group in enumerate(groups):
|
||||
try:
|
||||
merged.append(summarize(group, f'merge{round_index}/{i}', instruction +
|
||||
f'\n全局汇总阶段,当前第 {i+1}/{len(groups)} 组章节摘要。统一主题、去重,保持原文语言及不同条件。'))
|
||||
finally:
|
||||
persist()
|
||||
if len(merged) == 1:
|
||||
outputs = merged
|
||||
break
|
||||
if count('\n\n'.join(merged)) >= count(joined):
|
||||
raise ValueError('合并结果未收敛,已保留完成的分块')
|
||||
outputs = merged
|
||||
else:
|
||||
raise ValueError('合并批次超过限制,已保留完成的分块')
|
||||
report('总结完成', 100)
|
||||
persist()
|
||||
return '\n\n'.join(outputs)
|
||||
|
|
|
|||
|
|
@ -34,4 +34,5 @@ def task_progress(task):
|
|||
if percent is not None:
|
||||
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}
|
||||
'percent': percent, 'elapsed_seconds': elapsed,
|
||||
**({'plan': detail['plan']} if isinstance(detail.get('plan'), dict) else {})}
|
||||
|
|
|
|||
|
|
@ -6,18 +6,21 @@ from system_manage import ai_hub as hub
|
|||
|
||||
|
||||
def claim():
|
||||
with transaction.atomic():
|
||||
task = AITask.objects.select_for_update(skip_locked=True).filter(status='PENDING').order_by('created_at').first()
|
||||
if not task:
|
||||
return None
|
||||
task.status = 'RUNNING'
|
||||
task.started_at = timezone.now()
|
||||
task.save(update_fields=['status', 'started_at', 'updated_at'])
|
||||
return task
|
||||
from system_manage.mcp_scheduler import admit
|
||||
return admit('worker')
|
||||
|
||||
|
||||
def execute(task):
|
||||
from users.models import User
|
||||
if task.snapshot.get('engine'):
|
||||
try:
|
||||
from system_manage.mcp_workflows import execute as execute_workflow
|
||||
return execute_workflow(task)
|
||||
except Exception:
|
||||
from system_manage.mcp_scheduler import finish
|
||||
finish(task, 'FAILED', error='流程执行失败,请检查组件、授权及流程版本')
|
||||
task.refresh_from_db()
|
||||
return task
|
||||
steps = []
|
||||
try:
|
||||
user = User.objects.get(pk=task.owner_id)
|
||||
|
|
@ -31,17 +34,31 @@ def execute(task):
|
|||
hub.require_use(user, service)
|
||||
if not service.enabled:
|
||||
raise ValueError('服务已停用')
|
||||
from system_manage.mcp_registry import pipeline_resources, resolve
|
||||
for key in pipeline_resources(task.snapshot['nodes']): resolve(task.workspace_id, user, key, check=False)
|
||||
markdown = task.input_data.get('markdown', '')
|
||||
original = markdown
|
||||
source = {}
|
||||
original = task.input_data.get('original', markdown)
|
||||
source = task.input_data.get('heading_source', {})
|
||||
if task.input_data.get('heading_structure'):
|
||||
source = {**source, 'heading_structure': task.input_data['heading_structure']}
|
||||
tree = {}
|
||||
task.result = {'original': original, 'markdown': markdown, 'source': source, 'tree': tree}
|
||||
previous_state = task.result.get('summary_state')
|
||||
previous_parsed = task.result.get('parse_checkpoint')
|
||||
task.result = {'original': original, 'markdown': markdown, 'source': source, 'tree': tree,
|
||||
**({'summary_state': previous_state} if previous_state else {}),
|
||||
**({'parse_checkpoint': previous_parsed} if previous_parsed else {})}
|
||||
task.save(update_fields=['result', 'updated_at'])
|
||||
for node in task.snapshot['nodes']:
|
||||
step = {'type': node['type'], 'status': 'RUNNING'}
|
||||
steps.append(step)
|
||||
task.steps = steps
|
||||
task.save(update_fields=['steps', 'updated_at'])
|
||||
if node['type'] == 'parse' and previous_parsed and previous_parsed.get('node') == node:
|
||||
markdown, original, source = (previous_parsed[key] for key in ('markdown', 'original', 'source'))
|
||||
step.update(status='SUCCESS', progress={'stage': '复用已完成解析', 'percent': 100})
|
||||
task.result.update(markdown=markdown, original=original, source=source)
|
||||
task.save(update_fields=['steps', 'result', 'updated_at'])
|
||||
continue
|
||||
if node['type'] == 'parse':
|
||||
parser = None
|
||||
if node.get('processor') != 'builtin':
|
||||
|
|
@ -54,63 +71,65 @@ def execute(task):
|
|||
AITask.objects.filter(pk=task.pk, attempt=task.attempt, status='RUNNING').update(
|
||||
steps=steps, updated_at=timezone.now())
|
||||
report_progress({'stage': '上传文档到解析服务', 'percent': None})
|
||||
if parser is None:
|
||||
from system_manage.parsers.native import parse
|
||||
report_progress({'stage': '使用内置处理器解析文档', 'percent': None})
|
||||
parsed = parse(task.file_name, bytes(task.file_data))
|
||||
else:
|
||||
parsed = hub.parse_file(parser, task.file_name, bytes(task.file_data), node['options'], progress_callback=report_progress)
|
||||
from system_manage.ai_hub_assets import asset_lock, normalize
|
||||
with asset_lock():
|
||||
markdown = original = hub.text(normalize(parsed['markdown'], task.workspace_id, user, parsed.get('images')), '解析结果')
|
||||
source = parsed['source']
|
||||
# Source JSON can contain image data too; preserve structure without inline payloads.
|
||||
import json
|
||||
source = json.loads(normalize(json.dumps(source), task.workspace_id, user))
|
||||
task.result = {'original': original, 'markdown': markdown, 'source': source}
|
||||
task.save(update_fields=['result', 'updated_at'])
|
||||
from system_manage import mcp_runtime
|
||||
key = 'file-reader' if parser is None else 'parser:' + str(parser.id)
|
||||
parsed = mcp_runtime.invoke(task.workspace_id, user, key, {'options': node.get('options', {})},
|
||||
parent=task.id, progress=report_progress, scope={'source': 'mindmap'})
|
||||
markdown = parsed['text']
|
||||
original = parsed.get('original', markdown)
|
||||
source = parsed['structured']
|
||||
task.refresh_from_db(fields=['result'])
|
||||
task.result.update(original=original, markdown=markdown, source=source,
|
||||
parse_checkpoint={'node': node, 'markdown': markdown, 'original': original, 'source': source})
|
||||
task.save(update_fields=['result', 'updated_at'])
|
||||
elif node['type'] == 'organize':
|
||||
from models_provider.tools import get_model, get_model_default_params
|
||||
from langchain_core.messages import HumanMessage, SystemMessage
|
||||
model = hub.model_for(task.workspace_id, user, node['model_id'])
|
||||
if model.update_time.isoformat() != node['resource_version']:
|
||||
raise ValueError('模型配置已变更,请重新提交')
|
||||
# Bound input; never silently truncate the original document.
|
||||
from system_manage.ai_hub_assets import protect_images, restore_images
|
||||
model_markdown, image_tokens = protect_images(markdown)
|
||||
try:
|
||||
# Per-model settings from model management win; task nodes override only when set.
|
||||
defaults = get_model_default_params(model)
|
||||
temperature = node.get('temperature')
|
||||
if temperature is None:
|
||||
temperature = defaults.get('temperature', 0.2)
|
||||
max_tokens = node.get('output_tokens')
|
||||
if max_tokens is None:
|
||||
max_tokens = defaults.get('max_tokens', 4096)
|
||||
instance = get_model(model, **{**defaults, 'temperature': temperature,
|
||||
'max_tokens': max_tokens, 'timeout': node.get('timeout', 180)})
|
||||
prompt = node['prompt'] + '\n保留 AIHUBIMAGE 数字 TOKEN 图片占位符,尽量放在相关内容旁;不要编造图片或重复占位符。'
|
||||
except Exception as exc:
|
||||
from rest_framework.exceptions import ValidationError
|
||||
raise ValidationError('模型调用失败,请检查接入状态或输入长度') from exc
|
||||
from system_manage.ai_hub_organize import organize
|
||||
def organize_progress(progress):
|
||||
step['progress'] = progress
|
||||
AITask.objects.filter(pk=task.pk, attempt=task.attempt, status='RUNNING').update(steps=steps, updated_at=timezone.now())
|
||||
def checkpoint(outputs):
|
||||
task.result['organize_chunks'] = outputs
|
||||
task.save(update_fields=['result', 'updated_at'])
|
||||
organized = organize(model_markdown, prompt,
|
||||
lambda instruction, content: instance.invoke([SystemMessage(content=instruction), HumanMessage(content=content)]),
|
||||
context_tokens=node.get('context_tokens', 32768), output_tokens=max_tokens,
|
||||
safety_tokens=node.get('safety_tokens', 1024), progress=organize_progress, checkpoint=checkpoint)
|
||||
markdown = restore_images(hub.text(organized, '整理结果'), image_tokens)
|
||||
if node.get('operation') == 'heading_repair':
|
||||
from system_manage.heading_service import invoke_model
|
||||
def heading_progress(progress):
|
||||
step['progress'] = progress
|
||||
AITask.objects.filter(pk=task.pk, attempt=task.attempt, status='RUNNING').update(steps=steps, updated_at=timezone.now())
|
||||
organized = invoke_model(task.workspace_id, user, node, markdown, source, task.id, heading_progress)
|
||||
markdown = organized['text']
|
||||
source = {**source, **organized['structured']}
|
||||
else:
|
||||
from system_manage import mcp_runtime
|
||||
model = hub.model_for(task.workspace_id, user, node['model_id'])
|
||||
if model.update_time.isoformat() != node['resource_version']:
|
||||
raise ValueError('模型配置已变更,请重新提交')
|
||||
def organize_progress(progress):
|
||||
step['progress'] = progress
|
||||
AITask.objects.filter(pk=task.pk, attempt=task.attempt, status='RUNNING').update(steps=steps, updated_at=timezone.now())
|
||||
def checkpoint(outputs):
|
||||
task.refresh_from_db(fields=['result'])
|
||||
task.result['summary_state' if isinstance(outputs, dict) else 'organize_chunks'] = outputs
|
||||
if isinstance(outputs, dict) and outputs.get('metrics'):
|
||||
step.setdefault('progress', {})['plan'] = outputs['metrics']
|
||||
task.save(update_fields=['result', 'steps', 'updated_at'])
|
||||
params = {'text': markdown, 'instruction': node['prompt'],
|
||||
'operation': node.get('operation'),
|
||||
'heading_structure': source.get('heading_structure', {}), 'heading_source': source,
|
||||
'resume': task.result.get('summary_state'),
|
||||
**{key: node[key] for key in (
|
||||
'temperature', 'top_p', 'context_tokens', 'max_input_tokens', 'max_output_tokens',
|
||||
'safety_tokens', 'retries', 'detail', 'structure_mode', 'parallel', 'auto_split',
|
||||
'max_depth', 'max_children', 'node_max_chars', 'concurrency', 'input_tokens') if key in node}}
|
||||
if node.get('output_tokens') is not None: params['max_tokens'] = node['output_tokens']
|
||||
organized = mcp_runtime.invoke(task.workspace_id, user, 'model:' + str(model.id), params,
|
||||
parent=task.id, timeout=node.get('timeout', 180), progress=organize_progress,
|
||||
checkpoint=checkpoint, scope={'source': 'mindmap'})
|
||||
markdown = organized['text']
|
||||
if isinstance(organized.get('structured'), dict):
|
||||
source = {**source, **organized['structured']}
|
||||
else:
|
||||
tree = hub.markdown_tree(markdown)
|
||||
from system_manage import mcp_runtime
|
||||
tree = mcp_runtime.invoke(task.workspace_id, user, 'mindmap-engine', {'text': markdown,
|
||||
'heading_structure': source.get('heading_structure')},
|
||||
parent=task.id, scope={'source': 'mindmap'})['structured']
|
||||
step['status'] = 'SUCCESS'
|
||||
task.steps = steps
|
||||
# Persist intermediate original output so a later model failure does not lose OCR work.
|
||||
task.result = {'original': original, 'markdown': markdown, 'source': source, 'tree': tree}
|
||||
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'])
|
||||
task.status = 'SUCCESS'
|
||||
except Exception as exc:
|
||||
|
|
|
|||
|
|
@ -1,37 +1,65 @@
|
|||
import signal
|
||||
import threading
|
||||
import time
|
||||
from datetime import timedelta
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from django.core.management.base import BaseCommand
|
||||
from django.db import close_old_connections
|
||||
from django.utils import timezone
|
||||
from system_manage.models.ai_hub import AITask
|
||||
from system_manage.ai_hub_worker import claim, execute
|
||||
from system_manage import mcp_scheduler
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = 'Run the isolated AI hub task consumer (does not run migrations).'
|
||||
help = 'Run the isolated component/hub consumer; never consumes Celery queues or migrates.'
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument('--once', action='store_true')
|
||||
parser.add_argument('--recover', action='store_true', help='Mark running tasks older than 30 minutes failed')
|
||||
parser.add_argument('--recover', action='store_true', help='Mark tasks without heartbeat for 30 minutes failed')
|
||||
parser.add_argument('--concurrency', type=int, default=1)
|
||||
|
||||
def handle(self, *args, **options):
|
||||
if options['recover']:
|
||||
AITask.objects.filter(status='RUNNING', updated_at__lt=timezone.now() - timedelta(minutes=30)).update(
|
||||
status='FAILED', error='执行中断,请重试', finished_at=timezone.now())
|
||||
concurrency = options.get('concurrency', 1)
|
||||
if not 1 <= concurrency <= 8:
|
||||
raise ValueError('concurrency must be between 1 and 8')
|
||||
if options.get('recover'):
|
||||
mcp_scheduler.recover()
|
||||
stop = threading.Event()
|
||||
if threading.current_thread() is threading.main_thread():
|
||||
signal.signal(signal.SIGTERM, lambda *_: stop.set())
|
||||
next_cleanup = time.monotonic() + 86400
|
||||
while True:
|
||||
def run(task):
|
||||
close_old_connections()
|
||||
if time.monotonic() >= next_cleanup:
|
||||
from system_manage.ai_hub_assets import cleanup
|
||||
try:
|
||||
cleanup(apply=True)
|
||||
except Exception:
|
||||
self.stderr.write('图片缓存清理失败,将于下次重试')
|
||||
next_cleanup = time.monotonic() + 86400
|
||||
task = claim()
|
||||
if task:
|
||||
execute(task)
|
||||
if options['once']:
|
||||
return
|
||||
if not task:
|
||||
time.sleep(2)
|
||||
try:
|
||||
return execute(task)
|
||||
except Exception:
|
||||
mcp_scheduler.finish(task, 'FAILED', error='执行器异常,请检查后重试')
|
||||
finally:
|
||||
close_old_connections()
|
||||
with ThreadPoolExecutor(max_workers=concurrency) as pool:
|
||||
running = {}
|
||||
while not stop.is_set() or running:
|
||||
close_old_connections()
|
||||
mcp_scheduler.heartbeat()
|
||||
for future, task in list(running.items()):
|
||||
if future.done():
|
||||
future.result()
|
||||
del running[future]
|
||||
else:
|
||||
AITask.objects.filter(pk=task.id, attempt=task.attempt, status='RUNNING').update(updated_at=timezone.now())
|
||||
if not stop.is_set():
|
||||
while len(running) < concurrency:
|
||||
task = claim()
|
||||
if not task: break
|
||||
if stop.is_set():
|
||||
AITask.objects.filter(pk=task.id, attempt=task.attempt, status='RUNNING').update(
|
||||
status='PENDING', started_at=None, updated_at=timezone.now())
|
||||
break
|
||||
running[pool.submit(run, task)] = task
|
||||
if options.get('once'): stop.set()
|
||||
if time.monotonic() >= next_cleanup and not running:
|
||||
from system_manage.ai_hub_assets import cleanup
|
||||
try: cleanup(apply=True)
|
||||
except Exception: self.stderr.write('图片缓存清理失败,将于下次重试')
|
||||
next_cleanup = time.monotonic() + 86400
|
||||
if running or not stop.is_set(): time.sleep(1)
|
||||
|
|
|
|||
|
|
@ -15,6 +15,18 @@ def parse(name, content):
|
|||
from system_manage.ai_hub import text
|
||||
from . import extension
|
||||
suffix = extension(name)
|
||||
if suffix in ('md', 'txt'):
|
||||
# Preserve headings and whitespace even when a document has no body paragraph.
|
||||
try:
|
||||
markdown = content.decode('utf-8-sig')
|
||||
except UnicodeDecodeError:
|
||||
from charset_normalizer import from_bytes
|
||||
decoded = from_bytes(content).best()
|
||||
if decoded is None:
|
||||
raise ValidationError('无法识别文本编码,请使用 UTF-8')
|
||||
markdown = str(decoded)
|
||||
return {'markdown': text(markdown, '解析结果'), 'images': {},
|
||||
'source': {'engine': 'maxkb-native', 'format': suffix}}
|
||||
if suffix == 'doc':
|
||||
command = shutil.which('libreoffice') or shutil.which('soffice')
|
||||
if command is None:
|
||||
|
|
@ -60,12 +72,31 @@ def parse(name, content):
|
|||
mime = mimetypes.guess_type(file.file_name)[0] or 'image/jpeg'
|
||||
images['./oss/file/' + str(file.id)] = f'data:{mime};base64,' + base64.b64encode(raw).decode()
|
||||
upload = SimpleUploadedFile(Path(name.replace('\\', '/')).name, content)
|
||||
result = handler.handle(upload, [], False, 2_000_000, lambda _: content, capture, 0)
|
||||
sections = result if isinstance(result, list) else [result]
|
||||
markdown = '\n\n'.join('\n\n'.join(
|
||||
((('# ' + item.get('title', '') + '\n') if item.get('title') else '') + item.get('content', ''))
|
||||
for item in section.get('content', [])) for section in sections if isinstance(section, dict))
|
||||
evidence = {}
|
||||
if suffix == 'docx':
|
||||
from docx import Document
|
||||
from .native_outline import word_markdown
|
||||
# Use the converter's Markdown before knowledge-base chunking flattens headings.
|
||||
markdown, evidence = word_markdown(Document(io.BytesIO(content)), handler, capture)
|
||||
elif suffix in ('html', 'htm'):
|
||||
from html2text import html2text
|
||||
from common.handle.impl.text.html_split_handle import get_encoding
|
||||
markdown = html2text(content.decode(get_encoding(content)))
|
||||
from .native_outline import html_evidence
|
||||
evidence = html_evidence(content)
|
||||
elif suffix == 'pdf':
|
||||
from .native_outline import pdf_markdown
|
||||
markdown, evidence = pdf_markdown(content)
|
||||
if markdown is None:
|
||||
result = handler.handle(upload, [], False, 2_000_000, lambda _: content, capture, 0)
|
||||
markdown = '\n\n'.join(item.get('content', '') for item in result.get('content', []))
|
||||
else:
|
||||
result = handler.handle(upload, [], False, 2_000_000, lambda _: content, capture, 0)
|
||||
sections = result if isinstance(result, list) else [result]
|
||||
markdown = '\n\n'.join('\n\n'.join(
|
||||
((('# ' + item.get('title', '') + '\n') if item.get('title') else '') + item.get('content', ''))
|
||||
for item in section.get('content', [])) for section in sections if isinstance(section, dict))
|
||||
for path, uri in images.items():
|
||||
markdown = markdown.replace(path, uri)
|
||||
return {'markdown': text(markdown, '解析结果'), 'images': {},
|
||||
'source': {'engine': 'maxkb-native', 'format': suffix}}
|
||||
'source': {'engine': 'maxkb-native', 'format': suffix, **evidence}}
|
||||
|
|
|
|||
|
|
@ -11,6 +11,83 @@ from system_manage.ai_hub_worker import claim, execute
|
|||
from system_manage.models.ai_hub import AIFlow, AIParserResource, AITask, AIArtifact, AIService
|
||||
|
||||
|
||||
class NativeParserTests(TestCase):
|
||||
def test_word_preserves_heading_only_sections_tables_and_images(self):
|
||||
import io
|
||||
from docx import Document
|
||||
from PIL import Image
|
||||
from system_manage.parsers.native import parse
|
||||
|
||||
document = Document()
|
||||
document.add_heading('Chapter42', 1)
|
||||
document.add_heading('Section42', 2)
|
||||
document.add_heading('Detail42', 3)
|
||||
document.add_paragraph('Body42')
|
||||
table = document.add_table(rows=2, cols=2)
|
||||
table.cell(0, 0).text = 'Name'
|
||||
table.cell(0, 1).text = 'Value'
|
||||
table.cell(1, 0).text = 'Count'
|
||||
table.cell(1, 1).text = '42'
|
||||
image = io.BytesIO()
|
||||
Image.new('RGB', (2, 2), 'white').save(image, format='PNG')
|
||||
image.seek(0)
|
||||
document.add_picture(image)
|
||||
buffer = io.BytesIO()
|
||||
document.save(buffer)
|
||||
|
||||
with patch('common.handle.impl.text.doc_split_handle.DocSplitHandle.handle',
|
||||
side_effect=AssertionError('Knowledge chunking must not run')):
|
||||
markdown = parse('headings.docx', buffer.getvalue())['markdown']
|
||||
self.assertEqual([line for line in markdown.splitlines() if line.startswith('#')],
|
||||
['# Chapter42', '## Section42', '### Detail42'])
|
||||
self.assertIn('Body42', markdown)
|
||||
self.assertIn('| Count | 42 |', markdown)
|
||||
self.assertIn('data:image/png;base64,', markdown)
|
||||
self.assertNotIn('./oss/file/', markdown)
|
||||
tree = hub.markdown_tree(markdown)
|
||||
self.assertEqual(tree['children'][0]['children'][0]['children'][0]['text'], 'Detail42')
|
||||
|
||||
def test_html_and_htm_preserve_headings_lists_and_links_without_chunking(self):
|
||||
from system_manage.parsers.native import parse
|
||||
content = (b'<meta charset="utf-8"><h1>Chapter42</h1><h2>Section42</h2>'
|
||||
b'<h3>Detail42</h3><p>Body42</p><ul><li>Parent42'
|
||||
b'<ul><li>Child42</li></ul></li></ul>'
|
||||
b'<p><a href="https://example.org">Link42</a></p>')
|
||||
for suffix in ('html', 'htm'):
|
||||
with self.subTest(suffix=suffix), patch(
|
||||
'common.handle.impl.text.html_split_handle.HTMLSplitHandle.handle',
|
||||
side_effect=AssertionError('Knowledge chunking must not run')):
|
||||
markdown = parse('headings.' + suffix, content)['markdown']
|
||||
self.assertEqual([line for line in markdown.splitlines() if line.startswith('#')],
|
||||
['# Chapter42', '## Section42', '### Detail42'])
|
||||
self.assertIn('Child42', markdown)
|
||||
self.assertIn('[Link42](https://example.org)', markdown)
|
||||
|
||||
def test_xlsx_uses_cached_formula_values_and_does_not_evaluate_formulas(self):
|
||||
import io
|
||||
import zipfile
|
||||
from openpyxl import Workbook
|
||||
from system_manage.parsers.native import parse
|
||||
|
||||
workbook = Workbook()
|
||||
workbook.active.append(['Cached', 'Uncached', 'Literal'])
|
||||
workbook.active.append(['=6*7', '=2+3', 17])
|
||||
buffer = io.BytesIO()
|
||||
workbook.save(buffer)
|
||||
cached = io.BytesIO()
|
||||
with zipfile.ZipFile(io.BytesIO(buffer.getvalue())) as source, zipfile.ZipFile(cached, 'w') as target:
|
||||
for item in source.infolist():
|
||||
data = source.read(item.filename)
|
||||
if item.filename == 'xl/worksheets/sheet1.xml':
|
||||
self.assertIn(b'<f>6*7</f><v></v>', data)
|
||||
data = data.replace(b'<f>6*7</f><v></v>', b'<f>6*7</f><v>42</v>')
|
||||
target.writestr(item, data)
|
||||
markdown = parse('cached.xlsx', cached.getvalue())['markdown']
|
||||
self.assertIn('| 42 | | 17 |', markdown)
|
||||
self.assertNotIn('=6*7', markdown)
|
||||
self.assertNotIn('=2+3', markdown)
|
||||
|
||||
|
||||
class HubTests(TestCase):
|
||||
def setUp(self):
|
||||
import tempfile
|
||||
|
|
|
|||
|
|
@ -1,10 +1,59 @@
|
|||
import unittest
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import patch
|
||||
from system_manage.ai_hub_organize import organize, split_markdown, token_bound
|
||||
from system_manage.ai_hub_organize import organize, split_markdown, token_bound, resolve_settings
|
||||
|
||||
|
||||
class OrganizeTests(unittest.TestCase):
|
||||
def test_model_management_budgets_and_workflow_precedence(self):
|
||||
defaults = {'context_tokens': '131072', 'output_tokens': '8192', 'safety_tokens': '2048', 'max_tokens': 16384}
|
||||
self.assertEqual(resolve_settings(defaults, {}), {'context_tokens': 131072, 'output_tokens': 8192, 'safety_tokens': 2048})
|
||||
self.assertEqual(resolve_settings(defaults, {'max_tokens': 4096, 'context_tokens': 65536})['output_tokens'], 4096)
|
||||
self.assertEqual(resolve_settings(defaults, {'output_tokens': 1024})['output_tokens'], 1024)
|
||||
for params in ({'context_tokens': True}, {'safety_tokens': -1}, {'context_tokens': 4096, 'max_tokens': 8192}):
|
||||
with self.assertRaises(ValueError): resolve_settings(defaults, params)
|
||||
|
||||
def test_truncated_response_retries_with_original_not_partial_draft(self):
|
||||
calls, saved, metrics = [], [], {}
|
||||
def invoke(instruction, content):
|
||||
calls.append((instruction, content))
|
||||
return SimpleNamespace(content='incomplete' if len(calls) == 1 else '# Result\n- fact42',
|
||||
response_metadata={'finish_reason': 'length' if len(calls) == 1 else 'stop'})
|
||||
self.assertIn('fact42', organize('Original fact42', '总结', invoke, checkpoint=saved.append, metrics=metrics))
|
||||
self.assertEqual(calls[0][1], calls[1][1])
|
||||
self.assertNotIn('incomplete', str(saved))
|
||||
self.assertEqual(metrics['compression_retries'], 1)
|
||||
|
||||
def test_complete_overlong_summary_is_semantically_compressed(self):
|
||||
contents = []
|
||||
def invoke(prompt, content):
|
||||
contents.append(content)
|
||||
return 'complete verbose result ' * 100 if len(contents) == 1 else '# Summary\n- fact42'
|
||||
result = organize('Original fact42', '总结', invoke, context_tokens=8192, output_tokens=256)
|
||||
self.assertIn('fact42', result)
|
||||
self.assertIn('complete verbose result', contents[1])
|
||||
|
||||
def test_repeated_truncation_splits_source_and_preserves_completed_sibling(self):
|
||||
saved, metrics = [], {}
|
||||
def invoke(prompt, content):
|
||||
if len(content) > 700:
|
||||
return SimpleNamespace(content='partial', response_metadata={'finish_reason': 'length'})
|
||||
if 'FAIL' in content:
|
||||
return SimpleNamespace(content='partial', response_metadata={'finish_reason': 'length'})
|
||||
return '# Completed42'
|
||||
with self.assertRaisesRegex(ValueError, '截断'):
|
||||
organize('A' * 700 + '\nFAIL' + 'B' * 700, '总结', invoke, checkpoint=saved.append, metrics=metrics)
|
||||
self.assertTrue(any('Completed42' in value for snapshot in saved for value in snapshot))
|
||||
self.assertNotIn('partial', str(saved))
|
||||
self.assertGreater(metrics['split_retries'], 0)
|
||||
|
||||
def test_echo_model_cannot_grow_merge_forever_or_be_accepted(self):
|
||||
calls = []
|
||||
def invoke(prompt, content): calls.append(1); return content + '\nextra'
|
||||
with self.assertRaisesRegex(ValueError, '长度'):
|
||||
organize('facts42 ' * 500, '总结', invoke, context_tokens=8192, output_tokens=256)
|
||||
self.assertLessEqual(len(calls), 160)
|
||||
|
||||
def test_split_preserves_long_unbroken_unicode_and_image_tokens(self):
|
||||
text = '# 标题\n' + '超长段落42' * 8000 + '\nAIHUBIMAGE0001TOKEN\n结束'
|
||||
chunks = split_markdown(text, 2048)
|
||||
|
|
@ -19,7 +68,7 @@ class OrganizeTests(unittest.TestCase):
|
|||
calls.append(content)
|
||||
return SimpleNamespace(content='# Result\n- fact42', response_metadata={})
|
||||
result = organize(source, '整理事实', invoke, context_tokens=8192, output_tokens=1024,
|
||||
progress=stages.append, checkpoint=saved.append)
|
||||
progress=stages.append, checkpoint=saved.append, options={'structure_mode': 'reorganize'})
|
||||
self.assertIn('fact42', result)
|
||||
self.assertGreater(len(calls), 2)
|
||||
self.assertGreater(len(saved), 2)
|
||||
|
|
@ -39,6 +88,261 @@ class OrganizeTests(unittest.TestCase):
|
|||
return '# saved42'
|
||||
with patch('system_manage.ai_hub_organize.time.sleep'):
|
||||
with self.assertRaisesRegex(ValueError, '已保留') as failure:
|
||||
organize('word42 ' * 5000, '整理', invoke, context_tokens=4096, output_tokens=512, checkpoint=saved.append)
|
||||
organize('word42 ' * 5000, '整理', invoke, context_tokens=8192, output_tokens=512, checkpoint=saved.append)
|
||||
self.assertNotIn('private', str(failure.exception))
|
||||
self.assertEqual(saved, [['# saved42']])
|
||||
|
||||
|
||||
class SummaryPlanningTests(unittest.TestCase):
|
||||
def test_short_document_one_call_and_language_rules(self):
|
||||
calls, metrics = [], {}
|
||||
def invoke(prompt, content):
|
||||
calls.append((prompt, content))
|
||||
return '# Operations\n- Inspect every 42 days.'
|
||||
result = organize('# Operations\nInspect every 42 days.', '保留事实', invoke, metrics=metrics)
|
||||
self.assertEqual(len(calls), 1)
|
||||
self.assertIn('42 days', result)
|
||||
self.assertIn('ENGLISH ONLY', calls[0][0])
|
||||
self.assertIn('Stage: full document', calls[0][0])
|
||||
self.assertEqual(metrics['expected_calls'], 1)
|
||||
self.assertFalse(metrics['needs_merge'])
|
||||
|
||||
def test_budget_counts_prompt_metadata_and_framing(self):
|
||||
calls, metrics = [], {}
|
||||
def invoke(prompt, content):
|
||||
calls.append(1)
|
||||
self.assertLessEqual(token_bound(prompt) + token_bound(content) + 256, 8192-1024-1024)
|
||||
return '- fact42'
|
||||
organize('English data 42. ' * 1500, 'Only facts', invoke, context_tokens=8192,
|
||||
output_tokens=1024, metrics=metrics)
|
||||
self.assertEqual(len(calls), metrics['map_chunks'])
|
||||
self.assertGreater(len(calls), 1)
|
||||
|
||||
def test_output_budget_and_detail_can_require_split(self):
|
||||
source = '# Operations\n' + 'Inspect equipment 42 days. ' * 180
|
||||
brief, detailed = {}, {}
|
||||
for detail, metric in [('brief', brief), ('detailed', detailed)]:
|
||||
organize(source, 'Summarize', lambda *a: '- 42 days', output_tokens=1024,
|
||||
options={'detail': detail}, metrics=metric)
|
||||
self.assertEqual(brief['map_chunks'], 1)
|
||||
self.assertGreater(detailed['map_chunks'], 1)
|
||||
self.assertGreater(detailed['estimated_output_tokens'], brief['estimated_output_tokens'])
|
||||
|
||||
def test_parallel_preserves_order_and_bounds_concurrency(self):
|
||||
import threading, time
|
||||
active, peak = 0, 0
|
||||
lock = threading.Lock()
|
||||
source = ''.join(f'## Chapter {i}\n' + 'facts ' * 700 + '\n' for i in range(4))
|
||||
def invoke(prompt, content):
|
||||
nonlocal active, peak
|
||||
with lock:
|
||||
active += 1; peak = max(peak, active)
|
||||
time.sleep(.02 if 'Chapter 0' in content else .005)
|
||||
with lock: active -= 1
|
||||
return '- ' + content[:35].replace('\n', ' ')
|
||||
metrics = {}
|
||||
result = organize(source, 'Summarize', invoke, context_tokens=8192, output_tokens=2048,
|
||||
options={'concurrency': 2}, metrics=metrics)
|
||||
self.assertEqual(peak, 2)
|
||||
self.assertEqual(metrics['model_calls'], metrics['map_chunks'])
|
||||
self.assertLess(result.index('Chapter 0'), result.index('Chapter 1'))
|
||||
|
||||
def test_failure_resume_reuses_only_matching_configuration(self):
|
||||
source = 'facts42 ' * 1200
|
||||
states, calls = [], []
|
||||
def fail(prompt, content):
|
||||
calls.append(1)
|
||||
if len(calls) > 1: raise RuntimeError('secret')
|
||||
return '- retained42'
|
||||
with self.assertRaises(ValueError):
|
||||
organize(source, 'Summarize', fail, context_tokens=8192, output_tokens=1024,
|
||||
options={'retries': 0}, state_checkpoint=states.append)
|
||||
metrics = {}
|
||||
organize(source, 'Summarize', lambda *a: '- resumed42', context_tokens=8192, output_tokens=1024,
|
||||
options={'retries': 0}, resume=states[-1], metrics=metrics)
|
||||
self.assertEqual(metrics['reused_chunks'], 1)
|
||||
self.assertEqual(metrics['model_calls'], metrics['map_chunks']-1)
|
||||
other = {}
|
||||
organize(source, 'New prompt', lambda *a: '- new42', context_tokens=8192, output_tokens=1024,
|
||||
options={'retries': 0}, resume=states[-1], metrics=other)
|
||||
self.assertEqual(other['reused_chunks'], 0)
|
||||
|
||||
def test_chunk_metadata_and_mixed_language_survive_merge(self):
|
||||
seen = []
|
||||
def invoke(prompt, content):
|
||||
seen.append(prompt)
|
||||
return '# Maintenance\n- 42 days\n## 检修\n- 每月一次' if '检修' in content else '# Maintenance\n- 42 days'
|
||||
organize('## Maintenance 检修\n' + '42 days ' * 1400, 'Summarize', invoke,
|
||||
context_tokens=8192, output_tokens=2048, options={'structure_mode': 'reorganize'})
|
||||
self.assertTrue(any('batch' in p and 'character range' in p and 'ancestor path' in p for p in seen))
|
||||
self.assertTrue(any('全局汇总阶段' in p for p in seen))
|
||||
self.assertTrue(all('original' in p.lower() for p in seen))
|
||||
|
||||
def test_no_split_option_rejects_before_call(self):
|
||||
invoke = unittest.mock.Mock()
|
||||
with self.assertRaisesRegex(ValueError, '开启自动拆分'):
|
||||
organize('facts ' * 5000, 'Summarize', invoke, options={'auto_split': False})
|
||||
invoke.assert_not_called()
|
||||
|
||||
def test_invalid_settings_and_declared_model_limits(self):
|
||||
from system_manage.summary_plan import validate_options
|
||||
for options in ({'concurrency': True}, {'top_p': float('nan')}, {'detail': 'unknown'},
|
||||
{'parallel': 'true'}, {'context_tokens': 1024}, {'max_depth': 7}):
|
||||
with self.assertRaises(ValueError): validate_options(options)
|
||||
with self.assertRaisesRegex(ValueError, '上限'):
|
||||
resolve_settings({'context_tokens': 8192}, {'context_tokens': 16384})
|
||||
with self.assertRaises(ValueError):
|
||||
resolve_settings({'max_output_tokens': 1024}, {'max_tokens': 2048})
|
||||
|
||||
def test_max_input_independent_limit(self):
|
||||
stats = {}
|
||||
organize('data42 ' * 2000, 'Summarize', lambda *a: '- data42', output_tokens=512,
|
||||
options={'max_input_tokens': 8192}, metrics=stats)
|
||||
self.assertLess(stats['input_budget'], 8192)
|
||||
self.assertGreater(stats['map_chunks'], 1)
|
||||
|
||||
|
||||
def test_table_list_and_fenced_example_stay_whole_when_they_fit(self):
|
||||
table = '| Key | Value |\n| --- | --- |\n| Count | 42 |\n'
|
||||
code = '```text\n# not a heading\nvalue42\n```\n'
|
||||
items = '- step 1\n- step 2\n'
|
||||
for block in (table, code, items):
|
||||
text = 'Intro ' * 45 + '\n' + block + 'After ' * 60
|
||||
parts = split_markdown(text, 300)
|
||||
self.assertEqual(''.join(parts), text)
|
||||
self.assertTrue(any(block in part for part in parts))
|
||||
self.assertTrue(all(token_bound(part) <= 300 for part in parts))
|
||||
|
||||
def test_custom_node_length_changes_output_planning(self):
|
||||
metrics = []
|
||||
for length in (20, 120):
|
||||
stats = {}
|
||||
organize('Equipment 42 days. ' * 240, 'Summarize', lambda *a: '- 42 days', output_tokens=1024,
|
||||
options={'node_max_chars': length}, metrics=stats)
|
||||
metrics.append(stats)
|
||||
self.assertGreater(metrics[1]['map_chunks'], metrics[0]['map_chunks'])
|
||||
|
||||
def test_provider_usage_prevents_unnecessary_compression_from_byte_estimate(self):
|
||||
calls = []
|
||||
def invoke(*a):
|
||||
calls.append(1)
|
||||
return SimpleNamespace(content='中文结论42。' * 30, response_metadata={'finish_reason': 'stop'},
|
||||
usage_metadata={'output_tokens': 200})
|
||||
organize('原文42', 'Summarize', invoke, output_tokens=256)
|
||||
self.assertEqual(len(calls), 1)
|
||||
|
||||
|
||||
def test_wrong_language_retries_only_current_source_and_unwraps_any_fence(self):
|
||||
contents, metrics = [], {}
|
||||
def invoke(prompt, content):
|
||||
contents.append(content)
|
||||
return '中文错误结果' if len(contents) == 1 else '```ruby\n# Maintenance\n- Inspect every 42 days.'
|
||||
output = organize('Inspect every 42 days.', 'Summarize', invoke, metrics=metrics)
|
||||
self.assertEqual(len(contents), 2)
|
||||
self.assertEqual(contents[0], contents[1])
|
||||
self.assertEqual(output, '# Maintenance\n- Inspect every 42 days.')
|
||||
self.assertEqual(metrics['language_retries'], 1)
|
||||
|
||||
def test_repeated_wrong_language_is_not_success(self):
|
||||
with self.assertRaisesRegex(ValueError, '未保持原文语言'):
|
||||
organize('Inspect every 42 days.', 'Summarize', lambda *a: '中文错误结果')
|
||||
|
||||
def test_language_detection_preserves_source_despite_translation_prompt(self):
|
||||
from system_manage.summary_plan import source_language
|
||||
self.assertEqual(source_language('English text', '请翻译为中文'), 'english')
|
||||
self.assertEqual(source_language('これは日本語の説明です'), 'japanese')
|
||||
self.assertEqual(source_language('한국어 설명입니다'), 'korean')
|
||||
|
||||
|
||||
def test_original_headings_are_validated_and_levels_restored(self):
|
||||
calls=[]
|
||||
def invoke(*a):
|
||||
calls.append(1)
|
||||
return '- fact42' if len(calls)==1 else '### Maintenance\n#### Inspection\n- fact42'
|
||||
result=organize('# Maintenance\n## Inspection\nInspect every 42 days.', 'Summarize', invoke,
|
||||
options={'structure_mode':'preserve'})
|
||||
self.assertTrue(result.startswith('# Maintenance\n## Inspection'))
|
||||
self.assertEqual(len(calls),2)
|
||||
|
||||
def test_ollama_length_stop_is_never_accepted(self):
|
||||
with self.assertRaisesRegex(ValueError,'截断'):
|
||||
organize('English source', 'Summarize', lambda *a: SimpleNamespace(content='Partial',
|
||||
response_metadata={'done_reason':'length','eval_count':256}),output_tokens=256)
|
||||
|
||||
|
||||
def test_mixed_language_failure_recovers_only_language_local_sections(self):
|
||||
calls, stats = [], {}
|
||||
source='# Maintenance / 维护\n## English operations\nInspect every 42 days.\n## 中文操作\n每31天记录一次运行情况。'
|
||||
def invoke(prompt, content):
|
||||
calls.append(content)
|
||||
if len(calls)==1:return '## 英文操作\n每42天检查一次。\n## 中文操作\n每31天记录一次运行情况。'
|
||||
if 'English operations' in content:return '## English operations\n- Inspect every 42 days.'
|
||||
return '## 中文操作\n- 每31天记录一次运行情况。'
|
||||
output=organize(source,'Summarize',invoke,options={'structure_mode':'preserve'},metrics=stats)
|
||||
self.assertEqual(len(calls),3)
|
||||
self.assertNotIn('中文操作',calls[1])
|
||||
self.assertNotIn('English operations',calls[2])
|
||||
self.assertIn('42 days',output)
|
||||
self.assertIn('每31天',output)
|
||||
self.assertEqual(stats['language_splits'],1)
|
||||
self.assertEqual(stats['expected_calls'],3)
|
||||
|
||||
def test_single_body_heading_chain_is_restored_without_another_call(self):
|
||||
calls=[]
|
||||
def invoke(*a):calls.append(1);return '### Maintenance\n- Inspect every 42 days.'
|
||||
output=organize('# Maintenance\n## Inspection\nInspect every 42 days.','Summarize',invoke,
|
||||
options={'structure_mode':'preserve'})
|
||||
self.assertEqual(len(calls),1)
|
||||
self.assertIn('# Maintenance\n## Inspection',output)
|
||||
|
||||
def test_customized_default_prompt_is_not_discarded(self):
|
||||
from system_manage.summary_plan import DEFAULT_SUMMARY_PROMPT,prompt_rules
|
||||
custom=DEFAULT_SUMMARY_PROMPT+'\nKeep the STOP condition explicit.'
|
||||
self.assertIn(custom,prompt_rules(custom,{},'english'))
|
||||
|
||||
|
||||
def test_independent_chapter_does_not_inherit_previous_sibling_context(self):
|
||||
seen=[]
|
||||
source='## Alpha\n'+'Alpha fact42. '*180+'\n## Beta\n'+'Beta fact43. '*180
|
||||
def invoke(prompt,content):
|
||||
seen.append((prompt,content));return '- fact42 and fact43'
|
||||
organize(source,'Summarize',invoke,context_tokens=8192,output_tokens=1024)
|
||||
beta=[prompt for prompt,content in seen if content.startswith('## Beta')]
|
||||
self.assertTrue(beta)
|
||||
self.assertTrue(all('ancestor path: document start' in prompt for prompt in beta))
|
||||
self.assertTrue(all('Previous source excerpt' not in prompt for prompt in beta))
|
||||
|
||||
|
||||
def test_model_wrapper_and_trailing_self_comment_are_removed_without_call(self):
|
||||
for wrapped in ('```markdown\n# Result\n- fact42\n```\nThis summary is complete.',
|
||||
'# Result\n- fact42\n```\nThis summary is complete.'):
|
||||
calls=[]
|
||||
def invoke(*a):calls.append(1);return wrapped
|
||||
result=organize('Original fact42','Summarize',invoke)
|
||||
self.assertEqual(result,'# Result\n- fact42')
|
||||
self.assertEqual(len(calls),1)
|
||||
|
||||
|
||||
def test_explicit_numeric_conditions_keep_strictness_negation_and_sign(self):
|
||||
from system_manage.summary_plan import boundaries_match
|
||||
self.assertTrue(boundaries_match('Stop above 91 degrees.', 'Stop when temperature exceeds 91.0 degrees.'))
|
||||
self.assertFalse(boundaries_match('Stop above 91 degrees.', 'Stop when it reaches 91 degrees.'))
|
||||
self.assertFalse(boundaries_match('温度超过90℃停止。', '温度达到90℃停止。'))
|
||||
self.assertTrue(boundaries_match('Do not exceed 90.', 'At most 90.0.'))
|
||||
self.assertFalse(boundaries_match('At least -5.', 'Below -5.'))
|
||||
self.assertTrue(boundaries_match('Thunder 42.', 'Thunder 42.'))
|
||||
|
||||
def test_changed_numeric_boundary_repairs_from_original_only(self):
|
||||
contents,stats=[],{}
|
||||
def invoke(prompt,content):
|
||||
contents.append(content)
|
||||
return '- Stop when it reaches 91 degrees.' if len(contents)==1 else '- Stop above 91 degrees.'
|
||||
output=organize('Stop above 91 degrees.','Summarize',invoke,metrics=stats)
|
||||
self.assertEqual(len(contents),2)
|
||||
self.assertEqual(contents[0],contents[1])
|
||||
self.assertIn('above 91',output)
|
||||
self.assertEqual(stats['fact_retries'],1)
|
||||
|
||||
def test_repeated_numeric_boundary_change_cannot_be_success(self):
|
||||
with self.assertRaisesRegex(ValueError,'数值边界条件'):
|
||||
organize('Stop above 91 degrees.','Summarize',lambda *a:'- Stop when it reaches 91 degrees.')
|
||||
|
|
|
|||
|
|
@ -1,13 +1,15 @@
|
|||
from system_manage.views.mcp_engine import EngineView
|
||||
from django.urls import path
|
||||
|
||||
from . import views
|
||||
from .views.ai_hub import HubView, PublishedRun, ParserTest, AssetContent, MarkdownExport, DocumentExport
|
||||
from .views.ai_hub import HubView, PublishedRun, ParserTest, AssetContent, MarkdownExport, DocumentExport, ServiceCalls
|
||||
from .views.platform_access import AccessView, RoleView, GrantsView, UserAccessView
|
||||
from .views.access_control_policy import AccessControlRuntimeView, AccessControlDecisionView
|
||||
|
||||
app_name = "system_manage"
|
||||
# @formatter:off
|
||||
urlpatterns = [
|
||||
path('workspace/<str:workspace_id>/mcp_engine/<str:kind>', EngineView.as_view()),
|
||||
path('platform_access', AccessView.as_view()),
|
||||
path('platform_access/roles', RoleView.as_view()),
|
||||
path('platform_access/roles/<uuid:pk>', RoleView.as_view()),
|
||||
|
|
@ -18,6 +20,7 @@ urlpatterns = [
|
|||
path('workspace/<str:workspace_id>/ai_hub/exports', MarkdownExport.as_view()),
|
||||
path('workspace/<str:workspace_id>/ai_hub/assets/<uuid:pk>/content', AssetContent.as_view()),
|
||||
path('workspace/<str:workspace_id>/ai_hub/parsers/<uuid:pk>/test', ParserTest.as_view()),
|
||||
path('workspace/<str:workspace_id>/ai_hub/services/<uuid:pk>/calls', ServiceCalls.as_view()),
|
||||
path('workspace/<str:workspace_id>/ai_hub/published/<uuid:pk>/run', PublishedRun.as_view()),
|
||||
path('workspace/<str:workspace_id>/ai_hub/<str:kind>', HubView.as_view()),
|
||||
path('workspace/<str:workspace_id>/ai_hub/<str:kind>/<uuid:pk>', HubView.as_view()),
|
||||
|
|
|
|||
|
|
@ -35,10 +35,18 @@ class HubView(APIView):
|
|||
raise PermissionDenied('未授权使用思维导图')
|
||||
|
||||
def get(self, request, workspace_id, kind, pk=None):
|
||||
if kind == 'artifact_limit' and not pk:
|
||||
from system_manage import artifact_settings
|
||||
hub.require_admin(request.user)
|
||||
return result.success({'limit': artifact_settings.limit()})
|
||||
if kind == 'workbench_defaults' and not pk:
|
||||
from system_manage import workbench_defaults
|
||||
return result.success(workbench_defaults.read(workspace_id, request.user))
|
||||
if kind == 'parser_catalog':
|
||||
from system_manage.parsers.catalog import catalogue
|
||||
return result.success(catalogue())
|
||||
if kind == 'resources':
|
||||
from system_manage.mcp_business import summary_model_metadata
|
||||
parsers = hub.visible(hub.AIParserResource.objects.filter(workspace_id=workspace_id, enabled=True), request.user)
|
||||
from users.models import User
|
||||
from users.models.user_manage_ext import UserActivationApplication
|
||||
|
|
@ -48,7 +56,9 @@ class HubView(APIView):
|
|||
resource_type__in=['PARSER', 'SERVICE'], actions__contains=['AUTHORIZE']).exists()
|
||||
return result.success({'parsers': [hub.serialize(p, user=request.user) for p in parsers
|
||||
if 'USE' in hub.serialize(p, user=request.user)['actions']],
|
||||
'models': list(hub.authorized_models(workspace_id, request.user).values('id', 'name', 'model_type')),
|
||||
'models': [summary_model_metadata(model)
|
||||
if model.model_type == 'LLM' else {'id': str(model.id), 'name': model.name, 'model_type': model.model_type}
|
||||
for model in hub.authorized_models(workspace_id, request.user)],
|
||||
'users': [{'id': str(u.id), 'name': u.nick_name or u.username} for u in User.objects.filter(is_active=True).exclude(id__in=pending)] if may_authorize else [],
|
||||
'is_admin': hub.admin(request.user)})
|
||||
if pk:
|
||||
|
|
@ -61,9 +71,33 @@ class HubView(APIView):
|
|||
raise ValidationError('资源类型无效')
|
||||
query = hub.visible(hub.KINDS[kind].objects.filter(workspace_id=workspace_id), request.user)
|
||||
if kind == 'tasks':
|
||||
from django.db.models import Q
|
||||
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')
|
||||
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'):
|
||||
raise ValidationError('查询参数无效')
|
||||
try:
|
||||
page = int(request.query_params.get('current_page', '1'))
|
||||
size = int(request.query_params.get('page_size', '20'))
|
||||
if page < 1 or size < 1 or size > 100:
|
||||
raise ValueError()
|
||||
except (ValueError, TypeError):
|
||||
raise ValidationError('分页参数无效')
|
||||
name = request.query_params.get('name', '')
|
||||
if len(name) > 128:
|
||||
raise ValidationError('名称无效')
|
||||
if mine == 'true':
|
||||
query = query.filter(owner_id=request.user.id)
|
||||
if name:
|
||||
query = query.filter(name__icontains=name)
|
||||
total = query.count()
|
||||
records = query.order_by('-updated_at', '-id')[(page - 1) * size:page * size]
|
||||
return result.success({'records': [hub.serialize(obj, user=request.user) for obj in records],
|
||||
'total': total, 'current': page})
|
||||
return result.success([hub.serialize(obj, user=request.user) for obj in query.order_by('-created_at')[:200]])
|
||||
|
||||
def post(self, request, workspace_id, kind, pk=None):
|
||||
|
|
@ -76,6 +110,20 @@ class HubView(APIView):
|
|||
raise ValidationError('任务参数无效')
|
||||
if not isinstance(data, dict):
|
||||
raise ValidationError('请求参数无效')
|
||||
if kind == 'artifact_limit' and not pk:
|
||||
from system_manage import artifact_settings
|
||||
return result.success(artifact_settings.save(request.user, data))
|
||||
if kind == 'workbench_defaults' and not pk:
|
||||
from system_manage import workbench_defaults
|
||||
return result.success(workbench_defaults.save(workspace_id, request.user, data))
|
||||
if kind == 'summary_preview':
|
||||
from system_manage.mcp_business import summary_preview
|
||||
from system_manage.access_control import enforce_task
|
||||
enforce_task(request, nodes=data.get('nodes'))
|
||||
return result.success(summary_preview(workspace_id, request.user, data))
|
||||
if kind == 'heading_structure':
|
||||
from system_manage.heading_service import process
|
||||
return result.success(process(workspace_id, request.user, data, request))
|
||||
if kind == 'tasks':
|
||||
from system_manage.access_control import enforce_task
|
||||
if pk:
|
||||
|
|
@ -97,7 +145,12 @@ class HubView(APIView):
|
|||
if kind == 'parsers':
|
||||
obj = hub.parser_save(workspace_id, request.user, data, obj)
|
||||
elif kind == 'artifacts':
|
||||
obj = hub.artifact_save(workspace_id, request.user, data, obj)
|
||||
if 'action' in data:
|
||||
if data['action'] != 'rename':
|
||||
raise ValidationError('操作无效')
|
||||
obj = hub.artifact_rename(obj, data)
|
||||
else:
|
||||
obj = hub.artifact_save(workspace_id, request.user, data, obj)
|
||||
elif kind == 'services':
|
||||
if set(data) - {'allowed_users'}:
|
||||
hub.require_resource_action(request.user, obj, 'EDIT')
|
||||
|
|
@ -120,12 +173,17 @@ class HubView(APIView):
|
|||
if data.get('revoke_key'):
|
||||
obj.key_hash = ''
|
||||
obj.save()
|
||||
if 'api_config' in data:
|
||||
from system_manage.ai_hub_service_api import save_config
|
||||
save_config(obj.id, data['api_config'])
|
||||
output = hub.serialize(obj)
|
||||
if key:
|
||||
output['api_key'] = key
|
||||
return result.success(output)
|
||||
elif kind == 'tasks':
|
||||
obj = AITask.objects.select_for_update().get(pk=obj.pk)
|
||||
if obj.snapshot.get('engine'):
|
||||
raise ValidationError('请在 MCP 引擎任务页重试该任务')
|
||||
if obj.status != 'FAILED':
|
||||
raise ValidationError('仅失败任务可重试')
|
||||
if hub.make_snapshot(workspace_id, request.user, obj.snapshot['nodes']) != obj.snapshot['nodes']:
|
||||
|
|
@ -151,6 +209,8 @@ class HubView(APIView):
|
|||
raise ValidationError('待处理任务已达上限')
|
||||
obj = hub.submit(workspace_id, request.user, data, request.FILES.get('file'), request=request)
|
||||
elif kind == 'artifacts':
|
||||
if 'action' in data:
|
||||
raise ValidationError('操作无效')
|
||||
obj = hub.artifact_save(workspace_id, request.user, data)
|
||||
else:
|
||||
raise ValidationError('资源类型无效')
|
||||
|
|
@ -225,7 +285,79 @@ class MarkdownExport(HubView):
|
|||
class PublishedRun(HubView):
|
||||
"""Internal service: login AND explicit resource grants; Key is an optional extra credential."""
|
||||
authentication_classes = [TokenAuth]
|
||||
http_method_names = ['post', 'options']
|
||||
http_method_names = ['get', 'post', 'options']
|
||||
|
||||
def handle_exception(self, exc):
|
||||
from rest_framework.exceptions import APIException
|
||||
from rest_framework.response import Response
|
||||
from common.exception.app_exception import AppApiException
|
||||
if isinstance(exc, (APIException, AppApiException)):
|
||||
status = exc.status_code
|
||||
defaults = {400: '请求参数无效', 401: '请先登录', 403: '无权调用该服务',
|
||||
404: '服务不存在或无权访问', 405: '不支持的请求方法', 429: '调用过于频繁'}
|
||||
message = defaults.get(status, '调用失败')
|
||||
detail = getattr(exc, 'detail', None)
|
||||
while isinstance(detail, (list, tuple, dict)) and detail:
|
||||
detail = next(iter(detail.values())) if isinstance(detail, dict) else detail[0]
|
||||
safe_messages = {'服务Key无效', '调用过于频繁', '请求方法与服务配置不一致', '请求参数过长',
|
||||
'任务参数无效', '包含未知参数', '参数不能重复', '文件参数无效', '参数类型无效',
|
||||
'缺少必填参数', '优先级无效', '服务已停用', '请选择文件', '文档内容不能为空',
|
||||
'文件任务需要解析节点', '待处理任务已达上限', '文件不能超过20MB'}
|
||||
if str(detail) in safe_messages:
|
||||
message = str(detail)
|
||||
self._call_error = message
|
||||
return Response({'code': getattr(exc, 'code', status), 'message': message}, status=status)
|
||||
raise exc
|
||||
|
||||
def dispatch(self, request, *args, **kwargs):
|
||||
import time
|
||||
self._call_started = time.monotonic()
|
||||
try:
|
||||
with transaction.atomic():
|
||||
return super().dispatch(request, *args, **kwargs)
|
||||
except Exception:
|
||||
import logging
|
||||
from system_manage.models import Log
|
||||
logging.getLogger(__name__).error('AI service call failed')
|
||||
try:
|
||||
Log.objects.create(menu='AI_SERVICE', operate='CALL', workspace_id=str(kwargs.get('workspace_id', '')),
|
||||
operation_object={'service_id': str(kwargs.get('pk', ''))}, user={}, status=500, ip_address='',
|
||||
details={'method': request.method, 'http_status': 500,
|
||||
'elapsed_ms': max(0, int((time.monotonic() - self._call_started) * 1000)),
|
||||
'status': 'FAILED', 'error': '服务执行失败', 'task_id': None})
|
||||
except Exception:
|
||||
return result.error('调用日志保存失败', response_status=503)
|
||||
return result.error('服务执行失败', response_status=500)
|
||||
|
||||
def finalize_response(self, request, response, *args, **kwargs):
|
||||
import time
|
||||
from system_manage.models import Log
|
||||
from rest_framework.response import Response
|
||||
response = super().finalize_response(request, response, *args, **kwargs)
|
||||
body = getattr(response, 'data', None)
|
||||
if body is None and response.get('Content-Type', '').startswith('application/json'):
|
||||
import json
|
||||
try:
|
||||
body = json.loads(response.content)
|
||||
except (ValueError, TypeError):
|
||||
body = None
|
||||
code = body.get('code') if isinstance(body, dict) else None
|
||||
success = response.status_code < 400 and code in (None, 0, 200)
|
||||
try:
|
||||
Log.objects.create(menu='AI_SERVICE', operate='CALL', workspace_id=str(kwargs.get('workspace_id', '')),
|
||||
operation_object={'service_id': str(kwargs.get('pk', ''))},
|
||||
user={'id': str(getattr(getattr(request, '_user', None), 'id', '') or '')}, status=response.status_code if success or response.status_code >= 400 else 400, ip_address='',
|
||||
details={'method': request.method, 'http_status': response.status_code,
|
||||
'elapsed_ms': max(0, int((time.monotonic() - self._call_started) * 1000)),
|
||||
'status': 'SUCCESS' if success else 'FAILED', 'error': '' if success else getattr(self, '_call_error', '调用失败'),
|
||||
'task_id': getattr(self, '_task_id', None) if success else None})
|
||||
except Exception:
|
||||
transaction.set_rollback(True)
|
||||
response = super().finalize_response(request, Response({'code': 503, 'message': '调用日志保存失败'}, status=503), *args, **kwargs)
|
||||
return response
|
||||
|
||||
def get(self, request, workspace_id, pk):
|
||||
return self.post(request, workspace_id, pk)
|
||||
|
||||
def post(self, request, workspace_id, pk):
|
||||
service = hub.one('services', pk, workspace_id, request.user)
|
||||
|
|
@ -241,17 +373,46 @@ class PublishedRun(HubView):
|
|||
cache.add(key, 0, 70)
|
||||
if cache.incr(key) > service.requests_per_minute:
|
||||
raise ValidationError('调用过于频繁')
|
||||
data = request.data
|
||||
if request.FILES:
|
||||
import json
|
||||
try:
|
||||
data = json.loads(request.data.get('data', '{}'))
|
||||
except (ValueError, TypeError):
|
||||
raise ValidationError('任务参数无效')
|
||||
task = hub.submit(workspace_id, request.user, data, request.FILES.get('file'), service=service, request=request)
|
||||
from system_manage.ai_hub_service_api import get_config, parse_request
|
||||
data, upload = parse_request(request, get_config(service.id))
|
||||
task = hub.submit(workspace_id, request.user, data, upload, service=service, request=request)
|
||||
self._task_id = str(task.id)
|
||||
return result.success(hub.serialize(task))
|
||||
|
||||
|
||||
class ServiceCalls(HubView):
|
||||
http_method_names = ['get', 'options']
|
||||
|
||||
def get(self, request, workspace_id, pk):
|
||||
from system_manage.models import Log
|
||||
service = hub.one('services', pk, workspace_id, request.user)
|
||||
hub.require_resource_action(request.user, service, 'EDIT')
|
||||
try:
|
||||
page = int(request.query_params.get('current_page', 1))
|
||||
size = int(request.query_params.get('page_size', 20))
|
||||
if page < 1 or size < 1 or size > 100:
|
||||
raise ValueError()
|
||||
except (ValueError, TypeError):
|
||||
raise ValidationError('分页参数无效')
|
||||
logs = Log.objects.filter(menu='AI_SERVICE', operate='CALL', workspace_id=workspace_id,
|
||||
operation_object__service_id=str(pk)).order_by('-create_time')
|
||||
total = logs.count()
|
||||
rows = list(logs[(page - 1) * size:page * size])
|
||||
ids = [row.details.get('task_id') for row in rows if row.details.get('task_id')]
|
||||
tasks = {str(t.id): t for t in AITask.objects.filter(id__in=ids, workspace_id=workspace_id)}
|
||||
records = []
|
||||
for row in rows:
|
||||
details = row.details
|
||||
task = tasks.get(details.get('task_id'))
|
||||
duration = int((task.finished_at - task.started_at).total_seconds() * 1000) if task and task.started_at and task.finished_at else None
|
||||
records.append({'id': str(row.id), 'created_at': row.create_time.isoformat(),
|
||||
'method': details.get('method'), 'status': details.get('status'),
|
||||
'http_status': details.get('http_status'), 'elapsed_ms': details.get('elapsed_ms'),
|
||||
'error': (task.error if task and task.status == 'FAILED' else details.get('error')), 'task_id': details.get('task_id'),
|
||||
'task_status': task.status if task else None, 'execution_ms': duration})
|
||||
return result.success({'records': records, 'total': total})
|
||||
|
||||
|
||||
class DocumentExport(HubView):
|
||||
http_method_names = ['post', 'options']
|
||||
|
||||
|
|
@ -259,6 +420,8 @@ class DocumentExport(HubView):
|
|||
from django.http import HttpResponse
|
||||
from system_manage.document_export import export_document, ExportError, MAX_HTML
|
||||
from system_manage.ai_hub_assets import ASSET, image_bytes
|
||||
import time
|
||||
started = time.perf_counter()
|
||||
def image(src):
|
||||
match = ASSET.fullmatch(src)
|
||||
if not match or match[1] != workspace_id:
|
||||
|
|
@ -275,6 +438,7 @@ class DocumentExport(HubView):
|
|||
except (ExportError, ValueError, TypeError) as exc:
|
||||
raise ValidationError(str(exc) if isinstance(exc, ExportError) else '文档内容格式无效') from exc
|
||||
response = HttpResponse(raw, content_type=mime)
|
||||
response['Server-Timing'] = f'document_export;dur={(time.perf_counter() - started) * 1000:.1f}'
|
||||
response['Content-Disposition'] = 'attachment; filename="document.' + format_name + '"'
|
||||
response['Cache-Control'] = 'private, no-store'
|
||||
response['X-Content-Type-Options'] = 'nosniff'
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ def main():
|
|||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('action', choices=['check', 'plan', 'migrate', 'worker', 'cleanup-assets'], nargs='?', default='check')
|
||||
parser.add_argument('--apply', action='store_true', help='Apply expired image cleanup (default: report only)')
|
||||
parser.add_argument('--concurrency', type=int, default=1)
|
||||
parser.add_argument('--once', action='store_true')
|
||||
parser.add_argument('--recover', action='store_true')
|
||||
args = parser.parse_args()
|
||||
|
|
@ -35,7 +36,7 @@ def main():
|
|||
elif args.action == 'cleanup-assets':
|
||||
call_command('ai_hub_cleanup_assets', apply=args.apply)
|
||||
elif args.action == 'worker':
|
||||
call_command('ai_hub_worker', once=args.once, recover=args.recover)
|
||||
call_command('ai_hub_worker', once=args.once, recover=args.recover, concurrency=args.concurrency)
|
||||
else:
|
||||
from system_manage.models.ai_hub import AIParserResource, AIFlow, AIService, AITask, AIArtifact
|
||||
tables = set(connection.introspection.table_names())
|
||||
|
|
|
|||
|
|
@ -29,5 +29,6 @@ export async function hubBinary(kind: 'assets' | 'exports' | 'document_export',
|
|||
try { const detail = JSON.parse(await response.data.text()); if (typeof detail.message === 'string') message = detail.message.slice(0, 200) } catch { /* retain a safe fallback */ }
|
||||
throw new Error(message)
|
||||
}
|
||||
return { content: await response.data.arrayBuffer(), mime: response.data.type }
|
||||
return { content: await response.data.arrayBuffer(), mime: response.data.type,
|
||||
serverTimings: response.headers?.['server-timing'] || '' }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -38,6 +38,7 @@
|
|||
</div>
|
||||
<el-collapse-transition>
|
||||
<div class="mt-12" v-if="data['show']">
|
||||
<EngineCallRecords :calls="data.engine_calls" />
|
||||
<template v-if="data.status === 200 || data.type == WorkflowType.LoopNode">
|
||||
<!-- 开始 -->
|
||||
<template
|
||||
|
|
@ -915,6 +916,12 @@
|
|||
</div>
|
||||
</div>
|
||||
</template>
|
||||
<template v-if="data.type === WorkflowType.ComponentNode">
|
||||
<div class="card-never border-r-6 p-8-12 pre-wrap">
|
||||
<p>组件:{{ data.component_id }}</p><p v-if="data.task_id">任务:{{ data.task_id }}</p>
|
||||
<p>{{ data.text }}</p><pre v-if="data.structured">{{ JSON.stringify(data.structured, null, 2) }}</pre>
|
||||
</div>
|
||||
</template>
|
||||
<!-- MCP 节点 -->
|
||||
<template v-if="data.type === WorkflowType.McpNode">
|
||||
<div class="card-never border-r-6">
|
||||
|
|
@ -1087,6 +1094,7 @@
|
|||
</template>
|
||||
<script setup lang="ts">
|
||||
import { ref, computed } from 'vue'
|
||||
import EngineCallRecords from '@/components/engine-call-records/index.vue'
|
||||
import ParagraphCard from '@/components/ai-chat/component/knowledge-source-component/ParagraphCard.vue'
|
||||
import DynamicsForm from '@/components/dynamics-form/index.vue'
|
||||
import { iconComponent } from '@/workflow/icons/utils'
|
||||
|
|
|
|||
|
|
@ -25,6 +25,7 @@ export enum WorkflowType {
|
|||
SpeechToTextNode = 'speech-to-text-node',
|
||||
ImageGenerateNode = 'image-generate-node',
|
||||
McpNode = 'mcp-node',
|
||||
ComponentNode = 'component-node',
|
||||
IntentNode = 'intent-node',
|
||||
TextToVideoGenerateNode = 'text-to-video-node',
|
||||
ImageToVideoGenerateNode = 'image-to-video-node',
|
||||
|
|
|
|||
|
|
@ -142,25 +142,23 @@
|
|||
{{ menuText.roleManagement }}
|
||||
</router-link>
|
||||
|
||||
<router-link
|
||||
v-if="user.is_admin()"
|
||||
to="/system/mindmap"
|
||||
style="
|
||||
display: block;
|
||||
padding: 10px 16px;
|
||||
border-radius: 8px;
|
||||
text-decoration: none;
|
||||
color: #333;
|
||||
font-size: 14px;
|
||||
"
|
||||
:style="
|
||||
activeSystemMenu === '/system/mindmap'
|
||||
? { backgroundColor: '#e6f0ff', color: '#1890ff' }
|
||||
: {}
|
||||
"
|
||||
>
|
||||
{{ menuText.mindmapManagement }}
|
||||
</router-link>
|
||||
<div v-if="user.is_admin()">
|
||||
<div style="display:flex;justify-content:space-between;align-items:center;padding:10px 16px;border-radius:8px;cursor:pointer"
|
||||
:style="isServiceMenu ? { backgroundColor:'#e6f0ff', color:'#1890ff' } : {}" @click="serviceExpanded = !serviceExpanded">
|
||||
<span>{{ menuText.mindmapManagement }}</span><span>{{ serviceExpanded ? '▾' : '▸' }}</span>
|
||||
</div>
|
||||
<div v-if="serviceExpanded" style="padding-left:32px;margin-top:4px;display:flex;flex-direction:column;gap:4px">
|
||||
<router-link to="/system/services" style="display:block;padding:8px 16px;border-radius:8px;text-decoration:none;color:#666;font-size:13px"
|
||||
:style="activeSystemMenu === '/system/services' ? { backgroundColor:'#e6f0ff', color:'#1890ff' } : {}">{{ menuText.mindmapSettings }}</router-link>
|
||||
<router-link to="/system/advanced-flows" style="display:block;padding:8px 16px;border-radius:8px;text-decoration:none;color:#666;font-size:13px"
|
||||
:style="activeSystemMenu === '/system/advanced-flows' ? { backgroundColor:'#e6f0ff', color:'#1890ff' } : {}">{{ menuText.advancedFlows }}</router-link>
|
||||
</div>
|
||||
</div>
|
||||
<router-link v-if="user.is_admin()" to="/system/mcp-engine"
|
||||
style="display:block;padding:10px 16px;border-radius:8px;text-decoration:none;"
|
||||
:style="activeSystemMenu === '/system/mcp-engine' ? {backgroundColor:'#e6f0ff',color:'#1890ff'} : {}">
|
||||
{{ menuText.mcpEngine }}
|
||||
</router-link>
|
||||
|
||||
<div>
|
||||
<div
|
||||
|
|
@ -241,7 +239,7 @@
|
|||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed, onMounted, ref } from 'vue'
|
||||
import { computed, onMounted, ref, watch } from 'vue'
|
||||
import { useRoute } from 'vue-router'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import UserAvatar from '@/layout/layout-header/avatar/index.vue'
|
||||
|
|
@ -256,7 +254,10 @@ const menuText = computed(() => {
|
|||
return {
|
||||
aiApplication: 'AI Applications',
|
||||
mindmap: 'Mind Map',
|
||||
mindmapManagement: 'Mind Map Management',
|
||||
mindmapManagement: 'Service Management',
|
||||
mindmapSettings: 'Mind Map Settings',
|
||||
advancedFlows: 'Advanced Orchestration',
|
||||
mcpEngine: 'MCP Engine',
|
||||
knowledge: 'Knowledge Base',
|
||||
toolManagement: 'Tool Management',
|
||||
modelManagement: 'Model Management',
|
||||
|
|
@ -273,7 +274,10 @@ const menuText = computed(() => {
|
|||
return {
|
||||
aiApplication: 'AI應用',
|
||||
mindmap: '思維導圖',
|
||||
mindmapManagement: '思維導圖管理',
|
||||
mindmapManagement: '服務管理',
|
||||
mindmapSettings: '導圖設定',
|
||||
advancedFlows: '高級編排',
|
||||
mcpEngine: 'MCP 引擎',
|
||||
knowledge: '知識庫',
|
||||
toolManagement: '工具管理',
|
||||
modelManagement: '模型管理',
|
||||
|
|
@ -289,7 +293,10 @@ const menuText = computed(() => {
|
|||
return {
|
||||
aiApplication: 'AI应用',
|
||||
mindmap: '思维导图',
|
||||
mindmapManagement: '思维导图管理',
|
||||
mindmapManagement: '服务管理',
|
||||
mindmapSettings: '导图设置',
|
||||
advancedFlows: '高级编排',
|
||||
mcpEngine: 'MCP 引擎',
|
||||
knowledge: '知识库',
|
||||
toolManagement: '工具管理',
|
||||
modelManagement: '模型管理',
|
||||
|
|
@ -320,8 +327,10 @@ const activeMainMenu = computed(() => {
|
|||
|
||||
const activeSystemMenu = computed(() => {
|
||||
const path = route.path
|
||||
if (path.endsWith('/system/mcp-engine')) return '/system/mcp-engine'
|
||||
if (path === '/system/role') return '/system/role'
|
||||
if (path === '/system/mindmap' || path === '/admin/system/mindmap') return '/system/mindmap'
|
||||
if (path === '/system/services' || path === '/admin/system/services') return '/system/services'
|
||||
if (path === '/system/advanced-flows' || path === '/admin/system/advanced-flows') return '/system/advanced-flows'
|
||||
if (path.startsWith('/system/user') || path.startsWith('/admin/system/user'))
|
||||
return '/system/user'
|
||||
if (path.startsWith('/system/authorization') || path.startsWith('/admin/system/authorization')) {
|
||||
|
|
@ -344,6 +353,9 @@ const activeSystemMenu = computed(() => {
|
|||
|
||||
const resourceAuthExpanded = ref(false)
|
||||
const systemSettingsExpanded = ref(false)
|
||||
const isServiceMenu = computed(() => ['/system/services', '/system/advanced-flows'].includes(activeSystemMenu.value))
|
||||
const serviceExpanded = ref(isServiceMenu.value)
|
||||
watch(isServiceMenu, value => { if (value) serviceExpanded.value = true })
|
||||
|
||||
const toggleResourceAuth = () => {
|
||||
resourceAuthExpanded.value = !resourceAuthExpanded.value
|
||||
|
|
|
|||
|
|
@ -176,7 +176,7 @@ onMounted(() => {
|
|||
}
|
||||
|
||||
:deep(.el-dropdown-menu__item) {
|
||||
padding: 12px 11px;
|
||||
padding: 12px 20px;
|
||||
|
||||
&:hover {
|
||||
background: var(--app-text-color-light-1);
|
||||
|
|
|
|||
|
|
@ -8,13 +8,17 @@ const systemRouter = {
|
|||
hidden: true,
|
||||
component: () => import('@/layout/layout-template/SystemMainLayout.vue'),
|
||||
children: [
|
||||
{ path: '/system/mcp-engine', name: 'mcpEngine',
|
||||
meta: { icon: 'Connection', iconActive: 'Connection', title: 'MCP 引擎', activeMenu: '/system',
|
||||
parentPath: '/system', parentName: 'system', sameRoute: 'mcpEngine', permission: [RoleConst.ADMIN] },
|
||||
component: () => import('@/views/system/mcp-engine/index.vue') },
|
||||
{
|
||||
path: '/system/mindmap',
|
||||
path: '/system/services',
|
||||
name: 'mindmapManagement',
|
||||
meta: {
|
||||
icon: 'app-document',
|
||||
iconActive: 'app-document',
|
||||
title: '思维导图管理',
|
||||
title: '导图设置',
|
||||
activeMenu: '/system',
|
||||
parentPath: '/system',
|
||||
parentName: 'system',
|
||||
|
|
@ -23,6 +27,13 @@ const systemRouter = {
|
|||
},
|
||||
component: () => import('@/views/system/mindmap/index.vue'),
|
||||
},
|
||||
{
|
||||
path: '/system/advanced-flows',
|
||||
name: 'advancedFlows',
|
||||
meta: { title: '高级编排', activeMenu: '/system', parentPath: '/system', parentName: 'system', permission: [RoleConst.ADMIN] },
|
||||
component: () => import('@/views/system/mindmap/AdvancedFlowsPage.vue'),
|
||||
},
|
||||
{ path: '/system/mindmap', redirect: (to: any) => ({ path: '/system/services', query: { ...to.query, tab: 'mindmap' } }) },
|
||||
{
|
||||
path: '/system/user',
|
||||
name: 'user',
|
||||
|
|
|
|||
|
|
@ -35,6 +35,7 @@
|
|||
<AppIcon iconName="app-debug-outlined" class="mr-4"></AppIcon>
|
||||
{{ $t('views.applicationWorkflow.setting.debug') }}
|
||||
</el-button>
|
||||
<el-button @click="engineRunVisible = true" v-if="permissionPrecise.edit(id)">后台任务</el-button>
|
||||
<el-button @click="saveApplication(true)" v-if="permissionPrecise.edit(id)">
|
||||
<AppIcon iconName="app-save-outlined" class="mr-4"></AppIcon>
|
||||
{{ $t('common.save') }}
|
||||
|
|
@ -139,8 +140,19 @@
|
|||
@refreshVersion="refreshVersion"
|
||||
/>
|
||||
</div>
|
||||
<el-dialog v-model="engineRunVisible" title="提交流程任务" width="520px">
|
||||
<el-form label-position="top"><el-form-item label="输入内容"><el-input v-model="engineQuestion" type="textarea" :rows="4" /></el-form-item>
|
||||
<el-form-item label="输入文件"><input type="file" @change="engineFile = ($event.target as HTMLInputElement).files?.[0]" /></el-form-item>
|
||||
<el-form-item label="优先级(0~9)"><el-input-number v-model="enginePriority" :min="0" :max="9" /></el-form-item>
|
||||
<el-text type="info">运行已发布版本;草稿修改需先发布。</el-text>
|
||||
<p v-if="engineTask">任务 {{ engineTask.id }}:{{ engineTask.status }} {{ engineTask.error }}</p>
|
||||
<pre v-if="engineTask?.result?.text" style="white-space:pre-wrap">{{ engineTask.result.text }}</pre>
|
||||
</el-form>
|
||||
<template #footer><el-button @click="engineRunVisible = false">关闭</el-button><el-button type="primary" :loading="engineSubmitting" @click="submitEngineTask">提交任务</el-button></template>
|
||||
</el-dialog>
|
||||
</template>
|
||||
<script setup lang="ts">
|
||||
import engineApi from '@/api/system/mcp-engine'
|
||||
import { ref, onMounted, onBeforeUnmount, computed, nextTick, provide } from 'vue'
|
||||
import { useRouter, useRoute } from 'vue-router'
|
||||
import type { Action } from 'element-plus'
|
||||
|
|
@ -163,6 +175,21 @@ import { WorkflowMode } from '@/enums/application'
|
|||
provide('getApplicationDetail', () => detail)
|
||||
provide('workflowMode', WorkflowMode.Application)
|
||||
const { theme } = useStore()
|
||||
const engineRunVisible = ref(false), engineQuestion = ref(''), enginePriority = ref(5), engineSubmitting = ref(false), engineTask = ref<any>()
|
||||
let engineFile: File | undefined, engineTimer: ReturnType<typeof setInterval> | undefined
|
||||
async function submitEngineTask() {
|
||||
engineSubmitting.value = true
|
||||
try {
|
||||
engineTask.value = (await engineApi.submit({application_id: route.params.id,question:engineQuestion.value,priority:enginePriority.value},engineFile)).data
|
||||
if(engineTimer)clearInterval(engineTimer)
|
||||
engineTimer=setInterval(async()=>{
|
||||
const task=(await engineApi.list('tasks',{id:engineTask.value.id})).data
|
||||
engineTask.value=task
|
||||
if(!['PENDING','RUNNING'].includes(task.status)&&engineTimer)clearInterval(engineTimer)
|
||||
},2000)
|
||||
} finally { engineSubmitting.value=false }
|
||||
}
|
||||
onBeforeUnmount(()=>{if(engineTimer)clearInterval(engineTimer)})
|
||||
const router = useRouter()
|
||||
const route = useRoute()
|
||||
const {
|
||||
|
|
|
|||
|
|
@ -54,6 +54,15 @@ const dynamicsFormRef = ref<InstanceType<typeof DynamicsForm>>()
|
|||
const form_data = ref<any>({})
|
||||
const dialogVisible = ref(false)
|
||||
const loading = ref(false)
|
||||
// These fields configure the mind-map summarization pipeline, not chat generation.
|
||||
const summaryFields = new Set([
|
||||
'context_tokens', 'max_input_tokens', 'input_tokens', 'max_output_tokens',
|
||||
'output_tokens', 'safety_tokens', 'retries', 'concurrency', 'max_depth',
|
||||
'max_children', 'node_max_chars', 'detail', 'structure_mode', 'parallel',
|
||||
'auto_split', 'tokenizer_path', 'supported_parameters', 'summary_temperature',
|
||||
'summary_enable_thinking',
|
||||
])
|
||||
const chatFields = (fields: FormField[]) => fields.filter((field) => !summaryFields.has(field.field))
|
||||
const getApi = (model_id: string, application_id?: string) => {
|
||||
return loadSharedApi({ type: 'model', systemType: apiType.value }).getModelParamsForm(
|
||||
model_id,
|
||||
|
|
@ -72,7 +81,7 @@ const open = (model_id: string, application_id?: string, model_setting_data?: an
|
|||
form_data.value = {}
|
||||
const api = getApi(model_id, application_id)
|
||||
api.then((ok: any) => {
|
||||
model_form_field.value = ok.data
|
||||
model_form_field.value = chatFields(ok.data)
|
||||
// 渲染动态表单
|
||||
dynamicsFormRef.value?.render(model_form_field.value, model_setting_data)
|
||||
})
|
||||
|
|
@ -82,8 +91,8 @@ const open = (model_id: string, application_id?: string, model_setting_data?: an
|
|||
const reset_default = (model_id: string, application_id?: string) => {
|
||||
const api = getApi(model_id, application_id)
|
||||
api.then((ok: any) => {
|
||||
model_form_field.value = ok.data
|
||||
const model_setting_data = ok.data
|
||||
model_form_field.value = chatFields(ok.data)
|
||||
const model_setting_data = model_form_field.value
|
||||
.map((item: any) => {
|
||||
if (item.show_default_value === false) {
|
||||
return { [item.field]: undefined }
|
||||
|
|
|
|||
|
|
@ -175,7 +175,7 @@ const form = reactive<{
|
|||
[propName: string]: any
|
||||
}>({
|
||||
patterns: [],
|
||||
limit: 4096,
|
||||
limit: 500,
|
||||
overlap: 0,
|
||||
with_filter: true,
|
||||
})
|
||||
|
|
@ -274,7 +274,7 @@ onMounted(async () => {
|
|||
const res = await loadSharedApi({ type: 'knowledge', systemType: apiType.value })
|
||||
.getKnowledgeDetail(id, loading)
|
||||
const defaults = res.data?.meta?.segmentation || {}
|
||||
form.limit = defaults.chunk_size ?? 4096
|
||||
form.limit = defaults.chunk_size ?? 500
|
||||
form.overlap = defaults.chunk_overlap ?? 0
|
||||
rulesReady.value = true
|
||||
splitDocument()
|
||||
|
|
|
|||
|
|
@ -2,18 +2,21 @@
|
|||
<div class="p-16-24">
|
||||
<div class="flex-between mb-16">
|
||||
<h2>{{ $t('common.setting') }}</h2>
|
||||
<KnowledgeVersions
|
||||
:knowledge-id="String(id)"
|
||||
:can-edit="!route.path.includes('share/') && permissionPrecise.edit(id)"
|
||||
/>
|
||||
<el-button
|
||||
v-if="!route.path.includes('share/') && permissionPrecise.edit(id)"
|
||||
type="primary"
|
||||
:loading="saving"
|
||||
:disabled="!settingsReady"
|
||||
@click="submit"
|
||||
>保存</el-button
|
||||
>
|
||||
<div class="flex align-center" style="gap: 12px">
|
||||
<KnowledgeVersions
|
||||
:knowledge-id="String(id)"
|
||||
:can-edit="!route.path.includes('share/') && permissionPrecise.edit(id)"
|
||||
@changed="getDetail"
|
||||
/>
|
||||
<el-button
|
||||
v-if="!route.path.includes('share/') && permissionPrecise.edit(id)"
|
||||
type="primary"
|
||||
:loading="saving"
|
||||
:disabled="!settingsReady"
|
||||
@click="submit"
|
||||
>保存</el-button
|
||||
>
|
||||
</div>
|
||||
</div>
|
||||
<el-card style="--el-card-padding: 0">
|
||||
<div class="knowledge-setting main-calc-height">
|
||||
|
|
@ -247,7 +250,11 @@
|
|||
:apiType="apiType"
|
||||
section="embedding"
|
||||
:form-value="baseSettings"
|
||||
:embedding-disabled="Boolean(detail?.embedding_change_block_reason)"
|
||||
/>
|
||||
<el-text v-if="detail?.embedding_change_block_reason" type="info">
|
||||
{{ detail.embedding_change_block_reason }}
|
||||
</el-text>
|
||||
<el-form label-position="top" class="mt-16">
|
||||
<el-row :gutter="24">
|
||||
<el-col :span="12">
|
||||
|
|
@ -263,7 +270,7 @@
|
|||
</el-col>
|
||||
<el-col :span="12">
|
||||
<el-form-item label="输出归一化">
|
||||
<el-checkbox v-model="embeddingParams.normalize">启用</el-checkbox>
|
||||
<el-checkbox v-model="embeddingParams.normalize" :disabled="Boolean(detail?.embedding_change_block_reason)">启用</el-checkbox>
|
||||
</el-form-item>
|
||||
</el-col>
|
||||
</el-row>
|
||||
|
|
@ -272,7 +279,7 @@
|
|||
<el-button
|
||||
link
|
||||
type="primary"
|
||||
:disabled="optionalEmbeddingParams.length >= 50"
|
||||
:disabled="Boolean(detail?.embedding_change_block_reason) || optionalEmbeddingParams.length >= 50"
|
||||
@click="addEmbeddingParam"
|
||||
>添加参数</el-button
|
||||
>
|
||||
|
|
@ -287,10 +294,11 @@
|
|||
v-model="param.key"
|
||||
placeholder="参数名称"
|
||||
:maxlength="64"
|
||||
:disabled="Boolean(detail?.embedding_change_block_reason)"
|
||||
style="width: 180px; flex-shrink: 0"
|
||||
/>
|
||||
<el-input v-model="param.value" placeholder="参数值" :maxlength="2000" />
|
||||
<el-button link type="danger" @click="optionalEmbeddingParams.splice(index, 1)"
|
||||
<el-input v-model="param.value" placeholder="参数值" :maxlength="2000" :disabled="Boolean(detail?.embedding_change_block_reason)" />
|
||||
<el-button link type="danger" :disabled="Boolean(detail?.embedding_change_block_reason)" @click="optionalEmbeddingParams.splice(index, 1)"
|
||||
>删除</el-button
|
||||
>
|
||||
</div>
|
||||
|
|
@ -499,7 +507,7 @@ const settingsReady = computed(
|
|||
)
|
||||
const detail = ref<any>({})
|
||||
const cloneModelId = ref('')
|
||||
const embeddingParams = reactive({ batch_size: 10, normalize: false })
|
||||
const embeddingParams = reactive({ batch_size: 10, normalize: true })
|
||||
const embeddingParamLabels: Record<string, string> = {
|
||||
query_prefix: '查询指令',
|
||||
document_prefix: '文档指令',
|
||||
|
|
@ -541,7 +549,7 @@ const metadataForm = ref<Array<{ name: string; value: string }>>([])
|
|||
const metadataCreator = ref('')
|
||||
const metadataReady = ref(false)
|
||||
const segmentation = ref<any>({
|
||||
chunk_size: 4096,
|
||||
chunk_size: 500,
|
||||
chunk_overlap: 0,
|
||||
updated_by: '',
|
||||
updated_at: '',
|
||||
|
|
@ -568,7 +576,7 @@ function editMetadata() {
|
|||
function loadSegmentation() {
|
||||
if (!id) return
|
||||
return knowledgeApi.getKnowledgeSegmentation(id, segmentationLoading).then((res: any) => {
|
||||
segmentation.value = res.data || { chunk_size: 4096, chunk_overlap: 0 }
|
||||
segmentation.value = res.data || { chunk_size: 500, chunk_overlap: 0 }
|
||||
savedSegmentation.value = JSON.stringify({
|
||||
chunk_size: segmentation.value.chunk_size,
|
||||
chunk_overlap: segmentation.value.chunk_overlap,
|
||||
|
|
@ -824,7 +832,7 @@ function getDetail() {
|
|||
const params = res.data?.meta?.embedding_params || {}
|
||||
Object.assign(embeddingParams, {
|
||||
batch_size: params.batch_size ?? 10,
|
||||
normalize: params.normalize ?? false,
|
||||
normalize: params.normalize ?? true,
|
||||
})
|
||||
optionalEmbeddingParams.value = Object.entries(params)
|
||||
.filter(([key]) => !['batch_size', 'normalize'].includes(key))
|
||||
|
|
|
|||
|
|
@ -45,6 +45,7 @@
|
|||
:placeholder="$t('views.knowledge.form.EmbeddingModel.placeholder')"
|
||||
:options="modelOptions"
|
||||
:model-type="'EMBEDDING'"
|
||||
:disabled="embeddingDisabled"
|
||||
showFooter
|
||||
></ModelSelect>
|
||||
</el-form-item>
|
||||
|
|
@ -63,6 +64,7 @@ const props = defineProps<{
|
|||
}
|
||||
apiType: 'systemShare' | 'workspace' | 'systemManage'
|
||||
section?: 'basic' | 'embedding'
|
||||
embeddingDisabled?: boolean
|
||||
formValue?: knowledgeData
|
||||
}>()
|
||||
const form = ref<knowledgeData>(props.formValue || {
|
||||
|
|
|
|||
|
|
@ -1,10 +1,10 @@
|
|||
<template>
|
||||
<div class="hub-manager" v-loading="loading">
|
||||
<el-tabs v-model="tab" @tab-change="load">
|
||||
<el-tab-pane label="服务" name="services" />
|
||||
<el-tab-pane label="流程" name="flows" />
|
||||
<el-tab-pane label="任务" name="tasks" />
|
||||
<el-tab-pane label="图谱" name="artifacts" />
|
||||
<div class="hub-manager" :class="{ embedded: hideTabs }" v-loading="loading">
|
||||
<el-tabs v-if="!hideTabs" v-model="tab" @tab-change="load">
|
||||
<el-tab-pane label="导图服务" name="services" />
|
||||
<el-tab-pane label="导图处理流程" name="flows" />
|
||||
<el-tab-pane label="运行记录" name="tasks" />
|
||||
<el-tab-pane label="导图管理" name="artifacts" />
|
||||
</el-tabs>
|
||||
<div class="mb-16">
|
||||
<el-button
|
||||
|
|
@ -14,11 +14,16 @@
|
|||
>{{ tab === 'services' ? '发布服务' : '新建流程' }}</el-button
|
||||
>
|
||||
<el-button @click="load">刷新</el-button>
|
||||
<template v-if="tab === 'artifacts' && resources.is_admin">
|
||||
<span class="ml-16 mr-8">每人保存上限</span>
|
||||
<el-input-number v-model="artifactLimit" :min="1" :max="10000" :precision="0" controls-position="right" />
|
||||
<el-button class="ml-8" :loading="limitSaving" @click="saveArtifactLimit">保存</el-button>
|
||||
</template>
|
||||
</div>
|
||||
<el-table :data="rows" row-key="id">
|
||||
<el-table :data="pagedRows" row-key="id">
|
||||
<el-table-column prop="name" label="名称" min-width="180" />
|
||||
<el-table-column
|
||||
v-if="tab === 'flows' || tab === 'artifacts'"
|
||||
v-if="tab === 'flows'"
|
||||
prop="revision"
|
||||
label="版本"
|
||||
width="90"
|
||||
|
|
@ -37,14 +42,18 @@
|
|||
<el-table-column label="操作" min-width="300"
|
||||
><template #default="{ row }">
|
||||
<el-button link type="primary" @click="open(row)">{{
|
||||
tab === 'flows' ? '查看 / 新版本' : tab === 'artifacts' ? '打开 / 复核' : '详情'
|
||||
tab === 'flows' ? '查看流程' : tab === 'artifacts' ? '查看导图' : tab === 'services' ? '接口信息' : '运行详情'
|
||||
}}</el-button>
|
||||
<el-button v-if="tab === 'flows'" link type="primary" @click="openFlowSubmit(row)"
|
||||
>试运行</el-button
|
||||
>
|
||||
<template
|
||||
v-if="tab === 'services' && (resources.is_admin || row.actions?.includes('EDIT'))"
|
||||
>
|
||||
<el-button link type="primary" @click="toggle(row)">{{
|
||||
row.enabled ? '停用' : '启用'
|
||||
}}</el-button>
|
||||
<el-button link type="primary" @click="showCalls(row)">调用日志</el-button>
|
||||
<el-button link type="primary" @click="rotate(row)">更新Key</el-button>
|
||||
<el-button v-if="row.key_configured" link @click="revoke(row)">撤销Key</el-button>
|
||||
</template>
|
||||
|
|
@ -66,15 +75,16 @@
|
|||
</template></el-table-column
|
||||
>
|
||||
</el-table>
|
||||
<el-dialog v-model="dialog" :title="title" width="740px" destroy-on-close>
|
||||
<SystemPagination v-model:current-page="listPage" v-model:page-size="listPageSize" :total="rows.length" />
|
||||
<el-dialog v-model="dialog" :title="title" width="740px" destroy-on-close @closed="runKey = ''">
|
||||
<el-form label-position="top">
|
||||
<el-form-item v-if="tab !== 'tasks'" label="名称"
|
||||
><el-input v-model="form.name" maxlength="128"
|
||||
><el-input v-model="form.name" maxlength="128" :disabled="tab === 'services' && !canEdit"
|
||||
/></el-form-item>
|
||||
<template v-if="tab === 'flows'">
|
||||
<el-form-item label="文档解析"
|
||||
><el-select v-model="form.parser_id" clearable placeholder="不解析文件"
|
||||
><el-option
|
||||
><el-option label="内置解析" value="builtin" /><el-option
|
||||
v-for="p in resources.parsers"
|
||||
:key="p.id"
|
||||
:label="p.name"
|
||||
|
|
@ -84,47 +94,103 @@
|
|||
><el-select v-model="form.model_id" clearable placeholder="按原文生成"
|
||||
><el-option v-for="m in llms" :key="m.id" :label="m.name" :value="m.id" /></el-select
|
||||
></el-form-item>
|
||||
<el-form-item v-if="form.model_id" label="提示词"
|
||||
><el-input v-model="form.prompt" type="textarea" :rows="4"
|
||||
/></el-form-item>
|
||||
<el-form-item v-if="form.model_id" label="温度"
|
||||
><el-input-number v-model="form.temperature" :min="0" :max="2" :step="0.1"
|
||||
/></el-form-item>
|
||||
<el-form-item label="输出">思维导图</el-form-item>
|
||||
<el-collapse v-if="form.model_id"
|
||||
><el-collapse-item title="高级参数"
|
||||
><el-form-item label="提示词"
|
||||
><el-input v-model="form.prompt" type="textarea" :rows="4"
|
||||
/></el-form-item>
|
||||
<el-form-item label="温度"
|
||||
><el-input-number v-model="form.temperature" :min="0" :max="2" :step="0.1"
|
||||
/></el-form-item> </el-collapse-item></el-collapse
|
||||
><el-form-item label="输出">思维导图</el-form-item>
|
||||
</template>
|
||||
<template v-if="tab === 'services'">
|
||||
<el-form-item label="流程版本"
|
||||
><el-select v-model="form.flow_id" :disabled="!!form.id"
|
||||
><el-select v-model="form.flow_id" :disabled="!!form.id || !canEdit"
|
||||
><el-option
|
||||
v-for="f in flows"
|
||||
:key="f.id"
|
||||
:label="`${f.name} · v${f.revision}`"
|
||||
:value="f.id" /></el-select
|
||||
></el-form-item>
|
||||
<el-form-item v-if="!form.id" label="每分钟调用次数"
|
||||
><el-input-number v-model="form.requests_per_minute" :min="1" :max="600"
|
||||
<el-form-item label="每分钟调用次数"
|
||||
><el-input-number
|
||||
v-model="form.requests_per_minute"
|
||||
:min="1"
|
||||
:max="600"
|
||||
:disabled="!canEdit"
|
||||
/></el-form-item>
|
||||
<el-form-item label="请求方法"
|
||||
><el-select v-model="form.api_config.method" :disabled="!canEdit" @change="changeMethod"
|
||||
><el-option label="POST" value="POST" /><el-option
|
||||
label="GET"
|
||||
value="GET" /></el-select
|
||||
></el-form-item>
|
||||
<el-form-item label="请求参数">
|
||||
<el-table :data="form.api_config.parameters">
|
||||
<el-table-column label="业务字段" width="110"
|
||||
><template #default="{ row }">{{ parameterLabels[row.target] }}</template></el-table-column
|
||||
>
|
||||
<el-table-column label="参数名称" min-width="150"
|
||||
><template #default="{ row }"
|
||||
><el-input v-model="row.name" :disabled="!canEdit" /></template
|
||||
></el-table-column>
|
||||
<el-table-column label="类型" width="90"
|
||||
><template #default="{ row }">{{ row.type === 'file' ? '文件' : '文本' }}</template></el-table-column
|
||||
>
|
||||
<el-table-column label="必填" width="80"
|
||||
><template #default="{ row }"
|
||||
><el-switch v-model="row.required" :disabled="!canEdit" /></template
|
||||
></el-table-column>
|
||||
<el-table-column label="操作" width="70"
|
||||
><template #default="{ $index }"
|
||||
><el-button
|
||||
link
|
||||
type="danger"
|
||||
:disabled="!canEdit"
|
||||
@click="form.api_config.parameters.splice($index, 1)"
|
||||
>删除</el-button
|
||||
></template
|
||||
></el-table-column
|
||||
>
|
||||
</el-table>
|
||||
<el-select
|
||||
v-if="canEdit && availableTargets.length"
|
||||
placeholder="添加参数"
|
||||
@change="addParameter"
|
||||
><el-option
|
||||
v-for="target in availableTargets"
|
||||
:key="target"
|
||||
:label="parameterLabels[target]"
|
||||
:value="target"
|
||||
/></el-select>
|
||||
</el-form-item>
|
||||
<el-form-item v-if="form.id" label="服务接口"
|
||||
><el-input :model-value="serviceUrl(form)" readonly
|
||||
/></el-form-item>
|
||||
<el-form-item
|
||||
v-if="form.id && (resources.is_admin || form.actions?.includes('USE'))"
|
||||
label="调用"
|
||||
><el-input
|
||||
v-model="runMarkdown"
|
||||
type="textarea"
|
||||
:rows="3"
|
||||
placeholder="Markdown"
|
||||
/><el-input
|
||||
v-model="runKey"
|
||||
type="password"
|
||||
show-password
|
||||
placeholder="服务Key(如已启用)"
|
||||
/><input
|
||||
type="file"
|
||||
@change="runFile = ($event.target as HTMLInputElement).files?.[0] || null"
|
||||
/><el-button @click="runService">提交任务</el-button></el-form-item
|
||||
>
|
||||
<template v-if="form.id && (resources.is_admin || form.actions?.includes('USE'))">
|
||||
<el-form-item
|
||||
v-for="param in form.api_config.parameters"
|
||||
:key="param.target"
|
||||
:label="param.name"
|
||||
:required="param.required"
|
||||
>
|
||||
<input
|
||||
v-if="param.type === 'file'"
|
||||
type="file"
|
||||
@change="runFile = ($event.target as HTMLInputElement).files?.[0] || null"
|
||||
/>
|
||||
<el-input
|
||||
v-else
|
||||
v-model="runValues[param.target]"
|
||||
:type="param.target === 'markdown' ? 'textarea' : 'text'"
|
||||
/>
|
||||
</el-form-item>
|
||||
<el-form-item label="服务Key"
|
||||
><el-input v-model="runKey" type="password" show-password
|
||||
/></el-form-item>
|
||||
<el-button @click="runService">调用服务</el-button>
|
||||
</template>
|
||||
</template>
|
||||
<el-form-item
|
||||
v-if="tab === 'services' && (resources.is_admin || form.actions?.includes('AUTHORIZE'))"
|
||||
|
|
@ -161,7 +227,7 @@
|
|||
><el-input v-model="reviewMarkdown" type="textarea" :rows="8"
|
||||
/></el-form-item>
|
||||
<el-button v-if="form.status === 'SUCCESS'" type="primary" @click="saveTaskResult"
|
||||
>确认保存图谱</el-button
|
||||
>保存导图</el-button
|
||||
>
|
||||
</template>
|
||||
<template v-if="tab === 'artifacts'">
|
||||
|
|
@ -179,14 +245,6 @@
|
|||
:label="name"
|
||||
:value="value" /></el-select
|
||||
></el-form-item>
|
||||
<el-form-item label="历史修订"
|
||||
><el-select placeholder="查看历史" @change="showHistory"
|
||||
><el-option
|
||||
v-for="h in form.history || []"
|
||||
:key="h.revision"
|
||||
:label="`v${h.revision}`"
|
||||
:value="h.revision" /></el-select
|
||||
></el-form-item>
|
||||
<el-button @click="openWorkbench">在工作台打开</el-button>
|
||||
</template>
|
||||
</el-form>
|
||||
|
|
@ -197,31 +255,222 @@
|
|||
}}</el-button></template
|
||||
>
|
||||
</el-dialog>
|
||||
<el-dialog v-model="flowSubmitDialog" title="试运行流程" width="600px" destroy-on-close>
|
||||
<el-form label-position="top">
|
||||
<el-form-item label="Markdown"
|
||||
><el-input v-model="flowMarkdown" type="textarea" :rows="6"
|
||||
/></el-form-item>
|
||||
<el-form-item label="文件"
|
||||
><input
|
||||
type="file"
|
||||
@change="flowFile = ($event.target as HTMLInputElement).files?.[0] || null"
|
||||
/></el-form-item>
|
||||
<el-collapse
|
||||
><el-collapse-item title="高级参数"
|
||||
><el-form-item label="优先级"
|
||||
><el-input-number
|
||||
v-model="flowPriority"
|
||||
:min="0"
|
||||
:max="9" /></el-form-item></el-collapse-item
|
||||
></el-collapse>
|
||||
</el-form>
|
||||
<template #footer
|
||||
><el-button @click="flowSubmitDialog = false">取消</el-button
|
||||
><el-button type="primary" :loading="flowSubmitting" @click="submitFlow"
|
||||
>提交任务</el-button
|
||||
></template
|
||||
>
|
||||
</el-dialog>
|
||||
<el-dialog v-model="callsDialog" title="调用日志" width="900px">
|
||||
<el-button @click="loadCalls">刷新</el-button>
|
||||
<el-table :data="calls" v-loading="callsLoading">
|
||||
<el-table-column prop="created_at" label="时间" min-width="160" /><el-table-column
|
||||
prop="method"
|
||||
label="方法"
|
||||
width="75"
|
||||
/>
|
||||
<el-table-column label="状态" width="100"
|
||||
><template #default="{ row }">{{
|
||||
statuses[row.status] || row.status
|
||||
}}</template></el-table-column
|
||||
>
|
||||
<el-table-column prop="http_status" label="HTTP" width="75" />
|
||||
<el-table-column label="HTTP耗时" width="100"
|
||||
><template #default="{ row }">{{ duration(row.elapsed_ms) }}</template></el-table-column
|
||||
>
|
||||
<el-table-column prop="task_id" label="任务ID" min-width="180" show-overflow-tooltip />
|
||||
<el-table-column label="任务状态" width="100"
|
||||
><template #default="{ row }">{{
|
||||
statuses[row.task_status] || row.task_status || '-'
|
||||
}}</template></el-table-column
|
||||
>
|
||||
<el-table-column label="任务耗时" width="100"
|
||||
><template #default="{ row }">{{ duration(row.execution_ms) }}</template></el-table-column
|
||||
>
|
||||
<el-table-column label="错误" min-width="150" show-overflow-tooltip
|
||||
><template #default="{ row }">{{
|
||||
row.error ? String(row.error).slice(0, 120) : '-'
|
||||
}}</template></el-table-column
|
||||
>
|
||||
</el-table>
|
||||
<SystemPagination
|
||||
v-model:current-page="callsPage"
|
||||
v-model:page-size="callsPageSize"
|
||||
:total="callsTotal"
|
||||
@change="changeCallsPage"
|
||||
/>
|
||||
</el-dialog>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed, onMounted, ref } from 'vue'
|
||||
import { computed, onMounted, ref, watch } from 'vue'
|
||||
import { ElMessage, ElMessageBox } from 'element-plus'
|
||||
import { hubGet, hubPost, hubDelete, hubBase } from '@/api/ai-hub'
|
||||
import { request } from '@/request/index'
|
||||
const emit = defineEmits(['open-artifact'])
|
||||
import SystemPagination from '@/components/system-pagination/index.vue'
|
||||
const emit = defineEmits(['open-artifact', 'tab-change'])
|
||||
const props = withDefaults(defineProps<{initialTab?: 'services' | 'flows' | 'tasks' | 'artifacts'; hideTabs?: boolean}>(), {initialTab: 'services', hideTabs: false})
|
||||
function openWorkbench() {
|
||||
emit('open-artifact', form.value)
|
||||
dialog.value = false
|
||||
}
|
||||
const tab = ref('services'),
|
||||
const tab = ref(props.initialTab),
|
||||
loading = ref(false),
|
||||
saving = ref(false),
|
||||
dialog = ref(false)
|
||||
watch(tab, value => { listPage.value = 1; if (props.hideTabs) emit('tab-change', value) })
|
||||
const rows = ref<any[]>([]),
|
||||
flows = ref<any[]>([]),
|
||||
form = ref<any>({}),
|
||||
resources = ref<any>({ parsers: [], models: [] })
|
||||
const artifactLimit = ref(10), limitSaving = ref(false)
|
||||
async function saveArtifactLimit() {
|
||||
limitSaving.value = true
|
||||
try {
|
||||
artifactLimit.value = (await hubPost('artifact_limit', { limit: artifactLimit.value })).data.limit
|
||||
ElMessage.success('已保存')
|
||||
} finally { limitSaving.value = false }
|
||||
}
|
||||
const listPage = ref(1), listPageSize = ref(10)
|
||||
const pagedRows = computed(() => rows.value.slice((listPage.value - 1) * listPageSize.value, listPage.value * listPageSize.value))
|
||||
watch([() => rows.value.length, listPageSize], ([total, size]) => { listPage.value = Math.min(listPage.value, Math.max(1, Math.ceil(total / size))) })
|
||||
const reviewMarkdown = ref(''),
|
||||
runMarkdown = ref(''),
|
||||
runValues = ref<Record<string, string>>({}),
|
||||
runKey = ref(''),
|
||||
runFile = ref<File | null>(null)
|
||||
const flowSubmitDialog = ref(false),
|
||||
flowSubmitting = ref(false),
|
||||
flowId = ref(''),
|
||||
flowMarkdown = ref(''),
|
||||
flowFile = ref<File | null>(null),
|
||||
flowPriority = ref(5)
|
||||
function openFlowSubmit(row: any) {
|
||||
flowId.value = row.id
|
||||
flowMarkdown.value = ''
|
||||
flowFile.value = null
|
||||
flowPriority.value = 5
|
||||
flowSubmitDialog.value = true
|
||||
}
|
||||
async function submitFlow() {
|
||||
if (!flowMarkdown.value.trim() && !flowFile.value) {
|
||||
ElMessage.warning('请输入 Markdown 或选择文件')
|
||||
return
|
||||
}
|
||||
flowSubmitting.value = true
|
||||
try {
|
||||
const payload = {
|
||||
flow_id: flowId.value,
|
||||
markdown: flowMarkdown.value,
|
||||
priority: flowPriority.value,
|
||||
}
|
||||
if (flowFile.value) {
|
||||
const data = new FormData()
|
||||
data.append('data', JSON.stringify(payload))
|
||||
data.append('file', flowFile.value)
|
||||
await request({ url: `${hubBase()}/tasks`, method: 'post', data })
|
||||
} else await hubPost('tasks', payload)
|
||||
ElMessage.success('任务已提交')
|
||||
flowSubmitDialog.value = false
|
||||
tab.value = 'tasks'
|
||||
await load()
|
||||
} finally {
|
||||
flowSubmitting.value = false
|
||||
}
|
||||
}
|
||||
const callsDialog = ref(false),
|
||||
callsLoading = ref(false),
|
||||
callsPage = ref(1),
|
||||
callsPageSize = ref(20),
|
||||
callsTotal = ref(0)
|
||||
let previousCallsPageSize = 20
|
||||
function changeCallsPage(_page: number, size: number) {
|
||||
if (size !== previousCallsPageSize) callsPage.value = 1
|
||||
previousCallsPageSize = size
|
||||
loadCalls()
|
||||
}
|
||||
const calls = ref<any[]>([]),
|
||||
callsServiceId = ref('')
|
||||
const parameterLabels: Record<string, string> = { markdown: 'Markdown 内容', name: '任务名称', file: '输入文件' }
|
||||
const defaultApiConfig = () => ({
|
||||
method: 'POST',
|
||||
parameters: ['markdown', 'name', 'file'].map((target) => ({
|
||||
name: target,
|
||||
target,
|
||||
type: target === 'file' ? 'file' : 'string',
|
||||
required: false,
|
||||
})),
|
||||
})
|
||||
const canEdit = computed(
|
||||
() => !form.value.id || resources.value.is_admin || form.value.actions?.includes('EDIT'),
|
||||
)
|
||||
const availableTargets = computed(() =>
|
||||
['markdown', 'name', 'file'].filter(
|
||||
(target) =>
|
||||
!(target === 'file' && form.value.api_config?.method === 'GET') &&
|
||||
!form.value.api_config?.parameters?.some((p: any) => p.target === target),
|
||||
),
|
||||
)
|
||||
function changeMethod(method: string) {
|
||||
if (method === 'GET') {
|
||||
form.value.api_config.parameters = form.value.api_config.parameters.filter(
|
||||
(p: any) => p.target !== 'file',
|
||||
)
|
||||
runFile.value = null
|
||||
}
|
||||
}
|
||||
function addParameter(target: string) {
|
||||
form.value.api_config.parameters.push({
|
||||
name: target,
|
||||
target,
|
||||
type: target === 'file' ? 'file' : 'string',
|
||||
required: false,
|
||||
})
|
||||
}
|
||||
function duration(value: number | null) {
|
||||
return value == null ? '-' : `${value} ms`
|
||||
}
|
||||
async function showCalls(row: any) {
|
||||
callsServiceId.value = row.id
|
||||
callsPage.value = 1
|
||||
calls.value = []
|
||||
callsDialog.value = true
|
||||
await loadCalls()
|
||||
}
|
||||
async function loadCalls() {
|
||||
callsLoading.value = true
|
||||
try {
|
||||
const result = await request({
|
||||
url: `${hubBase()}/services/${callsServiceId.value}/calls`,
|
||||
method: 'get',
|
||||
params: { current_page: callsPage.value, page_size: callsPageSize.value },
|
||||
})
|
||||
calls.value = result.data.data.records || []
|
||||
callsTotal.value = result.data.data.total || 0
|
||||
} finally {
|
||||
callsLoading.value = false
|
||||
}
|
||||
}
|
||||
const llms = computed(() => resources.value.models.filter((m: any) => m.model_type === 'LLM'))
|
||||
const statuses: Record<string, string> = {
|
||||
PENDING: '待处理',
|
||||
|
|
@ -237,7 +486,7 @@ const nodeLabels: Record<string, string> = {
|
|||
const layouts = { tree: '树状' }
|
||||
const title = computed(
|
||||
() =>
|
||||
({ services: '导图服务', flows: '处理流程', tasks: '任务详情', artifacts: '图谱复核' })[
|
||||
({ services: '导图服务', flows: '导图处理流程', tasks: '运行详情', artifacts: '导图详情' })[
|
||||
tab.value as 'services'
|
||||
],
|
||||
)
|
||||
|
|
@ -257,6 +506,8 @@ async function load() {
|
|||
hubGet('flows'),
|
||||
])
|
||||
resources.value = r.data
|
||||
if (tab.value === 'artifacts' && resources.value.is_admin)
|
||||
artifactLimit.value = (await hubGet('artifact_limit')).data.limit
|
||||
rows.value = list.data
|
||||
flows.value = f.data
|
||||
} finally {
|
||||
|
|
@ -273,28 +524,45 @@ function create() {
|
|||
prompt: '保留原文事实、数值和语言,整理为分级Markdown标题和要点。',
|
||||
temperature: 0.2,
|
||||
requests_per_minute: 20,
|
||||
api_config: defaultApiConfig(),
|
||||
}
|
||||
dialog.value = true
|
||||
}
|
||||
async function open(row: any) {
|
||||
form.value = (await hubGet(tab.value, row.id)).data
|
||||
if (tab.value === 'services') form.value.api_config = form.value.api_config || defaultApiConfig()
|
||||
if (tab.value === 'flows') {
|
||||
const p = form.value.nodes.find((n: any) => n.type === 'parse'),
|
||||
m = form.value.nodes.find((n: any) => n.type === 'organize')
|
||||
Object.assign(form.value, {
|
||||
parser_id: p?.resource_id || '',
|
||||
parser_id: p?.processor === 'builtin' ? 'builtin' : p?.resource_id || '',
|
||||
model_id: m?.model_id || '',
|
||||
prompt: m?.prompt || '',
|
||||
temperature: m?.temperature ?? 0.2,
|
||||
})
|
||||
}
|
||||
reviewMarkdown.value = form.value.result?.markdown || ''
|
||||
runMarkdown.value = ''
|
||||
runValues.value = {}
|
||||
runKey.value = ''
|
||||
runFile.value = null
|
||||
dialog.value = true
|
||||
}
|
||||
async function save() {
|
||||
if (tab.value === 'services' && canEdit.value) {
|
||||
const config = form.value.api_config
|
||||
const names = config.parameters.map((p: any) => p.name.trim())
|
||||
if (names.some((name: string) => !name) || new Set(names).size !== names.length) {
|
||||
ElMessage.warning('参数名称不能为空或重复')
|
||||
return
|
||||
}
|
||||
if (config.method === 'GET' && config.parameters.some((p: any) => p.target === 'file')) {
|
||||
ElMessage.warning('GET 不支持文件参数')
|
||||
return
|
||||
}
|
||||
config.parameters.forEach((p: any) => {
|
||||
p.name = p.name.trim()
|
||||
})
|
||||
}
|
||||
saving.value = true
|
||||
try {
|
||||
let body: any = { ...form.value },
|
||||
|
|
@ -315,7 +583,11 @@ async function save() {
|
|||
body = { name: body.name, previous_id: id, nodes: [] }
|
||||
id = undefined
|
||||
if (form.value.parser_id)
|
||||
body.nodes.push({ type: 'parse', resource_id: form.value.parser_id })
|
||||
body.nodes.push(
|
||||
form.value.parser_id === 'builtin'
|
||||
? { type: 'parse', processor: 'builtin' }
|
||||
: { type: 'parse', resource_id: form.value.parser_id },
|
||||
)
|
||||
if (form.value.model_id)
|
||||
body.nodes.push({
|
||||
type: 'organize',
|
||||
|
|
@ -342,10 +614,15 @@ async function remove(row: any) {
|
|||
const effects: Record<string, string> = {
|
||||
services: '对应授权和服务Key将失效,已有任务与图谱保留。',
|
||||
flows: '已有任务与图谱保留。',
|
||||
tasks: '任务记录及输入文件将删除,已保存图谱保留。',
|
||||
artifacts: '图谱及其历史修订将删除,原任务保留。',
|
||||
tasks: '任务记录及输入文件将删除,已保存导图保留。',
|
||||
artifacts: '导图将删除,原任务保留。',
|
||||
}
|
||||
const confirmed = await ElMessageBox.confirm(`删除“${row.name}”?${effects[kind]}`, '确认删除', { type: 'warning' }).then(() => true, () => false)
|
||||
const confirmed = await ElMessageBox.confirm(`删除“${row.name}”?${effects[kind]}`, '确认删除', {
|
||||
type: 'warning',
|
||||
}).then(
|
||||
() => true,
|
||||
() => false,
|
||||
)
|
||||
if (!confirmed) return
|
||||
await hubDelete(kind, row.id)
|
||||
ElMessage.success('已删除')
|
||||
|
|
@ -370,16 +647,30 @@ function serviceUrl(row: any) {
|
|||
return `${location.origin}${import.meta.env.BASE_URL}api${hubBase()}/published/${row.id}/run`
|
||||
}
|
||||
async function runService() {
|
||||
const config = form.value.api_config
|
||||
if (config.method === 'GET' && config.parameters.some((p: any) => p.type === 'file')) {
|
||||
ElMessage.warning('GET 不支持文件参数')
|
||||
return
|
||||
}
|
||||
const values: Record<string, string | File> = {}
|
||||
for (const param of config.parameters) {
|
||||
const value = param.type === 'file' ? runFile.value : runValues.value[param.target]
|
||||
if (param.required && !value) {
|
||||
ElMessage.warning(`请填写${param.name}`)
|
||||
return
|
||||
}
|
||||
if (value) values[param.name] = value
|
||||
}
|
||||
const data = new FormData()
|
||||
data.append('data', JSON.stringify({ markdown: runMarkdown.value }))
|
||||
if (runFile.value) data.append('file', runFile.value)
|
||||
Object.entries(values).forEach(([name, value]) => data.append(name, value))
|
||||
await request({
|
||||
url: `${hubBase()}/published/${form.value.id}/run`,
|
||||
method: 'post',
|
||||
data,
|
||||
headers: { 'X-Service-Key': runKey.value },
|
||||
method: config.method.toLowerCase(),
|
||||
...(config.method === 'GET' ? { params: values } : { data }),
|
||||
headers: runKey.value ? { 'X-Service-Key': runKey.value } : {},
|
||||
timeout: 30000,
|
||||
})
|
||||
runKey.value = ''
|
||||
ElMessage.success('任务已提交')
|
||||
dialog.value = false
|
||||
tab.value = 'tasks'
|
||||
|
|
@ -397,18 +688,6 @@ async function saveTaskResult() {
|
|||
tab.value = 'artifacts'
|
||||
await load()
|
||||
}
|
||||
function showHistory(revision: number) {
|
||||
const h = form.value.history.find((item: any) => item.revision === revision)
|
||||
if (h)
|
||||
ElMessageBox.alert(h.markdown, `v${revision}`, {
|
||||
customStyle: {
|
||||
maxWidth: '80vw',
|
||||
whiteSpace: 'pre-wrap',
|
||||
maxHeight: '80vh',
|
||||
overflow: 'auto',
|
||||
},
|
||||
})
|
||||
}
|
||||
onMounted(load)
|
||||
</script>
|
||||
<style scoped>
|
||||
|
|
@ -417,6 +696,11 @@ onMounted(load)
|
|||
height: 100%;
|
||||
overflow: auto;
|
||||
}
|
||||
.hub-manager.embedded {
|
||||
padding: 0;
|
||||
height: auto;
|
||||
overflow: visible;
|
||||
}
|
||||
.el-select {
|
||||
width: 100%;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ export interface ParseParams {
|
|||
}
|
||||
|
||||
export interface ParseResult {
|
||||
task_id?: string
|
||||
backend: string
|
||||
version: string
|
||||
results: Record<
|
||||
|
|
@ -28,6 +29,8 @@ export interface ParseResult {
|
|||
model_output?: string
|
||||
content_list?: string
|
||||
images?: Record<string, string>
|
||||
original_md?: string
|
||||
heading_structure?: any
|
||||
}
|
||||
>
|
||||
}
|
||||
|
|
@ -41,7 +44,15 @@ export interface ParseProgress {
|
|||
}
|
||||
|
||||
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>()
|
||||
|
|
@ -52,7 +63,7 @@ const states: Record<string, ParseProgress['status']> = {
|
|||
FAILED: 'failed',
|
||||
}
|
||||
async function progress(taskId: string): Promise<MindmapOrganizeProgress> {
|
||||
const id = ids.get(taskId)
|
||||
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)
|
||||
|
|
@ -70,10 +81,40 @@ async function progress(taskId: string): Promise<MindmapOrganizeProgress> {
|
|||
: '失败'),
|
||||
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) {
|
||||
|
|
@ -108,6 +149,8 @@ export const documentApi = {
|
|||
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
|
||||
|
|
@ -116,7 +159,7 @@ export const documentApi = {
|
|||
}
|
||||
if (!completed) throw new Error('任务仍在处理中,请到管理页查看')
|
||||
}
|
||||
return { backend: 'shared', version: '1', results }
|
||||
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: '' }
|
||||
|
|
@ -127,19 +170,30 @@ export const documentApi = {
|
|||
markdown: string,
|
||||
_mode = 'smart',
|
||||
prompt?: string,
|
||||
config?: { model_id: string; temperature?: number | null },
|
||||
config?: { model_id: string; temperature?: number | null; options?: SummaryOptions },
|
||||
context?: { heading_structure?: any; original?: string },
|
||||
): Promise<MindmapOrganizeProgress> {
|
||||
if (!config?.model_id) throw new Error('请选择整理模型')
|
||||
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: '智能整理',
|
||||
name: '智能总结',
|
||||
markdown,
|
||||
nodes: [node, { type: 'render' }],
|
||||
...context,
|
||||
})
|
||||
ids.set(taskId, task.id)
|
||||
return progress(taskId)
|
||||
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)
|
||||
},
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
<template>
|
||||
<div class="config-panel">
|
||||
<div class="config-panel" :class="{ 'platform-config': platform }">
|
||||
<div v-if="!platform" class="export-hint">个人设置仅在当前页面会话有效,刷新后恢复平台默认。</div>
|
||||
<el-form label-position="top">
|
||||
<el-form-item label="PDF / 图片解析服务"
|
||||
><el-select v-model="draft.parserResourceId" placeholder="请选择"
|
||||
|
|
@ -9,21 +10,6 @@
|
|||
:label="p.name"
|
||||
:value="p.id" /></el-select
|
||||
></el-form-item>
|
||||
<el-form-item label="整理模型"
|
||||
><el-select v-model="draft.mindmapModelId" clearable placeholder="请选择"
|
||||
><el-option v-for="m in models" :key="m.id" :label="m.name" :value="m.id" /></el-select
|
||||
></el-form-item>
|
||||
<el-form-item label="温度(留空跟随模型配置)"
|
||||
><el-input-number
|
||||
v-model="draft.mindmapLlmTemperature"
|
||||
:min="0"
|
||||
:max="2"
|
||||
:step="0.1"
|
||||
placeholder="跟随模型配置"
|
||||
/></el-form-item>
|
||||
<el-form-item label="整理提示词"
|
||||
><el-input v-model="draft.mindmapPrompt" type="textarea" :rows="6"
|
||||
/></el-form-item>
|
||||
<el-form-item label="导出长边">
|
||||
<el-select v-model="draft.exportResolution" aria-label="导出长边">
|
||||
<el-option label="1024 px" :value="1024" />
|
||||
|
|
@ -33,10 +19,35 @@
|
|||
</el-select>
|
||||
<span class="export-hint">仅 PNG/JPEG;SVG 保留矢量尺寸。</span>
|
||||
</el-form-item>
|
||||
<div v-if="selectedModel" class="export-hint effective-summary-budget">
|
||||
本次输入 ≤ {{ effectiveInput.toLocaleString() }} Token · 输出 ≤ {{ effectiveOutput.toLocaleString() }} Token · 保留原文语言
|
||||
</div>
|
||||
<el-form-item label="总结模型"
|
||||
><el-select v-model="draft.mindmapModelId" clearable placeholder="请选择" @change="draft.summaryOptions.top_p = null"
|
||||
><el-option v-for="m in models" :key="m.id" :label="m.name" :value="m.id" /></el-select
|
||||
></el-form-item>
|
||||
<el-collapse v-model="advancedPanels">
|
||||
<el-collapse-item title="高级参数" name="advanced">
|
||||
<span class="export-hint">长度单位为 Token,留空使用默认值。</span>
|
||||
<el-form-item v-for="field in visibleBudgetFields" :key="field.key" :label="field.label">
|
||||
<el-input-number v-model="draft.summaryOptions[field.key]" :min="field.min" :max="field.max"
|
||||
:step="field.step || 1" :placeholder="modelDefault(field.key)" :aria-label="field.label" />
|
||||
</el-form-item>
|
||||
<el-form-item label="总结粒度">
|
||||
<el-select v-model="draft.summaryOptions.detail"><el-option label="精简" value="brief" /><el-option label="标准" value="standard" /><el-option label="详细" value="detailed" /></el-select>
|
||||
</el-form-item>
|
||||
<el-form-item label="结构策略">
|
||||
<el-select v-model="draft.summaryOptions.structure_mode"><el-option label="保留原目录" value="preserve" /><el-option label="按主题重组" value="reorganize" /></el-select>
|
||||
</el-form-item>
|
||||
<el-form-item label="总结提示词"
|
||||
><el-input v-model="draft.mindmapPrompt" type="textarea" :rows="6"
|
||||
/></el-form-item>
|
||||
</el-collapse-item>
|
||||
</el-collapse>
|
||||
<div v-if="error" class="error">{{ error }}</div>
|
||||
<div class="actions">
|
||||
<el-button @click="load">刷新资源</el-button
|
||||
><el-button type="primary" @click="confirm">确认</el-button>
|
||||
><el-button type="primary" :loading="saving" @click="confirm">{{ platform ? '保存' : '确认' }}</el-button>
|
||||
</div>
|
||||
</el-form>
|
||||
</div>
|
||||
|
|
@ -44,20 +55,54 @@
|
|||
<script setup lang="ts">
|
||||
import { computed, onMounted, reactive, ref, watch } from 'vue'
|
||||
import type { DocumentConfig } from '@/views/mindmap/workbench/composables/useDocumentProcessor'
|
||||
import type { SummaryOptions } from '@/views/mindmap/workbench/api/document'
|
||||
import { hubCall } from '@/views/mindmap/workbench/api/hub'
|
||||
import { DEFAULT_SUMMARY_BUDGET } from '@/views/mindmap/workbench/utils/contextDefaults'
|
||||
const props = defineProps<{
|
||||
modelValue: DocumentConfig
|
||||
backendOptions: any[]
|
||||
languageOptions: any[]
|
||||
platform?: boolean
|
||||
saving?: boolean
|
||||
}>()
|
||||
const emit = defineEmits(['update:modelValue', 'backendChange', 'close'])
|
||||
const draft = reactive({ ...props.modelValue })
|
||||
const emit = defineEmits(['update:modelValue', 'backendChange', 'close', 'confirm'])
|
||||
const copyConfig = (value: DocumentConfig) => ({ ...value, summaryOptions: { ...value.summaryOptions } })
|
||||
const draft = reactive(copyConfig(props.modelValue))
|
||||
const advancedPanels = ref<string[]>([])
|
||||
const resources = ref<any>({ parsers: [], models: [] }),
|
||||
error = ref('')
|
||||
const models = computed(() => resources.value.models.filter((m: any) => m.model_type === 'LLM'))
|
||||
const selectedModel = computed(() => models.value.find((m: any) => m.id === draft.mindmapModelId))
|
||||
type NumericKey = { [K in keyof SummaryOptions]: SummaryOptions[K] extends number | null ? K : never }[keyof SummaryOptions]
|
||||
const budgetFields: { key: NumericKey; label: string; min: number; max: number; step?: number }[] = [
|
||||
{ key: 'context_tokens', label: '上下文窗口', min: 4096, max: 2000000 },
|
||||
{ key: 'max_input_tokens', label: '最大输入长度', min: 1024, max: 2000000 },
|
||||
{ key: 'input_tokens', label: '本次输入上限', min: 1024, max: 2000000 },
|
||||
{ key: 'max_output_tokens', label: '最大输出长度', min: 256, max: 524288 },
|
||||
{ key: 'output_tokens', label: '本次输出上限', min: 256, max: 524288 },
|
||||
{ key: 'safety_tokens', label: '安全预留长度', min: 256, max: 200000 },
|
||||
{ key: 'timeout', label: '调用超时(秒)', min: 30, max: 600 },
|
||||
{ key: 'retries', label: '失败重试次数', min: 0, max: 2 },
|
||||
{ key: 'concurrency', label: '并发上限', min: 1, max: 8 },
|
||||
{ key: 'max_depth', label: '最大层级', min: 2, max: 6 },
|
||||
{ key: 'max_children', label: '子节点数量目标', min: 2, max: 30 },
|
||||
{ key: 'node_max_chars', label: '节点长度目标', min: 10, max: 300 },
|
||||
]
|
||||
const visibleBudgetFields = budgetFields.filter((field) => ['input_tokens', 'output_tokens'].includes(field.key))
|
||||
function modelDefault(key: string) {
|
||||
return String(selectedModel.value?.summary_defaults?.[key]
|
||||
?? DEFAULT_SUMMARY_BUDGET[key as keyof typeof DEFAULT_SUMMARY_BUDGET] ?? '跟随模型配置')
|
||||
}
|
||||
const effectiveOutput = computed(() => Number(draft.summaryOptions.output_tokens ?? selectedModel.value?.summary_defaults?.output_tokens ?? DEFAULT_SUMMARY_BUDGET.output_tokens))
|
||||
const effectiveInput = computed(() => Math.max(0, Math.min(
|
||||
Number(draft.summaryOptions.context_tokens ?? selectedModel.value?.summary_defaults?.context_tokens ?? DEFAULT_SUMMARY_BUDGET.context_tokens)
|
||||
- effectiveOutput.value - Number(draft.summaryOptions.safety_tokens ?? selectedModel.value?.summary_defaults?.safety_tokens ?? DEFAULT_SUMMARY_BUDGET.safety_tokens),
|
||||
Number(draft.summaryOptions.max_input_tokens ?? selectedModel.value?.summary_limits?.max_input_tokens ?? DEFAULT_SUMMARY_BUDGET.max_input_tokens),
|
||||
Number(draft.summaryOptions.input_tokens ?? selectedModel.value?.summary_defaults?.input_tokens ?? DEFAULT_SUMMARY_BUDGET.input_tokens),
|
||||
)))
|
||||
watch(
|
||||
() => props.modelValue,
|
||||
(value) => Object.assign(draft, value),
|
||||
(value) => Object.assign(draft, copyConfig(value)),
|
||||
{ deep: true },
|
||||
)
|
||||
async function load() {
|
||||
|
|
@ -67,14 +112,31 @@ async function load() {
|
|||
if (!draft.parserResourceId && resources.value.parsers?.length)
|
||||
draft.parserResourceId = resources.value.parsers[0].id
|
||||
if (!draft.mindmapModelId && models.value.length) draft.mindmapModelId = models.value[0].id
|
||||
if (draft.parserResourceId || draft.mindmapModelId) emit('update:modelValue', { ...draft })
|
||||
} catch (e: any) {
|
||||
error.value = e.message
|
||||
}
|
||||
}
|
||||
function confirm() {
|
||||
emit('update:modelValue', { ...draft })
|
||||
emit('close')
|
||||
const options = draft.summaryOptions
|
||||
for (const field of budgetFields) {
|
||||
const value = options[field.key]
|
||||
if (value != null && (!Number.isInteger(value) || value < field.min || value > field.max)) {
|
||||
error.value = `${field.label}超出范围`; return
|
||||
}
|
||||
}
|
||||
for (const key of ['context_tokens', 'max_input_tokens', 'max_output_tokens'] as const) {
|
||||
const limit = selectedModel.value?.summary_limits?.[key]
|
||||
if (limit && options[key] && options[key]! > Number(limit)) { error.value = '参数超过模型配置上限'; return }
|
||||
}
|
||||
const context = options.context_tokens ?? selectedModel.value?.summary_defaults?.context_tokens ?? DEFAULT_SUMMARY_BUDGET.context_tokens
|
||||
const output = options.output_tokens ?? selectedModel.value?.summary_defaults?.output_tokens ?? DEFAULT_SUMMARY_BUDGET.output_tokens
|
||||
const safety = options.safety_tokens ?? selectedModel.value?.summary_defaults?.safety_tokens ?? DEFAULT_SUMMARY_BUDGET.safety_tokens
|
||||
if (output + safety >= context || output > Number(options.max_output_tokens ?? selectedModel.value?.summary_limits?.max_output_tokens ?? DEFAULT_SUMMARY_BUDGET.max_output_tokens)) {
|
||||
error.value = '输出与预留长度超过模型预算'; return
|
||||
}
|
||||
emit('update:modelValue', copyConfig(draft))
|
||||
emit('confirm', copyConfig(draft))
|
||||
if (!props.platform) emit('close')
|
||||
}
|
||||
onMounted(load)
|
||||
</script>
|
||||
|
|
@ -82,6 +144,9 @@ onMounted(load)
|
|||
.config-panel {
|
||||
padding: 12px;
|
||||
}
|
||||
.config-panel.platform-config {
|
||||
padding: 0;
|
||||
}
|
||||
.el-select {
|
||||
width: 100%;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -38,7 +38,7 @@
|
|||
<div ref="htmlRef" class="markdown-content rendered-html" v-html="renderedContent"></div>
|
||||
</div>
|
||||
|
||||
<div v-else-if="mode === 'pdf'" class="render-shell">
|
||||
<div v-else-if="mode === 'pdf'" class="render-shell pdf-render-shell">
|
||||
<iframe v-if="pdfUrl" :src="pdfUrl" class="actual-pdf-preview" title="PDF预览"></iframe>
|
||||
<el-empty v-else description="点击生成预览,查看实际分页效果" />
|
||||
</div>
|
||||
|
|
@ -67,6 +67,7 @@ import { markdownEngine } from '@/views/mindmap/workbench/utils/graphModel'
|
|||
import { hubCall } from '@/views/mindmap/workbench/api/hub'
|
||||
import 'katex/dist/katex.min.css'
|
||||
import { useImageAssets, hasImageAssets, exportMarkdownBundle, embedImages } from '@/views/mindmap/workbench/utils/imageAssets'
|
||||
import { createExportImageCache, runLimited } from '@/views/mindmap/workbench/utils/exportPreparation'
|
||||
|
||||
type RenderMode = 'markdown' | 'html' | 'pdf' | 'richtext'
|
||||
type FlavorMode = 'commonmark' | 'gfm'
|
||||
|
|
@ -126,13 +127,18 @@ function createMarkdownRenderer(flavor: FlavorMode) {
|
|||
|
||||
const exporting = ref(false), pdfUrl = ref(''), exportStage = ref('')
|
||||
const exportCache = new Map<string, Blob>()
|
||||
const formulaImages = createExportImageCache()
|
||||
let pdfBlob: Blob | undefined, preparedHtml: Promise<string> | undefined, exportRevision = 0
|
||||
function revokePdf() { if (pdfUrl.value) URL.revokeObjectURL(pdfUrl.value); pdfUrl.value = ''; pdfBlob = undefined }
|
||||
function invalidateExports() { exportRevision++; exportCache.clear(); preparedHtml = undefined; revokePdf() }
|
||||
watch(() => props.content, invalidateExports)
|
||||
onBeforeUnmount(invalidateExports)
|
||||
onBeforeUnmount(() => { invalidateExports(); formulaImages.clear() })
|
||||
const elapsed = (start: number) => Math.round(performance.now() - start)
|
||||
let preparationTimings: Record<string, number> = {}
|
||||
async function prepareDocumentHtml() {
|
||||
const source = props.content
|
||||
const revision = exportRevision, timings: Record<string, number> = {}, started = performance.now()
|
||||
const checkCurrent = () => { if (revision !== exportRevision) throw new Error('内容已更新,请重新导出') }
|
||||
if (assetsLoading.value) {
|
||||
exportStage.value = '正在加载图片'
|
||||
await new Promise<void>(resolve => { const stop = watch(assetsLoading, loading => { if (!loading) { stop(); resolve() } }, { flush:'sync' }) })
|
||||
|
|
@ -147,31 +153,63 @@ async function prepareDocumentHtml() {
|
|||
document.body.appendChild(holder)
|
||||
try {
|
||||
await document.fonts.ready
|
||||
checkCurrent()
|
||||
timings.assetsAndFontsMs = elapsed(started)
|
||||
const formulas = Array.from(holder.querySelectorAll<HTMLElement>('.export-math'))
|
||||
if (formulas.length) {
|
||||
const formulaStarted = performance.now()
|
||||
exportStage.value = `正在处理公式 0/${formulas.length}`
|
||||
const { toPng, getFontEmbedCSS } = await import('html-to-image')
|
||||
const fontEmbedCSS = await getFontEmbedCSS(holder)
|
||||
const formulaImages = new Map<string, string>()
|
||||
for (const formula of formulas) {
|
||||
const fontEmbedCSS = await getFontEmbedCSS(holder, { preferredFontFormat: 'woff2' })
|
||||
// Include font content in the signature without retaining it in every cache key.
|
||||
let fontSignature = 2166136261
|
||||
for (let i = 0; i < fontEmbedCSS.length; i++) fontSignature = Math.imul(fontSignature ^ fontEmbedCSS.charCodeAt(i), 16777619)
|
||||
const jobs = formulas.map(formula => {
|
||||
const target = formula.querySelector<HTMLElement>('.katex') || formula
|
||||
target.style.display = 'inline-block'
|
||||
return { formula, target }
|
||||
}).map(({ formula, target }) => {
|
||||
const rect = target.getBoundingClientRect()
|
||||
const style = getComputedStyle(target)
|
||||
const width = Math.max(1, Math.ceil(rect.width)), height = Math.max(1, Math.ceil(rect.height))
|
||||
const key = JSON.stringify([formula.dataset.formula, width, height, !!formula.querySelector('.katex-display'), style.fontFamily, style.fontSize, style.fontWeight, style.fontStyle, style.letterSpacing, style.color, style.lineHeight, fontSignature])
|
||||
return { formula, target, width, height, key, src: '' }
|
||||
})
|
||||
let completed = 0
|
||||
await runLimited(jobs, 3, async job => {
|
||||
checkCurrent()
|
||||
job.src = await formulaImages.get(job.key, () => toPng(job.target, { width:job.width, height:job.height, pixelRatio:2, backgroundColor:'#ffffff', cacheBust:false, fontEmbedCSS }))
|
||||
checkCurrent()
|
||||
exportStage.value = `正在处理公式 ${++completed}/${jobs.length}`
|
||||
})
|
||||
// Replace only after all measurements and renders settle, avoiding layout churn.
|
||||
for (const { formula, src, width, height } of jobs) {
|
||||
const img = document.createElement('img')
|
||||
const key = `${formula.dataset.formula}:${rect.width}:${rect.height}`
|
||||
if (!formulaImages.has(key)) formulaImages.set(key, await toPng(target, { width:Math.max(1, Math.ceil(rect.width)), height:Math.max(1, Math.ceil(rect.height)), pixelRatio:2, backgroundColor:'#ffffff', cacheBust:false, fontEmbedCSS }))
|
||||
img.src = formulaImages.get(key)!
|
||||
img.width = Math.ceil(rect.width); img.height = Math.ceil(rect.height)
|
||||
img.src = src
|
||||
img.width = width; img.height = height
|
||||
img.alt = formula.dataset.formula || formula.textContent || '公式'
|
||||
formula.replaceWith(img)
|
||||
}
|
||||
timings.formulasMs = elapsed(formulaStarted)
|
||||
timings.formulaCount = formulas.length
|
||||
}
|
||||
return await embedImages(holder.innerHTML)
|
||||
checkCurrent()
|
||||
const imagesStarted = performance.now()
|
||||
const html = await embedImages(holder.innerHTML, false, (done, total) => {
|
||||
checkCurrent(); exportStage.value = `正在整理图片 ${done}/${total}`
|
||||
})
|
||||
checkCurrent()
|
||||
timings.imagesMs = elapsed(imagesStarted)
|
||||
timings.totalPreparationMs = elapsed(started)
|
||||
preparationTimings = timings
|
||||
return html
|
||||
} finally { holder.remove() }
|
||||
}
|
||||
async function documentBlob(format: 'docx' | 'pdf' | 'rtf') {
|
||||
const cached = exportCache.get(format)
|
||||
if (cached) return cached
|
||||
const revision = exportRevision
|
||||
const started = performance.now(), reusedPreparation = !!preparedHtml
|
||||
exportStage.value = '正在整理文档'
|
||||
if (!preparedHtml) {
|
||||
const pending = prepareDocumentHtml()
|
||||
|
|
@ -179,8 +217,14 @@ async function documentBlob(format: 'docx' | 'pdf' | 'rtf') {
|
|||
void pending.catch(() => { if (preparedHtml === pending) preparedHtml = undefined })
|
||||
}
|
||||
const html = await preparedHtml
|
||||
if (revision !== exportRevision) throw new Error('内容已更新,请重新导出')
|
||||
const preparationWaitMs = elapsed(started), requestStarted = performance.now()
|
||||
exportStage.value = `正在生成 ${format.toUpperCase()}`
|
||||
const result = await hubCall('document_export', 'post', { html, format, paper:'A4' })
|
||||
if (revision !== exportRevision) throw new Error('内容已更新,请重新导出')
|
||||
// Numeric metadata only: never log document text, formulas, images or credentials.
|
||||
console.info('[document-export]', { format, reusedPreparation, preparationWaitMs, ...preparationTimings,
|
||||
requestMs: elapsed(requestStarted), totalMs: elapsed(started), serverTimings: result.serverTimings || '' })
|
||||
const blob = new Blob([result.content], { type:result.mime })
|
||||
if (revision === exportRevision) exportCache.set(format, blob)
|
||||
return blob
|
||||
|
|
@ -240,7 +284,9 @@ function createFullHtmlDocument(innerHtml: string, title: string) {
|
|||
}
|
||||
|
||||
async function exportHtml(innerHtml: string, fileName: string) {
|
||||
const html = createFullHtmlDocument(await embedImages(innerHtml), fileName)
|
||||
const html = createFullHtmlDocument(await embedImages(innerHtml, false, (done, total) => {
|
||||
exportStage.value = `正在整理图片 ${done}/${total}`
|
||||
}), fileName)
|
||||
downloadBlob(new Blob([html], { type: 'text/html;charset=utf-8' }), fileName)
|
||||
}
|
||||
|
||||
|
|
@ -285,7 +331,8 @@ async function handleDownload() {
|
|||
|
||||
<style scoped>
|
||||
.export-stage { font-size:12px; color:var(--el-text-color-secondary); }
|
||||
.actual-pdf-preview { display:block; width:100%; height:100%; min-height:520px; border:0; }
|
||||
.actual-pdf-preview { display:block; width:100%; flex:1; min-height:0; border:0; }
|
||||
.pdf-render-shell { height:100%; min-height:0; }
|
||||
.markdown-renderer {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
|
|
|
|||
|
|
@ -37,10 +37,12 @@
|
|||
<el-option label="不自动换行" :value="0" />
|
||||
</el-select>
|
||||
<el-switch v-model="showImages" active-text="显示图片" aria-label="显示图片" size="small" />
|
||||
<el-button size="small" @click="resetView"><el-icon><Refresh /></el-icon><span>重置视图</span></el-button>
|
||||
</div>
|
||||
</el-popover>
|
||||
<span class="content-toolbar-meta" :title="nodeSummary">{{ totalNodeCount }} 节点<span v-if="hiddenNodeCount"> · 隐藏 {{ hiddenNodeCount }}</span></span>
|
||||
<el-button size="small" :disabled="!editable || !content" @click="emit('heading-edit')">标题层级</el-button>
|
||||
<el-button size="small" @click="resetView"><el-icon><Refresh /></el-icon><span>重置视图</span></el-button>
|
||||
<span v-if="isExporting" class="content-toolbar-meta">{{ exportStage }}</span>
|
||||
<el-popover v-model:visible="downloadVisible" trigger="click" placement="bottom-end" :width="220">
|
||||
<template #reference>
|
||||
<el-button type="primary" size="small" :disabled="!content || isRendering || isExporting" aria-label="下载">
|
||||
|
|
@ -94,6 +96,7 @@ const props = defineProps({
|
|||
title: { type: String, default: '思维导图' },
|
||||
sourceName: { type: String, default: '未命名文档' },
|
||||
editable: { type: Boolean, default: true },
|
||||
excludedLines: { type: Array as () => number[], default: () => [] },
|
||||
content: {
|
||||
type: String,
|
||||
default: ''
|
||||
|
|
@ -111,6 +114,7 @@ const props = defineProps({
|
|||
const emit = defineEmits<{
|
||||
(e: 'node-edit', payload: { start: number; end: number; original: string; newText: string }): void
|
||||
(e: 'update:layout', value: GraphLayout): void
|
||||
(e: 'heading-edit'): void
|
||||
}>()
|
||||
|
||||
const layout = computed({ get: () => props.layout as GraphLayout, set: value => emit('update:layout', value) })
|
||||
|
|
@ -119,7 +123,7 @@ const downloadVisible = ref(false)
|
|||
const exportScope = ref<'filtered' | 'viewport' | 'full'>('filtered')
|
||||
let exportingFull = false, skipRemember = false
|
||||
const fullTree = computed(() => {
|
||||
const root = graphFromMarkdown(props.content, props.title)
|
||||
const root = graphFromMarkdown(props.content, props.title, props.excludedLines)
|
||||
const original = [...props.content.matchAll(/\/admin\/api\/workspace\/[^/\s"<>]+\/ai_hub\/assets\/[a-f0-9-]{36}\/content/g)]
|
||||
const resolved = [...graphContent.value.matchAll(/blob:[^\s)"<>]+/g)]
|
||||
if (showImages.value && original.length === resolved.length) walkGraph(root).forEach(node => { original.forEach((match, i) => { node.content = node.content.split(match[0]).join(resolved[i][0]) }) })
|
||||
|
|
@ -200,13 +204,13 @@ function readNodeWidth() {
|
|||
}
|
||||
}
|
||||
const nodeWidth = ref(readNodeWidth())
|
||||
const viewportSize = ref('')
|
||||
watch(nodeWidth, width => {
|
||||
try { localStorage.setItem(NODE_WIDTH_KEY, String(width)) } catch { /* Storage may be disabled. */ }
|
||||
})
|
||||
const scale = ref(1)
|
||||
const isRendering = ref(false)
|
||||
const isExporting = ref(false)
|
||||
const exportStage = ref('')
|
||||
const totalNodeCount = ref(0)
|
||||
const hiddenNodeCount = ref(0)
|
||||
|
||||
|
|
@ -471,28 +475,18 @@ async function customMap(root: GraphNode, maxWidth: number): Promise<any> {
|
|||
}
|
||||
|
||||
// 监听内容变化自动重绘
|
||||
watch([graphContent, showImages, nodeWidth, layout, () => matchesOnly.value ? search.value : '', matchesOnly, depthLimit, branch, () => props.documentKey], () => {
|
||||
watch([graphContent, showImages, nodeWidth, layout, () => matchesOnly.value ? search.value : '', matchesOnly, depthLimit, branch, () => props.documentKey, () => props.excludedLines], () => {
|
||||
window.clearTimeout(renderTimer)
|
||||
renderTimer = window.setTimeout(() => {
|
||||
renderTimer = undefined
|
||||
renderQueue = renderQueue.catch(() => undefined).then(initMarkmap)
|
||||
}, 160)
|
||||
})
|
||||
watch(viewportSize, () => {
|
||||
if (nodeWidth.value !== -1) return
|
||||
window.clearTimeout(renderTimer)
|
||||
renderTimer = window.setTimeout(() => {
|
||||
renderTimer = undefined
|
||||
renderQueue = renderQueue.catch(() => undefined).then(initMarkmap)
|
||||
}, 160)
|
||||
})
|
||||
|
||||
onMounted(() => {
|
||||
renderQueue = renderQueue.then(initMarkmap)
|
||||
viewportObserver = new ResizeObserver(() => {
|
||||
const bounds = svgRef.value?.getBoundingClientRect()
|
||||
if (!bounds || bounds.width <= 0 || bounds.height <= 0) return
|
||||
viewportSize.value = `${Math.round(bounds.width)}x${Math.round(bounds.height)}`
|
||||
if (needsVisibleRender) {
|
||||
needsVisibleRender = false
|
||||
renderQueue = renderQueue.catch(() => undefined).then(initMarkmap)
|
||||
|
|
@ -504,14 +498,11 @@ onMounted(() => {
|
|||
}
|
||||
})
|
||||
if (svgRef.value) viewportObserver.observe(svgRef.value)
|
||||
// 窗口缩放时自动调整导图大小
|
||||
window.addEventListener('resize', handleResize)
|
||||
})
|
||||
|
||||
onUnmounted(() => {
|
||||
viewportObserver?.disconnect()
|
||||
window.clearTimeout(renderTimer)
|
||||
window.removeEventListener('resize', handleResize)
|
||||
if (mmInstance && typeof mmInstance.destroy === 'function') {
|
||||
mmInstance.destroy()
|
||||
} else if (mmInstance) {
|
||||
|
|
@ -519,10 +510,6 @@ onUnmounted(() => {
|
|||
}
|
||||
})
|
||||
|
||||
const handleResize = () => {
|
||||
mmInstance?.fit()
|
||||
}
|
||||
|
||||
function downloadBlob(blob: Blob, fileName: string) {
|
||||
const url = URL.createObjectURL(blob)
|
||||
const a = document.createElement('a')
|
||||
|
|
@ -609,6 +596,9 @@ function createFullExportSvg() {
|
|||
const downloadMindMap = async (format: ExportFormat = 'svg') => {
|
||||
if (isExporting.value) return
|
||||
isExporting.value = true
|
||||
exportStage.value = '正在准备导图'
|
||||
const started = performance.now()
|
||||
let layoutMs = 0, imagesMs = 0, encodeMs = 0
|
||||
const fileName = graphFilename(format)
|
||||
try {
|
||||
// Export the whole visible tree at its native layout size, independently of pan/zoom.
|
||||
|
|
@ -636,7 +626,12 @@ const downloadMindMap = async (format: ExportFormat = 'svg') => {
|
|||
if (exportScope.value === 'full') { rememberView(); skipRemember = true; exportingFull = true; await initMarkmap() }
|
||||
const exportSvg = createFullExportSvg()
|
||||
if (!exportSvg) return
|
||||
exportSvg.data = await embedImages(exportSvg.data, true)
|
||||
layoutMs = Math.round(performance.now() - started)
|
||||
const imagesStarted = performance.now()
|
||||
exportSvg.data = await embedImages(exportSvg.data, true, (done, total) => {
|
||||
exportStage.value = `正在整理图片 ${done}/${total}`
|
||||
})
|
||||
imagesMs = Math.round(performance.now() - imagesStarted)
|
||||
|
||||
if (format === 'svg') {
|
||||
downloadBlob(new Blob([exportSvg.data], { type: 'image/svg+xml;charset=utf-8' }), fileName)
|
||||
|
|
@ -659,6 +654,8 @@ const downloadMindMap = async (format: ExportFormat = 'svg') => {
|
|||
ctx.scale(scaleFactor, scaleFactor)
|
||||
|
||||
const img = new Image()
|
||||
exportStage.value = '正在生成图片'
|
||||
const encodeStarted = performance.now()
|
||||
img.src = 'data:image/svg+xml;charset=utf-8,' + encodeURIComponent(exportSvg.data)
|
||||
await img.decode()
|
||||
ctx.drawImage(img, 0, 0, exportSvg.width, exportSvg.height)
|
||||
|
|
@ -666,13 +663,16 @@ const downloadMindMap = async (format: ExportFormat = 'svg') => {
|
|||
const blob = await new Promise<Blob>((resolve, reject) => {
|
||||
canvas.toBlob(result => result ? resolve(result) : reject(new Error('导出尺寸过大,请选择 SVG')), mimeType, 0.96)
|
||||
})
|
||||
encodeMs = Math.round(performance.now() - encodeStarted)
|
||||
downloadBlob(blob, fileName)
|
||||
if (scaleFactor < requestedScale * 0.999) {
|
||||
ElMessage.warning(`图片过大,已自动缩小为 ${canvas.width} × ${canvas.height} 像素;需无损放大请下载 SVG`)
|
||||
}
|
||||
} finally {
|
||||
if (exportingFull) { exportingFull = false; try { await initMarkmap() } finally { skipRemember = false } }
|
||||
isExporting.value = false
|
||||
console.info('[mindmap-export]', { format, layoutMs, imagesMs, encodeMs, totalMs: Math.round(performance.now() - started) })
|
||||
try {
|
||||
if (exportingFull) { exportingFull = false; try { await initMarkmap() } finally { skipRemember = false } }
|
||||
} finally { isExporting.value = false; exportStage.value = '' }
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,9 @@
|
|||
import { ref, reactive, watch } from 'vue'
|
||||
import { ref, reactive } from 'vue'
|
||||
import type { Ref } from 'vue'
|
||||
import { documentApi, type ParseParams } from '@/views/mindmap/workbench/api/document'
|
||||
import { documentApi, type ParseParams, type SummaryOptions } from '@/views/mindmap/workbench/api/document'
|
||||
import { buildMindmapMarkdown, documentTitleFromName } from '@/views/mindmap/workbench/utils/mindmapMarkdown'
|
||||
import { hubCall } from '@/views/mindmap/workbench/api/hub'
|
||||
import useStore from '@/stores'
|
||||
|
||||
// 根据上一级标题自动补全下一级标题
|
||||
export function autoPromoteParagraphsToSubheading(text: string): string {
|
||||
|
|
@ -16,7 +18,9 @@ export interface DocumentConfig {
|
|||
mindmapPrompt: string
|
||||
parserResourceId: string
|
||||
mindmapModelId: string
|
||||
headingModelRepair: boolean
|
||||
mindmapLlmTemperature: number | null
|
||||
summaryOptions: SummaryOptions
|
||||
tableEnable: boolean
|
||||
formulaEnable: boolean
|
||||
language: string
|
||||
|
|
@ -24,6 +28,10 @@ export interface DocumentConfig {
|
|||
}
|
||||
|
||||
export interface ProcessResult {
|
||||
original?: string
|
||||
headingStructure?: any
|
||||
headingMarkdown?: string
|
||||
taskId?: string
|
||||
markdown: string
|
||||
source: string
|
||||
mindmap: string
|
||||
|
|
@ -32,7 +40,7 @@ export interface ProcessResult {
|
|||
|
||||
export const DOCUMENT_CONFIG_STORAGE_KEY = 'mineru.documentProcessor.config.v5'
|
||||
|
||||
export const DEFAULT_MINDMAP_PROMPT = `你是文档结构整理助手。请基于用户提供的 Markdown 生成适合思维导图展示的 Markdown。
|
||||
export const DEFAULT_MINDMAP_PROMPT = `你是文档总结助手。请将提供的正文提炼为适合思维导图展示的 Markdown。
|
||||
|
||||
要求:
|
||||
1. 保留原文标题结构,不要重写或打乱主要标题层级。
|
||||
|
|
@ -40,11 +48,12 @@ export const DEFAULT_MINDMAP_PROMPT = `你是文档结构整理助手。请基
|
|||
3. 保留原文语言:英文内容输出英文,中文内容输出中文,多语言内容按原文语言分别保留。
|
||||
4. 不要编造原文没有的信息。
|
||||
5. 保留关键数字、公式、专有名词、步骤和结论。
|
||||
6. 最大层级不超过 4 层。
|
||||
7. 每个父节点下最多 8 个子节点。
|
||||
6. 按本次提供的粒度、层级和节点参数整理;保留完整步骤、条件、例外和否定关系。
|
||||
7. 若当前正文是分块,结合批次范围和章节路径,仅总结本批内容,不推测其他批次内容。
|
||||
8. 节点标题尽量简短,正文说明使用短句列表。
|
||||
9. 只输出 Markdown,不要输出解释、代码块围栏或额外说明。`
|
||||
|
||||
|
||||
export const DEFAULT_DOCUMENT_CONFIG: DocumentConfig = {
|
||||
maxPages: 1000,
|
||||
backend: 'hybrid-auto-engine',
|
||||
|
|
@ -53,29 +62,34 @@ export const DEFAULT_DOCUMENT_CONFIG: DocumentConfig = {
|
|||
mindmapPrompt: DEFAULT_MINDMAP_PROMPT,
|
||||
parserResourceId: '',
|
||||
mindmapModelId: '',
|
||||
headingModelRepair: false,
|
||||
mindmapLlmTemperature: null,
|
||||
summaryOptions: {
|
||||
context_tokens: null, max_input_tokens: null, max_output_tokens: null, input_tokens: null,
|
||||
output_tokens: null, safety_tokens: null, top_p: null,
|
||||
timeout: 300, retries: 2, concurrency: 1, max_depth: 6, max_children: 8,
|
||||
node_max_chars: 60, detail: 'standard', structure_mode: 'preserve', parallel: true, auto_split: true,
|
||||
},
|
||||
tableEnable: true,
|
||||
formulaEnable: true,
|
||||
language: 'ch',
|
||||
forceOcr: false
|
||||
}
|
||||
|
||||
function loadCachedConfig(): Partial<DocumentConfig> {
|
||||
try {
|
||||
const raw = window.localStorage.getItem(DOCUMENT_CONFIG_STORAGE_KEY)
|
||||
const cached = raw ? JSON.parse(raw) : {}
|
||||
const safe = Object.fromEntries(Object.keys(DEFAULT_DOCUMENT_CONFIG).filter(key => key in cached).map(key => [key, cached[key]]))
|
||||
if ('exportResolution' in safe) {
|
||||
const legacySizes: Record<number, number> = { 1080: 1024, 2560: 2048, 3840: 4096, 7680: 8192, 16384: 8192 }
|
||||
const size = Number(safe.exportResolution)
|
||||
safe.exportResolution = legacySizes[size] ?? ([1024, 2048, 4096, 8192].includes(size) ? size : 4096)
|
||||
}
|
||||
window.localStorage.setItem(DOCUMENT_CONFIG_STORAGE_KEY, JSON.stringify(safe))
|
||||
return safe
|
||||
} catch {
|
||||
try { window.localStorage.removeItem(DOCUMENT_CONFIG_STORAGE_KEY) } catch { /* Storage unavailable. */ }
|
||||
return {}
|
||||
}
|
||||
type WorkbenchFields = Pick<DocumentConfig, 'parserResourceId' | 'mindmapModelId' | 'mindmapLlmTemperature' | 'mindmapPrompt' | 'summaryOptions' | 'exportResolution'>
|
||||
type WorkbenchOverride = Partial<Omit<WorkbenchFields, 'summaryOptions'>> & { summaryOptions?: Partial<SummaryOptions> }
|
||||
// Browser-tab memory only: SPA navigation retains changes, any reload clears them.
|
||||
const personalOverrides = new Map<string, WorkbenchOverride>()
|
||||
export function workbenchFields(config: DocumentConfig): WorkbenchFields {
|
||||
const { parserResourceId, mindmapModelId, mindmapLlmTemperature, mindmapPrompt, exportResolution } = config
|
||||
return { parserResourceId, mindmapModelId, mindmapLlmTemperature, mindmapPrompt, exportResolution, summaryOptions: { ...config.summaryOptions } }
|
||||
}
|
||||
export function mergeWorkbenchDefaults(base: DocumentConfig, value: WorkbenchOverride | null | undefined): DocumentConfig {
|
||||
return { ...base, ...value, summaryOptions: { ...base.summaryOptions, ...(value?.summaryOptions || {}) } }
|
||||
}
|
||||
function identity() {
|
||||
const user = useStore().user
|
||||
return user.userInfo?.id ? `${user.userInfo.id}:${user.getWorkspaceId()}` : ''
|
||||
}
|
||||
|
||||
export function useDocumentProcessor() {
|
||||
|
|
@ -85,22 +99,49 @@ export function useDocumentProcessor() {
|
|||
const isUploading = ref(false)
|
||||
|
||||
// 配置相关
|
||||
const config = reactive<DocumentConfig>({
|
||||
...DEFAULT_DOCUMENT_CONFIG,
|
||||
...loadCachedConfig()
|
||||
})
|
||||
|
||||
watch(
|
||||
config,
|
||||
(value) => {
|
||||
try {
|
||||
window.localStorage.setItem(DOCUMENT_CONFIG_STORAGE_KEY, JSON.stringify(value))
|
||||
} catch {
|
||||
// 浏览器禁用本地存储时不影响当前页面使用。
|
||||
}
|
||||
},
|
||||
{ deep: true }
|
||||
)
|
||||
const config = reactive<DocumentConfig>(mergeWorkbenchDefaults(DEFAULT_DOCUMENT_CONFIG, {}))
|
||||
const configReady = ref(false)
|
||||
let platformDefaults = mergeWorkbenchDefaults(DEFAULT_DOCUMENT_CONFIG, {})
|
||||
async function loadWorkbenchDefaults() {
|
||||
// Remove old persistent per-user preferences, which predate the new policy.
|
||||
try { window.localStorage.removeItem(DOCUMENT_CONFIG_STORAGE_KEY) } catch { /* Storage unavailable. */ }
|
||||
const [settings, resources] = await Promise.allSettled([hubCall('workbench_defaults'), hubCall('resources')])
|
||||
platformDefaults = mergeWorkbenchDefaults(DEFAULT_DOCUMENT_CONFIG, settings.status === 'fulfilled' ? settings.value?.config : {})
|
||||
const available = resources.status === 'fulfilled' ? resources.value : null
|
||||
if (available) {
|
||||
const models = (available.models || []).filter((m: any) => m.model_type === 'LLM')
|
||||
const parsers = available.parsers || []
|
||||
if (!models.some((m: any) => m.id === platformDefaults.mindmapModelId)) platformDefaults.mindmapModelId = settings.status === 'fulfilled' && settings.value?.model_unavailable ? '' : (models[0]?.id || '')
|
||||
if (platformDefaults.parserResourceId !== 'builtin' && !parsers.some((p: any) => p.id === platformDefaults.parserResourceId)) platformDefaults.parserResourceId = settings.status === 'fulfilled' && settings.value?.parser_unavailable ? '' : (parsers[0]?.id || '')
|
||||
}
|
||||
const key = identity()
|
||||
const effective = mergeWorkbenchDefaults(platformDefaults, key ? personalOverrides.get(key) : {})
|
||||
if (available) {
|
||||
if (!(available.models || []).some((m: any) => m.model_type === 'LLM' && m.id === effective.mindmapModelId)) effective.mindmapModelId = platformDefaults.mindmapModelId
|
||||
if (effective.parserResourceId !== 'builtin' && !(available.parsers || []).some((p: any) => p.id === effective.parserResourceId)) effective.parserResourceId = platformDefaults.parserResourceId
|
||||
}
|
||||
Object.assign(config, effective)
|
||||
configReady.value = true
|
||||
return settings.status === 'fulfilled' && resources.status === 'fulfilled'
|
||||
}
|
||||
function setPersonalConfig(next: DocumentConfig) {
|
||||
const key = identity()
|
||||
const value = workbenchFields(next), baseline = workbenchFields(platformDefaults)
|
||||
const override: WorkbenchOverride = {}
|
||||
for (const field of ['parserResourceId', 'mindmapModelId', 'mindmapLlmTemperature', 'mindmapPrompt', 'exportResolution'] as const) {
|
||||
if (value[field] !== baseline[field]) (override as any)[field] = value[field]
|
||||
}
|
||||
const options: Partial<SummaryOptions> = {}
|
||||
for (const field of Object.keys(value.summaryOptions) as (keyof SummaryOptions)[]) {
|
||||
if (value.summaryOptions[field] !== baseline.summaryOptions[field]) (options as any)[field] = value.summaryOptions[field]
|
||||
}
|
||||
if (Object.keys(options).length) override.summaryOptions = options
|
||||
if (key) {
|
||||
if (Object.keys(override).length) personalOverrides.set(key, override)
|
||||
else personalOverrides.delete(key)
|
||||
}
|
||||
Object.assign(config, mergeWorkbenchDefaults(platformDefaults, override))
|
||||
}
|
||||
|
||||
// 结果相关
|
||||
const results = ref<ProcessResult | null>(null)
|
||||
|
|
@ -178,6 +219,7 @@ export function useDocumentProcessor() {
|
|||
validFiles.push(file)
|
||||
displayFiles.push(file)
|
||||
results.value = {
|
||||
original: markdown,
|
||||
markdown,
|
||||
source: markdown,
|
||||
mindmap: buildMindmapMarkdown(markdown, documentTitleFromName(file.name))
|
||||
|
|
@ -286,6 +328,7 @@ export function useDocumentProcessor() {
|
|||
|
||||
// 处理文档转换
|
||||
const processDocument = async () => {
|
||||
error.value = null
|
||||
if (uploadedFiles.value.length === 0) {
|
||||
error.value = '请先上传文件'
|
||||
return
|
||||
|
|
@ -344,9 +387,13 @@ export function useDocumentProcessor() {
|
|||
const mdContent = resultData.md_content || ''
|
||||
const mindmapContent = buildMindmapMarkdown(mdContent, documentTitleFromName(uploadedDisplayFiles.value[0]?.name || ''))
|
||||
results.value = {
|
||||
original: resultData.original_md || mdContent,
|
||||
headingStructure: resultData.heading_structure,
|
||||
headingMarkdown: mdContent,
|
||||
markdown: mdContent,
|
||||
source: mdContent,
|
||||
mindmap: mindmapContent
|
||||
mindmap: mindmapContent,
|
||||
taskId: response.task_id
|
||||
}
|
||||
progressPercent.value = 100
|
||||
}
|
||||
|
|
@ -389,6 +436,9 @@ export function useDocumentProcessor() {
|
|||
uploadedFiles,
|
||||
uploadedDisplayFiles,
|
||||
config,
|
||||
configReady,
|
||||
loadWorkbenchDefaults,
|
||||
setPersonalConfig,
|
||||
results,
|
||||
isUploading,
|
||||
isProcessing,
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ markdownEngine.renderer.rules.workbench_math = (tokens: any, index: number) => {
|
|||
return `<span class="export-math" data-formula="${markdownEngine.utils.escapeHtml(token.content)}">${katex.renderToString(token.content, { throwOnError: false, displayMode: token.block, trust: false })}</span>`
|
||||
}
|
||||
|
||||
export function graphFromMarkdown(source: string, title: string): GraphNode {
|
||||
export function graphFromMarkdown(source: string, title: string, excludedLines: number[] = []): GraphNode {
|
||||
const root: GraphNode = { id: 'root', text: title, content: markdownEngine.utils.escapeHtml(title), children: [], payload: {} }
|
||||
const lines = source.split('\n'), offsets: number[] = [0]
|
||||
lines.forEach(line => offsets.push(offsets[offsets.length - 1] + line.length + 1))
|
||||
|
|
@ -38,6 +38,7 @@ export function graphFromMarkdown(source: string, title: string): GraphNode {
|
|||
const headings: { level: number; node: GraphNode }[] = [{ level: 0, node: root }]
|
||||
const items: (GraphNode | undefined)[] = []
|
||||
let depth = 0
|
||||
const excluded = new Set(excludedLines)
|
||||
for (let i = 0; i < tokens.length; i++) {
|
||||
const token = tokens[i]
|
||||
if (['bullet_list_open', 'ordered_list_open'].includes(token.type)) { depth++; continue }
|
||||
|
|
@ -47,6 +48,7 @@ export function graphFromMarkdown(source: string, title: string): GraphNode {
|
|||
if (!heading && !['paragraph_open', 'fence', 'code_block', 'table_open', 'html_block'].includes(token.type)) continue
|
||||
const map = token.map
|
||||
if (!map) continue
|
||||
if (excluded.has(map[0]) && lines.slice(map[0], map[1]).every((line, n) => !line.trim() || excluded.has(map[0] + n))) continue
|
||||
const start = offsets[map[0]], end = Math.min(source.length, offsets[map[1]] - 1)
|
||||
const raw = source.slice(start, end)
|
||||
const inline = tokens[i + 1]?.type === 'inline' ? tokens[i + 1].content : token.content
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
import { onBeforeUnmount, ref, watch, type Ref } from 'vue'
|
||||
import { hubCall } from '@/views/mindmap/workbench/api/hub'
|
||||
import { createExportImageCache, runLimited } from './exportPreparation'
|
||||
|
||||
const pattern = () => /\/admin\/api\/workspace\/[^/\s"<>]+\/ai_hub\/assets\/([a-f0-9-]{36})\/content/g
|
||||
const cache = new Map<string, Blob>()
|
||||
|
|
@ -79,16 +80,31 @@ export function blobDataUrl(blob: Blob): Promise<string> {
|
|||
}
|
||||
|
||||
// Standalone HTML and SVG must not contain browser-session Blob URLs.
|
||||
export async function embedImages(html: string, svg = false) {
|
||||
const embeddedImages = createExportImageCache()
|
||||
export async function embedImages(html: string, svg = false, progress?: (done: number, total: number) => void) {
|
||||
const document = new DOMParser().parseFromString(html, svg ? 'image/svg+xml' : 'text/html')
|
||||
if (document.querySelector('parsererror')) throw new Error('导出内容格式错误')
|
||||
for (const image of Array.from(document.querySelectorAll('img'))) {
|
||||
const src = image.getAttribute('src') || ''
|
||||
if (src && !src.startsWith('data:')) {
|
||||
const response = await fetch(src)
|
||||
if (!response.ok) throw new Error('导出图片加载失败')
|
||||
image.setAttribute('src', await blobDataUrl(await response.blob()))
|
||||
const images = Array.from(document.querySelectorAll('img'))
|
||||
const sources = [...new Set(images.map(image => image.getAttribute('src') || '').filter(src => src && !src.startsWith('data:')))]
|
||||
const replacements = new Map<string, string>()
|
||||
let completed = 0
|
||||
progress?.(0, sources.length)
|
||||
await runLimited(sources, 4, async src => {
|
||||
const load = async () => {
|
||||
const controller = new AbortController(), timeout = setTimeout(() => controller.abort(), 30000)
|
||||
try {
|
||||
const response = await fetch(src, { signal: controller.signal })
|
||||
if (!response.ok) throw new Error('导出图片加载失败')
|
||||
return await blobDataUrl(await response.blob())
|
||||
} finally { clearTimeout(timeout) }
|
||||
}
|
||||
// Blob URLs identify immutable local images; remote URLs may change contents.
|
||||
replacements.set(src, await (src.startsWith('blob:') ? embeddedImages.get(src, load) : load()))
|
||||
progress?.(++completed, sources.length)
|
||||
})
|
||||
for (const image of images) {
|
||||
const replacement = replacements.get(image.getAttribute('src') || '')
|
||||
if (replacement) image.setAttribute('src', replacement)
|
||||
}
|
||||
return svg ? new XMLSerializer().serializeToString(document) : document.body.innerHTML
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,10 +1,19 @@
|
|||
<template>
|
||||
<div class="document-processor">
|
||||
<MyMindmapsDialog v-model="myMindmapsVisible" :before-open="confirmOpenSavedArtifact" @open="openSavedArtifact" @renamed="renameSavedArtifact" @deleted="deleteSavedArtifact" />
|
||||
<el-dialog v-model="summarySourcesVisible" title="总结与原文" width="80%" destroy-on-close>
|
||||
<el-collapse>
|
||||
<el-collapse-item v-for="item in smartSources" :key="item.id" :title="`${item.path.join(' / ') || '正文'} · 原文 ${item.start_line}–${item.end_line} 行${item.fallback ? ' · 保留原文' : item.review ? ' · 条件待核对' : ''}`" :name="item.id">
|
||||
<div class="summary-source-columns"><div><strong>总结</strong><pre>{{ item.summary }}</pre></div><div><strong>原文</strong><pre>{{ item.source }}</pre></div></div>
|
||||
</el-collapse-item>
|
||||
</el-collapse>
|
||||
</el-dialog>
|
||||
<header class="top-header">
|
||||
<h2 class="ellipsis" title="思维导图">思维导图</h2>
|
||||
<h2 class="ellipsis" title="思维导图工作台">思维导图工作台</h2>
|
||||
<div class="header-actions">
|
||||
<el-button :disabled="!results" @click="saveArtifact">保存图谱</el-button>
|
||||
<el-button type="text" @click="toggleSettings" class="settings-button">
|
||||
<el-button :disabled="isProcessing || isSmartOrganizing || savingArtifact" @click="myMindmapsVisible = true">我的导图</el-button>
|
||||
<el-button :loading="savingArtifact" :disabled="!results || isProcessing || isSmartOrganizing" @click="saveArtifact">保存导图</el-button>
|
||||
<el-button type="text" :disabled="!configReady" @click="toggleSettings" class="settings-button">
|
||||
设置
|
||||
</el-button>
|
||||
</div>
|
||||
|
|
@ -40,7 +49,7 @@
|
|||
/>
|
||||
<el-button
|
||||
type="primary"
|
||||
:loading="isProcessing || isUploading"
|
||||
:loading="isProcessing || isUploading || isSmartOrganizing" :disabled="!configReady"
|
||||
@click.stop="handleProcessDocument"
|
||||
>
|
||||
开始转换
|
||||
|
|
@ -149,22 +158,26 @@
|
|||
v-model="sourceContentMode"
|
||||
:options="sourceContentModeOptions"
|
||||
size="small"
|
||||
:disabled="sourceViewMode === 'ast' || isSmartOrganizing"
|
||||
:disabled="sourceViewMode === 'ast' || isSmartOrganizing || !configReady"
|
||||
/>
|
||||
<span v-if="isSmartOrganizing" class="smart-organize-status">
|
||||
{{ smartOrganizeStage || '智能整理中' }} {{ smartOrganizeProgress === null ? '' : `${smartOrganizeProgress}%` }}
|
||||
{{ smartOrganizeStage || '智能总结中' }} {{ smartOrganizeProgress === null ? '' : `${smartOrganizeProgress}%` }}
|
||||
</span>
|
||||
<span v-else-if="smartOrganizeError" class="template-error-inline">
|
||||
{{ smartOrganizeError }}
|
||||
</span>
|
||||
<span v-else-if="sourceContentMode === 'smart' && smartMarkdownContent" class="smart-organize-status">
|
||||
智能整理结果
|
||||
{{ smartResultStale ? '原文或配置已变更,请重新总结' : smartPlan?.fallback_sections || smartPlan?.review_sections ? '部分内容待核对' : '智能总结结果' }}
|
||||
</span>
|
||||
<span v-else-if="templateRenderError" class="template-error-inline">
|
||||
{{ templateRenderError }}
|
||||
</span>
|
||||
</div>
|
||||
<div class="content-toolbar-actions">
|
||||
<el-button v-if="canShowSmartSources" @click="summarySourcesVisible = true">原文对照</el-button>
|
||||
<el-button v-if="smartMarkdownContent || smartOrganizeError" :disabled="isSmartOrganizing" @click="startSmartOrganize(true)">重新总结</el-button>
|
||||
<el-button v-if="smartTaskFailed && !smartResultStale" :disabled="isSmartOrganizing" @click="retrySmartOrganize">继续处理</el-button>
|
||||
<el-button :disabled="headingBusyDisabled" @click="headingPanelVisible = true">标题层级</el-button>
|
||||
<el-button
|
||||
:type="hasActiveReplacementRules ? 'primary' : 'default'"
|
||||
class="replacement-config-button"
|
||||
|
|
@ -174,7 +187,6 @@
|
|||
</el-button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="source-panel">
|
||||
<el-input
|
||||
v-model="sourcePanelContent"
|
||||
|
|
@ -195,9 +207,11 @@
|
|||
:source-name="exportSourceName"
|
||||
v-model:layout="graphLayout"
|
||||
:editable="!hasActiveReplacementRules"
|
||||
:excluded-lines="activeHeadingStructure?.excluded_lines || []"
|
||||
:document-key="mindmapDocumentKey"
|
||||
:export-resolution="config.exportResolution"
|
||||
@node-edit="handleMindmapNodeEdit"
|
||||
@heading-edit="headingPanelVisible = true"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
|
@ -249,19 +263,28 @@
|
|||
</div>
|
||||
</template>
|
||||
</el-dialog>
|
||||
<HeadingStructurePanel v-model="headingPanelVisible"
|
||||
:markdown="activeSourceMarkdownContent" :original="originalRecognized || results?.original || manualMarkdownContent"
|
||||
:structure="activeHeadingStructure" :task-id="results?.taskId" :artifact-id="openedArtifact?.id"
|
||||
:document-key="mindmapDocumentKey"
|
||||
@inspect="inspectHeadingStructure" @apply="applyHeadingStructure" />
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed, onMounted, onUnmounted, ref, watch } from 'vue'
|
||||
import { useRouter } from 'vue-router'
|
||||
import useStore from '@/stores'
|
||||
import { Delete, Document, Folder, Close, Loading, ArrowDown } from '@element-plus/icons-vue'
|
||||
import { ElMessage } from 'element-plus'
|
||||
import { ElMessage, ElMessageBox } from 'element-plus'
|
||||
import ConfigPanel from '@/views/mindmap/workbench/components/ConfigPanel.vue'
|
||||
import MyMindmapsDialog from '@/views/mindmap/workbench/components/MyMindmapsDialog.vue'
|
||||
import MarkdownRenderer from '@/views/mindmap/workbench/components/MarkdownRenderer.vue'
|
||||
import MindMapRenderer from '@/views/mindmap/workbench/components/MindMapRenderer.vue'
|
||||
import HeadingStructurePanel from '@/views/mindmap/workbench/components/HeadingStructurePanel.vue'
|
||||
import { hubCall, onOpenArtifact } from '@/views/mindmap/workbench/api/hub'
|
||||
import { documentApi } from '@/views/mindmap/workbench/api/document'
|
||||
import { DOCUMENT_CONFIG_STORAGE_KEY, useDocumentProcessor } from '@/views/mindmap/workbench/composables/useDocumentProcessor'
|
||||
import { DEFAULT_DOCUMENT_CONFIG, useDocumentProcessor } from '@/views/mindmap/workbench/composables/useDocumentProcessor'
|
||||
import { markdownToAstString } from '@/views/mindmap/workbench/utils/markdownAst'
|
||||
import { buildMindmapMarkdown, documentTitleFromName } from '@/views/mindmap/workbench/utils/mindmapMarkdown'
|
||||
import { createEmptyReplacementRule, renderMarkdownTemplate, type ReplacementRule } from '@/views/mindmap/workbench/utils/markdownTemplate'
|
||||
|
|
@ -300,6 +323,9 @@ const {
|
|||
uploadedFiles,
|
||||
uploadedDisplayFiles,
|
||||
config,
|
||||
configReady,
|
||||
loadWorkbenchDefaults,
|
||||
setPersonalConfig,
|
||||
results,
|
||||
isUploading,
|
||||
isProcessing,
|
||||
|
|
@ -316,6 +342,7 @@ const {
|
|||
} = useDocumentProcessor()
|
||||
|
||||
const showSettings = ref(false)
|
||||
const myMindmapsVisible = ref(false), savingArtifact = ref(false)
|
||||
const fileInput = ref<HTMLInputElement | null>(null)
|
||||
const isDragging = ref(false)
|
||||
const activeTab = ref('markdown')
|
||||
|
|
@ -330,30 +357,76 @@ const sourceViewMode = ref<'markdown' | 'ast'>(
|
|||
const sourceContentMode = ref<'source' | 'smart'>('source')
|
||||
const sourceContentModeOptions = [
|
||||
{ label: '源码', value: 'source' },
|
||||
{ label: '智能整理', value: 'smart' }
|
||||
{ label: '智能总结', value: 'smart' }
|
||||
]
|
||||
const showReplacementDialog = ref(false)
|
||||
const isMobileDialog = ref(false)
|
||||
const replacementRules = ref<ReplacementRule[]>([])
|
||||
const draftReplacementRules = ref<ReplacementRule[]>([])
|
||||
const smartMarkdownContent = ref('')
|
||||
const smartHeadingStructure = ref<any>()
|
||||
const smartHeadingMarkdown = ref('')
|
||||
const headingPanelVisible = ref(false)
|
||||
const smartOrganizeTaskId = ref('')
|
||||
const isSmartOrganizing = ref(false)
|
||||
const smartOrganizeProgress = ref<number | null>(null)
|
||||
const smartOrganizeStage = ref('')
|
||||
const smartOrganizeError = ref('')
|
||||
const smartPlan = ref<Record<string, any> | null>(null)
|
||||
const smartSources = ref<any[]>([])
|
||||
const smartSourceSnapshot = ref('')
|
||||
const summarySourcesVisible = ref(false)
|
||||
const smartRequestSnapshot = ref('')
|
||||
const smartTaskFailed = ref(false)
|
||||
let smartPollingGeneration = 0
|
||||
let applyingSmartResult = false
|
||||
let smartOrganizeTimer: ReturnType<typeof setInterval> | null = null
|
||||
const summaryRecoveryKey = () => `mindmap.summary.task.v1:${useStore().user.userInfo?.id || ''}:${useStore().user.getWorkspaceId()}`
|
||||
function rememberSummaryTask(taskId: string) {
|
||||
try { sessionStorage.setItem(summaryRecoveryKey(), taskId) } catch { /* Optional recovery storage. */ }
|
||||
}
|
||||
function forgetSummaryTask() {
|
||||
try { sessionStorage.removeItem(summaryRecoveryKey()) } catch { /* Optional recovery storage. */ }
|
||||
}
|
||||
function showSmartResult() {
|
||||
if (sourceContentMode.value !== 'smart') {
|
||||
applyingSmartResult = true
|
||||
sourceContentMode.value = 'smart'
|
||||
}
|
||||
}
|
||||
|
||||
watch(markdownRenderMode, (value) => storeValue(MARKDOWN_RENDER_MODE_KEY, value))
|
||||
watch(sourceViewMode, (value) => storeValue(SOURCE_VIEW_MODE_KEY, value))
|
||||
|
||||
const manualMarkdownContent = computed(() => results.value?.source || '')
|
||||
const currentSmartRequest = computed(() => JSON.stringify([manualMarkdownContent.value,
|
||||
config.mindmapModelId, config.mindmapPrompt, config.mindmapLlmTemperature,
|
||||
config.summaryOptions, results.value?.headingStructure]))
|
||||
const smartResultStale = computed(() => !!smartRequestSnapshot.value && smartRequestSnapshot.value !== currentSmartRequest.value)
|
||||
const activeHeadingStructure = computed(() => {
|
||||
const state = sourceContentMode.value === 'smart' ? smartHeadingStructure.value : results.value?.headingStructure
|
||||
const snapshot = sourceContentMode.value === 'smart' ? smartHeadingMarkdown.value : results.value?.headingMarkdown
|
||||
return state && snapshot !== activeSourceMarkdownContent.value ? { ...state, excluded_lines: [] } : state
|
||||
})
|
||||
const headingBusyDisabled = computed(() => !activeSourceMarkdownContent.value.trim() || isProcessing.value || isSmartOrganizing.value || hasActiveReplacementRules.value)
|
||||
function inspectHeadingStructure(value: any) {
|
||||
if (sourceContentMode.value === 'smart') { smartHeadingStructure.value = value.heading_structure; smartHeadingMarkdown.value = activeSourceMarkdownContent.value }
|
||||
else if (results.value) { results.value.headingStructure = value.heading_structure; results.value.headingMarkdown = activeSourceMarkdownContent.value }
|
||||
}
|
||||
function applyHeadingStructure(value: any) {
|
||||
if (!results.value) return
|
||||
if (sourceContentMode.value === 'smart') smartMarkdownContent.value = value.markdown
|
||||
else { results.value.source = value.markdown; results.value.markdown = value.markdown }
|
||||
if (value.task_id) results.value.taskId = value.task_id
|
||||
inspectHeadingStructure(value)
|
||||
}
|
||||
const activeSourceMarkdownContent = computed(() => {
|
||||
if (sourceContentMode.value === 'smart' && smartMarkdownContent.value) {
|
||||
return smartMarkdownContent.value
|
||||
}
|
||||
return manualMarkdownContent.value
|
||||
})
|
||||
const canShowSmartSources = computed(() => smartSources.value.length > 0 && !!smartSourceSnapshot.value && smartSourceSnapshot.value === activeSourceMarkdownContent.value)
|
||||
const templateRenderResult = computed(() => renderMarkdownTemplate(activeSourceMarkdownContent.value, replacementRules.value))
|
||||
const renderedMarkdownContent = computed(() => templateRenderResult.value.markdown)
|
||||
const openedArtifact = ref<any>(null)
|
||||
|
|
@ -403,6 +476,7 @@ const sourcePanelContent = computed({
|
|||
})
|
||||
|
||||
const stopSmartOrganizePolling = () => {
|
||||
smartPollingGeneration += 1
|
||||
if (smartOrganizeTimer) {
|
||||
clearInterval(smartOrganizeTimer)
|
||||
smartOrganizeTimer = null
|
||||
|
|
@ -413,78 +487,170 @@ const resetSmartOrganizeState = () => {
|
|||
stopSmartOrganizePolling()
|
||||
sourceContentMode.value = 'source'
|
||||
smartMarkdownContent.value = ''
|
||||
smartHeadingStructure.value = undefined
|
||||
smartHeadingMarkdown.value = ''
|
||||
headingPanelVisible.value = false
|
||||
smartOrganizeTaskId.value = ''
|
||||
isSmartOrganizing.value = false
|
||||
smartOrganizeProgress.value = 0
|
||||
smartOrganizeStage.value = ''
|
||||
smartOrganizeError.value = ''
|
||||
smartPlan.value = null
|
||||
smartSources.value = []
|
||||
smartSourceSnapshot.value = ''
|
||||
forgetSummaryTask()
|
||||
smartRequestSnapshot.value = ''
|
||||
smartTaskFailed.value = false
|
||||
}
|
||||
|
||||
const startSmartOrganizePolling = (taskId: string) => {
|
||||
stopSmartOrganizePolling()
|
||||
const generation = smartPollingGeneration
|
||||
let polling = false
|
||||
let failures = 0
|
||||
smartOrganizeTimer = setInterval(async () => {
|
||||
if (polling) return
|
||||
polling = true
|
||||
try {
|
||||
const data = await documentApi.getMindmapProgress(taskId)
|
||||
if (generation !== smartPollingGeneration) return
|
||||
failures = 0
|
||||
if (data.plan) smartPlan.value = data.plan
|
||||
smartOrganizeProgress.value = data.progress
|
||||
smartOrganizeStage.value = data.stage
|
||||
if (data.status === 'completed') {
|
||||
stopSmartOrganizePolling()
|
||||
isSmartOrganizing.value = false
|
||||
smartMarkdownContent.value = data.result_md || ''
|
||||
smartHeadingStructure.value = data.heading_structure
|
||||
smartHeadingMarkdown.value = smartMarkdownContent.value
|
||||
smartSources.value = data.sources || []
|
||||
smartSourceSnapshot.value = smartMarkdownContent.value
|
||||
if (results.value && data.task_id) results.value.taskId = data.task_id
|
||||
smartOrganizeError.value = ''
|
||||
sourceContentMode.value = 'smart'
|
||||
ElMessage.success('智能整理完成')
|
||||
showSmartResult()
|
||||
if (data.warnings?.length) ElMessage.warning('总结完成,部分章节待核对')
|
||||
else ElMessage.success('智能总结完成')
|
||||
} else if (data.status === 'failed') {
|
||||
stopSmartOrganizePolling()
|
||||
isSmartOrganizing.value = false
|
||||
sourceContentMode.value = 'source'
|
||||
smartOrganizeError.value = data.error || '智能整理失败'
|
||||
smartOrganizeError.value = data.error || '智能总结失败'
|
||||
smartTaskFailed.value = true
|
||||
ElMessage.error(smartOrganizeError.value)
|
||||
}
|
||||
} catch (err) {
|
||||
stopSmartOrganizePolling()
|
||||
isSmartOrganizing.value = false
|
||||
sourceContentMode.value = 'source'
|
||||
smartOrganizeError.value = (err as Error).message || '智能整理进度查询失败'
|
||||
ElMessage.error(smartOrganizeError.value)
|
||||
if (generation !== smartPollingGeneration) return
|
||||
failures += 1
|
||||
smartOrganizeStage.value = failures >= 3 ? '进度查询暂不可用,正在重试' : '正在查询进度'
|
||||
} finally {
|
||||
polling = false
|
||||
}
|
||||
}, 1500)
|
||||
}
|
||||
|
||||
const startSmartOrganize = async () => {
|
||||
const startSmartOrganize = async (force = false) => {
|
||||
if (isSmartOrganizing.value) return
|
||||
const markdown = manualMarkdownContent.value.trim()
|
||||
if (!markdown) {
|
||||
sourceContentMode.value = 'source'
|
||||
ElMessage.warning('暂无 Markdown 源码可整理')
|
||||
return
|
||||
}
|
||||
if (smartMarkdownContent.value) {
|
||||
if (!force && smartMarkdownContent.value && !smartResultStale.value) {
|
||||
sourceContentMode.value = 'smart'
|
||||
return
|
||||
}
|
||||
|
||||
const taskId = `mindmap-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`
|
||||
smartOrganizeTaskId.value = taskId
|
||||
smartRequestSnapshot.value = currentSmartRequest.value
|
||||
smartPlan.value = null
|
||||
smartTaskFailed.value = false
|
||||
isSmartOrganizing.value = true
|
||||
smartOrganizeProgress.value = 0
|
||||
smartOrganizeStage.value = '创建智能整理任务'
|
||||
smartOrganizeStage.value = '创建智能总结任务'
|
||||
smartOrganizeError.value = ''
|
||||
|
||||
try {
|
||||
await documentApi.createMindmapTask(taskId, markdown, 'hybrid', config.mindmapPrompt, {
|
||||
const options = Object.fromEntries(Object.entries(config.summaryOptions).filter(([, value]) => value != null))
|
||||
const node: Record<string, unknown> = {type: 'organize', model_id: config.mindmapModelId, prompt: config.mindmapPrompt, ...options}
|
||||
if (config.mindmapLlmTemperature != null) node.temperature = config.mindmapLlmTemperature
|
||||
smartPlan.value = await documentApi.previewSummary(markdown, node)
|
||||
const created = await documentApi.createMindmapTask(taskId, markdown, 'hybrid', config.mindmapPrompt, {
|
||||
model_id: config.mindmapModelId,
|
||||
temperature: config.mindmapLlmTemperature
|
||||
})
|
||||
startSmartOrganizePolling(taskId)
|
||||
temperature: config.mindmapLlmTemperature,
|
||||
options: config.summaryOptions,
|
||||
}, { heading_structure: results.value?.headingStructure, original: originalRecognized.value || results.value?.original || markdown })
|
||||
smartOrganizeTaskId.value = created.task_id || taskId
|
||||
if (created.task_id) rememberSummaryTask(created.task_id)
|
||||
startSmartOrganizePolling(smartOrganizeTaskId.value)
|
||||
} catch (err) {
|
||||
isSmartOrganizing.value = false
|
||||
sourceContentMode.value = 'source'
|
||||
smartOrganizeError.value = (err as Error).message || '智能整理任务创建失败'
|
||||
smartOrganizeError.value = (err as Error).message || '智能总结任务创建失败'
|
||||
ElMessage.error(smartOrganizeError.value)
|
||||
}
|
||||
}
|
||||
|
||||
async function restoreSummaryTask() {
|
||||
if (router.currentRoute.value.query.artifact) return
|
||||
let taskId = ''
|
||||
try { taskId = sessionStorage.getItem(summaryRecoveryKey()) || '' } catch { return }
|
||||
if (!/^[a-f0-9-]{36}$/i.test(taskId) || results.value || isProcessing.value) return
|
||||
try {
|
||||
const data = await documentApi.getMindmapProgress(taskId)
|
||||
if (results.value || isProcessing.value || router.currentRoute.value.query.artifact) return
|
||||
const source = data.summary_input?.markdown
|
||||
const node = data.summary_node
|
||||
if (!source || !node) { forgetSummaryTask(); return }
|
||||
const taskOptions = { ...DEFAULT_DOCUMENT_CONFIG.summaryOptions }
|
||||
for (const key of Object.keys(taskOptions)) {
|
||||
if (node[key] != null) (taskOptions as any)[key] = node[key]
|
||||
else if (['context_tokens', 'max_input_tokens', 'max_output_tokens', 'input_tokens', 'output_tokens', 'safety_tokens', 'top_p'].includes(key)) (taskOptions as any)[key] = null
|
||||
}
|
||||
originalRecognized.value = data.original_md || source
|
||||
results.value = { markdown: source, source, original: data.original_md || source,
|
||||
headingStructure: data.summary_input?.heading_structure, headingMarkdown: source,
|
||||
mindmap: buildMindmapMarkdown(source), taskId }
|
||||
smartOrganizeTaskId.value = taskId
|
||||
smartRequestSnapshot.value = JSON.stringify([source, node.model_id, node.prompt, node.temperature ?? null,
|
||||
taskOptions, data.summary_input?.heading_structure])
|
||||
smartPlan.value = data.plan || null
|
||||
smartSources.value = data.sources || []
|
||||
activeTab.value = 'source'
|
||||
if (data.status === 'completed') {
|
||||
smartMarkdownContent.value = data.result_md || ''
|
||||
smartSourceSnapshot.value = smartMarkdownContent.value
|
||||
smartHeadingStructure.value = data.heading_structure
|
||||
smartHeadingMarkdown.value = smartMarkdownContent.value
|
||||
showSmartResult()
|
||||
} else if (data.status === 'failed') {
|
||||
smartTaskFailed.value = true
|
||||
smartOrganizeError.value = data.error || '总结失败,可继续处理'
|
||||
} else {
|
||||
isSmartOrganizing.value = true
|
||||
startSmartOrganizePolling(taskId)
|
||||
}
|
||||
} catch { forgetSummaryTask() }
|
||||
}
|
||||
|
||||
const retrySmartOrganize = async () => {
|
||||
if (isSmartOrganizing.value || smartResultStale.value) return
|
||||
isSmartOrganizing.value = true
|
||||
smartOrganizeError.value = ''
|
||||
try {
|
||||
await documentApi.retryMindmapTask(smartOrganizeTaskId.value)
|
||||
smartTaskFailed.value = false
|
||||
startSmartOrganizePolling(smartOrganizeTaskId.value)
|
||||
} catch (err) {
|
||||
isSmartOrganizing.value = false
|
||||
smartOrganizeError.value = (err as Error).message
|
||||
}
|
||||
}
|
||||
|
||||
watch(sourceContentMode, (value) => {
|
||||
if (applyingSmartResult) { applyingSmartResult = false; return }
|
||||
if (value === 'smart') {
|
||||
startSmartOrganize()
|
||||
}
|
||||
|
|
@ -545,15 +711,12 @@ const handleBackendChange = (backend: string) => {
|
|||
}
|
||||
|
||||
const applyConfig = (nextConfig: typeof config) => {
|
||||
Object.assign(config, nextConfig)
|
||||
try {
|
||||
window.localStorage.setItem(DOCUMENT_CONFIG_STORAGE_KEY, JSON.stringify(nextConfig))
|
||||
} catch {
|
||||
// 浏览器禁用本地存储时不影响当前页面使用。
|
||||
}
|
||||
setPersonalConfig(nextConfig)
|
||||
}
|
||||
|
||||
const router = useRouter()
|
||||
const handleProcessDocument = async () => {
|
||||
if (isProcessing.value || isSmartOrganizing.value) return
|
||||
mindmapDocumentKey.value = `parse-${Date.now()}`
|
||||
openedArtifact.value = null
|
||||
originalRecognized.value = ''
|
||||
|
|
@ -564,7 +727,7 @@ const handleProcessDocument = async () => {
|
|||
return
|
||||
}
|
||||
if (results.value) {
|
||||
activeTab.value = 'markdown'
|
||||
activeTab.value = 'mindmap'
|
||||
sourceViewMode.value = 'markdown'
|
||||
ElMessage.success('转换完成')
|
||||
}
|
||||
|
|
@ -629,6 +792,7 @@ const handleMindmapNodeEdit = ({ start, end, original, newText }: { start: numbe
|
|||
}
|
||||
|
||||
const triggerUpload = () => {
|
||||
if (isProcessing.value || isSmartOrganizing.value) return
|
||||
fileInput.value?.click()
|
||||
}
|
||||
|
||||
|
|
@ -656,6 +820,7 @@ const handleFileInputChange = async (event: Event) => {
|
|||
const handleDrop = async (event: DragEvent) => {
|
||||
event.preventDefault()
|
||||
isDragging.value = false
|
||||
if (isProcessing.value || isSmartOrganizing.value) return
|
||||
|
||||
if (event.dataTransfer?.files) {
|
||||
mindmapDocumentKey.value = `upload-${Date.now()}`
|
||||
|
|
@ -676,6 +841,7 @@ const handleDrop = async (event: DragEvent) => {
|
|||
}
|
||||
|
||||
const removeFile = (index: number) => {
|
||||
if (isProcessing.value || isSmartOrganizing.value) return
|
||||
uploadedFiles.value.splice(index, 1)
|
||||
uploadedDisplayFiles.value.splice(index, 1)
|
||||
if (uploadedFiles.value.length === 0) {
|
||||
|
|
@ -690,41 +856,100 @@ const clearAllFiles = () => {
|
|||
}
|
||||
|
||||
const originalRecognized = ref('')
|
||||
const stopArtifactListener = onOpenArtifact((artifact) => {
|
||||
let artifactLoadGeneration = 0
|
||||
function applySavedArtifact(artifact: any) {
|
||||
const generation = ++artifactLoadGeneration
|
||||
resetSmartOrganizeState()
|
||||
replacementRules.value = []
|
||||
draftReplacementRules.value = []
|
||||
showReplacementDialog.value = false
|
||||
openedArtifact.value = artifact
|
||||
graphLayout.value = artifact.layout || 'tree'
|
||||
mindmapDocumentKey.value = `artifact-${artifact.id}`
|
||||
uploadedFiles.value = []
|
||||
uploadedDisplayFiles.value = []
|
||||
originalRecognized.value = artifact.original || ''
|
||||
results.value = { markdown: artifact.markdown, source: artifact.markdown, mindmap: buildMindmapMarkdown(artifact.markdown) }
|
||||
const savedSummary = artifact.summary_state || {}
|
||||
const source = savedSummary.source_markdown || artifact.markdown
|
||||
smartMarkdownContent.value = savedSummary.smart_markdown || ''
|
||||
const restoreSmart = savedSummary.mode === 'smart' && !!smartMarkdownContent.value
|
||||
if (restoreSmart) {
|
||||
sourceContentMode.value = 'smart'
|
||||
smartHeadingStructure.value = artifact.heading_structure
|
||||
smartHeadingMarkdown.value = smartMarkdownContent.value
|
||||
}
|
||||
results.value = { markdown: source, source, original: artifact.original, headingStructure: sourceContentMode.value === 'source' ? artifact.heading_structure : undefined, headingMarkdown: source, mindmap: buildMindmapMarkdown(artifact.markdown), taskId: artifact.task_id || undefined }
|
||||
if (artifact.task_id) documentApi.getMindmapProgress(artifact.task_id).then(data => {
|
||||
if (generation === artifactLoadGeneration && openedArtifact.value?.id === artifact.id && data.status === 'completed' && data.result_md === artifact.markdown) {
|
||||
smartSources.value = data.sources || []
|
||||
smartSourceSnapshot.value = data.result_md || ''
|
||||
smartPlan.value = data.plan || null
|
||||
}
|
||||
}).catch(() => { /* The original task may have been deleted. */ })
|
||||
activeTab.value = 'mindmap'
|
||||
}
|
||||
const stopArtifactListener = onOpenArtifact((artifact) => {
|
||||
if (openedArtifact.value?.id !== artifact.id || openedArtifact.value?.revision !== artifact.revision) applySavedArtifact(artifact)
|
||||
})
|
||||
async function openSavedArtifact(artifact: any) {
|
||||
applySavedArtifact(artifact)
|
||||
await router.replace({path: '/mindmap', query: {...router.currentRoute.value.query, artifact: artifact.id}})
|
||||
}
|
||||
async function confirmOpenSavedArtifact() {
|
||||
const current = openedArtifact.value
|
||||
const changed = !!activeSourceMarkdownContent.value.trim() && (!current || current.markdown !== activeSourceMarkdownContent.value || current.layout !== graphLayout.value)
|
||||
if (!changed) return true
|
||||
return ElMessageBox.confirm('当前内容尚未保存,是否打开这张导图?', '打开导图', {type: 'warning', confirmButtonText: '打开', cancelButtonText: '取消'}).then(() => true, () => false)
|
||||
}
|
||||
function renameSavedArtifact(artifact: any) {
|
||||
if (openedArtifact.value?.id === artifact.id) openedArtifact.value = {...openedArtifact.value, name: artifact.name, updated_at: artifact.updated_at}
|
||||
}
|
||||
async function deleteSavedArtifact(id: string) {
|
||||
if (openedArtifact.value?.id !== id) return
|
||||
openedArtifact.value = null
|
||||
const query = {...router.currentRoute.value.query}
|
||||
delete query.artifact
|
||||
await router.replace({query})
|
||||
}
|
||||
watch(() => results.value?.markdown, (value) => {
|
||||
if (!openedArtifact.value && value) originalRecognized.value = value
|
||||
if (!openedArtifact.value && value) originalRecognized.value = results.value?.original || originalRecognized.value || value
|
||||
})
|
||||
async function saveArtifact() {
|
||||
if (savingArtifact.value) return
|
||||
savingArtifact.value = true
|
||||
try {
|
||||
const current = openedArtifact.value
|
||||
const saved = await hubCall('artifacts', 'post', {
|
||||
name: current?.name || uploadedDisplayFiles.value[0]?.name || '思维导图',
|
||||
revision: current?.revision, markdown: activeSourceMarkdownContent.value,
|
||||
task_id: results.value?.taskId,
|
||||
original: originalRecognized.value || manualMarkdownContent.value,
|
||||
summary_state: { source_markdown: manualMarkdownContent.value, smart_markdown: smartMarkdownContent.value, mode: sourceContentMode.value },
|
||||
heading_structure: activeHeadingStructure.value,
|
||||
layout: graphLayout.value
|
||||
}, current?.id)
|
||||
openedArtifact.value = saved
|
||||
originalRecognized.value = saved.original || ''
|
||||
results.value = { markdown: saved.markdown, source: saved.markdown, mindmap: buildMindmapMarkdown(saved.markdown) }
|
||||
const source = saved.summary_state?.source_markdown || saved.markdown
|
||||
results.value = { markdown: source, source, original: saved.original, headingStructure: sourceContentMode.value === 'source' ? saved.heading_structure : undefined, headingMarkdown: source, mindmap: buildMindmapMarkdown(saved.markdown), taskId: saved.task_id || undefined }
|
||||
await router.replace({path: '/mindmap', query: {...router.currentRoute.value.query, artifact: saved.id}})
|
||||
ElMessage.success('已保存')
|
||||
} catch (e: any) { ElMessage.error(e.message || '保存失败') }
|
||||
finally { savingArtifact.value = false }
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
let componentActive = true
|
||||
onMounted(async () => {
|
||||
updateViewportState()
|
||||
window.addEventListener('resize', updateViewportState)
|
||||
const loaded = await loadWorkbenchDefaults()
|
||||
if (!componentActive) return
|
||||
if (!loaded) ElMessage.warning('工作台默认设置加载失败,请检查网络')
|
||||
await restoreSummaryTask()
|
||||
})
|
||||
|
||||
onUnmounted(() => {
|
||||
componentActive = false
|
||||
stopArtifactListener()
|
||||
stopSmartOrganizePolling()
|
||||
window.removeEventListener('resize', updateViewportState)
|
||||
|
|
@ -732,6 +957,9 @@ onUnmounted(() => {
|
|||
</script>
|
||||
|
||||
<style scoped>
|
||||
.summary-source-columns { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }
|
||||
.summary-source-columns > div { min-width: 0; }
|
||||
.summary-source-columns pre { white-space: pre-wrap; overflow-wrap: anywhere; max-height: 55vh; overflow: auto; font: inherit; }
|
||||
.document-processor {
|
||||
position: relative;
|
||||
height: 100%;
|
||||
|
|
@ -788,6 +1016,7 @@ onUnmounted(() => {
|
|||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
|
||||
.drag-upload-area {
|
||||
border: 2px dashed #CED4DA;
|
||||
border-radius: 8px;
|
||||
|
|
|
|||
|
|
@ -49,7 +49,7 @@ async function importFiles() {
|
|||
}
|
||||
try {
|
||||
selected.value = (await post('/local_model/imports', body, undefined, loading)).data
|
||||
MsgSuccess('已导入'); path.value = ''; files.value = []; await load()
|
||||
MsgSuccess(selected.value?.already_imported ? '模型已存在' : '已导入'); path.value = ''; files.value = []; await load()
|
||||
} catch { /* request interceptor displays the server error; retain the form */ }
|
||||
}
|
||||
onMounted(load)
|
||||
|
|
|
|||
|
|
@ -142,6 +142,7 @@
|
|||
<script setup lang="ts">
|
||||
import type { Provider, Model } from '@/api/type/model'
|
||||
import { computed, ref, onMounted, onBeforeUnmount } from 'vue'
|
||||
import { Download } from '@element-plus/icons-vue'
|
||||
import EditModel from '@/views/model/component/EditModel.vue'
|
||||
import ModelLoadingDialog from './ModelLoadingDialog.vue'
|
||||
import useStore from '@/stores'
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@
|
|||
<el-card shadow="never" class="policy-page__card">
|
||||
<el-tabs v-model="activeTab">
|
||||
<el-tab-pane :label="t('views.system.policy.configTab')" name="policy">
|
||||
<div class="policy-page__toolbar mb-16">
|
||||
<div class="policy-page__toolbar policy-page__toolbar--actions mb-16">
|
||||
<div class="policy-page__filters">
|
||||
<el-input
|
||||
v-model="policyQuery.name"
|
||||
|
|
@ -32,7 +32,7 @@
|
|||
</el-button>
|
||||
</div>
|
||||
|
||||
<el-table :data="policyList" border v-loading="policyLoading">
|
||||
<el-table :data="pagedPolicies" border v-loading="policyLoading">
|
||||
<el-table-column
|
||||
prop="name"
|
||||
:label="t('views.system.policy.policyName')"
|
||||
|
|
@ -81,6 +81,7 @@
|
|||
</template>
|
||||
</el-table-column>
|
||||
</el-table>
|
||||
<SystemPagination v-model:current-page="policyPage" v-model:page-size="policyPageSize" :total="policyList.length" />
|
||||
</el-tab-pane>
|
||||
|
||||
<el-tab-pane :label="t('views.system.policy.recordTab')" name="record">
|
||||
|
|
@ -105,7 +106,7 @@
|
|||
</div>
|
||||
</div>
|
||||
|
||||
<el-table :data="recordList" border v-loading="recordLoading">
|
||||
<el-table :data="pagedRecords" border v-loading="recordLoading">
|
||||
<el-table-column
|
||||
prop="policy_name"
|
||||
:label="t('views.system.policy.policyName')"
|
||||
|
|
@ -147,10 +148,11 @@
|
|||
show-overflow-tooltip
|
||||
/>
|
||||
</el-table>
|
||||
<SystemPagination v-model:current-page="recordPage" v-model:page-size="recordPageSize" :total="recordList.length" />
|
||||
</el-tab-pane>
|
||||
|
||||
<el-tab-pane label="当前绑定" name="bindings">
|
||||
<el-table :data="runtime.bindings" border>
|
||||
<el-table :data="pagedBindings" border>
|
||||
<el-table-column prop="policy_name" label="策略名称" min-width="180" />
|
||||
<el-table-column prop="target_name" label="应用对象" min-width="220" />
|
||||
<el-table-column prop="target_identifier" label="对象标识" min-width="250" />
|
||||
|
|
@ -161,13 +163,16 @@
|
|||
</template></el-table-column
|
||||
>
|
||||
</el-table>
|
||||
<SystemPagination v-model:current-page="bindingPage" v-model:page-size="bindingPageSize" :total="runtime.bindings.length" />
|
||||
<p class="mt-12 color-secondary">
|
||||
多个策略同时绑定时,须全部满足。停用或生效期外的策略不限制访问。
|
||||
</p>
|
||||
</el-tab-pane>
|
||||
|
||||
<el-tab-pane label="访问记录" name="decisions">
|
||||
<div class="policy-page__filters decision-filters mb-16">
|
||||
<div class="policy-page__toolbar policy-page__toolbar--actions mb-16">
|
||||
<el-button :disabled="decisionLoading" @click="loadDecisions">刷新</el-button>
|
||||
<div class="policy-page__filters decision-filters">
|
||||
<el-date-picker v-model="decisionQuery.time" type="datetimerange"
|
||||
value-format="YYYY-MM-DDTHH:mm:ss" start-placeholder="开始时间" end-placeholder="结束时间"
|
||||
range-separator="至" aria-label="访问时间(北京时间)" :default-time="decisionDefaultTime" />
|
||||
|
|
@ -181,7 +186,7 @@
|
|||
<el-input v-model="decisionQuery.reason" placeholder="拒绝原因" aria-label="拒绝原因" clearable maxlength="200" @keyup.enter="searchDecisions" />
|
||||
<el-button type="primary" :loading="decisionLoading" @click="searchDecisions">查询</el-button>
|
||||
<el-button @click="resetDecisionFilters">重置</el-button>
|
||||
<el-button :disabled="decisionLoading" @click="loadDecisions">刷新</el-button>
|
||||
</div>
|
||||
</div>
|
||||
<el-table v-loading="decisionLoading" :data="decisions" border>
|
||||
<el-table-column label="时间" min-width="180"
|
||||
|
|
@ -201,9 +206,8 @@
|
|||
<el-table-column prop="target" label="服务标识" min-width="250" />
|
||||
<el-table-column prop="reason" label="拒绝原因" min-width="220" />
|
||||
</el-table>
|
||||
<el-pagination class="mt-16" v-model:current-page="decisionPage.current"
|
||||
v-model:page-size="decisionPage.page_size" :total="decisionPage.total" :page-sizes="[10, 20, 50, 100]"
|
||||
layout="total, sizes, prev, pager, next, jumper" @change="changeDecisionPage" />
|
||||
<SystemPagination v-model:current-page="decisionPage.current"
|
||||
v-model:page-size="decisionPage.page_size" :total="decisionPage.total" @change="changeDecisionPage" />
|
||||
</el-tab-pane>
|
||||
|
||||
<el-tab-pane label="区域网段" name="regions">
|
||||
|
|
@ -461,6 +465,7 @@
|
|||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import SystemPagination from '@/components/system-pagination/index.vue'
|
||||
import { computed, onMounted, reactive, ref, watch } from 'vue'
|
||||
import type { FormInstance, FormRules } from 'element-plus'
|
||||
import accessControlPolicyApi from '@/api/system-settings/access-control-policy'
|
||||
|
|
@ -484,6 +489,14 @@ const applyFormRef = ref<FormInstance>()
|
|||
const policyList = ref<any[]>([])
|
||||
const recordList = ref<any[]>([])
|
||||
const runtime = reactive<any>({ bindings: [], targets: [], region_networks: [] })
|
||||
const policyPage = ref(1), policyPageSize = ref(10), recordPage = ref(1), recordPageSize = ref(10), bindingPage = ref(1), bindingPageSize = ref(10)
|
||||
const pageRows = (rows: any[], page: number, size: number) => rows.slice((page - 1) * size, page * size)
|
||||
const pagedPolicies = computed(() => pageRows(policyList.value, policyPage.value, policyPageSize.value))
|
||||
const pagedRecords = computed(() => pageRows(recordList.value, recordPage.value, recordPageSize.value))
|
||||
const pagedBindings = computed(() => pageRows(runtime.bindings, bindingPage.value, bindingPageSize.value))
|
||||
watch([() => policyList.value.length, policyPageSize], ([total, size]) => { policyPage.value = Math.min(policyPage.value, Math.max(1, Math.ceil(total / size))) })
|
||||
watch([() => recordList.value.length, recordPageSize], ([total, size]) => { recordPage.value = Math.min(recordPage.value, Math.max(1, Math.ceil(total / size))) })
|
||||
watch([() => runtime.bindings.length, bindingPageSize], ([total, size]) => { bindingPage.value = Math.min(bindingPage.value, Math.max(1, Math.ceil(total / size))) })
|
||||
const decisions = ref<any[]>([])
|
||||
const decisionLoading = ref(false)
|
||||
const decisionQuery = reactive({ time: [] as string[] | null, result: '', ip: '', device_type: '', reason: '' })
|
||||
|
|
@ -616,11 +629,13 @@ async function loadPolicies() {
|
|||
policyLoading,
|
||||
)
|
||||
policyList.value = response.data || []
|
||||
policyPage.value = 1
|
||||
}
|
||||
|
||||
async function loadRecords() {
|
||||
const response = await accessControlPolicyApi.getApplicationRecordList(recordQuery, recordLoading)
|
||||
recordList.value = response.data || []
|
||||
recordPage.value = 1
|
||||
}
|
||||
|
||||
function resetPolicyFilters() {
|
||||
|
|
@ -827,10 +842,11 @@ onMounted(async () => {
|
|||
&__toolbar {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
justify-content: flex-end;
|
||||
gap: 12px;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
&__toolbar--actions { justify-content: space-between; .policy-page__filters { order: 2; } }
|
||||
|
||||
&__filters {
|
||||
display: flex;
|
||||
|
|
|
|||
|
|
@ -1,17 +1,42 @@
|
|||
<template>
|
||||
<div class="mindmap-management p-16-24">
|
||||
<h2 class="mb-16">思维导图管理</h2>
|
||||
<el-card :body-style="{ padding: '0' }">
|
||||
<HubManager @open-artifact="openArtifact" />
|
||||
<el-breadcrumb separator-icon="ArrowRight" class="mb-16"><el-breadcrumb-item>服务管理</el-breadcrumb-item><el-breadcrumb-item><h5 class="ml-4 color-text-primary">导图设置</h5></el-breadcrumb-item></el-breadcrumb>
|
||||
<el-card>
|
||||
<el-tabs v-model="tab" @tab-change="changeTab">
|
||||
<el-tab-pane label="处理流程" name="flows" />
|
||||
<el-tab-pane label="导图服务" name="services" />
|
||||
<el-tab-pane label="运行记录" name="tasks" />
|
||||
<el-tab-pane label="导图管理" name="artifacts" />
|
||||
<el-tab-pane v-if="isAdmin" label="工作台设置" name="workbench" />
|
||||
</el-tabs>
|
||||
<WorkbenchSettings v-if="tab === 'workbench' && isAdmin" />
|
||||
<HubManager v-else :key="tab" :initial-tab="hubTab" hide-tabs @open-artifact="openArtifact" @tab-change="changeTab" />
|
||||
</el-card>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { useRouter } from 'vue-router'
|
||||
import { computed, ref, watch, onMounted } from 'vue'
|
||||
import { useRouter, useRoute } from 'vue-router'
|
||||
import HubManager from '@/views/mindmap/HubManager.vue'
|
||||
import WorkbenchSettings from './WorkbenchSettings.vue'
|
||||
import useStore from '@/stores'
|
||||
|
||||
const router = useRouter()
|
||||
const route = useRoute()
|
||||
const isAdmin = computed(() => useStore().user.is_admin())
|
||||
const tabs = ['flows', 'services', 'tasks', 'artifacts', 'workbench']
|
||||
function selectedTab(value: unknown) { return typeof value === 'string' && tabs.includes(value) && (value !== 'workbench' || isAdmin.value) ? value : 'services' }
|
||||
const tab = ref(selectedTab(route.query.tab))
|
||||
const hubTab = computed(() => tab.value as 'services' | 'flows' | 'tasks' | 'artifacts')
|
||||
function syncTab(value: unknown) {
|
||||
if (value === 'advanced') { router.replace({ path: '/system/advanced-flows' }); return }
|
||||
tab.value = selectedTab(value)
|
||||
}
|
||||
watch(() => route.query.tab, syncTab)
|
||||
watch(isAdmin, () => syncTab(route.query.tab))
|
||||
onMounted(() => syncTab(route.query.tab))
|
||||
function changeTab(value: string | number) { router.replace({ query: { ...route.query, tab: String(value) } }) }
|
||||
function openArtifact(artifact: { id: string }) {
|
||||
router.push({ path: '/mindmap', query: { artifact: artifact.id } })
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,12 +1,13 @@
|
|||
<template>
|
||||
<div class="p-16-24 access-page" v-loading="loading">
|
||||
<h2 class="mb-16">角色与授权</h2>
|
||||
<el-breadcrumb separator-icon="ArrowRight" class="mb-16"><el-breadcrumb-item><h5 class="color-text-primary">角色与授权</h5></el-breadcrumb-item></el-breadcrumb>
|
||||
<el-card shadow="never">
|
||||
<el-tabs v-model="tab">
|
||||
<el-tab-pane label="角色管理" name="roles">
|
||||
<div class="flex-between mb-16">
|
||||
<el-button type="primary" @click="editRole()">新建角色</el-button>
|
||||
</div>
|
||||
<el-table :data="config.roles" border>
|
||||
<el-table :data="pagedRoles" border>
|
||||
<el-table-column prop="name" label="角色名称" min-width="140"
|
||||
><template #default="{ row }"
|
||||
>{{ row.name }} <el-tag v-if="row.is_builtin" size="small">内置角色</el-tag
|
||||
|
|
@ -55,9 +56,16 @@
|
|||
></el-table-column
|
||||
>
|
||||
</el-table>
|
||||
<SystemPagination v-model:current-page="rolePage" v-model:page-size="rolePageSize" :total="config.roles.length" />
|
||||
</el-tab-pane>
|
||||
<el-tab-pane label="授权管理" name="grants">
|
||||
<div class="user-roles mb-16">
|
||||
<div class="grant-toolbar mb-16">
|
||||
<el-button type="primary" @click="editGrant()">新增授权</el-button>
|
||||
<div class="user-roles">
|
||||
<el-radio-group v-model="grantView"
|
||||
><el-radio-button value="resources">授权项</el-radio-button
|
||||
><el-radio-button value="subjects">授权明细</el-radio-button></el-radio-group
|
||||
>
|
||||
<el-radio-group v-model="grantCategory"
|
||||
><el-radio-button value="">全部</el-radio-button
|
||||
><el-radio-button value="MODULE">菜单</el-radio-button
|
||||
|
|
@ -83,13 +91,9 @@
|
|||
:label="userLabel(u)" /></el-option-group
|
||||
></el-select>
|
||||
<el-input v-model="keyword" placeholder="搜索菜单或资源" clearable style="width: 240px" />
|
||||
<el-button type="primary" @click="editGrant()">新增授权</el-button
|
||||
><el-button @click="clearFilters">重置</el-button>
|
||||
<el-button @click="clearFilters">重置</el-button>
|
||||
</div>
|
||||
</div>
|
||||
<el-radio-group v-model="grantView" class="mb-16"
|
||||
><el-radio-button value="resources">按授权项查看</el-radio-button
|
||||
><el-radio-button value="subjects">授权明细</el-radio-button></el-radio-group
|
||||
>
|
||||
<el-table key="overview" v-if="grantView === 'resources'" :data="pagedOverview" border>
|
||||
<el-table-column label="菜单 / 资源" min-width="220"
|
||||
><template #default="{ row }">{{ resourceName(row) }}</template></el-table-column
|
||||
|
|
@ -159,15 +163,11 @@
|
|||
></el-table-column
|
||||
>
|
||||
</el-table>
|
||||
<div class="app-table__pagination mt-16">
|
||||
<el-pagination
|
||||
<SystemPagination
|
||||
v-model:current-page="grantPage"
|
||||
v-model:page-size="grantPageSize"
|
||||
:page-sizes="[10, 20, 50, 100]"
|
||||
:total="grantTotal"
|
||||
layout="total, prev, pager, next, sizes"
|
||||
/>
|
||||
</div>
|
||||
</el-tab-pane>
|
||||
<el-tab-pane label="用户权限" name="users">
|
||||
<el-select
|
||||
|
|
@ -232,6 +232,7 @@
|
|||
</template>
|
||||
</el-tab-pane>
|
||||
</el-tabs>
|
||||
</el-card>
|
||||
<el-dialog
|
||||
v-model="roleDialog"
|
||||
:title="roleForm.id ? '编辑角色' : '新建角色'"
|
||||
|
|
@ -371,6 +372,7 @@ import { computed, onMounted, ref, watch } from 'vue'
|
|||
import { useRoute } from 'vue-router'
|
||||
import { ElMessage, ElMessageBox } from 'element-plus'
|
||||
import api from '@/api/system/platform-access'
|
||||
import SystemPagination from '@/components/system-pagination/index.vue'
|
||||
const tab = ref('roles'),
|
||||
loading = ref(false),
|
||||
saving = ref(false),
|
||||
|
|
@ -383,6 +385,9 @@ const config = ref<any>({
|
|||
modules: {},
|
||||
actions: {},
|
||||
})
|
||||
const rolePage = ref(1), rolePageSize = ref(10)
|
||||
const pagedRoles = computed(() => config.value.roles.slice((rolePage.value - 1) * rolePageSize.value, rolePage.value * rolePageSize.value))
|
||||
watch([() => config.value.roles.length, rolePageSize], ([total, size]) => { rolePage.value = Math.min(rolePage.value, Math.max(1, Math.ceil(total / size))) })
|
||||
const roleDialog = ref(false),
|
||||
grantDialog = ref(false),
|
||||
roleForm = ref<any>({}),
|
||||
|
|
@ -762,9 +767,10 @@ onMounted(async () => {
|
|||
})
|
||||
</script>
|
||||
<style scoped>
|
||||
.grant-toolbar { display:flex; justify-content:space-between; align-items:center; gap:12px; flex-wrap:wrap; }
|
||||
.access-page {
|
||||
padding-top: 40px;
|
||||
height: 100%;
|
||||
box-sizing: border-box;
|
||||
overflow: auto;
|
||||
background: var(--app-layout-bg-color);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
<template>
|
||||
<div class="user-manage p-16-24">
|
||||
<h2 class="mb-16">{{ $t('views.userManage.title') }}</h2>
|
||||
<el-breadcrumb separator-icon="ArrowRight" class="mb-16"><el-breadcrumb-item><h5 class="color-text-primary">{{ $t('views.userManage.title') }}</h5></el-breadcrumb-item></el-breadcrumb>
|
||||
<el-card class="user-manage__card" style="--el-card-padding: 0">
|
||||
<div class="user-manage__body p-24">
|
||||
<div class="user-manage__toolbar mb-16">
|
||||
|
|
@ -264,17 +264,13 @@
|
|||
</el-table>
|
||||
</div>
|
||||
|
||||
<div class="app-table__pagination mt-16">
|
||||
<el-pagination
|
||||
<SystemPagination
|
||||
v-model:current-page="paginationConfig.current_page"
|
||||
v-model:page-size="paginationConfig.page_size"
|
||||
:page-sizes="[10, 20, 50, 100]"
|
||||
:total="paginationConfig.total"
|
||||
layout="total, prev, pager, next, sizes"
|
||||
@size-change="handleSizeChange"
|
||||
@current-change="getList"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</el-card>
|
||||
<UserDrawer :title="title" ref="UserDrawerRef" @refresh="refresh" />
|
||||
|
|
@ -285,6 +281,7 @@
|
|||
</template>
|
||||
|
||||
<script lang="ts" setup>
|
||||
import SystemPagination from '@/components/system-pagination/index.vue'
|
||||
import { onMounted, reactive, ref } from 'vue'
|
||||
import UserDrawer from './component/UserDrawer.vue'
|
||||
import UserPwdDialog from './component/UserPwdDialog.vue'
|
||||
|
|
|
|||
|
|
@ -362,6 +362,15 @@ export const variableAssignNode = {
|
|||
},
|
||||
}
|
||||
|
||||
export const componentNode = {
|
||||
type: WorkflowType.ComponentNode,
|
||||
text: '调用文件读取、Markdown、模型、导图或 MCP 组件', label: '组件调用', height: 260,
|
||||
properties: { stepName: '组件调用', config: { fields: [
|
||||
{ label: '文本结果', value: 'text' }, { label: '结构化结果', value: 'structured' },
|
||||
{ label: '任务编号', value: 'task_id' },
|
||||
] } },
|
||||
}
|
||||
|
||||
export const mcpNode = {
|
||||
type: WorkflowType.McpNode,
|
||||
text: t('views.applicationWorkflow.nodes.mcpNode.text'),
|
||||
|
|
@ -375,6 +384,7 @@ export const mcpNode = {
|
|||
label: t('common.result'),
|
||||
value: 'result',
|
||||
},
|
||||
{ label: '文本结果', value: 'text' }, { label: '结构化结果', value: 'structured' }, { label: '任务编号', value: 'task_id' },
|
||||
],
|
||||
},
|
||||
},
|
||||
|
|
@ -653,7 +663,7 @@ export const menuNodes = [
|
|||
},
|
||||
{
|
||||
label: t('views.applicationWorkflow.nodes.classify.other'),
|
||||
list: [mcpNode, toolNode],
|
||||
list: [componentNode, mcpNode, toolNode],
|
||||
},
|
||||
]
|
||||
export const applicationLoopMenuNodes = [
|
||||
|
|
@ -691,7 +701,7 @@ export const applicationLoopMenuNodes = [
|
|||
},
|
||||
{
|
||||
label: t('views.applicationWorkflow.nodes.classify.other'),
|
||||
list: [mcpNode, toolNode],
|
||||
list: [componentNode, mcpNode, toolNode],
|
||||
},
|
||||
]
|
||||
|
||||
|
|
@ -785,6 +795,7 @@ export const nodeDict: any = {
|
|||
[WorkflowType.ImageGenerateNode]: imageGenerateNode,
|
||||
[WorkflowType.VariableAssignNode]: variableAssignNode,
|
||||
[WorkflowType.McpNode]: mcpNode,
|
||||
[WorkflowType.ComponentNode]: componentNode,
|
||||
[WorkflowType.TextToVideoGenerateNode]: textToVideoNode,
|
||||
[WorkflowType.ImageToVideoGenerateNode]: imageToVideoNode,
|
||||
[WorkflowType.IntentNode]: intentNode,
|
||||
|
|
|
|||
|
|
@ -250,6 +250,12 @@
|
|||
</div>
|
||||
</template>
|
||||
</div>
|
||||
<el-form-item label="平台能力">
|
||||
<el-select v-model="chat_data.model_setting.engine_tools" multiple placeholder="按需启用" class="w-full">
|
||||
<el-option label="知识库检索" value="knowledge-search" />
|
||||
<el-option label="生成并保存导图" value="mindmap-save" />
|
||||
</el-select>
|
||||
</el-form-item>
|
||||
<el-form-item @click.prevent v-if="chat_data.mcp_enable || chat_data.tool_enable">
|
||||
<template #label>
|
||||
<div class="flex-between">
|
||||
|
|
|
|||
|
|
@ -82,12 +82,12 @@
|
|||
</el-button>
|
||||
</div>
|
||||
</template>
|
||||
<el-select v-model="form_data.mcp_tool" @change="changeTool" filterable>
|
||||
<el-select v-model="toolSelection" @change="changeTool" filterable>
|
||||
<el-option
|
||||
v-for="item in form_data.mcp_tools"
|
||||
:key="item.value"
|
||||
:key="JSON.stringify([item.server, item.name])"
|
||||
:label="item.name"
|
||||
:value="item.name"
|
||||
:value="JSON.stringify([item.server, item.name])"
|
||||
class="flex align-center"
|
||||
>
|
||||
<el-tooltip
|
||||
|
|
@ -99,7 +99,7 @@
|
|||
<AppIcon iconName="app-warning" class="app-warning-icon"></AppIcon>
|
||||
</el-tooltip>
|
||||
|
||||
<span class="ml-4">{{ item.name }}</span>
|
||||
<span class="ml-4">{{ item.server }} / {{ item.name }}</span>
|
||||
</el-option>
|
||||
</el-select>
|
||||
</el-form-item>
|
||||
|
|
@ -248,6 +248,11 @@
|
|||
</el-form>
|
||||
</div>
|
||||
</template>
|
||||
<el-form label-position="top" class="p-8-12" @submit.prevent>
|
||||
<el-form-item label="调用超时(秒)"><el-input-number v-model="form_data.timeout" :min="1" :max="600" /></el-form-item>
|
||||
<el-form-item label="网络重试次数"><el-input-number v-model="form_data.retries" :min="0" :max="2" /><el-text size="small" type="info">仅为可重复调用的工具开启重试。</el-text></el-form-item>
|
||||
<el-form-item label="优先级(0~9)"><el-input-number v-model="form_data.priority" :min="0" :max="9" /></el-form-item>
|
||||
</el-form>
|
||||
<McpServerInputDialog ref="mcpServerInputDialogRef" @refresh="handleMcpVariables" />
|
||||
</NodeContainer>
|
||||
</template>
|
||||
|
|
@ -302,6 +307,7 @@ const wheel = (e: any) => {
|
|||
}
|
||||
}
|
||||
const form = {
|
||||
timeout: 60, retries: 0, priority: 5,
|
||||
mcp_tool: '',
|
||||
mcp_tools: [],
|
||||
mcp_servers: '',
|
||||
|
|
@ -365,7 +371,7 @@ function _getTools(mcp_servers: any) {
|
|||
MsgSuccess(t('views.applicationWorkflow.nodes.mcpNode.getToolsSuccess'))
|
||||
// 修改了json,刷新mcp_server
|
||||
form_data.value.mcp_server = form_data.value.mcp_tools.find(
|
||||
(item: any) => item.name === form_data.value.mcp_tool,
|
||||
(item: any) => item.name === form_data.value.mcp_tool && (!form_data.value.mcp_server || item.server === form_data.value.mcp_server),
|
||||
)?.server
|
||||
})
|
||||
}
|
||||
|
|
@ -411,13 +417,21 @@ function handleMcpVariables(vars: any) {
|
|||
_getTools(mcp_servers)
|
||||
}
|
||||
|
||||
const toolSelection = computed({
|
||||
get: () => form_data.value.mcp_tool ? JSON.stringify([form_data.value.mcp_server, form_data.value.mcp_tool]) : '',
|
||||
set: (value: string) => {
|
||||
const [server, name] = JSON.parse(value)
|
||||
form_data.value.mcp_server = server; form_data.value.mcp_tool = name
|
||||
},
|
||||
})
|
||||
|
||||
function changeTool() {
|
||||
form_data.value.mcp_server = form_data.value.mcp_tools.find(
|
||||
(item: any) => item.name === form_data.value.mcp_tool,
|
||||
(item: any) => item.name === form_data.value.mcp_tool && (!form_data.value.mcp_server || item.server === form_data.value.mcp_server),
|
||||
)?.server
|
||||
|
||||
const args_schema = form_data.value.mcp_tools.find(
|
||||
(item: any) => item.name === form_data.value.mcp_tool,
|
||||
(item: any) => item.name === form_data.value.mcp_tool && (!form_data.value.mcp_server || item.server === form_data.value.mcp_server),
|
||||
)?.args_schema
|
||||
form_data.value.tool_form_field = []
|
||||
for (const item in args_schema?.properties) {
|
||||
|
|
@ -428,7 +442,7 @@ function changeTool() {
|
|||
let input_type = 'TextInput'
|
||||
if (params[item2].type === 'string') {
|
||||
input_type = 'TextInput'
|
||||
} else if (params[item2].type === 'number') {
|
||||
} else if (['number', 'integer'].includes(params[item2].type)) {
|
||||
input_type = 'NumberInput'
|
||||
} else if (params[item2].type === 'boolean') {
|
||||
input_type = 'SwitchInput'
|
||||
|
|
@ -447,11 +461,11 @@ function changeTool() {
|
|||
},
|
||||
input_type: input_type,
|
||||
source: 'referencing',
|
||||
required: args_schema.properties[item].required?.indexOf(item2) !== -1,
|
||||
required: (args_schema.properties[item].required?.includes(item2) ?? false),
|
||||
props_info: {
|
||||
rules: [
|
||||
{
|
||||
required: args_schema.properties[item].required?.indexOf(item2) !== -1,
|
||||
required: (args_schema.properties[item].required?.includes(item2) ?? false),
|
||||
message: t('dynamicsForm.tip.requiredMessage'),
|
||||
trigger: 'blur',
|
||||
},
|
||||
|
|
@ -464,7 +478,7 @@ function changeTool() {
|
|||
let input_type = 'TextInput'
|
||||
if (args_schema.properties[item].type === 'string') {
|
||||
input_type = 'TextInput'
|
||||
} else if (args_schema.properties[item].type === 'number') {
|
||||
} else if (['number', 'integer'].includes(args_schema.properties[item].type)) {
|
||||
input_type = 'NumberInput'
|
||||
} else if (args_schema.properties[item].type === 'boolean') {
|
||||
input_type = 'SwitchInput'
|
||||
|
|
@ -483,11 +497,11 @@ function changeTool() {
|
|||
},
|
||||
input_type: input_type,
|
||||
source: 'referencing',
|
||||
required: args_schema.required?.indexOf(item) !== -1,
|
||||
required: (args_schema.required?.includes(item) ?? false),
|
||||
props_info: {
|
||||
rules: [
|
||||
{
|
||||
required: args_schema.required?.indexOf(item) !== -1,
|
||||
required: (args_schema.required?.includes(item) ?? false),
|
||||
message: t('dynamicsForm.tip.requiredMessage'),
|
||||
trigger: 'blur',
|
||||
},
|
||||
|
|
@ -520,6 +534,7 @@ const form_data = computed({
|
|||
|
||||
const replyNodeFormRef = ref()
|
||||
|
||||
const isEmpty = (value: any) => value === undefined || value === null || value === '' || (Array.isArray(value) && !value.length)
|
||||
const validate = async () => {
|
||||
// 对动态表单,只验证必填字段
|
||||
if (dynamicsFormRef.value) {
|
||||
|
|
@ -530,7 +545,7 @@ const validate = async () => {
|
|||
if (requiredFields.length > 0) {
|
||||
for (const item of requiredFields) {
|
||||
if (form_data.value.params_nested) {
|
||||
if (!form_data.value.tool_params[form_data.value.params_nested][item]) {
|
||||
if (isEmpty(form_data.value.tool_params[form_data.value.params_nested][item])) {
|
||||
return Promise.reject({
|
||||
node: props.nodeModel,
|
||||
errMessage: item + t('dynamicsForm.tip.requiredMessage'),
|
||||
|
|
@ -538,7 +553,7 @@ const validate = async () => {
|
|||
}
|
||||
} else {
|
||||
// 这里是没有嵌套的情况
|
||||
if (!form_data.value.tool_params[item]) {
|
||||
if (isEmpty(form_data.value.tool_params[item])) {
|
||||
return Promise.reject({
|
||||
node: props.nodeModel,
|
||||
errMessage: item + t('dynamicsForm.tip.requiredMessage'),
|
||||
|
|
@ -588,6 +603,7 @@ function getMcpToolSelectOptions() {
|
|||
}
|
||||
|
||||
onMounted(() => {
|
||||
form_data.value.timeout ??= 60; form_data.value.retries ??= 0; form_data.value.priority ??= 5
|
||||
if (typeof props.nodeModel.properties.node_data?.is_result === 'undefined') {
|
||||
if (isLastNode(props.nodeModel)) {
|
||||
set(props.nodeModel.properties.node_data, 'is_result', true)
|
||||
|
|
|
|||
Loading…
Reference in New Issue