(entry: LogEntry)
| 97 | * @param entry Log entry to store |
| 98 | */ |
| 99 | export async function vectorStoreLog(entry: LogEntry): Promise<void> { |
| 100 | try { |
| 101 | // If OpenAI client is not available, use mock implementation |
| 102 | if (!openai) { |
| 103 | console.debug("Vector store: Storing log entry (mock)", entry.message, entry.level); |
| 104 | return; |
| 105 | } |
| 106 | |
| 107 | // Ensure vector store is initialized |
| 108 | const storeId = await initializeVectorStore(); |
| 109 | |
| 110 | // Create a text file with the log entry |
| 111 | const timestamp = new Date().toISOString(); |
| 112 | const content = `Log Entry (${timestamp}) |
| 113 | Level: ${entry.level} |
| 114 | Message: ${entry.message} |
| 115 | Metadata: ${JSON.stringify(entry.metadata || {}, null, 2)}`; |
| 116 | |
| 117 | const file = new File( |
| 118 | [content], |
| 119 | `log-${Date.now()}.txt`, |
| 120 | { type: "text/plain" }, |
| 121 | ); |
| 122 | |
| 123 | // Upload the file to OpenAI |
| 124 | const uploadedFile = await openai.files.create({ |
| 125 | file, |
| 126 | purpose: "assistants", |
| 127 | }); |
| 128 | |
| 129 | // Add the file to the vector store |
| 130 | await openai.vectorStores.files.create(storeId, { |
| 131 | file_id: uploadedFile.id, |
| 132 | }); |
| 133 | |
| 134 | console.debug("Log entry stored in vector database", { |
| 135 | timestamp: entry.timestamp, |
| 136 | level: entry.level, |
| 137 | message: entry.message.substring(0, 50) + (entry.message.length > 50 ? "..." : ""), |
| 138 | }); |
| 139 | } catch (error: unknown) { |
| 140 | console.debug("Failed to store log entry in vector database, using mock", { |
| 141 | error: String(error), |
| 142 | }); |
| 143 | console.debug("Vector store: Storing log entry (mock)", entry.message, entry.level); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Index a diff entry in the vector database |
no test coverage detected