destinyebuka commited on
Commit
161761f
·
1 Parent(s): 33c989a
app/ai/agent/agent_hub.py CHANGED
@@ -37,12 +37,14 @@ AGENT_GENERAL = "general" # brain.py — default for everything
37
  AGENT_CONCIERGE = "concierge" # short-stay booking flow
38
  AGENT_BROKER = "broker" # viewing scheduling + deal flow
39
  AGENT_MATCHER = "matcher" # roommate matching
 
40
 
41
  HANDOFF_MAP = {
42
  "HANDOFF_CONCIERGE": AGENT_CONCIERGE,
43
  "HANDOFF_BROKER": AGENT_BROKER,
44
  "HANDOFF_MATCHER": AGENT_MATCHER,
45
  "HANDOFF_GENERAL": AGENT_GENERAL,
 
46
  }
47
 
48
  # ============================================================
@@ -56,6 +58,9 @@ _BOOK_TAG_PATTERN = _re_hub_signals.compile(r'\[BOOK_LISTING:[^\]]+\]')
56
  _COMPARE_TAG_PATTERN = _re_hub_signals.compile(r'\[COMPARE_LISTINGS:[^\]]+\]|\[COMPARE_PROPERTIES:[^\]]+\]')
57
  _SCHEDULE_TAG_PATTERN = _re_hub_signals.compile(r'\[SCHEDULE_VIEWING:[^\]]+\]')
58
 
 
 
 
59
  # Roommate — seeker intent in any language
60
  _ROOMMATE_SIGNALS = [
61
  "roommate", "room mate", "colocataire", "colocation", "co-living", "coliving",
@@ -79,6 +84,10 @@ def _fast_route(message: str, state: AgentState) -> Optional[str]:
79
  """
80
  msg = message.lower()
81
 
 
 
 
 
82
  # Button-tag triggers (highest priority — unambiguous UI actions)
83
  if _BOOK_TAG_PATTERN.search(message) or _COMPARE_TAG_PATTERN.search(message):
84
  return AGENT_CONCIERGE
@@ -193,7 +202,7 @@ async def classify_intent(
193
  caller="hub_classify_intent",
194
  )
195
  result = response.content.strip().lower().split()[0]
196
- if result in (AGENT_GENERAL, AGENT_CONCIERGE, AGENT_BROKER, AGENT_MATCHER):
197
  logger.info("Hub LLM-classified intent", result=result, message=message[:60])
198
  return result
199
  except Exception as e:
@@ -221,6 +230,10 @@ async def _dispatch(agent_id: str, state: AgentState) -> AgentState:
221
  from app.ai.agent.matcher_brain import process
222
  return await process(state)
223
 
 
 
 
 
224
  else:
225
  # general or unknown → general brain (brain.py agent_think)
226
  from app.ai.agent.brain import agent_think
@@ -781,4 +794,5 @@ def get_agent_display_name(agent_id: str) -> str:
781
  AGENT_CONCIERGE: "AIDA-Primary (Concierge)",
782
  AGENT_BROKER: "AIDA-Market (Broker)",
783
  AGENT_MATCHER: "AIDA-Social (Matcher)",
 
784
  }.get(agent_id, "AIDA")
 
37
  AGENT_CONCIERGE = "concierge" # short-stay booking flow
38
  AGENT_BROKER = "broker" # viewing scheduling + deal flow
39
  AGENT_MATCHER = "matcher" # roommate matching
40
+ AGENT_SCOUT = "scout" # proactive property-hunting agent
41
 
42
  HANDOFF_MAP = {
43
  "HANDOFF_CONCIERGE": AGENT_CONCIERGE,
44
  "HANDOFF_BROKER": AGENT_BROKER,
45
  "HANDOFF_MATCHER": AGENT_MATCHER,
46
  "HANDOFF_GENERAL": AGENT_GENERAL,
47
+ "HANDOFF_SCOUT": AGENT_SCOUT,
48
  }
49
 
50
  # ============================================================
 
58
  _COMPARE_TAG_PATTERN = _re_hub_signals.compile(r'\[COMPARE_LISTINGS:[^\]]+\]|\[COMPARE_PROPERTIES:[^\]]+\]')
59
  _SCHEDULE_TAG_PATTERN = _re_hub_signals.compile(r'\[SCHEDULE_VIEWING:[^\]]+\]')
60
 
61
+ # @Scout / @Démarcheur mention — highest priority, always routes to Scout
62
+ _SCOUT_MENTION_PATTERN = _re_hub_signals.compile(r'@[Ss]cout|@[Dd][eé]marcheur', _re_hub_signals.IGNORECASE)
63
+
64
  # Roommate — seeker intent in any language
65
  _ROOMMATE_SIGNALS = [
66
  "roommate", "room mate", "colocataire", "colocation", "co-living", "coliving",
 
84
  """
85
  msg = message.lower()
86
 
87
+ # @Scout / @Démarcheur mention — absolute highest priority
88
+ if _SCOUT_MENTION_PATTERN.search(message):
89
+ return AGENT_SCOUT
90
+
91
  # Button-tag triggers (highest priority — unambiguous UI actions)
92
  if _BOOK_TAG_PATTERN.search(message) or _COMPARE_TAG_PATTERN.search(message):
93
  return AGENT_CONCIERGE
 
202
  caller="hub_classify_intent",
203
  )
