Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.
import type { AdvertisedEndpoint, DesktopBridge, DesktopWslState } from "@akeru/contracts";
import { getPairingTokenFromUrl, readHostedPairingRequest } from "@akeru/shared/remote";
type WslEnableBridge = Pick<DesktopBridge, "setWslBackendEnabled" | "setWslDistro" | "setWslOnly">;
/**
* A QR code encoding a loopback URL makes the scanning device dial itself, so
* loopback endpoints stay copyable from the endpoint menu but are never
* offered as QR targets.
*/
export function isQrShareableEndpoint(endpoint: AdvertisedEndpoint): boolean {
return endpoint.status !== "unavailable" && endpoint.reachability !== "loopback";
}
/** A remote backend is reachable when any advertised non-loopback endpoint is available. */
export function isAdvertisedEndpointRemotelyReachable(
endpoints: ReadonlyArray<AdvertisedEndpoint>,
): boolean {
return endpoints.some(
(endpoint) => endpoint.status === "available" && endpoint.reachability !== "loopback",
);
}
export type QrEndpointOption = {
/** Unique per endpoint instance (AdvertisedEndpoint.id); safe as a React key. */
readonly id: string;
/**
* Stable per endpoint *type* (endpointDefaultPreferenceKey). Multiple
* endpoints can share one, so it is only used to match the saved default.
*/
readonly preferenceKey: string;
/** False for endpoints that stay copyable but must never render as a QR. */
readonly qrShareable: boolean;
};
/**
* Resolves which endpoint the share panel shows: the user's explicit pick,
* else the saved default endpoint, else the first QR-shareable option (so the
* panel never opens on a loopback QR), else the first option. A stale
* selectedId (endpoint disappeared) falls back rather than blanking the panel.
*/
export function selectQrEndpointOption<T extends QrEndpointOption>(
options: ReadonlyArray<T>,
selectedId: string | null,
defaultPreferenceKey: string | null,
): T | null {
return (
(selectedId !== null ? options.find((option) => option.id === selectedId) : undefined) ??
(defaultPreferenceKey !== null
? options.find((option) => option.preferenceKey === defaultPreferenceKey)
: undefined) ??
options.find((option) => option.qrShareable) ??
options[0] ??
null
);
}
export async function applyWslEnableSelection(input: {
readonly bridge: WslEnableBridge;
readonly mode: "both" | "wsl-only";
readonly nextDistro: string | null;
readonly persistedDistro: string | null;
}): Promise<DesktopWslState> {
const { bridge, mode, nextDistro, persistedDistro } = input;
// Stage every preference before enabling. The desktop only relaunches for
// mode/distro changes while WSL is active, so the final enable observes the
// complete selection and is the only call that may relaunch.
await bridge.setWslOnly(mode === "wsl-only");
if (persistedDistro !== nextDistro) {
await bridge.setWslDistro(nextDistro);
}
return await bridge.setWslBackendEnabled(true);
}
/**
* Reads a pasted pairing link into the host and code the add-environment form
* submits. A hosted link (`/pair?host=…#token=…`) pairs with its `host`, not
* with the origin that served the link; a direct link pairs with its own origin.
* Returns null when the input is not a link that carries a token.
*/
export function parsePairingUrlFields(
input: string,
baseOrigin: string,
): { readonly host: string; readonly pairingCode: string } | null {
const trimmed = input.trim();
if (!trimmed) return null;
try {
const urlLikeInput =
/^[a-zA-Z][a-zA-Z\d+.-]*:\/\//u.test(trimmed) || trimmed.startsWith("//")
? trimmed
: `https://${trimmed}`;
const url = new URL(urlLikeInput, baseOrigin);
const hostedPairingRequest = readHostedPairingRequest(url);
if (hostedPairingRequest) {
return { host: hostedPairingRequest.host, pairingCode: hostedPairingRequest.token };
}
const pairingCode = getPairingTokenFromUrl(url);
if (!pairingCode) return null;
return { host: url.origin, pairingCode };
} catch {
return null;
}
}