- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/client/src/command-env.ts?version=310&format=json
92 linesFirst import 63% · pinch 21% · groups 7% · speed 7% · readspeed 3%Last change pinch in version 290, 9 versions changed it
You're reading version 310. The newest is 318, and no version since then changed this file.
1// What every abso command shares: its output, its environment, the folder it opens, and the news it took.
3import { createHash } from "node:crypto";
4import { hostname } from "node:os";
5import { callerHeader } from "../../src/caller.ts";
6import { Api, type News, newsLines, type RunKind, takeSeatNotices } from "./api.ts";
7import { UsageError } from "./args.ts";
8import { type Config, findRoot, readConfig } from "./config.ts";
9import { newsBlockLines, newsShown } from "./core.ts";
10import { logNews } from "./folder.ts";
12export const EXIT = { ok: 0, error: 1, refused: 2 } as const;
14export interface Output {
15 readonly text: string;
16 readonly json: Record<string, unknown>;
17 readonly exit?: number;
18}
20export interface Env {
21 readonly cwd: string;
22 readonly env: Readonly<Record<string, string | undefined>>;
23 /** Where `run` streams the box's output as it arrives. */
24 readonly write: (text: string) => void;
25 /** --token-file: this machine's ~/.abso/token may sign in, where nothing else does. Never read otherwise. */
26 readonly tokenFile?: boolean;
27 /** Where `run` streams the box's stderr; `write` when not given. */
28 readonly writeErr?: (text: string) => void;
29 /** What a box run is for, when a command runs one for its own ends (shot, bisect, publish --check). */
30 readonly runKind?: RunKind;
31 /** What a -file flag of - reads: stdin, unless a test gives it. */
32 readonly readStdin?: () => string;
33 /**
34 * The server --server named, checked (server-choice.ts), when the command was given one. It is used as given,
35 * so no environment setting redirects it (ABSO_ISSUES_SERVER included).
36 */
37 readonly server?: string;
38}
40export function plural(n: number, word: string): string {
41 return `${n} ${word}${n === 1 ? "" : "s"}`;
42}
44export function open(env: Env): { root: string; config: Config; api: Api } {
45 const root = findRoot(env.cwd);
46 if (root === undefined)
47 throw new UsageError(
48 `You're not in an abso folder: ${env.cwd} has no .abso/config.json, nor does any folder above it. abso join <project> makes one, or abso init a new project.`,
49 );
50 const config = readConfig(root, env.env, env.tokenFile === true);
51 const api = new Api(config);
52 api.caller = callerFor(env.env.CLAUDE_CODE_SESSION_ID, hostname(), root);
53 return { root, config, api };
54}
56/**
57 * The caller header for this chat, computer and folder. The chat's session id goes as a short SHA-256 of it and the
58 * folder as its last part, so neither the raw id nor the path (which names the person) leaves the machine.
59 */
60export function callerFor(session: string | undefined, machine: string, root: string): string | undefined {
61 const hashed = session ? createHash("sha256").update(session).digest("hex").slice(0, 16) : undefined;
62 // The last part on either separator, so a Windows path names its folder on any machine.
63 const folder =
64 root
65 .split(/[\\/]+/)
66 .filter((p) => p !== "")
67 .at(-1) ?? "";
68 return callerHeader({ session: hashed, machine, folder });
69}
71/**
72 * News a command took from the server while recording first. main() prints it
73 * after whatever the command says, so every command that takes news shows it
74 * (#37, News Report: sync, run and diff took it, wrote it to .avcs/news.log and
75 * printed nothing; 94 of the game's 613 items, 54 of them announcements).
76 */
77const takenNews: string[] = [];
78export function takeNews(): string[] {
79 return [...takeSeatNotices(), ...takenNews.splice(0)];
80}
82/** News a record answered with: kept in .avcs/news.log, and printed by main(). */
83export function heard(root: string, news: News | undefined): void {
84 const lines = newsLines(news);
85 logNews(root, lines);
86 takenNews.push(...lines);
87}
89export function newsText(news: News): string[] {
90 const lines = newsLines(news);
91 return newsBlockLines(lines.map((n) => newsShown(n)));
92}