Co-Authored-By: Quentin Torroba <quentin.torroba@mistral.ai>
Co-Authored-By: Michel Thomazo <michel.thomazo@mistral.ai>
Co-Authored-By: Clément Drouin <clement.drouin@mistral.ai>
Co-Authored-By: Vincent Guilloux <vincent.guilloux@mistral.ai>
Co-Authored-By: Clément Siriex <clement.sirieix@mistral.ai>
Co-Authored-By: Kim-Adeline Miguel <kimadeline.miguel@mistral.ai>
Co-Authored-By: Thaddee Tyl <thaddee.tyl@gmail.com>
Co-Authored-By: David Brochart <david.brochart@gmail.com>
Co-Authored-By: Joseph Guhlin <joseph.guhlin@gmail.com>
Co-Authored-By: Thomas Kenbeek <thomaskenbeek@gmail.com>
Co-Authored-By: Remenby31 <baptiste.cruvellier31@gmail.com>
This commit is contained in:
Mathias Gesbert 2026-01-27 16:39:30 +01:00 committed by Mathias Gesbert
parent 79f215d91c
commit d33db9fff8
217 changed files with 16911 additions and 4305 deletions

View file

@ -0,0 +1,31 @@
from __future__ import annotations
from vibe.core.agents.manager import AgentManager
from vibe.core.agents.models import (
ACCEPT_EDITS,
AUTO_APPROVE,
BUILTIN_AGENTS,
DEFAULT,
EXPLORE,
PLAN,
PLAN_AGENT_TOOLS,
AgentProfile,
AgentSafety,
AgentType,
BuiltinAgentName,
)
__all__ = [
"ACCEPT_EDITS",
"AUTO_APPROVE",
"BUILTIN_AGENTS",
"DEFAULT",
"EXPLORE",
"PLAN",
"PLAN_AGENT_TOOLS",
"AgentManager",
"AgentProfile",
"AgentSafety",
"AgentType",
"BuiltinAgentName",
]

165
vibe/core/agents/manager.py Normal file
View file

@ -0,0 +1,165 @@
from __future__ import annotations
from collections.abc import Callable
from logging import getLogger
from pathlib import Path
from typing import TYPE_CHECKING
from vibe.core.agents.models import (
BUILTIN_AGENTS,
AgentProfile,
AgentType,
BuiltinAgentName,
)
from vibe.core.paths.config_paths import resolve_local_agents_dir
from vibe.core.paths.global_paths import GLOBAL_AGENTS_DIR
from vibe.core.utils import name_matches
if TYPE_CHECKING:
from vibe.core.config import VibeConfig
logger = getLogger("vibe")
class AgentManager:
def __init__(
self,
config_getter: Callable[[], VibeConfig],
initial_agent: str = BuiltinAgentName.DEFAULT,
) -> None:
self._config_getter = config_getter
self._search_paths = self._compute_search_paths(self._config)
self._available: dict[str, AgentProfile] = self._discover_agents()
custom_count = len(self._available) - len(BUILTIN_AGENTS)
if custom_count > 0:
custom_names = [
name for name in self._available if name not in BUILTIN_AGENTS
]
logger.info(
"Discovered custom agents %s in %s",
" ".join(custom_names),
" ".join(str(p) for p in self._search_paths),
)
self.active_profile = self._available.get(
initial_agent, self._available[BuiltinAgentName.DEFAULT]
)
self._cached_config: VibeConfig | None = None
@property
def _config(self) -> VibeConfig:
return self._config_getter()
@property
def available_agents(self) -> dict[str, AgentProfile]:
if self._config.enabled_agents:
return {
name: profile
for name, profile in self._available.items()
if name_matches(name, self._config.enabled_agents)
}
if self._config.disabled_agents:
return {
name: profile
for name, profile in self._available.items()
if not name_matches(name, self._config.disabled_agents)
}
return dict(self._available)
@property
def config(self) -> VibeConfig:
if self._cached_config is None:
self._cached_config = self.active_profile.apply_to_config(self._config)
return self._cached_config
def switch_profile(self, name: str) -> None:
self.active_profile = self.get_agent(name)
self._cached_config = None
def invalidate_config(self) -> None:
self._cached_config = None
@staticmethod
def _compute_search_paths(config: VibeConfig) -> list[Path]:
paths: list[Path] = []
for path in config.agent_paths:
if path.is_dir():
paths.append(path)
if (agents_dir := resolve_local_agents_dir(Path.cwd())) is not None:
paths.append(agents_dir)
if GLOBAL_AGENTS_DIR.path.is_dir():
paths.append(GLOBAL_AGENTS_DIR.path)
unique: list[Path] = []
for p in paths:
rp = p.resolve()
if rp not in unique:
unique.append(rp)
return unique
def _discover_agents(self) -> dict[str, AgentProfile]:
agents: dict[str, AgentProfile] = dict(BUILTIN_AGENTS)
for base in self._search_paths:
if not base.is_dir():
continue
for agent_file in base.glob("*.toml"):
if not agent_file.is_file():
continue
if (agent := self._try_load_agent(agent_file)) is not None:
if agent.name in BUILTIN_AGENTS:
logger.info(
"Custom agent '%s' overrides builtin agent", agent.name
)
elif agent.name in agents:
logger.debug(
"Skipping duplicate agent '%s' at %s",
agent.name,
agent_file,
)
continue
agents[agent.name] = agent
return agents
def _try_load_agent(self, agent_file: Path) -> AgentProfile | None:
try:
agent = AgentProfile.from_toml(agent_file)
agent.apply_to_config(self._config)
return agent
except Exception as e:
logger.warning("Failed to load agent at %s: %s", agent_file, e)
return None
def get_agent(self, name: str) -> AgentProfile:
if agent := self.available_agents.get(name):
return agent
raise ValueError(f"Agent '{name}' not found")
def get_subagents(self) -> list[AgentProfile]:
return [
a
for a in self.available_agents.values()
if a.agent_type == AgentType.SUBAGENT
]
def get_agent_order(self) -> list[str]:
builtin_order: list[str] = [
BuiltinAgentName.DEFAULT,
BuiltinAgentName.PLAN,
BuiltinAgentName.ACCEPT_EDITS,
BuiltinAgentName.AUTO_APPROVE,
]
primary_agents = [
name
for name, agent in self.available_agents.items()
if agent.agent_type == AgentType.AGENT
]
order = [name for name in builtin_order if name in primary_agents]
custom = sorted(name for name in primary_agents if name not in builtin_order)
return order + custom
def next_agent(self, current: AgentProfile) -> AgentProfile:
order = self.get_agent_order()
idx = order.index(current.name) if current.name in order else -1
return self.available_agents[order[(idx + 1) % len(order)]]

