@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,120 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Core conversation loop engine for open-multi-agent.
|
|
3
|
+
*
|
|
4
|
+
* {@link AgentRunner} is the heart of the framework. It handles:
|
|
5
|
+
* - Sending messages to the LLM adapter
|
|
6
|
+
* - Extracting tool-use blocks from the response
|
|
7
|
+
* - Executing tool calls in parallel via {@link ToolExecutor}
|
|
8
|
+
* - Appending tool results and looping back until `end_turn`
|
|
9
|
+
* - Accumulating token usage and timing data across all turns
|
|
10
|
+
*
|
|
11
|
+
* The loop follows a standard agentic conversation pattern:
|
|
12
|
+
* one outer `while (true)` that breaks on `end_turn` or maxTurns exhaustion.
|
|
13
|
+
*/
|
|
14
|
+
import type { LLMMessage, ToolCallRecord, TokenUsage, StreamEvent, ToolResult, LLMAdapter } from '../types.js';
|
|
15
|
+
import type { ToolRegistry } from '../tool/framework.js';
|
|
16
|
+
import type { ToolExecutor } from '../tool/executor.js';
|
|
17
|
+
/**
|
|
18
|
+
* Static configuration for an {@link AgentRunner} instance.
|
|
19
|
+
* These values are constant across every `run` / `stream` call.
|
|
20
|
+
*/
|
|
21
|
+
export interface RunnerOptions {
|
|
22
|
+
/** LLM model identifier, e.g. `'claude-opus-4-6'`. */
|
|
23
|
+
readonly model: string;
|
|
24
|
+
/** Optional system prompt prepended to every conversation. */
|
|
25
|
+
readonly systemPrompt?: string;
|
|
26
|
+
/**
|
|
27
|
+
* Maximum number of tool-call round-trips before the runner stops.
|
|
28
|
+
* Prevents unbounded loops. Defaults to `10`.
|
|
29
|
+
*/
|
|
30
|
+
readonly maxTurns?: number;
|
|
31
|
+
/** Maximum output tokens per LLM response. */
|
|
32
|
+
readonly maxTokens?: number;
|
|
33
|
+
/** Sampling temperature passed to the adapter. */
|
|
34
|
+
readonly temperature?: number;
|
|
35
|
+
/** AbortSignal that cancels any in-flight adapter call and stops the loop. */
|
|
36
|
+
readonly abortSignal?: AbortSignal;
|
|
37
|
+
/**
|
|
38
|
+
* Whitelist of tool names this runner is allowed to use.
|
|
39
|
+
* When provided, only tools whose name appears in this list are sent to the
|
|
40
|
+
* LLM. When omitted, all registered tools are available.
|
|
41
|
+
*/
|
|
42
|
+
readonly allowedTools?: readonly string[];
|
|
43
|
+
/** Display name of the agent driving this runner (used in tool context). */
|
|
44
|
+
readonly agentName?: string;
|
|
45
|
+
/** Short role description of the agent (used in tool context). */
|
|
46
|
+
readonly agentRole?: string;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Per-call callbacks for observing tool execution in real time.
|
|
50
|
+
* All callbacks are optional; unused ones are simply skipped.
|
|
51
|
+
*/
|
|
52
|
+
export interface RunOptions {
|
|
53
|
+
/** Fired just before each tool is dispatched. */
|
|
54
|
+
readonly onToolCall?: (name: string, input: Record<string, unknown>) => void;
|
|
55
|
+
/** Fired after each tool result is received. */
|
|
56
|
+
readonly onToolResult?: (name: string, result: ToolResult) => void;
|
|
57
|
+
/** Fired after each complete {@link LLMMessage} is appended. */
|
|
58
|
+
readonly onMessage?: (message: LLMMessage) => void;
|
|
59
|
+
}
|
|
60
|
+
/** The aggregated result returned when a full run completes. */
|
|
61
|
+
export interface RunResult {
|
|
62
|
+
/** All messages accumulated during this run (assistant + tool results). */
|
|
63
|
+
readonly messages: LLMMessage[];
|
|
64
|
+
/** The final text output from the last assistant turn. */
|
|
65
|
+
readonly output: string;
|
|
66
|
+
/** All tool calls made during this run, in execution order. */
|
|
67
|
+
readonly toolCalls: ToolCallRecord[];
|
|
68
|
+
/** Aggregated token counts across every LLM call in this run. */
|
|
69
|
+
readonly tokenUsage: TokenUsage;
|
|
70
|
+
/** Total number of LLM turns (including tool-call follow-ups). */
|
|
71
|
+
readonly turns: number;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Drives a full agentic conversation: LLM calls, tool execution, and looping.
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* ```ts
|
|
78
|
+
* const runner = new AgentRunner(adapter, registry, executor, {
|
|
79
|
+
* model: 'claude-opus-4-6',
|
|
80
|
+
* maxTurns: 10,
|
|
81
|
+
* })
|
|
82
|
+
* const result = await runner.run(messages)
|
|
83
|
+
* console.log(result.output)
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
86
|
+
export declare class AgentRunner {
|
|
87
|
+
private readonly adapter;
|
|
88
|
+
private readonly toolRegistry;
|
|
89
|
+
private readonly toolExecutor;
|
|
90
|
+
private readonly options;
|
|
91
|
+
private readonly maxTurns;
|
|
92
|
+
constructor(adapter: LLMAdapter, toolRegistry: ToolRegistry, toolExecutor: ToolExecutor, options: RunnerOptions);
|
|
93
|
+
/**
|
|
94
|
+
* Run a complete conversation starting from `messages`.
|
|
95
|
+
*
|
|
96
|
+
* The call may internally make multiple LLM requests (one per tool-call
|
|
97
|
+
* round-trip). It returns only when:
|
|
98
|
+
* - The LLM emits `end_turn` with no tool-use blocks, or
|
|
99
|
+
* - `maxTurns` is exceeded, or
|
|
100
|
+
* - The abort signal is triggered.
|
|
101
|
+
*/
|
|
102
|
+
run(messages: LLMMessage[], options?: RunOptions): Promise<RunResult>;
|
|
103
|
+
/**
|
|
104
|
+
* Run the conversation and yield {@link StreamEvent}s incrementally.
|
|
105
|
+
*
|
|
106
|
+
* Callers receive:
|
|
107
|
+
* - `{ type: 'text', data: string }` for each text delta
|
|
108
|
+
* - `{ type: 'tool_use', data: ToolUseBlock }` when the model requests a tool
|
|
109
|
+
* - `{ type: 'tool_result', data: ToolResultBlock }` after each execution
|
|
110
|
+
* - `{ type: 'done', data: RunResult }` at the very end
|
|
111
|
+
* - `{ type: 'error', data: Error }` on unrecoverable failure
|
|
112
|
+
*/
|
|
113
|
+
stream(initialMessages: LLMMessage[], options?: RunOptions): AsyncGenerator<StreamEvent>;
|
|
114
|
+
/**
|
|
115
|
+
* Build the {@link ToolUseContext} passed to every tool execution.
|
|
116
|
+
* Identifies this runner as the invoking agent.
|
|
117
|
+
*/
|
|
118
|
+
private buildToolContext;
|
|
119
|
+
}
|
|
120
|
+
//# sourceMappingURL=runner.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,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. It handles:
|
|
5
|
+
* - Sending messages to the LLM adapter
|
|
6
|
+
* - Extracting tool-use blocks from the response
|
|
7
|
+
* - Executing tool calls in parallel via {@link ToolExecutor}
|
|
8
|
+
* - Appending tool results and looping back until `end_turn`
|
|
9
|
+
* - Accumulating token usage and timing data across all turns
|
|
10
|
+
*
|
|
11
|
+
* The loop follows a standard agentic conversation pattern:
|
|
12
|
+
* one outer `while (true)` that breaks on `end_turn` or maxTurns exhaustion.
|
|
13
|
+
*/
|
|
14
|
+
// ---------------------------------------------------------------------------
|
|
15
|
+
// Internal helpers
|
|
16
|
+
// ---------------------------------------------------------------------------
|
|
17
|
+
/** Extract every TextBlock from a content array and join them. */
|
|
18
|
+
function extractText(content) {
|
|
19
|
+
return content
|
|
20
|
+
.filter((b) => b.type === 'text')
|
|
21
|
+
.map(b => b.text)
|
|
22
|
+
.join('');
|
|
23
|
+
}
|
|
24
|
+
/** Extract every ToolUseBlock from a content array. */
|
|
25
|
+
function extractToolUseBlocks(content) {
|
|
26
|
+
return content.filter((b) => b.type === 'tool_use');
|
|
27
|
+
}
|
|
28
|
+
/** Add two {@link TokenUsage} values together, returning a new object. */
|
|
29
|
+
function addTokenUsage(a, b) {
|
|
30
|
+
return {
|
|
31
|
+
input_tokens: a.input_tokens + b.input_tokens,
|
|
32
|
+
output_tokens: a.output_tokens + b.output_tokens,
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
const ZERO_USAGE = { input_tokens: 0, output_tokens: 0 };
|
|
36
|
+
// ---------------------------------------------------------------------------
|
|
37
|
+
// AgentRunner
|
|
38
|
+
// ---------------------------------------------------------------------------
|
|
39
|
+
/**
|
|
40
|
+
* Drives a full agentic conversation: LLM calls, tool execution, and looping.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```ts
|
|
44
|
+
* const runner = new AgentRunner(adapter, registry, executor, {
|
|
45
|
+
* model: 'claude-opus-4-6',
|
|
46
|
+
* maxTurns: 10,
|
|
47
|
+
* })
|
|
48
|
+
* const result = await runner.run(messages)
|
|
49
|
+
* console.log(result.output)
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
export class AgentRunner {
|
|
53
|
+
adapter;
|
|
54
|
+
toolRegistry;
|
|
55
|
+
toolExecutor;
|
|
56
|
+
options;
|
|
57
|
+
maxTurns;
|
|
58
|
+
constructor(adapter, toolRegistry, toolExecutor, options) {
|
|
59
|
+
this.adapter = adapter;
|
|
60
|
+
this.toolRegistry = toolRegistry;
|
|
61
|
+
this.toolExecutor = toolExecutor;
|
|
62
|
+
this.options = options;
|
|
63
|
+
this.maxTurns = options.maxTurns ?? 10;
|
|
64
|
+
}
|
|
65
|
+
// -------------------------------------------------------------------------
|
|
66
|
+
// Public API
|
|
67
|
+
// -------------------------------------------------------------------------
|
|
68
|
+
/**
|
|
69
|
+
* Run a complete conversation starting from `messages`.
|
|
70
|
+
*
|
|
71
|
+
* The call may internally make multiple LLM requests (one per tool-call
|
|
72
|
+
* round-trip). It returns only when:
|
|
73
|
+
* - The LLM emits `end_turn` with no tool-use blocks, or
|
|
74
|
+
* - `maxTurns` is exceeded, or
|
|
75
|
+
* - The abort signal is triggered.
|
|
76
|
+
*/
|
|
77
|
+
async run(messages, options = {}) {
|
|
78
|
+
// Collect everything yielded by the internal streaming loop.
|
|
79
|
+
const accumulated = {
|
|
80
|
+
messages: [],
|
|
81
|
+
output: '',
|
|
82
|
+
toolCalls: [],
|
|
83
|
+
tokenUsage: ZERO_USAGE,
|
|
84
|
+
turns: 0,
|
|
85
|
+
};
|
|
86
|
+
for await (const event of this.stream(messages, options)) {
|
|
87
|
+
if (event.type === 'done') {
|
|
88
|
+
const result = event.data;
|
|
89
|
+
accumulated.messages = result.messages;
|
|
90
|
+
accumulated.output = result.output;
|
|
91
|
+
accumulated.toolCalls = result.toolCalls;
|
|
92
|
+
accumulated.tokenUsage = result.tokenUsage;
|
|
93
|
+
accumulated.turns = result.turns;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return accumulated;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Run the conversation and yield {@link StreamEvent}s incrementally.
|
|
100
|
+
*
|
|
101
|
+
* Callers receive:
|
|
102
|
+
* - `{ type: 'text', data: string }` for each text delta
|
|
103
|
+
* - `{ type: 'tool_use', data: ToolUseBlock }` when the model requests a tool
|
|
104
|
+
* - `{ type: 'tool_result', data: ToolResultBlock }` after each execution
|
|
105
|
+
* - `{ type: 'done', data: RunResult }` at the very end
|
|
106
|
+
* - `{ type: 'error', data: Error }` on unrecoverable failure
|
|
107
|
+
*/
|
|
108
|
+
async *stream(initialMessages, options = {}) {
|
|
109
|
+
// Working copy of the conversation — mutated as turns progress.
|
|
110
|
+
const conversationMessages = [...initialMessages];
|
|
111
|
+
// Accumulated state across all turns.
|
|
112
|
+
let totalUsage = ZERO_USAGE;
|
|
113
|
+
const allToolCalls = [];
|
|
114
|
+
let finalOutput = '';
|
|
115
|
+
let turns = 0;
|
|
116
|
+
// Build the stable LLM options once; model / tokens / temp don't change.
|
|
117
|
+
// toToolDefs() returns LLMToolDef[] (inputSchema, camelCase) — matches
|
|
118
|
+
// LLMChatOptions.tools from types.ts directly.
|
|
119
|
+
const allDefs = this.toolRegistry.toToolDefs();
|
|
120
|
+
const toolDefs = this.options.allowedTools
|
|
121
|
+
? allDefs.filter(d => this.options.allowedTools.includes(d.name))
|
|
122
|
+
: allDefs;
|
|
123
|
+
const baseChatOptions = {
|
|
124
|
+
model: this.options.model,
|
|
125
|
+
tools: toolDefs.length > 0 ? toolDefs : undefined,
|
|
126
|
+
maxTokens: this.options.maxTokens,
|
|
127
|
+
temperature: this.options.temperature,
|
|
128
|
+
systemPrompt: this.options.systemPrompt,
|
|
129
|
+
abortSignal: this.options.abortSignal,
|
|
130
|
+
};
|
|
131
|
+
try {
|
|
132
|
+
// -----------------------------------------------------------------
|
|
133
|
+
// Main agentic loop — `while (true)` until end_turn or maxTurns
|
|
134
|
+
// -----------------------------------------------------------------
|
|
135
|
+
while (true) {
|
|
136
|
+
// Respect abort before each LLM call.
|
|
137
|
+
if (this.options.abortSignal?.aborted) {
|
|
138
|
+
break;
|
|
139
|
+
}
|
|
140
|
+
// Guard against unbounded loops.
|
|
141
|
+
if (turns >= this.maxTurns) {
|
|
142
|
+
break;
|
|
143
|
+
}
|
|
144
|
+
turns++;
|
|
145
|
+
// ------------------------------------------------------------------
|
|
146
|
+
// Step 1: Call the LLM and collect the full response for this turn.
|
|
147
|
+
// ------------------------------------------------------------------
|
|
148
|
+
const response = await this.adapter.chat(conversationMessages, baseChatOptions);
|
|
149
|
+
totalUsage = addTokenUsage(totalUsage, response.usage);
|
|
150
|
+
// ------------------------------------------------------------------
|
|
151
|
+
// Step 2: Build the assistant message from the response content.
|
|
152
|
+
// ------------------------------------------------------------------
|
|
153
|
+
const assistantMessage = {
|
|
154
|
+
role: 'assistant',
|
|
155
|
+
content: response.content,
|
|
156
|
+
};
|
|
157
|
+
conversationMessages.push(assistantMessage);
|
|
158
|
+
options.onMessage?.(assistantMessage);
|
|
159
|
+
// Yield text deltas so streaming callers can display them promptly.
|
|
160
|
+
const turnText = extractText(response.content);
|
|
161
|
+
if (turnText.length > 0) {
|
|
162
|
+
yield { type: 'text', data: turnText };
|
|
163
|
+
}
|
|
164
|
+
// Announce each tool-use block the model requested.
|
|
165
|
+
const toolUseBlocks = extractToolUseBlocks(response.content);
|
|
166
|
+
for (const block of toolUseBlocks) {
|
|
167
|
+
yield { type: 'tool_use', data: block };
|
|
168
|
+
}
|
|
169
|
+
// ------------------------------------------------------------------
|
|
170
|
+
// Step 3: Decide whether to continue looping.
|
|
171
|
+
// ------------------------------------------------------------------
|
|
172
|
+
if (toolUseBlocks.length === 0) {
|
|
173
|
+
// No tools requested — this is the terminal assistant turn.
|
|
174
|
+
finalOutput = turnText;
|
|
175
|
+
break;
|
|
176
|
+
}
|
|
177
|
+
// ------------------------------------------------------------------
|
|
178
|
+
// Step 4: Execute all tool calls in PARALLEL.
|
|
179
|
+
//
|
|
180
|
+
// Parallel execution is critical for multi-tool responses where the
|
|
181
|
+
// tools are independent (e.g. reading several files at once).
|
|
182
|
+
// ------------------------------------------------------------------
|
|
183
|
+
const toolContext = this.buildToolContext();
|
|
184
|
+
const executionPromises = toolUseBlocks.map(async (block) => {
|
|
185
|
+
options.onToolCall?.(block.name, block.input);
|
|
186
|
+
const startTime = Date.now();
|
|
187
|
+
let result;
|
|
188
|
+
try {
|
|
189
|
+
result = await this.toolExecutor.execute(block.name, block.input, toolContext);
|
|
190
|
+
}
|
|
191
|
+
catch (err) {
|
|
192
|
+
// Tool executor errors become error results — the loop continues.
|
|
193
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
194
|
+
result = { data: message, isError: true };
|
|
195
|
+
}
|
|
196
|
+
const duration = Date.now() - startTime;
|
|
197
|
+
options.onToolResult?.(block.name, result);
|
|
198
|
+
const record = {
|
|
199
|
+
toolName: block.name,
|
|
200
|
+
input: block.input,
|
|
201
|
+
output: result.data,
|
|
202
|
+
duration,
|
|
203
|
+
};
|
|
204
|
+
const resultBlock = {
|
|
205
|
+
type: 'tool_result',
|
|
206
|
+
tool_use_id: block.id,
|
|
207
|
+
content: result.data,
|
|
208
|
+
is_error: result.isError,
|
|
209
|
+
};
|
|
210
|
+
return { resultBlock, record };
|
|
211
|
+
});
|
|
212
|
+
// Wait for every tool in this turn to finish.
|
|
213
|
+
const executions = await Promise.all(executionPromises);
|
|
214
|
+
// ------------------------------------------------------------------
|
|
215
|
+
// Step 5: Accumulate results and build the user message that carries
|
|
216
|
+
// them back to the LLM in the next turn.
|
|
217
|
+
// ------------------------------------------------------------------
|
|
218
|
+
const toolResultBlocks = executions.map(e => e.resultBlock);
|
|
219
|
+
for (const { record, resultBlock } of executions) {
|
|
220
|
+
allToolCalls.push(record);
|
|
221
|
+
yield { type: 'tool_result', data: resultBlock };
|
|
222
|
+
}
|
|
223
|
+
const toolResultMessage = {
|
|
224
|
+
role: 'user',
|
|
225
|
+
content: toolResultBlocks,
|
|
226
|
+
};
|
|
227
|
+
conversationMessages.push(toolResultMessage);
|
|
228
|
+
options.onMessage?.(toolResultMessage);
|
|
229
|
+
// Loop back to Step 1 — send updated conversation to the LLM.
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
catch (err) {
|
|
233
|
+
const error = err instanceof Error ? err : new Error(String(err));
|
|
234
|
+
yield { type: 'error', data: error };
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
// If the loop exited due to maxTurns, use whatever text was last emitted.
|
|
238
|
+
if (finalOutput === '' && conversationMessages.length > 0) {
|
|
239
|
+
const lastAssistant = [...conversationMessages]
|
|
240
|
+
.reverse()
|
|
241
|
+
.find(m => m.role === 'assistant');
|
|
242
|
+
if (lastAssistant !== undefined) {
|
|
243
|
+
finalOutput = extractText(lastAssistant.content);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
const runResult = {
|
|
247
|
+
// Return only the messages added during this run (not the initial seed).
|
|
248
|
+
messages: conversationMessages.slice(initialMessages.length),
|
|
249
|
+
output: finalOutput,
|
|
250
|
+
toolCalls: allToolCalls,
|
|
251
|
+
tokenUsage: totalUsage,
|
|
252
|
+
turns,
|
|
253
|
+
};
|
|
254
|
+
yield { type: 'done', data: runResult };
|
|
255
|
+
}
|
|
256
|
+
// -------------------------------------------------------------------------
|
|
257
|
+
// Private helpers
|
|
258
|
+
// -------------------------------------------------------------------------
|
|
259
|
+
/**
|
|
260
|
+
* Build the {@link ToolUseContext} passed to every tool execution.
|
|
261
|
+
* Identifies this runner as the invoking agent.
|
|
262
|
+
*/
|
|
263
|
+
buildToolContext() {
|
|
264
|
+
return {
|
|
265
|
+
agent: {
|
|
266
|
+
name: this.options.agentName ?? 'runner',
|
|
267
|
+
role: this.options.agentRole ?? 'assistant',
|
|
268
|
+
model: this.options.model,
|
|
269
|
+
},
|
|
270
|
+
abortSignal: this.options.abortSignal,
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|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CAAC;QAED,MAAM,SAAS,GAAc;YAC3B,yEAAyE;YACzE,QAAQ,EAAE,oBAAoB,CAAC,KAAK,CAAC,eAAe,CAAC,MAAM,CAAC;YAC5D,MAAM,EAAE,WAAW;YACnB,SAAS,EAAE,YAAY;YACvB,UAAU,EAAE,UAAU;YACtB,KAAK;SACN,CAAA;QAED,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,SAAS,EAAwB,CAAA;IAC/D,CAAC;IAED,4EAA4E;IAC5E,kBAAkB;IAClB,4EAA4E;IAE5E;;;OAGG;IACK,gBAAgB;QACtB,OAAO;YACL,KAAK,EAAE;gBACL,IAAI,EAAE,IAAI,CAAC,OAAO,CAAC,SAAS,IAAI,QAAQ;gBACxC,IAAI,EAAE,IAAI,CAAC,OAAO,CAAC,SAAS,IAAI,WAAW;gBAC3C,KAAK,EAAE,IAAI,CAAC,OAAO,CAAC,KAAK;aAC1B;YACD,WAAW,EAAE,IAAI,CAAC,OAAO,CAAC,WAAW;SACtC,CAAA;IACH,CAAC;CACF"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview open-multi-agent — public API surface.
|
|
3
|
+
*
|
|
4
|
+
* Import from `'open-multi-agent'` to access everything you need:
|
|
5
|
+
*
|
|
6
|
+
* ```ts
|
|
7
|
+
* import { OpenMultiAgent, Agent, Team, defineTool } from 'open-multi-agent'
|
|
8
|
+
* ```
|
|
9
|
+
*
|
|
10
|
+
* ## Quickstart
|
|
11
|
+
*
|
|
12
|
+
* ### Single agent
|
|
13
|
+
* ```ts
|
|
14
|
+
* const orchestrator = new OpenMultiAgent({ defaultModel: 'claude-opus-4-6' })
|
|
15
|
+
* const result = await orchestrator.runAgent(
|
|
16
|
+
* { name: 'assistant', model: 'claude-opus-4-6' },
|
|
17
|
+
* 'Explain monads in one paragraph.',
|
|
18
|
+
* )
|
|
19
|
+
* console.log(result.output)
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* ### Multi-agent team (auto-orchestrated)
|
|
23
|
+
* ```ts
|
|
24
|
+
* const orchestrator = new OpenMultiAgent()
|
|
25
|
+
* const team = orchestrator.createTeam('writers', {
|
|
26
|
+
* name: 'writers',
|
|
27
|
+
* agents: [
|
|
28
|
+
* { name: 'researcher', model: 'claude-opus-4-6', systemPrompt: 'You research topics thoroughly.' },
|
|
29
|
+
* { name: 'writer', model: 'claude-opus-4-6', systemPrompt: 'You write clear documentation.' },
|
|
30
|
+
* ],
|
|
31
|
+
* sharedMemory: true,
|
|
32
|
+
* })
|
|
33
|
+
* const result = await orchestrator.runTeam(team, 'Write a guide on TypeScript generics.')
|
|
34
|
+
* console.log(result.agentResults.get('coordinator')?.output)
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* ### Custom tools
|
|
38
|
+
* ```ts
|
|
39
|
+
* import { z } from 'zod'
|
|
40
|
+
*
|
|
41
|
+
* const myTool = defineTool({
|
|
42
|
+
* name: 'fetch_data',
|
|
43
|
+
* description: 'Fetch JSON data from a URL.',
|
|
44
|
+
* inputSchema: z.object({ url: z.string().url() }),
|
|
45
|
+
* execute: async ({ url }) => {
|
|
46
|
+
* const res = await fetch(url)
|
|
47
|
+
* return { data: await res.text() }
|
|
48
|
+
* },
|
|
49
|
+
* })
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
export { OpenMultiAgent } from './orchestrator/orchestrator.js';
|
|
53
|
+
export { Scheduler } from './orchestrator/scheduler.js';
|
|
54
|
+
export type { SchedulingStrategy } from './orchestrator/scheduler.js';
|
|
55
|
+
export { Agent } from './agent/agent.js';
|
|
56
|
+
export { AgentPool, Semaphore } from './agent/pool.js';
|
|
57
|
+
export type { PoolStatus } from './agent/pool.js';
|
|
58
|
+
export { Team } from './team/team.js';
|
|
59
|
+
export { MessageBus } from './team/messaging.js';
|
|
60
|
+
export type { Message } from './team/messaging.js';
|
|
61
|
+
export { TaskQueue } from './task/queue.js';
|
|
62
|
+
export { createTask, isTaskReady, getTaskDependencyOrder, validateTaskDependencies } from './task/task.js';
|
|
63
|
+
export type { TaskQueueEvent } from './task/queue.js';
|
|
64
|
+
export { defineTool, ToolRegistry, zodToJsonSchema } from './tool/framework.js';
|
|
65
|
+
export { ToolExecutor } from './tool/executor.js';
|
|
66
|
+
export type { ToolExecutorOptions, BatchToolCall } from './tool/executor.js';
|
|
67
|
+
export { registerBuiltInTools, BUILT_IN_TOOLS, bashTool, fileReadTool, fileWriteTool, fileEditTool, grepTool, } from './tool/built-in/index.js';
|
|
68
|
+
export { createAdapter } from './llm/adapter.js';
|
|
69
|
+
export type { SupportedProvider } from './llm/adapter.js';
|
|
70
|
+
export { InMemoryStore } from './memory/store.js';
|
|
71
|
+
export { SharedMemory } from './memory/shared.js';
|
|
72
|
+
export type { TextBlock, ToolUseBlock, ToolResultBlock, ImageBlock, ContentBlock, LLMMessage, LLMResponse, LLMAdapter, LLMChatOptions, LLMStreamOptions, LLMToolDef, TokenUsage, StreamEvent, ToolDefinition, ToolResult, ToolUseContext, AgentInfo, TeamInfo, AgentConfig, AgentState, AgentRunResult, ToolCallRecord, TeamConfig, TeamRunResult, Task, TaskStatus, OrchestratorConfig, OrchestratorEvent, MemoryEntry, MemoryStore, } from './types.js';
|
|
73
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkDG;AAMH,OAAO,EAAE,cAAc,EAAE,MAAM,gCAAgC,CAAA;AAC/D,OAAO,EAAE,SAAS,EAAE,MAAM,6BAA6B,CAAA;AACvD,YAAY,EAAE,kBAAkB,EAAE,MAAM,6BAA6B,CAAA;AAMrE,OAAO,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAA;AACxC,OAAO,EAAE,SA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,UAAU,EACV,aAAa,EAGb,IAAI,EACJ,UAAU,EAGV,kBAAkB,EAClB,iBAAiB,EAGjB,WAAW,EACX,WAAW,GACZ,MAAM,YAAY,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview open-multi-agent — public API surface.
|
|
3
|
+
*
|
|
4
|
+
* Import from `'open-multi-agent'` to access everything you need:
|
|
5
|
+
*
|
|
6
|
+
* ```ts
|
|
7
|
+
* import { OpenMultiAgent, Agent, Team, defineTool } from 'open-multi-agent'
|
|
8
|
+
* ```
|
|
9
|
+
*
|
|
10
|
+
* ## Quickstart
|
|
11
|
+
*
|
|
12
|
+
* ### Single agent
|
|
13
|
+
* ```ts
|
|
14
|
+
* const orchestrator = new OpenMultiAgent({ defaultModel: 'claude-opus-4-6' })
|
|
15
|
+
* const result = await orchestrator.runAgent(
|
|
16
|
+
* { name: 'assistant', model: 'claude-opus-4-6' },
|
|
17
|
+
* 'Explain monads in one paragraph.',
|
|
18
|
+
* )
|
|
19
|
+
* console.log(result.output)
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* ### Multi-agent team (auto-orchestrated)
|
|
23
|
+
* ```ts
|
|
24
|
+
* const orchestrator = new OpenMultiAgent()
|
|
25
|
+
* const team = orchestrator.createTeam('writers', {
|
|
26
|
+
* name: 'writers',
|
|
27
|
+
* agents: [
|
|
28
|
+
* { name: 'researcher', model: 'claude-opus-4-6', systemPrompt: 'You research topics thoroughly.' },
|
|
29
|
+
* { name: 'writer', model: 'claude-opus-4-6', systemPrompt: 'You write clear documentation.' },
|
|
30
|
+
* ],
|
|
31
|
+
* sharedMemory: true,
|
|
32
|
+
* })
|
|
33
|
+
* const result = await orchestrator.runTeam(team, 'Write a guide on TypeScript generics.')
|
|
34
|
+
* console.log(result.agentResults.get('coordinator')?.output)
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* ### Custom tools
|
|
38
|
+
* ```ts
|
|
39
|
+
* import { z } from 'zod'
|
|
40
|
+
*
|
|
41
|
+
* const myTool = defineTool({
|
|
42
|
+
* name: 'fetch_data',
|
|
43
|
+
* description: 'Fetch JSON data from a URL.',
|
|
44
|
+
* inputSchema: z.object({ url: z.string().url() }),
|
|
45
|
+
* execute: async ({ url }) => {
|
|
46
|
+
* const res = await fetch(url)
|
|
47
|
+
* return { data: await res.text() }
|
|
48
|
+
* },
|
|
49
|
+
* })
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
// ---------------------------------------------------------------------------
|
|
53
|
+
// Orchestrator (primary entry point)
|
|
54
|
+
// ---------------------------------------------------------------------------
|
|
55
|
+
export { OpenMultiAgent } from './orchestrator/orchestrator.js';
|
|
56
|
+
export { Scheduler } from './orchestrator/scheduler.js';
|
|
57
|
+
// ---------------------------------------------------------------------------
|
|
58
|
+
// Agent layer
|
|
59
|
+
// ---------------------------------------------------------------------------
|
|
60
|
+
export { Agent } from './agent/agent.js';
|
|
61
|
+
export { AgentPool, Semaphore } from './agent/pool.js';
|
|
62
|
+
// ---------------------------------------------------------------------------
|
|
63
|
+
// Team layer
|
|
64
|
+
// ---------------------------------------------------------------------------
|
|
65
|
+
export { Team } from './team/team.js';
|
|
66
|
+
export { MessageBus } from './team/messaging.js';
|
|
67
|
+
// ---------------------------------------------------------------------------
|
|
68
|
+
// Task layer
|
|
69
|
+
// ---------------------------------------------------------------------------
|
|
70
|
+
export { TaskQueue } from './task/queue.js';
|
|
71
|
+
export { createTask, isTaskReady, getTaskDependencyOrder, validateTaskDependencies } from './task/task.js';
|
|
72
|
+
// ---------------------------------------------------------------------------
|
|
73
|
+
// Tool system
|
|
74
|
+
// ---------------------------------------------------------------------------
|
|
75
|
+
export { defineTool, ToolRegistry, zodToJsonSchema } from './tool/framework.js';
|
|
76
|
+
export { ToolExecutor } from './tool/executor.js';
|
|
77
|
+
export { registerBuiltInTools, BUILT_IN_TOOLS, bashTool, fileReadTool, fileWriteTool, fileEditTool, grepTool, } from './tool/built-in/index.js';
|
|
78
|
+
// ---------------------------------------------------------------------------
|
|
79
|
+
// LLM adapters
|
|
80
|
+
// ---------------------------------------------------------------------------
|
|
81
|
+
export { createAdapter } from './llm/adapter.js';
|
|
82
|
+
// ---------------------------------------------------------------------------
|
|
83
|
+
// Memory
|
|
84
|
+
// ---------------------------------------------------------------------------
|
|
85
|
+
export { InMemoryStore } from './memory/store.js';
|
|
86
|
+
export { SharedMemory } from './memory/shared.js';
|
|
87
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkDG;AAEH,8EAA8E;AAC9E,qCAAqC;AACrC,8EAA8E;AAE9E,OAAO,EAAE,cAAc,EAAE,MAAM,gCAAgC,CAAA;AAC/D,OAAO,EAAE,SAAS,EAAE,MAAM,6BAA6B,CAAA;AAGvD,8EAA8E;AAC9E,cAAc;AACd,8EAA8E;AAE9E,OAAO,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAA;AACxC,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAGtD,8EAA8E;AAC9E,aAAa;AACb,8EAA8E;AAE9E,OAAO,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAA;AACrC,OAAO,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAA;AAGhD,8EAA8E;AAC9E,aAAa;AACb,8EAA8E;AAE9E,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAC3C,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,sBAAsB,EAAE,wBAAwB,EAAE,MAAM,gBAAgB,CAAA;AAG1G,8EAA8E;AAC9E,cAAc;AACd,8EAA8E;AAE9E,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAA;AAC/E,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAA;AAEjD,OAAO,EACL,oBAAoB,EACpB,cAAc,EACd,QAAQ,EACR,YAAY,EACZ,aAAa,EACb,YAAY,EACZ,QAAQ,GACT,MAAM,0BAA0B,CAAA;AAEjC,8EAA8E;AAC9E,eAAe;AACf,8EAA8E;AAE9E,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAA;AAGhD,8EAA8E;AAC9E,SAAS;AACT,8EAA8E;AAE9E,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAA;AACjD,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAA"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview LLM adapter factory.
|
|
3
|
+
*
|
|
4
|
+
* Re-exports the {@link LLMAdapter} interface and provides a
|
|
5
|
+
* {@link createAdapter} factory that returns the correct concrete
|
|
6
|
+
* implementation based on the requested provider.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* import { createAdapter } from './adapter.js'
|
|
11
|
+
*
|
|
12
|
+
* const anthropic = createAdapter('anthropic')
|
|
13
|
+
* const openai = createAdapter('openai', process.env.OPENAI_API_KEY)
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export type { LLMAdapter, LLMChatOptions, LLMStreamOptions, LLMToolDef, LLMMessage, LLMResponse, StreamEvent, TokenUsage, ContentBlock, TextBlock, ToolUseBlock, ToolResultBlock, ImageBlock, } from '../types.js';
|
|
17
|
+
import type { LLMAdapter } from '../types.js';
|
|
18
|
+
/**
|
|
19
|
+
* The set of LLM providers supported out of the box.
|
|
20
|
+
* Additional providers can be integrated by implementing {@link LLMAdapter}
|
|
21
|
+
* directly and bypassing this factory.
|
|
22
|
+
*/
|
|
23
|
+
export type SupportedProvider = 'anthropic' | 'openai';
|
|
24
|
+
/**
|
|
25
|
+
* Instantiate the appropriate {@link LLMAdapter} for the given provider.
|
|
26
|
+
*
|
|
27
|
+
* API keys fall back to the standard environment variables
|
|
28
|
+
* (`ANTHROPIC_API_KEY` / `OPENAI_API_KEY`) when not supplied explicitly.
|
|
29
|
+
*
|
|
30
|
+
* Adapters are imported lazily so that projects using only one provider
|
|
31
|
+
* are not forced to install the SDK for the other.
|
|
32
|
+
*
|
|
33
|
+
* @param provider - Which LLM provider to target.
|
|
34
|
+
* @param apiKey - Optional API key override; falls back to env var.
|
|
35
|
+
* @throws {Error} When the provider string is not recognised.
|
|
36
|
+
*/
|
|
37
|
+
export declare function createAdapter(provider: SupportedProvider, apiKey?: string): Promise<LLMAdapter>;
|
|
38
|
+
//# sourceMappingURL=adapter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"adapter.d.ts","sourceRoot":"","sources":["../../src/llm/adapter.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,YAAY,EACV,UAAU,EACV,cAAc,EACd,gBAAgB,EAChB,UAAU,EACV,UAAU,EACV,WAAW,EACX,WAAW,EACX,UAAU,EACV,YAAY,EACZ,SAAS,EACT,YAAY,EACZ,eAAe,EACf,UAAU,GACX,MAAM,aAAa,CAAA;AAEpB,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AAE7C;;;;GAIG;AACH,MAAM,MAAM,iBAAiB,GAAG,WAAW,GAAG,QAAQ,CAAA;AAEtD;;;;;;;;;;;;GAYG;AACH,wBAAsB,aAAa,CACjC,QAAQ,EAAE,iBAAiB,EAC3B,MAAM,CAAC,EAAE,MAAM,GACd,OAAO,CAAC,UAAU,CAAC,CAgBrB"}
|