import type { TerminalSummary } from "@t3tools/contracts"; /** Human-readable label for a terminal tab; matches mobile and web sidebars. */ export function getTerminalLabel(terminalId: string): string { const numericSuffix = /^term(?:inal)?-(\d+)$/i.exec(terminalId)?.[1]; if (numericSuffix) { return `Terminal ${numericSuffix}`; } return terminalId; } /** Prefer server summary label when present; otherwise fall back to `getTerminalLabel`. */ export function resolveTerminalSessionLabel( terminalId: string, summary: Pick | null | undefined, ): string { const trimmed = summary?.label?.trim(); if (trimmed && trimmed.length > 0) { return trimmed; } return getTerminalLabel(terminalId); } /** * Client-side terminal id allocator. Ids are ALWAYS chosen by the client and sent explicitly * on every `terminal.open` / `terminal.attach` call — the server never allocates. * * Returns the lowest unused `term-N` id (starting at `term-1`), skipping any ids already in * `existingTerminalIds`. */ export function nextTerminalId(existingTerminalIds: ReadonlyArray): string { const usedIds = new Set(existingTerminalIds.filter((id) => id.trim().length > 0)); let nextIndex = 1; while (usedIds.has(`term-${nextIndex}`)) { nextIndex += 1; } return `term-${nextIndex}`; }