Watch
0
0
Fork
You've already forked hyperhive
0

add a default archetype, preselected by the clients

This commit is contained in:
atlas 2026-11-27 17:41:52 +01:00 • committed by mara
commit 8f7c45d40f
3 changed files with 253 additions and 47 deletions

View file

@ -1,10 +1,11 @@
// <ArchetypePicker> — the create form's archetype choice, drawn like the
// broadcast screen's agent picker: one tile per choice with its icon and name,
// where the broadcast screen has one per agent. One choice at a time, so each
// tile is a radio button. The first tile is "none", an agent with no archetype.
// tile is a radio button. The first tiles are `default` (when the swarm lists
// it) and "none", an agent with no archetype.
import {
archetypeKey,
iconSrc,
pickerTiles,
type Archetype,
} from "./CreateAgentArchetype.js";
import { FormField } from "../ui/form-field/FormField.js";
@ -35,14 +36,7 @@ export function ArchetypePicker({
onChange: (key: string) => void;
disabled?: boolean;
}) {
const tiles: { key: string; name: string; svg?: string }[] = [
{ key: "", name: "none" },
...archetypes.map((a) => ({
key: archetypeKey(a),
name: a.name,
svg: a.icon,
})),
];
const tiles = pickerTiles(archetypes);
return (
<FormField label="archetype" labelId={`${id}-label`}>
<div

View file

@ -101,3 +101,43 @@ export function iconSrc(svg: string | undefined): string | null {
if (svg === undefined || svg.trim() === "") return null;
return `data:image/svg+xml;charset=utf-8,${encodeURIComponent(svg)}`;
}
/** The archetype with the default config and no config repo. */
export const DEFAULT_ARCHETYPE = { source: "hyperhive", name: "default" };
/**
* The `archetypeKey` the form opens on: `default` when the list has it, else
* `""`, a custom agent, which is what a controller or builder that predates
* `default` gets.
* `null` until the list has loaded.
*/
export function openingKey(list: Archetype[] | null): string | null {
if (list === null) return null;
const def = findArchetype(list, DEFAULT_ARCHETYPE);
return def ? archetypeKey(def) : "";
}
/** One picker tile: `key` is the `archetypeKey`, `""` for a custom agent. */
export interface Tile {
key: string;
name: string;
svg?: string;
}
/**
* The picker's tiles: `default` first when the list has it, then `none` (a
* custom agent), then the rest in list order.
*/
export function pickerTiles(list: Archetype[]): Tile[] {
const def = findArchetype(list, DEFAULT_ARCHETYPE);
const tile = (a: Archetype): Tile => ({
key: archetypeKey(a),
name: a.name,
svg: a.icon,
});
return [
...(def ? [tile(def)] : []),
{ key: "", name: "none" },
...list.filter((a) => a !== def).map(tile),
];
}

View file

@ -24,8 +24,9 @@
// page-scoped input/button chrome — see that kit's own comments for why.
//
// Permissions: checkboxes from `GET /api/permissions`, on the default set or the
// picked archetype's; sent only when that differs from the default. A caller can
// open the form on an archetype with `initialArchetype`.
// picked archetype's; sent only when that differs from the default. The form opens
// on the `default` archetype when the swarm lists it, else on none (a custom
// agent); a caller can open it on another archetype with `initialArchetype`.
import { useEffect, useRef, useState } from "preact/hooks";
import { Link } from "wouter-preact";
import { ApiErrorPanel } from "@hive/shared/api-error-panel.js";
@ -60,6 +61,7 @@ import {
import {
archetypeKey,
findArchetype,
openingKey,
prefill,
requestArchetype,
type ArchetypeList,
@ -143,7 +145,7 @@ export function CreateAgentForm({
// Unticked: the server writes `false`, so the agent starts restricted.
const [internet, setInternet] = useState(false);
// `null` until `/api/archetypes` answers; `archetype` is the picked one's
// `archetypeKey`, `""` for none.
// `archetypeKey`, `""` for none (a custom agent).
const [archetypes, setArchetypes] = useState<ArchetypeList | null>(null);
const [archetypesError, setArchetypesError] = useState<ProblemDetails | null>(
null,
@ -214,15 +216,20 @@ export function CreateAgentForm({
setInference(p.inference);
}
// Opens on `initialArchetype` once the list and the permission catalogue are
// both in, and only once, so a later pick of "none" stays.
// Opens on `initialArchetype`, else on `default` when the list has it, once
// the list and the permission catalogue are both in, and only once, so a
// later pick of "none" stays.
const seeded = useRef(false);
useEffect(() => {
if (seeded.current || !initialArchetype || !archetypes || !catalogue)
return;
if (seeded.current || !archetypes || !catalogue) return;
seeded.current = true;
const found = findArchetype(archetypes.archetypes, initialArchetype);
if (found) pickArchetype(archetypeKey(found));
if (initialArchetype) {
const found = findArchetype(archetypes.archetypes, initialArchetype);
if (found) pickArchetype(archetypeKey(found));
return;
}
const key = openingKey(archetypes.archetypes);
if (key) pickArchetype(key);
}, [archetypes, catalogue]);
const unlisted =
initialArchetype !== undefined &&