Compare commits

...

3 Commits

Author SHA1 Message Date
fbda01a624 两端同步流式输出,刷新状态,强制退出。 2026-08-04 17:49:42 +08:00
b7c3fc9ff6 输出后闪白修复 2026-08-04 15:31:01 +08:00
2f1702c858 接了智能体列表,其他接口暂不可接 2026-08-04 14:44:48 +08:00
5 changed files with 440 additions and 21 deletions

View File

@@ -212,6 +212,194 @@
background: linear-gradient(135deg, #8b5cf6, #7c3aed); background: linear-gradient(135deg, #8b5cf6, #7c3aed);
} }
/* ========== 智能体选择弹窗 ========== */
.agent-modal-mask {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.45);
backdrop-filter: blur(4px);
-webkit-backdrop-filter: blur(4px);
z-index: 1000;
display: flex;
align-items: center;
justify-content: center;
}
.agent-modal {
width: 85%;
max-height: 70%;
background-color: #ffffff;
border-radius: 24rpx;
overflow: hidden;
display: flex;
flex-direction: column;
box-shadow: 0 16rpx 48rpx rgba(0, 0, 0, 0.12);
}
.agent-modal-header {
display: flex;
justify-content: center;
align-items: center;
padding: 30rpx 30rpx 20rpx;
position: relative;
border-bottom: 2rpx solid #f0f0f0;
}
.agent-modal-title {
font-size: 34rpx;
font-weight: 700;
color: #1a1a2e;
}
.agent-modal-close {
position: absolute;
right: 24rpx;
top: 50%;
transform: translateY(-50%);
padding: 10rpx;
}
.agent-list {
box-sizing: border-box;
flex: 1;
padding: 20rpx 24rpx;
max-height: 600rpx;
}
.agent-loading {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 60rpx 0;
}
.agent-loading-text {
margin-top: 16rpx;
font-size: 26rpx;
color: #999;
}
.agent-empty {
display: flex;
align-items: center;
justify-content: center;
padding: 80rpx 0;
}
.agent-empty-text {
font-size: 28rpx;
color: #999;
}
.agent-card {
box-sizing: border-box;
width: 100%;
background: linear-gradient(135deg, #f8f5ff, #efe8ff);
border: 2rpx solid #c4b5fd;
border-radius: 20rpx;
padding: 28rpx 24rpx;
margin-bottom: 20rpx;
transition: all 0.2s ease;
overflow: hidden;
}
.agent-card:active {
transform: scale(0.98);
border-color: #a78bfa;
background: linear-gradient(135deg, #f3efff, #e8dcff);
}
.agent-card-header {
display: flex;
align-items: center;
margin-bottom: 16rpx;
}
.agent-avatar {
width: 70rpx;
height: 70rpx;
border-radius: 20rpx;
background: linear-gradient(135deg, #8b5cf6, #7c3aed);
display: flex;
align-items: center;
justify-content: center;
flex-shrink: 0;
}
.agent-card-info {
margin-left: 16rpx;
display: flex;
flex-direction: column;
flex: 1;
min-width: 0;
}
.agent-card-title {
font-size: 30rpx;
font-weight: 600;
color: #1a1a2e;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.agent-card-category {
font-size: 22rpx;
color: #8b5cf6;
margin-top: 4rpx;
}
.agent-card-desc {
font-size: 26rpx;
color: #555;
line-height: 1.5;
word-break: break-all;
display: -webkit-box;
-webkit-line-clamp: 5;
-webkit-box-orient: vertical;
overflow: hidden;
}
.agent-card-welcome {
font-size: 24rpx;
color: #7c3aed;
margin-top: 12rpx;
padding-top: 12rpx;
border-top: 2rpx dashed #ddd6fe;
word-break: break-all;
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
overflow: hidden;
}
.agent-modal-footer {
padding: 20rpx 30rpx 30rpx;
border-top: 2rpx solid #f0f0f0;
display: flex;
justify-content: flex-end;
}
.agent-modal-btn {
padding: 18rpx 48rpx;
border-radius: 16rpx;
font-size: 28rpx;
font-weight: 500;
}
.agent-modal-btn-cancel {
background: #f0f0f0;
color: #666;
}
.agent-modal-btn-cancel:active {
background: #e0e0e0;
}
/* ==========新建聊天列表相关====== */ /* ==========新建聊天列表相关====== */
.close-option-card { .close-option-card {
width: 100%; width: 100%;
@@ -319,6 +507,10 @@
font-size: 48rpx; font-size: 48rpx;
} }
.chat-btn-group .head-btn:nth-child(4) {
/* 刷新按钮 */
}
.log-out { .log-out {
/* background: #ffd4d4; */ /* background: #ffd4d4; */
} }

View File

@@ -20,15 +20,59 @@
</view> </view>
<uni-icons type="arrow-right" class="arrow-right-style"></uni-icons> <uni-icons type="arrow-right" class="arrow-right-style"></uni-icons>
</view> </view>
<view class="ncd-option ncd-intelligence"> <view class="ncd-option ncd-intelligence" @click="selectAgentChat">
<view class="ncd-opt-icon"> <view class="ncd-opt-icon">
<uni-icons type="star" color="#ffffff" size="30"></uni-icons> <uni-icons type="star" color="#ffffff" size="30"></uni-icons>
</view>
<view class="ncd-opt-title">
<view class="ncd-opt-title-1">智能体会话</view>
<view class="ncd-opt-title-2">选择专属智能体获得精准专业服务</view>
</view>
<uni-icons type="arrow-right" class="arrow-right-style"></uni-icons>
</view>
</view>
</view>
</view>
<!-- 智能体选择弹窗 -->
<view class="agent-modal-mask" v-if="showAgentModal" @click="closeAgentModal">
<view class="agent-modal" @click.stop>
<view class="agent-modal-header">
<text class="agent-modal-title">选择智能体</text>
<view class="agent-modal-close" @click="closeAgentModal">
<uni-icons type="closeempty" size="20" color="#999"></uni-icons>
</view>
</view>
<scroll-view class="agent-list" scroll-y="true">
<view v-if="agentLoading" class="agent-loading">
<uni-icons type="spinner-cycle" size="24" color="#1677ff"></uni-icons>
<text class="agent-loading-text">加载智能体列表...</text>
</view>
<view v-else-if="agentList.length === 0" class="agent-empty">
<text class="agent-empty-text">暂无可用智能体</text>
</view>
<view v-else
v-for="agent in agentList"
:key="agent._id"
class="agent-card"
@click="handleAgentSelect(agent)"
>
<view class="agent-card-header">
<view class="agent-avatar">
<uni-icons type="vip" size="22" color="#fff"></uni-icons>
</view>
<view class="agent-card-info">
<text class="agent-card-title">{{ agent.title }}</text>
<!-- <text class="agent-card-category">{{ agent.category_name }}</text> -->
</view>
</view> </view>
<view class="ncd-opt-title"> <text class="agent-card-desc">{{ agent.description }}</text>
<view class="ncd-opt-title-1">智能体会话</view> <!-- <text class="agent-card-welcome" v-if="agent.welcome_message">{{ agent.welcome_message }}</text> -->
<view class="ncd-opt-title-2">选择专属智能体获得精准专业服务</view> </view>
</view> </scroll-view>
<uni-icons type="arrow-right" class="arrow-right-style"></uni-icons> <view class="agent-modal-footer">
<view class="agent-modal-btn agent-modal-btn-cancel" @click="closeAgentModal">
<text>取消</text>
</view> </view>
</view> </view>
</view> </view>
@@ -55,10 +99,13 @@
<view class="head-btn" @click="goWorkSpace"> <view class="head-btn" @click="goWorkSpace">
<view class="iconfont icon-wenjianjia"></view> <view class="iconfont icon-wenjianjia"></view>
</view> </view>
<view class="head-btn" @click="goCloudDatabase"> <view class="head-btn" @click="goCloudDatabase">
<view class="iconfont icon-cloud"></view> <view class="iconfont icon-cloud"></view>
</view> </view>
<view class="head-btn log-out" @click="logOut"> <view class="head-btn" @click="handleRefresh">
<uni-icons type="refreshempty" size="24" color="#52c41a"></uni-icons>
</view>
<view class="head-btn log-out" @click="logOut">
<view class="iconfont icon-tuichu"></view> <view class="iconfont icon-tuichu"></view>
</view> </view>
</view> </view>
@@ -315,7 +362,8 @@
createConversation, createConversation,
getUserInfo, getUserInfo,
getUserAvatar, getUserAvatar,
conversationMessageDownload conversationMessageDownload,
getAgentList
} from '@/utils/cloud-api.js' } from '@/utils/cloud-api.js'
import { import {
getToken, getToken,
@@ -689,6 +737,66 @@ import { openFile as openFileNative, downloadAndOpen } from '@/utils/fileOpener.
showNewChatModal.value = false; showNewChatModal.value = false;
} }
// 智能体选择相关
const showAgentModal = ref(false)
const agentList = ref([])
const agentLoading = ref(false)
const selectAgentChat = async () => {
try {
showNewChatModal.value = false
showAgentModal.value = true
agentLoading.value = true
agentList.value = []
// getAgentList 直接返回智能体对象数组
const agents = await getAgentList(userToken.value)
if (agents && Array.isArray(agents)) {
agentList.value = agents
} else {
uni.showToast({ title: '获取智能体列表失败', icon: 'none' })
}
} catch (error) {
console.error('获取智能体列表失败:', error)
uni.showToast({ title: '获取智能体列表失败,请重试', icon: 'none' })
} finally {
agentLoading.value = false
}
}
const closeAgentModal = () => {
showAgentModal.value = false
agentList.value = []
}
const handleAgentSelect = async (agent) => {
console.log("点击了",agent.title);
// try {
// closeAgentModal()
// const chars = '0123456789abcdef'
// let name = ''
// for (let i = 0; i < 24; i++) {
// name += chars[Math.floor(Math.random() * chars.length)]
// }
// const workspaceId = await createWorkspace(userToken.value, name)
// if (!workspaceId) {
// uni.showToast({ title: '工作区创建失败', icon: 'none' })
// return
// }
// const loginInfo = uni.getStorageSync('yxd_login_info')
// const userName = loginInfo ? (loginInfo.username || '') : ''
// const conversationId = await createConversation(userToken.value, workspaceId, agent.title, userName)
// if (!conversationId) {
// uni.showToast({ title: '智能体会话创建失败', icon: 'none' })
// return
// }
// uni.showToast({ title: `已创建「${agent.title}」会话`, icon: 'success' })
// takeUserConversations()
// } catch (error) {
// console.error('创建智能体会话失败:', error)
// uni.showToast({ title: '创建失败,请重试', icon: 'none' })
// }
}
// 侧边栏相关 // 侧边栏相关
const isChatSidebar = ref(false) const isChatSidebar = ref(false)
const handleChatSidebar = () => { const handleChatSidebar = () => {
@@ -734,6 +842,71 @@ import { openFile as openFileNative, downloadAndOpen } from '@/utils/fileOpener.
}) })
} }
// 刷新当前会话消息
const handleRefresh = async () => {
// 1. 重置输入框状态
isThinking.value = false
isSelfSent.value = false
socketStore.isThinking = false
textMessage.value = ''
uploadedFiles.value = []
previewFileArray.value = []
// 移除流式消息占位
if (streamingMessageId.value) {
allmessages.value = allmessages.value.filter(m => m._id !== streamingMessageId.value)
streamingMessageId.value = null
}
const titleMap = { 0: 'AI消息', 1: '好友消息', 2: '群聊消息' }
const title = titleMap[ChatType.value] || '消息'
uni.showLoading({ title: `刷新${title}中...`, mask: true })
try {
// 2. 检查当前 token 是否还有效
const token = getToken()
if (!token) {
uni.hideLoading()
uni.showToast({ title: '登录已失效,请重新登录', icon: 'none', duration: 1500 })
setTimeout(() => uni.reLaunch({ url: '/pages/Login/Login' }), 1500)
return
}
await getUserInfo(token)
// 3. 确保 AI 对话模式下 socket 连接正常PC 离线由 watcher 实时监控)
if (ChatType.value === 0 && !socketStore.isConnected) {
await handleConnect()
}
// 4. 刷新消息
switch (ChatType.value) {
case 0:
await takeConversationMessages()
break
case 1:
await takeFriendMessages()
break
case 2:
await takeGroupMessages()
break
default:
await takeConversationMessages()
break
}
uni.hideLoading()
uni.showToast({ title: '已刷新', icon: 'success', duration: 1200 })
} catch (e) {
uni.hideLoading()
// 区分 token 失效和其他错误
const errMsg = typeof e === 'string' ? e : (e?.message || '')
if (errMsg.includes('token') || errMsg.includes('Token') || errMsg.includes('登录') || errMsg.includes('过期')) {
uni.showToast({ title: '登录已失效,请重新登录', icon: 'none', duration: 1500 })
setTimeout(() => uni.reLaunch({ url: '/pages/Login/Login' }), 1500)
} else {
uni.showToast({ title: '刷新失败', icon: 'error', duration: 1500 })
}
}
}
const previewFileArray = ref([]) const previewFileArray = ref([])
const uploadPhoto = () => { const uploadPhoto = () => {
console.log('点击了拍照上传'); console.log('点击了拍照上传');
@@ -1257,13 +1430,16 @@ import { openFile as openFileNative, downloadAndOpen } from '@/utils/fileOpener.
} }
// 全局监听 socketStore.isThinking跨设备同步思考状态锁/解锁输入框 // 全局监听 socketStore.isThinking跨设备同步思考状态锁/解锁输入框
watch(() => socketStore.isThinking, (newVal, oldVal) => { watch(() => socketStore.isThinking, async (newVal, oldVal) => {
console.log("isThinking 变化:", oldVal, "→", newVal, "messageString:", socketStore.messageString); console.log("isThinking 变化:", oldVal, "→", newVal, "messageString:", socketStore.messageString);
// false → true后端开始回复锁定输入框 // false → true后端开始回复锁定输入框
if (!oldVal && newVal) { if (!oldVal && newVal) {
console.log('AI开始回复跨设备同步锁定输入框'); console.log('AI开始回复跨设备同步锁定输入框');
isThinking.value = true isThinking.value = true
// 非本设备发起时,也需要添加流式气泡 // 跨设备场景:先加载最新消息(含 PC 发的用户消息),再追加流式气泡
if (!isSelfSent.value) {
await takeConversationMessages()
}
if (!streamingMessageId.value) { if (!streamingMessageId.value) {
streamingMessageId.value = 'streaming-' + Date.now() streamingMessageId.value = 'streaming-' + Date.now()
allmessages.value = [...allmessages.value, { allmessages.value = [...allmessages.value, {
@@ -1278,12 +1454,14 @@ import { openFile as openFileNative, downloadAndOpen } from '@/utils/fileOpener.
// true → falseAI 回复结束或中断,解锁输入框并刷新消息列表 // true → falseAI 回复结束或中断,解锁输入框并刷新消息列表
if (oldVal && !newVal) { if (oldVal && !newVal) {
console.log('AI回复结束正常/中断),解锁并刷新消息列表'); console.log('AI回复结束正常/中断),解锁并刷新消息列表');
// 移除流式消息气泡,从服务器加载完整消息
// 先从服务器加载完整消息列表
await takeConversationMessages()
// 加载完成后再移除流式消息气泡,避免闪白
if (streamingMessageId.value) { if (streamingMessageId.value) {
allmessages.value = allmessages.value.filter(m => m._id !== streamingMessageId.value) allmessages.value = allmessages.value.filter(m => m._id !== streamingMessageId.value)
streamingMessageId.value = null streamingMessageId.value = null
} }
takeConversationMessages()
isThinking.value = false isThinking.value = false
isSelfSent.value = false isSelfSent.value = false
textMessage.value = '' textMessage.value = ''
@@ -1309,6 +1487,25 @@ import { openFile as openFileNative, downloadAndOpen } from '@/utils/fileOpener.
} }
}) })
// 监听 pc_offlinePC 端下线时立即强制跳转登录页
watch(() => socketStore.pcOffline, (offline) => {
if (offline) {
isThinking.value = false
socketStore.isThinking = false
socketStore.pcOffline = false
uni.showToast({
title: 'PC端不在线请先登录',
icon: 'none',
duration: 1500
})
setTimeout(() => {
uni.reLaunch({
url: '/pages/Login/Login'
})
}, 1500)
}
})
// 监听流式消息内容,实时更新 AI 回复(流式输出核心) // 监听流式消息内容,实时更新 AI 回复(流式输出核心)
watch(() => socketStore.messageString, (newContent) => { watch(() => socketStore.messageString, (newContent) => {
if (!streamingMessageId.value || !socketStore.isThinking) return if (!streamingMessageId.value || !socketStore.isThinking) return
@@ -1500,8 +1697,8 @@ import { openFile as openFileNative, downloadAndOpen } from '@/utils/fileOpener.
const takeConversationMessages = async () => { const takeConversationMessages = async () => {
try { try {
allmessages.value = await getConversationMessages(userToken.value, currentSessionId.value) || []; allmessages.value = await getConversationMessages(userToken.value, currentSessionId.value) || [];
console.log("获取到的所有消息:", JSON.stringify(allmessages.value) ); // console.log("获取到的所有消息:", JSON.stringify(allmessages.value) );
console.log("获取到的所有消息:", allmessages.value); // console.log("获取到的所有消息:", allmessages.value);
// currentMessages.value = allmessages.value.slice(-pageInfoNumber); // currentMessages.value = allmessages.value.slice(-pageInfoNumber);
// 数据获取后执行滚动 // 数据获取后执行滚动
scrollToBottom(); scrollToBottom();
@@ -1702,6 +1899,8 @@ import { openFile as openFileNative, downloadAndOpen } from '@/utils/fileOpener.
<style lang="scss" scoped> <style lang="scss" scoped>
@import "./chat.css"; @import "./chat.css";
</style> </style>
<!-- v-html 渲染的 markdown 内容无法被 scoped 样式命中需非 scoped 样式块 --> <!-- v-html 渲染的 markdown 内容无法被 scoped 样式命中需非 scoped 样式块 -->

View File

@@ -30,6 +30,7 @@ export const useSocketStore = defineStore('socket', () => {
const maxLogCount = ref(20) const maxLogCount = ref(20)
const authFailReason = ref('') const authFailReason = ref('')
const pcOffline = ref(false)
// ==================== 计算属性 ==================== // ==================== 计算属性 ====================
const isConnected = computed(() => connectionStatus.value === 'connected') const isConnected = computed(() => connectionStatus.value === 'connected')
@@ -66,6 +67,7 @@ export const useSocketStore = defineStore('socket', () => {
...config.value, ...config.value,
...options ...options
} }
pcOffline.value = false // 每次连接时重置 PC 离线标记
addLog('info', addLog('info',
`准备连接WebSocket。Token:${config.value.token || '未设置'}。ConversationId: ${config.value.conversationId || '未设置'}` `准备连接WebSocket。Token:${config.value.token || '未设置'}。ConversationId: ${config.value.conversationId || '未设置'}`
) )
@@ -182,6 +184,7 @@ export const useSocketStore = defineStore('socket', () => {
break break
case 'pc_offline': case 'pc_offline':
pcOffline.value = true
addLog('warn', 'PC 端已离线') addLog('warn', 'PC 端已离线')
break break
@@ -287,6 +290,7 @@ export const useSocketStore = defineStore('socket', () => {
logs, logs,
config, config,
authFailReason, authFailReason,
pcOffline,
// 计算属性 // 计算属性
isConnected, isConnected,

View File

@@ -6692,8 +6692,6 @@ This will fail in production.`);
const takeConversationMessages = async () => { const takeConversationMessages = async () => {
try { try {
allmessages.value = await getConversationMessages(userToken.value, currentSessionId.value) || []; allmessages.value = await getConversationMessages(userToken.value, currentSessionId.value) || [];
formatAppLog("log", "at pages/Chat/Chat.vue:1503", "获取到的所有消息:", JSON.stringify(allmessages.value));
formatAppLog("log", "at pages/Chat/Chat.vue:1504", "获取到的所有消息:", allmessages.value);
scrollToBottom(); scrollToBottom();
} catch (error) { } catch (error) {
uni.showToast({ uni.showToast({

View File

@@ -1718,3 +1718,29 @@ export const conversationMessageDownload = (token, workspacesId, localUrl) => {
}); });
}); });
}; };
// 智能体
// 获取智能体列表
export const getAgentList = (token) =>{
return new Promise((resolve,reject)=>{
uni.request({
url:`${BASE_URL}/cloud_api/agent/list`,
method:"POST",
header:{'Content-Type':'application/json'},
data:{"access_token":token},
success: (res) => {
if(res.statusCode === 200){
const respond = res.data;
if(respond.success){
resolve(respond.data)
} else {
const msg = '获取智能体列表出错啦';
reject(msg);
}
}else{
reject(`获取智能体列表失败:${res.statusCode}`)
}
}
})
})
}