src / toolsProvider.ts
import { tool, Tool, ToolsProviderController } from "@lmstudio/sdk";
import { z } from "zod";
import { configSchematics } from "./configSchematics";
export async function toolsProvider(ctl: ToolsProviderController) {
const config = ctl.getPluginConfig(configSchematics);
const apiKey = config.get("apiKey");
const tools: Tool[] = [];
const perplexitySearchTool = tool({
name: "perplexity_search",
description:
"Search the web using Perplexity. Use this for current events, " +
"recent facts, or anything requiring up-to-date information.",
parameters: { query: z.string() },
implementation: async ({ query }) => {
if (!apiKey) {
return "Error: No Perplexity API key set. Add it in Plugin Settings.";
}
try {
const response = await fetch("https://api.perplexity.ai/search", {
method: "POST",
headers: {
Authorization: `Bearer ${apiKey}`,
"Content-Type": "application/json",
},
body: JSON.stringify({ query, max_results: 5 }),
});
if (!response.ok) {
return `Search failed: ${response.status} ${response.statusText}`;
}
const data = await response.json();
return data.results
.map(
(r: any, i: number) =>
`[${i + 1}] ${r.title}\n${r.url}\n${r.snippet}`
)
.join("\n\n");
} catch (err) {
return `Error calling Perplexity: ${err}`;
}
},
});
tools.push(perplexitySearchTool);
return tools;
}