local-agent
Version:
A CLI agentic system for orchestrating tools and memory with per-folder scoping
130 lines (122 loc) • 4.8 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
/**
* @fileoverview
* Command-line interface for launching the local agent with configured tools and API keys.
* This CLI loads configuration files, initializes all Model Context Protocol (MCP) tools,
* and starts an interactive session for agent operations.
*
* Usage: bun cli.ts [options]
*
* For configuration and usage details, see README.md.
*/
require("dotenv/config");
const initialization_1 = require("./initialization");
// Help option: show usage and config instructions if -h or --help is present
if (process.argv.includes('-h') || process.argv.includes('--help')) {
console.log(`
Usage: bun cli.ts [options]
This CLI launches the agent with your configured tools and keys.
Options:
-h, --help Show this help message
Configuration:
1. Place the following files in the project root:
- system.md (system prompt and instructions)
- local-agent.json (agent configuration)
- mcp-tools.json (MCP tool definitions)
- keys.json (API keys for providers)
2. Edit keys.json to include your API keys:
{
"openai": "sk-...",
"openrouter": "or-...",
// Add other provider keys as needed
}
3. Edit mcp-tools.json to define your MCP tools.
See README.md for examples.
4. To run the CLI:
npx local-agent
The agent will load your configuration and keys, initialize all MCP tools, and start an interactive session.
For more details, see README.md.
`);
process.exit(0);
}
const interactions_1 = require("./interactions");
const memory_1 = require("./memory");
const path_1 = require("path");
const fs_1 = require("fs");
const REQUIRED_FILES = [
"system.md",
"local-agent.json",
"mcp-tools.json",
"keys.json"
];
const MEMORY_DIR = "memory";
/**
* Determines the agent's display name based on the configuration.
* If a name is specified in the config, it is used. Otherwise, falls back to the parent folder name.
*
* @param {any} config - The agent configuration object, expected to have a 'name' property.
* @returns {string} The resolved agent name for display and logging.
*/
function getAgentName(config) {
if (config.name && typeof config.name === "string" && config.name.trim() !== "") {
return config.name.trim();
}
// Fallback: just the parent folder name (no "agent-" prefix)
let configPath = "local-agent.json";
if (!(0, fs_1.existsSync)(configPath)) {
// Try to find local-agent.json in cwd or subfolders
configPath = require.resolve("./local-agent.json", { paths: [process.cwd()] });
}
return (0, path_1.basename)((0, path_1.dirname)((0, path_1.resolve)(configPath)));
}
/**
* Main entry point for the CLI.
* Loads configuration files, sets up API keys, initializes all MCP tools,
* prepares the session memory log, and starts the interactive agent session.
*
* This function is asynchronous and is invoked immediately at the end of the script.
*/
async function main() {
const { config, tools, keys } = await (0, initialization_1.validateAndLoadFiles)(REQUIRED_FILES, MEMORY_DIR);
const agentName = getAgentName(config);
// Set API keys from keys.json or environment variables
const openaiApiKey = keys["openai"] || process.env.OPENAI_API_KEY;
if (openaiApiKey) {
process.env.OPENAI_API_KEY = openaiApiKey;
}
const openrouterApiKey = keys["openrouter"] || process.env.OPENROUTER_API_KEY;
if (openrouterApiKey) {
process.env.OPENROUTER_API_KEY = openrouterApiKey;
}
// Load all MCP tools
const { loadedTools, toolStatus } = await (0, initialization_1.loadAllMcpTools)(tools);
// Display a single row listing all tools, green if loaded, red if not
const toolRow = "tools: [" +
toolStatus
.map((t) => t.status === "success"
? `${initialization_1.GREEN}${t.name}${initialization_1.RESET}`
: `${initialization_1.RED}${t.name}${initialization_1.RESET}`)
.join(", ") +
"]";
console.log(`${agentName} CLI`);
console.log(toolRow);
// Prepare session memory log file
const now = new Date();
let toolStatusMd = "";
for (const t of toolStatus) {
if (t.status === "success") {
toolStatusMd += `- ${t.name}: ✅ loaded\n`;
}
else {
toolStatusMd += `- ${t.name}: ❌ failed (${t.error})\n`;
}
}
toolStatusMd += "\n";
const sessionFile = (0, memory_1.createSessionFile)(now, toolRow, toolStatusMd, agentName);
// Pass all loaded tools to the session
(0, interactions_1.runInteractiveSession)(config, loadedTools, sessionFile, agentName);
}
main();
//# sourceMappingURL=cli.js.map