UNPKG

@jackhua/mini-langchain

Version:

A lightweight TypeScript implementation of LangChain with cost optimization features

102 lines 2.6 kB
"use strict"; /** * Base tool interface and implementations */ Object.defineProperty(exports, "__esModule", { value: true }); exports.ToolManager = exports.BaseTool = void 0; /** * Base class for tools */ class BaseTool { /** * Format the tool for prompt inclusion */ formatForPrompt() { return `${this.name}: ${this.description}`; } } exports.BaseTool = BaseTool; /** * Tool manager for agent use */ class ToolManager { constructor() { this.tools = new Map(); } /** * Register a tool */ registerTool(tool) { this.tools.set(tool.name.toLowerCase(), tool); } /** * Register multiple tools */ registerTools(tools) { tools.forEach(tool => this.registerTool(tool)); } /** * Get a tool by name */ getTool(name) { return this.tools.get(name.toLowerCase()); } /** * Get all available tools */ getAllTools() { return Array.from(this.tools.values()); } /** * Execute a tool */ async executeTool(toolName, input) { const startTime = Date.now(); const tool = this.getTool(toolName); if (!tool) { return { tool: toolName, input, output: '', error: `Tool "${toolName}" not found` }; } try { const output = await tool.execute(input); return { tool: toolName, input, output, executionTime: Date.now() - startTime }; } catch (error) { return { tool: toolName, input, output: '', error: error instanceof Error ? error.message : String(error), executionTime: Date.now() - startTime }; } } /** * Format all tools for prompt */ formatToolsForPrompt() { const tools = this.getAllTools(); if (tools.length === 0) return 'No tools available.'; return tools .map(tool => { // Use formatForPrompt if available (BaseTool), otherwise fallback to name and description if ('formatForPrompt' in tool && typeof tool.formatForPrompt === 'function') { return `- ${tool.formatForPrompt()}`; } return `- ${tool.name}: ${tool.description}`; }) .join('\n'); } } exports.ToolManager = ToolManager; //# sourceMappingURL=base.js.map