1250 lines
41 KiB
HTML
1250 lines
41 KiB
HTML
<div id="{{ panel_id }}" class="compose-shell">
|
|
<div class="is-flex is-justify-content-space-between is-align-items-center" style="gap: 0.5rem; flex-wrap: wrap;">
|
|
<div>
|
|
<p class="is-size-7 has-text-weight-semibold" style="margin-bottom: 0.2rem;">Manual Text Mode</p>
|
|
<p class="is-size-6" style="margin-bottom: 0;">
|
|
{% if person %}
|
|
{{ person.name }}
|
|
{% else %}
|
|
{{ identifier }}
|
|
{% endif %}
|
|
</p>
|
|
<p class="is-size-7 compose-meta-line" style="margin-bottom: 0;">
|
|
{{ service|title }} · {{ identifier }}
|
|
</p>
|
|
</div>
|
|
<div class="buttons are-small" style="margin: 0;">
|
|
<button type="button" class="button is-light is-rounded js-ai-trigger" data-kind="drafts">
|
|
<span class="icon is-small"><i class="fa-solid fa-pen"></i></span>
|
|
<span>Drafts</span>
|
|
</button>
|
|
<button type="button" class="button is-light is-rounded js-ai-trigger" data-kind="summary">
|
|
<span class="icon is-small"><i class="fa-solid fa-list"></i></span>
|
|
<span>Summary</span>
|
|
</button>
|
|
<button type="button" class="button is-light is-rounded js-ai-trigger" data-kind="engage">
|
|
<span class="icon is-small"><i class="fa-solid fa-handshake"></i></span>
|
|
<span>Engage</span>
|
|
</button>
|
|
<a class="button is-light is-rounded" href="{{ ai_workspace_url }}">
|
|
<span class="icon is-small"><i class="{{ manual_icon_class }}"></i></span>
|
|
<span>AI Workspace</span>
|
|
</a>
|
|
</div>
|
|
</div>
|
|
|
|
<div id="{{ panel_id }}-status" class="compose-status">
|
|
{% include "partials/compose-send-status.html" %}
|
|
</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 class="compose-ai-card" data-kind="drafts">
|
|
<p class="compose-ai-title">Draft Suggestions</p>
|
|
<div class="compose-ai-loading">
|
|
<div class="compose-ai-skel"></div>
|
|
<div class="compose-ai-skel"></div>
|
|
<div class="compose-ai-skel"></div>
|
|
</div>
|
|
<div class="compose-ai-content"></div>
|
|
</div>
|
|
<div class="compose-ai-card" data-kind="summary">
|
|
<p class="compose-ai-title">Conversation Summary</p>
|
|
<div class="compose-ai-loading">
|
|
<div class="compose-ai-skel"></div>
|
|
<div class="compose-ai-skel"></div>
|
|
<div class="compose-ai-skel"></div>
|
|
</div>
|
|
<div class="compose-ai-content"></div>
|
|
</div>
|
|
<div class="compose-ai-card" data-kind="engage">
|
|
<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">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-skel"></div>
|
|
<div class="compose-ai-skel"></div>
|
|
</div>
|
|
<div class="compose-ai-content"></div>
|
|
<div class="compose-ai-safety">
|
|
<label class="checkbox is-size-7">
|
|
<input type="checkbox" class="engage-confirm"> Confirm Send To Other Party
|
|
</label>
|
|
<button type="button" class="button is-link is-light is-small engage-send-btn" disabled>
|
|
Send Engage
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div
|
|
id="{{ panel_id }}-thread"
|
|
class="compose-thread"
|
|
data-poll-url="{% url 'compose_thread' %}"
|
|
data-service="{{ service }}"
|
|
data-identifier="{{ identifier }}"
|
|
data-person="{% if person %}{{ person.id }}{% endif %}"
|
|
data-limit="{{ limit }}"
|
|
data-last-ts="{{ last_ts }}"
|
|
data-ws-url="{{ compose_ws_url }}"
|
|
data-drafts-url="{{ compose_drafts_url }}"
|
|
data-summary-url="{{ compose_summary_url }}"
|
|
data-engage-preview-url="{{ compose_engage_preview_url }}"
|
|
data-engage-send-url="{{ compose_engage_send_url }}">
|
|
{% for msg in serialized_messages %}
|
|
<div class="compose-row {% if msg.outgoing %}is-out{% else %}is-in{% endif %}" data-ts="{{ msg.ts }}">
|
|
<article class="compose-bubble {% if msg.outgoing %}is-out{% else %}is-in{% endif %}">
|
|
{% if msg.image_urls %}
|
|
{% for image_url in msg.image_urls %}
|
|
<figure class="compose-media">
|
|
<img
|
|
class="compose-image"
|
|
src="{{ image_url }}"
|
|
alt="Attachment"
|
|
loading="lazy"
|
|
decoding="async">
|
|
</figure>
|
|
{% endfor %}
|
|
{% elif msg.image_url %}
|
|
<figure class="compose-media">
|
|
<img
|
|
class="compose-image"
|
|
src="{{ msg.image_url }}"
|
|
alt="Attachment"
|
|
loading="lazy"
|
|
decoding="async">
|
|
</figure>
|
|
{% endif %}
|
|
{% if not msg.hide_text %}
|
|
<p class="compose-body">{{ msg.display_text|default:"(no text)" }}</p>
|
|
{% else %}
|
|
<p class="compose-body compose-image-fallback is-hidden">(no text)</p>
|
|
{% endif %}
|
|
<p class="compose-msg-meta">
|
|
{{ msg.display_ts }}{% if msg.author %} · {{ msg.author }}{% endif %}
|
|
</p>
|
|
</article>
|
|
</div>
|
|
{% empty %}
|
|
<p class="compose-empty">No stored messages for this contact yet.</p>
|
|
{% endfor %}
|
|
</div>
|
|
<p id="{{ panel_id }}-typing" class="compose-typing is-hidden">
|
|
{% if person %}{{ person.name }}{% else %}Contact{% endif %} is typing...
|
|
</p>
|
|
|
|
<form
|
|
id="{{ panel_id }}-form"
|
|
class="compose-form"
|
|
hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'
|
|
hx-post="{% url 'compose_send' %}"
|
|
hx-target="#{{ panel_id }}-status"
|
|
hx-swap="innerHTML">
|
|
<input type="hidden" name="service" value="{{ service }}">
|
|
<input type="hidden" name="identifier" value="{{ identifier }}">
|
|
<input type="hidden" name="render_mode" value="{{ render_mode }}">
|
|
<input type="hidden" name="limit" value="{{ limit }}">
|
|
<input type="hidden" name="failsafe_arm" value="0">
|
|
<input type="hidden" name="failsafe_confirm" value="0">
|
|
{% if person %}
|
|
<input type="hidden" name="person" value="{{ person.id }}">
|
|
{% endif %}
|
|
<div class="compose-send-safety">
|
|
<label class="checkbox is-size-7">
|
|
<input type="checkbox" class="manual-confirm"> Confirm Send
|
|
</label>
|
|
</div>
|
|
<div class="compose-composer-capsule">
|
|
<textarea
|
|
id="{{ panel_id }}-textarea"
|
|
class="compose-textarea"
|
|
name="text"
|
|
rows="1"
|
|
placeholder="Type a message. Enter to send, Shift+Enter for newline."></textarea>
|
|
<button class="button is-link is-light compose-send-btn" type="submit" disabled>
|
|
<span class="icon is-small"><i class="{{ manual_icon_class }}"></i></span>
|
|
<span>Send</span>
|
|
</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
|
|
<style>
|
|
#{{ panel_id }}.compose-shell {
|
|
position: relative;
|
|
border: 1px solid rgba(0, 0, 0, 0.16);
|
|
border-radius: 8px;
|
|
box-shadow: none;
|
|
padding: 0.7rem;
|
|
background: #fff;
|
|
}
|
|
#{{ panel_id }} .compose-thread {
|
|
margin-top: 0.55rem;
|
|
margin-bottom: 0.55rem;
|
|
min-height: 16rem;
|
|
max-height: 62vh;
|
|
overflow-y: auto;
|
|
border: 1px solid rgba(0, 0, 0, 0.12);
|
|
border-radius: 8px;
|
|
padding: 0.65rem;
|
|
background: linear-gradient(180deg, rgba(248, 250, 252, 0.7), rgba(255, 255, 255, 0.98));
|
|
}
|
|
#{{ panel_id }} .compose-row {
|
|
display: flex;
|
|
margin-bottom: 0.5rem;
|
|
}
|
|
#{{ panel_id }} .compose-row.is-in {
|
|
justify-content: flex-start;
|
|
}
|
|
#{{ panel_id }} .compose-row.is-out {
|
|
justify-content: flex-end;
|
|
}
|
|
#{{ panel_id }} .compose-bubble {
|
|
max-width: min(85%, 46rem);
|
|
border-radius: 8px;
|
|
border: 1px solid rgba(0, 0, 0, 0.13);
|
|
padding: 0.52rem 0.62rem;
|
|
box-shadow: none;
|
|
}
|
|
#{{ panel_id }} .compose-bubble.is-in {
|
|
background: rgba(255, 255, 255, 0.96);
|
|
}
|
|
#{{ panel_id }} .compose-bubble.is-out {
|
|
background: #eef6ff;
|
|
}
|
|
#{{ panel_id }} .compose-media {
|
|
margin: 0 0 0.28rem 0;
|
|
}
|
|
#{{ panel_id }} .compose-image {
|
|
display: block;
|
|
max-width: min(100%, 22rem);
|
|
max-height: 18rem;
|
|
border-radius: 8px;
|
|
border: 1px solid rgba(0, 0, 0, 0.14);
|
|
object-fit: cover;
|
|
background: #f8f8f8;
|
|
}
|
|
#{{ panel_id }} .compose-body {
|
|
margin: 0 0 0.2rem 0;
|
|
white-space: pre-wrap;
|
|
word-break: break-word;
|
|
}
|
|
#{{ panel_id }} .compose-msg-meta,
|
|
#{{ panel_id }} .compose-meta-line {
|
|
color: #616161;
|
|
font-size: 0.72rem;
|
|
}
|
|
#{{ panel_id }} .compose-msg-meta {
|
|
margin: 0;
|
|
}
|
|
#{{ panel_id }} .compose-empty {
|
|
margin: 0;
|
|
color: #6f6f6f;
|
|
font-size: 0.78rem;
|
|
}
|
|
#{{ panel_id }} .compose-typing {
|
|
margin: 0 0 0.5rem 0.2rem;
|
|
font-size: 0.78rem;
|
|
color: #4e6381;
|
|
min-height: 1rem;
|
|
}
|
|
#{{ panel_id }} .compose-typing.is-hidden {
|
|
visibility: hidden;
|
|
}
|
|
#{{ panel_id }} .compose-composer-capsule {
|
|
display: flex;
|
|
align-items: flex-end;
|
|
gap: 0.45rem;
|
|
border: 1px solid rgba(0, 0, 0, 0.16);
|
|
border-radius: 8px;
|
|
background: #fff;
|
|
padding: 0.35rem;
|
|
}
|
|
#{{ panel_id }} .compose-textarea {
|
|
flex: 1 1 auto;
|
|
min-height: 2.45rem;
|
|
max-height: 8rem;
|
|
resize: none;
|
|
border: none;
|
|
box-shadow: none;
|
|
outline: none;
|
|
background: transparent;
|
|
line-height: 1.35;
|
|
font-size: 0.98rem;
|
|
padding: 0.45rem 0.5rem;
|
|
}
|
|
#{{ panel_id }} .compose-send-btn {
|
|
height: 2.45rem;
|
|
border-radius: 8px;
|
|
margin: 0;
|
|
}
|
|
#{{ panel_id }} .compose-send-btn[disabled] {
|
|
opacity: 0.55;
|
|
}
|
|
#{{ panel_id }} .compose-send-safety {
|
|
display: flex;
|
|
gap: 0.85rem;
|
|
flex-wrap: wrap;
|
|
margin-bottom: 0.45rem;
|
|
color: #505050;
|
|
}
|
|
#{{ panel_id }} .compose-status {
|
|
margin-top: 0.55rem;
|
|
min-height: 1.1rem;
|
|
}
|
|
#{{ panel_id }} .compose-status-line {
|
|
margin: 0;
|
|
font-size: 0.76rem;
|
|
color: #5f6a7a;
|
|
}
|
|
#{{ panel_id }} .compose-status-line.is-warning,
|
|
#{{ panel_id }} .compose-status-line.is-danger {
|
|
color: #c0392b;
|
|
}
|
|
#{{ panel_id }} .compose-status-line.is-success {
|
|
color: #2f855a;
|
|
}
|
|
#{{ panel_id }} .compose-ai-popover {
|
|
position: absolute;
|
|
top: 4.2rem;
|
|
right: 0.7rem;
|
|
width: min(34rem, calc(100% - 1.4rem));
|
|
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 {
|
|
display: none;
|
|
}
|
|
#{{ panel_id }} .compose-ai-card {
|
|
display: none;
|
|
border: 1px solid rgba(0, 0, 0, 0.16);
|
|
border-radius: 8px;
|
|
background: #fff;
|
|
padding: 0.65rem;
|
|
box-shadow: 0 12px 24px rgba(0, 0, 0, 0.08);
|
|
}
|
|
#{{ panel_id }} .compose-ai-card.is-active {
|
|
display: block;
|
|
animation: composeFadeIn 180ms ease-out;
|
|
}
|
|
#{{ panel_id }} .compose-ai-title {
|
|
font-weight: 600;
|
|
margin-bottom: 0.45rem;
|
|
}
|
|
#{{ panel_id }} .compose-ai-loading.is-hidden {
|
|
display: none;
|
|
}
|
|
#{{ panel_id }} .compose-ai-skel {
|
|
height: 0.7rem;
|
|
border-radius: 999px;
|
|
margin-bottom: 0.4rem;
|
|
background: linear-gradient(90deg, rgba(233, 236, 239, 0.8), rgba(210, 214, 218, 0.95), rgba(233, 236, 239, 0.8));
|
|
background-size: 200% 100%;
|
|
animation: composePulse 1s ease-in-out infinite;
|
|
}
|
|
#{{ panel_id }} .compose-ai-skel:last-child {
|
|
margin-bottom: 0;
|
|
}
|
|
#{{ panel_id }} .compose-ai-content {
|
|
white-space: pre-wrap;
|
|
}
|
|
#{{ panel_id }} .compose-draft-option {
|
|
width: 100%;
|
|
text-align: left;
|
|
margin-bottom: 0.45rem;
|
|
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 {
|
|
margin-bottom: 0;
|
|
}
|
|
#{{ panel_id }} .buttons {
|
|
overflow: visible;
|
|
}
|
|
#{{ panel_id }} .js-ai-trigger {
|
|
position: relative;
|
|
overflow: visible;
|
|
}
|
|
#{{ panel_id }} .js-ai-trigger.is-expanded {
|
|
background: #eef6ff;
|
|
border-color: #8bb2e6;
|
|
}
|
|
#{{ panel_id }} .js-ai-trigger.is-expanded::after {
|
|
content: "";
|
|
position: absolute;
|
|
left: 50%;
|
|
bottom: -0.34rem;
|
|
width: 0;
|
|
height: 0;
|
|
transform: translateX(-50%);
|
|
border-left: 6px solid transparent;
|
|
border-right: 6px solid transparent;
|
|
border-top: 6px solid #8bb2e6;
|
|
pointer-events: none;
|
|
}
|
|
#{{ panel_id }} .compose-image-fallback.is-hidden {
|
|
display: none;
|
|
}
|
|
#{{ panel_id }}.is-send-success .compose-composer-capsule {
|
|
animation: composeSendFlash 360ms ease-out;
|
|
}
|
|
#{{ panel_id }}.is-send-fail .compose-composer-capsule {
|
|
animation: composeSendShake 330ms ease-out;
|
|
border-color: rgba(192, 57, 43, 0.7);
|
|
}
|
|
#{{ panel_id }} .compose-ai-safety {
|
|
margin-top: 0.55rem;
|
|
display: flex;
|
|
gap: 0.65rem;
|
|
flex-wrap: wrap;
|
|
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 {
|
|
0% { background-position: 100% 0; }
|
|
100% { background-position: 0 0; }
|
|
}
|
|
@keyframes composeFadeIn {
|
|
from { opacity: 0; transform: translateY(4px); }
|
|
to { opacity: 1; transform: translateY(0); }
|
|
}
|
|
@keyframes composeSendFlash {
|
|
0% { box-shadow: 0 0 0 0 rgba(60, 132, 218, 0); }
|
|
25% { box-shadow: 0 0 0 3px rgba(60, 132, 218, 0.25); }
|
|
100% { box-shadow: 0 0 0 0 rgba(60, 132, 218, 0); }
|
|
}
|
|
@keyframes composeSendShake {
|
|
0%, 100% { transform: translateX(0); }
|
|
25% { transform: translateX(-2px); }
|
|
50% { transform: translateX(2px); }
|
|
75% { transform: translateX(-1px); }
|
|
}
|
|
@media (max-width: 768px) {
|
|
#{{ panel_id }} .compose-thread {
|
|
max-height: 52vh;
|
|
}
|
|
#{{ panel_id }} .compose-send-btn span:last-child {
|
|
display: none;
|
|
}
|
|
#{{ panel_id }} .compose-ai-popover {
|
|
left: 0.7rem;
|
|
right: 0.7rem;
|
|
width: auto;
|
|
}
|
|
}
|
|
</style>
|
|
|
|
<script>
|
|
(function () {
|
|
const panelId = "{{ panel_id }}";
|
|
const panel = document.getElementById(panelId);
|
|
if (!panel) {
|
|
return;
|
|
}
|
|
const thread = document.getElementById(panelId + "-thread");
|
|
const form = document.getElementById(panelId + "-form");
|
|
const textarea = document.getElementById(panelId + "-textarea");
|
|
if (!thread || !form || !textarea) {
|
|
return;
|
|
}
|
|
|
|
const statusBox = document.getElementById(panelId + "-status");
|
|
const typingNode = document.getElementById(panelId + "-typing");
|
|
const popover = document.getElementById(panelId + "-popover");
|
|
const popoverBackdrop = document.getElementById(panelId + "-popover-backdrop");
|
|
const csrfToken = "{{ csrf_token }}";
|
|
|
|
window.giaComposePanels = window.giaComposePanels || {};
|
|
const previousState = window.giaComposePanels[panelId];
|
|
if (previousState && previousState.timer) {
|
|
clearInterval(previousState.timer);
|
|
}
|
|
if (previousState && previousState.eventHandler) {
|
|
document.body.removeEventListener("composeMessageSent", previousState.eventHandler);
|
|
}
|
|
if (previousState && previousState.docClickHandler) {
|
|
document.removeEventListener("mousedown", previousState.docClickHandler);
|
|
}
|
|
const panelState = {
|
|
timer: null,
|
|
polling: false,
|
|
socket: null,
|
|
websocketReady: false,
|
|
activePanel: null,
|
|
engageToken: ""
|
|
};
|
|
window.giaComposePanels[panelId] = panelState;
|
|
const triggerButtons = Array.from(panel.querySelectorAll(".js-ai-trigger"));
|
|
|
|
const toInt = function (value) {
|
|
const parsed = parseInt(value || "0", 10);
|
|
return Number.isFinite(parsed) ? parsed : 0;
|
|
};
|
|
|
|
let lastTs = toInt(thread.dataset.lastTs);
|
|
|
|
const autosize = function () {
|
|
textarea.style.height = "auto";
|
|
const targetHeight = Math.min(Math.max(textarea.scrollHeight, 40), 128);
|
|
textarea.style.height = targetHeight + "px";
|
|
};
|
|
textarea.addEventListener("input", autosize);
|
|
autosize();
|
|
|
|
const nearBottom = function () {
|
|
return thread.scrollHeight - thread.scrollTop - thread.clientHeight < 100;
|
|
};
|
|
|
|
const scrollToBottom = function (force) {
|
|
if (force || nearBottom()) {
|
|
thread.scrollTop = thread.scrollHeight;
|
|
}
|
|
};
|
|
|
|
const wireImageFallbacks = function (rootNode) {
|
|
const scope = rootNode || thread;
|
|
if (!scope) {
|
|
return;
|
|
}
|
|
scope.querySelectorAll(".compose-bubble").forEach(function (bubble) {
|
|
const fallback = bubble.querySelector(".compose-image-fallback");
|
|
const refresh = function () {
|
|
if (!fallback) {
|
|
return;
|
|
}
|
|
const remaining = bubble.querySelectorAll(".compose-image").length;
|
|
fallback.classList.toggle("is-hidden", remaining > 0);
|
|
};
|
|
bubble.querySelectorAll(".compose-image").forEach(function (img) {
|
|
if (img.dataset.fallbackBound === "1") {
|
|
return;
|
|
}
|
|
img.dataset.fallbackBound = "1";
|
|
img.addEventListener("error", function () {
|
|
const figure = img.closest(".compose-media");
|
|
if (figure) {
|
|
figure.remove();
|
|
}
|
|
refresh();
|
|
});
|
|
img.addEventListener("load", function () {
|
|
if (fallback) {
|
|
fallback.classList.add("is-hidden");
|
|
}
|
|
});
|
|
});
|
|
refresh();
|
|
});
|
|
};
|
|
|
|
const appendBubble = function (msg) {
|
|
const row = document.createElement("div");
|
|
const outgoing = !!msg.outgoing;
|
|
row.className = "compose-row " + (outgoing ? "is-out" : "is-in");
|
|
row.dataset.ts = String(msg.ts || 0);
|
|
|
|
const bubble = document.createElement("article");
|
|
bubble.className = "compose-bubble " + (outgoing ? "is-out" : "is-in");
|
|
|
|
const imageCandidates = Array.isArray(msg.image_urls) && msg.image_urls.length
|
|
? msg.image_urls
|
|
: (msg.image_url ? [msg.image_url] : []);
|
|
imageCandidates.forEach(function (candidateUrl) {
|
|
const figure = document.createElement("figure");
|
|
figure.className = "compose-media";
|
|
const img = document.createElement("img");
|
|
img.className = "compose-image";
|
|
img.src = String(candidateUrl);
|
|
img.alt = "Attachment";
|
|
img.loading = "lazy";
|
|
img.decoding = "async";
|
|
figure.appendChild(img);
|
|
bubble.appendChild(figure);
|
|
});
|
|
|
|
if (!msg.hide_text) {
|
|
const body = document.createElement("p");
|
|
body.className = "compose-body";
|
|
body.textContent = String(
|
|
msg.display_text ||
|
|
msg.text ||
|
|
(msg.image_url ? "" : "(no text)")
|
|
);
|
|
bubble.appendChild(body);
|
|
} else {
|
|
const fallback = document.createElement("p");
|
|
fallback.className = "compose-body compose-image-fallback is-hidden";
|
|
fallback.textContent = "(no text)";
|
|
bubble.appendChild(fallback);
|
|
}
|
|
|
|
const meta = document.createElement("p");
|
|
meta.className = "compose-msg-meta";
|
|
let metaText = String(msg.display_ts || msg.ts || "");
|
|
if (msg.author) {
|
|
metaText += " · " + String(msg.author);
|
|
}
|
|
meta.textContent = metaText;
|
|
bubble.appendChild(meta);
|
|
|
|
row.appendChild(bubble);
|
|
const empty = thread.querySelector(".compose-empty");
|
|
if (empty) {
|
|
empty.remove();
|
|
}
|
|
thread.appendChild(row);
|
|
wireImageFallbacks(row);
|
|
};
|
|
|
|
const appendMessages = function (messages, forceScroll) {
|
|
const shouldStick = nearBottom() || forceScroll;
|
|
(messages || []).forEach(function (msg) {
|
|
appendBubble(msg);
|
|
lastTs = Math.max(lastTs, toInt(msg.ts));
|
|
});
|
|
thread.dataset.lastTs = String(lastTs);
|
|
if ((messages || []).length > 0) {
|
|
scrollToBottom(shouldStick);
|
|
}
|
|
};
|
|
|
|
const applyTyping = function (typingPayload) {
|
|
if (!typingNode || !typingPayload || typeof typingPayload !== "object") {
|
|
return;
|
|
}
|
|
const isTyping = !!typingPayload.typing;
|
|
if (!isTyping) {
|
|
typingNode.classList.add("is-hidden");
|
|
return;
|
|
}
|
|
const displayName = String(typingPayload.display_name || "").trim();
|
|
typingNode.textContent = (displayName || "Contact") + " is typing...";
|
|
typingNode.classList.remove("is-hidden");
|
|
};
|
|
|
|
const poll = async function (forceScroll) {
|
|
if (panelState.polling || panelState.websocketReady) {
|
|
return;
|
|
}
|
|
panelState.polling = true;
|
|
try {
|
|
const params = new URLSearchParams();
|
|
params.set("service", thread.dataset.service || "");
|
|
params.set("identifier", thread.dataset.identifier || "");
|
|
if (thread.dataset.person) {
|
|
params.set("person", thread.dataset.person);
|
|
}
|
|
params.set("limit", thread.dataset.limit || "60");
|
|
params.set("after_ts", String(lastTs));
|
|
const response = await fetch(thread.dataset.pollUrl + "?" + params.toString(), {
|
|
method: "GET",
|
|
credentials: "same-origin",
|
|
headers: { Accept: "application/json" }
|
|
});
|
|
if (!response.ok) {
|
|
return;
|
|
}
|
|
const payload = await response.json();
|
|
appendMessages(payload.messages || [], forceScroll);
|
|
if (payload.typing) {
|
|
applyTyping(payload.typing);
|
|
}
|
|
if (payload.last_ts !== undefined && payload.last_ts !== null) {
|
|
lastTs = Math.max(lastTs, toInt(payload.last_ts));
|
|
thread.dataset.lastTs = String(lastTs);
|
|
}
|
|
} catch (err) {
|
|
console.debug("compose poll error", err);
|
|
} finally {
|
|
panelState.polling = false;
|
|
}
|
|
};
|
|
|
|
const setupWebSocket = function () {
|
|
const wsPath = thread.dataset.wsUrl || "";
|
|
if (!wsPath || !window.WebSocket) {
|
|
return;
|
|
}
|
|
const protocol = window.location.protocol === "https:" ? "wss://" : "ws://";
|
|
const socketUrl = protocol + window.location.host + wsPath;
|
|
try {
|
|
const socket = new WebSocket(socketUrl);
|
|
panelState.socket = socket;
|
|
socket.onopen = function () {
|
|
panelState.websocketReady = true;
|
|
try {
|
|
socket.send(JSON.stringify({ kind: "sync", last_ts: lastTs }));
|
|
} catch (err) {
|
|
// Ignore.
|
|
}
|
|
};
|
|
socket.onmessage = function (event) {
|
|
try {
|
|
const payload = JSON.parse(event.data || "{}");
|
|
appendMessages(payload.messages || [], false);
|
|
if (payload.typing) {
|
|
applyTyping(payload.typing);
|
|
}
|
|
if (payload.last_ts !== undefined && payload.last_ts !== null) {
|
|
lastTs = Math.max(lastTs, toInt(payload.last_ts));
|
|
thread.dataset.lastTs = String(lastTs);
|
|
}
|
|
} catch (err) {
|
|
console.debug("compose websocket payload error", err);
|
|
}
|
|
};
|
|
socket.onclose = function () {
|
|
panelState.websocketReady = false;
|
|
};
|
|
socket.onerror = function () {
|
|
panelState.websocketReady = false;
|
|
};
|
|
} catch (err) {
|
|
panelState.websocketReady = false;
|
|
}
|
|
};
|
|
|
|
const manualConfirm = form.querySelector(".manual-confirm");
|
|
const armInput = form.querySelector("input[name='failsafe_arm']");
|
|
const confirmInput = form.querySelector("input[name='failsafe_confirm']");
|
|
const sendButton = form.querySelector(".compose-send-btn");
|
|
const updateManualSafety = function () {
|
|
const confirm = !!(manualConfirm && manualConfirm.checked);
|
|
if (armInput) {
|
|
armInput.value = confirm ? "1" : "0";
|
|
}
|
|
if (confirmInput) {
|
|
confirmInput.value = confirm ? "1" : "0";
|
|
}
|
|
if (sendButton) {
|
|
sendButton.disabled = !confirm;
|
|
}
|
|
};
|
|
if (manualConfirm) {
|
|
manualConfirm.addEventListener("change", updateManualSafety);
|
|
}
|
|
updateManualSafety();
|
|
try {
|
|
const initialTyping = JSON.parse("{{ typing_state_json|escapejs }}");
|
|
applyTyping(initialTyping);
|
|
} catch (err) {
|
|
// Ignore invalid initial typing state payload.
|
|
}
|
|
|
|
const setStatus = function (message, level) {
|
|
if (!statusBox) {
|
|
return;
|
|
}
|
|
if (!message) {
|
|
statusBox.innerHTML = "";
|
|
return;
|
|
}
|
|
const row = document.createElement("p");
|
|
row.className = "compose-status-line is-" + (level || "info");
|
|
row.textContent = String(message);
|
|
statusBox.innerHTML = "";
|
|
statusBox.appendChild(row);
|
|
};
|
|
|
|
const flashCompose = function (className) {
|
|
panel.classList.remove("is-send-success", "is-send-fail");
|
|
void panel.offsetWidth;
|
|
panel.classList.add(className);
|
|
window.setTimeout(function () {
|
|
panel.classList.remove(className);
|
|
}, 420);
|
|
};
|
|
|
|
const setActiveTrigger = function (kind) {
|
|
triggerButtons.forEach(function (button) {
|
|
button.classList.toggle("is-expanded", !!kind && button.dataset.kind === kind);
|
|
});
|
|
};
|
|
|
|
const hideAllCards = function () {
|
|
if (!popover) {
|
|
return;
|
|
}
|
|
if (popoverBackdrop) {
|
|
popoverBackdrop.classList.add("is-hidden");
|
|
}
|
|
popover.classList.add("is-hidden");
|
|
popover.querySelectorAll(".compose-ai-card").forEach(function (card) {
|
|
card.classList.remove("is-active");
|
|
});
|
|
panelState.activePanel = null;
|
|
setActiveTrigger(null);
|
|
};
|
|
|
|
const showCard = function (kind) {
|
|
if (!popover) {
|
|
return null;
|
|
}
|
|
if (popoverBackdrop) {
|
|
popoverBackdrop.classList.remove("is-hidden");
|
|
}
|
|
popover.classList.remove("is-hidden");
|
|
let active = null;
|
|
popover.querySelectorAll(".compose-ai-card").forEach(function (card) {
|
|
const isActive = card.dataset.kind === kind;
|
|
card.classList.toggle("is-active", isActive);
|
|
if (isActive) {
|
|
active = card;
|
|
}
|
|
});
|
|
panelState.activePanel = kind;
|
|
setActiveTrigger(kind);
|
|
return active;
|
|
};
|
|
|
|
const queryParams = function () {
|
|
const params = new URLSearchParams();
|
|
params.set("service", thread.dataset.service || "");
|
|
params.set("identifier", thread.dataset.identifier || "");
|
|
if (thread.dataset.person) {
|
|
params.set("person", thread.dataset.person);
|
|
}
|
|
params.set("limit", thread.dataset.limit || "60");
|
|
return params;
|
|
};
|
|
|
|
const setCardLoading = function (card, loading) {
|
|
const loadingNode = card.querySelector(".compose-ai-loading");
|
|
const contentNode = card.querySelector(".compose-ai-content");
|
|
if (loadingNode) {
|
|
loadingNode.classList.toggle("is-hidden", !loading);
|
|
}
|
|
if (contentNode && loading) {
|
|
contentNode.innerHTML = "";
|
|
}
|
|
};
|
|
|
|
const openEngage = function (sourceRef) {
|
|
const engageCard = showCard("engage");
|
|
if (!engageCard) {
|
|
return;
|
|
}
|
|
if (!engageCard.dataset.bound) {
|
|
bindEngageControls(engageCard);
|
|
engageCard.dataset.bound = "1";
|
|
}
|
|
loadEngage(engageCard, sourceRef || "auto");
|
|
};
|
|
|
|
const loadDrafts = async function () {
|
|
const card = showCard("drafts");
|
|
if (!card) {
|
|
return;
|
|
}
|
|
setCardLoading(card, true);
|
|
try {
|
|
const response = await fetch(thread.dataset.draftsUrl + "?" + 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 drafts.";
|
|
return;
|
|
}
|
|
const drafts = Array.isArray(payload.drafts) ? payload.drafts : [];
|
|
const container = card.querySelector(".compose-ai-content");
|
|
container.innerHTML = "";
|
|
const engageButton = document.createElement("button");
|
|
engageButton.type = "button";
|
|
engageButton.className = "button is-link is-light compose-draft-option";
|
|
const engageStrong = document.createElement("strong");
|
|
engageStrong.textContent = "Custom Engage: ";
|
|
const engageText = document.createElement("span");
|
|
engageText.textContent = "Choose a source or write your own engagement text.";
|
|
engageButton.appendChild(engageStrong);
|
|
engageButton.appendChild(engageText);
|
|
engageButton.addEventListener("click", function () {
|
|
openEngage("custom");
|
|
});
|
|
container.appendChild(engageButton);
|
|
drafts.forEach(function (item) {
|
|
const button = document.createElement("button");
|
|
button.type = "button";
|
|
button.className = "button is-light compose-draft-option";
|
|
const strong = document.createElement("strong");
|
|
strong.textContent = String(item.label || "Option") + ": ";
|
|
const span = document.createElement("span");
|
|
span.textContent = String(item.text || "");
|
|
button.appendChild(strong);
|
|
button.appendChild(span);
|
|
button.addEventListener("click", function () {
|
|
textarea.value = String(item.text || "");
|
|
autosize();
|
|
textarea.focus();
|
|
hideAllCards();
|
|
});
|
|
container.appendChild(button);
|
|
});
|
|
} catch (err) {
|
|
setCardLoading(card, false);
|
|
card.querySelector(".compose-ai-content").textContent = "Failed to load drafts.";
|
|
}
|
|
};
|
|
|
|
const loadSummary = async function () {
|
|
const card = showCard("summary");
|
|
if (!card) {
|
|
return;
|
|
}
|
|
setCardLoading(card, true);
|
|
try {
|
|
const response = await fetch(thread.dataset.summaryUrl + "?" + 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 summary.";
|
|
return;
|
|
}
|
|
card.querySelector(".compose-ai-content").textContent = String(payload.summary || "");
|
|
} catch (err) {
|
|
setCardLoading(card, false);
|
|
card.querySelector(".compose-ai-content").textContent = "Failed to load summary.";
|
|
}
|
|
};
|
|
|
|
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 confirm = card.querySelector(".engage-confirm");
|
|
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 () {
|
|
send.disabled = !(confirm.checked && panelState.engageToken);
|
|
};
|
|
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 () {
|
|
if (!panelState.engageToken) {
|
|
return;
|
|
}
|
|
const formData = new URLSearchParams();
|
|
formData.set("service", thread.dataset.service || "");
|
|
formData.set("identifier", thread.dataset.identifier || "");
|
|
if (thread.dataset.person) {
|
|
formData.set("person", thread.dataset.person);
|
|
}
|
|
formData.set("engage_token", panelState.engageToken);
|
|
formData.set("failsafe_arm", confirm.checked ? "1" : "0");
|
|
formData.set("failsafe_confirm", confirm.checked ? "1" : "0");
|
|
try {
|
|
const response = await fetch(thread.dataset.engageSendUrl, {
|
|
method: "POST",
|
|
credentials: "same-origin",
|
|
headers: {
|
|
"X-CSRFToken": csrfToken,
|
|
"Content-Type": "application/x-www-form-urlencoded",
|
|
Accept: "application/json"
|
|
},
|
|
body: formData.toString()
|
|
});
|
|
const payload = await response.json();
|
|
if (!payload.ok) {
|
|
flashCompose("is-send-fail");
|
|
setStatus(payload.error || "Engage send failed.", "danger");
|
|
return;
|
|
}
|
|
flashCompose("is-send-success");
|
|
setStatus("", "success");
|
|
hideAllCards();
|
|
poll(true);
|
|
} catch (err) {
|
|
flashCompose("is-send-fail");
|
|
setStatus("Engage send failed.", "danger");
|
|
}
|
|
});
|
|
};
|
|
|
|
panel.querySelectorAll(".js-ai-trigger").forEach(function (button) {
|
|
button.addEventListener("click", function () {
|
|
const kind = button.dataset.kind;
|
|
if (panelState.activePanel === kind) {
|
|
hideAllCards();
|
|
return;
|
|
}
|
|
if (kind === "drafts") {
|
|
loadDrafts();
|
|
} else if (kind === "summary") {
|
|
loadSummary();
|
|
} else if (kind === "engage") {
|
|
openEngage("auto");
|
|
}
|
|
});
|
|
});
|
|
|
|
panelState.docClickHandler = function (event) {
|
|
if (!panel.contains(event.target)) {
|
|
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);
|
|
|
|
textarea.addEventListener("keydown", function (event) {
|
|
if (event.key === "Enter" && !event.shiftKey) {
|
|
event.preventDefault();
|
|
if (sendButton && sendButton.disabled) {
|
|
setStatus("Enable send confirmation before sending.", "warning");
|
|
return;
|
|
}
|
|
form.requestSubmit();
|
|
}
|
|
});
|
|
|
|
form.addEventListener("htmx:afterRequest", function () {
|
|
textarea.focus();
|
|
});
|
|
|
|
panelState.eventHandler = function () {
|
|
poll(true);
|
|
};
|
|
document.body.addEventListener("composeMessageSent", panelState.eventHandler);
|
|
|
|
panelState.sendResultHandler = function (event) {
|
|
const detail = (event && event.detail) || {};
|
|
const ok = !!detail.ok;
|
|
if (ok) {
|
|
flashCompose("is-send-success");
|
|
setStatus("", "success");
|
|
textarea.value = "";
|
|
autosize();
|
|
poll(true);
|
|
} else {
|
|
flashCompose("is-send-fail");
|
|
if (detail.message) {
|
|
setStatus(detail.message, detail.level || "danger");
|
|
}
|
|
}
|
|
textarea.focus();
|
|
};
|
|
document.body.addEventListener("composeSendResult", panelState.sendResultHandler);
|
|
|
|
wireImageFallbacks(thread);
|
|
scrollToBottom(true);
|
|
setupWebSocket();
|
|
panelState.timer = setInterval(function () {
|
|
if (!document.getElementById(panelId)) {
|
|
clearInterval(panelState.timer);
|
|
document.body.removeEventListener("composeMessageSent", panelState.eventHandler);
|
|
document.body.removeEventListener("composeSendResult", panelState.sendResultHandler);
|
|
document.removeEventListener("mousedown", panelState.docClickHandler);
|
|
if (panelState.socket) {
|
|
try {
|
|
panelState.socket.close();
|
|
} catch (err) {
|
|
// Ignore.
|
|
}
|
|
}
|
|
delete window.giaComposePanels[panelId];
|
|
return;
|
|
}
|
|
poll(false);
|
|
}, 4000);
|
|
})();
|
|
</script>
|