-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbasic-chat.html
More file actions
200 lines (180 loc) · 5.31 KB
/
basic-chat.html
File metadata and controls
200 lines (180 loc) · 5.31 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Web Agent API Example: Basic Chat</title>
<style>
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
font-family: system-ui, -apple-system, sans-serif;
max-width: 600px;
margin: 0 auto;
padding: 20px;
background: #1a1a2e;
color: #eee;
min-height: 100vh;
}
h1 { margin-bottom: 20px; color: #fff; }
.status {
padding: 10px;
border-radius: 8px;
margin-bottom: 20px;
font-size: 14px;
}
.status.available { background: #0d4d0d; }
.status.unavailable { background: #4d0d0d; }
.chat {
background: #16213e;
border-radius: 12px;
padding: 20px;
min-height: 300px;
max-height: 400px;
overflow-y: auto;
margin-bottom: 20px;
}
.message {
margin-bottom: 15px;
padding: 10px 15px;
border-radius: 8px;
}
.message.user {
background: #0f3460;
margin-left: 40px;
}
.message.assistant {
background: #1a1a2e;
margin-right: 40px;
}
.message .role {
font-size: 11px;
text-transform: uppercase;
opacity: 0.6;
margin-bottom: 5px;
}
.input-area {
display: flex;
gap: 10px;
}
input {
flex: 1;
padding: 12px 16px;
border: none;
border-radius: 8px;
background: #16213e;
color: #fff;
font-size: 16px;
}
input:focus { outline: 2px solid #e94560; }
button {
padding: 12px 24px;
border: none;
border-radius: 8px;
background: #e94560;
color: #fff;
font-weight: 600;
cursor: pointer;
}
button:hover { background: #ff6b6b; }
button:disabled { opacity: 0.5; cursor: not-allowed; }
.code { font-family: monospace; background: #0f3460; padding: 2px 6px; border-radius: 4px; }
</style>
</head>
<body>
<h1>💬 Web Agent API: Basic Chat</h1>
<div id="status" class="status unavailable">
Checking Web Agent API availability...
</div>
<div id="chat" class="chat">
<div class="message assistant">
<div class="role">Assistant</div>
<div>Hello! I'm ready to chat. Type a message below.</div>
</div>
</div>
<div class="input-area">
<input type="text" id="input" placeholder="Type a message..." disabled>
<button id="send" disabled>Send</button>
</div>
<script type="module">
const statusEl = document.getElementById('status');
const chatEl = document.getElementById('chat');
const inputEl = document.getElementById('input');
const sendBtn = document.getElementById('send');
let session = null;
// Initialize
async function init() {
// Check if Web Agent API is available
if (typeof window.agent === 'undefined') {
statusEl.textContent = '❌ Web Agent API not available';
return;
}
// Request permission
const result = await window.agent.requestPermissions({
scopes: ['model:prompt'],
reason: 'This demo needs AI to chat with you.'
});
if (!result.granted) {
statusEl.textContent = '❌ Permission denied';
return;
}
// Create session
try {
session = await window.ai.createTextSession({
systemPrompt: 'You are a friendly assistant. Be concise but helpful.'
});
statusEl.className = 'status available';
statusEl.textContent = '✓ Web Agent API connected';
inputEl.disabled = false;
sendBtn.disabled = false;
inputEl.focus();
} catch (err) {
statusEl.textContent = '❌ Error: ' + err.message;
}
}
// Send message
async function sendMessage() {
const message = inputEl.value.trim();
if (!message || !session) return;
inputEl.value = '';
sendBtn.disabled = true;
// Add user message
addMessage('user', message);
// Add placeholder for assistant
const assistantDiv = addMessage('assistant', '');
try {
// Stream the response
for await (const event of session.promptStreaming(message)) {
if (event.type === 'token') {
assistantDiv.querySelector('.content').textContent += event.token;
chatEl.scrollTop = chatEl.scrollHeight;
} else if (event.type === 'error') {
assistantDiv.querySelector('.content').textContent = 'Error: ' + event.error.message;
}
}
} catch (err) {
assistantDiv.querySelector('.content').textContent = 'Error: ' + err.message;
}
sendBtn.disabled = false;
inputEl.focus();
}
function addMessage(role, content) {
const div = document.createElement('div');
div.className = `message ${role}`;
div.innerHTML = `
<div class="role">${role}</div>
<div class="content">${content}</div>
`;
chatEl.appendChild(div);
chatEl.scrollTop = chatEl.scrollHeight;
return div;
}
// Event listeners
sendBtn.addEventListener('click', sendMessage);
inputEl.addEventListener('keypress', (e) => {
if (e.key === 'Enter') sendMessage();
});
// Start
init();
</script>
</body>
</html>