Skip to content

A desktop coding agent with free access to frontier models.

TypeScript
40.1 KB
Raw Blame
import { workflowRunPreviewSchema, type WorkflowRunPreview } from "@omnirush/types/workflows";
import { DEN_WORKER_POLL_INTERVAL_MS } from "./CONSTS";
import { denApiCredentials, denApiEndpoint } from "./den-api-origin";
import { ORG_SCOPE_HEADER, getRequestOrgScope, shouldPinOrgScopePath } from "./org-scope";
import { getRuntimeConfig } from "./runtime-config";

export type AuthMode = "sign-in" | "sign-up";
export type SocialAuthProvider = "github" | "google";
export type WorkerStatusBucket = "ready" | "starting" | "attention" | "other";
export type RuntimeServiceName = "omnirush-server" | "opencode";
export type EventLevel = "info" | "success" | "warning" | "error";
export type AuthMethod = "email" | SocialAuthProvider;

export type BillingPrice = {
  amount: number | null;
  currency: string | null;
  recurringInterval: string | null;
  recurringIntervalCount: number | null;
};

export type BillingSubscription = {
  id: string;
  status: string;
  amount: number | null;
  currency: string | null;
  recurringInterval: string | null;
  recurringIntervalCount: number | null;
  currentPeriodStart: string | null;
  currentPeriodEnd: string | null;
  cancelAtPeriodEnd: boolean;
  canceledAt: string | null;
  endedAt: string | null;
};

export type BillingInvoice = {
  id: string;
  createdAt: string | null;
  status: string;
  totalAmount: number | null;
  currency: string | null;
  invoiceNumber: string | null;
  invoiceUrl: string | null;
};

export type BillingSummary = {
  featureGateEnabled: boolean;
  hasActivePlan: boolean;
  checkoutRequired: boolean;
  portalUrl: string | null;
  price: BillingPrice | null;
  subscription: BillingSubscription | null;
  invoices: BillingInvoice[];
  productId: string | null;
  benefitId: string | null;
};

export type OrgLimitError = {
  error: "org_limit_reached";
  message: string;
  limitType: "members" | "workers";
  currentCount: number;
  limit: number;
};

export type OrgPaymentRequiredError = {
  error: "payment_required";
  reason: "seat_subscription_required";
  subscriptionType: "seat";
  message: string;
  currentCount: number;
  freeSeatCount: number;
};

export type AuthUser = {
  id: string;
  email: string;
  name: string | null;
  authProviders: string[];
};

export class ReauthRequiredError extends Error {
  readonly error = "reauth";
  readonly reason: string | null;

  constructor(message: string, reason: string | null) {
    super(message);
    this.name = "ReauthRequiredError";
    this.reason = reason;
  }
}

function formatDeadlineDuration(timeoutMs: number): string {
  if (timeoutMs < 1000) return `${timeoutMs} milliseconds`;
  const seconds = timeoutMs / 1000;
  return Number.isInteger(seconds) ? `${seconds} seconds` : `${seconds.toFixed(1)} seconds`;
}

export class DenRequestTimeoutError extends Error {
  readonly timeoutMs: number;
  readonly outcome: "unknown" = "unknown";

  constructor(timeoutMs: number, cause?: unknown) {
    super(
      `OmniRush.ai stopped waiting after ${formatDeadlineDuration(timeoutMs)}. The operation’s outcome is unknown.`,
      cause === undefined ? undefined : { cause },
    );
    this.name = "DenRequestTimeoutError";
    this.timeoutMs = timeoutMs;
  }
}

export class DenRequestCanceledError extends Error {
  readonly outcome: "unknown" = "unknown";

  constructor(cause?: unknown) {
    super(
      "The OmniRush.ai request was canceled before the dashboard received a result. The operation’s outcome is unknown.",
      cause === undefined ? undefined : { cause },
    );
    this.name = "DenRequestCanceledError";
  }
}

export type WorkerLaunch = {
  workerId: string;
  workerName: string;
  status: string;
  provider: string | null;
  instanceUrl: string | null;
  omnirushUrl: string | null;
  previewOmniRushUrl?: string | null;
  previewExpiresAt?: string | null;
  workspaceId: string | null;
  clientToken: string | null;
  ownerToken: string | null;
  hostToken: string | null;
};

export type WorkerSummary = {
  workerId: string;
  workerName: string;
  status: string;
  instanceUrl: string | null;
  provider: string | null;
  isMine: boolean;
};

export type WorkerTokens = {
  clientToken: string | null;
  ownerToken: string | null;
  hostToken: string | null;
  omnirushUrl: string | null;
  previewOmniRushUrl: string | null;
  previewExpiresAt: string | null;
  workspaceId: string | null;
};

export type WorkerListItem = {
  workerId: string;
  workerName: string;
  status: string;
  instanceUrl: string | null;
  provider: string | null;
  isMine: boolean;
  createdAt: string | null;
};

export type WorkflowRun = {
  id: string;
  workflow: WorkflowRunPreview | null;
  source: string;
  status: "succeeded" | "failed";
  errorKind: string | null;
  errorMessage: string | null;
  toolCallCount: number;
  toolCalls: Array<{ name: string }>;
  durationMs: number;
  startedAt: string;
  finishedAt: string;
  createdAt: string;
  orgMembershipId: string | null;
};

