Commit 36718c39 authored by StyleZhang's avatar StyleZhang

features

parent fca97531
'use client' 'use client'
import type { FC } from 'react' import type { FC } from 'react'
import React, { useCallback } from 'react' import React, { useCallback } from 'react'
import produce from 'immer'
import cn from 'classnames' import cn from 'classnames'
import s from './style.module.css' import s from './style.module.css'
import Switch from '@/app/components/base/switch' import Switch from '@/app/components/base/switch'
import type { FeatureEnum } from '@/app/components/base/features/types' import { FeatureEnum } from '@/app/components/base/features/types'
import { useFeaturesStore } from '@/app/components/base/features/hooks'
import { useModalContext } from '@/context/modal-context'
export type IFeatureItemProps = { export type IFeatureItemProps = {
icon: React.ReactNode icon: React.ReactNode
...@@ -25,9 +28,43 @@ const FeatureItem: FC<IFeatureItemProps> = ({ ...@@ -25,9 +28,43 @@ const FeatureItem: FC<IFeatureItemProps> = ({
onChange, onChange,
type, type,
}) => { }) => {
const featuresStore = useFeaturesStore()
const { setShowModerationSettingModal } = useModalContext()
const handleChange = useCallback((newValue: boolean) => { const handleChange = useCallback((newValue: boolean) => {
const {
features,
setFeatures,
} = featuresStore!.getState()
if (newValue && !features.moderation.type && type === FeatureEnum.moderation) {
setShowModerationSettingModal({
payload: {
enabled: true,
type: 'keywords',
config: {
keywords: '',
inputs_config: {
enabled: true,
preset_response: '',
},
},
},
onSaveCallback: (newModeration) => {
setFeatures(produce(features, (draft) => {
draft.moderation = newModeration
}))
},
onCancelCallback: () => {
setFeatures(produce(features, (draft) => {
draft.moderation = { enabled: false }
}))
},
})
return
}
onChange(type, newValue) onChange(type, newValue)
}, [type, onChange]) }, [type, onChange, featuresStore, setShowModerationSettingModal])
return ( return (
<div className={cn(s.wrap, 'relative flex justify-between p-3 rounded-xl border border-transparent bg-gray-50 hover:border-gray-200 cursor-pointer')}> <div className={cn(s.wrap, 'relative flex justify-between p-3 rounded-xl border border-transparent bg-gray-50 hover:border-gray-200 cursor-pointer')}>
......
...@@ -7,6 +7,7 @@ import { ...@@ -7,6 +7,7 @@ import {
useFeatures, useFeatures,
useFeaturesStore, useFeaturesStore,
} from '../hooks' } from '../hooks'
import type { OnFeaturesChange } from '../types'
import FeatureGroup from './feature-group' import FeatureGroup from './feature-group'
import FeatureItem from './feature-item' import FeatureItem from './feature-item'
import Modal from '@/app/components/base/modal' import Modal from '@/app/components/base/modal'
...@@ -19,10 +20,9 @@ import { ...@@ -19,10 +20,9 @@ import {
MessageHeartCircle, MessageHeartCircle,
} from '@/app/components/base/icons/src/vender/solid/communication' } from '@/app/components/base/icons/src/vender/solid/communication'
import { FeatureEnum } from '@/app/components/base/features/types' import { FeatureEnum } from '@/app/components/base/features/types'
import type { Features } from '@/app/components/base/features/types'
export type FeatureModalProps = { export type FeatureModalProps = {
onChange?: (features: Features) => void onChange?: OnFeaturesChange
showTextToSpeechItem?: boolean showTextToSpeechItem?: boolean
showSpeechToTextItem?: boolean showSpeechToTextItem?: boolean
} }
......
...@@ -2,13 +2,13 @@ ...@@ -2,13 +2,13 @@
import React from 'react' import React from 'react'
import { useTranslation } from 'react-i18next' import { useTranslation } from 'react-i18next'
import { useFeatures } from '../hooks' import { useFeatures } from '../hooks'
import type { OnFeaturesChange } from '../types'
import FeatureModal from './feature-modal' import FeatureModal from './feature-modal'
import Button from '@/app/components/base/button' import Button from '@/app/components/base/button'
import { Plus02 } from '@/app/components/base/icons/src/vender/line/general' import { Plus02 } from '@/app/components/base/icons/src/vender/line/general'
import type { Features } from '@/app/components/base/features/types'
type ChooseFeatureProps = { type ChooseFeatureProps = {
onChange?: (features: Features) => void onChange?: OnFeaturesChange
} }
const ChooseFeature = ({ const ChooseFeature = ({
onChange, onChange,
......
...@@ -3,6 +3,7 @@ import { ...@@ -3,6 +3,7 @@ import {
useMemo, useMemo,
} from 'react' } from 'react'
import { useTranslation } from 'react-i18next' import { useTranslation } from 'react-i18next'
import type { OnFeaturesChange } from '../types'
import { useFeatures } from '../hooks' import { useFeatures } from '../hooks'
import OpeningStatement from './opening-statement' import OpeningStatement from './opening-statement'
import type { OpeningStatementProps } from './opening-statement' import type { OpeningStatementProps } from './opening-statement'
...@@ -11,14 +12,16 @@ import TextToSpeech from './text-to-speech' ...@@ -11,14 +12,16 @@ import TextToSpeech from './text-to-speech'
import SpeechToText from './speech-to-text' import SpeechToText from './speech-to-text'
import Citation from './citation' import Citation from './citation'
import Moderation from './moderation' import Moderation from './moderation'
import Annotation from './annotation/config-param' import Annotation from './annotation'
import type { AnnotationProps } from './annotation/config-param' import type { AnnotationProps } from './annotation'
export type FeaturePanelProps = { export type FeaturePanelProps = {
onChange?: OnFeaturesChange
openingStatementProps: OpeningStatementProps openingStatementProps: OpeningStatementProps
annotationProps: AnnotationProps annotationProps: AnnotationProps
} }
const FeaturePanel = ({ const FeaturePanel = ({
onChange,
openingStatementProps, openingStatementProps,
annotationProps, annotationProps,
}: FeaturePanelProps) => { }: FeaturePanelProps) => {
...@@ -50,7 +53,7 @@ const FeaturePanel = ({ ...@@ -50,7 +53,7 @@ const FeaturePanel = ({
<div className='py-2 space-y-2'> <div className='py-2 space-y-2'>
{ {
features.opening.enabled && ( features.opening.enabled && (
<OpeningStatement {...openingStatementProps} /> <OpeningStatement {...openingStatementProps} onChange={onChange} />
) )
} }
{ {
...@@ -92,7 +95,7 @@ const FeaturePanel = ({ ...@@ -92,7 +95,7 @@ const FeaturePanel = ({
<div className='py-2 space-y-2'> <div className='py-2 space-y-2'>
{ {
features.moderation.enabled && ( features.moderation.enabled && (
<Moderation /> <Moderation onChange={onChange} />
) )
} }
{ {
......
...@@ -7,13 +7,19 @@ import { ...@@ -7,13 +7,19 @@ import {
useFeatures, useFeatures,
useFeaturesStore, useFeaturesStore,
} from '../../hooks' } from '../../hooks'
import type { OnFeaturesChange } from '../../types'
import { FileSearch02 } from '@/app/components/base/icons/src/vender/solid/files' import { FileSearch02 } from '@/app/components/base/icons/src/vender/solid/files'
import { Settings01 } from '@/app/components/base/icons/src/vender/line/general' import { Settings01 } from '@/app/components/base/icons/src/vender/line/general'
import { useModalContext } from '@/context/modal-context' import { useModalContext } from '@/context/modal-context'
import { fetchCodeBasedExtensionList } from '@/service/common' import { fetchCodeBasedExtensionList } from '@/service/common'
import I18n from '@/context/i18n' import I18n from '@/context/i18n'
const Moderation = () => { type ModerationProps = {
onChange?: OnFeaturesChange
}
const Moderation = ({
onChange,
}: ModerationProps) => {
const { t } = useTranslation() const { t } = useTranslation()
const { setShowModerationSettingModal } = useModalContext() const { setShowModerationSettingModal } = useModalContext()
const { locale } = useContext(I18n) const { locale } = useContext(I18n)
...@@ -33,9 +39,12 @@ const Moderation = () => { ...@@ -33,9 +39,12 @@ const Moderation = () => {
setShowModerationSettingModal({ setShowModerationSettingModal({
payload: moderation as any, payload: moderation as any,
onSaveCallback: (newModeration) => { onSaveCallback: (newModeration) => {
setFeatures(produce(features, (draft) => { const newFeatures = produce(features, (draft) => {
draft.moderation = newModeration draft.moderation = newModeration
})) })
setFeatures(newFeatures)
if (onChange)
onChange(newFeatures)
}, },
}) })
} }
......
...@@ -11,6 +11,7 @@ import { ...@@ -11,6 +11,7 @@ import {
useFeatures, useFeatures,
useFeaturesStore, useFeaturesStore,
} from '../../hooks' } from '../../hooks'
import type { OnFeaturesChange } from '../../types'
import Panel from '@/app/components/app/configuration/base/feature-panel' import Panel from '@/app/components/app/configuration/base/feature-panel'
import Button from '@/app/components/base/button' import Button from '@/app/components/base/button'
import OperationBtn from '@/app/components/app/configuration/base/operation-btn' import OperationBtn from '@/app/components/app/configuration/base/operation-btn'
...@@ -24,6 +25,7 @@ import type { PromptVariable } from '@/models/debug' ...@@ -24,6 +25,7 @@ import type { PromptVariable } from '@/models/debug'
const MAX_QUESTION_NUM = 5 const MAX_QUESTION_NUM = 5
export type OpeningStatementProps = { export type OpeningStatementProps = {
onChange?: OnFeaturesChange
readonly?: boolean readonly?: boolean
promptVariables?: PromptVariable[] promptVariables?: PromptVariable[]
onAutoAddPromptVariable: (variable: PromptVariable[]) => void onAutoAddPromptVariable: (variable: PromptVariable[]) => void
...@@ -33,6 +35,7 @@ export type OpeningStatementProps = { ...@@ -33,6 +35,7 @@ export type OpeningStatementProps = {
const regex = /\{\{([^}]+)\}\}/g const regex = /\{\{([^}]+)\}\}/g
const OpeningStatement: FC<OpeningStatementProps> = ({ const OpeningStatement: FC<OpeningStatementProps> = ({
onChange,
readonly, readonly,
promptVariables = [], promptVariables = [],
onAutoAddPromptVariable, onAutoAddPromptVariable,
...@@ -112,10 +115,14 @@ const OpeningStatement: FC<OpeningStatementProps> = ({ ...@@ -112,10 +115,14 @@ const OpeningStatement: FC<OpeningStatementProps> = ({
setFeatures, setFeatures,
} = getState() } = getState()
setFeatures(produce(features, (draft) => { const newFeatures = produce(features, (draft) => {
draft.opening.opening_statement = tempValue draft.opening.opening_statement = tempValue
draft.opening.suggested_questions = tempSuggestedQuestions draft.opening.suggested_questions = tempSuggestedQuestions
})) })
setFeatures(newFeatures)
if (onChange)
onChange(newFeatures)
} }
const cancelAutoAddVar = () => { const cancelAutoAddVar = () => {
...@@ -125,9 +132,13 @@ const OpeningStatement: FC<OpeningStatementProps> = ({ ...@@ -125,9 +132,13 @@ const OpeningStatement: FC<OpeningStatementProps> = ({
setFeatures, setFeatures,
} = getState() } = getState()
setFeatures(produce(features, (draft) => { const newFeatures = produce(features, (draft) => {
draft.opening.opening_statement = tempValue draft.opening.opening_statement = tempValue
})) })
setFeatures(newFeatures)
if (onChange)
onChange(newFeatures)
hideConfirmAddVar() hideConfirmAddVar()
setBlur() setBlur()
} }
...@@ -139,9 +150,12 @@ const OpeningStatement: FC<OpeningStatementProps> = ({ ...@@ -139,9 +150,12 @@ const OpeningStatement: FC<OpeningStatementProps> = ({
setFeatures, setFeatures,
} = getState() } = getState()
setFeatures(produce(features, (draft) => { const newFeatures = produce(features, (draft) => {
draft.opening.opening_statement = tempValue draft.opening.opening_statement = tempValue
})) })
setFeatures(newFeatures)
if (onChange)
onChange(newFeatures)
onAutoAddPromptVariable([...notIncludeKeys.map(key => getNewVar(key, 'string'))]) onAutoAddPromptVariable([...notIncludeKeys.map(key => getNewVar(key, 'string'))])
hideConfirmAddVar() hideConfirmAddVar()
setBlur() setBlur()
......
...@@ -51,3 +51,5 @@ export type Features = { ...@@ -51,3 +51,5 @@ export type Features = {
[FeatureEnum.moderation]: SensitiveWordAvoidance [FeatureEnum.moderation]: SensitiveWordAvoidance
[FeatureEnum.annotation]: AnnotationReply [FeatureEnum.annotation]: AnnotationReply
} }
export type OnFeaturesChange = (features: Features) => void
import { memo } from 'react' import {
memo,
useCallback,
} from 'react'
import { useTranslation } from 'react-i18next' import { useTranslation } from 'react-i18next'
import { useStore } from './store' import { useStore } from './store'
import { useWorkflow } from './hooks'
import { XClose } from '@/app/components/base/icons/src/vender/line/general' import { XClose } from '@/app/components/base/icons/src/vender/line/general'
import { import {
FeaturesChoose, FeaturesChoose,
...@@ -10,13 +14,18 @@ import { ...@@ -10,13 +14,18 @@ import {
const Features = () => { const Features = () => {
const { t } = useTranslation() const { t } = useTranslation()
const setShowFeaturesPanel = useStore(state => state.setShowFeaturesPanel) const setShowFeaturesPanel = useStore(state => state.setShowFeaturesPanel)
const { handleSyncWorkflowDraft } = useWorkflow()
const handleFeaturesChange = useCallback(() => {
handleSyncWorkflowDraft()
}, [handleSyncWorkflowDraft])
return ( return (
<div className='fixed top-16 left-2 bottom-2 w-[600px] rounded-2xl border-[0.5px] border-gray-200 bg-white shadow-xl z-10'> <div className='fixed top-16 left-2 bottom-2 w-[600px] rounded-2xl border-[0.5px] border-gray-200 bg-white shadow-xl z-10'>
<div className='flex items-center justify-between px-4 pt-3'> <div className='flex items-center justify-between px-4 pt-3'>
{t('workflow.common.features')} {t('workflow.common.features')}
<div className='flex items-center'> <div className='flex items-center'>
<FeaturesChoose /> <FeaturesChoose onChange={handleFeaturesChange} />
<div className='mx-3 w-[1px] h-[14px] bg-gray-200'></div> <div className='mx-3 w-[1px] h-[14px] bg-gray-200'></div>
<div <div
className='flex items-center justify-center w-6 h-6 cursor-pointer' className='flex items-center justify-center w-6 h-6 cursor-pointer'
......
...@@ -17,7 +17,7 @@ const EditingTitle = () => { ...@@ -17,7 +17,7 @@ const EditingTitle = () => {
draftUpdatedAt && ( draftUpdatedAt && (
<> <>
<span className='flex items-center mx-1'>·</span> <span className='flex items-center mx-1'>·</span>
{t('workflow.common.autoSaved')} {dayjs(draftUpdatedAt).format('HH:mm:ss')} {t('workflow.common.autoSaved')} {dayjs(draftUpdatedAt * 1000).format('HH:mm:ss')}
</> </>
) )
} }
......
...@@ -52,6 +52,7 @@ export const useWorkflow = () => { ...@@ -52,6 +52,7 @@ export const useWorkflow = () => {
const appId = useAppStore.getState().appDetail?.id const appId = useAppStore.getState().appDetail?.id
if (appId) { if (appId) {
const features = featuresStore!.getState().features
syncWorkflowDraft({ syncWorkflowDraft({
url: `/apps/${appId}/workflows/draft`, url: `/apps/${appId}/workflows/draft`,
params: { params: {
...@@ -60,11 +61,22 @@ export const useWorkflow = () => { ...@@ -60,11 +61,22 @@ export const useWorkflow = () => {
edges, edges,
viewport: getViewport(), viewport: getViewport(),
}, },
features: {}, features: {
opening_statement: features.opening.opening_statement,
suggested_questions: features.opening.suggested_questions,
suggested_questions_after_answer: features.suggested,
text_to_speech: features.text2speech,
speech_to_text: features.speech2text,
retriever_resource: features.citation,
sensitive_word_avoidance: features.moderation,
annotation_reply: features.annotation,
}, },
},
}).then((res) => {
useStore.setState({ draftUpdatedAt: res.updated_at })
}) })
} }
}, [store, reactFlow]) }, [store, reactFlow, featuresStore])
const handleLayout = useCallback(async () => { const handleLayout = useCallback(async () => {
const { const {
......
...@@ -9,8 +9,8 @@ export const fetchWorkflowDraft: Fetcher<FetchWorkflowDraftResponse, string> = ( ...@@ -9,8 +9,8 @@ export const fetchWorkflowDraft: Fetcher<FetchWorkflowDraftResponse, string> = (
return get<FetchWorkflowDraftResponse>(url, {}, { silent: true }) return get<FetchWorkflowDraftResponse>(url, {}, { silent: true })
} }
export const syncWorkflowDraft: Fetcher<CommonResponse, { url: string; params: Pick<FetchWorkflowDraftResponse, 'graph' | 'features'> }> = ({ url, params }) => { export const syncWorkflowDraft = ({ url, params }: { url: string; params: Pick<FetchWorkflowDraftResponse, 'graph' | 'features'> }) => {
return post<CommonResponse>(url, { body: params }) return post<CommonResponse & { updated_at: number }>(url, { body: params })
} }
export const fetchNodesDefaultConfigs: Fetcher<any, string> = (url) => { export const fetchNodesDefaultConfigs: Fetcher<any, string> = (url) => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment