Skip to content

Commit b2e86d7

Browse files
fix(web): an invalid provider setting is refused at its field instead of taking the provider offline (#35100)
1 parent 12d4c62 commit b2e86d7

2 files changed

Lines changed: 79 additions & 14 deletions

File tree

‎apps/web/src/components/settings/ProviderSettingsForm.test.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,24 @@ describe("ProviderSettingsForm helpers", () => {
5252
expect(next).toEqual({ forkOwned: 1 });
5353
});
5454

55+
it("does not store a text value its field's schema rejects", () => {
56+
const claude = providerClients.get(ProviderDriverKind.make("claudeAgent"));
57+
expect(claude).toBeDefined();
58+
const autoCompactWindow = deriveProviderSettingsFields(claude!).find(
59+
(field) => field.key === "autoCompactWindow",
60+
);
61+
expect(autoCompactWindow).toBeDefined();
62+
63+
for (const invalid of ["300k", "50000"]) {
64+
expect(
65+
nextProviderConfigWithFieldValue({ launchArgs: "--x" }, autoCompactWindow!, invalid),
66+
).toEqual({ launchArgs: "--x" });
67+
}
68+
expect(
69+
nextProviderConfigWithFieldValue({ launchArgs: "--x" }, autoCompactWindow!, "300000"),
70+
).toEqual({ launchArgs: "--x", autoCompactWindow: "300000" });
71+
});
72+
5573
it("stores false boolean fields when true is the default", () => {
5674
const next = nextProviderConfigWithFieldValue(
5775
undefined,

‎apps/web/src/components/settings/ProviderSettingsForm.tsx‎

Lines changed: 61 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ export interface ProviderSettingsFieldModel {
4242
/** Typed `SettingsSecretValue`: may name a secret provider reference instead of a value. */
4343
readonly acceptsSecretReference?: boolean | undefined;
4444
readonly defaultBooleanValue?: boolean | undefined;
45+
/** Whether the field's schema decodes a typed string; one it rejects is never stored. */
46+
readonly accepts?: ((value: string) => boolean) | undefined;
4547

4648
readonly options?: ReadonlyArray<ProviderSettingsFormOption> | undefined;
4749
}
@@ -89,6 +91,13 @@ function readFieldBooleanDefault(
8991
return Option.isSome(decoded) && typeof decoded.value === "boolean" ? decoded.value : undefined;
9092
}
9193

94+
function fieldAcceptsString(
95+
fieldSchema: ProviderClientDefinition["settingsSchema"]["fields"][string],
96+
): (value: string) => boolean {
97+
const decode = Schema.decodeUnknownOption(fieldSchema as Schema.Decoder<unknown>);
98+
return (value) => Option.isSome(decode(value));
99+
}
100+
92101
export function deriveProviderSettingsFields(
93102
definition: SettingsFormDefinition,
94103
value?: unknown,
@@ -142,6 +151,7 @@ export function deriveProviderSettingsFields(
142151
...(formAnnotation.control === "switch"
143152
? { defaultBooleanValue: readFieldBooleanDefault(fieldSchema) }
144153
: {}),
154+
accepts: fieldAcceptsString(fieldSchema),
145155
...(formAnnotation.control === "select" && formAnnotation.options
146156
? { options: formAnnotation.options }
147157
: {}),
@@ -322,7 +332,7 @@ export function nextProviderConfigWithFieldValue(
322332
const trimmed = value.trim();
323333
if (field.clearWhenEmpty === "omit" && trimmed.length === 0) {
324334
delete base[field.key];
325-
} else {
335+
} else if (field.accepts?.(value) !== false) {
326336
base[field.key] = value;
327337
}
328338
return Object.keys(base).length > 0 ? base : undefined;
@@ -434,6 +444,50 @@ function ProviderSettingsSelect({
434444
);
435445
}
436446

447+
function ProviderSettingsTextInput({
448+
field,
449+
value,
450+
inputId,
451+
descriptionId,
452+
onChange,
453+
}: {
454+
readonly field: ProviderSettingsFieldModel;
455+
readonly value: unknown;
456+
readonly inputId: string;
457+
readonly descriptionId: string | undefined;
458+
readonly onChange: ProviderSettingsFormProps["onChange"];
459+
}) {
460+
const [rejected, setRejected] = useState<string | null>(null);
461+
return (
462+
<div className="flex w-full min-w-0 flex-col gap-1 @min-[32rem]/settings-row:w-56">
463+
<DraftInput
464+
id={inputId}
465+
aria-describedby={descriptionId}
466+
size="sm"
467+
className="w-full max-w-full"
468+
type={field.control === "password" ? "password" : undefined}
469+
autoComplete={field.control === "password" ? "off" : undefined}
470+
value={rejected ?? readProviderConfigString(value, field.key)}
471+
placeholder={field.placeholder}
472+
onCommit={(next) => {
473+
const clears = field.clearWhenEmpty === "omit" && next.trim().length === 0;
474+
if (!clears && field.accepts?.(next) === false) {
475+
setRejected(next);
476+
return;
477+
}
478+
setRejected(null);
479+
onChange(nextProviderConfigWithFieldValue(value, field, next));
480+
}}
481+
aria-invalid={rejected !== null || undefined}
482+
spellCheck={false}
483+
/>
484+
{rejected !== null ? (
485+
<p className="text-2xs text-destructive">Enter a valid value. It was not saved.</p>
486+
) : null}
487+
</div>
488+
);
489+
}
490+
437491
function FieldFrame(props: {
438492
readonly variant: ProviderSettingsFormProps["variant"];
439493
readonly children: ReactNode;
@@ -541,19 +595,12 @@ function ProviderSettingsFieldRow({
541595
) : null}
542596
</div>
543597
) : (
544-
<DraftInput
545-
id={inputId}
546-
aria-describedby={descriptionId}
547-
size="sm"
548-
className="w-full max-w-full @min-[32rem]/settings-row:w-56"
549-
type={field.control === "password" ? "password" : undefined}
550-
autoComplete={field.control === "password" ? "off" : undefined}
551-
value={readProviderConfigString(value, field.key)}
552-
placeholder={field.placeholder}
553-
onCommit={(next) => {
554-
onChange(nextProviderConfigWithFieldValue(value, field, next));
555-
}}
556-
spellCheck={false}
598+
<ProviderSettingsTextInput
599+
field={field}
600+
value={value}
601+
inputId={inputId}
602+
descriptionId={descriptionId}
603+
onChange={onChange}
557604
/>
558605
);
559606

0 commit comments

Comments
 (0)