|
@@ -9,6 +9,8 @@ import {
|
|
|
Attachments,
|
|
|
AttachmentsProps,
|
|
|
} from "@ant-design/x";
|
|
|
+import { Conversation } from "@ant-design/x/lib/conversations";
|
|
|
+import { useChat } from "@/hooks/useChat";
|
|
|
|
|
|
import {
|
|
|
Card,
|
|
@@ -19,15 +21,10 @@ import {
|
|
|
Space,
|
|
|
Spin,
|
|
|
Typography,
|
|
|
+ Modal,
|
|
|
+ Input,
|
|
|
} from "antd";
|
|
|
-import {
|
|
|
- CSSProperties,
|
|
|
- ReactNode,
|
|
|
- useEffect,
|
|
|
- useMemo,
|
|
|
- useRef,
|
|
|
- useState,
|
|
|
-} from "react";
|
|
|
+import { useEffect, useRef, useState } from "react";
|
|
|
import {
|
|
|
BulbOutlined,
|
|
|
SmileOutlined,
|
|
@@ -39,23 +36,13 @@ import {
|
|
|
LinkOutlined,
|
|
|
CopyOutlined,
|
|
|
RedoOutlined,
|
|
|
+ ReadOutlined,
|
|
|
} from "@ant-design/icons";
|
|
|
import type { GetProp, GetRef } from "antd";
|
|
|
import type { ConversationsProps } from "@ant-design/x";
|
|
|
import type { AgentItem } from "./data";
|
|
|
import MarkdownViewer from "@/components/ai/MarkdownViewer";
|
|
|
-import { useModel } from "umi";
|
|
|
-import { useSessionStorageState } from "ahooks";
|
|
|
-
|
|
|
-// 消息类型
|
|
|
-type MessageItem = {
|
|
|
- id: string;
|
|
|
- content: string;
|
|
|
- role: "user" | "assistant" | "system";
|
|
|
- status: "loading" | "done" | "error" | "stop";
|
|
|
- loading?: boolean;
|
|
|
- footer?: ReactNode;
|
|
|
-};
|
|
|
+import { ChangeSessionName, DeleteSession } from "@/api/ai";
|
|
|
|
|
|
type AssistantProps = {
|
|
|
agent?: AgentItem;
|
|
@@ -69,7 +56,7 @@ const roles: GetProp<typeof Bubble.List, "roles"> = {
|
|
|
icon: <i className="iconfont icon-AI1" />,
|
|
|
style: { background: "#fde3cf" },
|
|
|
},
|
|
|
- typing: { step: 5, interval: 20 },
|
|
|
+ // typing: { step: 5, interval: 20 },
|
|
|
loadingRender: () => (
|
|
|
<Space>
|
|
|
<Spin size="small" />
|
|
@@ -77,12 +64,12 @@ const roles: GetProp<typeof Bubble.List, "roles"> = {
|
|
|
</Space>
|
|
|
),
|
|
|
messageRender: (content) => {
|
|
|
- return content ? (
|
|
|
+ return typeof content === "string" ? (
|
|
|
<Typography>
|
|
|
<MarkdownViewer content={content} />
|
|
|
</Typography>
|
|
|
) : (
|
|
|
- <></>
|
|
|
+ content
|
|
|
);
|
|
|
},
|
|
|
header: "易码工坊AI助手",
|
|
@@ -93,40 +80,146 @@ const roles: GetProp<typeof Bubble.List, "roles"> = {
|
|
|
},
|
|
|
};
|
|
|
|
|
|
-const defaultConversation = {
|
|
|
- // 会话id
|
|
|
- key: "1",
|
|
|
- label: "新的对话",
|
|
|
-};
|
|
|
-
|
|
|
export default (props: AssistantProps) => {
|
|
|
- const { agent, loading, cancel } = useModel("aiModel");
|
|
|
const [senderVal, setSenderVal] = useState("");
|
|
|
- const [messages, setMessages] = useState<Array<MessageItem>>([]);
|
|
|
- const [conversationList, setConversationList] = useState<
|
|
|
- ConversationsProps["items"]
|
|
|
- >([{ ...defaultConversation }]);
|
|
|
- const [activeConversation, setActiveConversation] = useState("1");
|
|
|
+ const {
|
|
|
+ messages,
|
|
|
+ setMessages,
|
|
|
+ activeConversation,
|
|
|
+ changeConversation,
|
|
|
+ conversationList,
|
|
|
+ onRequest,
|
|
|
+ cancel,
|
|
|
+ loading,
|
|
|
+ loadingSession,
|
|
|
+ addConversation,
|
|
|
+ setConversationList,
|
|
|
+ } = useChat({
|
|
|
+ app_name: props.agent?.key || "",
|
|
|
+ onSuccess: (msg) => {
|
|
|
+ setMessages((messages) => {
|
|
|
+ const arr = [...messages];
|
|
|
+ const query = arr[messages.length - 2].content as string;
|
|
|
+ arr[messages.length - 1].status = "done";
|
|
|
+ arr[messages.length - 1].footer = (
|
|
|
+ <BubbleFooter
|
|
|
+ content={arr[messages.length - 1].content as string}
|
|
|
+ query={query}
|
|
|
+ />
|
|
|
+ );
|
|
|
+ return arr;
|
|
|
+ });
|
|
|
+ },
|
|
|
+ onUpdate: (msg) => {
|
|
|
+ setMessages((messages) => {
|
|
|
+ const arr = [...messages];
|
|
|
+ arr[messages.length - 1].content += msg.answer;
|
|
|
+ arr[messages.length - 1].id = msg.message_id;
|
|
|
+ arr[messages.length - 1].loading = false;
|
|
|
+ arr[messages.length - 1].status = "error";
|
|
|
+ return arr;
|
|
|
+ });
|
|
|
+ },
|
|
|
+ onError: (error) => {
|
|
|
+ message.error(error.message);
|
|
|
+ setMessages((messages) => {
|
|
|
+ const arr = [...messages];
|
|
|
+ arr[messages.length - 1].content = (
|
|
|
+ <Typography.Text type="danger">{error.message}</Typography.Text>
|
|
|
+ );
|
|
|
+ arr[messages.length - 1].status = "error";
|
|
|
+ arr[messages.length - 1].loading = false;
|
|
|
+ return arr;
|
|
|
+ });
|
|
|
+ },
|
|
|
+ });
|
|
|
|
|
|
- const [currentAgent, setCurrentAgent] = useSessionStorageState("agent-map");
|
|
|
- const menuConfig: ConversationsProps["menu"] = (conversation) => ({
|
|
|
- items: [
|
|
|
- {
|
|
|
- label: "修改对话名称",
|
|
|
- key: "edit",
|
|
|
- icon: <EditOutlined />,
|
|
|
+ const handleChangeConversationName = (conversation: Conversation) => {
|
|
|
+ let new_name: string;
|
|
|
+ Modal.info({
|
|
|
+ title: "修改对话名称",
|
|
|
+ okText: "提交",
|
|
|
+ closable: true,
|
|
|
+ content: (
|
|
|
+ <div>
|
|
|
+ <Input
|
|
|
+ type="text"
|
|
|
+ defaultValue={conversation.label + ""}
|
|
|
+ onChange={(e) => {
|
|
|
+ new_name = e.target.value;
|
|
|
+ }}
|
|
|
+ />
|
|
|
+ </div>
|
|
|
+ ),
|
|
|
+ onOk: () => {
|
|
|
+ return ChangeSessionName({
|
|
|
+ app_name: props.agent?.key || "",
|
|
|
+ session_id: conversation.key,
|
|
|
+ new_name,
|
|
|
+ }).then(() => {
|
|
|
+ message.success("修改成功");
|
|
|
+ setConversationList((list) =>
|
|
|
+ list?.map((item) =>
|
|
|
+ item.key === conversation.key
|
|
|
+ ? { ...item, label: new_name }
|
|
|
+ : item
|
|
|
+ )
|
|
|
+ );
|
|
|
+ });
|
|
|
},
|
|
|
- {
|
|
|
- label: "删除对话",
|
|
|
- key: "del",
|
|
|
- icon: <DeleteOutlined />,
|
|
|
- danger: true,
|
|
|
+ });
|
|
|
+ };
|
|
|
+
|
|
|
+ const handleDeleteConversation = (conversation: Conversation) => {
|
|
|
+ Modal.confirm({
|
|
|
+ title: "删除对话",
|
|
|
+ content: "是否删除对话?",
|
|
|
+ okText: "删除",
|
|
|
+ cancelText: "取消",
|
|
|
+ onOk: () => {
|
|
|
+ return DeleteSession({
|
|
|
+ app_name: props.agent?.key || "",
|
|
|
+ session_id: conversation.key,
|
|
|
+ }).then(() => {
|
|
|
+ message.success("删除成功");
|
|
|
+ setConversationList((list) =>
|
|
|
+ list?.filter((item) => item.key !== conversation.key)
|
|
|
+ );
|
|
|
+
|
|
|
+ addConversation();
|
|
|
+ });
|
|
|
},
|
|
|
- ],
|
|
|
- onClick: (menuInfo) => {
|
|
|
- message.info(`Click ${conversation.key} - ${menuInfo.key}`);
|
|
|
- },
|
|
|
- });
|
|
|
+ });
|
|
|
+ };
|
|
|
+
|
|
|
+ const menuConfig: ConversationsProps["menu"] = (conversation) => {
|
|
|
+ if (conversation.key === "1") return undefined;
|
|
|
+ return {
|
|
|
+ items: [
|
|
|
+ {
|
|
|
+ label: "修改对话名称",
|
|
|
+ key: "edit",
|
|
|
+ icon: <EditOutlined />,
|
|
|
+ },
|
|
|
+ {
|
|
|
+ label: "删除对话",
|
|
|
+ key: "del",
|
|
|
+ icon: <DeleteOutlined />,
|
|
|
+ danger: true,
|
|
|
+ },
|
|
|
+ ],
|
|
|
+ onClick: (menuInfo) => {
|
|
|
+ // 修改对话名称
|
|
|
+ if (menuInfo.key === "edit") {
|
|
|
+ handleChangeConversationName(conversation);
|
|
|
+ }
|
|
|
+ // 删除对话
|
|
|
+ if (menuInfo.key === "del") {
|
|
|
+ handleDeleteConversation(conversation);
|
|
|
+ }
|
|
|
+ },
|
|
|
+ };
|
|
|
+ };
|
|
|
|
|
|
const [openAttachment, setOpenAttachment] = useState(false);
|
|
|
const attachmentsRef = useRef<GetRef<typeof Attachments>>(null);
|
|
@@ -135,22 +228,18 @@ export default (props: AssistantProps) => {
|
|
|
GetProp<AttachmentsProps, "items">
|
|
|
>([]);
|
|
|
const contentRef = useRef<HTMLDivElement>(null);
|
|
|
+ const [contentHeight, setContentHeight] = useState(0);
|
|
|
|
|
|
- const contentStyle = useMemo((): CSSProperties => {
|
|
|
- if (!contentRef.current) return {};
|
|
|
- return {
|
|
|
- maxHeight: contentRef.current?.clientHeight,
|
|
|
- overflowY: "auto",
|
|
|
- };
|
|
|
- }, [contentRef.current]);
|
|
|
-
|
|
|
+ const setHeight = () => {
|
|
|
+ setContentHeight(contentRef.current?.clientHeight || 0);
|
|
|
+ };
|
|
|
useEffect(() => {
|
|
|
- // 切换类型时清除回话 加载回话列表
|
|
|
- if (agent) {
|
|
|
- setMessages([]);
|
|
|
- setActiveConversation("1");
|
|
|
- }
|
|
|
- }, [props.agent]);
|
|
|
+ setHeight();
|
|
|
+ window.addEventListener("resize", setHeight);
|
|
|
+ return () => {
|
|
|
+ window.removeEventListener("resize", setHeight);
|
|
|
+ };
|
|
|
+ }, []);
|
|
|
|
|
|
// 附件组件
|
|
|
const senderHeader = (
|
|
@@ -218,68 +307,6 @@ export default (props: AssistantProps) => {
|
|
|
);
|
|
|
};
|
|
|
|
|
|
- // 发起请求
|
|
|
- const onRequest = (message: string) => {
|
|
|
- agent.request(
|
|
|
- {
|
|
|
- app_name: "app1",
|
|
|
- chat_name: activeConversation === "1" ? "新会话" : undefined,
|
|
|
- chat_query: message,
|
|
|
- conversation_id:
|
|
|
- activeConversation === "1" ? undefined : activeConversation,
|
|
|
- },
|
|
|
- {
|
|
|
- onSuccess: (msg) => {
|
|
|
- console.log("success", msg);
|
|
|
- setMessages((messages) => {
|
|
|
- const arr = [...messages];
|
|
|
- const query = arr[messages.length - 2].content;
|
|
|
- arr[messages.length - 1].status = "done";
|
|
|
- arr[messages.length - 1].footer = (
|
|
|
- <BubbleFooter
|
|
|
- content={arr[messages.length - 1].content}
|
|
|
- query={query}
|
|
|
- />
|
|
|
- );
|
|
|
- return arr;
|
|
|
- });
|
|
|
- },
|
|
|
- onError: (error) => {
|
|
|
- console.log("err:", error);
|
|
|
-
|
|
|
- },
|
|
|
- onUpdate: (msg) => {
|
|
|
- console.log("update", msg);
|
|
|
- setMessages((messages) => {
|
|
|
- const arr = [...messages];
|
|
|
- arr[messages.length - 1].content += msg.answer;
|
|
|
- arr[messages.length - 1].id = msg.message_id;
|
|
|
- arr[messages.length - 1].loading = false;
|
|
|
- // 第一次提交后保存会话记录
|
|
|
- if (
|
|
|
- !conversationList?.find(
|
|
|
- (item) => item.key === msg.conversation_id
|
|
|
- )
|
|
|
- ) {
|
|
|
- setConversationList((list) => {
|
|
|
- return list?.map((item) => {
|
|
|
- return {
|
|
|
- ...item,
|
|
|
- key: msg.conversation_id,
|
|
|
- label: message,
|
|
|
- };
|
|
|
- });
|
|
|
- });
|
|
|
- setActiveConversation(msg.conversation_id);
|
|
|
- }
|
|
|
-
|
|
|
- return arr;
|
|
|
- });
|
|
|
- },
|
|
|
- }
|
|
|
- );
|
|
|
- };
|
|
|
-
|
|
|
// 提交消息
|
|
|
const submitMessage = (msg: string) => {
|
|
|
setSenderVal("");
|
|
@@ -303,7 +330,6 @@ export default (props: AssistantProps) => {
|
|
|
|
|
|
// 点击提示词
|
|
|
const handlePromptItem = (item: any) => {
|
|
|
- console.log(item);
|
|
|
const msg = item.data.description || item.data.label;
|
|
|
const index = messages.length;
|
|
|
setMessages([
|
|
@@ -329,29 +355,17 @@ export default (props: AssistantProps) => {
|
|
|
arr[messages.length - 1].loading = false;
|
|
|
arr[messages.length - 1].footer = (
|
|
|
<div>
|
|
|
- <div className="text-12px text-text-secondary">已停止思考</div>
|
|
|
- <BubbleFooter content={arr[messages.length - 1].content} query={arr[messages.length - 2].content}/>
|
|
|
+ <div className="text-12px text-text-secondary pl-12px">(已停止思考)</div>
|
|
|
+ <BubbleFooter
|
|
|
+ content={arr[messages.length - 1].content as string}
|
|
|
+ query={arr[messages.length - 2].content as string}
|
|
|
+ />
|
|
|
</div>
|
|
|
);
|
|
|
return arr;
|
|
|
});
|
|
|
};
|
|
|
|
|
|
- // 新增会话
|
|
|
- const handleAddConversation = () => {
|
|
|
- setMessages([]);
|
|
|
- // 还没产生对话时 直接清除当前对话
|
|
|
- if (!conversationList?.find((item) => item.key === "1")) {
|
|
|
- setConversationList([
|
|
|
- {
|
|
|
- ...defaultConversation,
|
|
|
- },
|
|
|
- ...(conversationList || []),
|
|
|
- ]);
|
|
|
- setActiveConversation("1");
|
|
|
- }
|
|
|
- };
|
|
|
-
|
|
|
return (
|
|
|
<>
|
|
|
<Card
|
|
@@ -373,29 +387,34 @@ export default (props: AssistantProps) => {
|
|
|
>
|
|
|
<XProvider direction="ltr">
|
|
|
<Flex style={{ height: "100%" }} gap={12}>
|
|
|
- <div className="w-200px">
|
|
|
- <div className="w-full px-12px">
|
|
|
- <Button
|
|
|
- type="primary"
|
|
|
- className="w-full"
|
|
|
- icon={<PlusOutlined />}
|
|
|
- onClick={handleAddConversation}
|
|
|
- >
|
|
|
- 新对话
|
|
|
- </Button>
|
|
|
+ <Spin spinning={loadingSession}>
|
|
|
+ <div className="w-200px">
|
|
|
+ <div className="w-full px-12px">
|
|
|
+ <Button
|
|
|
+ type="primary"
|
|
|
+ className="w-full"
|
|
|
+ icon={<PlusOutlined />}
|
|
|
+ onClick={addConversation}
|
|
|
+ >
|
|
|
+ 新对话
|
|
|
+ </Button>
|
|
|
+ </div>
|
|
|
+ <Conversations
|
|
|
+ style={{ width: 200 }}
|
|
|
+ activeKey={activeConversation}
|
|
|
+ onActiveChange={changeConversation}
|
|
|
+ menu={menuConfig}
|
|
|
+ items={conversationList}
|
|
|
+ />
|
|
|
</div>
|
|
|
- <Conversations
|
|
|
- style={{ width: 200 }}
|
|
|
- defaultActiveKey="1"
|
|
|
- activeKey={activeConversation}
|
|
|
- onActiveChange={setActiveConversation}
|
|
|
- menu={menuConfig}
|
|
|
- items={conversationList}
|
|
|
- />
|
|
|
- </div>
|
|
|
+ </Spin>
|
|
|
<Divider type="vertical" style={{ height: "100%" }} />
|
|
|
<Flex vertical style={{ flex: 1 }} gap={8}>
|
|
|
- <div className="flex-1" ref={contentRef} style={contentStyle}>
|
|
|
+ <div
|
|
|
+ className="flex-1"
|
|
|
+ ref={contentRef}
|
|
|
+ style={{ height: contentHeight }}
|
|
|
+ >
|
|
|
{!messages.length ? (
|
|
|
<>
|
|
|
<div className="mt-20 mb-10">
|
|
@@ -421,7 +440,12 @@ export default (props: AssistantProps) => {
|
|
|
/>
|
|
|
</>
|
|
|
) : (
|
|
|
- <Bubble.List autoScroll roles={roles} items={messages} />
|
|
|
+ <Bubble.List
|
|
|
+ style={{ maxHeight: contentHeight }}
|
|
|
+ autoScroll
|
|
|
+ roles={roles}
|
|
|
+ items={messages}
|
|
|
+ />
|
|
|
)}
|
|
|
</div>
|
|
|
<Prompts
|
|
@@ -438,7 +462,7 @@ export default (props: AssistantProps) => {
|
|
|
},
|
|
|
{
|
|
|
key: "3",
|
|
|
- icon: <SmileOutlined style={{ color: "#52C41A" }} />,
|
|
|
+ icon: <ReadOutlined style={{ color: "#52C41A" }} />,
|
|
|
label: "问题解答",
|
|
|
},
|
|
|
]}
|