fix(ai-nutritionist): unwrap double-layer Coze API responses

CRMEB wraps CozeBaseResponse again, so chat/retrieve/message payloads live at response.data.data. Added unwrapCozeResponse() and applied it to sendToAI, pollChatStatus, and getChatMessages. Updated test log.

Made-with: Cursor
This commit is contained in:
panchengyong
2026-03-25 17:21:12 +08:00
parent 6ec9487597
commit 3329a2b296
2 changed files with 60 additions and 20 deletions

View File

@@ -637,6 +637,19 @@ export default {
return new Promise(resolve => setTimeout(resolve, ms));
},
/**
* 解包 Coze API 响应:后端返回双层包装 { code, data: { code, data: actualPayload } }
* 此方法统一提取最内层的业务数据
*/
unwrapCozeResponse(response) {
if (!response) return null;
let data = response.data;
if (data && typeof data === 'object' && data.code !== undefined && data.data !== undefined) {
data = data.data;
}
return data;
},
async sendToAI(content, type) {
this.isLoading = true;
@@ -707,15 +720,17 @@ export default {
autoSaveHistory: true
};
if (this.conversationId) requestData.conversationId = this.conversationId;
const response = await api.cozeChat(requestData);
if (response && response.data) {
const chat = response.data.chat || response.data;
const response = await api.cozeChat(requestData);
const cozeData = this.unwrapCozeResponse(response);
if (cozeData) {
const chat = cozeData.chat || cozeData;
const conversationId = chat.conversation_id || chat.conversationID || chat.conversationId;
const chatId = chat.id;
if (conversationId && chatId) {
this.conversationId = conversationId;
await this.pollChatStatus(conversationId, chatId, aiMsg);
} else {
console.error('Coze chat response structure:', JSON.stringify(response));
throw new Error('发起对话失败未返回会话或对话ID');
}
} else {
@@ -746,15 +761,16 @@ export default {
}
try {
const res = await api.cozeRetrieveChat({
conversationId,
chatId
});
if (res && res.data) {
console.log("====api.cozeRetrieveChat response====", res.data);
const chatObj = res.data.chat || res.data;
const status = chatObj && chatObj.status;
const res = await api.cozeRetrieveChat({
conversationId,
chatId
});
const retrieveData = this.unwrapCozeResponse(res);
if (retrieveData) {
console.log("====api.cozeRetrieveChat response====", retrieveData);
const chatObj = retrieveData.chat || retrieveData;
const status = chatObj && chatObj.status;
if (status === 'completed') {
// 对话完成,获取消息详情
@@ -784,14 +800,15 @@ export default {
async getChatMessages(conversationId, chatId, aiMsg) {
try {
const res = await api.cozeMessageList({
conversationId,
chatId
});
this.isLoading = false;
console.log("====api.cozeMessageList response====", res.data);
const rawMessages = res && res.data && (Array.isArray(res.data.messages) ? res.data.messages : (Array.isArray(res.data) ? res.data : null));
const res = await api.cozeMessageList({
conversationId,
chatId
});
this.isLoading = false;
const msgData = this.unwrapCozeResponse(res);
console.log("====api.cozeMessageList response====", msgData);
const rawMessages = msgData && (Array.isArray(msgData.messages) ? msgData.messages : (Array.isArray(msgData) ? msgData : null));
if (rawMessages && rawMessages.length > 0) {
// 过滤出 type='answer' 且 role='assistant' 的消息
const answerMsgs = rawMessages.filter(msg => msg.role === 'assistant' && msg.type === 'answer');