File size: 11,462 Bytes
53f8186
059f0de
 
 
 
 
 
 
6b71b4b
059f0de
 
 
6b71b4b
 
059f0de
 
 
 
 
6b71b4b
059f0de
 
 
6b71b4b
 
 
 
 
059f0de
 
 
6b71b4b
 
 
 
 
 
 
 
 
 
 
53f8186
 
059f0de
53f8186
059f0de
 
 
 
 
 
 
 
53f8186
 
 
 
 
 
 
 
 
 
 
 
059f0de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6b71b4b
 
 
 
 
 
 
059f0de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6b71b4b
059f0de
 
 
6b71b4b
059f0de
 
 
 
 
 
 
 
 
6b71b4b
 
059f0de
 
 
 
 
 
 
 
 
 
 
 
6b71b4b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
059f0de
 
6b71b4b
 
 
 
059f0de
6b71b4b
 
059f0de
 
6b71b4b
 
 
 
 
 
 
 
 
 
 
 
059f0de
 
 
 
 
 
 
 
 
 
53f8186
059f0de
 
 
53f8186
059f0de
 
 
53f8186
059f0de
 
 
 
 
6b71b4b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53f8186
 
 
 
6b71b4b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53f8186
6b71b4b
 
 
 
 
53f8186
 
6b71b4b
53f8186
 
6b71b4b
 
 
 
 
 
 
 
 
059f0de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53f8186
6b71b4b
059f0de
 
53f8186
6b71b4b
53f8186
6b71b4b
 
53f8186
6b71b4b
 
53f8186
059f0de
 
 
 
53f8186
059f0de
53f8186
 
059f0de
 
6b71b4b
 
53f8186
 
6b71b4b
 
 
 
 
 
 
 
53f8186
 
 
 
 
6b71b4b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
059f0de
 
53f8186
 
6b71b4b
 
059f0de
 
6b71b4b
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { DEFAULT_LOCAL_MODEL_KEY, LOCAL_MODELS, createPiAgent } from "./piAgent.js";
import { createSandbox } from "./sandbox.js";
import "./styles.css";

const nodes = {
  status: document.querySelector("#status"),
  modelStatus: document.querySelector("#model-status"),
  sandboxStatus: document.querySelector("#sandbox-status"),
  chat: document.querySelector("#chat"),
  eventLog: document.querySelector("#event-log"),
  files: document.querySelector("#files"),
  prompt: document.querySelector("#prompt"),
  composer: document.querySelector("#composer"),
  send: document.querySelector("#send"),
  boot: document.querySelector("#boot-sandbox"),
  reset: document.querySelector("#reset-sandbox"),
  demo: document.querySelector("#demo-prompt"),
  mode: document.querySelector("#mode"),
  device: document.querySelector("#device"),
  gateDevice: document.querySelector("#gate-device"),
  maxTokens: document.querySelector("#max-new-tokens"),
  temperature: document.querySelector("#temperature"),
  modelLabel: document.querySelector("#model-label"),
  loadModel: document.querySelector("#load-model"),
  confirmLoadModel: document.querySelector("#confirm-load-model"),
  useTestModel: document.querySelector("#use-test-model"),
  modelGate: document.querySelector("#model-gate"),
  gateStatus: document.querySelector("#gate-status"),
};

const params = new URLSearchParams(window.location.search);
let modelReady = false;
let transcriptText = "";

if (!navigator.gpu) {
  nodes.device.value = "wasm";
  nodes.gateDevice.value = "wasm";
}
if (params.get("device")) {
  nodes.device.value = params.get("device");
  nodes.gateDevice.value = params.get("device");
}
if (LOCAL_MODELS[params.get("model")]) nodes.mode.value = params.get("model");
if (LOCAL_MODELS[params.get("mode")]) nodes.mode.value = params.get("mode");
if (params.get("mode") === "mock") nodes.mode.value = "mock";
if (!LOCAL_MODELS[nodes.mode.value] && nodes.mode.value !== "mock") nodes.mode.value = DEFAULT_LOCAL_MODEL_KEY;

const sandbox = createSandbox({
  onStatus: (text) => setSandboxStatus(text),
  onLog: (text) => logEvent("sandbox", text),
});

let agent = createAgent();

function selectedModel() {
  return LOCAL_MODELS[nodes.mode.value] || LOCAL_MODELS[DEFAULT_LOCAL_MODEL_KEY];
}

function isMockMode() {
  return nodes.mode.value === "mock";
}

function updateModelLabel() {
  nodes.modelLabel.textContent = isMockMode() ? "Deterministic test model" : selectedModel().id;
}

