Skip to content

Sifa Agent Internal Team Test

This workflow implements the core conversational AI agent for Sifa, a business coaching chatbot designed for young entrepreneurs in Kenya. It processes incoming messages through a sophisticated intent analysis system and generates contextually appropriate responses using AI agents with access to business tracking tools and message templates.

Purpose

No business context provided yet — add a context.md to enrich this documentation.

How It Works

  1. Message Reception: The workflow receives an incoming message with user phone number, query text, and channel information
  2. User Context Loading: Retrieves user profile, business records, and recent chat history from the database
  3. Intent Analysis: An AI agent analyzes the conversation state, determines the current stage, validates data collection, and identifies system actions needed
  4. Conversation State Check: Evaluates whether the conversation is concluded or should continue processing
  5. Main Agent Processing: The primary AI agent generates appropriate responses using the intent analysis, user context, and available tools
  6. Response Generation: Creates structured JSON output containing messages for youth and potentially CEA alerts
  7. Message Logging: Saves the conversation turn to the chat history database
  8. Output Formatting: Returns the final response message for delivery

Mermaid Diagram

graph TD
    A[When Executed by Another Workflow] --> B[getUserRecord]
    B --> C[getProfitData]
    C --> D[getChatHistory]
    C --> E[Aggregate Business Records]
    D --> F[Limit Chat History]
    F --> G[chatHistory Aggregate]
    E --> H[Merge Data]
    G --> H
    H --> I[Aggregate All Data]
    I --> J[Intent Analysis Tool]
    J --> K[Json Object Parser]
    K --> L[Intent Analysis Object]
    L --> M{Conversation Concluded?}
    M -->|No| N[AI Agent]
    M -->|Yes| O[No Operation]
    N --> P[Structured Output Parser]
    P --> Q[Messages Formatter]
    Q --> R[Insert Chat Log]
    R --> S[setOutputField]

    T[Postgres Chat Memory] --> N
    U[OpenRouter Chat Model] --> N
    V[dailySalesDataCollection Tool] --> N
    W[updateUserDataTool] --> N
    X[updateUserStatusTool] --> N
    Y[messageTemplates Tool] --> N
    Z[Think Tool] --> J
    AA[OpenRouter Chat Model1] --> J

Trigger

This workflow is triggered by another workflow (webhook or parent workflow) that passes: - phoneNumber: User's phone number identifier - query: The incoming message text - channel: Communication channel (whatsapp/sms/callcenter)

Nodes Used

Node Type Node Name Purpose
Postgres getUserRecord Retrieves user profile from youthEntrepreneursReal table
Postgres getProfitData Fetches recent business records from dailyProfitTracking table
Postgres getChatHistory Gets conversation history from n8n_chat_histories table
Postgres Insert rows in a table Logs conversation turn to chatLog table
AI Agent Intent Analysis Tool Analyzes conversation state and determines next actions
AI Agent AI Agent Main conversational agent that generates responses
Workflow Tool dailySalesDataCollection Records daily sales and cost data
Workflow Tool updateUserDataTool Updates user profile information
Workflow Tool updateUserStatusTool Updates user onboarding status
Workflow Tool messageTemplates Retrieves appropriate message templates
LangChain Postgres Chat Memory Manages conversation memory
LangChain OpenRouter Chat Model AI language model for main agent
LangChain Structured Output Parser Parses AI responses into JSON format
Control If Node Checks if conversation is concluded
Data Aggregate/Merge/Set Data transformation and formatting

External Services & Credentials Required

Database Connections

  • PostgresOnSupabase: Database connection for user records, business data, and chat history
    • Tables: youthEntrepreneursReal, dailyProfitTracking, n8n_chat_histories, chatLog

AI Services

  • OpenRouter API: AI language model access
    • Credentials: "Prompt Testing for Design team" and "OpenRouter account"
    • Models: openai/gpt-5.2

Sub-Workflows

  • dailySalesDataCollection (ID: fBc6cSMp9Wz1brlZ)
  • updateUserDataTool (ID: xVkMVJOCq5ptCOHj)
  • updateUserStatusTool (ID: nfVcA8ei8x9dE06Z)
  • messageTemplates (ID: IIuvQx5MoV7CkTAJ)

Environment Variables

No explicit environment variables are defined in this workflow. Configuration is handled through: - Database connection credentials - OpenRouter API credentials
- Sub-workflow references by ID

Data Flow

Input

1
2
3
4
5
{
  "phoneNumber": "string",
  "query": "string", 
  "channel": "whatsapp|sms|callcenter"
}

Processing Data

  • User profile and business records from database
  • Chat history (last 25 messages)
  • Intent analysis JSON with conversation state
  • AI-generated response with tools integration

Output

1
2
3
{
  "output": "string"
}

The output contains the final message to be sent to the user, processed through message templates and formatted according to the channel requirements.

Error Handling

The workflow includes several error handling mechanisms:

  1. Data Validation: Intent analysis validates incoming data and flags outliers
  2. Conversation State Management: Checks if conversation is concluded before processing
  3. Graceful Degradation: "No Operation" node handles concluded conversations
  4. Database Error Handling: Uses alwaysOutputData: true on critical database queries
  5. JSON Parsing: Structured output parser ensures valid response format

Known Limitations

Based on the workflow configuration:

  1. Inactive Status: The workflow is currently marked as inactive ("active": false)
  2. Complex Dependencies: Relies on multiple sub-workflows that must be properly configured
  3. Database Dependencies: Requires specific database schema and data structure
  4. AI Model Costs: Uses premium AI models which may incur significant costs
  5. Language Processing: Designed specifically for Swahili/English mix (Sheng) communication

This workflow integrates with several sub-workflows: - dailySalesDataCollection: Handles business data recording - updateUserDataTool: Manages user profile updates
- updateUserStatusTool: Tracks onboarding progress - messageTemplates: Provides contextual message templates

Setup Instructions

  1. Import Workflow: Import the JSON configuration into your n8n instance

  2. Configure Database Connection:

    • Set up PostgreSQL connection named "PostgresOnSupabase"
    • Ensure required tables exist: youthEntrepreneursReal, dailyProfitTracking, n8n_chat_histories, chatLog
  3. Configure AI Services:

    • Set up OpenRouter API credentials
    • Verify access to openai/gpt-5.2 model
  4. Deploy Sub-Workflows:

    • Import and configure all referenced sub-workflows
    • Update workflow IDs if they differ in your environment
  5. Test Database Queries:

    • Verify all PostgreSQL nodes can connect and query successfully
    • Test with sample user data
  6. Activate Workflow:

    • Change "active": false to true in the workflow settings
    • Set up appropriate triggers or parent workflows
  7. Monitor and Debug:

    • Test with sample messages
    • Monitor chat logs and error outputs
    • Verify AI responses are appropriate and well-formatted