The bot service in docker-compose.yml builds from ./bot, but the agent code lived in 'ai procurement/' and ./bot was only a Windows junction on reza.g's machine (and was gitignored) — exactly as the team reported: 'bot/ was never committed — prod-v7's bot is un-buildable from the repo.' - junction removed; 'ai procurement/' renamed to 'bot/' via git mv (all 41 files keep their history as pure renames) - 'bot/' dropped from .gitignore (voice/ stays ignored — runtime voice notes; bot/.env stays ignored by bot/.gitignore) - compose needs no change: context: ./bot and ./bot/app now resolve to committed files - bot/INTEGRATION-REPORT.md added: full story (problem, root cause, git state, security note on the committed BALE_BOT_TOKEN / AI_API_KEY compose defaults, and usage instructions for the team) Verified: 41 renames staged with zero secret/cache files; voice/ and bot/.env still ignored.
99 lines
4.2 KiB
Python
99 lines
4.2 KiB
Python
"""Add-material flow: name + canonical unit (required)."""
|
|
import logging
|
|
|
|
from app import drafts as draft_store
|
|
from app.api.client import ApiError
|
|
from app.drafts import (
|
|
KIND_MATERIAL, merge_material_patch, material_missing, new_material_payload,
|
|
)
|
|
from app.keyboards import pick_buttons, simple_review_keyboard
|
|
from app.resolve import safe_resolve
|
|
from app.texts import ERR_GENERIC, esc, material_committed_report, material_review_text
|
|
|
|
logger = logging.getLogger("bot.material")
|
|
|
|
|
|
async def handle_entities(ctx, chat_id, user, patch: dict, source: str | None) -> None:
|
|
draft = await draft_store.get_active(chat_id, KIND_MATERIAL)
|
|
if draft is not None:
|
|
payload = draft.payload
|
|
merge_material_patch(payload, patch)
|
|
else:
|
|
payload = new_material_payload()
|
|
merge_material_patch(payload, patch)
|
|
draft = await draft_store.create(chat_id, KIND_MATERIAL, payload, source=source)
|
|
await resolve_payload(ctx, payload)
|
|
await draft_store.save_payload(draft, missing={"fields": material_missing(payload)})
|
|
await render(ctx, chat_id, draft, fresh=draft.review_message_id is None)
|
|
|
|
|
|
async def resolve_payload(ctx, payload: dict) -> None:
|
|
options = payload.setdefault("options", {})
|
|
if not payload.get("canonical_unit_id") and payload.get("unit_name"):
|
|
res = await safe_resolve(ctx.resolver, "unit", payload["unit_name"])
|
|
if res.resolved:
|
|
payload["canonical_unit_id"] = res.candidates[0].id
|
|
payload["unit_name"] = res.candidates[0].name
|
|
options.pop("unit", None)
|
|
elif res.candidates:
|
|
options["unit"] = [{"id": c.id, "name": c.name} for c in res.candidates]
|
|
else:
|
|
options.pop("unit", None)
|
|
|
|
|
|
async def apply_pick(ctx, chat_id, user, draft, field: str, choice: dict) -> None:
|
|
if field == "unit":
|
|
draft.payload["canonical_unit_id"] = choice["id"]
|
|
draft.payload["unit_name"] = choice["name"]
|
|
draft.payload.setdefault("options", {}).pop("unit", None)
|
|
await draft_store.save_payload(draft, missing={"fields": material_missing(draft.payload)})
|
|
await render(ctx, chat_id, draft, fresh=False)
|
|
|
|
|
|
def _keyboard(payload: dict, draft_id: str) -> dict:
|
|
rows: list[list[dict]] = []
|
|
options = (payload.get("options") or {}).get("unit") or []
|
|
if options:
|
|
rows += pick_buttons(draft_id, "unit", [o["name"] for o in options[:6]])
|
|
return simple_review_keyboard(draft_id, KIND_MATERIAL, rows)
|
|
|
|
|
|
async def render(ctx, chat_id, draft, fresh: bool = True) -> None:
|
|
payload = draft.payload
|
|
missing = material_missing(payload)
|
|
text = material_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})
|
|
|
|
|
|
async def commit(ctx, chat_id, user, draft) -> None:
|
|
payload = draft.payload
|
|
await resolve_payload(ctx, payload)
|
|
missing = {m["key"]: m["ok"] for m in material_missing(payload)}
|
|
problems = [label for key, label, ok in [
|
|
("name", "نام کالا", missing.get("name")),
|
|
("unit", "واحد اصلی", missing.get("unit")),
|
|
] if not ok]
|
|
if problems:
|
|
await ctx.send(chat_id, "ثبت ممکن نیست؛ اول اینها را مشخص کن:\n" +
|
|
"\n".join(f"❌ {esc(p)}" for p in problems))
|
|
return
|
|
body = {"name": payload["name"], "canonical_unit_id": int(payload["canonical_unit_id"])}
|
|
if payload.get("topic"):
|
|
body["topic"] = payload["topic"]
|
|
try:
|
|
created = await ctx.api.create_material(body, actor_id=user.actor_id)
|
|
except ApiError as exc:
|
|
await ctx.send(chat_id, ERR_GENERIC.format(detail=esc(exc.message)))
|
|
return
|
|
await draft_store.set_status(draft.id, "committed")
|
|
ctx.resolver.invalidate("materials")
|
|
await ctx.send(chat_id, material_committed_report(created))
|