@amitdeshmukh/ax-crew 8.7.3 → 9.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,95 @@
1
+ import { AxSignature as AxSignatureClass } from "@ax-llm/ax";
2
+ import type { AxFunction } from "@ax-llm/ax";
3
+ import type { AxCrewConfig } from "../types.js";
4
+ import type { StatefulAxAgent } from "./statefulAgent.js";
5
+ import { parseCrewConfig } from "./agentConfig.js";
6
+
7
+ /**
8
+ * Lightweight proxy that stands in for a real agent in the crew's agent map.
9
+ * It exposes the same `getFunction()` interface (built from the crew config)
10
+ * but defers the expensive `createAgent()` call — and therefore MCP server
11
+ * startup — until the Manager actually delegates to it.
12
+ *
13
+ * Usage: `crew.addLazyAgent("CreateChart")` instead of `crew.addAgent("CreateChart")`
14
+ */
15
+ class LazyStatefulAxAgent {
16
+ private realAgent: StatefulAxAgent | null = null;
17
+ private crewRef: any; // AxCrew — forward-declared to avoid circular ref
18
+ private agentName: string;
19
+ private description: string;
20
+ private signatureStr: string;
21
+ private func: AxFunction;
22
+ private _id: string = "lazy";
23
+
24
+ constructor(crewRef: any, agentName: string, crewConfig: AxCrewConfig) {
25
+ this.crewRef = crewRef;
26
+ this.agentName = agentName;
27
+
28
+ const agentDef = parseCrewConfig(crewConfig).crew.find(
29
+ (a) => a.name === agentName
30
+ );
31
+ if (!agentDef) {
32
+ throw new Error(`Agent "${agentName}" not found in crew config`);
33
+ }
34
+
35
+ this.description = agentDef.description;
36
+ this.signatureStr = agentDef.signature as string;
37
+
38
+ // Build the tool schema from the signature's input fields
39
+ const sig = new AxSignatureClass(this.signatureStr);
40
+ const parameters = sig.toInputJSONSchema();
41
+
42
+ this.func = {
43
+ name: agentName.replace(/[^a-zA-Z0-9_]/g, "_").toLowerCase(),
44
+ description: this.description,
45
+ parameters,
46
+ func: async (args?: any) => {
47
+ const agent = await this.resolve();
48
+ return agent.forward(args);
49
+ },
50
+ };
51
+ }
52
+
53
+ private async resolve(): Promise<StatefulAxAgent> {
54
+ if (!this.realAgent) {
55
+ const agent = await this.crewRef.createAgent(this.agentName) as StatefulAxAgent;
56
+ agent.setId(this._id);
57
+ this.realAgent = agent;
58
+ }
59
+ return this.realAgent!;
60
+ }
61
+
62
+ // AxAgentic interface
63
+ getFunction(): AxFunction {
64
+ return this.func;
65
+ }
66
+
67
+ getSignature() {
68
+ return new AxSignatureClass(this.signatureStr);
69
+ }
70
+
71
+ // AxProgrammable / AxTunable stubs
72
+ getId(): string { return this._id; }
73
+ setId(id: string): void { this._id = id; }
74
+ getTraces(): any[] { return this.realAgent?.getTraces() ?? []; }
75
+ setDemos(): void { /* no-op until resolved */ }
76
+ getUsage(): any[] { return this.realAgent?.getUsage() ?? []; }
77
+ resetUsage(): void { this.realAgent?.resetUsage(); }
78
+
79
+ // Forward / streaming — resolve on demand
80
+ async forward(...args: any[]): Promise<any> {
81
+ const agent = await this.resolve();
82
+ return (agent as any).forward(...args);
83
+ }
84
+ streamingForward(...args: any[]): any {
85
+ // Must be sync to match the interface, so we wrap in an async generator
86
+ const self = this;
87
+ async function* lazyStream() {
88
+ const agent = await self.resolve();
89
+ yield* (agent as any).streamingForward(...args);
90
+ }
91
+ return lazyStream();
92
+ }
93
+ }
94
+
95
+ export { LazyStatefulAxAgent };