Skip to content
Open
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
17 changes: 13 additions & 4 deletions agent/image_routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,12 @@
_IMAGE_EXT_PATTERN = "|".join(e.lstrip(".") for e in _IMAGE_EXTS)

# Absolute / home-relative local image path. Matches the same shape gateway's
# extract_local_files() uses: anchors to ``~/`` or ``/``, ignores matches inside
# URLs (the ``(?<![/:\w.])`` lookbehind), and case-insensitive on the extension.
# extract_local_files() uses, plus native Windows drive paths. The lookbehind

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add regression coverage for both C:\\...\\image.png and C:/.../image.png, plus a relative-drive and URL-fragment negative case. Current extractor tests cover only POSIX-shaped paths.

# prevents matching path-shaped fragments inside URLs.
_LOCAL_IMAGE_PATH_RE = re.compile(
r"(?<![/:\w.])(?:~/|/)(?:[\w.\-]+/)*[\w.\-]+\.(?:" + _IMAGE_EXT_PATTERN + r")\b",
r"(?<![/:\w.])(?:~[/\\]|/|[A-Za-z]:[/\\])[^<>\"'`\r\n]+?\.(?:"
+ _IMAGE_EXT_PATTERN
+ r")\b",
re.IGNORECASE,
)

Expand Down Expand Up @@ -109,13 +111,20 @@ def extract_image_refs(text: str) -> Tuple[List[str], List[str]]:
def _in_code(pos: int) -> bool:
return any(s <= pos < e for s, e in code_spans)

def _expand_local_path(raw: str) -> str:
if raw.startswith(("~/", "~\\")):
home = os.environ.get("HOME")
if home:
return os.path.normpath(os.path.join(home, raw[2:]))
return os.path.expanduser(raw)

local_paths: list[str] = []
seen_paths: set[str] = set()
for match in _LOCAL_IMAGE_PATH_RE.finditer(text):
if _in_code(match.start()):
continue
raw = match.group(0)
expanded = os.path.expanduser(raw)
expanded = _expand_local_path(raw)
try:
if not os.path.isfile(expanded):
continue
Expand Down
Loading