AiChat.vue
10.1 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
<template>
<div ref="chatContainerRef" class="chat-container" :style="chatContainerStyle">
<template v-if="dataSource">
<div class="leftArea" :class="[expand ? 'expand' : 'shrink']">
<div class="content">
<slide v-if="uuid" :dataSource="dataSource" @save="handleSave" :prologue="prologue" :appData="appData" @click="handleChatClick"></slide>
</div>
<div class="toggle-btn" @click="handleToggle">
<span class="icon">
<svg viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
<path
d="M5.64645 3.14645C5.45118 3.34171 5.45118 3.65829 5.64645 3.85355L9.79289 8L5.64645 12.1464C5.45118 12.3417 5.45118 12.6583 5.64645 12.8536C5.84171 13.0488 6.15829 13.0488 6.35355 12.8536L10.8536 8.35355C11.0488 8.15829 11.0488 7.84171 10.8536 7.64645L6.35355 3.14645C6.15829 2.95118 5.84171 2.95118 5.64645 3.14645Z"
fill="currentColor"
></path>
</svg>
</span>
</div>
</div>
<div class="rightArea" :class="[expand ? 'expand' : 'shrink']">
<chat
url="/airag/chat/send"
v-if="uuid && chatVisible"
:uuid="uuid"
:historyData="chatData"
type="view"
@save="handleSave"
:formState="appData"
:prologue="prologue"
:presetQuestion="presetQuestion"
@reload-message-title="reloadMessageTitle"
:chatTitle="chatTitle"
:quickCommandData="quickCommandData"
></chat>
</div>
</template>
<Spin v-else :spinning="true"></Spin>
</div>
</template>
<script setup lang="ts">
import slide from './slide.vue';
import chat from './chat.vue';
import { Spin } from 'ant-design-vue';
import { ref, watch, nextTick, onUnmounted, onMounted } from 'vue';
import { useUserStore } from '/@/store/modules/user';
import { JEECG_CHAT_KEY } from '/@/enums/cacheEnum';
import { defHttp } from '/@/utils/http/axios';
import { useRouter } from 'vue-router';
const router = useRouter();
const userId = useUserStore().getUserInfo?.id;
const localKey = JEECG_CHAT_KEY + userId;
let timer: any = null;
let unwatch01: any = null;
const dataSource = ref<any>({});
const uuid = ref<string>('');
const chatData = ref<any>([]);
const expand = ref<any>(true);
const chatVisible = ref(true);
const chatContainerRef = ref<any>(null);
const chatContainerStyle = ref({});
//左侧聊天信息
const chatTitle = ref<string>('');
//左侧聊天点击的坐标
const chatActiveKey = ref<number>(0);
//预置开场白
const presetQuestion = ref<string>('');
const handleToggle = () => {
expand.value = !expand.value;
};
//应用id
const appId = ref<string>('');
//应用数据
const appData = ref<any>({});
//开场白
const prologue = ref<string>('');
//快捷指令
const quickCommandData = ref<any>([]);
const priming = () => {
dataSource.value = {
active: '1002',
usingContext: true,
history: [{ id: '1002', title: '新建聊天', isEdit: false, disabled: true }],
};
chatTitle.value = '新建聊天';
chatActiveKey.value = 0;
};
const handleSave = () => {
// 删除标签或清空内容之后的保存
//save(dataSource.value);
setTimeout(() => {
// 删除标签或清空内容也会触发watch保存,此时不需watch保存需清除
//clearTimeout(timer);
}, 50);
};
// 监听dataSource变化执行操作
const execute = () => {
unwatch01 = watch(
() => dataSource.value.active,
(value) => {
if (value) {
if (value == '1002') {
uuid.value = '1002';
chatData.value = [];
chatTitle.value = "新建聊天";
chatVisible.value = false;
nextTick(() => {
chatVisible.value = true;
});
return;
}
//update-begin---author:wangshuai---date:2025-03-14---for:【QQYUN-11421】聊天,删除会话后,聊天切换到新的会话,但是聊天标题没有变---
let values = dataSource.value.history.filter((item) => item.id === value);
if(values && values.length>0){
chatTitle.value = values[0]?.title
}
//update-end---author:wangshuai---date:2025-03-14---for:【QQYUN-11421】聊天,删除会话后,聊天切换到新的会话,但是聊天标题没有变---
//根据选中的id查询聊天内容
let params = { conversationId: value };
uuid.value = value;
defHttp.get({ url: '/airag/chat/messages', params }, { isTransformResponse: false }).then((res) => {
if (res.success) {
chatData.value = res.result;
} else {
chatData.value = [];
}
chatVisible.value = false;
nextTick(() => {
chatVisible.value = true;
});
});
}else{
chatData.value = [];
chatTitle.value = "";
}
},
{ immediate: true }
);
};
/**
* 初始化聊天信息
* @param appId
*/
function initChartData(appId = '') {
defHttp
.get(
{
url: '/airag/chat/conversations',
params: { appId: appId },
},
{ isTransformResponse: false }
)
.then((res) => {
if (res.success && res.result && res.result.length > 0) {
dataSource.value.history = res.result;
dataSource.value.active = res.result[0].id;
chatTitle.value = res.result[0].title;
chatActiveKey.value = 0;
} else {
priming();
}
!unwatch01 && execute();
})
.catch(() => {
priming();
});
}
onMounted(() => {
let params: any = router.currentRoute.value.params;
if (params.appId) {
appId.value = params.appId;
getApplicationData(params.appId);
initChartData(params.appId);
} else {
initChartData();
quickCommandData.value = [
{ name: '请介绍一下JeecgBoot', descr: "请介绍一下JeecgBoot" },
{ name: 'JEECG有哪些优势?', descr: "JEECG有哪些优势?" },
{ name: 'JEECG可以做哪些事情?', descr: "JEECG可以做哪些事情?" },];
}
});
onUnmounted(() => {
chatData.value = [];
chatTitle.value = "";
prologue.value = ""
presetQuestion.value = "";
quickCommandData.value = [];
})
/**
* 获取应用id
*
* @param appId
*/
async function getApplicationData(appId) {
await defHttp
.get(
{
url: '/airag/app/queryById',
params: { id: appId },
},
{ isTransformResponse: false }
)
.then((res) => {
if (res.success) {
appData.value = res.result;
if (res.result && res.result.prologue) {
prologue.value = res.result.prologue;
}
if (res.result && res.result.quickCommand) {
quickCommandData.value = JSON.parse(res.result.quickCommand);
}
if (res.result && res.result.presetQuestion) {
presetQuestion.value = res.result.presetQuestion;
}
} else {
appData.value = {};
}
});
}
/**
* 左侧消息列表点击事件
* @param title
* @param index
*/
function handleChatClick(title, index) {
chatTitle.value = title;
chatActiveKey.value = index;
}
/**
* 重新加载标题消息
* @param text
*/
function reloadMessageTitle(text) {
let title = dataSource.value.history[chatActiveKey.value].title;
if(title === '新建聊天'){
dataSource.value.history[chatActiveKey.value].title = text;
dataSource.value.history[chatActiveKey.value]['disabled'] = false;
}
}
/**
* 初始化聊天:用于icon点击
*/
function initChat(value) {
appId.value = value;
getApplicationData(value);
initChartData(value);
}
defineExpose({
initChat
})
onUnmounted(() => {
unwatch01 && unwatch01();
});
watch(
() => chatContainerRef.value,
() => {
if(chatContainerRef.value.offsetHeight){
chatContainerStyle.value = { height: `${chatContainerRef.value.offsetHeight} px` };
}
}
);
</script>
<style scoped lang="less">
@width: 260px;
.chat-container {
height: 100%;
width: 100%;
position: relative;
background: white;
display: flex;
overflow: hidden;
z-index: 999;
border: 1px solid #eeeeee;
:deep(.ant-spin) {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
}
.leftArea {
width: @width;
transition: 0.3s left;
position: absolute;
left: 0;
height: 100%;
.content {
width: 100%;
height: 100%;
overflow: hidden;
}
&.shrink {
left: -@width;
.toggle-btn {
.icon {
transform: rotate(0deg);
}
}
}
.toggle-btn {
transition:
color 0.3s cubic-bezier(0.4, 0, 0.2, 1),
right 0.3s cubic-bezier(0.4, 0, 0.2, 1),
left 0.3s cubic-bezier(0.4, 0, 0.2, 1),
border-color 0.3s cubic-bezier(0.4, 0, 0.2, 1),
background-color 0.3s cubic-bezier(0.4, 0, 0.2, 1);
cursor: pointer;
width: 24px;
height: 24px;
position: absolute;
top: 50%;
right: 0;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
font-size: 18px;
color: rgb(51, 54, 57);
border: 1px solid rgb(239, 239, 245);
background-color: #fff;
box-shadow: 0 2px 4px 0px rgba(0, 0, 0, 0.06);
transform: translateX(50%) translateY(-50%);
z-index: 1;
}
.icon {
transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
transform: rotate(180deg);
font-size: 18px;
height: 18px;
svg {
height: 1em;
width: 1em;
vertical-align: top;
}
}
}
.rightArea {
margin-left: @width;
transition: 0.3s margin-left;
&.shrink {
margin-left: 0;
}
flex: 1;
min-width: 0;
}
</style>