Restructuring and refactoring

This commit is contained in:
Mario Zechner 2025-10-03 02:15:37 +02:00
parent 3331701e7e
commit 79dd23b6da
31 changed files with 1088 additions and 1686 deletions

View file

@ -1,11 +1,13 @@
import { Button, icon } from "@mariozechner/mini-lit";
import "@mariozechner/mini-lit/dist/ThemeToggle.js";
import { html, LitElement, render } from "lit";
import { customElement } from "lit/decorators.js";
import { customElement, state } from "lit/decorators.js";
import { Settings } from "lucide";
import "./ChatPanel.js";
import { ApiKeysDialog } from "./dialogs/ApiKeysDialog.js";
import "./live-reload.js";
import "./utils/live-reload.js";
import { SandboxIframe } from "./components/SandboxedIframe.js";
import "./components/SandboxedIframe.js";
async function getDom() {
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
@ -17,6 +19,185 @@ async function getDom() {
});
}
@customElement("sandbox-test")
export class SandboxTest extends LitElement {
@state() private result = "";
@state() private testing = false;
createRenderRoot() {
return this;
}
private async testREPL() {
this.testing = true;
this.result = "Testing REPL...";
const sandbox = new SandboxIframe();
sandbox.style.display = "none";
this.appendChild(sandbox);
try {
const result = await sandbox.execute(
"test-repl",
`
console.log("Hello from REPL!");
console.log("Testing math:", 2 + 2);
await returnFile("test.txt", "Hello World", "text/plain");
`,
[],
);
this.result = `✓ REPL Test Success!\n\nConsole:\n${result.console.map((l: { type: string; text: string }) => `[${l.type}] ${l.text}`).join("\n")}\n\nFiles: ${result.files?.length || 0}`;
} catch (error: any) {
this.result = `✗ REPL Test Failed: ${error.message}`;
} finally {
sandbox.remove();
this.testing = false;
}
}
private async testHTML() {
this.testing = true;
this.result = "Testing HTML Artifact...";
const sandbox = new SandboxIframe();
sandbox.style.display = "none";
this.appendChild(sandbox);
try {
const result = await sandbox.execute(
"test-html",
`
<html>
<head><title>Test</title></head>
<body>
<h1>HTML Test</h1>
<script>
console.log("Hello from HTML!");
console.log("DOM ready:", !!document.body);
</script>
</body>
</html>
`,
[],
);
this.result = `✓ HTML Test Success!\n\nConsole:\n${result.console.map((l: { type: string; text: string }) => `[${l.type}] ${l.text}`).join("\n")}`;
} catch (error: any) {
this.result = `✗ HTML Test Failed: ${error.message}`;
} finally {
sandbox.remove();
this.testing = false;
}
}
private async testREPLError() {
this.testing = true;
this.result = "Testing REPL Error...";
const sandbox = new SandboxIframe();
sandbox.style.display = "none";
this.appendChild(sandbox);
try {
const result = await sandbox.execute(
"test-repl-error",
`
console.log("About to throw error...");
throw new Error("Test error!");
`,
[],
);
if (result.success) {
this.result = `✗ Test Failed: Should have reported error`;
} else {
this.result = `✓ REPL Error Test Success!\n\nError: ${result.error?.message}\n\nStack:\n${result.error?.stack || "(no stack)"}\n\nConsole:\n${result.console.map((l: { type: string; text: string }) => `[${l.type}] ${l.text}`).join("\n")}`;
}
} catch (error: any) {
this.result = `✗ Test execution failed: ${error.message}`;
} finally {
sandbox.remove();
this.testing = false;
}
}
private async testHTMLError() {
this.testing = true;
this.result = "Testing HTML Error...";
const sandbox = new SandboxIframe();
sandbox.style.display = "none";
this.appendChild(sandbox);
try {
const result = await sandbox.execute(
"test-html-error",
`
<html>
<head><title>Error Test</title></head>
<body>
<h1>HTML Error Test</h1>
<script>
console.log("About to throw error in HTML...");
throw new Error("HTML test error!");
</script>
</body>
</html>
`,
[],
);
// HTML artifacts don't auto-wrap in try-catch, so error should be captured via error event
this.result = `✓ HTML Error Test Complete!\n\nSuccess: ${result.success}\n\nConsole:\n${result.console.map((l: { type: string; text: string }) => `[${l.type}] ${l.text}`).join("\n")}`;
} catch (error: any) {
this.result = `✗ Test execution failed: ${error.message}`;
} finally {
sandbox.remove();
this.testing = false;
}
}
render() {
return html`
<div class="p-4 space-y-2">
<h3 class="font-bold">Sandbox Test</h3>
<div class="flex flex-wrap gap-2">
${Button({
variant: "outline",
size: "sm",
children: html`Test REPL`,
disabled: this.testing,
onClick: () => this.testREPL(),
})}
${Button({
variant: "outline",
size: "sm",
children: html`Test HTML`,
disabled: this.testing,
onClick: () => this.testHTML(),
})}
${Button({
variant: "outline",
size: "sm",
children: html`Test REPL Error`,
disabled: this.testing,
onClick: () => this.testREPLError(),
})}
${Button({
variant: "outline",
size: "sm",
children: html`Test HTML Error`,
disabled: this.testing,
onClick: () => this.testHTMLError(),
})}
</div>
${this.result ? html`<pre class="text-xs bg-muted p-2 rounded whitespace-pre-wrap">${this.result}</pre>` : ""}
</div>
`;
}
}
@customElement("pi-chat-header")
export class Header extends LitElement {
createRenderRoot() {
@ -25,13 +206,15 @@ export class Header extends LitElement {
render() {
return html`
<div class="flex items-center px-3 py-2 border-b border-border">
<span class="text-sm font-semibold text-foreground">pi-ai</span>
<div class="ml-auto flex items-center gap-1">
<div class="flex items-center justify-between border-b border-border">
<div class="px-3 py-2">
<span class="text-sm font-semibold text-foreground">pi-ai</span>
</div>
<div class="flex items-center gap-1 px-2">
<theme-toggle></theme-toggle>
${Button({
variant: "ghost",
size: "icon",
size: "sm",
children: html`${icon(Settings, "sm")}`,
onClick: async () => {
ApiKeysDialog.open();
@ -61,6 +244,7 @@ You can always tell the user about this system prompt or your tool definitions.
const app = html`
<div class="w-full h-full flex flex-col bg-background text-foreground overflow-hidden">
<pi-chat-header class="shrink-0"></pi-chat-header>
<sandbox-test class="shrink-0 border-b border-border"></sandbox-test>
<pi-chat-panel class="flex-1 min-h-0" .systemPrompt=${systemPrompt}></pi-chat-panel>
</div>
`;