Fix business plans

This commit is contained in:
2026-03-02 00:23:27 +00:00
parent b3e183eb0a
commit a9f5f3f75d
5 changed files with 117 additions and 9 deletions

View File

@@ -15,6 +15,22 @@ log = logs.get_logger("command_engine")
_REGISTERED = False
def _channel_variants(service: str, channel_identifier: str) -> list[str]:
value = str(channel_identifier or "").strip()
if not value:
return []
variants = [value]
svc = str(service or "").strip().lower()
if svc == "whatsapp":
bare = value.split("@", 1)[0].strip()
if bare and bare not in variants:
variants.append(bare)
group = f"{bare}@g.us" if bare else ""
if group and group not in variants:
variants.append(group)
return variants
def ensure_handlers_registered():
global _REGISTERED
if _REGISTERED:
@@ -25,6 +41,9 @@ def ensure_handlers_registered():
async def _eligible_profiles(ctx: CommandContext) -> list[CommandProfile]:
def _load():
direct_variants = _channel_variants(ctx.service, ctx.channel_identifier)
if not direct_variants:
return []
direct = list(
CommandProfile.objects.filter(
user_id=ctx.user_id,
@@ -32,7 +51,7 @@ async def _eligible_profiles(ctx: CommandContext) -> list[CommandProfile]:
channel_bindings__enabled=True,
channel_bindings__direction="ingress",
channel_bindings__service=ctx.service,
channel_bindings__channel_identifier=ctx.channel_identifier,
channel_bindings__channel_identifier__in=direct_variants,
).distinct()
)
if direct:
@@ -49,7 +68,8 @@ async def _eligible_profiles(ctx: CommandContext) -> list[CommandProfile]:
identifier = getattr(getattr(trigger, "session", None), "identifier", None)
fallback_service = str(getattr(identifier, "service", "") or "").strip().lower()
fallback_identifier = str(getattr(identifier, "identifier", "") or "").strip()
if not fallback_service or not fallback_identifier:
fallback_variants = _channel_variants(fallback_service, fallback_identifier)
if not fallback_service or not fallback_variants:
return []
return list(
CommandProfile.objects.filter(
@@ -58,7 +78,7 @@ async def _eligible_profiles(ctx: CommandContext) -> list[CommandProfile]:
channel_bindings__enabled=True,
channel_bindings__direction="ingress",
channel_bindings__service=fallback_service,
channel_bindings__channel_identifier=fallback_identifier,
channel_bindings__channel_identifier__in=fallback_variants,
).distinct()
)

View File

@@ -330,8 +330,17 @@ class BPCommandHandler(CommandHandler):
)
fanout_stats = {"sent_bindings": 0, "failed_bindings": 0}
fanout_text = summary
if ai_warning:
warning_text = str(ai_warning or "").strip()
if len(warning_text) > 300:
warning_text = warning_text[:297].rstrip() + "..."
fanout_text = (
"[bp] AI generation failed. Draft document was saved in fallback mode."
+ (f"\nReason: {warning_text}" if warning_text else "")
)
if "post_result" in action_types:
fanout_stats = await self._fanout(run, summary)
fanout_stats = await self._fanout(run, fanout_text)
if "status_in_source" == profile.visibility_mode:
status_text = f"[bp] Generated business plan: {document.title}"