fix: AI客服+合同审查回复格式化;review.js修复;积分充值流程完善
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
module.exports = {
|
||||
main: async (event) => {
|
||||
let { data = {}, userInfo } = event;
|
||||
|
||||
const COZE_TOKEN = 'sat_OiXlI4Da5Q99fxmul6pNfExmA6hR898UbxBDwrvjZ638APxRSlFnXbz6zyFedKNG';
|
||||
// 从前端传入 bot_id,不同项目用不同智能体
|
||||
const BOT_ID = data.bot_id || '7680345813062680618';
|
||||
|
||||
const res = await uniCloud.httpclient.request('https://api.coze.cn/v3/chat', {
|
||||
method: 'POST',
|
||||
dataType: 'text',
|
||||
contentType: 'json',
|
||||
timeout: 120000,
|
||||
headers: { 'Authorization': 'Bearer ' + COZE_TOKEN },
|
||||
data: {
|
||||
bot_id: BOT_ID,
|
||||
user_id: (userInfo && userInfo.uid) || 'anonymous',
|
||||
stream: true,
|
||||
auto_save_history: true,
|
||||
additional_messages: [{
|
||||
role: 'user',
|
||||
content: data.message,
|
||||
content_type: 'text'
|
||||
}]
|
||||
}
|
||||
});
|
||||
|
||||
let answer = '';
|
||||
let conversationId = '';
|
||||
const text = res.data || '';
|
||||
const lines = text.split('\n');
|
||||
for (const line of lines) {
|
||||
if (!line.startsWith('data:')) continue;
|
||||
const d = line.slice(5).trim();
|
||||
if (d === '[DONE]' || d === '"[DONE]"') continue;
|
||||
try {
|
||||
const p = JSON.parse(d);
|
||||
if (p.conversation_id) conversationId = p.conversation_id;
|
||||
if (p.type === 'answer' && p.content) {
|
||||
answer += p.content; // 拼接,不是覆盖
|
||||
}
|
||||
} catch (e) { /* 跳过 */ }
|
||||
}
|
||||
if (!answer) answer = '暂时无法回复,请拨打 400-686-5559 咨询';
|
||||
|
||||
return {
|
||||
code: 0,
|
||||
msg: 'ok',
|
||||
data: { answer, conversation_id: conversationId }
|
||||
};
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,47 @@
|
||||
module.exports = {
|
||||
main: async (event) => {
|
||||
let { data = {} } = event;
|
||||
const COZE_TOKEN = 'sat_OiXlI4Da5Q99fxmul6pNfExmA6hR898UbxBDwrvjZ638APxRSlFnXbz6zyFedKNG';
|
||||
const { chat_id, conversation_id } = data;
|
||||
|
||||
if (!chat_id || !conversation_id) return { code: -1, msg: '参数不完整' };
|
||||
|
||||
// 查询对话状态
|
||||
const statusRes = await uniCloud.httpclient.request(
|
||||
'https://api.coze.cn/v3/chat?chat_id=' + chat_id + '&conversation_id=' + conversation_id + '&bot_id=7689821072762011698',
|
||||
{ method: 'GET', dataType: 'text', headers: { 'Authorization': 'Bearer ' + COZE_TOKEN }, timeout: 15000 }
|
||||
);
|
||||
|
||||
let status = 'unknown';
|
||||
try {
|
||||
const parsed = JSON.parse(statusRes.data);
|
||||
status = parsed.data && parsed.data.status;
|
||||
} catch (e) {}
|
||||
|
||||
if (status !== 'completed') {
|
||||
return { code: 0, data: { status } };
|
||||
}
|
||||
|
||||
// 获取消息
|
||||
const msgRes = await uniCloud.httpclient.request(
|
||||
'https://api.coze.cn/v3/chat/message/list?chat_id=' + chat_id + '&conversation_id=' + conversation_id,
|
||||
{ method: 'GET', dataType: 'text', headers: { 'Authorization': 'Bearer ' + COZE_TOKEN }, timeout: 15000 }
|
||||
);
|
||||
|
||||
let answer = '';
|
||||
try {
|
||||
const parsed = JSON.parse(msgRes.data);
|
||||
const messages = parsed.data;
|
||||
if (Array.isArray(messages)) {
|
||||
for (const m of messages) {
|
||||
if (m.type === 'answer' && m.role === 'assistant') {
|
||||
answer = m.content;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e) {}
|
||||
|
||||
return { code: 0, data: { status: 'completed', answer } };
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,85 @@
|
||||
module.exports = {
|
||||
main: async (event) => {
|
||||
let { data = {}, userInfo } = event;
|
||||
const COZE_TOKEN = 'sat_OiXlI4Da5Q99fxmul6pNfExmA6hR898UbxBDwrvjZ638APxRSlFnXbz6zyFedKNG';
|
||||
const BOT_ID = '7689821072762011698';
|
||||
|
||||
let fileId = data.file_id;
|
||||
|
||||
// 模式1:只上传文件(选文件时调用)
|
||||
if (data.upload_only && data.file_base64) {
|
||||
const fileBuffer = Buffer.from(data.file_base64, 'base64');
|
||||
const boundary = '----FormBoundary' + Date.now();
|
||||
const header = Buffer.from(
|
||||
'--' + boundary + '\r\n' +
|
||||
'Content-Disposition: form-data; name="file"; filename="' + (data.file_name || 'file') + '"\r\n' +
|
||||
'Content-Type: application/octet-stream\r\n\r\n'
|
||||
);
|
||||
const footer = Buffer.from('\r\n--' + boundary + '--\r\n');
|
||||
const uploadBody = Buffer.concat([header, fileBuffer, footer]);
|
||||
|
||||
try {
|
||||
const uploadRes = await uniCloud.httpclient.request('https://api.coze.cn/v1/files/upload', {
|
||||
method: 'POST',
|
||||
dataType: 'json',
|
||||
headers: {
|
||||
'Authorization': 'Bearer ' + COZE_TOKEN,
|
||||
'Content-Type': 'multipart/form-data; boundary=' + boundary,
|
||||
'Content-Length': uploadBody.length.toString()
|
||||
},
|
||||
content: uploadBody,
|
||||
timeout: 60000
|
||||
});
|
||||
if (uploadRes.data.code !== 0) {
|
||||
return { code: -1, msg: '文件上传失败: ' + uploadRes.data.msg };
|
||||
}
|
||||
fileId = uploadRes.data.data.id;
|
||||
return { code: 0, msg: 'ok', data: { file_id: fileId } };
|
||||
} catch (e) {
|
||||
return { code: -1, msg: '上传异常: ' + e.message };
|
||||
}
|
||||
}
|
||||
|
||||
// 模式2:发起审查(不等结果,立即返回 chat_id)
|
||||
if (!fileId) {
|
||||
return { code: -1, msg: '缺少 file_id' };
|
||||
}
|
||||
|
||||
const objectString = JSON.stringify([
|
||||
{ type: 'file', file_id: fileId },
|
||||
{ type: 'text', text: '请审查这份合同' }
|
||||
]);
|
||||
|
||||
try {
|
||||
const chatRes = await uniCloud.httpclient.request('https://api.coze.cn/v3/chat', {
|
||||
method: 'POST',
|
||||
dataType: 'json',
|
||||
contentType: 'json',
|
||||
timeout: 30000,
|
||||
headers: { 'Authorization': 'Bearer ' + COZE_TOKEN },
|
||||
data: {
|
||||
bot_id: BOT_ID,
|
||||
user_id: (userInfo && userInfo.uid) || 'anonymous',
|
||||
stream: false,
|
||||
auto_save_history: true,
|
||||
additional_messages: [{
|
||||
role: 'user',
|
||||
content: objectString,
|
||||
content_type: 'object_string'
|
||||
}]
|
||||
}
|
||||
});
|
||||
|
||||
const chatData = chatRes.data && chatRes.data.data;
|
||||
return {
|
||||
code: 0, msg: 'ok',
|
||||
data: {
|
||||
chat_id: chatData && chatData.id,
|
||||
conversation_id: chatData && chatData.conversation_id
|
||||
}
|
||||
};
|
||||
} catch (e) {
|
||||
return { code: -1, msg: '审查发起失败: ' + e.message };
|
||||
}
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user