MCPcopy Index your code
hub / github.com/CodebuffAI/codebuff / getAllChats

Function getAllChats

cli/src/utils/chat-history.ts:48–124  ·  view source on GitHub ↗
(maxChats: number = 500)

Source from the content-addressed store, hash-verified

46 * @param maxChats - Maximum number of chats to load (default: 500)
47 */
48export function getAllChats(maxChats: number = 500): ChatHistoryEntry[] {
49 try {
50 const chatsDir = getChatsDir()
51
52 if (!fs.existsSync(chatsDir)) {
53 return []
54 }
55
56 const chatDirs = fs.readdirSync(chatsDir)
57
58 // First pass: get mtime for all chat directories (fast, no file reading)
59 const chatDirInfos: ChatDirInfo[] = []
60 for (const chatId of chatDirs) {
61 const chatPath = path.join(chatsDir, chatId)
62 try {
63 const stat = fs.statSync(chatPath)
64 if (!stat.isDirectory()) continue
65
66 chatDirInfos.push({
67 chatId,
68 chatPath,
69 messagesPath: path.join(chatPath, 'chat-messages.json'),
70 mtime: stat.mtime,
71 })
72 } catch {
73 // Skip directories we can't stat
74 }
75 }
76
77 // Sort by mtime first (most recent first)
78 chatDirInfos.sort((a, b) => b.mtime.getTime() - a.mtime.getTime())
79
80 // Second pass: only read message content for the top N chats
81 const chats: ChatHistoryEntry[] = []
82 const chatsToLoad = chatDirInfos.slice(0, maxChats)
83
84 for (const info of chatsToLoad) {
85 try {
86 let messageCount = 0
87 let lastPrompt = '(empty chat)'
88
89 if (fs.existsSync(info.messagesPath)) {
90 const content = fs.readFileSync(info.messagesPath, 'utf8')
91 const messages = JSON.parse(content) as ChatMessage[]
92 messageCount = messages.length
93 lastPrompt = getFirstUserPrompt(messages)
94 }
95
96 // Skip empty chats (no messages)
97 if (messageCount > 0) {
98 chats.push({
99 chatId: info.chatId,
100 lastPrompt,
101 timestamp: info.mtime,
102 messageCount,
103 })
104 }
105 } catch (error) {

Callers 2

ChatHistoryScreenFunction · 0.90

Calls 3

getChatsDirFunction · 0.85
getFirstUserPromptFunction · 0.85
parseMethod · 0.80

Tested by

no test coverage detected