Compare commits
8 Commits
34b911825a
...
c3ead26677
| Author | SHA1 | Date |
|---|---|---|
|
|
c3ead26677 | |
|
|
d78af2787f | |
|
|
cd57c2ee31 | |
|
|
b6ee63bda0 | |
|
|
1e154b22fd | |
|
|
0485bfb82f | |
|
|
fa4533e8d0 | |
|
|
5305cb2321 |
45
README.md
45
README.md
|
|
@ -17,7 +17,7 @@ CrewAI is Python. LangGraph makes you draw the graph by hand. `open-multi-agent`
|
|||
|
||||
- **Goal to result in one call.** `runTeam(team, "Build a REST API")` kicks off a coordinator agent that decomposes the goal into a task DAG, resolves dependencies, runs independent tasks in parallel, and synthesizes the final output. No graph to draw, no tasks to wire up.
|
||||
- **TypeScript-native, three runtime dependencies.** `@anthropic-ai/sdk`, `openai`, `zod`. That is the whole runtime. Embed in Express, Next.js, serverless functions, or CI/CD pipelines. No Python runtime, no subprocess bridge, no cloud sidecar.
|
||||
- **Multi-model teams.** Claude, GPT, Gemini, Grok, Copilot, or any OpenAI-compatible local model (Ollama, vLLM, LM Studio, llama.cpp) in the same team. Run the architect on Opus 4.6, the developer on GPT-5.4, the reviewer on local Gemma 4, all in one `runTeam()` call. Gemini ships as an optional peer dependency: `npm install @google/genai` to enable.
|
||||
- **Multi-model teams.** Claude, GPT, Gemini, Grok, MiniMax, DeepSeek, Copilot, or any OpenAI-compatible local model (Ollama, vLLM, LM Studio, llama.cpp) in the same team. Run the architect on Opus 4.6, the developer on GPT-5.4, the reviewer on local Gemma 4, all in one `runTeam()` call. Gemini ships as an optional peer dependency: `npm install @google/genai` to enable.
|
||||
|
||||
Other features (MCP integration, context strategies, structured output, task retry, human-in-the-loop, lifecycle hooks, loop detection, observability) live below the fold and in [`examples/`](./examples/).
|
||||
|
||||
|
|
@ -72,6 +72,9 @@ Set the API key for your provider. Local models via Ollama require no API key
|
|||
- `OPENAI_API_KEY`
|
||||
- `GEMINI_API_KEY`
|
||||
- `XAI_API_KEY` (for Grok)
|
||||
- `MINIMAX_API_KEY` (for MiniMax)
|
||||
- `MINIMAX_BASE_URL` (for MiniMax — optional, selects endpoint)
|
||||
- `DEEPSEEK_API_KEY` (for DeepSeek)
|
||||
- `GITHUB_TOKEN` (for Copilot)
|
||||
|
||||
**CLI (`oma`).** For shell and CI, the package exposes a JSON-first binary. See [docs/cli.md](./docs/cli.md) for `oma run`, `oma task`, `oma provider`, exit codes, and file formats.
|
||||
|
|
@ -139,12 +142,14 @@ For MapReduce-style fan-out without task dependencies, use `AgentPool.runParalle
|
|||
|
||||
## Examples
|
||||
|
||||
16 runnable scripts and 1 full-stack demo in [`examples/`](./examples/). Start with these:
|
||||
18 runnable scripts and 1 full-stack demo in [`examples/`](./examples/). Start with these:
|
||||
|
||||
- [02 — Team Collaboration](examples/02-team-collaboration.ts): `runTeam()` coordinator pattern.
|
||||
- [06 — Local Model](examples/06-local-model.ts): Ollama and Claude in one pipeline via `baseURL`.
|
||||
- [09 — Structured Output](examples/09-structured-output.ts): any agent returns Zod-validated JSON.
|
||||
- [11 — Trace Observability](examples/11-trace-observability.ts): `onTrace` spans for LLM calls, tools, and tasks.
|
||||
- [17 — MiniMax](examples/17-minimax.ts): three-agent team using MiniMax M2.7.
|
||||
- [18 — DeepSeek](examples/18-deepseek.ts): three-agent team using DeepSeek Chat.
|
||||
- [with-vercel-ai-sdk](examples/with-vercel-ai-sdk/): Next.js app — OMA `runTeam()` + AI SDK `useChat` streaming.
|
||||
|
||||
Run scripts with `npx tsx examples/02-team-collaboration.ts`.
|
||||
|
|
@ -183,6 +188,8 @@ Run scripts with `npx tsx examples/02-team-collaboration.ts`.
|
|||
│ │ - CopilotAdapter │
|
||||
│ │ - GeminiAdapter │
|
||||
│ │ - GrokAdapter │
|
||||
│ │ - MiniMaxAdapter │
|
||||
│ │ - DeepSeekAdapter │
|
||||
│ └──────────────────────┘
|
||||
┌────────▼──────────┐
|
||||
│ AgentRunner │ ┌──────────────────────┐
|
||||
|
|
@ -282,6 +289,9 @@ Notes:
|
|||
| Anthropic (Claude) | `provider: 'anthropic'` | `ANTHROPIC_API_KEY` | Verified |
|
||||
| OpenAI (GPT) | `provider: 'openai'` | `OPENAI_API_KEY` | Verified |
|
||||
| Grok (xAI) | `provider: 'grok'` | `XAI_API_KEY` | Verified |
|
||||
| MiniMax (global) | `provider: 'minimax'` | `MINIMAX_API_KEY` | Verified |
|
||||
| MiniMax (China) | `provider: 'minimax'` + `MINIMAX_BASE_URL` | `MINIMAX_API_KEY` | Verified |
|
||||
| DeepSeek | `provider: 'deepseek'` | `DEEPSEEK_API_KEY` | Verified |
|
||||
| GitHub Copilot | `provider: 'copilot'` | `GITHUB_TOKEN` | Verified |
|
||||
| Gemini | `provider: 'gemini'` | `GEMINI_API_KEY` | Verified |
|
||||
| Ollama / vLLM / LM Studio | `provider: 'openai'` + `baseURL` | — | Verified |
|
||||
|
|
@ -291,7 +301,7 @@ Gemini requires `npm install @google/genai` (optional peer dependency).
|
|||
|
||||
Verified local models with tool-calling: **Gemma 4** (see [example 08](examples/08-gemma4-local.ts)).
|
||||
|
||||
Any OpenAI-compatible API should work via `provider: 'openai'` + `baseURL` (DeepSeek, Groq, Mistral, Qwen, MiniMax, etc.). **Grok now has first-class support** via `provider: 'grok'`.
|
||||
Any OpenAI-compatible API should work via `provider: 'openai'` + `baseURL` (Groq, Mistral, Qwen, etc.). **Grok, MiniMax, and DeepSeek now have first-class support** via `provider: 'grok'`, `provider: 'minimax'`, and `provider: 'deepseek'`.
|
||||
|
||||
### Local Model Tool-Calling
|
||||
|
||||
|
|
@ -331,7 +341,34 @@ const grokAgent: AgentConfig = {
|
|||
}
|
||||
```
|
||||
|
||||
(Set your `XAI_API_KEY` environment variable — no `baseURL` needed anymore.)
|
||||
(Set your `XAI_API_KEY` environment variable — no `baseURL` needed.)
|
||||
|
||||
```typescript
|
||||
const minimaxAgent: AgentConfig = {
|
||||
name: 'minimax-agent',
|
||||
provider: 'minimax',
|
||||
model: 'MiniMax-M2.7',
|
||||
systemPrompt: 'You are a helpful assistant.',
|
||||
}
|
||||
```
|
||||
|
||||
Set `MINIMAX_API_KEY`. The adapter selects the endpoint via `MINIMAX_BASE_URL`:
|
||||
|
||||
- `https://api.minimax.io/v1` Global, default
|
||||
- `https://api.minimaxi.com/v1` China mainland endpoint
|
||||
|
||||
You can also pass `baseURL` directly in `AgentConfig` to override the env var.
|
||||
|
||||
```typescript
|
||||
const deepseekAgent: AgentConfig = {
|
||||
name: 'deepseek-agent',
|
||||
provider: 'deepseek',
|
||||
model: 'deepseek-chat',
|
||||
systemPrompt: 'You are a helpful assistant.',
|
||||
}
|
||||
```
|
||||
|
||||
Set `DEEPSEEK_API_KEY`. Available models: `deepseek-chat` (DeepSeek-V3, recommended for coding) and `deepseek-reasoner` (thinking mode).
|
||||
|
||||
## Contributing
|
||||
|
||||
|
|
|
|||
43
README_zh.md
43
README_zh.md
|
|
@ -17,7 +17,7 @@ CrewAI 是 Python。LangGraph 需要你自己画图。`open-multi-agent` 是你
|
|||
|
||||
- **一次调用从目标到结果。** `runTeam(team, "构建一个 REST API")` 启动一个协调者 agent,把目标拆成任务 DAG,解析依赖,独立任务并行执行,最终合成输出。不需要画图,不需要手动连任务。
|
||||
- **TypeScript 原生,3 个运行时依赖。** `@anthropic-ai/sdk`、`openai`、`zod`。这就是全部运行时。可嵌入 Express、Next.js、Serverless 函数或 CI/CD 流水线。没有 Python 运行时,没有子进程桥接,没有云端 sidecar。
|
||||
- **多模型团队。** Claude、GPT、Gemini、Grok、Copilot,或任何 OpenAI 兼容的本地模型(Ollama、vLLM、LM Studio、llama.cpp)可以在同一个团队中使用。让架构师用 Opus 4.6,开发者用 GPT-5.4,评审用本地的 Gemma 4,一次 `runTeam()` 调用全部搞定。Gemini 作为 optional peer dependency 提供:使用前需 `npm install @google/genai`。
|
||||
- **多模型团队。** Claude、GPT、Gemini、Grok、MiniMax、DeepSeek、Copilot,或任何 OpenAI 兼容的本地模型(Ollama、vLLM、LM Studio、llama.cpp)可以在同一个团队中使用。让架构师用 Opus 4.6,开发者用 GPT-5.4,评审用本地的 Gemma 4,一次 `runTeam()` 调用全部搞定。Gemini 作为 optional peer dependency 提供:使用前需 `npm install @google/genai`。
|
||||
|
||||
其他能力(MCP 集成、上下文策略、结构化输出、任务重试、人机协同、生命周期钩子、循环检测、可观测性)在下方章节和 [`examples/`](./examples/) 里。
|
||||
|
||||
|
|
@ -72,6 +72,9 @@ npm install @jackchen_me/open-multi-agent
|
|||
- `OPENAI_API_KEY`
|
||||
- `GEMINI_API_KEY`
|
||||
- `XAI_API_KEY`(Grok)
|
||||
- `MINIMAX_API_KEY`(MiniMax)
|
||||
- `MINIMAX_BASE_URL`(MiniMax — 可选,用于选择接入端点)
|
||||
- `DEEPSEEK_API_KEY`(DeepSeek)
|
||||
- `GITHUB_TOKEN`(Copilot)
|
||||
|
||||
三个智能体,一个目标——框架处理剩下的一切:
|
||||
|
|
@ -137,12 +140,14 @@ Tokens: 12847 output tokens
|
|||
|
||||
## 示例
|
||||
|
||||
[`examples/`](./examples/) 里有 16 个可运行脚本和 1 个完整项目。推荐从这几个开始:
|
||||
[`examples/`](./examples/) 里有 18 个可运行脚本和 1 个完整项目。推荐从这几个开始:
|
||||
|
||||
- [02 — 团队协作](examples/02-team-collaboration.ts):`runTeam()` 协调者模式。
|
||||
- [06 — 本地模型](examples/06-local-model.ts):通过 `baseURL` 把 Ollama 和 Claude 放在同一条管线。
|
||||
- [09 — 结构化输出](examples/09-structured-output.ts):任意 agent 产出 Zod 校验过的 JSON。
|
||||
- [11 — 可观测性](examples/11-trace-observability.ts):`onTrace` 回调,为 LLM 调用、工具、任务发出结构化 span。
|
||||
- [17 — MiniMax](examples/17-minimax.ts):使用 MiniMax M2.7 的三智能体团队。
|
||||
- [18 — DeepSeek](examples/18-deepseek.ts):使用 DeepSeek Chat 的三智能体团队。
|
||||
- [with-vercel-ai-sdk](examples/with-vercel-ai-sdk/):Next.js 应用 — OMA `runTeam()` + AI SDK `useChat` 流式输出。
|
||||
|
||||
用 `npx tsx examples/02-team-collaboration.ts` 运行脚本示例。
|
||||
|
|
@ -181,6 +186,8 @@ Tokens: 12847 output tokens
|
|||
│ │ - CopilotAdapter │
|
||||
│ │ - GeminiAdapter │
|
||||
│ │ - GrokAdapter │
|
||||
│ │ - MiniMaxAdapter │
|
||||
│ │ - DeepSeekAdapter │
|
||||
│ └──────────────────────┘
|
||||
┌────────▼──────────┐
|
||||
│ AgentRunner │ ┌──────────────────────┐
|
||||
|
|
@ -256,6 +263,9 @@ const customAgent: AgentConfig = {
|
|||
| Anthropic (Claude) | `provider: 'anthropic'` | `ANTHROPIC_API_KEY` | 已验证 |
|
||||
| OpenAI (GPT) | `provider: 'openai'` | `OPENAI_API_KEY` | 已验证 |
|
||||
| Grok (xAI) | `provider: 'grok'` | `XAI_API_KEY` | 已验证 |
|
||||
| MiniMax(全球) | `provider: 'minimax'` | `MINIMAX_API_KEY` | 已验证 |
|
||||
| MiniMax(国内) | `provider: 'minimax'` + `MINIMAX_BASE_URL` | `MINIMAX_API_KEY` | 已验证 |
|
||||
| DeepSeek | `provider: 'deepseek'` | `DEEPSEEK_API_KEY` | 已验证 |
|
||||
| GitHub Copilot | `provider: 'copilot'` | `GITHUB_TOKEN` | 已验证 |
|
||||
| Gemini | `provider: 'gemini'` | `GEMINI_API_KEY` | 已验证 |
|
||||
| Ollama / vLLM / LM Studio | `provider: 'openai'` + `baseURL` | — | 已验证 |
|
||||
|
|
@ -265,7 +275,7 @@ Gemini 需要 `npm install @google/genai`(optional peer dependency)。
|
|||
|
||||
已验证支持 tool-calling 的本地模型:**Gemma 4**(见[示例 08](examples/08-gemma4-local.ts))。
|
||||
|
||||
任何 OpenAI 兼容 API 均可通过 `provider: 'openai'` + `baseURL` 接入(DeepSeek、Groq、Mistral、Qwen、MiniMax 等)。**Grok 现已原生支持**,使用 `provider: 'grok'`。
|
||||
任何 OpenAI 兼容 API 均可通过 `provider: 'openai'` + `baseURL` 接入(Groq、Mistral、Qwen 等)。**Grok、MiniMax 和 DeepSeek 现已原生支持**,分别使用 `provider: 'grok'`、`provider: 'minimax'` 和 `provider: 'deepseek'`。
|
||||
|
||||
### 本地模型 Tool-Calling
|
||||
|
||||
|
|
@ -307,6 +317,33 @@ const grokAgent: AgentConfig = {
|
|||
|
||||
(设置 `XAI_API_KEY` 环境变量即可,无需 `baseURL`。)
|
||||
|
||||
```typescript
|
||||
const minimaxAgent: AgentConfig = {
|
||||
name: 'minimax-agent',
|
||||
provider: 'minimax',
|
||||
model: 'MiniMax-M2.7',
|
||||
systemPrompt: 'You are a helpful assistant.',
|
||||
}
|
||||
```
|
||||
|
||||
设置 `MINIMAX_API_KEY`。适配器通过 `MINIMAX_BASE_URL` 选择接入端点:
|
||||
|
||||
- `https://api.minimax.io/v1` 全球端点,默认
|
||||
- `https://api.minimaxi.com/v1` 中国大陆端点
|
||||
|
||||
也可在 `AgentConfig` 中直接传入 `baseURL` 覆盖环境变量。
|
||||
|
||||
```typescript
|
||||
const deepseekAgent: AgentConfig = {
|
||||
name: 'deepseek-agent',
|
||||
provider: 'deepseek',
|
||||
model: 'deepseek-chat',
|
||||
systemPrompt: '你是一个有用的助手。',
|
||||
}
|
||||
```
|
||||
|
||||
设置 `DEEPSEEK_API_KEY`。可用模型:`deepseek-chat`(DeepSeek-V3,推荐用于编码任务)和 `deepseek-reasoner`(思考模式)。
|
||||
|
||||
## 参与贡献
|
||||
|
||||
欢迎提 Issue、功能需求和 PR。以下方向的贡献尤其有价值:
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ npm run build
|
|||
node dist/cli/oma.js help
|
||||
```
|
||||
|
||||
Set the usual provider API keys in the environment (see [README](../README.md#quick-start)); the CLI does not read secrets from flags.
|
||||
Set the usual provider API keys in the environment (see [README](../README.md#quick-start)); the CLI does not read secrets from flags. MiniMax additionally reads `MINIMAX_BASE_URL` to select the global (`https://api.minimax.io/v1`) or China (`https://api.minimaxi.com/v1`) endpoint.
|
||||
|
||||
---
|
||||
|
||||
|
|
@ -55,7 +55,7 @@ Global flags: [`--pretty`](#output-flags), [`--include-messages`](#output-flags)
|
|||
Read-only helper for wiring JSON configs and env vars.
|
||||
|
||||
- **`oma provider`** or **`oma provider list`** — Prints JSON: built-in provider ids, API key environment variable names, whether `baseURL` is supported, and short notes (e.g. OpenAI-compatible servers, Copilot in CI).
|
||||
- **`oma provider template <provider>`** — Prints a JSON object with example `orchestrator` and `agent` fields plus placeholder `env` entries. `<provider>` is one of: `anthropic`, `openai`, `gemini`, `grok`, `copilot`.
|
||||
- **`oma provider template <provider>`** — Prints a JSON object with example `orchestrator` and `agent` fields plus placeholder `env` entries. `<provider>` is one of: `anthropic`, `openai`, `gemini`, `grok`, `minimax`, `deepseek`, `copilot`.
|
||||
|
||||
Supports `--pretty`.
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,159 @@
|
|||
/**
|
||||
* Example 17 — Multi-Agent Team Collaboration with MiniMax
|
||||
*
|
||||
* Three specialized agents (architect, developer, reviewer) collaborate via `runTeam()`
|
||||
* to build a minimal Express.js REST API. Every agent uses MiniMax's flagship model.
|
||||
*
|
||||
* Run:
|
||||
* npx tsx examples/17-minimax.ts
|
||||
*
|
||||
* Prerequisites:
|
||||
* MINIMAX_API_KEY environment variable must be set.
|
||||
* MINIMAX_BASE_URL environment variable can be set to switch to the China mainland endpoint if needed.
|
||||
*
|
||||
* Endpoints:
|
||||
* Global (default): https://api.minimax.io/v1
|
||||
* China mainland: https://api.minimaxi.com/v1 (set MINIMAX_BASE_URL)
|
||||
*/
|
||||
|
||||
import { OpenMultiAgent } from '../src/index.js'
|
||||
import type { AgentConfig, OrchestratorEvent } from '../src/types.js'
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Agent definitions (all using MiniMax-M2.7)
|
||||
// ---------------------------------------------------------------------------
|
||||
const architect: AgentConfig = {
|
||||
name: 'architect',
|
||||
model: 'MiniMax-M2.7',
|
||||
provider: 'minimax',
|
||||
systemPrompt: `You are a software architect with deep experience in Node.js and REST API design.
|
||||
Your job is to design clear, production-quality API contracts and file/directory structures.
|
||||
Output concise plans in markdown — no unnecessary prose.`,
|
||||
tools: ['bash', 'file_write'],
|
||||
maxTurns: 5,
|
||||
temperature: 0.2,
|
||||
}
|
||||
|
||||
const developer: AgentConfig = {
|
||||
name: 'developer',
|
||||
model: 'MiniMax-M2.7',
|
||||
provider: 'minimax',
|
||||
systemPrompt: `You are a TypeScript/Node.js developer. You implement what the architect specifies.
|
||||
Write clean, runnable code with proper error handling. Use the tools to write files and run tests.`,
|
||||
tools: ['bash', 'file_read', 'file_write', 'file_edit'],
|
||||
maxTurns: 12,
|
||||
temperature: 0.1,
|
||||
}
|
||||
|
||||
const reviewer: AgentConfig = {
|
||||
name: 'reviewer',
|
||||
model: 'MiniMax-M2.7',
|
||||
provider: 'minimax',
|
||||
systemPrompt: `You are a senior code reviewer. Review code for correctness, security, and clarity.
|
||||
Provide a structured review with: LGTM items, suggestions, and any blocking issues.
|
||||
Read files using the tools before reviewing.`,
|
||||
tools: ['bash', 'file_read', 'grep'],
|
||||
maxTurns: 5,
|
||||
temperature: 0.3,
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Progress tracking
|
||||
// ---------------------------------------------------------------------------
|
||||
const startTimes = new Map<string, number>()
|
||||
|
||||
function handleProgress(event: OrchestratorEvent): void {
|
||||
const ts = new Date().toISOString().slice(11, 23) // HH:MM:SS.mmm
|
||||
switch (event.type) {
|
||||
case 'agent_start':
|
||||
startTimes.set(event.agent ?? '', Date.now())
|
||||
console.log(`[${ts}] AGENT START → ${event.agent}`)
|
||||
break
|
||||
case 'agent_complete': {
|
||||
const elapsed = Date.now() - (startTimes.get(event.agent ?? '') ?? Date.now())
|
||||
console.log(`[${ts}] AGENT DONE ← ${event.agent} (${elapsed}ms)`)
|
||||
break
|
||||
}
|
||||
case 'task_start':
|
||||
console.log(`[${ts}] TASK START ↓ ${event.task}`)
|
||||
break
|
||||
case 'task_complete':
|
||||
console.log(`[${ts}] TASK DONE ↑ ${event.task}`)
|
||||
break
|
||||
case 'message':
|
||||
console.log(`[${ts}] MESSAGE • ${event.agent} → (team)`)
|
||||
break
|
||||
case 'error':
|
||||
console.error(`[${ts}] ERROR ✗ agent=${event.agent} task=${event.task}`)
|
||||
if (event.data instanceof Error) console.error(` ${event.data.message}`)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Orchestrate
|
||||
// ---------------------------------------------------------------------------
|
||||
const orchestrator = new OpenMultiAgent({
|
||||
defaultModel: 'MiniMax-M2.7',
|
||||
defaultProvider: 'minimax',
|
||||
maxConcurrency: 1, // sequential for readable output
|
||||
onProgress: handleProgress,
|
||||
})
|
||||
|
||||
const team = orchestrator.createTeam('api-team', {
|
||||
name: 'api-team',
|
||||
agents: [architect, developer, reviewer],
|
||||
sharedMemory: true,
|
||||
maxConcurrency: 1,
|
||||
})
|
||||
|
||||
console.log(`Team "${team.name}" created with agents: ${team.getAgents().map(a => a.name).join(', ')}`)
|
||||
console.log('\nStarting team run...\n')
|
||||
console.log('='.repeat(60))
|
||||
|
||||
const goal = `Create a minimal Express.js REST API in /tmp/express-api/ with:
|
||||
- GET /health → { status: "ok" }
|
||||
- GET /users → returns a hardcoded array of 2 user objects
|
||||
- POST /users → accepts { name, email } body, logs it, returns 201
|
||||
- Proper error handling middleware
|
||||
- The server should listen on port 3001
|
||||
- Include a package.json with the required dependencies`
|
||||
|
||||
const result = await orchestrator.runTeam(team, goal)
|
||||
|
||||
console.log('\n' + '='.repeat(60))
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Results
|
||||
// ---------------------------------------------------------------------------
|
||||
console.log('\nTeam run complete.')
|
||||
console.log(`Success: ${result.success}`)
|
||||
console.log(`Total tokens — input: ${result.totalTokenUsage.input_tokens}, output: ${result.totalTokenUsage.output_tokens}`)
|
||||
|
||||
console.log('\nPer-agent results:')
|
||||
for (const [agentName, agentResult] of result.agentResults) {
|
||||
const status = agentResult.success ? 'OK' : 'FAILED'
|
||||
const tools = agentResult.toolCalls.length
|
||||
console.log(` ${agentName.padEnd(12)} [${status}] tool_calls=${tools}`)
|
||||
if (!agentResult.success) {
|
||||
console.log(` Error: ${agentResult.output.slice(0, 120)}`)
|
||||
}
|
||||
}
|
||||
|
||||
// Sample outputs
|
||||
const developerResult = result.agentResults.get('developer')
|
||||
if (developerResult?.success) {
|
||||
console.log('\nDeveloper output (last 600 chars):')
|
||||
console.log('─'.repeat(60))
|
||||
const out = developerResult.output
|
||||
console.log(out.length > 600 ? '...' + out.slice(-600) : out)
|
||||
console.log('─'.repeat(60))
|
||||
}
|
||||
|
||||
const reviewerResult = result.agentResults.get('reviewer')
|
||||
if (reviewerResult?.success) {
|
||||
console.log('\nReviewer output:')
|
||||
console.log('─'.repeat(60))
|
||||
console.log(reviewerResult.output)
|
||||
console.log('─'.repeat(60))
|
||||
}
|
||||
|
|
@ -0,0 +1,158 @@
|
|||
/**
|
||||
* Example 18 — Multi-Agent Team Collaboration with DeepSeek
|
||||
*
|
||||
* Three specialized agents (architect, developer, reviewer) collaborate via `runTeam()`
|
||||
* to build a minimal Express.js REST API. Every agent uses DeepSeek's flagship model.
|
||||
*
|
||||
* Run:
|
||||
* npx tsx examples/18-deepseek.ts
|
||||
*
|
||||
* Prerequisites:
|
||||
* DEEPSEEK_API_KEY environment variable must be set.
|
||||
*
|
||||
* Available models:
|
||||
* deepseek-chat — DeepSeek-V3 (non-thinking mode, recommended for coding tasks)
|
||||
* deepseek-reasoner — DeepSeek-V3 (thinking mode, for complex reasoning)
|
||||
*/
|
||||
|
||||
import { OpenMultiAgent } from '../src/index.js'
|
||||
import type { AgentConfig, OrchestratorEvent } from '../src/types.js'
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Agent definitions (all using deepseek-chat)
|
||||
// ---------------------------------------------------------------------------
|
||||
const architect: AgentConfig = {
|
||||
name: 'architect',
|
||||
model: 'deepseek-reasoner',
|
||||
provider: 'deepseek',
|
||||
systemPrompt: `You are a software architect with deep experience in Node.js and REST API design.
|
||||
Your job is to design clear, production-quality API contracts and file/directory structures.
|
||||
Output concise plans in markdown — no unnecessary prose.`,
|
||||
tools: ['bash', 'file_write'],
|
||||
maxTurns: 5,
|
||||
temperature: 0.2,
|
||||
}
|
||||
|
||||
const developer: AgentConfig = {
|
||||
name: 'developer',
|
||||
model: 'deepseek-chat',
|
||||
provider: 'deepseek',
|
||||
systemPrompt: `You are a TypeScript/Node.js developer. You implement what the architect specifies.
|
||||
Write clean, runnable code with proper error handling. Use the tools to write files and run tests.`,
|
||||
tools: ['bash', 'file_read', 'file_write', 'file_edit'],
|
||||
maxTurns: 12,
|
||||
temperature: 0.1,
|
||||
}
|
||||
|
||||
const reviewer: AgentConfig = {
|
||||
name: 'reviewer',
|
||||
model: 'deepseek-chat',
|
||||
provider: 'deepseek',
|
||||
systemPrompt: `You are a senior code reviewer. Review code for correctness, security, and clarity.
|
||||
Provide a structured review with: LGTM items, suggestions, and any blocking issues.
|
||||
Read files using the tools before reviewing.`,
|
||||
tools: ['bash', 'file_read', 'grep'],
|
||||
maxTurns: 5,
|
||||
temperature: 0.3,
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Progress tracking
|
||||
// ---------------------------------------------------------------------------
|
||||
const startTimes = new Map<string, number>()
|
||||
|
||||
function handleProgress(event: OrchestratorEvent): void {
|
||||
const ts = new Date().toISOString().slice(11, 23) // HH:MM:SS.mmm
|
||||
switch (event.type) {
|
||||
case 'agent_start':
|
||||
startTimes.set(event.agent ?? '', Date.now())
|
||||
console.log(`[${ts}] AGENT START → ${event.agent}`)
|
||||
break
|
||||
case 'agent_complete': {
|
||||
const elapsed = Date.now() - (startTimes.get(event.agent ?? '') ?? Date.now())
|
||||
console.log(`[${ts}] AGENT DONE ← ${event.agent} (${elapsed}ms)`)
|
||||
break
|
||||
}
|
||||
case 'task_start':
|
||||
console.log(`[${ts}] TASK START ↓ ${event.task}`)
|
||||
break
|
||||
case 'task_complete':
|
||||
console.log(`[${ts}] TASK DONE ↑ ${event.task}`)
|
||||
break
|
||||
case 'message':
|
||||
console.log(`[${ts}] MESSAGE • ${event.agent} → (team)`)
|
||||
break
|
||||
case 'error':
|
||||
console.error(`[${ts}] ERROR ✗ agent=${event.agent} task=${event.task}`)
|
||||
if (event.data instanceof Error) console.error(` ${event.data.message}`)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Orchestrate
|
||||
// ---------------------------------------------------------------------------
|
||||
const orchestrator = new OpenMultiAgent({
|
||||
defaultModel: 'deepseek-chat',
|
||||
defaultProvider: 'deepseek',
|
||||
maxConcurrency: 1, // sequential for readable output
|
||||
onProgress: handleProgress,
|
||||
})
|
||||
|
||||
const team = orchestrator.createTeam('api-team', {
|
||||
name: 'api-team',
|
||||
agents: [architect, developer, reviewer],
|
||||
sharedMemory: true,
|
||||
maxConcurrency: 1,
|
||||
})
|
||||
|
||||
console.log(`Team "${team.name}" created with agents: ${team.getAgents().map(a => a.name).join(', ')}`)
|
||||
console.log('\nStarting team run...\n')
|
||||
console.log('='.repeat(60))
|
||||
|
||||
const goal = `Create a minimal Express.js REST API in /tmp/express-api/ with:
|
||||
- GET /health → { status: "ok" }
|
||||
- GET /users → returns a hardcoded array of 2 user objects
|
||||
- POST /users → accepts { name, email } body, logs it, returns 201
|
||||
- Proper error handling middleware
|
||||
- The server should listen on port 3001
|
||||
- Include a package.json with the required dependencies`
|
||||
|
||||
const result = await orchestrator.runTeam(team, goal)
|
||||
|
||||
console.log('\n' + '='.repeat(60))
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Results
|
||||
// ---------------------------------------------------------------------------
|
||||
console.log('\nTeam run complete.')
|
||||
console.log(`Success: ${result.success}`)
|
||||
console.log(`Total tokens — input: ${result.totalTokenUsage.input_tokens}, output: ${result.totalTokenUsage.output_tokens}`)
|
||||
|
||||
console.log('\nPer-agent results:')
|
||||
for (const [agentName, agentResult] of result.agentResults) {
|
||||
const status = agentResult.success ? 'OK' : 'FAILED'
|
||||
const tools = agentResult.toolCalls.length
|
||||
console.log(` ${agentName.padEnd(12)} [${status}] tool_calls=${tools}`)
|
||||
if (!agentResult.success) {
|
||||
console.log(` Error: ${agentResult.output.slice(0, 120)}`)
|
||||
}
|
||||
}
|
||||
|
||||
// Sample outputs
|
||||
const developerResult = result.agentResults.get('developer')
|
||||
if (developerResult?.success) {
|
||||
console.log('\nDeveloper output (last 600 chars):')
|
||||
console.log('─'.repeat(60))
|
||||
const out = developerResult.output
|
||||
console.log(out.length > 600 ? '...' + out.slice(-600) : out)
|
||||
console.log('─'.repeat(60))
|
||||
}
|
||||
|
||||
const reviewerResult = result.agentResults.get('reviewer')
|
||||
if (reviewerResult?.success) {
|
||||
console.log('\nReviewer output:')
|
||||
console.log('─'.repeat(60))
|
||||
console.log(reviewerResult.output)
|
||||
console.log('─'.repeat(60))
|
||||
}
|
||||
|
|
@ -50,6 +50,8 @@ const PROVIDER_REFERENCE: ReadonlyArray<{
|
|||
{ id: 'openai', apiKeyEnv: ['OPENAI_API_KEY'], baseUrlSupported: true, notes: 'Set baseURL for Ollama / vLLM / LM Studio; apiKey may be a placeholder.' },
|
||||
{ id: 'gemini', apiKeyEnv: ['GEMINI_API_KEY', 'GOOGLE_API_KEY'], baseUrlSupported: false },
|
||||
{ id: 'grok', apiKeyEnv: ['XAI_API_KEY'], baseUrlSupported: true },
|
||||
{ id: 'minimax', apiKeyEnv: ['MINIMAX_API_KEY'], baseUrlSupported: true, notes: 'Global endpoint: https://api.minimax.io/v1 (default). China endpoint: https://api.minimaxi.com/v1. Set MINIMAX_BASE_URL to choose, or pass baseURL in agent config.' },
|
||||
{ id: 'deepseek', apiKeyEnv: ['DEEPSEEK_API_KEY'], baseUrlSupported: true, notes: 'OpenAI-compatible endpoint at https://api.deepseek.com/v1. Models: deepseek-chat (V3), deepseek-reasoner (thinking).' },
|
||||
{
|
||||
id: 'copilot',
|
||||
apiKeyEnv: ['GITHUB_COPILOT_TOKEN', 'GITHUB_TOKEN'],
|
||||
|
|
@ -259,6 +261,8 @@ const DEFAULT_MODEL_HINT: Record<SupportedProvider, string> = {
|
|||
gemini: 'gemini-2.0-flash',
|
||||
grok: 'grok-2-latest',
|
||||
copilot: 'gpt-4o',
|
||||
minimax: 'MiniMax-M2.7',
|
||||
deepseek: 'deepseek-chat',
|
||||
}
|
||||
|
||||
async function cmdProvider(sub: string | undefined, arg: string | undefined, pretty: boolean): Promise<number> {
|
||||
|
|
|
|||
|
|
@ -89,7 +89,7 @@ export type { TaskQueueEvent } from './task/queue.js'
|
|||
// ---------------------------------------------------------------------------
|
||||
|
||||
export { defineTool, ToolRegistry, zodToJsonSchema } from './tool/framework.js'
|
||||
export { ToolExecutor } from './tool/executor.js'
|
||||
export { ToolExecutor, truncateToolOutput } from './tool/executor.js'
|
||||
export type { ToolExecutorOptions, BatchToolCall } from './tool/executor.js'
|
||||
export {
|
||||
registerBuiltInTools,
|
||||
|
|
|
|||
|
|
@ -38,7 +38,7 @@ import type { LLMAdapter } from '../types.js'
|
|||
* Additional providers can be integrated by implementing {@link LLMAdapter}
|
||||
* directly and bypassing this factory.
|
||||
*/
|
||||
export type SupportedProvider = 'anthropic' | 'copilot' | 'grok' | 'openai' | 'gemini'
|
||||
export type SupportedProvider = 'anthropic' | 'copilot' | 'deepseek' | 'grok' | 'minimax' | 'openai' | 'gemini'
|
||||
|
||||
/**
|
||||
* Instantiate the appropriate {@link LLMAdapter} for the given provider.
|
||||
|
|
@ -49,6 +49,8 @@ export type SupportedProvider = 'anthropic' | 'copilot' | 'grok' | 'openai' | 'g
|
|||
* - `openai` → `OPENAI_API_KEY`
|
||||
* - `gemini` → `GEMINI_API_KEY` / `GOOGLE_API_KEY`
|
||||
* - `grok` → `XAI_API_KEY`
|
||||
* - `minimax` → `MINIMAX_API_KEY`
|
||||
* - `deepseek` → `DEEPSEEK_API_KEY`
|
||||
* - `copilot` → `GITHUB_COPILOT_TOKEN` / `GITHUB_TOKEN`, or interactive
|
||||
* OAuth2 device flow if neither is set
|
||||
*
|
||||
|
|
@ -89,6 +91,14 @@ export async function createAdapter(
|
|||
const { GrokAdapter } = await import('./grok.js')
|
||||
return new GrokAdapter(apiKey, baseURL)
|
||||
}
|
||||
case 'minimax': {
|
||||
const { MiniMaxAdapter } = await import('./minimax.js')
|
||||
return new MiniMaxAdapter(apiKey, baseURL)
|
||||
}
|
||||
case 'deepseek': {
|
||||
const { DeepSeekAdapter } = await import('./deepseek.js')
|
||||
return new DeepSeekAdapter(apiKey, baseURL)
|
||||
}
|
||||
default: {
|
||||
// The `never` cast here makes TypeScript enforce exhaustiveness.
|
||||
const _exhaustive: never = provider
|
||||
|
|
|
|||
|
|
@ -0,0 +1,29 @@
|
|||
/**
|
||||
* @fileoverview DeepSeek adapter.
|
||||
*
|
||||
* Thin wrapper around OpenAIAdapter that hard-codes the official DeepSeek
|
||||
* OpenAI-compatible endpoint and DEEPSEEK_API_KEY environment variable fallback.
|
||||
*/
|
||||
|
||||
import { OpenAIAdapter } from './openai.js'
|
||||
|
||||
/**
|
||||
* LLM adapter for DeepSeek models (deepseek-chat, deepseek-reasoner, and future models).
|
||||
*
|
||||
* Thread-safe. Can be shared across agents.
|
||||
*
|
||||
* Usage:
|
||||
* provider: 'deepseek'
|
||||
* model: 'deepseek-chat' (or 'deepseek-reasoner' for the thinking model)
|
||||
*/
|
||||
export class DeepSeekAdapter extends OpenAIAdapter {
|
||||
readonly name = 'deepseek'
|
||||
|
||||
constructor(apiKey?: string, baseURL?: string) {
|
||||
// Allow override of baseURL (for proxies or future changes) but default to official DeepSeek endpoint.
|
||||
super(
|
||||
apiKey ?? process.env['DEEPSEEK_API_KEY'],
|
||||
baseURL ?? 'https://api.deepseek.com/v1'
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
/**
|
||||
* @fileoverview MiniMax adapter.
|
||||
*
|
||||
* Thin wrapper around OpenAIAdapter that hard-codes the official MiniMax
|
||||
* OpenAI-compatible endpoint and MINIMAX_API_KEY environment variable fallback.
|
||||
*/
|
||||
|
||||
import { OpenAIAdapter } from './openai.js'
|
||||
|
||||
/**
|
||||
* LLM adapter for MiniMax models (MiniMax-M2.7 series and future models).
|
||||
*
|
||||
* Thread-safe. Can be shared across agents.
|
||||
*
|
||||
* Usage:
|
||||
* provider: 'minimax'
|
||||
* model: 'MiniMax-M2.7' (or any current MiniMax model name)
|
||||
*/
|
||||
export class MiniMaxAdapter extends OpenAIAdapter {
|
||||
readonly name = 'minimax'
|
||||
|
||||
constructor(apiKey?: string, baseURL?: string) {
|
||||
// Allow override of baseURL (for proxies or future changes) but default to official MiniMax endpoint.
|
||||
super(
|
||||
apiKey ?? process.env['MINIMAX_API_KEY'],
|
||||
baseURL ?? process.env['MINIMAX_BASE_URL'] ?? 'https://api.minimax.io/v1'
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
@ -217,7 +217,11 @@ function buildAgent(config: AgentConfig): Agent {
|
|||
registry.register(tool, { runtimeAdded: true })
|
||||
}
|
||||
}
|
||||
const executor = new ToolExecutor(registry)
|
||||
const executor = new ToolExecutor(registry, {
|
||||
...(config.maxToolOutputChars !== undefined
|
||||
? { maxToolOutputChars: config.maxToolOutputChars }
|
||||
: {}),
|
||||
})
|
||||
return new Agent(config, registry, executor)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -24,6 +24,11 @@ export interface ToolExecutorOptions {
|
|||
* Defaults to 4.
|
||||
*/
|
||||
maxConcurrency?: number
|
||||
/**
|
||||
* Agent-level default for maximum tool output length in characters.
|
||||
* Per-tool `maxOutputChars` takes priority over this value.
|
||||
*/
|
||||
maxToolOutputChars?: number
|
||||
}
|
||||
|
||||
/** Describes one call in a batch. */
|
||||
|
|
@ -47,10 +52,12 @@ export interface BatchToolCall {
|
|||
export class ToolExecutor {
|
||||
private readonly registry: ToolRegistry
|
||||
private readonly semaphore: Semaphore
|
||||
private readonly maxToolOutputChars?: number
|
||||
|
||||
constructor(registry: ToolRegistry, options: ToolExecutorOptions = {}) {
|
||||
this.registry = registry
|
||||
this.semaphore = new Semaphore(options.maxConcurrency ?? 4)
|
||||
this.maxToolOutputChars = options.maxToolOutputChars
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
|
|
@ -156,7 +163,7 @@ export class ToolExecutor {
|
|||
// --- Execute ---
|
||||
try {
|
||||
const result = await tool.execute(parseResult.data, context)
|
||||
return result
|
||||
return this.maybeTruncate(tool, result)
|
||||
} catch (err) {
|
||||
const message =
|
||||
err instanceof Error
|
||||
|
|
@ -164,10 +171,26 @@ export class ToolExecutor {
|
|||
: typeof err === 'string'
|
||||
? err
|
||||
: JSON.stringify(err)
|
||||
return this.errorResult(`Tool "${tool.name}" threw an error: ${message}`)
|
||||
return this.maybeTruncate(tool, this.errorResult(`Tool "${tool.name}" threw an error: ${message}`))
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply truncation to a tool result if a character limit is configured.
|
||||
* Priority: per-tool `maxOutputChars` > agent-level `maxToolOutputChars`.
|
||||
*/
|
||||
private maybeTruncate(
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
tool: ToolDefinition<any>,
|
||||
result: ToolResult,
|
||||
): ToolResult {
|
||||
const maxChars = tool.maxOutputChars ?? this.maxToolOutputChars
|
||||
if (maxChars === undefined || maxChars <= 0 || result.data.length <= maxChars) {
|
||||
return result
|
||||
}
|
||||
return { ...result, data: truncateToolOutput(result.data, maxChars) }
|
||||
}
|
||||
|
||||
/** Construct an error ToolResult. */
|
||||
private errorResult(message: string): ToolResult {
|
||||
return {
|
||||
|
|
@ -176,3 +199,37 @@ export class ToolExecutor {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Truncation helper
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Truncate tool output to fit within `maxChars`, preserving the head (~70%)
|
||||
* and tail (~30%) with a marker indicating how many characters were removed.
|
||||
*
|
||||
* The marker itself is counted against the budget so the returned string
|
||||
* never exceeds `maxChars`. When `maxChars` is too small to fit any
|
||||
* content alongside the marker, a marker-only string is returned.
|
||||
*/
|
||||
export function truncateToolOutput(data: string, maxChars: number): string {
|
||||
if (data.length <= maxChars) return data
|
||||
|
||||
// Estimate marker length (digit count may shrink after subtracting content,
|
||||
// but using data.length gives a safe upper-bound for the digit count).
|
||||
const markerTemplate = '\n\n[...truncated characters...]\n\n'
|
||||
const markerOverhead = markerTemplate.length + String(data.length).length
|
||||
|
||||
// When maxChars is too small to fit any content alongside the marker,
|
||||
// fall back to a hard slice so the result never exceeds maxChars.
|
||||
if (maxChars <= markerOverhead) {
|
||||
return data.slice(0, maxChars)
|
||||
}
|
||||
|
||||
const available = maxChars - markerOverhead
|
||||
const headChars = Math.floor(available * 0.7)
|
||||
const tailChars = available - headChars
|
||||
const truncatedCount = data.length - headChars - tailChars
|
||||
|
||||
return `${data.slice(0, headChars)}\n\n[...truncated ${truncatedCount} characters...]\n\n${data.slice(-tailChars)}`
|
||||
}
|
||||
|
|
|
|||
|
|
@ -76,6 +76,12 @@ export function defineTool<TInput>(config: {
|
|||
* Optional JSON Schema for the LLM (bypasses Zod → JSON Schema conversion).
|
||||
*/
|
||||
llmInputSchema?: Record<string, unknown>
|
||||
/**
|
||||
* Per-tool maximum output length in characters. When set, tool output
|
||||
* exceeding this limit is truncated (head + tail with a marker in between).
|
||||
* Takes priority over agent-level `maxToolOutputChars`.
|
||||
*/
|
||||
maxOutputChars?: number
|
||||
execute: (input: TInput, context: ToolUseContext) => Promise<ToolResult>
|
||||
}): ToolDefinition<TInput> {
|
||||
return {
|
||||
|
|
@ -85,6 +91,9 @@ export function defineTool<TInput>(config: {
|
|||
...(config.llmInputSchema !== undefined
|
||||
? { llmInputSchema: config.llmInputSchema }
|
||||
: {}),
|
||||
...(config.maxOutputChars !== undefined
|
||||
? { maxOutputChars: config.maxOutputChars }
|
||||
: {}),
|
||||
execute: config.execute,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
13
src/types.ts
13
src/types.ts
|
|
@ -194,6 +194,12 @@ export interface ToolDefinition<TInput = Record<string, unknown>> {
|
|||
* deriving JSON Schema from `inputSchema` (Zod).
|
||||
*/
|
||||
readonly llmInputSchema?: Record<string, unknown>
|
||||
/**
|
||||
* Per-tool maximum output length in characters. When set, tool output
|
||||
* exceeding this limit is truncated (head + tail with a marker in between).
|
||||
* Takes priority over {@link AgentConfig.maxToolOutputChars}.
|
||||
*/
|
||||
readonly maxOutputChars?: number
|
||||
execute(input: TInput, context: ToolUseContext): Promise<ToolResult>
|
||||
}
|
||||
|
||||
|
|
@ -257,6 +263,13 @@ export interface AgentConfig {
|
|||
* calls and text outputs to detect stuck loops before `maxTurns` is reached.
|
||||
*/
|
||||
readonly loopDetection?: LoopDetectionConfig
|
||||
/**
|
||||
* Maximum tool output length in characters for all tools used by this agent.
|
||||
* When set, tool outputs exceeding this limit are truncated (head + tail
|
||||
* with a marker in between). Per-tool {@link ToolDefinition.maxOutputChars}
|
||||
* takes priority over this value.
|
||||
*/
|
||||
readonly maxToolOutputChars?: number
|
||||
/**
|
||||
* Optional Zod schema for structured output. When set, the agent's final
|
||||
* output is parsed as JSON and validated against this schema. A single
|
||||
|
|
|
|||
|
|
@ -0,0 +1,74 @@
|
|||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Mock OpenAI constructor (must be hoisted for Vitest)
|
||||
// ---------------------------------------------------------------------------
|
||||
const OpenAIMock = vi.hoisted(() => vi.fn())
|
||||
|
||||
vi.mock('openai', () => ({
|
||||
default: OpenAIMock,
|
||||
}))
|
||||
|
||||
import { DeepSeekAdapter } from '../src/llm/deepseek.js'
|
||||
import { createAdapter } from '../src/llm/adapter.js'
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// DeepSeekAdapter tests
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
describe('DeepSeekAdapter', () => {
|
||||
beforeEach(() => {
|
||||
OpenAIMock.mockClear()
|
||||
})
|
||||
|
||||
it('has name "deepseek"', () => {
|
||||
const adapter = new DeepSeekAdapter()
|
||||
expect(adapter.name).toBe('deepseek')
|
||||
})
|
||||
|
||||
it('uses DEEPSEEK_API_KEY by default', () => {
|
||||
const original = process.env['DEEPSEEK_API_KEY']
|
||||
process.env['DEEPSEEK_API_KEY'] = 'deepseek-test-key-123'
|
||||
|
||||
try {
|
||||
new DeepSeekAdapter()
|
||||
expect(OpenAIMock).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
apiKey: 'deepseek-test-key-123',
|
||||
baseURL: 'https://api.deepseek.com/v1',
|
||||
})
|
||||
)
|
||||
} finally {
|
||||
if (original === undefined) {
|
||||
delete process.env['DEEPSEEK_API_KEY']
|
||||
} else {
|
||||
process.env['DEEPSEEK_API_KEY'] = original
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
it('uses official DeepSeek baseURL by default', () => {
|
||||
new DeepSeekAdapter('some-key')
|
||||
expect(OpenAIMock).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
apiKey: 'some-key',
|
||||
baseURL: 'https://api.deepseek.com/v1',
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it('allows overriding apiKey and baseURL', () => {
|
||||
new DeepSeekAdapter('custom-key', 'https://custom.endpoint/v1')
|
||||
expect(OpenAIMock).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
apiKey: 'custom-key',
|
||||
baseURL: 'https://custom.endpoint/v1',
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it('createAdapter("deepseek") returns DeepSeekAdapter instance', async () => {
|
||||
const adapter = await createAdapter('deepseek')
|
||||
expect(adapter).toBeInstanceOf(DeepSeekAdapter)
|
||||
})
|
||||
})
|
||||
|
|
@ -0,0 +1,95 @@
|
|||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Mock OpenAI constructor (must be hoisted for Vitest)
|
||||
// ---------------------------------------------------------------------------
|
||||
const OpenAIMock = vi.hoisted(() => vi.fn())
|
||||
|
||||
vi.mock('openai', () => ({
|
||||
default: OpenAIMock,
|
||||
}))
|
||||
|
||||
import { MiniMaxAdapter } from '../src/llm/minimax.js'
|
||||
import { createAdapter } from '../src/llm/adapter.js'
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// MiniMaxAdapter tests
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
describe('MiniMaxAdapter', () => {
|
||||
beforeEach(() => {
|
||||
OpenAIMock.mockClear()
|
||||
})
|
||||
|
||||
it('has name "minimax"', () => {
|
||||
const adapter = new MiniMaxAdapter()
|
||||
expect(adapter.name).toBe('minimax')
|
||||
})
|
||||
|
||||
it('uses MINIMAX_API_KEY by default', () => {
|
||||
const original = process.env['MINIMAX_API_KEY']
|
||||
process.env['MINIMAX_API_KEY'] = 'minimax-test-key-123'
|
||||
|
||||
try {
|
||||
new MiniMaxAdapter()
|
||||
expect(OpenAIMock).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
apiKey: 'minimax-test-key-123',
|
||||
baseURL: 'https://api.minimax.io/v1',
|
||||
})
|
||||
)
|
||||
} finally {
|
||||
if (original === undefined) {
|
||||
delete process.env['MINIMAX_API_KEY']
|
||||
} else {
|
||||
process.env['MINIMAX_API_KEY'] = original
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
it('uses official MiniMax global baseURL by default', () => {
|
||||
new MiniMaxAdapter('some-key')
|
||||
expect(OpenAIMock).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
apiKey: 'some-key',
|
||||
baseURL: 'https://api.minimax.io/v1',
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it('uses MINIMAX_BASE_URL env var when set', () => {
|
||||
const original = process.env['MINIMAX_BASE_URL']
|
||||
process.env['MINIMAX_BASE_URL'] = 'https://api.minimaxi.com/v1'
|
||||
|
||||
try {
|
||||
new MiniMaxAdapter('some-key')
|
||||
expect(OpenAIMock).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
apiKey: 'some-key',
|
||||
baseURL: 'https://api.minimaxi.com/v1',
|
||||
})
|
||||
)
|
||||
} finally {
|
||||
if (original === undefined) {
|
||||
delete process.env['MINIMAX_BASE_URL']
|
||||
} else {
|
||||
process.env['MINIMAX_BASE_URL'] = original
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
it('allows overriding apiKey and baseURL', () => {
|
||||
new MiniMaxAdapter('custom-key', 'https://custom.endpoint/v1')
|
||||
expect(OpenAIMock).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
apiKey: 'custom-key',
|
||||
baseURL: 'https://custom.endpoint/v1',
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it('createAdapter("minimax") returns MiniMaxAdapter instance', async () => {
|
||||
const adapter = await createAdapter('minimax')
|
||||
expect(adapter).toBeInstanceOf(MiniMaxAdapter)
|
||||
})
|
||||
})
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
import { describe, it, expect, vi } from 'vitest'
|
||||
import { z } from 'zod'
|
||||
import { ToolRegistry, defineTool } from '../src/tool/framework.js'
|
||||
import { ToolExecutor } from '../src/tool/executor.js'
|
||||
import { ToolExecutor, truncateToolOutput } from '../src/tool/executor.js'
|
||||
import type { ToolUseContext } from '../src/types.js'
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
|
@ -191,3 +191,222 @@ describe('ToolRegistry', () => {
|
|||
expect(defs[0].inputSchema).toHaveProperty('properties')
|
||||
})
|
||||
})
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// truncateToolOutput
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
describe('truncateToolOutput', () => {
|
||||
it('returns data unchanged when under the limit', () => {
|
||||
const data = 'short output'
|
||||
expect(truncateToolOutput(data, 100)).toBe(data)
|
||||
})
|
||||
|
||||
it('returns data unchanged when exactly at the limit', () => {
|
||||
const data = 'x'.repeat(100)
|
||||
expect(truncateToolOutput(data, 100)).toBe(data)
|
||||
})
|
||||
|
||||
it('truncates data exceeding the limit with head/tail and marker', () => {
|
||||
const data = 'A'.repeat(300) + 'B'.repeat(700)
|
||||
const result = truncateToolOutput(data, 500)
|
||||
expect(result).toContain('[...truncated')
|
||||
expect(result.length).toBeLessThanOrEqual(500)
|
||||
// Head portion starts with As
|
||||
expect(result.startsWith('A')).toBe(true)
|
||||
// Tail portion ends with Bs
|
||||
expect(result.endsWith('B')).toBe(true)
|
||||
})
|
||||
|
||||
it('result never exceeds maxChars', () => {
|
||||
const data = 'x'.repeat(10000)
|
||||
const result = truncateToolOutput(data, 1000)
|
||||
expect(result.length).toBeLessThanOrEqual(1000)
|
||||
expect(result).toContain('[...truncated')
|
||||
})
|
||||
|
||||
it('handles empty string', () => {
|
||||
expect(truncateToolOutput('', 100)).toBe('')
|
||||
})
|
||||
|
||||
it('handles very small maxChars gracefully', () => {
|
||||
const data = 'x'.repeat(100)
|
||||
// With maxChars=1, the marker alone exceeds the budget — falls back to hard slice
|
||||
const result = truncateToolOutput(data, 1)
|
||||
expect(result.length).toBeLessThanOrEqual(1)
|
||||
})
|
||||
})
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Tool output truncation (integration)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
describe('ToolExecutor output truncation', () => {
|
||||
it('truncates output when agent-level maxToolOutputChars is set', async () => {
|
||||
const bigTool = defineTool({
|
||||
name: 'big',
|
||||
description: 'Returns large output.',
|
||||
inputSchema: z.object({}),
|
||||
execute: async () => ({ data: 'x'.repeat(5000) }),
|
||||
})
|
||||
const registry = new ToolRegistry()
|
||||
registry.register(bigTool)
|
||||
const executor = new ToolExecutor(registry, { maxToolOutputChars: 200 })
|
||||
|
||||
const result = await executor.execute('big', {}, dummyContext)
|
||||
expect(result.data.length).toBeLessThan(5000)
|
||||
expect(result.data).toContain('[...truncated')
|
||||
})
|
||||
|
||||
it('does not truncate when output is under the limit', async () => {
|
||||
const smallTool = defineTool({
|
||||
name: 'small',
|
||||
description: 'Returns small output.',
|
||||
inputSchema: z.object({}),
|
||||
execute: async () => ({ data: 'hello' }),
|
||||
})
|
||||
const registry = new ToolRegistry()
|
||||
registry.register(smallTool)
|
||||
const executor = new ToolExecutor(registry, { maxToolOutputChars: 200 })
|
||||
|
||||
const result = await executor.execute('small', {}, dummyContext)
|
||||
expect(result.data).toBe('hello')
|
||||
})
|
||||
|
||||
it('per-tool maxOutputChars overrides agent-level setting (smaller)', async () => {
|
||||
const toolWithLimit = defineTool({
|
||||
name: 'limited',
|
||||
description: 'Has its own limit.',
|
||||
inputSchema: z.object({}),
|
||||
maxOutputChars: 200,
|
||||
execute: async () => ({ data: 'y'.repeat(5000) }),
|
||||
})
|
||||
const registry = new ToolRegistry()
|
||||
registry.register(toolWithLimit)
|
||||
// Agent-level is 1000 but tool-level is 200 -- tool wins
|
||||
const executor = new ToolExecutor(registry, { maxToolOutputChars: 1000 })
|
||||
|
||||
const result = await executor.execute('limited', {}, dummyContext)
|
||||
expect(result.data).toContain('[...truncated')
|
||||
expect(result.data.length).toBeLessThanOrEqual(200)
|
||||
})
|
||||
|
||||
it('per-tool maxOutputChars overrides agent-level setting (larger)', async () => {
|
||||
const toolWithLimit = defineTool({
|
||||
name: 'limited',
|
||||
description: 'Has its own limit.',
|
||||
inputSchema: z.object({}),
|
||||
maxOutputChars: 2000,
|
||||
execute: async () => ({ data: 'y'.repeat(5000) }),
|
||||
})
|
||||
const registry = new ToolRegistry()
|
||||
registry.register(toolWithLimit)
|
||||
// Agent-level is 500 but tool-level is 2000 -- tool wins
|
||||
const executor = new ToolExecutor(registry, { maxToolOutputChars: 500 })
|
||||
|
||||
const result = await executor.execute('limited', {}, dummyContext)
|
||||
expect(result.data).toContain('[...truncated')
|
||||
expect(result.data.length).toBeLessThanOrEqual(2000)
|
||||
expect(result.data.length).toBeGreaterThan(500)
|
||||
})
|
||||
|
||||
it('per-tool maxOutputChars works without agent-level setting', async () => {
|
||||
const toolWithLimit = defineTool({
|
||||
name: 'limited',
|
||||
description: 'Has its own limit.',
|
||||
inputSchema: z.object({}),
|
||||
maxOutputChars: 300,
|
||||
execute: async () => ({ data: 'z'.repeat(5000) }),
|
||||
})
|
||||
const registry = new ToolRegistry()
|
||||
registry.register(toolWithLimit)
|
||||
const executor = new ToolExecutor(registry)
|
||||
|
||||
const result = await executor.execute('limited', {}, dummyContext)
|
||||
expect(result.data).toContain('[...truncated')
|
||||
expect(result.data.length).toBeLessThanOrEqual(300)
|
||||
})
|
||||
|
||||
it('truncates error results too', async () => {
|
||||
const errorTool = defineTool({
|
||||
name: 'errorbig',
|
||||
description: 'Throws a huge error.',
|
||||
inputSchema: z.object({}),
|
||||
execute: async () => { throw new Error('E'.repeat(5000)) },
|
||||
})
|
||||
const registry = new ToolRegistry()
|
||||
registry.register(errorTool)
|
||||
const executor = new ToolExecutor(registry, { maxToolOutputChars: 200 })
|
||||
|
||||
const result = await executor.execute('errorbig', {}, dummyContext)
|
||||
expect(result.isError).toBe(true)
|
||||
expect(result.data).toContain('[...truncated')
|
||||
expect(result.data.length).toBeLessThan(5000)
|
||||
})
|
||||
|
||||
it('no truncation when maxToolOutputChars is 0', async () => {
|
||||
const bigTool = defineTool({
|
||||
name: 'big',
|
||||
description: 'Returns large output.',
|
||||
inputSchema: z.object({}),
|
||||
execute: async () => ({ data: 'x'.repeat(5000) }),
|
||||
})
|
||||
const registry = new ToolRegistry()
|
||||
registry.register(bigTool)
|
||||
const executor = new ToolExecutor(registry, { maxToolOutputChars: 0 })
|
||||
|
||||
const result = await executor.execute('big', {}, dummyContext)
|
||||
expect(result.data.length).toBe(5000)
|
||||
})
|
||||
|
||||
it('no truncation when maxToolOutputChars is negative', async () => {
|
||||
const bigTool = defineTool({
|
||||
name: 'big',
|
||||
description: 'Returns large output.',
|
||||
inputSchema: z.object({}),
|
||||
execute: async () => ({ data: 'x'.repeat(5000) }),
|
||||
})
|
||||
const registry = new ToolRegistry()
|
||||
registry.register(bigTool)
|
||||
const executor = new ToolExecutor(registry, { maxToolOutputChars: -100 })
|
||||
|
||||
const result = await executor.execute('big', {}, dummyContext)
|
||||
expect(result.data.length).toBe(5000)
|
||||
})
|
||||
|
||||
it('defineTool passes maxOutputChars to the ToolDefinition', () => {
|
||||
const tool = defineTool({
|
||||
name: 'test',
|
||||
description: 'test',
|
||||
inputSchema: z.object({}),
|
||||
maxOutputChars: 500,
|
||||
execute: async () => ({ data: 'ok' }),
|
||||
})
|
||||
expect(tool.maxOutputChars).toBe(500)
|
||||
})
|
||||
|
||||
it('defineTool omits maxOutputChars when not specified', () => {
|
||||
const tool = defineTool({
|
||||
name: 'test',
|
||||
description: 'test',
|
||||
inputSchema: z.object({}),
|
||||
execute: async () => ({ data: 'ok' }),
|
||||
})
|
||||
expect(tool.maxOutputChars).toBeUndefined()
|
||||
})
|
||||
|
||||
it('no truncation when neither limit is set', async () => {
|
||||
const bigTool = defineTool({
|
||||
name: 'big',
|
||||
description: 'Returns large output.',
|
||||
inputSchema: z.object({}),
|
||||
execute: async () => ({ data: 'x'.repeat(50000) }),
|
||||
})
|
||||
const registry = new ToolRegistry()
|
||||
registry.register(bigTool)
|
||||
const executor = new ToolExecutor(registry)
|
||||
|
||||
const result = await executor.execute('big', {}, dummyContext)
|
||||
expect(result.data.length).toBe(50000)
|
||||
})
|
||||
})
|
||||
|
|
|
|||
Loading…
Reference in New Issue