blob: 881285a705b1518a5cab2cdde857b9202f2376fe [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 Snyderbeaa86a2025-07-23 03:37:21 +00004import { 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
banksean3d1308e2025-07-29 17:20:10 +000066 class="bg-gray-200 dark:bg-gray-700 text-black dark:text-gray-100 p-2 rounded whitespace-pre-wrap break-words max-w-full w-full box-border overflow-wrap-break-word ${additionalClasses}"
banksean333aa672025-07-13 19:49:21 +000067 >
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
banksean3d1308e2025-07-29 17:20:10 +0000104 class="bg-gray-200 dark:bg-gray-700 text-black dark:text-gray-100 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"
Josh Bleecher Snyder45943882025-07-18 02:13:31 +0000105 >
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("+")) {
banksean3d1308e2025-07-29 17:20:10 +0000214 return html`<div
215 class="text-green-600 dark:text-green-400 bg-green-50 dark:bg-green-900/20"
216 >
217 ${line}
218 </div>`;
Josh Bleecher Snyder3dd3e412025-07-22 20:32:03 -0700219 } else if (line.startsWith("-")) {
banksean3d1308e2025-07-29 17:20:10 +0000220 return html`<div
221 class="text-red-600 dark:text-red-400 bg-red-50 dark:bg-red-900/20"
222 >
223 ${line}
224 </div>`;
Josh Bleecher Snyder3dd3e412025-07-22 20:32:03 -0700225 } else if (line.startsWith("@@")) {
226 // prettier-ignore
banksean3d1308e2025-07-29 17:20:10 +0000227 return html`<div class="text-cyan-600 dark:text-cyan-400 bg-cyan-50 dark:bg-cyan-900/20 font-semibold">${line}</div>`;
Josh Bleecher Snyder3dd3e412025-07-22 20:32:03 -0700228 } else {
banksean3d1308e2025-07-29 17:20:10 +0000229 return html`<div class="text-gray-800 dark:text-gray-200">
230 ${line}
231 </div>`;
Josh Bleecher Snyder3dd3e412025-07-22 20:32:03 -0700232 }
233 });
234
235 return html`<pre
banksean3d1308e2025-07-29 17:20:10 +0000236 class="bg-gray-100 dark:bg-gray-800 text-xs p-2 rounded whitespace-pre-wrap break-words max-w-full w-full box-border overflow-x-auto font-mono text-gray-900 dark:text-gray-100"
Josh Bleecher Snyder3dd3e412025-07-22 20:32:03 -0700237 >
238 ${coloredLines}
239 </pre
240 >`;
241 }
242
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100243 render() {
244 const patchInput = JSON.parse(this.toolCall?.input);
banksean333aa672025-07-13 19:49:21 +0000245
246 const summaryContent = html`<span
247 class="text-gray-600 font-mono overflow-hidden text-ellipsis whitespace-nowrap rounded"
248 >
249 ${patchInput?.path}: ${patchInput.patches.length}
250 edit${patchInput.patches.length > 1 ? "s" : ""}
251 </span>`;
252
Josh Bleecher Snyder3dd3e412025-07-22 20:32:03 -0700253 const inputContent = html``;
banksean333aa672025-07-13 19:49:21 +0000254
Josh Bleecher Snyder3dd3e412025-07-22 20:32:03 -0700255 // Show diff if available, otherwise show the regular result
256 let resultContent;
257 if (
258 this.toolCall?.result_message?.display &&
259 typeof this.toolCall.result_message.display === "string"
260 ) {
261 // Render the diff with syntax highlighting
262 resultContent = html`<div class="w-full relative">
263 ${this.renderDiff(this.toolCall.result_message.display)}
264 </div>`;
265 } else if (this.toolCall?.result_message?.tool_result) {
266 resultContent = createPreElement(
267 this.toolCall.result_message.tool_result,
268 );
269 } else {
270 resultContent = "";
271 }
banksean333aa672025-07-13 19:49:21 +0000272
273 return html`<sketch-tool-card-base
274 .open=${this.open}
275 .toolCall=${this.toolCall}
276 .summaryContent=${summaryContent}
277 .inputContent=${inputContent}
278 .resultContent=${resultContent}
279 ></sketch-tool-card-base>`;
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100280 }
281}
282
283@customElement("sketch-tool-card-think")
banksean333aa672025-07-13 19:49:21 +0000284export class SketchToolCardThink extends SketchTailwindElement {
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100285 @property() toolCall: ToolCall;
286 @property() open: boolean;
287
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100288 render() {
banksean333aa672025-07-13 19:49:21 +0000289 const thoughts = JSON.parse(this.toolCall?.input)?.thoughts || "";
290
291 const summaryContent = html`<span
292 class="overflow-hidden text-ellipsis font-mono"
293 >
294 ${thoughts.split("\n")[0]}
295 </span>`;
296
297 const inputContent = html`<div
banksean3d1308e2025-07-29 17:20:10 +0000298 class="overflow-x-auto mb-1 font-mono px-2 py-1 bg-gray-200 dark:bg-gray-700 rounded select-text cursor-text text-sm leading-relaxed text-gray-900 dark:text-gray-100"
banksean333aa672025-07-13 19:49:21 +0000299 >
300 <div class="markdown-content">
301 ${unsafeHTML(renderMarkdown(thoughts))}
302 </div>
303 </div>`;
304
305 return html`<sketch-tool-card-base
306 .open=${this.open}
307 .toolCall=${this.toolCall}
308 .summaryContent=${summaryContent}
309 .inputContent=${inputContent}
310 ></sketch-tool-card-base>`;
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100311 }
312}
313
Josh Bleecher Snyder112b9232025-05-23 11:26:33 -0700314@customElement("sketch-tool-card-todo-write")
banksean333aa672025-07-13 19:49:21 +0000315export class SketchToolCardTodoWrite extends SketchTailwindElement {
Josh Bleecher Snyder112b9232025-05-23 11:26:33 -0700316 @property() toolCall: ToolCall;
317 @property() open: boolean;
318
Josh Bleecher Snyder112b9232025-05-23 11:26:33 -0700319 render() {
320 const inputData = JSON.parse(this.toolCall?.input || "{}");
321 const tasks = inputData.tasks || [];
Josh Bleecher Snyder991164f2025-05-29 05:02:10 +0000322
Josh Bleecher Snyder112b9232025-05-23 11:26:33 -0700323 // Generate circles based on task status
Josh Bleecher Snyder991164f2025-05-29 05:02:10 +0000324 const circles = tasks
325 .map((task) => {
326 switch (task.status) {
327 case "completed":
328 return "●"; // full circle
329 case "in-progress":
330 return "◐"; // half circle
331 case "queued":
332 default:
333 return "○"; // empty circle
334 }
335 })
336 .join(" ");
337
banksean333aa672025-07-13 19:49:21 +0000338 const summaryContent = html`<span class="italic text-gray-600">
339 ${circles}
340 </span>`;
341 const resultContent = this.toolCall?.result_message?.tool_result
342 ? createPreElement(this.toolCall.result_message.tool_result)
343 : "";
344
345 return html`<sketch-tool-card-base
346 .open=${this.open}
347 .toolCall=${this.toolCall}
348 .summaryContent=${summaryContent}
349 .resultContent=${resultContent}
350 ></sketch-tool-card-base>`;
Josh Bleecher Snyder991164f2025-05-29 05:02:10 +0000351 }
352}
353
354@customElement("sketch-tool-card-keyword-search")
banksean333aa672025-07-13 19:49:21 +0000355export class SketchToolCardKeywordSearch extends SketchTailwindElement {
Josh Bleecher Snyder991164f2025-05-29 05:02:10 +0000356 @property() toolCall: ToolCall;
357 @property() open: boolean;
358
Josh Bleecher Snyder991164f2025-05-29 05:02:10 +0000359 render() {
360 const inputData = JSON.parse(this.toolCall?.input || "{}");
361 const query = inputData.query || "";
362 const searchTerms = inputData.search_terms || [];
363
banksean333aa672025-07-13 19:49:21 +0000364 const summaryContent = html`<div
365 class="flex flex-col gap-0.5 w-full max-w-full overflow-hidden"
366 >
367 <div
368 class="text-gray-800 text-xs normal-case whitespace-normal break-words leading-tight"
369 >
370 🔍 ${query}
Josh Bleecher Snyder991164f2025-05-29 05:02:10 +0000371 </div>
banksean333aa672025-07-13 19:49:21 +0000372 <div
373 class="text-gray-600 text-xs normal-case whitespace-normal break-words leading-tight mt-px"
374 >
375 🗝️ ${searchTerms.join(", ")}
Josh Bleecher Snyder991164f2025-05-29 05:02:10 +0000376 </div>
banksean333aa672025-07-13 19:49:21 +0000377 </div>`;
378
379 const inputContent = html`<div>
380 <div><strong>Query:</strong> ${query}</div>
381 <div><strong>Search terms:</strong> ${searchTerms.join(", ")}</div>
382 </div>`;
383
384 const resultContent = this.toolCall?.result_message?.tool_result
385 ? createPreElement(this.toolCall.result_message.tool_result)
386 : "";
387
388 return html`<sketch-tool-card-base
389 .open=${this.open}
390 .toolCall=${this.toolCall}
391 .summaryContent=${summaryContent}
392 .inputContent=${inputContent}
393 .resultContent=${resultContent}
394 ></sketch-tool-card-base>`;
Josh Bleecher Snyder112b9232025-05-23 11:26:33 -0700395 }
396}
397
398@customElement("sketch-tool-card-todo-read")
banksean333aa672025-07-13 19:49:21 +0000399export class SketchToolCardTodoRead extends SketchTailwindElement {
Josh Bleecher Snyder112b9232025-05-23 11:26:33 -0700400 @property() toolCall: ToolCall;
401 @property() open: boolean;
402
Josh Bleecher Snyder112b9232025-05-23 11:26:33 -0700403 render() {
banksean333aa672025-07-13 19:49:21 +0000404 const summaryContent = html`<span class="italic text-gray-600">
405 Read todo list
406 </span>`;
407 const resultContent = this.toolCall?.result_message?.tool_result
408 ? createPreElement(this.toolCall.result_message.tool_result)
409 : "";
410
411 return html`<sketch-tool-card-base
412 .open=${this.open}
413 .toolCall=${this.toolCall}
414 .summaryContent=${summaryContent}
415 .resultContent=${resultContent}
416 ></sketch-tool-card-base>`;
Josh Bleecher Snyder112b9232025-05-23 11:26:33 -0700417 }
418}
419
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100420@customElement("sketch-tool-card-generic")
banksean333aa672025-07-13 19:49:21 +0000421export class SketchToolCardGeneric extends SketchTailwindElement {
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100422 @property() toolCall: ToolCall;
423 @property() open: boolean;
424
425 render() {
banksean333aa672025-07-13 19:49:21 +0000426 const summaryContent = html`<span
427 class="block whitespace-normal break-words max-w-full w-full"
428 >
429 ${this.toolCall?.input}
430 </span>`;
431
432 const inputContent = html`<div class="max-w-full break-words">
433 Input:
434 ${createPreElement(
435 this.toolCall?.input || "",
436 "max-w-full whitespace-pre-wrap break-words",
437 )}
438 </div>`;
439
440 const resultContent = this.toolCall?.result_message?.tool_result
441 ? html`<div class="max-w-full break-words">
442 Result: ${createPreElement(this.toolCall.result_message.tool_result)}
443 </div>`
444 : "";
445
446 return html`<sketch-tool-card-base
447 .open=${this.open}
448 .toolCall=${this.toolCall}
449 .summaryContent=${summaryContent}
450 .inputContent=${inputContent}
451 .resultContent=${resultContent}
452 ></sketch-tool-card-base>`;
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100453 }
454}
455
Sean McCulloughec3ad1a2025-04-18 13:55:16 -0700456declare global {
457 interface HTMLElementTagNameMap {
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100458 "sketch-tool-card-generic": SketchToolCardGeneric;
459 "sketch-tool-card-bash": SketchToolCardBash;
460 "sketch-tool-card-codereview": SketchToolCardCodeReview;
461 "sketch-tool-card-done": SketchToolCardDone;
462 "sketch-tool-card-patch": SketchToolCardPatch;
463 "sketch-tool-card-think": SketchToolCardThink;
Josh Bleecher Snyder112b9232025-05-23 11:26:33 -0700464 "sketch-tool-card-todo-write": SketchToolCardTodoWrite;
465 "sketch-tool-card-todo-read": SketchToolCardTodoRead;
Josh Bleecher Snyder991164f2025-05-29 05:02:10 +0000466 "sketch-tool-card-keyword-search": SketchToolCardKeywordSearch;
Sean McCulloughec3ad1a2025-04-18 13:55:16 -0700467 }
468}