| 1 | <html> |
| 2 | |
| 3 | <head> |
| 4 | <title>Agent Zero External API Examples</title> |
| 5 | </head> |
| 6 | |
| 7 | <body> |
| 8 | <div |
| 9 | x-data |
| 10 | x-init="window.initExternalApiExamples && window.initExternalApiExamples($el)" |
| 11 | x-destroy="window.destroyExternalApiExamples && window.destroyExternalApiExamples($el)" |
| 12 | > |
| 13 | <p>Agent Zero provides external API endpoints for integration with other applications.</p> |
| 14 | <p>These endpoints use API key authentication and support text messages and file attachments.</p> |
| 15 | |
| 16 | <!-- API Token Information --> |
| 17 | <div style="background-color: var(--color-bg-secondary); border: 1px solid var(--color-border); border-radius: 6px; padding: 12px; margin: 16px 0;"> |
| 18 | <h4 style="margin: 0 0 8px 0; color: var(--color-text-primary);">API Token Information</h4> |
| 19 | <p style="margin: 0; color: var(--color-text-secondary); font-size: 14px;"> |
| 20 | The API token is automatically generated from your username and password. |
| 21 | This same token is used for both MCP server connections and external API endpoints. |
| 22 | The token changes when you update your credentials. |
| 23 | </p> |
| 24 | </div> |
| 25 | |
| 26 | <!-- Section 1: api_message Endpoint --> |
| 27 | <div class="api-section"> |
| 28 | <h2>POST /api_message</h2> |
| 29 | <p style="margin: 0 0 16px 0; color: var(--color-text-secondary); font-size: 14px;"> |
| 30 | Send messages to Agent Zero and receive responses. Supports text messages, file attachments, and conversation continuity. |
| 31 | </p> |
| 32 | |
| 33 | <!-- API Reference --> |
| 34 | <h3 style="margin: 20px 0 12px 0; color: var(--color-text-primary);">API Reference</h3> |
| 35 | <div style="background-color: var(--color-bg-secondary); border: 1px solid var(--color-border); border-radius: 6px; padding: 12px; margin: 0 0 20px 0;"> |
| 36 | <p style="margin: 0 0 8px 0; color: var(--color-text-secondary); font-size: 14px;"> |
| 37 | <strong>Parameters:</strong><br> |
| 38 | • <code>context_id</code> (string, optional): Existing chat context ID<br> |
| 39 | • <code>message</code> (string, required): The message to send<br> |
| 40 | • <code>attachments</code> (array, optional): Array of {filename, base64} objects<br> |
| 41 | • <code>lifetime_hours</code> (number, optional): Chat lifetime in hours (default: 24)<br> |
| 42 | • <code>project_name</code> (string, optional): Project name to activate (only on first message) |
| 43 | </p> |
| 44 | <p style="margin: 0; color: var(--color-text-secondary); font-size: 14px;"> |
| 45 | <strong>Headers:</strong> <code>X-API-KEY</code> (required), <code>Content-Type: application/json</code> |
| 46 | </p> |
| 47 | </div> |
| 48 | |
| 49 | <!-- JavaScript Examples --> |
| 50 | <h3 style="margin: 20px 0 12px 0; color: var(--color-text-primary);">JavaScript Examples</h3> |
| 51 | |
| 52 | <h4>Basic Usage Example</h4> |
| 53 | <div id="api-basic-example"></div> |
| 54 | |
| 55 | <h4>Conversation Continuation Example</h4> |
| 56 | <div id="api-continuation-example"></div> |
| 57 | |
| 58 | <h4>File Attachment Example</h4> |
| 59 | <div id="api-attachment-example"></div> |
| 60 | |
| 61 | <h4>Project Usage Example</h4> |
| 62 | <div id="api-project-example"></div> |
| 63 | </div> |
| 64 | |
| 65 | <!-- Section 2: api_log_get Endpoint --> |
| 66 | <div class="api-section"> |
| 67 | <h2>GET/POST /api_log_get</h2> |
| 68 | <p style="margin: 0 0 16px 0; color: var(--color-text-secondary); font-size: 14px;"> |
| 69 | Retrieve log data by context ID, limited to a specified number of entries from the newest. |
| 70 | </p> |
| 71 | |
| 72 | <!-- API Reference --> |
| 73 | <h3 style="margin: 20px 0 12px 0; color: var(--color-text-primary);">API Reference</h3> |
| 74 | <div style="background-color: var(--color-bg-secondary); border: 1px solid var(--color-border); border-radius: 6px; padding: 12px; margin: 0 0 20px 0;"> |
| 75 | <p style="margin: 0 0 8px 0; color: var(--color-text-secondary); font-size: 14px;"> |
| 76 | <strong>Parameters:</strong><br> |
| 77 | • <code>context_id</code> (string, required): Context ID to get logs from<br> |
| 78 | • <code>length</code> (integer, optional): Number of log items to return from newest (default: 100) |
| 79 | </p> |
| 80 | <p style="margin: 0; color: var(--color-text-secondary); font-size: 14px;"> |
| 81 | <strong>Headers:</strong> <code>X-API-KEY</code> (required), <code>Content-Type: application/json</code> (for POST) |
| 82 | </p> |
| 83 | </div> |
| 84 | |
| 85 | <!-- JavaScript Examples --> |
| 86 | <h3 style="margin: 20px 0 12px 0; color: var(--color-text-primary);">JavaScript Examples</h3> |
| 87 | |
| 88 | <h4>GET Request Example</h4> |
| 89 | <div id="api-log-get-example"></div> |
| 90 | |
| 91 | <h4>POST Request Example</h4> |
| 92 | <div id="api-log-post-example"></div> |
| 93 | </div> |
| 94 | |
| 95 | <!-- Section 3: api_terminate_chat Endpoint --> |
| 96 | <div class="api-section"> |
| 97 | <h2>POST /api_terminate_chat</h2> |
| 98 | <p style="margin: 0 0 16px 0; color: var(--color-text-secondary); font-size: 14px;"> |
| 99 | Terminate and remove a chat context to free up resources. Similar to the MCP finish_chat function. |
| 100 | </p> |
| 101 | |
| 102 | <!-- API Reference --> |
| 103 | <h3 style="margin: 20px 0 12px 0; color: var(--color-text-primary);">API Reference</h3> |
| 104 | <div style="background-color: var(--color-bg-secondary); border: 1px solid var(--color-border); border-radius: 6px; padding: 12px; margin: 0 0 20px 0;"> |
| 105 | <p style="margin: 0 0 8px 0; color: var(--color-text-secondary); font-size: 14px;"> |
| 106 | <strong>Parameters:</strong><br> |
| 107 | • <code>context_id</code> (string, required): Context ID of the chat to terminate |
| 108 | </p> |
| 109 | <p style="margin: 0; color: var(--color-text-secondary); font-size: 14px;"> |
| 110 | <strong>Headers:</strong> <code>X-API-KEY</code> (required), <code>Content-Type: application/json</code> |
| 111 | </p> |
| 112 | </div> |
| 113 | |
| 114 | <!-- JavaScript Examples --> |
| 115 | <h3 style="margin: 20px 0 12px 0; color: var(--color-text-primary);">JavaScript Examples</h3> |
| 116 | <h4>Basic Termination Examples</h4> |
| 117 | <div id="api-terminate-example"></div> |
| 118 | </div> |
| 119 | |
| 120 | <!-- Section 4: api_reset_chat Endpoint --> |
| 121 | <div class="api-section"> |
| 122 | <h2>POST /api_reset_chat</h2> |
| 123 | <p style="margin: 0 0 16px 0; color: var(--color-text-secondary); font-size: 14px;"> |
| 124 | Reset a chat context to clear conversation history while keeping the context_id alive for continued use. |
| 125 | </p> |
| 126 | |
| 127 | <!-- API Reference --> |
| 128 | <h3 style="margin: 20px 0 12px 0; color: var(--color-text-primary);">API Reference</h3> |
| 129 | <div style="background-color: var(--color-bg-secondary); border: 1px solid var(--color-border); border-radius: 6px; padding: 12px; margin: 0 0 20px 0;"> |
| 130 | <p style="margin: 0 0 8px 0; color: var(--color-text-secondary); font-size: 14px;"> |
| 131 | <strong>Parameters:</strong><br> |
| 132 | • <code>context_id</code> (string, required): Context ID of the chat to reset |
| 133 | </p> |
| 134 | <p style="margin: 0; color: var(--color-text-secondary); font-size: 14px;"> |
| 135 | <strong>Headers:</strong> <code>X-API-KEY</code> (required), <code>Content-Type: application/json</code> |
| 136 | </p> |
| 137 | </div> |
| 138 | |
| 139 | <!-- JavaScript Examples --> |
| 140 | <h3 style="margin: 20px 0 12px 0; color: var(--color-text-primary);">JavaScript Examples</h3> |
| 141 | <h4>Basic Reset Examples</h4> |
| 142 | <div id="api-reset-example"></div> |
| 143 | </div> |
| 144 | |
| 145 | <!-- Section 5: api_files_get Endpoint --> |
| 146 | <div class="api-section"> |
| 147 | <h2>POST /api_files_get</h2> |
| 148 | <p style="margin: 0 0 16px 0; color: var(--color-text-secondary); font-size: 14px;"> |
| 149 | Retrieve file contents by paths, returning files as base64 encoded data. Useful for retrieving uploaded attachments. |
| 150 | </p> |
| 151 | |
| 152 | <!-- API Reference --> |
| 153 | <h3 style="margin: 20px 0 12px 0; color: var(--color-text-primary);">API Reference</h3> |
| 154 | <div style="background-color: var(--color-bg-secondary); border: 1px solid var(--color-border); border-radius: 6px; padding: 12px; margin: 0 0 20px 0;"> |
| 155 | <p style="margin: 0 0 8px 0; color: var(--color-text-secondary); font-size: 14px;"> |
| 156 | <strong>Parameters:</strong><br> |
| 157 | • <code>paths</code> (array, required): Array of file paths to retrieve (e.g., ["/a0/usr/uploads/file.txt"]) |
| 158 | </p> |
| 159 | <p style="margin: 0; color: var(--color-text-secondary); font-size: 14px;"> |
| 160 | <strong>Headers:</strong> <code>X-API-KEY</code> (required), <code>Content-Type: application/json</code> |
| 161 | </p> |
| 162 | </div> |
| 163 | |
| 164 | <!-- JavaScript Examples --> |
| 165 | <h3 style="margin: 20px 0 12px 0; color: var(--color-text-primary);">JavaScript Examples</h3> |
| 166 | <h4>File Retrieval Examples</h4> |
| 167 | <div id="api-files-get-example"></div> |
| 168 | </div> |
| 169 | |
| 170 | <!-- Section 6: Additional Endpoints --> |
| 171 | <!-- |
| 172 | Example template for new endpoint sections: |
| 173 | |
| 174 | <div class="api-section"> |
| 175 | <h2>POST /endpoint_name</h2> |
| 176 | <p style="margin: 0 0 16px 0; color: var(--color-text-secondary); font-size: 14px;"> |
| 177 | Description of what this endpoint does... |
| 178 | </p> |
| 179 | |
| 180 | <h3 style="margin: 20px 0 12px 0; color: var(--color-text-primary);">API Reference</h3> |
| 181 | <div style="background-color: var(--color-bg-secondary); border: 1px solid var(--color-border); border-radius: 6px; padding: 12px; margin: 0 0 20px 0;"> |
| 182 | <p style="margin: 0 0 8px 0; color: var(--color-text-secondary); font-size: 14px;"> |
| 183 | <strong>Parameters:</strong><br> |
| 184 | • <code>param_name</code> (type, required/optional): Description |
| 185 | </p> |
| 186 | <p style="margin: 0; color: var(--color-text-secondary); font-size: 14px;"> |
| 187 | <strong>Headers:</strong> <code>X-API-KEY</code> (required), <code>Content-Type: application/json</code> |
| 188 | </p> |
| 189 | </div> |
| 190 | |
| 191 | <h3 style="margin: 20px 0 12px 0; color: var(--color-text-primary);">JavaScript Examples</h3> |
| 192 | <h4>Example Title</h4> |
| 193 | <div id="example-id"></div> |
| 194 | </div> |
| 195 | --> |
| 196 | |
| 197 | <script type="module"> |
| 198 | import * as API from "/js/api.js"; |
| 199 | |
| 200 | const mountedEditors = new WeakMap(); |
| 201 | |
| 202 | function scheduleEditorResize(root, editors) { |
| 203 | const state = { |
| 204 | editors, |
| 205 | frameId: null, |
| 206 | timeoutIds: [], |
| 207 | }; |
| 208 | const resize = () => { |
| 209 | if (mountedEditors.get(root) !== state) return; |
| 210 | editors.forEach((editor) => editor.resize(true)); |
| 211 | }; |
| 212 | state.frameId = requestAnimationFrame(() => { |
| 213 | state.frameId = null; |
| 214 | resize(); |
| 215 | state.timeoutIds = [ |
| 216 | setTimeout(resize, 80), |
| 217 | setTimeout(resize, 240), |
| 218 | ]; |
| 219 | }); |
| 220 | mountedEditors.set(root, state); |
| 221 | } |
| 222 | |
| 223 | function destroyEditors(root) { |
| 224 | const state = mountedEditors.get(root); |
| 225 | if (!state) return; |
| 226 | |
| 227 | if (state.frameId) { |
| 228 | cancelAnimationFrame(state.frameId); |
| 229 | } |
| 230 | state.timeoutIds.forEach((timeoutId) => clearTimeout(timeoutId)); |
| 231 | |
| 232 | state.editors.forEach((editor) => { |
| 233 | try { |
| 234 | editor.destroy(); |
| 235 | editor.container.removeAttribute("data-ace-editor-id"); |
| 236 | editor.container.textContent = ""; |
| 237 | } catch (error) { |
| 238 | console.warn("Failed to destroy API example editor:", error); |
| 239 | } |
| 240 | }); |
| 241 | mountedEditors.delete(root); |
| 242 | } |
| 243 | |
| 244 | async function buildExamples() { |
| 245 | const url = window.location.origin; |
| 246 | |
| 247 | // Fetch token from settings API |
| 248 | let token = ""; |
| 249 | try { |
| 250 | const response = await API.callJsonApi("settings_get", null); |
| 251 | token = response?.settings?.mcp_server_token || ""; |
| 252 | } catch (e) { |
| 253 | console.error("Failed to fetch token:", e); |
| 254 | } |
| 255 | |
| 256 | // Basic usage example |
| 257 | const basicExample = `// Basic message example |
| 258 | async function sendMessage() { |
| 259 | try { |
| 260 | const response = await fetch('${url}/api/api_message', { |
| 261 | method: 'POST', |
| 262 | headers: { |
| 263 | 'Content-Type': 'application/json', |
| 264 | 'X-API-KEY': '${token}' |
| 265 | }, |
| 266 | body: JSON.stringify({ |
| 267 | message: "Hello, how can you help me?", |
| 268 | lifetime_hours: 24 |
| 269 | }) |
| 270 | }); |
| 271 | |
| 272 | const data = await response.json(); |
| 273 | |
| 274 | if (response.ok) { |
| 275 | console.log('✅ Success!'); |
| 276 | console.log('Response:', data.response); |
| 277 | console.log('Context ID:', data.context_id); |
| 278 | return data; |
| 279 | } else { |
| 280 | console.error('❌ Error:', data.error); |
| 281 | return null; |
| 282 | } |
| 283 | } catch (error) { |
| 284 | console.error('❌ Request failed:', error); |
| 285 | return null; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | // Call the function |
| 290 | sendMessage().then(result => { |
| 291 | if (result) { |
| 292 | console.log('Message sent successfully!'); |
| 293 | } |
| 294 | });`; |
| 295 | |
| 296 | // Continuation example |
| 297 | const continuationExample = `// Continue conversation example |
| 298 | async function continueConversation(contextId) { |
| 299 | try { |
| 300 | const response = await fetch('${url}/api/api_message', { |
| 301 | method: 'POST', |
| 302 | headers: { |
| 303 | 'Content-Type': 'application/json', |
| 304 | 'X-API-KEY': '${token}' |
| 305 | }, |
| 306 | body: JSON.stringify({ |
| 307 | context_id: contextId, |
| 308 | message: "Can you tell me more about that?", |
| 309 | lifetime_hours: 24 |
| 310 | }) |
| 311 | }); |
| 312 | |
| 313 | const data = await response.json(); |
| 314 | |
| 315 | if (response.ok) { |
| 316 | console.log('✅ Continuation Success!'); |
| 317 | console.log('Response:', data.response); |
| 318 | return data; |
| 319 | } else { |
| 320 | console.error('❌ Error:', data.error); |
| 321 | return null; |
| 322 | } |
| 323 | } catch (error) { |
| 324 | console.error('❌ Request failed:', error); |
| 325 | return null; |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | // Example: First send a message, then continue the conversation |
| 330 | async function fullConversationExample() { |
| 331 | const firstResult = await sendMessage(); |
| 332 | if (firstResult && firstResult.context_id) { |
| 333 | await continueConversation(firstResult.context_id); |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | fullConversationExample();`; |
| 338 | |
| 339 | // Attachment example |
| 340 | const attachmentExample = `// File attachment example |
| 341 | async function sendWithAttachment() { |
| 342 | try { |
| 343 | // Example with text content (convert to base64) |
| 344 | const textContent = "Hello World from attachment!"; |
| 345 | const base64Content = btoa(textContent); |
| 346 | |
| 347 | const response = await fetch('${url}/api/api_message', { |
| 348 | method: 'POST', |
| 349 | headers: { |
| 350 | 'Content-Type': 'application/json', |
| 351 | 'X-API-KEY': '${token}' |
| 352 | }, |
| 353 | body: JSON.stringify({ |
| 354 | message: "Please analyze this file:", |
| 355 | attachments: [ |
| 356 | { |
| 357 | filename: "document.txt", |
| 358 | base64: base64Content |
| 359 | } |
| 360 | ], |
| 361 | lifetime_hours: 12 |
| 362 | }) |
| 363 | }); |
| 364 | |
| 365 | const data = await response.json(); |
| 366 | |
| 367 | if (response.ok) { |
| 368 | console.log('✅ File sent successfully!'); |
| 369 | console.log('Response:', data.response); |
| 370 | return data; |
| 371 | } else { |
| 372 | console.error('❌ Error:', data.error); |
| 373 | return null; |
| 374 | } |
| 375 | } catch (error) { |
| 376 | console.error('❌ Request failed:', error); |
| 377 | return null; |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | // Call the function |
| 382 | sendWithAttachment();`; |
| 383 | |
| 384 | // Log GET example |
| 385 | const logGetExample = `// Get logs using GET request |
| 386 | async function getLogsGET(contextId, length = 50) { |
| 387 | try { |
| 388 | const params = new URLSearchParams({ |
| 389 | context_id: contextId, |
| 390 | length: length.toString() |
| 391 | }); |
| 392 | |
| 393 | const response = await fetch('${url}/api/api_log_get?' + params, { |
| 394 | method: 'GET', |
| 395 | headers: { |
| 396 | 'X-API-KEY': '${token}' |
| 397 | } |
| 398 | }); |
| 399 | |
| 400 | const data = await response.json(); |
| 401 | |
| 402 | if (response.ok) { |
| 403 | console.log('✅ Logs retrieved successfully!'); |
| 404 | console.log('Total items:', data.log.total_items); |
| 405 | console.log('Returned items:', data.log.returned_items); |
| 406 | console.log('Log items:', data.log.items); |
| 407 | return data; |
| 408 | } else { |
| 409 | console.error('❌ Error:', data.error); |
| 410 | return null; |
| 411 | } |
| 412 | } catch (error) { |
| 413 | console.error('❌ Request failed:', error); |
| 414 | return null; |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | // Example usage |
| 419 | getLogsGET('ctx_abc123', 20);`; |
| 420 | |
| 421 | // Log POST example |
| 422 | const logPostExample = `// Get logs using POST request |
| 423 | async function getLogsPOST(contextId, length = 50) { |
| 424 | try { |
| 425 | const response = await fetch('${url}/api/api_log_get', { |
| 426 | method: 'POST', |
| 427 | headers: { |
| 428 | 'Content-Type': 'application/json', |
| 429 | 'X-API-KEY': '${token}' |
| 430 | }, |
| 431 | body: JSON.stringify({ |
| 432 | context_id: contextId, |
| 433 | length: length |
| 434 | }) |
| 435 | }); |
| 436 | |
| 437 | const data = await response.json(); |
| 438 | |
| 439 | if (response.ok) { |
| 440 | console.log('✅ Logs retrieved successfully!'); |
| 441 | console.log('Context ID:', data.context_id); |
| 442 | console.log('Log GUID:', data.log.guid); |
| 443 | console.log('Total items:', data.log.total_items); |
| 444 | console.log('Returned items:', data.log.returned_items); |
| 445 | console.log('Start position:', data.log.start_position); |
| 446 | console.log('Progress:', data.log.progress); |
| 447 | console.log('Log items:', data.log.items); |
| 448 | return data; |
| 449 | } else { |
| 450 | console.error('❌ Error:', data.error); |
| 451 | return null; |
| 452 | } |
| 453 | } catch (error) { |
| 454 | console.error('❌ Request failed:', error); |
| 455 | return null; |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | // Example usage - get latest 10 log entries |
| 460 | getLogsPOST('ctx_abc123', 10);`; |
| 461 | |
| 462 | // Terminate chat example |
| 463 | const terminateExample = `// Basic terminate chat function |
| 464 | async function terminateChat(contextId) { |
| 465 | try { |
| 466 | const response = await fetch('${url}/api/api_terminate_chat', { |
| 467 | method: 'POST', |
| 468 | headers: { |
| 469 | 'Content-Type': 'application/json', |
| 470 | 'X-API-KEY': '${token}' |
| 471 | }, |
| 472 | body: JSON.stringify({ |
| 473 | context_id: contextId |
| 474 | }) |
| 475 | }); |
| 476 | |
| 477 | const data = await response.json(); |
| 478 | |
| 479 | if (response.ok) { |
| 480 | console.log('✅ Chat deleted successfully!'); |
| 481 | console.log('Message:', data.message); |
| 482 | return data; |
| 483 | } else { |
| 484 | console.error('❌ Error:', data.error); |
| 485 | return null; |
| 486 | } |
| 487 | } catch (error) { |
| 488 | console.error('❌ Request failed:', error); |
| 489 | return null; |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | // Example 1: Terminate a specific chat |
| 494 | terminateChat('ctx_abc123'); |
| 495 | |
| 496 | // Example 2: Complete workflow - send message, then terminate |
| 497 | async function simpleWorkflow() { |
| 498 | // Send a message |
| 499 | const result = await sendMessage(); |
| 500 | |
| 501 | if (result && result.context_id) { |
| 502 | console.log('Chat created:', result.context_id); |
| 503 | |
| 504 | // Do some work with the chat... |
| 505 | // await continueConversation(result.context_id); |
| 506 | |
| 507 | // Clean up when done |
| 508 | await terminateChat(result.context_id); |
| 509 | console.log('Chat cleaned up'); |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | // Run the workflow |
| 514 | simpleWorkflow();`; |
| 515 | |
| 516 | // Reset chat example |
| 517 | const resetExample = `// Basic reset chat function |
| 518 | async function resetChat(contextId) { |
| 519 | try { |
| 520 | const response = await fetch('${url}/api/api_reset_chat', { |
| 521 | method: 'POST', |
| 522 | headers: { |
| 523 | 'Content-Type': 'application/json', |
| 524 | 'X-API-KEY': '${token}' |
| 525 | }, |
| 526 | body: JSON.stringify({ |
| 527 | context_id: contextId |
| 528 | }) |
| 529 | }); |
| 530 | |
| 531 | const data = await response.json(); |
| 532 | |
| 533 | if (response.ok) { |
| 534 | console.log('✅ Chat reset successfully!'); |
| 535 | console.log('Message:', data.message); |
| 536 | console.log('Context ID:', data.context_id); |
| 537 | return data; |
| 538 | } else { |
| 539 | console.error('❌ Error:', data.error); |
| 540 | return null; |
| 541 | } |
| 542 | } catch (error) { |
| 543 | console.error('❌ Request failed:', error); |
| 544 | return null; |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | // Example 1: Reset a specific chat |
| 549 | resetChat('ctx_abc123'); |
| 550 | |
| 551 | // Example 2: Reset and continue conversation |
| 552 | async function resetAndContinue() { |
| 553 | const contextId = 'ctx_abc123'; |
| 554 | |
| 555 | // Reset the chat to clear history |
| 556 | const resetResult = await resetChat(contextId); |
| 557 | |
| 558 | if (resetResult) { |
| 559 | console.log('Chat reset, starting fresh conversation...'); |
| 560 | |
| 561 | // Continue with same context_id but fresh history |
| 562 | const response = await fetch('${url}/api/api_message', { |
| 563 | method: 'POST', |
| 564 | headers: { |
| 565 | 'Content-Type': 'application/json', |
| 566 | 'X-API-KEY': '${token}' |
| 567 | }, |
| 568 | body: JSON.stringify({ |
| 569 | context_id: contextId, // Same context ID |
| 570 | message: "Hello, this is a fresh start!", |
| 571 | lifetime_hours: 24 |
| 572 | }) |
| 573 | }); |
| 574 | |
| 575 | const data = await response.json(); |
| 576 | console.log('New conversation started:', data.response); |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | // Run the example |
| 581 | resetAndContinue();`; |
| 582 | |
| 583 | // Files get example |
| 584 | const filesGetExample = `// Basic file retrieval |
| 585 | async function getFiles(filePaths) { |
| 586 | try { |
| 587 | const response = await fetch('${url}/api/api_files_get', { |
| 588 | method: 'POST', |
| 589 | headers: { |
| 590 | 'Content-Type': 'application/json', |
| 591 | 'X-API-KEY': '${token}' |
| 592 | }, |
| 593 | body: JSON.stringify({ |
| 594 | paths: filePaths |
| 595 | }) |
| 596 | }); |
| 597 | |
| 598 | const data = await response.json(); |
| 599 | |
| 600 | if (response.ok) { |
| 601 | console.log('✅ Files retrieved successfully!'); |
| 602 | console.log('Retrieved files:', Object.keys(data)); |
| 603 | |
| 604 | // Convert base64 back to text for display |
| 605 | for (const [filename, base64Content] of Object.entries(data)) { |
| 606 | try { |
| 607 | const textContent = atob(base64Content); |
| 608 | console.log(\`\${filename}: \${textContent.substring(0, 100)}...\`); |
| 609 | } catch (e) { |
| 610 | console.log(\`\${filename}: Binary file (\${base64Content.length} chars)\`); |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | return data; |
| 615 | } else { |
| 616 | console.error('❌ Error:', data.error); |
| 617 | return null; |
| 618 | } |
| 619 | } catch (error) { |
| 620 | console.error('❌ Request failed:', error); |
| 621 | return null; |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | // Example 1: Get specific files |
| 626 | const filePaths = [ |
| 627 | "/a0/usr/uploads/document.txt", |
| 628 | "/a0/usr/uploads/data.json" |
| 629 | ]; |
| 630 | getFiles(filePaths); |
| 631 | |
| 632 | // Example 2: Complete attachment workflow |
| 633 | async function attachmentWorkflow() { |
| 634 | // Step 1: Send message with attachments |
| 635 | const messageResponse = await fetch('${url}/api/api_message', { |
| 636 | method: 'POST', |
| 637 | headers: { |
| 638 | 'Content-Type': 'application/json', |
| 639 | 'X-API-KEY': '${token}' |
| 640 | }, |
| 641 | body: JSON.stringify({ |
| 642 | message: "Please analyze this file", |
| 643 | attachments: [{ |
| 644 | filename: "test.txt", |
| 645 | base64: btoa("Hello, this is test content!") |
| 646 | }], |
| 647 | lifetime_hours: 1 |
| 648 | }) |
| 649 | }); |
| 650 | |
| 651 | if (messageResponse.ok) { |
| 652 | console.log('Message sent with attachment'); |
| 653 | |
| 654 | // Step 2: Retrieve the uploaded file |
| 655 | const retrievedFiles = await getFiles(["/a0/usr/uploads/test.txt"]); |
| 656 | |
| 657 | if (retrievedFiles && retrievedFiles["test.txt"]) { |
| 658 | const originalContent = atob(retrievedFiles["test.txt"]); |
| 659 | console.log('Retrieved content:', originalContent); |
| 660 | } |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | // Run the complete workflow |
| 665 | attachmentWorkflow();`; |
| 666 | |
| 667 | // Project usage example |
| 668 | const projectExample = `// Working with projects |
| 669 | async function sendMessageWithProject() { |
| 670 | try { |
| 671 | // First message - activate project |
| 672 | const response = await fetch('${url}/api/api_message', { |
| 673 | method: 'POST', |
| 674 | headers: { |
| 675 | 'Content-Type': 'application/json', |
| 676 | 'X-API-KEY': '${token}' |
| 677 | }, |
| 678 | body: JSON.stringify({ |
| 679 | message: "Analyze the project structure", |
| 680 | project_name: "my-web-app" // Activates this project |
| 681 | }) |
| 682 | }); |
| 683 | |
| 684 | const data = await response.json(); |
| 685 | |
| 686 | if (response.ok) { |
| 687 | console.log('✅ Project activated!'); |
| 688 | console.log('Context ID:', data.context_id); |
| 689 | console.log('Response:', data.response); |
| 690 | |
| 691 | // Continue conversation - project already set |
| 692 | const followUp = await fetch('${url}/api/api_message', { |
| 693 | method: 'POST', |
| 694 | headers: { |
| 695 | 'Content-Type': 'application/json', |
| 696 | 'X-API-KEY': '${token}' |
| 697 | }, |
| 698 | body: JSON.stringify({ |
| 699 | context_id: data.context_id, |
| 700 | message: "What files are in the project?" |
| 701 | // Do NOT include project_name here - already set on first message |
| 702 | }) |
| 703 | }); |
| 704 | |
| 705 | const followUpData = await followUp.json(); |
| 706 | console.log('Follow-up response:', followUpData.response); |
| 707 | return followUpData; |
| 708 | } else { |
| 709 | console.error('❌ Error:', data.error); |
| 710 | return null; |
| 711 | } |
| 712 | } catch (error) { |
| 713 | console.error('❌ Request failed:', error); |
| 714 | return null; |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | // Call the function |
| 719 | sendMessageWithProject();`; |
| 720 | |
| 721 | return [ |
| 722 | { id: "api-basic-example", content: basicExample }, |
| 723 | { id: "api-continuation-example", content: continuationExample }, |
| 724 | { id: "api-attachment-example", content: attachmentExample }, |
| 725 | { id: "api-project-example", content: projectExample }, |
| 726 | { id: "api-log-get-example", content: logGetExample }, |
| 727 | { id: "api-log-post-example", content: logPostExample }, |
| 728 | { id: "api-terminate-example", content: terminateExample }, |
| 729 | { id: "api-reset-example", content: resetExample }, |
| 730 | { id: "api-files-get-example", content: filesGetExample } |
| 731 | ]; |
| 732 | } |
| 733 | |
| 734 | window.destroyExternalApiExamples = destroyEditors; |
| 735 | |
| 736 | window.initExternalApiExamples = async (root) => { |
| 737 | if (!root || !window.ace) return; |
| 738 | |
| 739 | destroyEditors(root); |
| 740 | const examples = await buildExamples(); |
| 741 | if (!root.isConnected) return; |
| 742 | |
| 743 | const editors = []; |
| 744 | |
| 745 | examples.forEach(({ id, content }) => { |
| 746 | const container = root.querySelector(`#${id}`); |
| 747 | if (!container) return; |
| 748 | container.textContent = ""; |
| 749 | |
| 750 | const editor = ace.edit(container); |
| 751 | const dark = localStorage.getItem("darkMode"); |
| 752 | if (dark != "false") { |
| 753 | editor.setTheme("ace/theme/github_dark"); |
| 754 | } else { |
| 755 | editor.setTheme("ace/theme/tomorrow"); |
| 756 | } |
| 757 | editor.session.setMode("ace/mode/javascript"); |
| 758 | editor.setValue(content, -1); |
| 759 | editor.setReadOnly(true); |
| 760 | editor.renderer.setShowGutter(true); |
| 761 | editor.renderer.setPadding(8); |
| 762 | editors.push(editor); |
| 763 | }); |
| 764 | |
| 765 | scheduleEditorResize(root, editors); |
| 766 | }; |
| 767 | </script> |
| 768 | </div> |
| 769 | |
| 770 | <style> |
| 771 | #api-basic-example, |
| 772 | #api-continuation-example, |
| 773 | #api-attachment-example, |
| 774 | #api-project-example, |
| 775 | #api-log-get-example, |
| 776 | #api-log-post-example, |
| 777 | #api-terminate-example, |
| 778 | #api-reset-example, |
| 779 | #api-files-get-example { |
| 780 | width: 100%; |
| 781 | height: 20em; |
| 782 | margin: 8px 0 16px 0; |
| 783 | border-radius: 4px; |
| 784 | } |
| 785 | |
| 786 | /* Section styling */ |
| 787 | .api-section { |
| 788 | border: 1px solid var(--color-border); |
| 789 | border-radius: 8px; |
| 790 | margin: 20px 0; |
| 791 | padding: 16px; |
| 792 | } |
| 793 | |
| 794 | .api-section h2 { |
| 795 | margin: 0 0 16px 0; |
| 796 | color: var(--color-text-primary); |
| 797 | border-bottom: 1px solid var(--color-border); |
| 798 | padding-bottom: 8px; |
| 799 | } |
| 800 | |
| 801 | .api-section h3 { |
| 802 | margin: 20px 0 12px 0; |
| 803 | color: var(--color-text-primary); |
| 804 | font-size: 1.2em; |
| 805 | } |
| 806 | |
| 807 | .api-section h4 { |
| 808 | margin: 16px 0 8px 0; |
| 809 | color: var(--color-text-primary); |
| 810 | } |
| 811 | </style> |
| 812 | |
| 813 | </body> |
| 814 | |
| 815 | </html> |