wujingjing
2025-02-11 ecb928a3b81a29467e0e118057d7c73adaf32041
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
<template>
    <div ref="chatContainerRef" :style="chatContainerStyle" class="opacity-90 small-chat-container" @mousedown="startDrag">
        <div class="bg-white rounded-lg shadow-lg flex flex-col w-[370px] max-h-[540px] absolute bottom-4 right-4">
            <!-- 头部 -->
            <div
                ref="chatHeaderRef"
                :style="handleStyle"
                class="small-chat-header h-12 flex items-center justify-between px-4"
                style="border-bottom: 1px solid #e0e0e0"
            >
                <div class="text-lg font-bold py-2">WI水务智能助手</div>
                <div class="flex items-center gap-2">
                    <!-- <el-icon class="cursor-pointer text-gray-400 hover:text-gray-600">
                    <Refresh />
                </el-icon>
                <el-icon class="cursor-pointer text-gray-400 hover:text-gray-600">
                    <FullScreen />
                </el-icon>
                <el-icon class="cursor-pointer text-gray-400 hover:text-gray-600">
                    <Star />
                </el-icon> -->
                    <!-- <el-icon class="cursor-pointer text-gray-400 hover:text-gray-600" @click="closeChat">
                        <Close />
                    </el-icon> -->
                </div>
            </div>
 
            <!-- 内容区 -->
            <div class="flex-1 overflow-y-auto p-4" ref="chatContentRef" v-show="showHistory">
                <div v-if="isInit">
                    <div class="mx-1">
                        <div class="robot-tip mt-1 p-0.5">
                            <span class="text-[14px] flex flex-col gap-1">
                                <span style="font-style: italic; font-size: 15px; font-weight: 550">欢迎来到AI世界,探索无限可能! </span>
                                <span>我是WI水务智能助手,你可以输入以下问题,进行地图操作。</span>
                            </span>
                        </div>
                    </div>
                    <!-- 欢迎语 -->
                    <!-- <div class="flex flex-col items-center gap-1.5 mt-8">
                        <div class="text-lg">你好, 我是</div>
                        <div class="text-lg text-blue-500">WI水务智能助手</div>
                        <span class="text-lg">你可以输入以下问题,进行地图操作</span>
                    </div> -->
                    <!-- 快捷问题 -->
                    <!-- <div class="mt-8 flex flex-col gap-3 mx-10">
                        <div
                            v-for="(item, index) in initQuestionList"
                            :key="index"
                            class="cursor-pointer hover:bg-gray-50 p-3 rounded-lg border border-solid border-gray-400"
                            @click="handleQuestionClick(item.question)"
                        >
                            {{ item.title }}
                        </div>
                    </div> -->
                    <div class="mr-1 ml-10 !mt-6 text-[13px] flex flex-col">
                        <div class="space-y-2">
                            <div
                                class="bg-gray-200 hover:bg-gray-300 cursor-pointer rounded-lg p-3"
                                v-for="(item, index) in initQuestionList"
                                :key="index"
                                style="width: fit-content"
                                @click="handleQuestionClick(item.question)"
                            >
                                <div class="over-ellisis-1" style="width: fit-content">
                                    {{ item.title }}
                                </div>
                            </div>
                        </div>
                        <!-- <div class="flex-items-center ml-auto mr-2 mt-1 text-gray-600 active:text-gray-500" @click="changeABatch">
                            <span class="ywifont ywicon-shuaxin !text-[12px] mr-1"></span>
                            <span>换一批</span>
                        </div> -->
                    </div>
                </div>
                <div v-else class="flex flex-col gap-1.5">
                    <!-- 对话内容 -->
                    <div class="flex flex-col gap-4" v-for="(item, index) in historyMessages" :key="item.id">
                        <!-- 用户提问 -->
                        <div class="flex gap-3 items-center" v-if="item.role === 'user'">
                            <img :src="userPic" class="w-10 h-10" alt="用户头像" />
                            <div class="flex-1 bg-blue-100 rounded-lg">
                                <div class="p-3 flex items-center">
                                    <span>
                                        {{ item.content.value }}
                                    </span>
 
                                    <!-- #region ====================== 回复反馈 ======================-->
                                    <el-icon v-if="(historyMessages[index+1].content as AssistantContent).isLoading" class="ml-2 animate-spin"
                                        ><Loading
                                    /></el-icon>
                                    <template v-else>
                                        <span
                                            v-if="(historyMessages[index+1].content as AssistantContent).isError"
                                            class="flex items-center ml-4 text-danger before:content-['('] after:content-[')']"
                                        >
                                            {{ (historyMessages[index + 1].content as AssistantContent).value }}
                                            <el-tooltip
                                                v-if="(historyMessages[index + 1].content as AssistantContent).reason"
                                                :content="(historyMessages[index + 1].content as AssistantContent).reason"
                                                placement="top"
                                            >
                                                <el-icon class="flex-center cursor-pointer ml-1">
                                                    <question-filled />
                                                </el-icon>
                                            </el-tooltip>
                                        </span>
                                        <span v-else class="ml-4 text-success before:content-['('] after:content-[')']">
                                            {{ (historyMessages[index + 1].content as AssistantContent).value }}
                                        </span>
                                    </template>
 
                                    <!-- #endregion -->
                                </div>
                            </div>
                        </div>
 
                        <!-- AI回答 -->
                        <!-- <div class="flex gap-3" v-else-if="item.role === 'assistant'">
                            <img :src="assistantPic" class="w-10 h-10" alt="AI头像" />
                            <div class="flex-1 bg-gray-100">
                                <div v-if="(item.content as AssistantContent)?.isLoading" class="p-4 rounded-lg flex items-center">
                                    <el-icon class="animate-spin mr-2"><Loading /></el-icon>
                                    AI正在思考中...
                                </div>
                                <div v-else class="p-4 rounded-lg" :class="{ 'text-danger': (item.content as AssistantContent)?.isError }">
                                    {{ item.content.value }}
                                </div>
                            </div>
                        </div> -->
                    </div>
                </div>
            </div>
 
            <!-- 底部输入框 -->
            <div class="p-2 border-t">
                <ChatInput v-model="inputText" @sendClick="sendClick" @toggleHistory="toggleHistory" :showHistory="showHistory" />
            </div>
        </div>
        <Teleport to="body">
            <WorkOrderDlg v-model="optDlgIsShow" :item="optDlgMapRow"></WorkOrderDlg>
 
        </Teleport>
 
    </div>
</template>
 
<script setup lang="ts" name="smallChat">
import { computed, nextTick, onMounted, ref } from 'vue';
import ChatInput from './ChatInput.vue';
import type { ChatMessage } from './types';
import { AssistantContent } from './types';
import { agentStreamByPost } from '/@/api/ai/chat';
import { Logger } from '/@/model/logger/Logger';
import { GaoDeSourceType, gaoDeSourceTypeMap, type OLMap } from '/@/model/map/OLMap';
import assistantPic from '/static/images/role/assistant-200x192.png';
import userPic from '/static/images/role/user-200x206.png';
import { useDrag } from '/@/hooks/useDrag';
import { cloneDeep, defaults } from 'lodash-es';
import WorkOrderDlg from './WorkOrderDlg.vue';
const props = defineProps<{
    olMap?: OLMap;
}>();
const isOpen = ref(false);
const inputText = ref('');
/** @description 对话完成前是否时初始状态 */
let lastIsInit = true;
const closeChat = () => {
    isOpen.value = false;
};
const chatHeaderRef = ref<HTMLDivElement>(null);
 
const chatContainerRef = ref<HTMLDivElement>(null);
 
const {
    startDrag,
    style: chatContainerStyle,
    handleStyle,
} = useDrag({
    handle: chatHeaderRef,
    startPos: {
        x: 200,
    },
});
 
