-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev-page.html
More file actions
170 lines (159 loc) · 6.97 KB
/
Copy pathdev-page.html
File metadata and controls
170 lines (159 loc) · 6.97 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
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<title>openchat dev</title>
<style>
* { box-sizing: border-box; }
body { margin: 0; font: 14px/1.5 -apple-system, "Microsoft YaHei", sans-serif; background: #1a1a1a; color: #e0e0e0; height: 100vh; display: flex; flex-direction: column; }
#log { flex: 1; overflow-y: auto; padding: 16px; }
.msg { margin: 8px 0; padding: 8px 12px; border-radius: 6px; }
.msg.user { background: #2a4a6e; border-left: 3px solid #4a90e2; }
.msg.assistant { background: #2a2a2a; border-left: 3px solid #888; }
.msg.system { background: #3a3a2a; color: #aaa; font-size: 12px; border-left: 3px solid #888; }
.msg.error { background: #4a2a2a; color: #ff8080; border-left: 3px solid #e74c3c; }
.think { color: #888; font-size: 12px; font-style: italic; padding: 4px 0 4px 12px; border-left: 2px solid #444; margin: 4px 0; }
.tool { color: #f0a040; font-family: monospace; padding: 4px 0 4px 12px; border-left: 2px solid #f0a040; margin: 4px 0; }
.tool-result { color: #80c080; font-family: monospace; padding: 4px 0 4px 12px; border-left: 2px solid #80c080; margin: 4px 0; font-size: 12px; }
.content { white-space: pre-wrap; }
.label { display: inline-block; padding: 1px 6px; border-radius: 3px; font-size: 11px; font-weight: bold; margin-right: 6px; }
.label.think { background: #444; color: #aaa; }
.label.tool { background: #f0a040; color: #000; }
.label.result { background: #80c080; color: #000; }
.label.final { background: #4a90e2; color: #fff; }
#input-row { border-top: 1px solid #333; padding: 12px; display: flex; gap: 8px; background: #222; }
#input { flex: 1; padding: 8px 12px; background: #1a1a1a; color: #e0e0e0; border: 1px solid #444; border-radius: 4px; font: inherit; }
#input:focus { outline: none; border-color: #4a90e2; }
#send { padding: 8px 20px; background: #4a90e2; color: #fff; border: none; border-radius: 4px; cursor: pointer; font: inherit; font-weight: bold; }
#send:disabled { background: #444; cursor: not-allowed; }
.spinner { display: inline-block; animation: spin 1s linear infinite; }
@keyframes spin { from { transform: rotate(0); } to { transform: rotate(360deg); } }
pre { background: #111; padding: 8px; border-radius: 4px; overflow-x: auto; font-size: 12px; }
</style>
</head>
<body>
<div id="log"></div>
<div id="input-row">
<input id="input" placeholder="说点什么... (Enter 发送, Shift+Enter 换行)" autofocus>
<button id="send">发送</button>
</div>
<script>
const log = document.getElementById('log');
const input = document.getElementById('input');
const sendBtn = document.getElementById('send');
let sessionId = null;
let currentAssistantDiv = null;
let busy = false;
function appendMsg(role, content) {
const div = document.createElement('div');
div.className = 'msg ' + role;
div.innerHTML = content;
log.appendChild(div);
log.scrollTop = log.scrollHeight;
return div;
}
function appendThink(text) {
if (!currentAssistantDiv) currentAssistantDiv = appendMsg('assistant', '');
const d = document.createElement('div');
d.className = 'think';
d.innerHTML = '<span class="label think">think</span>' + escapeHtml(text);
currentAssistantDiv.appendChild(d);
log.scrollTop = log.scrollHeight;
}
function appendTool(name, args) {
if (!currentAssistantDiv) currentAssistantDiv = appendMsg('assistant', '');
const d = document.createElement('div');
d.className = 'tool';
const argsStr = Object.entries(args || {}).map(([k,v]) => k+'='+(typeof v === 'string' ? v.substring(0,40) : JSON.stringify(v).substring(0,40))).join(', ');
d.innerHTML = '<span class="label tool">→ '+escapeHtml(name)+'</span>' + (argsStr ? '(' + escapeHtml(argsStr) + ')' : '');
currentAssistantDiv.appendChild(d);
log.scrollTop = log.scrollHeight;
}
function appendToolResult(tool, result) {
if (!currentAssistantDiv) return;
const d = document.createElement('div');
d.className = 'tool-result';
const preview = typeof result === 'string' ? result.substring(0, 200) : JSON.stringify(result).substring(0, 200);
d.innerHTML = '<span class="label result">← '+escapeHtml(tool)+'</span><pre style="margin:4px 0 0 0">'+escapeHtml(preview)+(preview.length >= 200 ? '... (truncated)' : '')+'</pre>';
currentAssistantDiv.appendChild(d);
log.scrollTop = log.scrollHeight;
}
function appendFinal(text) {
if (!currentAssistantDiv) currentAssistantDiv = appendMsg('assistant', '');
// 剥离 <think> 块
text = text.replace(/<think>[\s\S]*?<\/think>/g, '').trim();
const d = document.createElement('div');
d.className = 'content';
d.innerHTML = '<span class="label final">final</span>' + escapeHtml(text);
currentAssistantDiv.appendChild(d);
log.scrollTop = log.scrollHeight;
}
function escapeHtml(s) {
return String(s || '').replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
}
async function send() {
const msg = input.value.trim();
if (!msg || busy) return;
busy = true;
sendBtn.disabled = true;
input.value = '';
appendMsg('user', '<b>你:</b> ' + escapeHtml(msg));
currentAssistantDiv = null;
try {
const r = await fetch('/api/chat/debug', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ message: msg, sessionId })
});
const reader = r.body.getReader();
const dec = new TextDecoder();
let buf = '';
while (true) {
const { done, value } = await reader.read();
if (done) break;
buf += dec.decode(value);
const lines = buf.split('\n');
buf = lines.pop() || '';
for (const line of lines) {
if (!line.startsWith('data: ')) continue;
const e = JSON.parse(line.slice(6));
handleEvent(e);
}
}
} catch (err) {
appendMsg('error', '请求失败: ' + escapeHtml(err.message));
} finally {
busy = false;
sendBtn.disabled = false;
input.focus();
}
}
function handleEvent(e) {
if (e.type === 'session') { sessionId = e.sessionId; return; }
if (e.type === 'thinking') { appendThink(e.content || ''); return; }
if (e.type === 'content') {
if (!currentAssistantDiv) currentAssistantDiv = appendMsg('assistant', '');
// 内容追加
const d = document.createElement('div');
d.className = 'think';
d.innerHTML = '<span class="label think">i</span>' + escapeHtml(e.content);
currentAssistantDiv.appendChild(d);
log.scrollTop = log.scrollHeight;
return;
}
if (e.type === 'tool_call') { appendTool(e.tool, e.args); return; }
if (e.type === 'tool_result') { appendToolResult(e.tool, e.result); return; }
if (e.type === 'iteration') { return; }
if (e.type === 'complete') { appendFinal(e.response || ''); return; }
if (e.type === 'error') { appendMsg('error', escapeHtml(e.error || e.data?.message || 'unknown')); return; }
if (e.type === 'done') { return; }
}
sendBtn.onclick = send;
input.addEventListener('keydown', e => {
if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); send(); }
});
// 欢迎
appendMsg('system', 'openchat dev — 输入消息按 Enter 发送');
</script>
</body>
</html>