Watch
0
0
Fork
You've already forked hyperhive
0

swarm ui: the create-agent name field says why a name is refused, beside the field

A name with an uppercase letter greyed out next with only the footer to say why. The text field now takes an error: it marks the field invalid, draws the reason under it and points aria-describedby at it. The create form passes it once something is typed, and nameProblem names the cause (uppercase first, then length, then other characters) instead of one catch-all rule. Next stays disabled, with the footer hint as before. The swarm UI doc says so.
This commit is contained in:
iris 2026-11-20 20:30:01 +01:00 • committed by mara
commit 94969b552e
6 changed files with 39 additions and 5 deletions

View file

@ -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 tiles on the broadcast page, with **none** first; an archetype with no icon
shows the dimmed hyperhive mark. shows the dimmed hyperhive mark.
**next** waits for a valid name (1-63 lowercase letters, digits and hyphens) **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 2. **instructions**: the text the agent's `AGENTS.md` starts with, in a box
of its own. of its own.
3. **permissions**: one checkbox per permission, starting from the default set 3. **permissions**: one checkbox per permission, starting from the default set

View file

@ -73,6 +73,7 @@ import {
import { import {
NAME_PATTERN, NAME_PATTERN,
lastReachableStep, lastReachableStep,
nameProblem,
nextStep, nextStep,
previousStep, previousStep,
STEP_TITLES, STEP_TITLES,
@ -391,6 +392,7 @@ export function CreateAgentForm({
pattern={NAME_PATTERN} pattern={NAME_PATTERN}
title="1-63 chars: lowercase letters, digits, hyphens" title="1-63 chars: lowercase letters, digits, hyphens"
required required
error={name === "" ? null : nameProblem(name)}
onInput={setName} onInput={setName}
/> />
<SelectField <SelectField

View file

@ -28,9 +28,16 @@ export const STEP_TITLES: Record<Step, string> = {
export const NAME_PATTERN = "[a-z0-9\\-]{1,63}"; export const NAME_PATTERN = "[a-z0-9\\-]{1,63}";
export function nameProblem(name: string): string | null { export function nameProblem(name: string): string | null {
return new RegExp(`^${NAME_PATTERN}$`).test(name) if (new RegExp(`^${NAME_PATTERN}$`).test(name)) return null;
? null // Said for the likeliest slip first, so the field can name the cause.
: "the name is 1-63 characters: lowercase letters, digits and hyphens"; 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. */ /** The step after `step`, or `step` itself on the last. */

View file

@ -45,6 +45,10 @@
box-sizing: border-box; box-sizing: border-box;
min-height: 2.75em; 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. */ /* A label that stays for assistive tech and is off the screen. */
.ui-form-field-label-hidden { .ui-form-field-label-hidden {
position: absolute; position: absolute;

View file

@ -4,6 +4,7 @@
// mode — promote that the day a real caller needs one, same "don't // mode — promote that the day a real caller needs one, same "don't
// build ahead of a caller" rule the rest of `ui/` follows. // build ahead of a caller" rule the rest of `ui/` follows.
import { FormField } from "../form-field/FormField.js"; import { FormField } from "../form-field/FormField.js";
import { Hint } from "../hint/Hint.js";
/** One value the field offers to complete to. */ /** One value the field offers to complete to. */
export interface TextFieldSuggestion { export interface TextFieldSuggestion {
@ -26,6 +27,7 @@ export function TextField({
suggestions, suggestions,
onKeyDown, onKeyDown,
combobox, combobox,
error,
}: { }: {
id: string; id: string;
label: string; label: string;
@ -43,6 +45,10 @@ export function TextField({
suggestions?: TextFieldSuggestion[]; suggestions?: TextFieldSuggestion[];
/** Key presses in the field, for a caller that draws its own menu. */ /** Key presses in the field, for a caller that draws its own menu. */
onKeyDown?: (e: KeyboardEvent) => void; 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. */ /** Marks the field as the text box of a menu the caller draws itself. */
combobox?: { combobox?: {
expanded: boolean; expanded: boolean;
@ -53,11 +59,15 @@ export function TextField({
}; };
}) { }) {
const listId = `${id}-suggestions`; const listId = `${id}-suggestions`;
const errorId = `${id}-error`;
const invalid = error !== undefined && error !== null;
return ( return (
<FormField label={label} htmlFor={id} hideLabel={hideLabel}> <FormField label={label} htmlFor={id} hideLabel={hideLabel}>
<input <input
id={id} id={id}
class="ui-form-control" class={"ui-form-control" + (invalid ? " ui-form-control-invalid" : "")}
aria-invalid={invalid ? true : undefined}
aria-describedby={invalid ? errorId : undefined}
// preact 11 types `type` as a union that ties each kind to its own // preact 11 types `type` as a union that ties each kind to its own
// `list` and `role` attributes; the field takes any of its kinds and // `list` and `role` attributes; the field takes any of its kinds and
// passes `list` only for suggestions, so the union is asserted here. // passes `list` only for suggestions, so the union is asserted here.
@ -77,6 +87,11 @@ export function TextField({
onKeyDown={onKeyDown} onKeyDown={onKeyDown}
onInput={(e) => onInput((e.target as HTMLInputElement).value)} onInput={(e) => onInput((e.target as HTMLInputElement).value)}
/> />
{invalid ? (
<Hint tone="error" id={errorId}>
{error}
</Hint>
) : null}
{suggestions ? ( {suggestions ? (
<datalist id={listId}> <datalist id={listId}>
{suggestions.map((s) => ( {suggestions.map((s) => (

View file

@ -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("a".repeat(64)), null);
assert.notEqual(nameProblem("Ruth"), null); assert.notEqual(nameProblem("Ruth"), null);
assert.notEqual(nameProblem("ru th"), 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", () => { test("identity needs a valid name and a hive before next", () => {