26 mandateId: string;
27 taskId: string;
28 log: LogFunction;
29 // NEW: Add configuration specific to this agent instance in the workflow
30 config?: Record<string, any>;
32}
33
34/** Defines the function signature for any agent. */
35type AgentFunction<InputPayload = any, OutputPayload = any> = (
36 input: AgentInput<InputPayload>,
37 context: AgentContext,
79// --- Logging ---
80
81type LogFunction = (level: LogLevel, component: string, message: string, details?: any) => void;
82type LogLevel = "DEBUG" | "INFO" | "WARN" | "ERROR" | "SUCCESS";
83
119 }
120
121 createLogFunction(mandateId: string, baseComponent?: string): LogFunction {
122 return (level, component, message, details) => {
123 const entry: Omit<LogEntry, "timestamp" | "taskId"> = {
174
175class AgentRegistry {
176 private agents: Map<string, AgentFunction<any, any>> = new Map();
177
178 register<InputPayload, OutputPayload>(
179 name: string,
180 agentFn: AgentFunction<InputPayload, OutputPayload>,
181 ): void {
182 if (this.agents.has(name)) {
187 }
188
189 getAgent(name: string): AgentFunction<any, any> | undefined {
190 return this.agents.get(name);
191 }
241 ): Promise<WorkflowResult<FinalPayload>> {
242 const mandateId = `M-${Date.now()}-${definition.id}`;
243 const log = this.logger.createLogFunction(mandateId, "WorkflowEngine");
244 // Store outputs of successful steps: Map<stepId, AgentOutput>
245 const stepResults = new Map<string, AgentOutput<any>>();
489
490// Agent 1: Summarizer (modified for new signature)
491async function summarizerAgent(
492 input: AgentInput<{ textToSummarize: string }>,
493 context: AgentContext,
519
520// Agent 2: Fetch External Data (using config)
521async function fetchAgent(
522 input: AgentInput<{ url_from_input?: string }>, // Input might override config
523 context: AgentContext,
558
559// Agent 3: Combiner Agent (New)
560async function combinerAgent(
561 input: AgentInput<{ summary?: string; externalDataTitle?: string }>,
562 context: AgentContext,
649agentRegistry.register("combiner", combinerAgent); // Register the new agent
650
651function generateHtmlShellV2() {
652 // Simple HTML UI for the enhanced workflow
653 return `<!DOCTYPE html>
759
760// Val Town Entry Point (or adapt for Node.js/Deno/Bun)
761export default async function(req: Request): Promise<Response> {
762 // IMPORTANT: Clear logs for each new request to avoid mixing logs between runs
763 globalLogger.clear();
764 const requestStartTime = Date.now();
765 const requestLog = globalLogger.createLogFunction(`REQ-${requestStartTime}`, "RequestHandler");
766
767 requestLog("INFO", "RequestReceived", `Incoming ${req.method} request for ${req.url}`);