File size: 1,947 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
# SPDX-FileCopyrightText: Copyright contributors to the CopilotJ project.
#
# SPDX-License-Identifier: Apache-2.0

import asyncio

from copilotj.core import Pattern, load_config
from copilotj.core.model_client import new_model_client
from copilotj.multiagent.agent_loader import load_agent_configs


class LeaderDriven(Pattern):
    def __init__(self) -> None:
        super().__init__("copilotj.leader_driven")
        self.dialog_counter = 1

        # Load all agents from agent_configs and store in a dictionary
        cfg = load_config()
        self.agents = load_agent_configs(model_client=new_model_client(cfg), cfg=cfg)
        for agent in self.agents.values():
            self.register(agent)

    async def run_agent(self, agent_name: str, task: str):
        agent = self.agents.get(agent_name)
        if not agent:
            return f"Agent '{agent_name}' not found. Available agents: {list(self.agents.keys())}"
        return await agent.run(task)

    async def test_all_agents(self):
        results = {}
        for name, agent in self.agents.items():
            results[name] = await agent.run("test")
        return results


if __name__ == "__main__":
    from copilotj.core import load_config

    async def main():
        load_config()

        group = LeaderDriven()

        while True:
            agent_name = input("Enter agent name (or 'all' for testing all agents, 'exit' to quit): ")
            if agent_name.lower() == "exit":
                print("Catch you later!")
                break
            task = input("Task: ")
            if agent_name.lower() == "all":
                results = await group.test_all_agents()
                for name, output in results.items():
                    print(f"Output from {name}: {output}")
            else:
                result = await group.run_agent(agent_name, task)
                print(f"Output from {agent_name}: {result}")

    asyncio.run(main())