Assistant.tsx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. import {
  2. Bubble,
  3. Conversations,
  4. Prompts,
  5. Sender,
  6. Suggestion,
  7. XProvider,
  8. useXAgent,
  9. useXChat,
  10. Welcome,
  11. Attachments,
  12. AttachmentsProps,
  13. } from "@ant-design/x";
  14. import { Card, Divider, Flex, App, Button } from "antd";
  15. import React from "react";
  16. import {
  17. BulbOutlined,
  18. SmileOutlined,
  19. UserOutlined,
  20. EditOutlined,
  21. DeleteOutlined,
  22. MessageOutlined,
  23. PlusOutlined,
  24. CloudUploadOutlined,
  25. LinkOutlined,
  26. } from "@ant-design/icons";
  27. import type { GetProp, GetRef } from "antd";
  28. import type { ConversationsProps } from "@ant-design/x";
  29. import type { AgentItem } from "./data";
  30. const roles: GetProp<typeof Bubble.List, "roles"> = {
  31. assient: {
  32. placement: "start",
  33. avatar: {
  34. icon: <i className="iconfont icon-AI1" />,
  35. style: { background: "#fde3cf" },
  36. },
  37. },
  38. user: {
  39. placement: "end",
  40. avatar: { icon: <UserOutlined />, style: { background: "#87d068" } },
  41. },
  42. };
  43. type AssistantProps = {
  44. agent?: AgentItem;
  45. };
  46. export default (props: AssistantProps) => {
  47. const [value, setValue] = React.useState("");
  48. const { message } = App.useApp();
  49. const [agent] = useXAgent<{ role: string; content: string }>({
  50. baseURL: "http://localhost:3000/ai/chat",
  51. });
  52. const { onRequest, messages } = useXChat({ agent });
  53. const menuConfig: ConversationsProps["menu"] = (conversation) => ({
  54. items: [
  55. {
  56. label: "修改对话名称",
  57. key: "operation1",
  58. icon: <EditOutlined />,
  59. },
  60. {
  61. label: "删除对话",
  62. key: "operation3",
  63. icon: <DeleteOutlined />,
  64. danger: true,
  65. },
  66. ],
  67. onClick: (menuInfo) => {
  68. message.info(`Click ${conversation.key} - ${menuInfo.key}`);
  69. },
  70. });
  71. const [openAttachment, setOpenAttachment] = React.useState(false);
  72. const attachmentsRef = React.useRef<GetRef<typeof Attachments>>(null);
  73. const senderRef = React.useRef<GetRef<typeof Sender>>(null);
  74. const [attachmentItems, setAttachmentItems] = React.useState<GetProp<AttachmentsProps, 'items'>>([]);
  75. const senderHeader = (
  76. <Sender.Header
  77. title="附件"
  78. styles={{
  79. content: {
  80. padding: 0,
  81. },
  82. }}
  83. open={openAttachment}
  84. onOpenChange={setOpenAttachment}
  85. forceRender
  86. >
  87. <Attachments
  88. ref={attachmentsRef}
  89. // Mock not real upload file
  90. beforeUpload={() => false}
  91. items={attachmentItems}
  92. onChange={({ fileList }) => setAttachmentItems(fileList)}
  93. placeholder={(type) =>
  94. type === "drop"
  95. ? {
  96. title: "拖拽文件到这里",
  97. }
  98. : {
  99. icon: <CloudUploadOutlined />,
  100. title: "文件列表",
  101. description: "点击或者拖拽文件到这里上传",
  102. }
  103. }
  104. getDropContainer={() => senderRef.current?.nativeElement}
  105. />
  106. </Sender.Header>
  107. );
  108. const submitMessage = (message: string) => {
  109. // onRequest({ role: "user", content: message });
  110. };
  111. const handlePromptItem = (item: any) => {
  112. // onRequest({ role: "user", content: item.data.description });
  113. };
  114. return (
  115. <>
  116. <Card
  117. className="w-full h-full"
  118. styles={{
  119. body: {
  120. height: "calc(100% - 48px)",
  121. },
  122. }}
  123. title={
  124. <span className="flex items-center">
  125. <img
  126. className="w-20px h-20px rounded-8px"
  127. src={props.agent?.icon}
  128. />
  129. <span className="ml-4px">{props.agent?.name}</span>
  130. </span>
  131. }
  132. >
  133. <XProvider direction="ltr">
  134. <Flex style={{ height: "100%" }} gap={12}>
  135. <div className="w-200px">
  136. <div className="w-full px-12px">
  137. <Button
  138. type="primary"
  139. className="w-full"
  140. icon={<PlusOutlined />}
  141. >
  142. 新对话
  143. </Button>
  144. </div>
  145. <Conversations
  146. style={{ width: 200 }}
  147. defaultActiveKey="1"
  148. menu={menuConfig}
  149. items={[
  150. {
  151. key: "1",
  152. label: "新的对话",
  153. icon: <MessageOutlined />,
  154. },
  155. ]}
  156. />
  157. </div>
  158. <Divider type="vertical" style={{ height: "100%" }} />
  159. <Flex vertical style={{ flex: 1 }} gap={8}>
  160. <div className="flex-1">
  161. {!messages.length ? (
  162. <>
  163. <div className="mt-20 mb-10">
  164. <Welcome
  165. icon={
  166. <img
  167. src={props.agent?.icon}
  168. className="rounded-8px"
  169. />
  170. }
  171. title={`你好!我是易码工坊${props.agent?.name || "AI"}助手`}
  172. description={props.agent?.description}
  173. />
  174. </div>
  175. <Prompts
  176. title={
  177. props.agent?.promptsItems ? "✨ 你可以这样问我:" : ""
  178. }
  179. items={props.agent?.promptsItems || []}
  180. wrap
  181. onItemClick={handlePromptItem}
  182. />
  183. </>
  184. ) : (
  185. <Bubble.List
  186. autoScroll
  187. roles={roles}
  188. items={
  189. []
  190. // messages.map(({ id, message, status }) => ({
  191. // key: id,
  192. // loading: status === "loading",
  193. // role: status === "user" ? "local" : "ai",
  194. // content: message,
  195. // }))
  196. }
  197. />
  198. )}
  199. </div>
  200. <Prompts
  201. items={[
  202. {
  203. key: "1",
  204. icon: <BulbOutlined style={{ color: "#FFD700" }} />,
  205. label: "一句话",
  206. },
  207. {
  208. key: "2",
  209. icon: <SmileOutlined style={{ color: "#52C41A" }} />,
  210. label: "自动生成",
  211. },
  212. ]}
  213. onItemClick={handlePromptItem}
  214. />
  215. <Suggestion
  216. items={[{ label: "写一个应用介绍", value: "report" }]}
  217. onSelect={submitMessage}
  218. >
  219. {({ onTrigger, onKeyDown }) => {
  220. return (
  221. <Sender
  222. ref={senderRef}
  223. header={senderHeader}
  224. prefix={
  225. <Button
  226. type="text"
  227. icon={<LinkOutlined />}
  228. onClick={() => {
  229. setOpenAttachment(!openAttachment);
  230. }}
  231. />
  232. }
  233. value={value}
  234. onPasteFile={(file) => {
  235. attachmentsRef.current?.upload(file);
  236. setOpenAttachment(true);
  237. }}
  238. onChange={(nextVal) => {
  239. if (nextVal === "/") {
  240. onTrigger();
  241. } else if (!nextVal) {
  242. onTrigger(false);
  243. }
  244. setValue(nextVal);
  245. }}
  246. onKeyDown={onKeyDown}
  247. placeholder="输入/获取快捷提示"
  248. onSubmit={submitMessage}
  249. />
  250. );
  251. }}
  252. </Suggestion>
  253. </Flex>
  254. </Flex>
  255. </XProvider>
  256. </Card>
  257. </>
  258. );
  259. };