Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
import type { AutopilotTrigger } from "../types";
/**
* Compose a usable absolute webhook URL for a webhook trigger.
*
* Resolution order:
* 1. trigger.webhook_url — present only when MULTICA_PUBLIC_URL is set on the
* server. This is the authoritative form when available.
* 2. apiBaseUrl + webhook_path — desktop apps and self-host setups where the
* server didn't mint an absolute URL but the client knows its API origin.
* 3. currentOrigin + webhook_path — browser fallback when getBaseUrl() is
* empty (e.g. same-origin Next.js dev).
*
* Returns null when the trigger has no token / path yet (a new trigger that
* hasn't been written back to the cache, or a non-webhook trigger).
*/
export function buildAutopilotWebhookUrl(params: {
trigger: Pick<AutopilotTrigger, "kind" | "webhook_token" | "webhook_path" | "webhook_url">;
apiBaseUrl?: string;
currentOrigin?: string;
}): string | null {
const { trigger, apiBaseUrl, currentOrigin } = params;
if (trigger.kind !== "webhook") return null;
if (typeof trigger.webhook_url === "string" && trigger.webhook_url) {
return trigger.webhook_url;
}
const path =
(typeof trigger.webhook_path === "string" && trigger.webhook_path) ||
(trigger.webhook_token ? `/api/webhooks/autopilots/${trigger.webhook_token}` : null);
if (!path) return null;
const base = stripTrailingSlash(apiBaseUrl) || stripTrailingSlash(currentOrigin);
if (!base) return path; // last resort — relative path will still work in-browser
return base + path;
}
function stripTrailingSlash(s: string | undefined): string {
if (!s) return "";
return s.endsWith("/") ? s.slice(0, -1) : s;
}
/** Fixed-width run — never derived from the token, so the mask leaks no length. */
const WEBHOOK_URL_MASK = "••••••••••••";
/**
* Mask the secret part of a webhook URL for display.
*
* Only the trailing token segment is a credential: anyone holding it can fire
* the autopilot. The origin and the `/api/webhooks/autopilots/` prefix carry no
* secret, so they stay readable and the value is still recognizable as this
* trigger's URL while hidden. Falls back to the bare mask when the URL has no
* separable last segment.
*/
export function maskAutopilotWebhookUrl(url: string): string {
const cut = url.lastIndexOf("/");
if (cut < 0 || cut === url.length - 1) return WEBHOOK_URL_MASK;
return url.slice(0, cut + 1) + WEBHOOK_URL_MASK;
}