2026-02-19 14:06:06 -08:00
|
|
|
"""Utilities for building multimodal content blocks."""
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import base64
|
|
|
|
|
import logging
|
|
|
|
|
import mimetypes
|
2026-02-22 18:57:49 -08:00
|
|
|
import os
|
2026-02-19 14:06:06 -08:00
|
|
|
import re
|
|
|
|
|
from typing import Any
|
2026-03-20 14:34:14 -07:00
|
|
|
from urllib.parse import urlparse
|
2026-02-19 14:06:06 -08:00
|
|
|
|
|
|
|
|
import httpx
|
|
|
|
|
from langchain_core.messages.content import create_image_block
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
IMAGE_MARKDOWN_RE = re.compile(r"!\[[^\]]*\]\((https?://[^\s)]+)\)")
|
|
|
|
|
IMAGE_URL_RE = re.compile(
|
|
|
|
|
r"(https?://[^\s)]+\.(?:png|jpe?g|gif|webp|bmp|tiff)(?:\?[^\s)]+)?)",
|
|
|
|
|
re.IGNORECASE,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def extract_image_urls(text: str) -> list[str]:
|
|
|
|
|
"""Extract image URLs from markdown image syntax and direct image links."""
|
|
|
|
|
if not text:
|
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
|
urls: list[str] = []
|
|
|
|
|
urls.extend(IMAGE_MARKDOWN_RE.findall(text))
|
|
|
|
|
urls.extend(IMAGE_URL_RE.findall(text))
|
|
|
|
|
|
2026-02-24 12:11:24 -08:00
|
|
|
deduped = dedupe_urls(urls)
|
2026-02-19 14:06:06 -08:00
|
|
|
if deduped:
|
|
|
|
|
logger.debug("Extracted %d image URL(s)", len(deduped))
|
|
|
|
|
return deduped
|
|
|
|
|
|
|
|
|
|
|
2026-06-17 09:12:52 -07:00
|
|
|
def vision_not_supported_warning(model_id: str, image_count: int) -> str:
|
|
|
|
|
"""Build a prompt-visible warning when images are sent to a text-only model."""
|
|
|
|
|
return (
|
|
|
|
|
f"\n\n**Note:** {image_count} image(s) were attached but the current model "
|
|
|
|
|
f"({model_id}) does not support image input. The images were not included. "
|
|
|
|
|
"Please switch to a vision-enabled model to process images."
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2026-02-19 14:06:06 -08:00
|
|
|
async def fetch_image_block(
|
|
|
|
|
image_url: str,
|
|
|
|
|
client: httpx.AsyncClient,
|
|
|
|
|
) -> dict[str, Any] | None:
|
|
|
|
|
"""Fetch image bytes and build an image content block."""
|
|
|
|
|
try:
|
|
|
|
|
logger.debug("Fetching image from %s", image_url)
|
2026-02-22 18:57:49 -08:00
|
|
|
headers = None
|
2026-03-20 14:34:14 -07:00
|
|
|
host = (urlparse(image_url).hostname or "").lower()
|
|
|
|
|
if host == "uploads.linear.app" or host.endswith(".uploads.linear.app"):
|
2026-02-24 12:11:24 -08:00
|
|
|
linear_api_key = os.environ.get("LINEAR_API_KEY", "")
|
2026-02-22 18:57:49 -08:00
|
|
|
if linear_api_key:
|
|
|
|
|
headers = {"Authorization": linear_api_key}
|
|
|
|
|
else:
|
|
|
|
|
logger.warning(
|
|
|
|
|
"LINEAR_API_KEY not set; cannot authenticate image fetch for %s",
|
|
|
|
|
image_url,
|
|
|
|
|
)
|
2026-03-20 14:34:14 -07:00
|
|
|
elif host == "files.slack.com" or host.endswith(".files.slack.com"):
|
|
|
|
|
slack_bot_token = os.environ.get("SLACK_BOT_TOKEN", "")
|
|
|
|
|
if slack_bot_token:
|
|
|
|
|
headers = {"Authorization": f"Bearer {slack_bot_token}"}
|
|
|
|
|
else:
|
|
|
|
|
logger.warning(
|
|
|
|
|
"SLACK_BOT_TOKEN not set; cannot authenticate image fetch for %s",
|
|
|
|
|
image_url,
|
|
|
|
|
)
|
|
|
|
|
response = await client.get(image_url, headers=headers, follow_redirects=True)
|
2026-02-19 14:06:06 -08:00
|
|
|
response.raise_for_status()
|
|
|
|
|
content_type = response.headers.get("Content-Type", "").split(";")[0].strip()
|
|
|
|
|
if not content_type:
|
|
|
|
|
guessed, _ = mimetypes.guess_type(image_url)
|
2026-02-24 12:11:24 -08:00
|
|
|
if not guessed:
|
|
|
|
|
logger.warning(
|
|
|
|
|
"Could not determine content type for %s; skipping image",
|
|
|
|
|
image_url,
|
|
|
|
|
)
|
|
|
|
|
return None
|
|
|
|
|
content_type = guessed
|
2026-02-19 14:06:06 -08:00
|
|
|
|
2026-03-20 14:34:14 -07:00
|
|
|
supported_types = {"image/jpeg", "image/png", "image/gif", "image/webp"}
|
|
|
|
|
if content_type not in supported_types:
|
|
|
|
|
logger.warning(
|
|
|
|
|
"Unsupported content type '%s' for %s; skipping image",
|
|
|
|
|
content_type,
|
|
|
|
|
image_url,
|
|
|
|
|
)
|
|
|
|
|
return None
|
|
|
|
|
|
2026-02-19 14:06:06 -08:00
|
|
|
encoded = base64.b64encode(response.content).decode("ascii")
|
|
|
|
|
logger.info(
|
|
|
|
|
"Fetched image %s (%s, %d bytes)",
|
|
|
|
|
image_url,
|
|
|
|
|
content_type,
|
|
|
|
|
len(response.content),
|
|
|
|
|
)
|
|
|
|
|
return create_image_block(base64=encoded, mime_type=content_type)
|
|
|
|
|
except Exception:
|
|
|
|
|
logger.exception("Failed to fetch image from %s", image_url)
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
2026-02-24 12:11:24 -08:00
|
|
|
def dedupe_urls(urls: list[str]) -> list[str]:
|
2026-02-24 12:36:09 -08:00
|
|
|
return list(dict.fromkeys(urls))
|