UNPKG

@iqai/adk-cli

Version:

CLI tool for creating, running, and testing ADK-TS agents

107 lines 3.94 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AgentResolver = void 0; class AgentResolver { logger; quiet; guards; constructor(logger, quiet, guards) { this.logger = logger; this.quiet = quiet; this.guards = guards; } async invokeFunctionSafely(fn) { let result = fn(); if (result && typeof result === "object" && "then" in result) { result = await result; } return result; } async extractBaseAgent(item) { if (this.guards.isLikelyAgentInstance(item)) return item; if (this.guards.isAgentBuilder(item)) return (await item.build()).agent; if (this.guards.isBuiltAgent(item)) return item.agent; return null; } async scanModuleExports(mod) { for (const [key, value] of Object.entries(mod)) { if (key === "default" || this.guards.isPrimitive(value)) continue; const found = await this.extractBaseAgent(value); if (found) return found; if (value && typeof value === "object" && "agent" in value) { const inner = await this.extractBaseAgent(value.agent); if (inner) return inner; } if (typeof value === "function" && /(agent|build|create)/i.test(key)) { try { const result = await this.invokeFunctionSafely(value); const inner = await this.extractBaseAgent(result); if (inner) return inner; if (result && typeof result === "object" && "agent" in result) { const nested = await this.extractBaseAgent(result.agent); if (nested) return nested; } } catch { } } } return null; } async tryResolvingDirectCandidate(candidate, mod) { if (this.guards.isPrimitive(candidate) || candidate === mod) return null; const direct = await this.extractBaseAgent(candidate); if (direct) return direct; if (candidate && typeof candidate === "object" && "agent" in candidate) return await this.extractBaseAgent(candidate.agent); return null; } async tryResolvingFunctionCandidate(fnCandidate) { try { const result = await this.invokeFunctionSafely(fnCandidate); const agent = await this.extractBaseAgent(result); if (agent) return agent; if (result && typeof result === "object" && "agent" in result) return await this.extractBaseAgent(result.agent); } catch (e) { throw new Error(`Failed executing exported agent function: ${e instanceof Error ? e.message : String(e)}`); } return null; } async resolveAgentExport(mod) { const moduleDefault = mod?.default; const candidate = mod?.agent ?? moduleDefault?.agent ?? moduleDefault ?? mod; const direct = await this.tryResolvingDirectCandidate(candidate, mod); if (direct) return direct; const scanned = await this.scanModuleExports(mod); if (scanned) return scanned; if (typeof candidate === "function") { const fnResult = await this.tryResolvingFunctionCandidate(candidate); if (fnResult) return fnResult; } throw new Error("No agent export resolved (expected BaseAgent, AgentBuilder, or BuiltAgent)"); } } exports.AgentResolver = AgentResolver; //# sourceMappingURL=agent-resolver.js.map