From 9f66030172582156115a528ab6f7d3316f203c4d Mon Sep 17 00:00:00 2001 From: InkiChang Date: Mon, 1 Jun 2026 18:49:37 +0800 Subject: [PATCH 1/2] Filter FILE placeholder hints --- frontends/chatapp_common.py | 20 +++++++++++++++++++- frontends/fsapp.py | 4 ++-- frontends/wechatapp.py | 5 ++--- tests/test_chatapp_common.py | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 6 deletions(-) create mode 100644 tests/test_chatapp_common.py diff --git a/frontends/chatapp_common.py b/frontends/chatapp_common.py index befaf1c8d..c49cbf173 100644 --- a/frontends/chatapp_common.py +++ b/frontends/chatapp_common.py @@ -57,8 +57,26 @@ def clean_reply(text): return re.sub(r"\n{3,}", "\n\n", text).strip() or "..." +_FILE_PLACEHOLDERS = { + "filepath", + "", + "path", + "", + "file_path", + "", + "...", +} + + def extract_files(text): - return re.findall(r"\[FILE:([^\]]+)\]", text or "") + files, seen = [], set() + for fpath in re.findall(r"\[FILE:([^\]]+)\]", text or ""): + fpath = fpath.strip() + if not fpath or fpath.lower() in _FILE_PLACEHOLDERS or fpath in seen: + continue + files.append(fpath) + seen.add(fpath) + return files def strip_files(text): diff --git a/frontends/fsapp.py b/frontends/fsapp.py index 8fa97beed..a1451d515 100644 --- a/frontends/fsapp.py +++ b/frontends/fsapp.py @@ -9,7 +9,7 @@ sys.path.insert(0, PROJECT_ROOT) os.chdir(PROJECT_ROOT) from agentmain import GeneraticAgent -from frontends.chatapp_common import format_restore +from frontends.chatapp_common import extract_files, format_restore from frontends.continue_cmd import handle_frontend_command as handle_continue_frontend, reset_conversation from llmcore import mykeys @@ -116,7 +116,7 @@ def _clean(text): def _extract_files(text): - return re.findall(r"\[FILE:([^\]]+)\]", text or "") + return extract_files(text) def _strip_files(text): diff --git a/frontends/wechatapp.py b/frontends/wechatapp.py index 3c711014a..b4d670883 100644 --- a/frontends/wechatapp.py +++ b/frontends/wechatapp.py @@ -6,6 +6,7 @@ sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) _TEMP_DIR = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'temp') from agentmain import GeneraticAgent +from frontends.chatapp_common import extract_files # ── WxBotClient (inline from wx_bot_client.py) ── for _k in ('HTTPS_PROXY', 'https_proxy'): @@ -374,9 +375,7 @@ def _send(show): rest = _clean('\n\n'.join(done[sent:] + ['\n\n[任务已完成]']).strip()) if rest: _wx_send(rest[-3000:]) - files = re.findall(r'\[FILE:([^\]]+)\]', result) - bad = {'filepath', '', 'path', '', 'file_path', '', '...'} - files = [f for f in files if f.strip().lower() not in bad and (f if os.path.isabs(f) else os.path.join(_TEMP_DIR, f)) not in media_paths] + files = [f for f in extract_files(result) if (f if os.path.isabs(f) else os.path.join(_TEMP_DIR, f)) not in media_paths] for fpath in set(files): if not os.path.isabs(fpath): fpath = os.path.join(_TEMP_DIR, fpath) try: diff --git a/tests/test_chatapp_common.py b/tests/test_chatapp_common.py new file mode 100644 index 000000000..009bdbe15 --- /dev/null +++ b/tests/test_chatapp_common.py @@ -0,0 +1,32 @@ +import os +import sys +import unittest + +ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) +sys.path[:0] = [ROOT, os.path.join(ROOT, "frontends")] + +from frontends.chatapp_common import extract_files + + +class ExtractFilesTest(unittest.TestCase): + def test_ignores_file_hint_placeholders(self): + text = ( + "If you need to show files to user, use [FILE:filepath] in your response.\n" + "Other placeholders: [FILE:] [FILE:path] [FILE:] " + "[FILE:file_path] [FILE:] [FILE:...]" + ) + + self.assertEqual(extract_files(text), []) + + def test_keeps_real_paths_and_deduplicates(self): + text = ( + "Created [FILE:/tmp/report.txt]\n" + "Again [FILE:/tmp/report.txt]\n" + "Relative [FILE:outputs/chart.png]" + ) + + self.assertEqual(extract_files(text), ["/tmp/report.txt", "outputs/chart.png"]) + + +if __name__ == "__main__": + unittest.main() \ No newline at end of file From 99f2f66c452b69abe5e7a2cf3288d4efa5ed2907 Mon Sep 17 00:00:00 2001 From: InkiChang Date: Mon, 1 Jun 2026 18:56:32 +0800 Subject: [PATCH 2/2] Remove WeChat frontend change from PR --- frontends/wechatapp.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/frontends/wechatapp.py b/frontends/wechatapp.py index b4d670883..3c711014a 100644 --- a/frontends/wechatapp.py +++ b/frontends/wechatapp.py @@ -6,7 +6,6 @@ sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) _TEMP_DIR = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'temp') from agentmain import GeneraticAgent -from frontends.chatapp_common import extract_files # ── WxBotClient (inline from wx_bot_client.py) ── for _k in ('HTTPS_PROXY', 'https_proxy'): @@ -375,7 +374,9 @@ def _send(show): rest = _clean('\n\n'.join(done[sent:] + ['\n\n[任务已完成]']).strip()) if rest: _wx_send(rest[-3000:]) - files = [f for f in extract_files(result) if (f if os.path.isabs(f) else os.path.join(_TEMP_DIR, f)) not in media_paths] + files = re.findall(r'\[FILE:([^\]]+)\]', result) + bad = {'filepath', '', 'path', '', 'file_path', '', '...'} + files = [f for f in files if f.strip().lower() not in bad and (f if os.path.isabs(f) else os.path.join(_TEMP_DIR, f)) not in media_paths] for fpath in set(files): if not os.path.isabs(fpath): fpath = os.path.join(_TEMP_DIR, fpath) try: