/**
* Analyze service
*
* Handles document and RFP analysis operations
*/
import axios from 'axios';
import { v4 as uuidv4 } from 'uuid';
import { config } from '../config/index.js';
import { logger } from '../utils/logger.js';
// Initialize HTTP client for analyze API
const analyzeApiClient = axios.create({
baseURL: `${config.MODEL_API.URL}/analyze`,
timeout: config.MODEL_API.TIMEOUT,
headers: {
'Content-Type': 'application/json',
'X-API-Key': config.MODEL_API.KEY,
},
});
export const analyzeService = {
/**
* Analyze a document
*/
analyzeDocument: async (document, options = {}) => {
try {
const response = await analyzeApiClient.post('/document', {
document,
options,
timestamp: new Date().toISOString()
});
return response.data;
} catch (error) {
logger.error('Document analysis failed:', error);
throw new Error(`Analyze API error: ${error.response?.data?.message || error.message}`);
}
},
/**
* Analyze multiple documents in batch
*/
analyzeDocumentBatch: async (documents, options = {}) => {
try {
const response = await analyzeApiClient.post('/documents/batch', {
documents,
options,
timestamp: new Date().toISOString()
});
return response.data;
} catch (error) {
logger.error('Batch document analysis failed:', error);
throw new Error(`Analyze API error: ${error.response?.data?.message || error.message}`);
}
},
/**
* Get document analysis results
*/
getDocumentAnalysisResults: async (analysisId) => {
try {
const response = await analyzeApiClient.get(`/document/${analysisId}`);
return response.data;
} catch (error) {
if (error.response?.status === 404) {
return null;
}
logger.error(`Failed to get analysis results for ${analysisId}:`, error);
throw new Error(`Analyze API error: ${error.response?.data?.message || error.message}`);
}
},
/**
* Analyze an RFP document
*/
analyzeRFP: async (rfpDocument, options = {}) => {
try {
const response = await analyzeApiClient.post('/rfp', {
rfpDocument,
options,
timestamp: new Date().toISOString()
});
return response.data;
} catch (error) {
logger.error('RFP analysis failed:', error);
throw new Error(`Analyze API error: ${error.response?.data?.message || error.message}`);
}
},
/**
* Get RFP analysis results
*/
getRFPAnalysisResults: async (analysisId) => {
try {
const response = await analyzeApiClient.get(`/rfp/${analysisId}`);
return response.data;
} catch (error) {
if (error.response?.status === 404) {
return null;
}
logger.error(`Failed to get RFP analysis results for ${analysisId}:`, error);
throw new Error(`Analyze API error: ${error.response?.data?.message || error.message}`);
}
},
/**
* Generate RFP response
*/
generateRFPResponse: async (analysisId, sections, template = null, options = {}) => {
try {
const response = await analyzeApiClient.post(`/rfp/${analysisId}/generate-response`, {
sections,
template,
options,
timestamp: new Date().toISOString()
});
return response.data;
} catch (error) {
logger.error(`RFP response generation failed for ${analysisId}:`, error);
throw new Error(`Analyze API error: ${error.response?.data?.message || error.message}`);
}
},
/**
* Extract entities from text
*/
extractEntities: async (text, entityTypes = null, options = {}) => {
try {
const response = await analyzeApiClient.post('/extract-entities', {
text,
entityTypes,
options
});
return response.data;
} catch (error) {
logger.error('Entity extraction failed:', error);
throw new Error(`Analyze API error: ${error.response?.data?.message || error.message}`);
}
},
/**
* Extract topics from text
*/
extractTopics: async (text, options = {}) => {
try {
const response = await analyzeApiClient.post('/extract-topics', {
text,
options
});
return response.data;
} catch (error) {
logger.error('Topic extraction failed:', error);
throw new Error(`Analyze API error: ${error.response?.data?.message || error.message}`);
}
},
/**
* Extract sections from document
*/
extractSections: async (document, options = {}) => {
try {
const response = await analyzeApiClient.post('/extract-sections', {
document,
options
});
return response.data;
} catch (error) {
logger.error('Section extraction failed:', error);
throw new Error(`Analyze API error: ${error.response?.data?.message || error.message}`);
}
},
/**
* Summarize content
*/
summarizeContent: async (text, maxLength = null, options = {}) => {
try {
const response = await analyzeApiClient.post('/summarize', {
text,
maxLength,
options
});
return response.data;
} catch (error) {
logger.error('Summarization failed:', error);
throw new Error(`Analyze API error: ${error.response?.data?.message || error.message}`);
}
},
/**
* Classify content
*/
classifyContent: async (text, categories = null, options = {}) => {
try {
const response = await analyzeApiClient.post('/classify', {
text,
categories,
options
});
return response.data;
} catch (error) {
logger.error('Classification failed:', error);
throw new Error(`Analyze API error: ${error.response?.data?.message || error.message}`);
}
},
/**
* Get analysis history
*/
getAnalysisHistory: async (filters = {}) => {
try {
const response = await analyzeApiClient.get('/history', { params: filters });
return response.data;
} catch (error) {
logger.error('Failed to retrieve analysis history:', error);
throw new Error(`Analyze API error: ${error.response?.data?.message || error.message}`);
}
},
/**
* Get analysis history details
*/
getAnalysisHistoryDetails: async (analysisId) => {
try {
const response = await analyzeApiClient.get(`/history/${analysisId}`);
return response.data;
} catch (error) {
if (error.response?.status === 404) {
return null;
}
logger.error(`Failed to retrieve analysis history details for ${analysisId}:`, error);
throw new Error(`Analyze API error: ${error.response?.data?.message || error.message}`);
}
},
/**
* Delete analysis history
*/
deleteAnalysisHistory: async (analysisId) => {
try {
await analyzeApiClient.delete(`/history/${analysisId}`);
return true;
} catch (error) {
if (error.response?.status === 404) {
return false;
}
logger.error(`Failed to delete analysis history for ${analysisId}:`, error);
throw new Error(`Analyze API error: ${error.response?.data?.message || error.message}`);
}
}
};
export default analyzeService;