UNPKG

@guyycodes/plugin-sdk

Version:

AI-powered plugin scaffolding tool - Create full-stack applications with 7+ AI models, 50+ business integrations, and production-ready infrastructure

221 lines (190 loc) 6.5 kB
// tools.js // This file will contain the functions for tools. const fs = require('fs-extra'); const path = require('path'); const chalk = require('chalk'); async function createToolsFiles(serverPath) { console.log(chalk.blue('🔧 Creating tools files...')); // Create tools directory const toolsPath = path.join(serverPath, 'tools'); fs.ensureDirSync(toolsPath); // Create __init__.py await createToolsInit(toolsPath); // Create tools.py await createTools(toolsPath); console.log(chalk.green('✅ Tools files created successfully')); } async function createToolsInit(toolsPath) { const initPy = `""" Tools package for LangGraph chatbot Provides tools for web search, calculations, and integrations """`; fs.writeFileSync(path.join(toolsPath, '__init__.py'), initPy); } async function createTools(toolsPath) { const toolsPy = `""" Tools module - Contains all available tools for the agent system """ import os import json import math from typing import Dict, List, Any from langchain_core.tools import tool from tavily import TavilyClient # Initialize Tavily client for web search TAVILY_API_KEY = os.getenv("TAVILY_API_KEY") if TAVILY_API_KEY: tavily_client = TavilyClient(api_key=TAVILY_API_KEY) else: tavily_client = None # Configuration TOP_RESULTS = 3 # Number of top search results to return @tool def web_search(query: str) -> str: """ Search the web for current information about any topic. Use this when you need up-to-date information or facts. Args: query: The search query """ if not tavily_client: return json.dumps({ "query": query, "error": "Web search is not configured. Please set TAVILY_API_KEY.", "results": [] }) try: # Perform the search search_response = tavily_client.search(query, max_results=TOP_RESULTS) print(f"Tavily results: {search_response}") # Format the results if search_response and "results" in search_response: results = search_response["results"][:TOP_RESULTS] formatted_results = [] for result in results: formatted_results.append({ "title": result.get("title", ""), "url": result.get("url", ""), "contentSummary": result.get("content", result.get("snippet", "")), "images": search_response.get("images", []), "sources": [result.get("url", "")], "files": [] }) # Return as JSON string for the LLM to process return json.dumps({ "query": query, "results": formatted_results, "resultCount": len(formatted_results) }, indent=2) else: return json.dumps({ "query": query, "results": [], "resultCount": 0, "message": "No results found for your search query." }) except Exception as e: print(f"Error in web search tool: {e}") return json.dumps({ "query": query, "error": f"Error performing search: {str(e)}", "results": [] }) @tool def calculator(expression: str) -> str: """ Perform mathematical calculations. Use this for arithmetic, algebra, or any mathematical computations. Args: expression: Mathematical expression to evaluate (e.g., "2 + 2", "sqrt(16)") """ try: # Create a safe namespace for eval safe_dict = { 'sqrt': math.sqrt, 'pow': pow, 'abs': abs, 'round': round, 'pi': math.pi, 'e': math.e, 'sin': math.sin, 'cos': math.cos, 'tan': math.tan, 'log': math.log, 'log10': math.log10, 'exp': math.exp, 'floor': math.floor, 'ceil': math.ceil, 'factorial': math.factorial, } # WARNING: eval is dangerous! Use a proper math parser in production # Consider using libraries like numexpr or sympy for safer evaluation result = eval(expression, {"__builtins__": {}}, safe_dict) return f"Result: {result}" except Exception as e: return f"Error calculating {expression}: {str(e)}" def list_tools() -> Dict[str, List[Dict[str, str]]]: """ Return a list of available tools with their descriptions """ return { "tools": [ { "name": "web_search", "description": "Search the web for current information about any topic", }, { "name": "calculator", "description": "Perform mathematical calculations and computations", }, ], } # Export tools for easy access __all__ = ['web_search', 'calculator', 'list_tools'] # ============================================================================= # Integration Module References # ============================================================================= # # To use the integration tools, import them from their respective modules: # # Payment & Finance: # from tools.quickbooks_integration import QUICKBOOKS_TOOLS # from tools.square_integration import SQUARE_TOOLS # from tools.stripe_integration import STRIPE_TOOLS # # E-commerce: # from tools.shopify_integration import SHOPIFY_TOOLS # # Marketing & CRM: # from tools.mailchimp_integration import MAILCHIMP_TOOLS # from tools.hubspot_integration import HUBSPOT_TOOLS # # Communication & Collaboration: # from tools.slack_integration import SLACK_TOOLS # # Scheduling: # from tools.calendly_integration import CALENDLY_TOOLS # # Analytics & BI: # from tools.looker_studio_integration import LOOKER_STUDIO_TOOLS # # Developer Tools: # from tools.mcp_integration import MCP_TOOLS # # Then register them with your agent system. Each integration module exports # a list of tool functions that can be used directly. # # Example integration: # AVAILABLE_TOOLS = { # 'web_search': web_search, # 'calculator': calculator, # } # # Add integration tools # for tool in QUICKBOOKS_TOOLS: # AVAILABLE_TOOLS[tool.__name__] = tool `; fs.writeFileSync(path.join(toolsPath, 'tools.py'), toolsPy); } module.exports = { createToolsFiles };