const historyMessages = ref<ChatMessage[]>([]);
const isInit = computed(() => historyMessages.value.length === 0);
const initQuestionList = ref([
    { title: '图层切换', question: '切换卫星地图' },
    { title: '地图缩放', question: '放大' },
    { title: '设备显隐', question: '隐藏设备' },
    { title: '设备聚焦', question: '聚焦设备' },
]);
const chatContentRef = ref<HTMLDivElement>(null);
 
const getLastAssistantMessage = () => {
    const last = historyMessages.value[historyMessages.value.length - 1];
    const result = last.role === 'assistant' ? last : null;
    return result as ChatMessage<AssistantContent>;
};
 
//#region ====================== 添加工单 ======================
const optDlgIsShow = ref(false);
const optDlgMapRow = ref(null);
 
const openOptDlg = (row?: any) => {
    optDlgMapRow.value = row;
    optDlgIsShow.value = true;
};
 
 
const submit = () => {
    
};
//#endregion
 
const mockCommand = (question: string) => {
    if (question === `切换${gaoDeSourceTypeMap[GaoDeSourceType.Vector]}`) {
        handleMapCommand({ operate: '切换标准地图' });
        return;
    } else if (question === `切换${gaoDeSourceTypeMap[GaoDeSourceType.Satellite]}`) {
        handleMapCommand({ operate: '切换卫星地图' });
        return;
    } else if (question === `切换${gaoDeSourceTypeMap[GaoDeSourceType.SatelliteRoad]}`) {
        handleMapCommand({ operate: '切换路网地图' });
        return;
    } else if (question === '显示设备') {
        handleMapCommand({ operate: '显示设备' });
        return;
    } else if (question === '隐藏设备') {
        handleMapCommand({ operate: '隐藏设备' });
        return;
    } else if (question === '聚焦设备') {
        handleMapCommand({ operate: '聚焦设备' });
        return;
    }
};
 
const startStream = (question: string) => {
    if (lastIsInit) {
        showHistory.value = false;
    }
 
    // mockCommand(question);
    // return;
    let haveMapOperate = false;
    agentStreamByPost(
        {
            question: question,
            agent_id: 'a_019471cdb0667a83956b76ac97283f1c',
        },
        (chunkRes) => {
            Logger.info('agent stream response:\n\n' + JSON.stringify(chunkRes));
 
            if (chunkRes.mode === 'map') {
                haveMapOperate = true;
                if (chunkRes.type === 'string') {
                    const jsonData = JSON.parse(chunkRes.value);
                    handleMapCommand(jsonData);
                }
            }
            if (chunkRes.mode === 'finish') {
                if (!haveMapOperate) {
                    refreshAssistantMessage({ reason: `未识别到操作:"${question}"` });
                }
            }
        }
    ).catch((error) => {
        Logger.error('agent stream error:\n\n' + error);
        refreshAssistantMessage({ reason: 'AI回答失败' });
    });
};
 
const refreshAssistantMessage = (
    content: Partial<AssistantContent> = { value: '失败', isError: true, reason: '', isLoading: false }
) => {
    const cloneContent = cloneDeep(content);
    const last = getLastAssistantMessage();
    content = defaults(cloneContent, {
        value: '失败',
        isError: true,
        reason: '',
        isLoading: false,
    });
    if (last) {
        for (const key in content) {
            if (Object.prototype.hasOwnProperty.call(content, key)) {
                last.content[key] = content[key];
            }
        }
    }
};
 
