Implement deeper analysis of people and access to the underlying data in the database

This commit is contained in:
2026-02-15 18:02:52 +00:00
parent a94bbff655
commit e7aac36ef9
4 changed files with 398 additions and 96 deletions

View File

@@ -260,6 +260,57 @@ def _best_engage_source(plan):
return (None, "")
def _engage_source_options(plan):
if plan is None:
return []
options = []
for rule in plan.rules.order_by("created_at"):
options.append(
{
"value": f"rule:{rule.id}",
"label": f"Rule: {rule.title}",
}
)
for game in plan.games.order_by("created_at"):
options.append(
{
"value": f"game:{game.id}",
"label": f"Game: {game.title}",
}
)
for correction in plan.corrections.order_by("created_at"):
options.append(
{
"value": f"correction:{correction.id}",
"label": f"Correction: {correction.title}",
}
)
return options
def _engage_source_from_ref(plan, source_ref):
if plan is None:
return (None, "", "")
ref = str(source_ref or "").strip()
if ":" not in ref:
return (None, "", "")
kind, raw_id = ref.split(":", 1)
kind = kind.strip().lower()
raw_id = raw_id.strip()
model_by_kind = {
"rule": plan.rules,
"game": plan.games,
"correction": plan.corrections,
}
queryset = model_by_kind.get(kind)
if queryset is None:
return (None, "", "")
obj = queryset.filter(id=raw_id).first()
if obj is None:
return (None, "", "")
return (obj, kind, f"{kind}:{obj.id}")
def _context_base(user, service, identifier, person):
person_identifier = None
if person is not None:
@@ -672,12 +723,54 @@ class ComposeEngagePreview(LoginRequiredMixin, View):
owner_name = _owner_name(request.user)
recipient_name = base["person"].name if base["person"] else "Other"
plan = _latest_plan_for_person(request.user, base["person"])
source_obj, source_kind = _best_engage_source(plan)
source_options = _engage_source_options(plan)
source_options_with_custom = (
[{"value": "auto", "label": "Auto"}]
+ source_options
+ [{"value": "custom", "label": "Custom"}]
)
source_ref = str(request.GET.get("source_ref") or "auto").strip().lower()
custom_text = str(request.GET.get("custom_text") or "").strip()
source_obj = None
source_kind = ""
selected_source = source_ref if source_ref else "auto"
if selected_source == "custom":
selected_source = "custom"
else:
if selected_source == "auto":
fallback_obj, fallback_kind = _best_engage_source(plan)
if fallback_obj is not None:
source_obj = fallback_obj
source_kind = fallback_kind
else:
source_obj, source_kind, explicit_ref = _engage_source_from_ref(
plan,
selected_source,
)
if source_obj is None:
selected_source = "auto"
fallback_obj, fallback_kind = _best_engage_source(plan)
if fallback_obj is not None:
source_obj = fallback_obj
source_kind = fallback_kind
else:
selected_source = explicit_ref
preview = ""
outbound = ""
artifact_label = "AI-generated"
if source_obj is not None:
if selected_source == "custom":
outbound = _plain_text(custom_text)
if outbound:
preview = f"**Custom Engage** (Correction)\n\nGuidance:\n{outbound}"
artifact_label = "Custom"
else:
preview = (
"**Custom Engage** (Correction)\n\nGuidance:\n"
"Enter your custom engagement text to preview."
)
elif source_obj is not None:
payload = _build_engage_payload(
source_obj=source_obj,
source_kind=source_kind,
@@ -707,17 +800,19 @@ class ComposeEngagePreview(LoginRequiredMixin, View):
)
preview = f"**Shared Engage** (Correction)\n\nGuidance:\n{outbound}"
token = signing.dumps(
{
"u": request.user.id,
"s": base["service"],
"i": base["identifier"],
"p": str(base["person"].id) if base["person"] else "",
"outbound": outbound,
"exp": int(time.time()) + (60 * 10),
},
salt=COMPOSE_ENGAGE_TOKEN_SALT,
)
token = ""
if outbound:
token = signing.dumps(
{
"u": request.user.id,
"s": base["service"],
"i": base["identifier"],
"p": str(base["person"].id) if base["person"] else "",
"outbound": outbound,
"exp": int(time.time()) + (60 * 10),
},
salt=COMPOSE_ENGAGE_TOKEN_SALT,
)
return JsonResponse(
{
"ok": True,
@@ -725,6 +820,9 @@ class ComposeEngagePreview(LoginRequiredMixin, View):
"outbound": outbound,
"token": token,
"artifact": artifact_label,
"options": source_options_with_custom,
"selected_source": selected_source,
"custom_text": custom_text,
}
)
@@ -744,7 +842,7 @@ class ComposeEngageSend(LoginRequiredMixin, View):
failsafe_confirm = str(request.POST.get("failsafe_confirm") or "").strip()
if failsafe_arm != "1" or failsafe_confirm != "1":
return JsonResponse(
{"ok": False, "error": "Enable both send safety switches first."}
{"ok": False, "error": "Enable send confirmation before sending."}
)
token = str(request.POST.get("engage_token") or "").strip()
@@ -814,7 +912,7 @@ class ComposeSend(LoginRequiredMixin, View):
request,
"partials/compose-send-status.html",
{
"notice_message": "Enable both send safety switches before sending.",
"notice_message": "Enable send confirmation before sending.",
"notice_level": "warning",
},
)

