lib / tools.js
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.toolsProvider = toolsProvider;
const sdk_1 = require("@lmstudio/sdk");
const zod_1 = require("zod");
const db = __importStar(require("./db"));
const config_1 = require("./config");
function getDatabaseConfig(ctl) {
const config = ctl.getPluginConfig(config_1.configSchematics);
return {
dbType: config.get("dbType"),
host: config.get("host"),
port: config.get("port"),
username: config.get("username"),
password: config.get("password"),
database: config.get("database"),
connectionPool: config.get("connectionPool"),
};
}
const jsonScalar = zod_1.z.union([zod_1.z.string(), zod_1.z.number(), zod_1.z.boolean(), zod_1.z.null()]);
const jsonValue = zod_1.z.lazy(() => zod_1.z.union([jsonScalar, zod_1.z.array(jsonValue), zod_1.z.record(jsonValue)]));
async function toolsProvider(ctl) {
const config = getDatabaseConfig(ctl);
const tools = [];
tools.push((0, sdk_1.tool)({
name: "query_database",
description: "Execute a SELECT query against the configured database and return the resulting rows.",
parameters: {
sql: zod_1.z.string().describe("SQL SELECT statement to execute."),
params: zod_1.z.array(jsonScalar).optional().describe("Positional query parameters."),
},
implementation: async ({ sql, params }) => {
try {
await db.initializeConnection(config);
const results = await db.query(sql, params ?? []);
return {
success: true,
rowCount: results.length,
data: results,
};
}
catch (error) {
return {
success: false,
error: String(error),
};
}
},
}));
tools.push((0, sdk_1.tool)({
name: "insert_data",
description: "Insert one or more rows into a table and return the affected row count.",
parameters: {
table: zod_1.z.string().describe("Table name."),
rows: zod_1.z
.array(zod_1.z.record(jsonValue))
.min(1)
.describe("Array of objects representing rows to insert."),
},
implementation: async ({ table, rows }) => {
try {
await db.initializeConnection(config);
const columns = Object.keys(rows[0]);
const placeholders = columns.map(() => "?").join(",");
const sql = `INSERT INTO ${table} (${columns.join(", ")}) VALUES (${placeholders})`;
let totalAffected = 0;
for (const row of rows) {
const values = columns.map((col) => row[col]);
const result = await db.execute(sql, values);
totalAffected += result.affectedRows;
}
return {
success: true,
affectedRows: totalAffected,
};
}
catch (error) {
return {
success: false,
error: String(error),
};
}
},
}));
tools.push((0, sdk_1.tool)({
name: "update_data",
description: "Update rows in a table using a WHERE clause.",
parameters: {
table: zod_1.z.string().describe("Table name."),
updates: zod_1.z.record(jsonValue).describe("Columns and values to update."),
whereClause: zod_1.z
.string()
.describe("WHERE clause without the WHERE keyword, for example id = ?."),
params: zod_1.z.array(jsonScalar).optional().describe("Parameters for the WHERE clause."),
},
implementation: async ({ table, updates, whereClause, params }) => {
try {
await db.initializeConnection(config);
const setClause = Object.keys(updates)
.map((col) => `${col} = ?`)
.join(", ");
const updateValues = Object.values(updates);
const allParams = [...updateValues, ...(params ?? [])];
const sql = `UPDATE ${table} SET ${setClause} WHERE ${whereClause}`;
const result = await db.execute(sql, allParams);
return {
success: true,
affectedRows: result.affectedRows,
};
}
catch (error) {
return {
success: false,
error: String(error),
};
}
},
}));
tools.push((0, sdk_1.tool)({
name: "delete_data",
description: "Delete rows from a table using a WHERE clause.",
parameters: {
table: zod_1.z.string().describe("Table name."),
whereClause: zod_1.z
.string()
.describe("WHERE clause without the WHERE keyword, for example id = ?."),
params: zod_1.z.array(jsonScalar).optional().describe("Parameters for the WHERE clause."),
},
implementation: async ({ table, whereClause, params }) => {
try {
await db.initializeConnection(config);
const sql = `DELETE FROM ${table} WHERE ${whereClause}`;
const result = await db.execute(sql, params ?? []);
return {
success: true,
affectedRows: result.affectedRows,
};
}
catch (error) {
return {
success: false,
error: String(error),
};
}
},
}));
tools.push((0, sdk_1.tool)({
name: "get_schema",
description: "Retrieve the database schema, including tables and columns.",
parameters: {},
implementation: async () => {
try {
await db.initializeConnection(config);
const schema = await db.getSchema();
return {
success: true,
schema,
};
}
catch (error) {
return {
success: false,
error: String(error),
};
}
},
}));
tools.push((0, sdk_1.tool)({
name: "execute_transaction",
description: "Execute a sequence of query or execute operations. Note: this currently runs sequentially and is not a real SQL transaction.",
parameters: {
operations: zod_1.z
.array(zod_1.z.object({
type: zod_1.z.enum(["query", "execute"]),
sql: zod_1.z.string(),
params: zod_1.z.array(jsonScalar).optional(),
}))
.min(1)
.describe("Operations to execute in sequence."),
},
implementation: async ({ operations }) => {
try {
await db.initializeConnection(config);
const results = [];
for (const op of operations) {
if (op.type === "query") {
const result = await db.query(op.sql, op.params ?? []);
results.push({ type: "query", data: result });
}
else {
const result = await db.execute(op.sql, op.params ?? []);
results.push({ type: "execute", affectedRows: result.affectedRows });
}
}
return {
success: true,
results,
};
}
catch (error) {
return {
success: false,
error: String(error),
};
}
},
}));
return tools;
}
//# sourceMappingURL=tools.js.map