Skip to content

Build-for-fun/second_soul

Repository files navigation

Second Soul — Life OS

A motive-driven, multi-agent personal AI system. Three core AI agents (Health, Finance, Productivity) plus dynamically generated sub-agents, all orchestrated by a Chief of Staff manager agent. Your primary life goal drives every decision.

Built for the Snowflake Hackathon with Snowflake Cortex (Llama 3.1 70b), CrewAI, Composio, and Skyfire.


Architecture

┌─────────────────────────────────────────────────────────────────────┐
│                     Frontend (Next.js :3000)                        │
│   Landing · Onboarding · Dashboard · Chat · Briefing · Agents       │
└──────────────────────────────┬──────────────────────────────────────┘
                               │ REST API
┌──────────────────────────────▼──────────────────────────────────────┐
│                    FastAPI Backend (:8000)                           │
│                                                                     │
│  ┌─────────────┐  ┌──────────────┐  ┌───────────────────────────┐  │
│  │   Routers   │  │   Services   │  │      CrewAI Crew          │  │
│  │             │  │              │  │                           │  │
│  │ /chat       │  │ cortex       │  │  ┌─────────────────────┐  │  │
│  │ /onboard    │  │ snowflake    │  │  │   Manager Agent     │  │  │
│  │ /dashboard  │  │ notification │  │  │  (Chief of Staff)   │  │  │
│  │ /agents     │  │ composio     │  │  └────────┬────────────┘  │  │
│  │ /briefing   │  │ skyfire      │  │           │ delegates     │  │
│  │ /actions    │  │ x_service    │  │  ┌────────▼────────────┐  │  │
│  │ /recommend  │  │ ml_cache     │  │  │ Health │ Finance │  │  │  │
│  │ /settings   │  │              │  │  │ Agent  │ Agent   │  │  │  │
│  │ /notifs     │  │              │  │  │────────┤─────────│  │  │  │
│  │ /ml         │  │              │  │  │ Productivity │    │  │  │  │
│  └─────────────┘  └──────────────┘  │  │ Agent        │    │  │  │  │
│                                     │  └──────────────┘    │  │  │  │
│                                     │  + Dynamic Sub-Agents│  │  │  │
│                                     └──────────────────────┘  │  │  │
└──────┬──────────────────┬──────────────────┬──────────────────┘  │
       │                  │                  │                     │
┌──────▼──────┐  ┌────────▼────────┐  ┌──────▼──────────────┐     │
│  Snowflake  │  │  Composio MCP   │  │   Skyfire MCP       │     │
│  Cortex LLM │  │                 │  │                     │     │
│  (Llama 3.1 │  │  Gmail          │  │  KYA+PAY Tokens     │     │
│   70b)      │  │  Discord        │  │  Autonomous         │     │
│             │  │  Google Calendar │  │  Payments            │     │
│  FORECAST   │  │  Google Sheets  │  │                     │     │
│  ANOMALY    │  │                 │  │                     │     │
│  COMPLETE   │  │                 │  │                     │     │
└─────────────┘  └─────────────────┘  └─────────────────────┘

How It Works

Agent Hierarchy

The system uses CrewAI hierarchical orchestration — a Manager (Chief of Staff) delegates tasks to specialist agents based on your motive priorities:

User Message
  │
  ├─ Greeting ──────────────────────── Fast response (no crew)
  ├─ "Log meal: ..." ───────────────── Cortex extraction → DB insert
  ├─ "Log expense: ..." ────────────── Cortex extraction → DB insert + alerts
  ├─ "Approve coffee machine" ──────── Skyfire token → purchase approved
  └─ Complex query ─────────────────── Full crew orchestration
        │
        ▼
    Manager delegates to Health / Finance / Productivity agents
        │
        ▼
    Agents query Snowflake, call tools, reason together
        │
        ▼
    Synthesized response aligned with your life motive

Dynamic Sub-Agents

The system auto-detects patterns in your data and suggests specialized agents:

  • Logged 5 coffees this week? → "Coffee Spending Monitor" agent suggested
  • Sleep declining 3 days in a row? → "Sleep Optimizer" agent suggested
  • X/Twitter shows fitness interest? → "Workout Planner" agent suggested

You approve or dismiss — approved agents join the crew.

Purchase Recommendations with Skyfire

/recommendations/generate
  │
  ▼
3 domain agents each propose 3 recommendations (9 total)
  │
  ▼
