| 1 | /** |
| 2 | * An agent's handle: how it is mentioned (`@ship`). Pure, so it is tested |
| 3 | * on its own. |
| 4 | * |
| 5 | * Lowercase letters, digits and hyphens, 2 to 32 characters, with no |
| 6 | * leading, trailing or doubled hyphen (the same shape as a username, so a |
| 7 | * mention reads the same for both). Never a name nobody may register: |
| 8 | * `g1t` is the platform's own agent, and routes such as `settings` would |
| 9 | * read as links. |
| 10 | */ |
| 11 | import { isReservedName } from "../../../packages/contracts/src/names.ts"; |
| 12 | |
| 13 | /** |
| 14 | * Handles the site's agent pages would read as their own routes |
| 15 | * (`/<workspace>/agents/new`, `…/runs`, `…/fleet`), besides the names |
| 16 | * nobody may register. |
| 17 | */ |
| 18 | const ROUTES = new Set(["new", "runs", "fleet"]); |
| 19 | |
| 20 | const HANDLE = /^[a-z0-9](?:[a-z0-9]|-(?=[a-z0-9])){1,31}$/; |
| 21 | |
| 22 | export const HANDLE_RULE = "A handle is 2 to 32 lowercase letters, digits and single hyphens, starting and ending with a letter or digit."; |
| 23 | |
| 24 | export type HandleCheck = { ok: true; handle: string } | { ok: false; message: string }; |
| 25 | |
| 26 | /** `value` as a handle (trimmed, lowercased, a leading `@` dropped), or why it cannot be one. */ |
| 27 | export function checkHandle(value: unknown): HandleCheck { |
| 28 | if (typeof value !== "string") return { ok: false, message: HANDLE_RULE }; |
| 29 | const handle = value.trim().replace(/^@/, "").toLowerCase(); |
| 30 | if (!HANDLE.test(handle)) return { ok: false, message: HANDLE_RULE }; |
| 31 | if (isReservedName(handle) || ROUTES.has(handle)) return { ok: false, message: `@${handle} is reserved. Choose another handle.` }; |
| 32 | return { ok: true, handle }; |
| 33 | } |