agentis
Version:
A TypeScript framework for building sophisticated multi-agent systems
190 lines (189 loc) • 5.64 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GraphBuilder = void 0;
const EnhancedToolOrchestrator_1 = require("./EnhancedToolOrchestrator");
/**
* Fluent API for building tool execution graphs
*/
class GraphBuilder {
constructor() {
this.nodes = [];
this.mode = EnhancedToolOrchestrator_1.ExecutionMode.SEQUENTIAL;
}
/**
* Create a new node in the graph
*/
addNode(nodeConfig) {
this.nodes.push({
id: nodeConfig.id,
toolName: nodeConfig.toolName,
input: nodeConfig.input,
priority: nodeConfig.priority || 0,
dependsOn: nodeConfig.dependsOn,
mode: nodeConfig.mode,
condition: nodeConfig.condition,
transformOutput: nodeConfig.transformOutput,
retryConfig: nodeConfig.retryConfig
});
return this;
}
/**
* Add a simple tool node with no dependencies
*/
addTool(id, toolName, input, priority = 0) {
return this.addNode({
id,
toolName,
input,
priority
});
}
/**
* Add a node that depends on the results of other nodes
*/
addDependentTool(id, toolName, inputFn, dependsOn, priority = 0) {
return this.addNode({
id,
toolName,
input: inputFn,
dependsOn,
priority
});
}
/**
* Add a conditional node that only executes if a condition is met
*/
addConditionalTool(id, toolName, input, condition, dependsOn, priority = 0) {
return this.addNode({
id,
toolName,
input,
dependsOn,
priority,
mode: EnhancedToolOrchestrator_1.ExecutionMode.CONDITIONAL,
condition
});
}
/**
* Add a node with output transformation
*/
addTransformingTool(id, toolName, input, transformOutput, dependsOn, priority = 0) {
return this.addNode({
id,
toolName,
input,
dependsOn,
priority,
transformOutput
});
}
/**
* Add a node with retry capabilities
*/
addRetryableTool(id, toolName, input, retryConfig, dependsOn, priority = 0) {
return this.addNode({
id,
toolName,
input,
dependsOn,
priority,
retryConfig
});
}
/**
* Set execution mode to sequential (default)
*/
sequential() {
this.mode = EnhancedToolOrchestrator_1.ExecutionMode.SEQUENTIAL;
return this;
}
/**
* Set execution mode to parallel
*/
parallel(maxConcurrency) {
this.mode = EnhancedToolOrchestrator_1.ExecutionMode.PARALLEL;
this.maxConcurrency = maxConcurrency;
return this;
}
/**
* Add a chain of dependent tools
*/
addChain(chainConfig) {
if (chainConfig.tools.length === 0) {
return this;
}
// Add first node
const firstTool = chainConfig.tools[0];
const firstNodeId = `${chainConfig.prefix}-0`;
this.addNode({
id: firstNodeId,
toolName: firstTool.toolName,
input: typeof firstTool.input === 'function'
? ((context) => firstTool.input(null))
: firstTool.input,
transformOutput: firstTool.transformOutput
});
// Add remaining nodes with dependencies
for (let i = 1; i < chainConfig.tools.length; i++) {
const tool = chainConfig.tools[i];
const nodeId = `${chainConfig.prefix}-${i}`;
const prevNodeId = `${chainConfig.prefix}-${i - 1}`;
this.addNode({
id: nodeId,
toolName: tool.toolName,
input: typeof tool.input === 'function'
? ((context) => {
const prevResult = context.getPreviousResult(prevNodeId);
return tool.input(prevResult?.result);
})
: tool.input,
dependsOn: [prevNodeId],
transformOutput: tool.transformOutput
});
}
return this;
}
/**
* Build and return the final execution graph
*/
build() {
if (this.nodes.length === 0) {
throw new Error('Cannot build an empty execution graph');
}
return {
nodes: [...this.nodes],
mode: this.mode,
maxConcurrency: this.maxConcurrency
};
}
/**
* Create a simple graph for a single tool
*/
static createSingleToolGraph(toolName, input, id = 'single-tool') {
return new GraphBuilder()
.addTool(id, toolName, input)
.build();
}
/**
* Create a simple sequential chain of tools
*/
static createSequentialChain(chainConfig) {
return new GraphBuilder()
.addChain({
prefix: 'chain',
tools: chainConfig.tools
})
.build();
}
/**
* Create a graph from an array of tools to run in parallel
*/
static createParallelGraph(tools, maxConcurrency) {
const builder = new GraphBuilder().parallel(maxConcurrency);
tools.forEach((tool, index) => {
builder.addTool(tool.id || `parallel-${index}`, tool.toolName, tool.input);
});
return builder.build();
}
}
exports.GraphBuilder = GraphBuilder;