fix(quotas): unifier le décompte IA (BYOK, rollback) et combler les fuites
Centralise la réserve via ai-quota, corrige admin unavailable (-1), brancher les routes sans quota et le host-pays brainstorm, avec usage-meter élargi, noms de clusters, MCP et ajustements dashboard/insights. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -2,7 +2,7 @@ import { NextRequest, NextResponse } from 'next/server'
|
||||
import { auth } from '@/auth'
|
||||
import prisma from '@/lib/prisma'
|
||||
import { notebookOrganizerService } from '@/lib/ai/services/notebook-organizer.service'
|
||||
import { reserveUsageOrThrow, QuotaExceededError } from '@/lib/entitlements'
|
||||
import { withAiQuota, handleQuotaHttpError } from '@/lib/ai-quota'
|
||||
import { syncNoteLabels } from '@/app/actions/notes'
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
@@ -17,19 +17,6 @@ export async function POST(request: NextRequest) {
|
||||
return NextResponse.json({ error: 'notebookId is required' }, { status: 400 })
|
||||
}
|
||||
|
||||
try {
|
||||
await reserveUsageOrThrow(session.user.id, 'reformulate')
|
||||
} catch (err) {
|
||||
if (err instanceof QuotaExceededError) {
|
||||
const isTierLocked = err.currentQuota === 0
|
||||
return NextResponse.json(
|
||||
{ error: isTierLocked ? 'feature_locked' : 'quota_exceeded', errorKey: isTierLocked ? 'ai.featureLocked' : 'ai.quotaExceeded' },
|
||||
{ status: 402 },
|
||||
)
|
||||
}
|
||||
throw err
|
||||
}
|
||||
|
||||
const notes = await prisma.note.findMany({
|
||||
where: { notebookId, trashedAt: null, userId: session.user.id },
|
||||
select: { id: true, title: true, content: true },
|
||||
@@ -46,13 +33,20 @@ export async function POST(request: NextRequest) {
|
||||
contentPreview: n.content.replace(/<[^>]+>/g, ' ').replace(/\s+/g, ' ').trim().slice(0, 300),
|
||||
}))
|
||||
|
||||
const result = await notebookOrganizerService.analyze(notesForAnalysis)
|
||||
|
||||
const result = await withAiQuota(
|
||||
session.user.id,
|
||||
'reformulate',
|
||||
() => notebookOrganizerService.analyze(notesForAnalysis),
|
||||
{ lane: 'chat' },
|
||||
)
|
||||
|
||||
return NextResponse.json(result)
|
||||
} catch (error: any) {
|
||||
} catch (error: unknown) {
|
||||
const quotaResp = handleQuotaHttpError(error)
|
||||
if (quotaResp) return quotaResp
|
||||
console.error('[Notebook Organizer] Error:', error)
|
||||
return NextResponse.json({ error: error.message || 'Failed to organize notebook' }, { status: 500 })
|
||||
const message = error instanceof Error ? error.message : 'Failed to organize notebook'
|
||||
return NextResponse.json({ error: message }, { status: 500 })
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user