feat(webui): read the derived resource schema in the document editor
The control-plane descriptors now derive a JSON Schema from the Go type of
the document they describe (a19cd24b), and nothing consumed it: the editor
rendered dashed keys, guessed no format, and synthesised a new row from a
literal skeleton per array path.
A pure reader (settings/resource-schema.ts) resolves the schema node for an
editor path -- nested keys, array items, and a keyed collection's
additionalProperties -- and the editor uses it for labels (title ahead of
the dashed-key fallback), a duration placeholder, a hint when the schema
carries prose, and the shape of a new row. A descriptor with no schema, or
one describing nothing, renders exactly as before.
Deletes ControlPlaneResourceCard.vue: 121 lines, zero references, the
abandoned schema-driven card and the only other consumer of schema_json.
This commit is contained in:
6 files changed
+315
-149
No files matched your search
@@ -1,121 +0,0 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, reactive, watch } from "vue";
|
||||
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Field, FieldDescription, FieldGroup, FieldLabel } from "@/components/ui/field";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Select, SelectContent, SelectGroup, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
|
||||
import { Spinner } from "@/components/ui/spinner";
|
||||
import { useControlPlaneStore, type ResourceDescriptor } from "@/stores/control-plane";
|
||||
import ApplyStatusRows from "./ApplyStatusRows.vue";
|
||||
import ConfigCard from "./ConfigCard.vue";
|
||||
import ResourceHistory from "./ResourceHistory.vue";
|
||||
|
||||
interface JSONSchemaProperty {
|
||||
type?: "integer" | "number" | "string" | "boolean";
|
||||
title?: string;
|
||||
description?: string;
|
||||
minimum?: number;
|
||||
maximum?: number;
|
||||
enum?: Array<string | number>;
|
||||
}
|
||||
|
||||
const props = defineProps<{ descriptor: ResourceDescriptor }>();
|
||||
const store = useControlPlaneStore();
|
||||
const state = computed(() => store.stateFor(props.descriptor.kind));
|
||||
const draft = reactive<Record<string, unknown>>({});
|
||||
const touched = reactive<Record<string, boolean>>({});
|
||||
|
||||
const schema = computed(() => {
|
||||
try { return JSON.parse(props.descriptor.schema_json) as { properties?: Record<string, JSONSchemaProperty> }; }
|
||||
catch { return {}; }
|
||||
});
|
||||
const fields = computed(() => Object.entries(schema.value.properties ?? {}));
|
||||
|
||||
watch(() => state.value.resource, (resource) => {
|
||||
if (!resource) return;
|
||||
for (const key of Object.keys(draft)) delete draft[key];
|
||||
Object.assign(draft, resource.value as Record<string, unknown>);
|
||||
}, { immediate: true });
|
||||
|
||||
function label(key: string, property: JSONSchemaProperty): string {
|
||||
if (property.title) return property.title;
|
||||
const taskLabels: Record<string, string> = {
|
||||
max_model_tool_steps: "Max model steps",
|
||||
max_runtime_seconds: "Max runtime (seconds)",
|
||||
max_consecutive_gate_failures: "Max gate failures",
|
||||
};
|
||||
return taskLabels[key] ?? key.replaceAll("_", " ").replace(/^./, (value) => value.toUpperCase());
|
||||
}
|
||||
|
||||
function update(key: string, property: JSONSchemaProperty, value: unknown): void {
|
||||
touched[key] = true;
|
||||
if (property.type === "integer" || property.type === "number") draft[key] = Number(value);
|
||||
else if (property.type === "boolean") draft[key] = value === true || value === "true";
|
||||
else draft[key] = value;
|
||||
}
|
||||
|
||||
function invalid(key: string, property: JSONSchemaProperty): boolean {
|
||||
if (!touched[key]) return false;
|
||||
const value = draft[key];
|
||||
if ((property.type === "integer" || property.type === "number") && !Number.isFinite(value)) return true;
|
||||
if (typeof value === "number" && property.minimum !== undefined && value < property.minimum) return true;
|
||||
if (typeof value === "number" && property.maximum !== undefined && value > property.maximum) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
async function save(): Promise<void> {
|
||||
for (const [key] of fields.value) touched[key] = true;
|
||||
if (fields.value.some(([key, property]) => invalid(key, property))) return;
|
||||
if (await store.replace(props.descriptor.kind, { ...draft })) {
|
||||
for (const key of Object.keys(touched)) touched[key] = false;
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<ConfigCard :title="descriptor.title">
|
||||
<form class="flex flex-col gap-5" @submit.prevent="save">
|
||||
<FieldGroup>
|
||||
<Field v-for="[key, property] in fields" :key="key" :data-invalid="invalid(key, property)">
|
||||
<FieldLabel :for="`${descriptor.kind}-${key}`">{{ label(key, property) }}</FieldLabel>
|
||||
<Select v-if="property.type === 'boolean' || property.enum" :model-value="String(draft[key])" @update:model-value="update(key, property, $event)">
|
||||
<SelectTrigger :id="`${descriptor.kind}-${key}`" :aria-invalid="invalid(key, property)"><SelectValue /></SelectTrigger>
|
||||
<SelectContent><SelectGroup>
|
||||
<SelectItem v-for="option in property.enum ?? ['true', 'false']" :key="String(option)" :value="String(option)">{{ option }}</SelectItem>
|
||||
</SelectGroup></SelectContent>
|
||||
</Select>
|
||||
<Input
|
||||
v-else
|
||||
:id="`${descriptor.kind}-${key}`"
|
||||
:model-value="draft[key] as string | number"
|
||||
:type="property.type === 'integer' || property.type === 'number' ? 'number' : 'text'"
|
||||
:min="property.minimum"
|
||||
:max="property.maximum"
|
||||
:step="property.type === 'integer' ? 1 : undefined"
|
||||
:aria-invalid="invalid(key, property)"
|
||||
autocomplete="off"
|
||||
@update:model-value="update(key, property, $event)"
|
||||
/>
|
||||
<FieldDescription v-if="property.description">{{ property.description }}</FieldDescription>
|
||||
<FieldDescription v-if="invalid(key, property)" class="text-destructive">Enter a valid value.</FieldDescription>
|
||||
</Field>
|
||||
</FieldGroup>
|
||||
<p v-if="state.error" class="text-sm text-destructive" role="alert">{{ state.error }}</p>
|
||||
<div class="flex items-center justify-between gap-3">
|
||||
<div class="flex items-center gap-2">
|
||||
<Badge :variant="state.stream === 'live' ? 'ok' : 'warn'">{{ state.stream === 'live' ? 'Live' : 'Connecting' }}</Badge>
|
||||
<Badge v-if="descriptor.apply_mode === 'restart-required'" variant="warn">Restart required</Badge>
|
||||
<span v-if="state.resource" class="text-xs text-muted-foreground">Version {{ state.resource.version }}</span>
|
||||
</div>
|
||||
<Button type="submit" :disabled="state.saving || state.loading">
|
||||
<Spinner v-if="state.saving" data-icon="inline-start" />
|
||||
Save changes
|
||||
</Button>
|
||||
</div>
|
||||
</form>
|
||||
<ApplyStatusRows :kind="descriptor.kind" />
|
||||
<ResourceHistory :kind="descriptor.kind" />
|
||||
</ConfigCard>
|
||||
</template>
|
||||
@@ -8,12 +8,19 @@ import { cloneControlPlaneValue, useControlPlaneStore, type ResourceDescriptor }
|
||||
import ConfigCard from "./ConfigCard.vue";
|
||||
import ResourceHistory from "./ResourceHistory.vue";
|
||||
import StructuredValueEditor from "./StructuredValueEditor.vue";
|
||||
import { parseSchema } from "./resource-schema";
|
||||
|
||||
const props = defineProps<{ descriptor: ResourceDescriptor; rootPath?: string }>();
|
||||
const store = useControlPlaneStore();
|
||||
const state = computed(() => store.stateFor(props.descriptor.kind));
|
||||
const draft = ref<unknown>({});
|
||||
|
||||
// The schema the descriptor derives from its document type: the editor reads
|
||||
// it for labels, hints, formatted placeholders and the shape of a new row. A
|
||||
// descriptor that carries none reads as null, and the editor falls back to
|
||||
// rendering the value alone.
|
||||
const schema = computed(() => parseSchema(props.descriptor.schema_json));
|
||||
|
||||
watch(() => state.value.resource, (resource) => {
|
||||
if (resource) draft.value = cloneControlPlaneValue(resource.value);
|
||||
}, { immediate: true });
|
||||
@@ -26,7 +33,12 @@ async function save(): Promise<void> {
|
||||
<template>
|
||||
<ConfigCard :title="descriptor.title">
|
||||
<form class="space-y-5" @submit.prevent="save">
|
||||
<StructuredValueEditor v-model="draft" :path="rootPath ?? descriptor.kind" />
|
||||
<StructuredValueEditor
|
||||
v-model="draft"
|
||||
:path="rootPath ?? descriptor.kind"
|
||||
:root-path="rootPath ?? descriptor.kind"
|
||||
:schema="schema"
|
||||
/>
|
||||
<p v-if="state.error" class="text-sm text-destructive" role="alert">{{ state.error }}</p>
|
||||
<div class="flex flex-wrap items-center justify-between gap-3">
|
||||
<div class="flex items-center gap-2">
|
||||
|
||||
@@ -7,12 +7,26 @@ import { Checkbox } from "@/components/ui/checkbox";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Label } from "@/components/ui/label";
|
||||
import { Textarea } from "@/components/ui/textarea";
|
||||
import { blankFromSchema, fieldHint, fieldPlaceholder, fieldTitle, schemaAt, type SchemaProperty } from "./resource-schema";
|
||||
|
||||
/**
|
||||
* The document editor, driven by the value it was given and annotated by the
|
||||
* schema the descriptor carries.
|
||||
*
|
||||
* The value decides which fields exist (this walks what is stored, so a
|
||||
* document with a field the schema has not caught up with still edits); the
|
||||
* schema decides what those fields are called, what example a formatted field
|
||||
* shows, what prose qualifies it, and what a newly added row starts as. A
|
||||
* descriptor with no schema, or one describing nothing, renders exactly as it
|
||||
* did before there was a reader.
|
||||
*/
|
||||
const props = withDefaults(defineProps<{
|
||||
modelValue: unknown;
|
||||
path?: string;
|
||||
rootPath?: string;
|
||||
schema?: SchemaProperty | null;
|
||||
locked?: boolean;
|
||||
}>(), { path: "value", locked: false });
|
||||
}>(), { path: "value", rootPath: "", schema: null, locked: false });
|
||||
const emit = defineEmits<{ "update:modelValue": [value: unknown] }>();
|
||||
|
||||
const isArray = computed(() => Array.isArray(props.modelValue));
|
||||
@@ -20,6 +34,12 @@ const isObject = computed(() => props.modelValue !== null && typeof props.modelV
|
||||
const entries = computed(() => Object.entries((props.modelValue ?? {}) as Record<string, unknown>));
|
||||
const readOnly = computed(() => props.locked || /(^|\.)(has_credential|credential_configured|headers_configured)$/.test(props.path));
|
||||
|
||||
/** The schema node for this editor, and for one of its keys. */
|
||||
const property = computed(() => schemaAt(props.schema, props.path, props.rootPath));
|
||||
function propertyFor(key: string): SchemaProperty | null {
|
||||
return schemaAt(props.schema, `${props.path}.${key}`, props.rootPath);
|
||||
}
|
||||
|
||||
function label(value: string): string {
|
||||
return value.replaceAll("_", " ").replace(/^./, (character) => character.toUpperCase());
|
||||
}
|
||||
@@ -55,28 +75,13 @@ function setArray(index: number, value: unknown): void {
|
||||
emit("update:modelValue", current);
|
||||
}
|
||||
|
||||
// A new row comes from the schema rather than from the path that holds it: the
|
||||
// editor used to carry a literal skeleton per array path (a repository, an MCP
|
||||
// server), which went stale the moment the document changed and said nothing
|
||||
// about fields it did not know.
|
||||
function addArray(): void {
|
||||
const current = props.modelValue as unknown[];
|
||||
const sample = current[0];
|
||||
let value: unknown = Array.isArray(sample) ? [] : sample && typeof sample === "object" ? {} : "";
|
||||
if (props.path === "repositories") value = {
|
||||
owner: "", name: "", base: "main", gate: [], protect: [], ecosystem: "go", preflight: [], test_glob: "",
|
||||
persistent_storage: false, max_retries: 0, allow_concurrent: false, review_enabled: false,
|
||||
};
|
||||
else if (props.path.endsWith("mcp_servers")) value = {
|
||||
name: "", transport: "stdio", command: "", args: [], work_dir: "", url: "",
|
||||
sse_endpoint: "", message_endpoint: "", headers_configured: false,
|
||||
};
|
||||
else if (props.path.endsWith("gate") || props.path.endsWith("preflight")) value = [];
|
||||
else if (props.path.endsWith("allowed_user_ids")) value = 0;
|
||||
else if (props.path.endsWith("personas")) value = { name: "", prompt: "" };
|
||||
else if (props.path === "schedules") value = {
|
||||
id: "", pool: "sequential", detail: "", kind: "workflow",
|
||||
schedule: { kind: "interval", interval: 3600000000000, start: "0001-01-01T00:00:00Z" },
|
||||
target: {}, payload: { text: "" }, next_run: "0001-01-01T00:00:00Z",
|
||||
created: "0001-01-01T00:00:00Z", updated: "0001-01-01T00:00:00Z",
|
||||
};
|
||||
emit("update:modelValue", [...current, value]);
|
||||
emit("update:modelValue", [...current, blankFromSchema(property.value?.items)]);
|
||||
}
|
||||
|
||||
function removeArray(index: number): void {
|
||||
@@ -92,7 +97,13 @@ function scalar(value: string | number): void {
|
||||
<div v-if="isArray" class="space-y-2 rounded-lg border border-border/70 p-3">
|
||||
<div v-for="(item, index) in modelValue as unknown[]" :key="index" class="flex items-start gap-2">
|
||||
<div class="min-w-0 flex-1">
|
||||
<StructuredValueEditor :model-value="item" :path="`${path}.${index}`" @update:model-value="setArray(index, $event)" />
|
||||
<StructuredValueEditor
|
||||
:model-value="item"
|
||||
:path="`${path}.${index}`"
|
||||
:root-path="rootPath"
|
||||
:schema="schema"
|
||||
@update:model-value="setArray(index, $event)"
|
||||
/>
|
||||
</div>
|
||||
<Button type="button" variant="ghost" size="icon-sm" :aria-label="`Remove ${label(path)} row ${index + 1}`" @click="removeArray(index)">
|
||||
<Trash2 />
|
||||
@@ -104,13 +115,21 @@ function scalar(value: string | number): void {
|
||||
<fieldset v-else-if="isObject" class="space-y-4 rounded-lg border border-border/70 p-3">
|
||||
<div v-for="[key, value] in entries" :key="key" class="space-y-1.5">
|
||||
<div class="flex items-center gap-2">
|
||||
<Label :for="`${path}.${key}`" class="flex-1">{{ label(key) }}</Label>
|
||||
<Label :for="`${path}.${key}`" class="flex-1">{{ fieldTitle(key, propertyFor(key)) }}</Label>
|
||||
<template v-if="path.endsWith('_map') || path.endsWith('labels') || path === 'providers' || path === 'roles'">
|
||||
<Input class="h-7 max-w-48" :model-value="key" aria-label="Entry key" @change="renameObject(key, ($event.target as HTMLInputElement).value)" />
|
||||
<Button type="button" variant="ghost" size="icon-sm" :aria-label="`Remove ${key}`" @click="removeObject(key)"><Trash2 /></Button>
|
||||
</template>
|
||||
</div>
|
||||
<StructuredValueEditor :id="`${path}.${key}`" :model-value="value" :path="`${path}.${key}`" @update:model-value="setObject(key, $event)" />
|
||||
<p v-if="fieldHint(propertyFor(key))" class="text-xs text-fg-subtle">{{ fieldHint(propertyFor(key)) }}</p>
|
||||
<StructuredValueEditor
|
||||
:id="`${path}.${key}`"
|
||||
:model-value="value"
|
||||
:path="`${path}.${key}`"
|
||||
:root-path="rootPath"
|
||||
:schema="schema"
|
||||
@update:model-value="setObject(key, $event)"
|
||||
/>
|
||||
</div>
|
||||
<Button v-if="path.endsWith('_map') || path.endsWith('labels') || path === 'providers' || path === 'roles'" type="button" variant="outline" size="sm" @click="addObject"><Plus /> Add entry</Button>
|
||||
</fieldset>
|
||||
@@ -120,5 +139,16 @@ function scalar(value: string | number): void {
|
||||
<span v-if="readOnly" class="text-xs text-muted-foreground">Reported by Archie</span>
|
||||
</div>
|
||||
<Textarea v-else-if="path.endsWith('.prompt')" :id="path" :model-value="modelValue as string" :readonly="readOnly" autocomplete="off" @update:model-value="scalar" />
|
||||
<Input v-else :id="path" :model-value="modelValue as string | number" :type="typeof modelValue === 'number' ? 'number' : 'text'" :readonly="readOnly" autocomplete="off" @update:model-value="scalar" />
|
||||
<div v-else class="space-y-1">
|
||||
<Input
|
||||
:id="path"
|
||||
:model-value="modelValue as string | number"
|
||||
:type="typeof modelValue === 'number' ? 'number' : 'text'"
|
||||
:readonly="readOnly"
|
||||
:placeholder="fieldPlaceholder(property)"
|
||||
autocomplete="off"
|
||||
@update:model-value="scalar"
|
||||
/>
|
||||
<p v-if="fieldHint(property)" class="text-xs text-fg-subtle">{{ fieldHint(property) }}</p>
|
||||
</div>
|
||||
</template>
|
||||
@@ -0,0 +1,140 @@
|
||||
/**
|
||||
* Reading the JSON Schema a control-plane descriptor advertises.
|
||||
*
|
||||
* The descriptor's schema is derived in internal/app/controlplane/schema.go
|
||||
* from the Go type of the document itself, so it is structure only: keys,
|
||||
* types, and the `format`/`title`/`doc` annotations a field declares in its own
|
||||
* tags. It carries no prose of its own -- field descriptions have one home in
|
||||
* internal/webui/config_schema.go -- so everything here treats a missing
|
||||
* annotation as absent rather than inventing one.
|
||||
*
|
||||
* The live editor is value-driven (it walks the document it was given), so this
|
||||
* is metadata layered onto fields that exist, never a field list. A document
|
||||
* whose schema describes no properties renders exactly as it did before.
|
||||
*/
|
||||
|
||||
/** One node of the derived schema. Only what the derivation emits is read. */
|
||||
export interface SchemaProperty {
|
||||
type?: string;
|
||||
format?: string;
|
||||
title?: string;
|
||||
description?: string;
|
||||
properties?: Record<string, SchemaProperty>;
|
||||
items?: SchemaProperty;
|
||||
additionalProperties?: SchemaProperty | boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* parseSchema reads a descriptor's schema_json. A descriptor that carries none,
|
||||
* or carries one this reader cannot parse, reports null: an unreadable schema
|
||||
* costs a placeholder, never a field.
|
||||
*/
|
||||
export function parseSchema(json: string | null | undefined): SchemaProperty | null {
|
||||
if (!json) return null;
|
||||
try {
|
||||
const parsed: unknown = JSON.parse(json);
|
||||
if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) return null;
|
||||
return parsed as SchemaProperty;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function isKeyed(property: SchemaProperty): boolean {
|
||||
return property.type === "object" && !property.properties && typeof property.additionalProperties === "object";
|
||||
}
|
||||
|
||||
/**
|
||||
* schemaAt resolves the property that describes one editor path.
|
||||
*
|
||||
* Paths are the editor's own (`repositories.0.owner`): dotted, with an array
|
||||
* item addressed by its index. `rootPath` is stripped first, because the
|
||||
* editor's path starts at the document rather than inside it -- the schema
|
||||
* describes the document, not a field of it.
|
||||
*/
|
||||
export function schemaAt(
|
||||
schema: SchemaProperty | null,
|
||||
path: string,
|
||||
rootPath = "",
|
||||
): SchemaProperty | null {
|
||||
if (!schema) return null;
|
||||
const root = rootPath.split(".").filter(Boolean);
|
||||
let segments = path.split(".").filter(Boolean);
|
||||
if (root.length && root.every((segment, index) => segments[index] === segment)) {
|
||||
segments = segments.slice(root.length);
|
||||
}
|
||||
|
||||
let current: SchemaProperty | null = schema;
|
||||
for (const segment of segments) {
|
||||
if (!current) return null;
|
||||
if (/^\d+$/.test(segment)) {
|
||||
current = current.items ?? null;
|
||||
continue;
|
||||
}
|
||||
const named = current.properties?.[segment];
|
||||
if (named) {
|
||||
current = named;
|
||||
continue;
|
||||
}
|
||||
// A keyed collection's keys belong to the operator, so the value's
|
||||
// schema describes whatever key they typed.
|
||||
current = typeof current.additionalProperties === "object" ? current.additionalProperties : null;
|
||||
}
|
||||
return current;
|
||||
}
|
||||
|
||||
/** fieldTitle is the schema's own label for a key, or the dashed-key fallback
|
||||
* the editor has always used. */
|
||||
export function fieldTitle(key: string, property: SchemaProperty | null | undefined): string {
|
||||
if (property?.title) return property.title;
|
||||
return key.replaceAll("_", " ").replace(/^./, (character) => character.toUpperCase());
|
||||
}
|
||||
|
||||
/**
|
||||
* fieldPlaceholder offers an example only where the schema declares a format
|
||||
* the field's value must match. A duration column is a string to the document
|
||||
* and a duration to the operator, and the example is what says so.
|
||||
*/
|
||||
export function fieldPlaceholder(property: SchemaProperty | null | undefined): string | undefined {
|
||||
if (property?.format === "duration") return "1m0s";
|
||||
if (property?.format === "date-time") return "2026-01-01T00:00:00Z";
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/** fieldHint is the schema's own prose for a field, empty when it has none. */
|
||||
export function fieldHint(property: SchemaProperty | null | undefined): string {
|
||||
return property?.description ?? "";
|
||||
}
|
||||
|
||||
/** isKeyedCollection reports a document whose keys are the operator's: a map
|
||||
* with no fixed properties, which the schema marks with additionalProperties. */
|
||||
export function isKeyedCollection(property: SchemaProperty | null | undefined): boolean {
|
||||
return Boolean(property) && isKeyed(property as SchemaProperty);
|
||||
}
|
||||
|
||||
/**
|
||||
* blankFromSchema synthesises a new row from the schema rather than from the
|
||||
* path that happens to hold it.
|
||||
*
|
||||
* The editor used to carry a literal template per array path -- a repository
|
||||
* skeleton, an MCP server skeleton -- which went stale the moment the document
|
||||
* was reshaped, and said nothing about the fields it did not know. A blank from
|
||||
* the schema cannot be stale, because the schema is derived from the document.
|
||||
* What it loses is the seeded convenience value (a default branch name, say):
|
||||
* a schema that wants those emits a default, which is the same single source of
|
||||
* truth this reader exists to honour.
|
||||
*/
|
||||
export function blankFromSchema(property: SchemaProperty | null | undefined): unknown {
|
||||
if (!property) return "";
|
||||
if (property.type === "object") {
|
||||
const blank: Record<string, unknown> = {};
|
||||
for (const [key, child] of Object.entries(property.properties ?? {})) {
|
||||
blank[key] = blankFromSchema(child);
|
||||
}
|
||||
return blank;
|
||||
}
|
||||
if (property.type === "array") return [];
|
||||
if (property.type === "boolean") return false;
|
||||
if (property.type === "integer" || property.type === "number") return 0;
|
||||
return "";
|
||||
}
|
||||
Reference in new issue
Block a user