File size: 7,653 Bytes
ddebc3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""WebUI imagine endpoint backed by Grok Imagine WebSocket only."""

import asyncio
import hmac
import uuid
from typing import Optional

import orjson
from fastapi import APIRouter, WebSocket, WebSocketDisconnect

from app.platform.auth.middleware import get_webui_key, is_webui_enabled
from app.platform.config.snapshot import get_config
from app.platform.logging.logger import logger
from app.platform.runtime.clock import now_s
from app.products.openai.images import resolve_aspect_ratio

router = APIRouter()


async def _acquire_token():
    from app.dataplane.account import _directory as _acct_dir
    if _acct_dir is None:
        return None, None
    from app.control.model.registry import get as get_model
    spec = get_model("grok-imagine-image")
    if spec is None:
        return None, None
    acct = await _acct_dir.reserve(
        pool_candidates=spec.pool_candidates(),
        mode_id=int(spec.mode_id),
        now_s_override=now_s(),
    )
    if acct is None:
        return None, None
    return acct.token, acct


def _extract_token(value: str | None) -> str:
    raw = str(value or "").strip()
    if not raw:
        return ""
    scheme, _, token = raw.partition(" ")
    if scheme.lower() == "bearer" and token:
        return token.strip()
    return raw


def _is_allowed(token: str) -> bool:
    webui_key = get_webui_key()
    if not webui_key:
        return is_webui_enabled()
    return bool(token) and hmac.compare_digest(token, webui_key)


def _websocket_token(websocket: WebSocket) -> str:
    return (
        _extract_token(websocket.headers.get("authorization"))
        or str(websocket.query_params.get("access_token") or "").strip()
    )


@router.websocket("/imagine/ws")
async def imagine_ws(websocket: WebSocket):
    if not _is_allowed(_websocket_token(websocket)):
        await websocket.close(code=1008)
        return

    await websocket.accept()
    stop_event = asyncio.Event()
    run_task: Optional[asyncio.Task] = None

    async def _send(payload: dict) -> bool:
        try:
            await websocket.send_text(orjson.dumps(payload).decode())
            return True
        except Exception:
            return False

    async def _stop_run():
        nonlocal run_task
        stop_event.set()
        if run_task and not run_task.done():
            run_task.cancel()
            try:
                await run_task
            except Exception:
                pass
        run_task = None
        stop_event.clear()

    async def _run(
        prompt: str,
        aspect_ratio: str,
        nsfw: Optional[bool],
        count: int,
        quality: str,
    ):
        from app.dataplane.account import _directory as _acct_dir
        from app.dataplane.reverse.transport.imagine_ws import stream_images

        run_id = uuid.uuid4().hex
        enable_pro = quality == "quality"
        await _send({
            "type": "status",
            "status": "running",
            "prompt": prompt,
            "aspect_ratio": aspect_ratio,
            "run_id": run_id,
            "count": count,
            "quality": quality,
        })

        acct = None
        try:
            token, acct = await _acquire_token()
            if not token:
                await _send({
                    "type": "error",
                    "message": "No available accounts for this model tier",
                    "code": "rate_limit_exceeded",
                })
                return

            enable_nsfw = nsfw if nsfw is not None else get_config().get_bool("features.enable_nsfw", True)
            async for event in stream_images(
                token,
                prompt,
                aspect_ratio=aspect_ratio,
                n=count,
                enable_nsfw=enable_nsfw,
                enable_pro=enable_pro,
            ):
                if stop_event.is_set():
                    return
                if not isinstance(event, dict) or event.get("type") == "_meta":
                    continue
                event.setdefault("run_id", run_id)
                await _send(event)
                if event.get("type") == "error":
                    return

            if not stop_event.is_set():
                await _send({
                    "type": "status",
                    "status": "completed",
                    "run_id": run_id,
                    "count": count,
                })
        except asyncio.CancelledError:
            pass
        except Exception as exc:
            logger.error(
                "webui imagine run failed: error_type={} error={}",
                type(exc).__name__,
                exc,
            )
            await _send({
                "type": "error",
                "message": str(exc),
                "code": "internal_error",
            })
        finally:
            if acct and _acct_dir:
                await _acct_dir.release(acct)
            if stop_event.is_set():
                await _send({"type": "status", "status": "stopped", "run_id": run_id})

    try:
        while True:
            try:
                raw = await websocket.receive_text()
            except (RuntimeError, WebSocketDisconnect):
                break

            try:
                payload = orjson.loads(raw)
            except Exception:
                await _send({
                    "type": "error",
                    "message": "Invalid message format.",
                    "code": "invalid_payload",
                })
                continue

            action = payload.get("type")
            if action == "start":
                prompt = str(payload.get("prompt") or "").strip()
                if not prompt:
                    await _send({
                        "type": "error",
                        "message": "Prompt cannot be empty.",
                        "code": "invalid_prompt",
                    })
                    continue
                aspect_ratio = resolve_aspect_ratio(str(payload.get("aspect_ratio") or "2:3").strip() or "2:3")
                quality = str(payload.get("quality") or "speed").strip().lower()
                if quality not in {"speed", "quality"}:
                    quality = "speed"
                nsfw = payload.get("nsfw")
                if nsfw is not None:
                    if isinstance(nsfw, str):
                        nsfw = nsfw.strip().lower() in {"1", "true", "yes", "on"}
                    else:
                        nsfw = bool(nsfw)
                try:
                    count = int(payload.get("count") or 6)
                except (TypeError, ValueError):
                    count = 6
                count = max(1, min(count, 6))
                await _stop_run()
                run_task = asyncio.create_task(_run(prompt, aspect_ratio, nsfw, count, quality))
                continue

            if action == "stop":
                await _stop_run()
                continue

            await _send({
                "type": "error",
                "message": "Unknown action.",
                "code": "invalid_action",
            })
    except WebSocketDisconnect:
        pass
    except Exception as exc:
        logger.error(
            "webui imagine websocket handler failed: error_type={} error={}",
            type(exc).__name__,
            exc,
        )
    finally:
        await _stop_run()
        try:
            from starlette.websockets import WebSocketState
            if websocket.client_state == WebSocketState.CONNECTED:
                await websocket.close(code=1000, reason="Server closing connection")
        except Exception:
            pass