Article Β· 2024-01-01

Project Nighthawks: Building an AI-Powered Task Scheduler

System Architecture

Nighthawks follows principles of high availability, scalability, and security. Below is an overview of its layered architecture.


+--------------------------------+
|        UI & Access Layer       |
|  +-----------+   +-----------+ |
|  |  Web UI   |-->| RESTful API | |
|  +-----------+   +-----------+ |
+--------------------------------+
                 |
                 v
+--------------------------------+
| Application Service Layer (FastAPI) |
|  +-----------+                 |
|  | Main App  |-----------------+
|  +-----------+                 |
|   | |   |   |                  |
|   v v   v   v                  |
| [Auth][Scheduler][NLP][Metrics]|
+--------------------------------+
    |      |      |      |
    |      |      |      +------>[Prometheus]-->[Grafana]
    |      |      |
    |      |      +------------->[AI Layer: OpenAI, Graphiti]
    |      |
    |      +--------------------->[AI Agent]
    |      +--------------------->[Execution Logs]
    |
    +---------------------------->[Data Storage: PostgreSQL, Redis]

Key Modules

Natural Language Processing (NLP) Module

This module parses user intent into executable scheduling directives. When a user enters "Generate and send the sales report every Friday at 5 PM," the NLP module calls the OpenAI GPT model to extract both the Cron expression 0 17 * * 5 and the core task "generate and send sales report" for the AI Agent. Parsing accuracy exceeds 95%.

AI Agent and Knowledge Graph

Each task runs through an independent AI Agent. Before execution, the Agent queries the Graphiti knowledge graph to retrieve relevant historical information, user preferences, or contextual data. For example, if a previous report failed due to data source delays, the Agent may automatically check the data source status before execution. This feedback loop enables continuous improvement based on historical failures.

Task Scheduler (APScheduler)

APScheduler handles the underlying execution engine, providing stable and reliable distributed task scheduling. The AI layer decides "what to do and when," while the scheduler ensures "execution on time."

Data Model (SQLModel)

SQLModel combines Pydantic and SQLAlchemy, providing type-safe ORM operations that improve development efficiency and code robustness.

Technology Stack

Tier Technology Description
Web Framework FastAPI Asynchronous, high-performance API framework.
AI Engine OpenAI GPT + Graphiti GPT handles intent understanding; Graphiti retains contextual knowledge.
Task Scheduling APScheduler Mature, stable, supports multiple triggers and distributed deployments.
Containerization Docker + Kubernetes Standardized deployment and auto-scaling via HPA.
Monitoring Prometheus + Grafana Real-time visibility into system status and performance.

Usage Example

1. User enters a natural language command:

curl -X POST http://localhost:9527/api/v1/nlp/parse \
  -d '{"text": "Check server health and send an email notification every weekday at 9 AM"}'

2. Nighthawks parses the request:

The system returns a structured task definition:

{
  "success": true,
  "task_name": "Server Health Check",
  "cron_expression": "0 9 * * 1-5",
  "agent_prompt": "Check server health status and email the results to the administrator.",
  "confidence": 0.98
}

3. Create and schedule the task:

After user confirmation, the task is persisted to the database and scheduled by APScheduler.

4. Task execution:

At 9 AM every weekday, the scheduler triggers the corresponding AI Agent. The Agent first queries the knowledge graph and may discover that "Server B had high CPU usage at 9 AM last week," so it prioritizes checking Server B. After completing the health check, it generates a report and sends it via email. Execution details, including token consumption and runtime, are logged for future optimization.

Β© 2026 Yuxu Ge Β·