@hashgraphonline/conversational-agent
Version:
Hashgraph Online conversational AI agent implementing HCS-10 communication, HCS-2 registries, and content inscription on Hedera. https://hol.org
188 lines (187 loc) • 5.28 kB
JavaScript
import { ZodError } from "zod";
import { Logger } from "@hashgraphonline/standards-sdk";
class ExecutionPipeline {
constructor(toolRegistry, formEngine, memory, logger) {
this.toolRegistry = toolRegistry;
this.formEngine = formEngine;
this.memory = memory;
this.logger = logger || new Logger({ module: "ExecutionPipeline" });
}
/**
* Execute a tool through the pipeline
*/
async execute(toolName, input, sessionContext) {
const traceId = `trace-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
const startTime = Date.now();
const toolEntry = this.toolRegistry.getTool(toolName);
if (!toolEntry) {
throw new Error(`Tool not found in registry: ${toolName}`);
}
const context = {
toolName,
input,
session: sessionContext || this.buildDefaultSession(),
memory: this.memory,
traceId,
toolEntry
};
try {
const shouldGenerateForm = await this.checkFormGeneration(context);
if (shouldGenerateForm.requiresForm && shouldGenerateForm.formMessage) {
return {
success: false,
output: "Form generation required",
requiresForm: true,
formMessage: shouldGenerateForm.formMessage,
traceId,
executionTime: Date.now() - startTime
};
}
const result = await this.executeToolDirect(context);
return {
success: true,
output: result,
traceId,
executionTime: Date.now() - startTime
};
} catch (error) {
return this.handleExecutionError(
error,
context,
traceId,
Date.now() - startTime
);
}
}
/**
* Execute tool with validation
*/
async executeWithValidation(toolName, input, sessionContext) {
return this.execute(toolName, input, sessionContext);
}
/**
* Process form submission
*/
async processFormSubmission(toolName, formId, parameters, sessionContext) {
const traceId = `form-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
const startTime = Date.now();
try {
const formSubmission = {
formId,
toolName,
parameters,
timestamp: Date.now()
};
const processedInput = await this.formEngine.processSubmission(
formSubmission
);
return this.execute(toolName, processedInput, sessionContext);
} catch (error) {
return {
success: false,
output: "Form submission processing failed",
error: error instanceof Error ? error.message : String(error),
traceId,
executionTime: Date.now() - startTime
};
}
}
/**
* Check if form generation is required
*/
async checkFormGeneration(context) {
const inputRecord = context.input;
if (inputRecord?.__fromForm === true || inputRecord?.renderForm === false) {
return { requiresForm: false };
}
if (!this.formEngine.shouldGenerateForm(context.toolEntry.tool, context.input)) {
return { requiresForm: false };
}
const formMessage = await this.formEngine.generateForm(
context.toolName,
context.toolEntry.tool,
context.input
);
if (formMessage) {
return { requiresForm: true, formMessage };
}
return { requiresForm: false };
}
/**
* Execute tool directly
*/
async executeToolDirect(context) {
const { toolEntry, input } = context;
const parameters = input || {};
const mergedArgs = { ...parameters, renderForm: false };
if (toolEntry.wrapper) {
return this.executeWrappedTool(toolEntry, mergedArgs);
}
return await toolEntry.tool.call(mergedArgs);
}
/**
* Execute wrapped tool
*/
async executeWrappedTool(toolEntry, mergedArgs) {
const wrapper = toolEntry.wrapper;
if (!wrapper) {
throw new Error("Tool wrapper not found");
}
const wrapperAsAny = wrapper;
if (wrapperAsAny.executeOriginal) {
return await wrapperAsAny.executeOriginal(mergedArgs);
}
if (wrapperAsAny.originalTool?.call) {
return await wrapperAsAny.originalTool.call(mergedArgs);
}
return await toolEntry.originalTool.call(mergedArgs);
}
/**
* Handle execution error
*/
handleExecutionError(error, context, traceId, executionTime) {
const errorMessage = error instanceof Error ? error.message : String(error);
if (error instanceof ZodError) {
return {
success: false,
output: "Validation error occurred",
error: errorMessage,
traceId,
executionTime
};
}
this.logger.error(`Tool execution failed: ${context.toolName}`, {
traceId,
error: errorMessage
});
return {
success: false,
output: "Tool execution failed",
error: errorMessage,
traceId,
executionTime
};
}
/**
* Build default session context
*/
buildDefaultSession() {
return {
sessionId: `session-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`,
timestamp: Date.now()
};
}
/**
* Get statistics about the pipeline
*/
getStatistics() {
return {
totalMiddleware: 0,
registeredMiddleware: []
};
}
}
export {
ExecutionPipeline
};
//# sourceMappingURL=index47.js.map