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): stop transformDates rewriting user inputs and zone-less strings (#15331)

Nicholas Tindle7f49471749e0
diff --git a/autogpt_platform/frontend/src/app/api/mutators/__tests__/custom-mutator.test.ts b/autogpt_platform/frontend/src/app/api/mutators/__tests__/custom-mutator.test.ts
index bee3de4250..329032c1f6 100644
--- a/autogpt_platform/frontend/src/app/api/mutators/__tests__/custom-mutator.test.ts
+++ b/autogpt_platform/frontend/src/app/api/mutators/__tests__/custom-mutator.test.ts
@@ -349,3 +349,46 @@ describe("customMutator validation errors", () => {
});
});
});
+
+describe("customMutator — response dates", () => {
+ beforeEach(() => {
+ vi.clearAllMocks();
+ mockIsClientSide.mockReturnValue(true);
+ mockGetSystemHeaders.mockReturnValue({});
+ });
+
+ it("returns server timestamps as Dates and user inputs verbatim", async () => {
+ const execution = {
+ id: "exec-1",
+ started_at: "2024-03-10T09:30:00Z",
+ inputs: { when: "2024-03-10T09:30:00", at: "2024-03-10T09:30:00Z" },
+ nodes: [{ input_default: { when: "2024-03-10T09:30:00Z" } }],
+ };
+ vi.stubGlobal(
+ "fetch",
+ vi.fn().mockResolvedValue({
+ ok: true,
+ status: 200,
+ headers: new Headers({ "content-type": "application/json" }),
+ json: () => Promise.resolve(execution),
+ }),
+ );
+
+ const res = await customMutator<{
+ data: typeof execution;
+ status: number;
+ headers: Headers;
+ }>("/test", {
+ method: "GET",
+ });
+
+ expect(res.data.started_at).toBeInstanceOf(Date);
+ expect(res.data.inputs).toEqual(execution.inputs);
+ expect(res.data.nodes[0].input_default).toEqual(
+ execution.nodes[0].input_default,
+ );
+ expect(JSON.parse(JSON.stringify(res.data.inputs))).toEqual(
+ execution.inputs,
+ );
+ });
+});
diff --git a/autogpt_platform/frontend/src/app/api/mutators/__tests__/date-transformer.test.ts b/autogpt_platform/frontend/src/app/api/mutators/__tests__/date-transformer.test.ts
new file mode 100644
index 0000000000..812b9924a3
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/api/mutators/__tests__/date-transformer.test.ts
@@ -0,0 +1,63 @@
+import { describe, expect, it } from "vitest";
+
+import { transformDates } from "../date-transformer";
+
+describe("transformDates", () => {
+ it("converts server timestamps with an explicit Z to Date", () => {
+ const out = transformDates({ created_at: "2024-03-10T09:30:00Z" });
+ expect(out.created_at).toBeInstanceOf(Date);
+ expect((out.created_at as unknown as Date).toISOString()).toBe(
+ "2024-03-10T09:30:00.000Z",
+ );
+ });
+
+ it("leaves zone-less timestamps as strings", () => {
+ const out = transformDates({ started_at: "2024-03-10T09:30:00" });
+ expect(out.started_at).toBe("2024-03-10T09:30:00");
+ });
+
+ it("does not touch user graph inputs/outputs", () => {
+ const payload = {
+ id: "exec-1",
+ created_at: "2024-03-10T09:30:00Z",
+ inputs: { when: "2024-03-10T09:30:00", at: "2024-03-10T09:30:00Z" },
+ outputs: { t: ["2024-03-10T09:30:00Z"] },
+ input_data: { when: "2024-03-10T09:30:00Z" },
+ output_data: { t: ["2024-03-10T09:30:00Z"] },
+ credential_inputs: { c: { expires: "2024-03-10T09:30:00Z" } },
+ nodes_input_masks: { n1: { when: "2024-03-10T09:30:00Z" } },
+ input_default: { when: "2024-03-10T09:30:00Z" },
+ payload: { when: "2024-03-10T09:30:00Z" },
+ reviewed_data: { when: "2024-03-10T09:30:00Z" },
+ agentInput: { when: "2024-03-10T09:30:00Z" },
+ };
+ const out = transformDates(payload);
+
+ expect(out.created_at).toBeInstanceOf(Date);
+ for (const key of [
+ "inputs",
+ "outputs",
+ "input_data",
+ "output_data",
+ "credential_inputs",
+ "nodes_input_masks",
+ "input_default",
+ "payload",
+ "reviewed_data",
+ "agentInput",
+ ] as const) {
+ expect(JSON.stringify(out[key])).toBe(JSON.stringify(payload[key]));
+ }
+ });
+
+ it("skips user payloads nested inside lists of executions", () => {
+ const out = transformDates([
+ {
+ started_at: "2024-03-10T09:30:00Z",
+ inputs: { d: "2024-03-10T09:30:00Z" },
+ },
+ ]);
+ expect(out[0].started_at).toBeInstanceOf(Date);
+ expect(out[0].inputs.d).toBe("2024-03-10T09:30:00Z");
+ });
+});
diff --git a/autogpt_platform/frontend/src/app/api/mutators/date-transformer.ts b/autogpt_platform/frontend/src/app/api/mutators/date-transformer.ts
index 575495a3db..d4f456a43c 100644
--- a/autogpt_platform/frontend/src/app/api/mutators/date-transformer.ts
+++ b/autogpt_platform/frontend/src/app/api/mutators/date-transformer.ts
@@ -3,8 +3,26 @@
* in API responses. This handles the conversion recursively for nested objects.
*/
-// ISO date regex pattern to match strings that look like ISO dates
-const ISO_DATE_REGEX = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?Z?$/;
+// ISO date regex pattern to match strings that look like UTC ISO dates.
+// The trailing `Z` is required: a zone-less string ("2024-03-10T09:30:00")
+// would otherwise be parsed as browser-local time and shift on re-serialize.
+const ISO_DATE_REGEX = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?Z$/;
+
+// Keys holding user-supplied data. Their values are copied verbatim so
+// "Run again", saving a graph or approving a review resubmits exactly what
+// the user entered.
+const USER_PAYLOAD_KEYS = new Set([
+ "inputs",
+ "outputs",
+ "input_data",
+ "output_data",
+ "credential_inputs",
+ "nodes_input_masks",
+ "input_default",
+ "payload",
+ "reviewed_data",
+ "agentInput",
+]);
/**
* Validates if a string is a valid ISO date and can be parsed
@@ -35,7 +53,10 @@ export function transformDates<T>(obj: T): T {
const transformed = {} as T;
for (const [key, value] of Object.entries(obj)) {
- if (typeof value === "string" && isValidISODate(value)) {
+ if (USER_PAYLOAD_KEYS.has(key)) {
+ // User data, not server timestamps: leave untouched
+ (transformed as any)[key] = value;
+ } else if (typeof value === "string" && isValidISODate(value)) {
// Convert ISO date string to Date object
(transformed as any)[key] = new Date(value);
} else if (typeof value === "object") {