- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/worker/project/announce.ts?format=json
122 linesFirst import 83% · groups 16% · infra-split 2%Last change groups in version 277, 3 versions changed it
1// Tell other agents something, as news. Writes go through AnnounceHost; today
2// the host is Project.
4import { agentAnnouncement, type NewsItem, personAnnouncement } from "./news.ts";
5import { ProjectError } from "./project-error.ts";
6import { checkPerson } from "./project-helpers.ts";
7import type { AgentState, Entry } from "./project-types.ts";
9/** What announcing needs from a project. */
10export interface AnnounceHost {
11 readonly agents: ReadonlyMap<string, AgentState>;
12 announced(): number;
13 setAnnounced(n: number): void;
14 agent(name: string, entries: Entry[]): AgentState;
15 queueNews(agent: AgentState, items: readonly NewsItem[], entries: Entry[]): void;
16 apply(entry: Entry): void;
17 flush(entries: Entry[]): void;
18 needInit(): void;
19}
21/**
22 * Tell other agents something, as news: "Announcement from <agent>:
23 * <text>". It reaches each one with its next edit, record or news call.
24 * `to` names agents; without it, every other agent the project has now.
25 */
26export function announce(
27 host: AnnounceHost,
28 agentName: string,
29 text: string,
30 to: readonly string[] | undefined,
31 _time: number,
32 how?: string,
33): { id: number; to: string[] } {
34 if (typeof text !== "string" || text.trim() === "") throw new ProjectError("bad-request", "announce needs {text}");
35 if (text.length > 4000) throw new ProjectError("bad-request", "an announcement is at most 4000 characters");
36 if (to !== undefined && (!Array.isArray(to) || !to.every((a) => typeof a === "string"))) {
37 throw new ProjectError("bad-request", "to must be a list of agent names");
38 }
39 for (const name of to ?? []) {
40 if (!host.agents.has(name)) throw new ProjectError("bad-request", `there is no agent ${JSON.stringify(name)}`);
41 }
42 const entries: Entry[] = [];
43 const sender = host.agent(agentName, entries);
44 const targets = [...new Set(to ?? [...host.agents.keys()].filter((a) => a !== sender.name))].toSorted();
45 const id = host.announced() + 1;
46 // "(as claimed)": every agent holds the same token, so the name is only
47 // the one the sender's route gave (#31). A staging key says
48 // which key (abso issue 103); claimMark allows only that wording.
49 const sentence = agentAnnouncement(sender.name, claimMark(how), text);
50 for (const name of targets)
51 host.queueNews(host.agents.get(name)!, [{ key: `announce ${id}`, text: sentence }], entries);
52 // Replay knows the number only from the news written, so it's taken only when some was.
53 if (entries.some((e) => e.type === "news")) host.setAnnounced(id);
54 host.flush(entries);
55 return { id, to: targets };
56}
58/**
59 * A signed-in person's announcement from the website (#41). The Worker has
60 * checked who sent it; the engine records it on each recipient's news with
61 * `from`, and never makes the person an agent. The text is JSON-quoted, so
62 * a line break in it can't pass for another item.
63 */
64export function announcePerson(
65 host: AnnounceHost,
66 person: { id: unknown; name: unknown },
67 text: string,
68 to: readonly string[] | undefined,
69 time: number,
70): { id: number; to: string[] } {
71 host.needInit();
72 const from = checkPerson(person);
73 const { id, name } = from.person;
74 if (typeof text !== "string" || text.trim() === "") throw new ProjectError("bad-request", "announce needs {text}");
75 if (text.length > 4000) throw new ProjectError("bad-request", "an announcement is at most 4000 characters");
76 if (to !== undefined && (!Array.isArray(to) || !to.every((a) => typeof a === "string")))
77 throw new ProjectError("bad-request", "to must be a list of agent names");
78 for (const a of to ?? [])
79 if (!host.agents.has(a)) throw new ProjectError("bad-request", `there is no agent ${JSON.stringify(a)}`);
80 const targets = [...new Set(to ?? [...host.agents.keys()])].toSorted();
81 const n = host.announced() + 1;
82 const sentence = personAnnouncement(name, id, text);
83 const entries: Entry[] = [];
84 for (const target of targets) {
85 const agent = host.agents.get(target)!;
86 const key = `announce ${n}`;
87 if (agent.told.has(key)) continue;
88 const entry: Entry = { type: "news", agent: target, pairs: [key], text: [sentence], time, from };
89 entries.push(entry);
90 host.apply(entry);
91 }
92 if (entries.length > 0) host.setAnnounced(n);
93 host.flush(entries);
94 return { id: n, to: targets };
95}
97/** The announcement's parenthetical. Only these wordings, so a caller can't write the sentence. */
98export function claimMark(how: string | undefined): "as claimed" | "shared token" | `key ${string}` {
99 if (how === "shared token") return how;
100 if (typeof how === "string" && /^key k_[0-9a-f]{8}(?:, box [A-Za-z0-9._-]{1,64})?$/.test(how))
101 return how as `key ${string}`;
102 return "as claimed";
103}
105/**
106 * A seat's earlier chat hears that another chat just used the seat (seats notice when someone else picks them up):
107 * "<agent> was just used from another chat (<place>)". The Worker says where, from the caller it checked; the wording
108 * is this one only. `key` keeps it to once per pair of chats per window: the same key again adds nothing.
109 */
110export function seatNotice(host: AnnounceHost, agentName: string, place: string, key: string): boolean {
111 const agent = host.agents.get(agentName);
112 if (agent === undefined) return false;
113 const where = place
114 .replace(/[\u0000-\u001f\u007f]+/g, " ")
115 .trim()
116 .slice(0, 120);
117 if (where === "" || !/^seat [\w:.-]{1,200}$/.test(key) || agent.told.has(key)) return false;
118 const entries: Entry[] = [];
119 host.queueNews(agent, [{ key, text: `${agent.name} was just used from another chat (${where})` }], entries);
120 host.flush(entries);
121 return entries.length > 0;
122}