( query: string, maxResults: number = 5, )
| 270 | * @returns Array of matching log entries with similarity scores |
| 271 | */ |
| 272 | export async function searchLogEntries( |
| 273 | query: string, |
| 274 | maxResults: number = 5, |
| 275 | ): Promise<VectorSearchResult[]> { |
| 276 | try { |
| 277 | // If OpenAI client is not available, use mock implementation |
| 278 | if (!openai) { |
| 279 | console.debug("Vector store: Searching for log entries (mock)", query); |
| 280 | return []; |
| 281 | } |
| 282 | |
| 283 | // Ensure vector store is initialized |
| 284 | const storeId = await initializeVectorStore(); |
| 285 | |
| 286 | // Search the vector store |
| 287 | const searchResponse = await openai.vectorStores.search(storeId, { |
| 288 | query, |
| 289 | max_num_results: maxResults, |
| 290 | }); |
| 291 | |
| 292 | // Transform the results into the expected format |
| 293 | const results: VectorSearchResult[] = searchResponse.data.map((result: any) => { |
| 294 | // Extract log entry information from the content |
| 295 | const content = result.content[0]?.text || ""; |
| 296 | |
| 297 | // Parse the content to extract log entry information |
| 298 | const timestampMatch = content.match(/Log Entry \((.*?)\)/); |
| 299 | const levelMatch = content.match(/Level: (.*?)$/m); |
| 300 | const messageMatch = content.match(/Message: (.*?)$/m); |
| 301 | const metadataMatch = content.match(/Metadata: ([\s\S]*?)$/); |
| 302 | |
| 303 | const timestamp = timestampMatch ? timestampMatch[1] : new Date().toISOString(); |
| 304 | const level = levelMatch ? levelMatch[1] : "info"; |
| 305 | const message = messageMatch ? messageMatch[1] : ""; |
| 306 | const metadata = metadataMatch ? JSON.parse(metadataMatch[1]) : {}; |
| 307 | |
| 308 | return { |
| 309 | entry: { |
| 310 | timestamp, |
| 311 | level, |
| 312 | message, |
| 313 | metadata, |
| 314 | } as LogEntry, |
| 315 | score: result.score || 0, |
| 316 | }; |
| 317 | }); |
| 318 | |
| 319 | console.debug("Searched for log entries", { |
| 320 | query, |
| 321 | maxResults, |
| 322 | resultsCount: results.length, |
| 323 | }); |
| 324 | |
| 325 | return results; |
| 326 | } catch (error: unknown) { |
| 327 | console.debug("Failed to search log entries, using mock", { error: String(error) }); |
| 328 | console.debug("Vector store: Searching for log entries (mock)", query); |
| 329 | return []; |
no test coverage detected