export type WorkerRuntimeService = {
  name: RuntimeServiceName;
  enabled: boolean;
  running: boolean;
  targetVersion: string | null;
  actualVersion: string | null;
  upgradeAvailable: boolean;
};

export type WorkerRuntimeSnapshot = {
  services: WorkerRuntimeService[];
  upgrade: {
    status: "idle" | "running" | "failed";
    startedAt: string | null;
    finishedAt: string | null;
    error: string | null;
  };
};

export type LaunchEvent = {
  id: string;
  level: EventLevel;
  label: string;
  detail: string;
  at: string;
};

export type OnboardingIntent = {
  version: 1;
  workerName: string;
  shouldLaunch: boolean;
  completed: boolean;
  authMethod: AuthMethod;
};

type PosthogClient = {
  capture?: (eventName: string, properties?: Record<string, unknown>) => void;
  identify?: (distinctId?: string, properties?: Record<string, unknown>) => void;
  reset?: () => void;
};

declare global {
  interface Window {
    posthog?: PosthogClient;
  }
}

export const LAST_WORKER_STORAGE_KEY = "omnirush:web:last-worker";
export const PENDING_SOCIAL_SIGNUP_STORAGE_KEY = "omnirush:web:pending-social-signup";
export const AUTH_TOKEN_STORAGE_KEY = "omnirush:web:auth-token";
export const ONBOARDING_INTENT_STORAGE_KEY = "omnirush:web:onboarding-intent";
export const PENDING_AUTH_INTENT_STORAGE_KEY = "omnirush:web:pending-auth-intent";
export const WORKER_STATUS_POLL_MS = DEN_WORKER_POLL_INTERVAL_MS;

export function getWorkerConnectionPollDelay(attempt: number): number {
  return Math.min(WORKER_STATUS_POLL_MS * 2 ** Math.min(Math.max(0, attempt - 1), 2), 5_000);
}
export const DEFAULT_AUTH_NAME = "OmniRush.ai User";
export const DEFAULT_WORKER_NAME = "My Worker";
export const WORKSPACE_REAUTH_SECURITY_MESSAGE = "For security, confirm it's you before changing workspace settings.";

export type AuthIntent = "models";

function isRecord(value: unknown): value is Record<string, unknown> {
  return typeof value === "object" && value !== null;
}

export function getAuthInfoForMode(mode: AuthMode): string {
  return mode === "sign-up"
    ? "Create an account to launch and manage cloud workers."
    : "Sign in to launch and manage cloud workers.";
}

export function getEmailDomain(email: string): string {
  const atIndex = email.lastIndexOf("@");
  if (atIndex === -1 || atIndex + 1 >= email.length) {
    return "unknown";
  }
  return email.slice(atIndex + 1).toLowerCase();
}

export function trackPosthogEvent(eventName: string, properties: Record<string, unknown> = {}) {
  if (typeof window === "undefined") {
    return;
  }

  try {
    window.posthog?.capture?.(eventName, properties);
  } catch {
    // Ignore analytics delivery failures.
  }
}

export function identifyPosthogUser(user: AuthUser) {
  if (typeof window === "undefined") {
    return;
  }

  try {
    window.posthog?.identify?.(user.id, {
      email: user.email,
      name: user.name ?? undefined
    });
  } catch {
    // Ignore analytics delivery failures.
  }
}

export function resetPosthogUser() {
  if (typeof window === "undefined") {
    return;
  }

  try {
    window.posthog?.reset?.();
  } catch {
    // Ignore analytics delivery failures.
  }
}

export function normalizeAuthModeParam(value: string | null | undefined): AuthMode | null {
  return value === "sign-in" || value === "sign-up" ? value : null;
}

export function normalizeAuthIntentParam(value: string | null | undefined): AuthIntent | null {
  return value === "models" ? value : null;
}

export function getSocialProviderLabel(provider: SocialAuthProvider): string {
  return provider === "github" ? "GitHub" : "Google";
}

export function normalizeWorkerName(input: string): string {
  const normalized = input.trim().replace(/\s+/g, " ");
  return normalized || DEFAULT_WORKER_NAME;
}

export function deriveOnboardingWorkerName(user: AuthUser): string {
  const rawIdentity = (user.name?.trim() || user.email.split("@")[0] || DEFAULT_WORKER_NAME).replace(/[._-]+/g, " ").trim();
  const base = rawIdentity
    .split(/\s+/)
    .filter(Boolean)
    .map((part) => `${part.slice(0, 1).toUpperCase()}${part.slice(1)}`)
    .join(" ");

  const owner = base || DEFAULT_WORKER_NAME;
  const suffix = owner.endsWith("s") ? "' Worker" : "'s Worker";
  return normalizeWorkerName(`${owner}${suffix}`);
}

export function getSocialCallbackUrl(authCallbackBaseUrl = ""): string {
  try {
    const origin = authCallbackBaseUrl || (typeof window !== "undefined" ? window.location.origin : "");
    if (!origin) {
      return "/";
    }
    const callbackUrl = new URL("/", origin);
    if (typeof window !== "undefined") {
      const params = new URLSearchParams(window.location.search);
      for (const key of ["mode", "desktopAuth", "desktopScheme", "webAuth", "webAuthReturn", "invite", "intent"]) {
        const value = params.get(key)?.trim() ?? "";
        if (value) {
          callbackUrl.searchParams.set(key, value);
        }
      }
    }
    return callbackUrl.toString();
  } catch {
    if (authCallbackBaseUrl) {
      try {
        return new URL("/", authCallbackBaseUrl).toString();
      } catch {
        // Fall through to the hosted default when the configured URL is invalid.
      }
    }
    return typeof window !== "undefined" ? `${window.location.origin}/` : "/";
  }
}

