Skip to content

A desktop coding agent with free access to frontier models.

TypeScript
5.2 KB
Raw Blame
import type { SettingsTab } from "../../app/types";

export function workspaceSessionRoute(workspaceId: string, sessionId?: string | null) {
  const workspace = encodeURIComponent(workspaceId.trim());
  const session = sessionId?.trim();
  return session
    ? `/workspace/${workspace}/session/${encodeURIComponent(session)}`
    : `/workspace/${workspace}/session`;
}

export function workspaceSettingsRoute(
  workspaceId: string,
  tab: SettingsTab | "extensions/mcp" | "extensions/plugins" | string = "general",
) {
  return `/workspace/${encodeURIComponent(workspaceId.trim())}/settings/${tab}`;
}

/**
 * Where closing Settings lands. The originating session is restored only when
 * Settings is still on the workspace it was opened from.
 */
export function settingsReturnRoute(
  selectedWorkspaceId: string,
  navigationWorkspaceId: string | null,
  navigationSessionId: string | null,
) {
  if (!selectedWorkspaceId) return "/session";
  const returnSessionId = navigationWorkspaceId === selectedWorkspaceId
    ? navigationSessionId
    : null;
  return workspaceSessionRoute(selectedWorkspaceId, returnSessionId);
}

/**
 * Settings entry from anywhere that only knows the current URL (native menu,
 * agent control actions). Keeps the workspace in the route and remembers the
 * open session in navigation state so closing Settings returns to exactly
 * where the user was, matching the in-app settings button.
 */
export function settingsNavigationFromPathname(pathname: string, tab: SettingsTab | string) {
  const match = /^\/workspace\/([^/]+)(?:\/session\/([^/]+))?/.exec(pathname);
  const workspaceId = match?.[1] ? decodeURIComponent(match[1]) : "";
  const sessionId = match?.[2] ? decodeURIComponent(match[2]) : null;
  return {
    to: workspaceId ? workspaceSettingsRoute(workspaceId, tab) : `/settings/${tab}`,
    state: { workspaceId, sessionId },
  };
}

/** The parallel-task board of a workspace's repository. */
export function workspaceTasksRoute(workspaceId: string) {
  return `/workspace/${encodeURIComponent(workspaceId.trim())}/tasks`;
}

export function automationsRoute() {
  return "/automations";
}

export function dashboardRoute() {
  return "/dashboard";
}

export function globalSettingsRoute(tab: SettingsTab) {
  return `/settings/${tab}`;
}

function extensionsRouteSuffix(path?: string | null) {
  const suffix = path?.trim().replace(/^\/+|\/+$/g, "") ?? "";
  return suffix ? `/${suffix}` : "";
}

export function workspaceExtensionsRoute(workspaceId: string, path?: string | null) {
  return `/workspace/${encodeURIComponent(workspaceId.trim())}/extensions${extensionsRouteSuffix(path)}`;
}

export function globalExtensionsRoute(path?: string | null) {
  return `/extensions${extensionsRouteSuffix(path)}`;
}

export function sessionIdForLegacyWorkspaceInference(
  routeWorkspaceId?: string | null,
  routeSessionId?: string | null,
): string | null {
  if (routeWorkspaceId?.trim()) return null;
  const sessionId = routeSessionId?.trim();
  return sessionId || null;
}

export function mergeWorkspaceRouteSession<T extends { id: string }>(sessions: T[], session: T): T[] {
  const index = sessions.findIndex((item) => item.id === session.id);
  if (index < 0) return [session, ...sessions];
  if (sessions[index] === session) return sessions;
  const next = [...sessions];
  next[index] = session;
  return next;
}

export function preserveWorkspaceRouteSession<T extends { id: string }>(
  fetched: T[],
  current: T[],
  sessionId?: string | null,
): T[] {
  const id = sessionId?.trim();
  if (!id || fetched.some((session) => session.id === id)) return fetched;
  const session = current.find((item) => item.id === id);
  return session ? mergeWorkspaceRouteSession(fetched, session) : fetched;
}

export function removeWorkspaceRouteSession<T extends { id: string }>(sessions: T[], sessionId: string): T[] {
  const next = sessions.filter((session) => session.id !== sessionId);
  return next.length === sessions.length ? sessions : next;
}

/**
 * Every workspace's list without a deleted session, whichever workspace
 * listed it, and the workspaces that did. The same object when none did.
 */
export function removeSessionFromWorkspaceLists<T extends { id: string }>(
  lists: Record<string, T[]>,
  sessionId: string,
): { lists: Record<string, T[]>; workspaceIds: string[] } {
  let next = lists;
  const workspaceIds: string[] = [];
  for (const [workspaceId, list] of Object.entries(lists)) {
    const nextList = removeWorkspaceRouteSession(list, sessionId);
    if (nextList === list) continue;
    if (next === lists) next = { ...lists };
    next[workspaceId] = nextList;
    workspaceIds.push(workspaceId);
  }
  return { lists: next, workspaceIds };
}

/** A fetched list without the sessions deleted since it was requested. */
export function withoutDeletedSessions<T extends { id?: string | null }>(sessions: T[], deleted: ReadonlySet<string>): T[] {
  if (deleted.size === 0) return sessions;
  const next = sessions.filter((session) => !deleted.has(String(session?.id ?? "")));
  return next.length === sessions.length ? sessions : next;
}

export function legacySessionRoute(sessionId?: string | null) {
  const session = sessionId?.trim();
  return session ? `/session/${encodeURIComponent(session)}` : "/session";
}