src / documentSearch.ts
import type { Chat, GeneratorController, LLMTool } from "@lmstudio/sdk";
import { prepareSearchHistory } from "./fileSearch/historyDocuments";
import { documentToolIdentity } from "./fileSearch/toolNames";
function hasDocumentSearch(tools: LLMTool[]): boolean {
return ["list_documents", "search_documents", "read_document"].every(name =>
tools.some(tool => {
const actual = tool.function.name;
const schema = tool.function.parameters as {properties?: Record<string, unknown>} | undefined;
return documentToolIdentity(actual)?.key===name
&& !!schema?.properties?.collectionId;
}));
}
export async function prepareDocumentSearch(history: Chat, ctl: GeneratorController, tools: LLMTool[]): Promise<Chat> {
if (!history.getAllFiles(ctl.client).some(file => !file.isImage())) return history;
if (!hasDocumentSearch(tools)) {
throw new Error("Document attachments are waiting in this chat (including Insert messages). Enable yuanjumay/file-search (File Search) with List Documents, Search Documents and Read Document (legacy list_documents/search_documents/read_document are also supported), then generate again. Strata cannot forward raw documents as images.");
}
return prepareSearchHistory(history, ctl.client, ctl.abortSignal);
}