122
vibe/core/agents/models.py Normal file
View file

@ -0,0 +1,122 @@
from __future__ import annotations
from dataclasses import dataclass, field
from enum import StrEnum, auto
from pathlib import Path
import tomllib
from typing import TYPE_CHECKING, Any
if TYPE_CHECKING:
from vibe.core.config import VibeConfig
def _deep_merge(base: dict[str, Any], override: dict[str, Any]) -> dict[str, Any]:
result = base.copy()
for key, value in override.items():
if key in result and isinstance(result[key], dict) and isinstance(value, dict):
result[key] = _deep_merge(result[key], value)
else:
result[key] = value
return result
class AgentSafety(StrEnum):
SAFE = auto()
NEUTRAL = auto()
DESTRUCTIVE = auto()
YOLO = auto()
class AgentType(StrEnum):
AGENT = auto()
SUBAGENT = auto()
class BuiltinAgentName(StrEnum):
DEFAULT = "default"
PLAN = "plan"
ACCEPT_EDITS = "accept-edits"
AUTO_APPROVE = "auto-approve"
EXPLORE = "explore"
@dataclass(frozen=True)
class AgentProfile:
name: str
display_name: str
description: str
safety: AgentSafety
agent_type: AgentType = AgentType.AGENT
overrides: dict[str, Any] = field(default_factory=dict)
def apply_to_config(self, base: VibeConfig) -> VibeConfig:
from vibe.core.config import VibeConfig as VC
merged = _deep_merge(base.model_dump(), self.overrides)
return VC.model_validate(merged)
@classmethod
def from_toml(cls, path: Path) -> AgentProfile:
with path.open("rb") as f:
data = tomllib.load(f)
return cls(
name=path.stem,
display_name=data.pop("display_name", path.stem.replace("-", " ").title()),
description=data.pop("description", ""),
safety=AgentSafety(data.pop("safety", AgentSafety.NEUTRAL)),
agent_type=AgentType(data.pop("agent_type", AgentType.AGENT)),
overrides=data,
)
PLAN_AGENT_TOOLS = ["grep", "read_file", "todo", "ask_user_question", "task"]
DEFAULT = AgentProfile(
BuiltinAgentName.DEFAULT,
"Default",
"Requires approval for tool executions",
AgentSafety.NEUTRAL,
)
PLAN = AgentProfile(
BuiltinAgentName.PLAN,
"Plan",
"Read-only agent for exploration and planning",
AgentSafety.SAFE,
overrides={"auto_approve": True, "enabled_tools": PLAN_AGENT_TOOLS},
)
ACCEPT_EDITS = AgentProfile(
BuiltinAgentName.ACCEPT_EDITS,
"Accept Edits",
"Auto-approves file edits only",
AgentSafety.DESTRUCTIVE,
overrides={
"tools": {
"write_file": {"permission": "always"},
"search_replace": {"permission": "always"},
}
},
)
AUTO_APPROVE = AgentProfile(
BuiltinAgentName.AUTO_APPROVE,
"Auto Approve",
"Auto-approves all tool executions",
AgentSafety.YOLO,
overrides={"auto_approve": True},
)
EXPLORE = AgentProfile(
name=BuiltinAgentName.EXPLORE,
display_name="Explore",
description="Read-only subagent for codebase exploration",
safety=AgentSafety.SAFE,
agent_type=AgentType.SUBAGENT,
overrides={"enabled_tools": ["grep", "read_file"]},
)
BUILTIN_AGENTS: dict[str, AgentProfile] = {
BuiltinAgentName.DEFAULT: DEFAULT,
BuiltinAgentName.PLAN: PLAN,
BuiltinAgentName.ACCEPT_EDITS: ACCEPT_EDITS,
BuiltinAgentName.AUTO_APPROVE: AUTO_APPROVE,
BuiltinAgentName.EXPLORE: EXPLORE,
}