const { DocumentProcessorServiceClient } = require('@google-cloud/documentai'); const { Storage } = require('@google-cloud/storage'); // Configuration const PROJECT_ID = 'cim-summarizer'; const LOCATION = 'us'; async function setupDocumentAI() { console.log('Setting up Document AI processors...'); const client = new DocumentProcessorServiceClient(); try { // List available processor types console.log('Available processor types:'); const [processorTypes] = await client.listProcessorTypes({ parent: `projects/${PROJECT_ID}/locations/${LOCATION}`, }); processorTypes.forEach(processorType => { console.log(`- ${processorType.name}: ${processorType.displayName}`); }); // Create a Document OCR processor console.log('\nCreating Document OCR processor...'); const [operation] = await client.createProcessor({ parent: `projects/${PROJECT_ID}/locations/${LOCATION}`, processor: { displayName: 'CIM Document Processor', type: 'projects/245796323861/locations/us/processorTypes/ocr-processor', }, }); const [processor] = await operation.promise(); console.log(`✅ Created processor: ${processor.name}`); console.log(`Processor ID: ${processor.name.split('/').pop()}`); // Save processor ID to environment console.log('\nAdd this to your .env file:'); console.log(`DOCUMENT_AI_PROCESSOR_ID=${processor.name.split('/').pop()}`); } catch (error) { console.error('Error setting up Document AI:', error.message); if (error.message.includes('already exists')) { console.log('Processor already exists. Listing existing processors...'); const [processors] = await client.listProcessors({ parent: `projects/${PROJECT_ID}/locations/${LOCATION}`, }); processors.forEach(processor => { console.log(`- ${processor.name}: ${processor.displayName}`); }); } } } async function testDocumentAI() { console.log('\nTesting Document AI setup...'); const client = new DocumentProcessorServiceClient(); const storage = new Storage(); try { // Test with a simple text file const testContent = 'This is a test document for CIM processing.'; const testFileName = `test-${Date.now()}.txt`; // Upload test file to GCS const bucket = storage.bucket('cim-summarizer-uploads'); const file = bucket.file(testFileName); await file.save(testContent, { metadata: { contentType: 'text/plain', }, }); console.log(`✅ Uploaded test file: gs://cim-summarizer-uploads/${testFileName}`); // Process with Document AI (if we have a processor) console.log('Document AI setup completed successfully!'); } catch (error) { console.error('Error testing Document AI:', error.message); } } async function main() { try { await setupDocumentAI(); await testDocumentAI(); } catch (error) { console.error('Setup failed:', error); } } if (require.main === module) { main(); } module.exports = { setupDocumentAI, testDocumentAI };