mirror of
https://github.com/rowboatlabs/rowboat.git
synced 2026-05-28 19:05:31 +02:00
added billing for image parsing
This commit is contained in:
parent
698988e714
commit
24b0ac7144
2 changed files with 71 additions and 3 deletions
|
|
@ -1,6 +1,8 @@
|
||||||
import { NextRequest, NextResponse } from 'next/server';
|
import { NextRequest, NextResponse } from 'next/server';
|
||||||
import { S3Client, GetObjectCommand, HeadObjectCommand } from '@aws-sdk/client-s3';
|
import { S3Client, GetObjectCommand, HeadObjectCommand } from '@aws-sdk/client-s3';
|
||||||
import { GoogleGenerativeAI } from '@google/generative-ai';
|
import { GoogleGenerativeAI } from '@google/generative-ai';
|
||||||
|
import { UsageTracker } from '@/app/lib/billing';
|
||||||
|
import { logUsage } from '@/app/actions/billing.actions';
|
||||||
|
|
||||||
export async function POST(request: NextRequest) {
|
export async function POST(request: NextRequest) {
|
||||||
try {
|
try {
|
||||||
|
|
@ -52,6 +54,7 @@ export async function POST(request: NextRequest) {
|
||||||
const buf = Buffer.concat(chunks);
|
const buf = Buffer.concat(chunks);
|
||||||
|
|
||||||
let descriptionMarkdown: string | null = null;
|
let descriptionMarkdown: string | null = null;
|
||||||
|
const usageTracker = new UsageTracker();
|
||||||
try {
|
try {
|
||||||
const apiKey = process.env.GOOGLE_API_KEY || process.env.GEMINI_API_KEY || '';
|
const apiKey = process.env.GOOGLE_API_KEY || process.env.GEMINI_API_KEY || '';
|
||||||
if (apiKey) {
|
if (apiKey) {
|
||||||
|
|
@ -62,16 +65,41 @@ export async function POST(request: NextRequest) {
|
||||||
{ inlineData: { data: buf.toString('base64'), mimeType: contentType } },
|
{ inlineData: { data: buf.toString('base64'), mimeType: contentType } },
|
||||||
prompt,
|
prompt,
|
||||||
]);
|
]);
|
||||||
descriptionMarkdown = result.response?.text?.() || null;
|
const response: any = result.response as any;
|
||||||
|
descriptionMarkdown = response?.text?.() || null;
|
||||||
|
|
||||||
|
// Track usage similar to agents-runtime
|
||||||
|
try {
|
||||||
|
const inputTokens = response?.usageMetadata?.promptTokenCount || 0;
|
||||||
|
const outputTokens = response?.usageMetadata?.candidatesTokenCount || 0;
|
||||||
|
usageTracker.track({
|
||||||
|
type: 'LLM_USAGE',
|
||||||
|
modelName: 'gemini-2.5-flash',
|
||||||
|
inputTokens,
|
||||||
|
outputTokens,
|
||||||
|
context: 'uploaded_images.describe',
|
||||||
|
});
|
||||||
|
} catch (_) {
|
||||||
|
// ignore usage tracking errors
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.warn('Gemini description failed', e);
|
console.warn('Gemini description failed', e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Log usage to billing if available
|
||||||
|
try {
|
||||||
|
const items = usageTracker.flush();
|
||||||
|
if (items.length > 0) {
|
||||||
|
await logUsage({ items });
|
||||||
|
}
|
||||||
|
} catch (_) {
|
||||||
|
// ignore billing logging errors
|
||||||
|
}
|
||||||
|
|
||||||
return NextResponse.json({ id, description: descriptionMarkdown });
|
return NextResponse.json({ id, description: descriptionMarkdown });
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('describe error', e);
|
console.error('describe error', e);
|
||||||
return NextResponse.json({ error: 'Failed to describe' }, { status: 500 });
|
return NextResponse.json({ error: 'Failed to describe' }, { status: 500 });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,8 @@ import { S3Client, PutObjectCommand } from '@aws-sdk/client-s3';
|
||||||
import crypto from 'crypto';
|
import crypto from 'crypto';
|
||||||
import { tempBinaryCache } from '@/src/application/services/temp-binary-cache';
|
import { tempBinaryCache } from '@/src/application/services/temp-binary-cache';
|
||||||
import { GoogleGenerativeAI } from '@google/generative-ai';
|
import { GoogleGenerativeAI } from '@google/generative-ai';
|
||||||
|
import { UsageTracker } from '@/app/lib/billing';
|
||||||
|
import { logUsage } from '@/app/actions/billing.actions';
|
||||||
|
|
||||||
// POST /api/uploaded-images
|
// POST /api/uploaded-images
|
||||||
// Accepts an image file (multipart/form-data, field name: "file")
|
// Accepts an image file (multipart/form-data, field name: "file")
|
||||||
|
|
@ -27,6 +29,7 @@ export async function POST(request: NextRequest) {
|
||||||
|
|
||||||
// Optionally describe image with Gemini
|
// Optionally describe image with Gemini
|
||||||
let descriptionMarkdown: string | null = null;
|
let descriptionMarkdown: string | null = null;
|
||||||
|
const usageTracker = new UsageTracker();
|
||||||
try {
|
try {
|
||||||
const apiKey = process.env.GOOGLE_API_KEY || process.env.GEMINI_API_KEY || '';
|
const apiKey = process.env.GOOGLE_API_KEY || process.env.GEMINI_API_KEY || '';
|
||||||
if (apiKey) {
|
if (apiKey) {
|
||||||
|
|
@ -37,7 +40,23 @@ export async function POST(request: NextRequest) {
|
||||||
{ inlineData: { data: buf.toString('base64'), mimeType: mime } },
|
{ inlineData: { data: buf.toString('base64'), mimeType: mime } },
|
||||||
prompt,
|
prompt,
|
||||||
]);
|
]);
|
||||||
descriptionMarkdown = result.response?.text?.() || null;
|
const response: any = result.response as any;
|
||||||
|
descriptionMarkdown = response?.text?.() || null;
|
||||||
|
|
||||||
|
// Track usage similar to agents-runtime
|
||||||
|
try {
|
||||||
|
const inputTokens = response?.usageMetadata?.promptTokenCount || 0;
|
||||||
|
const outputTokens = response?.usageMetadata?.candidatesTokenCount || 0;
|
||||||
|
usageTracker.track({
|
||||||
|
type: 'LLM_USAGE',
|
||||||
|
modelName: 'gemini-2.5-flash',
|
||||||
|
inputTokens,
|
||||||
|
outputTokens,
|
||||||
|
context: 'uploaded_images.upload_with_description',
|
||||||
|
});
|
||||||
|
} catch (_) {
|
||||||
|
// ignore usage tracking errors
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.warn('Gemini description failed', e);
|
console.warn('Gemini description failed', e);
|
||||||
|
|
@ -70,6 +89,17 @@ export async function POST(request: NextRequest) {
|
||||||
}));
|
}));
|
||||||
|
|
||||||
const url = `/api/uploaded-images/${imageId}`;
|
const url = `/api/uploaded-images/${imageId}`;
|
||||||
|
|
||||||
|
// Log usage to billing if available
|
||||||
|
try {
|
||||||
|
const items = usageTracker.flush();
|
||||||
|
if (items.length > 0) {
|
||||||
|
await logUsage({ items });
|
||||||
|
}
|
||||||
|
} catch (_) {
|
||||||
|
// ignore billing logging errors
|
||||||
|
}
|
||||||
|
|
||||||
return NextResponse.json({ url, storage: 's3', id: imageId, mimeType: mime, description: descriptionMarkdown });
|
return NextResponse.json({ url, storage: 's3', id: imageId, mimeType: mime, description: descriptionMarkdown });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -77,6 +107,16 @@ export async function POST(request: NextRequest) {
|
||||||
const ttlSec = 10 * 60; // 10 minutes
|
const ttlSec = 10 * 60; // 10 minutes
|
||||||
const id = tempBinaryCache.put(buf, mime, ttlSec * 1000);
|
const id = tempBinaryCache.put(buf, mime, ttlSec * 1000);
|
||||||
const url = `/api/tmp-images/${id}`;
|
const url = `/api/tmp-images/${id}`;
|
||||||
|
// Log usage to billing if available
|
||||||
|
try {
|
||||||
|
const items = usageTracker.flush();
|
||||||
|
if (items.length > 0) {
|
||||||
|
await logUsage({ items });
|
||||||
|
}
|
||||||
|
} catch (_) {
|
||||||
|
// ignore billing logging errors
|
||||||
|
}
|
||||||
|
|
||||||
return NextResponse.json({ url, storage: 'temp', id, mimeType: mime, expiresInSec: ttlSec, description: descriptionMarkdown });
|
return NextResponse.json({ url, storage: 'temp', id, mimeType: mime, expiresInSec: ttlSec, description: descriptionMarkdown });
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('upload image error', e);
|
console.error('upload image error', e);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue