-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathweb-chat.html
More file actions
298 lines (286 loc) · 8.41 KB
/
Copy pathweb-chat.html
File metadata and controls
298 lines (286 loc) · 8.41 KB
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>lead-engine · channel-web demo</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style>
:root {
color-scheme: dark light;
--bg: #0b0d10;
--panel: #13161b;
--text: #e8eaed;
--muted: #8a8f98;
--accent: #6aa6ff;
--err: #ff8b8b;
--ok: #91d990;
}
html,
body {
height: 100%;
margin: 0;
background: var(--bg);
color: var(--text);
font: 14px/1.4 ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}
body {
display: grid;
grid-template-rows: auto 1fr auto;
max-width: 760px;
margin: 0 auto;
padding: 16px;
gap: 12px;
}
h1 {
font-size: 14px;
font-weight: 600;
letter-spacing: 0.05em;
text-transform: uppercase;
color: var(--muted);
margin: 0;
}
.controls {
display: grid;
grid-template-columns: 1fr 1fr 1fr auto;
gap: 8px;
background: var(--panel);
padding: 10px;
border-radius: 6px;
}
.controls label {
display: grid;
gap: 4px;
font-size: 11px;
color: var(--muted);
}
.controls input {
background: var(--bg);
border: 1px solid #2a2f37;
color: var(--text);
padding: 6px 8px;
border-radius: 4px;
font: inherit;
}
button {
background: var(--accent);
color: #0b0d10;
border: 0;
padding: 6px 12px;
border-radius: 4px;
cursor: pointer;
font: inherit;
font-weight: 600;
}
button:disabled {
opacity: 0.45;
cursor: not-allowed;
}
button.secondary {
background: transparent;
color: var(--text);
border: 1px solid #2a2f37;
}
#log {
background: var(--panel);
border-radius: 6px;
padding: 12px;
overflow-y: auto;
white-space: pre-wrap;
word-break: break-word;
font-size: 12px;
}
.row {
margin: 4px 0;
}
.row .ts {
color: var(--muted);
margin-right: 8px;
}
.row.user {
color: var(--accent);
}
.row.bot {
color: var(--ok);
}
.row.system {
color: var(--muted);
font-style: italic;
}
.row.error {
color: var(--err);
}
.compose {
display: grid;
grid-template-columns: 1fr auto;
gap: 8px;
}
.compose input {
background: var(--panel);
border: 1px solid #2a2f37;
color: var(--text);
padding: 8px 12px;
border-radius: 4px;
font: inherit;
}
.status {
display: flex;
align-items: center;
gap: 6px;
font-size: 11px;
color: var(--muted);
}
.status::before {
content: "";
width: 8px;
height: 8px;
border-radius: 50%;
background: #6c727b;
}
.status.connected::before {
background: var(--ok);
}
.status.error::before {
background: var(--err);
}
</style>
</head>
<body>
<header>
<h1>lead-engine · channel-web demo client</h1>
<p style="color: var(--muted); margin: 4px 0 0">
Standalone HTML — открыть локально (file://) или с любого host'а. Соединяется через
WebSocket с <code>ws[s]://<host>/ws/<tenantSlug></code>. Auth через
опциональный shared-secret (<code>?auth=...</code>) — должен совпадать с
<code>WEB_WS_AUTH_SECRET</code> в env apps/api.
</p>
<div id="status" class="status">disconnected</div>
</header>
<section class="controls">
<label>
host
<input id="host" value="localhost:3000" />
</label>
<label>
tenant slug
<input id="slug" value="legacy" />
</label>
<label>
user id
<input id="user" value="demo-user-1" />
</label>
<label>
auth (optional)
<input id="auth" value="" placeholder="WEB_WS_AUTH_SECRET" />
</label>
<div style="grid-column: 1 / -1; display: flex; gap: 8px">
<button id="connect">Connect</button>
<button id="disconnect" class="secondary" disabled>Disconnect</button>
<button id="clear" class="secondary">Clear log</button>
</div>
</section>
<div id="log"></div>
<form id="compose" class="compose">
<input id="text" placeholder="Type a message and press Enter" autocomplete="off" />
<button type="submit" id="send" disabled>Send</button>
</form>
<script>
const $ = (id) => document.getElementById(id);
const log = $("log");
const status = $("status");
let ws = null;
function nowTs() {
return new Date().toISOString().slice(11, 19);
}
function append(kind, text) {
const row = document.createElement("div");
row.className = `row ${kind}`;
const ts = document.createElement("span");
ts.className = "ts";
ts.textContent = nowTs();
const arrow = kind === "user" ? "→" : kind === "bot" ? "←" : "·";
row.appendChild(ts);
// textContent (not innerHTML) — message text is never parsed as HTML.
row.appendChild(document.createTextNode(`${arrow} ${text}`));
log.appendChild(row);
log.scrollTop = log.scrollHeight;
}
function setConnected(open) {
$("connect").disabled = open;
$("disconnect").disabled = !open;
$("send").disabled = !open;
status.className = open ? "status connected" : "status";
status.textContent = open ? "connected" : "disconnected";
}
function uuid() {
return crypto.randomUUID();
}
$("connect").onclick = () => {
const host = $("host").value.trim();
const slug = $("slug").value.trim();
const user = $("user").value.trim();
const auth = $("auth").value.trim();
if (!host || !slug || !user) {
append("error", "host, slug, user — все обязательны");
return;
}
const proto = location.protocol === "https:" ? "wss" : "ws";
const params = new URLSearchParams({ user });
if (auth) params.set("auth", auth);
const url = `${proto}://${host}/ws/${slug}?${params.toString()}`;
append("system", `connecting to ${url}`);
try {
ws = new WebSocket(url);
} catch (err) {
append("error", `WebSocket constructor: ${err.message}`);
return;
}
ws.addEventListener("open", () => {
setConnected(true);
});
ws.addEventListener("message", (ev) => {
let frame;
try {
frame = JSON.parse(ev.data);
} catch {
append("error", `non-JSON: ${ev.data}`);
return;
}
if (frame.type === "ready") {
append("system", `ready (channel=${frame.channelId}, user=${frame.userId})`);
} else if (frame.type === "bot_text") {
append("bot", frame.text);
} else if (frame.type === "error") {
append("error", `server: ${frame.code} — ${frame.message}`);
} else {
append("system", JSON.stringify(frame));
}
});
ws.addEventListener("close", (ev) => {
append("system", `closed code=${ev.code}${ev.reason ? ` reason=${ev.reason}` : ""}`);
setConnected(false);
ws = null;
});
ws.addEventListener("error", () => {
status.className = "status error";
status.textContent = "error (см. devtools)";
});
};
$("disconnect").onclick = () => {
ws?.close(1000, "client requested");
};
$("clear").onclick = () => {
log.innerHTML = "";
};
$("compose").onsubmit = (e) => {
e.preventDefault();
if (!ws || ws.readyState !== WebSocket.OPEN) return;
const text = $("text").value.trim();
if (!text) return;
const id = uuid();
ws.send(JSON.stringify({ type: "user_text", id, text }));
append("user", text);
$("text").value = "";
};
</script>
</body>
</html>