From 2fded1f83c06b58727d3457d1d4945e960080381 Mon Sep 17 00:00:00 2001 From: ymq Date: Sat, 15 Aug 2026 19:23:32 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20read=5Ffile=E6=94=AF=E6=8C=81=E8=A7=A3?= =?UTF-8?q?=E6=9E=90docx(zipfile=E6=8F=90=E5=8F=96=E6=96=87=E6=9C=AC),?= =?UTF-8?q?=E9=81=BF=E5=85=8Dagent=E8=AF=BBdocx=E6=8A=A5=E9=94=99=E7=BB=95?= =?UTF-8?q?=E5=8E=BBrun=5Fcommand=E6=8E=A2=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pipeline_service/agent_loop_v2.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/pipeline_service/agent_loop_v2.py b/pipeline_service/agent_loop_v2.py index 532babf..38f6acc 100644 --- a/pipeline_service/agent_loop_v2.py +++ b/pipeline_service/agent_loop_v2.py @@ -775,8 +775,21 @@ class AgentExecutor: try: if not os.path.isfile(full): return f"FAIL: 文件不存在 {path}" - with open(full, encoding="utf-8") as f: - return f.read()[:30000] + ext = os.path.splitext(full)[1].lower() + # docx:提取 word/document.xml 文本 + if ext == '.docx': + import zipfile + import re as _re + with zipfile.ZipFile(full) as z: + xml = z.read('word/document.xml').decode('utf-8', errors='ignore') + texts = _re.findall(r']*>(.*?)', xml) + return ('\n'.join(texts)[:30000]) or '(docx 无文本内容)' + # 纯文本类:直接读 + if ext in ('.txt', '.md', '.json', '.csv', '.py', '.log', '.yaml', '.yml', '.xml', '.html', '.ini', ''): + with open(full, encoding="utf-8", errors="ignore") as f: + return f.read()[:30000] + # 其他二进制 + return f"该文件是二进制格式({ext or '无扩展名'}),无法直接读取文本。可改用 run_command 处理,或让用户上传文本版本。" except Exception as e: return f"ERROR: {str(e)[:300]}"