204
  result = response.content.strip().lower().split()[0]
205
+ if result in (AGENT_GENERAL, AGENT_CONCIERGE, AGENT_BROKER, AGENT_MATCHER, AGENT_SCOUT):
206
  logger.info("Hub LLM-classified intent", result=result, message=message[:60])
207
  return result
208
  except Exception as e:
 
230
  from app.ai.agent.matcher_brain import process
231
  return await process(state)
232
 
233
+ elif agent_id == AGENT_SCOUT:
234
+ from app.ai.agent.scout_brain import process
235
+ return await process(state)
236
+
237
  else:
238
  # general or unknown → general brain (brain.py agent_think)
239
  from app.ai.agent.brain import agent_think
 
794
  AGENT_CONCIERGE: "AIDA-Primary (Concierge)",
795
  AGENT_BROKER: "AIDA-Market (Broker)",
796
  AGENT_MATCHER: "AIDA-Social (Matcher)",
797
+ AGENT_SCOUT: "Scout",
798
  }.get(agent_id, "AIDA")
app/ai/agent/scout_brain.py ADDED
@@ -0,0 +1,292 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app/ai/agent/scout_brain.py
2
+ """
3
+ Scout (EN) / Démarcheur (FR) — Proactive property-hunting agent.
4
+
5
+ Scout is triggered whenever the user mentions @Scout or @Démarcheur in
6
+ AIDA chat or AIDA DM. She manages search mandates: hunts properties in
7
+ the background and delivers results as personal DM messages.
8
+
9
+ Mandate lifecycle:
10
+ ACTIVE → Scout is searching; results delivered via DM
11
+ PAUSED → User paused the mandate
12
+ COMPLETED → User found what they wanted
13
+ EXPIRED → Deadline passed or 30 days of inactivity
14
+
15
+ Scout NEVER initiates conversation — users must @mention her.
16
+ Scout NEVER repeats the same listing twice per mandate.
17
+ """
18
+
19
+ import re
20
+ import json
21
+ import random
22
+ import logging
23
+ from datetime import datetime, timedelta
24
+ from typing import Optional
25
+
26
+ from structlog import get_logger
27
+ from bson import ObjectId
28
+
29
+ from app.ai.agent.state import AgentState
30
+ from app.ai.agent.actions import AgentAction, set_action
31
+ from app.ai.agent.brain import _invoke_with_fallback, generate_localized_response
32
+ from app.database import get_db
33
+ from app.models.scout_mandate import ScoutMandate, MandateStatus
34
+
35
+ logger = get_logger(__name__)
36
+
37
+ # ── Persona ──────────────────────────────────────────────────────────────────
38
+
39
+ _SCOUT_INTRO = {
40
+ "en": "I'm Scout — I hunt properties for you in the background.",
41
+ "fr": "Je suis Scout — je cherche des biens pour toi en arrière-plan.",
42
+ }
43
+
44
+ _CONFIRM_CREATE = {
45
+ "en": [
46
+ "On it! I'll start hunting and ping you in this DM when I find something good. 🔍",
47
+ "Mandate received. I'll search quietly and drop results right here when I find matches. 🏠",
48
+ "Got it — I'll track this for you. I'll message you here whenever something fits.",
49
+ "Consider it done. I'll keep an eye out and DM you when something shows up.",
50
+ ],
51
+ "fr": [
52
+ "C'est noté ! Je commence à chercher et je te préviens ici dès que je trouve quelque chose. 🔍",
53
+ "Mandat reçu. Je cherche en silence et je t'envoie les résultats ici dès qu'il y a des correspondances. 🏠",
54
+ "Compris — je vais surveiller ça pour toi. Je te dis ici quand quelque chose correspond.",
55
+ "Considère c'est fait. Je garde un œil et je t'écris dès qu'un bien apparaît.",
56
+ ],
57
+ }
58
+
59
+ _CONFIRM_PAUSE = {
60
+ "en": "OK, I've paused that mandate. Say '@Scout resume' whenever you want me back on it.",
61
+ "fr": "OK, j'ai mis ce mandat en pause. Dis '@Scout reprends' quand tu veux que je reprenne.",
62
+ }
63
+
64
+ _CONFIRM_STOP = {
65
+ "en": "Mandate stopped. Let me know if you ever need me to search for something else.",
66
+ "fr": "Mandat arrêté. Dis-moi si tu as besoin que je cherche autre chose.",
67
+ }
68
+
69
+ _NO_MANDATES = {
70
+ "en": "You don't have any active mandates right now. Tell me what to hunt — for example: '@Scout find me a 2-bed apartment in Cotonou under 150k'.",
71
+ "fr": "Tu n'as aucun mandat actif pour le moment. Dis-moi ce qu'il faut chercher — par exemple : '@Scout trouve-moi un appartement 2 chambres à Cotonou sous 150k'.",
72
+ }
73
+
74
+
75
+ def _pick(pool: list) -> str:
76
+ return random.choice(pool)
77
+
78
+
79
+ def _lang(state: AgentState) -> str:
80
+ return (state.language_detected or state.app_language or "en")[:2].lower()
81
+
82
+
83
+ # ── Mandate extraction via LLM ────────────────────────────────────────────────
84
+
85
+ _EXTRACT_PROMPT = """You are a parameter extractor for Scout, a property-hunting agent.
86
+
87
+ Extract search mandate parameters from the user's message. Reply with ONLY valid JSON.
88
+
89
+ User message: {message}
90
+
91
+ Return this schema (omit any field that is not mentioned):
92
+ {{
93
+ "action": "create" | "list" | "pause" | "stop" | "resume" | "status",
94
+ "search_params": {{
95
+ "location": "string or null",
96
+ "listing_type": "rent" | "sale" | "short-stay" | "roommate" | null,
97
+ "max_price": number or null,
98
+ "min_price": number or null,
99
+ "bedrooms": number or null,
100
+ "amenities": ["string"] or null
101
+ }},
102
+ "deadline_days": number or null,
103
+ "user_query": "cleaned original intent, max 80 chars"
104
+ }}
105
+
106
+ Rules:
107
+ - "action" is REQUIRED — infer from context (mention of "find", "hunt", "search" = create; "pause", "stop", "cancel" = stop; "list", "what mandates", "my mandates" = list; "resume", "continue", "restart" = resume).
108
+ - If action is not create, omit search_params.
109
+ - deadline_days: only set if user mentions a time limit (e.g. "find within 3 days" → 3).
110
+ - Reply with ONLY the JSON object. No explanation."""
111
+
112
+
113
+ async def _extract_mandate(message: str, state: AgentState) -> dict:
114
+ from langchain_core.messages import HumanMessage
115
+ try:
116
+ prompt = _EXTRACT_PROMPT.format(message=message)
117
+ resp = await _invoke_with_fallback(
118
+ [HumanMessage(content=prompt)],
119
+ caller="scout_extract_mandate",
120
+ )
121
+ raw = resp.content.strip()
122
+ # strip markdown fences if present
123
+ raw = re.sub(r"^```json\s*", "", raw, flags=re.IGNORECASE)
124
+ raw = re.sub(r"```$", "", raw)
125
+ return json.loads(raw)
126
+ except Exception as e:
127
+ logger.warning("Scout: mandate extraction failed", error=str(e))
128
+ return {"action": "create", "search_params": {}, "user_query": message[:80]}
129
+
130
+
131
+ # ── DB helpers ────────────────────────────────────────────────────────────────
132
+
133
+ async def _get_active_mandates(user_id: str) -> list[ScoutMandate]:
134
+ db = await get_db()
135
+ cursor = db["scout_mandates"].find({
136
+ "user_id": user_id,
137
+ "status": {"$in": [MandateStatus.ACTIVE.value, MandateStatus.PAUSED.value]},
138
+ })
139
+ mandates = []
140
+ async for doc in cursor:
141
+ mandates.append(ScoutMandate(**doc))
142
+ return mandates
143
+
144
+
145
+ async def _create_mandate(user_id: str, params: dict, query: str, language: str, deadline_days: Optional[int]) -> ScoutMandate:
146
+ db = await get_db()
147
+ deadline = datetime.utcnow() + timedelta(days=deadline_days) if deadline_days else None
148
+ mandate = ScoutMandate(
149
+ user_id=user_id,
150
+ search_params=params,
151
+ user_query=query,
152
+ language=language,
153
+ deadline=deadline,
154
+ status=MandateStatus.ACTIVE,
155
+ )
156
+ doc = mandate.model_dump(by_alias=True, exclude={"id"})
157
+ result = await db["scout_mandates"].insert_one(doc)
158
+ inserted = await db["scout_mandates"].find_one({"_id": result.inserted_id})
159
+ return ScoutMandate(**inserted)
160
+
161
+
162
+ async def _pause_latest_mandate(user_id: str) -> bool:
163
+ db = await get_db()
164
+ doc = await db["scout_mandates"].find_one(
165
+ {"user_id": user_id, "status": MandateStatus.ACTIVE.value},
166
+ sort=[("created_at", -1)],
167
+ )
168
+ if not doc:
169
+ return False
170
+ await db["scout_mandates"].update_one(
171
+ {"_id": doc["_id"]},
172
+ {"$set": {"status": MandateStatus.PAUSED.value}},
173
+ )
174
+ return True
175
+
176
+
177
+ async def _resume_latest_mandate(user_id: str) -> bool:
178
+ db = await get_db()
179
+ doc = await db["scout_mandates"].find_one(
180
+ {"user_id": user_id, "status": MandateStatus.PAUSED.value},
181
+ sort=[("created_at", -1)],
182
+ )
183
+ if not doc:
184
+ return False
185
+ await db["scout_mandates"].update_one(
186
+ {"_id": doc["_id"]},
187
+ {"$set": {"status": MandateStatus.ACTIVE.value}},
188
+ )
189
+ return True
190
+
191
+
192
+ async def _stop_latest_mandate(user_id: str) -> bool:
193
+ db = await get_db()
194
+ doc = await db["scout_mandates"].find_one(
195
+ {"user_id": user_id, "status": {"$in": [MandateStatus.ACTIVE.value, MandateStatus.PAUSED.value]}},
196
+ sort=[("created_at", -1)],
197
+ )
198
+ if not doc:
199
+ return False
200
+ await db["scout_mandates"].update_one(
201
+ {"_id": doc["_id"]},
202
+ {"$set": {"status": MandateStatus.COMPLETED.value}},
203
+ )
204
+ return True
205
+
206
+
207
+ def _mandate_summary(mandates: list[ScoutMandate], lang: str) -> str:
208
+ if not mandates:
209
+ return _NO_MANDATES.get(lang, _NO_MANDATES["en"])
210
+ lines = []
211
+ for m in mandates:
212
+ status_emoji = "🟢" if m.status == MandateStatus.ACTIVE else "⏸"
213
+ deadline_str = f" (until {m.deadline.strftime('%b %d')})" if m.deadline else ""
214
+ lines.append(f"{status_emoji} {m.user_query}{deadline_str} — {m.delivery_count} result(s) sent")
215
+ header = "Your mandates:" if lang == "en" else "Tes mandats :"
216
+ return f"{header}\n" + "\n".join(lines)
217
+
218
+
219
+ # ── Main process entry point ──────────────────────────────────────────────────
220
+
221
+ async def process(state: AgentState) -> AgentState:
222
+ """Scout brain entry point — called by agent_hub when @Scout is detected."""
223
+ lang = _lang(state)
224
+ message = (state.last_user_message or "").strip()
225
+ user_id = state.user_id
226
+
227
+ try:
228
+ extracted = await _extract_mandate(message, state)
229
+ action = extracted.get("action", "create")
230
+ params = extracted.get("search_params", {})
231
+ query = extracted.get("user_query", message[:80])
232
+ deadline_days = extracted.get("deadline_days")
233
+
234
+ if action == "list" or action == "status":
235
+ mandates = await _get_active_mandates(user_id)
236
+ reply = _mandate_summary(mandates, lang)
237
+
238
+ elif action == "pause":
239
+ ok = await _pause_latest_mandate(user_id)
240
+ reply = _CONFIRM_PAUSE.get(lang, _CONFIRM_PAUSE["en"]) if ok else (
241
+ "No active mandate to pause." if lang == "en" else "Aucun mandat actif à mettre en pause."
242
+ )
243
+
244
+ elif action == "resume":
245
+ ok = await _resume_latest_mandate(user_id)
246
+ reply = (
247
+ ("Resumed! I'm back on it. 🔍" if lang == "en" else "Repris ! Je suis de retour. 🔍") if ok else
248
+ ("No paused mandate found." if lang == "en" else "Aucun mandat en pause trouvé.")
249
+ )
250
+
251
+ elif action == "stop":
252
+ ok = await _stop_latest_mandate(user_id)
253
+ reply = _CONFIRM_STOP.get(lang, _CONFIRM_STOP["en"]) if ok else (
254
+ "No active mandate to stop." if lang == "en" else "Aucun mandat actif à arrêter."
255
+ )
256
+
257
+ else:
258
+ # Default: create mandate
259
+ if not params.get("location") and not params.get("listing_type"):
260
+ # Unclear — ask for more details
261
+ reply = await generate_localized_response(
262
+ context=(
263
+ "The user wants Scout to find a property but hasn't specified enough details. "
264
+ "Ask them to clarify: what type of property, where, and their budget. "
265
+ "Be conversational, 1-2 sentences, Scout persona."
266
+ ),
267
+ language=lang,
268
+ tone="friendly",
269
+ max_length="short",
270
+ )
271
+ else:
272
+ mandate = await _create_mandate(user_id, params, query, lang, deadline_days)
273
+ reply = _pick(_CONFIRM_CREATE.get(lang, _CONFIRM_CREATE["en"]))
274
+ logger.info(
275
+ "Scout: mandate created",
276
+ mandate_id=mandate.id,
277
+ user_id=user_id,
278
+ query=query,
279
+ )
280
+
281
+ except Exception as e:
282
+ logger.error("Scout brain error", error=str(e), user_id=user_id)
283
+ reply = await generate_localized_response(
284
+ context="Tell the user Scout hit a small snag and to try again in a moment. Keep it light.",
285
+ language=lang,
286
+ tone="friendly",
287
+ max_length="short",
288
+ )
289
+
290
+ state.temp_data["response_text"] = reply
291
+ set_action(state, AgentAction.RESPOND)
292
+ return state
app/ai/services/search_responder.py CHANGED
@@ -5,6 +5,7 @@ Used by both the REST API and the AI Agent.
5
  """
6
 
7
  import logging
 
8
  from app.core.mimo_client import get_mimo_client
9
 
10
  logger = logging.getLogger(__name__)
@@ -57,6 +58,30 @@ YOUR TASK - Generate a NATURAL, CONVERSATIONAL response:
57
 
58
  Write ONLY the response message (no quotes, no extra formatting)."""
