| Josh Bleecher Snyder | 31785ae | 2025-05-06 01:50:58 +0000 | [diff] [blame] | 1 | import { css, html, LitElement } from "lit"; |
| 2 | import { unsafeHTML } from "lit/directives/unsafe-html.js"; |
| 3 | import { customElement, property } from "lit/decorators.js"; |
| 4 | import { ToolCall } from "../types"; |
| 5 | import { marked } from "marked"; |
| 6 | |
| 7 | // Safely renders markdown with fallback to plain text on failure |
| 8 | function renderMarkdown(markdownContent: string): string { |
| 9 | try { |
| 10 | return marked.parse(markdownContent, { |
| 11 | gfm: true, |
| 12 | breaks: true, |
| 13 | async: false, |
| 14 | }) as string; |
| 15 | } catch (error) { |
| 16 | console.error("Error rendering markdown:", error); |
| 17 | return markdownContent; |
| 18 | } |
| 19 | } |
| 20 | |
| Josh Bleecher Snyder | 74d690e | 2025-05-14 18:16:03 -0700 | [diff] [blame] | 21 | @customElement("sketch-tool-card-about-sketch") |
| 22 | export class SketchToolCardAboutSketch extends LitElement { |
| Josh Bleecher Snyder | 31785ae | 2025-05-06 01:50:58 +0000 | [diff] [blame] | 23 | @property() toolCall: ToolCall; |
| 24 | @property() open: boolean; |
| 25 | |
| 26 | static styles = css` |
| 27 | .summary-text { |
| 28 | font-style: italic; |
| 29 | } |
| Josh Bleecher Snyder | 74d690e | 2025-05-14 18:16:03 -0700 | [diff] [blame] | 30 | .about-sketch-content { |
| Josh Bleecher Snyder | 31785ae | 2025-05-06 01:50:58 +0000 | [diff] [blame] | 31 | background: rgb(246, 248, 250); |
| 32 | border-radius: 6px; |
| 33 | padding: 12px; |
| 34 | margin-top: 10px; |
| 35 | max-height: 300px; |
| 36 | overflow-y: auto; |
| 37 | border: 1px solid #e1e4e8; |
| 38 | } |
| Josh Bleecher Snyder | 74d690e | 2025-05-14 18:16:03 -0700 | [diff] [blame] | 39 | .sketch-label { |
| Josh Bleecher Snyder | 31785ae | 2025-05-06 01:50:58 +0000 | [diff] [blame] | 40 | font-weight: bold; |
| 41 | color: #24292e; |
| 42 | } |
| 43 | .icon { |
| 44 | margin-right: 6px; |
| 45 | } |
| 46 | `; |
| 47 | |
| 48 | render() { |
| Josh Bleecher Snyder | 31785ae | 2025-05-06 01:50:58 +0000 | [diff] [blame] | 49 | const resultText = this.toolCall?.result_message?.tool_result || ""; |
| Autoformatter | 7fb3499 | 2025-05-13 02:23:55 +0000 | [diff] [blame] | 50 | |
| Josh Bleecher Snyder | 31785ae | 2025-05-06 01:50:58 +0000 | [diff] [blame] | 51 | return html` |
| 52 | <sketch-tool-card .open=${this.open} .toolCall=${this.toolCall}> |
| 53 | <span slot="summary" class="summary-text"> |
| Josh Bleecher Snyder | 74d690e | 2025-05-14 18:16:03 -0700 | [diff] [blame] | 54 | <span class="icon">📚</span> About Sketch |
| Josh Bleecher Snyder | 31785ae | 2025-05-06 01:50:58 +0000 | [diff] [blame] | 55 | </span> |
| 56 | <div slot="input"> |
| Josh Bleecher Snyder | 74d690e | 2025-05-14 18:16:03 -0700 | [diff] [blame] | 57 | <div><span class="sketch-label"></span></div> |
| Josh Bleecher Snyder | 31785ae | 2025-05-06 01:50:58 +0000 | [diff] [blame] | 58 | </div> |
| 59 | ${this.toolCall?.result_message?.tool_result |
| 60 | ? html`<div slot="result"> |
| Josh Bleecher Snyder | 74d690e | 2025-05-14 18:16:03 -0700 | [diff] [blame] | 61 | <div class="about-sketch-content"> |
| Josh Bleecher Snyder | 31785ae | 2025-05-06 01:50:58 +0000 | [diff] [blame] | 62 | ${unsafeHTML(renderMarkdown(resultText))} |
| 63 | </div> |
| 64 | </div>` |
| 65 | : ""} |
| 66 | </sketch-tool-card> |
| 67 | `; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | declare global { |
| 72 | interface HTMLElementTagNameMap { |
| Josh Bleecher Snyder | 74d690e | 2025-05-14 18:16:03 -0700 | [diff] [blame] | 73 | "sketch-tool-card-about-sketch": SketchToolCardAboutSketch; |
| Josh Bleecher Snyder | 31785ae | 2025-05-06 01:50:58 +0000 | [diff] [blame] | 74 | } |
| 75 | } |