{"draft":null,"from":232,"to":318,"files":[{"path":"client/src/api.ts","added":92,"removed":2,"diff":"--- a/client/src/api.ts\n+++ b/client/src/api.ts\n@@ -4,6 +4,7 @@\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@@ -19,6 +20,7 @@\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@@ -58,11 +60,16 @@\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@@ -137,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@@ -165,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@@ -178,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@@ -187,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@@ -221,6 +260,8 @@\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@@ -254,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@@ -685,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@@ -931,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\"), {});"}],"unmatched":[]}