@lelouchhe/webagent 0.1.7 → 0.1.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,87 +0,0 @@
1
- // WebSocket connection lifecycle
2
-
3
- import { state, setBusy, getHashSessionId, requestNewSession, resetSessionUI, setConnectionStatus, clearCancelTimer } from './state.mmmmfxhu.js';
4
- import { addSystem, finishThinking, finishAssistant, finishBash, scrollToBottom } from './render.mmmmfxhu.js';
5
- import { handleEvent, loadHistory, loadNewEvents, retryUnconfirmedPermissions } from './events.mmmmfxhu.js';
6
-
7
- export function connect() {
8
- const proto = location.protocol === 'https:' ? 'wss:' : 'ws:';
9
- setConnectionStatus('connecting', 'connecting');
10
- state.ws = new WebSocket(`${proto}//${location.host}`);
11
-
12
- state.ws.onopen = async () => {
13
- setConnectionStatus('connecting', 'session loading');
14
- // Report actual visibility to server for push notification gating
15
- state.ws.send(JSON.stringify({ type: 'visibility', visible: !document.hidden }));
16
-
17
- const existingId = getHashSessionId();
18
-
19
- // Incremental reconnect: same session still in memory — skip DOM wipe
20
- if (existingId && existingId === state.sessionId && state.lastEventSeq > 0) {
21
- await loadNewEvents(existingId);
22
- retryUnconfirmedPermissions();
23
- scrollToBottom(false);
24
- state.ws.send(JSON.stringify({ type: 'resume_session', sessionId: existingId }));
25
- return;
26
- }
27
-
28
- // Full load: different session in hash, or first connect to a hash
29
- if (existingId) {
30
- resetSessionUI();
31
- const loaded = await loadHistory(existingId);
32
- if (loaded) {
33
- scrollToBottom(true);
34
- }
35
- state.ws.send(JSON.stringify({ type: 'resume_session', sessionId: existingId }));
36
- return;
37
- }
38
-
39
- // No session in URL — try to resume last active session
40
- try {
41
- const res = await fetch('/api/sessions');
42
- const sessions = await res.json();
43
- if (sessions.length > 0) {
44
- const last = sessions[0];
45
- resetSessionUI();
46
- const loaded = await loadHistory(last.id);
47
- if (loaded) {
48
- scrollToBottom(true);
49
- }
50
- state.ws.send(JSON.stringify({ type: 'resume_session', sessionId: last.id }));
51
- return;
52
- }
53
- } catch {}
54
-
55
- // No previous sessions — create new
56
- requestNewSession();
57
- };
58
-
59
- state.ws.onclose = () => {
60
- setConnectionStatus('disconnected', 'disconnected');
61
- finishThinking();
62
- finishAssistant();
63
- if (state.currentBashEl) { finishBash(state.currentBashEl, null, 'disconnected'); }
64
- state.pendingToolCallIds.clear();
65
- state.pendingPermissionRequestIds.clear();
66
- state.pendingPromptDone = false;
67
- state.turnEnded = false;
68
- clearCancelTimer();
69
- setBusy(false);
70
- setTimeout(connect, 3000);
71
- };
72
-
73
- state.ws.onerror = () => state.ws.close();
74
-
75
- state.ws.onmessage = (e) => {
76
- const msg = JSON.parse(e.data);
77
- handleEvent(msg);
78
- };
79
- }
80
-
81
- // --- Visibility reporting for push notifications ---
82
-
83
- document.addEventListener('visibilitychange', () => {
84
- if (state.ws && state.ws.readyState === 1) {
85
- state.ws.send(JSON.stringify({ type: 'visibility', visible: !document.hidden }));
86
- }
87
- });