@@ -13,6 +13,7 @@ import {
1313 projectFiles ,
1414 apiKeys ,
1515 user ,
16+ modelPerformanceStats ,
1617} from '$lib/db/schema' ;
1718import { existsSync , mkdirSync , readFileSync , writeFileSync } from 'fs' ;
1819import { extractTextFromPDF } from '$lib/utils/pdf-extraction' ;
@@ -1159,6 +1160,76 @@ ${attachedRules.map((r) => `- ${r.name}: ${r.rule}`).join('\n')}`;
11591160 } )
11601161 . where ( eq ( messages . id , assistantMessageId ) ) ;
11611162
1163+ // Track analytics asynchronously
1164+ ( async ( ) => {
1165+ try {
1166+ // Only track if we have the necessary data
1167+ if ( tokenCount !== undefined && costUsd !== undefined ) {
1168+ const existing = await db
1169+ . select ( )
1170+ . from ( modelPerformanceStats )
1171+ . where (
1172+ and (
1173+ eq ( modelPerformanceStats . userId , userId ) ,
1174+ eq ( modelPerformanceStats . modelId , model . modelId ) ,
1175+ eq ( modelPerformanceStats . provider , Provider . NanoGPT )
1176+ )
1177+ )
1178+ . get ( ) ;
1179+
1180+ if ( existing ) {
1181+ // Incremental update - recalculate averages
1182+ const newTotalMessages = existing . totalMessages + 1 ;
1183+ const newTotalCost = existing . totalCost + costUsd ;
1184+ const newAvgTokens = existing . avgTokens
1185+ ? ( existing . avgTokens * existing . totalMessages + tokenCount ) / newTotalMessages
1186+ : tokenCount ;
1187+ const newAvgResponseTime =
1188+ responseTimeMs !== undefined && existing . avgResponseTime
1189+ ? ( existing . avgResponseTime * existing . totalMessages + responseTimeMs ) /
1190+ newTotalMessages
1191+ : existing . avgResponseTime ;
1192+
1193+ await db
1194+ . update ( modelPerformanceStats )
1195+ . set ( {
1196+ totalMessages : newTotalMessages ,
1197+ totalCost : newTotalCost ,
1198+ avgTokens : newAvgTokens ,
1199+ avgResponseTime : responseTimeMs !== undefined ? newAvgResponseTime : undefined ,
1200+ lastUpdated : new Date ( ) ,
1201+ } )
1202+ . where ( eq ( modelPerformanceStats . id , existing . id ) ) ;
1203+ } else {
1204+ // Create new stats entry
1205+ await db . insert ( modelPerformanceStats ) . values ( {
1206+ id : generateId ( ) ,
1207+ userId,
1208+ modelId : model . modelId ,
1209+ provider : Provider . NanoGPT ,
1210+ totalMessages : 1 ,
1211+ totalCost : costUsd ,
1212+ avgTokens : tokenCount ,
1213+ avgResponseTime : responseTimeMs ,
1214+ errorCount : 0 ,
1215+ thumbsUpCount : 0 ,
1216+ thumbsDownCount : 0 ,
1217+ regenerateCount : 0 ,
1218+ accurateCount : 0 ,
1219+ helpfulCount : 0 ,
1220+ creativeCount : 0 ,
1221+ fastCount : 0 ,
1222+ costEffectiveCount : 0 ,
1223+ lastUpdated : new Date ( ) ,
1224+ } ) ;
1225+ }
1226+ log ( `[analytics] Updated stats for ${ model . modelId } ` , startTime ) ;
1227+ }
1228+ } catch ( e ) {
1229+ log ( `[analytics] Error updating stats: ${ e } ` , startTime ) ;
1230+ }
1231+ } ) ( ) ;
1232+
11621233 // Generate title if needed (after response is complete)
11631234 // We do this before setting generating=false so the UI continues polling
11641235 // until the title is generated
0 commit comments