File size: 9,176 Bytes
fed1528
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29393b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fed1528
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import type { NodeHealth, PresenceStage } from "@/lib/stageMachine/types";

export type BusLinkState = "offline" | "connecting" | "live" | "mock" | "error";

export interface AceNodeSnapshot {
    id: string;
    health: NodeHealth;
    latencyMs: number;
    load: number;
    message?: string;
}

export interface AceSnapshot {
    type?: string;
    tMediaMs?: number;
    stage: PresenceStage;
    targetStage?: PresenceStage;
    warmProgress: number;
    mode: string;
    controlMs?: number;
    nodes?: Record<string, AceNodeSnapshot>;
    event?: {
        kind?: string;
        level?: "info" | "ok" | "warn" | "error";
        message?: string;
    };
    nvidia?: {
        configured: boolean;
        model?: string | null;
    };
    lastTalk?: {
        userText?: string;
        reply?: string;
        ok?: boolean;
        model?: string | null;
        latencyMs?: number;
        error?: string | null;
        at?: number;
    } | null;
}

export interface TalkResult {
    ok: boolean;
    reply: string;
    model?: string | null;
    latencyMs?: number;
    error?: string | null;
    stage?: PresenceStage;
    mode?: string;
}

/** Pipe 3 Phoneme-Direct frame — mouth truth derived from text, not audio. */
export interface VisemeFrame {
    tMediaMs: number;
    viseme: string;
    energy: number;
    weights: Record<string, number>;
}

type SnapshotHandler = (snap: AceSnapshot) => void;
type LinkHandler = (state: BusLinkState, detail?: string) => void;
type VisemeHandler = (frames: VisemeFrame[], durationMs: number, source?: string) => void;

const DEFAULT_HTTP = "http://127.0.0.1:8100";
const DEFAULT_WS = "ws://127.0.0.1:8100/ws";

function httpBase() {
    return (import.meta.env.VITE_ACE_HTTP_URL || DEFAULT_HTTP).replace(/\/$/, "");
}

function wsUrl() {
    return import.meta.env.VITE_ACE_WS_URL || DEFAULT_WS;
}

class AceControllerClient {
    private ws: WebSocket | null = null;
    private reconnectTimer: number | null = null;
    private intentionalClose = false;
    private onSnapshot: SnapshotHandler | null = null;
    private onLink: LinkHandler | null = null;
    private onVisemes: VisemeHandler | null = null;
    private link: BusLinkState = "offline";

    get linkState() {
        return this.link;
    }

    configure(handlers: {
        onSnapshot: SnapshotHandler;
        onLink: LinkHandler;
        onVisemes?: VisemeHandler;
    }) {
        this.onSnapshot = handlers.onSnapshot;
        this.onLink = handlers.onLink;
        this.onVisemes = handlers.onVisemes ?? null;
    }

    private setLink(state: BusLinkState, detail?: string) {
        this.link = state;
        this.onLink?.(state, detail);
    }

    start() {
        this.intentionalClose = false;
        void this.probeAndConnect();
    }

    stop() {
        this.intentionalClose = true;
        if (this.reconnectTimer != null) {
            window.clearTimeout(this.reconnectTimer);
            this.reconnectTimer = null;
        }
        this.ws?.close();
        this.ws = null;
        this.setLink("offline", "bus stopped");
    }

    private scheduleReconnect() {
        if (this.intentionalClose) return;
        if (this.reconnectTimer != null) return;
        this.reconnectTimer = window.setTimeout(() => {
            this.reconnectTimer = null;
            void this.probeAndConnect();
        }, 1500);
    }

    private async probeAndConnect() {
        this.setLink("connecting", "probing ACE controller");
        try {
            const res = await fetch(`${httpBase()}/health`, {
                method: "GET",
                cache: "no-store",
            });
            if (!res.ok) throw new Error(`health ${res.status}`);
            const body = (await res.json()) as { mode?: string; stage?: string };
            const mode = body.mode || "unknown";
            this.connectWs(mode);
        } catch (err) {
            const msg = err instanceof Error ? err.message : "unreachable";
            this.setLink("error", msg);
            this.scheduleReconnect();
        }
    }

