Initial upload to Gitea
This commit is contained in:
1 parent
78193254ad
commit
8478998cae
1485 files changed
+461025
-42348
No files matched your search
@@ -0,0 +1,102 @@
|
||||
'use strict';
|
||||
|
||||
const serverAgentHistory = (() => {
|
||||
const pages = new WeakMap();
|
||||
const reviewOutcomes = Object.freeze({
|
||||
applying: 'The panel received the approval but has not recorded a result yet. Reopen this chat to check the outcome.',
|
||||
dismissed: 'Action declined. Nothing was sent.', refused: 'The panel refused the action.',
|
||||
unknown: 'The outcome is unknown. Check the server before trying again.', expired: 'The review expired.',
|
||||
stale: 'The file changed after review. Nothing was applied.',
|
||||
});
|
||||
|
||||
function reviewOutcomeMessage(state) {
|
||||
const key = state === 'file_changed_on_disk' ? 'stale' : state;
|
||||
return Object.hasOwn(reviewOutcomes, key) ? reviewOutcomes[key] : '';
|
||||
}
|
||||
|
||||
function isProposalMessage(content) {
|
||||
return content === 'Action prepared.' || content === 'Action prepared. Review it before applying.';
|
||||
}
|
||||
|
||||
function fromPage(data, chatId) {
|
||||
const entries = Array.isArray(data?.messages) ? data.messages : [];
|
||||
const messages = entries.map((entry) => ({
|
||||
role: entry.role === 'assistant' ? 'ai' : entry.role,
|
||||
content: entry.content,
|
||||
thinking_time_ms: entry.thinking_time_ms,
|
||||
thinking_content: entry.thinking_content,
|
||||
model: entry.model,
|
||||
agentActions: entry.role === 'assistant' ? readActions(entry.agent_actions) : [],
|
||||
timestamp: new Date(entry.created_at).getTime(),
|
||||
attachment: entry.image_data ? { isImage: true, data: entry.image_data, name: 'Image' } : null,
|
||||
}));
|
||||
const status = data?.chat?.latest_job_status;
|
||||
const last = messages.at(-1);
|
||||
if ((last?.role === 'user' || (last?.role === 'ai' && Number(data?.chat?.active_job_is_review) === 1)) &&
|
||||
String(entries.at(-1).id) === data.chat?.latest_message_id &&
|
||||
(status === 'failed' || status === 'cancelled')) {
|
||||
last.agentOutcome = { status, error: data.chat.latest_job_error };
|
||||
}
|
||||
if (data?.chat?.review_state) {
|
||||
const reply = entries.findIndex((entry) => entry.role === 'assistant' && String(entry.id) === data.chat.review_message_id);
|
||||
if (reply !== -1 && isProposalMessage(messages[reply].content)) messages[reply].agentReviewState = data.chat.review_state;
|
||||
}
|
||||
pages.set(messages, { chatId, before: data?.nextBeforeId || null });
|
||||
if (data?.chat?.review_job_id && last?.role === 'ai' && String(entries.at(-1).id) === data.chat.review_message_id) {
|
||||
last.agentReview = { chatId, jobId: data.chat.review_job_id, server: data.chat.review_server };
|
||||
}
|
||||
return messages;
|
||||
}
|
||||
|
||||
function clone(messages) {
|
||||
const copy = messages.map((message) => ({ ...message }));
|
||||
if (pages.has(messages)) pages.set(copy, pages.get(messages));
|
||||
return copy;
|
||||
}
|
||||
|
||||
function reviewJobId(messages, chatId, server) {
|
||||
const review = messages.at(-1)?.agentReview;
|
||||
return review && review.chatId === chatId && review.server === server ? review.jobId : null;
|
||||
}
|
||||
|
||||
function readActions(stored) {
|
||||
try {
|
||||
const actions = typeof stored === 'string' ? JSON.parse(stored) : stored;
|
||||
return Array.isArray(actions) ? actions.filter((entry) => typeof entry?.action === 'string' &&
|
||||
typeof entry.status === 'string' && typeof entry.target === 'string') : [];
|
||||
} catch { return []; }
|
||||
}
|
||||
|
||||
function bindOlderButton(button, { messages, chatId, scrollPane, loadPage, isCurrent, applyPage, showError }) {
|
||||
if (!button) return;
|
||||
const page = pages.get(messages);
|
||||
button.hidden = !page?.before || page.chatId !== chatId;
|
||||
button.disabled = false;
|
||||
button.onclick = button.hidden ? null : loadEarlier;
|
||||
|
||||
async function loadEarlier() {
|
||||
if (button.disabled || !isCurrent()) return;
|
||||
button.disabled = true;
|
||||
try {
|
||||
const data = await loadPage(chatId, page.before);
|
||||
if (!isCurrent()) return;
|
||||
if (!Array.isArray(data?.messages)) {
|
||||
showError();
|
||||
return;
|
||||
}
|
||||
const older = fromPage(data, chatId);
|
||||
const combined = older.concat(messages);
|
||||
pages.set(combined, pages.get(older));
|
||||
const { scrollHeight, scrollTop } = scrollPane;
|
||||
applyPage(combined);
|
||||
scrollPane.scrollTop = Math.max(0, scrollTop + scrollPane.scrollHeight - scrollHeight);
|
||||
} finally {
|
||||
if (button.onclick === loadEarlier) button.disabled = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return { fromPage, clone, bindOlderButton, reviewJobId, reviewOutcomeMessage, isProposalMessage, readActions };
|
||||
})();
|
||||
|
||||
if (typeof module === 'object' && module.exports) module.exports = serverAgentHistory;
|
||||
Reference in new issue
Block a user