mirror of https://github.com/langgenius/dify.git
57 lines
1.8 KiB
TypeScript
57 lines
1.8 KiB
TypeScript
import {
|
|
useCallback,
|
|
} from 'react'
|
|
import {
|
|
useStore,
|
|
useWorkflowStore,
|
|
} from '@/app/components/workflow/store'
|
|
import { ChatVarType } from '@/app/components/workflow/panel/chat-variable-panel/type'
|
|
import type { MemoryVariable } from '@/app/components/workflow/types'
|
|
|
|
export const useMemoryVariable = () => {
|
|
const workflowStore = useWorkflowStore()
|
|
const setMemoryVariables = useStore(s => s.setMemoryVariables)
|
|
|
|
const handleAddMemoryVariable = useCallback((memoryVariable: MemoryVariable) => {
|
|
const { memoryVariables } = workflowStore.getState()
|
|
setMemoryVariables([memoryVariable, ...memoryVariables])
|
|
}, [setMemoryVariables, workflowStore])
|
|
|
|
const handleUpdateMemoryVariable = useCallback((memoryVariable: MemoryVariable) => {
|
|
const { memoryVariables } = workflowStore.getState()
|
|
setMemoryVariables(memoryVariables.map(v => v.id === memoryVariable.id ? memoryVariable : v))
|
|
}, [setMemoryVariables, workflowStore])
|
|
|
|
const handleDeleteMemoryVariable = useCallback((memoryVariable: MemoryVariable) => {
|
|
const { memoryVariables } = workflowStore.getState()
|
|
setMemoryVariables(memoryVariables.filter(v => v.id !== memoryVariable.id))
|
|
}, [setMemoryVariables, workflowStore])
|
|
|
|
return {
|
|
handleAddMemoryVariable,
|
|
handleUpdateMemoryVariable,
|
|
handleDeleteMemoryVariable,
|
|
}
|
|
}
|
|
|
|
export const useFormatMemoryVariables = () => {
|
|
const formatMemoryVariables = useCallback((memoryVariables: MemoryVariable[]) => {
|
|
return memoryVariables.map((v) => {
|
|
return {
|
|
...v,
|
|
value_type: ChatVarType.Memory,
|
|
model: v.model ? {
|
|
completion_params: v.model?.completion_params,
|
|
mode: v.model?.mode,
|
|
provider: v.model?.provider,
|
|
model: v.model?.name,
|
|
} : undefined,
|
|
}
|
|
})
|
|
}, [])
|
|
|
|
return {
|
|
formatMemoryVariables,
|
|
}
|
|
}
|