feat: 对话的调试调整
This commit is contained in:
14
src/renderer/api/ConversationApi.ts
Normal file
14
src/renderer/api/ConversationApi.ts
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
/* eslint-disable */
|
||||||
|
// @ts-ignore
|
||||||
|
import { getRequest, ResponseModel } from '@utils/request'
|
||||||
|
|
||||||
|
export interface CreateConversationResponse {
|
||||||
|
conversationId: string
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 创建会话 创建会话创建会话 GET /agent/assistant/createConversation */
|
||||||
|
|
||||||
|
export const createConversation = async () => {
|
||||||
|
const res: ResponseModel = await getRequest('/agent/assistant/createConversation')
|
||||||
|
return res.data as CreateConversationResponse
|
||||||
|
}
|
||||||
@@ -87,6 +87,7 @@ instance.interceptors.request.use(
|
|||||||
// 添加响应拦截器
|
// 添加响应拦截器
|
||||||
instance.interceptors.response.use(
|
instance.interceptors.response.use(
|
||||||
(res) => {
|
(res) => {
|
||||||
|
console.log("🚀 ~ response:", res.data)
|
||||||
// 未设置状态码则默认成功状态
|
// 未设置状态码则默认成功状态
|
||||||
const code = res.data.code || 200
|
const code = res.data.code || 200
|
||||||
// 获取错误信息
|
// 获取错误信息
|
||||||
@@ -138,4 +139,34 @@ instance.interceptors.response.use(
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// 封装基于 request 的 POST 请求(
|
||||||
|
export const postRequest = <ResponseModel>(url: string, data?: any, options?: any): Promise<ResponseModel> => {
|
||||||
|
return instance.request({
|
||||||
|
url,
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
data,
|
||||||
|
...(options || {}),
|
||||||
|
}) as Promise<ResponseModel>
|
||||||
|
}
|
||||||
|
|
||||||
|
// 封装基于 request 的 GET 请求
|
||||||
|
export const getRequest = <ResponseModel>(url: string, params?: any, options?: any): Promise<ResponseModel> => {
|
||||||
|
return instance.request({
|
||||||
|
url,
|
||||||
|
method: 'GET',
|
||||||
|
params,
|
||||||
|
...(options || {}),
|
||||||
|
}) as Promise<ResponseModel>
|
||||||
|
}
|
||||||
|
|
||||||
export default instance
|
export default instance
|
||||||
|
|
||||||
|
/// 响应模型
|
||||||
|
export interface ResponseModel {
|
||||||
|
code: number;
|
||||||
|
msg: string | null;
|
||||||
|
data: any | null;
|
||||||
|
};
|
||||||
@@ -88,6 +88,8 @@ import { Session } from '../../utils/storage';
|
|||||||
|
|
||||||
import userAvatar from '@assets/images/login/user_icon.png';
|
import userAvatar from '@assets/images/login/user_icon.png';
|
||||||
import aiAvatar from '@assets/images/login/blue_logo.png';
|
import aiAvatar from '@assets/images/login/blue_logo.png';
|
||||||
|
import { createConversation } from '../../api/ConversationApi';
|
||||||
|
import { ElMessage, ElLoading } from 'element-plus'
|
||||||
|
|
||||||
// 支持外部通过 prop 控制是否为引导页
|
// 支持外部通过 prop 控制是否为引导页
|
||||||
const props = defineProps({
|
const props = defineProps({
|
||||||
@@ -108,6 +110,7 @@ watch(isGuidePage, (v) => {
|
|||||||
if (v) {
|
if (v) {
|
||||||
// 当切换到引导页时,重置/清理会话状态
|
// 当切换到引导页时,重置/清理会话状态
|
||||||
resetConversation();
|
resetConversation();
|
||||||
|
createConversationRequest();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -122,7 +125,7 @@ const inputMessage = ref("");
|
|||||||
const isSendingMessage = ref(false);
|
const isSendingMessage = ref(false);
|
||||||
|
|
||||||
/// agentId 首页接口中获取 1953462165250859010
|
/// agentId 首页接口中获取 1953462165250859010
|
||||||
const agentId = ref("1");
|
const agentId = ref("1953462165250859010");
|
||||||
/// 会话ID 历史数据接口中获取
|
/// 会话ID 历史数据接口中获取
|
||||||
const conversationId = ref("");
|
const conversationId = ref("");
|
||||||
// 会话进行中标志
|
// 会话进行中标志
|
||||||
@@ -261,11 +264,12 @@ onMounted(() => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// token存在,初始化数据
|
// token存在,初始化数据
|
||||||
const initHandler = () => {
|
const initHandler = async () => {
|
||||||
console.log("initHandler");
|
console.log("initHandler");
|
||||||
const token = getAccessToken();
|
const token = getAccessToken();
|
||||||
if (!token) return;
|
if (!token) return;
|
||||||
initWebSocket();
|
await createConversationRequest();
|
||||||
|
await initWebSocket();
|
||||||
};
|
};
|
||||||
|
|
||||||
const getAccessToken = () => {
|
const getAccessToken = () => {
|
||||||
@@ -280,6 +284,12 @@ const checkToken = async () => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// 调用接口创建新会话
|
||||||
|
const createConversationRequest = async () => {
|
||||||
|
const res = await createConversation();
|
||||||
|
conversationId.value = res.conversationId;
|
||||||
|
};
|
||||||
|
|
||||||
/// =============对话↓================
|
/// =============对话↓================
|
||||||
// 初始化WebSocket
|
// 初始化WebSocket
|
||||||
const initWebSocket = async () => {
|
const initWebSocket = async () => {
|
||||||
@@ -475,10 +485,7 @@ const sendMessage = async (message: string, isInstruct: boolean = false) => {
|
|||||||
if (!isWsConnected()) {
|
if (!isWsConnected()) {
|
||||||
console.log("WebSocket未连接,尝试重新连接...");
|
console.log("WebSocket未连接,尝试重新连接...");
|
||||||
// 显示加载提示
|
// 显示加载提示
|
||||||
// uni.showLoading({
|
const loadingInstance = ElLoading.service({ fullscreen: true, text: '正在连接服务器...' });
|
||||||
// title: "正在连接服务器...",
|
|
||||||
// });
|
|
||||||
|
|
||||||
// 尝试重新初始化WebSocket连接
|
// 尝试重新初始化WebSocket连接
|
||||||
try {
|
try {
|
||||||
await initWebSocket();
|
await initWebSocket();
|
||||||
@@ -487,31 +494,22 @@ const sendMessage = async (message: string, isInstruct: boolean = false) => {
|
|||||||
|
|
||||||
// 检查连接是否成功建立
|
// 检查连接是否成功建立
|
||||||
if (!isWsConnected()) {
|
if (!isWsConnected()) {
|
||||||
// uni.hideLoading();
|
loadingInstance.close();
|
||||||
// uni.showToast({
|
ElMessage({ message: '连接服务器失败,请稍后重试', type: 'error' })
|
||||||
// title: "连接服务器失败,请稍后重试",
|
|
||||||
// icon: "none",
|
|
||||||
// });
|
|
||||||
console.error("重新连接WebSocket后仍未连接成功");
|
console.error("重新连接WebSocket后仍未连接成功");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// uni.hideLoading();
|
loadingInstance.close();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
loadingInstance.close();
|
||||||
console.error("重新连接WebSocket失败:", error);
|
console.error("重新连接WebSocket失败:", error);
|
||||||
// uni.hideLoading();
|
ElMessage({ message: '连接服务器失败,请稍后重试', type: 'error' })
|
||||||
// uni.showToast({
|
|
||||||
// title: "连接服务器失败,请稍后重试",
|
|
||||||
// icon: "none",
|
|
||||||
// });
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isSessionActive.value) {
|
if (isSessionActive.value) {
|
||||||
// uni.showToast({
|
ElMessage({ message: '当前会话正在进行中,请等待回复完成', type: 'warning' })
|
||||||
// title: "请等待当前回复完成",
|
|
||||||
// icon: "none",
|
|
||||||
// });
|
|
||||||
console.warn("当前会话正在进行中,请等待回复完成");
|
console.warn("当前会话正在进行中,请等待回复完成");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -801,5 +799,4 @@ const resetConversation = () => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
Reference in New Issue
Block a user