blob: 9c1ae62c33184c294c73ccf38a42a435a2dcdba9 [file] [log] [blame]
banksean333aa672025-07-13 19:49:21 +00001import { html } from "lit";
Pokey Rule7ac5ed02025-05-07 15:26:10 +01002import { unsafeHTML } from "lit/directives/unsafe-html.js";
banksean333aa672025-07-13 19:49:21 +00003import { customElement, property } from "lit/decorators.js";
Josh Bleecher Snyder3dd3e412025-07-22 20:32:03 -07004import { State, ToolCall } from "../types";
philip.zeyliger26bc6592025-06-30 20:15:30 -07005import { marked } from "marked";
Philip Zeyliger53ab2452025-06-04 17:49:33 +00006import DOMPurify from "dompurify";
banksean333aa672025-07-13 19:49:21 +00007import { SketchTailwindElement } from "./sketch-tailwind-element";
8import "./sketch-tool-card-base";
Pokey Rule7ac5ed02025-05-07 15:26:10 +01009
Philip Zeyliger53ab2452025-06-04 17:49:33 +000010// Shared utility function for markdown rendering with DOMPurify sanitization
Pokey Rule7ac5ed02025-05-07 15:26:10 +010011function renderMarkdown(markdownContent: string): string {
12 try {
Philip Zeyliger53ab2452025-06-04 17:49:33 +000013 // Parse markdown with default settings
14 const htmlOutput = marked.parse(markdownContent, {
Pokey Rule7ac5ed02025-05-07 15:26:10 +010015 gfm: true,
16 breaks: true,
17 async: false,
18 }) as string;
Philip Zeyliger53ab2452025-06-04 17:49:33 +000019
20 // Sanitize the output HTML with DOMPurify
21 return DOMPurify.sanitize(htmlOutput, {
22 // Allow common safe HTML elements
23 ALLOWED_TAGS: [
24 "p",
25 "br",
26 "strong",
27 "em",
28 "b",
29 "i",
30 "u",
31 "s",
32 "code",
33 "pre",
34 "h1",
35 "h2",
36 "h3",
37 "h4",
38 "h5",
39 "h6",
40 "ul",
41 "ol",
42 "li",
43 "blockquote",
44 "a",
45 ],
46 ALLOWED_ATTR: [
47 "href",
48 "title",
49 "target",
50 "rel", // For links
51 "class", // For basic styling
52 ],
53 // Keep content formatting
54 KEEP_CONTENT: true,
55 });
Pokey Rule7ac5ed02025-05-07 15:26:10 +010056 } catch (error) {
57 console.error("Error rendering markdown:", error);
Philip Zeyliger53ab2452025-06-04 17:49:33 +000058 // Fallback to sanitized plain text if markdown parsing fails
59 return DOMPurify.sanitize(markdownContent);
Pokey Rule7ac5ed02025-05-07 15:26:10 +010060 }
61}
62
banksean333aa672025-07-13 19:49:21 +000063// Shared utility function for creating Tailwind pre elements
64function createPreElement(content: string, additionalClasses: string = "") {
65 return html`<pre
66 class="bg-gray-200 text-black p-2 rounded whitespace-pre-wrap break-words max-w-full w-full box-border overflow-wrap-break-word ${additionalClasses}"
67 >
68${content}</pre
69 >`;
Sean McCulloughec3ad1a2025-04-18 13:55:16 -070070}
71
Pokey Rule7ac5ed02025-05-07 15:26:10 +010072@customElement("sketch-tool-card-bash")
banksean333aa672025-07-13 19:49:21 +000073export class SketchToolCardBash extends SketchTailwindElement {
Pokey Rule7ac5ed02025-05-07 15:26:10 +010074 @property() toolCall: ToolCall;
75 @property() open: boolean;
76
Pokey Rule7ac5ed02025-05-07 15:26:10 +010077 render() {
78 const inputData = JSON.parse(this.toolCall?.input || "{}");
79 const isBackground = inputData?.background === true;
Josh Bleecher Snyder17b2fd92025-07-09 22:47:13 +000080 const isSlowOk = inputData?.slow_ok === true;
Autoformatter9d9c8122025-07-18 03:37:23 +000081 const backgroundIcon = isBackground
82 ? html`<span title="Running in background">🥷</span> `
83 : "";
84 const slowIcon = isSlowOk
85 ? html`<span title="Extended timeouts">🐢</span> `
86 : "";
Pokey Rule7ac5ed02025-05-07 15:26:10 +010087
Philip Zeyligere31d2a92025-05-11 15:22:35 -070088 // Truncate the command if it's too long to display nicely
89 const command = inputData?.command || "";
90 const displayCommand =
91 command.length > 80 ? command.substring(0, 80) + "..." : command;
92
banksean333aa672025-07-13 19:49:21 +000093 const summaryContent = html`<div
94 class="max-w-full overflow-hidden text-ellipsis whitespace-nowrap"
95 >
96 ${backgroundIcon}${slowIcon}${displayCommand}
97 </div>`;
98
99 const inputContent = html`<div
100 class="flex w-full max-w-full flex-col overflow-wrap-break-word break-words"
101 >
102 <div class="w-full relative">
Josh Bleecher Snyder45943882025-07-18 02:13:31 +0000103 <pre
104 class="bg-gray-200 text-black p-2 rounded whitespace-pre-wrap break-words max-w-full w-full box-border overflow-wrap-break-word w-full mb-0 rounded-t rounded-b-none box-border"
105 >
Autoformatter9d9c8122025-07-18 03:37:23 +0000106${backgroundIcon}${slowIcon}${inputData?.command}</pre
107 >
banksean333aa672025-07-13 19:49:21 +0000108 </div>
109 </div>`;
110
111 const resultContent = this.toolCall?.result_message?.tool_result
112 ? html`<div class="w-full relative">
113 ${createPreElement(
114 this.toolCall.result_message.tool_result,
115 "mt-0 text-gray-600 rounded-t-none rounded-b w-full box-border max-h-[300px] overflow-y-auto",
116 )}
117 </div>`
118 : "";
119
120 return html`<sketch-tool-card-base
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100121 .open=${this.open}
122 .toolCall=${this.toolCall}
banksean333aa672025-07-13 19:49:21 +0000123 .summaryContent=${summaryContent}
124 .inputContent=${inputContent}
125 .resultContent=${resultContent}
126 ></sketch-tool-card-base>`;
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100127 }
128}
129
130@customElement("sketch-tool-card-codereview")
banksean333aa672025-07-13 19:49:21 +0000131export class SketchToolCardCodeReview extends SketchTailwindElement {
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100132 @property() toolCall: ToolCall;
133 @property() open: boolean;
134
135 // Determine the status icon based on the content of the result message
136 getStatusIcon(resultText: string): string {
137 if (!resultText) return "";
138 if (resultText === "OK") return "✔️";
139 if (resultText.includes("# Errors")) return "⚠️";
140 if (resultText.includes("# Info")) return "ℹ️";
141 if (resultText.includes("uncommitted changes in repo")) return "🧹";
142 if (resultText.includes("no new commits have been added")) return "🐣";
143 if (resultText.includes("git repo is not clean")) return "🧼";
144 return "❓";
145 }
146
147 render() {
148 const resultText = this.toolCall?.result_message?.tool_result || "";
149 const statusIcon = this.getStatusIcon(resultText);
150
banksean333aa672025-07-13 19:49:21 +0000151 const summaryContent = html`<span>${statusIcon}</span>`;
152 const resultContent = resultText ? createPreElement(resultText) : "";
153
154 return html`<sketch-tool-card-base
155 .open=${this.open}
156 .toolCall=${this.toolCall}
157 .summaryContent=${summaryContent}
158 .resultContent=${resultContent}
159 ></sketch-tool-card-base>`;
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100160 }
161}
162
163@customElement("sketch-tool-card-done")
banksean333aa672025-07-13 19:49:21 +0000164export class SketchToolCardDone extends SketchTailwindElement {
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100165 @property() toolCall: ToolCall;
166 @property() open: boolean;
167
168 render() {
169 const doneInput = JSON.parse(this.toolCall.input);
banksean333aa672025-07-13 19:49:21 +0000170
171 const summaryContent = html`<span></span>`;
172
173 const resultContent = html`<div>
174 ${Object.keys(doneInput.checklist_items).map((key) => {
175 const item = doneInput.checklist_items[key];
176 let statusIcon = "〰️";
177 if (item.status == "yes") {
178 statusIcon = "✅";
179 } else if (item.status == "not applicable") {
180 statusIcon = "🤷";
181 }
182 return html`<div class="mb-1">
183 <span>${statusIcon}</span> ${key}:${item.status}
184 </div>`;
185 })}
186 </div>`;
187
188 return html`<sketch-tool-card-base
189 .open=${this.open}
190 .toolCall=${this.toolCall}
191 .summaryContent=${summaryContent}
192 .resultContent=${resultContent}
193 ></sketch-tool-card-base>`;
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100194 }
195}
196
197@customElement("sketch-tool-card-patch")
banksean333aa672025-07-13 19:49:21 +0000198export class SketchToolCardPatch extends SketchTailwindElement {
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100199 @property() toolCall: ToolCall;
200 @property() open: boolean;
201
Josh Bleecher Snyder3dd3e412025-07-22 20:32:03 -0700202 // Render a diff with syntax highlighting
203 renderDiff(diff: string) {
204 // Remove ---/+++ header lines and trim leading/trailing blank lines
205 const lines = diff
206 .split("\n")
207 .filter((line) => !line.startsWith("---") && !line.startsWith("+++"))
208 .join("\n")
209 .trim()
210 .split("\n");
211
212 const coloredLines = lines.map((line) => {
213 if (line.startsWith("+")) {
214 return html`<div class="text-green-600 bg-green-50">${line}</div>`;
215 } else if (line.startsWith("-")) {
216 return html`<div class="text-red-600 bg-red-50">${line}</div>`;
217 } else if (line.startsWith("@@")) {
218 // prettier-ignore
219 return html`<div class="text-cyan-600 bg-cyan-50 font-semibold">${line}</div>`;
220 } else {
221 return html`<div class="text-gray-800">${line}</div>`;
222 }
223 });
224
225 return html`<pre
226 class="bg-gray-100 text-xs p-2 rounded whitespace-pre-wrap break-words max-w-full w-full box-border overflow-x-auto font-mono"
227 >
228 ${coloredLines}
229 </pre
230 >`;
231 }
232
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100233 render() {
234 const patchInput = JSON.parse(this.toolCall?.input);
banksean333aa672025-07-13 19:49:21 +0000235
236 const summaryContent = html`<span
237 class="text-gray-600 font-mono overflow-hidden text-ellipsis whitespace-nowrap rounded"
238 >
239 ${patchInput?.path}: ${patchInput.patches.length}
240 edit${patchInput.patches.length > 1 ? "s" : ""}
241 </span>`;
242
Josh Bleecher Snyder3dd3e412025-07-22 20:32:03 -0700243 const inputContent = html``;
banksean333aa672025-07-13 19:49:21 +0000244
Josh Bleecher Snyder3dd3e412025-07-22 20:32:03 -0700245 // Show diff if available, otherwise show the regular result
246 let resultContent;
247 if (
248 this.toolCall?.result_message?.display &&
249 typeof this.toolCall.result_message.display === "string"
250 ) {
251 // Render the diff with syntax highlighting
252 resultContent = html`<div class="w-full relative">
253 ${this.renderDiff(this.toolCall.result_message.display)}
254 </div>`;
255 } else if (this.toolCall?.result_message?.tool_result) {
256 resultContent = createPreElement(
257 this.toolCall.result_message.tool_result,
258 );
259 } else {
260 resultContent = "";
261 }
banksean333aa672025-07-13 19:49:21 +0000262
263 return html`<sketch-tool-card-base
264 .open=${this.open}
265 .toolCall=${this.toolCall}
266 .summaryContent=${summaryContent}
267 .inputContent=${inputContent}
268 .resultContent=${resultContent}
269 ></sketch-tool-card-base>`;
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100270 }
271}
272
273@customElement("sketch-tool-card-think")
banksean333aa672025-07-13 19:49:21 +0000274export class SketchToolCardThink extends SketchTailwindElement {
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100275 @property() toolCall: ToolCall;
276 @property() open: boolean;
277
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100278 render() {
banksean333aa672025-07-13 19:49:21 +0000279 const thoughts = JSON.parse(this.toolCall?.input)?.thoughts || "";
280
281 const summaryContent = html`<span
282 class="overflow-hidden text-ellipsis font-mono"
283 >
284 ${thoughts.split("\n")[0]}
285 </span>`;
286
287 const inputContent = html`<div
288 class="overflow-x-auto mb-1 font-mono px-2 py-1 bg-gray-200 rounded select-text cursor-text text-sm leading-relaxed"
289 >
290 <div class="markdown-content">
291 ${unsafeHTML(renderMarkdown(thoughts))}
292 </div>
293 </div>`;
294
295 return html`<sketch-tool-card-base
296 .open=${this.open}
297 .toolCall=${this.toolCall}
298 .summaryContent=${summaryContent}
299 .inputContent=${inputContent}
300 ></sketch-tool-card-base>`;
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100301 }
302}
303
Josh Bleecher Snyder19969a92025-06-05 14:34:02 -0700304@customElement("sketch-tool-card-commit-message-style")
banksean333aa672025-07-13 19:49:21 +0000305export class SketchToolCardCommitMessageStyle extends SketchTailwindElement {
Josh Bleecher Snydera2a31502025-05-07 12:37:18 +0000306 @property()
307 toolCall: ToolCall;
308
309 @property()
310 open: boolean;
311
Philip Zeyligerbe7802a2025-06-04 20:15:25 +0000312 @property()
313 state: State;
314
Josh Bleecher Snydera2a31502025-05-07 12:37:18 +0000315 constructor() {
316 super();
317 }
318
319 connectedCallback() {
320 super.connectedCallback();
321 }
322
323 disconnectedCallback() {
324 super.disconnectedCallback();
325 }
326
327 render() {
banksean333aa672025-07-13 19:49:21 +0000328 return html`<sketch-tool-card-base
329 .open=${this.open}
330 .toolCall=${this.toolCall}
331 ></sketch-tool-card-base>`;
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100332 }
333}
334
Josh Bleecher Snyder112b9232025-05-23 11:26:33 -0700335@customElement("sketch-tool-card-todo-write")
banksean333aa672025-07-13 19:49:21 +0000336export class SketchToolCardTodoWrite extends SketchTailwindElement {
Josh Bleecher Snyder112b9232025-05-23 11:26:33 -0700337 @property() toolCall: ToolCall;
338 @property() open: boolean;
339
Josh Bleecher Snyder112b9232025-05-23 11:26:33 -0700340 render() {
341 const inputData = JSON.parse(this.toolCall?.input || "{}");
342 const tasks = inputData.tasks || [];
Josh Bleecher Snyder991164f2025-05-29 05:02:10 +0000343
Josh Bleecher Snyder112b9232025-05-23 11:26:33 -0700344 // Generate circles based on task status
Josh Bleecher Snyder991164f2025-05-29 05:02:10 +0000345 const circles = tasks
346 .map((task) => {
347 switch (task.status) {
348 case "completed":
349 return "●"; // full circle
350 case "in-progress":
351 return "◐"; // half circle
352 case "queued":
353 default:
354 return "○"; // empty circle
355 }
356 })
357 .join(" ");
358
banksean333aa672025-07-13 19:49:21 +0000359 const summaryContent = html`<span class="italic text-gray-600">
360 ${circles}
361 </span>`;
362 const resultContent = this.toolCall?.result_message?.tool_result
363 ? createPreElement(this.toolCall.result_message.tool_result)
364 : "";
365
366 return html`<sketch-tool-card-base
367 .open=${this.open}
368 .toolCall=${this.toolCall}
369 .summaryContent=${summaryContent}
370 .resultContent=${resultContent}
371 ></sketch-tool-card-base>`;
Josh Bleecher Snyder991164f2025-05-29 05:02:10 +0000372 }
373}
374
375@customElement("sketch-tool-card-keyword-search")
banksean333aa672025-07-13 19:49:21 +0000376export class SketchToolCardKeywordSearch extends SketchTailwindElement {
Josh Bleecher Snyder991164f2025-05-29 05:02:10 +0000377 @property() toolCall: ToolCall;
378 @property() open: boolean;
379
Josh Bleecher Snyder991164f2025-05-29 05:02:10 +0000380 render() {
381 const inputData = JSON.parse(this.toolCall?.input || "{}");
382 const query = inputData.query || "";
383 const searchTerms = inputData.search_terms || [];
384
banksean333aa672025-07-13 19:49:21 +0000385 const summaryContent = html`<div
386 class="flex flex-col gap-0.5 w-full max-w-full overflow-hidden"
387 >
388 <div
389 class="text-gray-800 text-xs normal-case whitespace-normal break-words leading-tight"
390 >
391 🔍 ${query}
Josh Bleecher Snyder991164f2025-05-29 05:02:10 +0000392 </div>
banksean333aa672025-07-13 19:49:21 +0000393 <div
394 class="text-gray-600 text-xs normal-case whitespace-normal break-words leading-tight mt-px"
395 >
396 🗝️ ${searchTerms.join(", ")}
Josh Bleecher Snyder991164f2025-05-29 05:02:10 +0000397 </div>
banksean333aa672025-07-13 19:49:21 +0000398 </div>`;
399
400 const inputContent = html`<div>
401 <div><strong>Query:</strong> ${query}</div>
402 <div><strong>Search terms:</strong> ${searchTerms.join(", ")}</div>
403 </div>`;
404
405 const resultContent = this.toolCall?.result_message?.tool_result
406 ? createPreElement(this.toolCall.result_message.tool_result)
407 : "";
408
409 return html`<sketch-tool-card-base
410 .open=${this.open}
411 .toolCall=${this.toolCall}
412 .summaryContent=${summaryContent}
413 .inputContent=${inputContent}
414 .resultContent=${resultContent}
415 ></sketch-tool-card-base>`;
Josh Bleecher Snyder112b9232025-05-23 11:26:33 -0700416 }
417}
418
419@customElement("sketch-tool-card-todo-read")
banksean333aa672025-07-13 19:49:21 +0000420export class SketchToolCardTodoRead extends SketchTailwindElement {
Josh Bleecher Snyder112b9232025-05-23 11:26:33 -0700421 @property() toolCall: ToolCall;
422 @property() open: boolean;
423
Josh Bleecher Snyder112b9232025-05-23 11:26:33 -0700424 render() {
banksean333aa672025-07-13 19:49:21 +0000425 const summaryContent = html`<span class="italic text-gray-600">
426 Read todo list
427 </span>`;
428 const resultContent = this.toolCall?.result_message?.tool_result
429 ? createPreElement(this.toolCall.result_message.tool_result)
430 : "";
431
432 return html`<sketch-tool-card-base
433 .open=${this.open}
434 .toolCall=${this.toolCall}
435 .summaryContent=${summaryContent}
436 .resultContent=${resultContent}
437 ></sketch-tool-card-base>`;
Josh Bleecher Snyder112b9232025-05-23 11:26:33 -0700438 }
439}
440
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100441@customElement("sketch-tool-card-generic")
banksean333aa672025-07-13 19:49:21 +0000442export class SketchToolCardGeneric extends SketchTailwindElement {
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100443 @property() toolCall: ToolCall;
444 @property() open: boolean;
445
446 render() {
banksean333aa672025-07-13 19:49:21 +0000447 const summaryContent = html`<span
448 class="block whitespace-normal break-words max-w-full w-full"
449 >
450 ${this.toolCall?.input}
451 </span>`;
452
453 const inputContent = html`<div class="max-w-full break-words">
454 Input:
455 ${createPreElement(
456 this.toolCall?.input || "",
457 "max-w-full whitespace-pre-wrap break-words",
458 )}
459 </div>`;
460
461 const resultContent = this.toolCall?.result_message?.tool_result
462 ? html`<div class="max-w-full break-words">
463 Result: ${createPreElement(this.toolCall.result_message.tool_result)}
464 </div>`
465 : "";
466
467 return html`<sketch-tool-card-base
468 .open=${this.open}
469 .toolCall=${this.toolCall}
470 .summaryContent=${summaryContent}
471 .inputContent=${inputContent}
472 .resultContent=${resultContent}
473 ></sketch-tool-card-base>`;
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100474 }
475}
476
Sean McCulloughec3ad1a2025-04-18 13:55:16 -0700477declare global {
478 interface HTMLElementTagNameMap {
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100479 "sketch-tool-card-generic": SketchToolCardGeneric;
480 "sketch-tool-card-bash": SketchToolCardBash;
481 "sketch-tool-card-codereview": SketchToolCardCodeReview;
482 "sketch-tool-card-done": SketchToolCardDone;
483 "sketch-tool-card-patch": SketchToolCardPatch;
484 "sketch-tool-card-think": SketchToolCardThink;
Josh Bleecher Snyder19969a92025-06-05 14:34:02 -0700485 "sketch-tool-card-commit-message-style": SketchToolCardCommitMessageStyle;
Josh Bleecher Snyder112b9232025-05-23 11:26:33 -0700486 "sketch-tool-card-todo-write": SketchToolCardTodoWrite;
487 "sketch-tool-card-todo-read": SketchToolCardTodoRead;
Josh Bleecher Snyder991164f2025-05-29 05:02:10 +0000488 "sketch-tool-card-keyword-search": SketchToolCardKeywordSearch;
Sean McCulloughec3ad1a2025-04-18 13:55:16 -0700489 }
490}