add a default archetype, preselected by the clients
This commit is contained in:
parent
a74d23d313
commit
8f7c45d40f
4 changed files with 253 additions and 47 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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),
|
||||
];
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 &&
|
||||
|
|
|
|||
|
|
@ -5,6 +5,8 @@ import { test } from "node:test";
|
|||
import {
|
||||
archetypeKey,
|
||||
iconSrc,
|
||||
openingKey,
|
||||
pickerTiles,
|
||||
prefill,
|
||||
requestArchetype,
|
||||
} from "../src/pages/CreateAgentArchetype.ts";
|
||||
|
|
@ -81,7 +83,7 @@ test("the key tells sources apart", () => {
|
|||
);
|
||||
});
|
||||
|
||||
test("a picked archetype is sent by source and name, none is left out", () => {
|
||||
test("a picked archetype is sent by source and name, none (a custom agent) is left out", () => {
|
||||
const reviewer = {
|
||||
source: "hyperhive",
|
||||
name: "reviewer",
|
||||
|
|
@ -102,3 +104,38 @@ test("an icon is a data URL of the SVG text, and none without text", () => {
|
|||
assert.ok(src.startsWith("data:image/svg+xml;charset=utf-8,%3Csvg"), src);
|
||||
assert.ok(!src.includes("<"), "the markup is percent-encoded");
|
||||
});
|
||||
|
||||
const listed = (...names) =>
|
||||
names.map((name) => ({ source: "hyperhive", name, description: "d" }));
|
||||
|
||||
test("the form opens on default once the list has it, and on none otherwise", () => {
|
||||
assert.equal(openingKey(null), null);
|
||||
assert.equal(
|
||||
openingKey(listed("reviewer", "default")),
|
||||
archetypeKey({ source: "hyperhive", name: "default", description: "d" }),
|
||||
);
|
||||
// An older controller or builder lists no default: a custom agent, as before.
|
||||
assert.equal(openingKey(listed("reviewer", "ruth")), "");
|
||||
assert.equal(openingKey([]), "");
|
||||
// A `default` from another source is not the built-in one.
|
||||
assert.equal(
|
||||
openingKey([{ source: "other", name: "default", description: "d" }]),
|
||||
"",
|
||||
);
|
||||
});
|
||||
|
||||
test("the tiles are default, then none, then the rest in list order", () => {
|
||||
const names = (list) => pickerTiles(list).map((t) => t.name);
|
||||
assert.deepEqual(names(listed("reviewer", "default", "ruth")), [
|
||||
"default",
|
||||
"none",
|
||||
"reviewer",
|
||||
"ruth",
|
||||
]);
|
||||
assert.deepEqual(names(listed("reviewer", "ruth")), [
|
||||
"none",
|
||||
"reviewer",
|
||||
"ruth",
|
||||
]);
|
||||
assert.equal(pickerTiles(listed("default"))[1].key, "");
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue