blob: ab0e723270a6c80a35c8e06bff4e78dd9f054124 [file] [log] [blame]
Josh Bleecher Snyder31785ae2025-05-06 01:50:58 +00001import { css, html, LitElement } from "lit";
2import { unsafeHTML } from "lit/directives/unsafe-html.js";
3import { customElement, property } from "lit/decorators.js";
4import { ToolCall } from "../types";
5import { marked } from "marked";
6
7// Safely renders markdown with fallback to plain text on failure
8function 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
21@customElement("sketch-tool-card-knowledge-base")
22export class SketchToolCardKnowledgeBase extends LitElement {
23 @property() toolCall: ToolCall;
24 @property() open: boolean;
25
26 static styles = css`
27 .summary-text {
28 font-style: italic;
29 }
30 .knowledge-content {
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 }
39 .topic-label {
40 font-weight: bold;
41 color: #24292e;
42 }
43 .icon {
44 margin-right: 6px;
45 }
46 `;
47
48 render() {
49 const inputData = JSON.parse(this.toolCall?.input || "{}");
50 const topic = inputData.topic || "unknown";
51 const resultText = this.toolCall?.result_message?.tool_result || "";
Autoformatter7fb34992025-05-13 02:23:55 +000052
Josh Bleecher Snyder31785ae2025-05-06 01:50:58 +000053 return html`
54 <sketch-tool-card .open=${this.open} .toolCall=${this.toolCall}>
55 <span slot="summary" class="summary-text">
56 <span class="icon">📚</span> Knowledge: ${topic}
57 </span>
58 <div slot="input">
Autoformatter7fb34992025-05-13 02:23:55 +000059 <div><span class="topic-label">Topic:</span> ${topic}</div>
Josh Bleecher Snyder31785ae2025-05-06 01:50:58 +000060 </div>
61 ${this.toolCall?.result_message?.tool_result
62 ? html`<div slot="result">
63 <div class="knowledge-content">
64 ${unsafeHTML(renderMarkdown(resultText))}
65 </div>
66 </div>`
67 : ""}
68 </sketch-tool-card>
69 `;
70 }
71}
72
73declare global {
74 interface HTMLElementTagNameMap {
75 "sketch-tool-card-knowledge-base": SketchToolCardKnowledgeBase;
76 }
77}