function textFromContent(content) {
  if (typeof content === "string") return content;
  if (!Array.isArray(content)) return "";
  return content
    .filter((part) => part.type === "text")
    .map((part) => part.text)
    .join("\n");
}

function setStatus(text) {
  nodes.status.textContent = text;
}

function setSandboxStatus(text) {
  nodes.sandboxStatus.textContent = text;
}

function setModelStatus(text) {
  nodes.modelStatus.textContent = text;
}

function setBusy(isBusy) {
  nodes.send.disabled = isBusy;
  nodes.loadModel.disabled = isBusy;
  nodes.confirmLoadModel.disabled = isBusy;
  nodes.useTestModel.disabled = isBusy;
}

function logEvent(kind, text) {
  const line = `[${new Date().toLocaleTimeString()}] ${kind}: ${text}`;
  nodes.eventLog.textContent = `${nodes.eventLog.textContent}${line}\n`;
  nodes.eventLog.scrollTop = nodes.eventLog.scrollHeight;
}

function createAgent() {
  const next = createPiAgent({
    sandbox,
    modelMode: () => nodes.mode.value,
    device: () => nodes.device.value,
    maxTokens: () => nodes.maxTokens.value,
    temperature: () => nodes.temperature.value,
    onModelStatus: setModelStatus,
  });
  next.subscribe((event) => {
    switch (event.type) {
      case "agent_start":
        setStatus("Agent running");
        setBusy(true);
        logEvent("agent", "start");
        break;
      case "message_end":
        renderChat();
        break;
      case "tool_execution_start":
        logEvent("tool", `${event.toolName} started`);
        break;
      case "tool_execution_end":
        logEvent("tool", `${event.toolName} finished`);
        break;
      case "agent_end":
        setStatus("Ready");
        setBusy(false);
        renderChat();
        refreshFiles().catch((error) => logEvent("files", error.message));
        break;
      default:
        break;
    }
  });
  return next;
}

function resetAgent() {
  agent.abort();
  agent = createAgent();
  renderChat();
}

function makeElement(tag, className, text) {
  const element = document.createElement(tag);
  if (className) element.className = className;
  if (text !== undefined) element.textContent = text;
  return element;
}

function renderMessage(message) {
  if (message.role === "user") {
    const bubble = makeElement("article", "message user");
    bubble.append(makeElement("div", "message-label", "You"));
    bubble.append(makeElement("div", "message-body", textFromContent(message.content)));
    return bubble;
  }

  if (message.role === "toolResult") {
    const bubble = makeElement("article", `message tool${message.isError ? " error" : ""}`);
    bubble.append(makeElement("div", "message-label", message.isError ? `Tool error: ${message.toolName}` : `Tool: ${message.toolName}`));
    const body = makeElement("pre", "message-code", textFromContent(message.content));
    bubble.append(body);
    return bubble;
  }

  const bubble = makeElement("article", "message assistant");
  bubble.append(makeElement("div", "message-label", "Pi"));
  const text = textFromContent(message.content);
  if (text) bubble.append(makeElement("div", "message-body", text));
  const toolCalls = Array.isArray(message.content) ? message.content.filter((part) => part.type === "toolCall") : [];
  for (const call of toolCalls) {
    const tool = makeElement("div", "tool-call");
    tool.append(makeElement("span", "tool-name", call.name));
    tool.append(makeElement("code", "", JSON.stringify(call.arguments)));
    bubble.append(tool);
  }
  return bubble;
}

function renderChat() {
  nodes.chat.textContent = "";
  const messages = agent.state.messages;
  transcriptText = messages
    .map((message) => {
      if (message.role === "toolResult") return `TOOL ${message.toolName}\n${textFromContent(message.content)}`;
      return `${message.role.toUpperCase()}\n${textFromContent(message.content)}`;
    })
    .join("\n\n");

  if (messages.length === 0) {
    const empty = makeElement("section", "empty-chat");
    empty.append(makeElement("h2", "", "What should Pi do in the sandbox?"));
    empty.append(makeElement("p", "", "Ready when you are."));
    nodes.chat.append(empty);
  } else {
    for (const message of messages) {
      nodes.chat.append(renderMessage(message));
    }
  }
  nodes.chat.scrollTop = nodes.chat.scrollHeight;
}

async function refreshFiles() {
  if (!sandbox.isReady) {
    nodes.files.textContent = "Sandbox not booted.";
    return;
  }
  nodes.files.textContent = await sandbox.listFiles(".");
}

async function bootSandbox({ refresh = true, rethrow = false } = {}) {
  nodes.boot.disabled = true;
  try {
    await sandbox.boot();
    if (refresh) await refreshFiles();
  } catch (error) {
    setSandboxStatus("Sandbox error");
    logEvent("sandbox", error.stack || error.message || String(error));
    if (rethrow) throw error;
  } finally {
    nodes.boot.disabled = false;
  }
}

