| Pokey Rule | ef58e06 | 2025-05-07 13:32:58 +0100 | [diff] [blame^] | 1 | import { LitElement, css, html } from "lit"; |
| 2 | import { customElement, property } from "lit/decorators.js"; |
| 3 | import { unsafeHTML } from "lit/directives/unsafe-html.js"; |
| 4 | import { marked } from "marked"; |
| 5 | import { ToolCall } from "../types"; |
| 6 | |
| 7 | @customElement("sketch-tool-card-think") |
| 8 | export class SketchToolCardThink extends LitElement { |
| 9 | @property() toolCall: ToolCall; |
| 10 | @property() open: boolean; |
| 11 | |
| 12 | static styles = css` |
| 13 | .thought-bubble { |
| 14 | overflow-x: auto; |
| 15 | margin-bottom: 3px; |
| 16 | font-family: monospace; |
| 17 | padding: 3px 5px; |
| 18 | background: rgb(236, 236, 236); |
| 19 | border-radius: 6px; |
| 20 | user-select: text; |
| 21 | cursor: text; |
| 22 | -webkit-user-select: text; |
| 23 | -moz-user-select: text; |
| 24 | -ms-user-select: text; |
| 25 | font-size: 13px; |
| 26 | line-height: 1.3; |
| 27 | } |
| 28 | .summary-text { |
| 29 | overflow: hidden; |
| 30 | text-overflow: ellipsis; |
| 31 | font-family: monospace; |
| 32 | } |
| 33 | `; |
| 34 | |
| 35 | render() { |
| 36 | return html` |
| 37 | <sketch-tool-card .open=${this.open} .toolCall=${this.toolCall}> |
| 38 | <span slot="summary" class="summary-text"> |
| 39 | ${JSON.parse(this.toolCall?.input)?.thoughts?.split("\n")[0]} |
| 40 | </span> |
| 41 | <div slot="input" class="thought-bubble"> |
| 42 | <div class="markdown-content"> |
| 43 | ${unsafeHTML( |
| 44 | renderMarkdown(JSON.parse(this.toolCall?.input)?.thoughts), |
| 45 | )} |
| 46 | </div> |
| 47 | </div> |
| 48 | </sketch-tool-card> |
| 49 | `; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | declare global { |
| 54 | interface HTMLElementTagNameMap { |
| 55 | "sketch-tool-card-think": SketchToolCardThink; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | function renderMarkdown(markdownContent: string): string { |
| 60 | try { |
| 61 | return marked.parse(markdownContent, { |
| 62 | gfm: true, |
| 63 | breaks: true, |
| 64 | async: false, |
| 65 | }) as string; |
| 66 | } catch (error) { |
| 67 | console.error("Error rendering markdown:", error); |
| 68 | return markdownContent; |
| 69 | } |
| 70 | } |