Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 | 3x 3x 3x 3x | import { FIELD_TYPES, FieldSpec, FieldType } from "@/objects/Skill";
import { ChevronDown, ChevronUp, Plus, Trash2 } from "lucide-react";
import React from "react";
/**
* Editor for a typed interface (list of FieldSpec) — used for worker
* input/output schemas, per-node schema overrides and skill interface inputs.
*
* Purely controlled: every change calls onChange with the full new list.
*/
const NAME_RE = /^[a-zA-Z_][a-zA-Z0-9_]*$/;
export function validateFields(fields: FieldSpec[]): string | null {
const seen = new Set<string>();
for (const f of fields) {
if (!f.name.trim()) return "Every field needs a name.";
if (!NAME_RE.test(f.name))
return `Field "${f.name}" must be an identifier (letters, digits, underscore).`;
if (seen.has(f.name)) return `Field "${f.name}" is declared more than once.`;
seen.add(f.name);
if (f.type === "enum" && !(f.values && f.values.length > 0))
return `Enum field "${f.name}" needs at least one value.`;
}
return null;
}
export const emptyField = (): FieldSpec => ({
name: "",
type: "string",
description: "",
required: true,
});
const inputCls =
"w-full text-xs px-2 py-1.5 rounded-md border border-gray-200 dark:border-gray-600 bg-white dark:bg-gray-700 text-gray-800 dark:text-gray-200 placeholder-gray-400 focus:outline-none focus:ring-1 focus:ring-violet-300";
const SchemaFieldsEditor: React.FC<{
value: FieldSpec[];
onChange: (fields: FieldSpec[]) => void;
/** Label for the add button, e.g. "Add input". */
addLabel?: string;
/** Hide the required toggle (outputs are always produced). */
hideRequired?: boolean;
emptyHint?: string;
disabled?: boolean;
}> = ({
value,
onChange,
addLabel = "Add field",
hideRequired = false,
emptyHint = "No fields declared.",
disabled = false,
}) => {
const update = (idx: number, patch: Partial<FieldSpec>) =>
onChange(value.map((f, i) => (i === idx ? { ...f, ...patch } : f)));
const remove = (idx: number) => onChange(value.filter((_, i) => i !== idx));
const move = (idx: number, dir: -1 | 1) => {
const next = [...value];
const j = idx + dir;
if (j < 0 || j >= next.length) return;
[next[idx], next[j]] = [next[j], next[idx]];
onChange(next);
};
const error = validateFields(value);
return (
<div className="space-y-2">
{value.length === 0 && (
<p className="text-xs text-gray-400 dark:text-gray-500 italic">
{emptyHint}
</p>
)}
{value.map((f, idx) => (
<div
key={idx}
className="rounded-lg border border-gray-200 dark:border-gray-600 bg-gray-50 dark:bg-gray-800/60 p-2 space-y-1.5"
>
<div className="flex items-center gap-1.5">
<input
type="text"
value={f.name}
disabled={disabled}
onChange={(e) =>
update(idx, {
name: e.target.value.replace(/[^a-zA-Z0-9_]/g, "_"),
})
}
placeholder="field_name"
className={inputCls + " font-mono flex-1"}
/>
<select
value={f.type}
disabled={disabled}
onChange={(e) =>
update(idx, {
type: e.target.value as FieldType,
values:
e.target.value === "enum" ? (f.values ?? []) : undefined,
items: e.target.value === "array" ? (f.items ?? "string") : undefined,
})
}
className={inputCls + " w-24 shrink-0"}
>
{FIELD_TYPES.map((t) => (
<option key={t} value={t}>
{t}
</option>
))}
</select>
<div className="flex items-center shrink-0">
<button
type="button"
onClick={() => move(idx, -1)}
disabled={disabled || idx === 0}
className="p-1 text-gray-400 hover:text-gray-700 dark:hover:text-gray-200 disabled:opacity-30"
title="Move up"
>
<ChevronUp className="size-3" />
</button>
<button
type="button"
onClick={() => move(idx, 1)}
disabled={disabled || idx === value.length - 1}
className="p-1 text-gray-400 hover:text-gray-700 dark:hover:text-gray-200 disabled:opacity-30"
title="Move down"
>
<ChevronDown className="size-3" />
</button>
<button
type="button"
onClick={() => remove(idx)}
disabled={disabled}
className="p-1 text-gray-400 hover:text-red-500 disabled:opacity-30"
title="Remove field"
>
<Trash2 className="size-3" />
</button>
</div>
</div>
<input
type="text"
value={f.description ?? ""}
disabled={disabled}
onChange={(e) => update(idx, { description: e.target.value })}
placeholder="Description — what this field means (shown to the model)"
className={inputCls}
/>
{f.type === "enum" && (
<input
type="text"
value={(f.values ?? []).join(", ")}
disabled={disabled}
onChange={(e) =>
update(idx, {
values: e.target.value
.split(",")
.map((v) => v.trim())
.filter(Boolean),
})
}
placeholder="Allowed values, comma-separated: low, medium, high"
className={inputCls + " font-mono"}
/>
)}
<div className="flex items-center gap-3 flex-wrap">
{f.type === "array" && (
<label className="flex items-center gap-1 text-[11px] text-gray-500 dark:text-gray-400">
items
<select
value={f.items ?? "string"}
disabled={disabled}
onChange={(e) =>
update(idx, { items: e.target.value as FieldType })
}
className={inputCls + " w-24 py-0.5"}
>
{(["string", "number", "integer", "boolean"] as FieldType[]).map(
(t) => (
<option key={t} value={t}>
{t}
</option>
),
)}
</select>
</label>
)}
{!hideRequired && (
<label className="flex items-center gap-1.5 text-[11px] text-gray-500 dark:text-gray-400 cursor-pointer">
<input
type="checkbox"
checked={f.required !== false}
disabled={disabled}
onChange={(e) => update(idx, { required: e.target.checked })}
className="accent-violet-500"
/>
required
</label>
)}
{f.required === false && (
<input
type="text"
value={
f.default === undefined || f.default === null
? ""
: typeof f.default === "string"
? f.default
: JSON.stringify(f.default)
}
disabled={disabled}
onChange={(e) =>
update(idx, {
default: e.target.value === "" ? undefined : e.target.value,
})
}
placeholder="default value"
className={inputCls + " flex-1 min-w-[120px] py-0.5"}
/>
)}
</div>
</div>
))}
{error && (
<p className="text-[11px] text-amber-600 dark:text-amber-400">{error}</p>
)}
<button
type="button"
disabled={disabled}
onClick={() => onChange([...value, emptyField()])}
className="flex items-center gap-1 text-xs font-medium text-violet-600 dark:text-violet-400 hover:underline disabled:opacity-40"
>
<Plus className="size-3" />
{addLabel}
</button>
</div>
);
};
export default SchemaFieldsEditor;
|