blob: 8016fbcf28c292026f044b70c8997a1fb430e90f [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";
Autoformatteraa22eb72025-07-23 19:59:36 +00004import { ToolCall, State } 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
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100202 render() {
203 const patchInput = JSON.parse(this.toolCall?.input);
banksean333aa672025-07-13 19:49:21 +0000204
205 const summaryContent = html`<span
206 class="text-gray-600 font-mono overflow-hidden text-ellipsis whitespace-nowrap rounded"
207 >
208 ${patchInput?.path}: ${patchInput.patches.length}
209 edit${patchInput.patches.length > 1 ? "s" : ""}
210 </span>`;
211
212 const inputContent = html`<div>
213 ${patchInput.patches.map((patch) => {
214 return html`<div class="mb-2">
215 Patch operation: <b>${patch.operation}</b>
216 ${createPreElement(patch.newText)}
217 </div>`;
218 })}
219 </div>`;
220
221 const resultContent = this.toolCall?.result_message?.tool_result
222 ? createPreElement(this.toolCall.result_message.tool_result)
223 : "";
224
225 return html`<sketch-tool-card-base
226 .open=${this.open}
227 .toolCall=${this.toolCall}
228 .summaryContent=${summaryContent}
229 .inputContent=${inputContent}
230 .resultContent=${resultContent}
231 ></sketch-tool-card-base>`;
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100232 }
233}
234
235@customElement("sketch-tool-card-think")
banksean333aa672025-07-13 19:49:21 +0000236export class SketchToolCardThink extends SketchTailwindElement {
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100237 @property() toolCall: ToolCall;
238 @property() open: boolean;
239
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100240 render() {
banksean333aa672025-07-13 19:49:21 +0000241 const thoughts = JSON.parse(this.toolCall?.input)?.thoughts || "";
242
243 const summaryContent = html`<span
244 class="overflow-hidden text-ellipsis font-mono"
245 >
246 ${thoughts.split("\n")[0]}
247 </span>`;
248
249 const inputContent = html`<div
250 class="overflow-x-auto mb-1 font-mono px-2 py-1 bg-gray-200 rounded select-text cursor-text text-sm leading-relaxed"
251 >
252 <div class="markdown-content">
253 ${unsafeHTML(renderMarkdown(thoughts))}
254 </div>
255 </div>`;
256
257 return html`<sketch-tool-card-base
258 .open=${this.open}
259 .toolCall=${this.toolCall}
260 .summaryContent=${summaryContent}
261 .inputContent=${inputContent}
262 ></sketch-tool-card-base>`;
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100263 }
264}
265
Josh Bleecher Snyder19969a92025-06-05 14:34:02 -0700266@customElement("sketch-tool-card-commit-message-style")
banksean333aa672025-07-13 19:49:21 +0000267export class SketchToolCardCommitMessageStyle extends SketchTailwindElement {
Josh Bleecher Snydera2a31502025-05-07 12:37:18 +0000268 @property()
269 toolCall: ToolCall;
270
271 @property()
272 open: boolean;
273
Philip Zeyligerbe7802a2025-06-04 20:15:25 +0000274 @property()
275 state: State;
276
Josh Bleecher Snydera2a31502025-05-07 12:37:18 +0000277 constructor() {
278 super();
279 }
280
281 connectedCallback() {
282 super.connectedCallback();
283 }
284
285 disconnectedCallback() {
286 super.disconnectedCallback();
287 }
288
289 render() {
banksean333aa672025-07-13 19:49:21 +0000290 return html`<sketch-tool-card-base
291 .open=${this.open}
292 .toolCall=${this.toolCall}
293 ></sketch-tool-card-base>`;
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100294 }
295}
296
Josh Bleecher Snyder112b9232025-05-23 11:26:33 -0700297@customElement("sketch-tool-card-todo-write")
banksean333aa672025-07-13 19:49:21 +0000298export class SketchToolCardTodoWrite extends SketchTailwindElement {
Josh Bleecher Snyder112b9232025-05-23 11:26:33 -0700299 @property() toolCall: ToolCall;
300 @property() open: boolean;
301
Josh Bleecher Snyder112b9232025-05-23 11:26:33 -0700302 render() {
303 const inputData = JSON.parse(this.toolCall?.input || "{}");
304 const tasks = inputData.tasks || [];
Josh Bleecher Snyder991164f2025-05-29 05:02:10 +0000305
Josh Bleecher Snyder112b9232025-05-23 11:26:33 -0700306 // Generate circles based on task status
Josh Bleecher Snyder991164f2025-05-29 05:02:10 +0000307 const circles = tasks
308 .map((task) => {
309 switch (task.status) {
310 case "completed":
311 return "●"; // full circle
312 case "in-progress":
313 return "◐"; // half circle
314 case "queued":
315 default:
316 return "○"; // empty circle
317 }
318 })
319 .join(" ");
320
banksean333aa672025-07-13 19:49:21 +0000321 const summaryContent = html`<span class="italic text-gray-600">
322 ${circles}
323 </span>`;
324 const resultContent = this.toolCall?.result_message?.tool_result
325 ? createPreElement(this.toolCall.result_message.tool_result)
326 : "";
327
328 return html`<sketch-tool-card-base
329 .open=${this.open}
330 .toolCall=${this.toolCall}
331 .summaryContent=${summaryContent}
332 .resultContent=${resultContent}
333 ></sketch-tool-card-base>`;
Josh Bleecher Snyder991164f2025-05-29 05:02:10 +0000334 }
335}
336
337@customElement("sketch-tool-card-keyword-search")
banksean333aa672025-07-13 19:49:21 +0000338export class SketchToolCardKeywordSearch extends SketchTailwindElement {
Josh Bleecher Snyder991164f2025-05-29 05:02:10 +0000339 @property() toolCall: ToolCall;
340 @property() open: boolean;
341
Josh Bleecher Snyder991164f2025-05-29 05:02:10 +0000342 render() {
343 const inputData = JSON.parse(this.toolCall?.input || "{}");
344 const query = inputData.query || "";
345 const searchTerms = inputData.search_terms || [];
346
banksean333aa672025-07-13 19:49:21 +0000347 const summaryContent = html`<div
348 class="flex flex-col gap-0.5 w-full max-w-full overflow-hidden"
349 >
350 <div
351 class="text-gray-800 text-xs normal-case whitespace-normal break-words leading-tight"
352 >
353 🔍 ${query}
Josh Bleecher Snyder991164f2025-05-29 05:02:10 +0000354 </div>
banksean333aa672025-07-13 19:49:21 +0000355 <div
356 class="text-gray-600 text-xs normal-case whitespace-normal break-words leading-tight mt-px"
357 >
358 🗝️ ${searchTerms.join(", ")}
Josh Bleecher Snyder991164f2025-05-29 05:02:10 +0000359 </div>
banksean333aa672025-07-13 19:49:21 +0000360 </div>`;
361
362 const inputContent = html`<div>
363 <div><strong>Query:</strong> ${query}</div>
364 <div><strong>Search terms:</strong> ${searchTerms.join(", ")}</div>
365 </div>`;
366
367 const resultContent = this.toolCall?.result_message?.tool_result
368 ? createPreElement(this.toolCall.result_message.tool_result)
369 : "";
370
371 return html`<sketch-tool-card-base
372 .open=${this.open}
373 .toolCall=${this.toolCall}
374 .summaryContent=${summaryContent}
375 .inputContent=${inputContent}
376 .resultContent=${resultContent}
377 ></sketch-tool-card-base>`;
Josh Bleecher Snyder112b9232025-05-23 11:26:33 -0700378 }
379}
380
381@customElement("sketch-tool-card-todo-read")
banksean333aa672025-07-13 19:49:21 +0000382export class SketchToolCardTodoRead extends SketchTailwindElement {
Josh Bleecher Snyder112b9232025-05-23 11:26:33 -0700383 @property() toolCall: ToolCall;
384 @property() open: boolean;
385
Josh Bleecher Snyder112b9232025-05-23 11:26:33 -0700386 render() {
banksean333aa672025-07-13 19:49:21 +0000387 const summaryContent = html`<span class="italic text-gray-600">
388 Read todo list
389 </span>`;
390 const resultContent = this.toolCall?.result_message?.tool_result
391 ? createPreElement(this.toolCall.result_message.tool_result)
392 : "";
393
394 return html`<sketch-tool-card-base
395 .open=${this.open}
396 .toolCall=${this.toolCall}
397 .summaryContent=${summaryContent}
398 .resultContent=${resultContent}
399 ></sketch-tool-card-base>`;
Josh Bleecher Snyder112b9232025-05-23 11:26:33 -0700400 }
401}
402
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100403@customElement("sketch-tool-card-generic")
banksean333aa672025-07-13 19:49:21 +0000404export class SketchToolCardGeneric extends SketchTailwindElement {
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100405 @property() toolCall: ToolCall;
406 @property() open: boolean;
407
408 render() {
banksean333aa672025-07-13 19:49:21 +0000409 const summaryContent = html`<span
410 class="block whitespace-normal break-words max-w-full w-full"
411 >
412 ${this.toolCall?.input}
413 </span>`;
414
415 const inputContent = html`<div class="max-w-full break-words">
416 Input:
417 ${createPreElement(
418 this.toolCall?.input || "",
419 "max-w-full whitespace-pre-wrap break-words",
420 )}
421 </div>`;
422
423 const resultContent = this.toolCall?.result_message?.tool_result
424 ? html`<div class="max-w-full break-words">
425 Result: ${createPreElement(this.toolCall.result_message.tool_result)}
426 </div>`
427 : "";
428
429 return html`<sketch-tool-card-base
430 .open=${this.open}
431 .toolCall=${this.toolCall}
432 .summaryContent=${summaryContent}
433 .inputContent=${inputContent}
434 .resultContent=${resultContent}
435 ></sketch-tool-card-base>`;
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100436 }
437}
438
Sean McCulloughec3ad1a2025-04-18 13:55:16 -0700439declare global {
440 interface HTMLElementTagNameMap {
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100441 "sketch-tool-card-generic": SketchToolCardGeneric;
442 "sketch-tool-card-bash": SketchToolCardBash;
443 "sketch-tool-card-codereview": SketchToolCardCodeReview;
444 "sketch-tool-card-done": SketchToolCardDone;
445 "sketch-tool-card-patch": SketchToolCardPatch;
446 "sketch-tool-card-think": SketchToolCardThink;
Josh Bleecher Snyder19969a92025-06-05 14:34:02 -0700447 "sketch-tool-card-commit-message-style": SketchToolCardCommitMessageStyle;
Josh Bleecher Snyder112b9232025-05-23 11:26:33 -0700448 "sketch-tool-card-todo-write": SketchToolCardTodoWrite;
449 "sketch-tool-card-todo-read": SketchToolCardTodoRead;
Josh Bleecher Snyder991164f2025-05-29 05:02:10 +0000450 "sketch-tool-card-keyword-search": SketchToolCardKeywordSearch;
Sean McCulloughec3ad1a2025-04-18 13:55:16 -0700451 }
452}