'use strict';
window.AssistantScheduleRecovery = {
timeLabel(status) {
return status === 'active'
? '下次触发'
: status === 'inactive'
? '最后触发时间'
: '停止前计划时间';
},
taskStatus(status) {
return (
{
active: '进行中',
inactive: '已结束',
capacity_stopped: '容量已满,已停止',
capacity_stopping: '停止结果待核实',
}[status] || status
);
},
mount(host, { root, api, esc, formatDate, user, isActive, getConversations, onChanged }) {
let signature = '';
let busy = false;
let disposed = false;
let history = [];
function open(row) {
if (busy || disposed || !row.retryable || user.role === 'viewer') return;
const choices = getConversations().filter((c) => !c.archived && c.remainingTurns !== 0);
const dialog = document.createElement('dialog');
dialog.className = 'page-leave-dialog aw-schedule-dialog';
dialog.setAttribute('aria-label', '重试定时消息');
dialog.innerHTML =
'
';
root.append(dialog);
const form = dialog.querySelector('form');
const save = form.querySelector('[type=submit]');
const checkbox = form.elements.consent;
const target = form.elements.target;
target.value = choices.some((c) => c.id === row.conversationId)
? row.conversationId
: choices[0]?.id || '';
checkbox.onchange = () => {
save.disabled = !checkbox.checked || !choices.length || dialog.dataset.locked === 'true';
};
form.querySelector('[data-retry-cancel]').onclick = () => {
if (!busy) dialog.close();
};
dialog.oncancel = (e) => {
if (busy) e.preventDefault();
};
dialog.onclose = () => dialog.remove();
form.onsubmit = async (e) => {
e.preventDefault();
if (busy || !checkbox.checked || !target.value || dialog.dataset.locked === 'true') return;
busy = true;
save.disabled = true;
target.disabled = true;
checkbox.disabled = true;
try {
const result = await api(
'/assistant/conversations/' +
row.conversationId +
'/schedules/deliveries/' +
row.id +
'/retry',
{ confirm: true, targetConversationId: target.value },
{ managedOutcome: true },
);
if (disposed || !isActive()) return;
dialog.close();
await onChanged(result.conversationId);
} catch (error) {
if (dialog.isConnected && !disposed) {
const uncertain = !error.status || error.status >= 500;
dialog.dataset.locked = uncertain || error.status === 409 ? 'true' : '';
form.querySelector('[data-retry-error]').textContent =
error.message + (uncertain ? ' 请刷新调用记录核实,勿重复提交。' : '');
target.disabled = false;
checkbox.disabled = false;
save.disabled = dialog.dataset.locked === 'true';
}
} finally {
busy = false;
}
};
dialog.showModal();
}
return {
update(rows) {
history = rows;
const pending = history.filter(
(h) =>
h.retryable || ['not_started', 'waiting_capacity', 'needs_review'].includes(h.status),
);
const next = JSON.stringify([pending, getConversations(), user.role]);
if (next === signature) return;
signature = next;
host.hidden = !pending.length;
host.innerHTML =
'需处理的定时调用
' +
pending
.map(
(h) =>
'' +
esc(h.title) +
'' +
esc(formatDate(h.scheduledAt)) +
' · ' +
esc(
{
not_started: '尚未调用,可确认后重试',
waiting_capacity: '等待选择其他会话',
needs_review: '已有调用记录,需人工核实',
}[h.status] || h.status,
) +
'
' +
(h.error ? '' + esc(h.error) + '
' : '') +
(h.retryable && user.role !== 'viewer'
? ''
: '') +
'',
)
.join('');
host
.querySelectorAll('[data-retry-delivery]')
.forEach(
(b) => (b.onclick = () => open(history.find((h) => h.id === b.dataset.retryDelivery))),
);
},
dispose() {
disposed = true;
history = [];
},
};
},
};