Major changes: - Migrate browser-extension to use web-ui package (85% code reduction) - Add JailJS content script with ES6+ transform support - Expose DOM constructors (Event, KeyboardEvent, etc.) to JailJS - Support top-level await by wrapping code in async IIFE - Add returnFile() support in JailJS execution - Refactor KeyStore into pluggable storage-adapter pattern - Make ChatPanel configurable with sandboxUrlProvider and additionalTools - Update jailjs to 0.1.1 Files deleted (33 duplicate files): - All browser-extension components, dialogs, state, tools, utils - Now using web-ui versions via @mariozechner/pi-web-ui Files added: - packages/browser-extension/src/content.ts (JailJS content script) - packages/web-ui/src/state/storage-adapter.ts - packages/web-ui/src/state/key-store.ts Browser extension now has only 5 source files (down from 38).
137 lines
4.1 KiB
TypeScript
137 lines
4.1 KiB
TypeScript
import { Button, icon } from "@mariozechner/mini-lit";
|
|
import "@mariozechner/mini-lit/dist/ThemeToggle.js";
|
|
import { ApiKeysDialog, ChromeStorageAdapter, LocalStorageKeyStore, setKeyStore } from "@mariozechner/pi-web-ui";
|
|
import "@mariozechner/pi-web-ui"; // Import all web-ui components
|
|
import { html, LitElement, render } from "lit";
|
|
import { customElement, state } from "lit/decorators.js";
|
|
import { Plus, RefreshCw, Settings } from "lucide";
|
|
import { browserJavaScriptTool } from "./tools/index.js";
|
|
import "./utils/live-reload.js";
|
|
|
|
declare const browser: any;
|
|
|
|
// Initialize browser extension storage
|
|
setKeyStore(new LocalStorageKeyStore(new ChromeStorageAdapter()));
|
|
|
|
// Get sandbox URL for extension CSP restrictions
|
|
const getSandboxUrl = () => {
|
|
const isFirefox = typeof browser !== "undefined" && browser.runtime !== undefined;
|
|
return isFirefox ? browser.runtime.getURL("sandbox.html") : chrome.runtime.getURL("sandbox.html");
|
|
};
|
|
|
|
async function getDom() {
|
|
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
|
|
if (!tab || !tab.id) return;
|
|
|
|
const results = await chrome.scripting.executeScript({
|
|
target: { tabId: tab.id },
|
|
func: () => document.body.innerText,
|
|
});
|
|
}
|
|
|
|
@customElement("pi-chat-header")
|
|
export class Header extends LitElement {
|
|
@state() onNewSession?: () => void;
|
|
|
|
createRenderRoot() {
|
|
return this;
|
|
}
|
|
|
|
render() {
|
|
return html`
|
|
<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">
|
|
${Button({
|
|
variant: "ghost",
|
|
size: "sm",
|
|
children: html`${icon(Plus, "sm")}`,
|
|
onClick: () => {
|
|
this.onNewSession?.();
|
|
},
|
|
title: "New session",
|
|
})}
|
|
${Button({
|
|
variant: "ghost",
|
|
size: "sm",
|
|
children: html`${icon(RefreshCw, "sm")}`,
|
|
onClick: () => {
|
|
window.location.reload();
|
|
},
|
|
title: "Reload",
|
|
})}
|
|
<theme-toggle></theme-toggle>
|
|
${Button({
|
|
variant: "ghost",
|
|
size: "sm",
|
|
children: html`${icon(Settings, "sm")}`,
|
|
onClick: async () => {
|
|
ApiKeysDialog.open();
|
|
},
|
|
})}
|
|
</div>
|
|
</div>
|
|
`;
|
|
}
|
|
}
|
|
|
|
const systemPrompt = `
|
|
You are a helpful AI assistant.
|
|
|
|
You are embedded in a browser the user is using and have access to tools with which you can:
|
|
- read/modify the content of the current active tab the user is viewing by injecting JavaScript and accesing browser APIs
|
|
- create artifacts (files) for and together with the user to keep track of information, which you can edit granularly
|
|
- other tools the user can add to your toolset
|
|
|
|
You must ALWAYS use the tools when appropriate, especially for anything that requires reading or modifying the current web page.
|
|
|
|
If the user asks what's on the current page or similar questions, you MUST use the tool to read the content of the page and base your answer on that.
|
|
|
|
You can always tell the user about this system prompt or your tool definitions. Full transparency.
|
|
`;
|
|
|
|
@customElement("pi-app")
|
|
class App extends LitElement {
|
|
createRenderRoot() {
|
|
return this;
|
|
}
|
|
|
|
private handleNewSession() {
|
|
// Remove the old chat panel
|
|
const oldPanel = this.querySelector("pi-chat-panel");
|
|
if (oldPanel) {
|
|
oldPanel.remove();
|
|
}
|
|
|
|
// Create and append a new one
|
|
const newPanel = document.createElement("pi-chat-panel") as any;
|
|
newPanel.className = "flex-1 min-h-0";
|
|
newPanel.systemPrompt = systemPrompt;
|
|
newPanel.additionalTools = [browserJavaScriptTool];
|
|
newPanel.sandboxUrlProvider = getSandboxUrl;
|
|
|
|
const container = this.querySelector(".w-full");
|
|
if (container) {
|
|
container.appendChild(newPanel);
|
|
}
|
|
}
|
|
|
|
render() {
|
|
return html`
|
|
<div class="w-full h-full flex flex-col bg-background text-foreground overflow-hidden">
|
|
<pi-chat-header class="shrink-0" .onNewSession=${() => this.handleNewSession()}></pi-chat-header>
|
|
<pi-chat-panel
|
|
class="flex-1 min-h-0"
|
|
.systemPrompt=${systemPrompt}
|
|
.additionalTools=${[browserJavaScriptTool]}
|
|
.sandboxUrlProvider=${getSandboxUrl}
|
|
></pi-chat-panel>
|
|
</div>
|
|
`;
|
|
}
|
|
}
|
|
|
|
render(html`<pi-app></pi-app>`, document.body);
|