diff --git a/.gitignore b/.gitignore index 684a756..9720f3b 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ *.pyc dist/ build/ +docs/ *.egg-info/ *.egg *.pyc diff --git a/README.md b/README.md index ec73b51..f120e3b 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,14 @@ cd nanobot pip install -e . ``` +**Install with uv** + +```bash +uv venv +source .venv/bin/activate +uv pip install nanobot-ai +``` + ## πŸš€ Quick Start > [!TIP] @@ -327,7 +335,11 @@ nanobot/ └── cli/ # πŸ–₯️ Commands ``` -## πŸ—ΊοΈ Roadmap +## 🀝 Contribute & Roadmap + +PRs welcome! The codebase is intentionally small and readable. πŸ€— + +**Roadmap** β€” Pick an item and [open a PR](https://github.com/HKUDS/nanobot/pulls)! - [x] **Voice Transcription** β€” Support for Groq Whisper (Issue #13) - [ ] **Multi-modal** β€” See and hear (images, voice, video) @@ -336,14 +348,16 @@ nanobot/ - [ ] **More integrations** β€” Discord, Slack, email, calendar - [ ] **Self-improvement** β€” Learn from feedback and mistakes -**Want to help?** Pick an item and [open a PR](https://github.com/HKUDS/nanobot/pulls)! +### Contributors + + + + --- ## ⭐ Star History -*Community Growth Trajectory* -
@@ -354,12 +368,6 @@ nanobot/
---- - -## 🀝 Contribute - -PRs welcome! The codebase is intentionally small and readable. πŸ€— -

Thanks for visiting ✨ nanobot!

