feat: improve application responsiveness
This commit is contained in:
@@ -1386,6 +1386,429 @@ describe('AssistantDatabase', () => {
|
||||
database.close()
|
||||
})
|
||||
|
||||
it('incrementally saves local changes without replacing unrelated or remote data', async () => {
|
||||
const directory = await mkdtemp(
|
||||
join(tmpdir(), 'goodbuddy-incremental-conversations-')
|
||||
)
|
||||
temporaryDirectories.push(directory)
|
||||
const databasePath = join(directory, 'assistant.sqlite')
|
||||
const database = new AssistantDatabase(databasePath)
|
||||
database.initialize('C:\\Workspace')
|
||||
const project = database.listProjects()[0]!
|
||||
const conversationId =
|
||||
'00000000-0000-4000-8000-000000000501'
|
||||
const unrelatedId =
|
||||
'00000000-0000-4000-8000-000000000502'
|
||||
const streamingMessageId =
|
||||
'00000000-0000-4000-8000-000000000503'
|
||||
const newMessageId =
|
||||
'00000000-0000-4000-8000-000000000504'
|
||||
database.replaceConversations([
|
||||
{
|
||||
id: conversationId,
|
||||
projectId: project.id,
|
||||
title: '增量对话',
|
||||
updatedAt: 1_775_000_000_000,
|
||||
messages: [
|
||||
{
|
||||
id: streamingMessageId,
|
||||
role: 'assistant',
|
||||
content: '生成中',
|
||||
createdAt: 1_775_000_000_001,
|
||||
state: 'streaming',
|
||||
status: '正在生成'
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
id: unrelatedId,
|
||||
title: '不相关本地对话',
|
||||
updatedAt: 1_775_000_000_002,
|
||||
messages: []
|
||||
}
|
||||
])
|
||||
const channelProject = database.ensureChannelProjects(
|
||||
'C:\\Users\\test',
|
||||
channelDefaultProfileId
|
||||
)[0]!
|
||||
const remote = database.getOrCreateRemoteConversation({
|
||||
projectId: channelProject.id,
|
||||
channel: 'weixin',
|
||||
accountId: 'default',
|
||||
externalConversationId: 'incremental-preserved',
|
||||
conversationType: 'direct',
|
||||
title: '保留的远程对话',
|
||||
accountDisplay: '发送者 ****0501'
|
||||
})
|
||||
const raw = new DatabaseSync(databasePath)
|
||||
raw
|
||||
.prepare('UPDATE messages SET request_id = ? WHERE id = ?')
|
||||
.run('preserved-request-id', streamingMessageId)
|
||||
raw.close()
|
||||
|
||||
const save = [
|
||||
{
|
||||
header: {
|
||||
id: conversationId,
|
||||
projectId: project.id,
|
||||
title: '增量对话(已完成)',
|
||||
updatedAt: 1_775_000_001_000
|
||||
},
|
||||
messages: [
|
||||
{
|
||||
id: streamingMessageId,
|
||||
role: 'assistant' as const,
|
||||
content: '生成完成',
|
||||
createdAt: 1_775_000_000_001,
|
||||
state: 'complete' as const,
|
||||
status: '已完成'
|
||||
},
|
||||
{
|
||||
id: newMessageId,
|
||||
role: 'user' as const,
|
||||
content: '继续',
|
||||
createdAt: 1_775_000_001_000,
|
||||
state: 'complete' as const
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
database.saveLocalConversations(save)
|
||||
database.saveLocalConversations(save)
|
||||
|
||||
expect(database.getConversation(conversationId)).toMatchObject({
|
||||
title: '增量对话(已完成)',
|
||||
messages: [
|
||||
{
|
||||
id: streamingMessageId,
|
||||
content: '生成完成',
|
||||
state: 'complete',
|
||||
status: '已完成'
|
||||
},
|
||||
{
|
||||
id: newMessageId,
|
||||
content: '继续',
|
||||
state: 'complete'
|
||||
}
|
||||
]
|
||||
})
|
||||
expect(database.getConversation(unrelatedId).title).toBe(
|
||||
'不相关本地对话'
|
||||
)
|
||||
expect(database.getConversation(remote.id).remote?.channel).toBe(
|
||||
'weixin'
|
||||
)
|
||||
|
||||
const durable = new DatabaseSync(databasePath)
|
||||
expect(
|
||||
durable
|
||||
.prepare(
|
||||
`SELECT id, sequence, request_id
|
||||
FROM messages
|
||||
WHERE conversation_id = ?
|
||||
ORDER BY sequence`
|
||||
)
|
||||
.all(conversationId)
|
||||
).toEqual([
|
||||
{
|
||||
id: streamingMessageId,
|
||||
sequence: 0,
|
||||
request_id: 'preserved-request-id'
|
||||
},
|
||||
{
|
||||
id: newMessageId,
|
||||
sequence: 1,
|
||||
request_id: null
|
||||
}
|
||||
])
|
||||
durable.close()
|
||||
database.close()
|
||||
})
|
||||
|
||||
it('keeps incremental local conversation storage bounded to 500 messages', async () => {
|
||||
const directory = await mkdtemp(
|
||||
join(tmpdir(), 'goodbuddy-bounded-local-conversation-')
|
||||
)
|
||||
temporaryDirectories.push(directory)
|
||||
const databasePath = join(directory, 'assistant.sqlite')
|
||||
const database = new AssistantDatabase(databasePath)
|
||||
database.initialize('C:\\Workspace')
|
||||
const project = database.listProjects()[0]!
|
||||
const conversationId =
|
||||
'00000000-0000-4000-8000-000000000521'
|
||||
const messageId = (index: number): string =>
|
||||
`00000000-0000-4000-8001-${String(index).padStart(12, '0')}`
|
||||
database.replaceConversations([
|
||||
{
|
||||
id: conversationId,
|
||||
projectId: project.id,
|
||||
title: '有界增量对话',
|
||||
updatedAt: 1_775_000_000_000,
|
||||
messages: Array.from({ length: 500 }, (_, index) => ({
|
||||
id: messageId(index),
|
||||
role: index % 2 === 0 ? 'user' as const : 'assistant' as const,
|
||||
content: `消息 ${index}`,
|
||||
createdAt: 1_775_000_000_000 + index,
|
||||
state: 'complete' as const
|
||||
}))
|
||||
}
|
||||
])
|
||||
|
||||
const newestMessageId = messageId(500)
|
||||
database.saveLocalConversations([
|
||||
{
|
||||
header: {
|
||||
id: conversationId,
|
||||
projectId: project.id,
|
||||
title: '有界增量对话',
|
||||
updatedAt: 1_775_000_001_000
|
||||
},
|
||||
messages: [
|
||||
{
|
||||
id: newestMessageId,
|
||||
role: 'user',
|
||||
content: '最新消息',
|
||||
createdAt: 1_775_000_001_000,
|
||||
state: 'complete'
|
||||
}
|
||||
]
|
||||
}
|
||||
])
|
||||
|
||||
const restored = database.getConversation(conversationId)
|
||||
expect(restored.messages).toHaveLength(500)
|
||||
expect(restored.messages[0]?.id).toBe(messageId(1))
|
||||
expect(restored.messages.at(-1)?.id).toBe(newestMessageId)
|
||||
const raw = new DatabaseSync(databasePath)
|
||||
expect(
|
||||
raw
|
||||
.prepare(
|
||||
`SELECT COUNT(*) AS count, MIN(sequence) AS minimum,
|
||||
MAX(sequence) AS maximum
|
||||
FROM messages
|
||||
WHERE conversation_id = ?`
|
||||
)
|
||||
.get(conversationId)
|
||||
).toEqual({
|
||||
count: 500,
|
||||
minimum: 1,
|
||||
maximum: 500
|
||||
})
|
||||
raw.close()
|
||||
database.close()
|
||||
})
|
||||
|
||||
it('rolls back incremental saves when message ownership or role changes', async () => {
|
||||
const database = await createDatabase()
|
||||
const firstConversationId =
|
||||
'00000000-0000-4000-8000-000000000511'
|
||||
const secondConversationId =
|
||||
'00000000-0000-4000-8000-000000000512'
|
||||
const firstMessageId =
|
||||
'00000000-0000-4000-8000-000000000513'
|
||||
const secondMessageId =
|
||||
'00000000-0000-4000-8000-000000000514'
|
||||
const rolledBackMessageId =
|
||||
'00000000-0000-4000-8000-000000000515'
|
||||
database.replaceConversations([
|
||||
{
|
||||
id: firstConversationId,
|
||||
title: '第一对话',
|
||||
updatedAt: 1,
|
||||
messages: [
|
||||
{
|
||||
id: firstMessageId,
|
||||
role: 'user',
|
||||
content: '第一条',
|
||||
createdAt: 1,
|
||||
state: 'complete'
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
id: secondConversationId,
|
||||
title: '第二对话',
|
||||
updatedAt: 2,
|
||||
messages: [
|
||||
{
|
||||
id: secondMessageId,
|
||||
role: 'assistant',
|
||||
content: '第二条',
|
||||
createdAt: 2,
|
||||
state: 'complete'
|
||||
}
|
||||
]
|
||||
}
|
||||
])
|
||||
|
||||
expect(() =>
|
||||
database.saveLocalConversations([
|
||||
{
|
||||
header: {
|
||||
id: firstConversationId,
|
||||
title: '不应提交的标题',
|
||||
updatedAt: 3
|
||||
},
|
||||
messages: [
|
||||
{
|
||||
id: rolledBackMessageId,
|
||||
role: 'user',
|
||||
content: '不应提交',
|
||||
createdAt: 3,
|
||||
state: 'complete'
|
||||
},
|
||||
{
|
||||
id: secondMessageId,
|
||||
role: 'assistant',
|
||||
content: '错误归属',
|
||||
createdAt: 2,
|
||||
state: 'complete'
|
||||
}
|
||||
]
|
||||
}
|
||||
])
|
||||
).toThrow('消息 ID 已属于其他对话')
|
||||
expect(database.getConversation(firstConversationId)).toMatchObject({
|
||||
title: '第一对话',
|
||||
messages: [{ id: firstMessageId }]
|
||||
})
|
||||
|
||||
expect(() =>
|
||||
database.saveLocalConversations([
|
||||
{
|
||||
header: {
|
||||
id: firstConversationId,
|
||||
title: '仍不应提交的标题',
|
||||
updatedAt: 4
|
||||
},
|
||||
messages: [
|
||||
{
|
||||
id: firstMessageId,
|
||||
role: 'assistant',
|
||||
content: '错误角色',
|
||||
createdAt: 1,
|
||||
state: 'complete'
|
||||
}
|
||||
]
|
||||
}
|
||||
])
|
||||
).toThrow('消息角色不能更改')
|
||||
expect(database.getConversation(firstConversationId)).toMatchObject({
|
||||
title: '第一对话',
|
||||
messages: [
|
||||
{
|
||||
id: firstMessageId,
|
||||
role: 'user',
|
||||
content: '第一条'
|
||||
}
|
||||
]
|
||||
})
|
||||
database.close()
|
||||
})
|
||||
|
||||
it('explicitly deletes only local conversations and cascades messages', async () => {
|
||||
const database = await createDatabase()
|
||||
const localId = '00000000-0000-4000-8000-000000000521'
|
||||
database.replaceConversations([
|
||||
{
|
||||
id: localId,
|
||||
title: '待删除本地对话',
|
||||
updatedAt: 1,
|
||||
messages: [
|
||||
{
|
||||
id: '00000000-0000-4000-8000-000000000522',
|
||||
role: 'user',
|
||||
content: '待删除消息',
|
||||
createdAt: 1,
|
||||
state: 'complete'
|
||||
}
|
||||
]
|
||||
}
|
||||
])
|
||||
const channelProject = database.ensureChannelProjects(
|
||||
'C:\\Users\\test',
|
||||
channelDefaultProfileId
|
||||
)[0]!
|
||||
const remote = database.getOrCreateRemoteConversation({
|
||||
projectId: channelProject.id,
|
||||
channel: 'weixin',
|
||||
accountId: 'default',
|
||||
externalConversationId: 'protected-delete',
|
||||
conversationType: 'direct',
|
||||
title: '受保护远程对话',
|
||||
accountDisplay: '发送者 ****0521'
|
||||
})
|
||||
database.appendRemoteConversationMessage({
|
||||
conversationId: remote.id,
|
||||
role: 'user',
|
||||
content: '远程消息'
|
||||
})
|
||||
|
||||
expect(database.deleteLocalConversation(localId)).toBe(true)
|
||||
expect(database.deleteLocalConversation(localId)).toBe(false)
|
||||
expect(() =>
|
||||
database.getConversation(localId)
|
||||
).toThrow('对话不存在')
|
||||
expect(() =>
|
||||
database.deleteLocalConversation(remote.id)
|
||||
).toThrow('远程对话不能作为本地对话删除')
|
||||
expect(() =>
|
||||
database.saveLocalConversations([
|
||||
{
|
||||
header: {
|
||||
id: remote.id,
|
||||
title: '冲突本地标题',
|
||||
updatedAt: 2
|
||||
},
|
||||
messages: []
|
||||
}
|
||||
])
|
||||
).toThrow('本地对话 ID 与远程对话冲突')
|
||||
expect(database.getConversation(remote.id)).toMatchObject({
|
||||
title: '受保护远程对话',
|
||||
messages: [{ content: '远程消息' }]
|
||||
})
|
||||
database.close()
|
||||
})
|
||||
|
||||
it('gets a targeted conversation outside the latest 100', async () => {
|
||||
const database = await createDatabase()
|
||||
database.saveLocalConversations(
|
||||
Array.from({ length: 100 }, (_, index) => ({
|
||||
header: {
|
||||
id: `00000000-0000-4000-8000-${String(index + 1).padStart(12, '0')}`,
|
||||
title: `较新对话 ${index}`,
|
||||
updatedAt: index + 2
|
||||
},
|
||||
messages: []
|
||||
}))
|
||||
)
|
||||
const oldestId = '00000000-0000-4000-8000-000000000999'
|
||||
database.saveLocalConversations([
|
||||
{
|
||||
header: {
|
||||
id: oldestId,
|
||||
title: '第 101 个对话',
|
||||
updatedAt: 1
|
||||
},
|
||||
messages: []
|
||||
}
|
||||
])
|
||||
|
||||
expect(database.listConversations()).toHaveLength(100)
|
||||
expect(
|
||||
database.listConversations().some(
|
||||
(conversation) => conversation.id === oldestId
|
||||
)
|
||||
).toBe(false)
|
||||
expect(database.getConversation(oldestId)).toMatchObject({
|
||||
id: oldestId,
|
||||
title: '第 101 个对话',
|
||||
messages: []
|
||||
})
|
||||
database.close()
|
||||
})
|
||||
|
||||
it('repairs unattended channel selections without rebinding ordinary conversations', async () => {
|
||||
const database = await createDatabase()
|
||||
const removedProfileId =
|
||||
|
||||
@@ -14,6 +14,7 @@ import type {
|
||||
AssistantProject,
|
||||
AssistantSchedule,
|
||||
AssistantTask,
|
||||
ConversationMessage,
|
||||
ConversationSnapshot,
|
||||
ExpertCreateInput,
|
||||
ExpertUpdateInput,
|
||||
@@ -21,6 +22,7 @@ import type {
|
||||
HeartbeatSummaryOutput,
|
||||
HeartbeatUpdateInput,
|
||||
LegacyWorkMode,
|
||||
LocalConversationSaveBatch,
|
||||
MemoryCreateInput,
|
||||
ModelUsageCallInput,
|
||||
ProjectChannel,
|
||||
@@ -749,6 +751,80 @@ function interruptActiveToolBlocks(
|
||||
)
|
||||
}
|
||||
|
||||
function toConversationSnapshot(
|
||||
conversation: ConversationRow,
|
||||
messages: MessageRow[]
|
||||
): ConversationSnapshot {
|
||||
return {
|
||||
id: conversation.id,
|
||||
projectId: conversation.project_id ?? undefined,
|
||||
runtimeSelection: parseRuntimeSelection(
|
||||
conversation.runtime_selection_json
|
||||
),
|
||||
knowledgeRetrievalMode:
|
||||
conversation.knowledge_retrieval_mode ?? undefined,
|
||||
...(conversation.channel &&
|
||||
conversation.conversation_type &&
|
||||
conversation.account_display
|
||||
? {
|
||||
remote: {
|
||||
channel: conversation.channel,
|
||||
accountDisplay: conversation.account_display,
|
||||
conversationType: conversation.conversation_type
|
||||
}
|
||||
}
|
||||
: {}),
|
||||
title: conversation.title,
|
||||
updatedAt: Date.parse(conversation.updated_at),
|
||||
messages: messages.map((message) => {
|
||||
const metadata = JSON.parse(
|
||||
message.metadata_json
|
||||
) as MessageMetadata
|
||||
const interrupted = message.state === 'streaming'
|
||||
return {
|
||||
id: message.id,
|
||||
role: message.role,
|
||||
content: message.content,
|
||||
reasoning: metadata.reasoning,
|
||||
blocks: interrupted
|
||||
? interruptActiveToolBlocks(metadata.blocks)
|
||||
: metadata.blocks,
|
||||
createdAt:
|
||||
metadata.createdAt ?? Date.parse(message.created_at),
|
||||
state: interrupted ? ('error' as const) : message.state,
|
||||
status: interrupted
|
||||
? interruptedMessageStatus
|
||||
: metadata.status,
|
||||
tools: interrupted
|
||||
? interruptActiveTools(metadata.tools)
|
||||
: metadata.tools,
|
||||
sources: metadata.sources,
|
||||
sourceReferences: metadata.sourceReferences,
|
||||
knowledgeRetrieval: metadata.knowledgeRetrieval,
|
||||
artifactIds: metadata.artifactIds,
|
||||
attachments: metadata.attachments
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
function serializeConversationMessageMetadata(
|
||||
message: ConversationMessage
|
||||
): string {
|
||||
return JSON.stringify({
|
||||
createdAt: message.createdAt,
|
||||
status: message.status,
|
||||
reasoning: message.reasoning,
|
||||
blocks: message.blocks,
|
||||
tools: message.tools,
|
||||
sources: message.sources,
|
||||
sourceReferences: message.sourceReferences,
|
||||
knowledgeRetrieval: message.knowledgeRetrieval,
|
||||
artifactIds: message.artifactIds,
|
||||
attachments: message.attachments
|
||||
})
|
||||
}
|
||||
|
||||
export class AssistantDatabase {
|
||||
private database?: DatabaseSync
|
||||
private channelEventWrites = 0
|
||||
@@ -1279,69 +1355,45 @@ export class AssistantDatabase {
|
||||
)
|
||||
ORDER BY sequence ASC`
|
||||
)
|
||||
return conversations.map((conversation) => ({
|
||||
id: conversation.id,
|
||||
projectId: conversation.project_id ?? undefined,
|
||||
runtimeSelection: parseRuntimeSelection(
|
||||
conversation.runtime_selection_json
|
||||
),
|
||||
knowledgeRetrievalMode:
|
||||
conversation.knowledge_retrieval_mode ?? undefined,
|
||||
...(conversation.channel &&
|
||||
conversation.conversation_type &&
|
||||
conversation.account_display
|
||||
? {
|
||||
remote: {
|
||||
channel: conversation.channel,
|
||||
accountDisplay: conversation.account_display,
|
||||
conversationType: conversation.conversation_type
|
||||
}
|
||||
}
|
||||
: {}),
|
||||
title: conversation.title,
|
||||
updatedAt: Date.parse(conversation.updated_at),
|
||||
messages: (
|
||||
return conversations.map((conversation) =>
|
||||
toConversationSnapshot(
|
||||
conversation,
|
||||
messageStatement.all(conversation.id) as MessageRow[]
|
||||
).map((message) => {
|
||||
const metadata = JSON.parse(
|
||||
message.metadata_json
|
||||
) as MessageMetadata
|
||||
const interrupted = message.state === 'streaming'
|
||||
return {
|
||||
id: message.id,
|
||||
role: message.role,
|
||||
content: message.content,
|
||||
reasoning: metadata.reasoning,
|
||||
blocks: interrupted
|
||||
? interruptActiveToolBlocks(metadata.blocks)
|
||||
: metadata.blocks,
|
||||
createdAt:
|
||||
metadata.createdAt ?? Date.parse(message.created_at),
|
||||
state: interrupted ? ('error' as const) : message.state,
|
||||
status: interrupted
|
||||
? interruptedMessageStatus
|
||||
: metadata.status,
|
||||
tools: interrupted
|
||||
? interruptActiveTools(metadata.tools)
|
||||
: metadata.tools,
|
||||
sources: metadata.sources,
|
||||
sourceReferences: metadata.sourceReferences,
|
||||
knowledgeRetrieval: metadata.knowledgeRetrieval,
|
||||
artifactIds: metadata.artifactIds,
|
||||
attachments: metadata.attachments
|
||||
}
|
||||
})
|
||||
}))
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
getConversation(conversationId: string): ConversationSnapshot {
|
||||
const conversation = this.listConversations().find(
|
||||
(candidate) => candidate.id === conversationId
|
||||
)
|
||||
const database = this.requireDatabase()
|
||||
const conversation = database
|
||||
.prepare(
|
||||
`SELECT id, project_id, runtime_selection_json,
|
||||
knowledge_retrieval_mode, title, channel,
|
||||
external_account_id, external_conversation_id,
|
||||
conversation_type, account_display, updated_at
|
||||
FROM conversations
|
||||
WHERE id = ? AND status = 'active'`
|
||||
)
|
||||
.get(conversationId) as ConversationRow | undefined
|
||||
if (!conversation) {
|
||||
throw new Error('对话不存在')
|
||||
}
|
||||
return conversation
|
||||
const messages = database
|
||||
.prepare(
|
||||
`SELECT id, conversation_id, role, content, state, metadata_json,
|
||||
created_at
|
||||
FROM (
|
||||
SELECT id, conversation_id, role, content, state,
|
||||
metadata_json, created_at, sequence
|
||||
FROM messages
|
||||
WHERE conversation_id = ?
|
||||
ORDER BY sequence DESC
|
||||
LIMIT 500
|
||||
)
|
||||
ORDER BY sequence ASC`
|
||||
)
|
||||
.all(conversationId) as MessageRow[]
|
||||
return toConversationSnapshot(conversation, messages)
|
||||
}
|
||||
|
||||
repairConversationRuntimeSelections(
|
||||
@@ -1502,6 +1554,167 @@ export class AssistantDatabase {
|
||||
}
|
||||
}
|
||||
|
||||
saveLocalConversations(batch: LocalConversationSaveBatch): void {
|
||||
const database = this.requireDatabase()
|
||||
const findConversation = database.prepare(
|
||||
'SELECT channel FROM conversations WHERE id = ?'
|
||||
)
|
||||
const insertConversation = database.prepare(
|
||||
`INSERT INTO conversations
|
||||
(id, project_id, runtime_selection_json, knowledge_retrieval_mode,
|
||||
work_mode, title, status, created_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, 'ask', ?, 'active', ?, ?)`
|
||||
)
|
||||
const updateConversation = database.prepare(
|
||||
`UPDATE conversations
|
||||
SET project_id = ?, runtime_selection_json = ?,
|
||||
knowledge_retrieval_mode = ?, title = ?, status = 'active',
|
||||
updated_at = ?
|
||||
WHERE id = ? AND channel IS NULL`
|
||||
)
|
||||
const findMessage = database.prepare(
|
||||
`SELECT conversation_id, role
|
||||
FROM messages
|
||||
WHERE id = ?`
|
||||
)
|
||||
const nextSequence = database.prepare(
|
||||
`SELECT COALESCE(MAX(sequence), -1) + 1 AS sequence
|
||||
FROM messages
|
||||
WHERE conversation_id = ?`
|
||||
)
|
||||
const insertMessage = database.prepare(
|
||||
`INSERT INTO messages
|
||||
(id, conversation_id, request_id, role, content, state, sequence,
|
||||
metadata_json, created_at)
|
||||
VALUES (?, ?, NULL, ?, ?, ?, ?, ?, ?)`
|
||||
)
|
||||
const updateMessage = database.prepare(
|
||||
`UPDATE messages
|
||||
SET content = ?, state = ?, metadata_json = ?
|
||||
WHERE id = ?`
|
||||
)
|
||||
const trimMessages = database.prepare(
|
||||
`DELETE FROM messages
|
||||
WHERE id IN (
|
||||
SELECT id
|
||||
FROM messages
|
||||
WHERE conversation_id = ?
|
||||
ORDER BY sequence DESC
|
||||
LIMIT -1 OFFSET 500
|
||||
)`
|
||||
)
|
||||
|
||||
database.exec('BEGIN IMMEDIATE')
|
||||
try {
|
||||
for (const save of batch) {
|
||||
const { header } = save
|
||||
const existingConversation = findConversation.get(
|
||||
header.id
|
||||
) as { channel: ProjectChannel | null } | undefined
|
||||
const updatedAt = new Date(header.updatedAt).toISOString()
|
||||
if (existingConversation?.channel) {
|
||||
throw new Error('本地对话 ID 与远程对话冲突')
|
||||
}
|
||||
if (existingConversation) {
|
||||
const result = updateConversation.run(
|
||||
header.projectId ?? null,
|
||||
header.runtimeSelection
|
||||
? JSON.stringify(header.runtimeSelection)
|
||||
: null,
|
||||
header.knowledgeRetrievalMode ?? null,
|
||||
header.title,
|
||||
updatedAt,
|
||||
header.id
|
||||
)
|
||||
if (result.changes !== 1) {
|
||||
throw new Error('无法更新本地对话')
|
||||
}
|
||||
} else {
|
||||
insertConversation.run(
|
||||
header.id,
|
||||
header.projectId ?? null,
|
||||
header.runtimeSelection
|
||||
? JSON.stringify(header.runtimeSelection)
|
||||
: null,
|
||||
header.knowledgeRetrievalMode ?? null,
|
||||
header.title,
|
||||
updatedAt,
|
||||
updatedAt
|
||||
)
|
||||
}
|
||||
|
||||
let sequence = (
|
||||
nextSequence.get(header.id) as { sequence: number }
|
||||
).sequence
|
||||
let insertedMessage = false
|
||||
for (const message of save.messages) {
|
||||
const existingMessage = findMessage.get(message.id) as
|
||||
| {
|
||||
conversation_id: string
|
||||
role: MessageRow['role']
|
||||
}
|
||||
| undefined
|
||||
if (existingMessage) {
|
||||
if (existingMessage.conversation_id !== header.id) {
|
||||
throw new Error('消息 ID 已属于其他对话')
|
||||
}
|
||||
if (existingMessage.role !== message.role) {
|
||||
throw new Error('消息角色不能更改')
|
||||
}
|
||||
updateMessage.run(
|
||||
message.content,
|
||||
message.state,
|
||||
serializeConversationMessageMetadata(message),
|
||||
message.id
|
||||
)
|
||||
continue
|
||||
}
|
||||
insertMessage.run(
|
||||
message.id,
|
||||
header.id,
|
||||
message.role,
|
||||
message.content,
|
||||
message.state,
|
||||
sequence,
|
||||
serializeConversationMessageMetadata(message),
|
||||
new Date(message.createdAt).toISOString()
|
||||
)
|
||||
sequence += 1
|
||||
insertedMessage = true
|
||||
}
|
||||
if (insertedMessage) {
|
||||
trimMessages.run(header.id)
|
||||
}
|
||||
}
|
||||
database.exec('COMMIT')
|
||||
} catch (error) {
|
||||
database.exec('ROLLBACK')
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
deleteLocalConversation(conversationId: string): boolean {
|
||||
const database = this.requireDatabase()
|
||||
const conversation = database
|
||||
.prepare('SELECT channel FROM conversations WHERE id = ?')
|
||||
.get(conversationId) as
|
||||
| { channel: ProjectChannel | null }
|
||||
| undefined
|
||||
if (!conversation) {
|
||||
return false
|
||||
}
|
||||
if (conversation.channel) {
|
||||
throw new Error('远程对话不能作为本地对话删除')
|
||||
}
|
||||
return (
|
||||
database
|
||||
.prepare(
|
||||
'DELETE FROM conversations WHERE id = ? AND channel IS NULL'
|
||||
)
|
||||
.run(conversationId).changes === 1
|
||||
)
|
||||
}
|
||||
|
||||
getOrCreateRemoteConversation(input: {
|
||||
projectId: string
|
||||
channel: ProjectChannel
|
||||
|
||||
Reference in New Issue
Block a user