export function isDesktopContext(): boolean {
  if (typeof window === "undefined") {
    return false;
  }

  const ua = window.navigator.userAgent || "";
  return !/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(ua);
}

export function shortValue(value: string): string {
  if (value.length <= 18) {
    return value;
  }
  return `${value.slice(0, 8)}...${value.slice(-6)}`;
}

export function formatMoneyMinor(amount: number | null, currency: string | null): string {
  if (typeof amount !== "number" || !Number.isFinite(amount)) {
    return "Not available";
  }

  const normalizedCurrency = (currency ?? "USD").toUpperCase();
  const majorValue = amount / 100;

  try {
    return new Intl.NumberFormat(undefined, {
      style: "currency",
      currency: normalizedCurrency
    }).format(majorValue);
  } catch {
    return `${majorValue.toFixed(2)} ${normalizedCurrency}`;
  }
}

export function formatIsoDate(value: string | null): string {
  if (!value) {
    return "Not available";
  }

  try {
    const date = new Date(value);
    if (Number.isNaN(date.getTime())) {
      return "Not available";
    }
    return date.toLocaleDateString();
  } catch {
    return "Not available";
  }
}

export function formatRecurringInterval(interval: string | null, count: number | null): string {
  if (!interval) {
    return "billing cycle";
  }

  const normalizedInterval = interval.replace(/_/g, " ");
  const normalizedCount = typeof count === "number" && Number.isFinite(count) ? count : 1;

  if (normalizedCount <= 1) {
    return `per ${normalizedInterval}`;
  }

  const pluralSuffix = normalizedInterval.endsWith("s") ? "" : "s";
  return `every ${normalizedCount} ${normalizedInterval}${pluralSuffix}`;
}

export function formatSubscriptionStatus(status: string): string {
  const normalized = status.trim().toLowerCase();
  if (!normalized) {
    return "Unknown";
  }

  return normalized
    .split("_")
    .map((part) => `${part.slice(0, 1).toUpperCase()}${part.slice(1)}`)
    .join(" ");
}

export function getErrorMessage(payload: unknown, fallback: string): string {
  if (typeof payload === "string" && payload.trim().length > 0) {
    const trimmed = payload.trim();
    const lower = trimmed.toLowerCase();
    if (lower.startsWith("<!doctype") || lower.startsWith("<html") || lower.includes("<body")) {
      return `${fallback} Upstream returned an HTML error page.`;
    }
    if (trimmed.length > 240) {
      return `${fallback} Upstream returned a non-JSON error payload.`;
    }
    return trimmed;
  }

  if (!isRecord(payload)) {
    return fallback;
  }

  const message = payload.message;
  if (typeof message === "string" && message.trim().length > 0) {
    return message;
  }

  const error = payload.error;
  if (typeof error === "string" && error.trim().length > 0) {
    return error;
  }

  return fallback;
}

export function getOrgLimitError(payload: unknown): OrgLimitError | null {
  if (!isRecord(payload) || payload.error !== "org_limit_reached") {
    return null;
  }

  if (
    (payload.limitType !== "members" && payload.limitType !== "workers") ||
    typeof payload.message !== "string" ||
    typeof payload.currentCount !== "number" ||
    typeof payload.limit !== "number"
  ) {
    return null;
  }

  return {
    error: "org_limit_reached",
    message: payload.message,
    limitType: payload.limitType,
    currentCount: payload.currentCount,
    limit: payload.limit,
  };
}

export function getOrgPaymentRequiredError(payload: unknown): OrgPaymentRequiredError | null {
  if (!isRecord(payload) || payload.error !== "payment_required") {
    return null;
  }

  if (
    payload.reason !== "seat_subscription_required" ||
    payload.subscriptionType !== "seat" ||
    typeof payload.message !== "string" ||
    typeof payload.currentCount !== "number" ||
    typeof payload.freeSeatCount !== "number"
  ) {
    return null;
  }

  return {
    error: "payment_required",
    reason: "seat_subscription_required",
    subscriptionType: "seat",
    message: payload.message,
    currentCount: payload.currentCount,
    freeSeatCount: payload.freeSeatCount,
  };
}

export function getReauthRequiredError(payload: unknown, response: Response): ReauthRequiredError | null {
  if (response.status !== 403 || !isRecord(payload) || payload.error !== "reauth") {
    return null;
  }

  return new ReauthRequiredError(
    getErrorMessage(payload, WORKSPACE_REAUTH_SECURITY_MESSAGE),
    typeof payload.reason === "string" ? payload.reason : null,
  );
}

export function getRequestError(payload: unknown, response: Response, fallback: string) {
  return getReauthRequiredError(payload, response) ?? new Error(getErrorMessage(payload, fallback));
}

export function isReauthRequiredError(error: unknown): error is ReauthRequiredError {
  return error instanceof ReauthRequiredError;
}

