dashboard.html 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Letta Scheduling 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: #f8f9fa;
  12. color: #1a1a1a;
  13. padding: 24px;
  14. line-height: 1.6;
  15. }
  16. .container { max-width: 1100px; margin: 0 auto; }
  17. /* Header */
  18. .header {
  19. display: flex;
  20. justify-content: space-between;
  21. align-items: center;
  22. margin-bottom: 24px;
  23. flex-wrap: wrap;
  24. gap: 16px;
  25. }
  26. .header h1 {
  27. font-size: 24px;
  28. font-weight: 600;
  29. }
  30. .header-links {
  31. display: flex;
  32. gap: 16px;
  33. }
  34. .header-links a {
  35. color: #666;
  36. text-decoration: none;
  37. font-size: 14px;
  38. }
  39. .header-links a:hover {
  40. color: #1a1a1a;
  41. }
  42. /* Migration Banner */
  43. .migration-banner {
  44. background: #fff3cd;
  45. border: 2px solid #ffc107;
  46. border-radius: 8px;
  47. padding: 16px;
  48. margin-bottom: 24px;
  49. }
  50. .migration-banner h3 {
  51. font-size: 16px;
  52. font-weight: 600;
  53. margin-bottom: 8px;
  54. color: #856404;
  55. }
  56. .migration-banner p {
  57. font-size: 14px;
  58. color: #856404;
  59. margin-bottom: 8px;
  60. }
  61. .migration-banner a {
  62. color: #856404;
  63. font-weight: 600;
  64. }
  65. h2 {
  66. font-size: 16px;
  67. font-weight: 600;
  68. margin: 24px 0 12px 0;
  69. color: #1a1a1a;
  70. }
  71. /* API Key Section */
  72. .card {
  73. background: white;
  74. border: 1px solid #e0e0e0;
  75. border-radius: 8px;
  76. padding: 20px;
  77. margin-bottom: 20px;
  78. }
  79. .card-title {
  80. font-size: 14px;
  81. font-weight: 600;
  82. margin-bottom: 12px;
  83. color: #666;
  84. }
  85. #api-key-section input {
  86. width: 100%;
  87. padding: 10px 12px;
  88. border: 1px solid #e0e0e0;
  89. border-radius: 6px;
  90. font-size: 14px;
  91. margin-bottom: 12px;
  92. }
  93. #api-key-section input:focus {
  94. outline: none;
  95. border-color: #1a1a1a;
  96. }
  97. .btn-group {
  98. display: flex;
  99. gap: 8px;
  100. }
  101. /* Buttons */
  102. button, .btn {
  103. background: #1a1a1a;
  104. color: white;
  105. border: none;
  106. padding: 8px 16px;
  107. border-radius: 6px;
  108. cursor: pointer;
  109. font-size: 14px;
  110. font-weight: 500;
  111. }
  112. button:hover { background: #333; }
  113. button.secondary {
  114. background: white;
  115. color: #1a1a1a;
  116. border: 1px solid #e0e0e0;
  117. }
  118. button.secondary:hover {
  119. background: #f5f5f5;
  120. }
  121. button.danger {
  122. background: white;
  123. color: #dc2626;
  124. border: 1px solid #fecaca;
  125. }
  126. button.danger:hover {
  127. background: #fef2f2;
  128. }
  129. button.sm {
  130. padding: 6px 12px;
  131. font-size: 13px;
  132. }
  133. /* Tabs */
  134. .tabs {
  135. display: flex;
  136. gap: 4px;
  137. border-bottom: 1px solid #e0e0e0;
  138. margin-bottom: 20px;
  139. }
  140. .tab {
  141. padding: 10px 20px;
  142. background: transparent;
  143. color: #666;
  144. border: none;
  145. border-bottom: 2px solid transparent;
  146. cursor: pointer;
  147. font-weight: 500;
  148. font-size: 14px;
  149. border-radius: 0;
  150. margin-bottom: -1px;
  151. }
  152. .tab:hover {
  153. color: #1a1a1a;
  154. background: transparent;
  155. }
  156. .tab.active {
  157. color: #1a1a1a;
  158. border-bottom-color: #1a1a1a;
  159. background: transparent;
  160. }
  161. /* Content */
  162. .content {
  163. display: none;
  164. }
  165. .content.active { display: block; }
  166. /* Tables */
  167. .table-container {
  168. background: white;
  169. border: 1px solid #e0e0e0;
  170. border-radius: 8px;
  171. overflow: hidden;
  172. margin-bottom: 16px;
  173. }
  174. table {
  175. width: 100%;
  176. border-collapse: collapse;
  177. }
  178. th, td {
  179. padding: 12px 16px;
  180. text-align: left;
  181. border-bottom: 1px solid #e0e0e0;
  182. }
  183. th {
  184. background: #f8f9fa;
  185. font-weight: 600;
  186. font-size: 13px;
  187. color: #666;
  188. }
  189. tr:last-child td { border-bottom: none; }
  190. tr:hover td { background: #fafafa; }
  191. td { font-size: 14px; }
  192. /* Forms */
  193. .form-group {
  194. margin-bottom: 16px;
  195. }
  196. label {
  197. display: block;
  198. margin-bottom: 6px;
  199. font-weight: 500;
  200. font-size: 14px;
  201. }
  202. input[type="text"], input[type="datetime-local"], textarea, select {
  203. width: 100%;
  204. padding: 10px 12px;
  205. border: 1px solid #e0e0e0;
  206. border-radius: 6px;
  207. font-size: 14px;
  208. font-family: inherit;
  209. }
  210. input:focus, textarea:focus, select:focus {
  211. outline: none;
  212. border-color: #1a1a1a;
  213. }
  214. textarea { min-height: 100px; resize: vertical; }
  215. .help-text { color: #666; font-size: 13px; margin-top: 4px; }
  216. /* Status badges */
  217. .badge {
  218. display: inline-flex;
  219. align-items: center;
  220. padding: 4px 10px;
  221. border-radius: 4px;
  222. font-size: 12px;
  223. font-weight: 500;
  224. }
  225. .badge.success {
  226. background: #dcfce7;
  227. color: #166534;
  228. }
  229. .badge.failed {
  230. background: #fef2f2;
  231. color: #dc2626;
  232. }
  233. .hidden { display: none; }
  234. .error { color: #dc2626; margin-top: 8px; font-size: 14px; }
  235. .empty {
  236. text-align: center;
  237. padding: 40px;
  238. color: #666;
  239. }
  240. /* Status indicator */
  241. .status-msg {
  242. display: flex;
  243. align-items: center;
  244. gap: 8px;
  245. padding: 8px 0;
  246. font-size: 14px;
  247. }
  248. .status-msg.success { color: #166534; }
  249. .status-msg.error { color: #dc2626; }
  250. /* Loading spinner */
  251. .loading {
  252. text-align: center;
  253. padding: 40px;
  254. color: #666;
  255. }
  256. .spinner {
  257. display: inline-block;
  258. width: 32px;
  259. height: 32px;
  260. border: 3px solid #e0e0e0;
  261. border-top-color: #1a1a1a;
  262. border-radius: 50%;
  263. animation: spin 0.8s linear infinite;
  264. margin-bottom: 8px;
  265. }
  266. @keyframes spin {
  267. to { transform: rotate(360deg); }
  268. }
  269. .mono {
  270. font-family: 'SF Mono', Consolas, monospace;
  271. font-size: 13px;
  272. }
  273. @media (max-width: 768px) {
  274. body { padding: 16px; }
  275. th, td { padding: 10px 12px; }
  276. .header { flex-direction: column; align-items: flex-start; }
  277. }
  278. </style>
  279. </head>
  280. <body>
  281. <div class="container">
  282. <div class="header">
  283. <h1>Letta Scheduling Dashboard</h1>
  284. <div class="header-links">
  285. <a href="/">Home</a>
  286. <a href="https://docs.letta.com/guides/agents/scheduling/" target="_blank">Letta Docs</a>
  287. <a href="https://github.com/cpfiffer/letta-switchboard">GitHub</a>
  288. <button id="logout-btn" onclick="clearApiKey()" class="secondary" style="display: none;">Logout</button>
  289. </div>
  290. </div>
  291. <!-- Migration Notice -->
  292. <div id="migration-banner" class="migration-banner">
  293. <h3>✨ Now powered by Letta's native scheduling!</h3>
  294. <p>
  295. This dashboard now calls Letta's API directly. The old Switchboard backend has been deprecated.
  296. Read the <a href="https://docs.letta.com/guides/agents/scheduling/" target="_blank">full scheduling docs →</a>
  297. </p>
  298. </div>
  299. <!-- API Key Input -->
  300. <div id="api-key-section" class="card">
  301. <div class="card-title">Authentication</div>
  302. <input type="password" id="api-key-input" placeholder="Enter your Letta API key (sk-let-...)">
  303. <div class="btn-group">
  304. <button onclick="saveApiKey()">Connect</button>
  305. <button onclick="clearApiKey()" class="secondary">Disconnect</button>
  306. </div>
  307. <div id="api-key-status"></div>
  308. </div>
  309. <div id="main-content" class="hidden">
  310. <!-- Tabs -->
  311. <div class="tabs">
  312. <button class="tab active" onclick="showTab('schedules')">Schedules</button>
  313. <button class="tab" onclick="showTab('create')">Create New</button>
  314. <button class="tab" onclick="showTab('results')">Results</button>
  315. </div>
  316. <!-- Schedules Tab -->
  317. <div id="schedules-tab" class="content active">
  318. <div class="card" style="margin-bottom: 20px;">
  319. <div class="form-group" style="margin-bottom: 0;">
  320. <label for="schedules-agent-id">Agent ID</label>
  321. <div style="display: flex; gap: 8px;">
  322. <input type="text" id="schedules-agent-id" placeholder="agent-xxx" style="flex: 1;">
  323. <button onclick="loadSchedulesFromInput()" class="secondary">Load Schedules</button>
  324. </div>
  325. </div>
  326. </div>
  327. <h2>One-Time Schedules</h2>
  328. <div class="table-container">
  329. <table id="onetime-table">
  330. <thead>
  331. <tr>
  332. <th>ID</th>
  333. <th>Agent ID</th>
  334. <th>Execute At</th>
  335. <th>Message</th>
  336. <th></th>
  337. </tr>
  338. </thead>
  339. <tbody></tbody>
  340. </table>
  341. </div>
  342. <h2>Recurring Schedules</h2>
  343. <div class="table-container">
  344. <table id="recurring-table">
  345. <thead>
  346. <tr>
  347. <th>ID</th>
  348. <th>Agent ID</th>
  349. <th>Cron</th>
  350. <th>Timezone</th>
  351. <th>Message</th>
  352. <th>Last Run</th>
  353. <th></th>
  354. </tr>
  355. </thead>
  356. <tbody></tbody>
  357. </table>
  358. </div>
  359. </div>
  360. <!-- Create Tab -->
  361. <div id="create-tab" class="content">
  362. <div class="card">
  363. <h2 style="margin-top: 0;">Create Schedule</h2>
  364. <div class="form-group">
  365. <label>Schedule Type</label>
  366. <select id="schedule-type" onchange="toggleScheduleType()">
  367. <option value="onetime">One-Time</option>
  368. <option value="recurring">Recurring</option>
  369. </select>
  370. </div>
  371. <div class="form-group">
  372. <label for="agent-id">Agent ID</label>
  373. <input type="text" id="agent-id" placeholder="agent-xxx">
  374. </div>
  375. <div class="form-group">
  376. <label for="message">Message</label>
  377. <textarea id="message" placeholder="Your message to the agent"></textarea>
  378. </div>
  379. <div id="onetime-fields">
  380. <div class="form-group">
  381. <label for="execute-at">Execute At</label>
  382. <input type="datetime-local" id="execute-at">
  383. </div>
  384. <div class="form-group">
  385. <label for="timezone">Timezone</label>
  386. <select id="timezone">
  387. <option value="local" selected>Local Browser Time</option>
  388. <option value="UTC">UTC</option>
  389. <option value="America/New_York">Eastern (US)</option>
  390. <option value="America/Chicago">Central (US)</option>
  391. <option value="America/Denver">Mountain (US)</option>
  392. <option value="America/Los_Angeles">Pacific (US)</option>
  393. <option value="Europe/London">London</option>
  394. <option value="Europe/Paris">Paris</option>
  395. <option value="Asia/Tokyo">Tokyo</option>
  396. </select>
  397. <div class="help-text">Time will be converted to UTC for storage</div>
  398. </div>
  399. </div>
  400. <div id="recurring-fields" class="hidden">
  401. <div class="form-group">
  402. <label for="cron">Cron Expression</label>
  403. <input type="text" id="cron" placeholder="0 9 * * 1-5">
  404. <div class="help-text">Examples: "0 9 * * *" (daily at 9am), "*/5 * * * *" (every 5 min)</div>
  405. </div>
  406. <div class="form-group">
  407. <label for="recurring-timezone">Timezone</label>
  408. <select id="recurring-timezone">
  409. <option value="UTC" selected>UTC</option>
  410. <option value="America/New_York">Eastern (US)</option>
  411. <option value="America/Chicago">Central (US)</option>
  412. <option value="America/Denver">Mountain (US)</option>
  413. <option value="America/Los_Angeles">Pacific (US)</option>
  414. <option value="Europe/London">London</option>
  415. <option value="Europe/Paris">Paris</option>
  416. <option value="Asia/Tokyo">Tokyo</option>
  417. </select>
  418. <div class="help-text">Cron expression will be evaluated in this timezone</div>
  419. </div>
  420. </div>
  421. <button onclick="createSchedule()">Create Schedule</button>
  422. <div id="create-error" class="error"></div>
  423. </div>
  424. </div>
  425. <!-- Results Tab -->
  426. <div id="results-tab" class="content">
  427. <h2>Execution Results</h2>
  428. <div class="table-container">
  429. <table id="results-table">
  430. <thead>
  431. <tr>
  432. <th>Schedule ID</th>
  433. <th>Type</th>
  434. <th>Status</th>
  435. <th>Agent ID</th>
  436. <th>Message</th>
  437. <th>Run ID / Error</th>
  438. <th>Executed At</th>
  439. </tr>
  440. </thead>
  441. <tbody></tbody>
  442. </table>
  443. </div>
  444. </div>
  445. </div>
  446. </div>
  447. <script>
  448. const LETTA_API = 'https://api.letta.com/v1';
  449. let API_KEY = sessionStorage.getItem('letta_api_key') || '';
  450. let DEFAULT_AGENT_ID = sessionStorage.getItem('default_agent_id') || '';
  451. // Initialize
  452. if (API_KEY) {
  453. document.getElementById('api-key-input').value = API_KEY;
  454. showAuthenticatedState();
  455. if (DEFAULT_AGENT_ID) {
  456. document.getElementById('agent-id').value = DEFAULT_AGENT_ID;
  457. document.getElementById('schedules-agent-id').value = DEFAULT_AGENT_ID;
  458. loadSchedules();
  459. }
  460. }
  461. function showAuthenticatedState() {
  462. document.getElementById('main-content').classList.remove('hidden');
  463. document.getElementById('api-key-section').style.display = 'none';
  464. document.getElementById('migration-banner').style.display = 'none';
  465. document.getElementById('logout-btn').style.display = 'inline-block';
  466. }
  467. function showUnauthenticatedState() {
  468. document.getElementById('main-content').classList.add('hidden');
  469. document.getElementById('api-key-section').style.display = 'block';
  470. document.getElementById('migration-banner').style.display = 'block';
  471. document.getElementById('logout-btn').style.display = 'none';
  472. }
  473. function loadSchedulesFromInput() {
  474. const agentId = document.getElementById('schedules-agent-id').value.trim();
  475. if (agentId) {
  476. // Sync to create tab
  477. document.getElementById('agent-id').value = agentId;
  478. DEFAULT_AGENT_ID = agentId;
  479. sessionStorage.setItem('default_agent_id', agentId);
  480. loadSchedules();
  481. }
  482. }
  483. // Helper: Convert ISO timestamp to Unix milliseconds
  484. function toUnixMs(isoString) {
  485. return new Date(isoString).getTime();
  486. }
  487. // Helper: Convert Unix milliseconds to readable date
  488. function fromUnixMs(ms) {
  489. return new Date(ms).toLocaleString();
  490. }
  491. function saveApiKey() {
  492. API_KEY = document.getElementById('api-key-input').value.trim();
  493. if (!API_KEY) {
  494. showStatus('api-key-status', 'API key is required', 'error');
  495. return;
  496. }
  497. sessionStorage.setItem('letta_api_key', API_KEY);
  498. showStatus('api-key-status', 'Connected successfully', 'success');
  499. setTimeout(() => {
  500. showAuthenticatedState();
  501. // Pre-fill agent ID if saved
  502. if (DEFAULT_AGENT_ID) {
  503. document.getElementById('schedules-agent-id').value = DEFAULT_AGENT_ID;
  504. document.getElementById('agent-id').value = DEFAULT_AGENT_ID;
  505. loadSchedules();
  506. }
  507. }, 500);
  508. }
  509. function clearApiKey() {
  510. sessionStorage.removeItem('letta_api_key');
  511. sessionStorage.removeItem('default_agent_id');
  512. API_KEY = '';
  513. DEFAULT_AGENT_ID = '';
  514. document.getElementById('api-key-input').value = '';
  515. document.getElementById('schedules-agent-id').value = '';
  516. document.getElementById('agent-id').value = '';
  517. showUnauthenticatedState();
  518. }
  519. function showStatus(elementId, message, type) {
  520. const el = document.getElementById(elementId);
  521. el.innerHTML = `<div class="status-msg ${type}">${message}</div>`;
  522. }
  523. function showTab(tab) {
  524. document.querySelectorAll('.tab').forEach(t => t.classList.remove('active'));
  525. document.querySelectorAll('.content').forEach(c => c.classList.remove('active'));
  526. event.target.classList.add('active');
  527. document.getElementById(`${tab}-tab`).classList.add('active');
  528. }
  529. function toggleScheduleType() {
  530. const type = document.getElementById('schedule-type').value;
  531. document.getElementById('onetime-fields').classList.toggle('hidden', type !== 'onetime');
  532. document.getElementById('recurring-fields').classList.toggle('hidden', type !== 'recurring');
  533. }
  534. async function loadSchedules() {
  535. // Try schedules tab input first, then create tab input
  536. let agentId = document.getElementById('schedules-agent-id').value.trim();
  537. if (!agentId) {
  538. agentId = document.getElementById('agent-id').value.trim();
  539. }
  540. if (!agentId) {
  541. const onetimeBody = document.querySelector('#onetime-table tbody');
  542. const recurringBody = document.querySelector('#recurring-table tbody');
  543. onetimeBody.innerHTML = '<tr><td colspan="5" class="empty">Enter an Agent ID above and click Load Schedules</td></tr>';
  544. recurringBody.innerHTML = '<tr><td colspan="7" class="empty">Enter an Agent ID above and click Load Schedules</td></tr>';
  545. return;
  546. }
  547. // Sync both inputs
  548. document.getElementById('schedules-agent-id').value = agentId;
  549. document.getElementById('agent-id').value = agentId;
  550. // Save for next time
  551. sessionStorage.setItem('default_agent_id', agentId);
  552. DEFAULT_AGENT_ID = agentId;
  553. const onetimeBody = document.querySelector('#onetime-table tbody');
  554. const recurringBody = document.querySelector('#recurring-table tbody');
  555. onetimeBody.innerHTML = '<tr><td colspan="5" class="loading"><div class="spinner"></div><p>Loading...</p></td></tr>';
  556. recurringBody.innerHTML = '<tr><td colspan="7" class="loading"><div class="spinner"></div><p>Loading...</p></td></tr>';
  557. try {
  558. // Letta API returns all schedules (one-time and recurring) in a single call
  559. const response = await fetch(`${LETTA_API}/agents/${agentId}/schedule`, {
  560. headers: { 'Authorization': `Bearer ${API_KEY}` }
  561. });
  562. if (!response.ok) {
  563. throw new Error(`Failed to load schedules: ${response.statusText}`);
  564. }
  565. const data = await response.json();
  566. console.log('Letta API response:', data); // Debug log
  567. const schedules = data.scheduled_messages || data.schedules || data || [];
  568. // Debug: Log first schedule if available
  569. if (schedules.length > 0) {
  570. console.log('First schedule:', schedules[0]);
  571. console.log('First schedule messages:', schedules[0].messages);
  572. }
  573. // Split into one-time and recurring
  574. const onetime = schedules.filter(s => s.schedule?.type === 'one-time');
  575. const recurring = schedules.filter(s => s.schedule?.type === 'recurring');
  576. onetimeBody.innerHTML = onetime.length ? onetime.map(s => `
  577. <tr>
  578. <td class="mono">${s.id?.substring(0, 8) || 'N/A'}...</td>
  579. <td class="mono">${s.agent_id || 'N/A'}</td>
  580. <td>${s.next_scheduled_at ? fromUnixMs(s.next_scheduled_at) : 'N/A'}</td>
  581. <td>${truncate(getMessageContent(s), 50)}</td>
  582. <td><button class="danger sm" onclick="deleteSchedule('${s.id}')">Delete</button></td>
  583. </tr>
  584. `).join('') : '<tr><td colspan="5" class="empty">No one-time schedules</td></tr>';
  585. recurringBody.innerHTML = recurring.length ? recurring.map(s => `
  586. <tr>
  587. <td class="mono">${s.id?.substring(0, 8) || 'N/A'}...</td>
  588. <td class="mono">${s.agent_id || 'N/A'}</td>
  589. <td class="mono">${s.schedule?.cron_expression || 'N/A'}</td>
  590. <td>UTC</td>
  591. <td>${truncate(getMessageContent(s), 50)}</td>
  592. <td>${s.next_scheduled_at ? fromUnixMs(s.next_scheduled_at) : 'Never'}</td>
  593. <td><button class="danger sm" onclick="deleteSchedule('${s.id}')">Delete</button></td>
  594. </tr>
  595. `).join('') : '<tr><td colspan="7" class="empty">No recurring schedules</td></tr>';
  596. } catch (error) {
  597. onetimeBody.innerHTML = `<tr><td colspan="5" class="empty">Error: ${error.message}</td></tr>`;
  598. recurringBody.innerHTML = `<tr><td colspan="7" class="empty">Error: ${error.message}</td></tr>`;
  599. }
  600. }
  601. async function loadResults() {
  602. const resultsBody = document.querySelector('#results-table tbody');
  603. resultsBody.innerHTML = '<tr><td colspan="7" class="empty">Results tracking not available yet. Check Letta Cloud dashboard for execution history.</td></tr>';
  604. // NOTE: Letta's scheduling API doesn't expose execution history yet
  605. // Users should check the Letta Cloud dashboard or use the runs API
  606. }
  607. async function createSchedule() {
  608. const type = document.getElementById('schedule-type').value;
  609. const agentId = document.getElementById('agent-id').value.trim();
  610. const message = document.getElementById('message').value.trim();
  611. const errorDiv = document.getElementById('create-error');
  612. errorDiv.textContent = '';
  613. if (!agentId || !message) {
  614. errorDiv.textContent = 'Please fill in all required fields';
  615. return;
  616. }
  617. // Save agent ID for convenience
  618. sessionStorage.setItem('default_agent_id', agentId);
  619. DEFAULT_AGENT_ID = agentId;
  620. try {
  621. // Build Letta API format
  622. const payload = {
  623. messages: [{
  624. role: 'user',
  625. content: message
  626. }]
  627. };
  628. if (type === 'onetime') {
  629. const executeAt = document.getElementById('execute-at').value;
  630. const timezone = document.getElementById('timezone').value;
  631. if (!executeAt) {
  632. errorDiv.textContent = 'Please specify execution time';
  633. return;
  634. }
  635. let executeDate;
  636. if (timezone === 'local') {
  637. executeDate = new Date(executeAt);
  638. } else if (timezone === 'UTC') {
  639. executeDate = new Date(executeAt + 'Z');
  640. } else {
  641. executeDate = new Date(executeAt);
  642. }
  643. // Convert to Unix milliseconds for Letta API
  644. payload.schedule = {
  645. type: 'one-time',
  646. scheduled_at: executeDate.getTime()
  647. };
  648. } else {
  649. const cron = document.getElementById('cron').value.trim();
  650. if (!cron) {
  651. errorDiv.textContent = 'Please specify cron expression';
  652. return;
  653. }
  654. // Letta uses 5-field cron (no seconds)
  655. payload.schedule = {
  656. type: 'recurring',
  657. cron_expression: cron
  658. };
  659. }
  660. const response = await fetch(`${LETTA_API}/agents/${agentId}/schedule`, {
  661. method: 'POST',
  662. headers: {
  663. 'Authorization': `Bearer ${API_KEY}`,
  664. 'Content-Type': 'application/json'
  665. },
  666. body: JSON.stringify(payload)
  667. });
  668. if (!response.ok) {
  669. const error = await response.json();
  670. throw new Error(error.detail || error.message || 'Failed to create schedule');
  671. }
  672. // Clear form
  673. document.getElementById('message').value = '';
  674. document.getElementById('execute-at').value = '';
  675. document.getElementById('cron').value = '';
  676. document.getElementById('recurring-timezone').value = 'UTC';
  677. // Switch to schedules tab
  678. showTab('schedules');
  679. document.querySelector('.tab').click();
  680. loadSchedules();
  681. } catch (error) {
  682. errorDiv.textContent = error.message;
  683. }
  684. }
  685. async function deleteSchedule(id) {
  686. if (!confirm('Delete this schedule?')) return;
  687. let agentId = document.getElementById('schedules-agent-id').value.trim();
  688. if (!agentId) {
  689. agentId = document.getElementById('agent-id').value.trim();
  690. }
  691. if (!agentId) {
  692. alert('Agent ID is required');
  693. return;
  694. }
  695. try {
  696. const response = await fetch(`${LETTA_API}/agents/${agentId}/schedule/${id}`, {
  697. method: 'DELETE',
  698. headers: { 'Authorization': `Bearer ${API_KEY}` }
  699. });
  700. if (!response.ok) {
  701. const error = await response.json();
  702. throw new Error(error.detail || error.message || 'Failed to delete');
  703. }
  704. loadSchedules();
  705. } catch (error) {
  706. alert('Error: ' + error.message);
  707. }
  708. }
  709. function truncate(str, len) {
  710. if (!str) return '';
  711. if (typeof str === 'object') str = JSON.stringify(str);
  712. return str.length > len ? str.substring(0, len) + '...' : str;
  713. }
  714. function getMessageContent(schedule) {
  715. // Try different possible message formats
  716. if (schedule.messages && Array.isArray(schedule.messages) && schedule.messages.length > 0) {
  717. const msg = schedule.messages[0];
  718. if (typeof msg === 'string') return msg;
  719. if (msg.content) {
  720. if (typeof msg.content === 'string') return msg.content;
  721. if (typeof msg.content === 'object') return JSON.stringify(msg.content);
  722. }
  723. if (msg.text) return msg.text;
  724. return JSON.stringify(msg);
  725. }
  726. if (schedule.message) return schedule.message;
  727. return 'N/A';
  728. }
  729. </script>
  730. </body>
  731. </html>