Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions web/src/modules/inferences/SiteProfilePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ function ProfileView({ p }: { p: SiteProfile }) {
{ label: f("overrides"), value: kv(p.model?.overrides) },
])}
<SectionCard title={t("profile.groups.inputs")}>
{p.inputs && Object.keys(p.inputs).length ? (
{p.inputs?.length ? (
<Table>
<TableHeader>
<TableRow>
Expand All @@ -175,10 +175,10 @@ function ProfileView({ p }: { p: SiteProfile }) {
</TableRow>
</TableHeader>
<TableBody>
{Object.entries(p.inputs).map(([k, x]) => (
<TableRow key={k}>
<TableCell className="font-mono text-xs break-all">{k}</TableCell>
<TableCell className="font-mono text-xs break-all">{answerText(x)}</TableCell>
{p.inputs.map((s) => (
<TableRow key={s.key}>
<TableCell className="font-mono text-xs break-all">{s.key}</TableCell>
<TableCell className="font-mono text-xs break-all">{answerText(s.value)}</TableCell>
</TableRow>
))}
</TableBody>
Expand Down
2 changes: 1 addition & 1 deletion web/src/modules/inferences/components/ProfileForm.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(
<QueryClientProvider client={new QueryClient()}>
<ProfileForm value={{ name: "prod", model: { pathTemplate: "/m/{{name}}" }, inputs: { "a.master": "mc:1", "a.port": 8000, "a.tag": "8000" } }} onChange={() => {}} submitted={false} />
<ProfileForm value={{ name: "prod", model: { pathTemplate: "/m/{{name}}" }, inputs: [{ key: "a.master", value: "mc:1" }, { key: "a.port", value: 8000 }, { key: "a.tag", value: "8000" }] }} onChange={() => {}} submitted={false} />
</QueryClientProvider>,
);
for (const v of ["a.master", "mc:1", "a.port", "8000", "a.tag", "&quot;8000&quot;"]) expect(html).toContain(`value="${v}"`);
Expand Down
2 changes: 1 addition & 1 deletion web/src/modules/inferences/profile-lib.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions web/src/modules/swiss/components/DeploySettings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
Expand Down Expand Up @@ -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]: "[]" });
});
Expand Down
4 changes: 2 additions & 2 deletions web/src/modules/swiss/components/DeploySettings.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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<string, string | number | boolean>;
siteInputs?: SiteInput[];
hf?: string;
// The values of the preset f picks.
preset?: Record<string, unknown>;
Expand Down
2 changes: 1 addition & 1 deletion web/src/modules/swiss/components/ProfileForm.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(
<QueryClientProvider client={new QueryClient()}>
<ProfileForm value={{ name: "prod", model: { pathTemplate: "/m/{{name}}" }, inputs: { "a.master": "mc:1", "a.port": 8000, "a.tag": "8000" } }} onChange={() => {}} />
<ProfileForm value={{ name: "prod", model: { pathTemplate: "/m/{{name}}" }, inputs: [{ key: "a.master", value: "mc:1" }, { key: "a.port", value: 8000 }, { key: "a.tag", value: "8000" }] }} onChange={() => {}} />
</QueryClientProvider>,
);
expect(html).toContain("Variant inputs");
Expand Down
10 changes: 8 additions & 2 deletions web/src/modules/swiss/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,12 @@ export interface Preset {
values?: Record<string, unknown>;
}

// 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;
Expand Down Expand Up @@ -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<string, string | number | boolean>;
// 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<string, Preset>;
nodes?: {
Expand Down
2 changes: 1 addition & 1 deletion web/src/modules/swiss/lib/inputs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const port: Input = { key: "service.port", title: "Port", type: "integer", examp
const site = (inputs: Record<string, string | number | boolean>): SiteProfile => ({
name: "prod",
model: { pathTemplate: "/models/{{name}}" },
inputs,
inputs: Object.entries(inputs).map(([key, value]) => ({ key, value })),
});

describe("fillTemplate", () => {
Expand Down
12 changes: 6 additions & 6 deletions web/src/modules/swiss/lib/inputs.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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"]][] = [
Expand Down Expand Up @@ -212,14 +212,14 @@ export function releaseInputState(

export function filledSiteAnswers(
inputs: Input[] | undefined,
siteInputs: Record<string, string | number | boolean> | undefined,
siteInputs: SiteInput[] | undefined,
hf: string,
model: string,
): Record<string, string> {
const out: Record<string, string> = {};
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;
Expand Down Expand Up @@ -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<string, Answer>) => a && Object.fromEntries(Object.entries(a).map(([k, v]) => [k, answerText(v)]));
export const parseAnswers = (r?: Record<string, string>) => 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<string, string>): SiteInput[] | undefined => r && Object.entries(r).map(([key, v]) => ({ key, value: parseAnswer(v) }));
15 changes: 11 additions & 4 deletions web/src/modules/swiss/lib/siteAnswers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
14 changes: 10 additions & 4 deletions web/src/modules/swiss/lib/siteAnswers.ts
Original file line number Diff line number Diff line change
@@ -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<SiteSave, "key" | "value">[]): 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);
}

Expand Down
4 changes: 2 additions & 2 deletions web/src/modules/swiss/routes/SiteProfile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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]) => <Row key={k} label={k} value={answerText(v)} mono />)
{p.inputs?.length ? (
p.inputs.map((s) => <Row key={s.key} label={s.key} value={answerText(s.value)} mono />)
) : (
<Row label="Answers" value={undefined} fallback="none: an input takes its default, or the deploy's answer" />
)}
Expand Down
Loading