export function getUser(payload: unknown): AuthUser | null {
  if (!isRecord(payload) || !isRecord(payload.user)) {
    return null;
  }

  const user = payload.user;
  if (typeof user.id !== "string" || typeof user.email !== "string") {
    return null;
  }

  return {
    id: user.id,
    email: user.email,
    name: typeof user.name === "string" ? user.name : null,
    authProviders: Array.isArray(user.authProviders)
      ? user.authProviders.filter((provider): provider is string => typeof provider === "string")
      : []
  };
}

export function getToken(payload: unknown): string | null {
  if (!isRecord(payload)) {
    return null;
  }
  return typeof payload.token === "string" ? payload.token : null;
}

function getDurableWorkerInstanceUrl(instance: Record<string, unknown> | null) {
  if (!instance || instance.provider === "daytona") return null;
  return typeof instance.url === "string" ? instance.url : null;
}

export function getWorker(payload: unknown): WorkerLaunch | null {
  if (!isRecord(payload) || !isRecord(payload.worker)) {
    return null;
  }

  const worker = payload.worker;
  if (typeof worker.id !== "string" || typeof worker.name !== "string") {
    return null;
  }

  const instance = isRecord(payload.instance) ? payload.instance : null;
  const tokens = isRecord(payload.tokens) ? payload.tokens : null;

  return {
    workerId: worker.id,
    workerName: worker.name,
    status: getEffectiveWorkerStatus(worker.status, instance),
    provider: instance && typeof instance.provider === "string" ? instance.provider : null,
    instanceUrl: getDurableWorkerInstanceUrl(instance),
    omnirushUrl: getDurableWorkerInstanceUrl(instance),
    previewOmniRushUrl: null,
    previewExpiresAt: null,
    workspaceId: null,
    clientToken: tokens && typeof tokens.client === "string" ? tokens.client : null,
    ownerToken: tokens && typeof tokens.owner === "string"
      ? tokens.owner
      : tokens && typeof tokens.host === "string"
        ? tokens.host
        : null,
    hostToken: tokens && typeof tokens.host === "string" ? tokens.host : null
  };
}

export function getWorkerSummary(payload: unknown): WorkerSummary | null {
  if (!isRecord(payload) || !isRecord(payload.worker)) {
    return null;
  }

  const worker = payload.worker;
  if (typeof worker.id !== "string" || typeof worker.name !== "string") {
    return null;
  }

  const instance = isRecord(payload.instance) ? payload.instance : null;

  return {
    workerId: worker.id,
    workerName: worker.name,
    status: getEffectiveWorkerStatus(worker.status, instance),
    instanceUrl: getDurableWorkerInstanceUrl(instance),
    provider: instance && typeof instance.provider === "string" ? instance.provider : null,
    isMine: worker.isMine === true
  };
}

export function getWorkerTokens(payload: unknown): WorkerTokens | null {
  if (!isRecord(payload) || !isRecord(payload.tokens)) {
    return null;
  }

  const tokens = payload.tokens;
  const connect = isRecord(payload.connect) ? payload.connect : null;
  const directPreview = isRecord(payload.directPreview) && payload.directPreview.version === 1
    ? payload.directPreview
    : null;
  const clientToken = typeof tokens.client === "string" ? tokens.client : null;
  const ownerToken = typeof tokens.owner === "string"
    ? tokens.owner
    : typeof tokens.host === "string"
      ? tokens.host
      : null;
  const hostToken = typeof tokens.host === "string" ? tokens.host : null;
  const omnirushUrl = connect && typeof connect.omnirushUrl === "string" ? connect.omnirushUrl : null;
  const previewOmniRushUrl = directPreview && typeof directPreview.omnirushUrl === "string" ? directPreview.omnirushUrl : null;
  const previewExpiresAt = directPreview && typeof directPreview.expiresAt === "string" ? directPreview.expiresAt : null;
  const workspaceId = connect && typeof connect.workspaceId === "string" ? connect.workspaceId : null;

  if (!clientToken && !ownerToken && !hostToken) {
    return null;
  }

  return { clientToken, ownerToken, hostToken, omnirushUrl, previewOmniRushUrl, previewExpiresAt, workspaceId };
}

export function withWorkerConnection(worker: WorkerLaunch, tokens: WorkerTokens): WorkerLaunch {
  return {
    ...worker,
    omnirushUrl: tokens.omnirushUrl,
    previewOmniRushUrl: tokens.previewOmniRushUrl,
    previewExpiresAt: tokens.previewExpiresAt,
    workspaceId: tokens.workspaceId,
    clientToken: tokens.clientToken,
    ownerToken: tokens.ownerToken,
    hostToken: tokens.hostToken,
  };
}

export function getWorkerConnectionTargets(worker: WorkerLaunch | null) {
  const desktopUrl = worker?.omnirushUrl ?? worker?.instanceUrl ?? null;
  const webUrl = worker?.previewOmniRushUrl
    ?? (worker?.provider === "daytona" || worker?.instanceUrl === null ? null : desktopUrl);
  return { desktopUrl, webUrl };
}

export function getWorkerConnectionTokens(worker: WorkerLaunch | null) {
  return {
    desktopToken: worker?.hostToken ?? worker?.ownerToken ?? null,
    webToken: worker?.clientToken ?? null,
  };
}

export function workerConnectionEquals(current: WorkerLaunch, next: WorkerLaunch) {
  return current.omnirushUrl === next.omnirushUrl
    && current.previewOmniRushUrl === next.previewOmniRushUrl
    && current.previewExpiresAt === next.previewExpiresAt
    && current.workspaceId === next.workspaceId
    && current.clientToken === next.clientToken
    && current.ownerToken === next.ownerToken
    && current.hostToken === next.hostToken;
}

