v1.3 工具调用及界面优化

This commit is contained in:
2026-11-10 17:32:21 +08:00
parent 46dc092b07
commit 76af622626
8 changed files with 835 additions and 171 deletions
+39 -5
View File
@@ -59,6 +59,16 @@ function sortSessionsByUpdatedAt(sessions: ChatSession[]): ChatSession[] {
return [...sessions].sort((first, second) => second.updatedAt - first.updatedAt)
}
/**
* 重算会话标题(第一条 user 消息;没有则「新对话」)。
* 删除消息 / 把消息插回会话都可能改变第一条 user ⇒ 标题要跟着重算,
* removeMessages 与 insertMessageAt 共用这一份,不写两套。
*/
function refreshSessionTitle(session: ChatSession): void {
const firstUserMessage = session.messages.find((item) => item.role === 'user')
session.title = firstUserMessage ? buildSessionTitle(firstUserMessage.content) : '新对话'
}
/**
* 把「生成中」形态的消息落定为 stopped(正文原样保留)。
* 两处共用同一变换,保证口径一致:
@@ -431,12 +441,36 @@ export const useChatStore = defineStore('chat', {
// 这是有意接受的取舍 —— 重新问一次即可,不值得为它引入节流。
this.persistPending = true
},
removeMessage(messageId: string) {
const targetSession = this.sessions.find((session) => session.messages.some((message) => message.id === messageId))
/**
* 批量删除消息(跨会话按 id 定位,同 removeMessage 的定位方式)。
* 零产出整轮作废用:一轮失败要同时删 user + assistant 两条,
* 一次 filter、一次重算标题、一次落盘 —— 不为每条消息各写一遍 localStorage。
*/
removeMessages(messageIds: string[]) {
const ids = new Set(messageIds)
const targetSession = this.sessions.find((session) => session.messages.some((message) => ids.has(message.id)))
if (!targetSession) return
targetSession.messages = targetSession.messages.filter((message) => message.id !== messageId)
const firstUserMessage = targetSession.messages.find((item) => item.role === 'user')
targetSession.title = firstUserMessage ? buildSessionTitle(firstUserMessage.content) : '新对话'
targetSession.messages = targetSession.messages.filter((message) => !ids.has(message.id))
refreshSessionTitle(targetSession)
targetSession.updatedAt = Date.now()
this.persistSessions()
},
removeMessage(messageId: string) {
// 口径只留一份:单条删除委托批量版
this.removeMessages([messageId])
},
/**
* 把消息按下标插回指定会话(按 sessionId 定位,不是 activeSession ——
* 重新生成零产出失败时要把原回复放回**原会话**,用户此刻可能已切走)。
* 幂等:同 id 已在会话里 ⇒ 直接 return(迟到的结算不得插出重复消息)。
*/
insertMessageAt(sessionId: string, index: number, message: ChatMessage) {
const targetSession = this.sessions.find((session) => session.id === sessionId)
if (!targetSession) return
if (targetSession.messages.some((item) => item.id === message.id)) return
const clampedIndex = Math.max(0, Math.min(index, targetSession.messages.length))
targetSession.messages.splice(clampedIndex, 0, message)
refreshSessionTitle(targetSession)
targetSession.updatedAt = Date.now()
this.persistSessions()
},