Manager reviews against budget + motive alignment scores
  │
  ▼
Final 3 selected (deduplicated vs last 7 days)
  │
  ▼
User approves in chat → Skyfire KYA+PAY JWT created
  │
  ▼
Token stored, decision logged, notification sent

Multi-Channel Notifications (Outbox Pattern)

Agent event → enqueue_notification_event() → notification_events table
                                                      │
                                              schedule_dispatch()
                                                      │
                                              resolve channel from
                                              user preferences
                                                      │
                                    ┌─────────────────┼─────────────────┐
                                    ▼                 ▼                 ▼
                                  Gmail           Discord          Calendar
                                (Composio)      (Bot API)        (Composio)

Each domain (health, finance, productivity) routes to the user's preferred channel. Supports Gmail, Discord DM (rich embeds with purchase buttons), Google Calendar events, and Google Sheets logging.


Tech Stack

Layer Technology
Frontend Next.js 16, React 19, TypeScript, Tailwind CSS 4, Recharts
Backend Python FastAPI, CrewAI (hierarchical process)
LLM Snowflake Cortex — Llama 3.1 70b
Database Snowflake (12 tables + ML functions: FORECAST, ANOMALY_DETECTION)
Notifications Composio MCP (Gmail, Calendar, Sheets) + Discord Bot API
Payments Skyfire MCP (KYA+PAY token generation)
Social X/Twitter API (interest analysis, agent suggestions)

Database Schema

12 Snowflake tables powering the system:

Table Purpose
user_profiles User identity, motive, context document (VARIANT)
meals_log Meal tracking with nutritional breakdown
transactions Expense tracking with categories
health_metrics Sleep, exercise, mood, water intake
tasks Productivity task management
agent_definitions Dynamic sub-agent registry (suggested/active/hibernated)
agent_decisions Audit trail for cross-agent decisions
weekly_reports Generated briefings with motive progress scores
onboarding_conversations Multi-turn onboarding transcripts
notification_events Outbox for preference-based notifications
purchase_recommendations Skyfire-powered purchase suggestions
x_activity X/Twitter data for interest analysis

Cortex ML Functions used:

  • FORECAST() — Spending prediction with confidence intervals
  • ANOMALY_DETECTION() — Health metric anomaly detection
  • COMPLETE() — LLM completion (Llama 3.1 70b)

API Endpoints

All routes prefixed with /api/:

Route Method Description
/chat POST Send message to agent crew (intent detection + orchestration)
/onboard/start POST Begin guided onboarding survey
/onboard/oneshot POST One-shot onboarding with free-form description
/onboard/respond POST Continue onboarding conversation
/onboard/complete POST Generate context document from transcript
/dashboard GET Aggregated metrics (30-day rolling)
/dashboard/charts GET Time-series chart data (14-day)
/agents GET List all agents (active, suggested, hibernated)
/agents/detect-patterns POST Trigger sub-agent suggestion
/agents/analyze-x POST Analyze X/Twitter for agent suggestions
/agents/approve/{id} POST Approve a suggested agent
/agents/hibernate/{id} POST Temporarily disable an agent
/briefing/generate POST Generate weekly intelligence briefing
/briefing GET Retrieve cached briefing
/actions/log-meal POST Log meal with NLP extraction
/actions/log-expense POST Log expense with NLP extraction
/recommendations/generate POST Generate purchase recommendations
/recommendations GET List purchase recommendations
/recommendations/{id}/approve POST Approve + create Skyfire token
/recommendations/{id}/dismiss POST Dismiss recommendation
/settings/{user_id} GET Get communication preferences
/settings/{user_id}/communication PUT Update channel preferences
/notifications GET List notification events
/notifications/dispatch POST Trigger pending notification dispatch

Project Structure

