UNPKG

adk-typescript

Version:

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

48 lines (47 loc) 1.85 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createEmptyState = createEmptyState; const LlmAgent_1 = require("../../agents/LlmAgent"); /** * Recursively creates an empty state for all state variables * referenced in agent instructions * * @param agent The agent to process * @param allState Object to collect the state variables in */ function _createEmptyState(agent, allState) { // Process sub-agents first for (const subAgent of agent.subAgents) { _createEmptyState(subAgent, allState); } // If this is an LLM agent with an instruction that has variables, add them to the state if (agent instanceof LlmAgent_1.LlmAgent && agent.instruction && typeof agent.instruction === 'string') { // Find all variables in the format {variableName} const matches = agent.instruction.match(/{([\w]+)}/g) || []; for (const match of matches) { // Extract the variable name without the braces const key = match.substring(1, match.length - 1); allState[key] = ''; } } } /** * Creates empty strings for all non-initialized state variables * referenced in agent instructions * * @param agent The root agent to process * @param initializedStates Optional object with already initialized states * @returns An object with empty strings for all non-initialized state variables */ function createEmptyState(agent, initializedStates = {}) { const nonInitializedStates = {}; // Find all state variables in the agent hierarchy _createEmptyState(agent, nonInitializedStates); // Remove any variables that are already initialized for (const key in initializedStates) { if (key in nonInitializedStates) { delete nonInitializedStates[key]; } } return nonInitializedStates; }