mirror of
https://github.com/documenso/documenso.git
synced 2025-11-19 11:12:06 +10:00
40 lines
1.2 KiB
TypeScript
40 lines
1.2 KiB
TypeScript
import { AppError, AppErrorCode } from '@documenso/lib/errors/app-error';
|
|
import type { TDetectedFormField } from '@documenso/lib/types/document-analysis';
|
|
|
|
export const detectFieldsInDocument = async (envelopeId: string): Promise<TDetectedFormField[]> => {
|
|
const response = await fetch('/api/ai/detect-fields', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({ envelopeId }),
|
|
credentials: 'include',
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const errorText = await response.text();
|
|
|
|
console.error('Field detection failed:', {
|
|
status: response.status,
|
|
statusText: response.statusText,
|
|
error: errorText,
|
|
});
|
|
|
|
throw new AppError(AppErrorCode.UNKNOWN_ERROR, {
|
|
message: `Field detection failed: ${response.statusText}`,
|
|
userMessage: 'Failed to detect fields in the document. Please try adding fields manually.',
|
|
});
|
|
}
|
|
|
|
const data = await response.json();
|
|
|
|
if (!Array.isArray(data)) {
|
|
throw new AppError(AppErrorCode.UNKNOWN_ERROR, {
|
|
message: 'Invalid response from field detection API - expected array',
|
|
userMessage: 'Failed to detect fields in the document. Please try again.',
|
|
});
|
|
}
|
|
|
|
return data;
|
|
};
|