{"draft":null,"from":175,"to":318,"files":[{"path":"client/src/group-commands.ts","added":204,"removed":0,"diff":"--- /dev/null\n+++ b/client/src/group-commands.ts\n@@ -0,0 +1,204 @@\n+// Groups: a project's named sets of agents and people, each with one shared history. abso groups lists them;\n+// abso group reads one, posts in it, and joins, leaves or changes it. The Issues Worker keeps them. Other agents'\n+// and people's words are quoted as JSON, so none can print as another line.\n+\n+import { bool, type Parsed, str, UsageError } from \"./args.ts\";\n+import { type Env, type Output, plural } from \"./commands.ts\";\n+import { openIssues } from \"./issues.ts\";\n+import type { Group, GroupEvent } from \"./issues-wire.ts\";\n+import { shortWhen } from \"./times.ts\";\n+\n+const q = (s: string): string => JSON.stringify(s);\n+\n+/** One event as a line: its number, time, who, and what happened. */\n+export function eventLine(e: GroupEvent, now = Date.now()): string {\n+\tconst head = `  ${String(e.n).padStart(4)}  ${shortWhen(e.at, now)}  ${e.by}`;\n+\tif (e.kind === \"message\") {\n+\t\tconst re = e.replyTo === undefined ? \"\" : ` (re ${e.replyTo})`;\n+\t\tconst quiet = e.news !== undefined && e.news.length === 0 ? \" (history only, no news)\" : \"\";\n+\t\treturn `${head}${re}: ${q(e.text ?? \"\")}${quiet}`;\n+\t}\n+\tif (e.kind === \"joined\") return `${head} joined`;\n+\tif (e.kind === \"left\") return `${head} left`;\n+\tif (e.kind === \"added\") return `${head} added ${e.who}`;\n+\tif (e.kind === \"removed\") return `${head} removed ${e.who}${e.by === \"abso\" ? \" (no longer in the project)\" : \"\"}`;\n+\tif (e.kind === \"created\") return `${head} made the group`;\n+\tconst changes = Object.entries(e.detail ?? {})\n+\t\t.map(([k, v]) => `${k} ${JSON.stringify((v as { to: unknown }).to)}`)\n+\t\t.join(\", \");\n+\treturn `${head} set ${changes}`;\n+}\n+\n+function settingsWords(g: Group): string {\n+\treturn `${g.private ? \"private\" : \"public\"}, ${g.joinable ? \"anyone in the project may join\" : \"members are added by its maker or a project owner\"}`;\n+}\n+\n+/** abso groups: the project's groups this agent can see. */\n+export async function runGroups(args: Parsed, env: Env): Promise<Output> {\n+\tif (args.positionals.length > 0) throw new UsageError(\"abso groups takes nothing; abso group <name> reads one\");\n+\tconst { api } = openIssues(env);\n+\tconst { groups } = await api.groupList();\n+\tif (groups.length === 0) return { text: \"No groups yet. abso group new <name> makes one.\", json: { groups } };\n+\treturn {\n+\t\ttext: [\n+\t\t\t`${plural(groups.length, \"group\")}:`,\n+\t\t\t...groups.map(\n+\t\t\t\t(g) =>\n+\t\t\t\t\t`  @${g.name.padEnd(20)} ${plural(g.members, \"member\").padEnd(11)} ${g.private ? \"private\" : \"public \"}${g.joinable ? \"\" : \", added only\"}${g.member ? \"  (you're in it)\" : \"\"}`,\n+\t\t\t),\n+\t\t\t\"abso group <name> reads one; abso group <name> --join joins it.\",\n+\t\t].join(\"\\n\"),\n+\t\tjson: { groups },\n+\t};\n+}\n+\n+const ACTIONS = [\"post\", \"join\", \"leave\", \"add\", \"remove\", \"rename\", \"private\", \"joinable\"] as const;\n+\n+function onOff(word: string, flag: string): boolean {\n+\tif (word === \"on\") return true;\n+\tif (word === \"off\") return false;\n+\tthrow new UsageError(`--${flag} is on or off`);\n+}\n+\n+function names(word: string): string[] {\n+\treturn word\n+\t\t.split(\",\")\n+\t\t.map((n) => n.trim())\n+\t\t.filter((n) => n !== \"\");\n+}\n+\n+/**\n+ * abso group <name> [--post \"...\" [--reply N] [--news all|none|a,b] [--except a,b]] [--join | --leave | --add x |\n+ * --remove x | --rename new | --private on|off | --joinable on|off] [--before N] [--limit N] [--kind message|member|settings]\n+ * abso group new <name> [--private] [--closed] [--add a,b]\n+ */\n+export async function runGroup(args: Parsed, env: Env): Promise<Output> {\n+\tconst [first, second, ...extra] = args.positionals;\n+\tif (first === undefined)\n+\t\tthrow new UsageError(\"abso group <name> reads a group; abso group new <name> makes one; abso groups lists them\");\n+\tconst { api, config } = openIssues(env);\n+\tconst me = config.agent;\n+\tif (first === \"new\") {\n+\t\tif (second === undefined || extra.length > 0)\n+\t\t\tthrow new UsageError(\"abso group new <name> [--private] [--closed] [--add a,b]\");\n+\t\tconst add = str(args, \"add\");\n+\t\tconst r = await api.groupWrite(undefined, undefined, {\n+\t\t\tname: second,\n+\t\t\tprivate: str(args, \"private\") === \"\" || str(args, \"private\") === \"on\",\n+\t\t\tjoinable: !bool(args, \"closed\"),\n+\t\t\t...(add ? { members: names(add) } : {}),\n+\t\t});\n+\t\tconst members = (r.event?.detail?.members as string[] | undefined) ?? [me];\n+\t\treturn {\n+\t\t\ttext: `Made @${r.group.name} (${settingsWords(r.group)}), with ${members.join(\", \")}. abso group ${r.group.name} --post \"...\" talks in it.`,\n+\t\t\tjson: { ...r },\n+\t\t};\n+\t}\n+\tif (second !== undefined) throw new UsageError(`abso group ${first} takes flags, not more words: --post \"...\" posts`);\n+\tconst ref = first.replace(/^@/, \"\");\n+\tconst given = ACTIONS.filter((a) => args.flags.has(a));\n+\tif (given.length > 1) throw new UsageError(`one change at a time: ${given.map((g) => `--${g}`).join(\" and \")}`);\n+\tconst action = given[0];\n+\tif (action === undefined) {\n+\t\tfor (const f of [\"reply\", \"news\", \"except\"]) if (args.flags.has(f)) throw new UsageError(`--${f} goes with --post`);\n+\t\treturn readGroup(ref, args, env);\n+\t}\n+\tif (action !== \"post\")\n+\t\tfor (const f of [\"reply\", \"news\", \"except\", \"before\", \"limit\", \"kind\"])\n+\t\t\tif (args.flags.has(f)) throw new UsageError(`--${f} doesn't go with --${action}`);\n+\tif (action === \"post\") {\n+\t\tconst text = str(args, \"post\");\n+\t\tif (text === undefined || text.trim() === \"\") throw new UsageError('--post \"<words>\" says what to post');\n+\t\tconst reply = str(args, \"reply\");\n+\t\tif (reply !== undefined && !/^\\d+$/.test(reply)) throw new UsageError(\"--reply is a message's number in the group\");\n+\t\tconst news = str(args, \"news\");\n+\t\tconst except = str(args, \"except\");\n+\t\tif (news !== undefined && except !== undefined) throw new UsageError(\"--news or --except, not both\");\n+\t\tconst audience =\n+\t\t\texcept !== undefined\n+\t\t\t\t? { except: names(except) }\n+\t\t\t\t: news === undefined || news === \"all\"\n+\t\t\t\t\t? \"all\"\n+\t\t\t\t\t: news === \"none\"\n+\t\t\t\t\t\t? \"none\"\n+\t\t\t\t\t\t: names(news);\n+\t\tconst r = await api.groupWrite(ref, \"post\", {\n+\t\t\ttext,\n+\t\t\t...(reply !== undefined ? { replyTo: Number(reply) } : {}),\n+\t\t\tnews: audience,\n+\t\t});\n+\t\tconst heard = r.event?.news ?? [];\n+\t\tconst again = r.replayed ? \" (a retry: this is the post already made)\" : \"\";\n+\t\treturn {\n+\t\t\ttext: `Posted ${r.event?.n} in @${r.group.name}${again}. ${heard.length === 0 ? \"No news went out: it's in the history only.\" : `${heard.join(\", \")} hear${heard.length === 1 ? \"s\" : \"\"} it.`}`,\n+\t\t\tjson: { ...r },\n+\t\t};\n+\t}\n+\tif (action === \"join\" || action === \"leave\") {\n+\t\tconst r = await api.groupWrite(ref, action, {});\n+\t\tconst what =\n+\t\t\tr.event === undefined\n+\t\t\t\t? action === \"join\"\n+\t\t\t\t\t? `You're already in @${r.group.name}.`\n+\t\t\t\t\t: `You weren't in @${r.group.name}.`\n+\t\t\t\t: action === \"join\"\n+\t\t\t\t\t? `Joined @${r.group.name}. abso group ${r.group.name} reads its history.`\n+\t\t\t\t\t: `Left @${r.group.name}. What you wrote stays in its history.`;\n+\t\treturn { text: what, json: { ...r } };\n+\t}\n+\tif (action === \"add\" || action === \"remove\") {\n+\t\tconst who = str(args, action);\n+\t\tif (who === undefined || who === \"\") throw new UsageError(`--${action} <agent or person:id>`);\n+\t\tconst r = await api.groupWrite(ref, action, { who });\n+\t\tconst what =\n+\t\t\tr.event === undefined\n+\t\t\t\t? `${who} ${action === \"add\" ? \"was already\" : \"wasn't\"} in @${r.group.name}.`\n+\t\t\t\t: `${action === \"add\" ? \"Added\" : \"Removed\"} ${who} ${action === \"add\" ? \"to\" : \"from\"} @${r.group.name}.`;\n+\t\treturn { text: what, json: { ...r } };\n+\t}\n+\tconst body =\n+\t\taction === \"rename\" ? { name: str(args, \"rename\") } : { [action]: onOff(str(args, action) ?? \"\", action) };\n+\tconst r = await api.groupWrite(ref, \"settings\", body);\n+\treturn {\n+\t\ttext:\n+\t\t\tr.event === undefined\n+\t\t\t\t? `@${r.group.name} is already like that.`\n+\t\t\t\t: `@${r.group.name} is now ${settingsWords(r.group)}.`,\n+\t\tjson: { ...r },\n+\t};\n+}\n+\n+async function readGroup(ref: string, args: Parsed, env: Env): Promise<Output> {\n+\tconst { api } = openIssues(env);\n+\tconst before = str(args, \"before\");\n+\tconst limit = str(args, \"limit\");\n+\tif (before !== undefined && !/^\\d+$/.test(before)) throw new UsageError(\"--before is an event number\");\n+\tif (limit !== undefined && !(/^\\d+$/.test(limit) && Number(limit) >= 1 && Number(limit) <= 100))\n+\t\tthrow new UsageError(\"--limit is a number from 1 to 100\");\n+\tconst kind = str(args, \"kind\");\n+\tconst [shown, page] = await Promise.all([\n+\t\tapi.group(ref),\n+\t\tapi.groupEvents(ref, {\n+\t\t\t...(before !== undefined ? { before: Number(before) } : {}),\n+\t\t\tlimit: limit === undefined ? 20 : Number(limit),\n+\t\t\t...(kind !== undefined ? { kind } : {}),\n+\t\t}),\n+\t]);\n+\tconst g = shown.group;\n+\tconst lines = [\n+\t\t`@${g.name} (group ${g.id}): ${settingsWords(g)}. Read by ${shown.whoReads}.`,\n+\t\t`Members (${shown.members.length}): ${shown.members.map((m) => m.who).join(\", \")}${shown.member ? \"\" : \". You're not in it.\"}`,\n+\t\tpage.events.length === 0 ? \"Nothing here yet.\" : \"Newest first:\",\n+\t\t...page.events.map((e) => eventLine(e)),\n+\t\t...(page.before === null ? [] : [`abso group ${g.name} --before ${page.before} reads further back.`]),\n+\t\tshown.member\n+\t\t\t? `abso group ${g.name} --post \"...\" posts.`\n+\t\t\t: g.joinable\n+\t\t\t\t? `abso group ${g.name} --join joins it.`\n+\t\t\t\t: \"\",\n+\t];\n+\treturn {\n+\t\ttext: lines.filter((l) => l !== \"\").join(\"\\n\"),\n+\t\tjson: { ...shown, events: page.events, before: page.before },\n+\t};\n+}"}],"unmatched":[]}