@jackchen_me/open-multi-agent 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +280 -0
- package/dist/agent/agent.d.ts +121 -0
- package/dist/agent/agent.d.ts.map +1 -0
- package/dist/agent/agent.js +294 -0
- package/dist/agent/agent.js.map +1 -0
- package/dist/agent/pool.d.ts +128 -0
- package/dist/agent/pool.d.ts.map +1 -0
- package/dist/agent/pool.js +236 -0
- package/dist/agent/pool.js.map +1 -0
- package/dist/agent/runner.d.ts +120 -0
- package/dist/agent/runner.d.ts.map +1 -0
- package/dist/agent/runner.js +274 -0
- package/dist/agent/runner.js.map +1 -0
- package/dist/index.d.ts +73 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +87 -0
- package/dist/index.js.map +1 -0
- package/dist/llm/adapter.d.ts +38 -0
- package/dist/llm/adapter.d.ts.map +1 -0
- package/dist/llm/adapter.js +46 -0
- package/dist/llm/adapter.js.map +1 -0
- package/dist/llm/anthropic.d.ts +56 -0
- package/dist/llm/anthropic.d.ts.map +1 -0
- package/dist/llm/anthropic.js +307 -0
- package/dist/llm/anthropic.js.map +1 -0
- package/dist/llm/openai.d.ts +62 -0
- package/dist/llm/openai.d.ts.map +1 -0
- package/dist/llm/openai.js +424 -0
- package/dist/llm/openai.js.map +1 -0
- package/dist/memory/shared.d.ts +86 -0
- package/dist/memory/shared.d.ts.map +1 -0
- package/dist/memory/shared.js +155 -0
- package/dist/memory/shared.js.map +1 -0
- package/dist/memory/store.d.ts +64 -0
- package/dist/memory/store.d.ts.map +1 -0
- package/dist/memory/store.js +103 -0
- package/dist/memory/store.js.map +1 -0
- package/dist/orchestrator/orchestrator.d.ts +173 -0
- package/dist/orchestrator/orchestrator.d.ts.map +1 -0
- package/dist/orchestrator/orchestrator.js +698 -0
- package/dist/orchestrator/orchestrator.js.map +1 -0
- package/dist/orchestrator/scheduler.d.ts +112 -0
- package/dist/orchestrator/scheduler.d.ts.map +1 -0
- package/dist/orchestrator/scheduler.js +282 -0
- package/dist/orchestrator/scheduler.js.map +1 -0
- package/dist/task/queue.d.ts +160 -0
- package/dist/task/queue.d.ts.map +1 -0
- package/dist/task/queue.js +337 -0
- package/dist/task/queue.js.map +1 -0
- package/dist/task/task.d.ts +86 -0
- package/dist/task/task.d.ts.map +1 -0
- package/dist/task/task.js +201 -0
- package/dist/task/task.js.map +1 -0
- package/dist/team/messaging.d.ts +106 -0
- package/dist/team/messaging.d.ts.map +1 -0
- package/dist/team/messaging.js +182 -0
- package/dist/team/messaging.js.map +1 -0
- package/dist/team/team.d.ts +141 -0
- package/dist/team/team.d.ts.map +1 -0
- package/dist/team/team.js +282 -0
- package/dist/team/team.js.map +1 -0
- package/dist/tool/built-in/bash.d.ts +12 -0
- package/dist/tool/built-in/bash.d.ts.map +1 -0
- package/dist/tool/built-in/bash.js +133 -0
- package/dist/tool/built-in/bash.js.map +1 -0
- package/dist/tool/built-in/file-edit.d.ts +14 -0
- package/dist/tool/built-in/file-edit.d.ts.map +1 -0
- package/dist/tool/built-in/file-edit.js +130 -0
- package/dist/tool/built-in/file-edit.js.map +1 -0
- package/dist/tool/built-in/file-read.d.ts +12 -0
- package/dist/tool/built-in/file-read.d.ts.map +1 -0
- package/dist/tool/built-in/file-read.js +82 -0
- package/dist/tool/built-in/file-read.js.map +1 -0
- package/dist/tool/built-in/file-write.d.ts +11 -0
- package/dist/tool/built-in/file-write.d.ts.map +1 -0
- package/dist/tool/built-in/file-write.js +70 -0
- package/dist/tool/built-in/file-write.js.map +1 -0
- package/dist/tool/built-in/grep.d.ts +15 -0
- package/dist/tool/built-in/grep.d.ts.map +1 -0
- package/dist/tool/built-in/grep.js +287 -0
- package/dist/tool/built-in/grep.js.map +1 -0
- package/dist/tool/built-in/index.d.ts +36 -0
- package/dist/tool/built-in/index.d.ts.map +1 -0
- package/dist/tool/built-in/index.js +45 -0
- package/dist/tool/built-in/index.js.map +1 -0
- package/dist/tool/executor.d.ts +71 -0
- package/dist/tool/executor.d.ts.map +1 -0
- package/dist/tool/executor.js +116 -0
- package/dist/tool/executor.js.map +1 -0
- package/dist/tool/framework.d.ts +143 -0
- package/dist/tool/framework.d.ts.map +1 -0
- package/dist/tool/framework.js +371 -0
- package/dist/tool/framework.js.map +1 -0
- package/dist/types.d.ts +285 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +8 -0
- package/dist/types.js.map +1 -0
- package/dist/utils/semaphore.d.ts +47 -0
- package/dist/utils/semaphore.d.ts.map +1 -0
- package/dist/utils/semaphore.js +85 -0
- package/dist/utils/semaphore.js.map +1 -0
- package/examples/01-single-agent.ts +131 -0
- package/examples/02-team-collaboration.ts +167 -0
- package/examples/03-task-pipeline.ts +201 -0
- package/examples/04-multi-model-team.ts +261 -0
- package/package.json +49 -0
- package/src/agent/agent.ts +364 -0
- package/src/agent/pool.ts +278 -0
- package/src/agent/runner.ts +413 -0
- package/src/index.ts +166 -0
- package/src/llm/adapter.ts +74 -0
- package/src/llm/anthropic.ts +388 -0
- package/src/llm/openai.ts +522 -0
- package/src/memory/shared.ts +181 -0
- package/src/memory/store.ts +124 -0
- package/src/orchestrator/orchestrator.ts +851 -0
- package/src/orchestrator/scheduler.ts +352 -0
- package/src/task/queue.ts +394 -0
- package/src/task/task.ts +232 -0
- package/src/team/messaging.ts +230 -0
- package/src/team/team.ts +334 -0
- package/src/tool/built-in/bash.ts +187 -0
- package/src/tool/built-in/file-edit.ts +154 -0
- package/src/tool/built-in/file-read.ts +105 -0
- package/src/tool/built-in/file-write.ts +81 -0
- package/src/tool/built-in/grep.ts +362 -0
- package/src/tool/built-in/index.ts +50 -0
- package/src/tool/executor.ts +178 -0
- package/src/tool/framework.ts +557 -0
- package/src/types.ts +362 -0
- package/src/utils/semaphore.ts +89 -0
- package/tsconfig.json +25 -0
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Inter-agent message bus.
|
|
3
|
+
*
|
|
4
|
+
* Provides a lightweight pub/sub system so agents can exchange typed messages
|
|
5
|
+
* without direct references to each other. All messages are retained in memory
|
|
6
|
+
* for replay and audit; read-state is tracked per recipient.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
// ---------------------------------------------------------------------------
|
|
10
|
+
// Message type
|
|
11
|
+
// ---------------------------------------------------------------------------
|
|
12
|
+
|
|
13
|
+
/** A single message exchanged between agents (or broadcast to all). */
|
|
14
|
+
export interface Message {
|
|
15
|
+
/** Stable UUID for this message. */
|
|
16
|
+
readonly id: string
|
|
17
|
+
/** Name of the sending agent. */
|
|
18
|
+
readonly from: string
|
|
19
|
+
/**
|
|
20
|
+
* Recipient agent name, or `'*'` when the message is a broadcast intended
|
|
21
|
+
* for every agent except the sender.
|
|
22
|
+
*/
|
|
23
|
+
readonly to: string
|
|
24
|
+
readonly content: string
|
|
25
|
+
readonly timestamp: Date
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// ---------------------------------------------------------------------------
|
|
29
|
+
// Internal helpers
|
|
30
|
+
// ---------------------------------------------------------------------------
|
|
31
|
+
|
|
32
|
+
/** Returns true when `message` is addressed to `agentName`. */
|
|
33
|
+
function isAddressedTo(message: Message, agentName: string): boolean {
|
|
34
|
+
if (message.to === '*') {
|
|
35
|
+
// Broadcasts are delivered to everyone except the sender.
|
|
36
|
+
return message.from !== agentName
|
|
37
|
+
}
|
|
38
|
+
return message.to === agentName
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// ---------------------------------------------------------------------------
|
|
42
|
+
// MessageBus
|
|
43
|
+
// ---------------------------------------------------------------------------
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* In-memory message bus for inter-agent communication.
|
|
47
|
+
*
|
|
48
|
+
* Agents can send point-to-point messages or broadcasts. Subscribers are
|
|
49
|
+
* notified synchronously (within the same microtask) when a new message
|
|
50
|
+
* arrives addressed to them.
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```ts
|
|
54
|
+
* const bus = new MessageBus()
|
|
55
|
+
*
|
|
56
|
+
* const unsubscribe = bus.subscribe('worker', (msg) => {
|
|
57
|
+
* console.log(`worker received: ${msg.content}`)
|
|
58
|
+
* })
|
|
59
|
+
*
|
|
60
|
+
* bus.send('coordinator', 'worker', 'Start task A')
|
|
61
|
+
* bus.broadcast('coordinator', 'All agents: stand by')
|
|
62
|
+
*
|
|
63
|
+
* unsubscribe()
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
export class MessageBus {
|
|
67
|
+
/** All messages ever sent, in insertion order. */
|
|
68
|
+
private readonly messages: Message[] = []
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Per-agent set of message IDs that have already been marked as read.
|
|
72
|
+
* A message absent from this set is considered unread.
|
|
73
|
+
*/
|
|
74
|
+
private readonly readState = new Map<string, Set<string>>()
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Active subscribers keyed by agent name. Each subscriber is a callback
|
|
78
|
+
* paired with a unique subscription ID used for unsubscription.
|
|
79
|
+
*/
|
|
80
|
+
private readonly subscribers = new Map<
|
|
81
|
+
string,
|
|
82
|
+
Map<symbol, (message: Message) => void>
|
|
83
|
+
>()
|
|
84
|
+
|
|
85
|
+
// ---------------------------------------------------------------------------
|
|
86
|
+
// Write operations
|
|
87
|
+
// ---------------------------------------------------------------------------
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Send a message from `from` to `to`.
|
|
91
|
+
*
|
|
92
|
+
* @returns The persisted {@link Message} including its generated ID and timestamp.
|
|
93
|
+
*/
|
|
94
|
+
send(from: string, to: string, content: string): Message {
|
|
95
|
+
const message: Message = {
|
|
96
|
+
id: crypto.randomUUID(),
|
|
97
|
+
from,
|
|
98
|
+
to,
|
|
99
|
+
content,
|
|
100
|
+
timestamp: new Date(),
|
|
101
|
+
}
|
|
102
|
+
this.persist(message)
|
|
103
|
+
return message
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Broadcast a message from `from` to all other agents (`to === '*'`).
|
|
108
|
+
*
|
|
109
|
+
* @returns The persisted broadcast {@link Message}.
|
|
110
|
+
*/
|
|
111
|
+
broadcast(from: string, content: string): Message {
|
|
112
|
+
return this.send(from, '*', content)
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// ---------------------------------------------------------------------------
|
|
116
|
+
// Read operations
|
|
117
|
+
// ---------------------------------------------------------------------------
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Returns messages that have not yet been marked as read by `agentName`,
|
|
121
|
+
* including both direct messages and broadcasts addressed to them.
|
|
122
|
+
*/
|
|
123
|
+
getUnread(agentName: string): Message[] {
|
|
124
|
+
const read = this.readState.get(agentName) ?? new Set<string>()
|
|
125
|
+
return this.messages.filter(
|
|
126
|
+
(m) => isAddressedTo(m, agentName) && !read.has(m.id),
|
|
127
|
+
)
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Returns every message (read or unread) addressed to `agentName`,
|
|
132
|
+
* preserving insertion order.
|
|
133
|
+
*/
|
|
134
|
+
getAll(agentName: string): Message[] {
|
|
135
|
+
return this.messages.filter((m) => isAddressedTo(m, agentName))
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Mark a set of messages as read for `agentName`.
|
|
140
|
+
* Passing IDs that were already marked, or do not exist, is a no-op.
|
|
141
|
+
*/
|
|
142
|
+
markRead(agentName: string, messageIds: string[]): void {
|
|
143
|
+
if (messageIds.length === 0) return
|
|
144
|
+
let read = this.readState.get(agentName)
|
|
145
|
+
if (!read) {
|
|
146
|
+
read = new Set<string>()
|
|
147
|
+
this.readState.set(agentName, read)
|
|
148
|
+
}
|
|
149
|
+
for (const id of messageIds) {
|
|
150
|
+
read.add(id)
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Returns all messages exchanged between `agent1` and `agent2` (in either
|
|
156
|
+
* direction), sorted chronologically.
|
|
157
|
+
*/
|
|
158
|
+
getConversation(agent1: string, agent2: string): Message[] {
|
|
159
|
+
return this.messages.filter(
|
|
160
|
+
(m) =>
|
|
161
|
+
(m.from === agent1 && m.to === agent2) ||
|
|
162
|
+
(m.from === agent2 && m.to === agent1),
|
|
163
|
+
)
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// ---------------------------------------------------------------------------
|
|
167
|
+
// Subscriptions
|
|
168
|
+
// ---------------------------------------------------------------------------
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Subscribe to new messages addressed to `agentName`.
|
|
172
|
+
*
|
|
173
|
+
* The `callback` is invoked synchronously after each matching message is
|
|
174
|
+
* persisted. Returns an unsubscribe function; calling it is idempotent.
|
|
175
|
+
*
|
|
176
|
+
* @example
|
|
177
|
+
* ```ts
|
|
178
|
+
* const off = bus.subscribe('agent-b', (msg) => handleMessage(msg))
|
|
179
|
+
* // Later…
|
|
180
|
+
* off()
|
|
181
|
+
* ```
|
|
182
|
+
*/
|
|
183
|
+
subscribe(
|
|
184
|
+
agentName: string,
|
|
185
|
+
callback: (message: Message) => void,
|
|
186
|
+
): () => void {
|
|
187
|
+
let agentSubs = this.subscribers.get(agentName)
|
|
188
|
+
if (!agentSubs) {
|
|
189
|
+
agentSubs = new Map()
|
|
190
|
+
this.subscribers.set(agentName, agentSubs)
|
|
191
|
+
}
|
|
192
|
+
const id = Symbol()
|
|
193
|
+
agentSubs.set(id, callback)
|
|
194
|
+
return () => {
|
|
195
|
+
agentSubs!.delete(id)
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// ---------------------------------------------------------------------------
|
|
200
|
+
// Private helpers
|
|
201
|
+
// ---------------------------------------------------------------------------
|
|
202
|
+
|
|
203
|
+
private persist(message: Message): void {
|
|
204
|
+
this.messages.push(message)
|
|
205
|
+
this.notifySubscribers(message)
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
private notifySubscribers(message: Message): void {
|
|
209
|
+
// Notify direct subscribers of `message.to` (unless broadcast).
|
|
210
|
+
if (message.to !== '*') {
|
|
211
|
+
this.fireCallbacks(message.to, message)
|
|
212
|
+
return
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// Broadcast: notify all subscribers except the sender.
|
|
216
|
+
for (const [agentName, subs] of this.subscribers) {
|
|
217
|
+
if (agentName !== message.from && subs.size > 0) {
|
|
218
|
+
this.fireCallbacks(agentName, message)
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
private fireCallbacks(agentName: string, message: Message): void {
|
|
224
|
+
const subs = this.subscribers.get(agentName)
|
|
225
|
+
if (!subs) return
|
|
226
|
+
for (const callback of subs.values()) {
|
|
227
|
+
callback(message)
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
}
|
package/src/team/team.ts
ADDED
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Team — the central coordination object for a named group of agents.
|
|
3
|
+
*
|
|
4
|
+
* A {@link Team} owns the agent roster, the inter-agent {@link MessageBus},
|
|
5
|
+
* the {@link TaskQueue}, and (optionally) a {@link SharedMemory} instance.
|
|
6
|
+
* It also exposes a typed event bus so orchestrators can react to lifecycle
|
|
7
|
+
* events without polling.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type {
|
|
11
|
+
AgentConfig,
|
|
12
|
+
MemoryStore,
|
|
13
|
+
OrchestratorEvent,
|
|
14
|
+
Task,
|
|
15
|
+
TaskStatus,
|
|
16
|
+
TeamConfig,
|
|
17
|
+
} from '../types.js'
|
|
18
|
+
import { SharedMemory } from '../memory/shared.js'
|
|
19
|
+
import { MessageBus } from './messaging.js'
|
|
20
|
+
import type { Message } from './messaging.js'
|
|
21
|
+
import { TaskQueue } from '../task/queue.js'
|
|
22
|
+
import { createTask } from '../task/task.js'
|
|
23
|
+
|
|
24
|
+
export type { Message }
|
|
25
|
+
|
|
26
|
+
// ---------------------------------------------------------------------------
|
|
27
|
+
// Internal event bus
|
|
28
|
+
// ---------------------------------------------------------------------------
|
|
29
|
+
|
|
30
|
+
type EventHandler = (data: unknown) => void
|
|
31
|
+
|
|
32
|
+
/** Minimal synchronous event emitter. */
|
|
33
|
+
class EventBus {
|
|
34
|
+
private readonly listeners = new Map<string, Map<symbol, EventHandler>>()
|
|
35
|
+
|
|
36
|
+
on(event: string, handler: EventHandler): () => void {
|
|
37
|
+
let map = this.listeners.get(event)
|
|
38
|
+
if (!map) {
|
|
39
|
+
map = new Map()
|
|
40
|
+
this.listeners.set(event, map)
|
|
41
|
+
}
|
|
42
|
+
const id = Symbol()
|
|
43
|
+
map.set(id, handler)
|
|
44
|
+
return () => {
|
|
45
|
+
map!.delete(id)
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
emit(event: string, data: unknown): void {
|
|
50
|
+
const map = this.listeners.get(event)
|
|
51
|
+
if (!map) return
|
|
52
|
+
for (const handler of map.values()) {
|
|
53
|
+
handler(data)
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// ---------------------------------------------------------------------------
|
|
59
|
+
// Team
|
|
60
|
+
// ---------------------------------------------------------------------------
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Coordinates a named group of agents with shared messaging, task queuing,
|
|
64
|
+
* and optional shared memory.
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```ts
|
|
68
|
+
* const team = new Team({
|
|
69
|
+
* name: 'research-team',
|
|
70
|
+
* agents: [researcherConfig, writerConfig],
|
|
71
|
+
* sharedMemory: true,
|
|
72
|
+
* maxConcurrency: 2,
|
|
73
|
+
* })
|
|
74
|
+
*
|
|
75
|
+
* team.on('task:complete', (data) => {
|
|
76
|
+
* const event = data as OrchestratorEvent
|
|
77
|
+
* console.log(`Task done: ${event.task}`)
|
|
78
|
+
* })
|
|
79
|
+
*
|
|
80
|
+
* const task = team.addTask({
|
|
81
|
+
* title: 'Research topic',
|
|
82
|
+
* description: 'Gather background on quantum computing',
|
|
83
|
+
* status: 'pending',
|
|
84
|
+
* assignee: 'researcher',
|
|
85
|
+
* })
|
|
86
|
+
* ```
|
|
87
|
+
*/
|
|
88
|
+
export class Team {
|
|
89
|
+
readonly name: string
|
|
90
|
+
readonly config: TeamConfig
|
|
91
|
+
|
|
92
|
+
private readonly agentMap: ReadonlyMap<string, AgentConfig>
|
|
93
|
+
private readonly bus: MessageBus
|
|
94
|
+
private readonly queue: TaskQueue
|
|
95
|
+
private readonly memory: SharedMemory | undefined
|
|
96
|
+
private readonly events: EventBus
|
|
97
|
+
|
|
98
|
+
constructor(config: TeamConfig) {
|
|
99
|
+
this.config = config
|
|
100
|
+
this.name = config.name
|
|
101
|
+
|
|
102
|
+
// Index agents by name for O(1) lookup.
|
|
103
|
+
this.agentMap = new Map(config.agents.map((a) => [a.name, a]))
|
|
104
|
+
this.bus = new MessageBus()
|
|
105
|
+
this.queue = new TaskQueue()
|
|
106
|
+
this.memory = config.sharedMemory ? new SharedMemory() : undefined
|
|
107
|
+
this.events = new EventBus()
|
|
108
|
+
|
|
109
|
+
// Bridge queue events onto the team's event bus.
|
|
110
|
+
this.queue.on('task:ready', (task) => {
|
|
111
|
+
const event: OrchestratorEvent = {
|
|
112
|
+
type: 'task_start',
|
|
113
|
+
task: task.id,
|
|
114
|
+
data: task,
|
|
115
|
+
}
|
|
116
|
+
this.events.emit('task:ready', event)
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
this.queue.on('task:complete', (task) => {
|
|
120
|
+
const event: OrchestratorEvent = {
|
|
121
|
+
type: 'task_complete',
|
|
122
|
+
task: task.id,
|
|
123
|
+
data: task,
|
|
124
|
+
}
|
|
125
|
+
this.events.emit('task:complete', event)
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
this.queue.on('task:failed', (task) => {
|
|
129
|
+
const event: OrchestratorEvent = {
|
|
130
|
+
type: 'error',
|
|
131
|
+
task: task.id,
|
|
132
|
+
data: task,
|
|
133
|
+
}
|
|
134
|
+
this.events.emit('task:failed', event)
|
|
135
|
+
})
|
|
136
|
+
|
|
137
|
+
this.queue.on('all:complete', () => {
|
|
138
|
+
this.events.emit('all:complete', undefined)
|
|
139
|
+
})
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// ---------------------------------------------------------------------------
|
|
143
|
+
// Agent roster
|
|
144
|
+
// ---------------------------------------------------------------------------
|
|
145
|
+
|
|
146
|
+
/** Returns a shallow copy of the agent configs in registration order. */
|
|
147
|
+
getAgents(): AgentConfig[] {
|
|
148
|
+
return Array.from(this.agentMap.values())
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Looks up an agent by name.
|
|
153
|
+
*
|
|
154
|
+
* @returns The {@link AgentConfig} or `undefined` when the name is not known.
|
|
155
|
+
*/
|
|
156
|
+
getAgent(name: string): AgentConfig | undefined {
|
|
157
|
+
return this.agentMap.get(name)
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// ---------------------------------------------------------------------------
|
|
161
|
+
// Messaging
|
|
162
|
+
// ---------------------------------------------------------------------------
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Sends a point-to-point message from `from` to `to`.
|
|
166
|
+
*
|
|
167
|
+
* The message is persisted on the bus and any active subscribers for `to`
|
|
168
|
+
* are notified synchronously.
|
|
169
|
+
*/
|
|
170
|
+
sendMessage(from: string, to: string, content: string): void {
|
|
171
|
+
const message = this.bus.send(from, to, content)
|
|
172
|
+
const event: OrchestratorEvent = {
|
|
173
|
+
type: 'message',
|
|
174
|
+
agent: from,
|
|
175
|
+
data: message,
|
|
176
|
+
}
|
|
177
|
+
this.events.emit('message', event)
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Returns all messages (read or unread) addressed to `agentName`, in
|
|
182
|
+
* chronological order.
|
|
183
|
+
*/
|
|
184
|
+
getMessages(agentName: string): Message[] {
|
|
185
|
+
return this.bus.getAll(agentName)
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Broadcasts `content` from `from` to every other agent.
|
|
190
|
+
*
|
|
191
|
+
* The `to` field of the resulting message is `'*'`.
|
|
192
|
+
*/
|
|
193
|
+
broadcast(from: string, content: string): void {
|
|
194
|
+
const message = this.bus.broadcast(from, content)
|
|
195
|
+
const event: OrchestratorEvent = {
|
|
196
|
+
type: 'message',
|
|
197
|
+
agent: from,
|
|
198
|
+
data: message,
|
|
199
|
+
}
|
|
200
|
+
this.events.emit('broadcast', event)
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// ---------------------------------------------------------------------------
|
|
204
|
+
// Task management
|
|
205
|
+
// ---------------------------------------------------------------------------
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Creates a new task, adds it to the queue, and returns the persisted
|
|
209
|
+
* {@link Task} (with generated `id`, `createdAt`, and `updatedAt`).
|
|
210
|
+
*
|
|
211
|
+
* @param task - Everything except the generated fields.
|
|
212
|
+
*/
|
|
213
|
+
addTask(
|
|
214
|
+
task: Omit<Task, 'id' | 'createdAt' | 'updatedAt'>,
|
|
215
|
+
): Task {
|
|
216
|
+
const created = createTask({
|
|
217
|
+
title: task.title,
|
|
218
|
+
description: task.description,
|
|
219
|
+
assignee: task.assignee,
|
|
220
|
+
dependsOn: task.dependsOn ? [...task.dependsOn] : undefined,
|
|
221
|
+
})
|
|
222
|
+
|
|
223
|
+
// Preserve any non-default status (e.g. 'blocked') supplied by the caller.
|
|
224
|
+
const finalTask: Task =
|
|
225
|
+
task.status !== 'pending'
|
|
226
|
+
? { ...created, status: task.status as TaskStatus, result: task.result }
|
|
227
|
+
: created
|
|
228
|
+
|
|
229
|
+
this.queue.add(finalTask)
|
|
230
|
+
return finalTask
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/** Returns a snapshot of all tasks in the queue (any status). */
|
|
234
|
+
getTasks(): Task[] {
|
|
235
|
+
return this.queue.list()
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/** Returns all tasks whose `assignee` is `agentName`. */
|
|
239
|
+
getTasksByAssignee(agentName: string): Task[] {
|
|
240
|
+
return this.queue.list().filter((t) => t.assignee === agentName)
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Applies a partial update to the task identified by `taskId`.
|
|
245
|
+
*
|
|
246
|
+
* @throws {Error} when the task is not found.
|
|
247
|
+
*/
|
|
248
|
+
updateTask(taskId: string, update: Partial<Task>): Task {
|
|
249
|
+
// Extract only mutable fields accepted by the queue.
|
|
250
|
+
const { status, result, assignee } = update
|
|
251
|
+
return this.queue.update(taskId, {
|
|
252
|
+
...(status !== undefined && { status }),
|
|
253
|
+
...(result !== undefined && { result }),
|
|
254
|
+
...(assignee !== undefined && { assignee }),
|
|
255
|
+
})
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Returns the next `'pending'` task for `agentName`, respecting dependencies.
|
|
260
|
+
*
|
|
261
|
+
* Tries to find a task explicitly assigned to the agent first; falls back to
|
|
262
|
+
* the first unassigned pending task.
|
|
263
|
+
*
|
|
264
|
+
* @returns `undefined` when no ready task exists for this agent.
|
|
265
|
+
*/
|
|
266
|
+
getNextTask(agentName: string): Task | undefined {
|
|
267
|
+
// Prefer a task explicitly assigned to this agent.
|
|
268
|
+
const assigned = this.queue.next(agentName)
|
|
269
|
+
if (assigned) return assigned
|
|
270
|
+
|
|
271
|
+
// Fall back to any unassigned pending task.
|
|
272
|
+
return this.queue.nextAvailable()
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
// ---------------------------------------------------------------------------
|
|
276
|
+
// Memory
|
|
277
|
+
// ---------------------------------------------------------------------------
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* Returns the shared {@link MemoryStore} for this team, or `undefined` if
|
|
281
|
+
* `sharedMemory` was not enabled in {@link TeamConfig}.
|
|
282
|
+
*
|
|
283
|
+
* Note: the returned value satisfies the {@link MemoryStore} interface.
|
|
284
|
+
* Callers that need the full {@link SharedMemory} API can use the
|
|
285
|
+
* `as SharedMemory` cast, but depending on the concrete type is discouraged.
|
|
286
|
+
*/
|
|
287
|
+
getSharedMemory(): MemoryStore | undefined {
|
|
288
|
+
return this.memory?.getStore()
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* Returns the raw {@link SharedMemory} instance (team-internal accessor).
|
|
293
|
+
* Use this when you need the namespacing / `getSummary` features.
|
|
294
|
+
*
|
|
295
|
+
* @internal
|
|
296
|
+
*/
|
|
297
|
+
getSharedMemoryInstance(): SharedMemory | undefined {
|
|
298
|
+
return this.memory
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
// ---------------------------------------------------------------------------
|
|
302
|
+
// Events
|
|
303
|
+
// ---------------------------------------------------------------------------
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* Subscribes to a team event.
|
|
307
|
+
*
|
|
308
|
+
* Built-in events:
|
|
309
|
+
* - `'task:ready'` — emitted when a task becomes runnable.
|
|
310
|
+
* - `'task:complete'` — emitted when a task completes successfully.
|
|
311
|
+
* - `'task:failed'` — emitted when a task fails.
|
|
312
|
+
* - `'all:complete'` — emitted when every task in the queue has terminated.
|
|
313
|
+
* - `'message'` — emitted on point-to-point messages.
|
|
314
|
+
* - `'broadcast'` — emitted on broadcast messages.
|
|
315
|
+
*
|
|
316
|
+
* `data` is typed as `unknown`; cast to {@link OrchestratorEvent} for
|
|
317
|
+
* structured access.
|
|
318
|
+
*
|
|
319
|
+
* @returns An unsubscribe function.
|
|
320
|
+
*/
|
|
321
|
+
on(event: string, handler: (data: unknown) => void): () => void {
|
|
322
|
+
return this.events.on(event, handler)
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* Emits a custom event on the team's event bus.
|
|
327
|
+
*
|
|
328
|
+
* Orchestrators can use this to signal domain-specific lifecycle milestones
|
|
329
|
+
* (e.g. `'phase:research:complete'`) without modifying the Team class.
|
|
330
|
+
*/
|
|
331
|
+
emit(event: string, data: unknown): void {
|
|
332
|
+
this.events.emit(event, data)
|
|
333
|
+
}
|
|
334
|
+
}
|