chore(stage-ui): remove sync schedule

This commit is contained in:
RainbowBird
2026-03-28 02:25:44 +08:00
committed by RainbowBird
parent 96b417f39e
commit 7a16121135
@@ -5,15 +5,13 @@ import { nanoid } from 'nanoid'
import { defineStore, storeToRefs } from 'pinia'
import { computed, ref, watch } from 'vue'
import { client } from '../../composables/api'
import { useLocalFirstRequest } from '../../composables/use-local-first'
import { chatSessionsRepo } from '../../database/repos/chat-sessions.repo'
import { useAuthStore } from '../auth'
import { useAiriCardStore } from '../modules/airi-card'
import { mergeLoadedSessionMessages } from './session-message-merge'
export const useChatSessionStore = defineStore('chat-session', () => {
const { userId, isAuthenticated } = storeToRefs(useAuthStore())
const { userId } = storeToRefs(useAuthStore())
const { activeCardId, systemPrompt } = storeToRefs(useAiriCardStore())
const activeSessionId = ref<string>('')
@@ -28,7 +26,6 @@ export const useChatSessionStore = defineStore('chat-session', () => {
let initializePromise: Promise<void> | null = null
let persistQueue = Promise.resolve()
let syncQueue = Promise.resolve()
const loadedSessions = new Set<string>()
const loadingSessions = new Map<string, Promise<void>>()
@@ -49,30 +46,10 @@ export const useChatSessionStore = defineStore('chat-session', () => {
return persistQueue
}
function enqueueSync(task: () => Promise<void>) {
syncQueue = syncQueue.then(task, task)
return syncQueue
}
function snapshotMessages(messages: ChatHistoryItem[]) {
return JSON.parse(JSON.stringify(messages)) as ChatHistoryItem[]
}
function extractMessageContent(message: ChatHistoryItem) {
if (typeof message.content === 'string')
return message.content
if (Array.isArray(message.content)) {
return message.content.map((part) => {
if (typeof part === 'string')
return part
if (part && typeof part === 'object' && 'text' in part)
return String(part.text ?? '')
return ''
}).join('')
}
return ''
}
function ensureSessionMessageIds(sessionId: string) {
const current = sessionMessages.value[sessionId] ?? []
let changed = false
@@ -92,87 +69,6 @@ export const useChatSessionStore = defineStore('chat-session', () => {
return next
}
function buildSyncMessages(messages: ChatHistoryItem[]) {
return messages.map(message => ({
id: message.id ?? nanoid(),
role: message.role,
content: extractMessageContent(message),
createdAt: message.createdAt,
}))
}
async function syncSessionToRemote(sessionId: string) {
let cachedRecord: ChatSessionRecord | null | undefined
const request = useLocalFirstRequest({
local: async () => {
cachedRecord = await chatSessionsRepo.getSession(sessionId)
return cachedRecord
},
remote: async () => {
if (!cachedRecord)
cachedRecord = await chatSessionsRepo.getSession(sessionId)
if (!cachedRecord)
return cachedRecord
const members: Array<
| { type: 'user', userId: string }
| { type: 'character', characterId: string }
> = [
{ type: 'user', userId: userId.value },
]
if (cachedRecord.meta.characterId && cachedRecord.meta.characterId !== 'default') {
members.push({
type: 'character',
characterId: cachedRecord.meta.characterId,
})
}
const normalizedMessages = cachedRecord.messages.map(message => message.id ? message : { ...message, id: nanoid() })
if (normalizedMessages.some((message, index) => cachedRecord?.messages[index]?.id !== message.id)) {
cachedRecord = {
...cachedRecord,
messages: normalizedMessages,
}
await chatSessionsRepo.saveSession(sessionId, cachedRecord)
}
const res = await client.api.chats.sync.$post({
json: {
chat: {
id: cachedRecord.meta.sessionId,
type: 'group',
title: cachedRecord.meta.title,
createdAt: cachedRecord.meta.createdAt,
updatedAt: cachedRecord.meta.updatedAt,
},
members,
messages: buildSyncMessages(cachedRecord.messages),
},
})
if (!res.ok)
throw new Error('Failed to sync chat session')
return cachedRecord
},
allowRemote: () => isAuthenticated.value,
lazy: true,
})
await request.execute()
}
function scheduleSync(sessionId: string) {
void enqueueSync(async () => {
try {
await syncSessionToRemote(sessionId)
}
catch (error) {
console.warn('Failed to sync chat session', error)
}
})
}
function generateInitialMessageFromPrompt(prompt: string) {
const content = codeBlockSystemPrompt + mathSyntaxSystemPrompt + prompt
@@ -244,7 +140,6 @@ export const useChatSessionStore = defineStore('chat-session', () => {
await chatSessionsRepo.saveIndex(snapshot)
}
})
scheduleSync(sessionId)
}
function persistSessionMessages(sessionId: string) {
@@ -335,7 +230,6 @@ export const useChatSessionStore = defineStore('chat-session', () => {
const record: ChatSessionRecord = { meta, messages: initialMessages }
await enqueuePersist(() => chatSessionsRepo.saveSession(sessionId, record))
await persistIndex()
scheduleSync(sessionId)
if (options?.setActive !== false)
activeSessionId.value = sessionId