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