File size: 2,850 Bytes
a1a5a09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# Chat Logging Implementation Summary

## Changes Made

### 1. **New File: `chat_logger.py`**
   - Implements local JSONL chat logging with thread-safe async processing
   - **Local logging**: Always enabled, writes to `logs/chat_logs.jsonl`
   - **Optional HuggingFace upload**: Only if `HF_TOKEN` and `HF_DATASET_REPO` environment variables are set
   - **Features**:
     - Queue-based async processing (non-blocking)
     - Automatic background worker thread
     - Periodic HuggingFace uploads (every 5 minutes)
     - Graceful shutdown with final flush
     - Automatic dataset creation if needed

### 2. **Updated `app.py`**
   - Added imports: `uuid4`, `log_chat`, `shutdown_logger`
   - Added `_session_id` state variable (unique per session)
   - Updated `sequential_thinking()`: Logs each thought with metadata
   - Updated `reset_session()`: Generates new session ID when resetting
   - Updated `__main__`: Calls `shutdown_logger()` on app exit for graceful cleanup

### 3. **New File: `LOGGING.md`**
   - Complete documentation of logging features
   - Configuration instructions
   - Environment variables reference
   - Data format examples
   - Troubleshooting guide

## Usage

### No Configuration (Local Logging Only)
```bash
python app.py
```
- Logs all thoughts to `logs/chat_logs.jsonl`
- No external dependencies required

### With HuggingFace Upload
```bash
export HF_TOKEN=hf_xxxxxxxxxxxxx
export HF_DATASET_REPO=username/my-logs
python app.py
```
- Logs locally AND uploads to HuggingFace every 5 minutes
- Automatic dataset creation

## What Gets Logged

Each thought entry includes:
- **session_id**: Unique session identifier
- **model_name**: `"sequential-thinking"`
- **thought**: The actual thought text
- **thought_number**: Step number in sequence
- **total_thoughts**: Estimated total steps
- **metadata**: Revision/branch information
- **timestamp**: ISO 8601 timestamp

Example:
```json
{
  "session_id": "abc123xyz...",
  "model_name": "sequential-thinking",
  "thought": "First, let me understand the problem...",
  "thought_number": 1,
  "total_thoughts": 5,
  "metadata": {
    "is_revision": false,
    "branch_id": null,
    "next_thought_needed": true
  },
  "timestamp": "2024-01-15T10:30:00.123456"
}
```

## Key Features

βœ… **Always on** - Local logging works without any configuration  
βœ… **Optional upload** - HuggingFace integration only if credentials set  
βœ… **Async processing** - Non-blocking, runs in background threads  
βœ… **Graceful shutdown** - Flushes all pending logs before exit  
βœ… **Session tracking** - Groups related thoughts by session ID  
βœ… **Error handling** - Continues working even if upload fails  

## Files Modified/Created

- βœ… Created: `chat_logger.py` (140 lines)
- βœ… Created: `LOGGING.md` (documentation)
- βœ… Updated: `app.py` (added logging integration)