import { FolkShape } from "./folk-shape"; import { css, html } from "./tags"; const styles = css` :host { background: white; border-radius: 8px; box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1); min-width: 400px; min-height: 450px; } .header { display: flex; align-items: center; justify-content: space-between; padding: 8px 12px; background: linear-gradient(135deg, #0ea5e9, #6366f1); color: white; border-radius: 8px 8px 0 0; font-size: 12px; font-weight: 600; cursor: move; } .header-title { display: flex; align-items: center; gap: 6px; } .header-actions { display: flex; gap: 4px; } .header-actions button { background: transparent; border: none; color: white; cursor: pointer; padding: 2px 6px; border-radius: 4px; font-size: 14px; } .header-actions button:hover { background: rgba(255, 255, 255, 0.2); } .content { display: flex; flex-direction: column; height: calc(100% - 36px); overflow: hidden; } .messages { flex: 1; overflow-y: auto; padding: 12px; display: flex; flex-direction: column; gap: 12px; } .message { max-width: 85%; padding: 10px 14px; border-radius: 12px; font-size: 13px; line-height: 1.5; } .message.user { align-self: flex-end; background: linear-gradient(135deg, #0ea5e9, #6366f1); color: white; border-bottom-right-radius: 4px; } .message.assistant { align-self: flex-start; background: #f1f5f9; color: #1e293b; border-bottom-left-radius: 4px; } .message.streaming { background: #f1f5f9; } .message.streaming::after { content: "\u258C"; animation: blink 1s infinite; } @keyframes blink { 0%, 100% { opacity: 1; } 50% { opacity: 0; } } .placeholder { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; color: #94a3b8; text-align: center; gap: 8px; padding: 24px; } .placeholder-icon { font-size: 48px; opacity: 0.5; } .input-area { padding: 12px; border-top: 1px solid #e2e8f0; display: flex; flex-direction: column; gap: 8px; } .model-select { padding: 6px 10px; border: 2px solid #e2e8f0; border-radius: 6px; font-size: 12px; background: white; cursor: pointer; width: fit-content; } .prompt-row { display: flex; gap: 8px; } .prompt-input { flex: 1; padding: 10px 14px; border: 2px solid #e2e8f0; border-radius: 8px; font-size: 13px; resize: none; outline: none; font-family: inherit; } .prompt-input:focus { border-color: #6366f1; } .send-btn { padding: 10px 16px; background: linear-gradient(135deg, #0ea5e9, #6366f1); color: white; border: none; border-radius: 8px; font-size: 16px; cursor: pointer; transition: opacity 0.2s; } .send-btn:hover { opacity: 0.9; } .send-btn:disabled { opacity: 0.5; cursor: not-allowed; } .error { color: #ef4444; padding: 12px; background: #fef2f2; border-radius: 6px; font-size: 13px; margin: 12px; } .clear-btn { font-size: 12px; padding: 4px 8px; background: #f1f5f9; border: none; border-radius: 4px; cursor: pointer; color: #64748b; } .clear-btn:hover { background: #e2e8f0; } pre { background: #1e293b; color: #e2e8f0; padding: 12px; border-radius: 6px; overflow-x: auto; font-size: 12px; margin: 8px 0; } code { font-family: "Monaco", "Consolas", monospace; } `; export interface ChatMessage { id: string; role: "user" | "assistant"; content: string; timestamp: Date; } declare global { interface HTMLElementTagNameMap { "folk-prompt": FolkPrompt; } } export class FolkPrompt extends FolkShape { static override tagName = "folk-prompt"; static { const sheet = new CSSStyleSheet(); const parentRules = Array.from(FolkShape.styles.cssRules) .map((r) => r.cssText) .join("\n"); const childRules = Array.from(styles.cssRules) .map((r) => r.cssText) .join("\n"); sheet.replaceSync(`${parentRules}\n${childRules}`); this.styles = sheet; } #messages: ChatMessage[] = []; #isStreaming = false; #error: string | null = null; #model = "gemini-1.5-flash"; #messagesEl: HTMLElement | null = null; #promptInput: HTMLTextAreaElement | null = null; #modelSelect: HTMLSelectElement | null = null; #sendBtn: HTMLButtonElement | null = null; get messages() { return this.#messages; } override createRenderRoot() { const root = super.createRenderRoot(); const wrapper = document.createElement("div"); wrapper.innerHTML = html`
$2");
// Inline code
html = html.replace(/`([^`]+)`/g, "$1");
// Bold
html = html.replace(/\*\*([^*]+)\*\*/g, "$1");
// Line breaks
html = html.replace(/\n/g, "