File size: 3,146 Bytes
20f108c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { PhantomAgent } from "./agent.js";
import { AgentMessage, AgentIdentity, AGENT_ARCHETYPES } from "./types.js";
import { EventBus } from "../core/eventbus.js";
import { LLMProvider } from "../providers/openai.js";

export class AgentManager {
  private agents: Map<string, PhantomAgent> = new Map();
  private bus: EventBus;
  private llm?: LLMProvider;

  constructor(llm?: LLMProvider) {
    this.bus = EventBus.getInstance();
    this.llm = llm;
  }

  setLLM(provider: LLMProvider): void {
    this.llm = provider;
    this.agents.forEach((a) => a.setLLM(provider));
  }

  spawnAgent(
    name?: string,
    role?: string,
    persona?: string
  ): PhantomAgent {
    const archetype =
      AGENT_ARCHETYPES[Math.floor(Math.random() * AGENT_ARCHETYPES.length)];
    const agent = new PhantomAgent(
      name || archetype.name,
      role || archetype.role,
      persona || archetype.persona,
      this.llm
    );
    this.agents.set(agent.identity.id, agent);
    this.bus.emit("agent:spawned", agent.identity);
    return agent;
  }

  spawnArchetype(index: number): PhantomAgent {
    if (index < 0 || index >= AGENT_ARCHETYPES.length) {
      return this.spawnAgent();
    }
    const a = AGENT_ARCHETYPES[index];
    return this.spawnAgent(a.name, a.role, a.persona);
  }

  spawnDefaults(): void {
    AGENT_ARCHETYPES.slice(0, 4).forEach((a) =>
      this.spawnAgent(a.name, a.role, a.persona)
    );
  }

  getAgent(id: string): PhantomAgent | undefined {
    return this.agents.get(id);
  }

  listAgents(): AgentIdentity[] {
    return Array.from(this.agents.values()).map((a) => a.identity);
  }

  async broadcast(fromId: string, content: string): Promise<void> {
    const from = this.agents.get(fromId);
    if (!from) return;

    const msg: AgentMessage = {
      from: fromId,
      to: "all",
      content,
      timestamp: Date.now(),
      type: "text",
    };

    await Promise.all(
      Array.from(this.agents.entries())
        .filter(([id]) => id !== fromId)
        .map(([, agent]) => agent.receive(msg))
    );
  }

  async sendMessage(fromId: string, toId: string, content: string): Promise<void> {
    const from = this.agents.get(fromId);
    const to = this.agents.get(toId);
    if (!from || !to) return;

    const msg: AgentMessage = {
      from: fromId,
      to: toId,
      content,
      timestamp: Date.now(),
      type: "text",
    };

    await to.receive(msg);
  }

  async debate(topic: string): Promise<void> {
    const allAgents = Array.from(this.agents.values());
    if (allAgents.length < 2) return;

    const starter = allAgents[0];
    const msg: AgentMessage = {
      from: starter.identity.id,
      to: "all",
      content: `Let's debate: ${topic}. Share your perspective.`,
      timestamp: Date.now(),
      type: "text",
    };

    await Promise.all(
      allAgents.slice(1).map((agent) => agent.receive(msg))
    );
  }

  removeAgent(id: string): void {
    this.agents.delete(id);
    this.bus.emit("agent:removed", id);
  }

  evolveAll(): void {
    this.agents.forEach((agent) => agent.evolve());
  }

  count(): number {
    return this.agents.size;
  }
}