Multiplayer agent harness for work.
import { messageLinkSeq } from "./message-link.ts";
export const UI_BASE = ((import.meta as unknown as { env?: { BASE_URL?: string } }).env?.BASE_URL ?? "/").replace(
/\/$/,
"",
);
export function deepLinkPath(
base: string,
view: string,
sessionId: string | null,
contextScope?: string | null,
itemId?: string | null,
): string {
const b = base.replace(/\/$/, "");
if (view === "contexts" && contextScope) {
if (itemId) throw new Error("the contexts view is addressed by scope, not by item id");
return `${b}/contexts?scope=${encodeURIComponent(contextScope)}`;
}
if (view !== "chats") {
const pathView = view === "deploys" ? "apps" : view;
return `${b}/${encodeURIComponent(pathView)}${itemId ? `/${encodeURIComponent(itemId)}` : ""}`;
}
if (itemId) throw new Error("the chats view is addressed by session, not by item id");
return sessionId ? `${b}/s/${encodeURIComponent(sessionId)}` : `${b}/`;
}
function decodeSegment(seg: string): string | null {
if (!seg) return null;
try {
return decodeURIComponent(seg);
} catch {
return null;
}
}
export function parseDeepLink(
base: string,
pathname: string,
search: string,
): { view: string | null; session: string | null; item: string | null } {
const params = new URLSearchParams(search);
const b = base.replace(/\/$/, "");
const rel = pathname.startsWith(b) ? pathname.slice(b.length) : pathname;
const segments = rel.replace(/^\/+/, "").split("/");
const pathView = decodeSegment(segments[0] ?? "");
const projectKind = segments[1] === "channel" || segments[1] === "group" ? segments[1] : null;
let projectItem: string | null = null;
if (pathView === "projects") {
projectItem = projectKind ? decodeSegment(segments[2] ?? "") : decodeSegment(segments[1] ?? "");
}
const sessionRoute = pathView === "s" || pathView === "c" || pathView === "sessions";
const sessionSeg = sessionRoute ? decodeSegment(segments[1] ?? "") : null;
const viewFor = (): string | null => {
if (pathView === "projects") return "contexts";
if (sessionRoute) return "chats";
return pathView;
};
const requestedView = params.get("view") ?? viewFor();
let view = requestedView;
if (view === "connectors") view = "keychain";
else if (view === "apps") view = "deploys";
const itemFor = (): string | null => {
if (sessionRoute) return null;
if (pathView === "projects" && projectKind && projectItem) return `${projectKind}:${projectItem}`;
return projectItem ?? decodeSegment(segments[1] ?? "");
};
return {
view,
session: sessionSeg ?? params.get("session"),
item: itemFor(),
};
}
export function sessionLink(origin: string, base: string, sessionId: string, seq?: number): string {
const suffix = seq !== undefined && Number.isSafeInteger(seq) && seq >= 0 ? `?seq=${seq}` : "";
return `${origin}${deepLinkPath(base, "chats", sessionId)}${suffix}`;
}
/** The session a same-app link points at (`/s/<id>`, optionally `?seq=N`), or null for anything else. */
export function sessionLinkTarget(
href: string,
origin: string,
base: string,
): { session: string; seq: number | null } | null {
let url: URL;
try {
url = new URL(href, origin);
} catch {
return null;
}
const b = base.replace(/\/$/, "");
if (url.origin !== origin || (b && url.pathname !== b && !url.pathname.startsWith(`${b}/`))) return null;
const linked = parseDeepLink(b, url.pathname, url.search);
if (linked.view !== "chats" || !linked.session) return null;
return { session: linked.session, seq: messageLinkSeq(url.search) };
}
/** True for an unmodified left click — the case an in-app link should handle itself (SPA nav). Modified clicks (cmd/ctrl/shift/alt, middle-click) fall through to the browser so "open in new tab" works. */
export function isPlainLeftClick(e: MouseEvent): boolean {
return !(e.metaKey || e.ctrlKey || e.shiftKey || e.altKey || e.button !== 0);
}