feat: enhanced AI usage logging (model, provider, response) and fixed build blockers

This commit is contained in:
2025-12-27 00:10:55 +01:00
parent 20659567fd
commit c51cd23d5e
10 changed files with 127 additions and 34 deletions

View File

@@ -83,7 +83,7 @@ function sleep(ms: number): Promise<void> {
* Analyze whisky label with OpenRouter (Gemma 3 27B)
* Includes retry logic for 429 rate limit errors
*/
async function analyzeWithOpenRouter(base64Data: string, mimeType: string): Promise<{ data: any; apiTime: number }> {
async function analyzeWithOpenRouter(base64Data: string, mimeType: string): Promise<{ data: any; apiTime: number; responseText: string }> {
const client = getOpenRouterClient();
const startApi = performance.now();
const maxRetries = 3;
@@ -129,6 +129,7 @@ async function analyzeWithOpenRouter(base64Data: string, mimeType: string): Prom
return {
data: JSON.parse(jsonStr),
apiTime: endApi - startApi,
responseText: content
};
} catch (error: any) {
@@ -155,7 +156,7 @@ async function analyzeWithOpenRouter(base64Data: string, mimeType: string): Prom
/**
* Analyze whisky label with Gemini
*/
async function analyzeWithGemini(base64Data: string, mimeType: string): Promise<{ data: any; apiTime: number }> {
async function analyzeWithGemini(base64Data: string, mimeType: string): Promise<{ data: any; apiTime: number; responseText: string }> {
const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY!);
const model = genAI.getGenerativeModel({
model: 'gemini-2.5-flash',
@@ -179,9 +180,11 @@ async function analyzeWithGemini(base64Data: string, mimeType: string): Promise<
]);
const endApi = performance.now();
const responseText = result.response.text();
return {
data: JSON.parse(result.response.text()),
data: JSON.parse(responseText),
apiTime: endApi - startApi,
responseText: responseText
};
}
@@ -243,7 +246,7 @@ export async function analyzeLabelWithGemini(imageBase64: string): Promise<Gemin
// Call appropriate provider
console.log(`[Vision] Using provider: ${provider}`);
let result: { data: any; apiTime: number };
let result: { data: any; apiTime: number; responseText: string };
if (provider === 'openrouter') {
result = await analyzeWithOpenRouter(base64Data, mimeType);
@@ -278,7 +281,10 @@ export async function analyzeLabelWithGemini(imageBase64: string): Promise<Gemin
userId: user.id,
apiType: 'gemini_ai', // Keep same type for tracking
endpoint: `analyzeLabelWith${provider === 'openrouter' ? 'OpenRouter' : 'Gemini'}`,
success: true
success: true,
provider: provider,
model: provider === 'openrouter' ? OPENROUTER_VISION_MODEL : 'gemini-2.5-flash',
responseText: result.responseText
});
await deductCredits(user.id, 'gemini_ai', `Vision label analysis (${provider})`);