mirror of
https://github.com/shinyobjectz/raea.git
synced 2026-12-06 05:33:38 -10:00
An agent asked for "the effect template for composer animations with Claude" found nothing: the catalog had one thin line per item and the search only read names and that line. Now: - tools/library-meta.sh (enrich.ts, props.ts, upstream.ts, words.ts, hand.json) matches each of the 609 items by content to the upstream component it came from (all 609 match; snapcn's terminal-simulator and text-highlight are ui/terminal-session and text/highlight-sweep here) and writes into catalog.json: upstream (a search alias only), what (what it draws and how it moves), use_when and avoid_when (upstream's useWhen/avoidWhen in plain words, other libraries' names out, pointers rewritten to our category/name or dropped), asked_as (upstream's askedAs plus the words people use for its kind: template, effect, composer, chat input, AI prompt, terminal, Claude Code...), props (name, type, default and a line each, read from its Props type with the TypeScript parser), length, fps, size, aspect and vibe. snapcn's and React Video Editor's items have no when-to-use upstream: hand.json holds those, 134. - raea make component list WORDS searches names, upstream names and asked_as heavily, what and use_when once; filler words are skipped and "template"/"effect"/"preset" alone list the items that are one with how to narrow it. - raea make component show NAME prints one item in full; list/show --json give the same as JSON. - sdk/guides/craft/components.md: a template, an effect or a branded screen goes to the library first. Co-authored-by: shinyobjectz <[email protected]>
145 lines
11 KiB
TypeScript
145 lines
11 KiB
TypeScript
// The component library (sdk/raea/library/components): other people's open components, sorted by what they're for,
|
|
// not by who wrote them. catalog.json lists each: its category and type, what it does, its own files, the shared files
|
|
// it needs, the fonts it loads and where it came from (CREDITS.md beside it holds the licences). `raea make component
|
|
// list WORDS` searches it and `raea make component add NAME` copies one into the project (index.ts). The host half
|
|
// (the files) runs on the native raea; the rest is plain strings.
|
|
import { existsSync, readFileSync, realpathSync } from "node:fs";
|
|
import { dirname, join } from "node:path";
|
|
|
|
/** One of a component's props, read from its Props type: its type as written, its default, a line on what it does */
|
|
export type Prop = { name: string; type: string; default?: string; says?: string; required?: boolean };
|
|
/** An item; the fields after `from` are tools/library-meta.sh's: the upstream component it was matched to by content
|
|
* (a search alias, never its name here), what it draws and how it moves, when to use it and when not, the phrases
|
|
* people ask for it with, its props, and the length, frame rate, size, aspect and feel it was designed at */
|
|
export type Item = {
|
|
name: string; category: string; type: string; says: string; component: string; default?: boolean; files: string[]; needs: string[]; fonts: string[]; packages?: string[]; from: string;
|
|
upstream?: string; what?: string; use_when?: string[]; avoid_when?: string[]; asked_as?: string[]; props?: Prop[]; length?: number; fps?: number; size?: string; aspect?: string; vibe?: string;
|
|
};
|
|
type Catalog = { sources: Record<string, { url: string; licence: string }>; items: Item[] };
|
|
export type Got = { item: Item; licence: string; files: Record<string, string> };
|
|
|
|
/** The library's folder: in RAEA_HOME, beside a release's bin/raea (share/raea), or in the tree a built raea is in */
|
|
function home(raea: string, env: Record<string, string | undefined>): string | undefined {
|
|
const bin = dirname(realpathSync(raea));
|
|
const rel = "sdk/raea/library/components";
|
|
return [env.RAEA_HOME && join(env.RAEA_HOME, rel), join(bin, "../share/raea", rel), join(bin, "../..", rel)].find((p) => p && existsSync(join(p, "catalog.json")));
|
|
}
|
|
|
|
/** An item by its name, category/name or category/type/name */
|
|
export const found = (items: Item[], want: string) => items.find((i) => i.name === want || `${i.category}/${i.name}` === want || `${i.category}/${i.type}/${i.name}` === want);
|
|
|
|
/** Host op "components": {op: "list"} is every item, {op: "get", name} one item with its files' text (by its path in
|
|
* the library, which is its path under components/). No library beside this raea: an empty list, and no item. */
|
|
export function componentsHost(raea: string, env: Record<string, string | undefined>, a: Record<string, unknown>): Item[] | Got | null {
|
|
const dir = home(raea, env);
|
|
if (!dir) return a.op === "get" ? null : [];
|
|
const cat = JSON.parse(readFileSync(join(dir, "catalog.json"), "utf8")) as Catalog;
|
|
if (a.op !== "get") return cat.items;
|
|
const want = String(a.name).replace(/^\/+|\/+$/g, "");
|
|
const item = found(cat.items, want);
|
|
if (!item) return null;
|
|
const files = Object.fromEntries([...item.files, ...item.needs].map((f) => [f, readFileSync(join(dir, f), "utf8")]));
|
|
return { item, licence: cat.sources[item.from]?.licence ?? "", files };
|
|
}
|
|
|
|
// what a row says: the item's own line, or what it draws when that line is a few words
|
|
const line = (i: Item) => (i.says.length >= 40 || !i.what ? i.says : i.what).split(". ")[0];
|
|
const row = (i: Item) => `${`${i.category}/${i.name}`.padEnd(34)} ${i.type.padEnd(13)} ${line(i).slice(0, 110)}`;
|
|
|
|
// words that say nothing about which one ("a", "animation", "component") are skipped; "template", "effect" and
|
|
// "preset" narrow to the items that are one when they're all a search says, and only rank otherwise
|
|
const FILLER = new Set(["a", "an", "the", "with", "for", "of", "and", "in", "on", "to", "my", "our", "some", "please", "use", "using", "raea", "component", "components", "animation", "animations", "animated", "thing", "something", "from", "like", "one"]);
|
|
const SOFT = new Set(["template", "templates", "effect", "effects", "preset", "presets"]);
|
|
const esc = (w: string) => w.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
// a word at a word's start ("count" finds "counter"); a short one only whole ("cli" isn't "click", "ai" isn't "aim")
|
|
const finds = (w: string) => (w.length <= 3 ? new RegExp(`(^|[^a-z0-9])${esc(w)}(s|es)?($|[^a-z0-9])`, "i") : new RegExp(`(^|[^a-z0-9])${esc(w.replace(/s$/, ""))}`, "i"));
|
|
|
|
/** How well an item answers the words: 0 when a word that matters isn't anywhere in it. The name, the upstream name
|
|
* and the phrases people ask for it with count most; what it does and when to use it count once */
|
|
export function scored(i: Item, ws: string[]): number {
|
|
const asked = (i.asked_as ?? []).join(" | ");
|
|
const told = [i.says, i.what ?? "", ...(i.use_when ?? [])].join(" ");
|
|
const named = (n: string | undefined, w: string) => (!n ? 0 : n.split("-").includes(w) ? 3 : w.length > 3 && n.includes(w) ? 2 : 0);
|
|
const key = ws.filter((w) => !SOFT.has(w));
|
|
let s = 0;
|
|
// the words together, as written: a phrase people ask with, a name, or a sentence of what it does
|
|
const phrase = key.join(" ");
|
|
if (key.length > 1) s += (i.asked_as ?? []).some((a) => a.toLowerCase().includes(phrase)) ? 6 : [i.name, i.upstream ?? ""].includes(key.join("-")) ? 6 : told.toLowerCase().includes(phrase) ? 3 : 0;
|
|
for (const w of ws) {
|
|
const [c, t] = w.split("/");
|
|
const re = finds(w);
|
|
let got = 0;
|
|
if (w.includes("/")) got = i.category === c && (!t || i.type === t) ? 4 : 0;
|
|
else if (i.category === w || i.type === w) got = 4;
|
|
// its name (or upstream's), the phrases it's asked for with, and its own words each add
|
|
else got = Math.max(named(i.name, w), named(i.upstream, w)) + (re.test(asked) ? 3 : 0) + (re.test(told) || i.from === w ? 1 : 0);
|
|
if (!got && !SOFT.has(w)) return 0;
|
|
s += got;
|
|
}
|
|
return s;
|
|
}
|
|
|
|
/** `raea make component list`'s lines for the library: with no words, each category and its types; with words, the
|
|
* items they name. A word is a category ("text"), a category/type ("text/reveal"), a name or upstream name, or
|
|
* anything in the phrases people ask with, what it does or when to use it; every word that matters must be found */
|
|
export function listed(items: Item[], words: string[]): string {
|
|
if (!items.length) return "";
|
|
const ws = terms(words);
|
|
if (!ws.length) {
|
|
const cats = [...new Set(items.map((i) => i.category))].sort();
|
|
const line = (c: string) => {
|
|
const of = items.filter((i) => i.category === c);
|
|
const types = [...new Set(of.map((i) => i.type))].sort().map((t) => `${t} ${of.filter((i) => i.type === t).length}`);
|
|
return `${c.padEnd(14)} ${String(of.length).padStart(3)} ${types.join(", ")}`;
|
|
};
|
|
return `\nThe library, by what a component is for (raea make component list text, list text/reveal, list "chat input", list "count up"; show NAME tells one in full; add NAME copies one):\n${cats.map(line).join("\n")}\n`;
|
|
}
|
|
const found = searched(items, ws);
|
|
if (!found.length) return `no component in the library for "${words.join(" ")}" (raea make component list shows the categories)\n`;
|
|
// "template" or "effect" alone: the items that are one, a page of them, and how to narrow it
|
|
if (ws.every((w) => SOFT.has(w))) {
|
|
const top = found.slice(0, 30);
|
|
return `${found.length} components are a ${ws[0].replace(/s$/, "")}; say what it shows to narrow it (list ${ws[0]} chat, list ${ws[0]} logo, list "${ws[0]} product demo"):\n${top.map(row).join("\n")}\n${found.length > top.length ? `... and ${found.length - top.length} more\n` : ""}`;
|
|
}
|
|
return found.map(row).join("\n") + "\n";
|
|
}
|
|
|
|
/** A search's words that matter, in lower case */
|
|
export const terms = (words: string[]) => words.join(" ").toLowerCase().split(/\s+/).filter((w) => w && !FILLER.has(w));
|
|
|
|
/** The items the words find, best first */
|
|
export function searched(items: Item[], ws: string[]): Item[] {
|
|
// equal scores: the one more of whose asked-for phrases the words are in is more about them
|
|
const res = ws.filter((w) => !w.includes("/")).map(finds);
|
|
const fit = (i: Item) => (i.asked_as ?? []).filter((a) => res.some((re) => re.test(a))).length;
|
|
return items
|
|
.map((i) => [scored(i, ws), fit(i), i] as const)
|
|
.filter(([s]) => s > 0)
|
|
.sort((a, b) => b[0] - a[0] || b[1] - a[1] || a[2].name.localeCompare(b[2].name))
|
|
.map(([, , i]) => i);
|
|
}
|
|
|
|
/** `raea make component show NAME`: everything an agent picks by, without opening the file */
|
|
export function shown(i: Item, from = "./components"): string {
|
|
const list = (head: string, xs?: string[]) => (xs?.length ? `${head}\n${xs.map((x) => ` - ${x}`).join("\n")}\n` : "");
|
|
const made = [i.size && `${i.size} (${i.aspect})`, !i.size && i.aspect, i.length && `${i.length} frames at ${i.fps ?? 30} fps`, i.vibe && `feels ${i.vibe}`].filter(Boolean).join(", ");
|
|
const props = (i.props ?? []).map((p) => ` ${p.name}${p.required ? "" : "?"}: ${p.type}${p.default !== undefined ? ` = ${p.default}` : ""}${p.says ? `\n ${p.says}` : ""}`);
|
|
return `${i.category}/${i.name} (${i.type}; from ${i.from}${i.upstream ? `, ${i.upstream} there` : ""})
|
|
${i.what ?? i.says}
|
|
${list("use it when:", i.use_when)}${list("not when:", i.avoid_when)}${i.asked_as?.length ? `asked for as: ${i.asked_as.join("; ")}\n` : ""}${made ? `designed at: ${made}\n` : ""}${props.length ? `props:\n${props.join("\n")}\n` : "props: none (its words and colours are written in the file)\n"}add it: raea make component add ${i.name}
|
|
import ${i.default ? i.component : `{ ${i.component} }`} from "${from}/${i.category}/${i.name}";
|
|
`;
|
|
}
|
|
|
|
/** What `add` says of a copied item: where it is, how to import it, the fonts it loads, whose it was */
|
|
export function added(g: Got, home: string, from: string, wrote: number, kept: number, installed = ""): string {
|
|
const i = g.item;
|
|
const pk = i.packages?.length ? `\n npm packages: ${i.packages.join(", ")}${installed ? ` (${installed})` : ` (not installed: raea add ${i.packages.join(" ")})`}` : "";
|
|
const shared = i.needs.length ? `, ${i.needs.length} shared under ${home}/_lib and beside it` : "";
|
|
const fonts = i.fonts.length ? `\n fonts: ${i.fonts.join(", ")} (loaded by name; set your brand's in its props or the file)` : "";
|
|
return `${home}/${i.category}/${i.name}/ ${i.files.length} files${shared}${kept ? ` (${kept} already here, kept)` : ""}; ${wrote} written
|
|
import ${i.default ? i.component : `{ ${i.component} }`} from "${from}/${i.category}/${i.name}"; (${i.says.split(". ")[0]})
|
|
its props and their defaults are at the top of index.tsx (or none: then its words and colours are in the file): the source's samples, make them the brief's${fonts}${pk}
|
|
from ${i.from}, ${g.licence}: yours to change; the first line of each file keeps the credit\n`;
|
|
}
|