Skip to main content

AI Manager Service

Table of Contents

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

  • sessionId string 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

  • transcript string The user's transcribed speech.
  • tenantId string The ID of the tenant.
  • userId string 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

  • content string The message content.
  • role string The role of the sender (e.g., "user", "ai").

constructAugmentedPrompt

Constructs the detailed, context-aware prompt for the Gemini LLM.

Parameters

  • transcript string The user's current request.
  • relevantDocuments [Array][24]<[string][21]> An array of relevant document snippets from the general vector DB.
  • insightContext string 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

  • tenantId string The ID of the tenant.
  • userId string The ID of the user.

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

  • unstructuredContext object Unstructured context from sources like uploaded documents.
  • structuredContext object Structured context from databases.
  • tenantId string 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

  • audioBytes Buffer The audio data in bytes.
  • encoding string The audio encoding (e.g., 'LINEAR16').
  • sampleRateHertz number The sample rate of the audio.
  • languageCode string 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

  • rawArticle object The raw article object from NewsAPI.
  • tenantId string The ID of the tenant for auditing.
  • userId string The ID of the user for auditing.

Returns Promise<EnrichedNewsArticle> The enriched article object.