Skip to content

Commit 29355be

Browse files
committed
fix(chat): resolve imagine ws auth mismatch and improve connection diagnostics
1 parent 2f0aa1e commit 29355be

5 files changed

Lines changed: 43 additions & 5 deletions

File tree

app/api/v1/admin.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,15 @@ async def _verify_ws_api_key(websocket: WebSocket) -> bool:
103103
token = str(websocket.query_params.get("api_key") or "").strip()
104104
if not token:
105105
return False
106-
return (api_key and token == api_key) or token in legacy_keys
106+
if (api_key and token == api_key) or token in legacy_keys:
107+
return True
108+
try:
109+
await api_key_manager.init()
110+
if api_key_manager.validate_key(token):
111+
return True
112+
except Exception as e:
113+
logger.warning(f"Imagine ws api_key validation fallback failed: {e}")
114+
return False
107115

108116

109117
async def _collect_imagine_batch(token: str, prompt: str, aspect_ratio: str) -> list[str]:

app/static/chat/chat.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@
199199
</main>
200200

201201
<script src="/static/common/toast.js"></script>
202-
<script src="/static/chat/chat.js?v=3"></script>
202+
<script src="/static/chat/chat.js?v=4"></script>
203203
</body>
204204

205205
</html>

app/static/chat/chat.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,10 @@ function openImageContinuousSocket(socketIndex, runToken, prompt, aspectRatio, a
498498
socketState.active = status === 'running';
499499
updateImageContinuousStats();
500500
if (imageContinuousRunning) {
501-
if (status === 'running') setImageStatusText('Running');
501+
if (status === 'running') {
502+
clearImageContinuousError();
503+
setImageStatusText('Running');
504+
}
502505
if (status === 'stopped') setImageStatusText('Stopped');
503506
}
504507
updateImageContinuousButtons();
@@ -507,6 +510,7 @@ function openImageContinuousSocket(socketIndex, runToken, prompt, aspectRatio, a
507510

508511
if (msgType === 'image') {
509512
socketState.active = true;
513+
clearImageContinuousError();
510514
appendWaterfallImage(data, socketIndex);
511515
if (imageContinuousRunning) setImageStatusText('Running');
512516
updateImageContinuousButtons();
@@ -545,7 +549,14 @@ function openImageContinuousSocket(socketIndex, runToken, prompt, aspectRatio, a
545549
setImageContinuousError('WebSocket auth rejected. Check API key.');
546550
setImageStatusText('Auth failed');
547551
} else if (socketState.hadError && stillActive <= 0 && stillOpen <= 0) {
548-
setImageContinuousError(socketState.lastError || `WS${socketIndex + 1} connection error`);
552+
const closeCode = Number(event?.code || 0);
553+
const closeReason = String(event?.reason || '').trim();
554+
if (closeCode > 0) {
555+
const suffix = closeReason ? `: ${closeReason}` : '';
556+
setImageContinuousError(`WebSocket closed (${closeCode})${suffix}`);
557+
} else {
558+
setImageContinuousError(socketState.lastError || `WS${socketIndex + 1} connection error`);
559+
}
549560
setImageStatusText('Disconnected');
550561
}
551562

app/static/chat/chat_admin.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@
195195
<script src="/static/common/admin-auth.js"></script>
196196
<script src="/static/common/header.js?v=3"></script>
197197
<script src="/static/common/footer.js"></script>
198-
<script src="/static/chat/chat.js?v=3"></script>
198+
<script src="/static/chat/chat.js?v=4"></script>
199199
</body>
200200

201201
</html>

tests/test_imagine_ws_api.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,25 @@ def test_imagine_ws_ping_pong(monkeypatch: pytest.MonkeyPatch):
4949
assert msg == {"type": "pong"}
5050

5151

52+
def test_imagine_ws_accepts_managed_api_key(monkeypatch: pytest.MonkeyPatch):
53+
client = _build_client(monkeypatch, api_key="global-key")
54+
55+
async def _fake_init():
56+
return None
57+
58+
monkeypatch.setattr(admin_api.api_key_manager, "init", _fake_init)
59+
monkeypatch.setattr(
60+
admin_api.api_key_manager,
61+
"validate_key",
62+
lambda token: {"key": token, "is_active": True} if token == "managed-key" else None,
63+
)
64+
65+
with client.websocket_connect("/api/v1/admin/imagine/ws?api_key=managed-key") as ws:
66+
ws.send_json({"type": "ping"})
67+
msg = ws.receive_json()
68+
assert msg == {"type": "pong"}
69+
70+
5271
def test_imagine_ws_empty_prompt_error(monkeypatch: pytest.MonkeyPatch):
5372
client = _build_client(monkeypatch, api_key="valid-key")
5473
with client.websocket_connect("/api/v1/admin/imagine/ws?api_key=valid-key") as ws:

0 commit comments

Comments
 (0)