UNPKG

adk-typescript

Version:

TypeScript port of Google's Agent Development Kit (ADK)

59 lines (58 loc) 1.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LoopAgent = void 0; const BaseAgent_1 = require("./BaseAgent"); /** * A shell agent that runs its sub-agents in a loop. * * When sub-agent generates an event with escalate or max_iterations are * reached, the loop agent will stop. */ class LoopAgent extends BaseAgent_1.BaseAgent { /** * Creates a new LoopAgent. * * @param name The name of the agent * @param options Options for the agent */ constructor(name, options = {}) { super(name, options); this.maxIterations = options.maxIterations; } /** * @inheritdoc */ async *runAsyncImpl(ctx) { let timesLooped = 0; while (!this.maxIterations || timesLooped < this.maxIterations) { for (const subAgent of this.subAgents) { for await (const event of subAgent.invoke(ctx)) { yield event; if (event.actions.escalate) { return; } } } timesLooped += 1; } } /** * @inheritdoc */ async *runLiveImpl(ctx) { throw new Error('The behavior for runLive is not defined yet for LoopAgent.'); // Return early - this code is unreachable but satisfies TypeScript's return type return; yield {}; // Unreachable code to satisfy AsyncGenerator type } /** * @inheritdoc */ setUserContent(content, invocationContext) { // For LoopAgent, we pass the content to all sub-agents for (const subAgent of this.subAgents) { subAgent.setUserContent(content, invocationContext); } } } exports.LoopAgent = LoopAgent;