src / toolsProvider.ts

import { tool, Tool, ToolsProviderController } from "@lmstudio/sdk";
import { z } from "zod";
import { configSchematics } from "./config";
import { extractWebContent } from "./services/ingestion/web";
import { downloadAndTranscribeYoutube } from "./services/ingestion/youtube";
import * as path from "path";
import * as fs from "fs";

export async function toolsProvider(ctl: ToolsProviderController) {
  const config = ctl.getPluginConfig(configSchematics);
  const tools: Tool[] = [];

  const addSourceTool = tool({
      name: "add_source",
      description: "Adds a web page or YouTube video to the OpenBook context.",
      parameters: {
          url: z.string().url().describe("The URL to add (Web page or YouTube)"),
          type: z.enum(["web", "youtube"]).describe("The type of source")
      },
      implementation: async ({ url, type }) => {
          try {
              if (type === "web") {
                  const result = await 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 downloadAndTranscribeYoutube(url, tempDir, whisperPath);
                  return `SOURCE ADDED (YOUTUBE):\nURL: ${url}\n\nTranscript:\n${transcript}`;
              }
              return "Unknown source type.";
          } catch (e: any) {
              return `Error adding source: ${e.message}`;
          }
      }
  });

  const podcastTool = tool({
      name: "generate_podcast_script",
      description: "Generates a detailed, long-form podcast script based on the current context.",
      parameters: {
          topic: z.string().describe("The main topic of the podcast"),
          hosts: z.array(z.string()).optional().default(["Nova", "Sage"]).describe("Names of the hosts (can be more than 2)"),
          dispositions: z.string().optional().describe("Describe the hosts' personalities (e.g., 'Nova is skeptical, Sage is an optimist')"),
          length: z.enum(["short", "medium", "long"]).optional().default("long").describe("Desired script length (short=500 words, long=2000+ words)")
      },
      implementation: async ({ topic, hosts, dispositions, length }) => {
          let lengthInstruction = "";
          if (length === "long") {
              lengthInstruction = "This should be an exhaustive, long-form deep dive (at least 2000 words). Go into granular detail about all provided source materials.";
          } else if (length === "medium") {
              lengthInstruction = "Provide a comprehensive discussion of moderate length (around 1000 words).";
          } else {
              lengthInstruction = "Provide a concise summary in dialogue format (around 500 words).";
          }

          const personalityNote = dispositions ? `\nHost Dispositions: ${dispositions}` : "";

          return `INSTRUCTION: Please generate a lively podcast script about "${topic}".
Hosts: ${hosts.join(", ")}${personalityNote}

Length Requirement: ${lengthInstruction}

GUIDELINES:
1. Use the FULL context provided in the previous messages (documents, web pages, transcripts).
2. Incorporate specific quotes and complex details from the sources.
3. Ensure a natural flow with host banter, transitions, and deep analysis.
4. DO NOT summarize; instead, have the hosts DISCUSS the material in depth.
5. Take advantage of the large context window to be as thorough as possible.

Format the output strictly as a dialogue script.`;
      }
  });

  tools.push(addSourceTool);
  tools.push(podcastTool);
  
  return tools;
}