chore: review

This commit is contained in:
Ephraim Atta-Duncan
2025-11-19 08:53:57 +00:00
parent 57c4c3fd48
commit a0a3e7fb93
4 changed files with 103 additions and 53 deletions

View File

@ -22,6 +22,7 @@ import {
type TDetectedRecipient,
ZAnalyzeRecipientsRequestSchema,
ZDetectFormFieldsRequestSchema,
ZDetectFormFieldsResponseSchema,
ZDetectedFormFieldSchema,
ZDetectedRecipientLLMSchema,
} from './types';
@ -180,6 +181,13 @@ export const aiRoute = new Hono<HonoEnv>()
try {
const { user } = await getSession(c.req.raw);
if (!user.emailVerified) {
throw new AppError(AppErrorCode.UNAUTHORIZED, {
message: 'Email verification required',
userMessage: 'Please verify your email to use AI features',
});
}
const body = await c.req.json();
const parsed = ZDetectFormFieldsRequestSchema.safeParse(body);
@ -259,15 +267,25 @@ export const aiRoute = new Hono<HonoEnv>()
);
const detectedFields: TDetectFormFieldsResponse = [];
const failedPages: number[] = [];
for (const [index, result] of results.entries()) {
if (result.status === 'fulfilled') {
detectedFields.push(...result.value);
} else {
const pageNumber = renderedPages[index]?.pageNumber ?? index + 1;
console.error(`Failed to detect fields on page ${pageNumber}:`, result.reason);
failedPages.push(pageNumber);
}
}
if (failedPages.length > 0) {
throw new AppError(AppErrorCode.UNKNOWN_ERROR, {
message: `Failed to detect fields on pages: ${failedPages.join(', ')}`,
userMessage: 'We could not detect fields on some pages. Please try again.',
});
}
if (env('NEXT_PUBLIC_AI_DEBUG_PREVIEW') === 'true') {
const debugDir = join(process.cwd(), '..', '..', 'packages', 'assets', 'ai-previews');
await mkdir(debugDir, { recursive: true });
@ -378,7 +396,9 @@ export const aiRoute = new Hono<HonoEnv>()
}
}
return c.json<TDetectFormFieldsResponse>(detectedFields);
const validatedResponse = ZDetectFormFieldsResponseSchema.parse(detectedFields);
return c.json<TDetectFormFieldsResponse>(validatedResponse);
} catch (error) {
if (error instanceof AppError) {
throw error;
@ -396,6 +416,13 @@ export const aiRoute = new Hono<HonoEnv>()
try {
const { user } = await getSession(c.req.raw);
if (!user.emailVerified) {
throw new AppError(AppErrorCode.UNAUTHORIZED, {
message: 'Email verification required',
userMessage: 'Please verify your email to use AI features',
});
}
const body = await c.req.json();
const parsed = ZAnalyzeRecipientsRequestSchema.safeParse(body);
@ -455,9 +482,13 @@ export const aiRoute = new Hono<HonoEnv>()
const allRecipients: TDetectedRecipient[] = [];
let recipientIndex = 1;
for (const result of results) {
const failedPages: number[] = [];
for (const [index, result] of results.entries()) {
if (result.status !== 'fulfilled') {
console.error('Failed to analyze recipients on a page:', result.reason);
const pageNumber = pagesToAnalyze[index]?.pageNumber ?? index + 1;
console.error(`Failed to analyze recipients on page ${pageNumber}:`, result.reason);
failedPages.push(pageNumber);
continue;
}
@ -479,6 +510,13 @@ export const aiRoute = new Hono<HonoEnv>()
allRecipients.push(...recipientsWithEmails);
}
if (failedPages.length > 0) {
throw new AppError(AppErrorCode.UNKNOWN_ERROR, {
message: `Failed to analyze recipients on pages: ${failedPages.join(', ')}`,
userMessage: 'We could not analyze recipients on some pages. Please try again.',
});
}
return c.json<TAnalyzeRecipientsResponse>(allRecipients);
} catch (error) {
if (error instanceof AppError) {