-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathindex.tsx
More file actions
156 lines (147 loc) · 5.93 KB
/
index.tsx
File metadata and controls
156 lines (147 loc) · 5.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import React, { useMemo, useState } from 'react';
import { Tabs } from 'antd';
import { Chat } from 'dt-react-component';
import { Conversation, Message, MessageStatus, Prompt } from 'dt-react-component/chat/entity';
import { produce } from 'immer';
import { mockSSE } from '../mockSSE';
export default function () {
const [tabs, setTabs] = useState([{ label: 'Tab 1', children: 'Content of Tab 1', key: '1' }]);
const [activeKey, setActiveKey] = useState('1');
const [conversations, setConversations] = useState<Record<string, Conversation>>({});
const add = () => {
const newActiveKey = new Date().valueOf().toString();
const newPanes = [...tabs];
newPanes.push({ label: 'New Tab', children: 'Content of new Tab', key: newActiveKey });
setTabs(newPanes);
setActiveKey(newActiveKey);
};
const remove = (targetKey: string) => {
let newActiveKey = activeKey;
let lastIndex = -1;
tabs.forEach((item, i) => {
if (item.key === targetKey) {
lastIndex = i - 1;
}
});
const newPanes = tabs.filter((item) => item.key !== targetKey);
if (newPanes.length && newActiveKey === targetKey) {
if (lastIndex >= 0) {
newActiveKey = newPanes[lastIndex].key;
} else {
newActiveKey = newPanes[0].key;
}
}
setTabs(newPanes);
setActiveKey(newActiveKey);
};
const data = useMemo(() => {
return conversations[activeKey];
}, [activeKey, conversations]);
const handleSubmit = (val = '') => {
if (!data) {
const promptId = new Date().valueOf().toString();
const messageId = (new Date().valueOf() + 1).toString();
const conversationId = (new Date().valueOf() + 2).toString();
const message = new (class extends Message {})({ id: messageId });
const prompt = new (class extends Prompt {})({
id: promptId,
title: val,
messages: [message],
});
const conversation = new (class extends Conversation {})({
id: conversationId,
prompts: [prompt],
});
setConversations((prev) => ({ ...prev, [activeKey]: conversation }));
mockSSE({
message: val,
onopen() {
setConversations((prev) => ({
...prev,
[activeKey]: produce(prev[activeKey], (draft) => {
const prompt = draft.prompts.find((i) => i.id === promptId);
const message = prompt?.messages.find((i) => i.id === messageId);
if (!message) return;
message.status = MessageStatus.GENERATING;
}),
}));
},
onmessage(str) {
setConversations((prev) => ({
...prev,
[activeKey]: produce(prev[activeKey], (draft) => {
const prompt = draft.prompts.find((i) => i.id === promptId);
const message = prompt?.messages.find((i) => i.id === messageId);
if (!message) return;
message.content += str;
}),
}));
},
onstop() {
setConversations((prev) => ({
...prev,
[activeKey]: produce(prev[activeKey], (draft) => {
const prompt = draft.prompts.find((i) => i.id === promptId);
const message = prompt?.messages.find((i) => i.id === messageId);
if (!message) return;
message.status = MessageStatus.DONE;
}),
}));
},
});
}
};
return (
<>
<Tabs
type="editable-card"
onChange={setActiveKey}
activeKey={activeKey}
onEdit={(targetKey, action) => {
if (action === 'add') {
add();
} else {
remove(targetKey as string);
}
}}
items={tabs}
/>
<AI data={data} onSubmit={handleSubmit} />
</>
);
}
function AI({ data, onSubmit }: { data?: Conversation; onSubmit?: (str?: string) => void }) {
const chat = Chat.useChat();
const [value, setValue] = useState<string | undefined>('');
return (
<div style={{ width: '100%', height: 400, marginBottom: 46 }}>
<Chat chat={chat}>
<Chat.Content
data={data?.prompts || []}
placeholder={
<h1>
<Chat.Welcome
title="dt-react-component"
description="React UI component library based on antd package"
/>
<br />
<Chat.Tag onClick={() => onSubmit?.('请告诉我一首诗')}>
返回一首诗
</Chat.Tag>
</h1>
}
/>
<Chat.Input
value={value}
onChange={setValue}
onPressEnter={() => onSubmit?.(value)}
onPressShiftEnter={() => setValue((v) => v + '\n')}
button={{
disabled: chat.loading() || !value?.trim(),
}}
placeholder="请输入想咨询的内容…"
/>
</Chat>
</div>
);
}