Model-config web apps: describe the app in configuration instead of writing code. Built on Polars and DuckDB.
ai_service_builder.html · 26.3 KB · 730 lines
| 1 | <!DOCTYPE html> |
| 2 | <html lang="en"> |
| 3 | <head> |
| 4 | <meta charset="UTF-8"> |
| 5 | <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 6 | <title>DBBasic AI Service Builder - Create Services from Natural Language</title> |
| 7 | <style> |
| 8 | * { |
| 9 | margin: 0; |
| 10 | padding: 0; |
| 11 | box-sizing: border-box; |
| 12 | } |
| 13 | |
| 14 | body { |
| 15 | font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif; |
| 16 | background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); |
| 17 | min-height: 100vh; |
| 18 | padding: 20px; |
| 19 | } |
| 20 | |
| 21 | .container { |
| 22 | max-width: 1200px; |
| 23 | margin: 0 auto; |
| 24 | } |
| 25 | |
| 26 | .header { |
| 27 | text-align: center; |
| 28 | color: white; |
| 29 | margin-bottom: 40px; |
| 30 | } |
| 31 | |
| 32 | .header h1 { |
| 33 | font-size: 48px; |
| 34 | margin-bottom: 10px; |
| 35 | text-shadow: 2px 2px 4px rgba(0,0,0,0.2); |
| 36 | } |
| 37 | |
| 38 | .header p { |
| 39 | font-size: 20px; |
| 40 | opacity: 0.9; |
| 41 | } |
| 42 | |
| 43 | .main-grid { |
| 44 | display: grid; |
| 45 | grid-template-columns: 1fr 1fr; |
| 46 | gap: 20px; |
| 47 | margin-bottom: 40px; |
| 48 | } |
| 49 | |
| 50 | @media (max-width: 768px) { |
| 51 | .main-grid { |
| 52 | grid-template-columns: 1fr; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | .card { |
| 57 | background: white; |
| 58 | border-radius: 12px; |
| 59 | padding: 30px; |
| 60 | box-shadow: 0 10px 30px rgba(0,0,0,0.2); |
| 61 | } |
| 62 | |
| 63 | .form-group { |
| 64 | margin-bottom: 20px; |
| 65 | } |
| 66 | |
| 67 | label { |
| 68 | display: block; |
| 69 | margin-bottom: 8px; |
| 70 | font-weight: 600; |
| 71 | color: #333; |
| 72 | } |
| 73 | |
| 74 | input, textarea, select { |
| 75 | width: 100%; |
| 76 | padding: 12px; |
| 77 | border: 2px solid #e0e0e0; |
| 78 | border-radius: 8px; |
| 79 | font-size: 14px; |
| 80 | transition: border-color 0.3s; |
| 81 | } |
| 82 | |
| 83 | input:focus, textarea:focus, select:focus { |
| 84 | outline: none; |
| 85 | border-color: #667eea; |
| 86 | } |
| 87 | |
| 88 | textarea { |
| 89 | min-height: 100px; |
| 90 | resize: vertical; |
| 91 | } |
| 92 | |
| 93 | .hint { |
| 94 | font-size: 12px; |
| 95 | color: #666; |
| 96 | margin-top: 4px; |
| 97 | } |
| 98 | |
| 99 | .button-group { |
| 100 | display: flex; |
| 101 | gap: 10px; |
| 102 | margin-top: 20px; |
| 103 | } |
| 104 | |
| 105 | button { |
| 106 | padding: 12px 24px; |
| 107 | border: none; |
| 108 | border-radius: 8px; |
| 109 | font-weight: 600; |
| 110 | cursor: pointer; |
| 111 | transition: all 0.3s; |
| 112 | } |
| 113 | |
| 114 | .btn-primary { |
| 115 | background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); |
| 116 | color: white; |
| 117 | flex: 1; |
| 118 | } |
| 119 | |
| 120 | .btn-primary:hover { |
| 121 | transform: translateY(-2px); |
| 122 | box-shadow: 0 6px 20px rgba(102, 126, 234, 0.4); |
| 123 | } |
| 124 | |
| 125 | .btn-secondary { |
| 126 | background: #f0f0f0; |
| 127 | color: #333; |
| 128 | } |
| 129 | |
| 130 | .btn-secondary:hover { |
| 131 | background: #e0e0e0; |
| 132 | } |
| 133 | |
| 134 | .service-list { |
| 135 | max-height: 400px; |
| 136 | overflow-y: auto; |
| 137 | } |
| 138 | |
| 139 | .service-item { |
| 140 | background: #f8f9fa; |
| 141 | border-radius: 8px; |
| 142 | padding: 15px; |
| 143 | margin-bottom: 10px; |
| 144 | cursor: pointer; |
| 145 | transition: all 0.3s; |
| 146 | } |
| 147 | |
| 148 | .service-item:hover { |
| 149 | background: #e9ecef; |
| 150 | transform: translateX(5px); |
| 151 | } |
| 152 | |
| 153 | .service-name { |
| 154 | font-weight: 600; |
| 155 | color: #333; |
| 156 | margin-bottom: 5px; |
| 157 | } |
| 158 | |
| 159 | .service-description { |
| 160 | font-size: 13px; |
| 161 | color: #666; |
| 162 | } |
| 163 | |
| 164 | .service-endpoint { |
| 165 | font-family: monospace; |
| 166 | font-size: 12px; |
| 167 | color: #667eea; |
| 168 | margin-top: 5px; |
| 169 | } |
| 170 | |
| 171 | .status-badge { |
| 172 | display: inline-block; |
| 173 | padding: 4px 8px; |
| 174 | border-radius: 4px; |
| 175 | font-size: 11px; |
| 176 | font-weight: 600; |
| 177 | text-transform: uppercase; |
| 178 | } |
| 179 | |
| 180 | .status-active { |
| 181 | background: #d4edda; |
| 182 | color: #155724; |
| 183 | } |
| 184 | |
| 185 | .status-generating { |
| 186 | background: #fff3cd; |
| 187 | color: #856404; |
| 188 | } |
| 189 | |
| 190 | .status-error { |
| 191 | background: #f8d7da; |
| 192 | color: #721c24; |
| 193 | } |
| 194 | |
| 195 | .code-preview { |
| 196 | background: #1e1e1e; |
| 197 | color: #d4d4d4; |
| 198 | padding: 20px; |
| 199 | border-radius: 8px; |
| 200 | font-family: 'Monaco', 'Menlo', monospace; |
| 201 | font-size: 13px; |
| 202 | overflow-x: auto; |
| 203 | max-height: 400px; |
| 204 | overflow-y: auto; |
| 205 | } |
| 206 | |
| 207 | /* Syntax highlighting classes */ |
| 208 | .code-comment { color: #6a9955; } |
| 209 | .code-keyword { color: #c586c0; } |
| 210 | .code-string { color: #ce9178; } |
| 211 | .code-function { color: #dcdcaa; } |
| 212 | .code-number { color: #b5cea8; } |
| 213 | |
| 214 | .test-panel { |
| 215 | background: #f8f9fa; |
| 216 | border-radius: 8px; |
| 217 | padding: 20px; |
| 218 | margin-top: 20px; |
| 219 | } |
| 220 | |
| 221 | .test-input, .test-output { |
| 222 | background: white; |
| 223 | border: 1px solid #dee2e6; |
| 224 | border-radius: 4px; |
| 225 | padding: 15px; |
| 226 | margin: 10px 0; |
| 227 | font-family: monospace; |
| 228 | font-size: 13px; |
| 229 | white-space: pre-wrap; |
| 230 | } |
| 231 | |
| 232 | .test-output.success { |
| 233 | border-color: #28a745; |
| 234 | background: #d4edda; |
| 235 | } |
| 236 | |
| 237 | .test-output.error { |
| 238 | border-color: #dc3545; |
| 239 | background: #f8d7da; |
| 240 | } |
| 241 | |
| 242 | .loading { |
| 243 | display: inline-block; |
| 244 | width: 20px; |
| 245 | height: 20px; |
| 246 | border: 3px solid rgba(255,255,255,.3); |
| 247 | border-radius: 50%; |
| 248 | border-top-color: white; |
| 249 | animation: spin 1s ease-in-out infinite; |
| 250 | } |
| 251 | |
| 252 | @keyframes spin { |
| 253 | to { transform: rotate(360deg); } |
| 254 | } |
| 255 | |
| 256 | .tabs { |
| 257 | display: flex; |
| 258 | gap: 10px; |
| 259 | margin-bottom: 20px; |
| 260 | border-bottom: 2px solid #e0e0e0; |
| 261 | } |
| 262 | |
| 263 | .tab { |
| 264 | padding: 10px 20px; |
| 265 | background: none; |
| 266 | border: none; |
| 267 | color: #666; |
| 268 | font-weight: 600; |
| 269 | cursor: pointer; |
| 270 | transition: all 0.3s; |
| 271 | } |
| 272 | |
| 273 | .tab.active { |
| 274 | color: #667eea; |
| 275 | border-bottom: 3px solid #667eea; |
| 276 | } |
| 277 | |
| 278 | .tab-content { |
| 279 | display: none; |
| 280 | } |
| 281 | |
| 282 | .tab-content.active { |
| 283 | display: block; |
| 284 | } |
| 285 | |
| 286 | .example-button { |
| 287 | background: #e3f2fd; |
| 288 | color: #1976d2; |
| 289 | padding: 8px 16px; |
| 290 | border-radius: 6px; |
| 291 | border: none; |
| 292 | cursor: pointer; |
| 293 | font-size: 13px; |
| 294 | margin: 5px; |
| 295 | } |
| 296 | |
| 297 | .example-button:hover { |
| 298 | background: #bbdefb; |
| 299 | } |
| 300 | |
| 301 | .stats { |
| 302 | display: grid; |
| 303 | grid-template-columns: repeat(4, 1fr); |
| 304 | gap: 15px; |
| 305 | margin-top: 20px; |
| 306 | } |
| 307 | |
| 308 | .stat-card { |
| 309 | background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); |
| 310 | color: white; |
| 311 | padding: 15px; |
| 312 | border-radius: 8px; |
| 313 | text-align: center; |
| 314 | } |
| 315 | |
| 316 | .stat-value { |
| 317 | font-size: 24px; |
| 318 | font-weight: bold; |
| 319 | } |
| 320 | |
| 321 | .stat-label { |
| 322 | font-size: 12px; |
| 323 | opacity: 0.9; |
| 324 | margin-top: 5px; |
| 325 | } |
| 326 | </style> |
| 327 | </head> |
| 328 | <body> |
| 329 | <div class="container"> |
| 330 | <div class="header"> |
| 331 | <h1>🚀 DBBasic AI Service Builder</h1> |
| 332 | <p>Create web services instantly from natural language descriptions</p> |
| 333 | <p style="font-size: 14px; margin-top: 10px; opacity: 0.8;">Nobody else does this - just describe what you want, get a running service!</p> |
| 334 | </div> |
| 335 | |
| 336 | <div class="main-grid"> |
| 337 | <!-- Service Creator --> |
| 338 | <div class="card"> |
| 339 | <h2 style="margin-bottom: 20px;">✨ Create New Service</h2> |
| 340 | |
| 341 | <div class="form-group"> |
| 342 | <label>Service Name</label> |
| 343 | <input type="text" id="serviceName" placeholder="e.g., calculate_shipping_cost"> |
| 344 | <div class="hint">Use snake_case. This becomes your endpoint: /ai/your_service_name</div> |
| 345 | </div> |
| 346 | |
| 347 | <div class="form-group"> |
| 348 | <label>Natural Language Description</label> |
| 349 | <textarea id="serviceDescription" placeholder="Describe what your service should do in plain English..."></textarea> |
| 350 | <div class="hint">Be specific! AI will generate the implementation from this description.</div> |
| 351 | </div> |
| 352 | |
| 353 | <div class="form-group"> |
| 354 | <label>Inputs (comma-separated)</label> |
| 355 | <input type="text" id="serviceInputs" placeholder="e.g., weight, zip_code, shipping_speed"> |
| 356 | </div> |
| 357 | |
| 358 | <div class="form-group"> |
| 359 | <label>Outputs (comma-separated)</label> |
| 360 | <input type="text" id="serviceOutputs" placeholder="e.g., shipping_cost, estimated_days, carrier"> |
| 361 | </div> |
| 362 | |
| 363 | <div style="margin: 20px 0;"> |
| 364 | <strong>Example Descriptions:</strong> |
| 365 | <div style="display: flex; flex-wrap: wrap;"> |
| 366 | <button class="example-button" onclick="loadExample('shipping')">Shipping Calculator</button> |
| 367 | <button class="example-button" onclick="loadExample('tax')">Tax Calculator</button> |
| 368 | <button class="example-button" onclick="loadExample('fraud')">Fraud Detection</button> |
| 369 | <button class="example-button" onclick="loadExample('email')">Email Validator</button> |
| 370 | </div> |
| 371 | </div> |
| 372 | |
| 373 | <div class="button-group"> |
| 374 | <button class="btn-secondary" onclick="testWithMockData()">Test with Mock Data</button> |
| 375 | <button class="btn-primary" onclick="createService()"> |
| 376 | <span id="createButtonText">🎯 Generate Service with AI</span> |
| 377 | <span id="createButtonLoading" style="display: none;" class="loading"></span> |
| 378 | </button> |
| 379 | </div> |
| 380 | </div> |
| 381 | |
| 382 | <!-- Service List --> |
| 383 | <div class="card"> |
| 384 | <h2 style="margin-bottom: 20px;">📦 Active Services</h2> |
| 385 | |
| 386 | <div class="service-list" id="serviceList"> |
| 387 | <!-- Services will be loaded here --> |
| 388 | </div> |
| 389 | |
| 390 | <div class="stats"> |
| 391 | <div class="stat-card"> |
| 392 | <div class="stat-value" id="totalServices">0</div> |
| 393 | <div class="stat-label">Total Services</div> |
| 394 | </div> |
| 395 | <div class="stat-card"> |
| 396 | <div class="stat-value" id="totalRequests">0</div> |
| 397 | <div class="stat-label">Total Requests</div> |
| 398 | </div> |
| 399 | <div class="stat-card"> |
| 400 | <div class="stat-value" id="avgResponse">0ms</div> |
| 401 | <div class="stat-label">Avg Response</div> |
| 402 | </div> |
| 403 | <div class="stat-card"> |
| 404 | <div class="stat-value">100%</div> |
| 405 | <div class="stat-label">Uptime</div> |
| 406 | </div> |
| 407 | </div> |
| 408 | </div> |
| 409 | </div> |
| 410 | |
| 411 | <!-- Test & Code Panel --> |
| 412 | <div class="card"> |
| 413 | <div class="tabs"> |
| 414 | <button class="tab active" onclick="switchTab('test')">🧪 Test Service</button> |
| 415 | <button class="tab" onclick="switchTab('code')">📝 Generated Code</button> |
| 416 | </div> |
| 417 | |
| 418 | <div id="testTab" class="tab-content active"> |
| 419 | <div class="form-group"> |
| 420 | <label>Select Service to Test</label> |
| 421 | <select id="testServiceSelect"> |
| 422 | <option value="">Choose a service...</option> |
| 423 | </select> |
| 424 | </div> |
| 425 | |
| 426 | <div class="form-group"> |
| 427 | <label>Test Data (JSON)</label> |
| 428 | <textarea id="testInput" class="test-input" rows="6">{}</textarea> |
| 429 | </div> |
| 430 | |
| 431 | <button class="btn-primary" onclick="runTest()" style="width: 200px;">▶️ Run Test</button> |
| 432 | |
| 433 | <div id="testResult" style="display: none;"> |
| 434 | <h3 style="margin: 20px 0 10px;">Result:</h3> |
| 435 | <div id="testOutput" class="test-output"></div> |
| 436 | </div> |
| 437 | </div> |
| 438 | |
| 439 | <div id="codeTab" class="tab-content"> |
| 440 | <div class="form-group"> |
| 441 | <label>Select Service to View Code</label> |
| 442 | <select id="codeServiceSelect"> |
| 443 | <option value="">Choose a service...</option> |
| 444 | </select> |
| 445 | </div> |
| 446 | |
| 447 | <div id="codePreview" class="code-preview" style="display: none;"> |
| 448 | <!-- Code will be displayed here --> |
| 449 | </div> |
| 450 | </div> |
| 451 | </div> |
| 452 | </div> |
| 453 | |
| 454 | <script> |
| 455 | // Example service descriptions |
| 456 | const examples = { |
| 457 | shipping: { |
| 458 | name: 'calculate_shipping_cost', |
| 459 | description: 'Calculate shipping cost based on weight, dimensions, destination zip code, and shipping speed. Use USPS rates for standard, UPS for express. Add $5 handling fee for fragile items. Free shipping for orders over $100.', |
| 460 | inputs: 'weight, dimensions, zip_code, shipping_speed, is_fragile, order_total', |
| 461 | outputs: 'shipping_cost, carrier, estimated_days, free_shipping_applied' |
| 462 | }, |
| 463 | tax: { |
| 464 | name: 'calculate_sales_tax', |
| 465 | description: 'Calculate sales tax based on customer location (state), product category, and subtotal. Apply correct state tax rates. Handle tax-exempt customers. Include local city taxes where applicable.', |
| 466 | inputs: 'subtotal, state, city, product_category, is_tax_exempt', |
| 467 | outputs: 'tax_amount, tax_rate, total_with_tax, tax_breakdown' |
| 468 | }, |
| 469 | fraud: { |
| 470 | name: 'detect_fraud', |
| 471 | description: 'Analyze order for fraud risk based on order amount, customer history, shipping address, billing address mismatch, and velocity of orders. Return risk score and recommended action.', |
| 472 | inputs: 'order_amount, customer_id, shipping_address, billing_address, previous_orders_count', |
| 473 | outputs: 'risk_score, risk_level, recommended_action, risk_factors' |
| 474 | }, |
| 475 | email: { |
| 476 | name: 'validate_email', |
| 477 | description: 'Validate email address format, check for disposable email domains, verify MX records exist for the domain. Return validation status and any issues found.', |
| 478 | inputs: 'email', |
| 479 | outputs: 'is_valid, issues, domain_type, mx_records_exist' |
| 480 | } |
| 481 | }; |
| 482 | |
| 483 | // Load example into form |
| 484 | function loadExample(type) { |
| 485 | const example = examples[type]; |
| 486 | document.getElementById('serviceName').value = example.name; |
| 487 | document.getElementById('serviceDescription').value = example.description; |
| 488 | document.getElementById('serviceInputs').value = example.inputs; |
| 489 | document.getElementById('serviceOutputs').value = example.outputs; |
| 490 | } |
| 491 | |
| 492 | // Create new service |
| 493 | async function createService() { |
| 494 | const name = document.getElementById('serviceName').value.trim(); |
| 495 | const description = document.getElementById('serviceDescription').value.trim(); |
| 496 | const inputs = document.getElementById('serviceInputs').value.split(',').map(s => s.trim()).filter(s => s); |
| 497 | const outputs = document.getElementById('serviceOutputs').value.split(',').map(s => s.trim()).filter(s => s); |
| 498 | |
| 499 | if (!name || !description || inputs.length === 0 || outputs.length === 0) { |
| 500 | alert('Please fill in all fields'); |
| 501 | return; |
| 502 | } |
| 503 | |
| 504 | // Show loading |
| 505 | document.getElementById('createButtonText').style.display = 'none'; |
| 506 | document.getElementById('createButtonLoading').style.display = 'inline-block'; |
| 507 | |
| 508 | try { |
| 509 | const response = await fetch('/api/services/create', { |
| 510 | method: 'POST', |
| 511 | headers: { 'Content-Type': 'application/json' }, |
| 512 | body: JSON.stringify({ name, description, inputs, outputs }) |
| 513 | }); |
| 514 | |
| 515 | if (!response.ok) throw new Error('Failed to create service'); |
| 516 | |
| 517 | const service = await response.json(); |
| 518 | alert(`✅ Service created successfully!\n\nEndpoint: /ai/${service.name}\n\nYou can now test it or use it in your application!`); |
| 519 | |
| 520 | // Clear form |
| 521 | document.getElementById('serviceName').value = ''; |
| 522 | document.getElementById('serviceDescription').value = ''; |
| 523 | document.getElementById('serviceInputs').value = ''; |
| 524 | document.getElementById('serviceOutputs').value = ''; |
| 525 | |
| 526 | // Refresh service list |
| 527 | loadServices(); |
| 528 | } catch (error) { |
| 529 | alert('❌ Error creating service: ' + error.message); |
| 530 | } finally { |
| 531 | // Hide loading |
| 532 | document.getElementById('createButtonText').style.display = 'inline'; |
| 533 | document.getElementById('createButtonLoading').style.display = 'none'; |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | // Load services list |
| 538 | async function loadServices() { |
| 539 | try { |
| 540 | const response = await fetch('/api/services'); |
| 541 | const services = await response.json(); |
| 542 | |
| 543 | // Update service list |
| 544 | const listHtml = services.map(service => ` |
| 545 | <div class="service-item" onclick="selectService('${service.name}')"> |
| 546 | <div class="service-name">${service.name}</div> |
| 547 | <div class="service-description">${service.description}</div> |
| 548 | <div class="service-endpoint">/ai/${service.name}</div> |
| 549 | <span class="status-badge status-${service.status}">${service.status}</span> |
| 550 | </div> |
| 551 | `).join(''); |
| 552 | |
| 553 | document.getElementById('serviceList').innerHTML = listHtml || '<p style="color: #666;">No services yet. Create your first one!</p>'; |
| 554 | |
| 555 | // Update dropdowns |
| 556 | const options = '<option value="">Choose a service...</option>' + |
| 557 | services.map(s => `<option value="${s.name}">${s.name}</option>`).join(''); |
| 558 | document.getElementById('testServiceSelect').innerHTML = options; |
| 559 | document.getElementById('codeServiceSelect').innerHTML = options; |
| 560 | |
| 561 | // Update stats |
| 562 | document.getElementById('totalServices').textContent = services.length; |
| 563 | |
| 564 | // Calculate total requests |
| 565 | const totalRequests = services.reduce((sum, s) => sum + (s.metrics?.requests || 0), 0); |
| 566 | document.getElementById('totalRequests').textContent = totalRequests; |
| 567 | |
| 568 | } catch (error) { |
| 569 | console.error('Error loading services:', error); |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | // Select a service |
| 574 | function selectService(name) { |
| 575 | document.getElementById('testServiceSelect').value = name; |
| 576 | document.getElementById('codeServiceSelect').value = name; |
| 577 | loadServiceCode(name); |
| 578 | } |
| 579 | |
| 580 | // Load service code |
| 581 | async function loadServiceCode(name) { |
| 582 | if (!name) return; |
| 583 | |
| 584 | try { |
| 585 | const response = await fetch(`/api/services/${name}/code`); |
| 586 | const data = await response.json(); |
| 587 | |
| 588 | // Display code with proper syntax highlighting |
| 589 | let highlightedCode = data.code; |
| 590 | |
| 591 | // Escape HTML entities first |
| 592 | highlightedCode = highlightedCode |
| 593 | .replace(/&/g, '&') |
| 594 | .replace(/</g, '<') |
| 595 | .replace(/>/g, '>'); |
| 596 | |
| 597 | // Apply syntax highlighting using CSS classes |
| 598 | // Comments (including docstrings) |
| 599 | highlightedCode = highlightedCode.replace(/"""[\s\S]*?"""/g, match => |
| 600 | `<span class="code-comment">${match}</span>`); |
| 601 | highlightedCode = highlightedCode.replace(/#.*$/gm, match => |
| 602 | `<span class="code-comment">${match}</span>`); |
| 603 | |
| 604 | // Keywords |
| 605 | highlightedCode = highlightedCode.replace(/\b(def|async|await|return|if|else|elif|try|except|import|from|for|while|in|class|True|False|None|and|or|not|as|with|raise|pass|break|continue)\b/g, match => |
| 606 | `<span class="code-keyword">${match}</span>`); |
| 607 | |
| 608 | // Strings |
| 609 | highlightedCode = highlightedCode.replace(/(["'])(?:(?=(\\?))\2.)*?\1/g, match => |
| 610 | `<span class="code-string">${match}</span>`); |
| 611 | |
| 612 | // Functions |
| 613 | highlightedCode = highlightedCode.replace(/\b([a-zA-Z_][a-zA-Z0-9_]*)\s*(?=\()/g, (match, funcName) => { |
| 614 | // Skip if it's a keyword |
| 615 | if (['if', 'for', 'while', 'except', 'with'].includes(funcName)) { |
| 616 | return match; |
| 617 | } |
| 618 | return `<span class="code-function">${funcName}</span>(`; |
| 619 | }); |
| 620 | |
| 621 | // Numbers |
| 622 | highlightedCode = highlightedCode.replace(/\b(\d+\.?\d*)\b/g, match => |
| 623 | `<span class="code-number">${match}</span>`); |
| 624 | |
| 625 | document.getElementById('codePreview').innerHTML = `<pre style="margin: 0; white-space: pre-wrap; word-wrap: break-word;">${highlightedCode}</pre>`; |
| 626 | document.getElementById('codePreview').style.display = 'block'; |
| 627 | } catch (error) { |
| 628 | console.error('Error loading code:', error); |
| 629 | document.getElementById('codePreview').innerHTML = '<pre style="color: #ff6b6b;">Error loading code</pre>'; |
| 630 | document.getElementById('codePreview').style.display = 'block'; |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | // Run test |
| 635 | async function runTest() { |
| 636 | const serviceName = document.getElementById('testServiceSelect').value; |
| 637 | if (!serviceName) { |
| 638 | alert('Please select a service to test'); |
| 639 | return; |
| 640 | } |
| 641 | |
| 642 | const testData = document.getElementById('testInput').value; |
| 643 | let parsedData; |
| 644 | |
| 645 | try { |
| 646 | parsedData = JSON.parse(testData); |
| 647 | } catch (error) { |
| 648 | alert('Invalid JSON in test data'); |
| 649 | return; |
| 650 | } |
| 651 | |
| 652 | try { |
| 653 | const response = await fetch(`/api/services/${serviceName}/test`, { |
| 654 | method: 'POST', |
| 655 | headers: { 'Content-Type': 'application/json' }, |
| 656 | body: JSON.stringify(parsedData) |
| 657 | }); |
| 658 | |
| 659 | const result = await response.json(); |
| 660 | |
| 661 | document.getElementById('testOutput').textContent = JSON.stringify(result, null, 2); |
| 662 | document.getElementById('testOutput').className = result.success ? 'test-output success' : 'test-output error'; |
| 663 | document.getElementById('testResult').style.display = 'block'; |
| 664 | } catch (error) { |
| 665 | document.getElementById('testOutput').textContent = 'Error: ' + error.message; |
| 666 | document.getElementById('testOutput').className = 'test-output error'; |
| 667 | document.getElementById('testResult').style.display = 'block'; |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | // Test with mock data |
| 672 | function testWithMockData() { |
| 673 | const name = document.getElementById('serviceName').value; |
| 674 | const inputs = document.getElementById('serviceInputs').value.split(',').map(s => s.trim()).filter(s => s); |
| 675 | |
| 676 | // Generate mock data based on input names |
| 677 | const mockData = {}; |
| 678 | inputs.forEach(input => { |
| 679 | if (input.includes('weight')) mockData[input] = 2.5; |
| 680 | else if (input.includes('amount') || input.includes('total')) mockData[input] = 99.99; |
| 681 | else if (input.includes('id')) mockData[input] = '12345'; |
| 682 | else if (input.includes('email')) mockData[input] = 'test@example.com'; |
| 683 | else if (input.includes('phone')) mockData[input] = '555-1234'; |
| 684 | else if (input.includes('zip') || input.includes('postal')) mockData[input] = '90210'; |
| 685 | else if (input.includes('state')) mockData[input] = 'CA'; |
| 686 | else if (input.includes('speed')) mockData[input] = 'standard'; |
| 687 | else if (input.includes('is_') || input.includes('has_')) mockData[input] = false; |
| 688 | else mockData[input] = 'sample_value'; |
| 689 | }); |
| 690 | |
| 691 | document.getElementById('testInput').value = JSON.stringify(mockData, null, 2); |
| 692 | switchTab('test'); |
| 693 | } |
| 694 | |
| 695 | // Switch tabs |
| 696 | function switchTab(tabName) { |
| 697 | // Update tab buttons |
| 698 | document.querySelectorAll('.tab').forEach(tab => { |
| 699 | tab.classList.remove('active'); |
| 700 | }); |
| 701 | event.target.classList.add('active'); |
| 702 | |
| 703 | // Update tab content |
| 704 | document.querySelectorAll('.tab-content').forEach(content => { |
| 705 | content.classList.remove('active'); |
| 706 | }); |
| 707 | document.getElementById(tabName + 'Tab').classList.add('active'); |
| 708 | |
| 709 | // Load code if switching to code tab |
| 710 | if (tabName === 'code') { |
| 711 | const selectedService = document.getElementById('codeServiceSelect').value; |
| 712 | if (selectedService) { |
| 713 | loadServiceCode(selectedService); |
| 714 | } |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | // Listen for dropdown changes |
| 719 | document.getElementById('codeServiceSelect').addEventListener('change', (e) => { |
| 720 | loadServiceCode(e.target.value); |
| 721 | }); |
| 722 | |
| 723 | // Load services on page load |
| 724 | loadServices(); |
| 725 | |
| 726 | // Refresh services every 5 seconds |
| 727 | setInterval(loadServices, 5000); |
| 728 | </script> |
| 729 | </body> |
| 730 | </html> |