@amitdeshmukh/ax-crew 3.3.2 → 3.3.3
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 +12 -0
- package/README.md +80 -0
- package/dist/agents/agentConfig.d.ts +6 -7
- package/dist/index.d.ts +4 -1
- package/dist/state/index.d.ts +9 -11
- package/dist/state/index.js +16 -21
- package/index.d.ts +89 -4
- package/package.json +1 -1
- package/src/agents/agentConfig.ts +4 -6
- package/src/agents/index.ts +1 -1
- package/src/index.ts +9 -1
- package/src/state/index.ts +23 -28
- package/test1.ts +67 -0
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,18 @@ This Changelog format is based on [Keep a Changelog]
|
|
|
5
5
|
adheres to [Semantic Versioning](https://semver.org/spec/
|
|
6
6
|
v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [3.3.3] - 2024-12-12
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
- TypeScript declaration file (`index.d.ts`)
|
|
12
|
+
- Improved type definitions for:
|
|
13
|
+
- `AxCrew` class and its methods
|
|
14
|
+
- `StatefulAxAgent` class
|
|
15
|
+
- State management interfaces
|
|
16
|
+
- Function registry types
|
|
17
|
+
- Configuration types
|
|
18
|
+
- TypeScript documentation in `README.md` with example
|
|
19
|
+
|
|
8
20
|
## [3.3.1] - 2024-12-11
|
|
9
21
|
|
|
10
22
|
### Changed
|
package/README.md
CHANGED
|
@@ -22,6 +22,86 @@ AxLLM is a peer dependency, so you will need to install it separately.
|
|
|
22
22
|
npm install @ax-llm/ax@latest
|
|
23
23
|
```
|
|
24
24
|
|
|
25
|
+
### TypeScript Support
|
|
26
|
+
This package includes TypeScript declarations and provides full type safety. Here's how to use it with TypeScript:
|
|
27
|
+
|
|
28
|
+
```typescript
|
|
29
|
+
import { AxCrew, AxCrewFunctions, FunctionRegistryType, StateInstance } from '@amitdeshmukh/ax-crew';
|
|
30
|
+
import type { AxFunction } from '@ax-llm/ax';
|
|
31
|
+
|
|
32
|
+
// Type-safe configuration
|
|
33
|
+
const config = {
|
|
34
|
+
crew: [{
|
|
35
|
+
name: "Planner",
|
|
36
|
+
description: "Creates a plan to complete a task",
|
|
37
|
+
signature: "task:string \"a task to be completed\" -> plan:string \"a plan to execute the task\"",
|
|
38
|
+
provider: "google-gemini",
|
|
39
|
+
providerKeyName: "GEMINI_API_KEY",
|
|
40
|
+
ai: {
|
|
41
|
+
model: "gemini-1.5-pro",
|
|
42
|
+
temperature: 0
|
|
43
|
+
}
|
|
44
|
+
}]
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
// Create custom functions with type safety
|
|
48
|
+
class MyCustomFunction {
|
|
49
|
+
constructor(private state: Record<string, any>) {}
|
|
50
|
+
|
|
51
|
+
toFunction(): AxFunction {
|
|
52
|
+
return {
|
|
53
|
+
name: 'MyCustomFunction',
|
|
54
|
+
description: 'Does something useful',
|
|
55
|
+
parameters: {
|
|
56
|
+
type: 'object',
|
|
57
|
+
properties: {
|
|
58
|
+
inputParam: { type: 'string', description: "input to the function" }
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
func: async ({ inputParam }) => {
|
|
62
|
+
// Implementation
|
|
63
|
+
return inputParam;
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Type-safe function registry
|
|
70
|
+
const myFunctions: FunctionRegistryType = {
|
|
71
|
+
MyCustomFunction
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
// Create crew with type checking
|
|
75
|
+
const crew = new AxCrew(config, myFunctions);
|
|
76
|
+
|
|
77
|
+
// Type-safe state management
|
|
78
|
+
crew.state.set('key', 'value');
|
|
79
|
+
const value: string = crew.state.get('key');
|
|
80
|
+
|
|
81
|
+
// Type-safe agent management
|
|
82
|
+
const agents = crew.addAgentsToCrew(['Planner']);
|
|
83
|
+
const planner = agents?.get('Planner');
|
|
84
|
+
|
|
85
|
+
if (planner) {
|
|
86
|
+
// Type-safe agent usage
|
|
87
|
+
const response = await planner.forward({ task: "Plan something" });
|
|
88
|
+
const cost = planner.getUsageCost();
|
|
89
|
+
|
|
90
|
+
if (cost) {
|
|
91
|
+
console.log(`Total cost: $${cost.totalCost}`);
|
|
92
|
+
console.log(`Total tokens: ${cost.tokenMetrics.totalTokens}`);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
Key TypeScript features:
|
|
98
|
+
- Full type definitions for all classes, methods, and properties
|
|
99
|
+
- Type-safe configuration objects
|
|
100
|
+
- Proper typing for function registries and custom functions
|
|
101
|
+
- Type checking for state management
|
|
102
|
+
- Comprehensive type safety for agent operations and responses
|
|
103
|
+
- Usage cost tracking with proper types
|
|
104
|
+
|
|
25
105
|
### Environment Setup
|
|
26
106
|
Refer to the [.env.example](.env.example) file for the required environment variables. These will need to be set in the environment where the agents are run.
|
|
27
107
|
|
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
import type { AxModelConfig, AxFunction, AxSignature } from '@ax-llm/ax';
|
|
2
2
|
import { FunctionRegistryType } from '../functions/index.js';
|
|
3
|
-
|
|
4
|
-
model: string;
|
|
5
|
-
};
|
|
6
|
-
interface AgentConfig {
|
|
3
|
+
export interface AgentConfig {
|
|
7
4
|
name: string;
|
|
8
5
|
description: string;
|
|
9
|
-
signature: AxSignature;
|
|
6
|
+
signature: string | AxSignature;
|
|
10
7
|
provider: string;
|
|
11
8
|
providerKeyName?: string;
|
|
12
|
-
ai:
|
|
9
|
+
ai: AxModelConfig & {
|
|
10
|
+
model: string;
|
|
11
|
+
};
|
|
13
12
|
debug?: boolean;
|
|
14
13
|
apiURL?: string;
|
|
15
14
|
options?: Record<string, any>;
|
|
@@ -37,7 +36,7 @@ declare const getAgentConfigParams: (agentName: string, agentConfig: AgentConfig
|
|
|
37
36
|
ai: any;
|
|
38
37
|
name: string;
|
|
39
38
|
description: string;
|
|
40
|
-
signature: AxSignature;
|
|
39
|
+
signature: string | AxSignature;
|
|
41
40
|
functions: (AxFunction | (new (state: Record<string, any>) => {
|
|
42
41
|
toFunction: () => AxFunction;
|
|
43
42
|
}) | undefined)[];
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
1
|
import { AxCrew } from './agents/index.js';
|
|
2
2
|
import { AxCrewFunctions, FunctionRegistryType } from './functions/index.js';
|
|
3
|
-
|
|
3
|
+
import type { UsageCost } from './agents/agentUseCosts.js';
|
|
4
|
+
import type { AgentConfig, AgentConfigInput } from './agents/agentConfig.js';
|
|
5
|
+
import type { StateInstance } from './state/index.js';
|
|
6
|
+
export { AxCrew, AxCrewFunctions, FunctionRegistryType, type UsageCost, type AgentConfig, type AgentConfigInput, type StateInstance };
|
package/dist/state/index.d.ts
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
|
-
interface StateInstance {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
getId: () => string;
|
|
8
|
-
reset: () => void;
|
|
1
|
+
export interface StateInstance {
|
|
2
|
+
reset(): void;
|
|
3
|
+
set(key: string, value: any): void;
|
|
4
|
+
get(key: string): any;
|
|
5
|
+
getAll(): Record<string, any>;
|
|
6
|
+
[key: string]: any;
|
|
9
7
|
}
|
|
10
|
-
declare
|
|
11
|
-
declare
|
|
12
|
-
export { createState, getState
|
|
8
|
+
declare function createState(id: string): StateInstance;
|
|
9
|
+
declare function getState(id: string): StateInstance | undefined;
|
|
10
|
+
export { createState, getState };
|
package/dist/state/index.js
CHANGED
|
@@ -1,32 +1,27 @@
|
|
|
1
1
|
const stateInstances = {};
|
|
2
|
-
|
|
3
|
-
if (!id) {
|
|
4
|
-
throw new Error('An ID is required to create a new state instance.');
|
|
5
|
-
}
|
|
2
|
+
function createState(id) {
|
|
6
3
|
if (stateInstances[id]) {
|
|
7
4
|
return stateInstances[id];
|
|
8
5
|
}
|
|
9
|
-
let
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
},
|
|
14
|
-
get: (key) => {
|
|
15
|
-
return data[key];
|
|
6
|
+
let state = {};
|
|
7
|
+
const instance = {
|
|
8
|
+
reset() {
|
|
9
|
+
state = {};
|
|
16
10
|
},
|
|
17
|
-
|
|
18
|
-
|
|
11
|
+
set(key, value) {
|
|
12
|
+
state[key] = value;
|
|
19
13
|
},
|
|
20
|
-
|
|
21
|
-
return
|
|
14
|
+
get(key) {
|
|
15
|
+
return state[key];
|
|
22
16
|
},
|
|
23
|
-
|
|
24
|
-
|
|
17
|
+
getAll() {
|
|
18
|
+
return { ...state };
|
|
25
19
|
}
|
|
26
20
|
};
|
|
21
|
+
stateInstances[id] = instance;
|
|
22
|
+
return instance;
|
|
23
|
+
}
|
|
24
|
+
function getState(id) {
|
|
27
25
|
return stateInstances[id];
|
|
28
|
-
}
|
|
29
|
-
const getState = (id) => {
|
|
30
|
-
return stateInstances[id];
|
|
31
|
-
};
|
|
26
|
+
}
|
|
32
27
|
export { createState, getState };
|
package/index.d.ts
CHANGED
|
@@ -1,7 +1,92 @@
|
|
|
1
|
+
import { AxAI, AxAgentic, AxFunction, AxSignature, AxProgramForwardOptions, AxModelConfig, AxAgent } from "@ax-llm/ax";
|
|
2
|
+
|
|
3
|
+
export interface UsageCost {
|
|
4
|
+
promptCost: number;
|
|
5
|
+
completionCost: number;
|
|
6
|
+
totalCost: number;
|
|
7
|
+
tokenMetrics: {
|
|
8
|
+
promptTokens: number;
|
|
9
|
+
completionTokens: number;
|
|
10
|
+
totalTokens: number;
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface StateInstance {
|
|
15
|
+
reset(): void;
|
|
16
|
+
set(key: string, value: any): void;
|
|
17
|
+
get(key: string): any;
|
|
18
|
+
getAll(): Record<string, any>;
|
|
19
|
+
[key: string]: any;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export type FunctionRegistryType = {
|
|
23
|
+
[key: string]: AxFunction | { new(state: Record<string, any>): { toFunction: () => AxFunction } };
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export interface AgentConfig {
|
|
27
|
+
name: string;
|
|
28
|
+
description: string;
|
|
29
|
+
signature: AxSignature;
|
|
30
|
+
provider: string;
|
|
31
|
+
providerKeyName?: string;
|
|
32
|
+
ai: AxModelConfig & { model: string };
|
|
33
|
+
debug?: boolean;
|
|
34
|
+
apiURL?: string;
|
|
35
|
+
options?: Record<string, any>;
|
|
36
|
+
functions?: string[];
|
|
37
|
+
agents?: string[];
|
|
38
|
+
examples?: Array<Record<string, any>>;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export type AgentConfigInput = string | { crew: AgentConfig[] };
|
|
42
|
+
|
|
43
|
+
export class StatefulAxAgent extends AxAgent<Record<string, any>, Record<string, any>> {
|
|
44
|
+
state: StateInstance;
|
|
45
|
+
axai: AxAI;
|
|
46
|
+
|
|
47
|
+
constructor(
|
|
48
|
+
ai: AxAI,
|
|
49
|
+
options: Readonly<{
|
|
50
|
+
name: string;
|
|
51
|
+
description: string;
|
|
52
|
+
signature: string | AxSignature;
|
|
53
|
+
agents?: AxAgentic[] | undefined;
|
|
54
|
+
functions?: (AxFunction | (() => AxFunction))[] | undefined;
|
|
55
|
+
examples?: Array<Record<string, any>> | undefined;
|
|
56
|
+
}>,
|
|
57
|
+
state: StateInstance
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
forward(
|
|
61
|
+
input: Record<string, any>,
|
|
62
|
+
options?: Readonly<AxProgramForwardOptions>
|
|
63
|
+
): Promise<Record<string, any>>;
|
|
64
|
+
|
|
65
|
+
getUsageCost(): UsageCost | null;
|
|
66
|
+
|
|
67
|
+
setExamples(examples: Array<Record<string, any>>): void;
|
|
68
|
+
}
|
|
69
|
+
|
|
1
70
|
export class AxCrew {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
71
|
+
private agentConfig: AgentConfigInput;
|
|
72
|
+
functionsRegistry: FunctionRegistryType;
|
|
73
|
+
crewId: string;
|
|
74
|
+
agents: Map<string, StatefulAxAgent> | null;
|
|
75
|
+
state: StateInstance;
|
|
76
|
+
|
|
77
|
+
constructor(
|
|
78
|
+
agentConfig: AgentConfigInput,
|
|
79
|
+
functionsRegistry?: FunctionRegistryType,
|
|
80
|
+
crewId?: string
|
|
81
|
+
);
|
|
82
|
+
|
|
83
|
+
createAgent(agentName: string): StatefulAxAgent;
|
|
84
|
+
addAgent(agentName: string): void;
|
|
85
|
+
addAgentsToCrew(agentNames: string[]): Map<string, StatefulAxAgent> | null;
|
|
6
86
|
destroy(): void;
|
|
7
87
|
}
|
|
88
|
+
|
|
89
|
+
export const AxCrewFunctions: {
|
|
90
|
+
CurrentDateTime: { new(state: Record<string, any>): { toFunction: () => AxFunction } };
|
|
91
|
+
DaysBetweenDates: { new(state: Record<string, any>): { toFunction: () => AxFunction } };
|
|
92
|
+
};
|
package/package.json
CHANGED
|
@@ -20,17 +20,15 @@ const AIConstructors: Record<string, any> = {
|
|
|
20
20
|
'together': AxAITogether
|
|
21
21
|
};
|
|
22
22
|
|
|
23
|
-
type ExtendedAxModelConfig = AxModelConfig & {
|
|
24
|
-
model: string;
|
|
25
|
-
};
|
|
26
23
|
|
|
27
|
-
|
|
24
|
+
|
|
25
|
+
export interface AgentConfig {
|
|
28
26
|
name: string;
|
|
29
27
|
description: string;
|
|
30
|
-
signature: AxSignature;
|
|
28
|
+
signature: string | AxSignature;
|
|
31
29
|
provider: string;
|
|
32
30
|
providerKeyName?: string;
|
|
33
|
-
ai:
|
|
31
|
+
ai: AxModelConfig & { model: string };
|
|
34
32
|
debug?: boolean;
|
|
35
33
|
apiURL?: string;
|
|
36
34
|
options?: Record<string, any>;
|
package/src/agents/index.ts
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,8 +1,16 @@
|
|
|
1
1
|
import { AxCrew } from './agents/index.js';
|
|
2
2
|
import { AxCrewFunctions, FunctionRegistryType } from './functions/index.js';
|
|
3
|
+
import type { UsageCost } from './agents/agentUseCosts.js';
|
|
4
|
+
import type { AgentConfig, AgentConfigInput } from './agents/agentConfig.js';
|
|
5
|
+
import type { StateInstance } from './state/index.js';
|
|
3
6
|
|
|
4
7
|
export {
|
|
5
8
|
AxCrew,
|
|
6
9
|
AxCrewFunctions,
|
|
7
|
-
FunctionRegistryType
|
|
10
|
+
FunctionRegistryType,
|
|
11
|
+
// Type exports
|
|
12
|
+
type UsageCost,
|
|
13
|
+
type AgentConfig,
|
|
14
|
+
type AgentConfigInput,
|
|
15
|
+
type StateInstance
|
|
8
16
|
};
|
package/src/state/index.ts
CHANGED
|
@@ -1,46 +1,41 @@
|
|
|
1
|
-
interface StateInstance {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
export interface StateInstance {
|
|
2
|
+
reset(): void;
|
|
3
|
+
set(key: string, value: any): void;
|
|
4
|
+
get(key: string): any;
|
|
5
|
+
getAll(): Record<string, any>;
|
|
6
|
+
[key: string]: any;
|
|
7
7
|
}
|
|
8
8
|
|
|
9
9
|
const stateInstances: { [id: string]: StateInstance } = {};
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
if (!id) {
|
|
13
|
-
throw new Error('An ID is required to create a new state instance.');
|
|
14
|
-
}
|
|
11
|
+
function createState(id: string): StateInstance {
|
|
15
12
|
if (stateInstances[id]) {
|
|
16
13
|
return stateInstances[id];
|
|
17
14
|
}
|
|
18
15
|
|
|
19
|
-
let
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
},
|
|
25
|
-
get: (key) => {
|
|
26
|
-
return data[key];
|
|
16
|
+
let state: Record<string, any> = {};
|
|
17
|
+
|
|
18
|
+
const instance: StateInstance = {
|
|
19
|
+
reset() {
|
|
20
|
+
state = {};
|
|
27
21
|
},
|
|
28
|
-
|
|
29
|
-
|
|
22
|
+
set(key: string, value: any) {
|
|
23
|
+
state[key] = value;
|
|
30
24
|
},
|
|
31
|
-
|
|
32
|
-
return
|
|
25
|
+
get(key: string) {
|
|
26
|
+
return state[key];
|
|
33
27
|
},
|
|
34
|
-
|
|
35
|
-
|
|
28
|
+
getAll() {
|
|
29
|
+
return { ...state };
|
|
36
30
|
}
|
|
37
31
|
};
|
|
38
32
|
|
|
39
|
-
|
|
40
|
-
|
|
33
|
+
stateInstances[id] = instance;
|
|
34
|
+
return instance;
|
|
35
|
+
}
|
|
41
36
|
|
|
42
|
-
|
|
37
|
+
function getState(id: string): StateInstance | undefined {
|
|
43
38
|
return stateInstances[id];
|
|
44
39
|
}
|
|
45
40
|
|
|
46
|
-
export { createState, getState
|
|
41
|
+
export { createState, getState };
|
package/test1.ts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { AxCrew, AxCrewFunctions, FunctionRegistryType, StateInstance } from './dist/index.js';
|
|
2
|
+
import type { AxFunction } from '@ax-llm/ax';
|
|
3
|
+
|
|
4
|
+
// Type-safe configuration
|
|
5
|
+
const config = {
|
|
6
|
+
crew: [{
|
|
7
|
+
name: "Planner",
|
|
8
|
+
description: "Creates a plan to complete a task",
|
|
9
|
+
signature: "task:string \"a task to be completed\" -> plan:string \"a plan to execute the task\"",
|
|
10
|
+
provider: "google-gemini",
|
|
11
|
+
providerKeyName: "GEMINI_API_KEY",
|
|
12
|
+
ai: {
|
|
13
|
+
model: "gemini-1.5-pro",
|
|
14
|
+
temperature: 0
|
|
15
|
+
}
|
|
16
|
+
}]
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
// Create custom functions with type safety
|
|
20
|
+
class MyCustomFunction {
|
|
21
|
+
constructor(private state: Record<string, any>) {}
|
|
22
|
+
|
|
23
|
+
toFunction(): AxFunction {
|
|
24
|
+
return {
|
|
25
|
+
name: 'MyCustomFunction',
|
|
26
|
+
description: 'Does something useful',
|
|
27
|
+
parameters: {
|
|
28
|
+
type: 'object',
|
|
29
|
+
properties: {
|
|
30
|
+
input: { type: 'string', description: "input to the function" }
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
func: async ({ input }) => {
|
|
34
|
+
// Implementation
|
|
35
|
+
return input;
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Type-safe function registry
|
|
42
|
+
const myFunctions: FunctionRegistryType = {
|
|
43
|
+
MyCustomFunction
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
// Create crew with type checking
|
|
47
|
+
const crew = new AxCrew(config, myFunctions);
|
|
48
|
+
|
|
49
|
+
// Type-safe state management
|
|
50
|
+
crew.state.set('key', 'value');
|
|
51
|
+
const value: string = crew.state.get('key');
|
|
52
|
+
|
|
53
|
+
// Type-safe agent management
|
|
54
|
+
const agents = crew.addAgentsToCrew(['Planner']);
|
|
55
|
+
const planner = agents?.get('Planner');
|
|
56
|
+
|
|
57
|
+
if (planner) {
|
|
58
|
+
// Type-safe agent usage
|
|
59
|
+
const response = await planner.forward({ task: "Plan something" });
|
|
60
|
+
console.log(response.plan);
|
|
61
|
+
const cost = planner.getUsageCost();
|
|
62
|
+
|
|
63
|
+
if (cost) {
|
|
64
|
+
console.log(`Total cost: $${cost.totalCost}`);
|
|
65
|
+
console.log(`Total tokens: ${cost.tokenMetrics.totalTokens}`);
|
|
66
|
+
}
|
|
67
|
+
}
|