@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,126 +0,0 @@
1
- /**
2
- * @fileoverview Structured output utilities for agent responses.
3
- *
4
- * Provides JSON extraction, Zod validation, and system-prompt injection so
5
- * that agents can return typed, schema-validated output.
6
- */
7
-
8
- import { type ZodSchema } from 'zod'
9
- import { zodToJsonSchema } from '../tool/framework.js'
10
-
11
- // ---------------------------------------------------------------------------
12
- // System-prompt instruction builder
13
- // ---------------------------------------------------------------------------
14
-
15
- /**
16
- * Build a JSON-mode instruction block to append to the agent's system prompt.
17
- *
18
- * Converts the Zod schema to JSON Schema and formats it as a clear directive
19
- * for the LLM to respond with valid JSON matching the schema.
20
- */
21
- export function buildStructuredOutputInstruction(schema: ZodSchema): string {
22
- const jsonSchema = zodToJsonSchema(schema)
23
- return [
24
- '',
25
- '## Output Format (REQUIRED)',
26
- 'You MUST respond with ONLY valid JSON that conforms to the following JSON Schema.',
27
- 'Do NOT include any text, markdown fences, or explanation outside the JSON object.',
28
- 'Do NOT wrap the JSON in ```json code fences.',
29
- '',
30
- '```',
31
- JSON.stringify(jsonSchema, null, 2),
32
- '```',
33
- ].join('\n')
34
- }
35
-
36
- // ---------------------------------------------------------------------------
37
- // JSON extraction
38
- // ---------------------------------------------------------------------------
39
-
40
- /**
41
- * Attempt to extract and parse JSON from the agent's raw text output.
42
- *
43
- * Handles three cases in order:
44
- * 1. The output is already valid JSON (ideal case)
45
- * 2. The output contains a ` ```json ` fenced block
46
- * 3. The output contains a bare JSON object/array (first `{`/`[` to last `}`/`]`)
47
- *
48
- * @throws {Error} when no valid JSON can be extracted
49
- */
50
- export function extractJSON(raw: string): unknown {
51
- const trimmed = raw.trim()
52
-
53
- // Case 1: Direct parse
54
- try {
55
- return JSON.parse(trimmed)
56
- } catch {
57
- // Continue to fallback strategies
58
- }
59
-
60
- // Case 2a: Prefer ```json tagged fence
61
- const jsonFenceMatch = trimmed.match(/```json\s*([\s\S]*?)```/)
62
- if (jsonFenceMatch?.[1]) {
63
- try {
64
- return JSON.parse(jsonFenceMatch[1].trim())
65
- } catch {
66
- // Continue
67
- }
68
- }
69
-
70
- // Case 2b: Fall back to bare ``` fence
71
- const bareFenceMatch = trimmed.match(/```\s*([\s\S]*?)```/)
72
- if (bareFenceMatch?.[1]) {
73
- try {
74
- return JSON.parse(bareFenceMatch[1].trim())
75
- } catch {
76
- // Continue
77
- }
78
- }
79
-
80
- // Case 3: Find first { to last } (object)
81
- const objStart = trimmed.indexOf('{')
82
- const objEnd = trimmed.lastIndexOf('}')
83
- if (objStart !== -1 && objEnd > objStart) {
84
- try {
85
- return JSON.parse(trimmed.slice(objStart, objEnd + 1))
86
- } catch {
87
- // Fall through
88
- }
89
- }
90
-
91
- // Case 3b: Find first [ to last ] (array)
92
- const arrStart = trimmed.indexOf('[')
93
- const arrEnd = trimmed.lastIndexOf(']')
94
- if (arrStart !== -1 && arrEnd > arrStart) {
95
- try {
96
- return JSON.parse(trimmed.slice(arrStart, arrEnd + 1))
97
- } catch {
98
- // Fall through
99
- }
100
- }
101
-
102
- throw new Error(
103
- `Failed to extract JSON from output. Raw output begins with: "${trimmed.slice(0, 100)}"`,
104
- )
105
- }
106
-
107
- // ---------------------------------------------------------------------------
108
- // Zod validation
109
- // ---------------------------------------------------------------------------
110
-
111
- /**
112
- * Validate a parsed JSON value against a Zod schema.
113
- *
114
- * @returns The validated (and potentially transformed) value on success.
115
- * @throws {Error} with a human-readable Zod error message on failure.
116
- */
117
- export function validateOutput(schema: ZodSchema, data: unknown): unknown {
118
- const result = schema.safeParse(data)
119
- if (result.success) {
120
- return result.data
121
- }
122
- const issues = result.error.issues
123
- .map(issue => ` - ${issue.path.length > 0 ? issue.path.join('.') : '(root)'}: ${issue.message}`)
124
- .join('\n')
125
- throw new Error(`Output validation failed:\n${issues}`)
126
- }
package/src/index.ts DELETED
@@ -1,167 +0,0 @@
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
- // ---------------------------------------------------------------------------
54
- // Orchestrator (primary entry point)
55
- // ---------------------------------------------------------------------------
56
-
57
- export { OpenMultiAgent, executeWithRetry, computeRetryDelay } from './orchestrator/orchestrator.js'
58
- export { Scheduler } from './orchestrator/scheduler.js'
59
- export type { SchedulingStrategy } from './orchestrator/scheduler.js'
60
-
61
- // ---------------------------------------------------------------------------
62
- // Agent layer
63
- // ---------------------------------------------------------------------------
64
-
65
- export { Agent } from './agent/agent.js'
66
- export { buildStructuredOutputInstruction, extractJSON, validateOutput } from './agent/structured-output.js'
67
- export { AgentPool, Semaphore } from './agent/pool.js'
68
- export type { PoolStatus } from './agent/pool.js'
69
-
70
- // ---------------------------------------------------------------------------
71
- // Team layer
72
- // ---------------------------------------------------------------------------
73
-
74
- export { Team } from './team/team.js'
75
- export { MessageBus } from './team/messaging.js'
76
- export type { Message } from './team/messaging.js'
77
-
78
- // ---------------------------------------------------------------------------
79
- // Task layer
80
- // ---------------------------------------------------------------------------
81
-
82
- export { TaskQueue } from './task/queue.js'
83
- export { createTask, isTaskReady, getTaskDependencyOrder, validateTaskDependencies } from './task/task.js'
84
- export type { TaskQueueEvent } from './task/queue.js'
85
-
86
- // ---------------------------------------------------------------------------
87
- // Tool system
88
- // ---------------------------------------------------------------------------
89
-
90
- export { defineTool, ToolRegistry, zodToJsonSchema } from './tool/framework.js'
91
- export { ToolExecutor } from './tool/executor.js'
92
- export type { ToolExecutorOptions, BatchToolCall } from './tool/executor.js'
93
- export {
94
- registerBuiltInTools,
95
- BUILT_IN_TOOLS,
96
- bashTool,
97
- fileReadTool,
98
- fileWriteTool,
99
- fileEditTool,
100
- grepTool,
101
- } from './tool/built-in/index.js'
102
-
103
- // ---------------------------------------------------------------------------
104
- // LLM adapters
105
- // ---------------------------------------------------------------------------
106
-
107
- export { createAdapter } from './llm/adapter.js'
108
- export type { SupportedProvider } from './llm/adapter.js'
109
-
110
- // ---------------------------------------------------------------------------
111
- // Memory
112
- // ---------------------------------------------------------------------------
113
-
114
- export { InMemoryStore } from './memory/store.js'
115
- export { SharedMemory } from './memory/shared.js'
116
-
117
- // ---------------------------------------------------------------------------
118
- // Types — all public interfaces re-exported for consumer type-checking
119
- // ---------------------------------------------------------------------------
120
-
121
- export type {
122
- // Content blocks
123
- TextBlock,
124
- ToolUseBlock,
125
- ToolResultBlock,
126
- ImageBlock,
127
- ContentBlock,
128
-
129
- // LLM
130
- LLMMessage,
131
- LLMResponse,
132
- LLMAdapter,
133
- LLMChatOptions,
134
- LLMStreamOptions,
135
- LLMToolDef,
136
- TokenUsage,
137
- StreamEvent,
138
-
139
- // Tools
140
- ToolDefinition,
141
- ToolResult,
142
- ToolUseContext,
143
- AgentInfo,
144
- TeamInfo,
145
-
146
- // Agent
147
- AgentConfig,
148
- AgentState,
149
- AgentRunResult,
150
- ToolCallRecord,
151
-
152
- // Team
153
- TeamConfig,
154
- TeamRunResult,
155
-
156
- // Task
157
- Task,
158
- TaskStatus,
159
-
160
- // Orchestrator
161
- OrchestratorConfig,
162
- OrchestratorEvent,
163
-
164
- // Memory
165
- MemoryEntry,
166
- MemoryStore,
167
- } from './types.js'
@@ -1,87 +0,0 @@
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
-
17
- export type {
18
- LLMAdapter,
19
- LLMChatOptions,
20
- LLMStreamOptions,
21
- LLMToolDef,
22
- LLMMessage,
23
- LLMResponse,
24
- StreamEvent,
25
- TokenUsage,
26
- ContentBlock,
27
- TextBlock,
28
- ToolUseBlock,
29
- ToolResultBlock,
30
- ImageBlock,
31
- } from '../types.js'
32
-
33
- import type { LLMAdapter } from '../types.js'
34
-
35
- /**
36
- * The set of LLM providers supported out of the box.
37
- * Additional providers can be integrated by implementing {@link LLMAdapter}
38
- * directly and bypassing this factory.
39
- */
40
- export type SupportedProvider = 'anthropic' | 'copilot' | 'openai'
41
-
42
- /**
43
- * Instantiate the appropriate {@link LLMAdapter} for the given provider.
44
- *
45
- * API keys fall back to the standard environment variables when not supplied
46
- * explicitly:
47
- * - `anthropic` → `ANTHROPIC_API_KEY`
48
- * - `openai` → `OPENAI_API_KEY`
49
- * - `copilot` → `GITHUB_COPILOT_TOKEN` / `GITHUB_TOKEN`, or interactive
50
- * OAuth2 device flow if neither is set
51
- *
52
- * Adapters are imported lazily so that projects using only one provider
53
- * are not forced to install the SDK for the other.
54
- *
55
- * @param provider - Which LLM provider to target.
56
- * @param apiKey - Optional API key override; falls back to env var.
57
- * @param baseURL - Optional base URL for OpenAI-compatible APIs (Ollama, vLLM, etc.).
58
- * @throws {Error} When the provider string is not recognised.
59
- */
60
- export async function createAdapter(
61
- provider: SupportedProvider,
62
- apiKey?: string,
63
- baseURL?: string,
64
- ): Promise<LLMAdapter> {
65
- switch (provider) {
66
- case 'anthropic': {
67
- const { AnthropicAdapter } = await import('./anthropic.js')
68
- return new AnthropicAdapter(apiKey, baseURL)
69
- }
70
- case 'copilot': {
71
- if (baseURL) {
72
- console.warn('[open-multi-agent] baseURL is not supported for the copilot provider and will be ignored.')
73
- }
74
- const { CopilotAdapter } = await import('./copilot.js')
75
- return new CopilotAdapter(apiKey)
76
- }
77
- case 'openai': {
78
- const { OpenAIAdapter } = await import('./openai.js')
79
- return new OpenAIAdapter(apiKey, baseURL)
80
- }
81
- default: {
82
- // The `never` cast here makes TypeScript enforce exhaustiveness.
83
- const _exhaustive: never = provider
84
- throw new Error(`Unsupported LLM provider: ${String(_exhaustive)}`)
85
- }
86
- }
87
- }