{"draft":null,"from":89,"to":318,"files":[{"path":"client/src/api.ts","added":134,"removed":6,"diff":"--- a/client/src/api.ts\n+++ b/client/src/api.ts\n@@ -2,7 +2,9 @@\n // except `run`, which sends a tar.gz and reads the answer as a stream of JSON\n // lines (box-run.ts).\n \n+import { randomUUID } from \"node:crypto\";\n import { setDefaultResultOrder } from \"node:dns\";\n+import { CALLER_HEADER } from \"../../src/caller.ts\";\n import type {\n \tChangeAnswer,\n \tDraftSyncAnswer,\n@@ -18,6 +20,8 @@\n \tSyncAnswer,\n } from \"./api-answers.ts\";\n import { ApiError, errorSentence, neverSent } from \"./api-error.ts\";\n+import { UsageError } from \"./args.ts\";\n+import type { AutosettleAnswer, AutosettleMode } from \"./autosettle-commands.ts\";\n import {\n \tboxQuery,\n \tconsumeRun,\n@@ -49,17 +53,23 @@\n \tWhere,\n \tWhereAnswer,\n \tWhyAnswer,\n+\tWithoutAnswer,\n } from \"./history-answers.ts\";\n import type {\n \tAsk,\n \tAttachment,\n \tDraft,\n \tDraftBody,\n+\tGroupEvent,\n+\tGroupShown,\n+\tGroupSummary,\n+\tGroupWrite,\n \tNews as IssueNews,\n \tIssuePage,\n \tIssueRow,\n \tIssueSearchHit,\n } from \"./issues-wire.ts\";\n+import { isLocal, LOCAL_BASE, localFetch, NO_STORE } from \"./local-store.ts\";\n import type { Modes } from \"./modes.ts\";\n \n export * from \"./api-answers.ts\";\n@@ -134,6 +144,23 @@\n \t| { dryRun: true; plan: CopyPlan }\n \t| { ok: true; id: number; from: { project: string; issue: number }; skipped: { comments: number[]; asks: number[] } };\n \n+/**\n+ * The server's line when another chat just used this seat (seatNotice on an answer): kept until main() prints it with\n+ * the news, once however many answers carried it.\n+ */\n+const seatNotices = new Set<string>();\n+function noteSeat(answer: unknown): void {\n+\tif (typeof answer !== \"object\" || answer === null || Array.isArray(answer)) return;\n+\tconst line = (answer as { seatNotice?: unknown }).seatNotice;\n+\tif (typeof line === \"string\" && line !== \"\" && line.length <= 300 && !/[\\u0000-\\u001f]/.test(line))\n+\t\tseatNotices.add(line);\n+}\n+export function takeSeatNotices(): string[] {\n+\tconst out = [...seatNotices];\n+\tseatNotices.clear();\n+\treturn out;\n+}\n+\n export class Api {\n \treadonly config: Config;\n \treadonly timeoutMs: number;\n@@ -162,10 +189,17 @@\n \t/** The Issues Worker (#54) this folder's issues calls go to; set by the issues commands (issues.ts). */\n \tissuesServer: string | undefined;\n \n+\t/** Who is calling this seat (src/caller.ts): this chat's session, this computer and this folder. Set by open(). */\n+\tcaller: string | undefined;\n+\n \turl(path: string, server = this.config.server): string {\n-\t\treturn `${server.replace(/\\/+$/, \"\")}/p/${encodeURIComponent(this.config.project)}${path}`;\n+\t\tconst base = isLocal(server) ? LOCAL_BASE : server;\n+\t\treturn `${base.replace(/\\/+$/, \"\")}/p/${encodeURIComponent(this.config.project)}${path}`;\n \t}\n \n+\t/** A local project's calls: answered in this process (local-store.ts). */\n+\tprivate local: ((input: string, init?: RequestInit) => Promise<Response>) | undefined;\n+\n \tagentPath(action: string): string {\n \t\treturn `/a/${encodeURIComponent(this.config.agent)}/${action}`;\n \t}\n@@ -175,6 +209,8 @@\n \t\t\t// Which client made the call (#29); the mods send theirs too.\n \t\t\t\"x-abso-client\": `abso-cli/${clientVersion()}`,\n \t\t\t...(this.config.token && !this.config.visitor ? { authorization: `Bearer ${this.config.token}` } : {}),\n+\t\t\t// A visitor says nothing about where it's reading from.\n+\t\t\t...(this.caller !== undefined && !this.config.visitor ? { [CALLER_HEADER]: this.caller } : {}),\n \t\t\t...(contentType ? { \"content-type\": contentType } : {}),\n \t\t};\n \t}\n@@ -184,6 +220,12 @@\n \t * and follows no redirect, so nothing it sends goes anywhere it didn't name (headers() adds no sign-in).\n \t */\n \tsend(url: string, init: RequestInit = {}): Promise<Response> {\n+\t\tif (isLocal(this.config.server)) {\n+\t\t\tconst dir = this.config.localStore;\n+\t\t\tif (dir === undefined) return Promise.reject(new UsageError(NO_STORE));\n+\t\t\tthis.local ??= localFetch(dir);\n+\t\t\treturn this.local(url, init);\n+\t\t}\n \t\tif (!this.config.visitor) return fetch(url, init);\n \t\tif ((init.method ?? \"GET\") !== \"GET\")\n \t\t\treturn Promise.reject(new ApiError(\"a folder that reads signed out sends only reads\"));\n@@ -201,6 +243,7 @@\n \t\tif (this.config.visitor && method !== \"GET\")\n \t\t\tthrow new ApiError(`${path}: a folder that reads signed out sends only reads`);\n \t\tconst sent = body === undefined ? undefined : JSON.stringify(body);\n+\t\tconst requestId = method === \"POST\" ? randomUUID() : undefined;\n \t\tconst timeoutMs = Math.max(atLeastMs, timeoutFor(this.timeoutMs, sent));\n \t\t// A connection that never opened is tried once more, IPv4 first (#37, GAME - UI/UX: one IPv6\n \t\t// address timed out, and the same command went through seconds later). Nothing went out, so\n@@ -209,11 +252,16 @@\n \t\t\ttry {\n \t\t\t\tres = await this.send(this.url(path, server), {\n \t\t\t\t\tmethod,\n-\t\t\t\t\theaders: this.headers(sent === undefined ? undefined : \"application/json\"),\n+\t\t\t\t\theaders: {\n+\t\t\t\t\t\t...this.headers(sent === undefined ? undefined : \"application/json\"),\n+\t\t\t\t\t\t...(requestId ? { \"Idempotency-Key\": requestId } : {}),\n+\t\t\t\t\t},\n \t\t\t\t\t...(sent === undefined ? {} : { body: sent }),\n \t\t\t\t\tsignal: AbortSignal.timeout(timeoutMs),\n \t\t\t\t});\n \t\t\t} catch (err) {\n+\t\t\t\t// A local project's refusal (no store, a Node without SQLite) says itself.\n+\t\t\t\tif (err instanceof UsageError) throw err;\n \t\t\t\tconst e = err as Error;\n \t\t\t\tconst why =\n \t\t\t\t\te.name === \"TimeoutError\"\n@@ -231,7 +279,13 @@\n \t\t\t\tawait new Promise((ok) => setTimeout(ok, 500));\n \t\t\t}\n \t\t}\n-\t\tconst text = await res.text();\n+\t\tlet text: string;\n+\t\ttry {\n+\t\t\ttext = await res.text();\n+\t\t} catch (err) {\n+\t\t\tconst reason = err instanceof Error ? err.message : String(err);\n+\t\t\tthrow new ApiError(`${method} ${path} response body ended unexpectedly: ${reason}`, res.status);\n+\t\t}\n \t\t// 409 is a refusal that says what to do (a drop took out lines your work builds on): print it as it is.\n \t\tif (!res.ok)\n \t\t\tthrow new ApiError(\n@@ -241,7 +295,9 @@\n \t\t\t\tres.status,\n \t\t\t);\n \t\ttry {\n-\t\t\treturn JSON.parse(text) as T;\n+\t\t\tconst parsed = JSON.parse(text) as T;\n+\t\t\tnoteSeat(parsed);\n+\t\t\treturn parsed;\n \t\t} catch {\n \t\t\tthrow new ApiError(\n \t\t\t\t`${method} ${path} answered with something that isn't JSON: ${text.slice(0, 300)}`,\n@@ -672,6 +728,48 @@\n \t\treturn this.#threads(this.#issues(\"POST\", this.agentPath(`reply/${id}`), body));\n \t}\n \n+\t/** A groups call, with a server from before groups named as that rather than a bare 404. */\n+\tasync #groups<T>(call: Promise<T>): Promise<T> {\n+\t\ttry {\n+\t\t\treturn await call;\n+\t\t} catch (err) {\n+\t\t\tif (err instanceof ApiError && err.status === 404 && /no such issues route|reads have no/.test(err.message))\n+\t\t\t\tthrow new ApiError(`${this.config.server} doesn't have groups yet`, 404);\n+\t\t\tthrow err;\n+\t\t}\n+\t}\n+\n+\t/** The project's groups this agent can see. */\n+\tgroupList(): Promise<{ groups: readonly GroupSummary[] }> {\n+\t\treturn this.#groups(this.#issues(\"GET\", this.agentPath(\"groups\")));\n+\t}\n+\n+\t/** One group's settings and roster, by name or id. */\n+\tgroup(ref: string): Promise<GroupShown> {\n+\t\treturn this.#groups(this.#issues(\"GET\", this.agentPath(`group/${encodeURIComponent(ref)}`)));\n+\t}\n+\n+\t/** A page of a group's history, newest first, and the `before` that reads further back (null at the start). */\n+\tgroupEvents(\n+\t\tref: string,\n+\t\tq: { before?: number; limit?: number; kind?: string } = {},\n+\t): Promise<{ events: readonly GroupEvent[]; before: number | null }> {\n+\t\tconst qs = query({ before: q.before, limit: q.limit, kind: q.kind }).toString();\n+\t\treturn this.#groups(\n+\t\t\tthis.#issues(\"GET\", this.agentPath(`group/${encodeURIComponent(ref)}/events${qs ? `?${qs}` : \"\"}`)),\n+\t\t);\n+\t}\n+\n+\t/** Make a group (ref undefined), or post, join, leave, add, remove or change settings in one. */\n+\tgroupWrite(\n+\t\tref: string | undefined,\n+\t\taction: \"post\" | \"join\" | \"leave\" | \"add\" | \"remove\" | \"settings\" | undefined,\n+\t\tbody: Record<string, unknown>,\n+\t): Promise<GroupWrite> {\n+\t\tconst path = ref === undefined ? \"groups\" : `group/${encodeURIComponent(ref)}/${action}`;\n+\t\treturn this.#groups(this.#issues(\"POST\", this.agentPath(path), body));\n+\t}\n+\n \t/** Takes back an ask this agent made, with a note on why; the one asked hears it. */\n \twithdraw(id: number, body: { note?: string }): Promise<{ ask: Ask; news?: IssueNews }> {\n \t\treturn this.#issues(\"POST\", this.agentPath(`withdraw/${id}`), body);\n@@ -705,6 +803,26 @@\n \t\treturn this.json(\"POST\", `/visibility?visibility=${v}`, {});\n \t}\n \n+\t/** The project's auto-settle choice (abso-core 23): off, advise or settle, and how sure before abso settles. */\n+\tautosettle(): Promise<AutosettleAnswer> {\n+\t\treturn this.json(\"GET\", \"/autosettle\");\n+\t}\n+\n+\t/** Change it (abso-core 23): an owner's key, or the shared token on staging. Without `at`, the server keeps its own. */\n+\tsetAutosettle(mode: AutosettleMode, at?: number): Promise<AutosettleAnswer> {\n+\t\treturn this.json(\"POST\", `/autosettle?mode=${mode}${at === undefined ? \"\" : `&at=${at}`}`, {});\n+\t}\n+\n+\t/** Auto-settle's history (abso-core 23): held pushes the model advised on, newest first. */\n+\tautosettleHistory(): Promise<unknown> {\n+\t\treturn this.json(\"GET\", \"/autosettle/history\");\n+\t}\n+\n+\t/** One held push from the history, with each collision's calls to the model. */\n+\tautosettleHold(id: number): Promise<unknown> {\n+\t\treturn this.json(\"GET\", `/autosettle/history/${id}`);\n+\t}\n+\n \t/** Take back this agent's queued publishes (#39); refused while one is being checked. */\n \tunqueue(): Promise<{ pulled: readonly number[] }> {\n \t\treturn this.json(\"POST\", this.agentPath(\"unqueue\"), {});\n@@ -867,6 +985,11 @@\n \t\treturn this.json(\"GET\", `/compare?${query({ from, to, ...where })}`);\n \t}\n \n+\t/** Version `version` of shared with `versions` left out, or the refusal naming what builds on them. */\n+\twithout(version: number, versions: readonly number[]): Promise<WithoutAnswer> {\n+\t\treturn this.json(\"GET\", `/without?${query({ version, versions: versions.join(\",\") })}`);\n+\t}\n+\n \t/** A message to the project's other agents (or those named), delivered once as news. */\n \tstarting(\n \t\ttext: string | null,\n@@ -893,6 +1016,11 @@\n \t\treturn this.json(\"POST\", this.agentPath(\"news\"), {});\n \t}\n \n+\t/** A ticket that opens this agent's news stream once, within a minute (abso 690; news-stream.ts). */\n+\tnewsTicket(): Promise<{ url: string; protocols: string[]; expiresAt: number | string }> {\n+\t\treturn this.json(\"POST\", this.agentPath(\"news/ticket\"), {});\n+\t}\n+\n \t/** This agent's unpublished changes, file by file. */\n \tdiff(): Promise<DiffAnswer> {\n \t\treturn this.json(\"POST\", this.agentPath(\"diff\"), {});\n@@ -908,8 +1036,8 @@\n \t\treturn this.json(\"POST\", this.agentPath(\"restore\"), files === null ? { all: true } : { files });\n \t}\n \n-\twhy(file: string, line: number, view: View = {}): Promise<WhyAnswer> {\n-\t\treturn this.json(\"GET\", `/why?${query({ file, line, ...view })}`);\n+\twhy(file: string, line: number, view: View = {}, version?: number): Promise<WhyAnswer> {\n+\t\treturn this.json(\"GET\", `/why?${query({ file, line, version, ...view })}`);\n \t}\n \n \twhere(): Promise<WhereAnswer> {"}],"unmatched":[]}