From 18762f574bcdf9813d45ea43b8caf35fe90a10a4 Mon Sep 17 00:00:00 2001 From: Shuchang Zheng Date: Fri, 6 Dec 2024 06:50:03 -0800 Subject: [PATCH] Resolve prolems with saved task (#1340) --- .../tasks/create/CreateNewTaskFormPage.tsx | 5 +- .../src/routes/tasks/create/SavedTaskForm.tsx | 60 ++++++------------- 2 files changed, 22 insertions(+), 43 deletions(-) diff --git a/skyvern-frontend/src/routes/tasks/create/CreateNewTaskFormPage.tsx b/skyvern-frontend/src/routes/tasks/create/CreateNewTaskFormPage.tsx index 01cbb9dba..8e36aa3bd 100644 --- a/skyvern-frontend/src/routes/tasks/create/CreateNewTaskFormPage.tsx +++ b/skyvern-frontend/src/routes/tasks/create/CreateNewTaskFormPage.tsx @@ -121,7 +121,10 @@ function CreateNewTaskFormPage() { dataExtractionGoal: data.workflow_definition.blocks[0].data_extraction_goal, extractedInformationSchema: JSON.stringify(dataSchema, null, 2), - navigationPayload, + navigationPayload: + typeof navigationPayload === "string" + ? navigationPayload + : JSON.stringify(navigationPayload, null, 2), maxStepsOverride, totpIdentifier: data.workflow_definition.blocks[0].totp_identifier, totpVerificationUrl: diff --git a/skyvern-frontend/src/routes/tasks/create/SavedTaskForm.tsx b/skyvern-frontend/src/routes/tasks/create/SavedTaskForm.tsx index 4c50c2732..7fec23a8b 100644 --- a/skyvern-frontend/src/routes/tasks/create/SavedTaskForm.tsx +++ b/skyvern-frontend/src/routes/tasks/create/SavedTaskForm.tsx @@ -43,61 +43,35 @@ function transform(value: unknown) { } function createTaskRequestObject(formValues: SavedTaskFormValues) { - let extractedInformationSchema = null; - if (formValues.extractedInformationSchema) { - try { - extractedInformationSchema = JSON.parse( - formValues.extractedInformationSchema, - ); - } catch (e) { - extractedInformationSchema = formValues.extractedInformationSchema; - } - } - - let errorCodeMapping = null; - if (formValues.errorCodeMapping) { - try { - errorCodeMapping = JSON.parse(formValues.errorCodeMapping); - } catch (e) { - errorCodeMapping = formValues.errorCodeMapping; - } - } - return { + title: formValues.title, url: formValues.url, webhook_callback_url: transform(formValues.webhookCallbackUrl), navigation_goal: transform(formValues.navigationGoal), data_extraction_goal: transform(formValues.dataExtractionGoal), proxy_location: transform(formValues.proxyLocation), navigation_payload: transform(formValues.navigationPayload), - extracted_information_schema: extractedInformationSchema, + extracted_information_schema: safeParseMaybeJSONString( + formValues.extractedInformationSchema, + ), totp_verification_url: transform(formValues.totpVerificationUrl), totp_identifier: transform(formValues.totpIdentifier), - error_code_mapping: errorCodeMapping, + error_code_mapping: safeParseMaybeJSONString(formValues.errorCodeMapping), }; } -function createTaskTemplateRequestObject(values: SavedTaskFormValues) { - let extractedInformationSchema = null; - if (values.extractedInformationSchema) { - try { - extractedInformationSchema = JSON.parse( - values.extractedInformationSchema, - ); - } catch (e) { - extractedInformationSchema = values.extractedInformationSchema; - } - } - - let errorCodeMapping = null; - if (values.errorCodeMapping) { +function safeParseMaybeJSONString(payload: unknown) { + if (typeof payload === "string") { try { - errorCodeMapping = JSON.parse(values.errorCodeMapping); - } catch (e) { - errorCodeMapping = values.errorCodeMapping; + return JSON.parse(payload); + } catch { + return payload; } } + return payload; +} +function createTaskTemplateRequestObject(values: SavedTaskFormValues) { return { title: values.title, description: values.description, @@ -110,7 +84,7 @@ function createTaskTemplateRequestObject(values: SavedTaskFormValues) { parameter_type: "workflow", workflow_parameter_type: "json", key: "navigation_payload", - default_value: JSON.stringify(values.navigationPayload), + default_value: safeParseMaybeJSONString(values.navigationPayload), }, ], blocks: [ @@ -120,11 +94,13 @@ function createTaskTemplateRequestObject(values: SavedTaskFormValues) { url: values.url, navigation_goal: values.navigationGoal, data_extraction_goal: values.dataExtractionGoal, - data_schema: extractedInformationSchema, + data_schema: safeParseMaybeJSONString( + values.extractedInformationSchema, + ), max_steps_per_run: values.maxStepsOverride, totp_verification_url: values.totpVerificationUrl, totp_identifier: values.totpIdentifier, - error_code_mapping: errorCodeMapping, + error_code_mapping: safeParseMaybeJSONString(values.errorCodeMapping), }, ], },