File size: 2,186 Bytes
89b6a21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7d5735a
89b6a21
 
 
 
 
 
 
 
7d5735a
89b6a21
 
 
 
 
 
 
7d5735a
89b6a21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fc31f69
 
 
 
 
 
 
 
 
89b6a21
 
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
import { triggerAutonomyTick } from "./api.js";
import {
    renderAutonomyRuntime,
    setAutonomyBusy,
    showAutonomyFeedback,
} from "./view.js";

export function createAutonomyController({
    autEls,
    refreshActionQueue,
    getErrorMessage,
}) {
    async function runManualAutonomyTick() {
        if (!autEls) return;
        setAutonomyBusy(autEls, true);
        const reasonInput = String(autEls.tickReasonInput?.value || "").trim();
        const reason = reasonInput || "manual_dashboard";
        showAutonomyFeedback(autEls, "Running 1 tick...", "warn");
        try {
            const payload = await triggerAutonomyTick({ force: true, reason });
            renderAutonomyRuntime(payload?.runtime || {}, autEls);
            const processedCount = Array.isArray(payload?.processed)
                ? payload.processed.length
                : Number(payload?.due_goals || 0);
            showAutonomyFeedback(
                autEls,
                `Tick completed. Goals processed: ${processedCount}.`,
                "ok"
            );
            await refreshActionQueue({ showFeedback: false });
        } catch (error) {
            console.error("Autonomy manual tick error", error);
            showAutonomyFeedback(
                autEls,
                `Error: ${getErrorMessage(error, "Failed to run tick.")}`,
                "error"
            );
        } finally {
            setAutonomyBusy(autEls, false);
        }
    }

    function bindAutonomyEvents() {
        if (!autEls?.tickBtn) return;
        autEls.tickBtn.addEventListener("click", () => {
            runManualAutonomyTick();
        });
    }

    return {
        bindAutonomyEvents,
        // O2b: render autonomy from the live ops snapshot. snap.control_plane is
        // the /api/control-plane payload, whose .autonomy is exactly what
        // renderAutonomyRuntime consumes (same path the control-plane controller
        // already uses).
        renderRuntimeFromSnapshot(controlPlaneSection) {
            if (autEls && controlPlaneSection) {
                renderAutonomyRuntime(controlPlaneSection.autonomy || {}, autEls);
            }
        },
    };
}