Phase 2 — jev.py client: POST /v1/systemone, PINNED model jev-1.13.0 (drift warning when the response model differs), one retry on 429/5xx, fail-soft (None on any error), usage/latency returned for observability. Config adds TYPESAFE_API_KEY / JEV_* (thresholds 0.85/0.60, 15 questions per field); no daily cap by owner decision. bot/.env is now mounted read-only into the container so the key never enters compose/git.
Phase 3 — matching.py: برونگروهی (one request per field, parallel), درونگروهی (one choice question per mention sharing the same full candidate list + «_none»), درونواریانسی (all STT spellings in one question). Routing: conf>=exact → bind; floor..exact → ambiguity; else/_none → new; missing/failed field → difflib fallback (bug fixed during testing: a failed request used to record 'new' instead of falling back). Resolver gains full cached lists for suppliers/materials (paged) plus brands (API+DB) and specs (EAV values + draft specs). NLU prompt now emits entity_mentions (6 fields) with the voice-variant grouping rule; sanitize_mentions guards the shape. handle_message runs matching only for register_quote/update_draft/add_material behind JEV_ENABLED; jev_map threads into quote/material resolve_payload (Jev hit skips difflib; exact writes ids; ambiguity skips difflib). Spec effect per owner: spelling normalization only (normalize_brand_spec).
Phase 4 — entity-match ambiguity sub-messages: create_ambiguities() builds deduped entries {kind: entity_match, field, variants, candidates, details with Persian percentages}; new keyboards.ambiguity_candidates_keyboard renders one button per candidate + «هیچکدام — ایجاد جدید»; callbacks handle amb⛏️{prefix}:{amb_id}:{idx} (applies the candidate via matching.apply_choice then re-resolves/renders) and amb:new (rejected → auto-create path at commit). Every user choice lands in observability (ambiguity.user_choice with arg+candidates).
Phase 5 — voice variants: NLU groups different STT spellings of one entity into one variants[] (verified live: مصطفوی/مستفدی → one supplier mention); tools/test_matching.py live section (RUN_LIVE=1) exercises real DeepSeek + real Jev without touching Bale.
Tests: test_jev.py 5/5 (mock HTTP: no-key, happy path, 429-retry, 400, exception), test_matching.py 11/11 mock + 3/3 live (real NLU produced 7 mentions with correct fields; real Jev matched «50 کیلویی»→«۵۰ کیلویی» 0.98 and «کیسه» 1.0 and correctly returned new against the emptied catalog); observability regression 8/8.
77 lines
3.4 KiB
Python
77 lines
3.4 KiB
Python
"""Add-project flow: name + optional description."""
|
|
import logging
|
|
|
|
from app import drafts as draft_store
|
|
from app import observability as obs
|
|
from app.api.client import ApiError
|
|
from app.drafts import (
|
|
KIND_PROJECT, merge_project_patch, new_project_payload, project_missing,
|
|
)
|
|
from app.keyboards import simple_review_keyboard
|
|
from app.texts import ERR_GENERIC, esc, project_committed_report, project_review_text
|
|
|
|
logger = logging.getLogger("bot.project")
|
|
|
|
|
|
async def handle_entities(ctx, chat_id, user, patch: dict, source: str | None,
|
|
trace_id: str | None = None) -> None:
|
|
draft = await draft_store.get_active(chat_id, KIND_PROJECT)
|
|
if draft is not None:
|
|
payload = draft.payload
|
|
merge_project_patch(payload, patch)
|
|
else:
|
|
payload = new_project_payload()
|
|
merge_project_patch(payload, patch)
|
|
draft = await draft_store.create(chat_id, KIND_PROJECT, payload, source=source)
|
|
await obs.link_draft(trace_id, draft.id, draft.kind)
|
|
await draft_store.save_payload(draft, missing={"fields": project_missing(payload)})
|
|
await render(ctx, chat_id, draft, fresh=draft.review_message_id is None, trace_id=trace_id)
|
|
|
|
|
|
def _keyboard(payload: dict, draft_id: str) -> dict:
|
|
return simple_review_keyboard(draft_id, KIND_PROJECT, [])
|
|
|
|
|
|
async def render(ctx, chat_id, draft, fresh: bool = True, trace_id: str | None = None) -> None:
|
|
payload = draft.payload
|
|
missing = project_missing(payload)
|
|
text = project_review_text(payload, missing)
|
|
keyboard = _keyboard(payload, draft.id)
|
|
if fresh:
|
|
message = await ctx.send(chat_id, text, reply_markup=keyboard)
|
|
draft.review_message_id = (message or {}).get("message_id")
|
|
await draft_store.save_payload(draft, missing={"fields": missing},
|
|
review_message_id=draft.review_message_id)
|
|
else:
|
|
await ctx.edit(chat_id, draft.review_message_id, text, reply_markup=keyboard)
|
|
await draft_store.save_payload(draft, missing={"fields": missing})
|
|
await obs.event(trace_id or await obs.latest_trace_for_draft(draft.id), "draft.render",
|
|
payload={"fresh": fresh, "missing": [m.get("key") for m in missing if not m.get("ok")]})
|
|
|
|
|
|
async def commit(ctx, chat_id, user, draft, trace_id: str | None = None) -> None:
|
|
payload = draft.payload
|
|
trace_id = trace_id or await obs.latest_trace_for_draft(draft.id)
|
|
missing = {m["key"]: m["ok"] for m in project_missing(payload)}
|
|
if not missing.get("name"):
|
|
await ctx.send(chat_id, "ثبت ممکن نیست؛ نام پروژه مشخص نشده است.")
|
|
return
|
|
body = {"name": payload["name"]}
|
|
if payload.get("description"):
|
|
body["description"] = payload["description"]
|
|
try:
|
|
created = await ctx.api.create_project(body, actor_id=user.actor_id)
|
|
except ApiError as exc:
|
|
await ctx.send(chat_id, ERR_GENERIC.format(detail=esc(exc.message)))
|
|
return
|
|
except Exception:
|
|
logger.exception("create_project failed")
|
|
await ctx.send(chat_id, ERR_GENERIC.format(detail="خطای سرور"))
|
|
return
|
|
await draft_store.set_status(draft.id, "committed")
|
|
await obs.event(trace_id, "draft.commit", payload={"kind": "project",
|
|
"project_id": (created or {}).get("id")})
|
|
await obs.set_trace_status(trace_id, "committed")
|
|
ctx.resolver.invalidate()
|
|
await ctx.send(chat_id, project_committed_report(created))
|