All checks were successful
Deploy to Production / Build and Deploy (push) Successful in 5s
- Fix useBrainstormSocket: stable guestId via useRef, remove setState in cleanup - Fix GhostCursor: direct DOM manipulation via refs, no useState re-renders - Fix all SQL embedding queries: add ::vector cast on text columns - Fix embedding truncation to 15000 chars (under 8192 token limit) - Fix NoteEmbedding INSERT: remove non-existent updatedAt column - Fix billing page: show all quota stats in grid instead of single metric - Fix usage meter: accordion expand/collapse, per-feature detail - Fix semantic search: rebuild 103 note embeddings, ::vector cast on vectorSearch - Fix brainstorm expand/manual-idea/create: ::vector cast on embedding SQL
92 lines
2.9 KiB
TypeScript
92 lines
2.9 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { auth } from '@/auth'
|
|
import { paragraphRefactorService } from '@/lib/ai/services/paragraph-refactor.service'
|
|
import { getAISettings } from '@/app/actions/ai-settings'
|
|
import { checkEntitlementOrThrow, QuotaExceededError, incrementUsageAsync } from '@/lib/entitlements'
|
|
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
const session = await auth()
|
|
|
|
if (!session?.user?.id) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
// Respect user's paragraphRefactor toggle (Assistant IA)
|
|
const userSettings = await getAISettings(session.user.id)
|
|
if (userSettings.paragraphRefactor === false) {
|
|
return NextResponse.json({ error: 'Feature disabled' }, { status: 403 })
|
|
}
|
|
|
|
const { text, option, format, language } = await request.json()
|
|
|
|
// Validation
|
|
if (!text || typeof text !== 'string') {
|
|
return NextResponse.json({ error: 'Text is required' }, { status: 400 })
|
|
}
|
|
|
|
// Map option to refactor mode
|
|
const modeMap: Record<string, 'clarify' | 'shorten' | 'improveStyle' | 'fix_grammar' | 'translate' | 'explain'> = {
|
|
'clarify': 'clarify',
|
|
'shorten': 'shorten',
|
|
'improve': 'improveStyle',
|
|
'fix_grammar': 'fix_grammar',
|
|
'translate': 'translate',
|
|
'explain': 'explain'
|
|
}
|
|
|
|
const mode = modeMap[option]
|
|
if (!mode) {
|
|
return NextResponse.json(
|
|
{ error: 'Invalid option. Use: clarify, shorten, improve, fix_grammar, translate, or explain' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
// Validate word count
|
|
const validation = paragraphRefactorService.validateWordCount(text)
|
|
if (!validation.valid) {
|
|
return NextResponse.json({
|
|
error: validation.error,
|
|
errorKey: validation.errorKey,
|
|
params: validation.params,
|
|
}, { status: 400 })
|
|
}
|
|
|
|
// Check quota
|
|
try {
|
|
await checkEntitlementOrThrow(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',
|
|
upgradeTier: err.upgradeTier,
|
|
quotaExceeded: true,
|
|
}, { status: 402 })
|
|
}
|
|
throw err
|
|
}
|
|
|
|
// Use the ParagraphRefactorService
|
|
const result = await paragraphRefactorService.refactor(text, mode, format === 'html' ? 'html' : 'markdown', language)
|
|
|
|
incrementUsageAsync(session.user.id, 'reformulate')
|
|
|
|
return NextResponse.json({
|
|
originalText: result.original,
|
|
reformulatedText: result.refactored,
|
|
option: option,
|
|
language: result.language,
|
|
wordCountChange: result.wordCountChange
|
|
})
|
|
} catch (error: any) {
|
|
return NextResponse.json(
|
|
{ error: error.message || 'Failed to reformulate text' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|
|
|