# iCoach Project Implementation Reference

## Project Location
`/home/agentuser/icoach/icoach/` — Next.js 16 app on Tencent Cloud server (111.229.192.217)

## Key Files

| File | Role |
|------|------|
| `src/lib/llm.ts` | LLM calls: `generateCoachResponse`, system prompt, style modules |
| `src/types/index.ts` | Type definitions including `SegmentType`, `CoachResponse` |
| `src/app/api/coach/route.ts` | API route, safety detection, mode routing |
| `src/app/(main)/session/[id]/SessionClient.tsx` | Frontend: segment rendering engine, streaming UI |
| `.env.local` | DeepSeek API key (`DEEPSEEK_API_KEY`), model config |

## Segment Rendering

Segments are rendered with staggered delays:

```
reflect (0ms) → challenge (60% of reflectDelay) → question (40% more delay)
```

- `reflect`: Plain text
- `challenge`: Italic + 3px left accent border + secondary color
- `question`: `→` prefix in warm accent color

Delay config by style (REFLECT_DELAY):
- warm: 1200ms
- direct: 700ms
- quiet: 2000ms
- curious: 1000ms

## Model Configuration

```env
DEEPSEEK_API_KEY=sk-...
DEEPSEEK_API_BASE=https://api.deepseek.com
MODEL_EXPLORE=deepseek-v4-pro  # coaching responses
MODEL_INTEGRATE=deepseek-v4-pro  # session summaries
```

## Service Management

- Port: 5000 (Cloudflare Tunnel points here)
- Public URL: https://icoach.chat
- Login password: in `.env.local` as `ICOACH_PASSWORD=icoach`

### Restart safely

```bash
# 1. Kill ALL processes on port
lsof -ti:5000 | xargs kill -9
sleep 2

# 2. Verify port free
ss -tlnp | grep 5000  # should be empty

# 3. Build (if source changed)
cd /home/agentuser/icoach/icoach && npm run build

# 4. Start
cd /home/agentuser/icoach/icoach && node node_modules/.bin/next start -p 5000

# 5. Verify
curl -s -o /dev/null -w "%{http_code}" http://localhost:5000/  # should be 307
```

## Database
Local JSON file: `/home/agentuser/icoach/icoach/icoach.json`
No external DB dependency (removed Supabase).
