-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsse-test.html
More file actions
538 lines (489 loc) · 20.6 KB
/
Copy pathsse-test.html
File metadata and controls
538 lines (489 loc) · 20.6 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
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>SSE 流式传输测试</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
max-width: 1000px;
margin: 0 auto;
padding: 20px;
background-color: #f8fafc;
line-height: 1.6;
}
.container {
background: white;
padding: 30px;
border-radius: 12px;
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
margin-bottom: 20px;
}
h1 {
color: #2d3748;
text-align: center;
margin-bottom: 30px;
}
.test-section {
margin-bottom: 30px;
border: 1px solid #e2e8f0;
border-radius: 8px;
padding: 20px;
}
.test-section h3 {
color: #4a5568;
margin-top: 0;
margin-bottom: 15px;
}
.input-group {
margin-bottom: 15px;
}
label {
display: block;
margin-bottom: 5px;
font-weight: 600;
color: #2d3748;
}
input[type="text"], textarea {
width: 100%;
padding: 10px;
border: 1px solid #d1d5db;
border-radius: 6px;
font-size: 16px;
box-sizing: border-box;
}
textarea {
min-height: 80px;
resize: vertical;
}
button {
background: #3b82f6;
color: white;
border: none;
padding: 12px 24px;
border-radius: 6px;
cursor: pointer;
margin-right: 10px;
margin-bottom: 10px;
font-size: 16px;
font-weight: 500;
transition: background-color 0.2s;
}
button:hover:not(:disabled) {
background: #2563eb;
}
button:disabled {
background: #9ca3af;
cursor: not-allowed;
}
button.stop {
background: #ef4444;
}
button.stop:hover:not(:disabled) {
background: #dc2626;
}
button.clear {
background: #6b7280;
}
button.clear:hover:not(:disabled) {
background: #4b5563;
}
.response-section {
margin-top: 20px;
}
.response-box {
background: #f8fafc;
border: 1px solid #e2e8f0;
border-radius: 6px;
padding: 15px;
min-height: 150px;
white-space: pre-wrap;
font-family: 'SF Mono', Monaco, 'Cascadia Code', 'Roboto Mono', Consolas, 'Courier New', monospace;
line-height: 1.5;
font-size: 14px;
max-height: 400px;
overflow-y: auto;
}
.status {
margin: 10px 0;
padding: 12px;
border-radius: 6px;
font-weight: 500;
}
.status.info {
background: #dbeafe;
color: #1e40af;
border: 1px solid #93c5fd;
}
.status.error {
background: #fee2e2;
color: #dc2626;
border: 1px solid #fca5a5;
}
.status.success {
background: #dcfce7;
color: #166534;
border: 1px solid #86efac;
}
.stats {
display: flex;
gap: 20px;
margin-top: 10px;
font-size: 14px;
color: #6b7280;
}
.stats span {
background: #f3f4f6;
padding: 4px 8px;
border-radius: 4px;
}
.hidden {
display: none;
}
code {
background: #f1f5f9;
color: #0f172a;
padding: 2px 6px;
border-radius: 4px;
font-family: 'SF Mono', Monaco, 'Cascadia Code', 'Roboto Mono', Consolas, 'Courier New', monospace;
font-size: 0.9em;
}
</style>
</head>
<body>
<div class="container">
<h1>🚀 SSE 流式传输测试</h1>
<div class="status info" style="margin-bottom: 20px;">
📌 <strong>注意:</strong>请确保后端服务运行在 <code>http://localhost:8100</code>
</div>
<!-- 简单测试 -->
<div class="test-section">
<h3>📝 简单文本流测试</h3>
<div class="input-group">
<label for="simpleText">测试文本:</label>
<input
type="text"
id="simpleText"
placeholder="输入要流式传输的文本..."
value="Hello SSE streaming! 这是一个流式传输测试。"
/>
</div>
<button onclick="startSimpleTest()" id="simpleTestBtn">开始简单测试</button>
<button onclick="stopStream()" class="stop" id="stopBtn" disabled>停止流</button>
<button onclick="clearResponse('simpleResponse')" class="clear">清空</button>
<div class="response-section">
<label>实时响应:</label>
<div id="simpleResponse" class="response-box"></div>
<div class="stats">
<span>接收到: <span id="simpleChunks">0</span> 个数据块</span>
<span>耗时: <span id="simpleTime">0</span>ms</span>
</div>
</div>
</div>
<!-- AI对话测试 -->
<div class="test-section">
<h3>🤖 AI 对话流测试</h3>
<div class="input-group">
<label for="aiQuestion">问题:</label>
<textarea
id="aiQuestion"
placeholder="输入你想问AI的问题..."
>请简单介绍一下区块链技术,包括它的基本概念、工作原理和主要应用场景。</textarea>
</div>
<div class="input-group">
<label for="authToken">认证Token(可选):</label>
<input
type="text"
id="authToken"
placeholder="Bearer token..."
/>
</div>
<button onclick="startAITest()" id="aiTestBtn">开始AI对话</button>
<button onclick="stopStream()" class="stop" id="stopAiBtn" disabled>停止对话</button>
<button onclick="clearResponse('aiResponse')" class="clear">清空</button>
<div class="response-section">
<label>AI响应:</label>
<div id="aiResponse" class="response-box"></div>
<div class="stats">
<span>接收到: <span id="aiChunks">0</span> 个数据块</span>
<span>耗时: <span id="aiTime">0</span>ms</span>
</div>
</div>
</div>
<!-- 使用GET接口的EventSource测试 -->
<div class="test-section">
<h3>📡 EventSource (GET) 测试</h3>
<div class="input-group">
<label for="eventSourceQuestion">问题:</label>
<input
type="text"
id="eventSourceQuestion"
placeholder="输入问题..."
value="什么是人工智能?"
/>
</div>
<button onclick="startEventSourceTest()" id="eventSourceBtn">使用EventSource</button>
<button onclick="stopEventSource()" class="stop" id="stopEventSourceBtn" disabled>停止连接</button>
<button onclick="clearResponse('eventSourceResponse')" class="clear">清空</button>
<div class="response-section">
<label>EventSource响应:</label>
<div id="eventSourceResponse" class="response-box"></div>
<div class="stats">
<span>连接状态: <span id="esStatus">未连接</span></span>
<span>接收到: <span id="esChunks">0</span> 个数据块</span>
</div>
</div>
</div>
<div id="status" class="status hidden"></div>
</div>
<script>
let currentEventSource = null;
let startTime = 0;
let chunkCount = 0;
function showStatus(message, type = 'info') {
const statusDiv = document.getElementById('status');
statusDiv.textContent = message;
statusDiv.className = `status ${type}`;
statusDiv.classList.remove('hidden');
}
function hideStatus() {
document.getElementById('status').classList.add('hidden');
}
function clearResponse(elementId) {
document.getElementById(elementId).textContent = '';
if (elementId === 'simpleResponse') {
document.getElementById('simpleChunks').textContent = '0';
document.getElementById('simpleTime').textContent = '0';
} else if (elementId === 'aiResponse') {
document.getElementById('aiChunks').textContent = '0';
document.getElementById('aiTime').textContent = '0';
} else if (elementId === 'eventSourceResponse') {
document.getElementById('esChunks').textContent = '0';
document.getElementById('esStatus').textContent = '未连接';
}
hideStatus();
}
function updateStats(type, chunks, time) {
if (type === 'simple') {
document.getElementById('simpleChunks').textContent = chunks;
document.getElementById('simpleTime').textContent = time;
} else if (type === 'ai') {
document.getElementById('aiChunks').textContent = chunks;
document.getElementById('aiTime').textContent = time;
} else if (type === 'es') {
document.getElementById('esChunks').textContent = chunks;
}
}
// 简单测试
async function startSimpleTest() {
const text = document.getElementById('simpleText').value.trim();
if (!text) {
showStatus('请输入测试文本', 'error');
return;
}
clearResponse('simpleResponse');
showStatus('正在连接简单测试流...', 'info');
document.getElementById('simpleTestBtn').disabled = true;
document.getElementById('stopBtn').disabled = false;
startTime = Date.now();
chunkCount = 0;
try {
const response = await fetch('http://localhost:8100/api/ai-agent/simple-test', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ text: text })
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const reader = response.body.getReader();
const decoder = new TextDecoder();
const responseDiv = document.getElementById('simpleResponse');
while (true) {
const { done, value } = await reader.read();
if (done) break;
const chunk = decoder.decode(value, { stream: true });
console.log('Received simple chunk:', chunk);
// 解析SSE格式
const lines = chunk.split('\n');
for (const line of lines) {
if (line.startsWith('data: ')) {
const data = line.substring(6);
if (data === '[DONE]') {
showStatus('简单测试完成', 'success');
updateStats('simple', chunkCount, Date.now() - startTime);
document.getElementById('simpleTestBtn').disabled = false;
document.getElementById('stopBtn').disabled = true;
return;
}
try {
const parsed = JSON.parse(data);
if (parsed.content) {
responseDiv.textContent += parsed.content;
chunkCount++;
updateStats('simple', chunkCount, Date.now() - startTime);
}
} catch (e) {
console.error('Failed to parse JSON:', data);
}
}
}
}
} catch (error) {
console.error('Simple test error:', error);
showStatus(`简单测试错误: ${error.message}`, 'error');
document.getElementById('simpleTestBtn').disabled = false;
document.getElementById('stopBtn').disabled = true;
}
}
// AI对话测试
async function startAITest() {
const question = document.getElementById('aiQuestion').value.trim();
const token = document.getElementById('authToken').value.trim();
if (!question) {
showStatus('请输入问题', 'error');
return;
}
clearResponse('aiResponse');
showStatus('正在连接AI对话流...', 'info');
document.getElementById('aiTestBtn').disabled = true;
document.getElementById('stopAiBtn').disabled = false;
startTime = Date.now();
chunkCount = 0;
try {
const headers = {
'Content-Type': 'application/json'
};
if (token) {
headers['Authorization'] = token.startsWith('Bearer ') ? token : `Bearer ${token}`;
}
const response = await fetch('http://localhost:8100/api/ai-agent/session', {
method: 'POST',
headers: headers,
body: JSON.stringify({ question: question })
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const reader = response.body.getReader();
const decoder = new TextDecoder();
const responseDiv = document.getElementById('aiResponse');
while (true) {
const { done, value } = await reader.read();
if (done) break;
const chunk = decoder.decode(value, { stream: true });
console.log('Received AI chunk:', chunk);
// 解析SSE格式
const lines = chunk.split('\n');
for (const line of lines) {
if (line.startsWith('data: ')) {
const data = line.substring(6);
if (data === '[DONE]') {
showStatus('AI对话完成', 'success');
updateStats('ai', chunkCount, Date.now() - startTime);
document.getElementById('aiTestBtn').disabled = false;
document.getElementById('stopAiBtn').disabled = true;
return;
}
try {
const parsed = JSON.parse(data);
if (parsed.content) {
responseDiv.textContent += parsed.content;
chunkCount++;
updateStats('ai', chunkCount, Date.now() - startTime);
} else if (parsed.error) {
showStatus(`AI错误: ${parsed.error}`, 'error');
}
} catch (e) {
console.error('Failed to parse AI JSON:', data);
}
}
}
}
} catch (error) {
console.error('AI test error:', error);
showStatus(`AI对话错误: ${error.message}`, 'error');
document.getElementById('aiTestBtn').disabled = false;
document.getElementById('stopAiBtn').disabled = true;
}
}
// EventSource测试
function startEventSourceTest() {
const question = document.getElementById('eventSourceQuestion').value.trim();
if (!question) {
showStatus('请输入问题', 'error');
return;
}
clearResponse('eventSourceResponse');
showStatus('正在使用EventSource连接...', 'info');
document.getElementById('eventSourceBtn').disabled = true;
document.getElementById('stopEventSourceBtn').disabled = false;
chunkCount = 0;
const responseDiv = document.getElementById('eventSourceResponse');
// 创建EventSource连接
const url = `http://localhost:8100/api/ai-agent/chat-stream?question=${encodeURIComponent(question)}`;
currentEventSource = new EventSource(url);
document.getElementById('esStatus').textContent = '连接中...';
currentEventSource.onopen = function(event) {
console.log('EventSource connection opened');
document.getElementById('esStatus').textContent = '已连接';
showStatus('EventSource连接成功', 'success');
};
currentEventSource.onmessage = function(event) {
console.log('EventSource message:', event.data);
if (event.data === '[DONE]') {
showStatus('EventSource对话完成', 'success');
stopEventSource();
return;
}
try {
const parsed = JSON.parse(event.data);
if (parsed.content) {
responseDiv.textContent += parsed.content;
chunkCount++;
updateStats('es', chunkCount, 0);
} else if (parsed.status === 'connected') {
showStatus('EventSource已连接,开始接收数据...', 'info');
} else if (parsed.error) {
showStatus(`EventSource错误: ${parsed.error}`, 'error');
}
} catch (e) {
console.error('Failed to parse EventSource data:', event.data);
}
};
currentEventSource.onerror = function(error) {
console.error('EventSource error:', error);
showStatus('EventSource连接错误', 'error');
document.getElementById('esStatus').textContent = '连接错误';
stopEventSource();
};
}
function stopEventSource() {
if (currentEventSource) {
currentEventSource.close();
currentEventSource = null;
}
document.getElementById('eventSourceBtn').disabled = false;
document.getElementById('stopEventSourceBtn').disabled = true;
document.getElementById('esStatus').textContent = '已断开';
}
function stopStream() {
// 这里可以添加中断当前请求的逻辑
document.getElementById('simpleTestBtn').disabled = false;
document.getElementById('aiTestBtn').disabled = false;
document.getElementById('stopBtn').disabled = true;
document.getElementById('stopAiBtn').disabled = true;
showStatus('已停止流传输', 'info');
}
// 页面卸载时清理EventSource
window.addEventListener('beforeunload', function() {
if (currentEventSource) {
currentEventSource.close();
}
});
</script>
</body>
</html>