-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathindex.html
More file actions
826 lines (714 loc) · 20.8 KB
/
index.html
File metadata and controls
826 lines (714 loc) · 20.8 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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Speakeasy — Chat with Alex</title>
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link href="https://fonts.googleapis.com/css2?family=Lora:ital,wght@0,400;0,500;1,400&family=DM+Sans:opsz,wght@9..40,300;9..40,400;9..40,500&display=swap" rel="stylesheet" />
<style>
:root {
--cream: #f5f0e8;
--cream-dark: #e8e0d0;
--cream-panel: #f0ead8;
--ink: #28200f;
--ink-mid: #5a5040;
--ink-light: #9a8e7a;
--sage: #6b9478;
--sage-dark: #4e7a5c;
--sage-pale: #d4e8da;
--white: #ffffff;
--shadow-sm: 0 1px 4px rgba(40,32,15,0.07);
--shadow-md: 0 4px 16px rgba(40,32,15,0.10);
--r-bubble: 20px;
--r-panel: 16px;
}
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html, body {
height: 100%;
background: var(--cream);
color: var(--ink);
font-family: 'DM Sans', sans-serif;
font-size: 16px;
-webkit-font-smoothing: antialiased;
}
/* ═══════════════════════════════════════
APP SHELL — desktop two-column layout
═══════════════════════════════════════ */
.app {
display: grid;
grid-template-columns: 280px 1fr;
grid-template-rows: 100vh;
height: 100vh;
max-width: 1200px;
margin: 0 auto;
}
/* ── Left Panel ── */
.panel {
background: var(--cream-panel);
border-right: 1px solid var(--cream-dark);
display: flex;
flex-direction: column;
padding: 32px 24px;
gap: 32px;
overflow-y: auto;
}
.brand {
display: flex;
align-items: center;
gap: 12px;
}
.brand-avatar {
width: 48px;
height: 48px;
background: linear-gradient(135deg, var(--sage) 0%, var(--sage-dark) 100%);
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
font-size: 22px;
box-shadow: var(--shadow-md);
flex-shrink: 0;
}
.brand-text h1 {
font-family: 'Lora', serif;
font-size: 20px;
font-weight: 500;
line-height: 1.2;
}
.brand-text p {
font-size: 13px;
color: var(--sage-dark);
margin-top: 3px;
display: flex;
align-items: center;
gap: 5px;
}
.brand-text p::before {
content: '';
width: 7px;
height: 7px;
background: var(--sage);
border-radius: 50%;
animation: pulse 2.5s infinite;
}
@keyframes pulse {
0%, 100% { opacity: 1; transform: scale(1); }
50% { opacity: 0.5; transform: scale(0.85); }
}
.panel-section h3 {
font-size: 11px;
font-weight: 500;
letter-spacing: 0.08em;
text-transform: uppercase;
color: var(--ink-light);
margin-bottom: 12px;
}
.panel-section p {
font-size: 14px;
color: var(--ink-mid);
line-height: 1.7;
}
.topic-list {
display: flex;
flex-direction: column;
gap: 8px;
}
.topic-btn {
background: var(--white);
border: 1.5px solid var(--cream-dark);
color: var(--ink);
font-family: 'DM Sans', sans-serif;
font-size: 14px;
padding: 10px 14px;
border-radius: 12px;
cursor: pointer;
text-align: left;
transition: all 0.2s;
display: flex;
align-items: center;
gap: 10px;
}
.topic-btn:hover {
border-color: var(--sage);
background: var(--sage-pale);
color: var(--sage-dark);
transform: translateX(3px);
}
.topic-icon { font-size: 16px; flex-shrink: 0; }
.end-btn {
margin-top: auto;
background: transparent;
border: 1.5px solid var(--cream-dark);
color: var(--ink-mid);
font-family: 'DM Sans', sans-serif;
font-size: 14px;
font-weight: 500;
padding: 11px 16px;
border-radius: 12px;
cursor: pointer;
transition: all 0.2s;
display: flex;
align-items: center;
justify-content: center;
gap: 8px;
}
.end-btn:hover {
border-color: var(--sage);
color: var(--sage-dark);
background: var(--sage-pale);
}
/* ── Right: Chat Column ── */
.chat-col {
display: flex;
flex-direction: column;
height: 100vh;
overflow: hidden;
}
/* ── Chat Header ── */
.chat-header {
padding: 20px 32px;
border-bottom: 1px solid var(--cream-dark);
display: flex;
align-items: center;
justify-content: space-between;
flex-shrink: 0;
background: var(--cream);
}
.chat-header-left {
display: flex;
align-items: center;
gap: 12px;
}
.chat-avatar {
width: 40px;
height: 40px;
background: linear-gradient(135deg, var(--sage) 0%, var(--sage-dark) 100%);
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
font-size: 18px;
box-shadow: var(--shadow-sm);
}
.chat-header h2 {
font-family: 'Lora', serif;
font-size: 18px;
font-weight: 500;
}
.chat-header .sub {
font-size: 13px;
color: var(--ink-light);
margin-top: 2px;
}
.turn-counter {
font-size: 13px;
color: var(--ink-light);
background: var(--cream-dark);
padding: 5px 12px;
border-radius: 20px;
}
/* ── Messages ── */
#chat {
flex: 1;
overflow-y: auto;
padding: 32px;
display: flex;
flex-direction: column;
gap: 20px;
scroll-behavior: smooth;
}
#chat::-webkit-scrollbar { width: 5px; }
#chat::-webkit-scrollbar-track { background: transparent; }
#chat::-webkit-scrollbar-thumb { background: var(--cream-dark); border-radius: 4px; }
/* ── Welcome ── */
.welcome {
text-align: center;
padding: 48px 24px 24px;
animation: fadeUp 0.5s ease;
max-width: 520px;
margin: 0 auto;
}
.welcome-icon { font-size: 48px; margin-bottom: 16px; display: block; }
.welcome h2 {
font-family: 'Lora', serif;
font-size: 28px;
font-weight: 500;
color: var(--ink);
margin-bottom: 12px;
}
.welcome p {
font-size: 16px;
color: var(--ink-mid);
line-height: 1.75;
}
@keyframes fadeUp {
from { opacity: 0; transform: translateY(12px); }
to { opacity: 1; transform: translateY(0); }
}
/* ── Message rows ── */
.msg {
display: flex;
gap: 14px;
animation: fadeUp 0.25s ease;
max-width: 720px;
}
.msg.user {
flex-direction: row-reverse;
align-self: flex-end;
}
.msg.ai {
align-self: flex-start;
}
.msg-avatar {
width: 36px;
height: 36px;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
font-size: 16px;
flex-shrink: 0;
margin-top: 2px;
}
.msg.ai .msg-avatar {
background: linear-gradient(135deg, var(--sage) 0%, var(--sage-dark) 100%);
box-shadow: var(--shadow-sm);
}
.msg.user .msg-avatar {
background: var(--cream-dark);
}
.bubble {
padding: 14px 18px;
border-radius: var(--r-bubble);
font-size: 16px;
line-height: 1.65;
box-shadow: var(--shadow-sm);
max-width: 560px;
}
.msg.ai .bubble {
background: var(--white);
border-bottom-left-radius: 5px;
color: var(--ink);
}
.msg.user .bubble {
background: var(--sage-pale);
border-bottom-right-radius: 5px;
color: var(--ink);
}
/* ── Typing dots ── */
.typing .bubble { padding: 16px 20px; }
.dots { display: flex; gap: 6px; align-items: center; }
.dots span {
width: 8px;
height: 8px;
background: var(--sage);
border-radius: 50%;
animation: bounce 1.3s infinite;
}
.dots span:nth-child(2) { animation-delay: 0.2s; }
.dots span:nth-child(3) { animation-delay: 0.4s; }
@keyframes bounce {
0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
30% { transform: translateY(-7px); opacity: 1; }
}
/* ── Tip card ── */
.tip-card {
background: linear-gradient(135deg, var(--sage-dark) 0%, var(--sage) 100%);
border-radius: var(--r-panel);
padding: 24px 28px;
color: white;
animation: fadeUp 0.5s ease;
box-shadow: 0 6px 24px rgba(78,122,92,0.3);
max-width: 560px;
align-self: center;
}
.tip-label {
font-size: 11px;
font-weight: 500;
letter-spacing: 0.1em;
text-transform: uppercase;
opacity: 0.75;
margin-bottom: 10px;
}
.tip-text {
font-family: 'Lora', serif;
font-size: 18px;
line-height: 1.65;
}
.tip-restart {
margin-top: 20px;
background: rgba(255,255,255,0.18);
border: 1.5px solid rgba(255,255,255,0.4);
color: white;
font-family: 'DM Sans', sans-serif;
font-size: 14px;
font-weight: 500;
padding: 10px 22px;
border-radius: 20px;
cursor: pointer;
transition: all 0.2s;
}
.tip-restart:hover { background: rgba(255,255,255,0.28); }
/* ── Divider ── */
.divider {
text-align: center;
font-size: 13px;
color: var(--ink-light);
position: relative;
align-self: stretch;
}
.divider::before, .divider::after {
content: '';
position: absolute;
top: 50%;
width: 40%;
height: 1px;
background: var(--cream-dark);
}
.divider::before { left: 0; }
.divider::after { right: 0; }
/* ── Input Area ── */
.input-area {
padding: 16px 32px 24px;
border-top: 1px solid var(--cream-dark);
background: var(--cream);
flex-shrink: 0;
}
.input-wrap {
display: flex;
gap: 12px;
align-items: flex-end;
background: var(--white);
border: 1.5px solid var(--cream-dark);
border-radius: 24px;
padding: 12px 12px 12px 22px;
box-shadow: var(--shadow-sm);
transition: border-color 0.2s, box-shadow 0.2s;
}
.input-wrap:focus-within {
border-color: var(--sage);
box-shadow: 0 0 0 3px rgba(107,148,120,0.12);
}
#input {
flex: 1;
border: none;
outline: none;
background: transparent;
font-family: 'DM Sans', sans-serif;
font-size: 16px;
color: var(--ink);
resize: none;
max-height: 140px;
line-height: 1.55;
min-height: 26px;
}
#input::placeholder { color: var(--ink-light); opacity: 0.65; }
#send {
width: 42px;
height: 42px;
background: linear-gradient(135deg, var(--sage) 0%, var(--sage-dark) 100%);
border: none;
border-radius: 50%;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
flex-shrink: 0;
transition: all 0.2s;
box-shadow: 0 2px 8px rgba(78,122,92,0.3);
}
#send:hover { transform: scale(1.08); box-shadow: 0 4px 12px rgba(78,122,92,0.4); }
#send:active { transform: scale(0.94); }
#send:disabled { opacity: 0.35; cursor: not-allowed; transform: none; box-shadow: none; }
.hint {
text-align: center;
font-size: 12px;
color: var(--ink-light);
opacity: 0.55;
margin-top: 8px;
}
/* ── Error ── */
.error-msg {
text-align: center;
font-size: 13px;
color: #b06050;
padding: 6px;
align-self: center;
}
/* ── Responsive: hide panel on narrow screens ── */
@media (max-width: 768px) {
.app { grid-template-columns: 1fr; }
.panel { display: none; }
#chat { padding: 20px 16px; }
.input-area { padding: 12px 16px 20px; }
.chat-header { padding: 16px 20px; }
}
</style>
</head>
<body>
<div class="app">
<!-- ── Left Panel ── -->
<aside class="panel">
<div class="brand">
<div class="brand-avatar">🌿</div>
<div class="brand-text">
<h1>Speakeasy</h1>
<p>Alex 在线</p>
</div>
</div>
<div class="panel-section">
<h3>关于 Alex</h3>
<p>英语母语朋友,永远不评判你,在聊天中悄悄帮你变流利。</p>
</div>
<div class="panel-section">
<h3>今天聊什么</h3>
<div class="topic-list">
<button class="topic-btn" onclick="startWith('Tell me about your day at work')">
<span class="topic-icon">💼</span> 工作日常
</button>
<button class="topic-btn" onclick="startWith('Something funny happened to me today')">
<span class="topic-icon">😄</span> 趣事分享
</button>
<button class="topic-btn" onclick="startWith('I watched a really good movie recently')">
<span class="topic-icon">🎬</span> 影视剧
</button>
<button class="topic-btn" onclick="startWith('I want to talk about my travel plans')">
<span class="topic-icon">✈️</span> 旅行计划
</button>
<button class="topic-btn" onclick="startWith('Let me tell you about my weekend')">
<span class="topic-icon">🌿</span> 周末生活
</button>
<button class="topic-btn" onclick="startWith('I have been thinking about changing jobs')">
<span class="topic-icon">🔄</span> 职业规划
</button>
</div>
</div>
<div class="panel-section">
<h3>使用提示</h3>
<p>不懂 Alex 说的词?直接问他,他会用更简单的方式解释。</p>
</div>
<button class="end-btn" onclick="endSession()">
✨ 结束对话,获取今日一句
</button>
</aside>
<!-- ── Right: Chat ── -->
<div class="chat-col">
<div class="chat-header">
<div class="chat-header-left">
<div class="chat-avatar">🌿</div>
<div>
<h2>Alex</h2>
<div class="sub">随时可以聊</div>
</div>
</div>
<div class="turn-counter" id="turnCounter">0 轮对话</div>
</div>
<div id="chat">
<div class="welcome" id="welcome">
<span class="welcome-icon">👋</span>
<h2>Hey, I'm Alex!</h2>
<p>今天想聊什么?从左边选一个话题,或者直接打字。<br/>没有对错,就像和朋友发消息一样。</p>
</div>
</div>
<div class="input-area">
<div class="input-wrap">
<textarea id="input" rows="1"
placeholder="Type in English... or mix Chinese, it's totally fine"
onkeydown="handleKey(event)"
oninput="autoResize(this)"></textarea>
<button id="send" onclick="sendMessage()">
<svg width="18" height="18" viewBox="0 0 18 18" fill="none">
<path d="M16 9L2 2.5l2.8 6.5L2 15.5 16 9z" fill="white"/>
</svg>
</button>
</div>
<div class="hint">Enter 发送 · Shift+Enter 换行 · 不懂的词直接问 Alex</div>
</div>
</div>
</div>
<script>
const API = 'http://localhost:8000';
let sessionId = crypto.randomUUID();
let history = [];
let turnCount = 0;
let isLoading = false;
const chatEl = document.getElementById('chat');
const inputEl = document.getElementById('input');
const sendBtn = document.getElementById('send');
const turnEl = document.getElementById('turnCounter');
function updateTurnCounter() {
turnEl.textContent = `${turnCount} 轮对话`;
}
function autoResize(el) {
el.style.height = 'auto';
el.style.height = Math.min(el.scrollHeight, 140) + 'px';
}
function handleKey(e) {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
sendMessage();
}
}
function startWith(text) {
inputEl.value = text;
autoResize(inputEl);
inputEl.focus();
sendMessage();
}
function hideWelcome() {
const w = document.getElementById('welcome');
if (w) w.remove();
}
function addMessage(role, content) {
hideWelcome();
const wrap = document.createElement('div');
wrap.className = `msg ${role}`;
const avatar = document.createElement('div');
avatar.className = 'msg-avatar';
avatar.textContent = role === 'ai' ? '🌿' : '🙂';
const bubble = document.createElement('div');
bubble.className = 'bubble';
bubble.textContent = content;
wrap.appendChild(avatar);
wrap.appendChild(bubble);
chatEl.appendChild(wrap);
scrollDown();
return wrap;
}
function addTyping() {
const wrap = document.createElement('div');
wrap.className = 'msg ai typing';
wrap.id = 'typing';
const avatar = document.createElement('div');
avatar.className = 'msg-avatar';
avatar.textContent = '🌿';
const bubble = document.createElement('div');
bubble.className = 'bubble';
bubble.innerHTML = '<div class="dots"><span></span><span></span><span></span></div>';
wrap.appendChild(avatar);
wrap.appendChild(bubble);
chatEl.appendChild(wrap);
scrollDown();
}
function removeTyping() {
const t = document.getElementById('typing');
if (t) t.remove();
}
function scrollDown() {
setTimeout(() => chatEl.scrollTop = chatEl.scrollHeight, 50);
}
function setLoading(val) {
isLoading = val;
sendBtn.disabled = val;
inputEl.disabled = val;
}
async function sendMessage() {
const text = inputEl.value.trim();
if (!text || isLoading) return;
inputEl.value = '';
inputEl.style.height = 'auto';
setLoading(true);
addMessage('user', text);
addTyping();
try {
const res = await fetch(`${API}/chat`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
session_id: sessionId,
message: text,
history: history
})
});
const data = await res.json();
removeTyping();
if (res.ok) {
const reply = data.reply;
addMessage('ai', reply);
history.push({ role: 'user', content: text });
history.push({ role: 'assistant', content: reply });
turnCount++;
updateTurnCounter();
} else {
addError(data.error || '出了点问题,再试一次?');
}
} catch (e) {
removeTyping();
addError('连接不上服务,确认后端已启动(localhost:8000)');
}
setLoading(false);
inputEl.focus();
}
function addError(msg) {
const div = document.createElement('div');
div.className = 'error-msg';
div.textContent = '⚠ ' + msg;
chatEl.appendChild(div);
scrollDown();
}
async function endSession() {
if (history.length === 0) {
inputEl.focus();
return;
}
const btn = document.querySelector('.end-btn');
btn.textContent = '生成中...';
btn.disabled = true;
try {
const res = await fetch(`${API}/chat/summary`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ session_id: sessionId, history })
});
const data = await res.json();
const divider = document.createElement('div');
divider.className = 'divider';
divider.textContent = '对话结束';
chatEl.appendChild(divider);
if (res.ok) showTipCard(data.tip);
else addError('今日一句生成失败,请重试');
} catch (e) {
addError('连接失败,请重试');
}
btn.textContent = '✨ 结束对话,获取今日一句';
btn.disabled = false;
}
function showTipCard(tip) {
const card = document.createElement('div');
card.className = 'tip-card';
card.innerHTML = `
<div class="tip-label">✨ 今日收获</div>
<div class="tip-text">${tip}</div>
<button class="tip-restart" onclick="restartSession()">开始新对话</button>
`;
chatEl.appendChild(card);
scrollDown();
inputEl.disabled = true;
sendBtn.disabled = true;
inputEl.placeholder = '点击「开始新对话」继续';
}
function restartSession() {
sessionId = crypto.randomUUID();
history = [];
turnCount = 0;
updateTurnCounter();
chatEl.innerHTML = '';
inputEl.disabled = false;
sendBtn.disabled = false;
inputEl.placeholder = 'Type in English... or mix Chinese, it\'s totally fine';
const welcome = document.createElement('div');
welcome.className = 'welcome';
welcome.id = 'welcome';
welcome.innerHTML = `
<span class="welcome-icon">👋</span>
<h2>Hey, I'm Alex!</h2>
<p>今天想聊什么?从左边选一个话题,或者直接打字。<br/>没有对错,就像和朋友发消息一样。</p>
`;
chatEl.appendChild(welcome);
inputEl.focus();
}
updateTurnCounter();
</script>
</body>
</html>