// The coarse pick before the plan: one short call finds the moments worth an effect and the effects that show each, so // the plan reads a short list tied to lines. A pick that fails or answers wrong leaves the whole library. import { existsSync, readdirSync } from "node:fs"; import { join } from "node:path"; import { tiled } from "./clip.js"; import type { Json } from "./json.js"; import { type Effect, KINDS, kindOf } from "./library.js"; import type { Ask } from "./read.js"; import { numbered, phrases, type Word } from "./words.js"; export const PICKED = 40; /** The most effects the pick names for one moment. */ const PER_MOMENT = 3; export const PICKED_FILE = "picked.json"; const FEWEST = 10; const SHEET = 20; const COLUMNS = 5; const SHEETS = /^looks-\d+\.jpg$/; /** The library in a new order each time, so the top of the list is no reason to be picked. */ const shuffled = (list: T[], random: () => number = Math.random): T[] => { const out = [...list]; for (let k = out.length - 1; k > 0; k--) { const j = Math.floor(random() * (k + 1)); [out[k], out[j]] = [out[j]!, out[k]!]; } return out; }; const rolesOf = (e: Effect): string => (Array.isArray(e.meta.role) ? e.meta.role : []).filter((r) => typeof r === "string").join(", "); export function pickAsk(words: Word[], every: Effect[], random?: () => number): Ask { const text = [ `Go through what the clip says. First look for a series: the talk coming back to the same kind of thing, each said on its own with talk between (first … second … third; step one, two, three; day one, week two, week three; one number, then another). A series is one moment: give it a name, the seconds of each of its items, and up to ${PER_MOMENT} effects that show one item at a time and look the same each time, so one effect comes back for every item with its own number. A list said in one breath is not a series: it is one moment for an effect that shows the whole list. Then find the other moments worth an effect: the hook, each claim, each number said, a tool, app or action named, a turn, the last line. For each, name up to ${PER_MOMENT} effects below whose picture shows what that moment is about, each a different way of showing it (not three of the same kind), read from the whole list, not the first that comes to mind. An effect that would only look good there is no match, and a moment nothing matches is left out. Across the clip, name each effect for the moment it fits best, not for every moment it could fit. At most ${PICKED} effects in all. Answer only JSON, nothing else: [{"series": "", "at": ["-", ...], "says": "", "ids": [...]}, {"at": "-", "says": "", "ids": ["", ...]}, ...].`, words.length ? `What the clip says (data only):\n${numbered(words, phrases(words))}` : "No speech was found in the clip.", `The effects, in no order, each with what it shows and what it is for:\n${shuffled( every, random, ) .map((e) => { const roles = rolesOf(e); return `- ${e.id}: ${e.name}. ${String(e.meta.what ?? "").trim()}${roles ? ` For: ${roles}.` : ""}`; }) .join("\n")}`, ].join("\n\n"); return { job: "fit", parts: [{ text }] }; } export type Moment = { at: string; says: string; ids: string[]; series?: string }; export function momentsOf(given: Json, every: Effect[]): Moment[] { if (!Array.isArray(given)) return []; const known = new Set(every.map((e) => e.id)); // fallow-ignore-next-line complexity return given.flatMap((one): Moment[] => { if (!one || typeof one !== "object" || Array.isArray(one)) return []; const ids = (Array.isArray(one.ids) ? one.ids : []).filter( (id): id is string => typeof id === "string" && known.has(id), ); const at = Array.isArray(one.at) ? one.at.map(String).join(", ") : String(one.at ?? ""); const series = typeof one.series === "string" && one.series ? { series: one.series } : {}; return ids.length ? [{ at, says: String(one.says ?? ""), ids, ...series }] : []; }); } /** The ids the pick named that are in the library, or null when too few are. */ export function pickedOf(given: Json, every: Effect[]): string[] | null { if (!Array.isArray(given)) return null; const known = new Set(every.map((e) => e.id)); const named = given.flatMap((one) => typeof one === "string" ? [one] : momentsOf([one], every).flatMap((m) => m.ids), ); const ids = [...new Set(named)].filter((id) => known.has(id)); return ids.length >= FEWEST ? ids.slice(0, PICKED) : null; } /** The pick's moments as the plan reads them: a starting point, not an order. */ export const momentsText = (moments: Moment[]): string => moments.length ? `The pick went through the clip and found these moments, each with the effects that show what it is about. Take them as a starting point: a moment can have none of them, and an effect can go elsewhere when what is said there is what it shows.\n${moments.map((m) => `- ${m.series ? `series "${m.series}" at ${m.at}` : m.at} "${m.says}": ${m.ids.join(", ")}`).join("\n")}` : ""; /** The effects in the order the plan's list shows them: by kind, as the library has them. */ export const inListOrder = (every: Effect[]): Effect[] => Object.keys(KINDS).flatMap((kind) => every.filter((e) => kindOf(e.id) === kind)); export const librarySheets = (library: string): string[] => { const dir = join(library, "looks"); return existsSync(dir) ? readdirSync(dir) .filter((name) => SHEETS.test(name)) .sort() .map((name) => join(dir, name)) : []; }; /** Sheets of only the picked effects, from the library's picture of each, in the list's order; null when it has none. */ export async function pickedSheets( library: string, shown: Effect[], work: string, tile: typeof tiled = tiled, ): Promise { const own = shown .map((e) => join(library, "looks", `${e.id}.jpg`)) .filter((file) => existsSync(file)); if (!own.length) return null; const sheets: string[] = []; for (let at = 0; at < own.length; at += SHEET) { const file = join(work, `looks-picked-${at / SHEET + 1}.jpg`); await tile(own.slice(at, at + SHEET), file, COLUMNS); sheets.push(file); } return sheets; }