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
MCP
code on this site's /mcp
CLI
none: the JSON below is the answer
JSON
/christopher/abso-core/files/src/caller.ts?version=280&format=json
74 linesgroups 96% · capacity 4%Last change capacity in version 279, 2 versions changed it

You're reading version 280. The newest is 318, and 1 version since then changed this file. Go to the newestCompare with 318

1// Who is calling a seat: the chat's session, the computer and the folder, so a seat can tell its callers when
2// someone else picks it up. It is information, never authority: the agent key decides who the caller is, and nothing
3// secret goes in it. Sent as one header, base64url of its JSON.
4
5export const CALLER_HEADER = "x-abso-caller";
6/** The header's most bytes. A folder that would go over keeps its end, which is the part that tells folders apart. */
7export const CALLER_MAX = 512;
8
9export interface Caller {
10 /** The chat's session id, when abso runs in one (Claude Code's CLAUDE_CODE_SESSION_ID, or the mod's own). */
11 readonly session?: string;
12 readonly machine: string;
13 readonly folder: string;
16const SESSION = /^[A-Za-z0-9_-]{1,64}$/;
17const MACHINE = /^[\w.-]{1,64}$/;
18const FOLDER_MAX = 300;
20const b64u = (text: string): string =>
21 btoa(String.fromCodePoint(...new TextEncoder().encode(text)))
22 .replace(/\+/g, "-")
23 .replace(/\//g, "_")
24 .replace(/=+$/, "");
26function fromB64u(text: string): string {
27 const bin = atob(text.replace(/-/g, "+").replace(/_/g, "/"));
28 return new TextDecoder("utf-8", { fatal: true, ignoreBOM: false }).decode(
29 Uint8Array.from(bin, (c) => c.codePointAt(0)!),
30 );
33/** The header for this caller, or undefined when there's nothing usable to say. */
34export function callerHeader(caller: {
35 session?: string | undefined;
36 machine: string;
37 folder: string;
38}): string | undefined {
39 const machine = caller.machine.replace(/[^\w.-]/g, "-").slice(0, 64);
40 if (!MACHINE.test(machine)) return undefined;
41 const session = caller.session !== undefined && SESSION.test(caller.session) ? caller.session : undefined;
42 let folder = caller.folder.slice(-FOLDER_MAX);
43 for (;;) {
44 const out = b64u(JSON.stringify({ ...(session === undefined ? {} : { session }), machine, folder }));
45 if (out.length <= CALLER_MAX) return out;
46 if (folder.length === 0) return undefined;
47 folder = folder.slice(Math.ceil(folder.length / 4));
48 }
51/** The caller a header names, or null when it is missing or isn't one (a bad header never guesses). */
52export function parseCaller(header: string | null | undefined): Caller | null {
53 if (header === null || header === undefined || header === "" || header.length > CALLER_MAX) return null;
54 if (!/^[A-Za-z0-9_-]+$/.test(header)) return null;
55 let raw: unknown;
56 try {
57 raw = JSON.parse(fromB64u(header));
58 } catch {
59 return null;
60 }
61 if (typeof raw !== "object" || raw === null || Array.isArray(raw)) return null;
62 const { session, machine, folder, ...rest } = raw as Record<string, unknown>;
63 if (Object.keys(rest).length > 0) return null;
64 if (typeof machine !== "string" || !MACHINE.test(machine)) return null;
65 if (typeof folder !== "string" || folder.length > FOLDER_MAX || /[\u0000-\u001f]/.test(folder)) return null;
66 if (session !== undefined && (typeof session !== "string" || !SESSION.test(session))) return null;
67 return { ...(session === undefined ? {} : { session }), machine, folder };
70/** How a caller reads in a sentence: "<folder> on <machine>", with only the folder's last part. */
71export function callerPlace(caller: Caller): string {
72 const parts = caller.folder.split(/[\\/]+/).filter((p) => p !== "");
73 return `${parts.at(-1) ?? "a folder"} on ${caller.machine}`;