-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathindex.html
More file actions
195 lines (170 loc) · 8.1 KB
/
Copy pathindex.html
File metadata and controls
195 lines (170 loc) · 8.1 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Adam Chat</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font-family: -apple-system, system-ui, sans-serif; background: #1a1a2e; color: #eee; height: 100vh; display: flex; flex-direction: column; }
.header { padding: 12px 20px; background: #16213e; border-bottom: 1px solid #0f3460; display: flex; align-items: center; gap: 12px; }
.header h1 { font-size: 18px; font-weight: 600; }
.header .status { font-size: 12px; color: #888; }
.setup { padding: 20px; background: #16213e; border-bottom: 1px solid #0f3460; display: flex; gap: 10px; align-items: end; flex-wrap: wrap; }
.setup label { font-size: 12px; color: #aaa; display: flex; flex-direction: column; gap: 4px; }
.setup select, .setup input { padding: 6px 10px; border-radius: 6px; border: 1px solid #0f3460; background: #1a1a2e; color: #eee; font-size: 13px; }
.setup input { width: 320px; }
.setup button { padding: 6px 16px; border-radius: 6px; border: none; background: #e94560; color: white; font-size: 13px; cursor: pointer; }
.setup button:hover { background: #c73e54; }
.messages { flex: 1; overflow-y: auto; padding: 20px; display: flex; flex-direction: column; gap: 12px; }
.msg { max-width: 80%; padding: 10px 14px; border-radius: 12px; line-height: 1.5; font-size: 14px; white-space: pre-wrap; word-wrap: break-word; }
.msg.user { align-self: flex-end; background: #0f3460; }
.msg.assistant { align-self: flex-start; background: #16213e; border: 1px solid #0f3460; }
.msg.system { align-self: center; background: none; color: #666; font-size: 12px; font-style: italic; }
.input-area { padding: 12px 20px; background: #16213e; border-top: 1px solid #0f3460; display: flex; gap: 10px; }
.input-area input { flex: 1; padding: 10px 14px; border-radius: 8px; border: 1px solid #0f3460; background: #1a1a2e; color: #eee; font-size: 14px; outline: none; }
.input-area input:focus { border-color: #e94560; }
.input-area button { padding: 10px 20px; border-radius: 8px; border: none; background: #e94560; color: white; font-size: 14px; cursor: pointer; }
.input-area button:disabled { opacity: 0.5; cursor: not-allowed; }
.thinking { color: #888; }
</style>
</head>
<body>
<div class="header">
<h1>Adam</h1>
<span class="status" id="status">Loading WASM...</span>
</div>
<div class="setup" id="setup">
<label>Provider
<select id="provider">
<option value="anthropic">Anthropic</option>
<option value="openai">OpenAI</option>
<option value="gemini">Gemini</option>
</select>
</label>
<label>API Key
<input type="password" id="apikey" placeholder="sk-ant-... or sk-... or AIza...">
</label>
<label>Model (optional)
<input type="text" id="model" placeholder="default">
</label>
<button onclick="connect()">Connect</button>
</div>
<div class="messages" id="messages"></div>
<div class="input-area">
<input type="text" id="input" placeholder="Type a message..." disabled onkeydown="if(event.key==='Enter')send()">
<button id="sendbtn" onclick="send()" disabled>Send</button>
</div>
<script src="adam.js?v=3"></script>
<script>
let Module = null;
let settings = 0;
let history = 0;
let busy = false;
// Adam API wrappers (set after module loads)
let adam_init, adam_cleanup, adam_create_settings, adam_settings_destroy;
let adam_settings_set_provider, adam_settings_set_identity;
let adam_history_create, adam_history_destroy, adam_history_clear;
let adam_run_simple;
async function initWasm() {
Module = await AdamModule();
adam_init = Module.cwrap('adam_init', 'number', []);
adam_cleanup = Module.cwrap('adam_cleanup', null, []);
adam_create_settings = Module.cwrap('adam_create_settings', 'number', []);
adam_settings_destroy = Module.cwrap('adam_settings_destroy', null, ['number']);
adam_settings_set_provider = Module.cwrap('adam_settings_set_provider', 'number', ['number', 'number', 'string', 'string']);
adam_settings_set_identity = Module.cwrap('adam_settings_set_identity', 'number', ['number', 'string']);
adam_history_create = Module.cwrap('adam_history_create', 'number', []);
adam_history_destroy = Module.cwrap('adam_history_destroy', null, ['number']);
adam_history_clear = Module.cwrap('adam_history_clear', null, ['number']);
// adam_run_simple: runs adam_run, returns response as a malloc'd string pointer
adam_run_simple = Module.cwrap('adam_run_simple', 'number', ['number', 'number', 'string'], { async: true });
adam_init();
document.getElementById('status').textContent = 'Ready — enter API key to start';
}
function addMessage(role, text) {
const div = document.createElement('div');
div.className = 'msg ' + role;
div.textContent = text;
const container = document.getElementById('messages');
container.appendChild(div);
container.scrollTop = container.scrollHeight;
return div;
}
function connect() {
const providerMap = { anthropic: 1, openai: 2, gemini: 3 };
const provider = document.getElementById('provider').value;
const apikey = document.getElementById('apikey').value.trim();
const model = document.getElementById('model').value.trim();
if (!apikey) { alert('Enter an API key'); return; }
// Clean up previous session
if (history) adam_history_destroy(history);
if (settings) adam_settings_destroy(settings);
settings = adam_create_settings();
history = adam_history_create();
const defaultModels = {
anthropic: 'claude-sonnet-4-20250514',
openai: 'gpt-4o-mini',
gemini: 'gemini-2.5-flash'
};
// Allocate persistent C strings — adam stores the pointers, so they must
// outlive the cwrap call (cwrap 'string' creates temporaries that get freed)
function allocStr(s) {
const len = Module.lengthBytesUTF8(s) + 1;
const ptr = Module._malloc(len);
Module.stringToUTF8(s, ptr, len);
return ptr;
}
const keyPtr = allocStr(apikey);
const modelPtr = allocStr(model || defaultModels[provider]);
Module.ccall('adam_settings_set_provider', 'number',
['number', 'number', 'number', 'number'],
[settings, providerMap[provider], keyPtr, modelPtr]);
const identPtr = allocStr('You are Adam, a helpful AI assistant. Be concise.');
Module.ccall('adam_settings_set_identity', 'number',
['number', 'number'], [settings, identPtr]);
document.getElementById('setup').style.display = 'none';
document.getElementById('input').disabled = false;
document.getElementById('sendbtn').disabled = false;
document.getElementById('input').focus();
document.getElementById('status').textContent = provider + ' / ' + (model || defaultModels[provider]);
addMessage('system', 'Connected. Type a message to start chatting.');
}
async function send() {
if (busy || !settings) return;
const input = document.getElementById('input');
const text = input.value.trim();
if (!text) return;
input.value = '';
addMessage('user', text);
busy = true;
document.getElementById('sendbtn').disabled = true;
const thinking = addMessage('assistant', '...');
thinking.classList.add('thinking');
try {
// adam_run_simple: runs the full agent loop, returns malloc'd string ptr
// On error, it returns the error message (not null)
const ptr = await adam_run_simple(settings, history, text);
if (ptr) {
const response = Module.UTF8ToString(ptr);
Module._free(ptr);
thinking.textContent = response || '(empty response)';
} else {
thinking.textContent = '(null response — check browser console)';
}
thinking.classList.remove('thinking');
} catch (e) {
thinking.textContent = 'Error: ' + (e.message || e);
thinking.classList.remove('thinking');
console.error('adam_run_simple error:', e);
}
busy = false;
document.getElementById('sendbtn').disabled = false;
document.getElementById('input').focus();
}
initWasm().catch(e => {
document.getElementById('status').textContent = 'Failed to load WASM: ' + e.message;
});
</script>
</body>
</html>