'use client'; import { useEffect, useMemo, useRef, useState } from 'react'; import { Modal } from 'antd'; import type { CreateRoleReq } from '@/service/role'; import { useLoadInfoStore } from '@/store/useLoadInfoStore'; interface CreateRoleModalProps { open: boolean; onClose: () => void; onSubmit: (data: CreateRoleReq) => Promise; loading?: boolean; } export default function CreateRoleModal({ open, onClose, onSubmit, loading = false }: CreateRoleModalProps) { const [l0Enabled, setL0Enabled] = useState(true); const [name, setName] = useState(''); const [description, setDescription] = useState(''); const promptChangeRef = useRef(false); const loadInfo = useLoadInfoStore((state) => state.loadInfo); const userName = useMemo(() => { return loadInfo?.name || 'user'; }, [loadInfo]); const originPrompt = useMemo(() => { return `You are ${userName}'s 'Second Me,' a personalized AI created by ${userName}. You act as ${userName}'s representative, engaging with others on ${userName}'s behalf. Currently, you are interacting with an external user in the role of an ${name || '{{role}}'}. Your responsibility is ${description || '{{responsibility}}'}.`; }, [userName, name, description]); const [systemPrompt, setSystemPrompt] = useState(originPrompt); useEffect(() => { if (!promptChangeRef.current) { setSystemPrompt(originPrompt); } }, [originPrompt]); const init = () => { setName(''); setDescription(''); setSystemPrompt(originPrompt); setL0Enabled(true); promptChangeRef.current = false; }; useEffect(() => { if (!open) { init(); } }, [open]); const handleSubmit = async () => { await onSubmit({ name, description, system_prompt: systemPrompt, icon: '', enable_l0_retrieval: l0Enabled }); }; return (
setName(e.target.value)} placeholder="e.g., Historical Figure, Professional Expert, etc." type="text" value={name} />