const WORKER_PREVIEW_REFRESH_LEAD_MS = 30_000;

export function workerNeedsConnectionResolution(worker: WorkerLaunch, now = Date.now()): boolean {
  if (worker.status.trim().toLowerCase() === "failed") return false;
  const hasRequiredTokens = Boolean(worker.clientToken?.trim() && (worker.hostToken?.trim() || worker.ownerToken?.trim()));
  if (!hasRequiredTokens || !worker.omnirushUrl?.trim()) return true;

  const usesExpiringPreview = worker.provider === "daytona" || worker.instanceUrl === null || Boolean(worker.previewOmniRushUrl);
  if (!usesExpiringPreview) return false;
  if (!worker.workspaceId?.trim() || !worker.previewOmniRushUrl?.trim() || !worker.previewExpiresAt) return true;

  const expiresAt = Date.parse(worker.previewExpiresAt);
  return !Number.isFinite(expiresAt) || expiresAt <= now + WORKER_PREVIEW_REFRESH_LEAD_MS;
}

export function getWorkerConnectionRefreshDelay(worker: WorkerLaunch, now = Date.now()): number | null {
  if (worker.status.trim().toLowerCase() === "failed") return null;
  if (workerNeedsConnectionResolution(worker, now)) return 0;
  if (!worker.previewExpiresAt) return null;
  return Math.max(0, Date.parse(worker.previewExpiresAt) - now - WORKER_PREVIEW_REFRESH_LEAD_MS);
}

export function getWorkerRuntimeSnapshot(payload: unknown): WorkerRuntimeSnapshot | null {
  if (!isRecord(payload) || !Array.isArray(payload.services)) {
    return null;
  }

  const services = payload.services
    .map((value) => {
      if (!isRecord(value) || typeof value.name !== "string") {
        return null;
      }

      return {
        name: value.name as RuntimeServiceName,
        enabled: value.enabled === true,
        running: value.running === true,
        targetVersion: typeof value.targetVersion === "string" ? value.targetVersion : null,
        actualVersion: typeof value.actualVersion === "string" ? value.actualVersion : null,
        upgradeAvailable: value.upgradeAvailable === true
      };
    })
    .filter((item): item is WorkerRuntimeService => item !== null);

  const upgrade = isRecord(payload.upgrade) ? payload.upgrade : null;

  return {
    services,
    upgrade: {
      status:
        upgrade?.status === "running" || upgrade?.status === "failed" || upgrade?.status === "idle"
          ? upgrade.status
          : "idle",
      startedAt: typeof upgrade?.startedAt === "number" ? new Date(upgrade.startedAt).toISOString() : null,
      finishedAt: typeof upgrade?.finishedAt === "number" ? new Date(upgrade.finishedAt).toISOString() : null,
      error: typeof upgrade?.error === "string" ? upgrade.error : null
    }
  };
}

export function getRuntimeServiceLabel(name: RuntimeServiceName): string {
  switch (name) {
    case "omnirush-server":
      return "OmniRush.ai server";
    case "opencode":
      return "OpenCode";
  }
}

function getBillingPrice(value: unknown): BillingPrice | null {
  if (!isRecord(value)) {
    return null;
  }

  return {
    amount: typeof value.amount === "number" ? value.amount : null,
    currency: typeof value.currency === "string" ? value.currency : null,
    recurringInterval: typeof value.recurringInterval === "string" ? value.recurringInterval : null,
    recurringIntervalCount: typeof value.recurringIntervalCount === "number" ? value.recurringIntervalCount : null
  };
}

function getBillingSubscription(value: unknown): BillingSubscription | null {
  if (!isRecord(value) || typeof value.id !== "string") {
    return null;
  }

  return {
    id: value.id,
    status: typeof value.status === "string" ? value.status : "unknown",
    amount: typeof value.amount === "number" ? value.amount : null,
    currency: typeof value.currency === "string" ? value.currency : null,
    recurringInterval: typeof value.recurringInterval === "string" ? value.recurringInterval : null,
    recurringIntervalCount: typeof value.recurringIntervalCount === "number" ? value.recurringIntervalCount : null,
    currentPeriodStart: typeof value.currentPeriodStart === "string" ? value.currentPeriodStart : null,
    currentPeriodEnd: typeof value.currentPeriodEnd === "string" ? value.currentPeriodEnd : null,
    cancelAtPeriodEnd: value.cancelAtPeriodEnd === true,
    canceledAt: typeof value.canceledAt === "string" ? value.canceledAt : null,
    endedAt: typeof value.endedAt === "string" ? value.endedAt : null
  };
}

function getBillingInvoice(value: unknown): BillingInvoice | null {
  if (!isRecord(value) || typeof value.id !== "string") {
    return null;
  }

  return {
    id: value.id,
    createdAt: typeof value.createdAt === "string" ? value.createdAt : null,
    status: typeof value.status === "string" ? value.status : "unknown",
    totalAmount: typeof value.totalAmount === "number" ? value.totalAmount : null,
    currency: typeof value.currency === "string" ? value.currency : null,
    invoiceNumber: typeof value.invoiceNumber === "string" ? value.invoiceNumber : null,
    invoiceUrl: typeof value.invoiceUrl === "string" ? value.invoiceUrl : null
  };
}

