mirror of
https://github.com/fosrl/pangolin.git
synced 2026-08-27 06:24:56 +02:00
Handle compression of ai session logs
This commit is contained in:
@@ -25,6 +25,7 @@ import {
|
||||
import logger from "@server/logger";
|
||||
import { and, eq, gt, desc, max, sql } from "drizzle-orm";
|
||||
import { decrypt } from "@server/lib/crypto";
|
||||
import { decompressText } from "@server/lib/textCompression";
|
||||
import config from "@server/lib/config";
|
||||
import {
|
||||
LogType,
|
||||
@@ -680,8 +681,8 @@ export class LogStreamingManager {
|
||||
Record<string, unknown> & { id: number }
|
||||
>;
|
||||
|
||||
case "aiSession":
|
||||
return (await logsDb
|
||||
case "aiSession": {
|
||||
const rows = (await logsDb
|
||||
.select()
|
||||
.from(aiSessionLog)
|
||||
.where(
|
||||
@@ -694,6 +695,33 @@ export class LogStreamingManager {
|
||||
.limit(limit)) as Array<
|
||||
Record<string, unknown> & { id: number }
|
||||
>;
|
||||
|
||||
const compressedFields = [
|
||||
"requestBody",
|
||||
"responseBody",
|
||||
"normalizedRequest",
|
||||
"normalizedResponse"
|
||||
] as const;
|
||||
|
||||
for (const row of rows) {
|
||||
for (const field of compressedFields) {
|
||||
const value = row[field];
|
||||
if (typeof value !== "string") {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
row[field] = decompressText(value);
|
||||
} catch (error) {
|
||||
logger.error(
|
||||
`Failed to decompress AI session log field ${field}`,
|
||||
{ error }
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return rows;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@ import { and, eq, lt } from "drizzle-orm";
|
||||
import cache from "#private/lib/cache";
|
||||
import { calculateCutoffTimestamp } from "@server/lib/cleanupLogs";
|
||||
import { sanitizeString } from "@server/lib/sanitize";
|
||||
import { compressText } from "@server/lib/textCompression";
|
||||
import type { AiCapability } from "@server/lib/aiCapabilities";
|
||||
import {
|
||||
normalizeAiRequest,
|
||||
@@ -151,7 +152,7 @@ async function getRetentionDays(orgId: string): Promise<number> {
|
||||
}
|
||||
|
||||
export async function cleanUpOldLogs(orgId: string, retentionDays: number) {
|
||||
const cutoffTimestamp = calculateCutoffTimestamp(retentionDays) * 1000;
|
||||
const cutoffTimestamp = calculateCutoffTimestamp(retentionDays);
|
||||
|
||||
try {
|
||||
await logsDb
|
||||
@@ -255,13 +256,19 @@ export function logAiSession(data: {
|
||||
),
|
||||
requestedModel: sanitizeString(data.requestedModel),
|
||||
isStream: data.isStream,
|
||||
requestBody: sanitizeString(requestBodyText.value),
|
||||
responseBody: sanitizeString(responseBodyText.value),
|
||||
requestBody: compressText(
|
||||
sanitizeString(requestBodyText.value)
|
||||
),
|
||||
responseBody: compressText(
|
||||
sanitizeString(responseBodyText.value)
|
||||
),
|
||||
normalizedRequest: normalizedRequestText
|
||||
? sanitizeString(normalizedRequestText.value)
|
||||
? compressText(sanitizeString(normalizedRequestText.value))
|
||||
: undefined,
|
||||
normalizedResponse: normalizedResponseText
|
||||
? sanitizeString(normalizedResponseText.value)
|
||||
? compressText(
|
||||
sanitizeString(normalizedResponseText.value)
|
||||
)
|
||||
: undefined,
|
||||
truncated:
|
||||
requestBodyText.truncated ||
|
||||
|
||||
Reference in New Issue
Block a user