Skip to content

Francis - SalesTrackingAgent - optimized

This workflow serves as an intelligent sales tracking agent that processes customer inquiries by retrieving their complete business context (chat history, profit records, credit information) from a PostgreSQL database and routing them to the Francis AI agent for personalized responses.

Purpose

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

How It Works

  1. Receives Input: The workflow is triggered by another workflow with customer details including phone number, query, communication channel, and API endpoint information
  2. Fetches Session Context: Queries the PostgreSQL database using a stored procedure to retrieve the customer's complete session context
  3. Processes User Data: Extracts and flattens user information from the database response for easier downstream processing
  4. Formats Context Data: Structures the customer's chat history, business records, credit information, and pending messages into JSON format
  5. Calls Francis AI: Sends the customer query along with their context to the Francis FastAPI service for intelligent processing
  6. Maps Response: Transforms the AI agent's reply into the expected output format
  7. Returns Result: Provides the final response as an output field for the calling workflow

Workflow Diagram

graph TD
    A[When Executed by Another Workflow] --> B[getSessionContext]
    B --> C[getUserRecord]
    C --> D[chatAndBusinessHistory]
    D --> E[Call Francis FastAPI]
    E --> F[Map reply → output]
    F --> G[setOutputField]

Trigger

Execute Workflow Trigger: This workflow is designed to be called by other workflows, not triggered directly. It expects the following input parameters: - phoneNumber - Customer's phone number for context lookup - query - Customer's message or question - channel - Communication channel (e.g., "whatsapp") - baseUrl - Base URL for the Francis API - webHookToken - Authentication token for the webhook

Nodes Used

Node Type Node Name Purpose
Execute Workflow Trigger When Executed by Another Workflow Receives input parameters from calling workflow
PostgreSQL getSessionContext Queries database for customer session context
Code getUserRecord Extracts and flattens user data from database response
Code chatAndBusinessHistory Formats context data (chat, business, credit records) as JSON
HTTP Request Call Francis (FastAPI) Sends customer query to Francis AI agent
Set Map reply → output Transforms AI response to expected format
Set setOutputField Prepares final output for return to calling workflow

External Services & Credentials Required

PostgreSQL Database

  • Credential: "Postgres account 2"
  • Purpose: Stores customer session context, chat history, business records, and credit information
  • Required Permissions: Execute access to get_session_context() stored procedure

Francis FastAPI Service

  • Credential: "FastAPI dispatcher" (HTTP Header Authentication)
  • Purpose: AI agent service that processes customer queries with context
  • Endpoint: {baseUrl}/webhooks/n8n/francis-turn/{webHookToken}
  • Timeout: 30 seconds

Environment Variables

This workflow uses dynamic URLs and tokens passed as input parameters rather than environment variables: - Base URL for Francis API (passed as baseUrl parameter) - Webhook authentication token (passed as webHookToken parameter)

Data Flow

Input

1
2
3
4
5
6
7
{
  "phoneNumber": "string",
  "query": "string", 
  "channel": "string",
  "baseUrl": "string",
  "webHookToken": "string"
}

Output

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

Internal Data Processing

  • Database returns customer context including user profile, chat history, profit records, credit information, and pending messages
  • Context data is formatted as JSON strings for the AI agent
  • Francis API receives customer query with full context and returns an intelligent response

Error Handling

  • getUserRecord node: Configured with retry on fail (max 2 attempts)
  • chatAndBusinessHistory node: Set to continue on error and always output data
  • HTTP Request timeout: 30-second timeout prevents hanging requests to Francis API

Known Limitations

No specific limitations documented in the workflow context.

This workflow is designed to be called by other workflows in the system but no specific related workflows are documented.

Setup Instructions

  1. Import the Workflow

    • Import the JSON into your n8n instance
    • The workflow will be created in draft mode
  2. Configure Database Credentials

    • Set up PostgreSQL credentials named "Postgres account 2"
    • Ensure the database contains the get_session_context() stored procedure
    • Grant appropriate permissions for the n8n user
  3. Configure FastAPI Credentials

    • Create HTTP Header Authentication credentials named "FastAPI dispatcher"
    • Configure the appropriate authentication header for your Francis API
  4. Verify Database Schema

    • Ensure your PostgreSQL database has the required stored procedure
    • Verify the procedure returns context with user, chat, profit, credit, and pending message data
  5. Test the Integration

    • Create a test workflow that calls this one with sample parameters
    • Verify the Francis API endpoint is accessible and returns expected responses
  6. Activate the Workflow

    • Once testing is complete, activate the workflow for production use
    • Monitor execution logs for any issues with database connections or API calls