Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

fix(frontend): preserve dictionary entries when clearing a key (#12847)

djpjronline-netizen371ebd34a31a
diff --git a/autogpt_platform/backend/backend/api/features/chat/routes.py b/autogpt_platform/backend/backend/api/features/chat/routes.py
index 418a75a4ea..845147754b 100644
--- a/autogpt_platform/backend/backend/api/features/chat/routes.py
+++ b/autogpt_platform/backend/backend/api/features/chat/routes.py
@@ -1007,7 +1007,8 @@ class CredentialSelectionRequest(BaseModel):
"""The credential the user picked for each provider on a connect card."""
selections: dict[str, str] = Field(
- description="Provider slug to credential id.", max_length=20
+ description="Provider slug to credential id.", # gitleaks:allow (schema text)
+ max_length=20,
)
diff --git a/autogpt_platform/frontend/src/components/renderers/InputRenderer/__tests__/DictionaryValues.test.tsx b/autogpt_platform/frontend/src/components/renderers/InputRenderer/__tests__/DictionaryValues.test.tsx
new file mode 100644
index 0000000000..5505e56071
--- /dev/null
+++ b/autogpt_platform/frontend/src/components/renderers/InputRenderer/__tests__/DictionaryValues.test.tsx
@@ -0,0 +1,140 @@
+import type { IChangeEvent } from "@rjsf/core";
+import type { RJSFSchema } from "@rjsf/utils";
+import { fireEvent, screen, waitFor } from "@testing-library/react";
+import { useState } from "react";
+import { describe, expect, it } from "vitest";
+
+import { BlockUIType } from "@/app/(platform)/build/components/types";
+import { render } from "@/tests/integrations/test-utils";
+
+import { FormRenderer } from "../FormRenderer";
+
+interface DictionaryData {
+ prompt_values: Record<string, string>;
+}
+
+const schema: RJSFSchema = {
+ type: "object",
+ properties: {
+ prompt_values: {
+ title: "Prompt values",
+ type: "object",
+ additionalProperties: { type: "string" },
+ },
+ },
+};
+
+function DictionaryForm({ values }: { values: Record<string, string> }) {
+ const [data, setData] = useState<DictionaryData>({ prompt_values: values });
+
+ function handleChange(event: IChangeEvent<DictionaryData>) {
+ if (event.formData) setData(event.formData);
+ }
+
+ return (
+ <>
+ <FormRenderer
+ jsonSchema={schema}
+ handleChange={handleChange}
+ uiSchema={{}}
+ initialValues={data}
+ formContext={{
+ nodeId: "dictionary-node",
+ uiType: BlockUIType.STANDARD,
+ showHandles: false,
+ size: "small",
+ }}
+ />
+ <output aria-label="Stored dictionary">{JSON.stringify(data)}</output>
+ </>
+ );
+}
+
+function readDictionary(): Record<string, string> {
+ return JSON.parse(
+ screen.getByLabelText("Stored dictionary").textContent || "{}",
+ ).prompt_values;
+}
+
+describe("Dictionary input preservation", () => {
+ it("adds a new key containing an existing key without dropping either entry", async () => {
+ render(<DictionaryForm values={{ "PR-Title": "First" }} />);
+
+ fireEvent.click(screen.getByRole("button", { name: /add item/i }));
+ const newKey = screen.getByDisplayValue("newKey");
+ for (const value of ["PR", "PR-Title", "PR-Title-test"]) {
+ fireEvent.change(newKey, { target: { value } });
+ expect(screen.getByDisplayValue("First")).toBeDefined();
+ }
+ fireEvent.blur(newKey);
+
+ await waitFor(() => {
+ expect(Object.keys(readDictionary())).toEqual([
+ "PR-Title",
+ "PR-Title-test",
+ ]);
+ expect(readDictionary()["PR-Title"]).toBe("First");
+ expect(screen.getByDisplayValue("PR-Title-test")).toBeDefined();
+ });
+ });
+
+ it("keeps a populated entry when its key is cleared and blurred", async () => {
+ render(<DictionaryForm values={{ title: "Keep this value" }} />);
+
+ const key = screen.getByDisplayValue("title");
+ fireEvent.change(key, { target: { value: "" } });
+ fireEvent.blur(key);
+
+ await waitFor(() => {
+ expect(readDictionary()).toEqual({ title: "Keep this value" });
+ expect(screen.getByDisplayValue("Keep this value")).toBeDefined();
+ expect(screen.getByDisplayValue("title")).toBeDefined();
+ });
+ });
+
+ it("renames an entry without losing its value", async () => {
+ render(<DictionaryForm values={{ title: "Keep this value" }} />);
+
+ const key = screen.getByDisplayValue("title");
+ fireEvent.change(key, { target: { value: "new-title" } });
+ fireEvent.blur(key);
+
+ await waitFor(() => {
+ expect(readDictionary()).toEqual({ "new-title": "Keep this value" });
+ });
+ });
+
+ it("removes an entry only through its remove action", async () => {
+ render(<DictionaryForm values={{ title: "Remove this value" }} />);
+
+ fireEvent.click(screen.getByRole("button", { name: /remove item/i }));
+
+ await waitFor(() => {
+ expect(readDictionary()).toEqual({});
+ expect(screen.queryByDisplayValue("Remove this value")).toBeNull();
+ });
+ });
+
+ it.each([
+ ["PR-Title", "PR-Title-test"],
+ ["key[0]", "key[0]-suffix"],
+ ["cost$&", "cost$&-suffix"],
+ ["cost$$", "cost$$-suffix"],
+ ])("preserves both values while editing %s and %s", async (first, second) => {
+ render(
+ <DictionaryForm values={{ [first]: "First", [second]: "Second" }} />,
+ );
+
+ fireEvent.change(screen.getByDisplayValue("Second"), {
+ target: { value: "Updated second" },
+ });
+
+ await waitFor(() => {
+ expect(readDictionary()).toEqual({
+ [first]: "First",
+ [second]: "Updated second",
+ });
+ expect(screen.getByDisplayValue("First")).toBeDefined();
+ });
+ });
+});
diff --git a/autogpt_platform/frontend/src/components/renderers/InputRenderer/base/object/WrapIfAdditionalTemplate.tsx b/autogpt_platform/frontend/src/components/renderers/InputRenderer/base/object/WrapIfAdditionalTemplate.tsx
index a8b3514d41..0c38c63fd2 100644
--- a/autogpt_platform/frontend/src/components/renderers/InputRenderer/base/object/WrapIfAdditionalTemplate.tsx
+++ b/autogpt_platform/frontend/src/components/renderers/InputRenderer/base/object/WrapIfAdditionalTemplate.tsx
@@ -58,13 +58,13 @@ export default function WrapIfAdditionalTemplate(
};
const title_id = generateObjectPropertyTitleId(id, label);
- const handleBlur = (e: React.FocusEvent<HTMLInputElement>) => {
- if (e.target.value == "") {
- onRemoveProperty();
- } else {
- onKeyRenameBlur(e);
+ function handleBlur(e: React.FocusEvent<HTMLInputElement>) {
+ if (e.currentTarget.value === "") {
+ e.currentTarget.value = label;
+ return;
}
- };
+ onKeyRenameBlur(e);
+ }
const isHandleConnected = isInputConnected(nodeId, handleId);