-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaiiscrazygogogaga.html
More file actions
607 lines (582 loc) · 23.2 KB
/
Copy pathaiiscrazygogogaga.html
File metadata and controls
607 lines (582 loc) · 23.2 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script>(function(){if(!sessionStorage.getItem('recaptcha_verified')||sessionStorage.getItem('recaptcha_verified')!=='true'){window.location.href='exp.html';}})();</script>
<title>SigmaWeb AI Chat (Claude)</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Fira Mono for code -->
<link href="https://fonts.googleapis.com/css?family=Fira+Mono:400,500&display=swap" rel="stylesheet">
<style>
body {
font-family: 'Segoe UI', 'Arial', sans-serif;
background: linear-gradient(135deg, #e7f0ff 0%, #f5f6fa 100%);
margin: 0;
min-height: 100vh;
}
.chat-container {
max-width: 700px;
margin: 48px auto;
background: #fff;
border-radius: 14px;
box-shadow: 0 8px 24px #c5d0e0;
padding: 0;
display: flex;
flex-direction: column;
overflow: hidden;
}
.logo {
text-align: center;
padding: 36px 0 20px 0;
background: linear-gradient(90deg, #007bff14 0%, #fff 70%);
}
.logo span {
font-weight: bold;
font-size: 2.2em;
color: #007bff;
letter-spacing: 2px;
}
.history-btn-row {
display: flex;
justify-content: space-between;
align-items: center;
padding: 12px 32px 0 32px;
}
.mode-switch {
margin-left: auto;
}
.mode-btn {
background: #e9ecef;
color: #007bff;
border: none;
border-radius: 7px;
font-size: 16px;
padding: 8px 22px;
cursor: pointer;
margin-left: 12px;
transition: background 0.2s;
}
.mode-btn.active {
background: #007bff;
color: #fff;
}
#deleteHistoryBtn {
background: #f44336;
color: #fff;
font-size: 16px;
padding: 8px 20px;
border-radius: 7px;
border: none;
cursor: pointer;
transition: background 0.2s;
}
#deleteHistoryBtn:hover {
background: #d32f2f;
}
.chat-history {
flex: 1;
min-height: 320px;
max-height: 520px;
overflow-y: auto;
border: 1px solid #e4e7ec;
border-radius: 10px;
padding: 24px;
margin: 0 32px 0 32px;
background: #f5f7ff;
box-shadow: 0 2px 16px #e3e8f0 inset;
transition: background 0.2s;
}
.msg {
margin-bottom: 24px;
display: flex;
}
.msg.user {
justify-content: flex-end;
}
.msg.ai {
justify-content: flex-start;
}
.msg .bubble {
display: inline-block;
padding: 17px 22px;
border-radius: 22px;
max-width: 75vw;
background: #e9ecef;
color: #222;
white-space: pre-wrap;
word-break: break-word;
font-size: 17px;
box-shadow: 0 2px 8px #dbe3f3;
border: 1px solid #e4e7ec;
}
.msg.user .bubble {
background: linear-gradient(90deg, #007bff 0%, #339af0 100%);
color: #fff;
border: 1px solid #b3d0ff;
box-shadow: 0 2px 8px #b3d0ff;
}
.msg.ai .bubble {
background: #f6f8fd;
color: #222;
border: 1px solid #cfd8dc;
}
.code-card {
background: #161d2a;
color: #eee;
border-radius: 14px;
margin: 14px 0;
font-size: 16px;
overflow-x: auto;
border: 1px solid #333;
box-shadow: 0 2px 12px #b3c0d7;
padding: 0;
position: relative;
max-width: 440px;
min-width: 220px;
margin-left: 18px;
font-family: 'Fira Mono', 'Menlo', 'Consolas', monospace;
}
.code-card-main {
padding: 22px 14px 14px 20px;
font-family: inherit;
white-space: pre;
font-size: 15.5px;
background: none;
}
.code-card-buttons {
position: absolute;
top: 10px;
right: 18px;
display: flex;
gap: 9px;
}
.code-label {
display: block;
font-size: 15px;
color: #007bff;
background: #e9ecef;
border-radius: 8px;
padding: 4px 12px;
margin-bottom: 8px;
margin-left: 18px;
margin-top: 16px;
width: fit-content;
font-family: 'Fira Mono', monospace;
font-weight: 600;
letter-spacing: 1px;
}
.code-btn {
background: #007bff;
color: #fff;
border: none;
border-radius: 8px;
font-size: 15px;
padding: 3px 14px;
cursor: pointer;
transition: background 0.2s;
font-family: inherit;
}
.code-btn:active {
background: #005bb5;
}
.input-row {
display: flex;
gap: 14px;
padding: 26px 32px 24px 32px;
background: #f9fbff;
border-radius: 0 0 14px 14px;
}
input[type="text"] {
flex: 1;
padding: 13px;
border-radius: 8px;
border: 1.5px solid #b5c6e0;
font-size: 19px;
background: #fefefe;
transition: box-shadow 0.2s;
box-shadow: 0 2px 8px #e2ebfa inset;
}
input[type="text"]:focus {
border-color: #007bff;
outline: none;
box-shadow: 0 2px 12px #cee3fc inset;
}
button {
padding: 13px 25px;
border-radius: 8px;
border: none;
background: #007bff;
color: #fff;
font-size: 19px;
cursor: pointer;
transition: background 0.2s;
font-family: inherit;
}
button:disabled {
background: #aaa;
cursor: not-allowed;
}
/* Markdown styles */
h1, h2, h3, h4, h5, h6 { margin: 0.7em 0 0.3em 0; font-weight: 600;}
ul { margin: 0.3em 0 0.3em 1em; }
li { margin-bottom: 0.2em; }
a { color: #007bff; text-decoration: underline; }
b { font-weight: bold; }
i { font-style: italic; }
code {
background: #eee;
color: #d6336c;
border-radius: 6px;
padding: 2px 8px;
font-family: 'Fira Mono', 'Menlo', 'Consolas', monospace;
font-size: 1em;
margin: 0 1px;
}
.streaming {
min-height: 19px;
animation: blink 1.3s infinite;
border-right: 2px solid #007bff;
}
@keyframes blink {
0%, 100% { border-right: 2px solid #007bff; }
50% { border-right: 2px solid transparent; }
}
.ref-card {
background: #f8fafc;
border-radius: 7px;
box-shadow: 0 1px 4px #e3e8f0;
margin: 8px 0;
padding: 8px 12px;
font-size: 15px;
display: flex;
align-items: center;
}
.ref-card img { margin-right: 6px; }
.ref-snippet { font-size: 13px; color: #444; margin-top: 3px; }
.references { margin-top: 12px; }
::-webkit-scrollbar { width: 9px; background: #f5f7ff;}
::-webkit-scrollbar-thumb { background: #b9c8d3; border-radius: 10px;}
::-webkit-scrollbar-thumb:hover { background: #007bff33;}
@media (max-width: 900px) {
.chat-container { max-width: 98vw; padding: 0;}
.code-card { max-width: 99vw; min-width: 0; margin-left: 0; }
.logo span { font-size: 1.1em;}
.chat-history { margin: 0 4vw 0 4vw;}
.input-row { padding: 24px 4vw 22px 4vw;}
}
</style>
</head>
<body>
<div class="chat-container">
<div class="logo"><span>SigmaWeb AI Chat (Claude)</span></div>
<div class="history-btn-row">
<button id="deleteHistoryBtn" title="Delete chat history">Delete history</button>
<div class="mode-switch">
<button class="mode-btn active" id="chatModeBtn">Chat</button>
<button class="mode-btn" id="searchModeBtn">Search</button>
</div>
</div>
<div id="chatHistory" class="chat-history"></div>
<form id="chatForm" class="input-row" autocomplete="off">
<input type="text" id="userInput" placeholder="Type your message..." required autofocus />
<button type="submit">Send</button>
</form>
</div>
<script>
const chatHistory = document.getElementById('chatHistory');
const chatForm = document.getElementById('chatForm');
const userInput = document.getElementById('userInput');
const deleteHistoryBtn = document.getElementById('deleteHistoryBtn');
const chatModeBtn = document.getElementById('chatModeBtn');
const searchModeBtn = document.getElementById('searchModeBtn');
let mode = 'chat'; // 'chat' or 'search'
chatModeBtn.onclick = () => {
mode = 'chat';
chatModeBtn.classList.add('active');
searchModeBtn.classList.remove('active');
userInput.placeholder = "Type your message...";
};
searchModeBtn.onclick = () => {
mode = 'search';
chatModeBtn.classList.remove('active');
searchModeBtn.classList.add('active');
userInput.placeholder = "Type your search query...";
};
let history = JSON.parse(localStorage.getItem('sigmaweb_claude_chat') || '[]');
renderHistory();
chatForm.addEventListener('submit', async (e) => {
e.preventDefault();
const text = userInput.value.trim();
if (!text) return;
addMessage('user', text);
userInput.value = '';
chatForm.querySelector('button').disabled = true;
await aiReplyStream(text, history);
chatForm.querySelector('button').disabled = false;
});
deleteHistoryBtn.onclick = function() {
if (confirm("Delete all chat history?")) {
localStorage.removeItem('sigmaweb_claude_chat');
history = [];
renderHistory();
}
};
function addMessage(role, content, meta={}) {
history.push({ role, content, meta });
localStorage.setItem('sigmaweb_claude_chat', JSON.stringify(history));
renderHistory();
}
// Markdown rendering (simple)
function renderMarkdown(text) {
text = escapeHtml(text);
text = text.replace(/^###### (.*)$/gm, '<h6>$1</h6>')
.replace(/^##### (.*)$/gm, '<h5>$1</h5>')
.replace(/^#### (.*)$/gm, '<h4>$1</h4>')
.replace(/^### (.*)$/gm, '<h3>$1</h3>')
.replace(/^## (.*)$/gm, '<h2>$1</h2>')
.replace(/^# (.*)$/gm, '<h1>$1</h1>');
text = text.replace(/^[ \t]*[-*] (.*)$/gm, '<li>$1</li>');
text = text.replace(/(<li>.*<\/li>)/g, '<ul>$1</ul>');
text = text.replace(/\*\*(.*?)\*\*/g, '<b>$1</b>')
.replace(/\*(.*?)\*/g, '<i>$1</i>');
text = text.replace(/`([^`]+)`/g, '<code>$1</code>');
text = text.replace(/\[([^\]]+)\]\(([^ )]+)\)/g, '<a href="$2" target="_blank">$1</a>');
return text;
}
// Recognize urls, code blocks, markdown, citations
function renderMsgContent(content, citations = [], streaming=false) {
// Code block
const codeBlockRegex = /```([a-zA-Z0-9]*)\s*([\s\S]*?)```/g;
let lastIndex = 0;
let htmlParts = [];
let match;
let isDesktop = window.innerWidth > 900;
while ((match = codeBlockRegex.exec(content)) !== null) {
if (match.index > lastIndex) {
htmlParts.push(renderMarkdown(content.substring(lastIndex, match.index)));
}
const lang = match[1] || 'code';
const code = match[2];
htmlParts.push(
isDesktop ?
`<div class="code-card">
<span class="code-label">${escapeHtml(lang.toUpperCase())} CODE</span>
<div class="code-card-main">${escapeHtml(code)}</div>
<div class="code-card-buttons">
<button class="code-btn copy-btn" data-code="${encodeURIComponent(code)}">Copy</button>
<button class="code-btn run-btn" data-code="${encodeURIComponent(code)}" data-lang="${lang}">Run</button>
</div>
</div>`
: `<div class="code-label">${escapeHtml(lang.toUpperCase())} CODE</div><pre class="code-block">${escapeHtml(code)}</pre>`
);
lastIndex = codeBlockRegex.lastIndex;
}
if (lastIndex < content.length) {
htmlParts.push(renderMarkdown(content.substring(lastIndex)));
}
// Recognize URLs and show rich cards
let urlRegex = /(https?:\/\/[^\s]+)/g;
htmlParts = htmlParts.map(part =>
part.replace(urlRegex, url => {
if (url.includes('youtube.com')) {
return `<div class="ref-card"><img src="https://www.youtube.com/s/desktop/0057e8c8/img/favicon.ico" style="height:20px;vertical-align:middle;margin-right:6px;"> <a href="${url}" target="_blank" style="color:#FF0000;font-weight:bold;">YouTube</a></div>`;
}
// Add more sites/logos as needed
return `<div class="ref-card"><a href="${url}" target="_blank">${url}</a></div>`;
})
);
// Citations/references (search mode)
if (citations && citations.length > 0) {
htmlParts.push('<hr><div class="references"><b>References:</b>');
citations.forEach(ref => {
htmlParts.push(
`<div class="ref-card">
<a href="${escapeHtml(ref.url)}" target="_blank">
${ref.title ? escapeHtml(ref.title) : escapeHtml(ref.url)}
</a>
${ref.cited_text ? `<div class="ref-snippet">${escapeHtml(ref.cited_text)}</div>` : ""}
</div>`
);
});
htmlParts.push('</div>');
}
let html = htmlParts.join('');
if (streaming) html += `<span class="streaming"></span>`;
return html;
}
function escapeHtml(text) {
return text
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
function renderHistory(streamingIdx = -1, partial = "", citations = []) {
chatHistory.innerHTML = '';
history.forEach((msg, idx) => {
const div = document.createElement('div');
div.className = 'msg ' + msg.role;
const bubble = document.createElement('div');
bubble.className = 'bubble';
if (msg.role === 'ai') {
if (idx === streamingIdx) {
bubble.innerHTML = renderMsgContent(partial, citations, true);
} else {
bubble.innerHTML = renderMsgContent(msg.content, msg.meta && msg.meta.citations ? msg.meta.citations : []);
}
} else {
bubble.textContent = msg.content;
}
div.appendChild(bubble);
chatHistory.appendChild(div);
});
chatHistory.scrollTop = chatHistory.scrollHeight;
attachCodeBtns();
}
// Attach Copy/Run event listeners
function attachCodeBtns() {
document.querySelectorAll('.copy-btn').forEach(btn => {
btn.onclick = function() {
const code = decodeURIComponent(this.getAttribute('data-code'));
navigator.clipboard.writeText(code);
btn.textContent = "Copied!";
setTimeout(() => btn.textContent = "Copy", 1200);
};
});
document.querySelectorAll('.run-btn').forEach(btn => {
btn.onclick = function() {
const code = decodeURIComponent(this.getAttribute('data-code'));
const lang = (this.getAttribute('data-lang') || '').toLowerCase();
if (lang === 'html') {
const win = window.open();
win.document.write(code);
} else if (lang === 'js' || lang === 'javascript') {
try { eval(code); } catch(e) { alert("Error: " + e.message); }
} else {
alert("Run only supported for HTML and JS.");
}
};
});
}
// Live streaming: simulates streaming (replace with real Claude streaming if available)
async function aiReplyStream(userMsg, historySnapshot) {
addMessage('ai', '');
let idx = history.length - 1;
let fullText = '';
const messages = [];
historySnapshot.forEach(msg => {
if (
(msg.role === 'user' || msg.role === 'ai') &&
msg.content && msg.content.trim() !== ""
) {
messages.push({
role: msg.role === 'user' ? 'user' : 'assistant',
content: msg.content
});
}
});
messages.push({ role: 'user', content: userMsg });
try {
const keyResp = await fetch('/sigmaweb/api/claudeapi.txt');
const apiKey = (await keyResp.text()).trim();
const endpoint = 'https://api.anthropic.com/v1/messages';
const model = mode === 'search' ? 'claude-opus-4-20250514' : 'claude-opus-4-20250514';
const resp = await fetch(endpoint, {
method: 'POST',
headers: {
'x-api-key': apiKey,
'anthropic-version': '2023-06-01',
'content-type': 'application/json',
'anthropic-dangerous-direct-browser-access': 'true'
},
body: JSON.stringify({
model,
max_tokens: 1024,
messages,
tools: mode === 'search' ? [{
"type": "web_search_20250305",
"name": "web_search",
"max_uses": 5
}] : undefined
})
});
if (!resp.ok) {
let errorMsg = 'Claude API error.';
try {
const errorData = await resp.json();
if (errorData.error && errorData.error.message) {
errorMsg += ' ' + errorData.error.message;
}
} catch {}
updateAiMsg(idx, errorMsg, {});
} else {
const data = await resp.json();
// Find citations
let citations = [];
if (mode === 'search') {
// Try common keys
if (data.content?.[0]?.citations) citations = data.content[0].citations;
else if (data.content?.[0]?.references) citations = data.content[0].references;
else if (data.content?.[0]?.sources) citations = data.content[0].sources;
// Fallback: find URLs in text
if ((!citations || citations.length === 0) && data.content?.[0]?.text) {
citations = [];
let urlRegex = /(https?:\/\/[^\s]+)/g;
let foundUrls = data.content[0].text.match(urlRegex);
if (foundUrls) {
foundUrls.forEach(url => citations.push({ url }));
}
}
}
const aiText = data.content?.[0]?.text || 'No reply from Claude.';
// Live streaming effect
for (let i = 0; i < aiText.length; i+=2) {
fullText = aiText.slice(0, i + 1);
renderHistory(idx, fullText, citations);
await new Promise(res => setTimeout(res, 7));
}
updateAiMsg(idx, aiText, {citations});
}
} catch (err) {
updateAiMsg(idx, 'Error contacting Claude API.', {});
}
}
function updateAiMsg(idx, text, meta={}) {
history[idx].content = text;
history[idx].meta = meta;
localStorage.setItem('sigmaweb_claude_chat', JSON.stringify(history));
renderHistory();
}
// Session timeout and tab exit detection
(function() {
const TIMEOUT_MS = 10 * 60 * 1000; // 10 minutes
function updateActivity() {
sessionStorage.setItem('last_activity', Date.now().toString());
}
function checkTimeout() {
const lastActivity = sessionStorage.getItem('last_activity');
if (!lastActivity || Date.now() - parseInt(lastActivity) > TIMEOUT_MS) {
sessionStorage.clear();
window.location.href = 'exp.html';
}
}
// Initialize last activity
updateActivity();
// Update activity on user interactions
['mousedown', 'keydown', 'scroll', 'touchstart'].forEach(function(event) {
document.addEventListener(event, updateActivity);
});
// Check timeout every minute
setInterval(checkTimeout, 60000);
// Clear session when tab is hidden/exited
document.addEventListener('visibilitychange', function() {
if (document.hidden) {
sessionStorage.clear();
window.location.href = 'exp.html';
}
});
})();
</script>
</body>
</html>