src/channels/web-page.ts
6 KB - הצגת הקובץ המקורי
// The local chat page served at GET / (lab 08). Plain HTML and JavaScript, no build step.
//
// The page reads server-sent events with fetch() instead of EventSource, because EventSource
// cannot send the Authorization header. The token is kept in sessionStorage for this tab only.
export const WEB_PAGE = `<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Steward</title>
<style>
:root { --bg: #f7f7f5; --panel: #ffffff; --text: #1c1c1a; --muted: #6b6b66; --accent: #2d6a4f; --border: #deded8; }
@media (prefers-color-scheme: dark) {
:root { --bg: #121412; --panel: #1b1e1b; --text: #e8e8e3; --muted: #9a9a92; --accent: #6fcf97; --border: #2c302c; }
}
* { box-sizing: border-box; }
body { margin: 0; font: 15px/1.5 system-ui, sans-serif; background: var(--bg); color: var(--text); }
main { max-width: 760px; margin: 0 auto; padding: 16px; display: flex; flex-direction: column; gap: 12px; min-height: 100vh; }
header { display: flex; justify-content: space-between; align-items: center; }
h1 { font-size: 18px; margin: 0; }
#log { flex: 1; display: flex; flex-direction: column; gap: 8px; }
.msg { padding: 10px 12px; border-radius: 10px; background: var(--panel); border: 1px solid var(--border); white-space: pre-wrap; overflow-wrap: anywhere; }
.user { align-self: flex-end; border-color: var(--accent); }
.note { color: var(--muted); font-size: 13px; }
.scheduled { border-left: 4px solid var(--accent); }
.approval { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
form { display: flex; gap: 8px; }
input, button { font: inherit; padding: 8px 10px; border-radius: 8px; border: 1px solid var(--border); background: var(--panel); color: var(--text); }
input { flex: 1; min-width: 0; }
button { cursor: pointer; }
button.primary { background: var(--accent); color: #fff; border-color: var(--accent); }
</style>
</head>
<body>
<main>
<header><h1>Steward</h1><button id="new" type="button">New conversation</button></header>
<div id="log"></div>
<form id="form"><input id="input" autocomplete="off" placeholder="Message Steward"><button class="primary">Send</button></form>
</main>
<script>
(function () {
var log = document.getElementById('log');
var input = document.getElementById('input');
var token = '';
try { token = sessionStorage.getItem('steward-token') || ''; } catch (e) {}
if (!token) {
token = prompt('Steward web token (printed when steward serve starts):') || '';
try { sessionStorage.setItem('steward-token', token); } catch (e) {}
}
function add(cls, text) {
var div = document.createElement('div');
div.className = 'msg ' + cls;
div.textContent = text;
log.appendChild(div);
div.scrollIntoView({ block: 'end' });
return div;
}
function api(path, method, body) {
return fetch(path, {
method: method || 'GET',
headers: { 'Authorization': 'Bearer ' + token, 'Content-Type': 'application/json' },
body: body ? JSON.stringify(body) : undefined
});
}
// Reads a text/event-stream response and calls onEvent(name, data) for each event.
function readEvents(response, onEvent) {
var reader = response.body.getReader();
var decoder = new TextDecoder();
var buffer = '';
function pump() {
return reader.read().then(function (r) {
if (r.done) return;
buffer += decoder.decode(r.value, { stream: true });
var parts = buffer.split('\\n\\n');
buffer = parts.pop();
parts.forEach(function (chunk) {
var name = 'message', data = '';
chunk.split('\\n').forEach(function (line) {
if (line.indexOf('event:') === 0) name = line.slice(6).trim();
if (line.indexOf('data:') === 0) data += line.slice(5).trim();
});
try { onEvent(name, JSON.parse(data || '{}')); } catch (e) {}
});
return pump();
});
}
return pump();
}
function showApprovals(list) {
list.forEach(function (a) {
var div = add('note approval', 'Approval needed [' + a.approvalId + ']: ' + a.summary + ' ');
[['Approve', 'approved'], ['Deny', 'denied']].forEach(function (pair) {
var b = document.createElement('button');
b.textContent = pair[0];
b.onclick = function () { div.remove(); streamTurn('/api/approvals/' + a.approvalId, { decision: pair[1] }); };
div.appendChild(b);
});
});
}
function streamTurn(path, body) {
var out = add('assistant', '');
return api(path, 'POST', body).then(function (res) {
if (!res.ok) { out.textContent = 'Error ' + res.status; return; }
return readEvents(res, function (name, data) {
if (name === 'text') out.textContent += data.delta;
if (name === 'tool' && data.phase === 'start') add('note', '[tool] ' + data.name);
if (name === 'error') add('note', 'Error: ' + data.message);
if (name === 'done') {
if (!out.textContent) out.textContent = data.text || '(' + data.stop + ')';
showApprovals(data.approvals || []);
}
});
});
}
document.getElementById('form').onsubmit = function (e) {
e.preventDefault();
var text = input.value.trim();
if (!text) return;
input.value = '';
add('user', text);
streamTurn('/api/chat', { text: text });
};
document.getElementById('new').onclick = function () {
api('/api/new', 'POST').then(function () { log.innerHTML = ''; add('note', 'New conversation started. Memory is kept.'); });
};
api('/api/history').then(function (r) { return r.ok ? r.json() : { messages: [], outbox: [] }; }).then(function (h) {
h.messages.forEach(function (m) { add(m.role, m.text); });
h.outbox.forEach(function (o) { add('assistant scheduled', '[' + o.source + '] ' + o.text); });
return api('/api/pending').then(function (r) { return r.ok ? r.json() : []; }).then(showApprovals);
});
api('/api/events').then(function (res) {
if (!res.ok) return;
return readEvents(res, function (name, data) {
if (name === 'outbound') add('assistant scheduled', '[' + data.source + '] ' + data.text);
});
});
})();
</script>
</body>
</html>
`;