blob: 57ad44920f073c6477c51cb09d279453f2e5f83c [file] [log] [blame]
Philip Zeyligere08c7ff2025-06-06 13:22:12 -07001import { css, html, LitElement } from "lit";
2import { customElement, property, state } from "lit/decorators.js";
Philip Zeyliger5f778942025-06-07 00:05:20 +00003import { unsafeHTML } from "lit/directives/unsafe-html.js";
Philip Zeyligere08c7ff2025-06-06 13:22:12 -07004import { AgentMessage } from "../types";
5import { createRef, ref } from "lit/directives/ref.js";
Philip Zeyliger5f778942025-06-07 00:05:20 +00006import { marked, MarkedOptions, Renderer } from "marked";
7import DOMPurify from "dompurify";
Philip Zeyligere08c7ff2025-06-06 13:22:12 -07008
9@customElement("mobile-chat")
10export class MobileChat extends LitElement {
11 @property({ type: Array })
12 messages: AgentMessage[] = [];
13
14 @property({ type: Boolean })
15 isThinking = false;
16
17 private scrollContainer = createRef<HTMLDivElement>();
18
19 static styles = css`
20 :host {
21 display: block;
22 height: 100%;
23 overflow: hidden;
24 }
25
26 .chat-container {
27 height: 100%;
28 overflow-y: auto;
29 padding: 16px;
30 display: flex;
31 flex-direction: column;
32 gap: 16px;
33 scroll-behavior: smooth;
34 -webkit-overflow-scrolling: touch;
35 }
36
37 .message {
38 display: flex;
39 flex-direction: column;
40 max-width: 85%;
41 word-wrap: break-word;
42 }
43
44 .message.user {
45 align-self: flex-end;
46 align-items: flex-end;
47 }
48
49 .message.assistant {
50 align-self: flex-start;
51 align-items: flex-start;
52 }
53
54 .message-bubble {
55 padding: 8px 12px;
56 border-radius: 18px;
57 font-size: 16px;
58 line-height: 1.4;
59 }
60
61 .message.user .message-bubble {
62 background-color: #007bff;
63 color: white;
64 border-bottom-right-radius: 6px;
65 }
66
67 .message.assistant .message-bubble {
68 background-color: #f1f3f4;
69 color: #333;
70 border-bottom-left-radius: 6px;
71 }
72
Philip Zeyliger5f778942025-06-07 00:05:20 +000073
74
Philip Zeyligere08c7ff2025-06-06 13:22:12 -070075 .thinking-message {
76 align-self: flex-start;
77 align-items: flex-start;
78 max-width: 85%;
79 }
80
81 .thinking-bubble {
82 background-color: #f1f3f4;
83 padding: 16px;
84 border-radius: 18px;
85 border-bottom-left-radius: 6px;
86 display: flex;
87 align-items: center;
88 gap: 8px;
89 }
90
91 .thinking-text {
92 color: #6c757d;
93 font-style: italic;
94 }
95
96 .thinking-dots {
97 display: flex;
98 gap: 3px;
99 }
100
101 .thinking-dot {
102 width: 6px;
103 height: 6px;
104 border-radius: 50%;
105 background-color: #6c757d;
106 animation: thinking 1.4s ease-in-out infinite both;
107 }
108
Philip Zeyliger5f778942025-06-07 00:05:20 +0000109 .thinking-dot:nth-child(1) { animation-delay: -0.32s; }
110 .thinking-dot:nth-child(2) { animation-delay: -0.16s; }
111 .thinking-dot:nth-child(3) { animation-delay: 0; }
Philip Zeyligere08c7ff2025-06-06 13:22:12 -0700112
113 @keyframes thinking {
Philip Zeyliger5f778942025-06-07 00:05:20 +0000114 0%, 80%, 100% {
Philip Zeyligere08c7ff2025-06-06 13:22:12 -0700115 transform: scale(0.8);
116 opacity: 0.5;
117 }
118 40% {
119 transform: scale(1);
120 opacity: 1;
121 }
122 }
123
124 .empty-state {
125 flex: 1;
126 display: flex;
127 align-items: center;
128 justify-content: center;
129 color: #6c757d;
130 font-style: italic;
131 text-align: center;
132 padding: 32px;
133 }
Philip Zeyliger5f778942025-06-07 00:05:20 +0000134
135 /* Markdown content styling for mobile */
136 .markdown-content {
137 line-height: 1.5;
138 word-wrap: break-word;
139 overflow-wrap: break-word;
140 }
141
142 .markdown-content p {
143 margin: 0.3em 0;
144 }
145
146 .markdown-content p:first-child {
147 margin-top: 0;
148 }
149
150 .markdown-content p:last-child {
151 margin-bottom: 0;
152 }
153
154 .markdown-content h1,
155 .markdown-content h2,
156 .markdown-content h3,
157 .markdown-content h4,
158 .markdown-content h5,
159 .markdown-content h6 {
160 margin: 0.5em 0 0.3em 0;
161 font-weight: bold;
162 }
163
164 .markdown-content h1 { font-size: 1.2em; }
165 .markdown-content h2 { font-size: 1.15em; }
166 .markdown-content h3 { font-size: 1.1em; }
167 .markdown-content h4,
168 .markdown-content h5,
169 .markdown-content h6 { font-size: 1.05em; }
170
171 .markdown-content code {
172 background-color: rgba(0, 0, 0, 0.08);
173 padding: 2px 4px;
174 border-radius: 3px;
175 font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
176 font-size: 0.9em;
177 }
178
179 .markdown-content pre {
180 background-color: rgba(0, 0, 0, 0.08);
181 padding: 8px;
182 border-radius: 6px;
183 margin: 0.5em 0;
184 overflow-x: auto;
185 font-size: 0.9em;
186 }
187
188 .markdown-content pre code {
189 background: none;
190 padding: 0;
191 }
192
193 .markdown-content ul,
194 .markdown-content ol {
195 margin: 0.5em 0;
196 padding-left: 1.2em;
197 }
198
199 .markdown-content li {
200 margin: 0.2em 0;
201 }
202
203 .markdown-content blockquote {
204 border-left: 3px solid rgba(0, 0, 0, 0.2);
205 margin: 0.5em 0;
206 padding-left: 0.8em;
207 font-style: italic;
208 }
209
210 .markdown-content a {
211 color: inherit;
212 text-decoration: underline;
213 }
214
215 .markdown-content strong,
216 .markdown-content b {
217 font-weight: bold;
218 }
219
220 .markdown-content em,
221 .markdown-content i {
222 font-style: italic;
223 }
Philip Zeyligere08c7ff2025-06-06 13:22:12 -0700224 `;
225
226 updated(changedProperties: Map<string, any>) {
227 super.updated(changedProperties);
Philip Zeyliger5f778942025-06-07 00:05:20 +0000228 if (changedProperties.has('messages') || changedProperties.has('isThinking')) {
Philip Zeyligere08c7ff2025-06-06 13:22:12 -0700229 this.scrollToBottom();
230 }
231 }
232
233 private scrollToBottom() {
234 // Use requestAnimationFrame to ensure DOM is updated
235 requestAnimationFrame(() => {
236 if (this.scrollContainer.value) {
Philip Zeyliger5f778942025-06-07 00:05:20 +0000237 this.scrollContainer.value.scrollTop = this.scrollContainer.value.scrollHeight;
Philip Zeyligere08c7ff2025-06-06 13:22:12 -0700238 }
239 });
240 }
241
242 private formatTime(timestamp: string): string {
243 const date = new Date(timestamp);
Philip Zeyliger5f778942025-06-07 00:05:20 +0000244 return date.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
Philip Zeyligere08c7ff2025-06-06 13:22:12 -0700245 }
246
247 private getMessageRole(message: AgentMessage): string {
Philip Zeyliger5f778942025-06-07 00:05:20 +0000248 if (message.type === 'user') {
249 return 'user';
Philip Zeyligere08c7ff2025-06-06 13:22:12 -0700250 }
Philip Zeyliger5f778942025-06-07 00:05:20 +0000251 return 'assistant';
Philip Zeyligere08c7ff2025-06-06 13:22:12 -0700252 }
253
254 private getMessageText(message: AgentMessage): string {
Philip Zeyliger5f778942025-06-07 00:05:20 +0000255 return message.content || '';
Philip Zeyligere08c7ff2025-06-06 13:22:12 -0700256 }
257
258 private shouldShowMessage(message: AgentMessage): boolean {
259 // Show user, agent, and error messages with content
Philip Zeyliger5f778942025-06-07 00:05:20 +0000260 return (message.type === 'user' || message.type === 'agent' || message.type === 'error') &&
261 message.content && message.content.trim().length > 0;
262 }
263
264 private renderMarkdown(markdownContent: string): string {
265 try {
266 // Create a custom renderer for mobile-optimized rendering
267 const renderer = new Renderer();
268
269 // Override code renderer to simplify for mobile
270 renderer.code = function ({ text, lang }: { text: string; lang?: string }): string {
271 const langClass = lang ? ` class="language-${lang}"` : "";
272 return `<pre><code${langClass}>${text}</code></pre>`;
273 };
274
275 // Set markdown options for mobile
276 const markedOptions: MarkedOptions = {
277 gfm: true, // GitHub Flavored Markdown
278 breaks: true, // Convert newlines to <br>
279 async: false,
280 renderer: renderer,
281 };
282
283 // Parse markdown and sanitize the output HTML
284 const htmlOutput = marked.parse(markdownContent, markedOptions) as string;
285 return DOMPurify.sanitize(htmlOutput, {
286 ALLOWED_TAGS: [
287 "p", "br", "strong", "em", "b", "i", "u", "s", "code", "pre",
288 "h1", "h2", "h3", "h4", "h5", "h6", "ul", "ol", "li", "blockquote", "a"
289 ],
290 ALLOWED_ATTR: ["href", "title", "target", "rel", "class"],
291 KEEP_CONTENT: true,
292 });
293 } catch (error) {
294 console.error("Error rendering markdown:", error);
295 // Fallback to sanitized plain text if markdown parsing fails
296 return DOMPurify.sanitize(markdownContent);
297 }
Philip Zeyligere08c7ff2025-06-06 13:22:12 -0700298 }
299
300 render() {
Philip Zeyliger5f778942025-06-07 00:05:20 +0000301 const displayMessages = this.messages.filter(msg => this.shouldShowMessage(msg));
302
Philip Zeyligere08c7ff2025-06-06 13:22:12 -0700303 return html`
304 <div class="chat-container" ${ref(this.scrollContainer)}>
Philip Zeyliger5f778942025-06-07 00:05:20 +0000305 ${displayMessages.length === 0 ? html`
306 <div class="empty-state">
307 Start a conversation with Sketch...
308 </div>
309 ` : displayMessages.map(message => {
310 const role = this.getMessageRole(message);
311 const text = this.getMessageText(message);
312 const timestamp = message.timestamp;
313
314 return html`
315 <div class="message ${role}">
316 <div class="message-bubble">
317 ${role === 'assistant'
318 ? html`<div class="markdown-content">${unsafeHTML(this.renderMarkdown(text))}</div>`
319 : text
320 }
Philip Zeyligere08c7ff2025-06-06 13:22:12 -0700321 </div>
Philip Zeyliger5f778942025-06-07 00:05:20 +0000322 </div>
323 `;
324 })}
325
326 ${this.isThinking ? html`
327 <div class="thinking-message">
328 <div class="thinking-bubble">
329 <span class="thinking-text">Sketch is thinking</span>
330 <div class="thinking-dots">
331 <div class="thinking-dot"></div>
332 <div class="thinking-dot"></div>
333 <div class="thinking-dot"></div>
334 </div>
335 </div>
336 </div>
337 ` : ''}
Philip Zeyligere08c7ff2025-06-06 13:22:12 -0700338 </div>
339 `;
340 }
341}