    private connectWs(mode: string) {
        try {
            const ws = new WebSocket(wsUrl());
            this.ws = ws;

            ws.onopen = () => {
                const link: BusLinkState =
                    mode.includes("live") || mode === "live" ? "live" : mode.includes("mock") ? "mock" : "live";
                this.setLink(link, `ws open · ${mode}`);
                ws.send(JSON.stringify({ type: "ping" }));
            };

            ws.onmessage = (ev) => {
                try {
                    const data = JSON.parse(String(ev.data)) as AceSnapshot & {
                        type?: string;
                        frames?: VisemeFrame[];
                        durationMs?: number;
                        source?: string;
                    };
                    if (data.type === "pong") return;
                    if (data.type === "visemes" && Array.isArray(data.frames)) {
                        this.onVisemes?.(data.frames, data.durationMs ?? 0, data.source);
                        return;
                    }
                    if (data.type === "snapshot" || data.stage) {
                        this.onSnapshot?.(data);
                        if (data.mode) {
                            const link: BusLinkState =
                                data.mode.includes("live") ? "live" : data.mode.includes("mock") ? "mock" : this.link;
                            if (link !== this.link && (link === "live" || link === "mock")) {
                                this.setLink(link, `mode ${data.mode}`);
                            }
                        }
                    }
                } catch {
                    /* ignore malformed */
                }
            };

            ws.onerror = () => {
                this.setLink("error", "websocket error");
            };

            ws.onclose = () => {
                this.ws = null;
                if (!this.intentionalClose) {
                    this.setLink("offline", "ws closed");
                    this.scheduleReconnect();
                }
            };
        } catch (err) {
            const msg = err instanceof Error ? err.message : "ws failed";
            this.setLink("error", msg);
            this.scheduleReconnect();
        }
    }

    /**
     * WO-2 T2 — sends one raw PCM audio frame (Float32, 16 kHz mono) over
     * the existing WebSocket as a binary message. Silently no-ops if the
     * link isn't open yet; MicCapture is expected to check `linkState`
     * before starting capture, but a dropped frame here must never throw
     * inside the audio callback.
     */
    sendAudioFrame(samples: Float32Array) {
        if (!this.ws || this.ws.readyState !== WebSocket.OPEN) return;
        this.ws.send(samples.buffer);
    }

    /** WO-2 T2 — tells ace-controller's in-process buffer to hand off (T3: to Transcribe). */
    signalSpeechEnd() {
        if (!this.ws || this.ws.readyState !== WebSocket.OPEN) return;
        this.ws.send(JSON.stringify({ type: "speech-end" }));
    }

    async setStage(stage: PresenceStage): Promise<AceSnapshot | null> {
        try {
            const res = await fetch(`${httpBase()}/v1/stage`, {
                method: "POST",
                headers: { "Content-Type": "application/json" },
                body: JSON.stringify({ stage }),
            });
            if (!res.ok) return null;
            const snap = (await res.json()) as AceSnapshot;
            this.onSnapshot?.(snap);
            return snap;
        } catch {
            // fall back to ws command
            this.ws?.send(JSON.stringify({ type: "stage", stage }));
            return null;
        }
    }

    async talk(text: string, opts?: { promote?: boolean }): Promise<TalkResult> {
        const payload = {
            text,
            promote: opts?.promote ?? true,
        };
        try {
            const res = await fetch(`${httpBase()}/v1/talk`, {
                method: "POST",
                headers: { "Content-Type": "application/json" },
                body: JSON.stringify(payload),
            });
            const data = (await res.json()) as TalkResult & AceSnapshot;
            if (data.stage) this.onSnapshot?.(data as AceSnapshot);
            return {
                ok: Boolean(data.ok),
                reply: data.reply || "",
                model: data.model,
                latencyMs: data.latencyMs,
                error: data.error,
                stage: data.stage,
                mode: data.mode,
            };
        } catch (err) {
            return {
                ok: false,
                reply: "",
                error: err instanceof Error ? err.message : "talk failed",
            };
        }
    }

    async status(): Promise<AceSnapshot | null> {
        try {
            const res = await fetch(`${httpBase()}/v1/status`, { cache: "no-store" });
            if (!res.ok) return null;
            return (await res.json()) as AceSnapshot;
        } catch {
            return null;
        }
    }
}

export const aceController = new AceControllerClient();