index_build
Create and manage search indexes for local LLMs by specifying root directories. Simplify information retrieval and enhance document accessibility within the TOOL4LM server environment.
Instructions
Alias of index.build
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| root | No |
Implementation Reference
- src/tools/doc.ts:58-76 (handler)The core handler function `indexBuild` that scans the specified root directory (or sandbox default), extracts text from supported files (txt, md, html, pdf), builds a MiniSearch index, and saves it to .cache/index.json.export async function indexBuild(root?: string) { const base = root ? path.resolve(root) : CONFIG.sandboxDir; const files = await collectFiles(base); const docs: DocRecord[] = []; for (const p of files) { const { title, text } = await fileToText(p); docs.push({ id: p, path: p, title, text }); } const mini = new MiniSearch({ fields: ['title','text'], storeFields: ['path','title'], searchOptions: { boost: { title: 2 } } }); mini.addAll(docs); const payload = { docs, index: mini.toJSON() }; await fs.mkdir(path.dirname(INDEX_PATH), { recursive: true }).catch(()=>{}); await fs.writeFile(INDEX_PATH, JSON.stringify(payload)); return { ok: true, indexed: docs.length }; }
- src/server.ts:150-156 (registration)Registration of the 'index_build' tool (alias), which invokes the `indexBuild` handler with the optional `root` parameter.server.tool('index_build', 'Alias of index.build', indexBuildShape, OPEN, async ({ root }) => { const res = await indexBuild(root); return { content: [{ type: 'text', text: JSON.stringify(res) }] }; } );
- src/server.ts:142-142 (schema)Zod schema defining the input for indexBuild tools: optional `root` directory path.const indexBuildShape = { root: z.string().optional() };
- src/server.ts:143-149 (registration)Primary registration of the 'index.build' tool, which the 'index_build' alias references; invokes the `indexBuild` handler.server.tool('index.build', 'Build MiniSearch index for documents in sandbox directory.', indexBuildShape, OPEN, async ({ root }) => { const res = await indexBuild(root); return { content: [{ type: 'text', text: JSON.stringify(res) }] }; } );