File size: 5,147 Bytes
04f192c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | # SPDX-FileCopyrightText: Copyright contributors to the CopilotJ project.
#
# SPDX-License-Identifier: Apache-2.0
import glob
import importlib
import logging
import os
import tomllib
from copilotj.core import FunctionTool, ModelClient, Tool
from copilotj.core.config import Config
from copilotj.multiagent.Executor import Executor
__all__ = ["load_agent_configs"]
_log = logging.getLogger(__name__)
GLOB_PATTERN = os.path.join(os.path.dirname(__file__), "agent_configs", "*_agent.toml")
def load_agent_configs(*, model_client: ModelClient, cfg: Config):
return _load_agent_configs(GLOB_PATTERN, model_client=model_client, cfg=cfg)
def _load_agent_configs(glob_pattern: str, *, model_client: ModelClient, cfg: Config):
agents = {}
configs = glob.glob(glob_pattern)
_log.info("Found %d configs in %s.", len(configs), glob_pattern)
for file in configs:
try:
with open(file, "rb") as f:
config = tomllib.load(f)
except Exception as e:
_log.error("Failed to load %s: %s", file, e)
continue
# Skip files that are commented out or empty
if not config or config == "pass":
_log.info("Skipping %s: empty or commented out", file)
continue
if "name" not in config or "class" not in config:
_log.warning("Configuration file %s is missing required 'name' or 'class' field", file)
continue
module_class_str = config["class"]
module_name, class_name = module_class_str.rsplit(".", 1)
_log.info("Loading agent class %s from module %s...", class_name, module_name)
try:
module = importlib.import_module(module_name)
agent_class = getattr(module, class_name)
assert issubclass(agent_class, Executor), f"{agent_class} is not a subclass of Executor"
name = config["name"]
description = config.get("description", "")
prompt = config.get("prompt", "")
agent_tools_config = config.get("tools", [])
# Load and prepare tools with descriptions
agent_tools: list[Tool] = []
for tool_conf in agent_tools_config:
tool_name = tool_conf.get("name")
tool_display_name = tool_conf.get("display_name")
tool_description = tool_conf.get("description", f"Tool for {tool_name}")
if "function" in tool_conf:
func_full = tool_conf["function"]
mod_name, func_name = func_full.rsplit(".", 1)
mod = importlib.import_module(mod_name)
fn = getattr(mod, func_name)
agent_tools.append(
FunctionTool(fn, tool_description, name=tool_name, display_name=tool_display_name)
)
elif "factory" in tool_conf:
factory_full = tool_conf["factory"]
mod_name, factory_name = factory_full.rsplit(".", 1)
mod = importlib.import_module(mod_name)
factory_fn = getattr(mod, factory_name)
fn = factory_fn(cfg)
agent_tools.append(
FunctionTool(fn, tool_description, name=tool_name, display_name=tool_display_name)
)
elif "class" in tool_conf:
class_full = tool_conf["class"]
mod_name, tool_class_name = class_full.rsplit(".", 1)
mod = importlib.import_module(mod_name)
tool_class = getattr(mod, tool_class_name)
assert issubclass(tool_class, Tool), f"{tool_class} is not a subclass of Tool"
agent_tools.append(tool_class())
else:
_log.warning(
"Tool configuration for %s is missing 'function', 'factory', or 'class' field", tool_name
)
_log.info("Loaded tools for %s: %s", name, list(agent_tools))
# Create agent instance with tool descriptions
agents[name] = agent_class(
name=name, description=description, prompt=prompt, tools=agent_tools, model_client=model_client
)
_log.info("Successfully loaded agent: %s", name)
except Exception as e:
_log.error("Error loading agent from %s: %s", file, e, exc_info=True)
return agents
if __name__ == "__main__":
from copilotj.core import load_config, new_model_client
cfg = load_config()
# Test: Load agent configurations and print each agent's tool list
print("Loading agent configurations...")
agent_configs = os.path.join(os.path.dirname(__file__), "agent_configs")
agents = load_agent_configs(model_client=new_model_client(cfg), cfg=cfg)
for name, agent in agents.items():
print(f"Loaded agent: {name}")
print(
"Agent tools:",
getattr(agent, "tools", "No tools attribute"),
getattr(agent, "tool_descriptions", "No tool descriptions"),
)
|