@amitdeshmukh/ax-crew 5.0.0 → 6.0.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/CHANGELOG.md +22 -0
- package/README.md +7 -59
- package/dist/agents/agentConfig.d.ts +6 -6
- package/dist/agents/agentConfig.js +41 -122
- package/dist/agents/compose.d.ts +15 -0
- package/dist/agents/compose.js +58 -0
- package/dist/agents/index.d.ts +20 -4
- package/dist/agents/index.js +19 -3
- package/dist/functions/index.d.ts +11 -0
- package/dist/functions/index.js +11 -1
- package/dist/index.d.ts +25 -8
- package/dist/index.js +26 -1
- package/dist/types.d.ts +40 -6
- package/examples/basic-researcher-writer.ts +5 -3
- package/examples/mcp-agent.ts +20 -43
- package/examples/perplexityDeepSearch.ts +6 -5
- package/examples/providerArgs.ts +2 -1
- package/examples/search-tweets.ts +5 -4
- package/examples/solve-math-problem.ts +7 -5
- package/examples/streaming.ts +2 -2
- package/package.json +4 -4
- package/src/agents/agentConfig.ts +46 -140
- package/src/agents/compose.ts +80 -0
- package/src/agents/index.ts +22 -6
- package/src/functions/index.ts +11 -1
- package/src/index.ts +25 -11
- package/src/types.ts +44 -21
- package/dist/config/index.d.ts +0 -5
- package/dist/config/index.js +0 -30
- package/src/config/index.ts +0 -40
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,27 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [6.0.0] - 2025-10-22
|
|
4
|
+
|
|
5
|
+
### Breaking
|
|
6
|
+
- Config is now object-only to be browser-safe. File-path based configs are no longer supported. Load JSON yourself and pass `{ crew: [...] }`.
|
|
7
|
+
|
|
8
|
+
### Added
|
|
9
|
+
- Runtime API key resolution via `providerKeyName`. Keys are read from `process.env[providerKeyName]` in Node or `globalThis[providerKeyName]` in the browser.
|
|
10
|
+
- Updated examples to use `dotenv/config` to ensure keys are available at runtime.
|
|
11
|
+
|
|
12
|
+
### Changed
|
|
13
|
+
- Removed reliance on a hardcoded `PROVIDER_API_KEYS` map; providers read secrets using the user-supplied env var name.
|
|
14
|
+
- `providerArgs` are forwarded unchanged to Ax; validation and semantics are handled by Ax provider implementations.
|
|
15
|
+
- Docs updated: object-only config, env resolution behavior, providerArgs guidance.
|
|
16
|
+
|
|
17
|
+
### Removed
|
|
18
|
+
- Hardcoded provider lists/registries and the old `providers.ts` list file.
|
|
19
|
+
- Temporary removal of router/balancer helpers and related examples to simplify this refactor.
|
|
20
|
+
|
|
21
|
+
### Migration notes
|
|
22
|
+
- If you previously passed a config file path, read the file yourself and pass the parsed object to `new AxCrew(config)`.
|
|
23
|
+
- Set `providerKeyName` to the exact environment variable name holding the API key. Ensure env is loaded before constructing the crew (e.g., `import 'dotenv/config'`).
|
|
24
|
+
|
|
3
25
|
## [5.0.0] - 2025-10-16
|
|
4
26
|
|
|
5
27
|
### Added
|
package/README.md
CHANGED
|
@@ -33,43 +33,17 @@ ANTHROPIC_API_KEY=...
|
|
|
33
33
|
OPENAI_API_KEY=...
|
|
34
34
|
AZURE_OPENAI_API_KEY=...
|
|
35
35
|
```
|
|
36
|
-
In each agent config, set `providerKeyName` to the env var name.
|
|
36
|
+
In each agent config, set `providerKeyName` to the env var name. AxCrew resolves the key at runtime by reading `process.env[providerKeyName]` in Node or `globalThis[providerKeyName]` in the browser. Ensure your env is loaded (for example, import `dotenv/config` in Node) before creating the crew.
|
|
37
37
|
|
|
38
38
|
#### Provider-specific arguments (`providerArgs`)
|
|
39
|
-
Some providers require extra top-level configuration beyond `ai.model` (
|
|
40
|
-
|
|
41
|
-
Supported mappings:
|
|
42
|
-
- `azure-openai`: `resourceName`, `deploymentName`, `version` (if `resourceName` omitted and `apiURL` set, `apiURL` is used as full endpoint)
|
|
43
|
-
- `anthropic`: `projectId`, `region`
|
|
44
|
-
- `google-gemini`: `projectId`, `region`, `endpointId`
|
|
45
|
-
- `openrouter`: `referer`, `title`
|
|
46
|
-
- `ollama`: `url`
|
|
47
|
-
|
|
48
|
-
Azure example with `providerArgs`:
|
|
49
|
-
```json
|
|
50
|
-
{
|
|
51
|
-
"name": "Writer",
|
|
52
|
-
"description": "Writes concise summaries",
|
|
53
|
-
"signature": "topic:string -> summary:string",
|
|
54
|
-
"provider": "azure-openai",
|
|
55
|
-
"providerKeyName": "AZURE_OPENAI_API_KEY",
|
|
56
|
-
"ai": { "model": "gpt-4o-mini", "temperature": 0 },
|
|
57
|
-
"apiURL": "https://your-resource.openai.azure.com", "providerArgs": {
|
|
58
|
-
"resourceName": "your-resource",
|
|
59
|
-
"deploymentName": "gpt-4o-mini",
|
|
60
|
-
"version": "api-version=2024-02-15-preview"
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
```
|
|
39
|
+
Some providers require extra top-level configuration beyond `ai.model` (for example, Azure deployment details or Vertex AI project/region). Pass these via `providerArgs`. AxCrew forwards `providerArgs` to Ax unchanged; validation and semantics are handled by Ax. Refer to Ax provider docs for supported fields.
|
|
64
40
|
|
|
65
41
|
### Quickstart
|
|
66
|
-
This package includes TypeScript declarations and provides type safety.
|
|
67
42
|
|
|
68
43
|
```typescript
|
|
69
44
|
import { AxCrew, AxCrewFunctions, FunctionRegistryType, StateInstance } from '@amitdeshmukh/ax-crew';
|
|
70
45
|
import type { AxFunction } from '@ax-llm/ax';
|
|
71
46
|
|
|
72
|
-
// Type-safe configuration
|
|
73
47
|
const config = {
|
|
74
48
|
crew: [{
|
|
75
49
|
name: "Planner",
|
|
@@ -156,25 +130,7 @@ Key TypeScript features:
|
|
|
156
130
|
## Usage
|
|
157
131
|
|
|
158
132
|
### Initializing a Crew
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
1. Using a JSON configuration file that defines the agents in the crew, along with their individual configurations.
|
|
162
|
-
2. Directly passing a JSON object with the crew configuration.
|
|
163
|
-
|
|
164
|
-
#### Using a Configuration File
|
|
165
|
-
See [agentConfig.json](agentConfig.json) for an example configuration file.
|
|
166
|
-
|
|
167
|
-
```javascript
|
|
168
|
-
// Import the AxCrew class
|
|
169
|
-
import { AxCrew } from '@amitdeshmukh/ax-crew';
|
|
170
|
-
|
|
171
|
-
// Create a new instance of AxCrew using a config file
|
|
172
|
-
const configFilePath = './agentConfig.json';
|
|
173
|
-
const crew = new AxCrew(configFilePath);
|
|
174
|
-
```
|
|
175
|
-
|
|
176
|
-
#### Using a Direct Configuration Object
|
|
177
|
-
You can also pass the configuration directly as a JSON object:
|
|
133
|
+
Pass a JSON object to the `AxCrew` constructor:
|
|
178
134
|
|
|
179
135
|
```javascript
|
|
180
136
|
// Import the AxCrew class
|
|
@@ -205,15 +161,7 @@ const config = {
|
|
|
205
161
|
const crew = new AxCrew(config);
|
|
206
162
|
```
|
|
207
163
|
|
|
208
|
-
|
|
209
|
-
- Use a configuration file when you want to:
|
|
210
|
-
- Keep your configuration separate from your code
|
|
211
|
-
- Share configurations across different projects
|
|
212
|
-
- Version control your configurations
|
|
213
|
-
- Use a direct configuration object when you want to:
|
|
214
|
-
- Generate configurations dynamically
|
|
215
|
-
- Modify configurations at runtime
|
|
216
|
-
- Keep everything in one file for simpler projects
|
|
164
|
+
AxCrew no longer supports reading from a configuration file to remain browser-compatible.
|
|
217
165
|
|
|
218
166
|
### Agent Persona: definition and prompt
|
|
219
167
|
|
|
@@ -417,7 +365,7 @@ An example of how to complete a task using the agents is shown below. The `Plann
|
|
|
417
365
|
import { AxCrew, AxCrewFunctions } from '@amitdeshmukh/ax-crew';
|
|
418
366
|
|
|
419
367
|
// Create a new instance of AxCrew
|
|
420
|
-
const crew = new AxCrew(
|
|
368
|
+
const crew = new AxCrew(config, AxCrewFunctions);
|
|
421
369
|
crew.addAgentsToCrew(['Planner', 'Calculator', 'Manager']);
|
|
422
370
|
|
|
423
371
|
// Get agent instances
|
|
@@ -449,7 +397,7 @@ The package supports streaming responses from agents, allowing you to receive an
|
|
|
449
397
|
import { AxCrew, AxCrewFunctions } from '@amitdeshmukh/ax-crew';
|
|
450
398
|
|
|
451
399
|
// Create and initialize crew as shown above
|
|
452
|
-
const crew = new AxCrew(
|
|
400
|
+
const crew = new AxCrew(config, AxCrewFunctions);
|
|
453
401
|
await crew.addAgentsToCrew(['Planner']);
|
|
454
402
|
|
|
455
403
|
const planner = crew.agents.get("Planner");
|
|
@@ -666,7 +614,7 @@ MCP functions are automatically available to agents once the servers are configu
|
|
|
666
614
|
import { AxCrew } from '@amitdeshmukh/ax-crew';
|
|
667
615
|
|
|
668
616
|
// Create crew with MCP-enabled agents
|
|
669
|
-
const crew = new AxCrew(
|
|
617
|
+
const crew = new AxCrew(config);
|
|
670
618
|
await crew.addAgent('DataAnalyst'); // Agent with MCP servers configured
|
|
671
619
|
|
|
672
620
|
const analyst = crew.agents.get('DataAnalyst');
|
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
import { AxDefaultCostTracker } from '@ax-llm/ax';
|
|
2
2
|
import type { AxFunction } from '@ax-llm/ax';
|
|
3
|
-
import type { AgentConfig,
|
|
3
|
+
import type { AgentConfig, AxCrewConfig, FunctionRegistryType, MCPTransportConfig, MCPStdioTransportConfig, MCPHTTPSSETransportConfig, MCPStreamableHTTPTransportConfig } from '../types.js';
|
|
4
4
|
export declare function isStdioTransport(config: MCPTransportConfig): config is MCPStdioTransportConfig;
|
|
5
5
|
export declare function isHTTPSSETransport(config: MCPTransportConfig): config is MCPHTTPSSETransportConfig;
|
|
6
6
|
export declare function isStreambleHTTPTransport(config: MCPTransportConfig): config is MCPStreamableHTTPTransportConfig;
|
|
7
7
|
/**
|
|
8
|
-
*
|
|
9
|
-
* @param {
|
|
8
|
+
* Returns the AxCrew config from a direct JSON object. Browser-safe.
|
|
9
|
+
* @param {CrewConfig} input - A JSON object with crew configuration.
|
|
10
10
|
* @returns {Object} The parsed crew config.
|
|
11
11
|
* @throws Will throw an error if reading/parsing fails.
|
|
12
12
|
*/
|
|
13
|
-
declare const parseCrewConfig: (input:
|
|
13
|
+
declare const parseCrewConfig: (input: AxCrewConfig) => {
|
|
14
14
|
crew: AgentConfig[];
|
|
15
15
|
};
|
|
16
16
|
/**
|
|
@@ -18,14 +18,14 @@ declare const parseCrewConfig: (input: CrewConfigInput) => {
|
|
|
18
18
|
* and creates an instance of the Agent with the appropriate settings.
|
|
19
19
|
*
|
|
20
20
|
* @param {string} agentName - The identifier for the AI agent to be initialized.
|
|
21
|
-
* @param {
|
|
21
|
+
* @param {AxCrewConfig} crewConfig - A JSON object with crew configuration.
|
|
22
22
|
* @param {FunctionRegistryType} functions - The functions available to the agent.
|
|
23
23
|
* @param {Object} state - The state object for the agent.
|
|
24
24
|
* @returns {Object} An object containing the Agents AI instance, its name, description, signature, functions and subAgentList.
|
|
25
25
|
* @throws {Error} Throws an error if the agent configuration is missing, the provider is unsupported,
|
|
26
26
|
* the API key is not found, or the provider key name is not specified in the configuration.
|
|
27
27
|
*/
|
|
28
|
-
declare const parseAgentConfig: (agentName: string, crewConfig:
|
|
28
|
+
declare const parseAgentConfig: (agentName: string, crewConfig: AxCrewConfig, functions: FunctionRegistryType, state: Record<string, any>) => Promise<{
|
|
29
29
|
ai: import("@ax-llm/ax").AxAI<string>;
|
|
30
30
|
name: string;
|
|
31
31
|
description: string;
|
|
@@ -1,27 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
// Import Ax factory and MCP transports (as exported by current package)
|
|
1
|
+
// Import Ax factory and MCP transports
|
|
3
2
|
import { ai, AxMCPClient, AxMCPHTTPSSETransport, AxMCPStreambleHTTPTransport, AxDefaultCostTracker } from '@ax-llm/ax';
|
|
4
3
|
// STDIO transport from tools package
|
|
5
4
|
import { AxMCPStdioTransport } from '@ax-llm/ax-tools';
|
|
6
|
-
import { PROVIDER_API_KEYS } from '../config/index.js';
|
|
7
|
-
// Canonical provider slugs supported by ai() factory
|
|
8
|
-
const PROVIDER_CANONICAL = new Set([
|
|
9
|
-
'openai',
|
|
10
|
-
'anthropic',
|
|
11
|
-
'google-gemini',
|
|
12
|
-
'mistral',
|
|
13
|
-
'groq',
|
|
14
|
-
'cohere',
|
|
15
|
-
'together',
|
|
16
|
-
'deepseek',
|
|
17
|
-
'ollama',
|
|
18
|
-
'huggingface',
|
|
19
|
-
'openrouter',
|
|
20
|
-
'azure-openai',
|
|
21
|
-
'reka',
|
|
22
|
-
'x-grok'
|
|
23
|
-
]);
|
|
24
|
-
// Provider type lives in src/types.ts
|
|
25
5
|
// Type guard to check if config is stdio transport
|
|
26
6
|
export function isStdioTransport(config) {
|
|
27
7
|
return 'command' in config;
|
|
@@ -44,48 +24,7 @@ export function isStreambleHTTPTransport(config) {
|
|
|
44
24
|
function isConstructor(func) {
|
|
45
25
|
return typeof func === 'function' && 'prototype' in func && 'toFunction' in func.prototype;
|
|
46
26
|
}
|
|
47
|
-
|
|
48
|
-
* Provides a user-friendly error message for JSON parsing errors
|
|
49
|
-
*/
|
|
50
|
-
const getFormattedJSONError = (error, fileContents) => {
|
|
51
|
-
if (error instanceof SyntaxError) {
|
|
52
|
-
const match = error.message.match(/position (\d+)/);
|
|
53
|
-
const position = match ? parseInt(match[1]) : -1;
|
|
54
|
-
if (position !== -1) {
|
|
55
|
-
const lines = fileContents.split('\n');
|
|
56
|
-
let currentPos = 0;
|
|
57
|
-
let errorLine = 0;
|
|
58
|
-
let errorColumn = 0;
|
|
59
|
-
// Find the line and column of the error
|
|
60
|
-
for (let i = 0; i < lines.length; i++) {
|
|
61
|
-
if (currentPos + lines[i].length >= position) {
|
|
62
|
-
errorLine = i + 1;
|
|
63
|
-
errorColumn = position - currentPos + 1;
|
|
64
|
-
break;
|
|
65
|
-
}
|
|
66
|
-
currentPos += lines[i].length + 1; // +1 for the newline character
|
|
67
|
-
}
|
|
68
|
-
const contextLines = lines.slice(Math.max(0, errorLine - 3), errorLine + 2)
|
|
69
|
-
.map((line, idx) => `${errorLine - 2 + idx}: ${line}`).join('\n');
|
|
70
|
-
return `JSON Parse Error in your agent configuration:
|
|
71
|
-
|
|
72
|
-
Error near line ${errorLine}, column ${errorColumn}
|
|
73
|
-
|
|
74
|
-
Context:
|
|
75
|
-
${contextLines}
|
|
76
|
-
|
|
77
|
-
Common issues to check:
|
|
78
|
-
- Missing or extra commas between properties
|
|
79
|
-
- Missing quotes around property names
|
|
80
|
-
- Unmatched brackets or braces
|
|
81
|
-
- Invalid JSON values
|
|
82
|
-
- Trailing commas (not allowed in JSON)
|
|
83
|
-
|
|
84
|
-
Original error: ${error.message}`;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
return `Error parsing agent configuration: ${error.message}`;
|
|
88
|
-
};
|
|
27
|
+
// Removed file/JSON parse helpers to keep browser-safe
|
|
89
28
|
const initializeMCPServers = async (agentConfigData) => {
|
|
90
29
|
const mcpServers = agentConfigData.mcpServers;
|
|
91
30
|
if (!mcpServers || Object.keys(mcpServers).length === 0) {
|
|
@@ -126,41 +65,23 @@ const initializeMCPServers = async (agentConfigData) => {
|
|
|
126
65
|
}
|
|
127
66
|
};
|
|
128
67
|
/**
|
|
129
|
-
*
|
|
130
|
-
* @param {
|
|
68
|
+
* Returns the AxCrew config from a direct JSON object. Browser-safe.
|
|
69
|
+
* @param {CrewConfig} input - A JSON object with crew configuration.
|
|
131
70
|
* @returns {Object} The parsed crew config.
|
|
132
71
|
* @throws Will throw an error if reading/parsing fails.
|
|
133
72
|
*/
|
|
134
73
|
const parseCrewConfig = (input) => {
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
// Handle file path input
|
|
138
|
-
const fileContents = fs.readFileSync(input, 'utf8');
|
|
139
|
-
const parsedConfig = JSON.parse(fileContents);
|
|
140
|
-
return parsedConfig;
|
|
141
|
-
}
|
|
142
|
-
else {
|
|
143
|
-
// Handle direct JSON object input
|
|
144
|
-
return input;
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
catch (e) {
|
|
148
|
-
if (e instanceof Error) {
|
|
149
|
-
if (typeof input === 'string') {
|
|
150
|
-
const formattedError = getFormattedJSONError(e, fs.readFileSync(input, 'utf8'));
|
|
151
|
-
throw new Error(formattedError);
|
|
152
|
-
}
|
|
153
|
-
throw new Error(`Error parsing agent configuration: ${e.message}`);
|
|
154
|
-
}
|
|
155
|
-
throw e;
|
|
74
|
+
if (!input || typeof input !== 'object' || !Array.isArray(input.crew)) {
|
|
75
|
+
throw new Error('Invalid crew configuration: expected an object with a crew array');
|
|
156
76
|
}
|
|
77
|
+
return input;
|
|
157
78
|
};
|
|
158
79
|
/**
|
|
159
80
|
* Parses the agent's configuration, validates the presence of the necessary API key,
|
|
160
81
|
* and creates an instance of the Agent with the appropriate settings.
|
|
161
82
|
*
|
|
162
83
|
* @param {string} agentName - The identifier for the AI agent to be initialized.
|
|
163
|
-
* @param {
|
|
84
|
+
* @param {AxCrewConfig} crewConfig - A JSON object with crew configuration.
|
|
164
85
|
* @param {FunctionRegistryType} functions - The functions available to the agent.
|
|
165
86
|
* @param {Object} state - The state object for the agent.
|
|
166
87
|
* @returns {Object} An object containing the Agents AI instance, its name, description, signature, functions and subAgentList.
|
|
@@ -174,18 +95,16 @@ const parseAgentConfig = async (agentName, crewConfig, functions, state) => {
|
|
|
174
95
|
if (!agentConfigData) {
|
|
175
96
|
throw new Error(`AI agent with name ${agentName} is not configured`);
|
|
176
97
|
}
|
|
177
|
-
//
|
|
178
|
-
const lower = agentConfigData.provider.toLowerCase();
|
|
179
|
-
if (!PROVIDER_CANONICAL.has(lower)) {
|
|
180
|
-
throw new Error(`AI provider ${agentConfigData.provider} is not supported. Use one of: ${Array.from(PROVIDER_CANONICAL).join(', ')}`);
|
|
181
|
-
}
|
|
98
|
+
// Normalize provider slug to lowercase and validate via Ax factory
|
|
99
|
+
const lower = String(agentConfigData.provider).toLowerCase();
|
|
182
100
|
const provider = lower;
|
|
183
|
-
//
|
|
101
|
+
// Resolve API key from user-supplied environment variable name
|
|
184
102
|
let apiKey = '';
|
|
185
103
|
if (agentConfigData.providerKeyName) {
|
|
186
|
-
|
|
104
|
+
const keyName = agentConfigData.providerKeyName;
|
|
105
|
+
apiKey = resolveApiKey(keyName) || '';
|
|
187
106
|
if (!apiKey) {
|
|
188
|
-
throw new Error(`API key for provider ${agentConfigData.provider} is not set in environment
|
|
107
|
+
throw new Error(`API key '${keyName}' for provider ${agentConfigData.provider} is not set in environment`);
|
|
189
108
|
}
|
|
190
109
|
}
|
|
191
110
|
else {
|
|
@@ -214,37 +133,20 @@ const parseAgentConfig = async (agentName, crewConfig, functions, state) => {
|
|
|
214
133
|
throw new Error(`Invalid apiURL provided: ${agentConfigData.apiURL}`);
|
|
215
134
|
}
|
|
216
135
|
}
|
|
217
|
-
// Forward provider-specific arguments
|
|
136
|
+
// Forward provider-specific arguments as-is; let Ax validate/ignore as needed
|
|
218
137
|
const providerArgs = agentConfigData.providerArgs;
|
|
219
|
-
if (
|
|
220
|
-
|
|
221
|
-
// If users supplied apiURL instead of resourceName, accept it (Ax supports full URL as resourceName)
|
|
222
|
-
if (!az.resourceName && agentConfigData.apiURL) {
|
|
223
|
-
az.resourceName = agentConfigData.apiURL;
|
|
224
|
-
}
|
|
225
|
-
Object.assign(aiArgs, az);
|
|
226
|
-
}
|
|
227
|
-
else if (provider === 'anthropic') {
|
|
228
|
-
const an = providerArgs ?? {};
|
|
229
|
-
Object.assign(aiArgs, an);
|
|
230
|
-
}
|
|
231
|
-
else if (provider === 'google-gemini') {
|
|
232
|
-
const g = providerArgs ?? {};
|
|
233
|
-
Object.assign(aiArgs, g);
|
|
234
|
-
}
|
|
235
|
-
else if (provider === 'openrouter') {
|
|
236
|
-
const o = providerArgs ?? {};
|
|
237
|
-
Object.assign(aiArgs, o);
|
|
138
|
+
if (providerArgs && typeof providerArgs === 'object') {
|
|
139
|
+
Object.assign(aiArgs, providerArgs);
|
|
238
140
|
}
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
141
|
+
// Validate provider by attempting instantiation; Ax will throw on unknown providers
|
|
142
|
+
let aiInstance;
|
|
143
|
+
try {
|
|
144
|
+
aiInstance = ai(aiArgs);
|
|
242
145
|
}
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
146
|
+
catch (e) {
|
|
147
|
+
const msg = e instanceof Error ? e.message : String(e);
|
|
148
|
+
throw new Error(`Unsupported provider '${provider}': ${msg}`);
|
|
246
149
|
}
|
|
247
|
-
const aiInstance = ai(aiArgs);
|
|
248
150
|
// If an mcpServers config is provided in the agent config, convert to functions
|
|
249
151
|
const mcpFunctions = await initializeMCPServers(agentConfigData);
|
|
250
152
|
// Prepare functions for the AI agent
|
|
@@ -284,3 +186,20 @@ const parseAgentConfig = async (agentName, crewConfig, functions, state) => {
|
|
|
284
186
|
}
|
|
285
187
|
};
|
|
286
188
|
export { parseAgentConfig, parseCrewConfig };
|
|
189
|
+
function resolveApiKey(varName) {
|
|
190
|
+
try {
|
|
191
|
+
// Prefer Node env when available
|
|
192
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
193
|
+
// @ts-ignore
|
|
194
|
+
if (typeof process !== 'undefined' && process?.env) {
|
|
195
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
196
|
+
// @ts-ignore
|
|
197
|
+
return process.env[varName];
|
|
198
|
+
}
|
|
199
|
+
// Fallback: allow global exposure in browser builds (e.g., injected at runtime)
|
|
200
|
+
return globalThis?.[varName];
|
|
201
|
+
}
|
|
202
|
+
catch {
|
|
203
|
+
return undefined;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { AxAI } from '@ax-llm/ax';
|
|
2
|
+
import type { AxCrewConfig } from '../types.js';
|
|
3
|
+
type BuildProviderArgs = {
|
|
4
|
+
provider: string;
|
|
5
|
+
apiKey: string;
|
|
6
|
+
config: any;
|
|
7
|
+
apiURL?: string;
|
|
8
|
+
providerArgs?: Record<string, unknown>;
|
|
9
|
+
options?: Record<string, unknown>;
|
|
10
|
+
};
|
|
11
|
+
export declare function instantiateProvider({ provider, apiKey, config, apiURL, providerArgs, options, }: BuildProviderArgs): AxAI<any>;
|
|
12
|
+
export declare function buildProvidersFromConfig(cfg: AxCrewConfig): AxAI<any>[];
|
|
13
|
+
export declare function discoverProvidersFromConfig(cfg: AxCrewConfig): string[];
|
|
14
|
+
export declare function listSelectableProviders(cfg: AxCrewConfig): string[];
|
|
15
|
+
export {};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { ai } from '@ax-llm/ax';
|
|
2
|
+
export function instantiateProvider({ provider, apiKey, config, apiURL, providerArgs, options, }) {
|
|
3
|
+
const args = { name: provider, apiKey, config, options };
|
|
4
|
+
if (apiURL)
|
|
5
|
+
args.apiURL = apiURL;
|
|
6
|
+
if (providerArgs && typeof providerArgs === 'object')
|
|
7
|
+
Object.assign(args, providerArgs);
|
|
8
|
+
return ai(args);
|
|
9
|
+
}
|
|
10
|
+
export function buildProvidersFromConfig(cfg) {
|
|
11
|
+
const services = [];
|
|
12
|
+
for (const agent of cfg.crew) {
|
|
13
|
+
const apiKeyName = agent.providerKeyName;
|
|
14
|
+
if (!apiKeyName)
|
|
15
|
+
throw new Error(`Provider key name is missing for agent ${agent.name}`);
|
|
16
|
+
const apiKey = resolveApiKey(apiKeyName) || '';
|
|
17
|
+
if (!apiKey)
|
|
18
|
+
throw new Error(`API key '${apiKeyName}' not set for agent ${agent.name}`);
|
|
19
|
+
const service = instantiateProvider({
|
|
20
|
+
provider: String(agent.provider).toLowerCase(),
|
|
21
|
+
apiKey,
|
|
22
|
+
config: agent.ai,
|
|
23
|
+
apiURL: agent.apiURL,
|
|
24
|
+
providerArgs: agent.providerArgs,
|
|
25
|
+
options: agent.options,
|
|
26
|
+
});
|
|
27
|
+
services.push(service);
|
|
28
|
+
}
|
|
29
|
+
return services;
|
|
30
|
+
}
|
|
31
|
+
// Provider discovery helpers consolidated here (previously in src/providers.ts)
|
|
32
|
+
export function discoverProvidersFromConfig(cfg) {
|
|
33
|
+
const providers = new Set();
|
|
34
|
+
for (const agent of cfg.crew) {
|
|
35
|
+
providers.add(String(agent.provider).toLowerCase());
|
|
36
|
+
}
|
|
37
|
+
return Array.from(providers);
|
|
38
|
+
}
|
|
39
|
+
export function listSelectableProviders(cfg) {
|
|
40
|
+
return discoverProvidersFromConfig(cfg);
|
|
41
|
+
}
|
|
42
|
+
function resolveApiKey(varName) {
|
|
43
|
+
try {
|
|
44
|
+
// Prefer Node env when available
|
|
45
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
46
|
+
// @ts-ignore
|
|
47
|
+
if (typeof process !== 'undefined' && process?.env) {
|
|
48
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
49
|
+
// @ts-ignore
|
|
50
|
+
return process.env[varName];
|
|
51
|
+
}
|
|
52
|
+
// Fallback: allow global exposure in browser builds (e.g., injected at runtime)
|
|
53
|
+
return globalThis?.[varName];
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
return undefined;
|
|
57
|
+
}
|
|
58
|
+
}
|
package/dist/agents/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { AxAgent, AxAI } from "@ax-llm/ax";
|
|
2
2
|
import type { AxSignature, AxAgentic, AxFunction, AxProgramForwardOptions, AxProgramStreamingForwardOptions, AxGenStreamingOut } from "@ax-llm/ax";
|
|
3
|
-
import type { StateInstance, FunctionRegistryType, UsageCost,
|
|
3
|
+
import type { StateInstance, FunctionRegistryType, UsageCost, AxCrewConfig, MCPTransportConfig } from "../types.js";
|
|
4
4
|
declare class StatefulAxAgent extends AxAgent<any, any> {
|
|
5
5
|
state: StateInstance;
|
|
6
6
|
axai: any;
|
|
@@ -39,7 +39,23 @@ declare class StatefulAxAgent extends AxAgent<any, any> {
|
|
|
39
39
|
resetMetrics(): void;
|
|
40
40
|
}
|
|
41
41
|
/**
|
|
42
|
-
*
|
|
42
|
+
* AxCrew orchestrates a set of Ax agents that share state,
|
|
43
|
+
* tools (functions), optional MCP servers, streaming, and a built-in metrics
|
|
44
|
+
* registry for tokens, requests, and estimated cost.
|
|
45
|
+
*
|
|
46
|
+
* Typical usage:
|
|
47
|
+
* const crew = new AxCrew(config, AxCrewFunctions)
|
|
48
|
+
* await crew.addAllAgents()
|
|
49
|
+
* const planner = crew.agents?.get("Planner")
|
|
50
|
+
* const res = await planner?.forward({ task: "Plan something" })
|
|
51
|
+
*
|
|
52
|
+
* Key behaviors:
|
|
53
|
+
* - Validates and instantiates agents from a config-first model
|
|
54
|
+
* - Shares a mutable state object across all agents in the crew
|
|
55
|
+
* - Supports sub-agents and a function registry per agent
|
|
56
|
+
* - Tracks per-agent and crew-level metrics via MetricsRegistry
|
|
57
|
+
* - Provides helpers to add agents (individually, a subset, or all) and
|
|
58
|
+
* to reset metrics/costs when needed
|
|
43
59
|
*/
|
|
44
60
|
declare class AxCrew {
|
|
45
61
|
private crewConfig;
|
|
@@ -49,11 +65,11 @@ declare class AxCrew {
|
|
|
49
65
|
state: StateInstance;
|
|
50
66
|
/**
|
|
51
67
|
* Creates an instance of AxCrew.
|
|
52
|
-
* @param {
|
|
68
|
+
* @param {AxCrewConfig} crewConfig - JSON object with crew configuration.
|
|
53
69
|
* @param {FunctionRegistryType} [functionsRegistry={}] - The registry of functions to use in the crew.
|
|
54
70
|
* @param {string} [crewId=uuidv4()] - The unique identifier for the crew.
|
|
55
71
|
*/
|
|
56
|
-
constructor(crewConfig:
|
|
72
|
+
constructor(crewConfig: AxCrewConfig, functionsRegistry?: FunctionRegistryType, crewId?: string);
|
|
57
73
|
/**
|
|
58
74
|
* Factory function for creating an agent.
|
|
59
75
|
* @param {string} agentName - The name of the agent to create.
|
package/dist/agents/index.js
CHANGED
|
@@ -176,7 +176,23 @@ class StatefulAxAgent extends AxAgent {
|
|
|
176
176
|
}
|
|
177
177
|
}
|
|
178
178
|
/**
|
|
179
|
-
*
|
|
179
|
+
* AxCrew orchestrates a set of Ax agents that share state,
|
|
180
|
+
* tools (functions), optional MCP servers, streaming, and a built-in metrics
|
|
181
|
+
* registry for tokens, requests, and estimated cost.
|
|
182
|
+
*
|
|
183
|
+
* Typical usage:
|
|
184
|
+
* const crew = new AxCrew(config, AxCrewFunctions)
|
|
185
|
+
* await crew.addAllAgents()
|
|
186
|
+
* const planner = crew.agents?.get("Planner")
|
|
187
|
+
* const res = await planner?.forward({ task: "Plan something" })
|
|
188
|
+
*
|
|
189
|
+
* Key behaviors:
|
|
190
|
+
* - Validates and instantiates agents from a config-first model
|
|
191
|
+
* - Shares a mutable state object across all agents in the crew
|
|
192
|
+
* - Supports sub-agents and a function registry per agent
|
|
193
|
+
* - Tracks per-agent and crew-level metrics via MetricsRegistry
|
|
194
|
+
* - Provides helpers to add agents (individually, a subset, or all) and
|
|
195
|
+
* to reset metrics/costs when needed
|
|
180
196
|
*/
|
|
181
197
|
class AxCrew {
|
|
182
198
|
crewConfig;
|
|
@@ -186,7 +202,7 @@ class AxCrew {
|
|
|
186
202
|
state;
|
|
187
203
|
/**
|
|
188
204
|
* Creates an instance of AxCrew.
|
|
189
|
-
* @param {
|
|
205
|
+
* @param {AxCrewConfig} crewConfig - JSON object with crew configuration.
|
|
190
206
|
* @param {FunctionRegistryType} [functionsRegistry={}] - The registry of functions to use in the crew.
|
|
191
207
|
* @param {string} [crewId=uuidv4()] - The unique identifier for the crew.
|
|
192
208
|
*/
|
|
@@ -196,7 +212,7 @@ class AxCrew {
|
|
|
196
212
|
throw new Error('Invalid crew configuration');
|
|
197
213
|
}
|
|
198
214
|
// Validate each agent in the crew
|
|
199
|
-
crewConfig.crew.forEach(agent => {
|
|
215
|
+
crewConfig.crew.forEach((agent) => {
|
|
200
216
|
if (!agent.name || agent.name.trim() === '') {
|
|
201
217
|
throw new Error('Agent name cannot be empty');
|
|
202
218
|
}
|
|
@@ -1,3 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Built-in function registry for AxCrew agents.
|
|
3
|
+
*
|
|
4
|
+
* Contains common utility tools/functions that can be referenced by name from
|
|
5
|
+
* agent configs (e.g., "functions": ["CurrentDateTime", "DaysBetweenDates"]).
|
|
6
|
+
* You can pass this object to the AxCrew constructor or merge with your
|
|
7
|
+
* own registry.
|
|
8
|
+
* Example:
|
|
9
|
+
* const crew = new AxCrew(config, AxCrewFunctions); or
|
|
10
|
+
* const crew = new AxCrew(config, { ...AxCrewFunctions, ...myFunctions });
|
|
11
|
+
*/
|
|
1
12
|
declare const AxCrewFunctions: {
|
|
2
13
|
CurrentDateTime: import("@ax-llm/ax").AxFunction;
|
|
3
14
|
DaysBetweenDates: import("@ax-llm/ax").AxFunction;
|
package/dist/functions/index.js
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
import { CurrentDateTime, DaysBetweenDates } from './dateTime.js';
|
|
2
|
-
|
|
2
|
+
/**
|
|
3
|
+
* Built-in function registry for AxCrew agents.
|
|
4
|
+
*
|
|
5
|
+
* Contains common utility tools/functions that can be referenced by name from
|
|
6
|
+
* agent configs (e.g., "functions": ["CurrentDateTime", "DaysBetweenDates"]).
|
|
7
|
+
* You can pass this object to the AxCrew constructor or merge with your
|
|
8
|
+
* own registry.
|
|
9
|
+
* Example:
|
|
10
|
+
* const crew = new AxCrew(config, AxCrewFunctions); or
|
|
11
|
+
* const crew = new AxCrew(config, { ...AxCrewFunctions, ...myFunctions });
|
|
12
|
+
*/
|
|
3
13
|
const AxCrewFunctions = {
|
|
4
14
|
CurrentDateTime,
|
|
5
15
|
DaysBetweenDates
|
package/dist/index.d.ts
CHANGED
|
@@ -1,15 +1,32 @@
|
|
|
1
1
|
import { AxCrew } from './agents/index.js';
|
|
2
2
|
import { AxCrewFunctions } from './functions/index.js';
|
|
3
|
-
import type {
|
|
3
|
+
import type { AxCrewConfig, AgentConfig } from './types.js';
|
|
4
4
|
import type { UsageCost, AggregatedMetrics, AggregatedCosts, StateInstance, FunctionRegistryType } from './types.js';
|
|
5
|
+
/**
|
|
6
|
+
* Metrics types and helpers for request counts, token usage, and estimated cost.
|
|
7
|
+
*
|
|
8
|
+
* Re-exports the metrics module for convenience:
|
|
9
|
+
* - Types: TokenUsage, MetricsSnapshot, etc.
|
|
10
|
+
* - Namespace: MetricsRegistry (record/snapshot/reset helpers)
|
|
11
|
+
*/
|
|
5
12
|
export * from './metrics/index.js';
|
|
13
|
+
/**
|
|
14
|
+
* MetricsRegistry provides functions to record requests, tokens, and cost,
|
|
15
|
+
* and to snapshot/reset metrics at agent or crew granularity.
|
|
16
|
+
*/
|
|
6
17
|
export { MetricsRegistry } from './metrics/index.js';
|
|
7
18
|
/**
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
|
|
19
|
+
* Create and manage a crew of Ax agents that share state and metrics.
|
|
20
|
+
* See the `AxCrew` class for full documentation.
|
|
21
|
+
*/
|
|
22
|
+
declare const _AxCrew: typeof AxCrew;
|
|
23
|
+
/**
|
|
24
|
+
* Built-in function registry with common tools that can be referenced by name
|
|
25
|
+
* from agent configs, or extended with your own functions.
|
|
11
26
|
*/
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
27
|
+
declare const _AxCrewFunctions: typeof AxCrewFunctions;
|
|
28
|
+
export {
|
|
29
|
+
/** See class JSDoc on the `AxCrew` implementation. */
|
|
30
|
+
_AxCrew as AxCrew,
|
|
31
|
+
/** Built-in function registry; see file docs in `src/functions/index.ts`. */
|
|
32
|
+
_AxCrewFunctions as AxCrewFunctions, FunctionRegistryType, type AggregatedMetrics, type AggregatedCosts, type AgentConfig, type AxCrewConfig, type StateInstance, type UsageCost, };
|