File size: 27,338 Bytes
ae99871
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
import { app } from "../../scripts/app.js";
import { api } from "../../scripts/api.js";

// ComfyUI RunpodDirect Extension
// Version: 1.0.6
console.log('[RunpodDirect] v1.0.6');

// Track download states
const downloadStates = new Map();
let downloadQueue = [];
let isDownloadingAll = false;
let completedDownloads = 0;
let totalDownloads = 0;
let downloadStartTimes = new Map();

// Format bytes to human readable
function formatBytes(bytes) {
    if (bytes === 0) return '0 B';
    const k = 1024;
    const sizes = ['B', 'KB', 'MB', 'GB'];
    const i = Math.floor(Math.log(bytes) / Math.log(k));
    return Math.round((bytes / Math.pow(k, i)) * 100) / 100 + ' ' + sizes[i];
}

// Calculate download speed
function calculateSpeed(downloadId, downloaded) {
    const startTime = downloadStartTimes.get(downloadId);
    if (!startTime) return '0 MB/s';

    const elapsedSeconds = (Date.now() - startTime) / 1000;
    if (elapsedSeconds < 1) return 'Calculating...';

    const bytesPerSecond = downloaded / elapsedSeconds;
    return formatBytes(bytesPerSecond) + '/s';
}

// Listen for server download events
api.addEventListener("server_download_progress", ({ detail }) => {
    const { download_id, progress, downloaded, total } = detail;

    if (!downloadStartTimes.has(download_id)) {
        downloadStartTimes.set(download_id, Date.now());
    }

    const speed = calculateSpeed(download_id, downloaded);

    downloadStates.set(download_id, {
        status: 'downloading',
        progress,
        downloaded,
        total,
        speed
    });

    // Trigger update event for UI components
    window.dispatchEvent(new CustomEvent('serverDownloadUpdate', {
        detail: { download_id, ...downloadStates.get(download_id) }
    }));
});

api.addEventListener("server_download_complete", ({ detail }) => {
    const { download_id, path, size } = detail;

    // Increment counter BEFORE updating UI
    if (isDownloadingAll) {
        completedDownloads++;
        console.log(`[RunpodDirect] Progress: ${completedDownloads}/${totalDownloads} completed`);
    }

    downloadStates.set(download_id, {
        status: 'completed',
        progress: 100,
        path,
        size
    });

    window.dispatchEvent(new CustomEvent('serverDownloadUpdate', {
        detail: { download_id, ...downloadStates.get(download_id) }
    }));

    console.log(`Download completed: ${download_id} -> ${path}`);

    // Check if all downloads are done
    if (isDownloadingAll && completedDownloads >= totalDownloads) {
        console.log('[RunpodDirect] All downloads completed!');
        isDownloadingAll = false;
        showRefreshPrompt();
    }
});

api.addEventListener("server_download_error", ({ detail }) => {
    const { download_id, error } = detail;

    // Increment counter BEFORE updating UI
    if (isDownloadingAll) {
        completedDownloads++;
        console.log(`[RunpodDirect] Progress: ${completedDownloads}/${totalDownloads} completed (1 error)`);
    }

    downloadStates.set(download_id, {
        status: 'error',
        error
    });

    window.dispatchEvent(new CustomEvent('serverDownloadUpdate', {
        detail: { download_id, ...downloadStates.get(download_id) }
    }));

    console.error(`Download error: ${download_id} - ${error}`);

    // Check if all downloads are done (including failed ones)
    if (isDownloadingAll && completedDownloads >= totalDownloads) {
        console.log('[RunpodDirect] All downloads completed!');
        isDownloadingAll = false;
        showRefreshPrompt();
    }
});

// Function to start a server download
async function startServerDownload(url, savePath, filename, markAsQueued = false) {
    try {
        const download_id = `${savePath}/${filename}`;

        // Mark as queued immediately if requested (for Download All)
        if (markAsQueued) {
            downloadStates.set(download_id, {
                status: 'queued',
                progress: 0
            });

            window.dispatchEvent(new CustomEvent('serverDownloadUpdate', {
                detail: { download_id, ...downloadStates.get(download_id) }
            }));
        }

        const response = await api.fetchApi("/server_download/start", {
            method: "POST",
            headers: {
                "Content-Type": "application/json",
            },
            body: JSON.stringify({
                url,
                save_path: savePath,
                filename
            })
        });

        const result = await response.json();

        if (response.ok) {
            // If not already marked as queued, set as queued now
            if (!markAsQueued) {
                downloadStates.set(download_id, {
                    status: 'queued',
                    progress: 0
                });

                window.dispatchEvent(new CustomEvent('serverDownloadUpdate', {
                    detail: { download_id, ...downloadStates.get(download_id) }
                }));
            }

            return { success: true, download_id };
        } else {
            return { success: false, error: result.error };
        }
    } catch (error) {
        console.error("Failed to start download:", error);
        return { success: false, error: error.message };
    }
}

