Skip to content

abso-core

Public

abso is version control for agents: the client, the engine and a server you can run on your own Cloudflare account.

Check passed
MCP
code on this site's /mcp
CLI
none: the JSON below is the answer
JSON
/christopher/abso-core/files/client/src/group-commands.ts?format=json
204 linesgroups 100%Last change groups in version 256, 1 version changed it
1// Groups: a project's named sets of agents and people, each with one shared history. abso groups lists them;
2// abso group reads one, posts in it, and joins, leaves or changes it. The Issues Worker keeps them. Other agents'
3// and people's words are quoted as JSON, so none can print as another line.
4
5import { bool, type Parsed, str, UsageError } from "./args.ts";
6import { type Env, type Output, plural } from "./commands.ts";
7import { openIssues } from "./issues.ts";
8import type { Group, GroupEvent } from "./issues-wire.ts";
9import { shortWhen } from "./times.ts";
11const q = (s: string): string => JSON.stringify(s);
13/** One event as a line: its number, time, who, and what happened. */
14export function eventLine(e: GroupEvent, now = Date.now()): string {
15 const head = ` ${String(e.n).padStart(4)} ${shortWhen(e.at, now)} ${e.by}`;
16 if (e.kind === "message") {
17 const re = e.replyTo === undefined ? "" : ` (re ${e.replyTo})`;
18 const quiet = e.news !== undefined && e.news.length === 0 ? " (history only, no news)" : "";
19 return `${head}${re}: ${q(e.text ?? "")}${quiet}`;
20 }
21 if (e.kind === "joined") return `${head} joined`;
22 if (e.kind === "left") return `${head} left`;
23 if (e.kind === "added") return `${head} added ${e.who}`;
24 if (e.kind === "removed") return `${head} removed ${e.who}${e.by === "abso" ? " (no longer in the project)" : ""}`;
25 if (e.kind === "created") return `${head} made the group`;
26 const changes = Object.entries(e.detail ?? {})
27 .map(([k, v]) => `${k} ${JSON.stringify((v as { to: unknown }).to)}`)
28 .join(", ");
29 return `${head} set ${changes}`;
32function settingsWords(g: Group): string {
33 return `${g.private ? "private" : "public"}, ${g.joinable ? "anyone in the project may join" : "members are added by its maker or a project owner"}`;
36/** abso groups: the project's groups this agent can see. */
37export async function runGroups(args: Parsed, env: Env): Promise<Output> {
38 if (args.positionals.length > 0) throw new UsageError("abso groups takes nothing; abso group <name> reads one");
39 const { api } = openIssues(env);
40 const { groups } = await api.groupList();
41 if (groups.length === 0) return { text: "No groups yet. abso group new <name> makes one.", json: { groups } };
42 return {
43 text: [
44 `${plural(groups.length, "group")}:`,
45 ...groups.map(
46 (g) =>
47 ` @${g.name.padEnd(20)} ${plural(g.members, "member").padEnd(11)} ${g.private ? "private" : "public "}${g.joinable ? "" : ", added only"}${g.member ? " (you're in it)" : ""}`,
48 ),
49 "abso group <name> reads one; abso group <name> --join joins it.",
50 ].join("\n"),
51 json: { groups },
52 };
55const ACTIONS = ["post", "join", "leave", "add", "remove", "rename", "private", "joinable"] as const;
57function onOff(word: string, flag: string): boolean {
58 if (word === "on") return true;
59 if (word === "off") return false;
60 throw new UsageError(`--${flag} is on or off`);
63function names(word: string): string[] {
64 return word
65 .split(",")
66 .map((n) => n.trim())
67 .filter((n) => n !== "");
70/**
71 * abso group <name> [--post "..." [--reply N] [--news all|none|a,b] [--except a,b]] [--join | --leave | --add x |
72 * --remove x | --rename new | --private on|off | --joinable on|off] [--before N] [--limit N] [--kind message|member|settings]
73 * abso group new <name> [--private] [--closed] [--add a,b]
74 */
75export async function runGroup(args: Parsed, env: Env): Promise<Output> {
76 const [first, second, ...extra] = args.positionals;
77 if (first === undefined)
78 throw new UsageError("abso group <name> reads a group; abso group new <name> makes one; abso groups lists them");
79 const { api, config } = openIssues(env);
80 const me = config.agent;
81 if (first === "new") {
82 if (second === undefined || extra.length > 0)
83 throw new UsageError("abso group new <name> [--private] [--closed] [--add a,b]");
84 const add = str(args, "add");
85 const r = await api.groupWrite(undefined, undefined, {
86 name: second,
87 private: str(args, "private") === "" || str(args, "private") === "on",
88 joinable: !bool(args, "closed"),
89 ...(add ? { members: names(add) } : {}),
90 });
91 const members = (r.event?.detail?.members as string[] | undefined) ?? [me];
92 return {
93 text: `Made @${r.group.name} (${settingsWords(r.group)}), with ${members.join(", ")}. abso group ${r.group.name} --post "..." talks in it.`,
94 json: { ...r },
95 };
96 }
97 if (second !== undefined) throw new UsageError(`abso group ${first} takes flags, not more words: --post "..." posts`);
98 const ref = first.replace(/^@/, "");
99 const given = ACTIONS.filter((a) => args.flags.has(a));
100 if (given.length > 1) throw new UsageError(`one change at a time: ${given.map((g) => `--${g}`).join(" and ")}`);
101 const action = given[0];
102 if (action === undefined) {
103 for (const f of ["reply", "news", "except"]) if (args.flags.has(f)) throw new UsageError(`--${f} goes with --post`);
104 return readGroup(ref, args, env);
105 }
106 if (action !== "post")
107 for (const f of ["reply", "news", "except", "before", "limit", "kind"])
108 if (args.flags.has(f)) throw new UsageError(`--${f} doesn't go with --${action}`);
109 if (action === "post") {
110 const text = str(args, "post");
111 if (text === undefined || text.trim() === "") throw new UsageError('--post "<words>" says what to post');
112 const reply = str(args, "reply");
113 if (reply !== undefined && !/^\d+$/.test(reply)) throw new UsageError("--reply is a message's number in the group");
114 const news = str(args, "news");
115 const except = str(args, "except");
116 if (news !== undefined && except !== undefined) throw new UsageError("--news or --except, not both");
117 const audience =
118 except !== undefined
119 ? { except: names(except) }
120 : news === undefined || news === "all"
121 ? "all"
122 : news === "none"
123 ? "none"
124 : names(news);
125 const r = await api.groupWrite(ref, "post", {
126 text,
127 ...(reply !== undefined ? { replyTo: Number(reply) } : {}),
128 news: audience,
129 });
130 const heard = r.event?.news ?? [];
131 const again = r.replayed ? " (a retry: this is the post already made)" : "";
132 return {
133 text: `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.`}`,
134 json: { ...r },
135 };
136 }
137 if (action === "join" || action === "leave") {
138 const r = await api.groupWrite(ref, action, {});
139 const what =
140 r.event === undefined
141 ? action === "join"
142 ? `You're already in @${r.group.name}.`
143 : `You weren't in @${r.group.name}.`
144 : action === "join"
145 ? `Joined @${r.group.name}. abso group ${r.group.name} reads its history.`
146 : `Left @${r.group.name}. What you wrote stays in its history.`;
147 return { text: what, json: { ...r } };
148 }
149 if (action === "add" || action === "remove") {
150 const who = str(args, action);
151 if (who === undefined || who === "") throw new UsageError(`--${action} <agent or person:id>`);
152 const r = await api.groupWrite(ref, action, { who });
153 const what =
154 r.event === undefined
155 ? `${who} ${action === "add" ? "was already" : "wasn't"} in @${r.group.name}.`
156 : `${action === "add" ? "Added" : "Removed"} ${who} ${action === "add" ? "to" : "from"} @${r.group.name}.`;
157 return { text: what, json: { ...r } };
158 }
159 const body =
160 action === "rename" ? { name: str(args, "rename") } : { [action]: onOff(str(args, action) ?? "", action) };
161 const r = await api.groupWrite(ref, "settings", body);
162 return {
163 text:
164 r.event === undefined
165 ? `@${r.group.name} is already like that.`
166 : `@${r.group.name} is now ${settingsWords(r.group)}.`,
167 json: { ...r },
168 };
171async function readGroup(ref: string, args: Parsed, env: Env): Promise<Output> {
172 const { api } = openIssues(env);
173 const before = str(args, "before");
174 const limit = str(args, "limit");
175 if (before !== undefined && !/^\d+$/.test(before)) throw new UsageError("--before is an event number");
176 if (limit !== undefined && !(/^\d+$/.test(limit) && Number(limit) >= 1 && Number(limit) <= 100))
177 throw new UsageError("--limit is a number from 1 to 100");
178 const kind = str(args, "kind");
179 const [shown, page] = await Promise.all([
180 api.group(ref),
181 api.groupEvents(ref, {
182 ...(before !== undefined ? { before: Number(before) } : {}),
183 limit: limit === undefined ? 20 : Number(limit),
184 ...(kind !== undefined ? { kind } : {}),
185 }),
186 ]);
187 const g = shown.group;
188 const lines = [
189 `@${g.name} (group ${g.id}): ${settingsWords(g)}. Read by ${shown.whoReads}.`,
190 `Members (${shown.members.length}): ${shown.members.map((m) => m.who).join(", ")}${shown.member ? "" : ". You're not in it."}`,
191 page.events.length === 0 ? "Nothing here yet." : "Newest first:",
192 ...page.events.map((e) => eventLine(e)),
193 ...(page.before === null ? [] : [`abso group ${g.name} --before ${page.before} reads further back.`]),
194 shown.member
195 ? `abso group ${g.name} --post "..." posts.`
196 : g.joinable
197 ? `abso group ${g.name} --join joins it.`
198 : "",
199 ];
200 return {
201 text: lines.filter((l) => l !== "").join("\n"),
202 json: { ...shown, events: page.events, before: page.before },
203 };