From 5c8f8f695aae4f7796f7c7379d82f9412bd967a6 Mon Sep 17 00:00:00 2001 From: ymq Date: Sat, 15 Aug 2026 18:55:13 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=90=8E=E7=AB=AF=E6=8F=90=E5=8F=96=E6=96=87=E6=9C=AC=E5=86=85?= =?UTF-8?q?=E5=AE=B9(docx/txt)=E4=BD=9C=E4=B8=BA=E4=B8=AD=E6=80=A7?= =?UTF-8?q?=E4=B8=8A=E4=B8=8B=E6=96=87=E6=B3=A8=E5=85=A5agent,=E7=BB=93?= =?UTF-8?q?=E5=90=88prompt=E7=94=B1agent=E5=86=B3=E5=AE=9A=E5=8A=A8?= =?UTF-8?q?=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- wwwroot/api/cockpit_chat.dspy | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/wwwroot/api/cockpit_chat.dspy b/wwwroot/api/cockpit_chat.dspy index abe39e8..b101ee6 100644 --- a/wwwroot/api/cockpit_chat.dspy +++ b/wwwroot/api/cockpit_chat.dspy @@ -1,9 +1,28 @@ # cockpit_chat.dspy - SDLC Agent Loop v3.1 (streaming NDJSON) import aiohttp import os +import re import shutil +import zipfile from ahserver.filestorage import FileStorage + +def _extract_text(path, name): + """提取文件文本内容(docx/txt/md等),返回文本或空字符串。二进制/无法解析返回空。""" + ext = os.path.splitext(name)[1].lower() + try: + if ext in ('.txt', '.md', '.json', '.csv', '.py', '.log', '.yaml', '.yml', '.xml', '.html', '.ini'): + with open(path, 'r', encoding='utf-8', errors='ignore') as f: + return f.read()[:15000] + if ext == '.docx': + with zipfile.ZipFile(path) as z: + xml = z.read('word/document.xml').decode('utf-8', errors='ignore') + texts = re.findall(r']*>(.*?)', xml) + return '\n'.join(texts)[:15000] + except Exception: + pass + return '' + action = (params_kw or {}).get('action', 'send_message') # AgentIO sends JSON body without action field — detect from message_text presence if action != 'send_message': @@ -567,8 +586,14 @@ if action == 'send_message': except Exception as _e: debug(f"UPLOAD FAIL {_fp}: {_e}") if uploaded_files: - _fl = "\n".join(f"- {n}(工作空间路径: {d})" for n, d in uploaded_files) - msgs.append({"role": "system", "content": f"用户本次上传了文件,已保存到工作空间 uploads/ 目录,可用 read_file 工具读取文件内容。文件清单:\n{_fl}"}) + _parts = [] + for _n, _d in uploaded_files: + _txt = _extract_text(_d, _n) + if _txt: + _parts.append(f"【文件 {_n} 内容】\n{_txt}") + else: + _parts.append(f"【文件 {_n}】无法直接读取文本(可能是二进制),相对路径 uploads/{_n},可用 run_command 处理") + msgs.append({"role": "system", "content": "用户本次上传了文件,内容如下:\n\n" + "\n\n".join(_parts)}) msgs.append({"role":"user","content":message_text}) if uid: await sor.C('pipeline_conversations',{'id':getID(),'role':'user','content':message_text,'msg_type':'text','org_id':org_id,'created_by':uid,'iteration_id':ctx['pid'] or ''})