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/announce.ts?format=json
149 linesFirst import 54% · box-grants 41% · pinch 3% · rename 2% · infra-split 1%Last change box-grants in version 291, 8 versions changed it
1// abso announce and abso news: a message to the project's other agents,
2// delivered once as news on each one's next edit (or the mod's next poll),
3// and the news waiting for this agent.
4
5import { writeFileSync } from "node:fs";
6import { join } from "node:path";
7import { newsLines } from "./api.ts";
8import { bool, type Parsed, str, UsageError } from "./args.ts";
9import { type Env, type Output, open, plural } from "./commands.ts";
10import { logNews } from "./folder.ts";
11import { follow, monitorArgs, type Socket, STREAM_MARK, streamMarkUntil, streamText } from "./news-stream.ts";
12import { stateDir } from "./state-dir.ts";
13import { newsWithTime } from "./times.ts";
15export async function runAnnounce(args: Parsed, env: Env): Promise<Output> {
16 const text = args.positionals.join(" ").trim();
17 if (text === "") throw new UsageError('say what to announce: abso announce [--to a,b] "text"');
18 const rawTo = str(args, "to");
19 const to =
20 rawTo === undefined
21 ? undefined
22 : rawTo
23 .split(",")
24 .map((a) => a.trim())
25 .filter((a) => a !== "");
26 if (to !== undefined && to.length === 0) throw new UsageError("--to names the agents, separated by commas");
27 const { api } = open(env);
28 const answer = await api.announce(text, to);
29 const who = answer.to.length === 0 ? "nobody (no other agents yet)" : answer.to.join(", ");
30 return {
31 text: `Announced to ${who}. Each gets it once, as news on their next edit, within about 5 minutes in a working chat where the abso mod runs, or at once where it holds its news stream.`,
32 json: { id: answer.id, to: answer.to },
33 };
36/** abso start: say what you are starting on, before any edit lands (#37, #13). */
37export async function runStart(args: Parsed, env: Env): Promise<Output> {
38 const { api } = open(env);
39 if (bool(args, "done")) {
40 await api.starting(null);
41 return { text: "Starting note taken back.", json: { starting: null, overlaps: [] } };
42 }
43 const text = args.positionals[0]?.trim() ?? "";
44 if (text === "") throw new UsageError('say what you are starting on: abso start "<what>" [files...]');
45 const files = args.positionals.slice(1);
46 // --issue N (#54): the issue this work is for; publish --done closes it.
47 const issueFlag = args.flags.get("issue");
48 const issue = typeof issueFlag === "string" ? Number(issueFlag) : undefined;
49 if (issue !== undefined && (!Number.isInteger(issue) || issue < 1))
50 throw new UsageError('--issue takes an issue number: abso start "<what>" --issue 12');
51 const answer = await api.starting(text, files, issue !== undefined ? { issue } : {});
52 const lines = [
53 `Starting on "${text}"${files.length > 0 ? `, naming ${files.join(", ")}` : ""}${issue !== undefined ? ` for issue ${issue}` : ""}. abso where shows it to everyone until you push (or abso start --done).` +
54 (issue !== undefined ? ` abso push --done closes issue ${issue}.` : ""),
55 ];
56 if (answer.overlaps.length > 0) {
57 lines.push("Already there:");
58 for (const o of answer.overlaps) {
59 const what = [
60 o.changes ? plural(o.changes, "unpushed change") : "",
61 o.starting ? `starting on "${o.starting}"` : "",
62 ]
63 .filter((w) => w !== "")
64 .join(", ");
65 lines.push(` ${o.file}: ${o.agent}${what === "" ? "" : ` (${what})`}`);
66 }
67 // The sentence people act on names who, and their note (#37: without it, an agent had to run abso where to learn it).
68 const agents = [...new Set(answer.overlaps.map((o) => o.agent))];
69 const notes = agents.flatMap((a) => {
70 const note = answer.overlaps.find((o) => o.agent === a && o.starting)?.starting;
71 return note === undefined ? [] : [`${a}'s "${note}"`];
72 });
73 lines.push(
74 `${agents.join(" and ")} ${agents.length === 1 ? "hears" : "hear"} of your note on their next edit. If it is the same work${notes.length > 0 ? ` as ${notes.join(" or ")}` : ""}, settle who does what first: abso announce --to ${agents.join(",")} "..." reaches ${agents.length === 1 ? "them" : "each of them"}.`,
75 );
76 }
77 return { text: lines.join("\n"), json: { ...answer } };
80/** Says this folder's seat holds its news stream until `until`, so the abso-hooks mod's poll stands down. Best effort. */
81function markStream(root: string, until: number): void {
82 try {
83 writeFileSync(join(stateDir(root), STREAM_MARK), JSON.stringify({ until }));
84 } catch {
85 // The mod keeps polling; nothing is lost.
86 }
89export async function runNews(args: Parsed, env: Env): Promise<Output> {
90 const { root, config, api } = open(env);
91 if (bool(args, "stream") && bool(args, "follow"))
92 throw new UsageError("abso news takes --stream or --follow, not both");
93 if (bool(args, "stream")) {
94 const ticket = await api.newsTicket();
95 markStream(root, streamMarkUntil(ticket, Date.now()));
96 return {
97 text: streamText(ticket, config.project, config.agent),
98 json: { monitor: monitorArgs(ticket, config.project, config.agent), expiresAt: ticket.expiresAt },
99 };
100 }
101 if (bool(args, "follow")) {
102 let stop = false;
103 const onInt = () => {
104 stop = true;
105 };
106 process.once("SIGINT", onInt);
107 try {
108 const ended = await follow({
109 mint: () => api.newsTicket(),
110 // A frame is a wake: read the news, which marks it heard, and print that (news-stream.ts).
111 take: async () => newsLines((await api.news()).news),
112 connect: (url, protocols) => new WebSocket(url, [...protocols]) as unknown as Socket,
113 write: (line) => {
114 logNews(root, [line]);
115 env.write(`${line.replace(/\r?\n/g, " ")}\n`);
116 },
117 held: (until) => markStream(root, until),
118 note: (line) =>
119 env.write(`${line}
120`),
121 sleep: (ms) => new Promise((ok) => setTimeout(ok, ms)),
122 now: () => Date.now(),
123 every: (ms, fn) => {
124 const t = setInterval(fn, ms);
125 return () => clearInterval(t);
126 },
127 stopped: () => stop,
128 });
129 return ended.ended === "refused"
130 ? { text: `The news stream refused this seat: ${ended.reason}`, json: ended, exit: 2 }
131 : { text: "", json: ended };
132 } finally {
133 process.off("SIGINT", onInt);
134 }
135 }
136 const answer = await api.news();
137 const news = newsLines(answer.news);
138 logNews(root, news);
139 if (news.length === 0) return { text: "No news.", json: { news } };
140 const at = new Map((answer.items ?? []).map((i) => [i.text, i.at]));
141 const items = news.map((text) => ({ text, at: at.get(text) ?? null }));
142 return {
143 text: [
144 `${plural(news.length, "news item")}:`,
145 ...items.map((i) => ` ${newsWithTime(i.text, i.at).replace(/\r?\n/g, "\n ")}`),
146 ].join("\n"),
147 json: { news, items, delivered: Date.now() },
148 };