From f294e9d065845b799fc201792da9e61b83d56eb5 Mon Sep 17 00:00:00 2001 From: Re-bin Date: Tue, 24 Feb 2026 16:15:21 +0000 Subject: [PATCH] refactor: merge runtime context helpers and move imports to top --- nanobot/agent/context.py | 40 ++++++++++++++-------------------------- 1 file changed, 14 insertions(+), 26 deletions(-) diff --git a/nanobot/agent/context.py b/nanobot/agent/context.py index b3c4791..088d4c5 100644 --- a/nanobot/agent/context.py +++ b/nanobot/agent/context.py @@ -3,6 +3,8 @@ import base64 import mimetypes import platform +import time +from datetime import datetime from pathlib import Path from typing import Any @@ -103,32 +105,21 @@ Reply directly with text for conversations. Only use the 'message' tool to send - Recall past events: grep {workspace_path}/memory/HISTORY.md""" @staticmethod - def _build_runtime_context(channel: str | None, chat_id: str | None) -> str: - """Build dynamic runtime context and attach it to the tail user message.""" - from datetime import datetime - import time as _time - + def _inject_runtime_context( + user_content: str | list[dict[str, Any]], + channel: str | None, + chat_id: str | None, + ) -> str | list[dict[str, Any]]: + """Append dynamic runtime context to the tail of the user message.""" now = datetime.now().strftime("%Y-%m-%d %H:%M (%A)") - tz = _time.strftime("%Z") or "UTC" + tz = time.strftime("%Z") or "UTC" lines = [f"Current Time: {now} ({tz})"] if channel and chat_id: - lines.append(f"Channel: {channel}") - lines.append(f"Chat ID: {chat_id}") - return "\n".join(lines) - - @staticmethod - def _append_runtime_context( - user_content: str | list[dict[str, Any]], - runtime_context: str, - ) -> str | list[dict[str, Any]]: - """Append runtime context at the tail of the user message.""" - runtime_block = f"[Runtime Context]\n{runtime_context}" + lines += [f"Channel: {channel}", f"Chat ID: {chat_id}"] + block = "[Runtime Context]\n" + "\n".join(lines) if isinstance(user_content, str): - return f"{user_content}\n\n{runtime_block}" - - content = list(user_content) - content.append({"type": "text", "text": runtime_block}) - return content + return f"{user_content}\n\n{block}" + return [*user_content, {"type": "text", "text": block}] def _load_bootstrap_files(self) -> str: """Load all bootstrap files from workspace.""" @@ -176,10 +167,7 @@ Reply directly with text for conversations. Only use the 'message' tool to send # Current message (with optional image attachments) user_content = self._build_user_content(current_message, media) - user_content = self._append_runtime_context( - user_content=user_content, - runtime_context=self._build_runtime_context(channel, chat_id), - ) + user_content = self._inject_runtime_context(user_content, channel, chat_id) messages.append({"role": "user", "content": user_content}) return messages