UNPKG

cline-sdk

Version:

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

121 lines 4.7 kB
"use strict"; /** * Database Query Tool * * Allows the AI to execute SQL queries on PostgreSQL databases with permission control */ Object.defineProperty(exports, "__esModule", { value: true }); exports.DatabaseQueryTool = void 0; class DatabaseQueryTool { constructor(dbManager) { this.dbManager = dbManager; } async execute(input) { const { query, parameters = [], explain = false, dryRun = false } = input; const startTime = Date.now(); try { // Validate input if (!query.trim()) { return { success: false, error: "Query cannot be empty", executionTime: Date.now() - startTime, query: query, }; } let result; let queryPlan = undefined; // If dry run, just validate the query if (dryRun) { // For dry run, we'll try to explain the query without executing it try { const explainQuery = `EXPLAIN (FORMAT JSON) ${query}`; result = await this.dbManager.executeQuery(explainQuery, parameters); queryPlan = result.data; return { success: true, data: undefined, rowCount: 0, executionTime: result.executionTime, query: query, queryPlan: queryPlan, warnings: ["This was a dry run - no data was modified"], }; } catch (error) { return { success: false, error: `Query validation failed: ${error instanceof Error ? error.message : "Unknown error"}`, executionTime: Date.now() - startTime, query: query, }; } } // Get query plan if requested if (explain) { try { const explainQuery = `EXPLAIN (ANALYZE true, FORMAT JSON) ${query}`; const explainResult = await this.dbManager.executeQuery(explainQuery, parameters); queryPlan = explainResult.data; } catch (explainError) { console.warn("Could not get query plan:", explainError); } } // Execute the actual query result = await this.dbManager.executeQuery(query, parameters); return { success: result.success, data: result.data, rowCount: result.rowCount, error: result.error, executionTime: result.executionTime, query: query, queryPlan: queryPlan, }; } catch (error) { return { success: false, error: error instanceof Error ? error.message : "Unknown error", executionTime: Date.now() - startTime, query: query, }; } } getDefinition() { return { name: "DatabaseQuery", description: "Execute SQL queries on the connected PostgreSQL database. Supports SELECT, INSERT, UPDATE, DELETE operations with permission control.", inputSchema: { type: "object", properties: { query: { type: "string", description: "The SQL query to execute. Use parameterized queries with $1, $2, etc. for safety.", }, parameters: { type: "array", description: "Parameters for the parameterized query", items: { type: "string", }, }, explain: { type: "boolean", description: "Whether to include query execution plan", default: false, }, dryRun: { type: "boolean", description: "Whether to validate the query without executing it", default: false, }, }, required: ["query"], }, }; } } exports.DatabaseQueryTool = DatabaseQueryTool; //# sourceMappingURL=database-query-tool.js.map