File size: 6,396 Bytes
8fb6433
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import {app} from "/scripts/app.js";
import {
    ADVANCED_POLICY_NODE,
    CHAIN_POLICY_NODE,
    LEGACY_POLICY_NODE,
    MODERN_PLAN_NODE,
    PLAN_NODE,
    PROFILE_POLICY_NODE,
    applySocketPresentation,
    hasAdvancedPresentation,
    nodeType,
    policyPlanConsumers,
    presentationForNode,
} from "./h3_socket_presentation_core.mjs?v=0.6.6";

const EXTENSION = "minimax_h3_context_loop.socket_presentation";
const WATCHED_POLICY_NODES = new Set([
    PROFILE_POLICY_NODE, CHAIN_POLICY_NODE, ADVANCED_POLICY_NODE,
    LEGACY_POLICY_NODE, PLAN_NODE, MODERN_PLAN_NODE,
]);
const WIDGET_LABELS = Object.freeze({
    [PROFILE_POLICY_NODE]: Object.freeze({
        scene_continuity:"Scene continuity",
        audio_profile:"Audio profile",
    }),
});

function collapseWidget(widget) {
    if (!widget || widget.h3PresentationHidden) return;
    widget.h3PresentationHidden = true;
    widget.h3PresentationOriginal = {
        type: widget.type,
        hidden: widget.hidden,
        computeSize: widget.computeSize,
        draw: widget.draw,
    };
    widget.hidden = true;
    widget.type = "hidden";
    widget.computeSize = () => [0, -4];
    widget.draw = () => {};
    for (const item of new Set([widget.inputEl, widget.element])) {
        if (!item?.style) continue;
        item.style.setProperty("display", "none", "important");
        item.style.setProperty("pointer-events", "none", "important");
        item.setAttribute?.("aria-hidden", "true");
    }
}

function restoreWidget(widget) {
    if (!widget?.h3PresentationHidden) return;
    const original = widget.h3PresentationOriginal ?? {};
    widget.type = original.type;
    widget.hidden = original.hidden;
    widget.computeSize = original.computeSize;
    widget.draw = original.draw;
    widget.h3PresentationHidden = false;
    for (const item of new Set([widget.inputEl, widget.element])) {
        if (!item?.style) continue;
        item.style.removeProperty("display");
        item.style.removeProperty("pointer-events");
        item.removeAttribute?.("aria-hidden");
    }
}

function refreshNode(node) {
    if (!node) return;
    node.properties ??= {};
    const labels = WIDGET_LABELS[nodeType(node)] ?? {};
    for (const widget of node.widgets ?? []) {
        if (labels[widget.name]) widget.label = labels[widget.name];
    }
    const advanced = Boolean(node.properties.h3_show_advanced_sockets);
    const presentation = presentationForNode(node, advanced);
    applySocketPresentation(node, advanced);
    for (const widget of node.widgets ?? []) {
        if (presentation.hiddenWidgets.has(widget.name)) collapseWidget(widget);
        else restoreWidget(widget);
    }
    node.graph?.setDirtyCanvas?.(true, true);
}

function refreshGraph(graph) {
    for (const node of graph?._nodes ?? app.graph?._nodes ?? []) refreshNode(node);
}

function scheduleGraphRefresh(node) {
    if (node._h3PresentationRefreshPending) return;
    node._h3PresentationRefreshPending = true;
    queueMicrotask(() => {
        node._h3PresentationRefreshPending = false;
        refreshGraph(node.graph ?? app.graph);
        refreshPolicyConsumers(node);
    });
}

function refreshPolicyConsumers(node) {
    const plans = new Set(policyPlanConsumers(node));
    if (!plans.size) return;
    for (const plan of plans) {
        plan._h3ChainEditorConnectionRefresh?.();
        plan.graph?.setDirtyCanvas?.(true, true);
    }
    // Companion editors cache the resolved timing policy separately from the
    // Plan JSON. A policy widget edit is not a LiteGraph connection event, so
    // explicitly invalidate companions bound to one of the affected Plans.
    for (const candidate of node.graph?._nodes ?? []) {
        if (candidate._h3PlanStudioState?.planNode
                && !plans.has(candidate._h3PlanStudioState.planNode)) continue;
        candidate._h3PlanStudioRefresh?.();
        candidate._h3ScenePromptEditorRefresh?.();
        candidate._h3RichPromptRefresh?.();
    }
}

function watchWidgets(node) {
    for (const widget of node.widgets ?? []) {
        if (widget.h3PresentationCallbackWrapped) continue;
        widget.h3PresentationCallbackWrapped = true;
        const original = widget.callback;
        widget.callback = function () {
            const result = original?.apply(this, arguments);
            scheduleGraphRefresh(node);
            return result;
        };
    }
}

app.registerExtension({
    name: EXTENSION,
    async beforeRegisterNodeDef(nodeClass, nodeData) {
        const relevant = String(nodeData.name ?? "").startsWith("MiniMaxH3")
            || WATCHED_POLICY_NODES.has(nodeData.name);
        if (!relevant) return;

        const originalCreated = nodeClass.prototype.onNodeCreated;
        nodeClass.prototype.onNodeCreated = function () {
            const result = originalCreated?.apply(this, arguments);
            watchWidgets(this);
            refreshNode(this);
            return result;
        };

        const originalConfigure = nodeClass.prototype.onConfigure;
        nodeClass.prototype.onConfigure = function () {
            const result = originalConfigure?.apply(this, arguments);
            watchWidgets(this);
            refreshNode(this);
            scheduleGraphRefresh(this);
            return result;
        };

        const originalConnectionsChange = nodeClass.prototype.onConnectionsChange;
        nodeClass.prototype.onConnectionsChange = function () {
            const result = originalConnectionsChange?.apply(this, arguments);
            scheduleGraphRefresh(this);
            return result;
        };

        const originalMenu = nodeClass.prototype.getExtraMenuOptions;
        nodeClass.prototype.getExtraMenuOptions = function (_, options) {
            const result = originalMenu?.apply(this, arguments);
            if (!hasAdvancedPresentation(this)) return result;
            const advanced = Boolean(this.properties?.h3_show_advanced_sockets);
            options.push({
                content: advanced
                    ? "Hide advanced H3 controls"
                    : "Show advanced H3 controls",
                callback: () => {
                    this.properties ??= {};
                    this.properties.h3_show_advanced_sockets = !advanced;
                    refreshNode(this);
                },
            });
            return result;
        };
    },
});