Address PR4 comments 66-69 with safe delivery retries, capacity stops, preserved effect evidence and stale proposal rejection. Prevent archived queue snapshots from resurrecting cancelled reminders.
164 lines
6.5 KiB
JavaScript
164 lines
6.5 KiB
JavaScript
'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 =
|
|
'<form><h2 class="h3">重试定时消息</h2><p>' +
|
|
esc(row.title) +
|
|
'</p><p>仅重新入队这条尚未建轮的消息,沿用同次投递标识,避免重复调用。</p>' +
|
|
(row.retryNeedsTarget
|
|
? '<p class="text-warning">原会话已满,未来定时任务已停止。本次仅重试未调用消息;周期任务请在新会话重新安排。</p>'
|
|
: '') +
|
|
'<label class="form-label">目标会话<select class="form-select" name="target" aria-label="目标会话" required>' +
|
|
choices
|
|
.map(
|
|
(c) =>
|
|
'<option value="' +
|
|
esc(c.id) +
|
|
'">' +
|
|
esc(c.title) +
|
|
'(剩余 ' +
|
|
esc(c.remainingTurns) +
|
|
' 轮)</option>',
|
|
)
|
|
.join('') +
|
|
'</select></label>' +
|
|
(!choices.length ? '<p>请先新建对话,再回到此处选择目标会话。</p>' : '') +
|
|
'<p class="small text-secondary">将使用目标会话的模型和历史,旧会话附件不会自动迁移,可能产生模型费用。不会自动执行邮件、配置或其他操作。</p><label class="form-check my-3"><input class="form-check-input" name="consent" type="checkbox"><span class="form-check-label">我确认仅重试这条未调用消息</span></label><p role="alert" class="text-danger" data-retry-error></p><div class="btn-list"><button type="button" class="btn" data-retry-cancel>取消</button><button type="submit" class="btn btn-primary" disabled>确认重试</button></div></form>';
|
|
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 =
|
|
'<h3 class="h4">需处理的定时调用</h3>' +
|
|
pending
|
|
.map(
|
|
(h) =>
|
|
'<article class="assistant-action"><strong>' +
|
|
esc(h.title) +
|
|
'</strong><p class="small text-secondary">' +
|
|
esc(formatDate(h.scheduledAt)) +
|
|
' · ' +
|
|
esc(
|
|
{
|
|
not_started: '尚未调用,可确认后重试',
|
|
waiting_capacity: '等待选择其他会话',
|
|
needs_review: '已有调用记录,需人工核实',
|
|
}[h.status] || h.status,
|
|
) +
|
|
'</p>' +
|
|
(h.error ? '<p class="small">' + esc(h.error) + '</p>' : '') +
|
|
(h.retryable && user.role !== 'viewer'
|
|
? '<button type="button" class="btn btn-sm btn-outline-primary" data-retry-delivery="' +
|
|
esc(h.id) +
|
|
'" aria-label="重试定时消息 ' +
|
|
esc(h.title) +
|
|
'">重试未调用消息</button>'
|
|
: '') +
|
|
'</article>',
|
|
)
|
|
.join('');
|
|
host
|
|
.querySelectorAll('[data-retry-delivery]')
|
|
.forEach(
|
|
(b) => (b.onclick = () => open(history.find((h) => h.id === b.dataset.retryDelivery))),
|
|
);
|
|
},
|
|
dispose() {
|
|
disposed = true;
|
|
history = [];
|
|
},
|
|
};
|
|
},
|
|
};
|