feat: add user alias mapping for jealousy detection context
Adds user_aliases config section mapping Discord IDs to known nicknames. Aliases are anonymized and injected into LLM analysis context so it can recognize when someone name-drops another member (even absent ones). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -253,6 +253,38 @@ class SentimentCog(commands.Cog):
|
||||
"""Replace display name keys with anonymous keys in user notes map."""
|
||||
return {anon_map.get(name, name): notes for name, notes in user_notes_map.items()}
|
||||
|
||||
@staticmethod
|
||||
def _build_alias_context(
|
||||
messages: list[discord.Message],
|
||||
anon_map: dict[str, str],
|
||||
alias_config: dict,
|
||||
) -> str:
|
||||
"""Build anonymized alias context string for the LLM.
|
||||
|
||||
Maps user IDs from messages to their known nicknames using the
|
||||
config, then replaces display names with anonymous keys.
|
||||
"""
|
||||
if not alias_config:
|
||||
return ""
|
||||
lines = []
|
||||
seen_ids: set[int] = set()
|
||||
for msg in messages:
|
||||
uid = msg.author.id
|
||||
if uid in seen_ids:
|
||||
continue
|
||||
seen_ids.add(uid)
|
||||
aliases = alias_config.get(uid)
|
||||
if aliases:
|
||||
anon_key = anon_map.get(msg.author.display_name, msg.author.display_name)
|
||||
lines.append(f" {anon_key} is also known as: {', '.join(aliases)}")
|
||||
# Also include aliases for members NOT in the conversation (so the LLM
|
||||
# can recognize name-drops of absent members)
|
||||
for uid, aliases in alias_config.items():
|
||||
uid = int(uid) if isinstance(uid, str) else uid
|
||||
if uid not in seen_ids:
|
||||
lines.append(f" (not in chat) also known as: {', '.join(aliases)}")
|
||||
return "\n".join(lines) if lines else ""
|
||||
|
||||
@staticmethod
|
||||
def _deanonymize_findings(result: dict, anon_map: dict[str, str]) -> None:
|
||||
"""Replace anonymous keys back to display names in LLM findings (in-place)."""
|
||||
@@ -466,6 +498,9 @@ class SentimentCog(commands.Cog):
|
||||
anon_conversation = self._anonymize_conversation(conversation, anon_map)
|
||||
anon_notes = self._anonymize_notes(user_notes_map, anon_map) if user_notes_map else user_notes_map
|
||||
|
||||
alias_config = config.get("user_aliases", {})
|
||||
alias_context = self._build_alias_context(all_messages, anon_map, alias_config)
|
||||
|
||||
channel_context = build_channel_context(ref_message, game_channels)
|
||||
|
||||
logger.info(
|
||||
@@ -480,6 +515,7 @@ class SentimentCog(commands.Cog):
|
||||
channel_context=channel_context,
|
||||
user_notes_map=anon_notes,
|
||||
new_message_start=new_message_start,
|
||||
user_aliases=alias_context,
|
||||
)
|
||||
|
||||
if result is None:
|
||||
@@ -499,6 +535,7 @@ class SentimentCog(commands.Cog):
|
||||
channel_context=channel_context,
|
||||
user_notes_map=anon_notes,
|
||||
new_message_start=new_message_start,
|
||||
user_aliases=alias_context,
|
||||
)
|
||||
if heavy_result is not None:
|
||||
logger.info(
|
||||
@@ -638,6 +675,9 @@ class SentimentCog(commands.Cog):
|
||||
anon_conversation = self._anonymize_conversation(conversation, anon_map)
|
||||
anon_notes = self._anonymize_notes(user_notes_map, anon_map) if user_notes_map else user_notes_map
|
||||
|
||||
alias_config = config.get("user_aliases", {})
|
||||
alias_context = self._build_alias_context(raw_messages, anon_map, alias_config)
|
||||
|
||||
channel_context = build_channel_context(raw_messages[0], game_channels)
|
||||
mention_context = (
|
||||
f"A user flagged this conversation and said: \"{mention_text}\"\n"
|
||||
@@ -650,6 +690,7 @@ class SentimentCog(commands.Cog):
|
||||
mention_context=mention_context,
|
||||
channel_context=channel_context,
|
||||
user_notes_map=anon_notes,
|
||||
user_aliases=alias_context,
|
||||
)
|
||||
|
||||
if result is None:
|
||||
|
||||
Reference in New Issue
Block a user