|
| 1 | +import OpenAI from "openai"; |
| 2 | +import path from "path"; |
| 3 | +import fs from "fs"; |
| 4 | +import { getFileErrors } from "./translator"; |
| 5 | +import { translationSummaryPrefix } from "../config"; |
| 6 | + |
| 7 | +// Function to save summary log - can be called from signal handlers |
| 8 | +export async function saveSummaryLog( |
| 9 | + xmlFiles: string[], |
| 10 | + failures: { file: string; error: any }[], |
| 11 | + translateNum: number, |
| 12 | + failureCount: number, |
| 13 | + successCount: number |
| 14 | +) { |
| 15 | + try { |
| 16 | + const ai = new OpenAI({ |
| 17 | + apiKey: process.env.API_KEY, |
| 18 | + baseURL: process.env.AI_BASEURL |
| 19 | + }); |
| 20 | + |
| 21 | + // list and delete all assistants |
| 22 | + const assistants = await ai.beta.assistants.list({ limit: 100 }); |
| 23 | + const failedDel: string[] = []; |
| 24 | + await Promise.all( |
| 25 | + assistants.data.map(async assistant => { |
| 26 | + try { |
| 27 | + await ai.beta.assistants.del(assistant.id); |
| 28 | + } catch (error) { |
| 29 | + failedDel.push(assistant.id); |
| 30 | + } |
| 31 | + }) |
| 32 | + ).then(() => console.log("successfully removed all assistants")); |
| 33 | + |
| 34 | + // list and delete all uploaded files |
| 35 | + const files = await ai.files.list(); |
| 36 | + await Promise.all( |
| 37 | + files.data.map(async file => { |
| 38 | + try { |
| 39 | + await ai.files.del(file.id); |
| 40 | + } catch (error) { |
| 41 | + failedDel.push(file.id); |
| 42 | + } |
| 43 | + }) |
| 44 | + ).then(() => console.log("successfully deleted all files")); |
| 45 | + |
| 46 | + const timestamp = new Date().toISOString().replace(/[:.]/g, "-"); |
| 47 | + let summaryLog = ` |
| 48 | +Translation Summary (${timestamp}) |
| 49 | +================================ |
| 50 | +Total files scanned: ${xmlFiles.length} |
| 51 | +Files needing translation: ${translateNum} |
| 52 | +Successfully translated: ${successCount} |
| 53 | +Failed translations: ${failureCount} |
| 54 | +Success rate: ${translateNum > 0 ? ((successCount / translateNum) * 100).toFixed(2) : 0}% |
| 55 | +`; |
| 56 | + |
| 57 | + if (failedDel.length > 0) { |
| 58 | + summaryLog += `\nFailed to remove ${failedDel.length} assistants\n`; |
| 59 | + failedDel.forEach( |
| 60 | + (assistant, index) => (summaryLog += `${index + 1}. ${assistant}\n`) |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + // Add failed translations to the log |
| 65 | + if (failures.length > 0) { |
| 66 | + summaryLog += `\nFailed Translations (High-level errors):\n`; |
| 67 | + failures.forEach((failure, index) => { |
| 68 | + summaryLog += `${index + 1}. ${failure.file}\n Error: ${failure.error}\n\n`; |
| 69 | + }); |
| 70 | + } |
| 71 | + |
| 72 | + // Add detailed errors captured during translation process |
| 73 | + const fileErrors = getFileErrors(); |
| 74 | + if (Object.keys(fileErrors).length > 0) { |
| 75 | + failureCount = Object.keys(fileErrors).length; |
| 76 | + summaryLog += `\nDetailed Translation Errors:\n`; |
| 77 | + summaryLog += `============================\n`; |
| 78 | + |
| 79 | + for (const [filePath, errors] of Object.entries(fileErrors)) { |
| 80 | + summaryLog += `\nFile: ${filePath}\n`; |
| 81 | + errors.forEach((error, index) => { |
| 82 | + summaryLog += ` ${index + 1}. ${error.error}\n`; |
| 83 | + if (error.error) { |
| 84 | + // Format the error object/message for better readability |
| 85 | + const errorStr = |
| 86 | + typeof error.error === "object" |
| 87 | + ? JSON.stringify(error.error, null, 2).substring(0, 500) // Limit very long errors |
| 88 | + : String(error.error); |
| 89 | + summaryLog += ` Details: ${errorStr}\n`; |
| 90 | + } |
| 91 | + }); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + const logDir = path.resolve(__dirname, "../logs"); |
| 96 | + if (!fs.existsSync(logDir)) { |
| 97 | + fs.mkdirSync(logDir, { recursive: true }); |
| 98 | + } |
| 99 | + |
| 100 | + const logPath = path.join( |
| 101 | + logDir, |
| 102 | + `${translationSummaryPrefix}-${timestamp}.log` |
| 103 | + ); |
| 104 | + fs.writeFileSync(logPath, summaryLog); |
| 105 | + console.log( |
| 106 | + `Summary log saved to logs/translation-summary-${timestamp}.log` |
| 107 | + ); |
| 108 | + } catch (logError) { |
| 109 | + console.error("Failed to save log file:", logError); |
| 110 | + } |
| 111 | +} |
0 commit comments