fix: gate submission on unsendable field text, and on required tool args
A field that cannot turn its text into a value reports `undefined` — which is exactly what an empty field reports. That made the two indistinguishable to every caller of `SchemaForm`, so invalid text in an optional field was submittable and the argument simply arrived absent (#2020). `SchemaForm` now tracks per-field draft validity and reports it through `onValidityChange`, covering both fields that hold text the form cannot send: the JSON editor (unparseable text) and the number input (a magnitude JS cannot hold exactly, which is dropped rather than silently rounded). The number field also states it on itself, as the JSON editor already did. A nested object form reports through the same channel, and both the field-level and form-level effects report "valid" on cleanup so a draft cannot outlive the field that held it — a remount under a new `resetKey`, or an unmounted panel. `ToolDetailPanel` gated Execute on `isExecuting` alone, so it also had no required-argument check at all — unlike the App and elicitation panels. It now uses `hasMissingRequiredFields` like they do. `InlineElicitationRequest` had neither gate and gets both. Signed-off-by: cliffhall <cliff@futurescale.com>
This commit is contained in:
parent
27a4820215
commit
6002c2ce33
2 changed files with 582 additions and 20 deletions
|
|
@ -161,4 +161,70 @@ describe("InlineElicitationRequest", () => {
|
|||
);
|
||||
expect(screen.getByText("Host")).toBeInTheDocument();
|
||||
});
|
||||
|
||||
// #2020: this inline form had no submit gate at all — neither the missing
|
||||
// required answer that `ElicitationFormPanel` already checked, nor text a
|
||||
// field could not turn into a value (which never reaches `values`).
|
||||
describe("submit gating (#2020)", () => {
|
||||
const requiredRequest = {
|
||||
message: "Please provide your name.",
|
||||
requestedSchema: {
|
||||
type: "object" as const,
|
||||
properties: { name: { type: "string" as const, title: "Name" } },
|
||||
required: ["name"],
|
||||
},
|
||||
} satisfies ElicitRequest["params"];
|
||||
|
||||
const numberRequest = {
|
||||
message: "Please provide the port.",
|
||||
requestedSchema: {
|
||||
type: "object" as const,
|
||||
properties: { port: { type: "number" as const, title: "Port number" } },
|
||||
},
|
||||
} satisfies ElicitRequest["params"];
|
||||
|
||||
it("disables Submit while a required answer is missing", () => {
|
||||
renderWithMantine(
|
||||
<InlineElicitationRequest
|
||||
{...baseProps}
|
||||
request={requiredRequest}
|
||||
values={{}}
|
||||
/>,
|
||||
);
|
||||
expect(screen.getByRole("button", { name: "Submit" })).toBeDisabled();
|
||||
});
|
||||
|
||||
it("enables Submit once the required answer is given", () => {
|
||||
renderWithMantine(
|
||||
<InlineElicitationRequest
|
||||
{...baseProps}
|
||||
request={requiredRequest}
|
||||
values={{ name: "Ada" }}
|
||||
/>,
|
||||
);
|
||||
expect(screen.getByRole("button", { name: "Submit" })).toBeEnabled();
|
||||
});
|
||||
|
||||
it("disables Submit while a field holds text it cannot send", async () => {
|
||||
const user = userEvent.setup();
|
||||
renderWithMantine(
|
||||
<InlineElicitationRequest
|
||||
{...baseProps}
|
||||
request={numberRequest}
|
||||
values={{}}
|
||||
/>,
|
||||
);
|
||||
const submit = screen.getByRole("button", { name: "Submit" });
|
||||
expect(submit).toBeEnabled();
|
||||
|
||||
// Past MAX_SAFE_INTEGER the field reports no value rather than a rounded
|
||||
// one, so the answer would go out without the property.
|
||||
const input = screen.getByLabelText(/Port number/);
|
||||
await user.type(input, "90071992547409910");
|
||||
expect(submit).toBeDisabled();
|
||||
|
||||
await user.clear(input);
|
||||
expect(submit).toBeEnabled();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -13,7 +13,11 @@ import type {
|
|||
ElicitRequestFormParams,
|
||||
} from "@modelcontextprotocol/client";
|
||||
import type { PendingRequestOrigin } from "@inspector/core/mcp/types.js";
|
||||
import type { InspectorFormSchema } from "../../../utils/jsonUtils";
|
||||
import { useState } from "react";
|
||||
import {
|
||||
hasMissingRequiredFields,
|
||||
type InspectorFormSchema,
|
||||
} from "../../../utils/jsonUtils";
|
||||
import { SchemaForm } from "../SchemaForm/SchemaForm";
|
||||
import { MrtrOriginNote } from "../../elements/MrtrOriginNote/MrtrOriginNote";
|
||||
|
||||
|
|
@ -86,6 +90,19 @@ export function InlineElicitationRequest({
|
|||
onSubmit,
|
||||
onCancel,
|
||||
}: InlineElicitationRequestProps) {
|
||||
// Same gate as `ElicitationFormPanel`, which this is the inline form of: a
|
||||
// missing required answer, or a field holding text it could not turn into a
|
||||
// value, must not be submittable — neither is visible in `values` (#2020).
|
||||
const [hasInvalidDraft, setHasInvalidDraft] = useState(false);
|
||||
const formValues = values ?? {};
|
||||
const submitDisabled =
|
||||
isFormMode(request) &&
|
||||
(hasMissingRequiredFields(
|
||||
request.requestedSchema as InspectorFormSchema,
|
||||
formValues,
|
||||
) ||
|
||||
hasInvalidDraft);
|
||||
|
||||
return (
|
||||
<RequestContainer>
|
||||
<Stack gap="sm">
|
||||
|
|
@ -100,8 +117,9 @@ export function InlineElicitationRequest({
|
|||
{isFormMode(request) && (
|
||||
<SchemaForm
|
||||
schema={request.requestedSchema as InspectorFormSchema}
|
||||
values={values ?? {}}
|
||||
values={formValues}
|
||||
onChange={onChange}
|
||||
onValidityChange={setHasInvalidDraft}
|
||||
/>
|
||||
)}
|
||||
|
||||
|
|
@ -120,7 +138,7 @@ export function InlineElicitationRequest({
|
|||
<ActionsRow>
|
||||
<CompactButton onClick={onCancel}>Cancel</CompactButton>
|
||||
{isFormMode(request) && (
|
||||
<Button size="xs" onClick={onSubmit}>
|
||||
<Button size="xs" onClick={onSubmit} disabled={submitDisabled}>
|
||||
Submit
|
||||
</Button>
|
||||
)}
|
||||
|
|
|
|||
Loading…
Reference in a new issue