@jackchen_me/open-multi-agent 0.2.0 → 1.0.1

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.
Files changed (117) hide show
  1. package/README.md +87 -20
  2. package/dist/agent/agent.d.ts +15 -1
  3. package/dist/agent/agent.d.ts.map +1 -1
  4. package/dist/agent/agent.js +144 -10
  5. package/dist/agent/agent.js.map +1 -1
  6. package/dist/agent/loop-detector.d.ts +39 -0
  7. package/dist/agent/loop-detector.d.ts.map +1 -0
  8. package/dist/agent/loop-detector.js +122 -0
  9. package/dist/agent/loop-detector.js.map +1 -0
  10. package/dist/agent/pool.d.ts +2 -1
  11. package/dist/agent/pool.d.ts.map +1 -1
  12. package/dist/agent/pool.js +4 -2
  13. package/dist/agent/pool.js.map +1 -1
  14. package/dist/agent/runner.d.ts +23 -1
  15. package/dist/agent/runner.d.ts.map +1 -1
  16. package/dist/agent/runner.js +113 -12
  17. package/dist/agent/runner.js.map +1 -1
  18. package/dist/index.d.ts +3 -1
  19. package/dist/index.d.ts.map +1 -1
  20. package/dist/index.js +2 -0
  21. package/dist/index.js.map +1 -1
  22. package/dist/llm/adapter.d.ts +4 -1
  23. package/dist/llm/adapter.d.ts.map +1 -1
  24. package/dist/llm/adapter.js +11 -0
  25. package/dist/llm/adapter.js.map +1 -1
  26. package/dist/llm/copilot.d.ts.map +1 -1
  27. package/dist/llm/copilot.js +2 -1
  28. package/dist/llm/copilot.js.map +1 -1
  29. package/dist/llm/gemini.d.ts +65 -0
  30. package/dist/llm/gemini.d.ts.map +1 -0
  31. package/dist/llm/gemini.js +317 -0
  32. package/dist/llm/gemini.js.map +1 -0
  33. package/dist/llm/grok.d.ts +21 -0
  34. package/dist/llm/grok.d.ts.map +1 -0
  35. package/dist/llm/grok.js +24 -0
  36. package/dist/llm/grok.js.map +1 -0
  37. package/dist/llm/openai-common.d.ts +8 -1
  38. package/dist/llm/openai-common.d.ts.map +1 -1
  39. package/dist/llm/openai-common.js +35 -2
  40. package/dist/llm/openai-common.js.map +1 -1
  41. package/dist/llm/openai.d.ts +1 -1
  42. package/dist/llm/openai.d.ts.map +1 -1
  43. package/dist/llm/openai.js +20 -2
  44. package/dist/llm/openai.js.map +1 -1
  45. package/dist/orchestrator/orchestrator.d.ts.map +1 -1
  46. package/dist/orchestrator/orchestrator.js +89 -9
  47. package/dist/orchestrator/orchestrator.js.map +1 -1
  48. package/dist/task/queue.d.ts +31 -2
  49. package/dist/task/queue.d.ts.map +1 -1
  50. package/dist/task/queue.js +69 -2
  51. package/dist/task/queue.js.map +1 -1
  52. package/dist/tool/text-tool-extractor.d.ts +32 -0
  53. package/dist/tool/text-tool-extractor.d.ts.map +1 -0
  54. package/dist/tool/text-tool-extractor.js +187 -0
  55. package/dist/tool/text-tool-extractor.js.map +1 -0
  56. package/dist/types.d.ts +139 -7
  57. package/dist/types.d.ts.map +1 -1
  58. package/dist/utils/trace.d.ts +12 -0
  59. package/dist/utils/trace.d.ts.map +1 -0
  60. package/dist/utils/trace.js +30 -0
  61. package/dist/utils/trace.js.map +1 -0
  62. package/package.json +18 -2
  63. package/.github/ISSUE_TEMPLATE/bug_report.md +0 -40
  64. package/.github/ISSUE_TEMPLATE/feature_request.md +0 -23
  65. package/.github/pull_request_template.md +0 -14
  66. package/.github/workflows/ci.yml +0 -23
  67. package/CLAUDE.md +0 -72
  68. package/CODE_OF_CONDUCT.md +0 -48
  69. package/CONTRIBUTING.md +0 -72
  70. package/DECISIONS.md +0 -43
  71. package/README_zh.md +0 -217
  72. package/SECURITY.md +0 -17
  73. package/examples/01-single-agent.ts +0 -131
  74. package/examples/02-team-collaboration.ts +0 -167
  75. package/examples/03-task-pipeline.ts +0 -201
  76. package/examples/04-multi-model-team.ts +0 -261
  77. package/examples/05-copilot-test.ts +0 -49
  78. package/examples/06-local-model.ts +0 -199
  79. package/examples/07-fan-out-aggregate.ts +0 -209
  80. package/examples/08-gemma4-local.ts +0 -203
  81. package/examples/09-gemma4-auto-orchestration.ts +0 -162
  82. package/src/agent/agent.ts +0 -473
  83. package/src/agent/pool.ts +0 -278
  84. package/src/agent/runner.ts +0 -413
  85. package/src/agent/structured-output.ts +0 -126
  86. package/src/index.ts +0 -167
  87. package/src/llm/adapter.ts +0 -87
  88. package/src/llm/anthropic.ts +0 -389
  89. package/src/llm/copilot.ts +0 -551
  90. package/src/llm/openai-common.ts +0 -255
  91. package/src/llm/openai.ts +0 -272
  92. package/src/memory/shared.ts +0 -181
  93. package/src/memory/store.ts +0 -124
  94. package/src/orchestrator/orchestrator.ts +0 -977
  95. package/src/orchestrator/scheduler.ts +0 -352
  96. package/src/task/queue.ts +0 -394
  97. package/src/task/task.ts +0 -239
  98. package/src/team/messaging.ts +0 -232
  99. package/src/team/team.ts +0 -334
  100. package/src/tool/built-in/bash.ts +0 -187
  101. package/src/tool/built-in/file-edit.ts +0 -154
  102. package/src/tool/built-in/file-read.ts +0 -105
  103. package/src/tool/built-in/file-write.ts +0 -81
  104. package/src/tool/built-in/grep.ts +0 -362
  105. package/src/tool/built-in/index.ts +0 -50
  106. package/src/tool/executor.ts +0 -178
  107. package/src/tool/framework.ts +0 -557
  108. package/src/types.ts +0 -391
  109. package/src/utils/semaphore.ts +0 -89
  110. package/tests/semaphore.test.ts +0 -57
  111. package/tests/shared-memory.test.ts +0 -122
  112. package/tests/structured-output.test.ts +0 -331
  113. package/tests/task-queue.test.ts +0 -244
  114. package/tests/task-retry.test.ts +0 -368
  115. package/tests/task-utils.test.ts +0 -155
  116. package/tests/tool-executor.test.ts +0 -193
  117. package/tsconfig.json +0 -25