View File

@@ -338,7 +338,7 @@ INSIGHT_METRICS = {
"last_event": {
"title": "Last Event",
"group": "timeline",
"history_field": "source_event_ts",
"history_field": None,
"calculation": "Unix ms timestamp of the newest message in this workspace.",
"psychology": (
"Long inactivity windows can indicate pause, repair distance, or "
@@ -493,14 +493,6 @@ INSIGHT_GRAPH_SPECS = [
"y_min": 0,
"y_max": 100,
},
{
"slug": "last_event",
"title": "Last Event Timestamp",
"field": "source_event_ts",
"group": "timeline",
"y_min": None,
"y_max": None,
},
]
@@ -708,6 +700,36 @@ def _format_metric_value(conversation, metric_slug, latest_snapshot=None):
def _metric_psychological_read(metric_slug, conversation):
if metric_slug == "stability_state":
state = conversation.stability_state
if state == WorkspaceConversation.StabilityState.CALIBRATING:
return (
"Calibrating means the system does not yet have enough longitudinal "
"signal to classify friction reliably. Prioritize collecting a few "
"more days of normal interaction before drawing conclusions."
)
if state == WorkspaceConversation.StabilityState.STABLE:
return (
"Stable indicates low-friction reciprocity and predictable cadence in "
"the sampled window. Keep routines consistent and focus on maintenance "
"habits rather than heavy corrective interventions."
)
if state == WorkspaceConversation.StabilityState.WATCH:
return (
"Watch indicates meaningful strain without full collapse. This often "
"matches early misunderstanding cycles: repair is still easy if you "
"slow pace, validate first, and reduce escalation triggers."
)
if state == WorkspaceConversation.StabilityState.FRAGILE:
return (
"Fragile indicates high volatility or directional imbalance in recent "
"interaction. Use short, clear, safety-first communication and avoid "
"high-load conversations until cadence normalizes."
)
return (
"State is an operational risk band, not a diagnosis. Read it alongside "
"confidence and recent events."
)
if metric_slug == "stability_score":
score = conversation.stability_score
if score is None:
@@ -760,6 +782,12 @@ def _history_points(conversation, field_name):
return points
def _metric_supports_history(metric_slug, metric_spec):
if not metric_spec.get("history_field"):
return False
return any(graph["slug"] == metric_slug for graph in INSIGHT_GRAPH_SPECS)
def _all_graph_payload(conversation):
graphs = []
for spec in INSIGHT_GRAPH_SPECS:
@@ -2902,8 +2930,9 @@ class AIWorkspaceInsightDetail(LoginRequiredMixin, View):
latest_snapshot = conversation.metric_snapshots.first()
value = _format_metric_value(conversation, metric, latest_snapshot)
group = INSIGHT_GROUPS[spec["group"]]
graph_applicable = _metric_supports_history(metric, spec)
points = []
if spec["history_field"]:
if graph_applicable:
points = _history_points(conversation, spec["history_field"])
context = {
@@ -2915,6 +2944,7 @@ class AIWorkspaceInsightDetail(LoginRequiredMixin, View):
"metric_psychology_hint": _metric_psychological_read(metric, conversation),
"metric_group": group,
"graph_points": points,
"graph_applicable": graph_applicable,
"graphs_url": reverse(
"ai_workspace_insight_graphs",
kwargs={"type": "page", "person_id": person.id},