diff --git a/web/src/modules/inferences/SiteProfilePage.tsx b/web/src/modules/inferences/SiteProfilePage.tsx index 4a9f86e..cd6601b 100644 --- a/web/src/modules/inferences/SiteProfilePage.tsx +++ b/web/src/modules/inferences/SiteProfilePage.tsx @@ -166,7 +166,7 @@ function ProfileView({ p }: { p: SiteProfile }) { { label: f("overrides"), value: kv(p.model?.overrides) }, ])} - {p.inputs && Object.keys(p.inputs).length ? ( + {p.inputs?.length ? ( @@ -175,10 +175,10 @@ function ProfileView({ p }: { p: SiteProfile }) { - {Object.entries(p.inputs).map(([k, x]) => ( - - {k} - {answerText(x)} + {p.inputs.map((s) => ( + + {s.key} + {answerText(s.value)} ))} diff --git a/web/src/modules/inferences/components/ProfileForm.test.tsx b/web/src/modules/inferences/components/ProfileForm.test.tsx index d562189..031fd7e 100644 --- a/web/src/modules/inferences/components/ProfileForm.test.tsx +++ b/web/src/modules/inferences/components/ProfileForm.test.tsx @@ -7,7 +7,7 @@ describe("variant inputs", () => { it("lists the profile's answers as rows, a string that reads as a number quoted", () => { const html = renderToStaticMarkup( - {}} submitted={false} /> + {}} submitted={false} /> , ); for (const v of ["a.master", "mc:1", "a.port", "8000", "a.tag", ""8000""]) expect(html).toContain(`value="${v}"`); diff --git a/web/src/modules/inferences/profile-lib.test.ts b/web/src/modules/inferences/profile-lib.test.ts index 0b3a9f5..8145168 100644 --- a/web/src/modules/inferences/profile-lib.test.ts +++ b/web/src/modules/inferences/profile-lib.test.ts @@ -55,7 +55,7 @@ describe("nodes", () => { describe("input answers", () => { it("maps a profile's answers to rows and back", () => { - const inputs = { "a.master": "mc:1", "a.port": 8000, "a.on": true }; + const inputs = [{ key: "a.master", value: "mc:1" }, { key: "a.port", value: 8000 }, { key: "v.[name=x].path", value: true }]; expect(parseAnswers(answersText(inputs))).toEqual(inputs); expect(parseAnswers(fromRows(toRows(answersText(inputs))))).toEqual(inputs); expect(parseAnswers(fromRows([]))).toBeUndefined(); diff --git a/web/src/modules/swiss/components/DeploySettings.test.ts b/web/src/modules/swiss/components/DeploySettings.test.ts index 13a02a8..1d6ea79 100644 --- a/web/src/modules/swiss/components/DeploySettings.test.ts +++ b/web/src/modules/swiss/components/DeploySettings.test.ts @@ -124,7 +124,7 @@ describe("catalog input answers", () => { extraArgs: ["--foo=OLD", "--bar"], untouched: 1, }; - const opts = { model: "m", inputs: [master, arg], values, formLayer, inputsAnnotation: annotation, siteInputs: { [arg.key]: "site-{{model}}" }, hf: "" }; + const opts = { model: "m", inputs: [master, arg], values, formLayer, inputsAnnotation: annotation, siteInputs: [{ key: arg.key, value: "site-{{model}}" }], hf: "" }; const seeded = formFromPlan( { release: { name: "r", namespace: "n" }, @@ -176,7 +176,7 @@ describe("catalog input answers", () => { }); it("fills a list key from the site when nothing is typed", () => { - const req = planRequest(EMPTY, { model: "glm", inputs: [arg], values, siteInputs: { [arg.key]: "mc-{{model}}" }, hf: "org/name" }); + const req = planRequest(EMPTY, { model: "glm", inputs: [arg], values, siteInputs: [{ key: arg.key, value: "mc-{{model}}" }], hf: "org/name" }); expect(req.overrides?.extraArgs).toEqual(["mc-glm", "--bar"]); expect(req.additionalAnnotations).toEqual({ [INPUTS_ANNOTATION]: "[]" }); }); diff --git a/web/src/modules/swiss/components/DeploySettings.tsx b/web/src/modules/swiss/components/DeploySettings.tsx index 28100da..a534632 100644 --- a/web/src/modules/swiss/components/DeploySettings.tsx +++ b/web/src/modules/swiss/components/DeploySettings.tsx @@ -1,6 +1,6 @@ import { useState } from "react"; import { ChevronDown, Plus, RefreshCw, RotateCcw, TriangleAlert, X } from "lucide-react"; -import type { ApplyFlags, ClusterInfo, GPUSpec, Input as CatalogInput, Node, Plan, PlanRequest, Preset, ProfileResponse, Variant } from "@swiss/lib/api"; +import type { ApplyFlags, ClusterInfo, GPUSpec, Input as CatalogInput, Node, Plan, PlanRequest, Preset, ProfileResponse, SiteInput, Variant } from "@swiss/lib/api"; import { cn } from "@swiss/lib/utils"; import { cacheFieldError, type CacheCard, type CacheDefaults } from "@swiss/lib/cache"; import { pickableProducts, productChoices, unlistedProducts, type GPUCapacity } from "@swiss/lib/gpu"; @@ -2252,7 +2252,7 @@ export function planRequest( // deployed before answers were recorded. inputsAnnotation?: string; // Site profile answers. A list key writes one; a field key leaves it to swissd. - siteInputs?: Record; + siteInputs?: SiteInput[]; hf?: string; // The values of the preset f picks. preset?: Record; diff --git a/web/src/modules/swiss/components/ProfileForm.test.tsx b/web/src/modules/swiss/components/ProfileForm.test.tsx index 44c4da5..f5151ac 100644 --- a/web/src/modules/swiss/components/ProfileForm.test.tsx +++ b/web/src/modules/swiss/components/ProfileForm.test.tsx @@ -7,7 +7,7 @@ describe("variant inputs", () => { it("lists the profile's answers as rows, a string that reads as a number quoted", () => { const html = renderToStaticMarkup( - {}} /> + {}} /> , ); expect(html).toContain("Variant inputs"); diff --git a/web/src/modules/swiss/lib/api.ts b/web/src/modules/swiss/lib/api.ts index 5542753..60beddd 100644 --- a/web/src/modules/swiss/lib/api.ts +++ b/web/src/modules/swiss/lib/api.ts @@ -329,6 +329,12 @@ export interface Preset { values?: Record; } +// One site answer. A list entry, so a key like volumes.[name=x].path is a plain string. +export interface SiteInput { + key: string; + value: string | number | boolean; +} + export interface SiteProfile { name: string; namespace?: string; @@ -371,8 +377,8 @@ export interface SiteProfile { // The site's preferences, beneath the catalog: a variant overrides them. // features unset is on (featureOn). defaults?: { features?: { cart?: boolean; serviceMonitor?: boolean; sloRequirement?: boolean; scaler?: boolean } }; - // Answers a variant's inputs by chart path; strings take pathTemplate's placeholders. - inputs?: Record; + // Answers a variant's inputs by key, each key once; strings take pathTemplate's placeholders. + inputs?: SiteInput[]; // Named values a deploy may pick one of; the console merges it into the form layer. presets?: Record; nodes?: { diff --git a/web/src/modules/swiss/lib/inputs.test.ts b/web/src/modules/swiss/lib/inputs.test.ts index 5cbfd39..94c6d88 100644 --- a/web/src/modules/swiss/lib/inputs.test.ts +++ b/web/src/modules/swiss/lib/inputs.test.ts @@ -12,7 +12,7 @@ const port: Input = { key: "service.port", title: "Port", type: "integer", examp const site = (inputs: Record): SiteProfile => ({ name: "prod", model: { pathTemplate: "/models/{{name}}" }, - inputs, + inputs: Object.entries(inputs).map(([key, value]) => ({ key, value })), }); describe("fillTemplate", () => { diff --git a/web/src/modules/swiss/lib/inputs.ts b/web/src/modules/swiss/lib/inputs.ts index 9003885..dcc4cb7 100644 --- a/web/src/modules/swiss/lib/inputs.ts +++ b/web/src/modules/swiss/lib/inputs.ts @@ -1,4 +1,4 @@ -import type { Input, SiteProfile } from "./api"; +import type { Input, SiteInput, SiteProfile } from "./api"; import { findAt, isFieldKey, readAt, splitKey, writeAt } from "./inputKey"; export interface InputChoice { @@ -47,7 +47,7 @@ export function inputRows( ): InputRow[] { const recorded = releaseInputState(inputs, variantValues, release, annotation).answers; return (inputs ?? []).map((input) => { - const v = profile?.inputs?.[input.key]; + const v = profile?.inputs?.find((s) => s.key === input.key)?.value; const site = v === undefined || v === null ? undefined : { answer: typeof v === "string" ? fillTemplate(v, hf, model) : String(v), written: String(v) }; const release = recorded[input.key]; const found: [string | undefined, InputChoice["from"]][] = [ @@ -212,14 +212,14 @@ export function releaseInputState( export function filledSiteAnswers( inputs: Input[] | undefined, - siteInputs: Record | undefined, + siteInputs: SiteInput[] | undefined, hf: string, model: string, ): Record { const out: Record = {}; if (!siteInputs) return out; for (const input of inputs ?? []) { - const v = siteInputs[input.key]; + const v = siteInputs.find((s) => s.key === input.key)?.value; if (v == null) continue; const text = typeof v === "string" ? fillTemplate(v, hf, model) : String(v); if (text) out[input.key] = text; @@ -273,5 +273,5 @@ export function parseAnswer(text: string): Answer { return /^"(-?\d+|true|false)"$/.test(text) ? text.slice(1, -1) : text; } -export const answersText = (a?: Record) => a && Object.fromEntries(Object.entries(a).map(([k, v]) => [k, answerText(v)])); -export const parseAnswers = (r?: Record) => r && Object.fromEntries(Object.entries(r).map(([k, v]) => [k, parseAnswer(v)])); +export const answersText = (a?: SiteInput[]) => a && Object.fromEntries(a.map((s) => [s.key, answerText(s.value)])); +export const parseAnswers = (r?: Record): SiteInput[] | undefined => r && Object.entries(r).map(([key, v]) => ({ key, value: parseAnswer(v) })); diff --git a/web/src/modules/swiss/lib/siteAnswers.test.ts b/web/src/modules/swiss/lib/siteAnswers.test.ts index 96ff948..e979607 100644 --- a/web/src/modules/swiss/lib/siteAnswers.test.ts +++ b/web/src/modules/swiss/lib/siteAnswers.test.ts @@ -16,16 +16,23 @@ describe("withSiteAnswers", () => { expect(parse(out)).toEqual({ name: "lab", model: { pathTemplate: "/models/{{name}}" }, - inputs: { "storageKVOffload.mooncake.master": "mc:1", "service.port": 8050 }, + inputs: [ + { key: "storageKVOffload.mooncake.master", value: "mc:1" }, + { key: "service.port", value: 8050 }, + ], }); }); it("replaces an answer and keeps the others", () => { - const out = withSiteAnswers(`${profile}inputs:\n a.b: old # site-wide\n c.d: kept\n`, [{ key: "a.b", value: "new" }]); - expect(parse(out).inputs).toEqual({ "a.b": "new", "c.d": "kept" }); + const out = withSiteAnswers(`${profile}inputs:\n - key: a.b\n value: old # site-wide\n - key: c.d\n value: kept\n`, [{ key: "a.b", value: "new" }]); + expect(parse(out).inputs).toEqual([{ key: "a.b", value: "new" }, { key: "c.d", value: "kept" }]); expect(out).toContain("# site-wide"); }); it("fills an empty inputs section", () => { - expect(parse(withSiteAnswers(`${profile}inputs:\n`, [{ key: "a.b", value: true }])).inputs).toEqual({ "a.b": true }); + expect(parse(withSiteAnswers(`${profile}inputs:\n`, [{ key: "a.b", value: true }])).inputs).toEqual([{ key: "a.b", value: true }]); + }); + it("adds a key with brackets as a plain string", () => { + const key = "volumes.[name=draft-model].hostPath.path"; + expect(parse(withSiteAnswers(profile, [{ key, value: "/data/x" }])).inputs).toEqual([{ key, value: "/data/x" }]); }); it("refuses a document that does not parse", () => { expect(() => withSiteAnswers("name: [", [{ key: "a.b", value: 1 }])).toThrow(); diff --git a/web/src/modules/swiss/lib/siteAnswers.ts b/web/src/modules/swiss/lib/siteAnswers.ts index 7932750..0116ea5 100644 --- a/web/src/modules/swiss/lib/siteAnswers.ts +++ b/web/src/modules/swiss/lib/siteAnswers.ts @@ -1,13 +1,19 @@ -import { isMap, parseDocument } from "yaml"; +import { isMap, isSeq, parseDocument } from "yaml"; import { api, type ProfileResponse } from "./api"; import type { SiteSave } from "./inputs"; -// The profile's text with each answer set under inputs, its comments and every other line kept. +// The profile's text with each answer set in the inputs list, its comments and every other line kept. export function withSiteAnswers(yaml: string, saves: Pick[]): string { const doc = parseDocument(yaml); if (doc.errors.length) throw new Error(doc.errors[0].message); - if (doc.has("inputs") && !isMap(doc.get("inputs", true))) doc.set("inputs", doc.createNode({})); - for (const s of saves) doc.setIn(["inputs", s.key], s.value); + if (!isSeq(doc.get("inputs", true))) doc.set("inputs", doc.createNode([])); + const list = doc.get("inputs", true); + if (!isSeq(list)) throw new Error("inputs is not a list"); + for (const s of saves) { + const at = list.items.find((it) => isMap(it) && it.get("key") === s.key); + if (isMap(at)) at.set("value", s.value); + else list.add(doc.createNode({ key: s.key, value: s.value })); + } return String(doc); } diff --git a/web/src/modules/swiss/routes/SiteProfile.tsx b/web/src/modules/swiss/routes/SiteProfile.tsx index 2313f0b..06b75fb 100644 --- a/web/src/modules/swiss/routes/SiteProfile.tsx +++ b/web/src/modules/swiss/routes/SiteProfile.tsx @@ -155,8 +155,8 @@ export function SiteProfile() { title="Variant inputs" hint="Answers to the inputs catalog variants declare, by chart key. Only a variant that declares a key takes its answer, and a deploy can still override it." > - {p.inputs && Object.keys(p.inputs).length > 0 ? ( - Object.entries(p.inputs).map(([k, v]) => ) + {p.inputs?.length ? ( + p.inputs.map((s) => ) ) : ( )}