MCPcopy Create free account
hub / github.com/Noumena-Network/code / loadLogList

Function loadLogList

src/utils/log.ts:232–284  ·  view source on GitHub ↗

* Internal function to load and process logs from a specified path * @param path Directory containing logs * @returns Array of logs sorted by date * @private

(path: string)

Source from the content-addressed store, hash-verified

230 * @private
231 */
232async function loadLogList(path: string): Promise<LogOption[]> {
233 let files: Awaited<ReturnType<typeof readdir>>
234 try {
235 files = await readdir(path, { withFileTypes: true })
236 } catch {
237 logError(new Error(`No logs found at ${path}`))
238 return []
239 }
240 const logData = await Promise.all(
241 files.map(async (file, i) => {
242 const fullPath = join(path, file.name)
243 const content = await readFile(fullPath, { encoding: 'utf8' })
244 const messages = jsonParse(content) as SerializedMessage[]
245 const firstMessage = messages[0]
246 const lastMessage = messages[messages.length - 1]
247 const firstPrompt =
248 firstMessage?.type === 'user' &&
249 typeof firstMessage?.message?.content === 'string'
250 ? firstMessage?.message?.content
251 : 'No prompt'
252
253 // For new random filenames, we'll get stats from the file itself
254 const fileStats = await stat(fullPath)
255
256 // Check if it's a sidechain by looking at filename
257 const isSidechain = fullPath.includes('sidechain')
258
259 // For new files, use the file modified time as date
260 const date = dateToFilename(fileStats.mtime)
261
262 return {
263 date,
264 fullPath,
265 messages,
266 value: i, // hack: overwritten after sorting, right below this
267 created: parseISOString(firstMessage?.timestamp || date),
268 modified: lastMessage?.timestamp
269 ? parseISOString(lastMessage.timestamp)
270 : parseISOString(date),
271 firstPrompt:
272 firstPrompt.split('\n')[0]?.slice(0, 50) +
273 (firstPrompt.length > 50 ? '…' : '') || 'No prompt',
274 messageCount: messages.length,
275 isSidechain,
276 }
277 }),
278 )
279
280 return sortLogs(logData.filter(_ => _ !== null)).map((_, i) => ({
281 ..._,
282 value: i,
283 }))
284}
285
286function parseISOString(s: string): Date {
287 const b = s.split(/\D+/)

Callers 1

loadErrorLogsFunction · 0.85

Calls 8

readdirFunction · 0.85
readFileFunction · 0.85
jsonParseFunction · 0.85
statFunction · 0.85
dateToFilenameFunction · 0.85
parseISOStringFunction · 0.85
sortLogsFunction · 0.85
logErrorFunction · 0.70

Tested by

no test coverage detected