|
@@ -4,6 +4,7 @@ const db = require('../../plugin/DataBase/db')
|
|
|
const ACTIVE_STATE = 1
|
|
const ACTIVE_STATE = 1
|
|
|
const DELETED_STATE = 2
|
|
const DELETED_STATE = 2
|
|
|
const ASSISTANT_UUID = 'e4fe0277-0b1a-41a1-b25f-8b6e4cec3281'
|
|
const ASSISTANT_UUID = 'e4fe0277-0b1a-41a1-b25f-8b6e4cec3281'
|
|
|
|
|
+const DEFAULT_TITLE = '新对话'
|
|
|
|
|
|
|
|
function toPositiveInt(value, fallback = null) {
|
|
function toPositiveInt(value, fallback = null) {
|
|
|
const n = Number(value)
|
|
const n = Number(value)
|
|
@@ -67,9 +68,11 @@ async function generateConversationNo() {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
async function assertUserConversation(conversationId, uuid) {
|
|
async function assertUserConversation(conversationId, uuid) {
|
|
|
|
|
+ const id = toPositiveInt(conversationId)
|
|
|
|
|
+ if (!id) return null
|
|
|
const rows = await db.query(
|
|
const rows = await db.query(
|
|
|
'SELECT id, conversation_no, create_user, title, state FROM ai_chat_conversation WHERE id = ? AND create_user = ? AND state = ? LIMIT 1',
|
|
'SELECT id, conversation_no, create_user, title, state FROM ai_chat_conversation WHERE id = ? AND create_user = ? AND state = ? LIMIT 1',
|
|
|
- [conversationId, uuid, ACTIVE_STATE]
|
|
|
|
|
|
|
+ [id, uuid, ACTIVE_STATE]
|
|
|
)
|
|
)
|
|
|
return rows && rows.length === 1 ? rows[0] : null
|
|
return rows && rows.length === 1 ? rows[0] : null
|
|
|
}
|
|
}
|
|
@@ -113,7 +116,7 @@ class ConversationService {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
const now = Date.now()
|
|
const now = Date.now()
|
|
|
- const safeTitle = normalizeText(title, 80) || '新对话'
|
|
|
|
|
|
|
+ const safeTitle = normalizeText(title, 80) || DEFAULT_TITLE
|
|
|
const conversationNo = await generateConversationNo()
|
|
const conversationNo = await generateConversationNo()
|
|
|
const result = await db.query(
|
|
const result = await db.query(
|
|
|
`INSERT INTO ai_chat_conversation
|
|
`INSERT INTO ai_chat_conversation
|
|
@@ -184,32 +187,57 @@ class ConversationService {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
async addUserMessage({ uuid, conversationId, content, images }) {
|
|
async addUserMessage({ uuid, conversationId, content, images }) {
|
|
|
- const conv = await assertUserConversation(conversationId, uuid)
|
|
|
|
|
- if (!conv) return null
|
|
|
|
|
-
|
|
|
|
|
const text = normalizeText(content, 2000)
|
|
const text = normalizeText(content, 2000)
|
|
|
const imgs = normalizeImages(images)
|
|
const imgs = normalizeImages(images)
|
|
|
if (!text && imgs.length === 0) return { missingContent: true }
|
|
if (!text && imgs.length === 0) return { missingContent: true }
|
|
|
|
|
|
|
|
|
|
+ const preview = buildPreview(text, imgs)
|
|
|
|
|
+ const conv = conversationId
|
|
|
|
|
+ ? await assertUserConversation(conversationId, uuid)
|
|
|
|
|
+ : await this.createConversation({ uuid, title: preview ? preview.slice(0, 40) : DEFAULT_TITLE })
|
|
|
|
|
+ if (!conv) return null
|
|
|
|
|
+
|
|
|
const now = Date.now()
|
|
const now = Date.now()
|
|
|
const result = await db.query(
|
|
const result = await db.query(
|
|
|
`INSERT INTO ai_chat_message
|
|
`INSERT INTO ai_chat_message
|
|
|
(conversation_id, create_user, role, content, images, status, error_msg, create_time, update_time)
|
|
(conversation_id, create_user, role, content, images, status, error_msg, create_time, update_time)
|
|
|
VALUES (?, ?, 'user', ?, ?, 'done', '', ?, ?)`,
|
|
VALUES (?, ?, 'user', ?, ?, 'done', '', ?, ?)`,
|
|
|
- [conversationId, uuid, text, JSON.stringify(imgs), now, now]
|
|
|
|
|
|
|
+ [conv.id, uuid, text, JSON.stringify(imgs), now, now]
|
|
|
)
|
|
)
|
|
|
- const preview = buildPreview(text, imgs)
|
|
|
|
|
- const title = conv.title === '新对话' && preview ? preview.slice(0, 40) : conv.title
|
|
|
|
|
|
|
+ const title = (!conv.title || conv.title === DEFAULT_TITLE || Number(conv.last_message_time || 0) === 0) && preview
|
|
|
|
|
+ ? preview.slice(0, 40)
|
|
|
|
|
+ : conv.title
|
|
|
await db.query(
|
|
await db.query(
|
|
|
'UPDATE ai_chat_conversation SET title = ?, last_message_preview = ?, last_message_time = ?, update_time = ? WHERE id = ?',
|
|
'UPDATE ai_chat_conversation SET title = ?, last_message_preview = ?, last_message_time = ?, update_time = ? WHERE id = ?',
|
|
|
- [title, preview, now, now, conversationId]
|
|
|
|
|
|
|
+ [title, preview, now, now, conv.id]
|
|
|
)
|
|
)
|
|
|
return {
|
|
return {
|
|
|
- conversationId,
|
|
|
|
|
|
|
+ conversationId: conv.id,
|
|
|
conversationNo: conv.conversation_no,
|
|
conversationNo: conv.conversation_no,
|
|
|
messageId: result?.insertId,
|
|
messageId: result?.insertId,
|
|
|
content: text,
|
|
content: text,
|
|
|
- images: imgs
|
|
|
|
|
|
|
+ images: imgs,
|
|
|
|
|
+ conversation: {
|
|
|
|
|
+ id: conv.id,
|
|
|
|
|
+ conversation_no: conv.conversation_no,
|
|
|
|
|
+ title,
|
|
|
|
|
+ state: ACTIVE_STATE,
|
|
|
|
|
+ last_message_preview: preview,
|
|
|
|
|
+ last_message_time: now,
|
|
|
|
|
+ create_time: conv.create_time || now,
|
|
|
|
|
+ update_time: now
|
|
|
|
|
+ },
|
|
|
|
|
+ message: {
|
|
|
|
|
+ id: result?.insertId,
|
|
|
|
|
+ conversation_id: conv.id,
|
|
|
|
|
+ role: 'user',
|
|
|
|
|
+ content: text,
|
|
|
|
|
+ images: imgs,
|
|
|
|
|
+ status: 'done',
|
|
|
|
|
+ error_msg: '',
|
|
|
|
|
+ create_time: now,
|
|
|
|
|
+ update_time: now
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|