-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
253 lines (214 loc) · 7.46 KB
/
main.js
File metadata and controls
253 lines (214 loc) · 7.46 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
/**
* ClientAgentJS Example - Chat Explorer
* See license: https://github.com/FranBarInstance/ClientAgentJS
*/
import { createAgent } from "../../dist/clientagentjs.esm.js";
const AGENTS_MD_PATH = "./AGENTS.md";
const DEMO_SYSTEM_PROMPT = "You are the demo assistant for the ClientAgentJS browser example.";
// --- DOM Elements ---
const promptInput = document.querySelector("#prompt");
const messagesEl = document.querySelector("#messages");
const eventLogEl = document.querySelector("#event-log");
const statusBadgeEl = document.querySelector("#agent-status-badge");
const agentsMdBadgeEl = document.querySelector("#agents-md-badge");
const btnSend = document.querySelector("#btn-send");
const btnStop = document.querySelector("#btn-stop");
// --- State ---
let currentSession = null;
let abortController = null;
// --- Agent Initialization ---
const agent = createAgent({
storageType: "session", // Initial storage
storageKey: "cajs_browser_basic",
agentsMd: AGENTS_MD_PATH,
dialogClass: "browser-basic-demo",
onEvent(event) {
logEvent(event);
if (event.type === "request:start") {
btnStop.disabled = false;
}
if (event.type === "request:end" || event.type === "request:error") {
btnStop.disabled = true;
updateStatus();
}
}
});
// --- Core Chat Functions ---
function showAgentsMdConfigured() {
if (!agentsMdBadgeEl) {
return;
}
agentsMdBadgeEl.textContent = "AGENTS.md configured";
agentsMdBadgeEl.className = "status-badge active";
}
async function sendMessage() {
const text = promptInput.value.trim();
if (!text) return;
if (!agent.isReady()) {
agent.openConfigPanel();
return;
}
// Clear input and add user message
promptInput.value = "";
addMessage("user", text);
// Ensure we have a session
if (!currentSession) {
currentSession = agent.createSession();
addMessage("system", "New session started.");
}
// Setup cancellation
abortController = new AbortController();
// Create agent message placeholder with thinking state
const agentMsgEl = addMessage("agent", "");
agentMsgEl.textContent = "Agent is thinking";
agentMsgEl.classList.add("thinking");
let fullText = "";
try {
// We use stream() for a better UX
const stream = currentSession.stream(text, {
signal: abortController.signal,
context: `User is viewing: ${document.title} (${location.href}). The current time is ${new Date().toLocaleTimeString()}.`
});
for await (const chunk of stream) {
if (fullText === "") {
agentMsgEl.classList.remove("thinking");
agentMsgEl.innerHTML = "";
}
fullText += chunk.text;
agentMsgEl.innerHTML = marked.parse(fullText);
messagesEl.scrollTop = messagesEl.scrollHeight;
}
} catch (error) {
if (error.name === "AbortError") {
addMessage("system", "Response stopped by user.");
} else {
console.error(error);
addMessage("system", `Error: ${error.message}`);
}
} finally {
// If we finished without any text (e.g. error or empty response),
// we should at least remove the thinking class
agentMsgEl.classList.remove("thinking");
if (!fullText && agentMsgEl.textContent === "Agent is thinking") {
agentMsgEl.textContent = "(No response)";
}
abortController = null;
}
}
function stopGeneration() {
if (abortController) {
abortController.abort();
}
}
function createNewSession() {
currentSession = agent.createSession();
addMessage("system", "Session reset. History cleared for new messages.");
}
function clearChat() {
messagesEl.innerHTML = '<div class="message system">Chat cleared.</div>';
currentSession = null;
}
// --- UI Helpers ---
function addMessage(role, text) {
const el = document.createElement("div");
el.className = `message ${role}`;
if (role === "agent" && text) {
el.innerHTML = marked.parse(text);
} else {
el.textContent = text;
}
messagesEl.appendChild(el);
messagesEl.scrollTop = messagesEl.scrollHeight;
return el;
}
function logEvent(event) {
const entry = document.createElement("div");
entry.className = "log-entry";
const time = new Date().toLocaleTimeString([], { hour12: false, hour: '2-digit', minute: '2-digit', second: '2-digit' });
entry.innerHTML = `<span class="log-time">[${time}]</span> ${event.type}`;
eventLogEl.prepend(entry);
}
function updateStatus() {
const ready = agent.isReady();
const profile = agent.getActiveProfile();
statusBadgeEl.textContent = ready ? `Ready: ${profile.name}` : "Not Configured";
statusBadgeEl.className = `status-badge ${ready ? "ready" : "not-ready"}`;
}
// showTyping removed in favor of in-bubble thinking indicator
// --- API Feature Demos ---
function createOllamaDemoProfile() {
const profileId = "demo-ollama";
agent.saveProfile(profileId, {
id: profileId,
name: "Local Ollama",
provider: "openai-compatible",
model: "llama3.2", // Common model
apiKey: "", // not needed for local Ollama
baseURL: "http://localhost:11434/v1",
systemPrompt: DEMO_SYSTEM_PROMPT
});
agent.setActiveProfile(profileId);
updateStatus();
addMessage("system", "Created and activated Ollama demo profile.");
}
function handleExport() {
const data = agent.exportProfiles();
const blob = new Blob([data], { type: "application/json" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = `client-agent-config-${new Date().toISOString().slice(0, 10)}.json`;
a.click();
URL.revokeObjectURL(url);
}
function handleImport() {
const input = document.createElement("input");
input.type = "file";
input.accept = ".json";
input.onchange = e => {
const file = e.target.files[0];
const reader = new FileReader();
reader.onload = async event => {
try {
await agent.importProfiles(event.target.result);
updateStatus();
addMessage("system", "Configuration imported successfully.");
} catch (err) {
alert("Import failed: " + err.message);
}
};
reader.readAsText(file);
};
input.click();
}
// --- Event Listeners ---
btnSend.addEventListener("click", sendMessage);
btnStop.addEventListener("click", stopGeneration);
document.querySelector("#btn-new-chat").addEventListener("click", createNewSession);
document.querySelector("#btn-clear").addEventListener("click", clearChat);
document.querySelector("#btn-check").addEventListener("click", updateStatus);
document.querySelector("#btn-open-config").addEventListener("click", () => agent.openConfigPanel());
document.querySelector("#btn-open-mcp").addEventListener("click", () => agent.openMcpPanel());
document.querySelector("#btn-quick-ollama").addEventListener("click", createOllamaDemoProfile);
document.querySelector("#btn-export").addEventListener("click", handleExport);
document.querySelector("#btn-import").addEventListener("click", handleImport);
// Storage Toggle
document.querySelectorAll('input[name="storage-type"]').forEach(input => {
input.addEventListener("change", (e) => {
agent.setStorageType(e.target.value);
addMessage("system", `Storage changed to: ${e.target.value}`);
});
});
// Shortcut: Shift+Enter to send
promptInput.addEventListener("keydown", (e) => {
if (e.key === "Enter" && !e.shiftKey) {
e.preventDefault();
sendMessage();
}
});
// Initial Status
updateStatus();
showAgentsMdConfigured();
if (!agent.isReady()) {
addMessage("system", "💡 Tip: Use 'Quick Ollama' if you have Ollama running locally, or open Profiles to add your OpenAI/Anthropic key.");
}