UNPKG

@iqai/adk-cli

Version:

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

148 lines 5.24 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.resolveAgentExport = resolveAgentExport; function isLikelyAgentInstance(obj) { return (obj != null && typeof obj === "object" && typeof obj.name === "string" && typeof obj.runAsync === "function"); } function isAgentBuilder(obj) { return (obj != null && typeof obj === "object" && typeof obj.build === "function" && typeof obj.withModel === "function"); } function isBuiltAgent(obj) { return (obj != null && typeof obj === "object" && "agent" in obj && "runner" in obj && "session" in obj); } function isPrimitive(v) { return v == null || ["string", "number", "boolean"].includes(typeof v); } async function invokeFunctionSafely(fn) { let result = fn(); if (result && typeof result === "object" && "then" in result) { try { result = await result; } catch (e) { throw new Error(`Failed to await function result: ${e instanceof Error ? e.message : String(e)}`); } } return result; } async function extractBaseAgent(item) { if (isLikelyAgentInstance(item)) return { agent: item }; if (isAgentBuilder(item)) { const built = await item.build(); return { agent: built.agent, builtAgent: built }; } if (isBuiltAgent(item)) { const builtItem = item; return { agent: builtItem.agent, builtAgent: builtItem }; } return null; } async function scanModuleExports(mod) { for (const [key, value] of Object.entries(mod)) { if (key === "default") continue; const keyLower = key.toLowerCase(); if (isPrimitive(value)) continue; const result = await extractBaseAgent(value); if (result) return result; if (value && typeof value === "object" && "agent" in value) { const container = value; const containerResult = await extractBaseAgent(container.agent); if (containerResult) return containerResult; } if (typeof value === "function" && (() => { if (/(agent|build|create)/i.test(keyLower)) return true; const fnLike = value; const fnName = fnLike?.name; return !!(fnName && /(agent|build|create)/i.test(fnName.toLowerCase())); })()) { try { const functionResult = await invokeFunctionSafely(value); const r = await extractBaseAgent(functionResult); if (r) return r; if (functionResult && typeof functionResult === "object" && "agent" in functionResult) { const containerResult = await extractBaseAgent(functionResult.agent); if (containerResult) return containerResult; } } catch { // swallow } } } return null; } async function tryResolvingDirectCandidate(candidateToResolve, mod) { if (isPrimitive(candidateToResolve) || (candidateToResolve && candidateToResolve === mod)) { return null; } const result = await extractBaseAgent(candidateToResolve); if (result) return result; if (candidateToResolve && typeof candidateToResolve === "object" && "agent" in candidateToResolve) { const container = candidateToResolve; return await extractBaseAgent(container.agent); } return null; } async function tryResolvingFunctionCandidate(functionCandidate) { try { const functionResult = await invokeFunctionSafely(functionCandidate); const r = await extractBaseAgent(functionResult); if (r) return r; if (functionResult && typeof functionResult === "object" && "agent" in functionResult) { const containerResult = await extractBaseAgent(functionResult.agent); if (containerResult) return containerResult; } } catch (e) { throw new Error(`Failed executing exported agent function: ${e instanceof Error ? e.message : String(e)}`); } return null; } async function resolveAgentExport(mod) { const moduleDefault = mod.default && typeof mod.default === "object" ? mod.default : undefined; const candidateToResolve = mod.agent ?? moduleDefault?.agent ?? moduleDefault ?? mod; const directResult = await tryResolvingDirectCandidate(candidateToResolve, mod); if (directResult) return directResult; const exportResult = await scanModuleExports(mod); if (exportResult) return exportResult; if (typeof candidateToResolve === "function") { const functionResult = await tryResolvingFunctionCandidate(candidateToResolve); if (functionResult) return functionResult; } throw new Error("No agent export resolved (expected BaseAgent, AgentBuilder, or BuiltAgent)"); } //# sourceMappingURL=resolver.js.map