| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 1 | import { css, html, LitElement } from "lit"; |
| Sean McCullough | 2c5bba4 | 2025-04-20 19:33:17 -0700 | [diff] [blame] | 2 | import { PropertyValues } from "lit"; |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 3 | import { repeat } from "lit/directives/repeat.js"; |
| Sean McCullough | 2c5bba4 | 2025-04-20 19:33:17 -0700 | [diff] [blame] | 4 | import { customElement, property, state } from "lit/decorators.js"; |
| Sean McCullough | d9f1337 | 2025-04-21 15:08:49 -0700 | [diff] [blame] | 5 | import { AgentMessage } from "../types"; |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 6 | import "./sketch-timeline-message"; |
| Pokey Rule | 4097e53 | 2025-04-24 18:55:28 +0100 | [diff] [blame] | 7 | import { Ref } from "lit/directives/ref"; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 8 | |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 9 | @customElement("sketch-timeline") |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 10 | export class SketchTimeline extends LitElement { |
| Pokey Rule | e2a8c2f | 2025-04-23 15:09:25 +0100 | [diff] [blame] | 11 | @property({ attribute: false }) |
| Sean McCullough | d9f1337 | 2025-04-21 15:08:49 -0700 | [diff] [blame] | 12 | messages: AgentMessage[] = []; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 13 | |
| Philip Zeyliger | 16fa8b4 | 2025-05-02 04:28:16 +0000 | [diff] [blame] | 14 | // Active state properties to show thinking indicator |
| 15 | @property({ attribute: false }) |
| 16 | agentState: string | null = null; |
| 17 | |
| 18 | @property({ attribute: false }) |
| 19 | llmCalls: number = 0; |
| 20 | |
| 21 | @property({ attribute: false }) |
| 22 | toolCalls: string[] = []; |
| 23 | |
| Sean McCullough | 2c5bba4 | 2025-04-20 19:33:17 -0700 | [diff] [blame] | 24 | // Track if we should scroll to the bottom |
| 25 | @state() |
| 26 | private scrollingState: "pinToLatest" | "floating" = "pinToLatest"; |
| 27 | |
| Pokey Rule | e2a8c2f | 2025-04-23 15:09:25 +0100 | [diff] [blame] | 28 | @property({ attribute: false }) |
| Pokey Rule | 4097e53 | 2025-04-24 18:55:28 +0100 | [diff] [blame] | 29 | scrollContainer: Ref<HTMLElement>; |
| Sean McCullough | 2c5bba4 | 2025-04-20 19:33:17 -0700 | [diff] [blame] | 30 | |
| Philip Zeyliger | b8a8f35 | 2025-06-02 07:39:37 -0700 | [diff] [blame] | 31 | @property({ attribute: false }) |
| 32 | firstMessageIndex: number = 0; |
| 33 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 34 | static styles = css` |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 35 | /* Hide views initially to prevent flash of content */ |
| 36 | .timeline-container .timeline, |
| 37 | .timeline-container .diff-view, |
| 38 | .timeline-container .chart-view, |
| 39 | .timeline-container .terminal-view { |
| 40 | visibility: hidden; |
| 41 | } |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 42 | |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 43 | /* Will be set by JavaScript once we know which view to display */ |
| 44 | .timeline-container.view-initialized .timeline, |
| 45 | .timeline-container.view-initialized .diff-view, |
| 46 | .timeline-container.view-initialized .chart-view, |
| 47 | .timeline-container.view-initialized .terminal-view { |
| 48 | visibility: visible; |
| 49 | } |
| 50 | |
| 51 | .timeline-container { |
| 52 | width: 100%; |
| 53 | position: relative; |
| Philip Zeyliger | 16fa8b4 | 2025-05-02 04:28:16 +0000 | [diff] [blame] | 54 | max-width: 100%; |
| 55 | margin: 0 auto; |
| 56 | padding: 0 15px; |
| 57 | box-sizing: border-box; |
| Philip Zeyliger | e31d2a9 | 2025-05-11 15:22:35 -0700 | [diff] [blame] | 58 | overflow-x: hidden; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 59 | flex: 1; |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| Philip Zeyliger | 16fa8b4 | 2025-05-02 04:28:16 +0000 | [diff] [blame] | 62 | /* Chat-like timeline styles */ |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 63 | .timeline { |
| 64 | position: relative; |
| 65 | margin: 10px 0; |
| 66 | scroll-behavior: smooth; |
| 67 | } |
| 68 | |
| Philip Zeyliger | 16fa8b4 | 2025-05-02 04:28:16 +0000 | [diff] [blame] | 69 | /* Remove the vertical timeline line */ |
| Sean McCullough | 2c5bba4 | 2025-04-20 19:33:17 -0700 | [diff] [blame] | 70 | |
| 71 | #scroll-container { |
| Philip Zeyliger | e31d2a9 | 2025-05-11 15:22:35 -0700 | [diff] [blame] | 72 | overflow-y: auto; |
| 73 | overflow-x: hidden; |
| Sean McCullough | 2c5bba4 | 2025-04-20 19:33:17 -0700 | [diff] [blame] | 74 | padding-left: 1em; |
| Philip Zeyliger | e31d2a9 | 2025-05-11 15:22:35 -0700 | [diff] [blame] | 75 | max-width: 100%; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 76 | width: 100%; |
| Sean McCullough | 2c5bba4 | 2025-04-20 19:33:17 -0700 | [diff] [blame] | 77 | } |
| 78 | #jump-to-latest { |
| 79 | display: none; |
| Josh Bleecher Snyder | 112b923 | 2025-05-23 11:26:33 -0700 | [diff] [blame] | 80 | position: absolute; |
| 81 | bottom: 20px; |
| 82 | right: 20px; |
| Sean McCullough | 2c5bba4 | 2025-04-20 19:33:17 -0700 | [diff] [blame] | 83 | background: rgb(33, 150, 243); |
| 84 | color: white; |
| 85 | border-radius: 8px; |
| 86 | padding: 0.5em; |
| 87 | margin: 0.5em; |
| 88 | font-size: x-large; |
| 89 | opacity: 0.5; |
| 90 | cursor: pointer; |
| Josh Bleecher Snyder | 112b923 | 2025-05-23 11:26:33 -0700 | [diff] [blame] | 91 | z-index: 50; |
| Sean McCullough | 2c5bba4 | 2025-04-20 19:33:17 -0700 | [diff] [blame] | 92 | } |
| 93 | #jump-to-latest:hover { |
| 94 | opacity: 1; |
| 95 | } |
| 96 | #jump-to-latest.floating { |
| 97 | display: block; |
| 98 | } |
| Philip Zeyliger | 5cf4926 | 2025-04-29 18:35:55 +0000 | [diff] [blame] | 99 | |
| 100 | /* Welcome box styles for the empty chat state */ |
| 101 | .welcome-box { |
| 102 | margin: 2rem auto; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 103 | max-width: 90%; |
| 104 | width: 90%; |
| Philip Zeyliger | 5cf4926 | 2025-04-29 18:35:55 +0000 | [diff] [blame] | 105 | padding: 2rem; |
| 106 | border: 2px solid #e0e0e0; |
| 107 | border-radius: 8px; |
| 108 | box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05); |
| 109 | background-color: #ffffff; |
| 110 | text-align: center; |
| 111 | } |
| 112 | |
| 113 | .welcome-box-title { |
| 114 | font-size: 1.5rem; |
| 115 | font-weight: 600; |
| 116 | margin-bottom: 1.5rem; |
| Philip Zeyliger | abc093c | 2025-04-30 10:46:27 -0700 | [diff] [blame] | 117 | text-align: center; |
| Philip Zeyliger | 5cf4926 | 2025-04-29 18:35:55 +0000 | [diff] [blame] | 118 | color: #333; |
| 119 | } |
| 120 | |
| 121 | .welcome-box-content { |
| 122 | color: #666; /* Slightly grey font color */ |
| 123 | line-height: 1.6; |
| 124 | font-size: 1rem; |
| Philip Zeyliger | abc093c | 2025-04-30 10:46:27 -0700 | [diff] [blame] | 125 | text-align: left; |
| Philip Zeyliger | 5cf4926 | 2025-04-29 18:35:55 +0000 | [diff] [blame] | 126 | } |
| Philip Zeyliger | 16fa8b4 | 2025-05-02 04:28:16 +0000 | [diff] [blame] | 127 | |
| 128 | /* Thinking indicator styles */ |
| 129 | .thinking-indicator { |
| 130 | padding-left: 85px; |
| 131 | margin-top: 5px; |
| 132 | margin-bottom: 15px; |
| 133 | display: flex; |
| 134 | } |
| 135 | |
| 136 | .thinking-bubble { |
| 137 | background-color: #f1f1f1; |
| 138 | border-radius: 15px; |
| 139 | padding: 10px 15px; |
| 140 | max-width: 80px; |
| 141 | color: black; |
| 142 | position: relative; |
| 143 | border-bottom-left-radius: 5px; |
| 144 | } |
| 145 | |
| 146 | .thinking-dots { |
| 147 | display: flex; |
| 148 | align-items: center; |
| 149 | justify-content: center; |
| 150 | gap: 4px; |
| 151 | height: 14px; |
| 152 | } |
| 153 | |
| 154 | .dot { |
| 155 | width: 6px; |
| 156 | height: 6px; |
| 157 | background-color: #888; |
| 158 | border-radius: 50%; |
| 159 | opacity: 0.6; |
| 160 | } |
| 161 | |
| 162 | .dot:nth-child(1) { |
| 163 | animation: pulse 1.5s infinite ease-in-out; |
| 164 | } |
| 165 | |
| 166 | .dot:nth-child(2) { |
| 167 | animation: pulse 1.5s infinite ease-in-out 0.3s; |
| 168 | } |
| 169 | |
| 170 | .dot:nth-child(3) { |
| 171 | animation: pulse 1.5s infinite ease-in-out 0.6s; |
| 172 | } |
| 173 | |
| 174 | @keyframes pulse { |
| 175 | 0%, |
| 176 | 100% { |
| 177 | opacity: 0.4; |
| 178 | transform: scale(1); |
| 179 | } |
| 180 | 50% { |
| 181 | opacity: 1; |
| 182 | transform: scale(1.2); |
| 183 | } |
| 184 | } |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 185 | `; |
| 186 | |
| 187 | constructor() { |
| 188 | super(); |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 189 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 190 | // Binding methods |
| 191 | this._handleShowCommitDiff = this._handleShowCommitDiff.bind(this); |
| Sean McCullough | 2c5bba4 | 2025-04-20 19:33:17 -0700 | [diff] [blame] | 192 | this._handleScroll = this._handleScroll.bind(this); |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Scroll to the bottom of the timeline |
| 197 | */ |
| 198 | private scrollToBottom(): void { |
| Josh Bleecher Snyder | dee39e0 | 2025-05-29 14:25:08 +0000 | [diff] [blame] | 199 | if (!this.scrollContainer.value) return; |
| Autoformatter | 71c73b5 | 2025-05-29 20:18:43 +0000 | [diff] [blame] | 200 | |
| Josh Bleecher Snyder | dee39e0 | 2025-05-29 14:25:08 +0000 | [diff] [blame] | 201 | // Use instant scroll to ensure we reach the exact bottom |
| 202 | this.scrollContainer.value.scrollTo({ |
| 203 | top: this.scrollContainer.value.scrollHeight, |
| 204 | behavior: "instant", |
| Sean McCullough | 2c5bba4 | 2025-04-20 19:33:17 -0700 | [diff] [blame] | 205 | }); |
| 206 | } |
| Autoformatter | 71c73b5 | 2025-05-29 20:18:43 +0000 | [diff] [blame] | 207 | |
| Josh Bleecher Snyder | dee39e0 | 2025-05-29 14:25:08 +0000 | [diff] [blame] | 208 | /** |
| 209 | * Scroll to bottom with retry logic to handle dynamic content |
| 210 | */ |
| 211 | private scrollToBottomWithRetry(): void { |
| 212 | if (!this.scrollContainer.value) return; |
| Autoformatter | 71c73b5 | 2025-05-29 20:18:43 +0000 | [diff] [blame] | 213 | |
| Josh Bleecher Snyder | dee39e0 | 2025-05-29 14:25:08 +0000 | [diff] [blame] | 214 | let attempts = 0; |
| 215 | const maxAttempts = 5; |
| 216 | const retryInterval = 50; |
| Autoformatter | 71c73b5 | 2025-05-29 20:18:43 +0000 | [diff] [blame] | 217 | |
| Josh Bleecher Snyder | dee39e0 | 2025-05-29 14:25:08 +0000 | [diff] [blame] | 218 | const tryScroll = () => { |
| 219 | if (!this.scrollContainer.value) return; |
| Autoformatter | 71c73b5 | 2025-05-29 20:18:43 +0000 | [diff] [blame] | 220 | |
| Josh Bleecher Snyder | dee39e0 | 2025-05-29 14:25:08 +0000 | [diff] [blame] | 221 | const container = this.scrollContainer.value; |
| 222 | const targetScrollTop = container.scrollHeight - container.clientHeight; |
| Autoformatter | 71c73b5 | 2025-05-29 20:18:43 +0000 | [diff] [blame] | 223 | |
| Josh Bleecher Snyder | dee39e0 | 2025-05-29 14:25:08 +0000 | [diff] [blame] | 224 | // Scroll to the calculated position |
| 225 | container.scrollTo({ |
| 226 | top: targetScrollTop, |
| 227 | behavior: "instant", |
| 228 | }); |
| Autoformatter | 71c73b5 | 2025-05-29 20:18:43 +0000 | [diff] [blame] | 229 | |
| Josh Bleecher Snyder | dee39e0 | 2025-05-29 14:25:08 +0000 | [diff] [blame] | 230 | attempts++; |
| Autoformatter | 71c73b5 | 2025-05-29 20:18:43 +0000 | [diff] [blame] | 231 | |
| Josh Bleecher Snyder | dee39e0 | 2025-05-29 14:25:08 +0000 | [diff] [blame] | 232 | // Check if we're actually at the bottom |
| 233 | const actualScrollTop = container.scrollTop; |
| 234 | const isAtBottom = Math.abs(targetScrollTop - actualScrollTop) <= 1; |
| Autoformatter | 71c73b5 | 2025-05-29 20:18:43 +0000 | [diff] [blame] | 235 | |
| Josh Bleecher Snyder | dee39e0 | 2025-05-29 14:25:08 +0000 | [diff] [blame] | 236 | if (!isAtBottom && attempts < maxAttempts) { |
| 237 | // Still not at bottom and we have attempts left, try again |
| 238 | setTimeout(tryScroll, retryInterval); |
| 239 | } |
| 240 | }; |
| Autoformatter | 71c73b5 | 2025-05-29 20:18:43 +0000 | [diff] [blame] | 241 | |
| Josh Bleecher Snyder | dee39e0 | 2025-05-29 14:25:08 +0000 | [diff] [blame] | 242 | tryScroll(); |
| 243 | } |
| Sean McCullough | 2c5bba4 | 2025-04-20 19:33:17 -0700 | [diff] [blame] | 244 | |
| 245 | /** |
| 246 | * Called after the component's properties have been updated |
| 247 | */ |
| 248 | updated(changedProperties: PropertyValues): void { |
| 249 | // If messages have changed, scroll to bottom if needed |
| 250 | if (changedProperties.has("messages") && this.messages.length > 0) { |
| 251 | if (this.scrollingState == "pinToLatest") { |
| Josh Bleecher Snyder | dee39e0 | 2025-05-29 14:25:08 +0000 | [diff] [blame] | 252 | // Use longer timeout and retry logic to handle dynamic content |
| 253 | setTimeout(() => this.scrollToBottomWithRetry(), 100); |
| Sean McCullough | 2c5bba4 | 2025-04-20 19:33:17 -0700 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | if (changedProperties.has("scrollContainer")) { |
| Pokey Rule | 4097e53 | 2025-04-24 18:55:28 +0100 | [diff] [blame] | 257 | this.scrollContainer.value?.addEventListener( |
| 258 | "scroll", |
| 259 | this._handleScroll, |
| 260 | ); |
| Sean McCullough | 2c5bba4 | 2025-04-20 19:33:17 -0700 | [diff] [blame] | 261 | } |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 262 | } |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 263 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 264 | /** |
| 265 | * Handle showCommitDiff event |
| 266 | */ |
| 267 | private _handleShowCommitDiff(event: CustomEvent) { |
| 268 | const { commitHash } = event.detail; |
| 269 | if (commitHash) { |
| 270 | // Bubble up the event to the app shell |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 271 | const newEvent = new CustomEvent("show-commit-diff", { |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 272 | detail: { commitHash }, |
| 273 | bubbles: true, |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 274 | composed: true, |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 275 | }); |
| 276 | this.dispatchEvent(newEvent); |
| 277 | } |
| 278 | } |
| 279 | |
| Sean McCullough | 2c5bba4 | 2025-04-20 19:33:17 -0700 | [diff] [blame] | 280 | private _handleScroll(event) { |
| Josh Bleecher Snyder | dee39e0 | 2025-05-29 14:25:08 +0000 | [diff] [blame] | 281 | if (!this.scrollContainer.value) return; |
| Autoformatter | 71c73b5 | 2025-05-29 20:18:43 +0000 | [diff] [blame] | 282 | |
| Josh Bleecher Snyder | dee39e0 | 2025-05-29 14:25:08 +0000 | [diff] [blame] | 283 | const container = this.scrollContainer.value; |
| Sean McCullough | 2c5bba4 | 2025-04-20 19:33:17 -0700 | [diff] [blame] | 284 | const isAtBottom = |
| 285 | Math.abs( |
| Autoformatter | 71c73b5 | 2025-05-29 20:18:43 +0000 | [diff] [blame] | 286 | container.scrollHeight - container.clientHeight - container.scrollTop, |
| Josh Bleecher Snyder | dee39e0 | 2025-05-29 14:25:08 +0000 | [diff] [blame] | 287 | ) <= 3; // Increased tolerance to 3px for better detection |
| Autoformatter | 71c73b5 | 2025-05-29 20:18:43 +0000 | [diff] [blame] | 288 | |
| Sean McCullough | 2c5bba4 | 2025-04-20 19:33:17 -0700 | [diff] [blame] | 289 | if (isAtBottom) { |
| 290 | this.scrollingState = "pinToLatest"; |
| 291 | } else { |
| 292 | // TODO: does scroll direction matter here? |
| 293 | this.scrollingState = "floating"; |
| 294 | } |
| 295 | } |
| 296 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 297 | // See https://lit.dev/docs/components/lifecycle/ |
| 298 | connectedCallback() { |
| 299 | super.connectedCallback(); |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 300 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 301 | // Listen for showCommitDiff events from the renderer |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 302 | document.addEventListener( |
| 303 | "showCommitDiff", |
| Philip Zeyliger | 72682df | 2025-04-23 13:09:46 -0700 | [diff] [blame] | 304 | this._handleShowCommitDiff as EventListener, |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 305 | ); |
| Pokey Rule | 4097e53 | 2025-04-24 18:55:28 +0100 | [diff] [blame] | 306 | |
| 307 | this.scrollContainer.value?.addEventListener("scroll", this._handleScroll); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | // See https://lit.dev/docs/components/lifecycle/ |
| 311 | disconnectedCallback() { |
| 312 | super.disconnectedCallback(); |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 313 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 314 | // Remove event listeners |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 315 | document.removeEventListener( |
| 316 | "showCommitDiff", |
| Philip Zeyliger | 72682df | 2025-04-23 13:09:46 -0700 | [diff] [blame] | 317 | this._handleShowCommitDiff as EventListener, |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 318 | ); |
| Sean McCullough | 2c5bba4 | 2025-04-20 19:33:17 -0700 | [diff] [blame] | 319 | |
| Pokey Rule | 4097e53 | 2025-04-24 18:55:28 +0100 | [diff] [blame] | 320 | this.scrollContainer.value?.removeEventListener( |
| 321 | "scroll", |
| 322 | this._handleScroll, |
| 323 | ); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 324 | } |
| 325 | |
| Sean McCullough | d9f1337 | 2025-04-21 15:08:49 -0700 | [diff] [blame] | 326 | // messageKey uniquely identifes a AgentMessage based on its ID and tool_calls, so |
| Sean McCullough | 2c5bba4 | 2025-04-20 19:33:17 -0700 | [diff] [blame] | 327 | // that we only re-render <sketch-message> elements that we need to re-render. |
| Sean McCullough | d9f1337 | 2025-04-21 15:08:49 -0700 | [diff] [blame] | 328 | messageKey(message: AgentMessage): string { |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 329 | // If the message has tool calls, and any of the tool_calls get a response, we need to |
| 330 | // re-render that message. |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 331 | const toolCallResponses = message.tool_calls |
| 332 | ?.filter((tc) => tc.result_message) |
| 333 | .map((tc) => tc.tool_call_id) |
| 334 | .join("-"); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 335 | return `message-${message.idx}-${toolCallResponses}`; |
| 336 | } |
| 337 | |
| 338 | render() { |
| Philip Zeyliger | 5cf4926 | 2025-04-29 18:35:55 +0000 | [diff] [blame] | 339 | // Check if messages array is empty and render welcome box if it is |
| 340 | if (this.messages.length === 0) { |
| 341 | return html` |
| Josh Bleecher Snyder | 112b923 | 2025-05-23 11:26:33 -0700 | [diff] [blame] | 342 | <div style="position: relative; height: 100%;"> |
| 343 | <div id="scroll-container"> |
| 344 | <div class="welcome-box"> |
| 345 | <h2 class="welcome-box-title">How to use Sketch</h2> |
| 346 | <p class="welcome-box-content"> |
| 347 | Sketch is an agentic coding assistant. |
| 348 | </p> |
| Philip Zeyliger | abc093c | 2025-04-30 10:46:27 -0700 | [diff] [blame] | 349 | |
| Josh Bleecher Snyder | 112b923 | 2025-05-23 11:26:33 -0700 | [diff] [blame] | 350 | <p class="welcome-box-content"> |
| 351 | Sketch has created a container with your repo. |
| 352 | </p> |
| Philip Zeyliger | abc093c | 2025-04-30 10:46:27 -0700 | [diff] [blame] | 353 | |
| Josh Bleecher Snyder | 112b923 | 2025-05-23 11:26:33 -0700 | [diff] [blame] | 354 | <p class="welcome-box-content"> |
| 355 | Ask it to implement a task or answer a question in the chat box |
| Autoformatter | 71c73b5 | 2025-05-29 20:18:43 +0000 | [diff] [blame] | 356 | below. It can edit and run your code, all in the container. |
| 357 | Sketch will create commits in a newly created git branch, which |
| 358 | you can look at and comment on in the Diff tab. Once you're |
| 359 | done, you'll find that branch available in your (original) repo. |
| Josh Bleecher Snyder | 112b923 | 2025-05-23 11:26:33 -0700 | [diff] [blame] | 360 | </p> |
| 361 | <p class="welcome-box-content"> |
| 362 | Because Sketch operates a container per session, you can run |
| Autoformatter | 71c73b5 | 2025-05-29 20:18:43 +0000 | [diff] [blame] | 363 | Sketch in parallel to work on multiple ideas or even the same |
| 364 | idea with different approaches. |
| Josh Bleecher Snyder | 112b923 | 2025-05-23 11:26:33 -0700 | [diff] [blame] | 365 | </p> |
| 366 | </div> |
| Philip Zeyliger | 5cf4926 | 2025-04-29 18:35:55 +0000 | [diff] [blame] | 367 | </div> |
| 368 | </div> |
| 369 | `; |
| 370 | } |
| 371 | |
| 372 | // Otherwise render the regular timeline with messages |
| Philip Zeyliger | 16fa8b4 | 2025-05-02 04:28:16 +0000 | [diff] [blame] | 373 | const isThinking = |
| 374 | this.llmCalls > 0 || (this.toolCalls && this.toolCalls.length > 0); |
| 375 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 376 | return html` |
| Josh Bleecher Snyder | 112b923 | 2025-05-23 11:26:33 -0700 | [diff] [blame] | 377 | <div style="position: relative; height: 100%;"> |
| 378 | <div id="scroll-container"> |
| 379 | <div class="timeline-container"> |
| 380 | ${repeat( |
| 381 | this.messages.filter((msg) => !msg.hide_output), |
| 382 | this.messageKey, |
| 383 | (message, index) => { |
| 384 | let previousMessageIndex = |
| 385 | this.messages.findIndex((m) => m === message) - 1; |
| 386 | let previousMessage = |
| Josh Bleecher Snyder | 4d54493 | 2025-05-07 13:33:53 +0000 | [diff] [blame] | 387 | previousMessageIndex >= 0 |
| 388 | ? this.messages[previousMessageIndex] |
| 389 | : undefined; |
| Josh Bleecher Snyder | 4d54493 | 2025-05-07 13:33:53 +0000 | [diff] [blame] | 390 | |
| Josh Bleecher Snyder | 112b923 | 2025-05-23 11:26:33 -0700 | [diff] [blame] | 391 | // Skip hidden messages when determining previous message |
| 392 | while (previousMessage && previousMessage.hide_output) { |
| 393 | previousMessageIndex--; |
| 394 | previousMessage = |
| 395 | previousMessageIndex >= 0 |
| 396 | ? this.messages[previousMessageIndex] |
| 397 | : undefined; |
| 398 | } |
| 399 | |
| 400 | return html`<sketch-timeline-message |
| 401 | .message=${message} |
| 402 | .previousMessage=${previousMessage} |
| 403 | .open=${false} |
| Philip Zeyliger | b8a8f35 | 2025-06-02 07:39:37 -0700 | [diff] [blame] | 404 | .firstMessageIndex=${this.firstMessageIndex} |
| Josh Bleecher Snyder | 112b923 | 2025-05-23 11:26:33 -0700 | [diff] [blame] | 405 | ></sketch-timeline-message>`; |
| 406 | }, |
| 407 | )} |
| 408 | ${isThinking |
| 409 | ? html` |
| 410 | <div class="thinking-indicator"> |
| 411 | <div class="thinking-bubble"> |
| 412 | <div class="thinking-dots"> |
| 413 | <div class="dot"></div> |
| 414 | <div class="dot"></div> |
| 415 | <div class="dot"></div> |
| 416 | </div> |
| Philip Zeyliger | 16fa8b4 | 2025-05-02 04:28:16 +0000 | [diff] [blame] | 417 | </div> |
| 418 | </div> |
| Josh Bleecher Snyder | 112b923 | 2025-05-23 11:26:33 -0700 | [diff] [blame] | 419 | ` |
| 420 | : ""} |
| 421 | </div> |
| Sean McCullough | 2c5bba4 | 2025-04-20 19:33:17 -0700 | [diff] [blame] | 422 | </div> |
| Josh Bleecher Snyder | 112b923 | 2025-05-23 11:26:33 -0700 | [diff] [blame] | 423 | <div |
| 424 | id="jump-to-latest" |
| 425 | class="${this.scrollingState}" |
| Josh Bleecher Snyder | dee39e0 | 2025-05-29 14:25:08 +0000 | [diff] [blame] | 426 | @click=${this.scrollToBottomWithRetry} |
| Josh Bleecher Snyder | 112b923 | 2025-05-23 11:26:33 -0700 | [diff] [blame] | 427 | > |
| 428 | ⇩ |
| 429 | </div> |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 430 | </div> |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 431 | `; |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | declare global { |
| 436 | interface HTMLElementTagNameMap { |
| 437 | "sketch-timeline": SketchTimeline; |
| 438 | } |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 439 | } |