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

@@ -47,13 +47,21 @@
<div class="column is-7"> <div class="column is-7">
<div class="box"> <div class="box">
<p class="heading">History</p> <p class="heading">History</p>
<div style="height: 360px;"> {% if graph_applicable %}
<canvas id="metric-detail-chart"></canvas> <div style="height: 360px;">
</div> <canvas id="metric-detail-chart"></canvas>
{% if not graph_points %} </div>
<p class="is-size-7 has-text-grey" style="margin-top: 0.65rem;"> {% if not graph_points %}
No historical points yet for this metric. <p class="is-size-7 has-text-grey" style="margin-top: 0.65rem;">
</p> No historical points yet for this metric.
</p>
{% endif %}
{% else %}
<article class="message is-light">
<div class="message-body">
This is a point-in-time metric and is not charted.
</div>
</article>
{% endif %} {% endif %}
</div> </div>
</div> </div>
@@ -68,6 +76,10 @@
return; return;
} }
var points = JSON.parse(node.textContent || "[]"); var points = JSON.parse(node.textContent || "[]");
var shouldRender = {{ graph_applicable|yesno:"true,false" }};
if (!shouldRender) {
return;
}
var labels = points.map(function(row) { var labels = points.map(function(row) {
var dt = new Date(row.x); var dt = new Date(row.x);
return dt.toLocaleString(); return dt.toLocaleString();

View File

@@ -37,6 +37,7 @@
{% include "partials/compose-send-status.html" %} {% include "partials/compose-send-status.html" %}
</div> </div>
<div id="{{ panel_id }}-popover-backdrop" class="compose-ai-popover-backdrop is-hidden"></div>
<div id="{{ panel_id }}-popover" class="compose-ai-popover is-hidden"> <div id="{{ panel_id }}-popover" class="compose-ai-popover is-hidden">
<div class="compose-ai-card" data-kind="drafts"> <div class="compose-ai-card" data-kind="drafts">
<p class="compose-ai-title">Draft Suggestions</p> <p class="compose-ai-title">Draft Suggestions</p>
@@ -58,6 +59,22 @@
</div> </div>
<div class="compose-ai-card" data-kind="engage"> <div class="compose-ai-card" data-kind="engage">
<p class="compose-ai-title">Quick Engage (Shared Framing)</p> <p class="compose-ai-title">Quick Engage (Shared Framing)</p>
<div class="compose-engage-source-row">
<div class="select is-small is-fullwidth">
<select class="engage-source-select">
<option value="">Auto</option>
</select>
</div>
<button type="button" class="button is-light is-small engage-refresh-btn">
Refresh
</button>
</div>
<div class="field compose-engage-custom-wrap is-hidden">
<textarea
class="textarea is-small engage-custom-text"
rows="2"
placeholder="Write custom engagement text..."></textarea>
</div>
<div class="compose-ai-loading"> <div class="compose-ai-loading">
<div class="compose-ai-skel"></div> <div class="compose-ai-skel"></div>
<div class="compose-ai-skel"></div> <div class="compose-ai-skel"></div>
@@ -65,10 +82,7 @@
<div class="compose-ai-content"></div> <div class="compose-ai-content"></div>
<div class="compose-ai-safety"> <div class="compose-ai-safety">
<label class="checkbox is-size-7"> <label class="checkbox is-size-7">
<input type="checkbox" class="engage-arm"> Arm Send <input type="checkbox" class="engage-confirm"> Confirm Send To Other Party
</label>
<label class="checkbox is-size-7">
<input type="checkbox" class="engage-confirm"> Confirm Share To Other Party
</label> </label>
<button type="button" class="button is-link is-light is-small engage-send-btn" disabled> <button type="button" class="button is-link is-light is-small engage-send-btn" disabled>
Send Engage Send Engage
@@ -123,10 +137,7 @@
{% endif %} {% endif %}
<div class="compose-send-safety"> <div class="compose-send-safety">
<label class="checkbox is-size-7"> <label class="checkbox is-size-7">
<input type="checkbox" class="manual-arm"> Arm Send <input type="checkbox" class="manual-confirm"> Confirm Send
</label>
<label class="checkbox is-size-7">
<input type="checkbox" class="manual-confirm"> Confirm Intent
</label> </label>
</div> </div>
<div class="compose-composer-capsule"> <div class="compose-composer-capsule">
@@ -253,6 +264,20 @@
width: min(34rem, calc(100% - 1.4rem)); width: min(34rem, calc(100% - 1.4rem));
z-index: 25; z-index: 25;
} }
#{{ panel_id }} .compose-ai-popover-backdrop {
position: absolute;
inset: 0.45rem;
border-radius: 8px;
background: radial-gradient(circle at 100% 0%, rgba(238, 245, 255, 0.42), rgba(255, 255, 255, 0.15) 42%, rgba(255, 255, 255, 0));
z-index: 24;
pointer-events: auto;
opacity: 1;
transition: opacity 140ms ease-out;
}
#{{ panel_id }} .compose-ai-popover-backdrop.is-hidden {
opacity: 0;
pointer-events: none;
}
#{{ panel_id }} .compose-ai-popover.is-hidden { #{{ panel_id }} .compose-ai-popover.is-hidden {
display: none; display: none;
} }
@@ -266,7 +291,7 @@
} }
#{{ panel_id }} .compose-ai-card.is-active { #{{ panel_id }} .compose-ai-card.is-active {
display: block; display: block;
animation: composeFadeIn 160ms ease-out; animation: composeFadeIn 180ms ease-out;
} }
#{{ panel_id }} .compose-ai-title { #{{ panel_id }} .compose-ai-title {
font-weight: 600; font-weight: 600;
@@ -294,6 +319,22 @@
text-align: left; text-align: left;
margin-bottom: 0.45rem; margin-bottom: 0.45rem;
border-radius: 8px; border-radius: 8px;
white-space: normal;
word-break: break-word;
height: auto;
justify-content: flex-start;
align-items: flex-start;
line-height: 1.35;
}
#{{ panel_id }} .compose-draft-option strong {
display: inline;
margin-right: 0.2rem;
white-space: normal;
}
#{{ panel_id }} .compose-draft-option span {
white-space: normal;
word-break: break-word;
overflow-wrap: anywhere;
} }
#{{ panel_id }} .compose-draft-option:last-child { #{{ panel_id }} .compose-draft-option:last-child {
margin-bottom: 0; margin-bottom: 0;
@@ -305,6 +346,18 @@
flex-wrap: wrap; flex-wrap: wrap;
align-items: center; align-items: center;
} }
#{{ panel_id }} .compose-engage-source-row {
display: flex;
gap: 0.45rem;
margin-bottom: 0.45rem;
align-items: center;
}
#{{ panel_id }} .compose-engage-source-row .select {
flex: 1 1 auto;
}
#{{ panel_id }} .compose-engage-custom-wrap {
margin-bottom: 0.45rem;
}
@keyframes composePulse { @keyframes composePulse {
0% { background-position: 100% 0; } 0% { background-position: 100% 0; }
100% { background-position: 0 0; } 100% { background-position: 0 0; }
@@ -344,6 +397,7 @@
const statusBox = document.getElementById(panelId + "-status"); const statusBox = document.getElementById(panelId + "-status");
const popover = document.getElementById(panelId + "-popover"); const popover = document.getElementById(panelId + "-popover");
const popoverBackdrop = document.getElementById(panelId + "-popover-backdrop");
const csrfToken = "{{ csrf_token }}"; const csrfToken = "{{ csrf_token }}";
window.giaComposePanels = window.giaComposePanels || {}; window.giaComposePanels = window.giaComposePanels || {};
@@ -511,27 +565,22 @@
} }
}; };
const manualArm = form.querySelector(".manual-arm");
const manualConfirm = form.querySelector(".manual-confirm"); const manualConfirm = form.querySelector(".manual-confirm");
const armInput = form.querySelector("input[name='failsafe_arm']"); const armInput = form.querySelector("input[name='failsafe_arm']");
const confirmInput = form.querySelector("input[name='failsafe_confirm']"); const confirmInput = form.querySelector("input[name='failsafe_confirm']");
const sendButton = form.querySelector(".compose-send-btn"); const sendButton = form.querySelector(".compose-send-btn");
const updateManualSafety = function () { const updateManualSafety = function () {
const arm = !!(manualArm && manualArm.checked);
const confirm = !!(manualConfirm && manualConfirm.checked); const confirm = !!(manualConfirm && manualConfirm.checked);
if (armInput) { if (armInput) {
armInput.value = arm ? "1" : "0"; armInput.value = confirm ? "1" : "0";
} }
if (confirmInput) { if (confirmInput) {
confirmInput.value = confirm ? "1" : "0"; confirmInput.value = confirm ? "1" : "0";
} }
if (sendButton) { if (sendButton) {
sendButton.disabled = !(arm && confirm); sendButton.disabled = !confirm;
} }
}; };
if (manualArm) {
manualArm.addEventListener("change", updateManualSafety);
}
if (manualConfirm) { if (manualConfirm) {
manualConfirm.addEventListener("change", updateManualSafety); manualConfirm.addEventListener("change", updateManualSafety);
} }
@@ -552,6 +601,9 @@
if (!popover) { if (!popover) {
return; return;
} }
if (popoverBackdrop) {
popoverBackdrop.classList.add("is-hidden");
}
popover.classList.add("is-hidden"); popover.classList.add("is-hidden");
popover.querySelectorAll(".compose-ai-card").forEach(function (card) { popover.querySelectorAll(".compose-ai-card").forEach(function (card) {
card.classList.remove("is-active"); card.classList.remove("is-active");
@@ -563,6 +615,9 @@
if (!popover) { if (!popover) {
return null; return null;
} }
if (popoverBackdrop) {
popoverBackdrop.classList.remove("is-hidden");
}
popover.classList.remove("is-hidden"); popover.classList.remove("is-hidden");
let active = null; let active = null;
popover.querySelectorAll(".compose-ai-card").forEach(function (card) { popover.querySelectorAll(".compose-ai-card").forEach(function (card) {
@@ -668,15 +723,144 @@
} }
}; };
const loadEngage = async function (card, preferredSource) {
card = card || showCard("engage");
if (!card) {
return;
}
setCardLoading(card, true);
panelState.engageToken = "";
const sourceSelect = card.querySelector(".engage-source-select");
const refreshBtn = card.querySelector(".engage-refresh-btn");
const sendBtn = card.querySelector(".engage-send-btn");
const confirm = card.querySelector(".engage-confirm");
const customWrap = card.querySelector(".compose-engage-custom-wrap");
const customText = card.querySelector(".engage-custom-text");
const selectedSource = (
preferredSource !== undefined
? preferredSource
: (sourceSelect ? sourceSelect.value : "")
);
const customValue = customText ? String(customText.value || "").trim() : "";
const showCustom = selectedSource === "custom";
confirm.checked = false;
sendBtn.disabled = true;
if (customWrap) {
customWrap.classList.toggle("is-hidden", !showCustom);
}
if (refreshBtn) {
refreshBtn.classList.add("is-loading");
}
try {
const params = queryParams();
if (selectedSource) {
params.set("source_ref", selectedSource);
}
if (showCustom && customValue) {
params.set("custom_text", customValue);
}
const response = await fetch(
thread.dataset.engagePreviewUrl + "?" + params.toString(),
{
method: "GET",
credentials: "same-origin",
headers: { Accept: "application/json" }
}
);
const payload = await response.json();
setCardLoading(card, false);
if (!payload.ok) {
card.querySelector(".compose-ai-content").textContent = payload.error || "Failed to load engage preview.";
panelState.engageToken = "";
return;
}
const options = Array.isArray(payload.options) ? payload.options : [];
if (sourceSelect) {
const before = sourceSelect.value;
sourceSelect.innerHTML = "";
options.forEach(function (opt) {
const option = document.createElement("option");
option.value = String(opt.value || "");
option.textContent = String(opt.label || "");
sourceSelect.appendChild(option);
});
const payloadSelected = String(payload.selected_source || "");
if (payloadSelected) {
sourceSelect.value = payloadSelected;
} else if (before) {
sourceSelect.value = before;
}
if (!sourceSelect.value && sourceSelect.options.length > 0) {
sourceSelect.selectedIndex = 0;
}
}
if (customText && payload.custom_text !== undefined) {
customText.value = String(payload.custom_text || "");
}
if (customWrap && sourceSelect) {
customWrap.classList.toggle("is-hidden", sourceSelect.value !== "custom");
}
panelState.engageToken = String(payload.token || "");
let text = String(payload.preview || "");
if (payload.artifact) {
text = text + "\n\nSource: " + String(payload.artifact);
}
card.querySelector(".compose-ai-content").textContent = text;
sendBtn.disabled = !(confirm.checked && panelState.engageToken);
} catch (err) {
setCardLoading(card, false);
card.querySelector(".compose-ai-content").textContent = "Failed to load engage preview.";
panelState.engageToken = "";
} finally {
if (refreshBtn) {
refreshBtn.classList.remove("is-loading");
}
}
};
const bindEngageControls = function (card) { const bindEngageControls = function (card) {
const arm = card.querySelector(".engage-arm");
const confirm = card.querySelector(".engage-confirm"); const confirm = card.querySelector(".engage-confirm");
const send = card.querySelector(".engage-send-btn"); const send = card.querySelector(".engage-send-btn");
const sourceSelect = card.querySelector(".engage-source-select");
const refreshBtn = card.querySelector(".engage-refresh-btn");
const customText = card.querySelector(".engage-custom-text");
const customWrap = card.querySelector(".compose-engage-custom-wrap");
let customDebounce = null;
const sync = function () { const sync = function () {
send.disabled = !(arm.checked && confirm.checked && panelState.engageToken); send.disabled = !(confirm.checked && panelState.engageToken);
}; };
arm.addEventListener("change", sync);
confirm.addEventListener("change", sync); confirm.addEventListener("change", sync);
if (sourceSelect) {
sourceSelect.addEventListener("change", function () {
if (customWrap) {
customWrap.classList.toggle("is-hidden", sourceSelect.value !== "custom");
}
loadEngage(card, sourceSelect.value);
});
}
if (customText) {
customText.addEventListener("input", function () {
if (!sourceSelect || sourceSelect.value !== "custom") {
return;
}
if (customDebounce) {
clearTimeout(customDebounce);
}
customDebounce = setTimeout(function () {
loadEngage(card, "custom");
}, 260);
});
}
if (refreshBtn) {
refreshBtn.addEventListener("click", function () {
loadEngage(card, sourceSelect ? sourceSelect.value : "");
});
}
send.addEventListener("click", async function () { send.addEventListener("click", async function () {
if (!panelState.engageToken) { if (!panelState.engageToken) {
return; return;
@@ -688,7 +872,7 @@
formData.set("person", thread.dataset.person); formData.set("person", thread.dataset.person);
} }
formData.set("engage_token", panelState.engageToken); formData.set("engage_token", panelState.engageToken);
formData.set("failsafe_arm", arm.checked ? "1" : "0"); formData.set("failsafe_arm", confirm.checked ? "1" : "0");
formData.set("failsafe_confirm", confirm.checked ? "1" : "0"); formData.set("failsafe_confirm", confirm.checked ? "1" : "0");
try { try {
const response = await fetch(thread.dataset.engageSendUrl, { const response = await fetch(thread.dataset.engageSendUrl, {
@@ -715,48 +899,6 @@
}); });
}; };
const loadEngage = async function () {
const card = showCard("engage");
if (!card) {
return;
}
setCardLoading(card, true);
panelState.engageToken = "";
const sendBtn = card.querySelector(".engage-send-btn");
const arm = card.querySelector(".engage-arm");
const confirm = card.querySelector(".engage-confirm");
arm.checked = false;
confirm.checked = false;
sendBtn.disabled = true;
try {
const response = await fetch(thread.dataset.engagePreviewUrl + "?" + queryParams().toString(), {
method: "GET",
credentials: "same-origin",
headers: { Accept: "application/json" }
});
const payload = await response.json();
setCardLoading(card, false);
if (!payload.ok) {
card.querySelector(".compose-ai-content").textContent = payload.error || "Failed to load engage preview.";
return;
}
panelState.engageToken = String(payload.token || "");
let text = String(payload.preview || "");
if (payload.artifact) {
text = text + "\n\nSource: " + String(payload.artifact);
}
card.querySelector(".compose-ai-content").textContent = text;
sendBtn.disabled = true;
} catch (err) {
setCardLoading(card, false);
card.querySelector(".compose-ai-content").textContent = "Failed to load engage preview.";
}
if (!card.dataset.bound) {
bindEngageControls(card);
card.dataset.bound = "1";
}
};
panel.querySelectorAll(".js-ai-trigger").forEach(function (button) { panel.querySelectorAll(".js-ai-trigger").forEach(function (button) {
button.addEventListener("click", function () { button.addEventListener("click", function () {
const kind = button.dataset.kind; const kind = button.dataset.kind;
@@ -769,7 +911,12 @@
} else if (kind === "summary") { } else if (kind === "summary") {
loadSummary(); loadSummary();
} else if (kind === "engage") { } else if (kind === "engage") {
loadEngage(); const card = showCard("engage");
if (card && !card.dataset.bound) {
bindEngageControls(card);
card.dataset.bound = "1";
}
loadEngage(card);
} }
}); });
}); });
@@ -777,15 +924,30 @@
panelState.docClickHandler = function (event) { panelState.docClickHandler = function (event) {
if (!panel.contains(event.target)) { if (!panel.contains(event.target)) {
hideAllCards(); hideAllCards();
return;
}
const clickedTrigger = event.target.closest(".js-ai-trigger");
if (clickedTrigger) {
return;
}
if (popover && !popover.classList.contains("is-hidden")) {
if (!popover.contains(event.target)) {
hideAllCards();
}
} }
}; };
if (popoverBackdrop) {
popoverBackdrop.addEventListener("click", function () {
hideAllCards();
});
}
document.addEventListener("mousedown", panelState.docClickHandler); document.addEventListener("mousedown", panelState.docClickHandler);
textarea.addEventListener("keydown", function (event) { textarea.addEventListener("keydown", function (event) {
if (event.key === "Enter" && !event.shiftKey) { if (event.key === "Enter" && !event.shiftKey) {
event.preventDefault(); event.preventDefault();
if (sendButton && sendButton.disabled) { if (sendButton && sendButton.disabled) {
setStatus("Enable both send safety switches before sending.", "warning"); setStatus("Enable send confirmation before sending.", "warning");
return; return;
} }
form.requestSubmit(); form.requestSubmit();

View File

@@ -260,6 +260,57 @@ def _best_engage_source(plan):
return (None, "") 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): def _context_base(user, service, identifier, person):
person_identifier = None person_identifier = None
if person is not None: if person is not None:
@@ -672,12 +723,54 @@ class ComposeEngagePreview(LoginRequiredMixin, View):
owner_name = _owner_name(request.user) owner_name = _owner_name(request.user)
recipient_name = base["person"].name if base["person"] else "Other" recipient_name = base["person"].name if base["person"] else "Other"
plan = _latest_plan_for_person(request.user, base["person"]) 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 = "" preview = ""
outbound = "" outbound = ""
artifact_label = "AI-generated" 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( payload = _build_engage_payload(
source_obj=source_obj, source_obj=source_obj,
source_kind=source_kind, source_kind=source_kind,
@@ -707,17 +800,19 @@ class ComposeEngagePreview(LoginRequiredMixin, View):
) )
preview = f"**Shared Engage** (Correction)\n\nGuidance:\n{outbound}" preview = f"**Shared Engage** (Correction)\n\nGuidance:\n{outbound}"
token = signing.dumps( token = ""
{ if outbound:
"u": request.user.id, token = signing.dumps(
"s": base["service"], {
"i": base["identifier"], "u": request.user.id,
"p": str(base["person"].id) if base["person"] else "", "s": base["service"],
"outbound": outbound, "i": base["identifier"],
"exp": int(time.time()) + (60 * 10), "p": str(base["person"].id) if base["person"] else "",
}, "outbound": outbound,
salt=COMPOSE_ENGAGE_TOKEN_SALT, "exp": int(time.time()) + (60 * 10),
) },
salt=COMPOSE_ENGAGE_TOKEN_SALT,
)
return JsonResponse( return JsonResponse(
{ {
"ok": True, "ok": True,
@@ -725,6 +820,9 @@ class ComposeEngagePreview(LoginRequiredMixin, View):
"outbound": outbound, "outbound": outbound,
"token": token, "token": token,
"artifact": artifact_label, "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() failsafe_confirm = str(request.POST.get("failsafe_confirm") or "").strip()
if failsafe_arm != "1" or failsafe_confirm != "1": if failsafe_arm != "1" or failsafe_confirm != "1":
return JsonResponse( 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() token = str(request.POST.get("engage_token") or "").strip()
@@ -814,7 +912,7 @@ class ComposeSend(LoginRequiredMixin, View):
request, request,
"partials/compose-send-status.html", "partials/compose-send-status.html",
{ {
"notice_message": "Enable both send safety switches before sending.", "notice_message": "Enable send confirmation before sending.",
"notice_level": "warning", "notice_level": "warning",
}, },
) )

View File

@@ -338,7 +338,7 @@ INSIGHT_METRICS = {
"last_event": { "last_event": {
"title": "Last Event", "title": "Last Event",
"group": "timeline", "group": "timeline",
"history_field": "source_event_ts", "history_field": None,
"calculation": "Unix ms timestamp of the newest message in this workspace.", "calculation": "Unix ms timestamp of the newest message in this workspace.",
"psychology": ( "psychology": (
"Long inactivity windows can indicate pause, repair distance, or " "Long inactivity windows can indicate pause, repair distance, or "
@@ -493,14 +493,6 @@ INSIGHT_GRAPH_SPECS = [
"y_min": 0, "y_min": 0,
"y_max": 100, "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): 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": if metric_slug == "stability_score":
score = conversation.stability_score score = conversation.stability_score
if score is None: if score is None:
@@ -760,6 +782,12 @@ def _history_points(conversation, field_name):
return points 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): def _all_graph_payload(conversation):
graphs = [] graphs = []
for spec in INSIGHT_GRAPH_SPECS: for spec in INSIGHT_GRAPH_SPECS:
@@ -2902,8 +2930,9 @@ class AIWorkspaceInsightDetail(LoginRequiredMixin, View):
latest_snapshot = conversation.metric_snapshots.first() latest_snapshot = conversation.metric_snapshots.first()
value = _format_metric_value(conversation, metric, latest_snapshot) value = _format_metric_value(conversation, metric, latest_snapshot)
group = INSIGHT_GROUPS[spec["group"]] group = INSIGHT_GROUPS[spec["group"]]
graph_applicable = _metric_supports_history(metric, spec)
points = [] points = []
if spec["history_field"]: if graph_applicable:
points = _history_points(conversation, spec["history_field"]) points = _history_points(conversation, spec["history_field"])
context = { context = {
@@ -2915,6 +2944,7 @@ class AIWorkspaceInsightDetail(LoginRequiredMixin, View):
"metric_psychology_hint": _metric_psychological_read(metric, conversation), "metric_psychology_hint": _metric_psychological_read(metric, conversation),
"metric_group": group, "metric_group": group,
"graph_points": points, "graph_points": points,
"graph_applicable": graph_applicable,
"graphs_url": reverse( "graphs_url": reverse(
"ai_workspace_insight_graphs", "ai_workspace_insight_graphs",
kwargs={"type": "page", "person_id": person.id}, kwargs={"type": "page", "person_id": person.id},