import BaseAgentService from '../BaseAgentService'; import { AGENT_ANALYTICS_DASHBOARD, AGENT_ANALYTICS_JOURNEY, AGENT_ANALYTICS_AI_INSIGHTS, AGENT_ANALYTICS_GAPS, AGENT_ANALYTICS_GAPS_PREPARE_KB_DRAFT, AGENT_ANALYTICS_GAPS_PRODUCT_SUGGESTION, AGENT_ANALYTICS_GAPS_STATE, AGENT_ANALYTICS_GAPS_SUGGEST_FIX, } from './agent-analytics.routes'; import type { AgentGapCategory, AgentGapStatus, IAgentDashboardData, IAgentGapFixRequest, IAgentGapFixSuggestion, IAgentGapKbDraft, IAgentGapKbDraftRequest, IAgentGapProductSuggestionRequest, IAgentGapProductSuggestionResponse, IAgentGapStateUpdateRequest, IAgentGapStateUpdateResponse, IAgentGapsResponse, IJourneyDetail, IJourneyListItem, IInsightsResponse, } from './agent-analytics.interface'; export const fetchAgentDashboard = async ( startDate: string, endDate: string ): Promise => { const response = await BaseAgentService.get( `${AGENT_ANALYTICS_DASHBOARD}?start_date=${startDate}&end_date=${endDate}` ); return response.data; }; export const fetchAgentJourneys = async ( startDate: string, endDate: string, limit = 100 ): Promise<{ journeys: IJourneyListItem[] }> => { const response = await BaseAgentService.get( `${AGENT_ANALYTICS_JOURNEY}?start_date=${startDate}&end_date=${endDate}&limit=${limit}` ); return response.data; }; export const fetchAgentJourneyDetail = async ( fingerprint: string ): Promise => { const response = await BaseAgentService.get( `${AGENT_ANALYTICS_JOURNEY}/${encodeURIComponent(fingerprint)}` ); return response.data; }; export const fetchAiInsights = async (): Promise => { const response = await BaseAgentService.get(AGENT_ANALYTICS_AI_INSIGHTS); return response.data; }; export const dismissAiInsight = async ( reportId: string, insightId: string ): Promise => { await BaseAgentService.post( `${AGENT_ANALYTICS_AI_INSIGHTS}/${reportId}/dismiss/${insightId}`, {} ); }; /** * Fetch one page of aggregated agent gaps for the period. * * @param {string} startDate - YYYY-MM-DD. * @param {string} endDate - YYYY-MM-DD. * @param {number} perPage - Items per page. * @param {string | null | undefined} cursor * @param {AgentGapCategory | null | undefined} category * @param {AgentGapStatus | null | undefined} status - Defaults to ``active``. * @return {Promise} */ export const fetchAgentGaps = async ( startDate: string, endDate: string, perPage: number, cursor: string | null | undefined, category: AgentGapCategory | null | undefined, status: AgentGapStatus | null | undefined = 'active' ): Promise => { const params: URLSearchParams = new URLSearchParams(); params.set('start_date', startDate); params.set('end_date', endDate); params.set('per_page', String(perPage)); if (cursor) params.set('cursor', cursor); if (category) params.set('category', category); if (status) params.set('status', status); const response = await BaseAgentService.get( `/${AGENT_ANALYTICS_GAPS}?${params.toString()}` ); return ( response?.data ?? { gaps: [], count: 0, total: 0, total_all_categories: 0, category_counts: {} as Record, next_cursor: null, } ); }; /** * @param {IAgentGapFixRequest} body * @return {Promise} */ export const suggestAgentGapFix = async ( body: IAgentGapFixRequest ): Promise => { const response = await BaseAgentService.post( `/${AGENT_ANALYTICS_GAPS_SUGGEST_FIX}`, body ); return response?.data ?? null; }; /** * @param {IAgentGapStateUpdateRequest} body * @return {Promise} */ export const updateAgentGapState = async ( body: IAgentGapStateUpdateRequest ): Promise => { const response = await BaseAgentService.patch( `/${AGENT_ANALYTICS_GAPS_STATE}`, body ); return response?.data ?? null; }; /** * Ask the agent to draft a KB entry scoped to one gap. The merchant edits * the returned ``{title, body}`` in a modal before publishing through * ``createTextEntry``; this call does not persist anything. * * @param {IAgentGapKbDraftRequest} body * @return {Promise} */ export const prepareGapKbDraft = async ( body: IAgentGapKbDraftRequest ): Promise => { const response = await BaseAgentService.post( `/${AGENT_ANALYTICS_GAPS_PREPARE_KB_DRAFT}`, body ); return response?.data ?? null; }; /** * Draft an editable storefront snippet (FAQ Q&A or description rewrite) * for one product-related gap. The agent identifies the product, looks * it up in the catalog, detects the storefront language via flash-lite, * and returns the snippet + a placement hint. * * @param {IAgentGapProductSuggestionRequest} body * @return {Promise} */ export const prepareProductSuggestionForGap = async ( body: IAgentGapProductSuggestionRequest ): Promise => { const response = await BaseAgentService.post( `/${AGENT_ANALYTICS_GAPS_PRODUCT_SUGGESTION}`, body ); return response?.data ?? null; };