import { describeEditorFailure, editorGet, type EditorDeps } from "./editorOps"; import type { ToolAnswer } from "./archivalTools"; // ─── x_search_leads (release 38 lane S) ─── // // An X SEARCH channel's leads, read through the local editor // (GET /api/ops/channel/?leads=1&limit=N): the accounts the channel's // matching posts came from, most matches first, with each account's first and // last matching day, a sample post, and whether an X channel archives it // already. Read only: acting on a lead (archive the account on no site, never // archive it) is the operator's — the channel page's Leads, or // `pnpm ops x-search-lead`. An X search channel is never on a site, so only // the editor can answer. const SLUG_RE = /^[a-z0-9][a-z0-9._-]*$/i; export const X_SEARCH_LEADS_MAX = 500; type Lead = { handle: string; name?: string; count: number; reposts: number; first: string; last: string; sample: { url: string; text: string; createdAt: string }; archivedAs?: string[]; }; type LeadsBody = { leads?: { query?: string; search?: string; walk?: string; leads?: Lead[]; hidden?: Array<{ handle: string; count: number }>; posts?: number; authors?: number; more?: number; }; }; export function renderXSearchLeads(slug: string, body: LeadsBody): string { const l = body.leads ?? {}; const lines: string[] = []; lines.push(`X search ${slug}: ${l.search ?? l.query ?? "?"}`); lines.push(`walk: ${l.walk ?? "?"} · ${l.posts ?? 0} matching posts from ${l.authors ?? 0} accounts`); const leads = l.leads ?? []; if (leads.length === 0) lines.push("No leads yet: the search has matched nothing (fetch it first)."); for (const [i, lead] of leads.entries()) { lines.push( `${i + 1}. @${lead.handle}${lead.name ? ` (${lead.name})` : ""} — ${lead.count} match${lead.count === 1 ? "" : "es"}` + (lead.reposts ? ` (${lead.reposts} reposts)` : "") + `, ${lead.first.slice(0, 10)} – ${lead.last.slice(0, 10)}` + (lead.archivedAs?.length ? ` · archived as ${lead.archivedAs.join(", ")}` : ""), ); lines.push(` ${lead.sample.createdAt.slice(0, 10)} ${lead.sample.url} — ${lead.sample.text}`); } if (l.more) lines.push(`… ${l.more} more accounts with fewer matches (raise limit).`); if (l.hidden?.length) lines.push(`Never archive (hidden): ${l.hidden.map((h) => `@${h.handle}`).join(", ")}`); return lines.join("\n"); } export async function xSearchLeadsTool(args: Record, deps: EditorDeps): Promise { const channel = typeof args.channel === "string" ? args.channel.trim() : ""; if (!channel || !SLUG_RE.test(channel)) { return { text: "x_search_leads: channel is required (the X search channel's slug)", isError: true }; } const q = new URLSearchParams({ leads: "1" }); if (args.limit !== undefined) { const n = Number(args.limit); if (!Number.isInteger(n) || n < 1 || n > X_SEARCH_LEADS_MAX) { return { text: `x_search_leads: limit is a whole number, 1–${X_SEARCH_LEADS_MAX}`, isError: true }; } q.set("limit", String(n)); } const a = await editorGet(deps, `/api/ops/channel/${encodeURIComponent(channel)}?${q.toString()}`); if (a.kind !== "ok") return { text: describeEditorFailure("x_search_leads", a), isError: true }; return { text: renderXSearchLeads(channel, a.body as LeadsBody) }; } export const X_SEARCH_TOOLS = [ { name: "x_search_leads", description: "An X SEARCH channel's leads: the accounts whose posts matched its saved X query, most " + "matches first, each with its first and last matching day, a sample post and whether an " + "X channel archives it already. For finding which accounts keep saying something. Read " + "only — archiving an account (always on no site) or hiding it is the operator's, on the " + "channel page or `pnpm ops x-search-lead`. Needs ARCHILYZER_EDITOR_URL and WORKER_TOKEN.", inputSchema: { type: "object", properties: { channel: { type: "string", description: "The X search channel's slug (the editor's)." }, limit: { type: "number", description: `Leads to list (default 100, at most ${X_SEARCH_LEADS_MAX}).` }, }, required: ["channel"], additionalProperties: false, }, }, ] as const;