mirror of
https://github.com/All-Hands-AI/OpenHands.git
synced 2026-01-10 07:18:10 -05:00
feat: Add user directory support for microagents (#9333)
Co-authored-by: openhands <openhands@all-hands.dev>
This commit is contained in:
@@ -2,6 +2,7 @@ import asyncio
|
||||
import os
|
||||
import uuid
|
||||
from datetime import datetime, timezone
|
||||
from pathlib import Path
|
||||
from typing import Callable
|
||||
|
||||
import openhands
|
||||
@@ -33,6 +34,8 @@ GLOBAL_MICROAGENTS_DIR = os.path.join(
|
||||
'microagents',
|
||||
)
|
||||
|
||||
USER_MICROAGENTS_DIR = Path.home() / '.openhands' / 'microagents'
|
||||
|
||||
|
||||
class Memory:
|
||||
"""
|
||||
@@ -77,6 +80,9 @@ class Memory:
|
||||
# from typically OpenHands/microagents (i.e., the PUBLIC microagents)
|
||||
self._load_global_microagents()
|
||||
|
||||
# Load user microagents from ~/.openhands/microagents/
|
||||
self._load_user_microagents()
|
||||
|
||||
def on_event(self, event: Event):
|
||||
"""Handle an event from the event stream."""
|
||||
asyncio.get_event_loop().run_until_complete(self._on_event(event))
|
||||
@@ -274,6 +280,41 @@ class Memory:
|
||||
if isinstance(r_agent, RepoMicroagent):
|
||||
self.repo_microagents[name] = r_agent
|
||||
|
||||
def _load_user_microagents(self) -> None:
|
||||
"""
|
||||
Loads microagents from the user's home directory (~/.openhands/microagents/)
|
||||
Creates the directory if it doesn't exist.
|
||||
"""
|
||||
try:
|
||||
# Create the user microagents directory if it doesn't exist
|
||||
os.makedirs(USER_MICROAGENTS_DIR, exist_ok=True)
|
||||
|
||||
# Load microagents from user directory
|
||||
repo_agents, knowledge_agents = load_microagents_from_dir(
|
||||
USER_MICROAGENTS_DIR
|
||||
)
|
||||
|
||||
# Add user microagents to the collections
|
||||
# User microagents can override global ones with the same name
|
||||
for name, agent in knowledge_agents.items():
|
||||
if isinstance(agent, KnowledgeMicroagent):
|
||||
self.knowledge_microagents[name] = agent
|
||||
logger.debug(f'Loaded user knowledge microagent: {name}')
|
||||
|
||||
for name, agent in repo_agents.items():
|
||||
if isinstance(agent, RepoMicroagent):
|
||||
self.repo_microagents[name] = agent
|
||||
logger.debug(f'Loaded user repo microagent: {name}')
|
||||
|
||||
if repo_agents or knowledge_agents:
|
||||
logger.info(
|
||||
f'Loaded {len(repo_agents) + len(knowledge_agents)} user microagents from {USER_MICROAGENTS_DIR}'
|
||||
)
|
||||
except Exception as e:
|
||||
logger.warning(
|
||||
f'Failed to load user microagents from {USER_MICROAGENTS_DIR}: {str(e)}'
|
||||
)
|
||||
|
||||
def get_microagent_mcp_tools(self) -> list[MCPConfig]:
|
||||
"""
|
||||
Get MCP tools from all repo microagents (always active)
|
||||
|
||||
Reference in New Issue
Block a user