// Get download status
function getDownloadStatus(downloadId) {
    return downloadStates.get(downloadId) || null;
}

// Pause download
async function pauseDownload(downloadId) {
    try {
        const response = await api.fetchApi("/server_download/pause", {
            method: "POST",
            headers: { "Content-Type": "application/json" },
            body: JSON.stringify({ download_id: downloadId })
        });

        const result = await response.json();
        return { success: response.ok, ...result };
    } catch (error) {
        console.error("Failed to pause download:", error);
        return { success: false, error: error.message };
    }
}

// Resume download
async function resumeDownload(downloadId) {
    try {
        const response = await api.fetchApi("/server_download/resume", {
            method: "POST",
            headers: { "Content-Type": "application/json" },
            body: JSON.stringify({ download_id: downloadId })
        });

        const result = await response.json();
        return { success: response.ok, ...result };
    } catch (error) {
        console.error("Failed to resume download:", error);
        return { success: false, error: error.message };
    }
}

// Cancel download
async function cancelDownload(downloadId) {
    try {
        const response = await api.fetchApi("/server_download/cancel", {
            method: "POST",
            headers: { "Content-Type": "application/json" },
            body: JSON.stringify({ download_id: downloadId })
        });

        const result = await response.json();
        return { success: response.ok, ...result };
    } catch (error) {
        console.error("Failed to cancel download:", error);
        return { success: false, error: error.message };
    }
}

// Process download queue - Sends all downloads to backend which handles queue management
async function processDownloadQueue() {
    if (downloadQueue.length === 0) {
        console.log('[RunpodDirect] No downloads in queue');
        return;
    }

    // Send all downloads to the backend (backend handles queue and priorities)
    console.log(`[RunpodDirect] Starting ${downloadQueue.length} downloads (backend will queue and prioritize)`);

    const downloadsToStart = [...downloadQueue];
    downloadQueue = []; // Clear queue as we're sending all to backend

    // Start all downloads - backend will queue and manage priorities
    // Pass markAsQueued=true so buttons show "Queued" status immediately
    for (const download of downloadsToStart) {
        console.log(`[RunpodDirect] Queuing download ${download.filename}`);
        await startServerDownload(download.url, download.directory, download.filename, true);
    }

    console.log(`[RunpodDirect] All ${downloadsToStart.length} downloads queued on backend`);
}

// Show refresh prompt
function showRefreshPrompt() {
    const dialog = document.querySelector('[role="dialog"]');
    if (!dialog) return;

    // Find the dialog content area
    const dialogContent = dialog.querySelector('.p-dialog-content');
    if (!dialogContent) return;

    // Check if prompt already exists
    if (document.querySelector('.server-download-refresh-prompt')) {
        console.log('[RunpodDirect] Refresh prompt already shown');
        return;
    }

    // Create refresh prompt
    const refreshPrompt = document.createElement('div');
    refreshPrompt.className = 'server-download-refresh-prompt';
    refreshPrompt.style.cssText = `

        margin-top: 20px;

        padding: 16px;

        background: #4caf50;

        color: white;

        border-radius: 8px;

        text-align: center;

        font-weight: 500;

    `;

    refreshPrompt.innerHTML = `

        <div style="margin-bottom: 12px;">

            ✅ All models downloaded successfully!

        </div>

        <button class="p-button p-component p-button-sm"

                style="background: white; color: #4caf50; border: none; padding: 8px 16px; border-radius: 4px; cursor: pointer; font-weight: 600;"

                onclick="location.reload()">

            Refresh Page

        </button>

    `;

    dialogContent.appendChild(refreshPrompt);
}

