evalstate's picture
download
raw
8.45 kB
import pino, {} from 'pino';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
const QUERY_LOGS_ENABLED = (process.env.LOG_QUERY_EVENTS ?? 'true').toLowerCase() === 'true';
const SYSTEM_LOGS_ENABLED = (process.env.LOG_SYSTEM_EVENTS ?? 'true').toLowerCase() === 'true';
const DATASET_CONFIGURED = !!process.env.LOGGING_DATASET_ID;
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
function createQueryLogger() {
if (process.env.NODE_ENV === 'test' || process.env.VITEST === 'true') {
return null;
}
if (!QUERY_LOGS_ENABLED || !DATASET_CONFIGURED) {
return null;
}
const datasetId = process.env.LOGGING_DATASET_ID;
const hfToken = process.env.LOGGING_HF_TOKEN || process.env.DEFAULT_HF_TOKEN;
if (!hfToken) {
console.warn('[Query Logger] Query logging disabled: No HF token found (set LOGGING_HF_TOKEN or DEFAULT_HF_TOKEN)');
return null;
}
console.log(`[Query Logger] Query logging enabled for dataset: ${datasetId}`);
try {
const transportPath = join(__dirname, 'hf-dataset-transport.js');
const baseOptions = {
level: 'info',
timestamp: pino.stdTimeFunctions.isoTime,
};
return pino({
...baseOptions,
transport: {
target: transportPath,
options: { sync: false, logType: 'Query' },
},
});
}
catch (error) {
console.error('[Query Logger] Failed to setup query logging transport:', error);
return null;
}
}
const queryLogger = createQueryLogger();
function createSystemLogger() {
if (process.env.NODE_ENV === 'test' || process.env.VITEST === 'true') {
return null;
}
if (!DATASET_CONFIGURED) {
return null;
}
if (!SYSTEM_LOGS_ENABLED) {
return null;
}
const hfToken = process.env.LOGGING_HF_TOKEN || process.env.DEFAULT_HF_TOKEN;
if (!hfToken) {
console.warn('[System Logger] System logging disabled: No HF token found (set LOGGING_HF_TOKEN or DEFAULT_HF_TOKEN)');
return null;
}
try {
const transportPath = join(__dirname, 'hf-dataset-transport.js');
const baseOptions = {
level: 'info',
timestamp: pino.stdTimeFunctions.isoTime,
};
return pino({
...baseOptions,
transport: {
target: transportPath,
options: { sync: false, logType: 'System' },
},
});
}
catch (error) {
console.error('[System Logger] Failed to setup system logging transport:', error);
return null;
}
}
const systemLogger = createSystemLogger();
function createGradioLogger() {
if (process.env.NODE_ENV === 'test' || process.env.VITEST === 'true') {
return null;
}
if (!DATASET_CONFIGURED) {
return null;
}
if (!SYSTEM_LOGS_ENABLED) {
return null;
}
const hfToken = process.env.LOGGING_HF_TOKEN || process.env.DEFAULT_HF_TOKEN;
if (!hfToken) {
console.warn('[Gradio Logger] Gradio logging disabled: No HF token found (set LOGGING_HF_TOKEN or DEFAULT_HF_TOKEN)');
return null;
}
try {
const transportPath = join(__dirname, 'hf-dataset-transport.js');
const baseOptions = {
level: 'info',
timestamp: pino.stdTimeFunctions.isoTime,
};
return pino({
...baseOptions,
transport: {
target: transportPath,
options: { sync: false, logType: 'Gradio' },
},
});
}
catch (error) {
console.error('[Gradio Logger] Failed to setup Gradio logging transport:', error);
return null;
}
}
const gradioLogger = createGradioLogger();
const mcpServerSessionId = crypto.randomUUID();
function getMcpServerSessionId() {
return mcpServerSessionId;
}
function logQuery(entry) {
if (!queryLogger) {
return;
}
queryLogger.info(entry);
}
function logQueryEvent(methodName, query, data, options) {
const mcpServerSessionId = getMcpServerSessionId();
const normalizedDurationMs = options?.durationMs !== undefined ? Math.round(options.durationMs) : undefined;
const serializedParameters = JSON.stringify(data);
const requestPayload = {
methodName,
query,
parameters: data,
};
const normalizedError = options?.error !== undefined && options?.error !== null ? normalizeError(options.error) : null;
logQuery({
query,
methodName,
parameters: serializedParameters,
requestJson: JSON.stringify(requestPayload),
mcpServerSessionId,
clientSessionId: options?.clientSessionId || null,
isAuthenticated: options?.isAuthenticated ?? false,
name: options?.clientName || null,
version: options?.clientVersion || null,
totalResults: options?.totalResults,
resultsShared: options?.resultsShared,
responseCharCount: options?.responseCharCount,
durationMs: normalizedDurationMs,
success: options?.success ?? true,
errorMessage: normalizedError,
});
}
export function logSearchQuery(methodName, query, data, options) {
logQueryEvent(methodName, query, data, options);
}
export function logPromptQuery(methodName, query, data, options) {
logQueryEvent(methodName, query, data, options);
}
export function logSystemEvent(methodName, sessionId, options) {
if (!systemLogger) {
return;
}
const mcpServerSessionId = getMcpServerSessionId();
let capabilitiesName = null;
let capabilitiesVersion = null;
if (options?.capabilities && typeof options.capabilities === 'object' && options.capabilities !== null) {
const caps = options.capabilities;
if (caps.clientInfo && typeof caps.clientInfo === 'object' && caps.clientInfo !== null) {
const clientInfo = caps.clientInfo;
capabilitiesName = typeof clientInfo.name === 'string' ? clientInfo.name : null;
capabilitiesVersion = typeof clientInfo.version === 'string' ? clientInfo.version : null;
}
}
systemLogger.info({
sessionId,
methodName,
name: options?.clientName || capabilitiesName || null,
version: options?.clientVersion || capabilitiesVersion || null,
authorized: options?.isAuthenticated ?? false,
ipAddress: options?.ipAddress || null,
capabilities: options?.capabilities ? JSON.stringify(options.capabilities) : null,
clientSessionId: options?.clientSessionId || null,
requestJson: options?.requestJson
? JSON.stringify(options.requestJson)
: JSON.stringify({ methodName, sessionId }),
mcpServerSessionId,
}, 'System event logged');
}
export function logGradioEvent(endpointName, sessionId, options) {
if (!gradioLogger) {
return;
}
const mcpServerSessionId = getMcpServerSessionId();
let errorString = null;
if (options.error !== undefined && options.error !== null) {
if (typeof options.error === 'string') {
errorString = options.error;
}
else if (options.error instanceof Error) {
errorString = options.error.message;
}
else {
try {
errorString = JSON.stringify(options.error);
}
catch {
errorString = String(options.error);
}
}
}
gradioLogger.info({
sessionId,
endpointName,
name: options.clientName || null,
version: options.clientVersion || null,
authorized: options.isAuthenticated ?? false,
durationMs: options.durationMs,
success: options.success,
error: errorString,
responseSizeBytes: options.responseSizeBytes || null,
notificationCount: options.notificationCount || 0,
isDynamic: options.isDynamic ?? false,
mcpServerSessionId,
}, 'Gradio event logged');
}
function normalizeError(error) {
if (error instanceof Error) {
return `${error.name}: ${error.message}`;
}
if (typeof error === 'string') {
return error;
}
try {
return JSON.stringify(error);
}
catch {
return String(error);
}
}
//# sourceMappingURL=query-logger.js.map

Xet Storage Details

Size:
8.45 kB
·
Xet hash:
2ee98fe21db4981a278cbcfbd978c8e775dddc9fbcde434bc3052626038aefef

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.