Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 66 additions & 7 deletions apps/discord_bot/src/five08/discord_bot/cogs/crm.py
Original file line number Diff line number Diff line change
Expand Up @@ -1345,6 +1345,10 @@ async def confirm_create(
create_payload = self.crm_cog._build_resume_create_contact_payload(
file_content=self.file_content
)
self.crm_cog._populate_name_fields(
create_payload,
source_name=str(create_payload.get("name", "")).strip(),
)
target_contact = self.crm_cog.espo_api.request(
"POST", "Contact", create_payload
)
Expand Down Expand Up @@ -1375,6 +1379,8 @@ async def confirm_create(
)
except Exception as exc:
status_code = getattr(self.crm_cog.espo_api, "status_code", None)
error_detail = str(exc).strip() or "Unknown error"
status_note = f" (status {status_code})" if status_code else ""
logger.exception(
"Failed to create contact from resume filename=%s target_scope=%s inferred_meta=%s status_code=%s payload=%s",
self.filename,
Expand All @@ -1399,7 +1405,7 @@ async def confirm_create(
metadata=audit_metadata,
)
await interaction.followup.send(
"⚠️ Could not create a contact from this resume. "
f"⚠️ Could not create a contact from this resume: `{error_detail}`{status_note}. "
"Please provide `search_term` or `link_user`.",
ephemeral=True,
Comment on lines 1407 to 1410
Copy link

Copilot AI Mar 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The user-facing failure message embeds error_detail = str(exc) verbatim. For EspoAPIError raised by the shared client, this may include boilerplate (e.g., "Wrong request, status code is …") and can duplicate the separately-appended status note, making the message noisy. Consider special-casing EspoAPIError to extract/display just the server reason (and escape/truncate any backticks/newlines) while keeping the full exception details only in logs/audit metadata.

Copilot uses AI. Check for mistakes.
)
Expand Down Expand Up @@ -1532,6 +1538,11 @@ def __init__(self, bot: commands.Bot) -> None:
discord_logs_webhook_wait=settings.discord_logs_webhook_wait,
)

@staticmethod
def _configured_linkedin_field() -> str:
"""Return the configured field for LinkedIn profile values."""
return str(getattr(settings, "crm_linkedin_field", "cLinkedInUrl"))
Copy link

Copilot AI Mar 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_configured_linkedin_field() reads settings.crm_linkedin_field, but the bot Settings model doesn’t declare this field (so Pydantic Settings won’t load it from env/config and it will always fall back). Also, if the attribute were ever present but None, str(None) would yield the literal string "None". Consider adding an explicit crm_linkedin_field: str = "cLinkedInUrl" to the Settings schema (and validating/stripping it here, falling back when empty).

Suggested change
return str(getattr(settings, "crm_linkedin_field", "cLinkedInUrl"))
default_field = "cLinkedInUrl"
raw_value = getattr(settings, "crm_linkedin_field", None)
if not isinstance(raw_value, str):
return default_field
value = raw_value.strip()
return value or default_field

Copilot uses AI. Check for mistakes.

def _audit_command(
self,
*,
Expand Down Expand Up @@ -3280,6 +3291,26 @@ def _to_values(raw_values: Any) -> list[str]:

return "\nParsed resume identifiers: " + "; ".join(summary_parts)

def _build_resume_parsed_identity_summary(self, file_content: bytes) -> str:
"""Build a short display summary of parsed contact identity fields."""
hints = self._extract_resume_contact_hints(file_content)
parsed_name = str(hints.get("name") or "").strip()
if not parsed_name:
parsed_name = self._extract_resume_name_fallback(file_content)

emails = hints.get("emails", [])
if not isinstance(emails, list):
emails = []
primary_email = "No email parsed"
if emails:
raw_email = str(emails[0]).strip()
if raw_email:
primary_email = raw_email

return (
f"\nParsed contact details: name=`{parsed_name}`, email=`{primary_email}`"
)
Comment on lines +3309 to +3312
Copy link

Copilot AI Mar 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The parsed name/email are interpolated directly into inline-code backticks. If the extracted name/email contains a backtick, it can break formatting and potentially hide/alter surrounding text. Consider sanitizing (e.g., replace/backslash-escape backticks and truncate to a safe length) before embedding user/LLM-derived values into Discord messages.

Copilot uses AI. Check for mistakes.

def _extract_resume_name_hint(self, file_content: bytes) -> str:
"""Best-effort contact name extraction from resume text."""
hints = self._extract_resume_contact_hints(file_content)
Expand All @@ -3288,6 +3319,18 @@ def _extract_resume_name_hint(self, file_content: bytes) -> str:
return extracted_name
return self._extract_resume_name_fallback(file_content)

def _populate_name_fields(
self, payload: dict[str, str], *, source_name: str
) -> None:
"""Populate firstName and lastName fields for CRM contact creation payloads."""
first_name, last_name = self.resume_extractor.split_name(
full_name=source_name,
first_name_hint=str(payload.get("firstName", "")).strip() or None,
last_name_hint=str(payload.get("lastName", "")).strip() or None,
)
Comment on lines +3326 to +3330
Copy link

Copilot AI Mar 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_populate_name_fields() calls resume_extractor.split_name(), which will invoke an additional LLM call whenever the OpenAI client is configured. In the resume-upload path you already may have done an LLM extraction, so this can introduce an extra API request (latency/cost/failure surface) just to split the name. Consider using heuristic-only splitting here, or plumbing through extracted first_name/last_name hints from the resume profile to avoid triggering another model call.

Copilot uses AI. Check for mistakes.
payload["firstName"] = first_name
payload["lastName"] = last_name

def _build_resume_create_contact_payload(
self, file_content: bytes
) -> dict[str, str]:
Expand All @@ -3312,6 +3355,7 @@ def _build_resume_create_contact_payload(
"type": "Prospect",
"name": contact_name,
}
self._populate_name_fields(payload, source_name=contact_name)
if emails:
primary_email = emails[0]
if primary_email.endswith("@508.dev"):
Expand All @@ -3321,7 +3365,7 @@ def _build_resume_create_contact_payload(
if github_usernames:
payload["cGitHubUsername"] = github_usernames[0]
if linkedin_urls:
payload["cLinkedInUrl"] = linkedin_urls[0]
payload[self._configured_linkedin_field()] = linkedin_urls[0]
phone = hints.get("phone")
if isinstance(phone, str) and phone.strip():
payload["phoneNumber"] = phone.strip()
Expand Down Expand Up @@ -3370,17 +3414,30 @@ def _build_contact_payload_for_link_user(
parsed_name = str(payload.get("name", "")).strip()
if not parsed_name or parsed_name == "Resume Candidate":
payload["name"] = self._fallback_contact_name_for_discord_user(user)
self._populate_name_fields(
payload, source_name=str(payload.get("name", "")).strip()
)
payload.update(self._discord_link_fields(user))
return payload

async def _search_contacts_by_field(
self, *, field: str, value: str, max_size: int = 10
) -> list[dict[str, Any]]:
"""Search contacts using an exact field equals match."""
select_fields = [
"id",
"name",
"emailAddress",
"c508Email",
"cDiscordUsername",
"cGitHubUsername",
]
if field not in select_fields:
select_fields.append(field)
search_params = {
"where": [{"type": "equals", "attribute": field, "value": value}],
"maxSize": max_size,
"select": "id,name,emailAddress,c508Email,cDiscordUsername,cGitHubUsername,cLinkedInUrl",
"select": ",".join(select_fields),
}

response = self.espo_api.request("GET", "Contact", search_params)
Expand Down Expand Up @@ -3450,7 +3507,7 @@ async def _infer_contact_from_resume(
for linkedin_url in linkedin_urls:
attempts.append({"method": "linkedin", "value": linkedin_url})
contacts = await self._search_contacts_by_field(
field="cLinkedInUrl", value=linkedin_url
field=self._configured_linkedin_field(), value=linkedin_url
)
if len(contacts) == 1:
return contacts[0], {
Expand Down Expand Up @@ -4948,7 +5005,7 @@ async def update_contact(
if linkedin is not None:
clean_linkedin = linkedin.strip()
if clean_linkedin:
update_data["cLinkedInUrl"] = clean_linkedin
update_data[self._configured_linkedin_field()] = clean_linkedin
requested_updates.append("linkedin")

if rate_range is not None:
Expand Down Expand Up @@ -5028,9 +5085,10 @@ async def update_contact(
inline=True,
)
if "linkedin" in requested_updates:
linkedin_field = self._configured_linkedin_field()
embed.add_field(
name="🔗 LinkedIn",
value=update_data["cLinkedInUrl"],
value=update_data[linkedin_field],
inline=True,
)
if "skills" in requested_updates:
Expand Down Expand Up @@ -5605,7 +5663,8 @@ async def upload_resume(
await interaction.followup.send(
"⚠️ Could not find a unique contact from this resume. "
"Would you like to create a new contact from the parsed details?"
+ inferred_attempts_text,
+ inferred_attempts_text
+ self._build_resume_parsed_identity_summary(file_content),
view=view,
ephemeral=True,
)
Expand Down
Loading
Loading