@superintern/sites 0.1.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,513 @@
1
+ // Every tool of SuperIntern's MCP server as a `sites` command.
2
+ //
3
+ // `sites crm …` and `sites site …` are not written one by one: they are read
4
+ // from the server's own `tools/list`, so whatever the server offers Claude,
5
+ // the command offers too, with the same names, arguments and refusals. A tool
6
+ // `<group>_<rest>` is `sites <group> <rest with hyphens>` (`crm_record_save`
7
+ // is `sites crm record-save`), and each argument of its input schema is a
8
+ // flag (`match_column` is `--match-column`).
9
+ //
10
+ // mcp-tools.json is the list as it was last read, for `--help` before
11
+ // signing in and for the test that every tool has a command. The command
12
+ // itself always asks the server.
13
+
14
+ import { readFileSync } from "node:fs";
15
+
16
+ import { printable } from "./api.mjs";
17
+
18
+ /** What each group of tools is for, in `sites --help`. */
19
+ export const GROUPS = /** @type {Record<string, string>} */ ({
20
+ crm: "The business's CRM: tables, records, saved views and automations",
21
+ site: "Websites: settings, history, media, redirects, analytics, traffic, search and more",
22
+ });
23
+
24
+ /** Flags every tool command takes, which no tool argument may be called. */
25
+ export const RESERVED_FLAGS = ["env", "json", "help", "input"];
26
+
27
+ /**
28
+ * The commands written by hand, and the tools each one calls. The CRM's
29
+ * public tables (`tables`, `rows`, `data-token`) are read with the site's
30
+ * data pass, as the site's build reads them: no tool reads only what is
31
+ * public. `login` checks the key with the site API's `/me`.
32
+ */
33
+ export const ALIASES = /** @type {Record<string, string[]>} */ ({
34
+ new: ["site_create", "site_git_access", "site_read"],
35
+ clone: ["site_read", "site_git_access"],
36
+ list: ["site_list"],
37
+ status: ["site_read"],
38
+ publish: ["site_read", "site_publish"],
39
+ unpublish: ["site_unpublish"],
40
+ remove: ["site_read", "site_remove"],
41
+ open: ["site_read"],
42
+ accounts: ["site_read", "site_update"],
43
+ media: ["site_read", "site_media_list", "site_media_upload", "site_media_delete"],
44
+ });
45
+
46
+ /** @typedef {import("./mcp.mjs").McpTool} McpTool */
47
+
48
+ /**
49
+ * The snapshot of every tool, for help before signing in and for the tests.
50
+ * @returns {McpTool[]}
51
+ */
52
+ export function knownTools() {
53
+ const text = readFileSync(new URL("./mcp-tools.json", import.meta.url), "utf8");
54
+ return JSON.parse(text).tools;
55
+ }
56
+
57
+ /**
58
+ * The command a tool is: `crm_record_save` → `{ group: "crm", command: "record-save" }`.
59
+ * @param {string} name
60
+ */
61
+ export function commandOf(name) {
62
+ const [group = "", ...rest] = name.split("_");
63
+ return { group, command: rest.length ? rest.join("-") : group };
64
+ }
65
+
66
+ /**
67
+ * The tool `sites <group> <words…>` names, and how many words it took:
68
+ * `record-save`, or `record save`.
69
+ * @param {McpTool[]} tools @param {string} group @param {string[]} words
70
+ * @returns {{ tool: McpTool, used: number } | null}
71
+ */
72
+ export function findTool(tools, group, words) {
73
+ const mine = tools.filter((tool) => commandOf(tool.name).group === group);
74
+ // The longest match first, so `removed list` is never read as `removed`.
75
+ for (let used = Math.min(words.length, 4); used >= 1; used--) {
76
+ const command = words.slice(0, used).join("-").replace(/_/g, "-").toLowerCase();
77
+ const tool = mine.find((candidate) => commandOf(candidate.name).command === command);
78
+ if (tool) return { tool, used };
79
+ }
80
+ return null;
81
+ }
82
+
83
+ // ---------------------------------------------------------------- flags
84
+
85
+ /**
86
+ * @typedef {object} Flag
87
+ * @property {string} property the tool's argument
88
+ * @property {string} flag `--match-column`
89
+ * @property {"string" | "number" | "boolean" | "list" | "json"} kind
90
+ * @property {"string" | "number" | "json"} [items] what each value of a list is
91
+ * @property {boolean} takesText a JSON flag that also takes plain text
92
+ * @property {boolean} required
93
+ * @property {unknown[]} [choices]
94
+ * @property {string} description
95
+ */
96
+
97
+ /** @param {any} schema @returns {string[]} */
98
+ function typesOf(schema) {
99
+ if (!schema || typeof schema !== "object") return [];
100
+ const alternatives = schema.anyOf ?? schema.oneOf;
101
+ if (Array.isArray(alternatives)) return [...new Set(alternatives.flatMap(typesOf))];
102
+ if (Array.isArray(schema.type)) return schema.type;
103
+ if (typeof schema.type === "string") return [schema.type];
104
+ if (Array.isArray(schema.enum)) return [...new Set(schema.enum.map((/** @type {unknown} */ value) => (typeof value === "number" ? "number" : typeof value)))];
105
+ return [];
106
+ }
107
+
108
+ /** @param {any} schema @returns {unknown[] | undefined} */
109
+ function choicesOf(schema) {
110
+ if (Array.isArray(schema?.enum)) return schema.enum;
111
+ const alternatives = schema?.anyOf ?? schema?.oneOf;
112
+ if (Array.isArray(alternatives)) {
113
+ const found = alternatives.flatMap((/** @type {any} */ each) => (Array.isArray(each?.enum) ? each.enum : []));
114
+ return found.length ? found : undefined;
115
+ }
116
+ return undefined;
117
+ }
118
+
119
+ /** @param {any} schema @returns {any} */
120
+ function itemsOf(schema) {
121
+ if (schema?.items) return schema.items;
122
+ const alternatives = schema?.anyOf ?? schema?.oneOf;
123
+ return Array.isArray(alternatives) ? alternatives.find((/** @type {any} */ each) => each?.items)?.items : undefined;
124
+ }
125
+
126
+ /**
127
+ * The flags a tool takes, from its input schema, required ones first.
128
+ * @param {McpTool} tool
129
+ * @returns {Flag[]}
130
+ */
131
+ export function flagsOf(tool) {
132
+ const properties = tool.inputSchema?.properties ?? {};
133
+ const required = new Set(tool.inputSchema?.required ?? []);
134
+ /** @type {Flag[]} */
135
+ const flags = Object.entries(properties).map(([property, schema]) => {
136
+ const types = typesOf(schema).filter((type) => type !== "null");
137
+ const scalar = (/** @type {string} */ type) => type === "string" || type === "number" || type === "integer";
138
+ /** @type {Flag["kind"]} */
139
+ let kind = "json";
140
+ /** @type {Flag["items"]} */
141
+ let items;
142
+ if (types.length === 1 && types[0] === "boolean") kind = "boolean";
143
+ else if (types.length > 0 && types.every((type) => type === "number" || type === "integer")) kind = "number";
144
+ else if (types.length === 1 && types[0] === "string") kind = "string";
145
+ else if (types.length === 1 && types[0] === "array") {
146
+ const itemTypes = typesOf(itemsOf(schema)).filter((type) => type !== "null");
147
+ if (itemTypes.length > 0 && itemTypes.every(scalar)) {
148
+ kind = "list";
149
+ items = itemTypes.every((type) => type !== "string") ? "number" : "string";
150
+ }
151
+ }
152
+ return {
153
+ property,
154
+ flag: `--${property.replace(/_/g, "-")}`,
155
+ kind,
156
+ items,
157
+ takesText: kind === "json" && types.includes("string"),
158
+ required: required.has(property),
159
+ choices: kind === "list" ? choicesOf(itemsOf(schema)) : choicesOf(schema),
160
+ description: typeof schema?.description === "string" ? schema.description : "",
161
+ };
162
+ });
163
+ return [...flags.filter((flag) => flag.required), ...flags.filter((flag) => !flag.required)];
164
+ }
165
+
166
+ /**
167
+ * The arguments that name a tool's subject without a flag, in order: `site`
168
+ * first, then the other required arguments that are plain text.
169
+ * @param {McpTool} tool
170
+ */
171
+ export function positionalsOf(tool) {
172
+ const text = flagsOf(tool).filter((flag) => flag.required && flag.kind === "string").map((flag) => flag.property);
173
+ return text.includes("site") ? ["site", ...text.filter((name) => name !== "site")] : text;
174
+ }
175
+
176
+ export class ArgumentError extends Error {}
177
+
178
+ /**
179
+ * A tool's arguments from what was typed after `sites <group> <command>`.
180
+ *
181
+ * Flags come from the schema. `--input` takes the whole arguments object as
182
+ * JSON (or `@file.json`, or `-` for what is piped in); flags then change it.
183
+ * The first words without a flag fill `positionalsOf(tool)`.
184
+ *
185
+ * @param {McpTool} tool @param {string[]} argv
186
+ * @param {{ readFile?: (path: string) => string, stdin?: () => string }} [io]
187
+ * @returns {{ args: Record<string, unknown>, positionals: string[] }}
188
+ */
189
+ export function argumentsFrom(tool, argv, io = {}) {
190
+ const flags = flagsOf(tool);
191
+ const byName = new Map(flags.map((flag) => [flag.flag, flag]));
192
+ // `--match_column` too: the tool's own name for it.
193
+ for (const flag of flags) byName.set(`--${flag.property}`, flag);
194
+ /** @type {Record<string, unknown>} */
195
+ let args = {};
196
+ /** @type {Record<string, unknown>} */
197
+ const given = {};
198
+ /** @type {string[]} */
199
+ const positionals = [];
200
+ const readFile = io.readFile ?? ((path) => readFileSync(path, "utf8"));
201
+
202
+ for (let index = 0; index < argv.length; index++) {
203
+ const word = /** @type {string} */ (argv[index]);
204
+ if (word === "--") {
205
+ positionals.push(...argv.slice(index + 1));
206
+ break;
207
+ }
208
+ if (!word.startsWith("--") || word === "--") {
209
+ positionals.push(word);
210
+ continue;
211
+ }
212
+ const equals = word.indexOf("=");
213
+ const name = equals === -1 ? word : word.slice(0, equals);
214
+ /** @type {string | undefined} */
215
+ let value = equals === -1 ? undefined : word.slice(equals + 1);
216
+ const next = () => {
217
+ if (value !== undefined) return value;
218
+ if (index + 1 >= argv.length) throw new ArgumentError(`${name} needs a value.`);
219
+ index += 1;
220
+ return /** @type {string} */ (argv[index]);
221
+ };
222
+
223
+ if (name === "--input") {
224
+ const text = next();
225
+ const raw = text === "-" ? (io.stdin ? io.stdin() : readFileSync(0, "utf8")) : text.startsWith("@") ? readFile(text.slice(1)) : text;
226
+ const parsed = parseJson(raw, "--input");
227
+ if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) throw new ArgumentError("--input takes a JSON object of the tool's arguments.");
228
+ args = { ...args, .../** @type {Record<string, unknown>} */ (parsed) };
229
+ continue;
230
+ }
231
+
232
+ let flag = byName.get(name);
233
+ let negated = false;
234
+ if (!flag && name.startsWith("--no-")) {
235
+ flag = byName.get(`--${name.slice(5)}`);
236
+ negated = flag?.kind === "boolean";
237
+ if (!negated) flag = undefined;
238
+ }
239
+ if (!flag) {
240
+ const known = flags.map((each) => each.flag).join(", ");
241
+ throw new ArgumentError(`${name} is not an option of this command.${known ? ` It takes: ${known}.` : " It takes no options."}`);
242
+ }
243
+
244
+ if (flag.kind === "boolean") {
245
+ if (negated) {
246
+ if (value !== undefined) throw new ArgumentError(`${name} takes no value.`);
247
+ given[flag.property] = false;
248
+ continue;
249
+ }
250
+ if (value === undefined && (argv[index + 1] === "true" || argv[index + 1] === "false")) value = argv[++index];
251
+ if (value === undefined) given[flag.property] = true;
252
+ else if (value === "true" || value === "false") given[flag.property] = value === "true";
253
+ else throw new ArgumentError(`${flag.flag} is true or false.`);
254
+ continue;
255
+ }
256
+
257
+ const text = next();
258
+ if (flag.kind === "list") {
259
+ const values = text.trim().startsWith("[") ? asArray(parseJson(text, flag.flag), flag.flag) : [text];
260
+ const list = /** @type {unknown[]} */ (given[flag.property] ?? []);
261
+ for (const each of values) list.push(flag.items === "number" ? asNumber(each, flag.flag) : String(each));
262
+ given[flag.property] = list;
263
+ continue;
264
+ }
265
+ if (flag.property in given) throw new ArgumentError(`${flag.flag} was given twice.`);
266
+ if (flag.kind === "number") given[flag.property] = asNumber(text, flag.flag);
267
+ else if (flag.kind === "string") given[flag.property] = text;
268
+ else given[flag.property] = jsonValue(text, flag, readFile);
269
+ }
270
+
271
+ args = { ...args, ...given };
272
+ for (const flag of flags) {
273
+ const value = args[flag.property];
274
+ if (flag.choices && value !== undefined && value !== null) {
275
+ const values = Array.isArray(value) && flag.kind === "list" ? value : [value];
276
+ // Only a word is held to the list: where an object is allowed too, it is the server's to check.
277
+ const wrong = values.find((each) => (typeof each === "string" || typeof each === "number") && !flag.choices?.includes(each));
278
+ if (wrong !== undefined) throw new ArgumentError(`${flag.flag} is one of: ${flag.choices.join(", ")}. Not "${printable(wrong)}".`);
279
+ }
280
+ }
281
+ return { args, positionals };
282
+ }
283
+
284
+ /**
285
+ * Put the words without a flag in their places, then say what is still missing.
286
+ * @param {McpTool} tool @param {Record<string, unknown>} args @param {string[]} positionals
287
+ * @param {{ site?: string | null }} [defaults] the site whose folder this is
288
+ */
289
+ export function completeArguments(tool, args, positionals, defaults = {}) {
290
+ const places = positionalsOf(tool).filter((name) => args[name] === undefined);
291
+ // In a site's folder its site is the one meant, unless one is named.
292
+ const usesSite = places.includes("site") && defaults.site;
293
+ const open = usesSite && positionals.length < places.length ? places.filter((name) => name !== "site") : places;
294
+ if (positionals.length > open.length) {
295
+ const extra = positionals.slice(open.length);
296
+ throw new ArgumentError(`"${printable(extra[0])}" is not something this command takes. Name each thing with its option (see --help).`);
297
+ }
298
+ open.forEach((name, index) => {
299
+ if (index < positionals.length) args[name] = positionals[index];
300
+ });
301
+ if (usesSite && args.site === undefined) args.site = defaults.site;
302
+ const missing = flagsOf(tool).filter((flag) => flag.required && args[flag.property] === undefined);
303
+ if (missing.length) throw new ArgumentError(`Missing: ${missing.map((flag) => flag.flag).join(", ")}. See: ${usageLine(tool)}`);
304
+ return args;
305
+ }
306
+
307
+ /** @param {string} text @param {string} what */
308
+ function parseJson(text, what) {
309
+ try {
310
+ return JSON.parse(text);
311
+ } catch {
312
+ throw new ArgumentError(`${what} must be JSON, as in ${what} '{"name": "…"}'.`);
313
+ }
314
+ }
315
+
316
+ /** @param {string} text @param {Flag} flag @param {(path: string) => string} readFile */
317
+ function jsonValue(text, flag, readFile) {
318
+ if (text.startsWith("@") && !flag.takesText) return parseJson(readFile(text.slice(1)), flag.flag);
319
+ try {
320
+ return JSON.parse(text);
321
+ } catch {
322
+ if (flag.takesText) return text;
323
+ throw new ArgumentError(`${flag.flag} must be JSON, as in ${flag.flag} '{"…": "…"}' or '[…]', or @file.json.`);
324
+ }
325
+ }
326
+
327
+ /** @param {unknown} value @param {string} flag */
328
+ function asArray(value, flag) {
329
+ if (!Array.isArray(value)) throw new ArgumentError(`${flag} takes a value, or a JSON list of them.`);
330
+ return value;
331
+ }
332
+
333
+ /** @param {unknown} value @param {string} flag */
334
+ function asNumber(value, flag) {
335
+ const number = Number(value);
336
+ if (value === "" || !Number.isFinite(number)) throw new ArgumentError(`${flag} needs a number.`);
337
+ return number;
338
+ }
339
+
340
+ // ---------------------------------------------------------------- help
341
+
342
+ /** @param {McpTool} tool */
343
+ export function usageLine(tool) {
344
+ const { group, command } = commandOf(tool.name);
345
+ const positionals = positionalsOf(tool);
346
+ const parts = flagsOf(tool).map((flag) => {
347
+ const shown = positionals.includes(flag.property) ? `<${flag.property}>` : flag.kind === "boolean" ? flag.flag : `${flag.flag} <${hint(flag)}>`;
348
+ return flag.required ? shown : `[${shown}]`;
349
+ });
350
+ return `sites ${group} ${command}${parts.length ? ` ${parts.join(" ")}` : ""}`;
351
+ }
352
+
353
+ /** @param {Flag} flag */
354
+ function hint(flag) {
355
+ if (flag.choices && flag.kind !== "list") return flag.choices.join("|");
356
+ return { string: "text", number: "number", boolean: "", list: flag.items === "number" ? "number" : "text", json: "json" }[flag.kind];
357
+ }
358
+
359
+ /**
360
+ * `sites <group> --help`: the group's commands.
361
+ * @param {string} group @param {McpTool[]} tools @param {string} [where] a note on where the list came from
362
+ */
363
+ export function groupHelp(group, tools, where) {
364
+ const mine = tools.filter((tool) => commandOf(tool.name).group === group);
365
+ const width = Math.max(0, ...mine.map((tool) => commandOf(tool.name).command.length));
366
+ const lines = [`sites ${group}: ${GROUPS[group] ?? "SuperIntern's tools"}.`, ""];
367
+ if (mine.length === 0) lines.push("Your key can't use any of these. Ask for a key that may, then: sites login");
368
+ for (const tool of mine) {
369
+ const { command } = commandOf(tool.name);
370
+ const marks = tool.annotations?.readOnlyHint ? "" : tool.annotations?.destructiveHint ? " (changes things)" : "";
371
+ lines.push(` sites ${group} ${command.padEnd(width)} ${oneLine(tool.title ?? tool.annotations?.title ?? "")}${marks}`);
372
+ }
373
+ lines.push("", `Each one's options: sites ${group} <command> --help. --json prints the service's answer as it came.`);
374
+ if (where) lines.push(where);
375
+ return lines.join("\n");
376
+ }
377
+
378
+ /** `sites <group> <command> --help`. @param {McpTool} tool */
379
+ export function toolHelp(tool) {
380
+ const lines = [usageLine(tool), ""];
381
+ const title = oneLine(tool.title ?? tool.annotations?.title ?? "");
382
+ // A description often starts with its title: say it once.
383
+ if (title && !oneLine(tool.description ?? "").startsWith(title)) lines.push(title, "");
384
+ if (tool.description) lines.push(...wrap(tool.description, ""), "");
385
+ const flags = flagsOf(tool);
386
+ if (flags.length) {
387
+ lines.push("Options:");
388
+ for (const flag of flags) {
389
+ const head = ` ${flag.kind === "boolean" ? `${flag.flag}, --no-${flag.flag.slice(2)}` : `${flag.flag} <${hint(flag)}>`}${flag.required ? " (required)" : ""}`;
390
+ lines.push(head);
391
+ if (flag.kind === "list") lines.push(" Give it once per value, or a JSON list.");
392
+ if (flag.kind === "json") lines.push(` JSON${flag.takesText ? " or plain text" : ", or @file.json"}.`);
393
+ if (flag.description) lines.push(...wrap(flag.description, " "));
394
+ }
395
+ }
396
+ lines.push(
397
+ " --input <json> All the arguments as one JSON object (or @file.json, or - to read it from a pipe); options given as well change it.",
398
+ " --json Print the service's answer as it came.",
399
+ );
400
+ if (positionalsOf(tool).includes("site")) lines.push("", "In a site's folder the site needs no name.");
401
+ return lines.join("\n");
402
+ }
403
+
404
+ /** @param {string} text @param {string} indent */
405
+ function wrap(text, indent) {
406
+ return printableBlock(text)
407
+ .split("\n")
408
+ .map((line) => `${indent}${line.trim()}`);
409
+ }
410
+
411
+ // ---------------------------------------------------------------- output
412
+
413
+ /** A value is cut to this many characters in a table; `--json` has all of it. */
414
+ const CELL = 60;
415
+
416
+ /** @param {unknown} text */
417
+ function oneLine(text) {
418
+ return printable(text).trim();
419
+ }
420
+
421
+ /** Text from the service with its own line breaks kept, and nothing else unprintable. @param {unknown} text */
422
+ function printableBlock(text) {
423
+ return String(text)
424
+ .split(/\r?\n/)
425
+ .map((line) => printable(line))
426
+ .join("\n");
427
+ }
428
+
429
+ /** @param {unknown} value */
430
+ function scalar(value) {
431
+ return value === null || ["string", "number", "boolean", "undefined"].includes(typeof value);
432
+ }
433
+
434
+ /** @param {unknown} value */
435
+ function shownScalar(value) {
436
+ if (value === null || value === undefined) return "-";
437
+ if (typeof value === "boolean") return value ? "yes" : "no";
438
+ return oneLine(value);
439
+ }
440
+
441
+ /** A flat object, or a short list of words: what fits in one table cell. @param {unknown} value */
442
+ function cellable(value) {
443
+ return scalar(value) || (Array.isArray(value) && value.length <= 5 && value.every(scalar));
444
+ }
445
+
446
+ /** @param {unknown} value */
447
+ function cell(value) {
448
+ const text = Array.isArray(value) ? value.map(shownScalar).join(", ") : shownScalar(value);
449
+ return text.length > CELL ? `${text.slice(0, CELL - 1)}…` : text;
450
+ }
451
+
452
+ /** @param {string} key */
453
+ function label(key) {
454
+ return key.replace(/_/g, " ");
455
+ }
456
+
457
+ /**
458
+ * A tool's answer, for a person to read: names and values, a list of alike
459
+ * things as a table, and everything made safe to print.
460
+ * @param {unknown} value @param {string} [indent]
461
+ * @returns {string[]}
462
+ */
463
+ export function render(value, indent = "") {
464
+ if (scalar(value)) {
465
+ const text = typeof value === "string" ? printableBlock(value) : shownScalar(value);
466
+ return text.split("\n").map((line) => `${indent}${line}`);
467
+ }
468
+ if (Array.isArray(value)) return renderList(value, indent);
469
+ /** @type {string[]} */
470
+ const lines = [];
471
+ for (const [key, each] of Object.entries(/** @type {Record<string, unknown>} */ (value))) {
472
+ const name = `${indent}${label(key)}`;
473
+ if (typeof each === "string" && each.includes("\n")) {
474
+ lines.push(`${name}:`, ...render(each, `${indent} `));
475
+ } else if (scalar(each)) {
476
+ lines.push(`${name}: ${shownScalar(each)}`);
477
+ } else if (Array.isArray(each) && each.length === 0) {
478
+ lines.push(`${name}: none`);
479
+ } else if (Array.isArray(each) && each.every(scalar)) {
480
+ lines.push(`${name}: ${each.map(shownScalar).join(", ")}`);
481
+ } else if (!Array.isArray(each) && Object.keys(/** @type {object} */ (each)).length === 0) {
482
+ lines.push(`${name}: -`);
483
+ } else {
484
+ lines.push(`${name}:`, ...render(each, `${indent} `));
485
+ }
486
+ }
487
+ return lines;
488
+ }
489
+
490
+ /** @param {unknown[]} items @param {string} indent */
491
+ function renderList(items, indent) {
492
+ if (items.length === 0) return [`${indent}none`];
493
+ if (items.every(scalar)) return items.map((item) => `${indent}- ${shownScalar(item)}`);
494
+ const objects = items.every((item) => item !== null && typeof item === "object" && !Array.isArray(item));
495
+ if (objects) {
496
+ const rows = /** @type {Record<string, unknown>[]} */ (items);
497
+ const columns = [...new Set(rows.flatMap((row) => Object.keys(row)))];
498
+ const fits = columns.length <= 8 && rows.every((row) => Object.values(row).every(cellable));
499
+ if (fits) {
500
+ const grid = [columns.map((column) => label(column).toUpperCase()), ...rows.map((row) => columns.map((column) => cell(row[column])))];
501
+ const widths = columns.map((_, index) => Math.max(...grid.map((row) => (row[index] ?? "").length)));
502
+ const table = grid.map((row) => `${indent}${row.map((text, index) => (index === row.length - 1 ? text : text.padEnd(widths[index] ?? 0))).join(" ")}`.trimEnd());
503
+ if (Math.max(...table.map((line) => line.length)) <= 200) return table;
504
+ }
505
+ }
506
+ /** @type {string[]} */
507
+ const lines = [];
508
+ for (const item of items) {
509
+ const block = render(item, `${indent} `);
510
+ lines.push(`${indent}- ${block[0]?.trimStart() ?? ""}`, ...block.slice(1));
511
+ }
512
+ return lines;
513
+ }