(
aiChatId: string,
role: AIMessageRole,
content: string,
dataKeywords?: string[],
dataMessageCount?: number,
contentBlocks?: ContentBlock[],
tokenUsage?: TokenUsageData
)
| 585 | // ==================== 消息管理 ==================== |
| 586 | |
| 587 | addMessage( |
| 588 | aiChatId: string, |
| 589 | role: AIMessageRole, |
| 590 | content: string, |
| 591 | dataKeywords?: string[], |
| 592 | dataMessageCount?: number, |
| 593 | contentBlocks?: ContentBlock[], |
| 594 | tokenUsage?: TokenUsageData |
| 595 | ): AIMessage { |
| 596 | const db = this.getDb() |
| 597 | const now = Math.floor(Date.now() / 1000) |
| 598 | const id = this.generateId('msg') |
| 599 | const parentId = this.getActiveMessageId(aiChatId) |
| 600 | const siblingGroupId = id |
| 601 | const branchIndex = 0 |
| 602 | |
| 603 | const pendingDebug = role === 'assistant' ? this.pendingDebugContextMap.get(aiChatId) : undefined |
| 604 | if (pendingDebug) { |
| 605 | this.pendingDebugContextMap.delete(aiChatId) |
| 606 | } |
| 607 | |
| 608 | db.prepare( |
| 609 | `INSERT INTO ai_message ( |
| 610 | id, ai_chat_id, role, content, timestamp, data_keywords, data_message_count, |
| 611 | content_blocks, token_usage, debug_context, parent_id, sibling_group_id, branch_index |
| 612 | ) |
| 613 | VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)` |
| 614 | ).run( |
| 615 | id, |
| 616 | aiChatId, |
| 617 | role, |
| 618 | content, |
| 619 | now, |
| 620 | dataKeywords ? JSON.stringify(dataKeywords) : null, |
| 621 | dataMessageCount ?? null, |
| 622 | contentBlocks ? JSON.stringify(contentBlocks) : null, |
| 623 | tokenUsage ? JSON.stringify(tokenUsage) : null, |
| 624 | pendingDebug ?? null, |
| 625 | parentId, |
| 626 | siblingGroupId, |
| 627 | branchIndex |
| 628 | ) |
| 629 | |
| 630 | db.prepare('UPDATE ai_chat SET active_message_id = ?, updated_at = ? WHERE id = ?').run(id, now, aiChatId) |
| 631 | |
| 632 | return { |
| 633 | id, |
| 634 | aiChatId, |
| 635 | role, |
| 636 | content, |
| 637 | timestamp: now, |
| 638 | parentId, |
| 639 | dataKeywords, |
| 640 | dataMessageCount, |
| 641 | contentBlocks, |
| 642 | tokenUsage, |
| 643 | } |
| 644 | } |
no test coverage detected