'use client' import type { FC } from 'react' import type { FormValue } from '@/app/components/header/account-setting/model-provider-page/declarations' import type { CompletionParams, Model } from '@/types/app' import { RiClipboardLine } from '@remixicon/react' import copy from 'copy-to-clipboard' import { useCallback, useEffect, useState } from 'react' import { useTranslation } from 'react-i18next' import ResPlaceholder from '@/app/components/app/configuration/config/automatic/res-placeholder' import VersionSelector from '@/app/components/app/configuration/config/automatic/version-selector' import Button from '@/app/components/base/button' import { Generator } from '@/app/components/base/icons/src/vender/other' import Loading from '@/app/components/base/loading' import Modal from '@/app/components/base/modal' import Textarea from '@/app/components/base/textarea' import Toast from '@/app/components/base/toast' import { ModelTypeEnum } from '@/app/components/header/account-setting/model-provider-page/declarations' import { useModelListAndDefaultModelAndCurrentProviderAndModel } from '@/app/components/header/account-setting/model-provider-page/hooks' import ModelParameterModal from '@/app/components/header/account-setting/model-provider-page/model-parameter-modal' import { ModelModeType } from '@/types/app' import { VIBE_APPLY_EVENT, VIBE_COMMAND_EVENT } from '../../constants' import { useStore, useWorkflowStore } from '../../store' import WorkflowPreview from '../../workflow-preview' const VibePanel: FC = () => { const { t } = useTranslation() const workflowStore = useWorkflowStore() const showVibePanel = useStore(s => s.showVibePanel) const isVibeGenerating = useStore(s => s.isVibeGenerating) const vibePanelInstruction = useStore(s => s.vibePanelInstruction) const currentFlowGraph = useStore(s => s.currentVibeFlow) const versions = useStore(s => s.vibeFlowVersions) const currentVersionIndex = useStore(s => s.vibeFlowCurrentIndex) const vibePanelPreviewNodes = currentFlowGraph?.nodes || [] const vibePanelPreviewEdges = currentFlowGraph?.edges || [] const localModel = localStorage.getItem('auto-gen-model') ? JSON.parse(localStorage.getItem('auto-gen-model') as string) as Model : null const [model, setModel] = useState(localModel || { name: '', provider: '', mode: ModelModeType.chat, completion_params: {} as CompletionParams, }) const { defaultModel } = useModelListAndDefaultModelAndCurrentProviderAndModel(ModelTypeEnum.textGeneration) useEffect(() => { if (defaultModel) { const localModel = localStorage.getItem('auto-gen-model') ? JSON.parse(localStorage.getItem('auto-gen-model') || '') : null if (localModel) { setModel(localModel) } else { setModel(prev => ({ ...prev, name: defaultModel.model, provider: defaultModel.provider.provider, })) } } }, [defaultModel]) const handleModelChange = useCallback((newValue: { modelId: string, provider: string, mode?: string, features?: string[] }) => { const newModel = { ...model, provider: newValue.provider, name: newValue.modelId, mode: newValue.mode as ModelModeType, } setModel(newModel) localStorage.setItem('auto-gen-model', JSON.stringify(newModel)) }, [model]) const handleCompletionParamsChange = useCallback((newParams: FormValue) => { const newModel = { ...model, completion_params: newParams as CompletionParams, } setModel(newModel) localStorage.setItem('auto-gen-model', JSON.stringify(newModel)) }, [model]) const handleInstructionChange = useCallback((e: React.ChangeEvent) => { workflowStore.setState(state => ({ ...state, vibePanelInstruction: e.target.value, })) }, [workflowStore]) const handleClose = useCallback(() => { workflowStore.setState(state => ({ ...state, showVibePanel: false, vibePanelMermaidCode: '', isVibeGenerating: false, })) }, [workflowStore]) const handleGenerate = useCallback(() => { const event = new CustomEvent(VIBE_COMMAND_EVENT, { detail: { dsl: vibePanelInstruction }, }) document.dispatchEvent(event) }, [vibePanelInstruction]) const handleAccept = useCallback(() => { const event = new CustomEvent(VIBE_APPLY_EVENT) document.dispatchEvent(event) handleClose() }, [handleClose]) const handleCopyMermaid = useCallback(() => { const { vibePanelMermaidCode } = workflowStore.getState() copy(vibePanelMermaidCode) Toast.notify({ type: 'success', message: t('common.actionMsg.copySuccessfully') }) }, [workflowStore, t]) const handleVersionChange = useCallback((index: number) => { const { setVibeFlowCurrentIndex } = workflowStore.getState() setVibeFlowCurrentIndex(index) }, [workflowStore]) if (!showVibePanel) return null const renderLoading = (
{t('workflow.vibe.generatingFlowchart')}
) return (
{t('app.gotoAnything.actions.vibeTitle')}
{t('app.gotoAnything.actions.vibeDesc')}
{t('appDebug.generate.instruction')}