Spaces:
Sleeping
Sleeping
feat: wire ai_clean.clean_delivery into the generate path (ARCHITECTURE §13.3) — the body's delivery sentence is now งวด-aware; deterministic fallback preserved
Browse files- letter_builder.py +8 -1
- tests/test_letter_builder.py +26 -0
letter_builder.py
CHANGED
|
@@ -50,6 +50,12 @@ def build_letter(*, selected_form, doc_number, po_number, doc_type, signer,
|
|
| 50 |
`now` exists only so tests can pin the filename timestamp."""
|
| 51 |
template_path = template_path_for(selected_form)
|
| 52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
# AI-generate the 2-paragraph body in the exact format (deterministic
|
| 54 |
# fallback keeps it on-format if the AI Builder flow fails)
|
| 55 |
ld = lookup_data or {}
|
|
@@ -60,8 +66,9 @@ def build_letter(*, selected_form, doc_number, po_number, doc_type, signer,
|
|
| 60 |
"unit": ld.get("prCreatorUnitName"),
|
| 61 |
"start_date": ai_clean.thai_date(start_date),
|
| 62 |
"days": ld.get("daySLA"),
|
| 63 |
-
"delivery_clause": delivery_raw, # TOR-driven "ภายใน X วัน นับถัดจาก…"
|
| 64 |
})
|
|
|
|
| 65 |
doc_type_final = doc_type if selected_form['uses_doc_type'] else ""
|
| 66 |
doc_type_final = suffix_doc_type(doc_type_final, selected_nguad)
|
| 67 |
|
|
|
|
| 50 |
`now` exists only so tests can pin the filename timestamp."""
|
| 51 |
template_path = template_path_for(selected_form)
|
| 52 |
|
| 53 |
+
# Clean the raw TOR delivery clause first (งวด-aware): the chosen งวด's
|
| 54 |
+
# เงื่อนไข reaches the letter body instead of whatever งวด happens to come
|
| 55 |
+
# first in the TOR. Never raises — deterministic fallback on any AI failure.
|
| 56 |
+
delivery_clean, used_ai_delivery = ai_clean.clean_delivery(
|
| 57 |
+
delivery_raw, (selected_nguad or {}).get("label"))
|
| 58 |
+
|
| 59 |
# AI-generate the 2-paragraph body in the exact format (deterministic
|
| 60 |
# fallback keeps it on-format if the AI Builder flow fails)
|
| 61 |
ld = lookup_data or {}
|
|
|
|
| 66 |
"unit": ld.get("prCreatorUnitName"),
|
| 67 |
"start_date": ai_clean.thai_date(start_date),
|
| 68 |
"days": ld.get("daySLA"),
|
| 69 |
+
"delivery_clause": delivery_clean or delivery_raw, # TOR-driven "ภายใน X วัน นับถัดจาก…"
|
| 70 |
})
|
| 71 |
+
used_ai = used_ai or used_ai_delivery
|
| 72 |
doc_type_final = doc_type if selected_form['uses_doc_type'] else ""
|
| 73 |
doc_type_final = suffix_doc_type(doc_type_final, selected_nguad)
|
| 74 |
|
tests/test_letter_builder.py
CHANGED
|
@@ -63,3 +63,29 @@ def test_build_letter_form1():
|
|
| 63 |
assert "บริษัท ทดสอบ จำกัด" in all_text # vendor code stripped
|
| 64 |
assert "(งวดที่ 2)" in all_text # งวด suffix landed in the doc type
|
| 65 |
assert "20 กรกฎาคม 2569" in all_text # start date in Thai
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
assert "บริษัท ทดสอบ จำกัด" in all_text # vendor code stripped
|
| 64 |
assert "(งวดที่ 2)" in all_text # งวด suffix landed in the doc type
|
| 65 |
assert "20 กรกฎาคม 2569" in all_text # start date in Thai
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
def test_build_letter_uses_selected_nguad_deadline():
|
| 69 |
+
"""clean_delivery is wired into the generate path (ARCHITECTURE §13.3):
|
| 70 |
+
with a multi-งวด TOR, the body's deadline comes from the CHOSEN งวด,
|
| 71 |
+
not from whichever งวด appears first in the clause."""
|
| 72 |
+
multi_nguad_tor = ("การส่งมอบงาน\n"
|
| 73 |
+
"งวดที่ 1 ส่งแผนงานภายใน 15 วัน นับถัดจากวันที่ได้รับหนังสือแจ้ง\n"
|
| 74 |
+
"งวดที่ 2 ส่งรายงานฉบับสมบูรณ์ภายใน 120 วัน นับถัดจากวันที่ได้รับหนังสือแจ้ง")
|
| 75 |
+
docx_bytes, _fn, _log, _used_ai = letter_builder.build_letter(
|
| 76 |
+
selected_form=FORM_TYPES[0],
|
| 77 |
+
doc_number="PTT-002/2568",
|
| 78 |
+
po_number="4500987654",
|
| 79 |
+
doc_type=DOC_TYPE_OPTIONS[0],
|
| 80 |
+
signer="นายทดสอบ ระบบดี",
|
| 81 |
+
signer_position="",
|
| 82 |
+
lookup_data=LOOKUP,
|
| 83 |
+
start_date=date(2026, 7, 20),
|
| 84 |
+
delivery_raw=multi_nguad_tor,
|
| 85 |
+
selected_nguad={"label": "งวดที่ 2", "number": 2, "manual": False},
|
| 86 |
+
now=datetime(2026, 7, 16, 12, 0, 0),
|
| 87 |
+
)
|
| 88 |
+
doc = Document(BytesIO(docx_bytes))
|
| 89 |
+
all_text = "\n".join(p.text for p in doc.paragraphs)
|
| 90 |
+
assert "ภายใน 120 วัน" in all_text # the chosen งวด's deadline
|
| 91 |
+
assert "ภายใน 15 วัน" not in all_text # not งวดที่ 1's
|