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¶
- Message Reception: The workflow receives an incoming message with user phone number, query text, and channel information
- User Context Loading: Retrieves user profile, business records, and recent chat history from the database
- Intent Analysis: An AI agent analyzes the conversation state, determines the current stage, validates data collection, and identifies system actions needed
- Conversation State Check: Evaluates whether the conversation is concluded or should continue processing
- Main Agent Processing: The primary AI agent generates appropriate responses using the intent analysis, user context, and available tools
- Response Generation: Creates structured JSON output containing messages for youth and potentially CEA alerts
- Message Logging: Saves the conversation turn to the chat history database
- 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
- Tables:
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 | |
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 | |
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:
- Data Validation: Intent analysis validates incoming data and flags outliers
- Conversation State Management: Checks if conversation is concluded before processing
- Graceful Degradation: "No Operation" node handles concluded conversations
- Database Error Handling: Uses
alwaysOutputData: trueon critical database queries - JSON Parsing: Structured output parser ensures valid response format
Known Limitations¶
Based on the workflow configuration:
- Inactive Status: The workflow is currently marked as inactive (
"active": false) - Complex Dependencies: Relies on multiple sub-workflows that must be properly configured
- Database Dependencies: Requires specific database schema and data structure
- AI Model Costs: Uses premium AI models which may incur significant costs
- Language Processing: Designed specifically for Swahili/English mix (Sheng) communication
Related Workflows¶
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¶
-
Import Workflow: Import the JSON configuration into your n8n instance
-
Configure Database Connection:
- Set up PostgreSQL connection named "PostgresOnSupabase"
- Ensure required tables exist:
youthEntrepreneursReal,dailyProfitTracking,n8n_chat_histories,chatLog
-
Configure AI Services:
- Set up OpenRouter API credentials
- Verify access to
openai/gpt-5.2model
-
Deploy Sub-Workflows:
- Import and configure all referenced sub-workflows
- Update workflow IDs if they differ in your environment
-
Test Database Queries:
- Verify all PostgreSQL nodes can connect and query successfully
- Test with sample user data
-
Activate Workflow:
- Change
"active": falsetotruein the workflow settings - Set up appropriate triggers or parent workflows
- Change
-
Monitor and Debug:
- Test with sample messages
- Monitor chat logs and error outputs
- Verify AI responses are appropriate and well-formatted