handoff fixes
This commit is contained in:
parent
aabb63f947
commit
3f4bcc26d9
4 changed files with 137 additions and 28 deletions
|
|
@ -1,8 +1,14 @@
|
|||
import re
|
||||
|
||||
HANDOFF_LABELS = ["Important", "Finance", "Clients", "Projects", "Personal", "Support"]
|
||||
ALL_MAIL_LABEL = "All mail"
|
||||
HANDOFF_LABELS = [ALL_MAIL_LABEL, "Important", "Finance", "Clients", "Projects", "Personal", "Support"]
|
||||
ALL_TYPES_ID = "all"
|
||||
|
||||
EMAIL_TYPE_META = {
|
||||
"all": {
|
||||
"title": "All types",
|
||||
"patterns": "",
|
||||
},
|
||||
"utility": {
|
||||
"title": "Utility",
|
||||
"patterns": r"account update|alert|notice|security|verification|password|service information|noreply|no-reply",
|
||||
|
|
@ -97,11 +103,28 @@ def evaluate_handoff(
|
|||
inferred_labels = infer_labels(email)
|
||||
inferred_types = infer_email_types(email)
|
||||
|
||||
label_ok = not labels or any(label in inferred_labels for label in labels)
|
||||
matched_labels = [label for label in labels if label in inferred_labels] if labels else inferred_labels[:1]
|
||||
all_mail = ALL_MAIL_LABEL in labels
|
||||
all_types = ALL_TYPES_ID in email_types
|
||||
selected_labels = [label for label in labels if label != ALL_MAIL_LABEL]
|
||||
selected_types = [item for item in email_types if item != ALL_TYPES_ID]
|
||||
|
||||
type_ok = not email_types or any(item in inferred_types for item in email_types)
|
||||
matched_types = [item for item in email_types if item in inferred_types] if email_types else inferred_types[:1]
|
||||
label_ok = all_mail or not selected_labels or any(
|
||||
label in inferred_labels for label in selected_labels
|
||||
)
|
||||
matched_labels = (
|
||||
[ALL_MAIL_LABEL]
|
||||
if all_mail
|
||||
else [label for label in selected_labels if label in inferred_labels]
|
||||
)
|
||||
|
||||
type_ok = all_types or not selected_types or any(
|
||||
item in inferred_types for item in selected_types
|
||||
)
|
||||
matched_types = (
|
||||
[ALL_TYPES_ID]
|
||||
if all_types
|
||||
else [item for item in selected_types if item in inferred_types]
|
||||
)
|
||||
|
||||
instruction_ok, instruction_reason = instruction_matches(instructions, email)
|
||||
would_handoff = label_ok and type_ok and instruction_ok
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
from app.services.handoff_evaluator import evaluate_handoff
|
||||
from app.services.handoff_evaluator import ALL_MAIL_LABEL, ALL_TYPES_ID, evaluate_handoff
|
||||
|
||||
|
||||
def test_handoff_requires_all_selected_filters():
|
||||
|
|
@ -31,3 +31,35 @@ def test_handoff_does_not_match_unrelated_instruction():
|
|||
instructions="Forward when a refund requires approval.",
|
||||
)
|
||||
assert result["would_handoff"] is False
|
||||
|
||||
|
||||
def test_handoff_all_mail_matches_generic_inbound():
|
||||
email = {
|
||||
"subject": "test handoff",
|
||||
"snippet": "did you receive this?",
|
||||
"body_text": "did you receive this?",
|
||||
}
|
||||
result = evaluate_handoff(
|
||||
email=email,
|
||||
labels=[ALL_MAIL_LABEL],
|
||||
email_types=[ALL_TYPES_ID],
|
||||
instructions="",
|
||||
)
|
||||
assert result["would_handoff"] is True
|
||||
assert result["label_matched"] is True
|
||||
assert result["type_matched"] is True
|
||||
|
||||
|
||||
def test_handoff_selected_categories_do_not_match_generic_email():
|
||||
email = {
|
||||
"subject": "hi",
|
||||
"snippet": "check the inbox please",
|
||||
"body_text": "check the inbox please",
|
||||
}
|
||||
result = evaluate_handoff(
|
||||
email=email,
|
||||
labels=["Important", "Finance", "Clients"],
|
||||
email_types=["transactional"],
|
||||
instructions="",
|
||||
)
|
||||
assert result["would_handoff"] is False
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue