blob: 28edf3c7d86954d433c065449d573944760e8fdd [file] [log] [blame]
Sean McCulloughec3ad1a2025-04-18 13:55:16 -07001import { css, html, LitElement } from "lit";
Pokey Rule7ac5ed02025-05-07 15:26:10 +01002import { unsafeHTML } from "lit/directives/unsafe-html.js";
Philip Zeyliger16fa8b42025-05-02 04:28:16 +00003import { customElement, property, state } from "lit/decorators.js";
Autoformatter7e5fe3c2025-06-04 22:24:53 +00004import {
5 ToolCall,
6 MultipleChoiceOption,
7 MultipleChoiceParams,
8 State,
9} from "../types";
Philip Zeyliger53ab2452025-06-04 17:49:33 +000010import { marked, MarkedOptions, Renderer } from "marked";
11import DOMPurify from "dompurify";
Pokey Rule7ac5ed02025-05-07 15:26:10 +010012
Philip Zeyliger53ab2452025-06-04 17:49:33 +000013// Shared utility function for markdown rendering with DOMPurify sanitization
Pokey Rule7ac5ed02025-05-07 15:26:10 +010014function renderMarkdown(markdownContent: string): string {
15 try {
Philip Zeyliger53ab2452025-06-04 17:49:33 +000016 // Parse markdown with default settings
17 const htmlOutput = marked.parse(markdownContent, {
Pokey Rule7ac5ed02025-05-07 15:26:10 +010018 gfm: true,
19 breaks: true,
20 async: false,
21 }) as string;
Philip Zeyliger53ab2452025-06-04 17:49:33 +000022
23 // Sanitize the output HTML with DOMPurify
24 return DOMPurify.sanitize(htmlOutput, {
25 // Allow common safe HTML elements
26 ALLOWED_TAGS: [
27 "p",
28 "br",
29 "strong",
30 "em",
31 "b",
32 "i",
33 "u",
34 "s",
35 "code",
36 "pre",
37 "h1",
38 "h2",
39 "h3",
40 "h4",
41 "h5",
42 "h6",
43 "ul",
44 "ol",
45 "li",
46 "blockquote",
47 "a",
48 ],
49 ALLOWED_ATTR: [
50 "href",
51 "title",
52 "target",
53 "rel", // For links
54 "class", // For basic styling
55 ],
56 // Keep content formatting
57 KEEP_CONTENT: true,
58 });
Pokey Rule7ac5ed02025-05-07 15:26:10 +010059 } catch (error) {
60 console.error("Error rendering markdown:", error);
Philip Zeyliger53ab2452025-06-04 17:49:33 +000061 // Fallback to sanitized plain text if markdown parsing fails
62 return DOMPurify.sanitize(markdownContent);
Pokey Rule7ac5ed02025-05-07 15:26:10 +010063 }
64}
65
66// Common styles shared across all tool cards
67const commonStyles = css`
Philip Zeyligere31d2a92025-05-11 15:22:35 -070068 :host {
69 display: block;
70 max-width: 100%;
71 width: 100%;
72 box-sizing: border-box;
73 overflow: hidden;
74 }
Pokey Rule7ac5ed02025-05-07 15:26:10 +010075 pre {
76 background: rgb(236, 236, 236);
77 color: black;
78 padding: 0.5em;
79 border-radius: 4px;
80 white-space: pre-wrap;
81 word-break: break-word;
82 max-width: 100%;
83 width: 100%;
84 box-sizing: border-box;
85 overflow-wrap: break-word;
86 }
87 .summary-text {
Philip Zeyligere31d2a92025-05-11 15:22:35 -070088 overflow: hidden !important;
89 text-overflow: ellipsis !important;
90 white-space: nowrap !important;
91 max-width: 100% !important;
92 width: 100% !important;
Pokey Rule7ac5ed02025-05-07 15:26:10 +010093 font-family: monospace;
Philip Zeyligere31d2a92025-05-11 15:22:35 -070094 display: block;
Pokey Rule7ac5ed02025-05-07 15:26:10 +010095 }
96`;
Pokey Rule5e8aead2025-05-06 16:21:57 +010097
Sean McCulloughec3ad1a2025-04-18 13:55:16 -070098@customElement("sketch-tool-card")
99export class SketchToolCard extends LitElement {
Pokey Rule5e8aead2025-05-06 16:21:57 +0100100 @property() toolCall: ToolCall;
101 @property() open: boolean;
102 @state() detailsVisible: boolean = false;
Philip Zeyliger16fa8b42025-05-02 04:28:16 +0000103
Sean McCulloughec3ad1a2025-04-18 13:55:16 -0700104 static styles = css`
105 .tool-call {
106 display: flex;
Philip Zeyliger16fa8b42025-05-02 04:28:16 +0000107 flex-direction: column;
108 width: 100%;
109 }
Philip Zeyliger16fa8b42025-05-02 04:28:16 +0000110 .tool-row {
111 display: flex;
112 width: 100%;
113 box-sizing: border-box;
114 padding: 6px 8px 6px 12px;
Sean McCulloughec3ad1a2025-04-18 13:55:16 -0700115 align-items: center;
Pokey Rule5e8aead2025-05-06 16:21:57 +0100116 gap: 8px;
Philip Zeyliger16fa8b42025-05-02 04:28:16 +0000117 cursor: pointer;
118 border-radius: 4px;
119 position: relative;
Pokey Rule5e8aead2025-05-06 16:21:57 +0100120 overflow: hidden;
Philip Zeyligere31d2a92025-05-11 15:22:35 -0700121 flex-wrap: wrap;
Philip Zeyliger16fa8b42025-05-02 04:28:16 +0000122 }
Philip Zeyliger16fa8b42025-05-02 04:28:16 +0000123 .tool-row:hover {
124 background-color: rgba(0, 0, 0, 0.02);
125 }
Philip Zeyliger16fa8b42025-05-02 04:28:16 +0000126 .tool-name {
127 font-family: monospace;
128 font-weight: 500;
129 color: #444;
130 background-color: rgba(0, 0, 0, 0.05);
131 border-radius: 3px;
132 padding: 2px 6px;
133 flex-shrink: 0;
134 min-width: 45px;
Philip Zeyliger16fa8b42025-05-02 04:28:16 +0000135 font-size: 12px;
136 text-align: center;
Sean McCulloughec3ad1a2025-04-18 13:55:16 -0700137 white-space: nowrap;
138 }
Philip Zeyliger16fa8b42025-05-02 04:28:16 +0000139 .tool-success {
140 color: #5cb85c;
141 font-size: 14px;
142 }
Philip Zeyliger16fa8b42025-05-02 04:28:16 +0000143 .tool-error {
Josh Bleecher Snydere750ec92025-05-05 23:01:57 +0000144 color: #6c757d;
Philip Zeyliger16fa8b42025-05-02 04:28:16 +0000145 font-size: 14px;
146 }
Philip Zeyliger16fa8b42025-05-02 04:28:16 +0000147 .tool-pending {
148 color: #f0ad4e;
149 font-size: 14px;
150 }
Philip Zeyliger16fa8b42025-05-02 04:28:16 +0000151 .summary-text {
Philip Zeyligere31d2a92025-05-11 15:22:35 -0700152 white-space: normal;
153 overflow-wrap: break-word;
154 word-break: break-word;
Philip Zeyliger16fa8b42025-05-02 04:28:16 +0000155 flex-grow: 1;
156 flex-shrink: 1;
157 color: #444;
158 font-family: monospace;
159 font-size: 12px;
160 padding: 0 4px;
161 min-width: 50px;
Philip Zeyligere31d2a92025-05-11 15:22:35 -0700162 max-width: calc(100% - 150px);
Pokey Rule5e8aead2025-05-06 16:21:57 +0100163 display: inline-block;
Philip Zeyliger16fa8b42025-05-02 04:28:16 +0000164 }
Philip Zeyliger16fa8b42025-05-02 04:28:16 +0000165 .tool-status {
166 display: flex;
167 align-items: center;
168 gap: 12px;
169 margin-left: auto;
170 flex-shrink: 0;
Pokey Rule5e8aead2025-05-06 16:21:57 +0100171 min-width: 120px;
Philip Zeyliger16fa8b42025-05-02 04:28:16 +0000172 justify-content: flex-end;
173 padding-right: 8px;
174 }
Sean McCulloughec3ad1a2025-04-18 13:55:16 -0700175 .tool-call-status {
Philip Zeyliger16fa8b42025-05-02 04:28:16 +0000176 display: flex;
177 align-items: center;
178 justify-content: center;
Sean McCulloughec3ad1a2025-04-18 13:55:16 -0700179 }
Sean McCulloughec3ad1a2025-04-18 13:55:16 -0700180 .tool-call-status.spinner {
181 animation: spin 1s infinite linear;
Sean McCulloughec3ad1a2025-04-18 13:55:16 -0700182 }
Sean McCulloughec3ad1a2025-04-18 13:55:16 -0700183 @keyframes spin {
184 0% {
185 transform: rotate(0deg);
186 }
187 100% {
188 transform: rotate(360deg);
189 }
190 }
Philip Zeyliger16fa8b42025-05-02 04:28:16 +0000191 .elapsed {
192 font-size: 11px;
193 color: #777;
194 white-space: nowrap;
195 min-width: 40px;
196 text-align: right;
197 }
Philip Zeyliger16fa8b42025-05-02 04:28:16 +0000198 .tool-details {
199 padding: 8px;
200 background-color: rgba(0, 0, 0, 0.02);
201 margin-top: 1px;
202 border-top: 1px solid rgba(0, 0, 0, 0.05);
203 display: none;
204 font-family: monospace;
205 font-size: 12px;
206 color: #333;
207 border-radius: 0 0 4px 4px;
208 max-width: 100%;
209 width: 100%;
210 box-sizing: border-box;
Pokey Rule5e8aead2025-05-06 16:21:57 +0100211 overflow: hidden;
Philip Zeyliger16fa8b42025-05-02 04:28:16 +0000212 }
Philip Zeyliger16fa8b42025-05-02 04:28:16 +0000213 .tool-details.visible {
214 display: block;
215 }
Sean McCulloughec3ad1a2025-04-18 13:55:16 -0700216 .cancel-button {
Sean McCulloughec3ad1a2025-04-18 13:55:16 -0700217 cursor: pointer;
Philip Zeyliger16fa8b42025-05-02 04:28:16 +0000218 color: white;
219 background-color: #d9534f;
220 border: none;
221 border-radius: 3px;
222 font-size: 11px;
223 padding: 2px 6px;
224 white-space: nowrap;
225 min-width: 50px;
Sean McCulloughec3ad1a2025-04-18 13:55:16 -0700226 }
Sean McCulloughec3ad1a2025-04-18 13:55:16 -0700227 .cancel-button:hover {
Philip Zeyliger16fa8b42025-05-02 04:28:16 +0000228 background-color: #c9302c;
Sean McCulloughec3ad1a2025-04-18 13:55:16 -0700229 }
Philip Zeyliger16fa8b42025-05-02 04:28:16 +0000230 .cancel-button[disabled] {
231 background-color: #999;
232 cursor: not-allowed;
Sean McCulloughec3ad1a2025-04-18 13:55:16 -0700233 }
Sean McCulloughec3ad1a2025-04-18 13:55:16 -0700234 `;
235
Sean McCulloughec3ad1a2025-04-18 13:55:16 -0700236 _cancelToolCall = async (tool_call_id: string, button: HTMLButtonElement) => {
Sean McCulloughec3ad1a2025-04-18 13:55:16 -0700237 button.innerText = "Cancelling";
238 button.disabled = true;
239 try {
240 const response = await fetch("cancel", {
241 method: "POST",
Pokey Rule5e8aead2025-05-06 16:21:57 +0100242 headers: { "Content-Type": "application/json" },
Sean McCulloughec3ad1a2025-04-18 13:55:16 -0700243 body: JSON.stringify({
244 tool_call_id: tool_call_id,
245 reason: "user requested cancellation",
246 }),
247 });
248 if (response.ok) {
Sean McCulloughec3ad1a2025-04-18 13:55:16 -0700249 button.parentElement.removeChild(button);
250 } else {
251 button.innerText = "Cancel";
Sean McCulloughec3ad1a2025-04-18 13:55:16 -0700252 }
253 } catch (e) {
254 console.error("cancel", tool_call_id, e);
255 }
256 };
257
Philip Zeyliger16fa8b42025-05-02 04:28:16 +0000258 _toggleDetails(e: Event) {
259 e.stopPropagation();
260 this.detailsVisible = !this.detailsVisible;
261 }
Sean McCulloughec3ad1a2025-04-18 13:55:16 -0700262
Philip Zeyliger16fa8b42025-05-02 04:28:16 +0000263 render() {
Pokey Rule5e8aead2025-05-06 16:21:57 +0100264 // Status indicator based on result
265 let statusIcon = html`<span class="tool-call-status spinner tool-pending"
266 >⏳</span
267 >`;
268 if (this.toolCall?.result_message) {
269 statusIcon = this.toolCall?.result_message.tool_error
Josh Bleecher Snyderc3c20232025-05-07 05:46:04 -0700270 ? html`<span class="tool-call-status tool-error">〰️</span>`
Pokey Rule5e8aead2025-05-06 16:21:57 +0100271 : html`<span class="tool-call-status tool-success">✓</span>`;
Philip Zeyliger16fa8b42025-05-02 04:28:16 +0000272 }
273
274 // Cancel button for pending operations
Sean McCulloughec3ad1a2025-04-18 13:55:16 -0700275 const cancelButton = this.toolCall?.result_message
276 ? ""
277 : html`<button
278 class="cancel-button"
279 title="Cancel this operation"
280 @click=${(e: Event) => {
281 e.stopPropagation();
Pokey Rule5e8aead2025-05-06 16:21:57 +0100282 this._cancelToolCall(
283 this.toolCall?.tool_call_id,
284 e.target as HTMLButtonElement,
285 );
Sean McCulloughec3ad1a2025-04-18 13:55:16 -0700286 }}
287 >
288 Cancel
289 </button>`;
290
Philip Zeyliger16fa8b42025-05-02 04:28:16 +0000291 // Elapsed time display
292 const elapsed = this.toolCall?.result_message?.elapsed
Sean McCullough2deac842025-04-21 18:17:57 -0700293 ? html`<span class="elapsed"
Philip Zeyliger16fa8b42025-05-02 04:28:16 +0000294 >${(this.toolCall?.result_message?.elapsed / 1e9).toFixed(1)}s</span
Sean McCullough2deac842025-04-21 18:17:57 -0700295 >`
Pokey Rule5e8aead2025-05-06 16:21:57 +0100296 : html`<span class="elapsed"></span>`;
Sean McCullough2deac842025-04-21 18:17:57 -0700297
Philip Zeyliger16fa8b42025-05-02 04:28:16 +0000298 // Initialize details visibility based on open property
299 if (this.open && !this.detailsVisible) {
300 this.detailsVisible = true;
301 }
302
303 return html`<div class="tool-call">
304 <div class="tool-row" @click=${this._toggleDetails}>
305 <span class="tool-name">${this.toolCall?.name}</span>
306 <span class="summary-text"><slot name="summary"></slot></span>
307 <div class="tool-status">${statusIcon} ${elapsed} ${cancelButton}</div>
308 </div>
309 <div class="tool-details ${this.detailsVisible ? "visible" : ""}">
Sean McCulloughec3ad1a2025-04-18 13:55:16 -0700310 <slot name="input"></slot>
311 <slot name="result"></slot>
Philip Zeyliger16fa8b42025-05-02 04:28:16 +0000312 </div>
313 </div>`;
Sean McCulloughec3ad1a2025-04-18 13:55:16 -0700314 }
315}
316
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100317@customElement("sketch-tool-card-bash")
318export class SketchToolCardBash extends LitElement {
319 @property() toolCall: ToolCall;
320 @property() open: boolean;
321
322 static styles = [
323 commonStyles,
324 css`
Philip Zeyligere31d2a92025-05-11 15:22:35 -0700325 :host {
326 max-width: 100%;
327 display: block;
328 }
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100329 .input {
330 display: flex;
331 width: 100%;
Philip Zeyligere31d2a92025-05-11 15:22:35 -0700332 max-width: 100%;
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100333 flex-direction: column;
Philip Zeyligere31d2a92025-05-11 15:22:35 -0700334 overflow-wrap: break-word;
335 word-break: break-word;
336 }
337 .command-wrapper {
338 max-width: 100%;
339 overflow: hidden;
340 text-overflow: ellipsis;
341 white-space: nowrap;
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100342 }
343 .input pre {
344 width: 100%;
345 margin-bottom: 0;
346 border-radius: 4px 4px 0 0;
347 box-sizing: border-box;
348 }
349 .result pre {
350 margin-top: 0;
351 color: #555;
352 border-radius: 0 0 4px 4px;
353 width: 100%;
354 box-sizing: border-box;
355 }
356 .result pre.scrollable-on-hover {
357 max-height: 300px;
358 overflow-y: auto;
359 }
360 .tool-call-result-container {
361 width: 100%;
362 position: relative;
363 }
364 .background-badge {
365 display: inline-block;
366 background-color: #6200ea;
367 color: white;
368 font-size: 10px;
369 font-weight: bold;
370 padding: 2px 6px;
371 border-radius: 10px;
372 margin-left: 8px;
373 vertical-align: middle;
374 }
375 .command-wrapper {
376 display: inline-block;
377 max-width: 100%;
378 overflow: hidden;
379 text-overflow: ellipsis;
380 white-space: nowrap;
381 }
382 `,
383 ];
384
385 render() {
386 const inputData = JSON.parse(this.toolCall?.input || "{}");
387 const isBackground = inputData?.background === true;
388 const backgroundIcon = isBackground ? "🔄 " : "";
389
Philip Zeyligere31d2a92025-05-11 15:22:35 -0700390 // Truncate the command if it's too long to display nicely
391 const command = inputData?.command || "";
392 const displayCommand =
393 command.length > 80 ? command.substring(0, 80) + "..." : command;
394
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100395 return html` <sketch-tool-card
396 .open=${this.open}
397 .toolCall=${this.toolCall}
398 >
Philip Zeyligere31d2a92025-05-11 15:22:35 -0700399 <span
400 slot="summary"
401 class="summary-text"
402 style="display: block; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;"
403 >
404 <div
405 class="command-wrapper"
406 style="max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;"
407 >
408 ${backgroundIcon}${displayCommand}
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100409 </div>
410 </span>
411 <div slot="input" class="input">
412 <div class="tool-call-result-container">
413 <pre>${backgroundIcon}${inputData?.command}</pre>
414 </div>
415 </div>
416 ${this.toolCall?.result_message?.tool_result
417 ? html`<div slot="result" class="result">
418 <div class="tool-call-result-container">
419 <pre class="tool-call-result">
420${this.toolCall?.result_message.tool_result}</pre
421 >
422 </div>
423 </div>`
424 : ""}
425 </sketch-tool-card>`;
426 }
427}
428
429@customElement("sketch-tool-card-codereview")
430export class SketchToolCardCodeReview extends LitElement {
431 @property() toolCall: ToolCall;
432 @property() open: boolean;
433
434 // Determine the status icon based on the content of the result message
435 getStatusIcon(resultText: string): string {
436 if (!resultText) return "";
437 if (resultText === "OK") return "✔️";
438 if (resultText.includes("# Errors")) return "⚠️";
439 if (resultText.includes("# Info")) return "ℹ️";
440 if (resultText.includes("uncommitted changes in repo")) return "🧹";
441 if (resultText.includes("no new commits have been added")) return "🐣";
442 if (resultText.includes("git repo is not clean")) return "🧼";
443 return "❓";
444 }
445
446 render() {
447 const resultText = this.toolCall?.result_message?.tool_result || "";
448 const statusIcon = this.getStatusIcon(resultText);
449
450 return html`<sketch-tool-card .open=${this.open} .toolCall=${this.toolCall}>
451 <span slot="summary" class="summary-text">${statusIcon}</span>
452 <div slot="result"><pre>${resultText}</pre></div>
453 </sketch-tool-card>`;
454 }
455}
456
457@customElement("sketch-tool-card-done")
458export class SketchToolCardDone extends LitElement {
459 @property() toolCall: ToolCall;
460 @property() open: boolean;
461
462 render() {
463 const doneInput = JSON.parse(this.toolCall.input);
464 return html`<sketch-tool-card .open=${this.open} .toolCall=${this.toolCall}>
465 <span slot="summary" class="summary-text"></span>
466 <div slot="result">
467 ${Object.keys(doneInput.checklist_items).map((key) => {
468 const item = doneInput.checklist_items[key];
Josh Bleecher Snyderfbbf83b2025-05-15 10:55:55 -0700469 let statusIcon = "〰️";
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100470 if (item.status == "yes") {
Josh Bleecher Snyderfbbf83b2025-05-15 10:55:55 -0700471 statusIcon = "✅";
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100472 } else if (item.status == "not applicable") {
Josh Bleecher Snyderfbbf83b2025-05-15 10:55:55 -0700473 statusIcon = "🤷";
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100474 }
475 return html`<div>
476 <span>${statusIcon}</span> ${key}:${item.status}
477 </div>`;
478 })}
479 </div>
480 </sketch-tool-card>`;
481 }
482}
483
484@customElement("sketch-tool-card-patch")
485export class SketchToolCardPatch extends LitElement {
486 @property() toolCall: ToolCall;
487 @property() open: boolean;
488
489 static styles = css`
490 .summary-text {
491 color: #555;
492 font-family: monospace;
493 overflow: hidden;
494 text-overflow: ellipsis;
495 white-space: nowrap;
496 border-radius: 3px;
497 }
498 `;
499
500 render() {
501 const patchInput = JSON.parse(this.toolCall?.input);
502 return html`<sketch-tool-card .open=${this.open} .toolCall=${this.toolCall}>
503 <span slot="summary" class="summary-text">
504 ${patchInput?.path}: ${patchInput.patches.length}
505 edit${patchInput.patches.length > 1 ? "s" : ""}
506 </span>
507 <div slot="input">
508 ${patchInput.patches.map((patch) => {
509 return html`Patch operation: <b>${patch.operation}</b>
510 <pre>${patch.newText}</pre>`;
511 })}
512 </div>
513 <div slot="result">
514 <pre>${this.toolCall?.result_message?.tool_result}</pre>
515 </div>
516 </sketch-tool-card>`;
517 }
518}
519
520@customElement("sketch-tool-card-think")
521export class SketchToolCardThink extends LitElement {
522 @property() toolCall: ToolCall;
523 @property() open: boolean;
524
525 static styles = css`
526 .thought-bubble {
527 overflow-x: auto;
528 margin-bottom: 3px;
529 font-family: monospace;
530 padding: 3px 5px;
531 background: rgb(236, 236, 236);
532 border-radius: 6px;
533 user-select: text;
534 cursor: text;
535 -webkit-user-select: text;
536 -moz-user-select: text;
537 -ms-user-select: text;
538 font-size: 13px;
539 line-height: 1.3;
540 }
541 .summary-text {
542 overflow: hidden;
543 text-overflow: ellipsis;
544 font-family: monospace;
545 }
546 `;
547
548 render() {
549 return html`
550 <sketch-tool-card .open=${this.open} .toolCall=${this.toolCall}>
551 <span slot="summary" class="summary-text">
552 ${JSON.parse(this.toolCall?.input)?.thoughts?.split("\n")[0]}
553 </span>
554 <div slot="input" class="thought-bubble">
555 <div class="markdown-content">
556 ${unsafeHTML(
557 renderMarkdown(JSON.parse(this.toolCall?.input)?.thoughts),
558 )}
559 </div>
560 </div>
561 </sketch-tool-card>
562 `;
563 }
564}
565
566@customElement("sketch-tool-card-title")
567export class SketchToolCardTitle extends LitElement {
568 @property() toolCall: ToolCall;
569 @property() open: boolean;
570
571 static styles = css`
572 .summary-text {
573 font-style: italic;
574 }
575 pre {
576 display: inline;
577 font-family: monospace;
578 background: rgb(236, 236, 236);
579 padding: 2px 4px;
580 border-radius: 2px;
581 margin: 0;
582 }
583 `;
584
585 render() {
586 const inputData = JSON.parse(this.toolCall?.input || "{}");
587 return html`
588 <sketch-tool-card .open=${this.open} .toolCall=${this.toolCall}>
589 <span slot="summary" class="summary-text">
Josh Bleecher Snydera2a31502025-05-07 12:37:18 +0000590 Title: "${inputData.title}"
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100591 </span>
592 <div slot="input">
593 <div>Set title to: <b>${inputData.title}</b></div>
Josh Bleecher Snydera2a31502025-05-07 12:37:18 +0000594 </div>
595 </sketch-tool-card>
596 `;
597 }
598}
599
600@customElement("sketch-tool-card-precommit")
601export class SketchToolCardPrecommit extends LitElement {
602 @property()
603 toolCall: ToolCall;
604
605 @property()
606 open: boolean;
607
Philip Zeyligerbe7802a2025-06-04 20:15:25 +0000608 @property()
609 state: State;
610
Josh Bleecher Snydera2a31502025-05-07 12:37:18 +0000611 static styles = css`
612 .summary-text {
613 font-style: italic;
614 }
615 pre {
616 display: inline;
617 font-family: monospace;
618 background: rgb(236, 236, 236);
619 padding: 2px 4px;
620 border-radius: 2px;
621 margin: 0;
622 }
623 `;
624 constructor() {
625 super();
626 }
627
628 connectedCallback() {
629 super.connectedCallback();
630 }
631
632 disconnectedCallback() {
633 super.disconnectedCallback();
634 }
635
636 render() {
637 const inputData = JSON.parse(this.toolCall?.input || "{}");
638 return html`
639 <sketch-tool-card .open=${this.open} .toolCall=${this.toolCall}>
640 <span slot="summary" class="summary-text">
Philip Zeyligerbe7802a2025-06-04 20:15:25 +0000641 Branch: ${this.state?.branch_prefix}${inputData.branch_name}
Josh Bleecher Snydera2a31502025-05-07 12:37:18 +0000642 </span>
643 <div slot="input">
Autoformatter7e5fe3c2025-06-04 22:24:53 +0000644 <div>
645 Set branch to:
646 <code>${this.state?.branch_prefix}${inputData.branch_name}</code>
647 </div>
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100648 </div>
649 </sketch-tool-card>
650 `;
651 }
652}
653
654@customElement("sketch-tool-card-multiple-choice")
655export class SketchToolCardMultipleChoice extends LitElement {
656 @property() toolCall: ToolCall;
657 @property() open: boolean;
658 @property() selectedOption: MultipleChoiceOption = null;
659
660 static styles = css`
661 .options-container {
662 display: flex;
663 flex-direction: row;
664 flex-wrap: wrap;
665 gap: 8px;
666 margin: 10px 0;
667 }
668 .option {
669 display: inline-flex;
670 align-items: center;
671 padding: 8px 12px;
672 border-radius: 4px;
673 background-color: #f5f5f5;
674 cursor: pointer;
675 transition: all 0.2s;
676 border: 1px solid transparent;
677 user-select: none;
678 }
679 .option:hover {
680 background-color: #e0e0e0;
681 border-color: #ccc;
682 transform: translateY(-1px);
683 box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
684 }
685 .option:active {
686 transform: translateY(0);
687 box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
688 background-color: #d5d5d5;
689 }
690 .option.selected {
691 background-color: #e3f2fd;
692 border-color: #2196f3;
693 border-width: 1px;
694 border-style: solid;
695 }
696 .option-checkmark {
697 margin-left: 6px;
698 color: #2196f3;
699 }
700 .summary-text {
701 font-style: italic;
702 padding: 0.5em;
703 }
704 .summary-text strong {
705 font-style: normal;
706 color: #2196f3;
707 font-weight: 600;
708 }
709 `;
710
711 connectedCallback() {
712 super.connectedCallback();
713 this.updateSelectedOption();
714 }
715
716 updated(changedProps) {
717 if (changedProps.has("toolCall")) {
718 this.updateSelectedOption();
719 }
720 }
721
722 updateSelectedOption() {
723 if (this.toolCall?.result_message?.tool_result) {
724 try {
725 this.selectedOption = JSON.parse(
726 this.toolCall.result_message.tool_result,
727 ).selected;
728 } catch (e) {
729 console.error("Error parsing result:", e);
730 }
731 } else {
732 this.selectedOption = null;
733 }
734 }
735
736 async handleOptionClick(choice) {
737 this.selectedOption = this.selectedOption === choice ? null : choice;
738
739 const event = new CustomEvent("multiple-choice-selected", {
740 detail: {
741 responseText: this.selectedOption.responseText,
742 toolCall: this.toolCall,
743 },
744 bubbles: true,
745 composed: true,
746 });
747 this.dispatchEvent(event);
748 }
749
750 render() {
751 let choices = [];
752 let question = "";
753 try {
754 const inputData = JSON.parse(
755 this.toolCall?.input || "{}",
756 ) as MultipleChoiceParams;
757 choices = inputData.responseOptions || [];
758 question = inputData.question || "Please select an option:";
759 } catch (e) {
760 console.error("Error parsing multiple-choice input:", e);
761 }
762
763 const summaryContent =
764 this.selectedOption !== null
765 ? html`<span class="summary-text">
766 ${question}: <strong>${this.selectedOption.caption}</strong>
767 </span>`
768 : html`<span class="summary-text">${question}</span>`;
769
770 return html`
771 <div class="multiple-choice-card">
772 ${summaryContent}
773 <div class="options-container">
774 ${choices.map((choice) => {
775 const isSelected =
776 this.selectedOption !== null && this.selectedOption === choice;
777 return html`
778 <div
779 class="option ${isSelected ? "selected" : ""}"
780 @click=${() => this.handleOptionClick(choice)}
781 title="${choice.responseText}"
782 >
783 <span class="option-label">${choice.caption}</span>
784 ${isSelected
785 ? html`<span class="option-checkmark">✓</span>`
786 : ""}
787 </div>
788 `;
789 })}
790 </div>
791 </div>
792 `;
793 }
794}
795
Josh Bleecher Snyder112b9232025-05-23 11:26:33 -0700796@customElement("sketch-tool-card-todo-write")
797export class SketchToolCardTodoWrite extends LitElement {
798 @property() toolCall: ToolCall;
799 @property() open: boolean;
800
801 static styles = css`
802 .summary-text {
803 font-style: italic;
804 color: #666;
805 }
806 `;
807
808 render() {
809 const inputData = JSON.parse(this.toolCall?.input || "{}");
810 const tasks = inputData.tasks || [];
Josh Bleecher Snyder991164f2025-05-29 05:02:10 +0000811
Josh Bleecher Snyder112b9232025-05-23 11:26:33 -0700812 // Generate circles based on task status
Josh Bleecher Snyder991164f2025-05-29 05:02:10 +0000813 const circles = tasks
814 .map((task) => {
815 switch (task.status) {
816 case "completed":
817 return "●"; // full circle
818 case "in-progress":
819 return "◐"; // half circle
820 case "queued":
821 default:
822 return "○"; // empty circle
823 }
824 })
825 .join(" ");
826
Josh Bleecher Snyder112b9232025-05-23 11:26:33 -0700827 return html`<sketch-tool-card .open=${this.open} .toolCall=${this.toolCall}>
Josh Bleecher Snyder991164f2025-05-29 05:02:10 +0000828 <span slot="summary" class="summary-text"> ${circles} </span>
829 <div slot="result">
830 <pre>${this.toolCall?.result_message?.tool_result}</pre>
831 </div>
832 </sketch-tool-card>`;
833 }
834}
835
836@customElement("sketch-tool-card-keyword-search")
837export class SketchToolCardKeywordSearch extends LitElement {
838 @property() toolCall: ToolCall;
839 @property() open: boolean;
840
841 static styles = css`
842 .summary-container {
843 display: flex;
844 flex-direction: column;
845 gap: 2px;
846 width: 100%;
847 max-width: 100%;
848 overflow: hidden;
849 }
850 .query-line {
851 color: #333;
852 font-family: inherit;
853 font-size: 12px;
854 font-weight: normal;
855 white-space: normal;
856 word-wrap: break-word;
857 word-break: break-word;
858 overflow-wrap: break-word;
859 line-height: 1.2;
860 }
861 .keywords-line {
862 color: #666;
863 font-family: inherit;
864 font-size: 11px;
865 font-weight: normal;
866 white-space: normal;
867 word-wrap: break-word;
868 word-break: break-word;
869 overflow-wrap: break-word;
870 line-height: 1.2;
871 margin-top: 1px;
872 }
873 `;
874
875 render() {
876 const inputData = JSON.parse(this.toolCall?.input || "{}");
877 const query = inputData.query || "";
878 const searchTerms = inputData.search_terms || [];
879
880 return html`<sketch-tool-card .open=${this.open} .toolCall=${this.toolCall}>
881 <div slot="summary" class="summary-container">
882 <div class="query-line">🔍 ${query}</div>
883 <div class="keywords-line">🗝️ ${searchTerms.join(", ")}</div>
884 </div>
885 <div slot="input">
886 <div><strong>Query:</strong> ${query}</div>
887 <div><strong>Search terms:</strong> ${searchTerms.join(", ")}</div>
888 </div>
Josh Bleecher Snyder112b9232025-05-23 11:26:33 -0700889 <div slot="result">
890 <pre>${this.toolCall?.result_message?.tool_result}</pre>
891 </div>
892 </sketch-tool-card>`;
893 }
894}
895
896@customElement("sketch-tool-card-todo-read")
897export class SketchToolCardTodoRead extends LitElement {
898 @property() toolCall: ToolCall;
899 @property() open: boolean;
900
901 static styles = css`
902 .summary-text {
903 font-style: italic;
904 color: #666;
905 }
906 `;
907
908 render() {
909 return html`<sketch-tool-card .open=${this.open} .toolCall=${this.toolCall}>
Josh Bleecher Snyder991164f2025-05-29 05:02:10 +0000910 <span slot="summary" class="summary-text"> Read todo list </span>
Josh Bleecher Snyder112b9232025-05-23 11:26:33 -0700911 <div slot="result">
912 <pre>${this.toolCall?.result_message?.tool_result}</pre>
913 </div>
914 </sketch-tool-card>`;
915 }
916}
917
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100918@customElement("sketch-tool-card-generic")
919export class SketchToolCardGeneric extends LitElement {
920 @property() toolCall: ToolCall;
921 @property() open: boolean;
922
923 render() {
924 return html`<sketch-tool-card .open=${this.open} .toolCall=${this.toolCall}>
Philip Zeyligere31d2a92025-05-11 15:22:35 -0700925 <span
926 slot="summary"
927 style="display: block; white-space: normal; word-break: break-word; overflow-wrap: break-word; max-width: 100%; width: 100%;"
928 >${this.toolCall?.input}</span
929 >
930 <div
931 slot="input"
932 style="max-width: 100%; overflow-wrap: break-word; word-break: break-word;"
933 >
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100934 Input:
Philip Zeyligere31d2a92025-05-11 15:22:35 -0700935 <pre
936 style="max-width: 100%; white-space: pre-wrap; overflow-wrap: break-word; word-break: break-word;"
937 >
938${this.toolCall?.input}</pre
939 >
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100940 </div>
Philip Zeyligere31d2a92025-05-11 15:22:35 -0700941 <div
942 slot="result"
943 style="max-width: 100%; overflow-wrap: break-word; word-break: break-word;"
944 >
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100945 Result:
946 ${this.toolCall?.result_message?.tool_result
947 ? html`<pre>${this.toolCall?.result_message.tool_result}</pre>`
948 : ""}
949 </div>
950 </sketch-tool-card>`;
951 }
952}
953
Sean McCulloughec3ad1a2025-04-18 13:55:16 -0700954declare global {
955 interface HTMLElementTagNameMap {
956 "sketch-tool-card": SketchToolCard;
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100957 "sketch-tool-card-generic": SketchToolCardGeneric;
958 "sketch-tool-card-bash": SketchToolCardBash;
959 "sketch-tool-card-codereview": SketchToolCardCodeReview;
960 "sketch-tool-card-done": SketchToolCardDone;
961 "sketch-tool-card-patch": SketchToolCardPatch;
962 "sketch-tool-card-think": SketchToolCardThink;
963 "sketch-tool-card-title": SketchToolCardTitle;
Josh Bleecher Snydera2a31502025-05-07 12:37:18 +0000964 "sketch-tool-card-precommit": SketchToolCardPrecommit;
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100965 "sketch-tool-card-multiple-choice": SketchToolCardMultipleChoice;
Josh Bleecher Snyder112b9232025-05-23 11:26:33 -0700966 "sketch-tool-card-todo-write": SketchToolCardTodoWrite;
967 "sketch-tool-card-todo-read": SketchToolCardTodoRead;
Josh Bleecher Snyder991164f2025-05-29 05:02:10 +0000968 "sketch-tool-card-keyword-search": SketchToolCardKeywordSearch;
Philip Zeyliger84a8ae62025-05-13 16:36:01 -0700969 // TODO: We haven't implemented this for browser tools.
Sean McCulloughec3ad1a2025-04-18 13:55:16 -0700970 }
971}