const handleMapCommand = (command: any) => {
    if (!command) return;
    // openOptDlg();
    switch (command.operate) {
        case '放大':
            props.olMap.zoomIn();
            break;
 
        case '缩小':
            props.olMap.zoomOut();
            break;
        case `切换${gaoDeSourceTypeMap[GaoDeSourceType.Vector]}`:
            props.olMap.setSourceType(GaoDeSourceType.Vector);
            break;
        case `切换${gaoDeSourceTypeMap[GaoDeSourceType.Satellite]}`:
            props.olMap.setSourceType(GaoDeSourceType.Satellite);
            break;
 
        case `切换${gaoDeSourceTypeMap[GaoDeSourceType.SatelliteRoad]}`:
            props.olMap.setSourceType(GaoDeSourceType.SatelliteRoad);
            break;
 
        case '显示设备':
            const equipOverlay = props.olMap.getEquipOverlay();
            if (equipOverlay) {
                equipOverlay.isVisible = true;
                // 强制触发更新
                props.olMap.layerInfo.value = props.olMap.layerInfo.value.concat([])
                // props.olMap.toggleMarkerOverlayVisible(true);
            }
            break;
        case '隐藏设备':
            const equipOverlay1 = props.olMap.getEquipOverlay();
            if (equipOverlay1) {
                equipOverlay1.isVisible = false;
                // 强制触发更新
                props.olMap.layerInfo.value = props.olMap.layerInfo.value.concat([])
                // props.olMap.toggleMarkerOverlayVisible(false);
 
            }
            break;
        case '聚焦设备':
            props.olMap.adjustViewToMarkers();
            break;
    }
    refreshAssistantMessage({ value: `成功`, isError: false });
};
 
const scrollToBottom = () => {
    if (chatContentRef.value) {
        chatContentRef.value.scrollTop = chatContentRef.value.scrollHeight;
    }
};
 
const applyMessage = () => {
    const time = Date.now().toString();
    const userMessageId = `user-${time}`;
    const userMessage: ChatMessage = {
        id: userMessageId,
        role: 'user',
        content: {
            value: inputText.value,
        },
    };
    historyMessages.value.push(userMessage);
    const assistantMessageId = `assistant-${time}`;
    const assistantMessage: ChatMessage = {
        id: assistantMessageId,
        role: 'assistant',
        content: {
            value: '',
            isLoading: true,
        },
    };
    historyMessages.value.push(assistantMessage);
 
    nextTick(() => {
        scrollToBottom();
    });
    const userReactive = historyMessages.value.find((item) => item.id === userMessageId);
    const assistantReactive = historyMessages.value.find((item) => item.id === assistantMessageId);
    return [userReactive, assistantReactive];
};
 
const sendClick = async () => {
    if (!inputText.value.trim()) return;
    lastIsInit = isInit.value;
    const lastMessage = historyMessages.value[historyMessages.value.length - 1];
    if (lastMessage && lastMessage.role === 'assistant' && (lastMessage.content as AssistantContent).isLoading) return;
    const [userMessage, assistantMessage] = applyMessage();
    const question = inputText.value;
    inputText.value = '';
 
    startStream(question);
};
 
const handleQuestionClick = (item: string) => {
    inputText.value = item;
    sendClick();
};
 
//#region ====================== 历史显示控制 ======================
const showHistory = ref(true);
const toggleHistory = () => {
    showHistory.value = !showHistory.value;
    nextTick(() => {
        scrollToBottom();
    });
};
//#endregion
onMounted(() => {});
</script>
 
<style scoped lang="scss">
:deep(.el-input__wrapper) {
    padding-right: 0;
}
 
.robot-tip {
    position: relative;
    width: fit-content;
    height: fit-content;
    background: linear-gradient(90deg, #ccdcf5 0%, #ccdcf5 25%, #ebf3fe 55%, #ccdcf5 100%);
    border: 4px solid transparent;
    border-radius: 10px;
}
.robot-tip::after {
    content: '';
    position: absolute;
    top: 100%;
    left: 20%;
    transform: translateX(-50%);
    border: 20px solid transparent;
    border-top: 20px solid transparent;
}
.robot-tip::before {
    content: '';
    position: absolute;
    top: 100%;
    left: 20%;
    transform: translateX(-50%);
    border: 20px solid transparent;
    border-top: 20px solid #ccdcf5;
    z-index: 1;
}
</style>