Project Files
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 fetcher = __importStar(require("./fetcher"));
const parser = __importStar(require("./parser"));
const config_1 = require("./config");
function getWebConfig(ctl) {
const config = ctl.getPluginConfig(config_1.configSchematics);
return {
apiKey: config.get("apiKey"),
requestDelaySeconds: config.get("requestDelaySeconds"),
userAgent: config.get("userAgent"),
maxRetries: config.get("maxRetries"),
};
}
async function toolsProvider(ctl) {
const config = getWebConfig(ctl);
const tools = [];
tools.push((0, sdk_1.tool)({
name: "fetch_webpage",
description: "Fetch a webpage from a URL and return cleaned text content.",
parameters: {
url: zod_1.z.string().url().describe("The full URL to fetch."),
},
implementation: async ({ url }) => {
try {
return await fetcher.fetchAndClean(url, config);
}
catch (error) {
return {
success: false,
error: String(error),
};
}
},
}));
tools.push((0, sdk_1.tool)({
name: "parse_html",
description: "Parse raw HTML and extract headings, paragraphs, links, and metadata.",
parameters: {
html: zod_1.z.string().describe("HTML content to parse."),
},
implementation: async ({ html }) => {
try {
const parsed = await parser.parseHtml(html);
return {
success: true,
data: parsed,
};
}
catch (error) {
return {
success: false,
error: String(error),
};
}
},
}));
tools.push((0, sdk_1.tool)({
name: "extract_elements",
description: "Extract HTML elements using a CSS selector.",
parameters: {
html: zod_1.z.string().describe("HTML content to parse."),
selector: zod_1.z.string().describe("CSS selector to match."),
},
implementation: async ({ html, selector }) => {
try {
const elements = parser.extractElementsBySelector(html, selector);
return {
success: true,
count: elements.length,
elements,
};
}
catch (error) {
return {
success: false,
error: String(error),
};
}
},
}));
tools.push((0, sdk_1.tool)({
name: "search_in_webpage",
description: "Fetch a webpage and search for keywords, returning matches and context snippets.",
parameters: {
url: zod_1.z.string().url().describe("The URL to fetch and search."),
keywords: zod_1.z.array(zod_1.z.string()).min(1).describe("Keywords to search for."),
},
implementation: async ({ url, keywords }) => {
try {
const fetchResult = await fetcher.fetchWebpage(url, config);
if (!fetchResult.success || !fetchResult.html) {
return {
success: false,
error: fetchResult.error,
};
}
const results = parser.searchInHtml(fetchResult.html, keywords);
return {
success: true,
url,
results,
};
}
catch (error) {
return {
success: false,
error: String(error),
};
}
},
}));
return tools;
}
//# sourceMappingURL=tools.js.map