| 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 | |
| Sean McCullough | 2c5bba4 | 2025-04-20 19:33:17 -0700 | [diff] [blame] | 14 | // Track if we should scroll to the bottom |
| 15 | @state() |
| 16 | private scrollingState: "pinToLatest" | "floating" = "pinToLatest"; |
| 17 | |
| Pokey Rule | e2a8c2f | 2025-04-23 15:09:25 +0100 | [diff] [blame] | 18 | @property({ attribute: false }) |
| Pokey Rule | 4097e53 | 2025-04-24 18:55:28 +0100 | [diff] [blame] | 19 | scrollContainer: Ref<HTMLElement>; |
| Sean McCullough | 2c5bba4 | 2025-04-20 19:33:17 -0700 | [diff] [blame] | 20 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 21 | static styles = css` |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 22 | /* Hide views initially to prevent flash of content */ |
| 23 | .timeline-container .timeline, |
| 24 | .timeline-container .diff-view, |
| 25 | .timeline-container .chart-view, |
| 26 | .timeline-container .terminal-view { |
| 27 | visibility: hidden; |
| 28 | } |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 29 | |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 30 | /* Will be set by JavaScript once we know which view to display */ |
| 31 | .timeline-container.view-initialized .timeline, |
| 32 | .timeline-container.view-initialized .diff-view, |
| 33 | .timeline-container.view-initialized .chart-view, |
| 34 | .timeline-container.view-initialized .terminal-view { |
| 35 | visibility: visible; |
| 36 | } |
| 37 | |
| 38 | .timeline-container { |
| 39 | width: 100%; |
| 40 | position: relative; |
| 41 | } |
| 42 | |
| 43 | /* Timeline styles that should remain unchanged */ |
| 44 | .timeline { |
| 45 | position: relative; |
| 46 | margin: 10px 0; |
| 47 | scroll-behavior: smooth; |
| 48 | } |
| 49 | |
| 50 | .timeline::before { |
| 51 | content: ""; |
| 52 | position: absolute; |
| 53 | top: 0; |
| 54 | bottom: 0; |
| 55 | left: 15px; |
| 56 | width: 2px; |
| 57 | background: #e0e0e0; |
| 58 | border-radius: 1px; |
| 59 | } |
| 60 | |
| 61 | /* Hide the timeline vertical line when there are no messages */ |
| 62 | .timeline.empty::before { |
| 63 | display: none; |
| 64 | } |
| Sean McCullough | 2c5bba4 | 2025-04-20 19:33:17 -0700 | [diff] [blame] | 65 | |
| 66 | #scroll-container { |
| 67 | overflow: auto; |
| 68 | padding-left: 1em; |
| 69 | } |
| 70 | #jump-to-latest { |
| 71 | display: none; |
| 72 | position: fixed; |
| 73 | bottom: 100px; |
| 74 | right: 0; |
| 75 | background: rgb(33, 150, 243); |
| 76 | color: white; |
| 77 | border-radius: 8px; |
| 78 | padding: 0.5em; |
| 79 | margin: 0.5em; |
| 80 | font-size: x-large; |
| 81 | opacity: 0.5; |
| 82 | cursor: pointer; |
| 83 | } |
| 84 | #jump-to-latest:hover { |
| 85 | opacity: 1; |
| 86 | } |
| 87 | #jump-to-latest.floating { |
| 88 | display: block; |
| 89 | } |
| Philip Zeyliger | 5cf4926 | 2025-04-29 18:35:55 +0000 | [diff] [blame] | 90 | |
| 91 | /* Welcome box styles for the empty chat state */ |
| 92 | .welcome-box { |
| 93 | margin: 2rem auto; |
| 94 | max-width: 80%; |
| 95 | padding: 2rem; |
| 96 | border: 2px solid #e0e0e0; |
| 97 | border-radius: 8px; |
| 98 | box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05); |
| 99 | background-color: #ffffff; |
| 100 | text-align: center; |
| 101 | } |
| 102 | |
| 103 | .welcome-box-title { |
| 104 | font-size: 1.5rem; |
| 105 | font-weight: 600; |
| 106 | margin-bottom: 1.5rem; |
| Philip Zeyliger | abc093c | 2025-04-30 10:46:27 -0700 | [diff] [blame] | 107 | text-align: center; |
| Philip Zeyliger | 5cf4926 | 2025-04-29 18:35:55 +0000 | [diff] [blame] | 108 | color: #333; |
| 109 | } |
| 110 | |
| 111 | .welcome-box-content { |
| 112 | color: #666; /* Slightly grey font color */ |
| 113 | line-height: 1.6; |
| 114 | font-size: 1rem; |
| Philip Zeyliger | abc093c | 2025-04-30 10:46:27 -0700 | [diff] [blame] | 115 | text-align: left; |
| Philip Zeyliger | 5cf4926 | 2025-04-29 18:35:55 +0000 | [diff] [blame] | 116 | } |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 117 | `; |
| 118 | |
| 119 | constructor() { |
| 120 | super(); |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 121 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 122 | // Binding methods |
| 123 | this._handleShowCommitDiff = this._handleShowCommitDiff.bind(this); |
| Sean McCullough | 2c5bba4 | 2025-04-20 19:33:17 -0700 | [diff] [blame] | 124 | this._handleScroll = this._handleScroll.bind(this); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Scroll to the bottom of the timeline |
| 129 | */ |
| 130 | private scrollToBottom(): void { |
| Pokey Rule | 4097e53 | 2025-04-24 18:55:28 +0100 | [diff] [blame] | 131 | this.scrollContainer.value?.scrollTo({ |
| 132 | top: this.scrollContainer.value?.scrollHeight, |
| Sean McCullough | 2c5bba4 | 2025-04-20 19:33:17 -0700 | [diff] [blame] | 133 | behavior: "smooth", |
| 134 | }); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Called after the component's properties have been updated |
| 139 | */ |
| 140 | updated(changedProperties: PropertyValues): void { |
| 141 | // If messages have changed, scroll to bottom if needed |
| 142 | if (changedProperties.has("messages") && this.messages.length > 0) { |
| 143 | if (this.scrollingState == "pinToLatest") { |
| 144 | setTimeout(() => this.scrollToBottom(), 50); |
| 145 | } |
| 146 | } |
| 147 | if (changedProperties.has("scrollContainer")) { |
| Pokey Rule | 4097e53 | 2025-04-24 18:55:28 +0100 | [diff] [blame] | 148 | this.scrollContainer.value?.addEventListener( |
| 149 | "scroll", |
| 150 | this._handleScroll, |
| 151 | ); |
| Sean McCullough | 2c5bba4 | 2025-04-20 19:33:17 -0700 | [diff] [blame] | 152 | } |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 153 | } |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 154 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 155 | /** |
| 156 | * Handle showCommitDiff event |
| 157 | */ |
| 158 | private _handleShowCommitDiff(event: CustomEvent) { |
| 159 | const { commitHash } = event.detail; |
| 160 | if (commitHash) { |
| 161 | // Bubble up the event to the app shell |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 162 | const newEvent = new CustomEvent("show-commit-diff", { |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 163 | detail: { commitHash }, |
| 164 | bubbles: true, |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 165 | composed: true, |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 166 | }); |
| 167 | this.dispatchEvent(newEvent); |
| 168 | } |
| 169 | } |
| 170 | |
| Sean McCullough | 2c5bba4 | 2025-04-20 19:33:17 -0700 | [diff] [blame] | 171 | private _handleScroll(event) { |
| 172 | const isAtBottom = |
| 173 | Math.abs( |
| Pokey Rule | 4097e53 | 2025-04-24 18:55:28 +0100 | [diff] [blame] | 174 | this.scrollContainer.value.scrollHeight - |
| 175 | this.scrollContainer.value.clientHeight - |
| 176 | this.scrollContainer.value.scrollTop, |
| Sean McCullough | 2c5bba4 | 2025-04-20 19:33:17 -0700 | [diff] [blame] | 177 | ) <= 1; |
| 178 | if (isAtBottom) { |
| 179 | this.scrollingState = "pinToLatest"; |
| 180 | } else { |
| 181 | // TODO: does scroll direction matter here? |
| 182 | this.scrollingState = "floating"; |
| 183 | } |
| 184 | } |
| 185 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 186 | // See https://lit.dev/docs/components/lifecycle/ |
| 187 | connectedCallback() { |
| 188 | super.connectedCallback(); |
| 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 | // Listen for showCommitDiff events from the renderer |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 191 | document.addEventListener( |
| 192 | "showCommitDiff", |
| Philip Zeyliger | 72682df | 2025-04-23 13:09:46 -0700 | [diff] [blame] | 193 | this._handleShowCommitDiff as EventListener, |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 194 | ); |
| Pokey Rule | 4097e53 | 2025-04-24 18:55:28 +0100 | [diff] [blame] | 195 | |
| 196 | this.scrollContainer.value?.addEventListener("scroll", this._handleScroll); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | // See https://lit.dev/docs/components/lifecycle/ |
| 200 | disconnectedCallback() { |
| 201 | super.disconnectedCallback(); |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 202 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 203 | // Remove event listeners |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 204 | document.removeEventListener( |
| 205 | "showCommitDiff", |
| Philip Zeyliger | 72682df | 2025-04-23 13:09:46 -0700 | [diff] [blame] | 206 | this._handleShowCommitDiff as EventListener, |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 207 | ); |
| Sean McCullough | 2c5bba4 | 2025-04-20 19:33:17 -0700 | [diff] [blame] | 208 | |
| Pokey Rule | 4097e53 | 2025-04-24 18:55:28 +0100 | [diff] [blame] | 209 | this.scrollContainer.value?.removeEventListener( |
| 210 | "scroll", |
| 211 | this._handleScroll, |
| 212 | ); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| Sean McCullough | d9f1337 | 2025-04-21 15:08:49 -0700 | [diff] [blame] | 215 | // 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] | 216 | // 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] | 217 | messageKey(message: AgentMessage): string { |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 218 | // If the message has tool calls, and any of the tool_calls get a response, we need to |
| 219 | // re-render that message. |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 220 | const toolCallResponses = message.tool_calls |
| 221 | ?.filter((tc) => tc.result_message) |
| 222 | .map((tc) => tc.tool_call_id) |
| 223 | .join("-"); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 224 | return `message-${message.idx}-${toolCallResponses}`; |
| 225 | } |
| 226 | |
| 227 | render() { |
| Philip Zeyliger | 5cf4926 | 2025-04-29 18:35:55 +0000 | [diff] [blame] | 228 | // Check if messages array is empty and render welcome box if it is |
| 229 | if (this.messages.length === 0) { |
| 230 | return html` |
| 231 | <div id="scroll-container"> |
| 232 | <div class="welcome-box"> |
| 233 | <h2 class="welcome-box-title">How to use Sketch</h2> |
| 234 | <p class="welcome-box-content"> |
| Philip Zeyliger | abc093c | 2025-04-30 10:46:27 -0700 | [diff] [blame] | 235 | Sketch is an agentic coding assistant. |
| 236 | </p> |
| 237 | |
| 238 | <p class="welcome-box-content"> |
| 239 | Sketch has created a container with your repo. |
| 240 | </p> |
| 241 | |
| 242 | <p class="welcome-box-content"> |
| 243 | Ask it to implement a task or answer a question in the chat box |
| 244 | below. Sketch has created a container with your repo, and it can |
| 245 | edit and run your code in the container. Sketch will create |
| 246 | commits, which you can look at and review with comments in the |
| 247 | Diff tab. Once you're done, you'll find the changes ready to go in |
| 248 | a |
| 249 | <code>sketch/*</code> branch. |
| 250 | </p> |
| 251 | <p class="welcome-box-content"> |
| 252 | Because Sketch operates a container per session, you can run |
| 253 | Sketch in parallel to work on multiple ideas or even the same idea |
| 254 | with different approaches. |
| Philip Zeyliger | 5cf4926 | 2025-04-29 18:35:55 +0000 | [diff] [blame] | 255 | </p> |
| 256 | </div> |
| 257 | </div> |
| 258 | `; |
| 259 | } |
| 260 | |
| 261 | // Otherwise render the regular timeline with messages |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 262 | return html` |
| Sean McCullough | 2c5bba4 | 2025-04-20 19:33:17 -0700 | [diff] [blame] | 263 | <div id="scroll-container"> |
| 264 | <div class="timeline-container"> |
| 265 | ${repeat(this.messages, this.messageKey, (message, index) => { |
| Sean McCullough | d9f1337 | 2025-04-21 15:08:49 -0700 | [diff] [blame] | 266 | let previousMessage: AgentMessage; |
| Sean McCullough | 2c5bba4 | 2025-04-20 19:33:17 -0700 | [diff] [blame] | 267 | if (index > 0) { |
| 268 | previousMessage = this.messages[index - 1]; |
| 269 | } |
| 270 | return html`<sketch-timeline-message |
| 271 | .message=${message} |
| 272 | .previousMessage=${previousMessage} |
| Philip Zeyliger | d20ded5 | 2025-04-30 17:38:36 +0000 | [diff] [blame^] | 273 | .open=${false} |
| Sean McCullough | 2c5bba4 | 2025-04-20 19:33:17 -0700 | [diff] [blame] | 274 | ></sketch-timeline-message>`; |
| 275 | })} |
| 276 | </div> |
| 277 | </div> |
| 278 | <div |
| 279 | id="jump-to-latest" |
| 280 | class="${this.scrollingState}" |
| 281 | @click=${this.scrollToBottom} |
| 282 | > |
| 283 | ⇩ |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 284 | </div> |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 285 | `; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | declare global { |
| 290 | interface HTMLElementTagNameMap { |
| 291 | "sketch-timeline": SketchTimeline; |
| 292 | } |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 293 | } |