Views diff --git a/nanobot/agent/context.py b/nanobot/agent/context.py index aaba890..f70103d 100644 --- a/nanobot/agent/context.py +++ b/nanobot/agent/context.py @@ -1,5 +1,7 @@ """Context builder for assembling agent prompts.""" +import base64 +import mimetypes from pathlib import Path from typing import Any @@ -114,32 +116,53 @@ When remembering something, write to {workspace_path}/memory/MEMORY.md""" self, history: list[dict[str, Any]], current_message: str, - skill_names: list[str] | None = None + skill_names: list[str] | None = None, + media: list[str] | None = None, ) -> list[dict[str, Any]]: """ Build the complete message list for an LLM call. - + Args: history: Previous conversation messages. current_message: The new user message. skill_names: Optional skills to include. - + media: Optional list of local file paths for images/media. + Returns: List of messages including system prompt. """ messages = [] - + # System prompt system_prompt = self.build_system_prompt(skill_names) messages.append({"role": "system", "content": system_prompt}) - + # History messages.extend(history) - - # Current message - messages.append({"role": "user", "content": current_message}) - + + # Current message (with optional image attachments) + user_content = self._build_user_content(current_message, media) + messages.append({"role": "user", "content": user_content}) + return messages + + def _build_user_content(self, text: str, media: list[str] | None) -> str | list[dict[str, Any]]: + """Build user message content with optional base64-encoded images.""" + if not media: + return text + + images = [] + for path in media: + p = Path(path) + mime, _ = mimetypes.guess_type(path) + if not p.is_file() or not mime or not mime.startswith("image/"): + continue + b64 = base64.b64encode(p.read_bytes()).decode() + images.append({"type": "image_url", "image_url": {"url": f"data:{mime};base64,{b64}"}}) + + if not images: + return text + return images + [{"type": "text", "text": text}] def add_tool_result( self, diff --git a/nanobot/agent/loop.py b/nanobot/agent/loop.py index 6fe2cfd..4a96b84 100644 --- a/nanobot/agent/loop.py +++ b/nanobot/agent/loop.py @@ -152,7 +152,8 @@ class AgentLoop: # Build initial messages (use get_history for LLM-formatted messages) messages = self.context.build_messages( history=session.get_history(), - current_message=msg.content + current_message=msg.content, + media=msg.media if msg.media else None, ) # Agent loop @@ -189,7 +190,8 @@ class AgentLoop: # Execute tools for tool_call in response.tool_calls: - logger.debug(f"Executing tool: {tool_call.name}") + args_str = json.dumps(tool_call.arguments) + logger.debug(f"Executing tool: {tool_call.name} with arguments: {args_str}") result = await self.tools.execute(tool_call.name, tool_call.arguments) messages = self.context.add_tool_result( messages, tool_call.id, tool_call.name, result @@ -281,7 +283,8 @@ class AgentLoop: ) for tool_call in response.tool_calls: - logger.debug(f"Executing tool: {tool_call.name}") + args_str = json.dumps(tool_call.arguments) + logger.debug(f"Executing tool: {tool_call.name} with arguments: {args_str}") result = await self.tools.execute(tool_call.name, tool_call.arguments) messages = self.context.add_tool_result( messages, tool_call.id, tool_call.name, result diff --git a/nanobot/channels/base.py b/nanobot/channels/base.py index d83367c..8f16399 100644 --- a/nanobot/channels/base.py +++ b/nanobot/channels/base.py @@ -72,7 +72,14 @@ class BaseChannel(ABC): if not allow_list: return True - return str(sender_id) in allow_list + sender_str = str(sender_id) + if sender_str in allow_list: + return True + if "|" in sender_str: + for part in sender_str.split("|"): + if part and part in allow_list: + return True + return False async def _handle_message( self, diff --git a/nanobot/channels/telegram.py b/nanobot/channels/telegram.py index dc2f77c..75b9299 100644 --- a/nanobot/channels/telegram.py +++ b/nanobot/channels/telegram.py @@ -199,8 +199,10 @@ class TelegramChannel(BaseChannel): user = update.effective_user chat_id = message.chat_id - # Get sender identifier (prefer username, fallback to user_id) - sender_id = str(user.username or user.id) + # Use stable numeric ID, but keep username for allowlist compatibility + sender_id = str(user.id) + if user.username: + sender_id = f"{sender_id}|{user.username}" # Store chat_id for replies self._chat_ids[sender_id] = chat_id diff --git a/nanobot/cli/commands.py b/nanobot/cli/commands.py index 8dcc460..d293564 100644 --- a/nanobot/cli/commands.py +++ b/nanobot/cli/commands.py @@ -506,6 +506,7 @@ def cron_add( at: str = typer.Option(None, "--at", help="Run once at time (ISO format)"), deliver: bool = typer.Option(False, "--deliver", "-d", help="Deliver response to channel"), to: str = typer.Option(None, "--to", help="Recipient for delivery"), + channel: str = typer.Option(None, "--channel", help="Channel for delivery (e.g. 'telegram', 'whatsapp')"), ): """Add a scheduled job.""" from nanobot.config.loader import get_data_dir @@ -534,6 +535,7 @@ def cron_add( message=message, deliver=deliver, to=to, + channel=channel, ) console.print(f"[green]βœ“[/green] Added job '{job.name}' ({job.id})") @@ -624,11 +626,13 @@ def status(): has_openrouter = bool(config.providers.openrouter.api_key) has_anthropic = bool(config.providers.anthropic.api_key) has_openai = bool(config.providers.openai.api_key) + has_gemini = bool(config.providers.gemini.api_key) has_vllm = bool(config.providers.vllm.api_base) console.print(f"OpenRouter API: {'[green]βœ“[/green]' if has_openrouter else '[dim]not set[/dim]'}") console.print(f"Anthropic API: {'[green]βœ“[/green]' if has_anthropic else '[dim]not set[/dim]'}") console.print(f"OpenAI API: {'[green]βœ“[/green]' if has_openai else '[dim]not set[/dim]'}") + console.print(f"Gemini API: {'[green]βœ“[/green]' if has_gemini else '[dim]not set[/dim]'}") vllm_status = f"[green]βœ“ {config.providers.vllm.api_base}[/green]" if has_vllm else "[dim]not set[/dim]" console.print(f"vLLM/Local: {vllm_status}") diff --git a/nanobot/config/schema.py b/nanobot/config/schema.py index ee245f1..71e3361 100644 --- a/nanobot/config/schema.py +++ b/nanobot/config/schema.py @@ -51,7 +51,9 @@ class ProvidersConfig(BaseModel): openai: ProviderConfig = Field(default_factory=ProviderConfig) openrouter: ProviderConfig = Field(default_factory=ProviderConfig) groq: ProviderConfig = Field(default_factory=ProviderConfig) + zhipu: ProviderConfig = Field(default_factory=ProviderConfig) vllm: ProviderConfig = Field(default_factory=ProviderConfig) + gemini: ProviderConfig = Field(default_factory=ProviderConfig) class GatewayConfig(BaseModel): @@ -90,20 +92,24 @@ class Config(BaseSettings): return Path(self.agents.defaults.workspace).expanduser() def get_api_key(self) -> str | None: - """Get API key in priority order: OpenRouter > Anthropic > OpenAI > Groq > vLLM.""" + """Get API key in priority order: OpenRouter > Anthropic > OpenAI > Gemini > Zhipu > Groq > vLLM.""" return ( self.providers.openrouter.api_key or self.providers.anthropic.api_key or self.providers.openai.api_key or + self.providers.gemini.api_key or + self.providers.zhipu.api_key or self.providers.groq.api_key or self.providers.vllm.api_key or None ) def get_api_base(self) -> str | None: - """Get API base URL if using OpenRouter or vLLM.""" + """Get API base URL if using OpenRouter, Zhipu or vLLM.""" if self.providers.openrouter.api_key: return self.providers.openrouter.api_base or "https://openrouter.ai/api/v1" + if self.providers.zhipu.api_key: + return self.providers.zhipu.api_base if self.providers.vllm.api_base: return self.providers.vllm.api_base return None diff --git a/nanobot/providers/litellm_provider.py b/nanobot/providers/litellm_provider.py index f8e8456..547626d 100644 --- a/nanobot/providers/litellm_provider.py +++ b/nanobot/providers/litellm_provider.py @@ -13,7 +13,7 @@ class LiteLLMProvider(LLMProvider): """ LLM provider using LiteLLM for multi-provider support. - Supports OpenRouter, Anthropic, OpenAI, and many other providers through + Supports OpenRouter, Anthropic, OpenAI, Gemini, and many other providers through a unified interface. """ @@ -47,6 +47,10 @@ class LiteLLMProvider(LLMProvider): os.environ.setdefault("ANTHROPIC_API_KEY", api_key) elif "openai" in default_model or "gpt" in default_model: os.environ.setdefault("OPENAI_API_KEY", api_key) + elif "gemini" in default_model.lower(): + os.environ.setdefault("GEMINI_API_KEY", api_key) + elif "zhipu" in default_model or "glm" in default_model or "zai" in default_model: + os.environ.setdefault("ZHIPUAI_API_KEY", api_key) elif "groq" in default_model: os.environ.setdefault("GROQ_API_KEY", api_key) @@ -83,11 +87,24 @@ class LiteLLMProvider(LLMProvider): if self.is_openrouter and not model.startswith("openrouter/"): model = f"openrouter/{model}" + # For Zhipu/Z.ai, ensure prefix is present + # Handle cases like "glm-4.7-flash" -> "zhipu/glm-4.7-flash" + if ("glm" in model.lower() or "zhipu" in model.lower()) and not ( + model.startswith("zhipu/") or + model.startswith("zai/") or + model.startswith("openrouter/") + ): + model = f"zhipu/{model}" + # For vLLM, use hosted_vllm/ prefix per LiteLLM docs # Convert openai/ prefix to hosted_vllm/ if user specified it if self.is_vllm: model = f"hosted_vllm/{model}" + # For Gemini, ensure gemini/ prefix if not already present + if "gemini" in model.lower() and not model.startswith("gemini/"): + model = f"gemini/{model}" + kwargs: dict[str, Any] = { "model": model, "messages": messages, diff --git a/workspace/AGENTS.md b/workspace/AGENTS.md index 0e5a055..a99a7b4 100644 --- a/workspace/AGENTS.md +++ b/workspace/AGENTS.md @@ -22,6 +22,16 @@ You have access to: - Use `memory/` directory for daily notes - Use `MEMORY.md` for long-term information +## Scheduled Reminders + +When user asks for a reminder at a specific time, use `exec` to run: +``` +nanobot cron add --name "reminder" --message "Your message" --at "YYYY-MM-DDTHH:MM:SS" --deliver --to "USER_ID" --channel "CHANNEL" +``` +Get USER_ID and CHANNEL from the current session (e.g., `8281248569` and `telegram` from `telegram:8281248569`). + +**Do NOT just write reminders to MEMORY.md** β€” that won't trigger actual notifications. + ## Heartbeat Tasks `HEARTBEAT.md` is checked every 30 minutes. You can manage periodic tasks by editing this file: