src / toolsProvider.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 config_1 = require("./config");
const web_1 = require("./services/ingestion/web");
const youtube_1 = require("./services/ingestion/youtube");
const path = __importStar(require("path"));
const fs = __importStar(require("fs"));
async function toolsProvider(ctl) {
    const config = ctl.getPluginConfig(config_1.configSchematics);
    const tools = [];
    const addSourceTool = (0, sdk_1.tool)({
        name: "add_source",
        description: "Adds a web page or YouTube video to the OpenBook context.",
        parameters: {
            url: zod_1.z.string().url().describe("The URL to add (Web page or YouTube)"),
            type: zod_1.z.enum(["web", "youtube"]).describe("The type of source")
        },
        implementation: async ({ url, type }) => {
            try {
                if (type === "web") {
                    const result = await (0, web_1.extractWebContent)(url);
                    // We need to save this as a 'virtual file' or just text. 
                    // For now, we'll write it to a temp file so the User can 'attach' it 
                    // or we can auto-attach it.
                    // *Limitation*: Plugins cannot auto-attach files to the *current* chat easily without user action 
                    // unless we manage a separate index. 
                    // Strategy: Save to a 'sources' folder and return the path, telling the model 
                    // to tell the user "I have saved the content to X, please upload it" 
                    // OR simpler: Return the content directly in the tool output so it's in context.
                    return `SOURCE ADDED (WEB):\nTitle: ${result.title}\n\nContent:\n${result.content}`;
                }
                else if (type === "youtube") {
                    const whisperPath = config.get("whisperBinaryPath");
                    const tempDir = path.join(ctl.getWorkingDirectory(), "temp_ingest");
                    if (!fs.existsSync(tempDir))
                        fs.mkdirSync(tempDir, { recursive: true });
                    const transcript = await (0, youtube_1.downloadAndTranscribeYoutube)(url, tempDir, whisperPath);
                    return `SOURCE ADDED (YOUTUBE):\nURL: ${url}\n\nTranscript:\n${transcript}`;
                }
                return "Unknown source type.";
            }
            catch (e) {
                return `Error adding source: ${e.message}`;
            }
        }
    });
    const podcastTool = (0, sdk_1.tool)({
        name: "generate_podcast_script",
        description: "Generates a podcast script based on the current context.",
        parameters: {
            topic: zod_1.z.string().describe("The main topic of the podcast"),
            hosts: zod_1.z.array(zod_1.z.string()).optional().default(["Nova", "Sage"]).describe("Names of the hosts")
        },
        implementation: async ({ topic, hosts }) => {
            // This tool acts as a signal for the model to format its output.
            // The actual generation happens by the model.
            return `INSTRUCTION: Please generate a lively podcast script about "${topic}" featuring hosts ${hosts.join(" and ")}. Use the context provided above. Format it as a dialogue.`;
        }
    });
    tools.push(addSourceTool);
    tools.push(podcastTool);
    return tools;
}