@@ -1,977 +0,0 @@
1
- /**
2
- * @fileoverview OpenMultiAgent — the top-level multi-agent orchestration class.
3
- *
4
- * {@link OpenMultiAgent} is the primary public API of the open-multi-agent framework.
5
- * It ties together every subsystem:
6
- *
7
- * - {@link Team} — Agent roster, shared memory, inter-agent messaging
8
- * - {@link TaskQueue} — Dependency-aware work queue
9
- * - {@link Scheduler} — Task-to-agent assignment strategies
10
- * - {@link AgentPool} — Concurrency-controlled execution pool
11
- * - {@link Agent} — Conversation + tool-execution loop
12
- *
13
- * ## Quick start
14
- *
15
- * ```ts
16
- * const orchestrator = new OpenMultiAgent({ defaultModel: 'claude-opus-4-6' })
17
- *
18
- * const team = orchestrator.createTeam('research', {
19
- * name: 'research',
20
- * agents: [
21
- * { name: 'researcher', model: 'claude-opus-4-6', systemPrompt: 'You are a researcher.' },
22
- * { name: 'writer', model: 'claude-opus-4-6', systemPrompt: 'You are a technical writer.' },
23
- * ],
24
- * sharedMemory: true,
25
- * })
26
- *
27
- * const result = await orchestrator.runTeam(team, 'Produce a report on TypeScript 5.5.')
28
- * console.log(result.agentResults.get('coordinator')?.output)
29
- * ```
30
- *
31
- * ## Key design decisions
32
- *
33
- * - **Coordinator pattern** — `runTeam()` spins up a temporary "coordinator" agent
34
- * that breaks the high-level goal into tasks, assigns them, and synthesises the
35
- * final answer. This is the framework's killer feature.
36
- * - **Parallel-by-default** — Independent tasks (no shared dependency) run in
37
- * parallel up to `maxConcurrency`.
38
- * - **Graceful failure** — A failed task marks itself `'failed'` and its direct
39
- * dependents remain `'blocked'` indefinitely; all non-dependent tasks continue.
40
- * - **Progress callbacks** — Callers can pass `onProgress` in the config to receive
41
- * structured {@link OrchestratorEvent}s without polling.
42
- */
43
-
44
- import type {
45
- AgentConfig,
46
- AgentRunResult,
47
- OrchestratorConfig,
48
- OrchestratorEvent,
49
- Task,
50
- TaskStatus,
51
- TeamConfig,
52
- TeamRunResult,
53
- TokenUsage,
54
- } from '../types.js'
55
- import { Agent } from '../agent/agent.js'
56
- import { AgentPool } from '../agent/pool.js'
57
- import { ToolRegistry } from '../tool/framework.js'
58
- import { ToolExecutor } from '../tool/executor.js'
59
- import { registerBuiltInTools } from '../tool/built-in/index.js'
60
- import { Team } from '../team/team.js'
61
- import { TaskQueue } from '../task/queue.js'
62
- import { createTask } from '../task/task.js'
63
- import { Scheduler } from './scheduler.js'
64
-
65
- // ---------------------------------------------------------------------------
66
- // Internal constants
67
- // ---------------------------------------------------------------------------
68
-
69
- const ZERO_USAGE: TokenUsage = { input_tokens: 0, output_tokens: 0 }
70
- const DEFAULT_MAX_CONCURRENCY = 5
71
- const DEFAULT_MODEL = 'claude-opus-4-6'
72
-
73
- // ---------------------------------------------------------------------------
74
- // Internal helpers
75
- // ---------------------------------------------------------------------------
76
-
77
- function addUsage(a: TokenUsage, b: TokenUsage): TokenUsage {
78
- return {
79
- input_tokens: a.input_tokens + b.input_tokens,
80
- output_tokens: a.output_tokens + b.output_tokens,
81
- }
82
- }
83
-
84
- /**
85
- * Build a minimal {@link Agent} with its own fresh registry/executor.
86
- * Registers all built-in tools so coordinator/worker agents can use them.
87
- */
88
- function buildAgent(config: AgentConfig): Agent {
89
- const registry = new ToolRegistry()
90
- registerBuiltInTools(registry)
91
- const executor = new ToolExecutor(registry)
92
- return new Agent(config, registry, executor)
93
- }
94
-
95
- /** Promise-based delay. */
96
- function sleep(ms: number): Promise<void> {
97
- return new Promise((resolve) => setTimeout(resolve, ms))
98
- }
99
-
100
- /** Maximum delay cap to prevent runaway exponential backoff (30 seconds). */
101
- const MAX_RETRY_DELAY_MS = 30_000
102
-
103
- /**
104
- * Compute the retry delay for a given attempt, capped at {@link MAX_RETRY_DELAY_MS}.
105
- */
106
- export function computeRetryDelay(
107
- baseDelay: number,
108
- backoff: number,
109
- attempt: number,
110
- ): number {
111
- return Math.min(baseDelay * backoff ** (attempt - 1), MAX_RETRY_DELAY_MS)
112
- }
113
-
114
- /**
115
- * Execute an agent task with optional retry and exponential backoff.
116
- *
117
- * Exported for testability — called internally by {@link executeQueue}.
118
- *
119
- * @param run - The function that executes the task (typically `pool.run`).
120
- * @param task - The task to execute (retry config read from its fields).
121
- * @param onRetry - Called before each retry sleep with event data.
122
- * @param delayFn - Injectable delay function (defaults to real `sleep`).
123
- * @returns The final {@link AgentRunResult} from the last attempt.
124
- */
125
- export async function executeWithRetry(
126
- run: () => Promise<AgentRunResult>,
127
- task: Task,
128
- onRetry?: (data: { attempt: number; maxAttempts: number; error: string; nextDelayMs: number }) => void,
129
- delayFn: (ms: number) => Promise<void> = sleep,
130
- ): Promise<AgentRunResult> {
131
- const maxAttempts = Math.max(0, task.maxRetries ?? 0) + 1
132
- const baseDelay = Math.max(0, task.retryDelayMs ?? 1000)
133
- const backoff = Math.max(1, task.retryBackoff ?? 2)
134
-
135
- let lastError: string = ''
136
- // Accumulate token usage across all attempts so billing/observability
137
- // reflects the true cost of retries.
138
- let totalUsage: TokenUsage = { input_tokens: 0, output_tokens: 0 }
139
-
140
- for (let attempt = 1; attempt <= maxAttempts; attempt++) {
141
- try {
142
- const result = await run()
143
- totalUsage = {
144
- input_tokens: totalUsage.input_tokens + result.tokenUsage.input_tokens,
145
- output_tokens: totalUsage.output_tokens + result.tokenUsage.output_tokens,
146
- }
147
-
148
- if (result.success) {
149
- return { ...result, tokenUsage: totalUsage }
150
- }
151
- lastError = result.output
152
-
153
- // Failure — retry or give up
154
- if (attempt < maxAttempts) {
155
- const delay = computeRetryDelay(baseDelay, backoff, attempt)
156
- onRetry?.({ attempt, maxAttempts, error: lastError, nextDelayMs: delay })
157
- await delayFn(delay)
158
- continue
159
- }
160
-
161
- return { ...result, tokenUsage: totalUsage }
162
- } catch (err) {
163
- lastError = err instanceof Error ? err.message : String(err)
164
-
165
- if (attempt < maxAttempts) {
166
- const delay = computeRetryDelay(baseDelay, backoff, attempt)
167
- onRetry?.({ attempt, maxAttempts, error: lastError, nextDelayMs: delay })
168
- await delayFn(delay)
169
- continue
170
- }
171
-
172
- // All retries exhausted — return a failure result
173
- return {
174
- success: false,
175
- output: lastError,
176
- messages: [],
177
- tokenUsage: totalUsage,
178
- toolCalls: [],
179
- }
180
- }
181
- }
182
-
183
- // Should not be reached, but TypeScript needs a return
184
- return {
185
- success: false,
186
- output: lastError,
187
- messages: [],
188
- tokenUsage: totalUsage,
189
- toolCalls: [],
190
- }
191
- }
192
-
193
- // ---------------------------------------------------------------------------
194
- // Parsed task spec (result of coordinator decomposition)
195
- // ---------------------------------------------------------------------------
196
-
197
- interface ParsedTaskSpec {
198
- title: string
199
- description: string
200
- assignee?: string
201
- dependsOn?: string[]
202
- }
203
-
204
- /**
205
- * Attempt to extract a JSON array of task specs from the coordinator's raw
206
- * output. The coordinator is prompted to emit JSON inside a ```json … ``` fence
207
- * or as a bare array. Returns `null` when no valid array can be extracted.
208
- */
209
- function parseTaskSpecs(raw: string): ParsedTaskSpec[] | null {
210
- // Strategy 1: look for a fenced JSON block
211
- const fenceMatch = raw.match(/```json\s*([\s\S]*?)```/)
212
- const candidate = fenceMatch ? fenceMatch[1]! : raw
213
-
214
- // Strategy 2: find the first '[' and last ']'
215
- const arrayStart = candidate.indexOf('[')
216
- const arrayEnd = candidate.lastIndexOf(']')
217
- if (arrayStart === -1 || arrayEnd === -1 || arrayEnd <= arrayStart) {
218
- return null
219
- }
220
-
221
- const jsonSlice = candidate.slice(arrayStart, arrayEnd + 1)
222
- try {
223
- const parsed: unknown = JSON.parse(jsonSlice)
224
- if (!Array.isArray(parsed)) return null
225
-
226
- const specs: ParsedTaskSpec[] = []
227
- for (const item of parsed) {
228
- if (typeof item !== 'object' || item === null) continue
229
- const obj = item as Record<string, unknown>
230
- if (typeof obj['title'] !== 'string') continue
231
- if (typeof obj['description'] !== 'string') continue
232
-
233
- specs.push({
234
- title: obj['title'],
235
- description: obj['description'],
236
- assignee: typeof obj['assignee'] === 'string' ? obj['assignee'] : undefined,
237
- dependsOn: Array.isArray(obj['dependsOn'])
238
- ? (obj['dependsOn'] as unknown[]).filter((x): x is string => typeof x === 'string')
239
- : undefined,
240
- })
241
- }
242
-
243
- return specs.length > 0 ? specs : null
244
- } catch {
245
- return null
246
- }
247
- }
248
-
249
- // ---------------------------------------------------------------------------
250
- // Orchestration loop
251
- // ---------------------------------------------------------------------------
252
-
253
- /**
254
- * Internal execution context assembled once per `runTeam` / `runTasks` call.
255
- */
256
- interface RunContext {
257
- readonly team: Team
258
- readonly pool: AgentPool
259
- readonly scheduler: Scheduler
260
- readonly agentResults: Map<string, AgentRunResult>
261
- readonly config: OrchestratorConfig
262
- }
263
-
264
- /**
265
- * Execute all tasks in `queue` using agents in `pool`, respecting dependencies
266
- * and running independent tasks in parallel.
267
- *
268
- * The orchestration loop works in rounds:
269
- * 1. Find all `'pending'` tasks (dependencies satisfied).
270
- * 2. Dispatch them in parallel via the pool.
271
- * 3. On completion, the queue automatically unblocks dependents.
272
- * 4. Repeat until no more pending tasks exist or all remaining tasks are
273
- * `'failed'`/`'blocked'` (stuck).
274
- */
275
- async function executeQueue(
276
- queue: TaskQueue,
277
- ctx: RunContext,
278
- ): Promise<void> {
279
- const { team, pool, scheduler, config } = ctx
280
-
281
- while (true) {
282
- // Re-run auto-assignment each iteration so tasks that were unblocked since
283
- // the last round (and thus have no assignee yet) get assigned before dispatch.
284
- scheduler.autoAssign(queue, team.getAgents())
285
-
286
- const pending = queue.getByStatus('pending')
287
- if (pending.length === 0) {
288
- // Either all done, or everything remaining is blocked/failed.
289
- break
290
- }
291
-
292
- // Dispatch all currently-pending tasks as a parallel batch.
293
- const dispatchPromises = pending.map(async (task): Promise<void> => {
294
- // Mark in-progress
295
- queue.update(task.id, { status: 'in_progress' as TaskStatus })
296
-
297
- const assignee = task.assignee
298
- if (!assignee) {
299
- // No assignee — mark failed and continue
300
- const msg = `Task "${task.title}" has no assignee.`
301
- queue.fail(task.id, msg)
302
- config.onProgress?.({
303
- type: 'error',
304
- task: task.id,
305
- data: msg,
306
- } satisfies OrchestratorEvent)
307
- return
308
- }
309
-
310
- const agent = pool.get(assignee)
311
- if (!agent) {
312
- const msg = `Agent "${assignee}" not found in pool for task "${task.title}".`
313
- queue.fail(task.id, msg)
314
- config.onProgress?.({
315
- type: 'error',
316
- task: task.id,
317
- agent: assignee,
318
- data: msg,
319
- } satisfies OrchestratorEvent)
320
- return
321
- }
322
-
323
- config.onProgress?.({
324
- type: 'task_start',
325
- task: task.id,
326
- agent: assignee,
327
- data: task,
328
- } satisfies OrchestratorEvent)
329
-
330
- config.onProgress?.({
331
- type: 'agent_start',
332
- agent: assignee,
333
- task: task.id,
334
- data: task,
335
- } satisfies OrchestratorEvent)
336
-
337
- // Build the prompt: inject shared memory context + task description
338
- const prompt = await buildTaskPrompt(task, team)
339
-
340
- const result = await executeWithRetry(
341
- () => pool.run(assignee, prompt),
342
- task,
343
- (retryData) => {
344
- config.onProgress?.({
345
- type: 'task_retry',
346
- task: task.id,
347
- agent: assignee,
348
- data: retryData,
349
- } satisfies OrchestratorEvent)
350
- },
351
- )
352
-
353
- ctx.agentResults.set(`${assignee}:${task.id}`, result)
354
-
355
- if (result.success) {
356
- // Persist result into shared memory so other agents can read it
357
- const sharedMem = team.getSharedMemoryInstance()
358
- if (sharedMem) {
359
- await sharedMem.write(assignee, `task:${task.id}:result`, result.output)
360
- }
361
-
362
- queue.complete(task.id, result.output)
363
-
364
- config.onProgress?.({
365
- type: 'task_complete',
366
- task: task.id,
367
- agent: assignee,
368
- data: result,
369
- } satisfies OrchestratorEvent)
370
-
371
- config.onProgress?.({
372
- type: 'agent_complete',
373
- agent: assignee,
374
- task: task.id,
375
- data: result,
376
- } satisfies OrchestratorEvent)
377
- } else {
378
- queue.fail(task.id, result.output)
379
- config.onProgress?.({
380
- type: 'error',
381
- task: task.id,
382
- agent: assignee,
383
- data: result,
384
- } satisfies OrchestratorEvent)
385
- }
386
- })
387
-
388
- // Wait for the entire parallel batch before checking for newly-unblocked tasks.
389
- await Promise.all(dispatchPromises)
390
- }
391
- }
392
-
393
- /**
394
- * Build the agent prompt for a specific task.
395
- *
396
- * Injects:
397
- * - Task title and description
398
- * - Dependency results from shared memory (if available)
399
- * - Any messages addressed to this agent from the team bus
400
- */
401
- async function buildTaskPrompt(task: Task, team: Team): Promise<string> {
402
- const lines: string[] = [
403
- `# Task: ${task.title}`,
404
- '',
405
- task.description,
406
- ]
407
-
408
- // Inject shared memory summary so the agent sees its teammates' work
409
- const sharedMem = team.getSharedMemoryInstance()
410
- if (sharedMem) {
411
- const summary = await sharedMem.getSummary()
412
- if (summary) {
413
- lines.push('', summary)
414
- }
415
- }
416
-
417
- // Inject messages from other agents addressed to this assignee
418
- if (task.assignee) {
419
- const messages = team.getMessages(task.assignee)
420
- if (messages.length > 0) {
421
- lines.push('', '## Messages from team members')
422
- for (const msg of messages) {
423
- lines.push(`- **${msg.from}**: ${msg.content}`)
424
- }
425
- }
426
- }
427
-
428
- return lines.join('\n')
429
- }
430
-
431
- // ---------------------------------------------------------------------------
432
- // OpenMultiAgent
433
- // ---------------------------------------------------------------------------
434
-
435
- /**
436
- * Top-level orchestrator for the open-multi-agent framework.
437
- *
438
- * Manages teams, coordinates task execution, and surfaces progress events.
439
- * Most users will interact with this class exclusively.
440
- */
441
- export class OpenMultiAgent {
442
- private readonly config: Required<
443
- Omit<OrchestratorConfig, 'onProgress' | 'defaultBaseURL' | 'defaultApiKey'>
444
- > & Pick<OrchestratorConfig, 'onProgress' | 'defaultBaseURL' | 'defaultApiKey'>
445
-
446
- private readonly teams: Map<string, Team> = new Map()
447
- private completedTaskCount = 0
448
-
449
- /**
450
- * @param config - Optional top-level configuration.
451
- *
452
- * Sensible defaults:
453
- * - `maxConcurrency`: 5
454
- * - `defaultModel`: `'claude-opus-4-6'`
455
- * - `defaultProvider`: `'anthropic'`
456
- */
457
- constructor(config: OrchestratorConfig = {}) {
458
- this.config = {
459
- maxConcurrency: config.maxConcurrency ?? DEFAULT_MAX_CONCURRENCY,
460
- defaultModel: config.defaultModel ?? DEFAULT_MODEL,
461
- defaultProvider: config.defaultProvider ?? 'anthropic',
462
- defaultBaseURL: config.defaultBaseURL,
463
- defaultApiKey: config.defaultApiKey,
464
- onProgress: config.onProgress,
465
- }
466
- }
467
-
468
- // -------------------------------------------------------------------------
469
- // Team management
470
- // -------------------------------------------------------------------------
471
-
472
- /**
473
- * Create and register a {@link Team} with the orchestrator.
474
- *
475
- * The team is stored internally so {@link getStatus} can report aggregate
476
- * agent counts. Returns the new {@link Team} for further configuration.
477
- *
478
- * @param name - Unique team identifier. Throws if already registered.
479
- * @param config - Team configuration (agents, shared memory, concurrency).
480
- */
481
- createTeam(name: string, config: TeamConfig): Team {
482
- if (this.teams.has(name)) {
483
- throw new Error(
484
- `OpenMultiAgent: a team named "${name}" already exists. ` +
485
- `Use a unique name or call shutdown() to clear all teams.`,
486
- )
487
- }
488
- const team = new Team(config)
489
- this.teams.set(name, team)
490
- return team
491
- }
492
-
493
- // -------------------------------------------------------------------------
494
- // Single-agent convenience
495
- // -------------------------------------------------------------------------
496
-
497
- /**
498
- * Run a single prompt with a one-off agent.
499
- *
500
- * Constructs a fresh agent from `config`, runs `prompt` in a single turn,
501
- * and returns the result. The agent is not registered with any pool or team.
502
- *
503
- * Useful for simple one-shot queries that do not need team orchestration.
504
- *
505
- * @param config - Agent configuration.
506
- * @param prompt - The user prompt to send.
507
- */
508
- async runAgent(config: AgentConfig, prompt: string): Promise<AgentRunResult> {
509
- const effective: AgentConfig = {
510
- ...config,
511
- provider: config.provider ?? this.config.defaultProvider,
512
- baseURL: config.baseURL ?? this.config.defaultBaseURL,
513
- apiKey: config.apiKey ?? this.config.defaultApiKey,
514
- }
515
- const agent = buildAgent(effective)
516
- this.config.onProgress?.({
517
- type: 'agent_start',
518
- agent: config.name,
519
- data: { prompt },
520
- })
521
-
522
- const result = await agent.run(prompt)
523
-
524
- this.config.onProgress?.({
525
- type: 'agent_complete',
526
- agent: config.name,
527
- data: result,
528
- })
529
-
530
- if (result.success) {
531
- this.completedTaskCount++
532
- }
533
-
534
- return result
535
- }
536
-
537
- // -------------------------------------------------------------------------
538
- // Auto-orchestrated team run (KILLER FEATURE)
539
- // -------------------------------------------------------------------------
540
-
541
- /**
542
- * Run a team on a high-level goal with full automatic orchestration.
543
- *
544
- * This is the flagship method of the framework. It works as follows:
545
- *
546
- * 1. A temporary "coordinator" agent receives the goal and the team's agent
547
- * roster, and is asked to decompose it into an ordered list of tasks with
548
- * JSON output.
549
- * 2. The tasks are loaded into a {@link TaskQueue}. Title-based dependency
550
- * tokens in the coordinator's output are resolved to task IDs.
551
- * 3. The {@link Scheduler} assigns unassigned tasks to team agents.
552
- * 4. Tasks are executed in dependency order, with independent tasks running
553
- * in parallel up to `maxConcurrency`.
554
- * 5. Results are persisted to shared memory after each task so subsequent
555
- * agents can read them.
556
- * 6. The coordinator synthesises a final answer from all task outputs.
557
- * 7. A {@link TeamRunResult} is returned.
558
- *
559
- * @param team - A team created via {@link createTeam} (or `new Team(...)`).
560
- * @param goal - High-level natural-language goal for the team.
561
- */
562
- async runTeam(team: Team, goal: string): Promise<TeamRunResult> {
563
- const agentConfigs = team.getAgents()
564
-
565
- // ------------------------------------------------------------------
566
- // Step 1: Coordinator decomposes goal into tasks
567
- // ------------------------------------------------------------------
568
- const coordinatorConfig: AgentConfig = {
569
- name: 'coordinator',
570
- model: this.config.defaultModel,
571
- provider: this.config.defaultProvider,
572
- baseURL: this.config.defaultBaseURL,
573
- apiKey: this.config.defaultApiKey,
574
- systemPrompt: this.buildCoordinatorSystemPrompt(agentConfigs),
575
- maxTurns: 3,
576
- }
577
-
578
- const decompositionPrompt = this.buildDecompositionPrompt(goal, agentConfigs)
579
- const coordinatorAgent = buildAgent(coordinatorConfig)
580
-
581
- this.config.onProgress?.({
582
- type: 'agent_start',
583
- agent: 'coordinator',
584
- data: { phase: 'decomposition', goal },
585
- })
586
-
587
- const decompositionResult = await coordinatorAgent.run(decompositionPrompt)
588
- const agentResults = new Map<string, AgentRunResult>()
589
- agentResults.set('coordinator:decompose', decompositionResult)
590
-
591
- // ------------------------------------------------------------------
592
- // Step 2: Parse tasks from coordinator output
593
- // ------------------------------------------------------------------
594
- const taskSpecs = parseTaskSpecs(decompositionResult.output)
595
-
596
- const queue = new TaskQueue()
597
- const scheduler = new Scheduler('dependency-first')
598
-
599
- if (taskSpecs && taskSpecs.length > 0) {
600
- // Map title-based dependsOn references to real task IDs so we can
601
- // build the dependency graph before adding tasks to the queue.
602
- this.loadSpecsIntoQueue(taskSpecs, agentConfigs, queue)
603
- } else {
604
- // Coordinator failed to produce structured output — fall back to
605
- // one task per agent using the goal as the description.
606
- for (const agentConfig of agentConfigs) {
607
- const task = createTask({
608
- title: `${agentConfig.name}: ${goal.slice(0, 80)}`,
609
- description: goal,
610
- assignee: agentConfig.name,
611
- })
612
- queue.add(task)
613
- }
614
- }
615
-
616
- // ------------------------------------------------------------------
617
- // Step 3: Auto-assign any unassigned tasks
618
- // ------------------------------------------------------------------
619
- scheduler.autoAssign(queue, agentConfigs)
620
-
621
- // ------------------------------------------------------------------
622
- // Step 4: Build pool and execute
623
- // ------------------------------------------------------------------
624
- const pool = this.buildPool(agentConfigs)
625
- const ctx: RunContext = {
626
- team,
627
- pool,
628
- scheduler,
629
- agentResults,
630
- config: this.config,
631
- }
632
-
633
- await executeQueue(queue, ctx)
634
-
635
- // ------------------------------------------------------------------
636
- // Step 5: Coordinator synthesises final result
637
- // ------------------------------------------------------------------
638
- const synthesisPrompt = await this.buildSynthesisPrompt(goal, queue.list(), team)
639
- const synthesisResult = await coordinatorAgent.run(synthesisPrompt)
640
- agentResults.set('coordinator', synthesisResult)
641
-
642
- this.config.onProgress?.({
643
- type: 'agent_complete',
644
- agent: 'coordinator',
645
- data: synthesisResult,
646
- })
647
-
648
- // Note: coordinator decompose and synthesis are internal meta-steps.
649
- // Only actual user tasks (non-coordinator keys) are counted in
650
- // buildTeamRunResult, so we do not increment completedTaskCount here.
651
-
652
- return this.buildTeamRunResult(agentResults)
653
- }
654
-
655
- // -------------------------------------------------------------------------
656
- // Explicit-task team run
657
- // -------------------------------------------------------------------------
658
-
659
- /**
660
- * Run a team with an explicitly provided task list.
661
- *
662
- * Simpler than {@link runTeam}: no coordinator agent is involved. Tasks are
663
- * loaded directly into the queue, unassigned tasks are auto-assigned via the
664
- * {@link Scheduler}, and execution proceeds in dependency order.
665
- *
666
- * @param team - A team created via {@link createTeam}.
667
- * @param tasks - Array of task descriptors.
668
- */
669
- async runTasks(
670
- team: Team,
671
- tasks: ReadonlyArray<{
672
- title: string
673
- description: string
674
- assignee?: string
675
- dependsOn?: string[]
676
- maxRetries?: number
677
- retryDelayMs?: number
678
- retryBackoff?: number
679
- }>,
680
- ): Promise<TeamRunResult> {
681
- const agentConfigs = team.getAgents()
682
- const queue = new TaskQueue()
683
- const scheduler = new Scheduler('dependency-first')
684
-
685
- this.loadSpecsIntoQueue(
686
- tasks.map((t) => ({
687
- title: t.title,
688
- description: t.description,
689
- assignee: t.assignee,
690
- dependsOn: t.dependsOn,
691
- maxRetries: t.maxRetries,
692
- retryDelayMs: t.retryDelayMs,
693
- retryBackoff: t.retryBackoff,
694
- })),
695
- agentConfigs,
696
- queue,
697
- )
698
-
699
- scheduler.autoAssign(queue, agentConfigs)
700
-
701
- const pool = this.buildPool(agentConfigs)
702
- const agentResults = new Map<string, AgentRunResult>()
703
- const ctx: RunContext = {
704
- team,
705
- pool,
706
- scheduler,
707
- agentResults,
708
- config: this.config,
709
- }
710
-
711
- await executeQueue(queue, ctx)
712
-
713
- return this.buildTeamRunResult(agentResults)
714
- }
715
-
716
- // -------------------------------------------------------------------------
717
- // Observability
718
- // -------------------------------------------------------------------------
719
-
720
- /**
721
- * Returns a lightweight status snapshot.
722
- *
723
- * - `teams` — Number of teams registered with this orchestrator.
724
- * - `activeAgents` — Total agents currently in `running` state.
725
- * - `completedTasks` — Cumulative count of successfully completed tasks
726
- * (coordinator meta-steps excluded).
727
- */
728
- getStatus(): { teams: number; activeAgents: number; completedTasks: number } {
729
- return {
730
- teams: this.teams.size,
731
- activeAgents: 0, // Pools are ephemeral per-run; no cross-run state to inspect.
732
- completedTasks: this.completedTaskCount,
733
- }
734
- }
735
-
736
- // -------------------------------------------------------------------------
737
- // Lifecycle
738
- // -------------------------------------------------------------------------
739
-
740
- /**
741
- * Deregister all teams and reset internal counters.
742
- *
743
- * Does not cancel in-flight runs. Call this when you want to reuse the
744
- * orchestrator instance for a fresh set of teams.
745
- *
746
- * Async for forward compatibility — shutdown may need to perform async
747
- * cleanup (e.g. graceful agent drain) in future versions.
748
- */
749
- async shutdown(): Promise<void> {
750
- this.teams.clear()
751
- this.completedTaskCount = 0
752
- }
753
-
754
- // -------------------------------------------------------------------------
755
- // Private helpers
756
- // -------------------------------------------------------------------------
757
-
758
- /** Build the system prompt given to the coordinator agent. */
759
- private buildCoordinatorSystemPrompt(agents: AgentConfig[]): string {
760
- const roster = agents
761
- .map(
762
- (a) =>
763
- `- **${a.name}** (${a.model}): ${a.systemPrompt?.slice(0, 120) ?? 'general purpose agent'}`,
764
- )
765
- .join('\n')
766
-
767
- return [
768
- 'You are a task coordinator responsible for decomposing high-level goals',
769
- 'into concrete, actionable tasks and assigning them to the right team members.',
770
- '',
771
- '## Team Roster',
772
- roster,
773
- '',
774
- '## Output Format',
775
- 'When asked to decompose a goal, respond ONLY with a JSON array of task objects.',
776
- 'Each task must have:',
777
- ' - "title": Short descriptive title (string)',
778
- ' - "description": Full task description with context and expected output (string)',
779
- ' - "assignee": One of the agent names listed in the roster (string)',
780
- ' - "dependsOn": Array of titles of tasks this task depends on (string[], may be empty)',
781
- '',
782
- 'Wrap the JSON in a ```json code fence.',
783
- 'Do not include any text outside the code fence.',
784
- '',
785
- '## When synthesising results',
786
- 'You will be given completed task outputs and asked to synthesise a final answer.',
787
- 'Write a clear, comprehensive response that addresses the original goal.',
788
- ].join('\n')
789
- }
790
-
791
- /** Build the decomposition prompt for the coordinator. */
792
- private buildDecompositionPrompt(goal: string, agents: AgentConfig[]): string {
793
- const names = agents.map((a) => a.name).join(', ')
794
- return [
795
- `Decompose the following goal into tasks for your team (${names}).`,
796
- '',
797
- `## Goal`,
798
- goal,
799
- '',
800
- 'Return ONLY the JSON task array in a ```json code fence.',
801
- ].join('\n')
802
- }
803
-
804
- /** Build the synthesis prompt shown to the coordinator after all tasks complete. */
805
- private async buildSynthesisPrompt(
806
- goal: string,
807
- tasks: Task[],
808
- team: Team,
809
- ): Promise<string> {
810
- const completedTasks = tasks.filter((t) => t.status === 'completed')
811
- const failedTasks = tasks.filter((t) => t.status === 'failed')
812
-
813
- const resultSections = completedTasks.map((t) => {
814
- const assignee = t.assignee ?? 'unknown'
815
- return `### ${t.title} (completed by ${assignee})\n${t.result ?? '(no output)'}`
816
- })
817
-
818
- const failureSections = failedTasks.map(
819
- (t) => `### ${t.title} (FAILED)\nError: ${t.result ?? 'unknown error'}`,
820
- )
821
-
822
- // Also include shared memory summary for additional context
823
- let memorySummary = ''
824
- const sharedMem = team.getSharedMemoryInstance()
825
- if (sharedMem) {
826
- memorySummary = await sharedMem.getSummary()
827
- }
828
-
829
- return [
830
- `## Original Goal`,
831
- goal,
832
- '',
833
- `## Task Results`,
834
- ...resultSections,
835
- ...(failureSections.length > 0 ? ['', '## Failed Tasks', ...failureSections] : []),
836
- ...(memorySummary ? ['', memorySummary] : []),
837
- '',
838
- '## Your Task',
839
- 'Synthesise the above results into a comprehensive final answer that addresses the original goal.',
840
- 'If some tasks failed, note any gaps in the result.',
841
- ].join('\n')
842
- }
843
-
844
- /**
845
- * Load a list of task specs into a queue.
846
- *
847
- * Handles title-based `dependsOn` references by building a title→id map first,
848
- * then resolving them to real IDs before adding tasks to the queue.
849
- */
850
- private loadSpecsIntoQueue(
851
- specs: ReadonlyArray<ParsedTaskSpec & {
852
- maxRetries?: number
853
- retryDelayMs?: number
854
- retryBackoff?: number
855
- }>,
856
- agentConfigs: AgentConfig[],
857
- queue: TaskQueue,
858
- ): void {
859
- const agentNames = new Set(agentConfigs.map((a) => a.name))
860
-
861
- // First pass: create tasks (without dependencies) to get stable IDs.
862
- const titleToId = new Map<string, string>()
863
- const createdTasks: Task[] = []
864
-
865
- for (const spec of specs) {
866
- const task = createTask({
867
- title: spec.title,
868
- description: spec.description,
869
- assignee: spec.assignee && agentNames.has(spec.assignee)
870
- ? spec.assignee
871
- : undefined,
872
- maxRetries: spec.maxRetries,
873
- retryDelayMs: spec.retryDelayMs,
874
- retryBackoff: spec.retryBackoff,
875
- })
876
- titleToId.set(spec.title.toLowerCase().trim(), task.id)
877
- createdTasks.push(task)
878
- }
879
-
880
- // Second pass: resolve title-based dependsOn to IDs.
881
- for (let i = 0; i < createdTasks.length; i++) {
882
- const spec = specs[i]!
883
- const task = createdTasks[i]!
884
-
885
- if (!spec.dependsOn || spec.dependsOn.length === 0) {
886
- queue.add(task)
887
- continue
888
- }
889
-
890
- const resolvedDeps: string[] = []
891
- for (const depRef of spec.dependsOn) {
892
- // Accept both raw IDs and title strings
893
- const byId = createdTasks.find((t) => t.id === depRef)
894
- const byTitle = titleToId.get(depRef.toLowerCase().trim())
895
- const resolvedId = byId?.id ?? byTitle
896
- if (resolvedId) {
897
- resolvedDeps.push(resolvedId)
898
- }
899
- }
900
-
901
- const taskWithDeps: Task = {
902
- ...task,
903
- dependsOn: resolvedDeps.length > 0 ? resolvedDeps : undefined,
904
- }
905
- queue.add(taskWithDeps)
906
- }
907
- }
908
-
909
- /** Build an {@link AgentPool} from a list of agent configurations. */
910
- private buildPool(agentConfigs: AgentConfig[]): AgentPool {
911
- const pool = new AgentPool(this.config.maxConcurrency)
912
- for (const config of agentConfigs) {
913
- const effective: AgentConfig = {
914
- ...config,
915
- model: config.model,
916
- provider: config.provider ?? this.config.defaultProvider,
917
- baseURL: config.baseURL ?? this.config.defaultBaseURL,
918
- apiKey: config.apiKey ?? this.config.defaultApiKey,
919
- }
920
- pool.add(buildAgent(effective))
921
- }
922
- return pool
923
- }
924
-
925
- /**
926
- * Aggregate the per-run `agentResults` map into a {@link TeamRunResult}.
927
- *
928
- * Merges results keyed as `agentName:taskId` back into a per-agent map
929
- * by agent name for the public result surface.
930
- *
931
- * Only non-coordinator entries are counted toward `completedTaskCount` to
932
- * avoid double-counting the coordinator's internal decompose/synthesis steps.
933
- */
934
- private buildTeamRunResult(
935
- agentResults: Map<string, AgentRunResult>,
936
- ): TeamRunResult {
937
- let totalUsage: TokenUsage = ZERO_USAGE
938
- let overallSuccess = true
939
- const collapsed = new Map<string, AgentRunResult>()
940
-
941
- for (const [key, result] of agentResults) {
942
- // Strip the `:taskId` suffix to get the agent name
943
- const agentName = key.includes(':') ? key.split(':')[0]! : key
944
-
945
- totalUsage = addUsage(totalUsage, result.tokenUsage)
946
- if (!result.success) overallSuccess = false
947
-
948
- const existing = collapsed.get(agentName)
949
- if (!existing) {
950
- collapsed.set(agentName, result)
951
- } else {
952
- // Merge multiple results for the same agent (multi-task case).
953
- // Keep the latest `structured` value (last completed task wins).
954
- collapsed.set(agentName, {
955
- success: existing.success && result.success,
956
- output: [existing.output, result.output].filter(Boolean).join('\n\n---\n\n'),
957
- messages: [...existing.messages, ...result.messages],
958
- tokenUsage: addUsage(existing.tokenUsage, result.tokenUsage),
959
- toolCalls: [...existing.toolCalls, ...result.toolCalls],
960
- structured: result.structured !== undefined ? result.structured : existing.structured,
961
- })
962
- }
963
-
964
- // Only count actual user tasks — skip coordinator meta-entries
965
- // (keys that start with 'coordinator') to avoid double-counting.
966
- if (result.success && !key.startsWith('coordinator')) {
967
- this.completedTaskCount++
968
- }
969
- }
970
-
971
- return {
972
- success: overallSuccess,
973
- agentResults: collapsed,
974
- totalTokenUsage: totalUsage,
975
- }
976
- }
977
- }