export function getBillingSummary(payload: unknown): BillingSummary | null {
  if (!isRecord(payload) || !isRecord(payload.billing)) {
    return null;
  }

  const billing = isRecord(payload.billing.polar) ? payload.billing.polar : payload.billing;
  const featureGateEnabled = billing.featureGateEnabled;
  const hasActivePlan = billing.hasActivePlan;
  const checkoutRequired = billing.checkoutRequired;

  if (
    typeof featureGateEnabled !== "boolean" ||
    typeof hasActivePlan !== "boolean" ||
    typeof checkoutRequired !== "boolean"
  ) {
    return null;
  }

  return {
    featureGateEnabled,
    hasActivePlan,
    checkoutRequired,
    portalUrl: typeof billing.portalUrl === "string" ? billing.portalUrl : null,
    price: getBillingPrice(billing.price),
    subscription: getBillingSubscription(billing.subscription),
    invoices: Array.isArray(billing.invoices)
      ? billing.invoices
          .map((item) => getBillingInvoice(item))
          .filter((item): item is BillingInvoice => item !== null)
      : [],
    productId: typeof billing.productId === "string" ? billing.productId : null,
    benefitId: typeof billing.benefitId === "string" ? billing.benefitId : null
  };
}

function parseWorkerListItem(value: unknown): WorkerListItem | null {
  if (!isRecord(value)) {
    return null;
  }

  const workerId = value.id;
  const workerName = value.name;
  if (typeof workerId !== "string" || typeof workerName !== "string") {
    return null;
  }

  const instance = isRecord(value.instance) ? value.instance : null;
  const createdAt = typeof value.createdAt === "string" ? value.createdAt : null;

  return {
    workerId,
    workerName,
    status: getEffectiveWorkerStatus(value.status, instance),
    instanceUrl: getDurableWorkerInstanceUrl(instance),
    provider: instance && typeof instance.provider === "string" ? instance.provider : null,
    isMine: value.isMine === true,
    createdAt
  };
}

export function getWorkersList(payload: unknown): WorkerListItem[] {
  if (!isRecord(payload) || !Array.isArray(payload.workers)) {
    return [];
  }

  const rows: WorkerListItem[] = [];
  for (const item of payload.workers) {
    const parsed = parseWorkerListItem(item);
    if (parsed) {
      rows.push(parsed);
    }
  }

  return rows;
}

function parseWorkflowRun(value: unknown): WorkflowRun | null {
  if (
    !isRecord(value)
    || typeof value.id !== "string"
    || typeof value.source !== "string"
    || (value.status !== "succeeded" && value.status !== "failed")
    || typeof value.toolCallCount !== "number"
    || typeof value.durationMs !== "number"
    || typeof value.startedAt !== "string"
    || typeof value.finishedAt !== "string"
    || typeof value.createdAt !== "string"
  ) {
    return null;
  }

  const toolCalls = Array.isArray(value.toolCalls)
    ? value.toolCalls.flatMap((call) =>
        isRecord(call) && typeof call.name === "string" ? [{ name: call.name }] : [],
      )
    : [];
  const workflow = workflowRunPreviewSchema.safeParse(value.workflow);

  return {
    id: value.id,
    workflow: workflow.success ? workflow.data : null,
    source: value.source,
    status: value.status,
    errorKind: typeof value.errorKind === "string" ? value.errorKind : null,
    errorMessage: typeof value.errorMessage === "string" ? value.errorMessage : null,
    toolCallCount: value.toolCallCount,
    toolCalls,
    durationMs: value.durationMs,
    startedAt: value.startedAt,
    finishedAt: value.finishedAt,
    createdAt: value.createdAt,
    orgMembershipId: typeof value.orgMembershipId === "string" ? value.orgMembershipId : null,
  };
}

export function getWorkflowRuns(payload: unknown): WorkflowRun[] {
  if (!isRecord(payload) || !Array.isArray(payload.runs)) {
    return [];
  }
  return payload.runs.flatMap((run) => {
    const parsed = parseWorkflowRun(run);
    return parsed ? [parsed] : [];
  });
}

export function getWorkerStatusMeta(status: string): { label: string; bucket: WorkerStatusBucket } {
  const normalized = status.trim().toLowerCase();

  if (normalized === "healthy" || normalized === "ready") {
    return { label: "Ready", bucket: "ready" };
  }

  if (normalized === "provisioning" || normalized === "starting") {
    return { label: "Starting", bucket: "starting" };
  }

  if (normalized === "failed" || normalized === "suspended" || normalized === "stopped") {
    return { label: "Needs attention", bucket: "attention" };
  }

  return { label: "Unknown", bucket: "other" };
}

export function getWorkerStatusCopy(status: string): string {
  const normalized = status.trim().toLowerCase();
  switch (normalized) {
    case "provisioning":
    case "starting":
      return "Starting...";
    case "healthy":
    case "ready":
      return "Ready to connect.";
    case "failed":
      return "Worker failed to start.";
    case "suspended":
    case "stopped":
      return "Worker is suspended.";
    default:
      return "Worker status unknown.";
  }
}

function getEffectiveWorkerStatus(workerStatus: unknown, instance: Record<string, unknown> | null): string {
  const normalizedWorkerStatus = typeof workerStatus === "string" ? workerStatus : "unknown";
  const normalized = normalizedWorkerStatus.trim().toLowerCase();
  const instanceStatus = instance && typeof instance.status === "string" ? instance.status.trim().toLowerCase() : null;

  if (!instanceStatus) {
    return normalizedWorkerStatus;
  }

  if (normalized === "provisioning" || normalized === "starting") {
    return instanceStatus;
  }

  return normalizedWorkerStatus;
}

