@agentionai/agents 0.3.0-beta
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/README.md +517 -0
- package/dist/agents/Agent.d.ts +29 -0
- package/dist/agents/Agent.js +28 -0
- package/dist/agents/AgentConfig.d.ts +118 -0
- package/dist/agents/AgentConfig.js +3 -0
- package/dist/agents/AgentEvent.d.ts +18 -0
- package/dist/agents/AgentEvent.js +26 -0
- package/dist/agents/BaseAgent.d.ts +82 -0
- package/dist/agents/BaseAgent.js +121 -0
- package/dist/agents/anthropic/ClaudeAgent.d.ts +46 -0
- package/dist/agents/anthropic/ClaudeAgent.js +262 -0
- package/dist/agents/errors/AgentError.d.ts +47 -0
- package/dist/agents/errors/AgentError.js +74 -0
- package/dist/agents/google/GeminiAgent.d.ts +63 -0
- package/dist/agents/google/GeminiAgent.js +395 -0
- package/dist/agents/mistral/MistralAgent.d.ts +47 -0
- package/dist/agents/mistral/MistralAgent.js +313 -0
- package/dist/agents/model-types.d.ts +30 -0
- package/dist/agents/model-types.js +8 -0
- package/dist/agents/openai/OpenAiAgent.d.ts +48 -0
- package/dist/agents/openai/OpenAiAgent.js +338 -0
- package/dist/chunkers/Chunker.d.ts +53 -0
- package/dist/chunkers/Chunker.js +174 -0
- package/dist/chunkers/RecursiveChunker.d.ts +52 -0
- package/dist/chunkers/RecursiveChunker.js +166 -0
- package/dist/chunkers/TextChunker.d.ts +27 -0
- package/dist/chunkers/TextChunker.js +50 -0
- package/dist/chunkers/TokenChunker.d.ts +60 -0
- package/dist/chunkers/TokenChunker.js +176 -0
- package/dist/chunkers/index.d.ts +6 -0
- package/dist/chunkers/index.js +14 -0
- package/dist/chunkers/types.d.ts +95 -0
- package/dist/chunkers/types.js +3 -0
- package/dist/graph/AgentGraph.d.ts +99 -0
- package/dist/graph/AgentGraph.js +115 -0
- package/dist/graph/BaseExecutor.d.ts +86 -0
- package/dist/graph/BaseExecutor.js +61 -0
- package/dist/graph/GraphMetrics.d.ts +143 -0
- package/dist/graph/GraphMetrics.js +264 -0
- package/dist/graph/MapExecutor.d.ts +39 -0
- package/dist/graph/MapExecutor.js +123 -0
- package/dist/graph/ParallelExecutor.d.ts +51 -0
- package/dist/graph/ParallelExecutor.js +103 -0
- package/dist/graph/Pipeline.d.ts +44 -0
- package/dist/graph/Pipeline.js +109 -0
- package/dist/graph/RouterExecutor.d.ts +89 -0
- package/dist/graph/RouterExecutor.js +209 -0
- package/dist/graph/SequentialExecutor.d.ts +44 -0
- package/dist/graph/SequentialExecutor.js +115 -0
- package/dist/graph/VotingSystem.d.ts +54 -0
- package/dist/graph/VotingSystem.js +106 -0
- package/dist/history/History.d.ts +107 -0
- package/dist/history/History.js +166 -0
- package/dist/history/RedisHistory.d.ts +27 -0
- package/dist/history/RedisHistory.js +55 -0
- package/dist/history/transformers.d.ts +102 -0
- package/dist/history/transformers.js +415 -0
- package/dist/history/types.d.ts +130 -0
- package/dist/history/types.js +55 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +48 -0
- package/dist/ingestion/IngestionPipeline.d.ts +86 -0
- package/dist/ingestion/IngestionPipeline.js +266 -0
- package/dist/ingestion/index.d.ts +3 -0
- package/dist/ingestion/index.js +7 -0
- package/dist/ingestion/types.d.ts +74 -0
- package/dist/ingestion/types.js +3 -0
- package/dist/team/Team.d.ts +46 -0
- package/dist/team/Team.js +104 -0
- package/dist/tools/Tool.d.ts +75 -0
- package/dist/tools/Tool.js +137 -0
- package/dist/vectorstore/Embeddings.d.ts +67 -0
- package/dist/vectorstore/Embeddings.js +54 -0
- package/dist/vectorstore/LanceDBVectorStore.d.ts +149 -0
- package/dist/vectorstore/LanceDBVectorStore.js +338 -0
- package/dist/vectorstore/OpenAIEmbeddings.d.ts +45 -0
- package/dist/vectorstore/OpenAIEmbeddings.js +109 -0
- package/dist/vectorstore/VectorStore.d.ts +255 -0
- package/dist/vectorstore/VectorStore.js +216 -0
- package/dist/vectorstore/index.d.ts +28 -0
- package/dist/vectorstore/index.js +35 -0
- package/dist/viz/VizConfig.d.ts +54 -0
- package/dist/viz/VizConfig.js +100 -0
- package/dist/viz/VizReporter.d.ts +127 -0
- package/dist/viz/VizReporter.js +595 -0
- package/dist/viz/index.d.ts +31 -0
- package/dist/viz/index.js +51 -0
- package/dist/viz/types.d.ts +105 -0
- package/dist/viz/types.js +7 -0
- package/package.json +109 -0
- package/readme.md +1 -0
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Pipeline = void 0;
|
|
4
|
+
const BaseExecutor_1 = require("./BaseExecutor");
|
|
5
|
+
/**
|
|
6
|
+
* Builds a pipeline of graph nodes that execute in sequence,
|
|
7
|
+
* where the output of each stage becomes the input of the next.
|
|
8
|
+
*
|
|
9
|
+
* Unlike SequentialExecutor which is specific to agents,
|
|
10
|
+
* Pipeline can chain any GraphNode implementations together.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* const pipeline = new Pipeline(
|
|
15
|
+
* AgentGraph.sequential(researchAgent, factChecker),
|
|
16
|
+
* AgentGraph.parallel({}, expertA, expertB),
|
|
17
|
+
* customTransformer,
|
|
18
|
+
* AgentGraph.votingSystem(judgeAgent)
|
|
19
|
+
* );
|
|
20
|
+
* const result = await pipeline.execute("Research topic X");
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
class Pipeline extends BaseExecutor_1.BaseExecutor {
|
|
24
|
+
constructor(...stages) {
|
|
25
|
+
super();
|
|
26
|
+
this.stages = [];
|
|
27
|
+
this.name = "Pipeline";
|
|
28
|
+
this.nodeType = "pipeline";
|
|
29
|
+
this.stages = stages;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Adds a stage to the end of the pipeline.
|
|
33
|
+
* @param stage - The GraphNode to add
|
|
34
|
+
* @returns The pipeline instance for chaining
|
|
35
|
+
*/
|
|
36
|
+
addStage(stage) {
|
|
37
|
+
this.stages.push(stage);
|
|
38
|
+
return this;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Executes all stages in sequence.
|
|
42
|
+
* @param input - The initial input
|
|
43
|
+
* @returns The output from the final stage
|
|
44
|
+
*/
|
|
45
|
+
async execute(input) {
|
|
46
|
+
const collector = this.getCollector();
|
|
47
|
+
const execId = collector?.startExecution(this.name, "pipeline", input);
|
|
48
|
+
if (this.stages.length === 0) {
|
|
49
|
+
if (execId)
|
|
50
|
+
collector?.endExecution(execId, true, input);
|
|
51
|
+
return input;
|
|
52
|
+
}
|
|
53
|
+
let result = input;
|
|
54
|
+
try {
|
|
55
|
+
for (let i = 0; i < this.stages.length; i++) {
|
|
56
|
+
const stage = this.stages[i];
|
|
57
|
+
const stageName = stage.name ?? `Stage ${i + 1}`;
|
|
58
|
+
// Get stage type from the node's nodeType property, or default to "custom"
|
|
59
|
+
const stageType = stage.nodeType ?? "custom";
|
|
60
|
+
// Only track non-executor stages directly
|
|
61
|
+
// Executors will track themselves when they have the collector
|
|
62
|
+
const isExecutor = stage instanceof BaseExecutor_1.BaseExecutor;
|
|
63
|
+
const stageExecId = !isExecutor
|
|
64
|
+
? collector?.startExecution(stageName, stageType, result)
|
|
65
|
+
: undefined;
|
|
66
|
+
// Pass metrics collector to child executors
|
|
67
|
+
if (collector && isExecutor) {
|
|
68
|
+
stage.withMetrics(collector);
|
|
69
|
+
}
|
|
70
|
+
try {
|
|
71
|
+
result = await stage.execute(result);
|
|
72
|
+
if (stageExecId) {
|
|
73
|
+
collector?.endExecution(stageExecId, true, result);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
catch (error) {
|
|
77
|
+
if (stageExecId) {
|
|
78
|
+
collector?.endExecution(stageExecId, false, undefined, undefined, error instanceof Error ? error.message : String(error));
|
|
79
|
+
}
|
|
80
|
+
throw error;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
if (execId)
|
|
84
|
+
collector?.endExecution(execId, true, result);
|
|
85
|
+
return result;
|
|
86
|
+
}
|
|
87
|
+
catch (error) {
|
|
88
|
+
if (execId) {
|
|
89
|
+
collector?.endExecution(execId, false, undefined, undefined, error instanceof Error ? error.message : String(error));
|
|
90
|
+
}
|
|
91
|
+
throw error;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Returns the number of stages in the pipeline.
|
|
96
|
+
*/
|
|
97
|
+
get length() {
|
|
98
|
+
return this.stages.length;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Enable metrics collection and return the pipeline for chaining.
|
|
102
|
+
*/
|
|
103
|
+
withMetrics(collector) {
|
|
104
|
+
super.withMetrics(collector);
|
|
105
|
+
return this;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
exports.Pipeline = Pipeline;
|
|
109
|
+
//# sourceMappingURL=Pipeline.js.map
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { BaseAgent } from "../agents/BaseAgent";
|
|
2
|
+
import { BaseExecutor, GraphNode } from "./BaseExecutor";
|
|
3
|
+
/**
|
|
4
|
+
* Represents a route option that the router can select.
|
|
5
|
+
*/
|
|
6
|
+
export interface Route<TInput = string, TOutput = string> {
|
|
7
|
+
/** Unique identifier for this route */
|
|
8
|
+
name: string;
|
|
9
|
+
/** Description of when this route should be selected */
|
|
10
|
+
description: string;
|
|
11
|
+
/** The handler to execute when this route is selected */
|
|
12
|
+
handler: GraphNode<TInput, TOutput> | BaseAgent;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Options for configuring the router executor.
|
|
16
|
+
*/
|
|
17
|
+
export interface RouterExecutorOptions {
|
|
18
|
+
/**
|
|
19
|
+
* Custom prompt template for the router agent.
|
|
20
|
+
* Use {input} for the user input and {routes} for the formatted route options.
|
|
21
|
+
*/
|
|
22
|
+
promptTemplate?: string;
|
|
23
|
+
/**
|
|
24
|
+
* If true, includes the original input when executing the selected route.
|
|
25
|
+
* If false, passes only the input to the route handler.
|
|
26
|
+
* @default true
|
|
27
|
+
*/
|
|
28
|
+
includeRouterContext?: boolean;
|
|
29
|
+
/**
|
|
30
|
+
* Fallback route name to use if the router fails to select a valid route.
|
|
31
|
+
* If not specified and router fails, an error is thrown.
|
|
32
|
+
*/
|
|
33
|
+
fallbackRoute?: string;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Routes input to one of several available handlers based on an agent's decision.
|
|
37
|
+
* The router agent analyzes the input and selects the most appropriate route.
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```typescript
|
|
41
|
+
* const router = new RouterExecutor(routerAgent, [
|
|
42
|
+
* { name: "technical", description: "Technical questions about code", handler: techAgent },
|
|
43
|
+
* { name: "general", description: "General knowledge questions", handler: generalAgent },
|
|
44
|
+
* { name: "creative", description: "Creative writing tasks", handler: creativeAgent },
|
|
45
|
+
* ]);
|
|
46
|
+
*
|
|
47
|
+
* const result = await router.execute("How do I fix this TypeScript error?");
|
|
48
|
+
* // Router selects "technical" route and executes techAgent
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
export declare class RouterExecutor extends BaseExecutor<string, string> {
|
|
52
|
+
private router;
|
|
53
|
+
private routes;
|
|
54
|
+
private options;
|
|
55
|
+
private routeNames;
|
|
56
|
+
constructor(router: BaseAgent, routes: Route[], options?: RouterExecutorOptions);
|
|
57
|
+
/**
|
|
58
|
+
* Routes the input to the appropriate handler based on the router agent's decision.
|
|
59
|
+
* @param input - The input string to route
|
|
60
|
+
* @returns The output from the selected route's handler
|
|
61
|
+
*/
|
|
62
|
+
execute(input: string): Promise<string>;
|
|
63
|
+
/**
|
|
64
|
+
* Execute the selected route's handler.
|
|
65
|
+
*/
|
|
66
|
+
private executeRoute;
|
|
67
|
+
/**
|
|
68
|
+
* Parse the router's response to extract the selected route name.
|
|
69
|
+
* Handles various response formats and normalizes the selection.
|
|
70
|
+
*/
|
|
71
|
+
private parseRouteSelection;
|
|
72
|
+
/**
|
|
73
|
+
* Check if a value is a GraphNode.
|
|
74
|
+
*/
|
|
75
|
+
private isGraphNode;
|
|
76
|
+
/**
|
|
77
|
+
* Attempt to extract token usage from an agent.
|
|
78
|
+
*/
|
|
79
|
+
private extractTokenUsage;
|
|
80
|
+
/**
|
|
81
|
+
* Returns the available route names.
|
|
82
|
+
*/
|
|
83
|
+
getRouteNames(): string[];
|
|
84
|
+
/**
|
|
85
|
+
* Returns the number of available routes.
|
|
86
|
+
*/
|
|
87
|
+
get length(): number;
|
|
88
|
+
}
|
|
89
|
+
//# sourceMappingURL=RouterExecutor.d.ts.map
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RouterExecutor = void 0;
|
|
4
|
+
const BaseExecutor_1 = require("./BaseExecutor");
|
|
5
|
+
const DEFAULT_PROMPT_TEMPLATE = `You are a routing agent that must select the most appropriate handler for the given input.
|
|
6
|
+
|
|
7
|
+
User Input: {input}
|
|
8
|
+
|
|
9
|
+
Available Routes:
|
|
10
|
+
{routes}
|
|
11
|
+
|
|
12
|
+
Instructions:
|
|
13
|
+
- Analyze the user input carefully
|
|
14
|
+
- Select the single most appropriate route based on the descriptions
|
|
15
|
+
- Respond with ONLY the route name, nothing else
|
|
16
|
+
- Do not explain your choice, just output the route name exactly as shown`;
|
|
17
|
+
/**
|
|
18
|
+
* Routes input to one of several available handlers based on an agent's decision.
|
|
19
|
+
* The router agent analyzes the input and selects the most appropriate route.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```typescript
|
|
23
|
+
* const router = new RouterExecutor(routerAgent, [
|
|
24
|
+
* { name: "technical", description: "Technical questions about code", handler: techAgent },
|
|
25
|
+
* { name: "general", description: "General knowledge questions", handler: generalAgent },
|
|
26
|
+
* { name: "creative", description: "Creative writing tasks", handler: creativeAgent },
|
|
27
|
+
* ]);
|
|
28
|
+
*
|
|
29
|
+
* const result = await router.execute("How do I fix this TypeScript error?");
|
|
30
|
+
* // Router selects "technical" route and executes techAgent
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
class RouterExecutor extends BaseExecutor_1.BaseExecutor {
|
|
34
|
+
constructor(router, routes, options = {}) {
|
|
35
|
+
super();
|
|
36
|
+
this.name = "RouterExecutor";
|
|
37
|
+
this.nodeType = "router";
|
|
38
|
+
this.router = router;
|
|
39
|
+
this.routes = new Map(routes.map((route) => [route.name.toLowerCase(), route]));
|
|
40
|
+
this.routeNames = routes.map((r) => r.name);
|
|
41
|
+
this.options = {
|
|
42
|
+
promptTemplate: options.promptTemplate ?? DEFAULT_PROMPT_TEMPLATE,
|
|
43
|
+
includeRouterContext: options.includeRouterContext ?? true,
|
|
44
|
+
fallbackRoute: options.fallbackRoute ?? "",
|
|
45
|
+
};
|
|
46
|
+
if (routes.length === 0) {
|
|
47
|
+
throw new Error("RouterExecutor requires at least one route");
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Routes the input to the appropriate handler based on the router agent's decision.
|
|
52
|
+
* @param input - The input string to route
|
|
53
|
+
* @returns The output from the selected route's handler
|
|
54
|
+
*/
|
|
55
|
+
async execute(input) {
|
|
56
|
+
const collector = this.getCollector();
|
|
57
|
+
const execId = collector?.startExecution(this.name, "router", input);
|
|
58
|
+
try {
|
|
59
|
+
// Build the routing prompt
|
|
60
|
+
const routesDescription = this.routeNames
|
|
61
|
+
.map((name) => {
|
|
62
|
+
const route = this.routes.get(name.toLowerCase());
|
|
63
|
+
return `- ${name}: ${route.description}`;
|
|
64
|
+
})
|
|
65
|
+
.join("\n");
|
|
66
|
+
const routingPrompt = this.options.promptTemplate
|
|
67
|
+
.replace("{input}", input)
|
|
68
|
+
.replace("{routes}", routesDescription);
|
|
69
|
+
// Execute the router agent to select a route
|
|
70
|
+
const routerName = this.router.getName?.() ?? "Router";
|
|
71
|
+
const routerExecId = collector?.startExecution(routerName, "agent", routingPrompt);
|
|
72
|
+
let selectedRouteName;
|
|
73
|
+
try {
|
|
74
|
+
const routerResponse = (await this.router.execute(routingPrompt));
|
|
75
|
+
selectedRouteName = this.parseRouteSelection(routerResponse);
|
|
76
|
+
const tokenUsage = this.extractTokenUsage(this.router);
|
|
77
|
+
if (routerExecId) {
|
|
78
|
+
collector?.endExecution(routerExecId, true, selectedRouteName, tokenUsage);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
catch (error) {
|
|
82
|
+
if (routerExecId) {
|
|
83
|
+
collector?.endExecution(routerExecId, false, undefined, undefined, error instanceof Error ? error.message : String(error));
|
|
84
|
+
}
|
|
85
|
+
throw error;
|
|
86
|
+
}
|
|
87
|
+
// Get the selected route
|
|
88
|
+
const route = this.routes.get(selectedRouteName.toLowerCase());
|
|
89
|
+
if (!route) {
|
|
90
|
+
// Try fallback if configured
|
|
91
|
+
if (this.options.fallbackRoute) {
|
|
92
|
+
const fallbackRoute = this.routes.get(this.options.fallbackRoute.toLowerCase());
|
|
93
|
+
if (fallbackRoute) {
|
|
94
|
+
return this.executeRoute(fallbackRoute, input, collector, execId);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
throw new Error(`Router selected invalid route: "${selectedRouteName}". Available routes: ${this.routeNames.join(", ")}`);
|
|
98
|
+
}
|
|
99
|
+
return this.executeRoute(route, input, collector, execId);
|
|
100
|
+
}
|
|
101
|
+
catch (error) {
|
|
102
|
+
if (execId) {
|
|
103
|
+
collector?.endExecution(execId, false, undefined, undefined, error instanceof Error ? error.message : String(error));
|
|
104
|
+
}
|
|
105
|
+
throw error;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Execute the selected route's handler.
|
|
110
|
+
*/
|
|
111
|
+
async executeRoute(route, input, collector, parentExecId) {
|
|
112
|
+
const handlerName = route.name;
|
|
113
|
+
const handlerExecId = collector?.startExecution(handlerName, "agent", input);
|
|
114
|
+
try {
|
|
115
|
+
// Prepare input for the handler
|
|
116
|
+
const handlerInput = this.options.includeRouterContext
|
|
117
|
+
? JSON.stringify({
|
|
118
|
+
originalInput: input,
|
|
119
|
+
selectedRoute: route.name,
|
|
120
|
+
})
|
|
121
|
+
: input;
|
|
122
|
+
let result;
|
|
123
|
+
if (this.isGraphNode(route.handler)) {
|
|
124
|
+
// Pass metrics collector to child executors
|
|
125
|
+
if (collector && route.handler instanceof BaseExecutor_1.BaseExecutor) {
|
|
126
|
+
route.handler.withMetrics(collector);
|
|
127
|
+
}
|
|
128
|
+
result = (await route.handler.execute(handlerInput));
|
|
129
|
+
}
|
|
130
|
+
else {
|
|
131
|
+
result = (await route.handler.execute(handlerInput));
|
|
132
|
+
}
|
|
133
|
+
// Try to extract token usage if handler is an agent
|
|
134
|
+
let tokenUsage;
|
|
135
|
+
if (!this.isGraphNode(route.handler)) {
|
|
136
|
+
tokenUsage = this.extractTokenUsage(route.handler);
|
|
137
|
+
}
|
|
138
|
+
if (handlerExecId) {
|
|
139
|
+
collector?.endExecution(handlerExecId, true, result, tokenUsage);
|
|
140
|
+
}
|
|
141
|
+
if (parentExecId) {
|
|
142
|
+
collector?.endExecution(parentExecId, true, result);
|
|
143
|
+
}
|
|
144
|
+
return result;
|
|
145
|
+
}
|
|
146
|
+
catch (error) {
|
|
147
|
+
if (handlerExecId) {
|
|
148
|
+
collector?.endExecution(handlerExecId, false, undefined, undefined, error instanceof Error ? error.message : String(error));
|
|
149
|
+
}
|
|
150
|
+
throw error;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Parse the router's response to extract the selected route name.
|
|
155
|
+
* Handles various response formats and normalizes the selection.
|
|
156
|
+
*/
|
|
157
|
+
parseRouteSelection(response) {
|
|
158
|
+
// Clean up the response - trim whitespace and handle common formats
|
|
159
|
+
const cleaned = response.trim().toLowerCase();
|
|
160
|
+
// Direct match
|
|
161
|
+
if (this.routes.has(cleaned)) {
|
|
162
|
+
return cleaned;
|
|
163
|
+
}
|
|
164
|
+
// Try to find a route name mentioned in the response
|
|
165
|
+
for (const name of this.routeNames) {
|
|
166
|
+
if (cleaned.includes(name.toLowerCase())) {
|
|
167
|
+
return name.toLowerCase();
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
// If no match found, return the cleaned response and let caller handle the error
|
|
171
|
+
return cleaned;
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Check if a value is a GraphNode.
|
|
175
|
+
*/
|
|
176
|
+
isGraphNode(value) {
|
|
177
|
+
return (typeof value === "object" &&
|
|
178
|
+
value !== null &&
|
|
179
|
+
"execute" in value &&
|
|
180
|
+
typeof value.execute === "function");
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Attempt to extract token usage from an agent.
|
|
184
|
+
*/
|
|
185
|
+
extractTokenUsage(agent) {
|
|
186
|
+
const agentWithUsage = agent;
|
|
187
|
+
if (!agentWithUsage.lastTokenUsage)
|
|
188
|
+
return undefined;
|
|
189
|
+
return {
|
|
190
|
+
inputTokens: agentWithUsage.lastTokenUsage.input_tokens,
|
|
191
|
+
outputTokens: agentWithUsage.lastTokenUsage.output_tokens,
|
|
192
|
+
totalTokens: agentWithUsage.lastTokenUsage.total_tokens,
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Returns the available route names.
|
|
197
|
+
*/
|
|
198
|
+
getRouteNames() {
|
|
199
|
+
return [...this.routeNames];
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Returns the number of available routes.
|
|
203
|
+
*/
|
|
204
|
+
get length() {
|
|
205
|
+
return this.routes.size;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
exports.RouterExecutor = RouterExecutor;
|
|
209
|
+
//# sourceMappingURL=RouterExecutor.js.map
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { BaseAgent } from "../agents/BaseAgent";
|
|
2
|
+
import { BaseExecutor } from "./BaseExecutor";
|
|
3
|
+
/**
|
|
4
|
+
* Options for configuring sequential execution behavior.
|
|
5
|
+
*/
|
|
6
|
+
export interface SequentialExecutorOptions {
|
|
7
|
+
/**
|
|
8
|
+
* If true, wraps the input in a JSON object with originalQuestion and resultFromPreviousAgent.
|
|
9
|
+
* If false, passes the raw result from one agent to the next.
|
|
10
|
+
* @default true
|
|
11
|
+
*/
|
|
12
|
+
wrapInput?: boolean;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Executes agents in sequence, passing the output of each agent to the next.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```typescript
|
|
19
|
+
* const executor = new SequentialExecutor(researchAgent, summaryAgent);
|
|
20
|
+
* const result = await executor.execute("What is quantum computing?");
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export declare class SequentialExecutor extends BaseExecutor<string, string> {
|
|
24
|
+
private agents;
|
|
25
|
+
private options;
|
|
26
|
+
constructor(...args: [...BaseAgent[]] | [SequentialExecutorOptions, ...BaseAgent[]]);
|
|
27
|
+
private isAgent;
|
|
28
|
+
/**
|
|
29
|
+
* Executes agents in sequence.
|
|
30
|
+
* @param input - The initial input string
|
|
31
|
+
* @returns The final output from the last agent in the sequence
|
|
32
|
+
*/
|
|
33
|
+
execute(input: string): Promise<string>;
|
|
34
|
+
/**
|
|
35
|
+
* Attempt to extract token usage from an agent.
|
|
36
|
+
* Converts from agent's snake_case format to metrics camelCase format.
|
|
37
|
+
*/
|
|
38
|
+
private extractTokenUsage;
|
|
39
|
+
/**
|
|
40
|
+
* Returns the number of agents in the sequence.
|
|
41
|
+
*/
|
|
42
|
+
get length(): number;
|
|
43
|
+
}
|
|
44
|
+
//# sourceMappingURL=SequentialExecutor.d.ts.map
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SequentialExecutor = void 0;
|
|
4
|
+
const BaseExecutor_1 = require("./BaseExecutor");
|
|
5
|
+
/**
|
|
6
|
+
* Executes agents in sequence, passing the output of each agent to the next.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* const executor = new SequentialExecutor(researchAgent, summaryAgent);
|
|
11
|
+
* const result = await executor.execute("What is quantum computing?");
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
class SequentialExecutor extends BaseExecutor_1.BaseExecutor {
|
|
15
|
+
constructor(...args) {
|
|
16
|
+
super();
|
|
17
|
+
this.name = "SequentialExecutor";
|
|
18
|
+
this.nodeType = "sequential";
|
|
19
|
+
// Check if first argument is options object
|
|
20
|
+
if (args.length > 0 && !this.isAgent(args[0])) {
|
|
21
|
+
const [options, ...agents] = args;
|
|
22
|
+
this.options = {
|
|
23
|
+
wrapInput: options.wrapInput ?? true,
|
|
24
|
+
};
|
|
25
|
+
this.agents = agents;
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
this.options = { wrapInput: true };
|
|
29
|
+
this.agents = args;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
isAgent(value) {
|
|
33
|
+
// Check for duck-typing: an agent has an execute method and is not a plain options object
|
|
34
|
+
return (typeof value === "object" &&
|
|
35
|
+
value !== null &&
|
|
36
|
+
"execute" in value &&
|
|
37
|
+
typeof value.execute === "function");
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Executes agents in sequence.
|
|
41
|
+
* @param input - The initial input string
|
|
42
|
+
* @returns The final output from the last agent in the sequence
|
|
43
|
+
*/
|
|
44
|
+
async execute(input) {
|
|
45
|
+
const collector = this.getCollector();
|
|
46
|
+
const execId = collector?.startExecution(this.name, "sequential", input);
|
|
47
|
+
if (this.agents.length === 0) {
|
|
48
|
+
if (execId)
|
|
49
|
+
collector?.endExecution(execId, true, input);
|
|
50
|
+
return input;
|
|
51
|
+
}
|
|
52
|
+
let result = input;
|
|
53
|
+
const originalInput = input;
|
|
54
|
+
try {
|
|
55
|
+
for (let i = 0; i < this.agents.length; i++) {
|
|
56
|
+
const agent = this.agents[i];
|
|
57
|
+
const agentName = agent.getName?.() ?? `Agent ${i + 1}`;
|
|
58
|
+
const agentExecId = collector?.startExecution(agentName, "agent", result);
|
|
59
|
+
const agentInput = this.options.wrapInput
|
|
60
|
+
? JSON.stringify({
|
|
61
|
+
originalQuestion: originalInput,
|
|
62
|
+
resultFromPreviousAgent: result,
|
|
63
|
+
})
|
|
64
|
+
: result;
|
|
65
|
+
try {
|
|
66
|
+
result = (await agent.execute(agentInput));
|
|
67
|
+
// Try to get token usage from agent if available
|
|
68
|
+
const tokenUsage = this.extractTokenUsage(agent);
|
|
69
|
+
if (agentExecId) {
|
|
70
|
+
collector?.endExecution(agentExecId, true, result, tokenUsage);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
catch (error) {
|
|
74
|
+
if (agentExecId) {
|
|
75
|
+
collector?.endExecution(agentExecId, false, undefined, undefined, error instanceof Error ? error.message : String(error));
|
|
76
|
+
}
|
|
77
|
+
throw error;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
if (execId)
|
|
81
|
+
collector?.endExecution(execId, true, result);
|
|
82
|
+
return result;
|
|
83
|
+
}
|
|
84
|
+
catch (error) {
|
|
85
|
+
if (execId) {
|
|
86
|
+
collector?.endExecution(execId, false, undefined, undefined, error instanceof Error ? error.message : String(error));
|
|
87
|
+
}
|
|
88
|
+
throw error;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Attempt to extract token usage from an agent.
|
|
93
|
+
* Converts from agent's snake_case format to metrics camelCase format.
|
|
94
|
+
*/
|
|
95
|
+
extractTokenUsage(agent) {
|
|
96
|
+
// Check if agent has lastTokenUsage property
|
|
97
|
+
const agentWithUsage = agent;
|
|
98
|
+
if (!agentWithUsage.lastTokenUsage)
|
|
99
|
+
return undefined;
|
|
100
|
+
// Convert from snake_case to camelCase
|
|
101
|
+
return {
|
|
102
|
+
inputTokens: agentWithUsage.lastTokenUsage.input_tokens,
|
|
103
|
+
outputTokens: agentWithUsage.lastTokenUsage.output_tokens,
|
|
104
|
+
totalTokens: agentWithUsage.lastTokenUsage.total_tokens,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Returns the number of agents in the sequence.
|
|
109
|
+
*/
|
|
110
|
+
get length() {
|
|
111
|
+
return this.agents.length;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
exports.SequentialExecutor = SequentialExecutor;
|
|
115
|
+
//# sourceMappingURL=SequentialExecutor.js.map
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { BaseAgent } from "../agents/BaseAgent";
|
|
2
|
+
import { BaseExecutor } from "./BaseExecutor";
|
|
3
|
+
/**
|
|
4
|
+
* Input format for the VotingSystem.
|
|
5
|
+
*/
|
|
6
|
+
export interface VotingInput {
|
|
7
|
+
/** The original question or input that was posed */
|
|
8
|
+
originalInput: string;
|
|
9
|
+
/** Array of solutions/answers from different sources to vote on */
|
|
10
|
+
solutions: string[];
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Options for configuring the voting system.
|
|
14
|
+
*/
|
|
15
|
+
export interface VotingSystemOptions {
|
|
16
|
+
/**
|
|
17
|
+
* Custom prompt template for the judge.
|
|
18
|
+
* Use {originalQuestion} and {expertAnswers} as placeholders.
|
|
19
|
+
*/
|
|
20
|
+
promptTemplate?: string;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* A voting system that uses a judge agent to select or synthesize
|
|
24
|
+
* the best answer from multiple solutions.
|
|
25
|
+
*
|
|
26
|
+
* Typically used after a ParallelExecutor to evaluate multiple expert opinions.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```typescript
|
|
30
|
+
* const voting = new VotingSystem(judgeAgent);
|
|
31
|
+
* const result = await voting.execute({
|
|
32
|
+
* originalInput: "What is the best approach?",
|
|
33
|
+
* solutions: [expertA_answer, expertB_answer, expertC_answer]
|
|
34
|
+
* });
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export declare class VotingSystem extends BaseExecutor<VotingInput, string> {
|
|
38
|
+
private judge;
|
|
39
|
+
private promptTemplate;
|
|
40
|
+
constructor(judge: BaseAgent, options?: VotingSystemOptions);
|
|
41
|
+
/**
|
|
42
|
+
* Evaluates the solutions and returns the judge's verdict.
|
|
43
|
+
* @param input - Object containing originalInput and solutions array
|
|
44
|
+
* @returns The judge's selected or synthesized answer
|
|
45
|
+
* @throws Error if input is a string (must be VotingInput object)
|
|
46
|
+
*/
|
|
47
|
+
execute(input: VotingInput | string): Promise<string>;
|
|
48
|
+
/**
|
|
49
|
+
* Attempt to extract token usage from an agent.
|
|
50
|
+
* Converts from agent's snake_case format to metrics camelCase format.
|
|
51
|
+
*/
|
|
52
|
+
private extractTokenUsage;
|
|
53
|
+
}
|
|
54
|
+
//# sourceMappingURL=VotingSystem.d.ts.map
|