├── backend/
│   ├── app/
│   │   ├── main.py                    # FastAPI entry point
│   │   ├── config.py                  # Environment configuration
│   │   ├── crew/
│   │   │   ├── config/
│   │   │   │   ├── agents.yaml        # Agent definitions (role, goal, backstory)
│   │   │   │   └── tasks.yaml         # Task templates with variable interpolation
│   │   │   ├── crew.py                # @CrewBase class + run_crew() + run_recommendation_crew()
│   │   │   ├── agent_factory.py       # Dynamic sub-agent generation + pattern detection
│   │   │   └── tools.py              # @tool functions (Snowflake queries, logging, payments)
│   │   ├── routers/                   # FastAPI route handlers
│   │   │   ├── chat.py               # Chat with intent detection + purchase approval
│   │   │   ├── onboarding.py         # Multi-turn onboarding survey
│   │   │   ├── dashboard.py          # Metrics + charts + AI insights
│   │   │   ├── agents.py             # Agent CRUD + pattern detection
│   │   │   ├── briefing.py           # Weekly briefing generation
│   │   │   ├── actions.py            # Meal/expense logging
│   │   │   ├── recommendations.py    # Purchase recommendations + Skyfire
│   │   │   ├── settings.py           # Communication preferences
│   │   │   └── notifications.py      # Notification management
│   │   └── services/
│   │       ├── snowflake_client.py    # Connection pooling + queries
│   │       ├── cortex.py             # Snowflake Cortex LLM + ML wrappers
│   │       ├── notification_service.py # Outbox pattern notifications
│   │       ├── composio_service.py    # Composio MCP integration
│   │       ├── skyfire_service.py     # Skyfire payment tokens
│   │       └── x_service.py          # X/Twitter API
│   └── requirements.txt
├── frontend/
│   └── src/
│       ├── app/                       # Next.js App Router pages
│       │   ├── page.tsx              # Landing page
│       │   ├── onboarding/           # Onboarding flow
│       │   ├── dashboard/            # Metrics dashboard
│       │   ├── chat/                 # Chat interface
│       │   ├── briefing/             # Weekly briefing
│       │   ├── agents/               # Agent management
│       │   └── settings/             # Preferences
│       ├── components/                # Reusable UI components
│       │   ├── Sidebar.tsx
│       │   ├── ChatMessage.tsx
│       │   ├── MetricCard.tsx
│       │   ├── AgentCard.tsx
│       │   └── NebulaCanvas.tsx
│       └── lib/
│           └── api.ts                # Axios API client
└── sql/
    ├── setup.sql                      # Database + table creation
    └── seed_data.sql                  # Sample data

Getting Started

Prerequisites

  • Python 3.11+
  • Node.js 18+
  • Snowflake account with Cortex enabled
  • Composio API key (for Gmail, Calendar, Sheets)
  • Skyfire API key (for payment tokens)

1. Database Setup

Run in Snowflake SQL editor:

-- Create database and tables
SOURCE sql/setup.sql;

-- Insert sample data (replace USER_ID_HERE with your UUID)
SOURCE sql/seed_data.sql;

2. Backend

cd backend
python -m venv venv
source venv/bin/activate
pip install -r requirements.txt

# Configure environment
cp .env.example .env
# Edit .env with your Snowflake, Composio, Skyfire, Discord credentials

# Run
python -m uvicorn app.main:app --reload --port 8000

3. Frontend

cd frontend
npm install
npm run dev

Open http://localhost:3000 to start.


Environment Variables

Required in backend/.env:

# Snowflake
SNOWFLAKE_ACCOUNT=your_account
SNOWFLAKE_USER=your_user
SNOWFLAKE_PASSWORD=your_password
SNOWFLAKE_WAREHOUSE=COMPUTE_WH
SNOWFLAKE_DATABASE=LIFE_OS
SNOWFLAKE_SCHEMA=PUBLIC
SNOWFLAKE_PAT=your_pat_token

# Integrations
COMPOSIO_API_KEY=your_composio_key
SKYFIRE_API_KEY=your_skyfire_key

# Discord Bot (for notification DMs)
DISCORD_BOT_TOKEN=your_bot_token
DISCORD_USER_ID=your_discord_user_id

# X/Twitter (optional, for social analysis)
X_API_KEY=your_key
X_API_SECRET=your_secret
X_ACCESS_TOKEN=your_token
X_ACCESS_SECRET=your_secret

Key Features

  • Motive-Driven AI — Every agent decision is aligned with your primary life goal
  • Hierarchical Agent Orchestration — Manager delegates to specialists, synthesizes unified responses
  • Dynamic Sub-Agents — Auto-detected from your data patterns, user-approved
  • Natural Language Logging — "Log meal: chicken salad 450cal" → structured data
  • Autonomous Purchases — Skyfire-powered payment tokens for agent-recommended products
  • Multi-Channel Alerts — Gmail, Discord (rich embeds), Calendar, Sheets — per-domain preferences
  • Snowflake ML — Spending forecasts and health anomaly detection built into the data layer
  • Weekly Briefings — Cross-agent intelligence reports with motive progress scoring

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors