feat: add chat history and session
This commit is contained in:
parent
441b94a061
commit
7d4e6bd299
@ -2,7 +2,7 @@ import { Query, QueryClient } from '@kevisual/query';
|
|||||||
import { QueryWs } from '@kevisual/query/ws';
|
import { QueryWs } from '@kevisual/query/ws';
|
||||||
import { modal } from './redirect-to-login';
|
import { modal } from './redirect-to-login';
|
||||||
import { create } from 'zustand';
|
import { create } from 'zustand';
|
||||||
export const query = new Query();
|
export const query = new QueryClient();
|
||||||
query.beforeRequest = async (config) => {
|
query.beforeRequest = async (config) => {
|
||||||
if (config.headers) {
|
if (config.headers) {
|
||||||
const token = localStorage.getItem('token');
|
const token = localStorage.getItem('token');
|
||||||
|
@ -12,3 +12,5 @@ export const App = () => {
|
|||||||
</Routes>
|
</Routes>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export * from './module/Select';
|
||||||
|
39
src/pages/ai-agent/module/Select.tsx
Normal file
39
src/pages/ai-agent/module/Select.tsx
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
import { query } from '@/modules';
|
||||||
|
import { Select as AntSelect, message, SelectProps } from 'antd';
|
||||||
|
import { useEffect, useState } from 'react';
|
||||||
|
|
||||||
|
export const Select = (props: SelectProps) => {
|
||||||
|
const [options, setOptions] = useState<{ value: string; id: string }[]>([]);
|
||||||
|
useEffect(() => {
|
||||||
|
fetch();
|
||||||
|
}, []);
|
||||||
|
const fetch = async () => {
|
||||||
|
const res = await query.post({
|
||||||
|
path: 'agent',
|
||||||
|
key: 'list',
|
||||||
|
});
|
||||||
|
if (res.code !== 200) {
|
||||||
|
message.error(res.message || '获取agent列表失败');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const data = res.data || [];
|
||||||
|
setOptions(
|
||||||
|
data.map((item: any) => {
|
||||||
|
return {
|
||||||
|
label: item.key,
|
||||||
|
value: item.id,
|
||||||
|
};
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
};
|
||||||
|
return (
|
||||||
|
<AntSelect
|
||||||
|
{...props}
|
||||||
|
options={options}
|
||||||
|
// onChange={(e) => {
|
||||||
|
// const labelValue = options.find((item) => item.value === e);
|
||||||
|
// props.onChange?.(e, options);
|
||||||
|
// }}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
};
|
@ -1,28 +1,180 @@
|
|||||||
import { useShallow } from 'zustand/react/shallow';
|
import { useShallow } from 'zustand/react/shallow';
|
||||||
import { useAiStore } from './store/ai-store';
|
import { useAiStore } from './store/ai-store';
|
||||||
import { CloseOutlined, HistoryOutlined } from '@ant-design/icons';
|
import { CloseOutlined, HistoryOutlined, PlusOutlined } from '@ant-design/icons';
|
||||||
import { Button, Form, Input, Tooltip } from 'antd';
|
import { Button, Dropdown, Form, Input, message, Modal, Tooltip } from 'antd';
|
||||||
import { useEffect } from 'react';
|
import { useEffect } from 'react';
|
||||||
import { TextArea } from '../container/components/TextArea';
|
import { TextArea } from '../container/components/TextArea';
|
||||||
import clsx from 'clsx';
|
import clsx from 'clsx';
|
||||||
import { marked } from 'marked';
|
import { query } from '@/modules';
|
||||||
|
import { nanoid } from 'nanoid';
|
||||||
|
import { ChatMessage } from './module/ChatMessage';
|
||||||
|
import { isObjectNull } from '@/utils/is-null';
|
||||||
|
import { useNavigate } from 'react-router';
|
||||||
|
const testId = '60aca66b-4be9-4266-9568-6001032c7e13';
|
||||||
|
|
||||||
|
type FormModalProps = {
|
||||||
|
send?: any;
|
||||||
|
};
|
||||||
|
const FormModal = (props: FormModalProps) => {
|
||||||
|
const [form] = Form.useForm();
|
||||||
|
const aiStore = useAiStore(
|
||||||
|
useShallow((state) => {
|
||||||
|
return {
|
||||||
|
showEdit: state.showEdit,
|
||||||
|
setShowEdit: state.setShowEdit,
|
||||||
|
formData: state.formData,
|
||||||
|
setMessage: state.setMessage,
|
||||||
|
};
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
useEffect(() => {
|
||||||
|
const open = aiStore.showEdit;
|
||||||
|
if (open) {
|
||||||
|
const isNull = isObjectNull(aiStore.formData);
|
||||||
|
if (isNull) {
|
||||||
|
form.setFieldsValue({});
|
||||||
|
} else form.setFieldsValue(aiStore.formData);
|
||||||
|
}
|
||||||
|
}, [aiStore.showEdit]);
|
||||||
|
const onFinish = async (values: any) => {
|
||||||
|
props.send({
|
||||||
|
type: 'changeSession',
|
||||||
|
data: {
|
||||||
|
id: testId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
aiStore.setMessage([]);
|
||||||
|
};
|
||||||
|
const onClose = () => {
|
||||||
|
aiStore.setShowEdit(false);
|
||||||
|
form.resetFields();
|
||||||
|
};
|
||||||
|
const isEdit = aiStore.formData.id;
|
||||||
|
return (
|
||||||
|
<Modal
|
||||||
|
title={isEdit ? 'Edit' : 'Add'}
|
||||||
|
open={aiStore.showEdit}
|
||||||
|
onClose={() => aiStore.setShowEdit(false)}
|
||||||
|
destroyOnClose
|
||||||
|
footer={false}
|
||||||
|
width={800}
|
||||||
|
onCancel={onClose}>
|
||||||
|
<Form
|
||||||
|
form={form}
|
||||||
|
onFinish={onFinish}
|
||||||
|
labelCol={{
|
||||||
|
span: 4,
|
||||||
|
}}
|
||||||
|
wrapperCol={{
|
||||||
|
span: 20,
|
||||||
|
}}>
|
||||||
|
<Form.Item name='id' hidden>
|
||||||
|
<Input />
|
||||||
|
</Form.Item>
|
||||||
|
<Form.Item name='sessionId' label='sessionId'>
|
||||||
|
<Input />
|
||||||
|
</Form.Item>
|
||||||
|
<Form.Item label=' ' colon={false}>
|
||||||
|
<Button type='primary' htmlType='submit'>
|
||||||
|
Submit
|
||||||
|
</Button>
|
||||||
|
<Button className='ml-2' htmlType='reset' onClick={onClose}>
|
||||||
|
Cancel
|
||||||
|
</Button>
|
||||||
|
</Form.Item>
|
||||||
|
</Form>
|
||||||
|
</Modal>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
export const useListenQuery = () => {
|
||||||
|
const aiStore = useAiStore(
|
||||||
|
useShallow((state) => {
|
||||||
|
return {
|
||||||
|
open: state.open,
|
||||||
|
key: state.key,
|
||||||
|
onMessage: state.onMessage,
|
||||||
|
};
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
// TODO: Implement useEffect
|
||||||
|
useEffect(() => {
|
||||||
|
if (!aiStore.open) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const open = aiStore.open;
|
||||||
|
send({
|
||||||
|
type: 'subscribe',
|
||||||
|
token: query.getToken(),
|
||||||
|
data: {
|
||||||
|
key: aiStore.key,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const close = query.qws.onMessage(onMessage, {
|
||||||
|
selector: (data) => {
|
||||||
|
const requestId = data.requestId;
|
||||||
|
return {
|
||||||
|
requestId,
|
||||||
|
...data.data,
|
||||||
|
};
|
||||||
|
},
|
||||||
|
});
|
||||||
|
return () => {
|
||||||
|
send({ type: 'unsubscribe' });
|
||||||
|
close();
|
||||||
|
};
|
||||||
|
}, [aiStore.open, query]);
|
||||||
|
const send = async (data: any) => {
|
||||||
|
query.qws.send(data, {
|
||||||
|
wrapper: (data) => ({
|
||||||
|
requestId: nanoid(16),
|
||||||
|
type: 'chat',
|
||||||
|
data: data,
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
};
|
||||||
|
const onMessage = (data: any) => {
|
||||||
|
type MessageData = {
|
||||||
|
chatId: string;
|
||||||
|
chatPromptId: string;
|
||||||
|
role: string;
|
||||||
|
root: boolean;
|
||||||
|
show: boolean;
|
||||||
|
data: {
|
||||||
|
message: string;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
if (data.code === 200 && data.type === 'messages') {
|
||||||
|
const messageData = data.data as MessageData;
|
||||||
|
aiStore.onMessage(messageData);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
return {
|
||||||
|
send,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
export const AiMoudle = () => {
|
export const AiMoudle = () => {
|
||||||
const [form] = Form.useForm();
|
const [form] = Form.useForm();
|
||||||
|
const navigate = useNavigate();
|
||||||
const aiStore = useAiStore(
|
const aiStore = useAiStore(
|
||||||
useShallow((state) => {
|
useShallow((state) => {
|
||||||
return {
|
return {
|
||||||
open: state.open,
|
open: state.open,
|
||||||
setOpen: state.setOpen,
|
setOpen: state.setOpen,
|
||||||
runAi: state.runAi,
|
|
||||||
formData: state.formData,
|
formData: state.formData,
|
||||||
setFormData: state.setFormData,
|
setFormData: state.setFormData,
|
||||||
messages: state.messages,
|
messages: state.messages,
|
||||||
setMessages: state.setMessage,
|
setMessages: state.setMessage,
|
||||||
|
key: state.key,
|
||||||
|
root: state.root,
|
||||||
|
setRoot: state.setRoot,
|
||||||
|
title: state.title,
|
||||||
|
setShowEdit: state.setShowEdit,
|
||||||
|
list: state.list,
|
||||||
|
getList: state.getList,
|
||||||
};
|
};
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!aiStore.open) {
|
if (!aiStore.open) {
|
||||||
return;
|
return;
|
||||||
@ -37,66 +189,36 @@ export const AiMoudle = () => {
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!aiStore.open) {
|
if (!aiStore.open) {
|
||||||
aiStore.setMessages([]);
|
aiStore.setMessages([]);
|
||||||
|
} else {
|
||||||
|
aiStore.getList();
|
||||||
}
|
}
|
||||||
}, [aiStore.open]);
|
}, [aiStore.open]);
|
||||||
|
const { send } = useListenQuery();
|
||||||
const onSend = () => {
|
const onSend = () => {
|
||||||
const data = form.getFieldsValue();
|
const data = form.getFieldsValue();
|
||||||
aiStore.setFormData(data);
|
send({ type: 'messages', data: { ...data, root: true } });
|
||||||
aiStore.runAi();
|
|
||||||
};
|
};
|
||||||
return (
|
const inputs = form.getFieldValue('inputs');
|
||||||
<div className={clsx('w-[600px] flex-shrink-0 bg-gray-100 border-l-2 shadow-lg flex flex-col', !aiStore?.open && 'hidden')}>
|
const isNotShow = inputs?.length === 0 || !inputs;
|
||||||
<div className='flex gap-4 bg-slate-400 p-2'>
|
const RootForm = (
|
||||||
<Button className='position ml-4 !bg-slate-400 !border-black' onClick={() => aiStore.setOpen(false)} icon={<CloseOutlined />}></Button>
|
<div className='flex flex-col relative'>
|
||||||
<h1 className='ml-4'>Ai Moudle</h1>
|
<div className={clsx('flex flex-col', isNotShow && 'invisible')}>
|
||||||
<div className='ml-4'>
|
<div className='mt-1 ml-2'>在当前页面,我能帮助你什么吗?</div>
|
||||||
<Tooltip title='历史会话'>
|
|
||||||
<Button className='!bg-slate-400 !border-black' icon={<HistoryOutlined />}></Button>
|
|
||||||
</Tooltip>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div className='flex-grow p-2 overflow-hidden h-full flex flex-col'>
|
|
||||||
<div className='flex flex-col'>
|
|
||||||
<div className='mb-3'> chat message</div>
|
|
||||||
{aiStore?.messages?.map((message, index) => {
|
|
||||||
const html = marked.parse(message?.content);
|
|
||||||
return (
|
|
||||||
<div key={index} className=' justify-between px-4 w-full'>
|
|
||||||
<div className='h3 font-bold'>{message?.role}</div>
|
|
||||||
<div
|
|
||||||
className='p-4 text-xs border shadow-sm rounded-sm scrollbar max-h-[200px] w-full overflow-scroll'
|
|
||||||
dangerouslySetInnerHTML={{ __html: html }}></div>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
})}
|
|
||||||
</div>
|
|
||||||
<div className='mt-4'>
|
<div className='mt-4'>
|
||||||
<Form form={form}>
|
<Form form={form}>
|
||||||
<Form.Item hidden name='id'>
|
|
||||||
<Input placeholder='message' />
|
|
||||||
</Form.Item>
|
|
||||||
<Form.Item name='messages' hidden>
|
|
||||||
<Input placeholder='message' />
|
|
||||||
</Form.Item>
|
|
||||||
<Form.Item name='key' hidden>
|
|
||||||
<Input placeholder='key' />
|
|
||||||
</Form.Item>
|
|
||||||
<Form.List name={'inputs'} initialValue={[]}>
|
<Form.List name={'inputs'} initialValue={[]}>
|
||||||
{(fields, { add, remove }) => {
|
{(fields, { add, remove }) => {
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{fields.map((field, index) => {
|
{fields.map((field, index) => {
|
||||||
const key = form.getFieldValue(['inputs', index, 'key']);
|
const key = form.getFieldValue(['inputs', index, 'key']);
|
||||||
console.log('key', key);
|
|
||||||
const isTitle = key === 'title';
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div key={field.name + '-' + index} className=''>
|
<div key={field.name + '-' + index} className=''>
|
||||||
<Form.Item name={[field.name, 'key']} rules={[{ required: true, message: 'Missing name' }]} hidden noStyle>
|
<Form.Item name={[field.name, 'key']} rules={[{ required: true, message: 'Missing name' }]} hidden noStyle>
|
||||||
<Input placeholder='name' />
|
<Input placeholder='name' />
|
||||||
</Form.Item>
|
</Form.Item>
|
||||||
<Form.Item className='!mb-0' name={[field.name, 'value']} rules={[{ required: true, message: 'Missing value' }]}>
|
<Form.Item className='!mb-0' name={[field.name, 'value']} rules={[{ required: true, message: 'Missing value' }]}>
|
||||||
<TextArea className='scrollbar' style={{ minHeight: isTitle ? 40 : 300 }} placeholder={key} />
|
<TextArea className='scrollbar' style={{ minHeight: 180 }} placeholder={key} />
|
||||||
</Form.Item>
|
</Form.Item>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
@ -106,13 +228,95 @@ export const AiMoudle = () => {
|
|||||||
}}
|
}}
|
||||||
</Form.List>
|
</Form.List>
|
||||||
</Form>
|
</Form>
|
||||||
<div className='p-4'>
|
<div className='px-4'>
|
||||||
<Button className='mt-4' onClick={onSend}>
|
<Button type='primary' className='mt-4' onClick={onSend}>
|
||||||
Send
|
发送
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
{isNotShow && (
|
||||||
|
<div className='mt-2 text-gray-400 flex absolute top-3'>
|
||||||
|
当前页面没有配置,点击
|
||||||
|
<div
|
||||||
|
className='text-blue-500 cursor-pointer'
|
||||||
|
onClick={(e) => {
|
||||||
|
navigate('/chat/chat-prompt/list');
|
||||||
|
}}>
|
||||||
|
去配置
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
const ChatForm = (
|
||||||
|
<ChatMessage
|
||||||
|
messages={aiStore.messages}
|
||||||
|
onSend={(value) => {
|
||||||
|
send({ type: 'messages', data: { message: value } });
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
const items = aiStore.list.map((item) => {
|
||||||
|
return {
|
||||||
|
key: item.id,
|
||||||
|
label: item.title || item.id,
|
||||||
|
onClick: () => {
|
||||||
|
// aiStore.setKey(item.id);
|
||||||
|
// 选中具体的
|
||||||
|
send({
|
||||||
|
type: 'changeSession',
|
||||||
|
data: {
|
||||||
|
id: item.id,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
aiStore.setMessages([]);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
});
|
||||||
|
return (
|
||||||
|
<div className={clsx('w-[600px] flex-shrink-0 bg-gray-100 border-l-2 shadow-lg flex flex-col', !aiStore?.open && 'hidden')}>
|
||||||
|
<div className='flex justify-between p-2 border bg-white'>
|
||||||
|
<div className='flex gap-4'>
|
||||||
|
<Button className='position ml-4 ' onClick={() => aiStore.setOpen(false)} icon={<CloseOutlined />}></Button>
|
||||||
|
<h1 className=''>{aiStore.title || 'Ai Module'}</h1>
|
||||||
|
</div>
|
||||||
|
<div className='mr-4 flex'>
|
||||||
|
{!aiStore.root && (
|
||||||
|
<Button
|
||||||
|
className='mr-2'
|
||||||
|
onClick={() => {
|
||||||
|
aiStore.setMessages([]);
|
||||||
|
aiStore.setRoot(true);
|
||||||
|
send({
|
||||||
|
type: 'changeSession',
|
||||||
|
data: {
|
||||||
|
id: null,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}}
|
||||||
|
icon={<PlusOutlined />}></Button>
|
||||||
|
)}
|
||||||
|
<Dropdown
|
||||||
|
className='mr-2'
|
||||||
|
placement='bottomRight'
|
||||||
|
menu={{
|
||||||
|
items,
|
||||||
|
}}>
|
||||||
|
<Button className='' icon={<HistoryOutlined />}></Button>
|
||||||
|
</Dropdown>
|
||||||
|
{/* <Tooltip title='历史会话'>
|
||||||
|
<Button
|
||||||
|
className=''
|
||||||
|
onClick={() => {
|
||||||
|
aiStore.setShowEdit(true);
|
||||||
|
}}
|
||||||
|
icon={<HistoryOutlined />}></Button>
|
||||||
|
</Tooltip> */}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div className='flex-grow p-2 overflow-hidden h-full flex flex-col'>{aiStore.root ? RootForm : ChatForm}</div>
|
||||||
|
<FormModal send={send} />
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
16
src/pages/ai-chat/assets/ai-avatar.svg
Normal file
16
src/pages/ai-chat/assets/ai-avatar.svg
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
<svg
|
||||||
|
xmlns="http://www.w3.org/2000/svg"
|
||||||
|
width="32"
|
||||||
|
height="32"
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
fill="none"
|
||||||
|
stroke="currentColor"
|
||||||
|
stroke-width="2"
|
||||||
|
stroke-linecap="round"
|
||||||
|
stroke-linejoin="round"
|
||||||
|
>
|
||||||
|
<rect x="2" y="7" width="20" height="14" rx="2" ry="2"></rect>
|
||||||
|
<rect x="6" y="11" width="4" height="4"></rect>
|
||||||
|
<rect x="14" y="11" width="4" height="4"></rect>
|
||||||
|
<path d="M6 7V5a2 2 0 012-2h8a2 2 0 012 2v2"></path>
|
||||||
|
</svg>
|
After Width: | Height: | Size: 429 B |
14
src/pages/ai-chat/assets/user-avatar.svg
Normal file
14
src/pages/ai-chat/assets/user-avatar.svg
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
<svg
|
||||||
|
xmlns="http://www.w3.org/2000/svg"
|
||||||
|
width="64"
|
||||||
|
height="64"
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
fill="none"
|
||||||
|
stroke="currentColor"
|
||||||
|
stroke-width="2"
|
||||||
|
stroke-linecap="round"
|
||||||
|
stroke-linejoin="round"
|
||||||
|
>
|
||||||
|
<circle cx="12" cy="7" r="5"></circle>
|
||||||
|
<path d="M5.5 21h13a2 2 0 002-2v-1a7 7 0 00-14 0v1a2 2 0 002 2z"></path>
|
||||||
|
</svg>
|
After Width: | Height: | Size: 324 B |
79
src/pages/ai-chat/module/ChatMessage.tsx
Normal file
79
src/pages/ai-chat/module/ChatMessage.tsx
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
import { marked } from 'marked';
|
||||||
|
import clsx from 'clsx';
|
||||||
|
import AIAvatar from '../assets/ai-avatar.svg'; // 替换为您的 AI 头像路径
|
||||||
|
import UserAvatar from '../assets/user-avatar.svg'; // 替换为您的用户头像路径
|
||||||
|
import { TextArea } from '@/pages/container/components/TextArea';
|
||||||
|
import { Button } from 'antd';
|
||||||
|
import { SendOutlined } from '@ant-design/icons';
|
||||||
|
import { useState } from 'react';
|
||||||
|
|
||||||
|
export type MessageType = 'ai' | 'user';
|
||||||
|
|
||||||
|
export type Message = {
|
||||||
|
role: string;
|
||||||
|
content: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const ChatMessageOne = (props: { message: Message }) => {
|
||||||
|
const { message } = props;
|
||||||
|
const htmlContent = marked.parse(message.content);
|
||||||
|
const isUser = message.role === 'user';
|
||||||
|
|
||||||
|
const containerClasses = clsx('flex mb-4 items-start', {
|
||||||
|
'justify-start flex-row': !isUser,
|
||||||
|
'flex-row-reverse': isUser,
|
||||||
|
});
|
||||||
|
const messageClasses = clsx('max-w-[76%] p-3 rounded-lg ', {
|
||||||
|
'bg-blue-500 text-white': isUser,
|
||||||
|
'bg-gray-200 text-gray-800': !isUser,
|
||||||
|
});
|
||||||
|
|
||||||
|
const avatarSrc = isUser ? UserAvatar : AIAvatar;
|
||||||
|
const avatarAlt = isUser ? 'User Avatar' : 'AI Avatar';
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className={containerClasses}>
|
||||||
|
<img src={avatarSrc} alt={avatarAlt} className='w-10 h-10 mx-2 rounded-full border border-gray-300 p-2' />
|
||||||
|
<div className={messageClasses} dangerouslySetInnerHTML={{ __html: htmlContent }} />
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
type ChatMessageProps = {
|
||||||
|
messages: Message[];
|
||||||
|
onSend?: (message: string) => void;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const ChatMessage = (props: ChatMessageProps) => {
|
||||||
|
const { messages } = props;
|
||||||
|
const [inputValue, setInputValue] = useState('');
|
||||||
|
const onSend = () => {
|
||||||
|
if (!inputValue) return;
|
||||||
|
props.onSend?.(inputValue);
|
||||||
|
setInputValue('');
|
||||||
|
};
|
||||||
|
return (
|
||||||
|
<div className='w-full h-full'>
|
||||||
|
<div className='max-h-[80%] overflow-y-auto scrollbar bg-white p-2 mx-1 border shadow-sm pt-5 rounded-lg'>
|
||||||
|
<div className='flex flex-col'>
|
||||||
|
{messages.map((message, index) => (
|
||||||
|
<ChatMessageOne key={index} message={message} />
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div className='relative'>
|
||||||
|
<TextArea
|
||||||
|
value={inputValue}
|
||||||
|
onChange={(e) => {
|
||||||
|
setInputValue(e);
|
||||||
|
}}
|
||||||
|
className='max-h-64'
|
||||||
|
style={{ minHeight: 180 }}
|
||||||
|
language='markdown'
|
||||||
|
placeholder='Message AI'
|
||||||
|
/>
|
||||||
|
<Button onClick={onSend} className='absolute bottom-4 right-4 cursor-pointer' type='primary' icon={<SendOutlined />}></Button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
25
src/pages/ai-chat/store/ai-history.ts
Normal file
25
src/pages/ai-chat/store/ai-history.ts
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
import { StateCreator } from 'zustand';
|
||||||
|
import { AiStore } from './ai-store';
|
||||||
|
import { query } from '@/modules';
|
||||||
|
export type AiHistoryStore = {
|
||||||
|
list: any[];
|
||||||
|
getList: () => Promise<any>;
|
||||||
|
};
|
||||||
|
export const createAiHistoryStore: StateCreator<AiStore, [], [], AiHistoryStore> = (set, get, store) => {
|
||||||
|
return {
|
||||||
|
list: [],
|
||||||
|
getList: async () => {
|
||||||
|
const { key } = get();
|
||||||
|
set({ loading: true });
|
||||||
|
const res = await query.post({
|
||||||
|
path: 'chat-session',
|
||||||
|
key: 'list-history',
|
||||||
|
data: { key },
|
||||||
|
});
|
||||||
|
if (res.success) {
|
||||||
|
set({ list: res.data });
|
||||||
|
}
|
||||||
|
console.log('res', res);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
};
|
@ -1,6 +1,8 @@
|
|||||||
import { query } from '@/modules';
|
import { query } from '@/modules';
|
||||||
import { message } from 'antd';
|
import { message } from 'antd';
|
||||||
|
import { produce } from 'immer';
|
||||||
import { create } from 'zustand';
|
import { create } from 'zustand';
|
||||||
|
import { AiHistoryStore, createAiHistoryStore } from './ai-history';
|
||||||
type ResData = {
|
type ResData = {
|
||||||
created_at: string;
|
created_at: string;
|
||||||
done?: boolean;
|
done?: boolean;
|
||||||
@ -14,48 +16,87 @@ type ResData = {
|
|||||||
prompt_eval_duration?: number;
|
prompt_eval_duration?: number;
|
||||||
total_duration?: number;
|
total_duration?: number;
|
||||||
};
|
};
|
||||||
|
type MessageData = {
|
||||||
|
chatId: string;
|
||||||
|
chatPromptId: string;
|
||||||
|
role: string;
|
||||||
|
root: boolean;
|
||||||
|
show: boolean;
|
||||||
|
data: {
|
||||||
|
message: string;
|
||||||
|
};
|
||||||
|
};
|
||||||
export type AiStore = {
|
export type AiStore = {
|
||||||
|
/** 打开侧边栏 */
|
||||||
open: boolean;
|
open: boolean;
|
||||||
setOpen: (open: boolean) => void;
|
setOpen: (open: boolean) => void;
|
||||||
|
showEdit: boolean;
|
||||||
|
setShowEdit: (showEdit: boolean) => void;
|
||||||
|
loading: boolean;
|
||||||
|
setLoading: (loading: boolean) => void;
|
||||||
type?: string;
|
type?: string;
|
||||||
|
/** 当前app执行的key */
|
||||||
key: string;
|
key: string;
|
||||||
setKey: (key: string) => void;
|
setKey: (key: string) => void;
|
||||||
setType?: (type: string) => void;
|
setType?: (type: string) => void;
|
||||||
sendMsg: (msg: string) => void;
|
|
||||||
formData: any;
|
formData: any;
|
||||||
setFormData: (data: any) => void;
|
setFormData: (data: any) => void;
|
||||||
data: any;
|
title: string; // AI Module的标题
|
||||||
setData: (data: any) => void;
|
|
||||||
runAi: () => any;
|
|
||||||
title: string;
|
|
||||||
setTitle: (title: string) => void;
|
setTitle: (title: string) => void;
|
||||||
messages: { role: string; content: string }[];
|
messages: { role: string; content: string }[];
|
||||||
setMessage: (message: { role: string; content: string }[]) => void;
|
setMessage: (message: { role: string; content: string }[]) => void;
|
||||||
};
|
getPrompt: (key?: string) => any;
|
||||||
|
// 设置预设数据,只有是root的情况下才成立
|
||||||
|
presetData?: any;
|
||||||
|
setPresetData: (data: any) => void;
|
||||||
|
/** 是否是第一条消息 */
|
||||||
|
root: boolean;
|
||||||
|
setRoot: (root: boolean) => void;
|
||||||
|
onMessage: (data: MessageData) => void;
|
||||||
|
} & AiHistoryStore;
|
||||||
|
|
||||||
export const useAiStore = create<AiStore>((set, get) => {
|
export const useAiStore = create<AiStore>((set, get, store) => {
|
||||||
return {
|
return {
|
||||||
open: false,
|
open: false,
|
||||||
setOpen: (open) => set({ open }),
|
setOpen: (open) => set({ open, root: true }),
|
||||||
|
loading: false,
|
||||||
|
setLoading: (loading) => set({ loading }),
|
||||||
key: '',
|
key: '',
|
||||||
setKey: (key) => set({ key }),
|
setKey: (key) => {
|
||||||
sendMsg: (msg) => {
|
const { getPrompt } = get();
|
||||||
console.log(msg);
|
if (key) {
|
||||||
|
getPrompt(key);
|
||||||
|
}
|
||||||
|
set({ key });
|
||||||
},
|
},
|
||||||
formData: {},
|
formData: {},
|
||||||
setFormData: (data) => set({ formData: data }),
|
setFormData: (data) => set({ formData: data }),
|
||||||
data: {},
|
title: '',
|
||||||
setData: (data) => {
|
setTitle: (title) => set({ title }),
|
||||||
const { key, presetData = {} } = data;
|
messages: [],
|
||||||
console.log('key', presetData, data);
|
setMessage: (messages) => set({ messages }),
|
||||||
if (!key) {
|
getPrompt: async (key) => {
|
||||||
console.error('key is required');
|
const state = get();
|
||||||
return;
|
const res = await query.post({
|
||||||
|
path: 'chat-prompt',
|
||||||
|
key: 'getByKey',
|
||||||
|
data: {
|
||||||
|
key: key || state.key,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
if (res.code === 200) {
|
||||||
|
const { prompt } = res.data;
|
||||||
|
const { presetData } = prompt;
|
||||||
|
console.log(res.data);
|
||||||
|
state.setPresetData(presetData);
|
||||||
|
set({ root: true });
|
||||||
}
|
}
|
||||||
const { inputs = [] } = presetData.data || {};
|
},
|
||||||
|
presetData: {},
|
||||||
|
setPresetData: (data) => {
|
||||||
|
set({ presetData: data });
|
||||||
|
const { inputs = [] } = data?.data || {};
|
||||||
const formData = {
|
const formData = {
|
||||||
key,
|
|
||||||
inputs: inputs.map((input) => {
|
inputs: inputs.map((input) => {
|
||||||
return {
|
return {
|
||||||
key: input.key,
|
key: input.key,
|
||||||
@ -65,40 +106,23 @@ export const useAiStore = create<AiStore>((set, get) => {
|
|||||||
}),
|
}),
|
||||||
messages: [],
|
messages: [],
|
||||||
};
|
};
|
||||||
console.log('formData', formData);
|
set({ formData });
|
||||||
set({ key, data, formData });
|
|
||||||
},
|
},
|
||||||
runAi: async () => {
|
root: false,
|
||||||
const { formData, messages } = get();
|
setRoot: (root) => set({ root }),
|
||||||
const loading = message.loading('loading');
|
onMessage: (data) => {
|
||||||
const res = await query.post({
|
const state = get();
|
||||||
path: 'ai',
|
if (state.root) set({ root: false });
|
||||||
key: 'run',
|
const { message } = data.data;
|
||||||
data: {
|
const role = data.role;
|
||||||
key: formData.key,
|
set(
|
||||||
inputs: [
|
produce((state) => {
|
||||||
// {
|
state.messages.push({ role, content: message });
|
||||||
// key: 'title',
|
}),
|
||||||
// value: '根据描述生成代码',
|
);
|
||||||
// },
|
|
||||||
// {
|
|
||||||
// key: 'description',
|
|
||||||
// value: '我想获取一个card, 包含标题和内容,标题是evision,内容是这是一个测试',
|
|
||||||
// },
|
|
||||||
...formData.inputs,
|
|
||||||
],
|
|
||||||
},
|
|
||||||
});
|
|
||||||
loading();
|
|
||||||
if (res.code === 200) {
|
|
||||||
console.log(res.data);
|
|
||||||
message.success('Success');
|
|
||||||
set({ messages: [...messages, res.data.message] });
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
title: '',
|
showEdit: false,
|
||||||
setTitle: (title) => set({ title }),
|
setShowEdit: (showEdit) => set({ showEdit }),
|
||||||
messages: [],
|
...createAiHistoryStore(set, get, store),
|
||||||
setMessage: (messages) => set({ messages }),
|
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
170
src/pages/chat-manager/chat-prompt/List.tsx
Normal file
170
src/pages/chat-manager/chat-prompt/List.tsx
Normal file
@ -0,0 +1,170 @@
|
|||||||
|
import { useShallow } from 'zustand/react/shallow';
|
||||||
|
import { useChatPromptStore } from '../store/chat-prompt';
|
||||||
|
import { useEffect } from 'react';
|
||||||
|
import { Button, Form, Input, message, Modal, Tooltip } from 'antd';
|
||||||
|
import { DeleteOutlined, EditOutlined, MessageOutlined, PlusOutlined } from '@ant-design/icons';
|
||||||
|
import { isObjectNull } from '@/utils/is-null';
|
||||||
|
import { Select as AiAgentSelect } from '../../ai-agent';
|
||||||
|
import { Select as PromptSelect } from '../../prompt';
|
||||||
|
import { CardBlank } from '@/components/card/CardBlank';
|
||||||
|
import { useAiStore } from '@/pages/ai-chat';
|
||||||
|
|
||||||
|
const FormModal = () => {
|
||||||
|
const [form] = Form.useForm();
|
||||||
|
const chatPromptStore = useChatPromptStore(
|
||||||
|
useShallow((state) => {
|
||||||
|
return {
|
||||||
|
showEdit: state.showEdit,
|
||||||
|
setShowEdit: state.setShowEdit,
|
||||||
|
formData: state.formData,
|
||||||
|
updateData: state.updateData,
|
||||||
|
};
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
useEffect(() => {
|
||||||
|
const open = chatPromptStore.showEdit;
|
||||||
|
if (open) {
|
||||||
|
const isNull = isObjectNull(chatPromptStore.formData);
|
||||||
|
if (isNull) {
|
||||||
|
form.setFieldsValue({});
|
||||||
|
} else form.setFieldsValue(chatPromptStore.formData);
|
||||||
|
}
|
||||||
|
}, [chatPromptStore.showEdit]);
|
||||||
|
const onFinish = async (values: any) => {
|
||||||
|
chatPromptStore.updateData(values);
|
||||||
|
};
|
||||||
|
const onClose = () => {
|
||||||
|
chatPromptStore.setShowEdit(false);
|
||||||
|
form.resetFields();
|
||||||
|
};
|
||||||
|
const isEdit = chatPromptStore.formData.id;
|
||||||
|
return (
|
||||||
|
<Modal
|
||||||
|
title={isEdit ? 'Edit' : 'Add'}
|
||||||
|
open={chatPromptStore.showEdit}
|
||||||
|
onClose={() => chatPromptStore.setShowEdit(false)}
|
||||||
|
destroyOnClose
|
||||||
|
footer={false}
|
||||||
|
width={800}
|
||||||
|
onCancel={onClose}>
|
||||||
|
<Form
|
||||||
|
form={form}
|
||||||
|
onFinish={onFinish}
|
||||||
|
labelCol={{
|
||||||
|
span: 4,
|
||||||
|
}}
|
||||||
|
wrapperCol={{
|
||||||
|
span: 20,
|
||||||
|
}}>
|
||||||
|
<Form.Item name='id' hidden>
|
||||||
|
<Input />
|
||||||
|
</Form.Item>
|
||||||
|
<Form.Item name='title' label='title'>
|
||||||
|
<Input />
|
||||||
|
</Form.Item>
|
||||||
|
<Form.Item name='description' label='description'>
|
||||||
|
<Input.TextArea rows={4} />
|
||||||
|
</Form.Item>
|
||||||
|
<Form.Item label='key' name='key'>
|
||||||
|
<Input />
|
||||||
|
</Form.Item>
|
||||||
|
<Form.Item label='agent' name={['data', 'aiAgentId']}>
|
||||||
|
<AiAgentSelect />
|
||||||
|
</Form.Item>
|
||||||
|
<Form.Item label='prompt' name={['data', 'promptId']}>
|
||||||
|
<PromptSelect />
|
||||||
|
</Form.Item>
|
||||||
|
<Form.Item label=' ' colon={false}>
|
||||||
|
<Button type='primary' htmlType='submit'>
|
||||||
|
Submit
|
||||||
|
</Button>
|
||||||
|
<Button className='ml-2' htmlType='reset' onClick={onClose}>
|
||||||
|
Cancel
|
||||||
|
</Button>
|
||||||
|
</Form.Item>
|
||||||
|
</Form>
|
||||||
|
</Modal>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
export const List = () => {
|
||||||
|
const chatPromptStore = useChatPromptStore(
|
||||||
|
useShallow((state) => {
|
||||||
|
return {
|
||||||
|
list: state.list,
|
||||||
|
getList: state.getList,
|
||||||
|
showEdit: state.showEdit,
|
||||||
|
setShowEdit: state.setShowEdit,
|
||||||
|
setFormData: state.setFormData,
|
||||||
|
deleteData: state.deleteData,
|
||||||
|
};
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
const aiStore = useAiStore(
|
||||||
|
useShallow((state) => {
|
||||||
|
return {
|
||||||
|
open: state.open,
|
||||||
|
setOpen: state.setOpen,
|
||||||
|
setKey: state.setKey,
|
||||||
|
};
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
useEffect(() => {
|
||||||
|
chatPromptStore.getList();
|
||||||
|
}, []);
|
||||||
|
return (
|
||||||
|
<div className='flex w-full h-full bg-slate-200'>
|
||||||
|
<div className='h-full bg-white flex-shrink-0 flex justify-center'>
|
||||||
|
<Button className='m-4' onClick={() => chatPromptStore.setShowEdit(true)} icon={<PlusOutlined />}></Button>
|
||||||
|
</div>
|
||||||
|
<div className='p-4'>
|
||||||
|
<div className='flex flex-wrap gap-4'>
|
||||||
|
{chatPromptStore.list.map((item) => {
|
||||||
|
return (
|
||||||
|
<div className='card w-[400px] max-h-[300px] scrollbar' key={item.id}>
|
||||||
|
<div className='card-title flex items-center '>
|
||||||
|
<div>{item.title} </div>
|
||||||
|
<div className='card-key ml-4'>{item.key}</div>
|
||||||
|
</div>
|
||||||
|
<div className='font-light mt-2'>{item.description ? item.description : '-'}</div>
|
||||||
|
<div>
|
||||||
|
<div className='flex mt-2 '>
|
||||||
|
<Button.Group>
|
||||||
|
<Tooltip title='Edit'>
|
||||||
|
<Button
|
||||||
|
onClick={(e) => {
|
||||||
|
chatPromptStore.setFormData(item);
|
||||||
|
chatPromptStore.setShowEdit(true);
|
||||||
|
e.stopPropagation();
|
||||||
|
}}
|
||||||
|
icon={<EditOutlined />}></Button>
|
||||||
|
</Tooltip>
|
||||||
|
<Tooltip title='Delete'>
|
||||||
|
<Button
|
||||||
|
onClick={(e) => {
|
||||||
|
chatPromptStore.deleteData(item.id);
|
||||||
|
e.stopPropagation();
|
||||||
|
}}
|
||||||
|
icon={<DeleteOutlined />}></Button>
|
||||||
|
</Tooltip>
|
||||||
|
<Tooltip title='Chat'>
|
||||||
|
<Button
|
||||||
|
onClick={(e) => {
|
||||||
|
aiStore.setKey(location.pathname);
|
||||||
|
aiStore.setOpen(true);
|
||||||
|
}}
|
||||||
|
icon={<MessageOutlined />}></Button>
|
||||||
|
</Tooltip>
|
||||||
|
</Button.Group>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
<CardBlank className='w-[300px]' />
|
||||||
|
{!chatPromptStore.list.length && <div className='text-center w-full'>No data</div>}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<FormModal />
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
@ -1,6 +1,9 @@
|
|||||||
import { useShallow } from 'zustand/react/shallow';
|
import { useShallow } from 'zustand/react/shallow';
|
||||||
import { useHistoryStore } from '../store/history';
|
import { useHistoryStore } from '../store/history';
|
||||||
import { useEffect } from 'react';
|
import { useEffect } from 'react';
|
||||||
|
import { CardBlank } from '@/components/card/CardBlank';
|
||||||
|
import { Button, Tooltip } from 'antd';
|
||||||
|
import { DeleteOutlined } from '@ant-design/icons';
|
||||||
|
|
||||||
export const List = () => {
|
export const List = () => {
|
||||||
const historyStore = useHistoryStore(
|
const historyStore = useHistoryStore(
|
||||||
@ -8,6 +11,7 @@ export const List = () => {
|
|||||||
return {
|
return {
|
||||||
list: state.list,
|
list: state.list,
|
||||||
getList: state.getList,
|
getList: state.getList,
|
||||||
|
deleteData: state.deleteData,
|
||||||
};
|
};
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
@ -15,10 +19,30 @@ export const List = () => {
|
|||||||
historyStore.getList();
|
historyStore.getList();
|
||||||
}, []);
|
}, []);
|
||||||
return (
|
return (
|
||||||
<div>
|
<div className='flex w-full h-full bg-slate-200'>
|
||||||
{historyStore.list.map((item) => {
|
<div className='p-4'>
|
||||||
return <div key={item.id}>{item.id}</div>;
|
<div className=' flex gap-4 flex-wrap'>
|
||||||
})}
|
{historyStore.list.map((item) => {
|
||||||
|
return (
|
||||||
|
<div key={item.id} className='card'>
|
||||||
|
<div>{item.id}</div>
|
||||||
|
<div>{item.title}</div>
|
||||||
|
<div>{item.chatId}</div>
|
||||||
|
<div>{item.promptId}</div>
|
||||||
|
{/* <div>{item.data.message || '-'}</div> */}
|
||||||
|
<div className='card-footer mt-3'>
|
||||||
|
<Button.Group>
|
||||||
|
<Tooltip title='Delete'>
|
||||||
|
<Button onClick={() => historyStore.deleteData(item.id)} icon={<DeleteOutlined />}></Button>
|
||||||
|
</Tooltip>
|
||||||
|
</Button.Group>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
<CardBlank />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
import { Navigate, Route, Routes } from 'react-router-dom';
|
import { Navigate, Route, Routes } from 'react-router-dom';
|
||||||
import { Main } from './layouts';
|
import { Main } from './layouts';
|
||||||
import {List as HistoryList } from './history/List'
|
import { List as HistoryList } from './history/List';
|
||||||
|
import { List as SessionList } from './session/List';
|
||||||
|
import { List as ChatPromptList } from './chat-prompt/List';
|
||||||
|
|
||||||
export const App = () => {
|
export const App = () => {
|
||||||
return (
|
return (
|
||||||
@ -8,8 +10,9 @@ export const App = () => {
|
|||||||
<Route element={<Main />}>
|
<Route element={<Main />}>
|
||||||
<Route path='/' element={<Navigate to='/chat/history/list' />}></Route>
|
<Route path='/' element={<Navigate to='/chat/history/list' />}></Route>
|
||||||
<Route path='history/list' element={<HistoryList />} />
|
<Route path='history/list' element={<HistoryList />} />
|
||||||
|
<Route path='session/list' element={<SessionList />} />
|
||||||
|
<Route path='chat-prompt/list' element={<ChatPromptList />} />
|
||||||
</Route>
|
</Route>
|
||||||
</Routes>
|
</Routes>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
136
src/pages/chat-manager/session/List.tsx
Normal file
136
src/pages/chat-manager/session/List.tsx
Normal file
@ -0,0 +1,136 @@
|
|||||||
|
import { useShallow } from 'zustand/react/shallow';
|
||||||
|
import { useSessionStore } from '../store/session';
|
||||||
|
import { useEffect } from 'react';
|
||||||
|
import { Button, Form, Input, message, Modal, Tooltip } from 'antd';
|
||||||
|
import { DeleteOutlined, HistoryOutlined, PlusOutlined } from '@ant-design/icons';
|
||||||
|
import { isObjectNull } from '@/utils/is-null';
|
||||||
|
import { useAiStore } from '@/pages/ai-chat';
|
||||||
|
|
||||||
|
const FormModal = () => {
|
||||||
|
const [form] = Form.useForm();
|
||||||
|
const sessionStore = useSessionStore(
|
||||||
|
useShallow((state) => {
|
||||||
|
return {
|
||||||
|
showEdit: state.showEdit,
|
||||||
|
setShowEdit: state.setShowEdit,
|
||||||
|
formData: state.formData,
|
||||||
|
updateData: state.updateData,
|
||||||
|
};
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
useEffect(() => {
|
||||||
|
const open = sessionStore.showEdit;
|
||||||
|
if (open) {
|
||||||
|
const isNull = isObjectNull(sessionStore.formData);
|
||||||
|
if (isNull) {
|
||||||
|
form.setFieldsValue({});
|
||||||
|
} else form.setFieldsValue(sessionStore.formData);
|
||||||
|
}
|
||||||
|
}, [sessionStore.showEdit]);
|
||||||
|
const onFinish = async (values: any) => {
|
||||||
|
sessionStore.updateData(values);
|
||||||
|
};
|
||||||
|
const onClose = () => {
|
||||||
|
sessionStore.setShowEdit(false);
|
||||||
|
form.resetFields();
|
||||||
|
};
|
||||||
|
const isEdit = sessionStore.formData.id;
|
||||||
|
return (
|
||||||
|
<Modal
|
||||||
|
title={isEdit ? 'Edit' : 'Add'}
|
||||||
|
open={sessionStore.showEdit}
|
||||||
|
onClose={() => sessionStore.setShowEdit(false)}
|
||||||
|
destroyOnClose
|
||||||
|
footer={false}
|
||||||
|
width={800}
|
||||||
|
onCancel={onClose}>
|
||||||
|
<Form
|
||||||
|
form={form}
|
||||||
|
onFinish={onFinish}
|
||||||
|
labelCol={{
|
||||||
|
span: 4,
|
||||||
|
}}
|
||||||
|
wrapperCol={{
|
||||||
|
span: 20,
|
||||||
|
}}>
|
||||||
|
<Form.Item name='id' hidden>
|
||||||
|
<Input />
|
||||||
|
</Form.Item>
|
||||||
|
<Form.Item name='type' label='type'>
|
||||||
|
<Input />
|
||||||
|
</Form.Item>
|
||||||
|
<Form.Item name='chatPromptId' label='chatPromptId'>
|
||||||
|
<Input />
|
||||||
|
</Form.Item>
|
||||||
|
<Form.Item label=' ' colon={false}>
|
||||||
|
<Button type='primary' htmlType='submit'>
|
||||||
|
Submit
|
||||||
|
</Button>
|
||||||
|
<Button className='ml-2' htmlType='reset' onClick={onClose}>
|
||||||
|
Cancel
|
||||||
|
</Button>
|
||||||
|
</Form.Item>
|
||||||
|
</Form>
|
||||||
|
</Modal>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
export const List = () => {
|
||||||
|
const sessionStore = useSessionStore(
|
||||||
|
useShallow((state) => {
|
||||||
|
return {
|
||||||
|
list: state.list,
|
||||||
|
getList: state.getList,
|
||||||
|
showEdit: state.showEdit,
|
||||||
|
setShowEdit: state.setShowEdit,
|
||||||
|
deleteData: state.deleteData,
|
||||||
|
};
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
const aiStore = useAiStore(
|
||||||
|
useShallow((state) => ({
|
||||||
|
setKey: state.setKey,
|
||||||
|
setOpen: state.setOpen,
|
||||||
|
})),
|
||||||
|
);
|
||||||
|
useEffect(() => {
|
||||||
|
sessionStore.getList();
|
||||||
|
}, []);
|
||||||
|
return (
|
||||||
|
<div className='flex w-full h-full bg-slate-200'>
|
||||||
|
<div className='h-full bg-white flex-shrink-0 flex justify-center'>
|
||||||
|
<Button className='m-4' onClick={() => sessionStore.setShowEdit(true)} icon={<PlusOutlined />}></Button>
|
||||||
|
</div>
|
||||||
|
<div className='p-4'>
|
||||||
|
<div className='flex flex-wrap gap-4'>
|
||||||
|
{sessionStore.list.map((item) => {
|
||||||
|
return (
|
||||||
|
<div key={item.id} className='card'>
|
||||||
|
<div className='card-title'>{item.titel || '-'}</div>
|
||||||
|
<div>{item.id}</div>
|
||||||
|
<div>{item.type}</div>
|
||||||
|
<div>{item.chatPromptId}</div>
|
||||||
|
<div className='card-footer mt-3'>
|
||||||
|
<Button.Group>
|
||||||
|
<Tooltip title='历史记录'>
|
||||||
|
<Button
|
||||||
|
onClick={() => {
|
||||||
|
// 继续回话,打开历史
|
||||||
|
aiStore.setKey(item.key);
|
||||||
|
aiStore.setOpen(true);
|
||||||
|
}}
|
||||||
|
icon={<HistoryOutlined />}></Button>
|
||||||
|
</Tooltip>
|
||||||
|
<Tooltip title='Delete'>
|
||||||
|
<Button onClick={() => sessionStore.deleteData(item.id)} icon={<DeleteOutlined />}></Button>
|
||||||
|
</Tooltip>
|
||||||
|
</Button.Group>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<FormModal />
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
68
src/pages/chat-manager/store/chat-prompt.ts
Normal file
68
src/pages/chat-manager/store/chat-prompt.ts
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
import { create } from 'zustand';
|
||||||
|
import { query } from '@/modules';
|
||||||
|
import { message } from 'antd';
|
||||||
|
type ChatPromptStore = {
|
||||||
|
showEdit: boolean;
|
||||||
|
setShowEdit: (showEdit: boolean) => void;
|
||||||
|
formData: any;
|
||||||
|
setFormData: (formData: any) => void;
|
||||||
|
loading: boolean;
|
||||||
|
setLoading: (loading: boolean) => void;
|
||||||
|
list: any[];
|
||||||
|
getList: () => Promise<void>;
|
||||||
|
updateData: (data: any) => Promise<void>;
|
||||||
|
deleteData: (id: string) => Promise<void>;
|
||||||
|
};
|
||||||
|
export const useChatPromptStore = create<ChatPromptStore>((set, get) => {
|
||||||
|
return {
|
||||||
|
showEdit: false,
|
||||||
|
setShowEdit: (showEdit) => set({ showEdit }),
|
||||||
|
formData: {},
|
||||||
|
setFormData: (formData) => set({ formData }),
|
||||||
|
loading: false,
|
||||||
|
setLoading: (loading) => set({ loading }),
|
||||||
|
list: [],
|
||||||
|
getList: async () => {
|
||||||
|
set({ loading: true });
|
||||||
|
const res = await query.post({
|
||||||
|
path: 'chat-prompt',
|
||||||
|
key: 'list',
|
||||||
|
});
|
||||||
|
set({ loading: false });
|
||||||
|
if (res.code === 200) {
|
||||||
|
set({ list: res.data });
|
||||||
|
} else {
|
||||||
|
message.error(res.message || 'Request failed');
|
||||||
|
}
|
||||||
|
},
|
||||||
|
updateData: async (data) => {
|
||||||
|
const { getList } = get();
|
||||||
|
const res = await query.post({
|
||||||
|
path: 'chat-prompt',
|
||||||
|
key: 'update',
|
||||||
|
data,
|
||||||
|
});
|
||||||
|
if (res.code === 200) {
|
||||||
|
message.success('Success');
|
||||||
|
set({ showEdit: false, formData: [] });
|
||||||
|
getList();
|
||||||
|
} else {
|
||||||
|
message.error(res.message || 'Request failed');
|
||||||
|
}
|
||||||
|
},
|
||||||
|
deleteData: async (id) => {
|
||||||
|
const { getList } = get();
|
||||||
|
const res = await query.post({
|
||||||
|
path: 'chat-prompt',
|
||||||
|
key: 'delete',
|
||||||
|
id,
|
||||||
|
});
|
||||||
|
if (res.code === 200) {
|
||||||
|
getList();
|
||||||
|
message.success('Success');
|
||||||
|
} else {
|
||||||
|
message.error(res.message || 'Request failed');
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
|
});
|
112
src/pages/code-editor/edit/Edit.tsx
Normal file
112
src/pages/code-editor/edit/Edit.tsx
Normal file
@ -0,0 +1,112 @@
|
|||||||
|
import { createEditorInstance, editor } from '@kevisual/codemirror/dist/editor.json';
|
||||||
|
import { useEffect, useRef, useLayoutEffect, useState, useCallback } from 'react';
|
||||||
|
import { useLocation, useNavigate } from 'react-router';
|
||||||
|
import { useCodeEditorStore, ParseData } from '../store';
|
||||||
|
import { Button, message, Tooltip } from 'antd';
|
||||||
|
import { LeftOutlined, MessageOutlined, SaveOutlined } from '@ant-design/icons';
|
||||||
|
import { useAiStore } from '@/pages/ai-chat';
|
||||||
|
import { useShallow } from 'zustand/react/shallow';
|
||||||
|
export const App = () => {
|
||||||
|
const ref = useRef<HTMLDivElement>(null);
|
||||||
|
const editorRef = useRef<typeof editor>(null);
|
||||||
|
const location = useLocation();
|
||||||
|
const store = useCodeEditorStore();
|
||||||
|
const [mounted, setMounted] = useState(false);
|
||||||
|
const navigator = useNavigate();
|
||||||
|
const aiStore = useAiStore(
|
||||||
|
useShallow((state) => {
|
||||||
|
return {
|
||||||
|
setOpen: state.setOpen,
|
||||||
|
setKey: state.setKey,
|
||||||
|
};
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
useEffect(() => {
|
||||||
|
initEditor();
|
||||||
|
const state = location.state as ParseData;
|
||||||
|
if (state && state.data) {
|
||||||
|
store.init(state);
|
||||||
|
}
|
||||||
|
setMounted(true);
|
||||||
|
return () => {
|
||||||
|
setMounted(false);
|
||||||
|
};
|
||||||
|
}, []);
|
||||||
|
useLayoutEffect(() => {
|
||||||
|
if (!mounted) return;
|
||||||
|
if (editorRef.current) {
|
||||||
|
setNewValue(store.code);
|
||||||
|
} else {
|
||||||
|
setNewValue('');
|
||||||
|
}
|
||||||
|
}, [store.code, mounted]);
|
||||||
|
const initEditor = () => {
|
||||||
|
const _editor = createEditorInstance(ref.current!);
|
||||||
|
editorRef.current = _editor;
|
||||||
|
};
|
||||||
|
const setNewValue = (value: string) => {
|
||||||
|
const editor = editorRef.current;
|
||||||
|
if (editor) {
|
||||||
|
editor.dispatch({
|
||||||
|
changes: [{ from: 0, to: editor.state.doc.length, insert: value }],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
const getValue = () => {
|
||||||
|
const editor = editorRef.current;
|
||||||
|
if (editor) {
|
||||||
|
return editor.state.doc.toString();
|
||||||
|
}
|
||||||
|
return '';
|
||||||
|
};
|
||||||
|
const onSave = useCallback(async () => {
|
||||||
|
const value = getValue();
|
||||||
|
// store.onUpdate(value);
|
||||||
|
if (store.dataType === 'object') {
|
||||||
|
try {
|
||||||
|
JSON.parse(value);
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
message.error('JSON format error');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
store.onUpdate(value);
|
||||||
|
}, [store.dataType]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className='w-full h-full bg-gray-400 flex flex-col'>
|
||||||
|
<div className='p-4 flex-grow overflow-hidden'>
|
||||||
|
<div className='h-full w-full overflow-hidden'>
|
||||||
|
<Button.Group className='mb-2'>
|
||||||
|
<Tooltip title='Go Back' placement='bottom'>
|
||||||
|
<Button
|
||||||
|
className=''
|
||||||
|
icon={<LeftOutlined />}
|
||||||
|
onClick={() => {
|
||||||
|
navigator(-1);
|
||||||
|
}}></Button>
|
||||||
|
</Tooltip>
|
||||||
|
<Tooltip title='Ai Chat'>
|
||||||
|
<Button
|
||||||
|
className=''
|
||||||
|
onClick={() => {
|
||||||
|
aiStore.setOpen(true);
|
||||||
|
aiStore.setKey(location.pathname);
|
||||||
|
}}
|
||||||
|
icon={<MessageOutlined />}></Button>
|
||||||
|
</Tooltip>
|
||||||
|
<Tooltip title='Save' placement='bottom'>
|
||||||
|
<Button className='' onClick={onSave} icon={<SaveOutlined />}></Button>
|
||||||
|
</Tooltip>
|
||||||
|
</Button.Group>
|
||||||
|
<div className='w-full p-4 rounded-md bg-white overflow-hidden relative' style={{ height: 'calc(100% - 130px)' }}>
|
||||||
|
<div className='w-full h-full scrollbar'>
|
||||||
|
<div className='w-full h-full' ref={ref}></div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
@ -1,92 +1,14 @@
|
|||||||
import { createEditorInstance, editor } from '@kevisual/codemirror/dist/editor.json';
|
import { Navigate, Route, Routes } from 'react-router-dom';
|
||||||
import { useEffect, useRef, useLayoutEffect, useState, useCallback } from 'react';
|
import { App as EditApp } from './edit/Edit';
|
||||||
import { useLocation, useNavigate } from 'react-router';
|
import { Main } from './layouts';
|
||||||
import { useCodeEditorStore, ParseData } from './store';
|
|
||||||
import { useToCodeEditor } from './hooks/use-to-code-editor';
|
import { useToCodeEditor } from './hooks/use-to-code-editor';
|
||||||
export { useToCodeEditor };
|
export { useToCodeEditor };
|
||||||
import { Button, message, Tooltip } from 'antd';
|
|
||||||
import { LeftOutlined, SaveOutlined } from '@ant-design/icons';
|
|
||||||
export const App = () => {
|
export const App = () => {
|
||||||
const ref = useRef<HTMLDivElement>(null);
|
|
||||||
const editorRef = useRef<typeof editor>(null);
|
|
||||||
const location = useLocation();
|
|
||||||
const store = useCodeEditorStore();
|
|
||||||
const [mounted, setMounted] = useState(false);
|
|
||||||
const navigator = useNavigate();
|
|
||||||
useEffect(() => {
|
|
||||||
initEditor();
|
|
||||||
const state = location.state as ParseData;
|
|
||||||
if (state && state.data) {
|
|
||||||
store.init(state);
|
|
||||||
}
|
|
||||||
setMounted(true);
|
|
||||||
return () => {
|
|
||||||
setMounted(false);
|
|
||||||
};
|
|
||||||
}, []);
|
|
||||||
useLayoutEffect(() => {
|
|
||||||
if (!mounted) return;
|
|
||||||
if (editorRef.current) {
|
|
||||||
setNewValue(store.code);
|
|
||||||
} else {
|
|
||||||
setNewValue('');
|
|
||||||
}
|
|
||||||
}, [store.code, mounted]);
|
|
||||||
const initEditor = () => {
|
|
||||||
const _editor = createEditorInstance(ref.current!);
|
|
||||||
editorRef.current = _editor;
|
|
||||||
};
|
|
||||||
const setNewValue = (value: string) => {
|
|
||||||
const editor = editorRef.current;
|
|
||||||
if (editor) {
|
|
||||||
editor.dispatch({
|
|
||||||
changes: [{ from: 0, to: editor.state.doc.length, insert: value }],
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
const getValue = () => {
|
|
||||||
const editor = editorRef.current;
|
|
||||||
if (editor) {
|
|
||||||
return editor.state.doc.toString();
|
|
||||||
}
|
|
||||||
return '';
|
|
||||||
};
|
|
||||||
const onSave = useCallback(async () => {
|
|
||||||
const value = getValue();
|
|
||||||
// store.onUpdate(value);
|
|
||||||
if (store.dataType === 'object') {
|
|
||||||
try {
|
|
||||||
JSON.parse(value);
|
|
||||||
} catch (error) {
|
|
||||||
console.error(error);
|
|
||||||
message.error('JSON format error');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
store.onUpdate(value);
|
|
||||||
}, [store.dataType]);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className='w-full h-full bg-gray-400 flex flex-col'>
|
<Routes>
|
||||||
<div className='layout-menu'>Code Editor</div>
|
<Route element={<Main />}>
|
||||||
<div className='p-4 flex-grow'>
|
<Route path='/' element={<EditApp />}></Route>
|
||||||
<Button.Group className='mb-2'>
|
</Route>
|
||||||
<Tooltip title='Go Back' placement='bottom'>
|
</Routes>
|
||||||
<Button
|
|
||||||
className=''
|
|
||||||
icon={<LeftOutlined />}
|
|
||||||
onClick={() => {
|
|
||||||
navigator(-1);
|
|
||||||
}}></Button>
|
|
||||||
</Tooltip>
|
|
||||||
<Tooltip title='Save' placement='bottom'>
|
|
||||||
<Button className='' onClick={onSave} icon={<SaveOutlined />}></Button>
|
|
||||||
</Tooltip>
|
|
||||||
</Button.Group>
|
|
||||||
<div className='w-full p-4 rounded-md bg-white' style={{ height: 'calc(100% - 130px)' }}>
|
|
||||||
<div className='w-full h-full' ref={ref}></div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
7
src/pages/code-editor/layouts/index.tsx
Normal file
7
src/pages/code-editor/layouts/index.tsx
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
import { useLocation } from 'react-router';
|
||||||
|
import { LayoutMain } from '@/modules/layout';
|
||||||
|
|
||||||
|
export const Main = () => {
|
||||||
|
const location = useLocation();
|
||||||
|
return <LayoutMain title={<>Edit Source JSON Code</>}></LayoutMain>;
|
||||||
|
};
|
@ -109,7 +109,7 @@ export const ContainerList = () => {
|
|||||||
return {
|
return {
|
||||||
open: state.open,
|
open: state.open,
|
||||||
setOpen: state.setOpen,
|
setOpen: state.setOpen,
|
||||||
setData: state.setData,
|
setKey: state.setKey,
|
||||||
};
|
};
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
@ -224,8 +224,8 @@ export const ContainerList = () => {
|
|||||||
<Tooltip title='ai编程'>
|
<Tooltip title='ai编程'>
|
||||||
<Button
|
<Button
|
||||||
onClick={(e) => {
|
onClick={(e) => {
|
||||||
|
aiStore.setKey(location.pathname);
|
||||||
aiStore.setOpen(true);
|
aiStore.setOpen(true);
|
||||||
// aiStore.setData({ ...containerStore.formData });
|
|
||||||
}}
|
}}
|
||||||
icon={<MessageOutlined />}></Button>
|
icon={<MessageOutlined />}></Button>
|
||||||
</Tooltip>
|
</Tooltip>
|
||||||
|
@ -26,6 +26,10 @@ const serverPath = [
|
|||||||
path: 'agent',
|
path: 'agent',
|
||||||
links: ['edit/list'],
|
links: ['edit/list'],
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
path: 'chat',
|
||||||
|
links: ['history/list', 'session/list', 'chat-prompt/list'],
|
||||||
|
},
|
||||||
];
|
];
|
||||||
const ServerPath = () => {
|
const ServerPath = () => {
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
|
@ -7,9 +7,10 @@ import { getContainerData } from '@/modules/deck-to-flow/deck';
|
|||||||
import { usePanelStore } from '../store';
|
import { usePanelStore } from '../store';
|
||||||
import { useShallow } from 'zustand/react/shallow';
|
import { useShallow } from 'zustand/react/shallow';
|
||||||
import { TextArea } from '@/pages/container/components/TextArea';
|
import { TextArea } from '@/pages/container/components/TextArea';
|
||||||
import { CloseOutlined, SaveOutlined, SelectOutlined } from '@ant-design/icons';
|
import { CloseOutlined, MessageOutlined, SaveOutlined, SelectOutlined } from '@ant-design/icons';
|
||||||
import { useDeckPageStore } from './deck-store';
|
import { useDeckPageStore } from './deck-store';
|
||||||
import { FormModal } from './Model.tsx';
|
import { FormModal } from './Model.tsx';
|
||||||
|
import { useAiStore } from '@/pages/ai-chat/index.tsx';
|
||||||
export const useListener = (id?: string, opts?: any) => {
|
export const useListener = (id?: string, opts?: any) => {
|
||||||
const { refresh } = opts || {};
|
const { refresh } = opts || {};
|
||||||
const connected = useStore((state) => state.connected);
|
const connected = useStore((state) => state.connected);
|
||||||
@ -75,6 +76,14 @@ export const Deck = () => {
|
|||||||
const deckPageStore = useDeckPageStore();
|
const deckPageStore = useDeckPageStore();
|
||||||
const { code, setCode } = deckPageStore;
|
const { code, setCode } = deckPageStore;
|
||||||
const { selected, setSelected } = deckPageStore;
|
const { selected, setSelected } = deckPageStore;
|
||||||
|
const aiStore = useAiStore(
|
||||||
|
useShallow((state) => {
|
||||||
|
return {
|
||||||
|
setOpen: state.setOpen,
|
||||||
|
setKey: state.setKey,
|
||||||
|
};
|
||||||
|
}),
|
||||||
|
);
|
||||||
const panelStore = usePanelStore(
|
const panelStore = usePanelStore(
|
||||||
useShallow((state) => {
|
useShallow((state) => {
|
||||||
return {
|
return {
|
||||||
@ -281,7 +290,7 @@ export const Deck = () => {
|
|||||||
return (
|
return (
|
||||||
<div className='w-full h-full relative'>
|
<div className='w-full h-full relative'>
|
||||||
<div className='w-full h-full bg-gray-200 '>
|
<div className='w-full h-full bg-gray-200 '>
|
||||||
<div className='text-center mb-10 font-bold text-4xl mt-4 flex items-center justify-center group'>
|
<div className='text-center mb-10 font-bold text-4xl pt-4 flex items-center justify-center group'>
|
||||||
Deck
|
Deck
|
||||||
<Tooltip>
|
<Tooltip>
|
||||||
<Button
|
<Button
|
||||||
@ -313,6 +322,14 @@ export const Deck = () => {
|
|||||||
}}
|
}}
|
||||||
icon={<CloseOutlined />}></Button>
|
icon={<CloseOutlined />}></Button>
|
||||||
</Tooltip>
|
</Tooltip>
|
||||||
|
<Tooltip title='Ai Chat'>
|
||||||
|
<Button
|
||||||
|
onClick={() => {
|
||||||
|
aiStore.setOpen(true);
|
||||||
|
aiStore.setKey(location.pathname);
|
||||||
|
}}
|
||||||
|
icon={<MessageOutlined />}></Button>
|
||||||
|
</Tooltip>
|
||||||
<Tooltip title='Save'>
|
<Tooltip title='Save'>
|
||||||
<Button
|
<Button
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
|
@ -2,11 +2,13 @@ import { Panel, useReactFlow, useStore, useStoreApi } from '@xyflow/react';
|
|||||||
import clsx from 'clsx';
|
import clsx from 'clsx';
|
||||||
import { useEffect, useState } from 'react';
|
import { useEffect, useState } from 'react';
|
||||||
import { debounce } from 'lodash-es';
|
import { debounce } from 'lodash-es';
|
||||||
import { Button, Form, Input, message } from 'antd';
|
import { Button, Form, Input, message, Tooltip } from 'antd';
|
||||||
import { Select } from '@/pages/container/module/Select';
|
import { Select } from '@/pages/container/module/Select';
|
||||||
import { SaveOutlined } from '@ant-design/icons';
|
import { MessageOutlined, SaveOutlined } from '@ant-design/icons';
|
||||||
import { emitter } from '@abearxiong/container';
|
import { emitter } from '@abearxiong/container';
|
||||||
import { usePanelStore } from '../../store';
|
import { usePanelStore } from '../../store';
|
||||||
|
import { useShallow } from 'zustand/react/shallow';
|
||||||
|
import { useAiStore } from '@/pages/ai-chat';
|
||||||
export const NodeProperties = () => {
|
export const NodeProperties = () => {
|
||||||
const reactflow = useReactFlow();
|
const reactflow = useReactFlow();
|
||||||
const [open, setOpen] = useState(false);
|
const [open, setOpen] = useState(false);
|
||||||
@ -16,6 +18,14 @@ export const NodeProperties = () => {
|
|||||||
updateNodeData: state.updateNodeData,
|
updateNodeData: state.updateNodeData,
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
const aiStore = useAiStore(
|
||||||
|
useShallow((state) => {
|
||||||
|
return {
|
||||||
|
setOpen: state.setOpen,
|
||||||
|
setKey: state.setKey,
|
||||||
|
};
|
||||||
|
}),
|
||||||
|
);
|
||||||
const store = useStore((state) => {
|
const store = useStore((state) => {
|
||||||
const setNode = (node: any) => {
|
const setNode = (node: any) => {
|
||||||
const newNodes = state.nodes.map((item) => {
|
const newNodes = state.nodes.map((item) => {
|
||||||
@ -76,7 +86,17 @@ export const NodeProperties = () => {
|
|||||||
<div className='card-title'>
|
<div className='card-title'>
|
||||||
{nodeData?.data?.label}
|
{nodeData?.data?.label}
|
||||||
<Button.Group className='ml-2'>
|
<Button.Group className='ml-2'>
|
||||||
<Button onClick={onSave} icon={<SaveOutlined />}></Button>
|
<Tooltip title='Save'>
|
||||||
|
<Button onClick={onSave} icon={<SaveOutlined />}></Button>
|
||||||
|
</Tooltip>
|
||||||
|
<Tooltip title='Ai Chat'>
|
||||||
|
<Button
|
||||||
|
onClick={() => {
|
||||||
|
aiStore.setKey(location.pathname);
|
||||||
|
aiStore.setOpen(true);
|
||||||
|
}}
|
||||||
|
icon={<MessageOutlined />}></Button>
|
||||||
|
</Tooltip>
|
||||||
</Button.Group>
|
</Button.Group>
|
||||||
</div>
|
</div>
|
||||||
<div className='p-4'>
|
<div className='p-4'>
|
||||||
|
@ -1,14 +1,5 @@
|
|||||||
import { Outlet } from 'react-router';
|
import { LayoutMain } from '@/modules/layout';
|
||||||
|
|
||||||
export const Main = () => {
|
export const Main = () => {
|
||||||
return (
|
return <LayoutMain title={<>Deck And Flow</>} />;
|
||||||
<div className='flex w-full h-full flex-col bg-gray-200'>
|
|
||||||
<div className='layout-menu'>Deck And Flow</div>
|
|
||||||
<div className='flex-grow w-full'>
|
|
||||||
<div className='w-full h-full overflow-hidden'>
|
|
||||||
<Outlet />
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
|
@ -110,10 +110,8 @@ export const List = () => {
|
|||||||
return {
|
return {
|
||||||
open: state.open,
|
open: state.open,
|
||||||
setOpen: state.setOpen,
|
setOpen: state.setOpen,
|
||||||
setData: state.setData,
|
|
||||||
key: state.key,
|
key: state.key,
|
||||||
setKey: state.setKey,
|
setKey: state.setKey,
|
||||||
sendMsg: state.sendMsg,
|
|
||||||
};
|
};
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
@ -244,10 +242,7 @@ export const List = () => {
|
|||||||
<Button
|
<Button
|
||||||
icon={<CaretRightOutlined />}
|
icon={<CaretRightOutlined />}
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
// navicate(`/prompt/${item.id}`);
|
aiStore.setKey(location.pathname);
|
||||||
// promptStore.setFormData(item);
|
|
||||||
// promptStore.runAi();
|
|
||||||
aiStore.setData(item);
|
|
||||||
aiStore.setOpen(true);
|
aiStore.setOpen(true);
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
@ -257,7 +252,7 @@ export const List = () => {
|
|||||||
</Fragment>
|
</Fragment>
|
||||||
);
|
);
|
||||||
})}
|
})}
|
||||||
<CardBlank className='w-[600px]' />
|
<CardBlank className='w-[600px]' />
|
||||||
{promptStore.list.length == 0 && (
|
{promptStore.list.length == 0 && (
|
||||||
<div className='text-center' key={'no-data'}>
|
<div className='text-center' key={'no-data'}>
|
||||||
No Data
|
No Data
|
||||||
|
@ -16,3 +16,4 @@ export const App = () => {
|
|||||||
</Routes>
|
</Routes>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
export * from './module/Select'
|
39
src/pages/prompt/module/Select.tsx
Normal file
39
src/pages/prompt/module/Select.tsx
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
import { query } from '@/modules';
|
||||||
|
import { Select as AntSelect, message, SelectProps } from 'antd';
|
||||||
|
import { useEffect, useState } from 'react';
|
||||||
|
|
||||||
|
export const Select = (props: SelectProps) => {
|
||||||
|
const [options, setOptions] = useState<{ value: string; id: string }[]>([]);
|
||||||
|
useEffect(() => {
|
||||||
|
fetch();
|
||||||
|
}, []);
|
||||||
|
const fetch = async () => {
|
||||||
|
const res = await query.post({
|
||||||
|
path: 'prompt',
|
||||||
|
key: 'list',
|
||||||
|
});
|
||||||
|
if (res.code !== 200) {
|
||||||
|
message.error(res.message || '获取agent列表失败');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const data = res.data || [];
|
||||||
|
setOptions(
|
||||||
|
data.map((item: any) => {
|
||||||
|
return {
|
||||||
|
label: item.key,
|
||||||
|
value: item.id,
|
||||||
|
};
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
};
|
||||||
|
return (
|
||||||
|
<AntSelect
|
||||||
|
{...props}
|
||||||
|
options={options}
|
||||||
|
// onChange={(e) => {
|
||||||
|
// const labelValue = options.find((item) => item.value === e);
|
||||||
|
// props.onChange?.(e, options);
|
||||||
|
// }}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
};
|
Loading…
x
Reference in New Issue
Block a user