system-prompts-and-models-o.../dealix/auto_client_acquisition/intelligence_layer/revenue_dna.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

91 lines
3.4 KiB
Python

"""Revenue DNA — extract the company's growth fingerprint."""
from __future__ import annotations
from collections import Counter
from typing import Any
def extract_revenue_dna(
*,
customer_id: str,
won_deals: list[dict[str, Any]] | None = None,
replies: list[dict[str, Any]] | None = None,
objections: list[dict[str, Any]] | None = None,
) -> dict[str, Any]:
"""
Compute the customer's growth DNA.
Inputs are optional; missing inputs return sensible defaults
so the dashboard always has something to render.
"""
won_deals = won_deals or []
replies = replies or []
objections = objections or []
# Best channel = channel that produced the most won_deals
chan_counter: Counter[str] = Counter()
seg_counter: Counter[str] = Counter()
angle_counter: Counter[str] = Counter()
cycle_days: list[float] = []
for d in won_deals:
chan_counter[d.get("channel", "?")] += 1
seg_counter[d.get("segment", "?")] += 1
angle_counter[d.get("message_angle", "?")] += 1
if "cycle_days" in d:
cycle_days.append(float(d["cycle_days"]))
# Common objection
obj_counter: Counter[str] = Counter()
for o in objections:
obj_counter[o.get("objection_id", "?")] += 1
next_experiment_ar = (
"اختبر رسالة قصيرة (≤4 سطور) + CTA واحد على القناة الأنجح."
if len(won_deals) >= 5 else
"ركّز على بناء أول 10 deals عبر «10 فرص في 10 دقائق»."
)
return {
"customer_id": customer_id,
"best_channel": (chan_counter.most_common(1)[0][0] if chan_counter else "whatsapp"),
"best_segment": (seg_counter.most_common(1)[0][0] if seg_counter else "inbound_lead"),
"best_message_angle": (
angle_counter.most_common(1)[0][0] if angle_counter
else "value_first_short_arabic"
),
"common_objection": (obj_counter.most_common(1)[0][0] if obj_counter else "send_offer_whatsapp"),
"fastest_conversion_days": round(
min(cycle_days) if cycle_days else 0, 1
),
"median_conversion_days": round(
sorted(cycle_days)[len(cycle_days) // 2] if cycle_days else 0, 1
),
"deals_observed": len(won_deals),
"next_experiment_ar": next_experiment_ar,
}
def build_revenue_dna_demo() -> dict[str, Any]:
"""Demo Revenue DNA with realistic Saudi B2B values."""
return extract_revenue_dna(
customer_id="demo",
won_deals=[
{"channel": "whatsapp", "segment": "inbound_lead",
"message_angle": "value_first_short_arabic", "cycle_days": 18},
{"channel": "whatsapp", "segment": "existing_customer",
"message_angle": "expansion_offer", "cycle_days": 12},
{"channel": "email", "segment": "referral",
"message_angle": "warm_intro", "cycle_days": 25},
{"channel": "whatsapp", "segment": "event_lead",
"message_angle": "value_first_short_arabic", "cycle_days": 30},
{"channel": "whatsapp", "segment": "inbound_lead",
"message_angle": "value_first_short_arabic", "cycle_days": 15},
],
objections=[
{"objection_id": "send_offer_whatsapp"},
{"objection_id": "send_offer_whatsapp"},
{"objection_id": "price_high"},
],
)