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

Versions 12 to 318

Compare

+186 −0 in 1 place
new file, 186 lines
1+// Organizations from the CLI: abso orgs and abso org <name>. They read as the
2+// person who approved this box's sign-in (abso login). abso org new makes one, and
3+// abso init --org makes a project in one, from a box approved for all its
4+// person's projects (abso 663). Changing an org (invite, role, remove, leave,
5+// settings) stays on the website: a box sits where agents run, so the server takes
6+// no other org change from it.
7+
8+import { type Parsed, str, UsageError } from "./args.ts";
9+import { type Answer, boxCreateOrg, boxGet, originOf, readBox } from "./box.ts";
10+import type { Output } from "./commands.ts";
12+/** The website for an API server: api.<site> serves <site>; any other server serves its own pages. */
13+export function siteOf(server: string): string {
14+ try {
15+ const url = new URL(server);
16+ if (url.hostname.startsWith("api.")) url.hostname = url.hostname.slice("api.".length);
17+ return url.origin;
18+ } catch {
19+ return server;
20+ }
21+}
23+/** The flags that change an org, in the order Design wrote them; each names where the website does it. */
24+const CHANGES: readonly { flag: string; tab: string }[] = [
25+ { flag: "invite", tab: "People" },
26+ { flag: "role", tab: "People" },
27+ { flag: "remove", tab: "People" },
28+ { flag: "leave", tab: "People" },
29+ { flag: "members", tab: "Settings" },
30+ { flag: "projects-by", tab: "Settings" },
31+];
33+const WEBSITE_ONLY =
34+ "Organizations are changed on the website; from the CLI they can be read, and made with abso org new, as the person who approved this box's sign-in";
36+/** Where the box's sign-in is read from and how the server is reached; tests pass their own. */
37+export interface OrgIo {
38+ readonly home?: string;
39+ readonly fetch?: typeof fetch;
40+}
42+async function read(server: string, path: string, io: OrgIo): Promise<Answer> {
43+ const record = io.home === undefined ? readBox(server) : readBox(server, io.home);
44+ if (!record)
45+ throw new UsageError(
46+ `this box isn't signed in to ${originOf(server)}. abso login signs it in; org reads answer as the person who approves it`,
47+ );
48+ return boxGet(record, path, io.fetch);
49+}
51+/** What a refusal from an org read means, in words. */
52+function refused(a: Answer, what: string, server: string): UsageError {
53+ const said = typeof a.body.error === "string" ? a.body.error : `status ${a.status}`;
54+ if (a.status === 401)
55+ return new UsageError(`${originOf(server)} didn't take this box's sign-in (${said}). abso login renews it`);
56+ if (a.status === 404 && /aren't on here/.test(said)) return new UsageError(`${originOf(server)}: ${said}`);
57+ if (a.status === 404) return new UsageError(`${what}: ${said}`);
58+ return new UsageError(`${originOf(server)} refused ${what} (${a.status}): ${said}`);
59+}
61+const when = (iso: unknown) => (typeof iso === "string" ? `${iso.slice(0, 16).replace("T", " ")} UTC` : "?");
63+interface OrgRow {
64+ name: string;
65+ displayName: string;
66+ role: string;
67+}
68+interface Invitation {
69+ id: string;
70+ org: string;
71+ role: string;
72+ expiresAt: string;
73+}
75+/** abso orgs: your organizations, your role in each, and invitations waiting for you. */
76+export async function runOrgs(server: string, io: OrgIo = {}): Promise<Output> {
77+ const a = await read(server, "/orgs", io);
78+ if (a.status !== 200) throw refused(a, "the list of your organizations", server);
79+ const orgs = (a.body.orgs ?? []) as OrgRow[];
80+ const invitations = (a.body.invitations ?? []) as Invitation[];
81+ const lines =
82+ orgs.length === 0
83+ ? ["You're in no organizations."]
84+ : [
85+ `Your organizations (${orgs.length}):`,
86+ ...orgs.map(
87+ (o) => ` ${o.name}${o.displayName && o.displayName !== o.name ? ` (${o.displayName})` : ""}: ${o.role}`,
88+ ),
89+ ];
90+ if (invitations.length > 0)
91+ lines.push(
92+ "Invited to:",
93+ ...invitations.map((i) => ` ${i.org} as ${i.role}, until ${when(i.expiresAt)}`),
94+ `Accept them on the website, signed in: ${siteOf(server)}.`,
95+ );
96+ return { text: lines.join("\n"), json: { orgs, invitations } };
97+}
99+interface Member {
100+ id: string;
101+ role: string;
102+ name: string | null;
103+ raised?: { project: string; role: string }[];
104+}
105+interface Invite {
106+ id: string;
107+ role: string;
108+ kind: "link" | "person";
109+ expiresAt: string;
110+}
112+/** abso org <name>, abso org new <name>, and the change flags, which say where the website does each. */
113+export async function runOrg(args: Parsed, server: string, io: OrgIo = {}): Promise<Output> {
114+ const [first, second, ...more] = args.positionals;
115+ const asked = CHANGES.filter((c) => args.flags.has(c.flag));
116+ if (first === "new") {
117+ if (second === undefined || more.length > 0) throw new UsageError('abso org new <name> [--name "<shown name>"]');
118+ if (asked.length > 0) throw new UsageError("abso org new takes --name only");
119+ const record = io.home === undefined ? readBox(server) : readBox(server, io.home);
120+ if (!record)
121+ throw new UsageError(
122+ `this box isn't signed in to ${originOf(server)}. abso login signs it in; approve all your projects to make organizations from here`,
123+ );
124+ const made = await boxCreateOrg(record, second, str(args, "name"), io.fetch);
125+ const name = typeof made.name === "string" ? made.name : second;
126+ return {
127+ text: `Made the organization ${name}; you own it. Its page: ${siteOf(server)}/${name}. abso init --org ${name} --project <name> makes a project in it.`,
128+ json: { org: made },
129+ };
130+ }
131+ if (first === undefined || second !== undefined)
132+ throw new UsageError("abso org <name> [one change], or abso org new <name>");
133+ if (args.flags.has("as") && !args.flags.has("invite")) throw new UsageError("--as goes with --invite");
134+ if (asked.length > 1)
135+ throw new UsageError(`one change at a time: --${asked.map((c) => c.flag).join(" and --")} were given together`);
136+ if (asked.length === 1)
137+ throw new UsageError(
138+ `${WEBSITE_ONLY}. --${asked[0]!.flag} is on ${first}'s ${asked[0]!.tab} tab: ${siteOf(server)}/${first}.`,
139+ );
141+ const a = await read(server, `/orgs/${encodeURIComponent(first)}`, io);
142+ if (a.status !== 200) throw refused(a, first, server);
143+ const org = a.body as {
144+ name: string;
145+ displayName: string;
146+ description: string | null;
147+ role?: string;
148+ membersCan?: string;
149+ whoMakesProjects?: string;
150+ members?: Member[];
151+ projects?: string[];
152+ invites?: Invite[];
153+ };
154+ const lines = [`${org.name}${org.displayName && org.displayName !== org.name ? ` (${org.displayName})` : ""}`];
155+ if (org.description) lines.push(` ${org.description.replace(/\r?\n/g, "\n ")}`);
156+ if (org.role === undefined) {
157+ lines.push("You aren't a member, so its people and settings aren't shown.");
158+ return { text: lines.join("\n"), json: { org } };
159+ }
160+ lines.push(
161+ `You're ${org.role === "owner" ? "an owner" : "a member"}. Members can ${org.membersCan ?? "?"} every project; ${org.whoMakesProjects === "owners" ? "only owners make" : "members make"} projects.`,
162+ );
163+ const projects = org.projects ?? [];
164+ lines.push(projects.length === 0 ? "Projects: none yet." : `Projects (${projects.length}): ${projects.join(", ")}`);
165+ const members = org.members ?? [];
166+ lines.push(`People (${members.length}):`);
167+ for (const m of members) {
168+ const raised = (m.raised ?? []).map((r) => `${r.role} in ${r.project}`);
169+ lines.push(
170+ ` ${m.name ?? "(no name yet)"}: ${m.role}${raised.length > 0 ? `, raised to ${raised.join(", ")}` : ""}`,
171+ );
172+ }
173+ if (org.invites !== undefined && org.invites.length > 0) {
174+ lines.push("Open invites:");
175+ for (const i of org.invites)
176+ lines.push(` ${i.kind === "link" ? "a link" : "a person, by name"}, as ${i.role}, until ${when(i.expiresAt)}`);
177+ }
178+ return { text: lines.join("\n"), json: { org } };
179+}
181+/** abso init --org with no box signed in to the server: an org's project is made as the person who approved one (abso 663). */
182+export function initInOrg(org: string, server: string): never {
183+ throw new UsageError(
184+ `this box isn't signed in to ${originOf(server)}, and a project in ${org} is made as a signed-in person. abso login, approving all your projects, then abso init --org ${org} again; or New project at ${siteOf(server)}.`,
185+ );
186+}
MCP
compare on this site's /mcp
CLI
abso diff 12 318 client/src/orgs.ts --json
JSON
/christopher/abso-core/compare/12..318?file=client%2Fsrc%2Forgs.ts&format=json