export function isWorkerLaunch(value: unknown): value is WorkerLaunch {
  if (!isRecord(value)) {
    return false;
  }

  return (
    typeof value.workerId === "string" &&
    typeof value.workerName === "string" &&
    typeof value.status === "string" &&
    (typeof value.provider === "string" || value.provider === null) &&
    (typeof value.instanceUrl === "string" || value.instanceUrl === null) &&
    (typeof value.omnirushUrl === "string" || value.omnirushUrl === null || typeof value.omnirushUrl === "undefined") &&
    (typeof value.previewOmniRushUrl === "string" || value.previewOmniRushUrl === null || typeof value.previewOmniRushUrl === "undefined") &&
    (typeof value.previewExpiresAt === "string" || value.previewExpiresAt === null || typeof value.previewExpiresAt === "undefined") &&
    (typeof value.workspaceId === "string" || value.workspaceId === null || typeof value.workspaceId === "undefined") &&
    (typeof value.clientToken === "string" || value.clientToken === null) &&
    (typeof value.ownerToken === "string" || value.ownerToken === null || typeof value.ownerToken === "undefined") &&
    (typeof value.hostToken === "string" || value.hostToken === null)
  );
}

export function listItemToWorker(item: WorkerListItem, current: WorkerLaunch | null = null): WorkerLaunch {
  return {
    workerId: item.workerId,
    workerName: item.workerName,
    status: item.status,
    provider: item.provider,
    instanceUrl: item.instanceUrl,
    omnirushUrl: current?.workerId === item.workerId ? current.omnirushUrl ?? item.instanceUrl : item.instanceUrl,
    previewOmniRushUrl: current?.workerId === item.workerId ? current.previewOmniRushUrl ?? null : null,
    previewExpiresAt: current?.workerId === item.workerId ? current.previewExpiresAt ?? null : null,
    workspaceId: current?.workerId === item.workerId ? current.workspaceId : null,
    clientToken: current?.workerId === item.workerId ? current.clientToken : null,
    ownerToken: current?.workerId === item.workerId ? current.ownerToken : null,
    hostToken: current?.workerId === item.workerId ? current.hostToken : null
  };
}

function normalizeUrl(value: string): string {
  return value.trim().replace(/\/+$/, "");
}