// Create global progress area with individual progress bars for each download
function createProgressArea(listbox) {
    // Remove existing progress area if any
    const existing = document.querySelector('.server-download-progress-area');
    if (existing) existing.remove();

    const progressArea = document.createElement('div');
    progressArea.className = 'server-download-progress-area';
    progressArea.style.cssText = `

        margin-top: 20px;

        padding: 16px;

        background: var(--p-content-background, #1e1e1e);

        border: 1px solid var(--p-content-border-color, #333);

        border-radius: 8px;

    `;

    progressArea.innerHTML = `

        <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px;">

            <div style="font-weight: 600; color: var(--p-text-color);">

                Download Progress

            </div>

        </div>

        <div id="server-download-overall-progress" style="margin-bottom: 12px; font-size: 13px; color: var(--p-text-muted-color);">

            Overall: 0/${totalDownloads} models completed

        </div>

        <div id="server-download-items-container" style="display: flex; flex-direction: column; gap: 12px;">

            <!-- Individual download progress items will be added here -->

        </div>

    `;

    listbox.parentElement.appendChild(progressArea);

    // Listen for updates
    const updateHandler = (event) => {
        const { download_id, status, progress, downloaded, total, speed } = event.detail;

        if (!isDownloadingAll) {
            return;
        }

        // Update overall progress
        const overallProgress = document.getElementById('server-download-overall-progress');
        if (overallProgress) {
            overallProgress.textContent = `Overall: ${completedDownloads}/${totalDownloads} models completed`;
        }

        // Update or create individual progress item
        updateDownloadProgressItem(download_id, status, progress, downloaded, total, speed);
    };

    window.addEventListener('serverDownloadUpdate', updateHandler);
}

// Update or create a progress item for a specific download
function updateDownloadProgressItem(download_id, status, progress, downloaded, total, speed) {
    // Declare variables at function scope so they're accessible across try blocks
    let item = null;
    let container = null;
    const itemId = `download-item-${download_id.replace(/\//g, '-')}`;

    try {
        container = document.getElementById('server-download-items-container');
        if (!container) return;

        item = document.getElementById(itemId);

        // Don't show queued items
        if (status === 'queued') {
            if (item) item.remove();
            return;
        }

        // Remove completed/error items after a delay
        if (status === 'completed' || status === 'error') {
            if (item && !item.dataset.removing) {
                item.dataset.removing = 'true';
                setTimeout(() => {
                    try {
                        if (item && item.parentNode) item.remove();
                    } catch (e) {
                        console.error('[RunpodDirect] Error removing progress item:', e);
                    }
                }, 2000);
            }
        }

        // Create new item if it doesn't exist
        if (!item) {
            item = document.createElement('div');
            item.id = itemId;
            item.style.cssText = `

                padding: 12px;

                background: rgba(255, 255, 255, 0.05);

                border-radius: 6px;

                border: 1px solid rgba(255, 255, 255, 0.1);

            `;
            container.appendChild(item);
        }
    } catch (e) {
        console.error('[RunpodDirect] Error in updateDownloadProgressItem:', e);
        return;
    }

    try {
        // Status icon and color (no priority badge needed - downloading one at a time)
        let statusIcon = '';
        let statusColor = '#2196F3';
        if (status === 'downloading') {
            statusIcon = '<i class="pi pi-spin pi-spinner" style="margin-right: 6px;"></i>';
            statusColor = '#2196F3';
        } else if (status === 'completed') {
            statusIcon = '<i class="pi pi-check-circle" style="margin-right: 6px;"></i>';
            statusColor = '#4CAF50';
        } else if (status === 'error') {
            statusIcon = '<i class="pi pi-times-circle" style="margin-right: 6px;"></i>';
            statusColor = '#ef4444';
        } else if (status === 'paused') {
            statusIcon = '<i class="pi pi-pause" style="margin-right: 6px;"></i>';
            statusColor = '#FF9800';
        }

        const progressPercent = progress || 0;
        const speedText = speed || '--';
        const sizeText = downloaded && total ? `${formatBytes(downloaded)} / ${formatBytes(total)}` : '--';

        if (item) {
            item.innerHTML = `

                <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 6px;">

                    <div style="font-size: 13px; color: ${statusColor}; font-weight: 500; display: flex; align-items: center;">

                        ${statusIcon}${download_id}

                    </div>

                    <div style="font-size: 12px; color: var(--p-text-muted-color);">

                        ${progressPercent.toFixed(1)}%

                    </div>

                </div>

                <div style="width: 100%; height: 8px; background: rgba(0,0,0,0.3); border-radius: 4px; overflow: hidden; margin-bottom: 6px; border: 1px solid rgba(255,255,255,0.1);">

                    <div style="height: 100%; background: linear-gradient(90deg, ${statusColor}, ${statusColor}aa); width: ${progressPercent}%; transition: width 0.3s;"></div>

                </div>

                <div style="display: flex; justify-content: space-between; font-size: 11px; color: var(--p-text-muted-color);">

                    <span>Speed: ${speedText}</span>

                    <span>${sizeText}</span>

                </div>

            `;
        }
    } catch (e) {
        console.error('[RunpodDirect] Error updating progress item HTML:', e);
    }
}

// Export functions for use in other modules
window.serverDownload = {
    start: startServerDownload,
    getStatus: getDownloadStatus,
    states: downloadStates
};

