Merge pull request #254 from SuanmoSuanyangTechnology/feature/prompt_zy
Feature/prompt zy
This commit is contained in:
@@ -1,13 +1,26 @@
|
||||
import { request } from '@/utils/request'
|
||||
import type { AiPromptForm } from '@/views/ApplicationConfig/types'
|
||||
import type { PromptReleaseData } from '@/views/Prompt/types'
|
||||
import { handleSSE, type SSEMessage } from '@/utils/stream'
|
||||
|
||||
// Create session
|
||||
export const createPromptSessions = () => {
|
||||
return request.post(`/prompt/sessions`)
|
||||
}
|
||||
export const getPrompt = (session_id: string) => {
|
||||
return request.get(`/prompt/sessions/${session_id}`)
|
||||
}
|
||||
// Get prompt optimization
|
||||
export const updatePromptMessages = (session_id: string, data: AiPromptForm, onMessage?: (data: SSEMessage[]) => void) => {
|
||||
return handleSSE(`/prompt/sessions/${session_id}/messages`, data, onMessage)
|
||||
}
|
||||
// Prompt release list
|
||||
export const getPromptReleaseListUrl = '/prompt/releases/list'
|
||||
export const getPromptReleaseList = () => {
|
||||
return request.get(getPromptReleaseListUrl)
|
||||
}
|
||||
// Save prompt
|
||||
export const savePrompt = (data: PromptReleaseData) => {
|
||||
return request.post('/prompt/releases', data)
|
||||
}
|
||||
// Delete prompt
|
||||
export const deletePrompt = (prompt_id: string) => {
|
||||
return request.delete(`/prompt/releases/${prompt_id}`)
|
||||
}
|
||||
15
web/src/assets/images/menu/prompt.svg
Normal file
15
web/src/assets/images/menu/prompt.svg
Normal file
@@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<svg width="16px" height="16px" viewBox="0 0 16 16" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
<title>提示词备份</title>
|
||||
<g id="v0.2.0" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd" stroke-linecap="round" stroke-linejoin="round">
|
||||
<g id="红熊空间-记忆管理" transform="translate(-54, -575)" stroke="#5B6167" stroke-width="1.1">
|
||||
<g id="提示词备份" transform="translate(54, 575)">
|
||||
<g id="编组-34" transform="translate(2.5, 2)">
|
||||
<path d="M3.96581416,12 L1.5,12 C0.671572875,12 0,11.3284271 0,10.5 L0,1.5 C0,0.671572875 0.671572875,0 1.5,0 L9.39685919,0 C10.2252863,-1.55431223e-15 10.8968592,0.671572875 10.8968592,1.5 L10.8968592,2.99293149 L10.8968592,2.99293149" id="路径"></path>
|
||||
<path d="M3.26905776,3.27272727 L7.62780143,3.27272727 M5.4484296,3.27272727 L5.4484296,7.63636364" id="形状结合"></path>
|
||||
<polygon id="路径-11" points="9.22121994 6.54545455 6.91984008 10.2384806 7.8543485 12 9.77860327 12 12 8.17112299"></polygon>
|
||||
</g>
|
||||
</g>
|
||||
</g>
|
||||
</g>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 1.2 KiB |
15
web/src/assets/images/menu/prompt_active.svg
Normal file
15
web/src/assets/images/menu/prompt_active.svg
Normal file
@@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<svg width="16px" height="16px" viewBox="0 0 16 16" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
<title>提示词</title>
|
||||
<g id="v0.2.0" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd" stroke-linecap="round" stroke-linejoin="round">
|
||||
<g id="红熊空间-记忆管理" transform="translate(-28, -575)" stroke="#212332" stroke-width="1.1">
|
||||
<g id="提示词" transform="translate(28, 575)">
|
||||
<g id="编组-34" transform="translate(2.5, 2)">
|
||||
<path d="M3.96581416,12 L1.5,12 C0.671572875,12 0,11.3284271 0,10.5 L0,1.5 C0,0.671572875 0.671572875,0 1.5,0 L9.39685919,0 C10.2252863,-1.55431223e-15 10.8968592,0.671572875 10.8968592,1.5 L10.8968592,2.99293149 L10.8968592,2.99293149" id="路径"></path>
|
||||
<path d="M3.26905776,3.27272727 L7.62780143,3.27272727 M5.4484296,3.27272727 L5.4484296,7.63636364" id="形状结合"></path>
|
||||
<polygon id="路径-11" points="9.22121994 6.54545455 6.91984008 10.2384806 7.8543485 12 9.77860327 12 12 8.17112299"></polygon>
|
||||
</g>
|
||||
</g>
|
||||
</g>
|
||||
</g>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 1.2 KiB |
@@ -19,6 +19,7 @@ interface RbMarkdownProps {
|
||||
showHtmlComments?: boolean; // 是否显示 HTML 注释,默认为 false(隐藏)
|
||||
editable?: boolean; // 是否可编辑,默认为 false
|
||||
onContentChange?: (content: string) => void; // 内容变化回调
|
||||
className?: string;
|
||||
}
|
||||
|
||||
const components = {
|
||||
@@ -98,6 +99,7 @@ const RbMarkdown: FC<RbMarkdownProps> = ({
|
||||
showHtmlComments = false,
|
||||
editable = false,
|
||||
onContentChange,
|
||||
className
|
||||
}) => {
|
||||
const [editContent, setEditContent] = useState(content)
|
||||
const textareaRef = useRef<any>(null)
|
||||
@@ -162,7 +164,7 @@ const RbMarkdown: FC<RbMarkdownProps> = ({
|
||||
|
||||
// 预览模式
|
||||
return (
|
||||
<div className="rb:relative" onKeyDown={handleKeyDown} tabIndex={0}>
|
||||
<div className={`rb:relative ${className || ''}`} onKeyDown={handleKeyDown} tabIndex={0}>
|
||||
<style>{`
|
||||
.html-comment {
|
||||
color: #999;
|
||||
|
||||
@@ -42,6 +42,8 @@ import pricingIcon from '@/assets/images/menu/pricing.svg'
|
||||
import pricingActiveIcon from '@/assets/images/menu/pricing_active.svg'
|
||||
import spaceConfigIcon from '@/assets/images/menu/spaceConfig.svg'
|
||||
import spaceConfigActiveIcon from '@/assets/images/menu/spaceConfig_active.svg'
|
||||
import promptIcon from '@/assets/images/menu/prompt.svg'
|
||||
import promptActiveIcon from '@/assets/images/menu/prompt_active.svg'
|
||||
|
||||
// 图标路径映射表
|
||||
const iconPathMap: Record<string, string> = {
|
||||
@@ -73,6 +75,8 @@ const iconPathMap: Record<string, string> = {
|
||||
'pricingActive': pricingActiveIcon,
|
||||
'spaceConfig': spaceConfigIcon,
|
||||
'spaceConfigActive': spaceConfigActiveIcon,
|
||||
'prompt': promptIcon,
|
||||
'promptActive': promptActiveIcon,
|
||||
};
|
||||
|
||||
const { Sider } = Layout;
|
||||
|
||||
@@ -112,7 +112,8 @@ export const en = {
|
||||
pricing: 'Pricing Management',
|
||||
orderPayment: 'Order Payment',
|
||||
orderHistory: 'Order History',
|
||||
spaceConfig: 'Space Configuration'
|
||||
spaceConfig: 'Space Configuration',
|
||||
prompt: 'Prompt Engineering',
|
||||
},
|
||||
dashboard: {
|
||||
total_models: 'Available Models',
|
||||
@@ -2435,6 +2436,21 @@ Memory Bear: After the rebellion, regional warlordism intensified for several re
|
||||
question: 'Lessons Learned',
|
||||
summary: 'Core Insights',
|
||||
none: 'None'
|
||||
}
|
||||
},
|
||||
prompt: {
|
||||
editor: 'Prompt Generator',
|
||||
history: 'My History',
|
||||
historySearchPlaceholder: 'Search by name',
|
||||
model: 'Model',
|
||||
you: 'You',
|
||||
ai: 'AI Assistant',
|
||||
promptPlaceholder: 'Conversation optimization prompt will be displayed here',
|
||||
promptChatEmpty: 'No conversation content available',
|
||||
promptChatPlaceholder: 'Describe the prompt you need, e.g.: I need a customer service assistant',
|
||||
conversationOptimizationPrompt: 'Conversation Optimization Prompt',
|
||||
addVariable: 'Insert Variable',
|
||||
initialInput: 'Original Input',
|
||||
saveTitle: 'Title',
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
@@ -111,7 +111,8 @@ export const zh = {
|
||||
pricing: '收费管理',
|
||||
orderPayment: '订单支付',
|
||||
orderHistory: '订单记录',
|
||||
spaceConfig: '空间配置'
|
||||
spaceConfig: '空间配置',
|
||||
prompt: '提示词工程',
|
||||
},
|
||||
knowledgeBase: {
|
||||
home: '首页',
|
||||
@@ -2524,6 +2525,21 @@ export const zh = {
|
||||
question: '踩过的坑',
|
||||
summary: '核心洞察',
|
||||
none: '无'
|
||||
}
|
||||
},
|
||||
prompt: {
|
||||
editor: '提示词生成器',
|
||||
history: '我的历史',
|
||||
historySearchPlaceholder: '按名称搜索',
|
||||
model: '模型',
|
||||
you: '你',
|
||||
ai: 'AI 助手',
|
||||
promptPlaceholder: '对话优化提示词将显示在这里',
|
||||
promptChatEmpty: '目前没有对话内容',
|
||||
promptChatPlaceholder: '描述你需要的提示词,例如:我需要一个客服助手',
|
||||
conversationOptimizationPrompt: '对话优化提示词',
|
||||
addVariable: '插入变量',
|
||||
initialInput: '原始输入',
|
||||
saveTitle: '标题',
|
||||
},
|
||||
},
|
||||
}
|
||||
@@ -68,6 +68,7 @@ const componentMap: Record<string, LazyExoticComponent<ComponentType<object>>> =
|
||||
Pricing: lazy(() => import('@/views/Pricing')),
|
||||
ToolManagement: lazy(() => import('@/views/ToolManagement')),
|
||||
SpaceConfig: lazy(() => import('@/views/SpaceConfig')),
|
||||
Prompt: lazy(() => import('@/views/Prompt')),
|
||||
Login: lazy(() => import('@/views/Login')),
|
||||
InviteRegister: lazy(() => import('@/views/InviteRegister')),
|
||||
NoPermission: lazy(() => import('@/views/NoPermission')),
|
||||
|
||||
@@ -34,6 +34,7 @@
|
||||
{ "path": "/emotion-engine/:id", "element": "EmotionEngine" },
|
||||
{ "path": "/reflection-engine/:id", "element": "SelfReflectionEngine" },
|
||||
{ "path": "/space-config", "element": "SpaceConfig" },
|
||||
{ "path": "/prompt", "element": "Prompt" },
|
||||
{ "path": "/no-permission", "element": "NoPermission" },
|
||||
{ "path": "/*", "element": "NotFound" }
|
||||
]
|
||||
|
||||
@@ -362,6 +362,21 @@
|
||||
"iconActive": null,
|
||||
"subs": null
|
||||
},
|
||||
{
|
||||
"id": 20,
|
||||
"parent": 0,
|
||||
"code": "prompt",
|
||||
"label": "提示词",
|
||||
"i18nKey": "menu.prompt",
|
||||
"path": "/prompt",
|
||||
"enable": true,
|
||||
"display": true,
|
||||
"level": 1,
|
||||
"sort": 0,
|
||||
"icon": null,
|
||||
"iconActive": null,
|
||||
"subs": null
|
||||
},
|
||||
{
|
||||
"id": 19,
|
||||
"parent": 0,
|
||||
|
||||
95
web/src/views/Prompt/History.tsx
Normal file
95
web/src/views/Prompt/History.tsx
Normal file
@@ -0,0 +1,95 @@
|
||||
import React, { useRef, type MouseEvent } from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { Tooltip, Space, App } from 'antd';
|
||||
import { EyeOutlined } from '@ant-design/icons';
|
||||
|
||||
import type { HistoryQuery, HistoryItem, PromptDetailRef } from './types';
|
||||
import RbCard from '@/components/RbCard/Card'
|
||||
import { getPromptReleaseListUrl, deletePrompt } from '@/api/prompt'
|
||||
import Markdown from '@/components/Markdown';
|
||||
import { formatDateTime } from '@/utils/format'
|
||||
import PromptDetail from './components/PromptDetail'
|
||||
import PageScrollList, { type PageScrollListRef } from '@/components/PageScrollList'
|
||||
|
||||
const History: React.FC<{ query: HistoryQuery; edit: (item: HistoryItem) => void; }> = ({ query, edit }) => {
|
||||
const { t } = useTranslation();
|
||||
const scrollListRef = useRef<PageScrollListRef>(null)
|
||||
const detailRef = useRef<PromptDetailRef>(null)
|
||||
const { message, modal } = App.useApp()
|
||||
|
||||
const handleView = (item: HistoryItem) => {
|
||||
detailRef.current?.handleOpen(item)
|
||||
}
|
||||
const handleDelete = (item: HistoryItem, e?: MouseEvent) => {
|
||||
e?.preventDefault();
|
||||
e?.stopPropagation();
|
||||
modal.confirm({
|
||||
title: t('common.confirmDeleteDesc', { name: item.title }),
|
||||
content: t('application.apiKeyDeleteContent'),
|
||||
okText: t('common.delete'),
|
||||
cancelText: t('common.cancel'),
|
||||
okType: 'danger',
|
||||
onOk: () => {
|
||||
deletePrompt(item.id).then(() => {
|
||||
message.success(t('common.deleteSuccess'))
|
||||
scrollListRef.current?.refresh()
|
||||
detailRef.current?.handleClose()
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
const handleEdit = (item: HistoryItem) => {
|
||||
edit(item)
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<PageScrollList
|
||||
ref={scrollListRef}
|
||||
url={getPromptReleaseListUrl}
|
||||
query={query}
|
||||
column={3}
|
||||
renderItem={(item) => {
|
||||
const historyItem = item as unknown as HistoryItem;
|
||||
return (
|
||||
<RbCard
|
||||
className="rb:cursor-pointer"
|
||||
headerType="borderless"
|
||||
bodyClassName="rb:p-4!"
|
||||
title={<Tooltip title={historyItem.title}>{historyItem.title}</Tooltip>}
|
||||
extra={<div className="rb:text-[12px] rb:text-[#5B6167]">{formatDateTime(historyItem.created_at, 'YYYY/MM/DD HH:mm')}</div>}
|
||||
onClick={() => handleView(historyItem)}
|
||||
>
|
||||
<div className="rb:text-[12px] rb:h-30 rb:overflow-hidden rb:px-3 rb:py-2.5 rb:bg-[#F6F8FC] rb:rounded-lg rb:border rb:border-[#DFE4ED] rb:shadow-[0px_4px_8px_0px_rgba(33,35,50,0.12)]">
|
||||
<Markdown content={historyItem.prompt} className="rb:h-full! rb:overflow-y-auto" />
|
||||
</div>
|
||||
|
||||
<div className="rb:mt-4 rb:text-[12px] rb:leading-4 rb:font-regular rb:text-[#5B6167] rb:flex rb:items-center rb:justify-end">
|
||||
<Space size={16}>
|
||||
<EyeOutlined className="rb:text-[16px]" onClick={() => handleView(historyItem)} />
|
||||
<div
|
||||
className="rb:w-5 rb:h-5 rb:cursor-pointer rb:bg-cover rb:bg-[url('@/assets/images/edit.svg')] rb:hover:bg-[url('@/assets/images/edit_hover.svg')]"
|
||||
onClick={() => handleEdit(historyItem)}
|
||||
></div>
|
||||
<div
|
||||
className="rb:w-5 rb:h-5 rb:cursor-pointer rb:bg-cover rb:bg-[url('@/assets/images/delete.svg')] rb:hover:bg-[url('@/assets/images/delete_hover.svg')]"
|
||||
onClick={(e) => handleDelete(historyItem, e)}
|
||||
></div>
|
||||
</Space>
|
||||
</div>
|
||||
</RbCard>
|
||||
);
|
||||
}}
|
||||
/>
|
||||
|
||||
<PromptDetail
|
||||
ref={detailRef}
|
||||
handleEdit={handleEdit}
|
||||
handleDelete={handleDelete}
|
||||
/>
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
export default History;
|
||||
227
web/src/views/Prompt/Prompt.tsx
Normal file
227
web/src/views/Prompt/Prompt.tsx
Normal file
@@ -0,0 +1,227 @@
|
||||
import { type FC, useState, useRef, useEffect } from 'react';
|
||||
import { Button, Form, Input, App, Row, Col } from 'antd';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import clsx from 'clsx'
|
||||
import copy from 'copy-to-clipboard';
|
||||
|
||||
import { updatePromptMessages, createPromptSessions } from '@/api/prompt'
|
||||
import { getModelListUrl } from '@/api/models'
|
||||
import type { PromptVariableModalRef, AiPromptForm, HistoryItem, PromptSaveModalRef } from './types'
|
||||
import ChatContent from '@/components/Chat/ChatContent'
|
||||
import Empty from '@/components/Empty'
|
||||
import ChatSendIcon from '@/assets/images/application/chatSend.svg'
|
||||
import ConversationEmptyIcon from '@/assets/images/conversation/conversationEmpty.svg'
|
||||
import type { ChatItem } from '@/components/Chat/types'
|
||||
import CustomSelect from '@/components/CustomSelect'
|
||||
import PromptVariableModal from './components/PromptVariableModal'
|
||||
import { type SSEMessage } from '@/utils/stream'
|
||||
import Editor from '@/views/ApplicationConfig/components/Editor'
|
||||
import PromptSaveModal from './components/PromptSaveModal'
|
||||
|
||||
const Prompt: FC<{ editVo: HistoryItem | null; refresh: () => void; }> = ({ editVo, refresh }) => {
|
||||
const { t } = useTranslation();
|
||||
const { message } = App.useApp()
|
||||
const [loading, setLoading] = useState(false)
|
||||
const [form] = Form.useForm<AiPromptForm>()
|
||||
const [chatList, setChatList] = useState<ChatItem[]>([])
|
||||
const [variables, setVariables] = useState<string[]>([])
|
||||
const [promptSession, setPromptSession] = useState<string | null>(null)
|
||||
const aiPromptVariableModalRef = useRef<PromptVariableModalRef>(null)
|
||||
const promptSaveModalRef = useRef<PromptSaveModalRef>(null)
|
||||
const editorRef = useRef<any>(null)
|
||||
const currentPromptValueRef = useRef<string>(undefined)
|
||||
const values = Form.useWatch([], form)
|
||||
|
||||
useEffect(() => {
|
||||
if (editVo?.id) {
|
||||
form.setFieldValue('current_prompt', editVo.prompt)
|
||||
setChatList([])
|
||||
}
|
||||
updateSession()
|
||||
}, [editVo])
|
||||
|
||||
const updateSession = () => {
|
||||
console.log('updateSession')
|
||||
createPromptSessions().then(res => {
|
||||
const response = res as { id: string }
|
||||
setPromptSession(response.id)
|
||||
})
|
||||
}
|
||||
|
||||
const handleSend = () => {
|
||||
if (!promptSession) return
|
||||
if (!values.model_id) {
|
||||
message.warning(t('common.selectPlaceholder', { title: t('prompt.model') }))
|
||||
return
|
||||
}
|
||||
if (!values.message) {
|
||||
message.warning(t('prompt.promptChatPlaceholder'))
|
||||
return
|
||||
}
|
||||
const messageContent = values.message
|
||||
setLoading(true)
|
||||
setChatList(prev => {
|
||||
return [...prev, { role: 'user', content: messageContent}]
|
||||
})
|
||||
form.setFieldsValue({ message: undefined, current_prompt: undefined })
|
||||
|
||||
const handleStreamMessage = (data: SSEMessage[]) => {
|
||||
data.map(item => {
|
||||
const { content, desc, variables } = item.data as { content: string; desc: string; variables: string[] };
|
||||
|
||||
switch (item.event) {
|
||||
case 'start':
|
||||
currentPromptValueRef.current = ''
|
||||
if (editorRef.current?.clear) {
|
||||
editorRef.current.clear();
|
||||
}
|
||||
break;
|
||||
case 'message':
|
||||
if (typeof content === 'string') {
|
||||
currentPromptValueRef.current += content;
|
||||
if (editorRef.current?.appendText) {
|
||||
editorRef.current.appendText(content);
|
||||
editorRef.current.scrollToBottom();
|
||||
} else {
|
||||
form.setFieldsValue({ current_prompt: currentPromptValueRef.current })
|
||||
}
|
||||
}
|
||||
if (desc) {
|
||||
setChatList(prev => {
|
||||
return [...prev, { role: 'assistant', content: desc }]
|
||||
})
|
||||
}
|
||||
if (variables) {
|
||||
setVariables(variables)
|
||||
}
|
||||
break;
|
||||
case 'end':
|
||||
setLoading(false)
|
||||
// 流结束时同步表单值
|
||||
form.setFieldsValue({ current_prompt: currentPromptValueRef.current })
|
||||
break
|
||||
}
|
||||
})
|
||||
};
|
||||
updatePromptMessages((promptSession) as string, values, handleStreamMessage)
|
||||
.finally(() => {
|
||||
setLoading(false)
|
||||
})
|
||||
}
|
||||
const handleCopy = () => {
|
||||
if (!values.current_prompt || values?.current_prompt?.trim() === '') return
|
||||
copy(values.current_prompt)
|
||||
message.success(t('common.copySuccess'))
|
||||
}
|
||||
const handleAdd = () => {
|
||||
aiPromptVariableModalRef.current?.handleOpen()
|
||||
}
|
||||
const handleVariableApply = (value: string) => {
|
||||
if (editorRef.current?.insertText) {
|
||||
editorRef.current.insertText(value)
|
||||
} else {
|
||||
form.setFieldValue('current_prompt', (values.current_prompt || '') + value)
|
||||
}
|
||||
}
|
||||
const handleSave = () => {
|
||||
if (!values.current_prompt || !promptSession) {
|
||||
return
|
||||
}
|
||||
promptSaveModalRef.current?.handleOpen({
|
||||
session_id: promptSession,
|
||||
prompt: values.current_prompt
|
||||
})
|
||||
}
|
||||
|
||||
const handleRefresh = () => {
|
||||
form.setFieldValue('current_prompt', undefined)
|
||||
currentPromptValueRef.current = undefined;
|
||||
setChatList([])
|
||||
refresh()
|
||||
}
|
||||
|
||||
console.log(values)
|
||||
return (
|
||||
<>
|
||||
<Form form={form}>
|
||||
<div className="rb:grid rb:grid-cols-2 rb:-my-4">
|
||||
<div className="rb:border-r rb:border-r-[#EBEBEB] rb:pr-6 rb:pt-3">
|
||||
<Form.Item
|
||||
label={t('prompt.model')}
|
||||
name="model_id"
|
||||
rules={[{ required: true, message: t('common.pleaseSelect') }]}
|
||||
>
|
||||
<CustomSelect
|
||||
url={getModelListUrl}
|
||||
params={{ type: 'llm,chat', pagesize: 100, is_active: true }}
|
||||
valueKey="id"
|
||||
labelKey="name"
|
||||
hasAll={false}
|
||||
style={{ width: '100%' }}
|
||||
/>
|
||||
</Form.Item>
|
||||
|
||||
<ChatContent
|
||||
classNames="rb:h-[calc(100vh-260px)] rb:px-[16px] rb:py-[20px] rb:bg-[#FBFDFF] rb:border rb:border-[#DFE4ED] rb:rounded-[8px]"
|
||||
contentClassNames="rb:max-w-[260px]!"
|
||||
empty={<Empty url={ConversationEmptyIcon} title={t('prompt.promptChatEmpty')} isNeedSubTitle={false} size={[240, 200]} className="rb:h-full" />}
|
||||
data={chatList || []}
|
||||
streamLoading={false}
|
||||
labelPosition="top"
|
||||
labelFormat={(item) => item.role === 'user' ? t('prompt.you') : t('prompt.ai')}
|
||||
/>
|
||||
|
||||
<div className="rb:flex rb:items-center rb:gap-2.5 rb:py-4">
|
||||
<Form.Item name="message" className="rb:mb-0!" style={{ width: 'calc(100% - 54px)' }}>
|
||||
<Input
|
||||
className="rb:h-11 rb:shadow-[0px_2px_8px_0px_rgba(33,35,50,0.1)]"
|
||||
placeholder={t('prompt.promptChatPlaceholder')}
|
||||
onPressEnter={handleSend}
|
||||
/>
|
||||
</Form.Item>
|
||||
<img src={ChatSendIcon} className={clsx("rb:w-11 rb:h-11 rb:cursor-pointer", {
|
||||
'rb:opacity-50': loading,
|
||||
})} onClick={handleSend} />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="rb:pl-6 rb:pt-3">
|
||||
<Row>
|
||||
<Col span={12}>
|
||||
<Form.Item label={t('prompt.conversationOptimizationPrompt')}></Form.Item>
|
||||
</Col>
|
||||
<Col span={12} className="rb:text-right">
|
||||
<Button onClick={handleAdd}>+ {t('prompt.addVariable')}</Button>
|
||||
</Col>
|
||||
</Row>
|
||||
<Form.Item name="current_prompt">
|
||||
<Editor
|
||||
ref={editorRef}
|
||||
placeholder={t('prompt.promptPlaceholder')}
|
||||
className="rb:h-[calc(100vh-260px)]"
|
||||
// onChange={(value) => form.setFieldValue('current_prompt', value)}
|
||||
/>
|
||||
</Form.Item>
|
||||
<div className="rb:grid rb:grid-cols-2 rb:gap-4 rb:mt-6">
|
||||
<Button type="primary" block disabled={!values?.current_prompt} onClick={handleSave}>{t('common.save')}</Button>
|
||||
<Button block disabled={!values?.current_prompt} onClick={handleCopy}>{t('common.copy')}</Button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</Form>
|
||||
|
||||
<PromptVariableModal
|
||||
ref={aiPromptVariableModalRef}
|
||||
variables={variables}
|
||||
refresh={handleVariableApply}
|
||||
/>
|
||||
|
||||
<PromptSaveModal
|
||||
ref={promptSaveModalRef}
|
||||
refresh={handleRefresh}
|
||||
/>
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
export default Prompt;
|
||||
82
web/src/views/Prompt/components/PromptDetail.tsx
Normal file
82
web/src/views/Prompt/components/PromptDetail.tsx
Normal file
@@ -0,0 +1,82 @@
|
||||
import { forwardRef, useImperativeHandle, useState } from 'react';
|
||||
import { Flex, Button, App } from 'antd';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import copy from 'copy-to-clipboard'
|
||||
|
||||
import type { HistoryItem, PromptDetailRef } from '../types'
|
||||
import RbModal from '@/components/RbModal'
|
||||
import Markdown from '@/components/Markdown';
|
||||
import { formatDateTime } from '@/utils/format'
|
||||
|
||||
const PromptDetail = forwardRef<PromptDetailRef, { handleEdit: (item: HistoryItem) => void; handleDelete: (item: HistoryItem) => void; }>(({ handleEdit, handleDelete }, ref) => {
|
||||
const { t } = useTranslation();
|
||||
const { message } = App.useApp()
|
||||
const [visible, setVisible] = useState(false);
|
||||
const [data, setData] = useState<HistoryItem>({} as HistoryItem)
|
||||
|
||||
// 封装取消方法,添加关闭弹窗逻辑
|
||||
const handleClose = () => {
|
||||
setVisible(false);
|
||||
};
|
||||
|
||||
const handleOpen = (vo: HistoryItem) => {
|
||||
setVisible(true);
|
||||
setData(vo)
|
||||
};
|
||||
const handleCopy = (text = '') => {
|
||||
copy(text)
|
||||
message.success(t('common.copySuccess'))
|
||||
}
|
||||
|
||||
useImperativeHandle(ref, () => ({
|
||||
handleOpen,
|
||||
handleClose
|
||||
}));
|
||||
return (
|
||||
<RbModal
|
||||
title={<div>
|
||||
{data.title}
|
||||
<div className="rb:text-[12px] rb:text-[#5B6167] rb:font-normal rb:mt-1!">{formatDateTime(data.created_at)}</div>
|
||||
</div>}
|
||||
open={visible}
|
||||
footer={
|
||||
<Flex justify="end" gap={8}>
|
||||
<Button danger onClick={() => handleDelete(data)}>{t('common.delete')}</Button>
|
||||
<Button type="primary" onClick={() => {
|
||||
handleClose()
|
||||
handleEdit(data)
|
||||
}}>{t('common.edit')}</Button>
|
||||
</Flex>
|
||||
}
|
||||
onCancel={handleClose}
|
||||
width={1000}
|
||||
>
|
||||
<Flex justify="space-between">
|
||||
{t('prompt.initialInput')}
|
||||
<Button className="rb:group" size="small" disabled={!data.first_message || data.first_message.trim() === ''} onClick={() => handleCopy(data.first_message)}>
|
||||
<div
|
||||
className="rb:w-4 rb:h-4 rb:cursor-pointer rb:bg-cover rb:bg-[url('@/assets/images/copy.svg')] rb:group-hover:bg-[url('@/assets/images/copy_active.svg')]"
|
||||
></div>
|
||||
</Button>
|
||||
</Flex>
|
||||
|
||||
<div className="rb:my-3 rb:bg-[#F6F8FC] rb:border-[#DFE4ED] rb:rounded-lg rb:p-3">
|
||||
<Markdown content={data.first_message} className="rb:min-h-5 rb:max-h-50 rb:overflow-y-auto" />
|
||||
</div>
|
||||
|
||||
<Flex justify="space-between">
|
||||
{t('prompt.conversationOptimizationPrompt')}
|
||||
<Button className="rb:group" size="small" onClick={() => handleCopy(data.prompt)}>
|
||||
<div
|
||||
className="rb:w-4 rb:h-4 rb:cursor-pointer rb:bg-cover rb:bg-[url('@/assets/images/copy.svg')] rb:group-hover:bg-[url('@/assets/images/copy_active.svg')]"
|
||||
></div>
|
||||
</Button>
|
||||
</Flex>
|
||||
<div className="rb:relative rb:my-3 rb:overflow-hidden rb:bg-[#F6F8FC] rb:border-[#DFE4ED] rb:rounded-lg rb:p-3">
|
||||
<Markdown content={data.prompt} className="rb:min-h-5 rb:max-h-70 rb:overflow-y-auto" />
|
||||
</div>
|
||||
</RbModal>
|
||||
);
|
||||
});
|
||||
|
||||
export default PromptDetail;
|
||||
90
web/src/views/Prompt/components/PromptSaveModal.tsx
Normal file
90
web/src/views/Prompt/components/PromptSaveModal.tsx
Normal file
@@ -0,0 +1,90 @@
|
||||
import { forwardRef, useImperativeHandle, useState } from 'react';
|
||||
import { Form, Input, App } from 'antd';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
|
||||
import type { PromptSaveModalRef, PromptReleaseData } from '../types'
|
||||
import RbModal from '@/components/RbModal'
|
||||
import { savePrompt } from '@/api/prompt'
|
||||
|
||||
const FormItem = Form.Item;
|
||||
|
||||
interface PromptSaveModalProps {
|
||||
refresh: () => void;
|
||||
}
|
||||
|
||||
const PromptSaveModal = forwardRef<PromptSaveModalRef, PromptSaveModalProps>(({
|
||||
refresh
|
||||
}, ref) => {
|
||||
const { t } = useTranslation();
|
||||
const { message } = App.useApp();
|
||||
const [visible, setVisible] = useState(false);
|
||||
const [form] = Form.useForm<{ title?: string; }>();
|
||||
const [loading, setLoading] = useState(false)
|
||||
const [data, setData] = useState<PromptReleaseData | null>(null)
|
||||
const title = Form.useWatch(['title'], form)
|
||||
|
||||
// 封装取消方法,添加关闭弹窗逻辑
|
||||
const handleClose = () => {
|
||||
setVisible(false);
|
||||
form.resetFields();
|
||||
setLoading(false)
|
||||
setData(null)
|
||||
};
|
||||
|
||||
const handleOpen = (vo: PromptReleaseData) => {
|
||||
setData(vo)
|
||||
setVisible(true);
|
||||
};
|
||||
// 封装保存方法,添加提交逻辑
|
||||
const handleSave = () => {
|
||||
if (!title || title.trim() === '') {
|
||||
message.warning(t('common.inputPlaceholder', { title: t('prompt.saveTitle') }))
|
||||
return
|
||||
}
|
||||
setLoading(true)
|
||||
savePrompt({
|
||||
...data,
|
||||
title
|
||||
} as PromptReleaseData)
|
||||
.then(() => {
|
||||
setLoading(false)
|
||||
refresh()
|
||||
handleClose()
|
||||
message.success(t('common.saveSuccess'))
|
||||
})
|
||||
.catch(() => {
|
||||
setLoading(false)
|
||||
});
|
||||
}
|
||||
|
||||
// 暴露给父组件的方法
|
||||
useImperativeHandle(ref, () => ({
|
||||
handleOpen,
|
||||
handleClose
|
||||
}));
|
||||
|
||||
return (
|
||||
<RbModal
|
||||
title={t('prompt.saveTitle')}
|
||||
open={visible}
|
||||
onCancel={handleClose}
|
||||
okText={t('common.save')}
|
||||
onOk={handleSave}
|
||||
confirmLoading={loading}
|
||||
>
|
||||
<Form
|
||||
form={form}
|
||||
layout="vertical"
|
||||
>
|
||||
<FormItem
|
||||
name="title"
|
||||
noStyle
|
||||
>
|
||||
<Input placeholder={t('common.enter')} />
|
||||
</FormItem>
|
||||
</Form>
|
||||
</RbModal>
|
||||
);
|
||||
});
|
||||
|
||||
export default PromptSaveModal;
|
||||
104
web/src/views/Prompt/components/PromptVariableModal.tsx
Normal file
104
web/src/views/Prompt/components/PromptVariableModal.tsx
Normal file
@@ -0,0 +1,104 @@
|
||||
import { forwardRef, useEffect, useImperativeHandle, useState } from 'react';
|
||||
import { Form, AutoComplete, type AutoCompleteProps } from 'antd';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
|
||||
import type { PromptVariableModalRef } from '../types'
|
||||
import RbModal from '@/components/RbModal'
|
||||
|
||||
const FormItem = Form.Item;
|
||||
|
||||
interface PromptVariableModalProps {
|
||||
refresh: (value: string) => void;
|
||||
variables: string[];
|
||||
}
|
||||
|
||||
const PromptVariableModal = forwardRef<PromptVariableModalRef, PromptVariableModalProps>(({
|
||||
refresh,
|
||||
variables
|
||||
}, ref) => {
|
||||
const { t } = useTranslation();
|
||||
const [visible, setVisible] = useState(false);
|
||||
const [form] = Form.useForm();
|
||||
const [loading, setLoading] = useState(false)
|
||||
const [options, setOptions] = useState<AutoCompleteProps['options']>([])
|
||||
|
||||
useEffect(() => {
|
||||
setOptions(variables.map(key => ({
|
||||
value: key,
|
||||
label: `{{${key}}}`
|
||||
})))
|
||||
}, [variables])
|
||||
const handleSearch = (value: string) => {
|
||||
const filterKeys = variables?.filter(key => key.includes(value))
|
||||
|
||||
if (filterKeys.length) {
|
||||
setOptions(filterKeys.map(key => ({
|
||||
value: key,
|
||||
label: `{{${key}}}`
|
||||
})))
|
||||
} else {
|
||||
setOptions([{
|
||||
value: value,
|
||||
label: `{{${value}}}`
|
||||
}])
|
||||
}
|
||||
}
|
||||
|
||||
// 封装取消方法,添加关闭弹窗逻辑
|
||||
const handleClose = () => {
|
||||
setVisible(false);
|
||||
form.resetFields();
|
||||
setLoading(false)
|
||||
};
|
||||
|
||||
const handleOpen = () => {
|
||||
setVisible(true);
|
||||
form.resetFields();
|
||||
};
|
||||
// 封装保存方法,添加提交逻辑
|
||||
const handleSave = () => {
|
||||
const variableName = form.getFieldValue('variableName')
|
||||
|
||||
if (!variableName) return
|
||||
|
||||
refresh(`{{${variableName}}}`)
|
||||
handleClose()
|
||||
}
|
||||
|
||||
// 暴露给父组件的方法
|
||||
useImperativeHandle(ref, () => ({
|
||||
handleOpen,
|
||||
handleClose
|
||||
}));
|
||||
|
||||
return (
|
||||
<RbModal
|
||||
title={t('application.addVariable')}
|
||||
open={visible}
|
||||
onCancel={handleClose}
|
||||
confirmLoading={loading}
|
||||
onOk={handleSave}
|
||||
okText={t('application.apply')}
|
||||
>
|
||||
<Form
|
||||
form={form}
|
||||
layout="vertical"
|
||||
scrollToFirstError={{ behavior: 'instant', block: 'end', focus: true }}
|
||||
>
|
||||
<FormItem
|
||||
name="variableName"
|
||||
label={t('application.defineVariableName')}
|
||||
extra={t('application.defineVariableNameExtra')}
|
||||
>
|
||||
<AutoComplete
|
||||
placeholder={t('application.defineVariableNamePlaceholder')}
|
||||
onSearch={handleSearch}
|
||||
options={options}
|
||||
/>
|
||||
</FormItem>
|
||||
</Form>
|
||||
</RbModal>
|
||||
);
|
||||
});
|
||||
|
||||
export default PromptVariableModal;
|
||||
59
web/src/views/Prompt/index.tsx
Normal file
59
web/src/views/Prompt/index.tsx
Normal file
@@ -0,0 +1,59 @@
|
||||
import { type FC, useState } from 'react';
|
||||
import { type SegmentedProps, Flex } from 'antd';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
|
||||
import PageTabs from '@/components/PageTabs';
|
||||
import SearchInput from '@/components/SearchInput'
|
||||
import PromptEditor from './Prompt';
|
||||
import History from './History'
|
||||
import type { HistoryQuery, HistoryItem } from './types';
|
||||
|
||||
const tabs = ['editor', 'history']
|
||||
const Prompt: FC = () => {
|
||||
const { t } = useTranslation();
|
||||
const [activeTab, setActiveTab] = useState<SegmentedProps['value']>(tabs[0])
|
||||
const [query, setQuery] = useState<HistoryQuery>({});
|
||||
const [editVo, setEditVo] = useState<HistoryItem | null>(null)
|
||||
|
||||
const handleChangeTab = (value: SegmentedProps['value']) => {
|
||||
setActiveTab(value)
|
||||
setEditVo(null)
|
||||
setQuery({})
|
||||
}
|
||||
const handleSearch = (value?: string) => {
|
||||
setQuery(prev => ({ ...prev, keyword: value }))
|
||||
}
|
||||
const handleEdit = (item: HistoryItem) => {
|
||||
console.log('edit', item)
|
||||
setEditVo(item)
|
||||
setActiveTab('editor')
|
||||
}
|
||||
const refresh = () => {
|
||||
setEditVo(null)
|
||||
}
|
||||
return (
|
||||
<>
|
||||
<Flex justify="space-between" align="center" className="rb:mb-4">
|
||||
<PageTabs
|
||||
value={activeTab}
|
||||
options={tabs.map(key => ({ label: t(`prompt.${key}`), value: key }))}
|
||||
onChange={handleChangeTab}
|
||||
/>
|
||||
{activeTab === 'history' &&
|
||||
<SearchInput
|
||||
placeholder={t('prompt.historySearchPlaceholder')}
|
||||
onSearch={handleSearch}
|
||||
className="rb:w-70"
|
||||
/>
|
||||
}
|
||||
</Flex>
|
||||
|
||||
<div className="rb:mt-4 rb:h-[calc(100vh-128px)]">
|
||||
{activeTab === 'editor' && <PromptEditor editVo={editVo} refresh={refresh} />}
|
||||
{activeTab === 'history' && <History query={query} edit={handleEdit} />}
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
export default Prompt;
|
||||
35
web/src/views/Prompt/types.ts
Normal file
35
web/src/views/Prompt/types.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
export interface PromptVariableModalRef {
|
||||
handleOpen: () => void;
|
||||
}
|
||||
|
||||
export interface AiPromptForm {
|
||||
model_id?: string;
|
||||
message?: string;
|
||||
current_prompt?: string;
|
||||
}
|
||||
|
||||
export interface PromptReleaseData {
|
||||
session_id: string;
|
||||
title?: string;
|
||||
prompt: string;
|
||||
}
|
||||
export interface HistoryQuery extends Record<string, unknown> {
|
||||
search?: string;
|
||||
}
|
||||
|
||||
export interface HistoryItem {
|
||||
id: string;
|
||||
title: string;
|
||||
prompt: string;
|
||||
created_at: number;
|
||||
first_message: string;
|
||||
}
|
||||
|
||||
export interface PromptDetailRef {
|
||||
handleOpen: (vo: HistoryItem) => void;
|
||||
handleClose: () => void;
|
||||
}
|
||||
|
||||
export interface PromptSaveModalRef {
|
||||
handleOpen: (vo: PromptReleaseData) => void;
|
||||
}
|
||||
Reference in New Issue
Block a user