export function parseWorkspaceIdFromUrl(value: string): string | null {
  const normalized = normalizeUrl(value);
  if (!normalized) {
    return null;
  }

  try {
    const url = new URL(normalized);
    const segments = url.pathname.split("/").filter(Boolean);
    const last = segments[segments.length - 1] ?? "";
    const prev = segments[segments.length - 2] ?? "";
    if (prev !== "w" || !last) {
      return null;
    }
    return decodeURIComponent(last);
  } catch {
    const match = normalized.match(/\/w\/([^/?#]+)/);
    if (!match?.[1]) {
      return null;
    }
    try {
      return decodeURIComponent(match[1]);
    } catch {
      return match[1];
    }
  }
}

function buildWorkspaceUrl(instanceUrl: string, workspaceId: string): string {
  return `${normalizeUrl(instanceUrl)}/w/${encodeURIComponent(workspaceId)}`;
}

export function buildOmniRushDeepLink(
  omnirushUrl: string | null,
  accessToken: string | null,
  workerId: string | null,
  workerName: string | null
): string | null {
  if (!omnirushUrl || !accessToken) {
    return null;
  }

  const params = new URLSearchParams({
    omnirushHostUrl: omnirushUrl,
    omnirushToken: accessToken,
    source: "omnirush-web"
  });

  if (workerId) {
    params.set("workerId", workerId);
  }

  if (workerName) {
    params.set("workerName", workerName);
  }

  return `omnirush://connect-remote?${params.toString()}`;
}

export function buildOmniRushAppConnectUrl(
  appConnectBaseUrl: string,
  omnirushUrl: string | null,
  accessToken: string | null,
  workerId: string | null,
  workerName: string | null,
  options?: { autoConnect?: boolean }
): string | null {
  if (!appConnectBaseUrl || !omnirushUrl || !accessToken) {
    return null;
  }

  let connectUrl: URL;
  try {
    connectUrl = new URL(appConnectBaseUrl);
  } catch {
    return null;
  }

  const normalizedPath = connectUrl.pathname.replace(/\/+$/, "");
  if (!normalizedPath || normalizedPath === "/") {
    connectUrl.pathname = "/connect-remote";
  } else {
    const pathSegments = normalizedPath.split("/").filter(Boolean);
    const lastSegment = (pathSegments[pathSegments.length - 1] ?? "").toLowerCase();
    connectUrl.pathname = lastSegment === "connect-remote" ? normalizedPath : `${normalizedPath}/connect-remote`;
  }

  connectUrl.searchParams.set("omnirushHostUrl", omnirushUrl);
  connectUrl.searchParams.set("omnirushToken", accessToken);
  if (options?.autoConnect) {
    connectUrl.searchParams.set("autoConnect", "1");
  }
  connectUrl.searchParams.set("source", "omnirush-web");

  if (workerId) {
    connectUrl.searchParams.set("workerId", workerId);
  }

  if (workerName) {
    connectUrl.searchParams.set("workerName", workerName);
  }

  return connectUrl.toString();
}

function parseWorkspaceIdFromWorkspacesPayload(payload: unknown): string | null {
  if (!isRecord(payload) || !Array.isArray(payload.items)) {
    return null;
  }

  const activeId = typeof payload.activeId === "string" ? payload.activeId : null;
  if (activeId && payload.items.some((item) => isRecord(item) && item.id === activeId)) {
    return activeId;
  }

  for (const item of payload.items) {
    if (isRecord(item) && typeof item.id === "string" && item.id.trim()) {
      return item.id;
    }
  }

  return null;
}

async function requestAbsoluteJson(url: string, init: RequestInit = {}, timeoutMs = 12000) {
  const headers = new Headers(init.headers);
  headers.set("Accept", "application/json");

  const shouldAttachTimeout = !init.signal && timeoutMs > 0;
  const timeoutController = shouldAttachTimeout ? new AbortController() : null;
  const timeoutHandle = timeoutController
    ? setTimeout(() => {
        timeoutController.abort();
      }, timeoutMs)
    : null;

  let response: Response;
  try {
    response = await fetch(url, {
      ...init,
      headers,
      credentials: "omit",
      signal: init.signal ?? timeoutController?.signal
    });
  } finally {
    if (timeoutHandle) {
      clearTimeout(timeoutHandle);
    }
  }

  const text = await response.text();
  let payload: unknown = null;
  if (text) {
    try {
      payload = JSON.parse(text);
    } catch {
      payload = text;
    }
  }

  return { response, payload };
}

export async function resolveOmniRushWorkspaceUrl(instanceUrl: string, accessToken: string): Promise<{ workspaceId: string; omnirushUrl: string } | null> {
  const baseUrl = normalizeUrl(instanceUrl);
  const token = accessToken.trim();
  if (!baseUrl || !token) {
    return null;
  }

  const mountedWorkspaceId = parseWorkspaceIdFromUrl(baseUrl);
  if (mountedWorkspaceId) {
    return {
      workspaceId: mountedWorkspaceId,
      omnirushUrl: baseUrl
    };
  }

  const { response, payload } = await requestAbsoluteJson(`${baseUrl}/workspaces`, {
    method: "GET",
    headers: {
      Authorization: `Bearer ${token}`
    }
  });

  if (!response.ok) {
    return null;
  }

  const workspaceId = parseWorkspaceIdFromWorkspacesPayload(payload);
  if (!workspaceId) {
    return null;
  }

  return {
    workspaceId,
    omnirushUrl: buildWorkspaceUrl(baseUrl, workspaceId)
  };
}

export async function requestJson(path: string, init: RequestInit = {}, timeoutMs = 30000) {
  const headers = new Headers(init.headers);
  headers.set("Accept", "application/json");

  if (init.body && !(init.body instanceof FormData) && !headers.has("Content-Type")) {
    headers.set("Content-Type", "application/json");
  }

  if (!headers.has("Authorization") && !path.startsWith("/api/auth/") && typeof window !== "undefined") {
    const token = window.localStorage.getItem(AUTH_TOKEN_STORAGE_KEY)?.trim() ?? "";
    if (token) headers.set("Authorization", "Bearer " + token);
  }

  const orgScope = getRequestOrgScope();
  if (orgScope && !headers.has(ORG_SCOPE_HEADER) && shouldPinOrgScopePath(path)) {
    headers.set(ORG_SCOPE_HEADER, orgScope);
  }

  const shouldAttachTimeout = !init.signal && timeoutMs > 0;
  const timeoutController = shouldAttachTimeout ? new AbortController() : null;
  let didReachDashboardDeadline = false;
  const timeoutHandle = timeoutController
    ? setTimeout(() => {
        didReachDashboardDeadline = true;
        timeoutController.abort();
      }, timeoutMs)
    : null;

  let response: Response;
  try {
    if (typeof window !== "undefined") {
      await getRuntimeConfig();
    }
    const endpoint = path.startsWith("/api/auth/") ? path : denApiEndpoint(path);
    response = await fetch(endpoint, {
      ...init,
      headers,
      credentials: init.credentials ?? denApiCredentials(endpoint, path),
      signal: init.signal ?? timeoutController?.signal
    });
    if (path === "/v1/me" && response.status === 401 && typeof window !== "undefined") {
      await fetch("/api/auth/clear-session-cookie", {
        method: "POST",
        credentials: "include"
      }).catch(() => null);
    }
  } catch (error) {
    // Only the deadline created by this helper becomes a timeout. An abort
    // supplied by a caller becomes a distinct cancellation error.
    if (didReachDashboardDeadline) {
      throw new DenRequestTimeoutError(timeoutMs, error);
    }
    if (init.signal?.aborted && error instanceof Error && error.name === "AbortError") {
      throw new DenRequestCanceledError(error);
    }
    throw error;
  } finally {
    if (timeoutHandle) {
      clearTimeout(timeoutHandle);
    }
  }

  const text = await response.text();
  let payload: unknown = null;

  if (text) {
    try {
      payload = JSON.parse(text);
    } catch {
      payload = text;
    }
  }

  const reauthError = getReauthRequiredError(payload, response);
  if (reauthError) {
    throw reauthError;
  }

  return { response, payload, text };
}