diff --git a/docs/4-implementation/web-ui/swarm-ui.md b/docs/4-implementation/web-ui/swarm-ui.md index 4f12df2a7..d60ebed38 100644 --- a/docs/4-implementation/web-ui/swarm-ui.md +++ b/docs/4-implementation/web-ui/swarm-ui.md @@ -1017,7 +1017,9 @@ order with **next** and can step back through with **back** or the step tabs: tiles on the broadcast page, with **none** first; an archetype with no icon shows the dimmed hyperhive mark. **next** waits for a valid name (1-63 lowercase letters, digits and hyphens) - and a hive. + and a hive. As soon as the operator types, the name field says what's wrong + with the name, beside the field: an uppercase letter, a length over 63, or + another character. 2. **instructions**: the text the agent's `AGENTS.md` starts with, in a box of its own. 3. **permissions**: one checkbox per permission, starting from the default set diff --git a/frontend/packages/swarm-ui/src/pages/CreateAgentForm.tsx b/frontend/packages/swarm-ui/src/pages/CreateAgentForm.tsx index 4f66184a3..b0c3284b0 100644 --- a/frontend/packages/swarm-ui/src/pages/CreateAgentForm.tsx +++ b/frontend/packages/swarm-ui/src/pages/CreateAgentForm.tsx @@ -73,6 +73,7 @@ import { import { NAME_PATTERN, lastReachableStep, + nameProblem, nextStep, previousStep, STEP_TITLES, @@ -391,6 +392,7 @@ export function CreateAgentForm({ pattern={NAME_PATTERN} title="1-63 chars: lowercase letters, digits, hyphens" required + error={name === "" ? null : nameProblem(name)} onInput={setName} /> = { export const NAME_PATTERN = "[a-z0-9\\-]{1,63}"; export function nameProblem(name: string): string | null { - return new RegExp(`^${NAME_PATTERN}$`).test(name) - ? null - : "the name is 1-63 characters: lowercase letters, digits and hyphens"; + if (new RegExp(`^${NAME_PATTERN}$`).test(name)) return null; + // Said for the likeliest slip first, so the field can name the cause. + if (/[A-Z]/.test(name)) { + return "uppercase letters aren't allowed, use lowercase"; + } + if (name.length > 63) return "the name is at most 63 characters"; + if (/[^a-z0-9-]/.test(name)) { + return "only lowercase letters, digits and hyphens are allowed"; + } + return "the name is 1-63 characters: lowercase letters, digits and hyphens"; } /** The step after `step`, or `step` itself on the last. */ diff --git a/frontend/packages/swarm-ui/src/ui/form-field/FormField.css b/frontend/packages/swarm-ui/src/ui/form-field/FormField.css index 2b936f9b1..ba33cb78b 100644 --- a/frontend/packages/swarm-ui/src/ui/form-field/FormField.css +++ b/frontend/packages/swarm-ui/src/ui/form-field/FormField.css @@ -45,6 +45,10 @@ box-sizing: border-box; min-height: 2.75em; } +/* A control whose value is not acceptable; the reason is a `Hint` below it. */ +.ui-form-control-invalid { + border-color: var(--red); +} /* A label that stays for assistive tech and is off the screen. */ .ui-form-field-label-hidden { position: absolute; diff --git a/frontend/packages/swarm-ui/src/ui/text-field/TextField.tsx b/frontend/packages/swarm-ui/src/ui/text-field/TextField.tsx index 4570b3523..9a2e8ffbb 100644 --- a/frontend/packages/swarm-ui/src/ui/text-field/TextField.tsx +++ b/frontend/packages/swarm-ui/src/ui/text-field/TextField.tsx @@ -4,6 +4,7 @@ // mode — promote that the day a real caller needs one, same "don't // build ahead of a caller" rule the rest of `ui/` follows. import { FormField } from "../form-field/FormField.js"; +import { Hint } from "../hint/Hint.js"; /** One value the field offers to complete to. */ export interface TextFieldSuggestion { @@ -26,6 +27,7 @@ export function TextField({ suggestions, onKeyDown, combobox, + error, }: { id: string; label: string; @@ -43,6 +45,10 @@ export function TextField({ suggestions?: TextFieldSuggestion[]; /** Key presses in the field, for a caller that draws its own menu. */ onKeyDown?: (e: KeyboardEvent) => void; + /** Why the value is not acceptable, shown under the field and marked on it. + * Null or absent while it is fine, so a caller can leave it out until the + * operator has typed something. */ + error?: string | null; /** Marks the field as the text box of a menu the caller draws itself. */ combobox?: { expanded: boolean; @@ -53,11 +59,15 @@ export function TextField({ }; }) { const listId = `${id}-suggestions`; + const errorId = `${id}-error`; + const invalid = error !== undefined && error !== null; return ( onInput((e.target as HTMLInputElement).value)} /> + {invalid ? ( + + {error} + + ) : null} {suggestions ? ( {suggestions.map((s) => ( diff --git a/frontend/packages/swarm-ui/test/create-agent-steps.test.mjs b/frontend/packages/swarm-ui/test/create-agent-steps.test.mjs index 95bf8fe91..7a88c8a10 100644 --- a/frontend/packages/swarm-ui/test/create-agent-steps.test.mjs +++ b/frontend/packages/swarm-ui/test/create-agent-steps.test.mjs @@ -41,6 +41,10 @@ test("a name is 1-63 lowercase letters, digits and hyphens", () => { assert.notEqual(nameProblem("a".repeat(64)), null); assert.notEqual(nameProblem("Ruth"), null); assert.notEqual(nameProblem("ru th"), null); + // The reason names the cause, uppercase first. + assert.match(nameProblem("Ruth"), /uppercase/); + assert.match(nameProblem("ru th"), /lowercase letters, digits and hyphens/); + assert.match(nameProblem("a".repeat(64)), /63/); }); test("identity needs a valid name and a hive before next", () => {