mirror of
https://github.com/fosrl/pangolin.git
synced 2026-08-13 07:50:22 +02:00
link the usage with the session to display together
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
import { randomUUID } from "crypto";
|
||||
import { logsDb, db, orgs, aiSessionLog, type AiProvider } from "@server/db";
|
||||
import type { InferInsertModel } from "drizzle-orm";
|
||||
import logger from "@server/logger";
|
||||
@@ -169,6 +168,7 @@ function truncateBody(value: string): { value: string; truncated: boolean } {
|
||||
}
|
||||
|
||||
export function logAiSession(data: {
|
||||
sessionId: string;
|
||||
capability: AiCapability;
|
||||
provider: AiProvider;
|
||||
requestedModel: string | undefined;
|
||||
@@ -230,7 +230,7 @@ export function logAiSession(data: {
|
||||
}
|
||||
|
||||
sessionLogBuffer.push({
|
||||
sessionId: randomUUID(),
|
||||
sessionId: data.sessionId,
|
||||
orgId: sanitizeString(data.orgId),
|
||||
providerId: data.provider.providerId,
|
||||
capability: data.capability,
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { randomUUID } from "crypto";
|
||||
import { Request, Response } from "express";
|
||||
import { and, eq, inArray } from "drizzle-orm";
|
||||
import {
|
||||
@@ -594,6 +595,12 @@ export function recordAiGatewayCompletion(args: {
|
||||
const pricing = getModelPricing(provider.type as AiProviderType, model);
|
||||
const cost = calculateAiCost(pricing, usage);
|
||||
|
||||
// Shared by the usage record and the session log so the two can be
|
||||
// joined later to show token/cost usage alongside the transcript -
|
||||
// generated up front since neither buffered insert's row id is known
|
||||
// until its next batch flush.
|
||||
const sessionId = randomUUID();
|
||||
|
||||
logger.info("AI gateway request usage", {
|
||||
capability,
|
||||
providerId: provider.providerId,
|
||||
@@ -618,7 +625,8 @@ export function recordAiGatewayCompletion(args: {
|
||||
userId: requestUserId,
|
||||
requestedModel: model ?? "unknown",
|
||||
usage,
|
||||
costUsd: cost?.totalCost ?? null
|
||||
costUsd: cost?.totalCost ?? null,
|
||||
sessionId
|
||||
});
|
||||
|
||||
if (budgets.length > 0) {
|
||||
@@ -635,6 +643,7 @@ export function recordAiGatewayCompletion(args: {
|
||||
}
|
||||
|
||||
logAiSession({
|
||||
sessionId,
|
||||
capability,
|
||||
provider,
|
||||
requestedModel,
|
||||
|
||||
Reference in New Issue
Block a user