(id: string)
| 37 | |
| 38 | // 删除会话及其消息和附件 |
| 39 | async deleteConversation(id: string): Promise<void> { |
| 40 | // 清理会话关联的附件 |
| 41 | const messages = await this.getMessages(id); |
| 42 | const attachmentIds: string[] = []; |
| 43 | for (const msg of messages) { |
| 44 | // 扫描 toolCalls 中的附件 |
| 45 | if (msg.toolCalls) { |
| 46 | for (const tc of msg.toolCalls) { |
| 47 | if (tc.attachments) { |
| 48 | for (const att of tc.attachments) { |
| 49 | attachmentIds.push(att.id); |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | // 扫描 ContentBlock[] 中的附件 |
| 55 | if (Array.isArray(msg.content)) { |
| 56 | for (const block of msg.content) { |
| 57 | if (block.type !== "text" && "attachmentId" in block) { |
| 58 | attachmentIds.push(block.attachmentId); |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | if (attachmentIds.length > 0) { |
| 64 | await this.deleteAttachments(attachmentIds); |
| 65 | } |
| 66 | |
| 67 | const conversations = await this.readJsonFile<Conversation[]>(CONVERSATIONS_FILE, []); |
| 68 | const filtered = conversations.filter((c) => c.id !== id); |
| 69 | await this.writeJsonFile(CONVERSATIONS_FILE, filtered); |
| 70 | // 删除对应消息文件 |
| 71 | const messagesDir = await this.getChildDir(MESSAGES_DIR); |
| 72 | await this.deleteFile(`${id}.json`, messagesDir); |
| 73 | // 删除关联的任务数据 |
| 74 | await this.deleteTasks(id).catch(() => {}); |
| 75 | } |
| 76 | |
| 77 | // 获取指定会话的所有消息 |
| 78 | async getMessages(conversationId: string): Promise<ChatMessage[]> { |
no test coverage detected