// Helper to inject buttons using MutationObserver
function setupDialogObserver() {
    console.log('[RunpodDirect] Setting up dialog observer');

    // Watch for the missing models dialog to appear
    const observer = new MutationObserver((mutations) => {
        for (const mutation of mutations) {
            if (mutation.addedNodes.length) {
                mutation.addedNodes.forEach(node => {
                    if (node.nodeType === Node.ELEMENT_NODE) {
                        // Check if this node or its descendants contain the missing models dialog
                        const hasDialog = node.querySelector && (
                            node.querySelector('.comfy-missing-models') ||
                            node.classList?.contains('comfy-missing-models')
                        );

                        if (hasDialog) {
                            console.log('[RunpodDirect] Detected missing models dialog, injecting buttons...');
                            setTimeout(() => {
                                injectServerDownloadButtons();
                            }, 500);
                        }
                    }
                });
            }
        }
    });

    // Start observing the document body for dialog additions
    observer.observe(document.body, {
        childList: true,
        subtree: true
    });

    console.log('[RunpodDirect] Observer active');
}

function injectServerDownloadButtons() {
    console.log('[RunpodDirect] injectServerDownloadButtons called');

    // Find the missing models listbox
    const listbox = document.querySelector('.comfy-missing-models');
    if (!listbox) {
        console.log('[RunpodDirect] Missing models listbox not found');
        return;
    }

    console.log('[RunpodDirect] Found .comfy-missing-models listbox');

    // Check if we already added our UI
    if (document.querySelector('.server-download-all-btn')) {
        console.log('[RunpodDirect] Buttons already injected');
        return;
    }

    const listItems = listbox.querySelectorAll('.p-listbox-option');
    console.log(`[RunpodDirect] Found ${listItems.length} list items`);

    if (listItems.length === 0) {
        console.log('[RunpodDirect] No list items found');
        return;
    }

    // Add "Download All" button before the listbox
    const downloadAllContainer = document.createElement('div');
    downloadAllContainer.style.cssText = 'margin-bottom: 16px; display: flex; justify-content: center;';

    const downloadAllBtn = document.createElement('button');
    downloadAllBtn.className = 'server-download-all-btn p-button p-component p-button-sm';
    downloadAllBtn.type = 'button';
    downloadAllBtn.style.cssText = 'background: #2196F3; color: white; border: none; padding: 10px 20px; font-weight: 600;';

    const downloadAllLabel = document.createElement('span');
    downloadAllLabel.className = 'p-button-label';
    downloadAllLabel.textContent = `Download All Models to Pod (${listItems.length})`;
    downloadAllBtn.appendChild(downloadAllLabel);

    downloadAllBtn.onclick = async (e) => {
        e.stopPropagation();
        downloadAllBtn.disabled = true;
        downloadAllLabel.textContent = 'Starting downloads...';

        // Collect all models
        downloadQueue = [];
        const models = [];

        listItems.forEach((item) => {
            const labelElement = item.querySelector('[title]');
            if (!labelElement) return;

            const label = labelElement.textContent.trim();
            const url = labelElement.getAttribute('title');
            const parts = label.split('/').map(p => p.trim());
            if (parts.length !== 2) return;

            const directory = parts[0];
            const filename = parts[1];

            models.push({ url, directory, filename });
        });

        downloadQueue = [...models];
        totalDownloads = models.length;
        completedDownloads = 0;
        isDownloadingAll = true;

        // Create progress area
        createProgressArea(listbox);

        // Start first download
        if (downloadQueue.length > 0) {
            processDownloadQueue();
        }
    };

    downloadAllContainer.appendChild(downloadAllBtn);
    listbox.parentElement.insertBefore(downloadAllContainer, listbox);

    listItems.forEach((item, index) => {
        console.log(`[RunpodDirect] Processing item ${index + 1}`);

        // Check if we already added the button
        if (item.querySelector('.server-download-btn')) {
            console.log(`[RunpodDirect] Item ${index + 1} already has server download button, skipping`);
            return;
        }

        // The HTML structure is:
        // <li class="p-listbox-option">
        //   <div class="flex flex-row items-center gap-2"> <- main container
        //     <div> model info </div>
        //     <div> <button>Download</button> </div>
        //     <div> <button>Copy URL</button> </div>
        //   </div>
        // </li>

        // Find the main flex container
        const mainContainer = item.querySelector('.flex.flex-row.items-center.gap-2');
        if (!mainContainer) {
            console.log('[RunpodDirect] Main flex container not found');
            return;
        }

        console.log('[RunpodDirect] Found main container');

        // We'll create a new div for our button (following the same pattern)
        const buttonWrapper = document.createElement('div');

        // Get model info from the item
        const labelElement = item.querySelector('[title]');
        if (!labelElement) {
            console.log('[RunpodDirect] No title element found');
            return;
        }

        const label = labelElement.textContent.trim();
        const url = labelElement.getAttribute('title');
        console.log(`[RunpodDirect] Model: ${label}, URL: ${url}`);

        // Parse "checkpoints / model.safetensors" format
        const parts = label.split('/').map(p => p.trim());
        if (parts.length !== 2) {
            console.log(`[RunpodDirect] Could not parse label format: ${label}`);
            return;
        }

        const directory = parts[0];
        const filename = parts[1];
        const download_id = `${directory}/${filename}`;
        console.log(`[RunpodDirect] Creating button for ${download_id}`);

        // Create server download button
        const serverDownloadBtn = document.createElement('button');
        serverDownloadBtn.className = 'server-download-btn p-button p-component p-button-outlined p-button-sm';
        serverDownloadBtn.type = 'button';

        // Create button content
        const btnLabel = document.createElement('span');
        btnLabel.className = 'p-button-label';
        btnLabel.textContent = 'Download to Pod';
        serverDownloadBtn.appendChild(btnLabel);

        // Status indicator (icon)
        const statusIcon = document.createElement('i');
        statusIcon.style.cssText = 'margin-left: 6px; font-size: 14px; display: none;';
        serverDownloadBtn.appendChild(statusIcon);

        // Button click handler
        serverDownloadBtn.onclick = async (e) => {
            e.stopPropagation();
            serverDownloadBtn.disabled = true;
            btnLabel.textContent = 'Starting...';

            const result = await startServerDownload(url, directory, filename);

            if (result.success) {
                btnLabel.textContent = 'Queued';
                statusIcon.className = 'pi pi-clock';
                statusIcon.style.display = 'inline';
                statusIcon.style.color = '#FF9800';
            } else {
                btnLabel.textContent = 'Error';
                statusIcon.className = 'pi pi-times-circle';
                statusIcon.style.display = 'inline';
                statusIcon.style.color = '#ef4444';
                console.error('Download start failed:', result.error);
            }
        };

        // Listen for download updates
        const updateHandler = (event) => {
            if (event.detail.download_id === download_id) {
                const { status, error } = event.detail;

                if (status === 'queued') {
                    serverDownloadBtn.disabled = true;
                    btnLabel.textContent = 'Queued';
                    statusIcon.className = 'pi pi-clock';
                    statusIcon.style.display = 'inline';
                    statusIcon.style.color = '#FF9800';
                } else if (status === 'downloading') {
                    serverDownloadBtn.disabled = true;
                    btnLabel.textContent = 'Downloading';
                    statusIcon.className = 'pi pi-spin pi-spinner';
                    statusIcon.style.display = 'inline';
                    statusIcon.style.color = '#2196F3';
                } else if (status === 'completed') {
                    btnLabel.textContent = 'Completed';
                    statusIcon.className = 'pi pi-check-circle';
                    statusIcon.style.display = 'inline';
                    statusIcon.style.color = '#4caf50';
                    serverDownloadBtn.style.borderColor = '#4caf50';
                } else if (status === 'error') {
                    btnLabel.textContent = 'Failed';
                    statusIcon.className = 'pi pi-times-circle';
                    statusIcon.style.display = 'inline';
                    statusIcon.style.color = '#ef4444';
                    serverDownloadBtn.title = error;
                }
            }
        };

        window.addEventListener('serverDownloadUpdate', updateHandler);

        // Add button to wrapper div
        buttonWrapper.appendChild(serverDownloadBtn);

        // Add wrapper to main container (alongside Download and Copy URL)
        mainContainer.appendChild(buttonWrapper);
        console.log(`[RunpodDirect] Button added to main container for ${download_id}`);
    });

    console.log('[RunpodDirect] Button injection complete');
}

// Register the extension
app.registerExtension({
    name: "ComfyUI.RunpodDirect",

    async setup() {
        console.log("[RunpodDirect] Extension setup starting");

        // Set up observer to watch for missing models dialog
        setupDialogObserver();

        // Also try to inject immediately if dialog already exists
        setTimeout(() => {
            console.log('[RunpodDirect] Checking for existing dialog...');
            injectServerDownloadButtons();
        }, 1000);

        // Try again after a longer delay
        setTimeout(() => {
            console.log('[RunpodDirect] Second check for dialog...');
            injectServerDownloadButtons();
        }, 3000);

        console.log("[RunpodDirect] Extension setup complete");
    }
});