// A SWEEP RESTRICTED TO GIVEN RECORDINGS (umtool R4; a general sweep option — // a refresh's new recordings, R6's escalation). The agent keeps its corpus // tools, narrowed: // // enumerate_matches scans only the given ids (R6.1: their pages) and // answers only their rows (and says so) // get_transcripts, the given ids only: the others are dropped from the // extract_batch call (and the answer says which); none left refuses // get_transcript, an id outside refuses // get_post, get_thread // search_transcripts not offered (its hits are the whole corpus's) // // Pure over the tools it is handed. import type { AgentTool } from "../agentTool"; const ROW = /^- (\S+) \| (?:video|post|audio) \| /; /** enumerate_matches' answer with only the rows of `only` kept, and a line saying so. */ export function restrictEnumerateText(text: string, only: ReadonlySet): string { let kept = 0; let dropped = 0; const lines: string[] = []; for (const raw of text.split("\n")) { const m = ROW.exec(raw.trim()); if (m) { if (only.has(m[1]!)) { kept++; lines.push(raw); } else dropped++; continue; } lines.push(raw); } return `${lines.join("\n")}\n\n(this sweep is restricted to ${only.size} recording(s): ${kept} of the ${kept + dropped} row(s) above are kept)`; } const idsOf = (v: unknown) => (Array.isArray(v) ? v.filter((x): x is string => typeof x === "string") : []); /** A tool narrowed to `only` (see the head of this file); null: not offered. */ export function restrictedTool(t: AgentTool, only: ReadonlySet): AgentTool | null { const outside = (id: string) => `restricted: ${id} is not one of this sweep's ${only.size} recording(s) (${[...only].slice(0, 20).join(", ")})`; switch (t.name) { case "search_transcripts": return null; case "enumerate_matches": // R6.1: the scan itself is restricted (`ids`: only the pages holding // them are read), so a cap on the scope never hides them; the rows are // still filtered here for an engine that ignores `ids`. return { ...t, run: async (input: unknown, ctx) => restrictEnumerateText(await t.run({ ...((input ?? {}) as Record), ids: [...only] }, ctx), only) }; case "get_transcripts": case "extract_batch": return { ...t, run: async (input: unknown, ctx) => { const i = (input ?? {}) as Record; const ids = idsOf(i.video_ids); const keep = ids.filter((id) => only.has(id)); const drop = ids.filter((id) => !only.has(id)); if (!keep.length) throw new Error(ids.length ? outside(ids[0]!) : `${t.name} needs video_ids`); const out = await t.run({ ...i, video_ids: keep }, ctx); return drop.length ? `${out}\n\n(not read — outside this sweep's recordings: ${drop.join(", ")})` : out; }, }; case "get_transcript": case "get_post": case "get_thread": { const key = t.name === "get_transcript" ? "video_id" : "post_id"; return { ...t, run: async (input: unknown, ctx) => { const i = (input ?? {}) as Record; const id = typeof i[key] === "string" ? (i[key] as string) : typeof i.thread_id === "string" ? (i.thread_id as string) : ""; if (id && !only.has(id)) throw new Error(outside(id)); return t.run(input, ctx); }, }; } default: return t; } } /** Every tool narrowed to `only` (none given: the tools as they are). */ export function restrictTools(tools: readonly AgentTool[], only: readonly string[] | undefined): AgentTool[] { if (!only?.length) return [...tools]; const set = new Set(only); return tools.map((t) => restrictedTool(t, set)).filter((t): t is AgentTool => t !== null); }