File size: 1,693 Bytes
0c85e62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// SPDX-FileCopyrightText: 2025-2026 Kforge Labs <https://github.com/malkuthro/ComfyUI-Koolook>
// SPDX-License-Identifier: GPL-3.0-only

function makeWorkflowId() {
    try {
        if (crypto && typeof crypto.randomUUID === "function") {
            return crypto.randomUUID();
        }
    } catch {
        // Fall through to the local fallback below.
    }
    return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
        const r = Math.floor(Math.random() * 16);
        const v = c === "x" ? r : (r & 0x3) | 0x8;
        return v.toString(16);
    });
}

function hash32(input, seed) {
    let h = (0x811c9dc5 ^ seed) >>> 0;
    for (let i = 0; i < input.length; i += 1) {
        h ^= input.charCodeAt(i);
        h = Math.imul(h, 0x01000193) >>> 0;
    }
    return h.toString(16).padStart(8, "0");
}

// Deterministic UUID-shaped key, not a spec UUIDv5. Comfy treats workflow ids
// as opaque draft-cache keys; stable shape is enough to replace per-workflow
// drafts instead of accumulating one browser-storage entry per sidebar load.
function stableWorkflowId(loadKey) {
    const key = String(loadKey);
    const hex =
        hash32(key, 0) +
        hash32(key, 0x9e3779b9) +
        hash32(key, 0x85ebca6b) +
        hash32(key, 0xc2b2ae35);
    return [
        hex.slice(0, 8),
        hex.slice(8, 12),
        `5${hex.slice(13, 16)}`,
        `8${hex.slice(17, 20)}`,
        hex.slice(20, 32),
    ].join("-");
}

export function cloneWorkflowForTemporaryLoad(graphData, loadKey = null) {
    const clone = JSON.parse(JSON.stringify(graphData || {}));
    clone.id = loadKey == null ? makeWorkflowId() : stableWorkflowId(loadKey);
    return clone;
}