From f60792bcc750b8174ce56b97f16b1db717444873 Mon Sep 17 00:00:00 2001 From: Joel Date: Mon, 21 Jul 2025 15:01:29 +0800 Subject: [PATCH] feat: can generate code --- .../config/automatic/get-automatic-res.tsx | 8 +- .../instruction-editor-in-workflow.tsx | 1 - .../config/automatic/prompt-toast.tsx | 16 ++- .../configuration/config/automatic/result.tsx | 4 +- .../code-generator/get-code-generator-res.tsx | 130 ++++++++---------- .../components/base/prompt-editor/index.tsx | 4 +- .../components/code-generator-button.tsx | 13 +- .../nodes/_base/components/editor/base.tsx | 1 + 8 files changed, 88 insertions(+), 89 deletions(-) diff --git a/web/app/components/app/configuration/config/automatic/get-automatic-res.tsx b/web/app/components/app/configuration/config/automatic/get-automatic-res.tsx index e14939b935..fbad952e79 100644 --- a/web/app/components/app/configuration/config/automatic/get-automatic-res.tsx +++ b/web/app/components/app/configuration/config/automatic/get-automatic-res.tsx @@ -229,17 +229,19 @@ const GetAutomaticRes: FC = ({ setLoadingTrue() try { let apiRes: GenRes + let hasError = false if (isBasicMode && !currentPrompt) { const { error, ...res } = await generateBasicAppFistTimeRule({ instruction, model_config: model, - no_varieable: false, + no_variable: false, }) apiRes = { ...res, modified: res.prompt, } as GenRes if (error) { + hasError = true Toast.notify({ type: 'error', message: error, @@ -257,13 +259,15 @@ const GetAutomaticRes: FC = ({ }) apiRes = res if (error) { + hasError = true Toast.notify({ type: 'error', message: error, }) } } - addVersion(apiRes) + if (!hasError) + addVersion(apiRes) } finally { setLoadingFalse() diff --git a/web/app/components/app/configuration/config/automatic/instruction-editor-in-workflow.tsx b/web/app/components/app/configuration/config/automatic/instruction-editor-in-workflow.tsx index 288843172f..95933ce794 100644 --- a/web/app/components/app/configuration/config/automatic/instruction-editor-in-workflow.tsx +++ b/web/app/components/app/configuration/config/automatic/instruction-editor-in-workflow.tsx @@ -33,7 +33,6 @@ const InstructionEditorInWorkflow: FC = ({ }) const getVarType = useWorkflowVariableType() - console.log(availableVars) return ( { const [isHide, { setTrue: hide, }] = useBoolean(false) - if(isHide) + if (isHide) return return ( -
+
{/* Background Effect */}
-
-
- -

This is the modified prompt with added context.

+
+
+ +

{message}

-
+
diff --git a/web/app/components/app/configuration/config/automatic/result.tsx b/web/app/components/app/configuration/config/automatic/result.tsx index c7504d3ea0..b99988bffb 100644 --- a/web/app/components/app/configuration/config/automatic/result.tsx +++ b/web/app/components/app/configuration/config/automatic/result.tsx @@ -55,8 +55,8 @@ const Result: FC = ({
{ - isGeneratorPrompt && ( - + current?.message && ( + ) }
{current?.modified}
diff --git a/web/app/components/app/configuration/config/code-generator/get-code-generator-res.tsx b/web/app/components/app/configuration/config/code-generator/get-code-generator-res.tsx index 544c74a356..0bf2f54b28 100644 --- a/web/app/components/app/configuration/config/code-generator/get-code-generator-res.tsx +++ b/web/app/components/app/configuration/config/code-generator/get-code-generator-res.tsx @@ -1,12 +1,10 @@ import type { FC } from 'react' import React, { useCallback, useEffect, useState } from 'react' -import cn from 'classnames' import useBoolean from 'ahooks/lib/useBoolean' import { useTranslation } from 'react-i18next' -import ConfigPrompt from '../../config-prompt' import { languageMap } from '../../../../workflow/nodes/_base/components/editor/code-editor/index' -import { generateRuleCode } from '@/service/debug' -import type { CodeGenRes } from '@/service/debug' +import { generateRule } from '@/service/debug' +import type { GenRes } from '@/service/debug' import type { ModelModeType } from '@/types/app' import type { AppType, CompletionParams, Model } from '@/types/app' import Modal from '@/app/components/base/modal' @@ -23,21 +21,26 @@ import type { FormValue } from '@/app/components/header/account-setting/model-pr import IdeaOutput from '../automatic/idea-output' import { GeneratorType } from '../automatic/types' import InstructionEditor from '../automatic/instruction-editor-in-workflow' +import useGenData from '../automatic/use-gen-data' +import Result from '../automatic/result' const i18nPrefix = 'appDebug.generate' export type IGetCodeGeneratorResProps = { - // flowId: string + flowId: string nodeId: string + currentCode?: string mode: AppType isShow: boolean codeLanguages: CodeLanguage onClose: () => void - onFinished: (res: CodeGenRes) => void + onFinished: (res: GenRes) => void } export const GetCodeGeneratorResModal: FC = ( { + flowId, nodeId, + currentCode, mode, isShow, codeLanguages, @@ -71,7 +74,19 @@ export const GetCodeGeneratorResModal: FC = ( const [ideaOutput, setIdeaOutput] = useState('Write comment in Janpanese') const [isLoading, { setTrue: setLoadingTrue, setFalse: setLoadingFalse }] = useBoolean(false) - const [res, setRes] = useState(null) + const storageKey = `${flowId}-${nodeId}}` + const { addVersion, current, currentVersionIndex, setCurrentVersionIndex, versions } = useGenData({ + storageKey, + }) + + // useEffect(() => { + // if (!versions?.length) { + // addVersion({ + // modified: `def main(num1: float, num2: float) -> dict: + // return {'result': num1 + num2}`, + // }) + // } + // }, []) const isValid = () => { if (instruction.trim() === '') { @@ -106,7 +121,6 @@ export const GetCodeGeneratorResModal: FC = ( localStorage.setItem('auto-gen-model', JSON.stringify(newModel)) }, [model, setModel]) - const isInLLMNode = true const onGenerate = async () => { if (!isValid()) return @@ -114,25 +128,35 @@ export const GetCodeGeneratorResModal: FC = ( return setLoadingTrue() try { - const { error, ...res } = await generateRuleCode({ + const { error, ...res } = await generateRule({ + flow_id: flowId, + node_id: nodeId, + current: currentCode, instruction, model_config: model, - no_variable: !!isInLLMNode, - code_language: languageMap[codeLanguages] || 'javascript', + idea_output: ideaOutput, + language: languageMap[codeLanguages] || 'javascript', }) - setRes(res) + if (error) { Toast.notify({ type: 'error', message: error, }) } + else { + addVersion(res) + } } finally { setLoadingFalse() } } - const [showConfirmOverwrite, setShowConfirmOverwrite] = React.useState(false) + + const [isShowConfirmOverwrite, { + setTrue: showConfirmOverwrite, + setFalse: hideShowConfirmOverwrite, + }] = useBoolean(false) useEffect(() => { if (defaultModel) { @@ -179,11 +203,10 @@ export const GetCodeGeneratorResModal: FC = ( isShow={isShow} onClose={onClose} className='min-w-[1140px] !p-0' - closable >
-
-
+
+
{t('appDebug.codegen.title')}
{t('appDebug.codegen.description')}
@@ -203,13 +226,7 @@ export const GetCodeGeneratorResModal: FC = (
-
{t('appDebug.codegen.instruction')}
- {/*