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