function hideGate() {
  nodes.modelGate.classList.add("hidden");
}

function showGate(text = "Ready.") {
  nodes.gateStatus.textContent = text;
  nodes.modelGate.classList.remove("hidden");
}

async function loadModelFromControls() {
  if (nodes.mode.value === "mock") {
    modelReady = true;
    setModelStatus("Deterministic test model");
    hideGate();
    return;
  }

  setBusy(true);
  nodes.gateStatus.textContent = "Downloading model...";
  try {
    nodes.gateStatus.textContent = "Booting sandbox...";
    await bootSandbox({ refresh: false, rethrow: true });
    refreshFiles().catch((error) => logEvent("files", error.message));
    nodes.gateStatus.textContent = "Downloading model...";
    await agent.preloadModel();
    modelReady = true;
    setModelStatus("Model ready");
    nodes.gateStatus.textContent = "Model ready.";
    hideGate();
  } catch (error) {
    const message = error.stack || error.message || String(error);
    setModelStatus("Model error");
    nodes.gateStatus.textContent = message;
    logEvent("model", message);
  } finally {
    setBusy(false);
  }
}

async function sendPrompt() {
  const prompt = nodes.prompt.value.trim();
  if (!prompt) return;
  if (!isMockMode() && !modelReady) {
    showGate("Download the model before sending, or use the test model.");
    return;
  }

  setBusy(true);
  setStatus("Agent running");
  logEvent("agent", "start");
  try {
    await bootSandbox({ refresh: false, rethrow: true });
    refreshFiles().catch((error) => logEvent("files", error.message));
    nodes.prompt.value = "";
    await agent.prompt(prompt);
  } catch (error) {
    setStatus("Error");
    setBusy(false);
    logEvent("agent", error.stack || error.message || String(error));
  }
}

nodes.boot.addEventListener("click", bootSandbox);

nodes.reset.addEventListener("click", async () => {
  nodes.reset.disabled = true;
  try {
    await sandbox.reset();
    resetAgent();
    await refreshFiles();
  } catch (error) {
    logEvent("reset", error.stack || error.message || String(error));
  } finally {
    nodes.reset.disabled = false;
  }
});

nodes.demo.addEventListener("click", () => {
  nodes.prompt.value = "Create hello.js containing JavaScript that computes 21 * 2 and prints result: 42, then run node hello.js.";
  nodes.prompt.focus();
});

nodes.composer.addEventListener("submit", (event) => {
  event.preventDefault();
  setTimeout(() => sendPrompt(), 0);
});

nodes.prompt.addEventListener("keydown", (event) => {
  if (event.key === "Enter" && (event.metaKey || event.ctrlKey)) {
    event.preventDefault();
    setTimeout(() => sendPrompt(), 0);
  }
});

nodes.mode.addEventListener("change", () => {
  modelReady = isMockMode();
  resetAgent();
  updateModelLabel();
  setModelStatus(isMockMode() ? "Deterministic test model" : "Model idle");
});

nodes.device.addEventListener("change", () => {
  nodes.gateDevice.value = nodes.device.value;
  modelReady = isMockMode();
  if (!isMockMode()) setModelStatus("Model idle");
  resetAgent();
});

nodes.gateDevice.addEventListener("change", () => {
  nodes.device.value = nodes.gateDevice.value;
});

nodes.loadModel.addEventListener("click", () => {
  if (isMockMode()) {
    nodes.mode.value = DEFAULT_LOCAL_MODEL_KEY;
    updateModelLabel();
    resetAgent();
  }
  showGate("Ready.");
});

nodes.confirmLoadModel.addEventListener("click", async () => {
  nodes.device.value = nodes.gateDevice.value;
  resetAgent();
  await loadModelFromControls();
});

nodes.useTestModel.addEventListener("click", () => {
  nodes.mode.value = "mock";
  modelReady = true;
  resetAgent();
  setModelStatus("Deterministic test model");
  hideGate();
});

setStatus("Ready");
setSandboxStatus("Not booted");
modelReady = isMockMode();
updateModelLabel();
setModelStatus(modelReady ? "Deterministic test model" : "Model idle");
renderChat();
refreshFiles().catch(() => {});

if (params.get("setup") === "skip" || nodes.mode.value === "mock") {
  hideGate();
} else {
  showGate("Ready.");
}

window.__piWebAgent = {
  get transcript() {
    return transcriptText;
  },
  get modelReady() {
    return modelReady;
  },
};