fix: read_file支持解析docx(zipfile提取文本),避免agent读docx报错绕去run_command探测
This commit is contained in:
parent
2cdd3cb986
commit
2fded1f83c
@ -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'<w:t[^>]*>(.*?)</w:t>', 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]}"
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user