forked from SWE-agent/SWE-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileViewer.js
133 lines (117 loc) · 5.31 KB
/
fileViewer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
let currentFileName = null; // Store the current file name
let trajectoryDirectory = ''; // Global variable to store the directory
let timeoutIds = []; // Store timeout IDs for pending operations
function getBaseUrl() {
const protocol = window.location.protocol;
const host = window.location.hostname;
const port = window.location.port;
// Use the default port if the port number is empty (for standard HTTP/HTTPS)
const defaultPort = (protocol === 'http:' && !port) ? '80' : (protocol === 'https:' && !port) ? '443' : port;
return `${protocol}//${host}:${defaultPort}`;
}
function fetchFiles() {
const baseUrl = getBaseUrl();
fetch(`${baseUrl}/files`)
.then(response => response.json())
.then(files => {
const fileList = document.getElementById('fileList');
fileList.innerHTML = '';
files.forEach(file => {
const fileElement = document.createElement('li');
fileElement.textContent = file;
fileElement.onclick = () => viewFile(file.split(' ')[0]);
fileList.appendChild(fileElement);
});
});
}
function viewFile(fileName) {
// Clear any pending message loading from previous files
timeoutIds.forEach(timeoutId => clearTimeout(timeoutId));
timeoutIds = []; // Reset the list of timeout IDs
const baseUrl = getBaseUrl();
fetch(`${baseUrl}/trajectory/${fileName}`)
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(content => {
const container = document.getElementById('fileContent');
container.innerHTML = ''; // Clear existing content
if (content.history && Array.isArray(content.history)) {
let delay = 200; // Initial delay
const delayIncrement = 50; // Delay between each message, in milliseconds
content.history.forEach((item, index) => {
const timeoutId = setTimeout(() => {
const contentText = item.content ? item.content.replace(/</g, '<').replace(/>/g, '>') : '';
let roleClass = item.agent && item.agent !== "primary" ? "subroutine" : item.role ? item.role.toLowerCase().replaceAll(' ', '-') : 'default';
const elementId = 'historyItem' + index;
const historyItem = document.createElement('div');
historyItem.className = `history-item ${roleClass} fade-in`;
historyItem.id = elementId;
if (contentText.includes("--- DEMONSTRATION ---")) {
item.role = "demo";
}
else if ('is_demo' in item && item.is_demo === true) {
item.role += '[demo]';
}
historyItem.innerHTML = `
<div class="role-bar ${roleClass}">
<strong>
<span>${item.role}</span>
</strong>
</div>
<div class="content-container">
<pre>${contentText}</pre>
</div>
<div class="shadow"></div>
`;
container.appendChild(historyItem);
}, delay);
delay += delayIncrement; // Increment delay for the next message
timeoutIds.push(timeoutId); // Store the timeout ID
});
} else {
container.textContent = 'No history content found.';
}
})
.catch(error => {
console.error('Error fetching file:', error);
document.getElementById('fileContent').textContent = 'Error loading content. ' + error;
});
// Highlight the selected file in the list
document.querySelectorAll('#fileList li').forEach(li => {
li.classList.remove('selected');
if (li.textContent.split(' ')[0] === fileName) {
li.classList.add('selected');
}
});
}
function refreshCurrentFile() {
if (currentFileName) {
const currentScrollPosition = document.documentElement.scrollTop || document.body.scrollTop;
viewFile(currentFileName.split(' ')[0]); // Reload the current file
// Restore the scroll position after the content is loaded
setTimeout(() => {
window.scrollTo(0, currentScrollPosition);
}, 100);
}
}
function fetchDirectoryInfo() {
const baseUrl = getBaseUrl();
fetch(`${baseUrl}/directory_info`)
.then(response => response.json())
.then(data => {
if (data.directory) {
trajectoryDirectory = data.directory; // Store the directory
document.title = `Trajectory Viewer: ${data.directory}`;
document.querySelector('h1').textContent = `Trajectory Viewer: ${data.directory}`;
}
})
.catch(error => console.error('Error fetching directory info:', error));
}
window.onload = function() {
fetchFiles();
fetchDirectoryInfo();
};