blob: c12f72252a16b61fc3cf4af0983ad458b7ffc031 [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
Josh Bleecher Snyder19969a92025-06-05 14:34:02 -0700566@customElement("sketch-tool-card-set-slug")
567export class SketchToolCardSetSlug extends LitElement {
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100568 @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 Snyder19969a92025-06-05 14:34:02 -0700590 Slug: "${inputData.slug}"
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100591 </span>
592 <div slot="input">
Josh Bleecher Snyder19969a92025-06-05 14:34:02 -0700593 <div>Set slug to: <b>${inputData.slug}</b></div>
Josh Bleecher Snydera2a31502025-05-07 12:37:18 +0000594 </div>
595 </sketch-tool-card>
596 `;
597 }
598}
599
Josh Bleecher Snyder19969a92025-06-05 14:34:02 -0700600@customElement("sketch-tool-card-commit-message-style")
601export class SketchToolCardCommitMessageStyle extends LitElement {
Josh Bleecher Snydera2a31502025-05-07 12:37:18 +0000602 @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() {
Josh Bleecher Snydera2a31502025-05-07 12:37:18 +0000637 return html`
638 <sketch-tool-card .open=${this.open} .toolCall=${this.toolCall}>
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100639 </sketch-tool-card>
640 `;
641 }
642}
643
644@customElement("sketch-tool-card-multiple-choice")
645export class SketchToolCardMultipleChoice extends LitElement {
646 @property() toolCall: ToolCall;
647 @property() open: boolean;
648 @property() selectedOption: MultipleChoiceOption = null;
649
650 static styles = css`
651 .options-container {
652 display: flex;
653 flex-direction: row;
654 flex-wrap: wrap;
655 gap: 8px;
656 margin: 10px 0;
657 }
658 .option {
659 display: inline-flex;
660 align-items: center;
661 padding: 8px 12px;
662 border-radius: 4px;
663 background-color: #f5f5f5;
664 cursor: pointer;
665 transition: all 0.2s;
666 border: 1px solid transparent;
667 user-select: none;
668 }
669 .option:hover {
670 background-color: #e0e0e0;
671 border-color: #ccc;
672 transform: translateY(-1px);
673 box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
674 }
675 .option:active {
676 transform: translateY(0);
677 box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
678 background-color: #d5d5d5;
679 }
680 .option.selected {
681 background-color: #e3f2fd;
682 border-color: #2196f3;
683 border-width: 1px;
684 border-style: solid;
685 }
686 .option-checkmark {
687 margin-left: 6px;
688 color: #2196f3;
689 }
690 .summary-text {
691 font-style: italic;
692 padding: 0.5em;
693 }
694 .summary-text strong {
695 font-style: normal;
696 color: #2196f3;
697 font-weight: 600;
698 }
699 `;
700
701 connectedCallback() {
702 super.connectedCallback();
703 this.updateSelectedOption();
704 }
705
706 updated(changedProps) {
707 if (changedProps.has("toolCall")) {
708 this.updateSelectedOption();
709 }
710 }
711
712 updateSelectedOption() {
713 if (this.toolCall?.result_message?.tool_result) {
714 try {
715 this.selectedOption = JSON.parse(
716 this.toolCall.result_message.tool_result,
717 ).selected;
718 } catch (e) {
719 console.error("Error parsing result:", e);
720 }
721 } else {
722 this.selectedOption = null;
723 }
724 }
725
726 async handleOptionClick(choice) {
727 this.selectedOption = this.selectedOption === choice ? null : choice;
728
729 const event = new CustomEvent("multiple-choice-selected", {
730 detail: {
731 responseText: this.selectedOption.responseText,
732 toolCall: this.toolCall,
733 },
734 bubbles: true,
735 composed: true,
736 });
737 this.dispatchEvent(event);
738 }
739
740 render() {
741 let choices = [];
742 let question = "";
743 try {
744 const inputData = JSON.parse(
745 this.toolCall?.input || "{}",
746 ) as MultipleChoiceParams;
747 choices = inputData.responseOptions || [];
748 question = inputData.question || "Please select an option:";
749 } catch (e) {
750 console.error("Error parsing multiple-choice input:", e);
751 }
752
753 const summaryContent =
754 this.selectedOption !== null
755 ? html`<span class="summary-text">
756 ${question}: <strong>${this.selectedOption.caption}</strong>
757 </span>`
758 : html`<span class="summary-text">${question}</span>`;
759
760 return html`
761 <div class="multiple-choice-card">
762 ${summaryContent}
763 <div class="options-container">
764 ${choices.map((choice) => {
765 const isSelected =
766 this.selectedOption !== null && this.selectedOption === choice;
767 return html`
768 <div
769 class="option ${isSelected ? "selected" : ""}"
770 @click=${() => this.handleOptionClick(choice)}
771 title="${choice.responseText}"
772 >
773 <span class="option-label">${choice.caption}</span>
774 ${isSelected
775 ? html`<span class="option-checkmark">✓</span>`
776 : ""}
777 </div>
778 `;
779 })}
780 </div>
781 </div>
782 `;
783 }
784}
785
Josh Bleecher Snyder112b9232025-05-23 11:26:33 -0700786@customElement("sketch-tool-card-todo-write")
787export class SketchToolCardTodoWrite extends LitElement {
788 @property() toolCall: ToolCall;
789 @property() open: boolean;
790
791 static styles = css`
792 .summary-text {
793 font-style: italic;
794 color: #666;
795 }
796 `;
797
798 render() {
799 const inputData = JSON.parse(this.toolCall?.input || "{}");
800 const tasks = inputData.tasks || [];
Josh Bleecher Snyder991164f2025-05-29 05:02:10 +0000801
Josh Bleecher Snyder112b9232025-05-23 11:26:33 -0700802 // Generate circles based on task status
Josh Bleecher Snyder991164f2025-05-29 05:02:10 +0000803 const circles = tasks
804 .map((task) => {
805 switch (task.status) {
806 case "completed":
807 return "●"; // full circle
808 case "in-progress":
809 return "◐"; // half circle
810 case "queued":
811 default:
812 return "○"; // empty circle
813 }
814 })
815 .join(" ");
816
Josh Bleecher Snyder112b9232025-05-23 11:26:33 -0700817 return html`<sketch-tool-card .open=${this.open} .toolCall=${this.toolCall}>
Josh Bleecher Snyder991164f2025-05-29 05:02:10 +0000818 <span slot="summary" class="summary-text"> ${circles} </span>
819 <div slot="result">
820 <pre>${this.toolCall?.result_message?.tool_result}</pre>
821 </div>
822 </sketch-tool-card>`;
823 }
824}
825
826@customElement("sketch-tool-card-keyword-search")
827export class SketchToolCardKeywordSearch extends LitElement {
828 @property() toolCall: ToolCall;
829 @property() open: boolean;
830
831 static styles = css`
832 .summary-container {
833 display: flex;
834 flex-direction: column;
835 gap: 2px;
836 width: 100%;
837 max-width: 100%;
838 overflow: hidden;
839 }
840 .query-line {
841 color: #333;
842 font-family: inherit;
843 font-size: 12px;
844 font-weight: normal;
845 white-space: normal;
846 word-wrap: break-word;
847 word-break: break-word;
848 overflow-wrap: break-word;
849 line-height: 1.2;
850 }
851 .keywords-line {
852 color: #666;
853 font-family: inherit;
854 font-size: 11px;
855 font-weight: normal;
856 white-space: normal;
857 word-wrap: break-word;
858 word-break: break-word;
859 overflow-wrap: break-word;
860 line-height: 1.2;
861 margin-top: 1px;
862 }
863 `;
864
865 render() {
866 const inputData = JSON.parse(this.toolCall?.input || "{}");
867 const query = inputData.query || "";
868 const searchTerms = inputData.search_terms || [];
869
870 return html`<sketch-tool-card .open=${this.open} .toolCall=${this.toolCall}>
871 <div slot="summary" class="summary-container">
872 <div class="query-line">🔍 ${query}</div>
873 <div class="keywords-line">🗝️ ${searchTerms.join(", ")}</div>
874 </div>
875 <div slot="input">
876 <div><strong>Query:</strong> ${query}</div>
877 <div><strong>Search terms:</strong> ${searchTerms.join(", ")}</div>
878 </div>
Josh Bleecher Snyder112b9232025-05-23 11:26:33 -0700879 <div slot="result">
880 <pre>${this.toolCall?.result_message?.tool_result}</pre>
881 </div>
882 </sketch-tool-card>`;
883 }
884}
885
886@customElement("sketch-tool-card-todo-read")
887export class SketchToolCardTodoRead extends LitElement {
888 @property() toolCall: ToolCall;
889 @property() open: boolean;
890
891 static styles = css`
892 .summary-text {
893 font-style: italic;
894 color: #666;
895 }
896 `;
897
898 render() {
899 return html`<sketch-tool-card .open=${this.open} .toolCall=${this.toolCall}>
Josh Bleecher Snyder991164f2025-05-29 05:02:10 +0000900 <span slot="summary" class="summary-text"> Read todo list </span>
Josh Bleecher Snyder112b9232025-05-23 11:26:33 -0700901 <div slot="result">
902 <pre>${this.toolCall?.result_message?.tool_result}</pre>
903 </div>
904 </sketch-tool-card>`;
905 }
906}
907
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100908@customElement("sketch-tool-card-generic")
909export class SketchToolCardGeneric extends LitElement {
910 @property() toolCall: ToolCall;
911 @property() open: boolean;
912
913 render() {
914 return html`<sketch-tool-card .open=${this.open} .toolCall=${this.toolCall}>
Philip Zeyligere31d2a92025-05-11 15:22:35 -0700915 <span
916 slot="summary"
917 style="display: block; white-space: normal; word-break: break-word; overflow-wrap: break-word; max-width: 100%; width: 100%;"
918 >${this.toolCall?.input}</span
919 >
920 <div
921 slot="input"
922 style="max-width: 100%; overflow-wrap: break-word; word-break: break-word;"
923 >
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100924 Input:
Philip Zeyligere31d2a92025-05-11 15:22:35 -0700925 <pre
926 style="max-width: 100%; white-space: pre-wrap; overflow-wrap: break-word; word-break: break-word;"
927 >
928${this.toolCall?.input}</pre
929 >
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100930 </div>
Philip Zeyligere31d2a92025-05-11 15:22:35 -0700931 <div
932 slot="result"
933 style="max-width: 100%; overflow-wrap: break-word; word-break: break-word;"
934 >
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100935 Result:
936 ${this.toolCall?.result_message?.tool_result
937 ? html`<pre>${this.toolCall?.result_message.tool_result}</pre>`
938 : ""}
939 </div>
940 </sketch-tool-card>`;
941 }
942}
943
Sean McCulloughec3ad1a2025-04-18 13:55:16 -0700944declare global {
945 interface HTMLElementTagNameMap {
946 "sketch-tool-card": SketchToolCard;
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100947 "sketch-tool-card-generic": SketchToolCardGeneric;
948 "sketch-tool-card-bash": SketchToolCardBash;
949 "sketch-tool-card-codereview": SketchToolCardCodeReview;
950 "sketch-tool-card-done": SketchToolCardDone;
951 "sketch-tool-card-patch": SketchToolCardPatch;
952 "sketch-tool-card-think": SketchToolCardThink;
Josh Bleecher Snyder19969a92025-06-05 14:34:02 -0700953 "sketch-tool-card-set-slug": SketchToolCardSetSlug;
954 "sketch-tool-card-commit-message-style": SketchToolCardCommitMessageStyle;
Pokey Rule7ac5ed02025-05-07 15:26:10 +0100955 "sketch-tool-card-multiple-choice": SketchToolCardMultipleChoice;
Josh Bleecher Snyder112b9232025-05-23 11:26:33 -0700956 "sketch-tool-card-todo-write": SketchToolCardTodoWrite;
957 "sketch-tool-card-todo-read": SketchToolCardTodoRead;
Josh Bleecher Snyder991164f2025-05-29 05:02:10 +0000958 "sketch-tool-card-keyword-search": SketchToolCardKeywordSearch;
Philip Zeyliger84a8ae62025-05-13 16:36:01 -0700959 // TODO: We haven't implemented this for browser tools.
Sean McCulloughec3ad1a2025-04-18 13:55:16 -0700960 }
961}