UNPKG

cline-sdk

Version:

Comprehensive SDK for Cline - AI-powered development assistant with database integration, execution modes, and custom functions

113 lines 4.38 kB
"use strict"; /** * Custom Function Tool - Bridge between AI and custom registered functions * * This tool allows the AI to call user-registered functions as if they were * built-in tools. It acts as a dynamic dispatcher. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.CustomFunctionTool = void 0; class CustomFunctionTool { constructor(registry) { this.registry = registry; } async execute(input) { const startTime = Date.now(); try { const { function_name, arguments: args } = input; // Validate function exists const func = this.registry.getFunction(function_name); if (!func) { return { success: false, error: `Function '${function_name}' is not registered`, function_name, execution_time_ms: Date.now() - startTime, }; } // Parse arguments - handle both array and string formats let parsedArgs = []; if (args) { if (typeof args === "string") { // If arguments are passed as a string, try to parse them parsedArgs = args.split(",").map((arg) => { const trimmed = arg.trim(); // Try to parse as number if (!isNaN(Number(trimmed))) { return Number(trimmed); } // Remove quotes if present if (trimmed.startsWith('"') && trimmed.endsWith('"')) { return trimmed.slice(1, -1); } if (trimmed.startsWith("'") && trimmed.endsWith("'")) { return trimmed.slice(1, -1); } return trimmed; }); } else if (Array.isArray(args)) { parsedArgs = args; } } // Call the function const result = await this.registry.call(function_name, parsedArgs); return { success: true, result, function_name, execution_time_ms: Date.now() - startTime, }; } catch (error) { return { success: false, error: error instanceof Error ? error.message : "Unknown error", function_name: input.function_name, execution_time_ms: Date.now() - startTime, }; } } getDefinition() { const availableFunctions = this.registry.getFunctionNames(); return { name: "CustomFunction", description: `Execute a custom registered function. Available functions: ${availableFunctions.join(", ")}`, inputSchema: { type: "object", properties: { function_name: { type: "string", description: "Name of the custom function to execute", enum: availableFunctions, }, arguments: { oneOf: [ { type: "array", description: "Arguments to pass to the function as an array", items: { type: "string", }, }, { type: "string", description: "Arguments to pass to the function as a comma-separated string", }, ], }, }, required: ["function_name"], }, }; } /** * Get detailed definitions for all registered functions * This provides better context to the AI about what each function does */ getDetailedDefinitions() { return this.registry.getToolDefinitions(); } } exports.CustomFunctionTool = CustomFunctionTool; //# sourceMappingURL=custom-function-tool.js.map