Feature:memory function complete (#11982)

### What problem does this PR solve?

memory function complete

### Type of change

- [x] New Feature (non-breaking change which adds functionality)
This commit is contained in:
chanx
2025-12-17 12:35:26 +08:00
committed by GitHub
parent 2595644dfd
commit 205a6483f5
20 changed files with 226 additions and 142 deletions

View File

@ -72,16 +72,18 @@ export const EmbeddingSelect = ({
isEdit,
field,
name,
disabled = false,
}: {
isEdit: boolean;
field: FieldValues;
name?: string;
disabled?: boolean;
}) => {
const { t } = useTranslate('knowledgeConfiguration');
const form = useFormContext();
const embeddingModelOptions = useSelectEmbeddingModelOptions();
const { handleChange } = useHandleKbEmbedding();
const disabled = useHasParsedDocument(isEdit);
const oldValue = useMemo(() => {
const embdStr = form.getValues(name || 'embd_id');
return embdStr || '';
@ -101,7 +103,7 @@ export const EmbeddingSelect = ({
setLoading(true);
const res = await handleChange({
embed_id: value,
callback: field.onChange,
// callback: field.onChange,
});
if (res.code !== 0) {
field.onChange(oldValue);
@ -109,6 +111,7 @@ export const EmbeddingSelect = ({
setLoading(false);
}
}}
disabled={disabled && !isEdit}
value={field.value}
options={embeddingModelOptions}
placeholder={t('embeddingModelPlaceholder')}
@ -120,6 +123,7 @@ export const EmbeddingSelect = ({
export function EmbeddingModelItem({ line = 1, isEdit }: IProps) {
const { t } = useTranslate('knowledgeConfiguration');
const form = useFormContext();
const disabled = useHasParsedDocument(isEdit);
return (
<>
<FormField
@ -149,6 +153,7 @@ export function EmbeddingModelItem({ line = 1, isEdit }: IProps) {
<EmbeddingSelect
isEdit={!!isEdit}
field={field}
disabled={disabled}
></EmbeddingSelect>
</FormControl>
</div>

View File

@ -1,9 +1,9 @@
import { DynamicForm, DynamicFormRef } from '@/components/dynamic-form';
import { DynamicForm } from '@/components/dynamic-form';
import { useModelOptions } from '@/components/llm-setting-items/llm-form-field';
import { HomeIcon } from '@/components/svg-icon';
import { Modal } from '@/components/ui/modal/modal';
import { t } from 'i18next';
import { memo, useCallback, useMemo, useState } from 'react';
import { memo, useMemo } from 'react';
import { useTranslation } from 'react-i18next';
import { createMemoryFields } from './constants';
import { IMemory } from './interface';
@ -17,21 +17,16 @@ type IProps = {
};
export const AddOrEditModal = memo((props: IProps) => {
const { open, onClose, onSubmit, initialMemory, isCreate } = props;
const [formInstance, setFormInstance] = useState<DynamicFormRef | null>(null);
const formCallbackRef = useCallback((node: DynamicFormRef | null) => {
if (node) {
// formRef.current = node;
setFormInstance(node);
}
}, []);
const { t } = useTranslation();
const { modelOptions } = useModelOptions();
const fields = useMemo(() => {
if (!isCreate) {
return createMemoryFields.filter((field: any) => field.name === 'name');
return createMemoryFields(t).filter(
(field: any) => field.name === 'name',
);
} else {
const tempFields = createMemoryFields.map((field: any) => {
const tempFields = createMemoryFields(t).map((field: any) => {
if (field.name === 'llm_id') {
return {
...field,
@ -57,14 +52,13 @@ export const AddOrEditModal = memo((props: IProps) => {
<div>
<HomeIcon name="memory" width={'24'} />
</div>
{t('memories.createMemory')}
{isCreate ? t('memories.createMemory') : t('memories.editName')}
</div>
}
showfooter={false}
confirmLoading={props.loading}
>
<DynamicForm.Root
ref={formCallbackRef}
fields={fields}
onSubmit={() => {}}
defaultValues={initialMemory}

View File

@ -1,41 +1,63 @@
import { FormFieldConfig, FormFieldType } from '@/components/dynamic-form';
import { EmbeddingSelect } from '@/pages/dataset/dataset-setting/configuration/common-item';
import { t } from 'i18next';
import { TFunction } from 'i18next';
export enum MemoryType {
Raw = 'raw',
Semantic = 'semantic',
Episodic = 'episodic',
Procedural = 'procedural',
}
export const createMemoryFields = (t: TFunction) =>
[
{
name: 'name',
label: t('memories.name'),
placeholder: t('memories.memoryNamePlaceholder'),
required: true,
},
{
name: 'memory_type',
label: t('memories.memoryType'),
type: FormFieldType.MultiSelect,
placeholder: t('memories.descriptionPlaceholder'),
tooltip: t('memories.memoryTypeTooltip'),
options: [
{ label: 'Raw', value: MemoryType.Raw },
{ label: 'Semantic', value: MemoryType.Semantic },
{ label: 'Episodic', value: MemoryType.Episodic },
{ label: 'Procedural', value: MemoryType.Procedural },
],
required: true,
customValidate: (value) => {
if (!value.includes(MemoryType.Raw) || !value.length) {
return t('memories.embeddingModelError');
}
return true;
},
},
{
name: 'embd_id',
label: t('memories.embeddingModel'),
placeholder: t('memories.selectModel'),
tooltip: t('memories.embeddingModelTooltip'),
required: true,
// hideLabel: true,
// type: 'custom',
render: (field) => <EmbeddingSelect field={field} isEdit={false} />,
},
{
name: 'llm_id',
label: t('memories.llm'),
placeholder: t('memories.selectModel'),
required: true,
type: FormFieldType.Select,
tooltip: t('memories.llmTooltip'),
},
] as FormFieldConfig[];
export const createMemoryFields = [
{
name: 'name',
label: t('memories.name'),
placeholder: t('memories.memoryNamePlaceholder'),
required: true,
},
{
name: 'memory_type',
label: t('memories.memoryType'),
type: FormFieldType.MultiSelect,
placeholder: t('memories.descriptionPlaceholder'),
options: [
{ label: 'Raw', value: 'raw' },
{ label: 'Semantic', value: 'semantic' },
{ label: 'Episodic', value: 'episodic' },
{ label: 'Procedural', value: 'procedural' },
],
required: true,
},
{
name: 'embd_id',
label: t('memories.embeddingModel'),
placeholder: t('memories.selectModel'),
required: true,
// hideLabel: true,
// type: 'custom',
render: (field) => <EmbeddingSelect field={field} isEdit={false} />,
},
{
name: 'llm_id',
label: t('memories.llm'),
placeholder: t('memories.selectModel'),
required: true,
type: FormFieldType.Select,
},
] as FormFieldConfig[];
export const defaultMemoryFields = {
name: '',
memory_type: [MemoryType.Raw],
embd_id: '',
llm_id: '',
};

View File

@ -74,12 +74,12 @@ export const useFetchMemoryList = () => {
queryFn: async () => {
const { data: response } = await memoryService.getMemoryList(
{
data: {
params: {
keywords: debouncedSearchString,
page_size: pagination.pageSize,
page: pagination.current,
},
params: {},
data: {},
},
true,
);
@ -197,6 +197,7 @@ export const useUpdateMemory = () => {
if (response.code !== 0) {
throw new Error(response.message || 'Failed to update memory');
}
return response.data;
},
onSuccess: (data, variables) => {

View File

@ -10,8 +10,9 @@ import { Plus } from 'lucide-react';
import { useCallback, useEffect, useState } from 'react';
import { useSearchParams } from 'umi';
import { AddOrEditModal } from './add-or-edit-modal';
import { defaultMemoryFields } from './constants';
import { useFetchMemoryList, useRenameMemory } from './hooks';
import { ICreateMemoryProps } from './interface';
import { ICreateMemoryProps, IMemory } from './interface';
import { MemoryCard } from './memory-card';
export default function MemoryList() {
@ -45,7 +46,7 @@ export default function MemoryList() {
const openCreateModalFun = useCallback(() => {
// setIsEdit(false);
setAddOrEditType('add');
showMemoryRenameModal();
showMemoryRenameModal(defaultMemoryFields as unknown as IMemory);
}, [showMemoryRenameModal]);
const handlePageChange = useCallback(
(page: number, pageSize?: number) => {
@ -131,12 +132,12 @@ export default function MemoryList() {
})}
</CardContainer>
</div>
{list?.data.total && list?.data.total > 0 && (
{list?.data.total_count && list?.data.total_count > 0 && (
<div className="px-8 mb-4">
<RAGFlowPagination
{...pick(pagination, 'current', 'pageSize')}
// total={pagination.total}
total={list?.data.total}
total={list?.data.total_count}
onChange={handlePageChange}
/>
</div>

View File

@ -36,12 +36,14 @@ export interface IMemory extends ICreateMemoryProps {
temperature: string;
system_prompt: string;
user_prompt: string;
create_date: string;
create_time: number;
}
export interface MemoryListResponse {
code: number;
data: {
memory_list: Array<IMemory>;
total: number;
total_count: number;
};
message: string;
}

View File

@ -17,6 +17,7 @@ export function MemoryCard({ data, showMemoryRenameModal }: IProps) {
name: data?.name,
avatar: data?.avatar,
description: data?.description,
update_time: data?.create_time,
}}
moreDropdown={
<MemoryDropdown

View File

@ -30,7 +30,7 @@ export const useFetchMemoryMessageList = () => {
queryFn: async () => {
if (memoryBaseId) {
const { data } = await getMemoryDetailById(memoryBaseId as string, {
keyword: searchString,
keywords: searchString,
page: pagination.current,
page_size: pagination.pageSize,
});
@ -53,27 +53,61 @@ export const useFetchMemoryMessageList = () => {
export const useMessageAction = () => {
const queryClient = useQueryClient();
const { id: memoryId } = useParams();
const [selectedMessage, setSelectedMessage] = useState<IMessageInfo>(
{} as IMessageInfo,
);
const [showDeleteDialog, setShowDeleteDialog] = useState(false);
const handleClickDeleteMessage = useCallback((message: IMessageInfo) => {
console.log('handleClickDeleteMessage', message);
setSelectedMessage(message);
setShowDeleteDialog(true);
}, []);
const handleDeleteMessage = useCallback(() => {
// delete message
memoryService.deleteMemoryMessage(selectedMessage.message_id).then(() => {
message.success(t('message.deleted'));
queryClient.invalidateQueries({
queryKey: [MemoryApiAction.FetchMemoryMessage],
memoryService
.deleteMemoryMessage({
memory_id: memoryId,
message_id: selectedMessage.message_id,
})
.then(() => {
message.success(t('message.deleted'));
queryClient.invalidateQueries({
queryKey: [MemoryApiAction.FetchMemoryMessage],
});
});
});
setShowDeleteDialog(false);
}, [selectedMessage.message_id, queryClient]);
const handleUpdateMessageState = useCallback(
(messageInfo: IMessageInfo, enable: boolean) => {
// delete message
const selectedMessageInfo = messageInfo || selectedMessage;
memoryService
.updateMessageState({
memory_id: memoryId,
message_id: selectedMessageInfo.message_id,
status: enable || false,
})
.then(() => {
message.success(t('message.updated'));
queryClient.invalidateQueries({
queryKey: [MemoryApiAction.FetchMemoryMessage],
});
});
setShowDeleteDialog(false);
},
[selectedMessage, queryClient, memoryId],
);
const handleClickUpdateMessageState = useCallback(
(message: IMessageInfo, enable: boolean) => {
setSelectedMessage(message);
handleUpdateMessageState(message, enable);
},
[handleUpdateMessageState],
);
const [showMessageContentDialog, setShowMessageContentDialog] =
useState(false);
const [selectedMessageContent, setSelectedMessageContent] =
@ -90,9 +124,10 @@ export const useMessageAction = () => {
],
mutationFn: async () => {
setShowMessageContentDialog(true);
const res = await memoryService.getMessageContent(
selectedMessage.message_id,
);
const res = await memoryService.getMessageContent({
memory_id: memoryId,
message_id: selectedMessage.message_id,
});
if (res.data.code === 0) {
setSelectedMessageContent(res.data.data);
} else {
@ -117,6 +152,7 @@ export const useMessageAction = () => {
setShowDeleteDialog,
handleClickDeleteMessage,
handleDeleteMessage,
handleUpdateMessageState,
messageContent,
fetchMessageContentLoading,
fetchMessageContent,
@ -124,5 +160,6 @@ export const useMessageAction = () => {
showMessageContentDialog,
setShowMessageContentDialog,
handleClickMessageContentDialog,
handleClickUpdateMessageState,
};
};

View File

@ -21,15 +21,16 @@ export default function MemoryMessage() {
title="Dataset"
onSearchChange={handleInputChange}
searchString={searchString}
showFilter={false}
// value={filterValue}
// onChange={handleFilterSubmit}
// onOpenChange={onOpenChange}
// filters={filters}
leftPanel={
<div className="items-start">
<div className="pb-1">{t('knowledgeDetails.subbarFiles')}</div>
<div className="pb-1">{t('memory.sideBar.messages')}</div>
<div className="text-text-secondary text-sm">
{t('knowledgeDetails.datasetDescription')}
{t('memory.messages.messageDescription')}
</div>
</div>
}

View File

@ -66,7 +66,7 @@ export function MemoryTable({
selectedMessage,
handleDeleteMessage,
fetchMessageContent,
handleClickUpdateMessageState,
selectedMessageContent,
showMessageContentDialog,
setShowMessageContentDialog,
@ -131,7 +131,12 @@ export function MemoryTable({
const isEnabled = row.getValue('status') as boolean;
return (
<div className="flex items-center">
<Switch defaultChecked={isEnabled} onChange={() => {}} />
<Switch
defaultChecked={isEnabled}
onCheckedChange={(val) => {
handleClickUpdateMessageState(row.original, val);
}}
/>
</div>
);
},

View File

@ -94,7 +94,7 @@ export const AdvancedSettingsForm = () => {
horizontal: true,
// placeholder: t('memory.config.storageTypePlaceholder'),
options: [
{ label: 'lru', value: 'lru' },
// { label: 'lru', value: 'lru' },
{ label: 'fifo', value: 'fifo' },
],
required: false,

View File

@ -1,7 +1,8 @@
import { FormFieldType, RenderField } from '@/components/dynamic-form';
import { useModelOptions } from '@/components/llm-setting-items/llm-form-field';
import { EmbeddingSelect } from '@/pages/dataset/dataset-setting/configuration/common-item';
import { t } from 'i18next';
import { MemoryType } from '@/pages/memories/constants';
import { useTranslation } from 'react-i18next';
import { z } from 'zod';
export const memoryModelFormSchema = {
@ -13,11 +14,12 @@ export const memoryModelFormSchema = {
export const defaultMemoryModelForm = {
embd_id: '',
llm_id: '',
memory_type: [],
memory_type: [MemoryType.Raw],
memory_size: 0,
};
export const MemoryModelForm = () => {
const { modelOptions } = useModelOptions();
const { t } = useTranslation();
return (
<>
<RenderField
@ -29,7 +31,12 @@ export const MemoryModelForm = () => {
horizontal: true,
// hideLabel: true,
type: FormFieldType.Custom,
render: (field) => <EmbeddingSelect field={field} isEdit={false} />,
disabled: true,
render: (field) => (
<EmbeddingSelect field={field} isEdit={false} disabled={true} />
),
tooltip: t('memories.embeddingModelTooltip'),
}}
/>
<RenderField
@ -41,6 +48,7 @@ export const MemoryModelForm = () => {
horizontal: true,
type: FormFieldType.Select,
options: modelOptions as { value: string; label: string }[],
tooltip: t('memories.llmTooltip'),
}}
/>
<RenderField
@ -50,6 +58,8 @@ export const MemoryModelForm = () => {
type: FormFieldType.MultiSelect,
horizontal: true,
placeholder: t('memories.memoryTypePlaceholder'),
tooltip: t('memories.memoryTypeTooltip'),
disabled: true,
options: [
{ label: 'Raw', value: 'raw' },
{ label: 'Semantic', value: 'semantic' },

View File

@ -3,7 +3,8 @@ import { Button } from '@/components/ui/button';
import { useSecondPathName } from '@/hooks/route-hook';
import { cn } from '@/lib/utils';
import { Routes } from '@/routes';
import { Banknote, Logs } from 'lucide-react';
import { formatPureDate } from '@/utils/date';
import { MemoryStick, Settings } from 'lucide-react';
import { useMemo } from 'react';
import { useTranslation } from 'react-i18next';
import { useFetchMemoryBaseConfiguration } from '../hooks/use-memory-setting';
@ -19,12 +20,12 @@ export function SideBar() {
const items = useMemo(() => {
const list = [
{
icon: <Logs className="size-4" />,
icon: <MemoryStick className="size-4" />,
label: t(`memory.sideBar.messages`),
key: Routes.MemoryMessage,
},
{
icon: <Banknote className="size-4" />,
icon: <Settings className="size-4" />,
label: t(`memory.sideBar.configuration`),
key: Routes.MemorySetting,
},
@ -44,15 +45,13 @@ export function SideBar() {
<h3 className="text-lg font-semibold line-clamp-1 text-text-primary text-ellipsis overflow-hidden">
{data.name}
</h3>
{/* <div className="flex justify-between">
<span>
{data.doc_num} {t('knowledgeDetails.files')}
</span>
<span>{formatBytes(data.size)}</span>
<div className="flex justify-between">
<span className="truncate ">{data.description}</span>
{/* <span>{formatBytes(data.size)}</span> */}
</div>
<div>
{t('knowledgeDetails.created')} {formatPureDate(data.)}
</div> */}
{t('knowledgeDetails.created')} {formatPureDate(data.create_time)}
</div>
</div>
</div>