// What every abso command shares: its output, its environment, the folder it opens, and the news it took. import { createHash } from "node:crypto"; import { hostname } from "node:os"; import { callerHeader } from "../../src/caller.ts"; import { Api, type News, newsLines, type RunKind, takeSeatNotices } from "./api.ts"; import { UsageError } from "./args.ts"; import { type Config, findRoot, readConfig } from "./config.ts"; import { newsBlockLines, newsShown } from "./core.ts"; import { logNews } from "./folder.ts"; export const EXIT = { ok: 0, error: 1, refused: 2 } as const; export interface Output { readonly text: string; readonly json: Record; readonly exit?: number; } export interface Env { readonly cwd: string; readonly env: Readonly>; /** Where `run` streams the box's output as it arrives. */ readonly write: (text: string) => void; /** --token-file: this machine's ~/.abso/token may sign in, where nothing else does. Never read otherwise. */ readonly tokenFile?: boolean; /** Where `run` streams the box's stderr; `write` when not given. */ readonly writeErr?: (text: string) => void; /** What a box run is for, when a command runs one for its own ends (shot, bisect, publish --check). */ readonly runKind?: RunKind; /** What a -file flag of - reads: stdin, unless a test gives it. */ readonly readStdin?: () => string; /** * The server --server named, checked (server-choice.ts), when the command was given one. It is used as given, * so no environment setting redirects it (ABSO_ISSUES_SERVER included). */ readonly server?: string; } export function plural(n: number, word: string): string { return `${n} ${word}${n === 1 ? "" : "s"}`; } export function open(env: Env): { root: string; config: Config; api: Api } { const root = findRoot(env.cwd); if (root === undefined) throw new UsageError( `You're not in an abso folder: ${env.cwd} has no .abso/config.json, nor does any folder above it. abso join makes one, or abso init a new project.`, ); const config = readConfig(root, env.env, env.tokenFile === true); const api = new Api(config); api.caller = callerFor(env.env.CLAUDE_CODE_SESSION_ID, hostname(), root); return { root, config, api }; } /** * The caller header for this chat, computer and folder. The chat's session id goes as a short SHA-256 of it and the * folder as its last part, so neither the raw id nor the path (which names the person) leaves the machine. */ export function callerFor(session: string | undefined, machine: string, root: string): string | undefined { const hashed = session ? createHash("sha256").update(session).digest("hex").slice(0, 16) : undefined; // The last part on either separator, so a Windows path names its folder on any machine. const folder = root .split(/[\\/]+/) .filter((p) => p !== "") .at(-1) ?? ""; return callerHeader({ session: hashed, machine, folder }); } /** * News a command took from the server while recording first. main() prints it * after whatever the command says, so every command that takes news shows it * (#37, News Report: sync, run and diff took it, wrote it to .avcs/news.log and * printed nothing; 94 of the game's 613 items, 54 of them announcements). */ const takenNews: string[] = []; export function takeNews(): string[] { return [...takeSeatNotices(), ...takenNews.splice(0)]; } /** News a record answered with: kept in .avcs/news.log, and printed by main(). */ export function heard(root: string, news: News | undefined): void { const lines = newsLines(news); logNews(root, lines); takenNews.push(...lines); } export function newsText(news: News): string[] { const lines = newsLines(news); return newsBlockLines(lines.map((n) => newsShown(n))); }