dashboard.html 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Switchboard Dashboard</title>
  5. <meta charset="UTF-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <style>
  8. * { box-sizing: border-box; margin: 0; padding: 0; }
  9. body {
  10. font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  11. background: #f5f5f5;
  12. padding: 20px;
  13. }
  14. .container { max-width: 1200px; margin: 0 auto; }
  15. h1 { color: #2563eb; margin-bottom: 20px; }
  16. h2 { color: #1e40af; margin: 30px 0 15px; font-size: 1.5em; }
  17. /* API Key Section */
  18. #api-key-section {
  19. background: white;
  20. padding: 20px;
  21. border-radius: 8px;
  22. margin-bottom: 20px;
  23. box-shadow: 0 1px 3px rgba(0,0,0,0.1);
  24. }
  25. #api-key-section input {
  26. width: 100%;
  27. padding: 10px;
  28. border: 1px solid #ddd;
  29. border-radius: 4px;
  30. font-size: 14px;
  31. }
  32. /* Buttons */
  33. button {
  34. background: #2563eb;
  35. color: white;
  36. border: none;
  37. padding: 10px 20px;
  38. border-radius: 4px;
  39. cursor: pointer;
  40. font-size: 14px;
  41. margin-top: 10px;
  42. }
  43. button:hover { background: #1d4ed8; }
  44. button.danger { background: #dc2626; }
  45. button.danger:hover { background: #b91c1c; }
  46. /* Tabs */
  47. .tabs {
  48. display: flex;
  49. gap: 10px;
  50. margin-bottom: 20px;
  51. }
  52. .tab {
  53. padding: 10px 20px;
  54. background: #e5e7eb;
  55. color: #374151;
  56. border: none;
  57. border-radius: 8px 8px 0 0;
  58. cursor: pointer;
  59. font-weight: 500;
  60. }
  61. .tab:hover { background: #d1d5db; }
  62. .tab.active { background: #2563eb; color: white; }
  63. /* Content */
  64. .content {
  65. background: white;
  66. padding: 20px;
  67. border-radius: 8px;
  68. box-shadow: 0 1px 3px rgba(0,0,0,0.1);
  69. display: none;
  70. }
  71. .content.active { display: block; }
  72. /* Tables */
  73. table {
  74. width: 100%;
  75. border-collapse: collapse;
  76. margin-top: 15px;
  77. }
  78. th, td {
  79. padding: 12px;
  80. text-align: left;
  81. border-bottom: 1px solid #e5e7eb;
  82. }
  83. th { background: #f9fafb; font-weight: 600; }
  84. tr:hover { background: #f9fafb; }
  85. /* Forms */
  86. .form-group {
  87. margin-bottom: 15px;
  88. }
  89. label {
  90. display: block;
  91. margin-bottom: 5px;
  92. font-weight: 500;
  93. }
  94. input[type="text"], input[type="datetime-local"], textarea {
  95. width: 100%;
  96. padding: 8px;
  97. border: 1px solid #ddd;
  98. border-radius: 4px;
  99. }
  100. textarea { min-height: 80px; }
  101. /* Status badges */
  102. .badge {
  103. padding: 4px 8px;
  104. border-radius: 4px;
  105. font-size: 12px;
  106. font-weight: 500;
  107. }
  108. .badge.success { background: #d1fae5; color: #065f46; }
  109. .badge.failed { background: #fee2e2; color: #991b1b; }
  110. .hidden { display: none; }
  111. .error { color: #dc2626; margin-top: 10px; }
  112. .empty { text-align: center; padding: 40px; color: #6b7280; }
  113. /* Loading spinner */
  114. .loading {
  115. text-align: center;
  116. padding: 40px;
  117. color: #6b7280;
  118. }
  119. .spinner {
  120. display: inline-block;
  121. width: 40px;
  122. height: 40px;
  123. border: 4px solid #e5e7eb;
  124. border-top-color: #2563eb;
  125. border-radius: 50%;
  126. animation: spin 1s linear infinite;
  127. }
  128. @keyframes spin {
  129. to { transform: rotate(360deg); }
  130. }
  131. </style>
  132. </head>
  133. <body>
  134. <div class="container">
  135. <h1>🔀 Switchboard Dashboard</h1>
  136. <!-- API Key Input -->
  137. <div id="api-key-section">
  138. <label for="api-key-input">Letta API Key:</label>
  139. <input type="password" id="api-key-input" placeholder="sk-let-...">
  140. <button onclick="saveApiKey()">Save Key</button>
  141. <button onclick="clearApiKey()" class="danger">Clear Key</button>
  142. <div id="api-key-status"></div>
  143. </div>
  144. <div id="main-content" class="hidden">
  145. <!-- Tabs -->
  146. <div class="tabs">
  147. <button class="tab active" onclick="showTab('schedules')">Schedules</button>
  148. <button class="tab" onclick="showTab('create')">Create New</button>
  149. <button class="tab" onclick="showTab('results')">Results</button>
  150. </div>
  151. <!-- Schedules Tab -->
  152. <div id="schedules-tab" class="content active">
  153. <h2>One-Time Schedules</h2>
  154. <table id="onetime-table">
  155. <thead>
  156. <tr>
  157. <th>ID</th>
  158. <th>Agent ID</th>
  159. <th>Execute At</th>
  160. <th>Message</th>
  161. <th>Actions</th>
  162. </tr>
  163. </thead>
  164. <tbody></tbody>
  165. </table>
  166. <h2>Recurring Schedules</h2>
  167. <table id="recurring-table">
  168. <thead>
  169. <tr>
  170. <th>ID</th>
  171. <th>Agent ID</th>
  172. <th>Cron</th>
  173. <th>Message</th>
  174. <th>Last Run</th>
  175. <th>Actions</th>
  176. </tr>
  177. </thead>
  178. <tbody></tbody>
  179. </table>
  180. <button onclick="loadSchedules()" style="margin-top: 20px;">Refresh</button>
  181. </div>
  182. <!-- Create Tab -->
  183. <div id="create-tab" class="content">
  184. <h2>Create Schedule</h2>
  185. <div class="form-group">
  186. <label>Schedule Type:</label>
  187. <select id="schedule-type" onchange="toggleScheduleType()">
  188. <option value="onetime">One-Time</option>
  189. <option value="recurring">Recurring</option>
  190. </select>
  191. </div>
  192. <div class="form-group">
  193. <label for="agent-id">Agent ID:</label>
  194. <input type="text" id="agent-id" placeholder="agent-xxx">
  195. </div>
  196. <div class="form-group">
  197. <label for="message">Message:</label>
  198. <textarea id="message" placeholder="Your message to the agent"></textarea>
  199. </div>
  200. <div id="onetime-fields">
  201. <div class="form-group">
  202. <label for="execute-at">Execute At:</label>
  203. <input type="datetime-local" id="execute-at">
  204. </div>
  205. <div class="form-group">
  206. <label for="timezone">Timezone:</label>
  207. <select id="timezone">
  208. <option value="local" selected>Local Browser Time</option>
  209. <option value="UTC">UTC</option>
  210. <option value="America/New_York">Eastern (US)</option>
  211. <option value="America/Chicago">Central (US)</option>
  212. <option value="America/Denver">Mountain (US)</option>
  213. <option value="America/Los_Angeles">Pacific (US)</option>
  214. <option value="Europe/London">London</option>
  215. <option value="Europe/Paris">Paris</option>
  216. <option value="Asia/Tokyo">Tokyo</option>
  217. </select>
  218. <small style="display:block;margin-top:5px;color:#6b7280;">Time will be converted to UTC for storage</small>
  219. </div>
  220. </div>
  221. <div id="recurring-fields" class="hidden">
  222. <div class="form-group">
  223. <label for="cron">Cron Expression:</label>
  224. <input type="text" id="cron" placeholder="0 9 * * 1-5">
  225. <small style="display:block;margin-top:5px;color:#6b7280;">Examples: "0 9 * * *" (daily at 9am), "*/5 * * * *" (every 5 min)</small>
  226. </div>
  227. </div>
  228. <button onclick="createSchedule()">Create Schedule</button>
  229. <div id="create-error" class="error"></div>
  230. </div>
  231. <!-- Results Tab -->
  232. <div id="results-tab" class="content">
  233. <h2>Execution Results</h2>
  234. <table id="results-table">
  235. <thead>
  236. <tr>
  237. <th>Schedule ID</th>
  238. <th>Type</th>
  239. <th>Status</th>
  240. <th>Agent ID</th>
  241. <th>Message</th>
  242. <th>Run ID / Error</th>
  243. <th>Executed At</th>
  244. </tr>
  245. </thead>
  246. <tbody></tbody>
  247. </table>
  248. <button onclick="loadResults()" style="margin-top: 20px;">Refresh</button>
  249. </div>
  250. </div>
  251. </div>
  252. <script>
  253. const API_BASE = window.location.origin;
  254. let API_KEY = sessionStorage.getItem('letta_api_key') || '';
  255. // Initialize
  256. if (API_KEY) {
  257. document.getElementById('api-key-input').value = API_KEY;
  258. document.getElementById('main-content').classList.remove('hidden');
  259. loadSchedules();
  260. loadResults();
  261. }
  262. function saveApiKey() {
  263. API_KEY = document.getElementById('api-key-input').value.trim();
  264. if (!API_KEY) {
  265. alert('Please enter an API key');
  266. return;
  267. }
  268. sessionStorage.setItem('letta_api_key', API_KEY);
  269. document.getElementById('main-content').classList.remove('hidden');
  270. document.getElementById('api-key-status').innerHTML = '<p style="color:green;margin-top:10px;">✓ API key saved</p>';
  271. loadSchedules();
  272. loadResults();
  273. }
  274. function clearApiKey() {
  275. sessionStorage.removeItem('letta_api_key');
  276. API_KEY = '';
  277. document.getElementById('api-key-input').value = '';
  278. document.getElementById('main-content').classList.add('hidden');
  279. document.getElementById('api-key-status').innerHTML = '<p style="color:red;margin-top:10px;">API key cleared</p>';
  280. }
  281. function showTab(tab) {
  282. document.querySelectorAll('.tab').forEach(t => t.classList.remove('active'));
  283. document.querySelectorAll('.content').forEach(c => c.classList.remove('active'));
  284. event.target.classList.add('active');
  285. document.getElementById(`${tab}-tab`).classList.add('active');
  286. }
  287. function toggleScheduleType() {
  288. const type = document.getElementById('schedule-type').value;
  289. document.getElementById('onetime-fields').classList.toggle('hidden', type !== 'onetime');
  290. document.getElementById('recurring-fields').classList.toggle('hidden', type !== 'recurring');
  291. }
  292. async function loadSchedules() {
  293. const onetimeBody = document.querySelector('#onetime-table tbody');
  294. const recurringBody = document.querySelector('#recurring-table tbody');
  295. // Show loading spinners
  296. onetimeBody.innerHTML = '<tr><td colspan="5" class="loading"><div class="spinner"></div><p>Loading...</p></td></tr>';
  297. recurringBody.innerHTML = '<tr><td colspan="6" class="loading"><div class="spinner"></div><p>Loading...</p></td></tr>';
  298. try {
  299. const [onetime, recurring] = await Promise.all([
  300. fetch(`${API_BASE}/schedules/one-time`, {
  301. headers: { 'Authorization': `Bearer ${API_KEY}` }
  302. }).then(r => r.json()),
  303. fetch(`${API_BASE}/schedules/recurring`, {
  304. headers: { 'Authorization': `Bearer ${API_KEY}` }
  305. }).then(r => r.json())
  306. ]);
  307. // One-time schedules
  308. onetimeBody.innerHTML = onetime.length ? onetime.map(s => `
  309. <tr>
  310. <td>${s.id.substring(0, 8)}...</td>
  311. <td>${s.agent_id}</td>
  312. <td>${new Date(s.execute_at).toLocaleString()}</td>
  313. <td>${truncate(s.message, 50)}</td>
  314. <td><button class="danger" onclick="deleteSchedule('one-time', '${s.id}')">Delete</button></td>
  315. </tr>
  316. `).join('') : '<tr><td colspan="5" class="empty">No one-time schedules</td></tr>';
  317. // Recurring schedules
  318. const recurringBody = document.querySelector('#recurring-table tbody');
  319. recurringBody.innerHTML = recurring.length ? recurring.map(s => `
  320. <tr>
  321. <td>${s.id.substring(0, 8)}...</td>
  322. <td>${s.agent_id}</td>
  323. <td>${s.cron}</td>
  324. <td>${truncate(s.message, 50)}</td>
  325. <td>${s.last_run ? new Date(s.last_run).toLocaleString() : 'Never'}</td>
  326. <td><button class="danger" onclick="deleteSchedule('recurring', '${s.id}')">Delete</button></td>
  327. </tr>
  328. `).join('') : '<tr><td colspan="6" class="empty">No recurring schedules</td></tr>';
  329. } catch (error) {
  330. alert('Failed to load schedules: ' + error.message);
  331. }
  332. }
  333. async function loadResults() {
  334. const resultsBody = document.querySelector('#results-table tbody');
  335. // Show loading spinner
  336. resultsBody.innerHTML = '<tr><td colspan="7" class="loading"><div class="spinner"></div><p>Loading...</p></td></tr>';
  337. try {
  338. const results = await fetch(`${API_BASE}/results`, {
  339. headers: { 'Authorization': `Bearer ${API_KEY}` }
  340. }).then(r => r.json());
  341. resultsBody.innerHTML = results.length ? results.map(r => `
  342. <tr>
  343. <td>${r.schedule_id.substring(0, 8)}...</td>
  344. <td>${r.schedule_type}</td>
  345. <td><span class="badge ${r.status}">${r.status}</span></td>
  346. <td>${r.agent_id}</td>
  347. <td>${truncate(r.message, 30)}</td>
  348. <td>${r.run_id || r.error || 'N/A'}</td>
  349. <td>${new Date(r.executed_at).toLocaleString()}</td>
  350. </tr>
  351. `).join('') : '<tr><td colspan="7" class="empty">No execution results</td></tr>';
  352. } catch (error) {
  353. alert('Failed to load results: ' + error.message);
  354. }
  355. }
  356. async function createSchedule() {
  357. const type = document.getElementById('schedule-type').value;
  358. const agentId = document.getElementById('agent-id').value.trim();
  359. const message = document.getElementById('message').value.trim();
  360. const errorDiv = document.getElementById('create-error');
  361. errorDiv.textContent = '';
  362. if (!agentId || !message) {
  363. errorDiv.textContent = 'Please fill in all fields';
  364. return;
  365. }
  366. try {
  367. const payload = {
  368. agent_id: agentId,
  369. message: message,
  370. role: 'user'
  371. };
  372. if (type === 'onetime') {
  373. const executeAt = document.getElementById('execute-at').value;
  374. const timezone = document.getElementById('timezone').value;
  375. if (!executeAt) {
  376. errorDiv.textContent = 'Please specify execution time';
  377. return;
  378. }
  379. // Convert datetime-local to ISO 8601 with timezone handling
  380. let executeDate;
  381. if (timezone === 'local') {
  382. // Use local browser timezone
  383. executeDate = new Date(executeAt);
  384. } else if (timezone === 'UTC') {
  385. // Treat input as UTC
  386. executeDate = new Date(executeAt + 'Z');
  387. } else {
  388. // For named timezones, we assume the datetime-local is in that timezone
  389. // Convert to UTC by parsing as local and adjusting
  390. executeDate = new Date(executeAt);
  391. // Note: This is a simplification. For accurate timezone conversion,
  392. // we'd need a library, but for now we assume UTC or local
  393. }
  394. payload.execute_at = executeDate.toISOString();
  395. } else {
  396. const cron = document.getElementById('cron').value.trim();
  397. if (!cron) {
  398. errorDiv.textContent = 'Please specify cron expression';
  399. return;
  400. }
  401. payload.cron = cron;
  402. }
  403. const endpoint = type === 'onetime' ? '/schedules/one-time' : '/schedules/recurring';
  404. const response = await fetch(`${API_BASE}${endpoint}`, {
  405. method: 'POST',
  406. headers: {
  407. 'Authorization': `Bearer ${API_KEY}`,
  408. 'Content-Type': 'application/json'
  409. },
  410. body: JSON.stringify(payload)
  411. });
  412. if (!response.ok) {
  413. const error = await response.json();
  414. throw new Error(error.detail || 'Failed to create schedule');
  415. }
  416. alert('Schedule created successfully!');
  417. document.getElementById('agent-id').value = '';
  418. document.getElementById('message').value = '';
  419. document.getElementById('execute-at').value = '';
  420. document.getElementById('cron').value = '';
  421. showTab('schedules');
  422. loadSchedules();
  423. } catch (error) {
  424. errorDiv.textContent = 'Error: ' + error.message;
  425. }
  426. }
  427. async function deleteSchedule(type, id) {
  428. if (!confirm('Are you sure you want to delete this schedule?')) return;
  429. try {
  430. const response = await fetch(`${API_BASE}/schedules/${type}/${id}`, {
  431. method: 'DELETE',
  432. headers: { 'Authorization': `Bearer ${API_KEY}` }
  433. });
  434. if (!response.ok) throw new Error('Failed to delete');
  435. alert('Schedule deleted successfully!');
  436. loadSchedules();
  437. } catch (error) {
  438. alert('Error deleting schedule: ' + error.message);
  439. }
  440. }
  441. function truncate(str, len) {
  442. return str.length > len ? str.substring(0, len) + '...' : str;
  443. }
  444. </script>
  445. </body>
  446. </html>