AI Manager Service
Table of Contents
- AIManager
AIManager
Manages AI-driven functionalities for the ChainAlign application. This class provides methods for processing user input, generating dynamic content, and integrating with various AI and data services.
Parameters
sessionIdstring The session ID for conversation history persistence (e.g., with Zep). (optional, default"default-sop-session")
initZepSession
Initializes a Zep session for conversation history persistence. This method is called fire-and-forget during AIManager instantiation. It handles cases where the session already exists gracefully.
Returns Promise<void> A promise that resolves when the Zep session is initialized or confirmed to exist.
processTranscript
Processes a transcribed utterance to generate a structured chart query.
Parameters
transcriptstring The user's transcribed speech.tenantIdstring The ID of the tenant.userIdstring The ID of the user.
Returns [Promise][22]<[object][23]> A structured JSON object for the charting engine.
addMessageToZep
Adds a message to the Zep session.
Parameters
constructAugmentedPrompt
Constructs the detailed, context-aware prompt for the Gemini LLM.
Parameters
transcriptstring The user's current request.relevantDocuments[Array][24]<[string][21]> An array of relevant document snippets from the general vector DB.insightContextstring Formatted string of relevant S&OP insights from the knowledge base.
Returns string The full prompt to be sent to the LLM.
generateAlertPage
Generates a dynamic UI page JSON based on a detected alert. It fetches alert data from a backend endpoint and uses Gemini to construct a structured JSON object that describes the layout and components of an alert page.
Parameters
Returns [Promise][22]<([object][23] | null)> A JSON object representing the dynamic page layout, or null if no alert is detected or an error occurs.
generateDynamicPageFromContext
Generates a dynamic UI page JSON based on unstructured and structured context. This method first checks for active alerts and, if none, constructs a prompt for Gemini to generate a structured JSON object for dynamic UI rendering, typically for insights display.
Parameters
unstructuredContextobject Unstructured context from sources like uploaded documents.structuredContextobject Structured context from databases.tenantIdstring The ID of the tenant. {/* */}- Throws Error If the LLM output does not conform to the expected page layout schema.
Returns [Promise][22]<[object][23]> A JSON object representing the dynamic page layout.
handleTranscriptionRequest
Handles a transcription request by transcribing audio and storing it in Firestore.
Parameters
audioBytesBuffer The audio data in bytes.encodingstring The audio encoding (e.g., 'LINEAR16').sampleRateHertznumber The sample rate of the audio.languageCodestring The language code (e.g., 'en-US').
- Throws Error If an error occurs during NLP processing.
Returns [Promise][22]<[object][23]> A promise that resolves with the structured query object.
enrichNewsArticle
Enriches a raw news article with AI-derived insights using Google Gemini. It extracts country information, fetches relevant World Bank data, and constructs a prompt for Gemini to analyze the article and provide structured S&OP impact assessments.
Parameters
rawArticleobject The raw article object from NewsAPI.tenantIdstring The ID of the tenant for auditing.userIdstring The ID of the user for auditing.
Returns Promise<EnrichedNewsArticle> The enriched article object.