system-prompts-and-models-o.../dealix/auto_client_acquisition/intelligence_layer/growth_brain.py
Dealix Builder 4e969131c7 feat(platform+intelligence): Growth Control Tower + Growth Neural Network — 20 modules + 25 endpoints + 60 tests
Platform Services Layer (10 modules) — برج التحكم بالنمو
- event_bus: 27 typed events (whatsapp/email/calendar/lead/payment/review/social/partner/sheet/crm/action)
- identity_resolution: cross-channel merge (phone+email+CRM+social) with confidence scoring
- channel_registry: 11 channels (WA, Gmail, Calendar, Moyasar, LinkedIn, X, IG, GBP, Sheets, CRM, Forms) with capabilities/risk/PDPL notes
- action_policy: 9 rules (block_cold_whatsapp, block_payment_no_confirm, block_secrets, external_send_needs_approval, calendar_insert_needs_approval, social_dm_needs_explicit, unknown_source_review, high_value_deal_review, draft_only_safe)
- tool_gateway: single execution chokepoint, env-flag-gated live actions (default OFF)
- unified_inbox: 8 card types, ≤3 buttons enforced, Arabic
- action_ledger: requested→approved→executed audit trail
- proof_ledger: leads/meetings/drafts/sends/payments/revenue/risks_blocked/time_saved per channel
- service_catalog: 12 sellable services
- router api/routers/platform_services.py — 13 endpoints under /api/v1/platform/

Intelligence Layer (10 modules) — الشبكة العصبية للنمو
- growth_brain: per-customer Brain + is_ready_for_autopilot() (≥30 signals + ≥40% accept)
- command_feed: 9 daily card types (opportunity/revenue_leak/partner_suggestion/meeting_prep/review_response/competitive_move/customer_reactivation/ai_visibility_alert/action_required)
- action_graph: 10 typed edges (signal→action→outcome) with what_works_summary
- mission_engine: 7 missions, KILL FEATURE first_10_opportunities (10 فرص في 10 دقائق)
- decision_memory: learns from accept/skip/edit/block, returns preferences (channels, tones, sectors, rejected actions, accept_rate)
- trust_score: composite 0-100 (source+opt_in+channel+content+freq+approval) → safe/needs_review/blocked
- revenue_dna: best_channel/segment/angle + common_objection + avg_cycle_days
- opportunity_simulator: 9 Saudi sectors, expected_replies/meetings/deals/pipeline_sar + risk_score
- competitive_moves: 8 move types with Arabic recommended_action_ar
- board_brief: weekly Founder Shadow Board (3 decisions + 3 opportunities + 3 risks + relationship + experiment + metric)
- router api/routers/intelligence_layer.py — 12 endpoints under /api/v1/intelligence/

Tests
- tests/unit/test_platform_services.py — 31 tests covering catalog/channels/events/policy/gateway/identity/inbox/ledger/proof
- tests/unit/test_intelligence_layer.py — 29 tests covering brain/feed/graph/missions/memory/trust/dna/simulator/competitive/brief
- 60/60 new tests pass; full suite 587 passed, 2 skipped

Docs
- docs/PLATFORM_SERVICES_STRATEGY.md (Arabic)
- docs/INTELLIGENCE_LAYER_STRATEGY.md (Arabic)
- docs/DEALIX_100_PERCENT_LAUNCH_PLAN.md — added §32 Platform Services + §33 Intelligence Layer

Safety
- No live send by default (all WA/Gmail/Calendar/Moyasar guarded by env flags, all OFF)
- All external actions go through Tool Gateway → Action Policy → draft/approval_required
- No secrets allowed in payloads (block_secrets policy)
- PDPL-aware: cold WhatsApp without consent is hard-blocked
- Existing 477+ tests untouched (no breaking changes)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-01 16:05:12 +03:00

81 lines
3.4 KiB
Python

"""Growth Brain — per-customer context + preferences + priorities."""
from __future__ import annotations
from dataclasses import dataclass, field
from typing import Any
@dataclass
class GrowthBrain:
"""The customer's growth context as a single object."""
customer_id: str
company_context: dict[str, Any]
channels_connected: tuple[str, ...]
target_segments: tuple[str, ...]
approved_actions: tuple[str, ...]
blocked_actions: tuple[str, ...]
growth_priorities: tuple[str, ...]
risk_tolerance: str = "medium" # low / medium / high
preferred_tone: str = "warm" # formal / warm / direct
accept_rate_30d: float = 0.0
avg_response_minutes: int = 0
learning_signal_count: int = 0
def to_dict(self) -> dict[str, Any]:
return {
"customer_id": self.customer_id,
"company_context": self.company_context,
"channels_connected": list(self.channels_connected),
"target_segments": list(self.target_segments),
"approved_actions": list(self.approved_actions),
"blocked_actions": list(self.blocked_actions),
"growth_priorities": list(self.growth_priorities),
"risk_tolerance": self.risk_tolerance,
"preferred_tone": self.preferred_tone,
"accept_rate_30d": self.accept_rate_30d,
"avg_response_minutes": self.avg_response_minutes,
"learning_signal_count": self.learning_signal_count,
}
def is_ready_for_autopilot(self) -> bool:
"""≥30 learned signals + ≥40% accept rate + non-empty channels."""
return (
self.learning_signal_count >= 30
and self.accept_rate_30d >= 0.40
and len(self.channels_connected) > 0
)
def build_growth_brain(payload: dict[str, Any] | None = None) -> GrowthBrain:
"""Build a brain from a customer payload — sane Saudi-B2B defaults."""
p = payload or {}
return GrowthBrain(
customer_id=str(p.get("customer_id") or "demo"),
company_context={
"company_name": p.get("company_name", "Demo Saudi B2B Co."),
"sector": p.get("sector", "real_estate"),
"city": p.get("city", "الرياض"),
"offer_one_liner": p.get("offer_one_liner", "تشغيل نمو B2B سعودي"),
"ideal_customer": p.get("ideal_customer", "شركات SMB سعودية"),
"average_deal_size_sar": float(p.get("average_deal_size_sar", 25_000)),
},
channels_connected=tuple(p.get("channels_connected", ("whatsapp",))),
target_segments=tuple(p.get("target_segments", ("inbound_lead", "existing_customer"))),
approved_actions=tuple(p.get("approved_actions", (
"create_draft", "send_with_approval", "ingest_lead",
))),
blocked_actions=tuple(p.get("blocked_actions", (
"cold_send_without_consent", "charge_card_without_user_action",
))),
growth_priorities=tuple(p.get("growth_priorities", (
"fill_pipeline", "improve_response_time", "build_partner_channel",
))),
risk_tolerance=p.get("risk_tolerance", "medium"),
preferred_tone=p.get("preferred_tone", "warm"),
accept_rate_30d=float(p.get("accept_rate_30d", 0.0)),
avg_response_minutes=int(p.get("avg_response_minutes", 0)),
learning_signal_count=int(p.get("learning_signal_count", 0)),
)