Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion frontends/chatapp_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,26 @@ def clean_reply(text):
return re.sub(r"\n{3,}", "\n\n", text).strip() or "..."


_FILE_PLACEHOLDERS = {
"filepath",
"<filepath>",
"path",
"<path>",
"file_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):
Expand Down
4 changes: 2 additions & 2 deletions frontends/fsapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand Down
32 changes: 32 additions & 0 deletions tests/test_chatapp_common.py
Original file line number Diff line number Diff line change
@@ -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:<filepath>] [FILE:path] [FILE:<path>] "
"[FILE:file_path] [FILE:<file_path>] [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()