# Next Session: Fix WhatsApp Image Intent Detection

## What

Annie's image intent detection uses brittle regex that fails on natural language. Rajesh sent "Suggest me a trekking near Bangalore, to get started and send me some pics" — Annie responded with text promising pics but never searched/sent images. Root cause: `detect_image_intent()` regex matched `get` in "get started" instead of `send` in "send me some pics", extracted empty query, returned False.

Rajesh's feedback: **"We have an LLM. Why are we still doing regex?"** — Replace regex-based intent detection with LLM-based.

## Current State

- `trigger.py:57` — `detect_image_intent()` is sync, uses 3 regex patterns (IMAGE_REQUEST_RE, IMAGE_FALSE_POSITIVE_RE, _IMAGE_PREFIX_RE)
- Called from `agent.py:427` — `is_image, search_query = detect_image_intent(text)` (sync call, needs `await` if made async)
- Image search pipeline works fine (`image_search.py` → SearXNG → download → `wa_sender.send_image`)
- The ONLY problem is intent detection + query extraction

## Failing Cases

```
"Suggest me a trekking near Bangalore, to get started and send me some pics"
→ Regex matches "get started...pics" (wrong verb), extracts empty query → False

"send me some pics" (at end of message, no "of X" after)
→ Regex matches but query extraction strips everything → empty query → False
```

## Fix: Replace with LLM-based Detection

Make `detect_image_intent()` async. Use Gemma 4 with a structured prompt:

1. **Quick regex pre-filter** — only call LLM if message contains image-related words (pic/photo/image/picture). Saves LLM calls for 95% of messages.
2. **LLM prompt** — "Does this message ask to send/show an image? Reply YES|<search query> or NO"
3. **Make call site async** — `agent.py:427` needs `await`

## Also Fix: Double DM Response

Both DOM poll and DM poll trigger for the same message when `GROUP_NAME == TRIGGER_SENDER` (current Panda config). Simplest fix: skip `_dm_poll_loop` when `GROUP_NAME == TRIGGER_SENDER`.

## Files to Modify

1. `services/whatsapp-agent/trigger.py` — Replace regex `detect_image_intent` with async LLM version
2. `services/whatsapp-agent/agent.py` — Add `await` to `detect_image_intent` call; skip DM poll when GROUP_NAME == TRIGGER_SENDER
3. `services/whatsapp-agent/tests/test_image_intent.py` — Update tests for async + LLM mock
4. `services/whatsapp-agent/tests/test_agent_poll.py` — Test DM poll skip logic

## Start Command

```
cat docs/NEXT-SESSION-WHATSAPP-IMAGE-INTENT.md
```

## Verification

1. Run tests: `cd services/whatsapp-agent && python3 -m pytest tests/ -v`
2. Deploy: `./stop.sh whatsapp && ./start.sh whatsapp`
3. Send DM: "Suggest me a trekking near Bangalore and send me some pics"
4. Verify: Annie responds WITH images (check logs for `[AGENT] Image intent detected`)
5. Verify: Only ONE response (no double trigger)