59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
 
61
  async def generate_natural_response(
62
  user_query: str,
@@ -156,13 +181,16 @@ async def generate_natural_response(
156
  )
157
 
158
  message = raw.strip()
159
-
160
  # Remove quotes if LLM added them
161
  if message.startswith('"') and message.endswith('"'):
162
  message = message[1:-1]
163
  if message.startswith("'") and message.endswith("'"):
164
  message = message[1:-1]
165
-
 
 
 
166
  return message
167
 
168
  except Exception as e:
 
5
  """
6
 
7
  import logging
8
+ import random
9
  from app.core.mimo_client import get_mimo_client
10
 
11
  logger = logging.getLogger(__name__)
 
58
 
59
  Write ONLY the response message (no quotes, no extra formatting)."""
60
 
61
+ # Scout ad pool — one is picked randomly and appended after every search result.
62
+ # Kept human-toned and varied so users don't feel they're seeing the same bot ad.
63
+ _SCOUT_ADS_EN = [
64
+ "you know I actually have a peer called Scout who can keep hunting this for you — just say '@Scout [what you want]' and she goes and does it.",
65
+ "by the way, if you want someone to track this automatically, Scout's got you — mention '@Scout [your search]' anytime.",
66
+ "not exactly what you wanted? Scout can search for it 24/7 while you sleep. Just say '@Scout find me [what you want]'.",
67
+ "want these results to come to you automatically? Try '@Scout [describe it]' and she'll ping you here whenever something fits.",
68
+ "Scout can keep an eye out for you — just drop '@Scout [what you're looking for]' and she handles the rest.",
69
+ ]
70
+ _SCOUT_ADS_FR = [
71
+ "tu sais, j'ai une collègue qui s'appelle Scout et qui peut continuer à chercher ça pour toi — dis juste '@Scout [ce que tu veux]' et elle s'en occupe.",
72
+ "au fait, si tu veux que quelqu'un traque ça automatiquement, Scout est là — mentionne '@Scout [ta recherche]' n'importe quand.",
73
+ "pas exactement ce que tu cherchais ? Scout peut rechercher 24h/24. Dis juste '@Scout trouve-moi [ce que tu veux]'.",
74
+ "tu veux recevoir des résultats automatiquement ? Essaie '@Scout [décris]' et elle te prévient ici dès qu'il y a une correspondance.",
75
+ "Scout peut surveiller ça pour toi — dis '@Scout [ce que tu cherches]' et elle s'occupe du reste.",
76
+ ]
77
+
78
+
79
+ def get_scout_ad(language: str = "en") -> str:
80
+ """Return a random, rotating Scout advertisement in the user's language."""
81
+ if language.startswith("fr"):
82
+ return random.choice(_SCOUT_ADS_FR)
83
+ return random.choice(_SCOUT_ADS_EN)
84
+
85
 
86
  async def generate_natural_response(
87
  user_query: str,
 
181
  )
182
 
183
  message = raw.strip()
184
+
185
  # Remove quotes if LLM added them
186
  if message.startswith('"') and message.endswith('"'):
187
  message = message[1:-1]
188
  if message.startswith("'") and message.endswith("'"):
189
  message = message[1:-1]
190
+
191
+ # Append rotating Scout ad on every search result
192
+ message = f"{message}\n\n{get_scout_ad(user_lang)}"
193
+
194
  return message
195
 
196
  except Exception as e:
app/jobs/scout_jobs.py ADDED
@@ -0,0 +1,287 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app/jobs/scout_jobs.py
2
+ """
3
+ Scout background job — runs every 4 hours.
4
+
5
+ For each ACTIVE ScoutMandate:
6
+ 1. Run the stored search_params against the listing DB.
7
+ 2. Filter out already-delivered listings (delivered_listing_ids).
8
+ 3. Score candidates — exact match first, then ±15% price / adjacent area.
9
+ 4. Deliver up to 3 new results per run via AIDA DM.
10
+ 5. Mark delivered listings so they are never repeated.
11
+ 6. Expire mandates past their deadline or with no activity for 30 days.
12
+ """
13
+
14
+ import logging
15
+ import random
16
+ from datetime import datetime, timedelta
17
+ from typing import Optional
18
+
19
+ from bson import ObjectId
20
+ from app.database import get_db
21
+ from app.models.scout_mandate import ScoutMandate, MandateStatus
22
+ from app.models.conversation import Conversation
23
+ from app.models.message import Message
24
+
25
+ logger = logging.getLogger(__name__)
26
+
27
+ AIDA_BOT_ID = "AIDA_BOT"
28
+ MAX_RESULTS_PER_RUN = 3
29
+ INACTIVITY_EXPIRY_DAYS = 30
30
+
31
+ # ── Pitch templates (Scout's personal property pitch) ─────────────────────────
32
+
33
+ _PITCHES_EN = [
34
+ "Hey, I found something that looks like a match for you 🏠",
35
+ "Scout here — this one just popped up and it fits your mandate.",
36
+ "Found a property that matches what you asked for. Take a look 👀",
37
+ "This just came up — I think you'd like it.",
38
+ "I've been watching and this one fits what you described. Worth a look!",
39
+ ]
40
+
41
+ _PITCHES_FR = [
42
+ "Hey, j'ai trouvé quelque chose qui correspond à ton mandat 🏠",
43
+ "Scout ici — celui-là vient d'apparaître et il correspond à ta recherche.",
44
+ "J'ai trouvé un bien qui correspond à ce que tu cherchais. Regarde 👀",
45
+ "Ça vient de sortir — je crois que ça t'intéresserait.",
46
+ "Je surveillais et celui-là correspond à ce que tu m'as décrit. Ça vaut le coup !",
47
+ ]
48
+
49
+
50
+ def _pick_pitch(language: str) -> str:
51
+ if language.startswith("fr"):
52
+ return random.choice(_PITCHES_FR)
53
+ return random.choice(_PITCHES_EN)
54
+
55
+
56
+ # ── Search helper ─────────────────────────────────────────────────────────────
57
+
58
+ async def _search_listings(params: dict, exclude_ids: list[str], limit: int = 10) -> list[dict]:
59
+ """Basic MongoDB search against the listings collection."""
60
+ db = await get_db()
61
+ query: dict = {"status": "active"}
62
+
63
+ location = params.get("location", "").strip()
64
+ if location:
65
+ query["$or"] = [
66
+ {"location": {"$regex": location, "$options": "i"}},
67
+ {"city": {"$regex": location, "$options": "i"}},
68
+ {"neighborhood": {"$regex": location, "$options": "i"}},
69
+ {"address": {"$regex": location, "$options": "i"}},
70
+ ]
71
+
72
+ listing_type = params.get("listing_type", "").strip()
73
+ if listing_type:
74
+ query["listing_type"] = listing_type
75
+
76
+ max_price = params.get("max_price")
77
+ min_price = params.get("min_price")
78
+ if max_price or min_price:
79
+ price_q: dict = {}
80
+ if min_price:
81
+ price_q["$gte"] = min_price * 0.85 # allow 15% below min
82
+ if max_price:
83
+ price_q["$lte"] = max_price * 1.15 # allow 15% above max
84
+ if price_q:
85
+ query["price"] = price_q
86
+
87
+ bedrooms = params.get("bedrooms")
88
+ if bedrooms is not None:
89
+ query["bedrooms"] = {"$in": [bedrooms - 1, bedrooms, bedrooms + 1]}
90
+
91
+ # Exclude already-delivered listings
92
+ if exclude_ids:
93
+ try:
94
+ oid_excludes = [ObjectId(lid) for lid in exclude_ids if ObjectId.is_valid(lid)]
95
+ if oid_excludes:
96
+ query["_id"] = {"$nin": oid_excludes}
97
+ except Exception:
98
+ pass
99
+
100
+ cursor = db["listings"].find(query).sort("createdAt", -1).limit(limit)
101
+ return await cursor.to_list(length=limit)
102
+
103
+
104
+ # ── DM delivery ───────────────────────────────────────────────────────────────
105
+
106
+ async def _get_or_create_aida_conversation(user_id: str) -> str:
107
+ db = await get_db()
108
+ participants = sorted([AIDA_BOT_ID, user_id])
109
+ participants_key = "::".join(participants)
110
+
111
+ existing = await db.conversations.find_one({"participants_key": participants_key})
112
+ if existing:
113
+ return str(existing["_id"])
114
+
115
+ conv_doc = Conversation.create_document(
116
+ listing_id="system",
117
+ participants=participants,
118
+ listing_title="AIDA Assistant",
119
+ listing_image=None,
120
+ )
121
+ try:
122
+ result = await db.conversations.insert_one(conv_doc)
123
+ return str(result.inserted_id)
124
+ except Exception as e:
125
+ if "duplicate key" in str(e).lower() or "E11000" in str(e):
126
+ existing = await db.conversations.find_one({"participants_key": participants_key})
127
+ if existing:
128
+ return str(existing["_id"])
129
+ raise
130
+
131
+
132
+ async def _deliver_listing(mandate: ScoutMandate, listing: dict, conv_id: str) -> None:
133
+ """Send a Scout DM with the listing as a property card attachment."""
134
+ db = await get_db()
135
+ now = datetime.utcnow()
136
+
137
+ listing_id = str(listing.get("_id", ""))
138
+ pitch = _pick_pitch(mandate.language)
139
+
140
+ property_card = {
141
+ "listing_id": listing_id,
142
+ "title": listing.get("title", ""),
143
+ "price": listing.get("price"),
144
+ "currency": listing.get("currency", "XOF"),
145
+ "bedrooms": listing.get("bedrooms"),
146
+ "location": listing.get("location") or listing.get("city", ""),
147
+ "image_url": (listing.get("images") or [None])[0],
148
+ "listing_type": listing.get("listing_type", ""),
149
+ }
150
+
151
+ message_doc = Message.create_document(
152
+ conversation_id=conv_id,
153
+ sender_id=AIDA_BOT_ID,
154
+ sender_name="Scout",
155
+ sender_avatar=None,
156
+ message_type="scout_result",
157
+ content=pitch,
158
+ property_card=property_card,
159
+ )
160
+ # Attach metadata so Flutter's bubble recognises this as a Scout result
161
+ message_doc["metadata"] = {
162
+ "scout_result": True,
163
+ "listing": {
164
+ "_id": listing_id,
165
+ "title": listing.get("title", ""),
166
+ "price": listing.get("price"),
167
+ "location": listing.get("location") or listing.get("city", ""),
168
+ "images": listing.get("images", [])[:1],
169
+ "listing_type": listing.get("listing_type", ""),
170
+ "bedrooms": listing.get("bedrooms"),
171
+ },
172
+ }
173
+ await db.messages.insert_one(message_doc)
174
+
175
+ # Update conversation last_message_at
176
+ await db.conversations.update_one(
177
+ {"_id": ObjectId(conv_id)},
178
+ {"$set": {"last_message_at": now, "last_message": pitch}},
179
+ )
180
+
181
+ # Mark in delivered_listing_ids
182
+ await db["scout_mandates"].update_one(
183
+ {"_id": ObjectId(mandate.id)},
184
+ {
185
+ "$addToSet": {"delivered_listing_ids": listing_id},
186
+ "$inc": {"delivery_count": 1},
187
+ "$set": {"last_run_at": now},
188
+ },
189
+ )
190
+
191
+ logger.info(
192
+ "Scout: delivered listing",
193
+ mandate_id=mandate.id,
194
+ listing_id=listing_id,
195
+ user_id=mandate.user_id,
196
+ )
197
+
198
+ # Push notification — wakes up the user even when the app is closed
199
+ try:
200
+ from app.services.push_service import PushService
201
+ listing_title = listing.get("title", "a new property")
202
+ await PushService.send_to_user(
203
+ user_id=mandate.user_id,
204
+ title="Scout found a match! 🏠",
205
+ body=f"{listing_title} — {listing.get('location') or ''}",
206
+ category="property_alerts",
207
+ data={
208
+ "type": "scout_result",
209
+ "listing_id": listing_id,
210
+ "mandate_id": mandate.id or "",
211
+ },
212
+ )
213
+ except Exception as _push_err:
214
+ logger.warning(f"Scout push notification failed (non-fatal): {_push_err}")
215
+
216
+
217
+ # ── Expiry helpers ────────────────────────────────────────────────────────────
218
+
219
+ async def _expire_stale_mandates() -> int:
220
+ db = await get_db()
221
+ cutoff = datetime.utcnow() - timedelta(days=INACTIVITY_EXPIRY_DAYS)
222
+ # Expire mandates that haven't run in 30 days OR whose deadline has passed
223
+ result = await db["scout_mandates"].update_many(
224
+ {
225
+ "status": {"$in": [MandateStatus.ACTIVE.value, MandateStatus.PAUSED.value]},
226
+ "$or": [
227
+ {"last_run_at": {"$lt": cutoff}, "last_run_at": {"$ne": None}},
228
+ {"created_at": {"$lt": cutoff}, "last_run_at": None},
229
+ {"deadline": {"$lt": datetime.utcnow(), "$ne": None}},
230
+ ],
231
+ },
232
+ {"$set": {"status": MandateStatus.EXPIRED.value}},
233
+ )
234
+ return result.modified_count
235
+
236
+
237
+ # ── Main job ──────────────────────────────────────────────────────────────────
238
+
239
+ async def dispatch_scout_job():
240
+ """
241
+ Main Scout background job. Called every 4 hours by APScheduler.
242
+ """
243
+ logger.info("[Scout] Starting background hunt job…")
244
+ db = await get_db()
245
+ now = datetime.utcnow()
246
+
247
+ # 1. Expire stale mandates first
248
+ expired = await _expire_stale_mandates()
249
+ if expired:
250
+ logger.info(f"[Scout] Expired {expired} stale mandate(s)")
251
+
252
+ # 2. Load all ACTIVE mandates
253
+ cursor = db["scout_mandates"].find({"status": MandateStatus.ACTIVE.value})
254
+ mandates = [ScoutMandate(**doc) async for doc in cursor]
255
+ logger.info(f"[Scout] Processing {len(mandates)} active mandate(s)")
256
+
257
+ for mandate in mandates:
258
+ try:
259
+ listings = await _search_listings(
260
+ mandate.search_params,
261
+ exclude_ids=mandate.delivered_listing_ids,
262
+ limit=MAX_RESULTS_PER_RUN + 5, # fetch a few extra to allow scoring
263
+ )
264
+
265
+ if not listings:
266
+ # Update last_run_at even when empty so inactivity clock resets
267
+ await db["scout_mandates"].update_one(
268
+ {"_id": ObjectId(mandate.id)},
269
+ {"$set": {"last_run_at": now}},
270
+ )
271
+ logger.info(f"[Scout] No new results for mandate {mandate.id}")
272
+ continue
273
+
274
+ conv_id = await _get_or_create_aida_conversation(mandate.user_id)
275
+ delivered = 0
276
+ for listing in listings:
277
+ if delivered >= MAX_RESULTS_PER_RUN:
278
+ break
279
+ await _deliver_listing(mandate, listing, conv_id)
280
+ delivered += 1
281
+
282
+ logger.info(f"[Scout] Delivered {delivered} result(s) for mandate {mandate.id}")
283
+
284
+ except Exception as e:
285
+ logger.error(f"[Scout] Error processing mandate {mandate.id}: {e}")
286
+
287
+ logger.info("[Scout] Hunt job complete")
app/models/scout_mandate.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from datetime import datetime
2
+ from typing import Optional, List
3
+ from enum import Enum
4
+ from pydantic import BaseModel, Field, field_validator
5
+ from bson import ObjectId
6
+
7
+
8
+ class MandateStatus(str, Enum):
9
+ ACTIVE = "active"
10
+ PAUSED = "paused"
11
+ COMPLETED = "completed"
12
+ EXPIRED = "expired"
13
+
14
+
15
+ class ScoutMandate(BaseModel):
16
+ id: Optional[str] = Field(alias="_id", default=None)
17
+ user_id: str
18
+ search_params: dict
19
+ user_query: str
20
+ language: str = "en"
21
+ deadline: Optional[datetime] = None
22
+ status: MandateStatus = MandateStatus.ACTIVE
23
+ created_at: datetime = Field(default_factory=datetime.utcnow)
24
+ last_run_at: Optional[datetime] = None
25
+ delivered_listing_ids: List[str] = Field(default_factory=list)
26
+ delivery_count: int = 0
27
+
28
+ @field_validator("id", mode="before")
29
+ @classmethod
30
+ def convert_objectid(cls, v):
31
+ if isinstance(v, ObjectId):
32
+ return str(v)
33
+ return v
34
+
35
+ class Config:
36
+ populate_by_name = True
37
+ arbitrary_types_allowed = True
38
+ json_encoders = {ObjectId: str}
main.py CHANGED
@@ -96,6 +96,7 @@ from app.models.viewing import ensure_viewing_indexes
96
  from app.models.calendar import ensure_calendar_indexes
97
  from app.jobs.payout_jobs import process_escrow_payouts
98
  from app.jobs.agent_jobs import dispatch_agent_deadlock_check
 
99
  from app.jobs.stay_notification_jobs import (
100
  dispatch_booking_status_transitions,
101
  dispatch_checkin_reminders,
@@ -237,6 +238,7 @@ async def lifespan(app: FastAPI):
237
  scheduler.add_job(_lock(dispatch_checkout_review_requests, 30), "interval", minutes=30)
238
  scheduler.add_job(_lock(dispatch_viewing_reminders, 30), "interval", minutes=30)
239
  scheduler.add_job(_lock(dispatch_post_viewing_followups, 30), "interval", minutes=30)
 
240
 
241
  # Register proactive alert scheduler
242
  try:
@@ -299,7 +301,7 @@ _openapi_tags = [
299
  {"name": "Wishlist", "description": "Save and manage favourite listings"},
300
  {"name": "Reviews", "description": "Property and user reviews"},
301
  {"name": "Notifications", "description": "Push notification preferences and inbox"},
302
- {"name": "Alerts", "description": "Saved search alerts"},
303
  {"name": "Stripe Connect", "description": "Landlord payout accounts"},
304
  {"name": "Admin", "description": "Admin-only dashboard endpoints"},
305
  {"name": "Claim", "description": "Shadow-user account claim flow"},
 
96
  from app.models.calendar import ensure_calendar_indexes
97
  from app.jobs.payout_jobs import process_escrow_payouts
98
  from app.jobs.agent_jobs import dispatch_agent_deadlock_check
99
+ from app.jobs.scout_jobs import dispatch_scout_job
100
  from app.jobs.stay_notification_jobs import (
101
  dispatch_booking_status_transitions,
102
  dispatch_checkin_reminders,
 
238
  scheduler.add_job(_lock(dispatch_checkout_review_requests, 30), "interval", minutes=30)
239
  scheduler.add_job(_lock(dispatch_viewing_reminders, 30), "interval", minutes=30)
240
  scheduler.add_job(_lock(dispatch_post_viewing_followups, 30), "interval", minutes=30)
241
+ scheduler.add_job(_lock(dispatch_scout_job, 240), "interval", minutes=240)
242
 
243
  # Register proactive alert scheduler
244
  try:
 
301
  {"name": "Wishlist", "description": "Save and manage favourite listings"},
302
  {"name": "Reviews", "description": "Property and user reviews"},
303
  {"name": "Notifications", "description": "Push notification preferences and inbox"},
304
+ {"name": "Scout", "description": "Scout proactive property-hunting mandates"},
305
  {"name": "Stripe Connect", "description": "Landlord payout accounts"},
306
  {"name": "Admin", "description": "Admin-only dashboard endpoints"},
307
  {"name": "Claim", "description": "Shadow-user account claim flow"},