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 | 15x 15x 257x 15x 255x 15x 15x 255x 255x 255x 255x 85x 1x 170x 85x 170x 33x 255x 255x | import type { RunField, RunFieldSource } from "@/objects/Skill";
import type { FormValue } from "@/util/runForm";
import { Plus, X } from "lucide-react";
import React from "react";
/**
* One input of a skill's run schema, drawn by its type: an enum is a select,
* a number a number box, an array a list, an object a JSON editor, anything
* else a text box. Shared by the skill page's Run form and the chat's
* command form, so a skill takes the same inputs in both. Lay the fields out
* in an `@container` grid (`grid @md:grid-cols-2`): a list or object field
* spans both columns.
*/
export const inputClass =
"w-full text-sm px-3 py-2 rounded-lg border 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";
export const borderClass = (invalid: boolean) =>
invalid
? "border-red-300 dark:border-red-700"
: "border-gray-200 dark:border-gray-600";
const SOURCE_HINT: Record<RunFieldSource, (f: RunField) => string | null> = {
interface: () => null,
node: (f) =>
`Asked for by ${f.consumers.map((c) => `“${c}”`).join(", ")} — its worker declares this input and nothing in the graph supplies it.`,
state: (f) =>
`A state variable ${f.consumers.length ? `read by ${f.consumers.map((c) => `“${c}”`).join(", ")} ` : ""}that nothing writes or defaults.`,
template: (f) =>
`Used in the prompt template of ${f.consumers.map((c) => `“${c}”`).join(", ")}.`,
};
const ArrayControl: React.FC<{
field: RunField;
value: string[];
invalid: boolean;
onChange: (v: string[]) => void;
}> = ({ field, value, invalid, onChange }) => {
const items = value.length ? value : [""];
const numeric = field.items === "number" || field.items === "integer";
return (
<div className="space-y-1.5">
{items.map((item, i) => (
<div key={i} className="flex items-center gap-1.5">
<input
type={numeric ? "number" : "text"}
value={item}
aria-label={`${field.name} item ${i + 1}`}
onChange={(e) =>
onChange(items.map((v, j) => (j === i ? e.target.value : v)))
}
className={`${inputClass} ${borderClass(invalid)}`}
/>
<button
type="button"
onClick={() => onChange(items.filter((_, j) => j !== i))}
disabled={items.length === 1 && !item}
aria-label={`Remove ${field.name} item ${i + 1}`}
className="p-1.5 rounded-md text-gray-400 hover:text-red-500 disabled:opacity-30"
>
<X className="size-3.5" />
</button>
</div>
))}
<button
type="button"
onClick={() => onChange([...items, ""])}
className="flex items-center gap-1 text-xs text-violet-600 dark:text-violet-400 hover:underline"
>
<Plus className="size-3" />
Add item
</button>
</div>
);
};
export const RunFieldControl: React.FC<{
field: RunField;
value: FormValue;
error?: string;
onChange: (v: FormValue) => void;
/** Prefixes the control's id, so two forms on one page cannot collide. */
idPrefix?: string;
}> = ({ field, value, error, onChange, idPrefix = "run-field" }) => {
const id = `${idPrefix}-${field.name}`;
const invalid = !!error;
const hint = SOURCE_HINT[field.source]?.(field);
let control: React.ReactNode;
switch (field.type) {
case "boolean":
control = (
<label className="inline-flex items-center gap-2 text-sm text-gray-700 dark:text-gray-300">
<input
id={id}
type="checkbox"
checked={value === true}
onChange={(e) => onChange(e.target.checked)}
className="size-4 rounded border-gray-300 text-violet-600 focus:ring-violet-300"
/>
{value === true ? "Yes" : "No"}
</label>
);
break;
case "enum":
control = (
<select
id={id}
value={String(value)}
onChange={(e) => onChange(e.target.value)}
className={`${inputClass} ${borderClass(invalid)}`}
>
<option value="">{field.required ? "Choose…" : "—"}</option>
{(field.values ?? []).map((v) => (
<option key={v} value={v}>
{v}
</option>
))}
</select>
);
break;
case "number":
case "integer":
control = (
<input
id={id}
type="number"
step={field.type === "integer" ? 1 : "any"}
value={String(value)}
onChange={(e) => onChange(e.target.value)}
className={`${inputClass} ${borderClass(invalid)}`}
/>
);
break;
case "array":
control = (
<ArrayControl
field={field}
value={Array.isArray(value) ? value : []}
invalid={invalid}
onChange={onChange}
/>
);
break;
case "object":
control = (
<textarea
id={id}
value={String(value)}
rows={5}
spellCheck={false}
placeholder="{ }"
onChange={(e) => onChange(e.target.value)}
className={`${inputClass} ${borderClass(invalid)} font-mono text-xs resize-y`}
/>
);
break;
default:
control = (
<textarea
id={id}
value={String(value)}
rows={2}
onChange={(e) => onChange(e.target.value)}
className={`${inputClass} ${borderClass(invalid)} resize-y`}
/>
);
}
const wide = field.type === "object" || field.type === "array";
return (
// A container query, not a breakpoint: the grid holding the fields is
// as wide as the card or chat it sits in, not the window.
<div className={wide ? "@md:col-span-2" : undefined}>
<label
htmlFor={field.type === "array" ? undefined : id}
className="flex items-center gap-1.5 mb-1 text-xs font-medium text-gray-700 dark:text-gray-300"
>
<span className="font-mono">{field.name}</span>
<span className="text-[10px] font-mono text-gray-400">
{field.type === "array" ? `${field.items}[]` : field.type}
</span>
{field.required && <span className="text-red-500">*</span>}
</label>
{control}
{field.description && (
<p className="mt-1 text-[11px] text-gray-500 dark:text-gray-400">
{field.description}
</p>
)}
{hint && (
<p className="mt-0.5 text-[10px] text-gray-400 dark:text-gray-500 italic">
{hint}
</p>
)}
{error && (
<p className="mt-1 text-[11px] text-red-600 dark:text-red-400">
{error}
</p>
)}
</div>
);
};
|