UNPKG

zephyr-agent

Version:
199 lines (198 loc) • 8.67 kB
"use strict"; var __webpack_modules__ = { "@toon-format/toon" (module) { module.exports = require("@toon-format/toon"); } }; var __webpack_module_cache__ = {}; function __webpack_require__(moduleId) { var cachedModule = __webpack_module_cache__[moduleId]; if (void 0 !== cachedModule) return cachedModule.exports; var module = __webpack_module_cache__[moduleId] = { exports: {} }; __webpack_modules__[moduleId](module, module.exports, __webpack_require__); return module.exports; } (()=>{ __webpack_require__.d = (exports1, getters, values)=>{ var define = (defs, kind)=>{ for(var key in defs)if (__webpack_require__.o(defs, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, { enumerable: true, [kind]: defs[key] }); }; define(getters, "get"); define(values, "value"); }; })(); (()=>{ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop); })(); (()=>{ __webpack_require__.r = (exports1)=>{ if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, { value: 'Module' }); Object.defineProperty(exports1, '__esModule', { value: true }); }; })(); var __webpack_exports__ = {}; (()=>{ __webpack_require__.r(__webpack_exports__); __webpack_require__.d(__webpack_exports__, { getCurrentRunDir: ()=>getCurrentRunDir, getLogBasePath: ()=>getLogBasePath, getLogFormat: ()=>getLogFormat, initializeLogRun: ()=>initializeLogRun, isFileLoggingEnabled: ()=>isFileLoggingEnabled, resetLogRun: ()=>resetLogRun, writeLogToFile: ()=>writeLogToFile, writeRunSummary: ()=>writeRunSummary }); const external_node_fs_namespaceObject = require("node:fs"); const external_node_path_namespaceObject = require("node:path"); const external_zephyr_edge_contract_namespaceObject = require("zephyr-edge-contract"); const storage_keys_js_namespaceObject = require("../node-persist/storage-keys.js"); const redaction_js_namespaceObject = require("../security/redaction.js"); let currentRunDir = null; let runStartTime = null; function getLogBasePath() { const customPath = process.env['ZEPHYR_LOG_PATH']; if (customPath) return (0, external_node_path_namespaceObject.resolve)(customPath); return (0, external_node_path_namespaceObject.join)(storage_keys_js_namespaceObject.ZE_PATH, 'logs'); } function initializeLogRun() { if (!runStartTime) runStartTime = Date.now(); if (!currentRunDir) { const timestamp = new Date().toISOString().replace(/[:.]/g, '-').slice(0, -5); const runId = `run-${timestamp}`; const logBasePath = getLogBasePath(); currentRunDir = (0, external_node_path_namespaceObject.join)(logBasePath, runId); try { (0, external_node_fs_namespaceObject.mkdirSync)(currentRunDir, { recursive: true }); } catch (error) { console.error((0, redaction_js_namespaceObject.redactString)(`Failed to create log directory: ${String(error)}`)); } } return currentRunDir; } function getCurrentRunDir() { return currentRunDir; } function resetLogRun() { currentRunDir = null; runStartTime = null; } function extractStructuredData(message) { const jsonRegex = /\{(?:[^{}]|\{(?:[^{}]|\{[^{}]*\})*\})*\}/g; const matches = message.match(jsonRegex); if (matches && matches.length > 0) { const parsed = []; let allValid = true; for (const match of matches)try { parsed.push(JSON.parse(match)); } catch { allValid = false; break; } if (allValid && parsed.length > 0) { let cleanMessage = message.replace(jsonRegex, '').trim(); cleanMessage = cleanMessage.replace(/\s+/g, ' ').trim(); cleanMessage = cleanMessage.replace(/\s*(Response|Options|Payload|Data):\s*/gi, ' ').trim(); if (!cleanMessage) cleanMessage = 'Structured data'; const payload = 1 === parsed.length ? parsed[0] : parsed; return { message: cleanMessage, payload }; } } return { message }; } function writeLogToFile(logData) { const runDir = initializeLogRun(); const format = getLogFormat(); const extension = 'toon' === format ? 'toon' : 'log'; let filename; if (logData.action?.startsWith('debug:')) { const context = logData.action.replace('debug:', ''); filename = `debug-${context}.${extension}`; } else if (logData.action) { const actionName = logData.action.replace(/:/g, '-'); filename = `action-${actionName}.${extension}`; } else filename = `${logData.level}.${extension}`; const logFilePath = (0, external_node_path_namespaceObject.join)(runDir, filename); const cleanMsg = (0, redaction_js_namespaceObject.redactString)((0, external_zephyr_edge_contract_namespaceObject.stripAnsi)(logData.message)); const { message, payload } = extractStructuredData(cleanMsg); const logEntry = { level: logData.level, message, timestamp: logData.timestamp || Date.now() }; if (logData.action) logEntry['action'] = logData.action; if (payload) logEntry['payload'] = (0, redaction_js_namespaceObject.sanitizeForLogging)(payload); if (logData.data) logEntry['data'] = (0, redaction_js_namespaceObject.sanitizeForLogging)(logData.data); try { let logLine; if ('toon' === format) { const { encode } = __webpack_require__("@toon-format/toon"); logLine = encode((0, redaction_js_namespaceObject.sanitizeForLogging)(logEntry)) + '\n'; } else logLine = (0, redaction_js_namespaceObject.safeStringifyForLogging)(logEntry) + '\n'; (0, external_node_fs_namespaceObject.appendFileSync)(logFilePath, logLine, 'utf8'); } catch (error) { console.error((0, redaction_js_namespaceObject.redactString)(`Failed to write to log file: ${String(error)}`)); } } function writeRunSummary(data) { const runDir = getCurrentRunDir(); if (!runDir) return; const summaryPath = (0, external_node_path_namespaceObject.join)(runDir, 'summary.json'); const summary = { ...data, startTime: runStartTime ? new Date(runStartTime).toISOString() : null, endTime: new Date().toISOString(), duration: data.duration || (runStartTime ? Date.now() - runStartTime : 0) }; try { (0, external_node_fs_namespaceObject.appendFileSync)(summaryPath, (0, redaction_js_namespaceObject.safeStringifyForLogging)(summary, 2), 'utf8'); } catch (error) { console.error((0, redaction_js_namespaceObject.redactString)(`Failed to write summary file: ${String(error)}`)); } } function isFileLoggingEnabled() { return 'true' === process.env['ZEPHYR_LOG_TO_FILE'] || '1' === process.env['ZEPHYR_LOG_TO_FILE']; } function getLogFormat() { const format = process.env['ZEPHYR_LOG_FORMAT']; return 'toon' === format ? 'toon' : 'json'; } })(); exports.getCurrentRunDir = __webpack_exports__.getCurrentRunDir; exports.getLogBasePath = __webpack_exports__.getLogBasePath; exports.getLogFormat = __webpack_exports__.getLogFormat; exports.initializeLogRun = __webpack_exports__.initializeLogRun; exports.isFileLoggingEnabled = __webpack_exports__.isFileLoggingEnabled; exports.resetLogRun = __webpack_exports__.resetLogRun; exports.writeLogToFile = __webpack_exports__.writeLogToFile; exports.writeRunSummary = __webpack_exports__.writeRunSummary; for(var __rspack_i in __webpack_exports__)if (-1 === [ "getCurrentRunDir", "getLogBasePath", "getLogFormat", "initializeLogRun", "isFileLoggingEnabled", "resetLogRun", "writeLogToFile", "writeRunSummary" ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i]; Object.defineProperty(exports, '__esModule', { value: true }); //# sourceMappingURL=file-logger.js.map