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 148 to 318

Compare

+143 −0 in 1 place
new file, 143 lines
1+// The --server flag (abso-core issue 10). Every command that talks to a server takes --server <url>, parsed and
2+// checked here the same way for all of them:
3+// - A command that works on a joined folder ("folder") talks only to that folder's server. --server there must
4+// name that same server; another one is refused, never swapped in, so the folder's work and key never go to
5+// a server it wasn't joined on. Outside a joined folder it is refused too, with how to join one there.
6+// - A command that picks a server for itself ("chosen": join, init, login, whoami and the rest about this box)
7+// uses the one --server names, as given.
8+// - A command that works on this machine only ("local") takes no --server, and says so.
9+// A value that isn't an http or https URL (a user name, a query or a fragment included) is refused before any
10+// call, as is an empty one.
11+//
12+// When nothing names a server (abso-core issue 9), every command picks it in one order (chooseServer):
13+// 1. --server <url>;
14+// 2. the server of the joined folder the command runs in (its .abso/config.json);
15+// 3. ABSO_SERVER (or the older AVCS_SERVER);
16+// 4. prod, https://api.abso.dev.
17+// Which servers this box is signed in to plays no part: signed in to staging and prod both, or to staging
18+// alone, a command with nothing named goes to prod, and staging is a deliberate --server, folder or ABSO_SERVER.
20+import { readFileSync } from "node:fs";
21+import { type Parsed, UsageError } from "./args.ts";
22+import { configPath, findRoot } from "./config.ts";
23+import { serverBase, TOKEN_SERVERS } from "./core.ts";
24+import { fromEnv } from "./env-names.ts";
26+/** How a command uses a server: its joined folder's, one it picks, or none. */
27+export type ServerUse = "folder" | "chosen" | "local";
29+/** The server a command goes to when nothing names one: prod. */
30+export const PROD_SERVER = TOKEN_SERVERS[0]!;
32+/** The order of precedence, in the words help uses. */
33+export const SERVER_ORDER = `--server <url>, else the server of the abso folder you're in, else ABSO_SERVER, else prod (${PROD_SERVER})`;
35+/**
36+ * The server a command goes to: `flag` (checked by serverFlag), else the joined folder's, else ABSO_SERVER
37+ * (checked here), else prod. An explicit choice is returned as given, never swapped for another.
38+ */
39+export function chooseServer(
40+ flag: string | undefined,
41+ cwd: string,
42+ env: Readonly<Record<string, string | undefined>>,
43+): string {
44+ if (flag !== undefined) return flag;
45+ const folder = folderServer(cwd);
46+ if (folder !== undefined) return folder;
47+ const named = fromEnv(env, "SERVER");
48+ if (named !== undefined) return checkedServer(named, env.ABSO_SERVER ? "ABSO_SERVER" : "AVCS_SERVER");
49+ return PROD_SERVER;
50+}
52+/** The commands that pick a server for themselves rather than use a joined folder's. */
53+const CHOSEN: ReadonlySet<string> = new Set([
54+ "init",
55+ "join",
56+ "key",
57+ "login",
58+ "box",
59+ "whoami",
60+ "doctor",
61+ "logout",
62+ "orgs",
63+ "org",
64+]);
66+/** The commands that never talk to a server. */
67+const LOCAL: ReadonlySet<string> = new Set(["skill", "check-ignore", "pause", "resume", "fix-eol"]);
69+/** How `command` uses a server. Anything not named above works on a joined folder. */
70+export function serverUse(command: string): ServerUse {
71+ return CHOSEN.has(command) ? "chosen" : LOCAL.has(command) ? "local" : "folder";
72+}
74+const EXAMPLE = "https://api.abso.dev";
76+/** `value` as a server, or a refusal naming where it came from (--server, ABSO_SERVER). */
77+export function checkedServer(value: string, from: string): string {
78+ const v = value.trim();
79+ if (v === "") throw new UsageError(`${from} needs a server's URL, like ${EXAMPLE}`);
80+ if (serverBase(v) === undefined)
81+ throw new UsageError(
82+ `${from} takes an http or https URL with no user name, query or fragment, like ${EXAMPLE} (got ${JSON.stringify(value)})`,
83+ );
84+ return v;
85+}
87+/** The server --server names, checked, or undefined when it isn't given. */
88+export function serverFlag(args: Parsed): string | undefined {
89+ const v = args.flags.get("server");
90+ return typeof v === "string" ? checkedServer(v, "--server") : undefined;
91+}
93+/** Whether two servers are the same one: the same scheme, host, port and path, ignoring case and a trailing slash. */
94+export function sameServer(a: string, b: string): boolean {
95+ return (serverBase(a) ?? a.trim()) === (serverBase(b) ?? b.trim());
96+}
98+/** The server in the .abso/config.json of the folder `cwd` is in, or undefined outside one (or when it can't be read). */
99+export function folderServer(cwd: string): string | undefined {
100+ const root = findRoot(cwd);
101+ if (root === undefined) return undefined;
102+ try {
103+ const server = (JSON.parse(readFileSync(configPath(root), "utf8")) as { server?: unknown }).server;
104+ return typeof server === "string" && server !== "" ? server : undefined;
105+ } catch {
106+ return undefined;
107+ }
108+}
110+/**
111+ * For a command that works on a joined folder: refuses a --server that isn't the folder's own server, and one
112+ * given outside a joined folder. Nothing is sent before this.
113+ */
114+export function checkFolderServer(command: string, flag: string, cwd: string): void {
115+ const own = folderServer(cwd);
116+ if (own === undefined)
117+ throw new UsageError(
118+ `abso ${command} works on a joined folder, and ${cwd} isn't in one. To work with ${flag}: abso join <project> --server ${flag}, or abso init --server ${flag} for a new project.`,
119+ );
120+ if (!sameServer(own, flag))
121+ throw new UsageError(
122+ `this folder is joined on ${own}, not ${flag}, so abso ${command} won't send its work or key there. Run it in a folder joined on ${flag} (abso join <project> --server ${flag}).`,
123+ );
124+}
126+/** What help says about --server for a command, the same words for every command of a kind. */
127+export function serverHelp(command: string): string | undefined {
128+ switch (serverUse(command)) {
129+ case "local":
130+ return undefined;
131+ case "folder":
132+ return "--server <url>: this folder's own server, which it uses anyway. Another server is refused, never swapped in, so the folder's work and key go only to the server it was joined on.";
133+ case "chosen":
134+ return command === "key"
135+ ? "--server <url>: the server the key is for; without it, this folder's. It is used as given, never swapped for another."
136+ : `--server <url>: the server to use, as given, never swapped for another. The order: ${SERVER_ORDER}. This box's sign-in for one server is never sent to another.`;
137+ }
138+}
140+/** The refusal for --server on a command that works on this machine only. */
141+export function localRefusal(command: string): string {
142+ return `abso ${command} works on this machine only and talks to no server, so it takes no --server`;
143+}
MCP
compare on this site's /mcp
CLI
abso diff 148 318 client/src/server-choice.ts --json
JSON
/christopher/abso-core/compare/148..318?file=client%2Fsrc%2Fserver-choice.ts&format=json