Your lightweight open-source personal agent.
Python agent-frameworkai-agentai-agentschatbotchatopsdiscord-botllm-agentsllmslocal-llmmcpmodel-context-protocolmulti-agentopenai-compatibleopenclawpersonal-ai-assistantpythonself-hostedtelegram-bot-ai-assistantwebuiworkflow-automation
refactor: move detect_image_mime to utils/helpers for reuse
Re-bin3a01fe536a37
diff --git a/nanobot/agent/context.py b/nanobot/agent/context.pyindex 7ead317f3..27511fa91 100644--- a/nanobot/agent/context.py+++ b/nanobot/agent/context.py@@ -10,19 +10,7 @@ from typing import Anyfrom nanobot.agent.memory import MemoryStorefrom nanobot.agent.skills import SkillsLoader---def _detect_image_mime(data: bytes) -> str | None:- """Detect image MIME type from magic bytes, ignoring file extension."""- if data[:8] == b"\x89PNG\r\n\x1a\n":- return "image/png"- if data[:3] == b"\xff\xd8\xff":- return "image/jpeg"- if data[:6] in (b"GIF87a", b"GIF89a"):- return "image/gif"- if data[:4] == b"RIFF" and data[8:12] == b"WEBP":- return "image/webp"- return None+from nanobot.utils.helpers import detect_image_mimeclass ContextBuilder:@@ -153,7 +141,7 @@ Reply directly with text for conversations. Only use the 'message' tool to sendcontinueraw = p.read_bytes()# Detect real MIME type from magic bytes; fallback to filename guess- mime = _detect_image_mime(raw) or mimetypes.guess_type(path)[0]+ mime = detect_image_mime(raw) or mimetypes.guess_type(path)[0]if not mime or not mime.startswith("image/"):continueb64 = base64.b64encode(raw).decode()diff --git a/nanobot/utils/helpers.py b/nanobot/utils/helpers.pyindex 3a8c8023f..b54317408 100644--- a/nanobot/utils/helpers.py+++ b/nanobot/utils/helpers.py@@ -5,6 +5,19 @@ from datetime import datetimefrom pathlib import Path+def detect_image_mime(data: bytes) -> str | None:+ """Detect image MIME type from magic bytes, ignoring file extension."""+ if data[:8] == b"\x89PNG\r\n\x1a\n":+ return "image/png"+ if data[:3] == b"\xff\xd8\xff":+ return "image/jpeg"+ if data[:6] in (b"GIF87a", b"GIF89a"):+ return "image/gif"+ if data[:4] == b"RIFF" and data[8:12] == b"WEBP":+ return "image/webp"+ return None++def ensure_dir(path: Path) -> Path:"""Ensure directory exists, return it."""path.mkdir(parents=True, exist_ok=True)