blob: c96d8e04ddf4340a41b7120be1c9a9db17322b1a [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
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 Snyder112b9232025-05-23 11:26:33 -0700304@customElement("sketch-tool-card-todo-write")
banksean333aa672025-07-13 19:49:21 +0000305export class SketchToolCardTodoWrite extends SketchTailwindElement {
Josh Bleecher Snyder112b9232025-05-23 11:26:33 -0700306 @property() toolCall: ToolCall;
307 @property() open: boolean;
308
Josh Bleecher Snyder112b9232025-05-23 11:26:33 -0700309 render() {
310 const inputData = JSON.parse(this.toolCall?.input || "{}");
311 const tasks = inputData.tasks || [];
Josh Bleecher Snyder991164f2025-05-29 05:02:10 +0000312
Josh Bleecher Snyder112b9232025-05-23 11:26:33 -0700313 // Generate circles based on task status
Josh Bleecher Snyder991164f2025-05-29 05:02:10 +0000314 const circles = tasks
315 .map((task) => {
316 switch (task.status) {
317 case "completed":
318 return "●"; // full circle
319 case "in-progress":
320 return "◐"; // half circle
321 case "queued":
322 default:
323 return "○"; // empty circle
324 }
325 })
326 .join(" ");
327
banksean333aa672025-07-13 19:49:21 +0000328 const summaryContent = html`<span class="italic text-gray-600">
329 ${circles}
330 </span>`;
331 const resultContent = this.toolCall?.result_message?.tool_result
332 ? createPreElement(this.toolCall.result_message.tool_result)
333 : "";
334
335 return html`<sketch-tool-card-base
336 .open=${this.open}
337 .toolCall=${this.toolCall}
338 .summaryContent=${summaryContent}
339 .resultContent=${resultContent}
340 ></sketch-tool-card-base>`;
Josh Bleecher Snyder991164f2025-05-29 05:02:10 +0000341 }
342}
343
344@customElement("sketch-tool-card-keyword-search")
banksean333aa672025-07-13 19:49:21 +0000345export class SketchToolCardKeywordSearch extends SketchTailwindElement {
Josh Bleecher Snyder991164f2025-05-29 05:02:10 +0000346 @property() toolCall: ToolCall;
347 @property() open: boolean;
348
Josh Bleecher Snyder991164f2025-05-29 05:02:10 +0000349 render() {
350 const inputData = JSON.parse(this.toolCall?.input || "{}");
351 const query = inputData.query || "";
352 const searchTerms = inputData.search_terms || [];
353
banksean333aa672025-07-13 19:49:21 +0000354 const summaryContent = html`<div
355 class="flex flex-col gap-0.5 w-full max-w-full overflow-hidden"
356 >
357 <div
358 class="text-gray-800 text-xs normal-case whitespace-normal break-words leading-tight"
359 >
360 🔍 ${query}
Josh Bleecher Snyder991164f2025-05-29 05:02:10 +0000361 </div>
banksean333aa672025-07-13 19:49:21 +0000362 <div
363 class="text-gray-600 text-xs normal-case whitespace-normal break-words leading-tight mt-px"
364 >
365 🗝️ ${searchTerms.join(", ")}
Josh Bleecher Snyder991164f2025-05-29 05:02:10 +0000366 </div>
banksean333aa672025-07-13 19:49:21 +0000367 </div>`;
368
369 const inputContent = html`<div>
370 <div><strong>Query:</strong> ${query}</div>
371 <div><strong>Search terms:</strong> ${searchTerms.join(", ")}</div>
372 </div>`;
373
374 const resultContent = this.toolCall?.result_message?.tool_result
375 ? createPreElement(this.toolCall.result_message.tool_result)
376 : "";
377
378 return html`<sketch-tool-card-base
379 .open=${this.open}
380 .toolCall=${this.toolCall}
381 .summaryContent=${summaryContent}
382 .inputContent=${inputContent}
383 .resultContent=${resultContent}
384 ></sketch-tool-card-base>`;
Josh Bleecher Snyder112b9232025-05-23 11:26:33 -0700385 }
386}
387
388@customElement("sketch-tool-card-todo-read")
banksean333aa672025-07-13 19:49:21 +0000389export class SketchToolCardTodoRead extends SketchTailwindElement {
Josh Bleecher Snyder112b9232025-05-23 11:26:33 -0700390 @property() toolCall: ToolCall;
391 @property() open: boolean;
392
Josh Bleecher Snyder112b9232025-05-23 11:26:33 -0700393 render() {
banksean333aa672025-07-13 19:49:21 +0000394 const summaryContent = html`<span class="italic text-gray-600">
395 Read todo list
396 </span>`;
397 const resultContent = this.toolCall?.result_message?.tool_result
398 ? createPreElement(this.toolCall.result_message.tool_result)
399 : "";
400
401 return html`<sketch-tool-card-base
402 .open=${this.open}
403 .toolCall=${this.toolCall}
404 .summaryContent=${summaryContent}
405 .resultContent=${resultContent}
406 ></sketch-tool-card-base>`;
Josh Bleecher Snyder112b9232025-05-23 11:26:33 -0700407 }
408}
409
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100410@customElement("sketch-tool-card-generic")
banksean333aa672025-07-13 19:49:21 +0000411export class SketchToolCardGeneric extends SketchTailwindElement {
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100412 @property() toolCall: ToolCall;
413 @property() open: boolean;
414
415 render() {
banksean333aa672025-07-13 19:49:21 +0000416 const summaryContent = html`<span
417 class="block whitespace-normal break-words max-w-full w-full"
418 >
419 ${this.toolCall?.input}
420 </span>`;
421
422 const inputContent = html`<div class="max-w-full break-words">
423 Input:
424 ${createPreElement(
425 this.toolCall?.input || "",
426 "max-w-full whitespace-pre-wrap break-words",
427 )}
428 </div>`;
429
430 const resultContent = this.toolCall?.result_message?.tool_result
431 ? html`<div class="max-w-full break-words">
432 Result: ${createPreElement(this.toolCall.result_message.tool_result)}
433 </div>`
434 : "";
435
436 return html`<sketch-tool-card-base
437 .open=${this.open}
438 .toolCall=${this.toolCall}
439 .summaryContent=${summaryContent}
440 .inputContent=${inputContent}
441 .resultContent=${resultContent}
442 ></sketch-tool-card-base>`;
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100443 }
444}
445
Sean McCulloughec3ad1a2025-04-18 13:55:16 -0700446declare global {
447 interface HTMLElementTagNameMap {
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100448 "sketch-tool-card-generic": SketchToolCardGeneric;
449 "sketch-tool-card-bash": SketchToolCardBash;
450 "sketch-tool-card-codereview": SketchToolCardCodeReview;
451 "sketch-tool-card-done": SketchToolCardDone;
452 "sketch-tool-card-patch": SketchToolCardPatch;
453 "sketch-tool-card-think": SketchToolCardThink;
Josh Bleecher Snyder112b9232025-05-23 11:26:33 -0700454 "sketch-tool-card-todo-write": SketchToolCardTodoWrite;
455 "sketch-tool-card-todo-read": SketchToolCardTodoRead;
Josh Bleecher Snyder991164f2025-05-29 05:02:10 +0000456 "sketch-tool-card-keyword-search": SketchToolCardKeywordSearch;
Sean McCulloughec3ad1a2025-04-18 13:55:16 -0700457 }
458}