UNPKG

adk-typescript

Version:

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

77 lines (76 loc) 3.02 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.loadDotenvForAgent = loadDotenvForAgent; const fs = __importStar(require("fs")); const path = __importStar(require("path")); const dotenv = __importStar(require("dotenv")); /** * Walk up directory structure until the specified file is found * * @param folder Starting folder to search from * @param filename Filename to look for * @returns Path to the file if found, empty string if not found */ function walkToRootUntilFound(folder, filename) { const checkpath = path.join(folder, filename); if (fs.existsSync(checkpath) && fs.statSync(checkpath).isFile()) { return checkpath; } const parentFolder = path.dirname(folder); if (parentFolder === folder) { // reached the root return ''; } return walkToRootUntilFound(parentFolder, filename); } /** * Loads the .env file for the agent module * * @param agentName Name of the agent * @param agentParentFolder Parent folder of the agent * @param filename Name of the environment file, defaults to .env */ function loadDotenvForAgent(agentName, agentParentFolder, filename = '.env') { // Get the absolute path of the agent folder as starting point const startingFolder = path.resolve(path.join(agentParentFolder, agentName)); const dotenvFilePath = walkToRootUntilFound(startingFolder, filename); if (dotenvFilePath) { dotenv.config({ path: dotenvFilePath, override: true }); console.log(`Loaded ${filename} file for ${agentName} at ${dotenvFilePath}`); } else { console.log(`No ${filename} file found for ${agentName}`); } }