command_chains
Returns commands executed within ±5 minutes of a matching query, revealing multi-step sequences like cd, build, deploy.
Instructions
For each match of query, return commands run within ±5 min — reveals multi-step sequences (cd → build → deploy).
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | ||
| window_ms | No | ||
| limit | No |
Implementation Reference
- src/search.ts:55-68 (handler)The core handler function `commandChains` that executes the tool logic: searches for matching commands (seeds), then for each seed fetches all commands within a ±windowMs time range, returning groups of related commands as chains.
export function commandChains(db: Database.Database, query: string, windowMs = 5 * 60 * 1000, limit = 10): SearchRow[][] { const seeds = searchHistory(db, query, limit); const out: SearchRow[][] = []; const ctx = db.prepare(` SELECT id, cmd, ts, shell, cwd, exit_code, duration_ms FROM commands WHERE ts BETWEEN ? AND ? ORDER BY ts ASC `); for (const s of seeds) { if (s.ts == null) { out.push([s]); continue; } const rows = ctx.all(s.ts - windowMs, s.ts + windowMs) as SearchRow[]; out.push(rows); } return out; } - src/index.ts:78-91 (registration)Registration of the 'command_chains' tool in the TOOLS array, defining its name, description, and input schema.
{ name: "command_chains", description: "For each match of query, return commands run within ±5 min — reveals multi-step sequences (cd → build → deploy).", inputSchema: { type: "object", properties: { query: { type: "string" }, window_ms: { type: "number", default: 300000 }, limit: { type: "number", default: 5 }, }, required: ["query"], }, }, ]; - src/index.ts:121-128 (handler)The request handler that parses arguments (query, window_ms, limit) with Zod, calls commandChains(), formats results, and returns them as tool content.
if (name === "command_chains") { const { query, window_ms, limit } = z.object({ query: z.string(), window_ms: z.number().optional(), limit: z.number().optional(), }).parse(args); const chains = commandChains(db, query, window_ms ?? 300000, limit ?? 5); const text = chains.map((c, i) => `--- chain ${i + 1} ---\n${fmt(c)}`).join("\n\n"); return { content: [{ type: "text", text: text || "(no chains)" }] }; } - src/index.ts:82-90 (schema)Input schema definition for the command_chains tool: query (string, required), window_ms (number, default 300000), limit (number, default 5).
type: "object", properties: { query: { type: "string" }, window_ms: { type: "number", default: 300000 }, limit: { type: "number", default: 5 }, }, required: ["query"], }, }, - src/search.ts:24-35 (helper)The `searchHistory` helper function is used internally by `commandChains` to find seed commands matching the query.
export function searchHistory(db: Database.Database, query: string, limit = 20): SearchRow[] { const fts = escapeFts(query); if (!fts) return []; const stmt = db.prepare(` SELECT c.id, c.cmd, c.ts, c.shell, c.cwd, c.exit_code, c.duration_ms, commands_fts.rank AS rank FROM commands_fts JOIN commands c ON c.id = commands_fts.rowid WHERE commands_fts MATCH ? ORDER BY rank LIMIT ? `); return stmt.all(fts, limit) as SearchRow[]; }