Project Files
src / tools / docs / buildDocQueryTool.ts
// tools/buildDocQueryTool.ts
import { tool } from "@lmstudio/sdk";
import { z } from "zod";
import { lookupDocFile } from "./docFileRegistry";
import { prepareRetrievalResultsContextInjection } from "../../utils/docs/retrieval/prepareRetrieval";
import { adaptFromTool } from "../../utils/shared/pluginCtl";
import { configSchematics } from "../../config"
import { runDocSubAgent } from "./docSubAgent";
// --------- Legacy Tool (Might Change back if needed) -----------
// export function buildDocQueryTool(ctl: any) {
// return tool({
// name: "query_document_data",
// description:
// `Use this ONLY when you already know the exact fileName of an attached document ` +
// `(confirmed via list_documents or explicitly stated by the user). ` +
// `NEVER guess a fileName. If you don't know the exact fileName, call list_documents first.`,
// parameters: { question: z.string(), fileName: z.string() },
// implementation: async ({ question, fileName }, toolCtx) => {
// const file = lookupDocFile(ctl.workingDirectoryPath, fileName);
// if (!file) {
// return `Error: no file named "${fileName}" is registered for this conversation.`;
// }
// const adapted = adaptFromTool(ctl, toolCtx, configSchematics);
// return await prepareRetrievalResultsContextInjection(adapted, question, [file]);
// },
// });
// }
// Sub Agent Tool for documents
export function buildDocQueryTool(ctl: any) {
return tool({
name: "query_document_data",
description:
`Get information from a SPECIFIC attached document (pdf, docx, txt) by exact fileName — not spreadsheets. ` +
`This tool's question is sent to a sub-agent with no view of this conversation: write it fully ` +
`self-contained, including any relevant criteria or context, not implicit references.`,
parameters: {
question: z.string(),
fileName: z.string(),
},
implementation: async ({ question, fileName }, toolCtx) => {
const file = lookupDocFile(ctl.workingDirectoryPath, fileName);
if (!file) {
return `Error: no file named "${fileName}" is registered for this conversation.`;
}
const adapted = adaptFromTool(ctl, toolCtx, configSchematics);
const { trace, answer } = await runDocSubAgent(adapted, { question, file });
// Visible to the user only, per SDK docs — status is not fed to the main model
// unless echoed in the return value.
if (typeof toolCtx?.status === "function") {
toolCtx.status(trace);
}
return answer;
},
});
}