| 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 | } |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 90 | `; |
| 91 | |
| 92 | constructor() { |
| 93 | super(); |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 94 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 95 | // Binding methods |
| 96 | this._handleShowCommitDiff = this._handleShowCommitDiff.bind(this); |
| Sean McCullough | 2c5bba4 | 2025-04-20 19:33:17 -0700 | [diff] [blame] | 97 | this._handleScroll = this._handleScroll.bind(this); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Scroll to the bottom of the timeline |
| 102 | */ |
| 103 | private scrollToBottom(): void { |
| Pokey Rule | 4097e53 | 2025-04-24 18:55:28 +0100 | [diff] [blame] | 104 | this.scrollContainer.value?.scrollTo({ |
| 105 | top: this.scrollContainer.value?.scrollHeight, |
| Sean McCullough | 2c5bba4 | 2025-04-20 19:33:17 -0700 | [diff] [blame] | 106 | behavior: "smooth", |
| 107 | }); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Called after the component's properties have been updated |
| 112 | */ |
| 113 | updated(changedProperties: PropertyValues): void { |
| 114 | // If messages have changed, scroll to bottom if needed |
| 115 | if (changedProperties.has("messages") && this.messages.length > 0) { |
| 116 | if (this.scrollingState == "pinToLatest") { |
| 117 | setTimeout(() => this.scrollToBottom(), 50); |
| 118 | } |
| 119 | } |
| 120 | if (changedProperties.has("scrollContainer")) { |
| Pokey Rule | 4097e53 | 2025-04-24 18:55:28 +0100 | [diff] [blame] | 121 | this.scrollContainer.value?.addEventListener( |
| 122 | "scroll", |
| 123 | this._handleScroll, |
| 124 | ); |
| Sean McCullough | 2c5bba4 | 2025-04-20 19:33:17 -0700 | [diff] [blame] | 125 | } |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 126 | } |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 127 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 128 | /** |
| 129 | * Handle showCommitDiff event |
| 130 | */ |
| 131 | private _handleShowCommitDiff(event: CustomEvent) { |
| 132 | const { commitHash } = event.detail; |
| 133 | if (commitHash) { |
| 134 | // Bubble up the event to the app shell |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 135 | const newEvent = new CustomEvent("show-commit-diff", { |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 136 | detail: { commitHash }, |
| 137 | bubbles: true, |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 138 | composed: true, |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 139 | }); |
| 140 | this.dispatchEvent(newEvent); |
| 141 | } |
| 142 | } |
| 143 | |
| Sean McCullough | 2c5bba4 | 2025-04-20 19:33:17 -0700 | [diff] [blame] | 144 | private _handleScroll(event) { |
| 145 | const isAtBottom = |
| 146 | Math.abs( |
| Pokey Rule | 4097e53 | 2025-04-24 18:55:28 +0100 | [diff] [blame] | 147 | this.scrollContainer.value.scrollHeight - |
| 148 | this.scrollContainer.value.clientHeight - |
| 149 | this.scrollContainer.value.scrollTop, |
| Sean McCullough | 2c5bba4 | 2025-04-20 19:33:17 -0700 | [diff] [blame] | 150 | ) <= 1; |
| 151 | if (isAtBottom) { |
| 152 | this.scrollingState = "pinToLatest"; |
| 153 | } else { |
| 154 | // TODO: does scroll direction matter here? |
| 155 | this.scrollingState = "floating"; |
| 156 | } |
| 157 | } |
| 158 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 159 | // See https://lit.dev/docs/components/lifecycle/ |
| 160 | connectedCallback() { |
| 161 | super.connectedCallback(); |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 162 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 163 | // Listen for showCommitDiff events from the renderer |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 164 | document.addEventListener( |
| 165 | "showCommitDiff", |
| Philip Zeyliger | 72682df | 2025-04-23 13:09:46 -0700 | [diff] [blame] | 166 | this._handleShowCommitDiff as EventListener, |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 167 | ); |
| Pokey Rule | 4097e53 | 2025-04-24 18:55:28 +0100 | [diff] [blame] | 168 | |
| 169 | this.scrollContainer.value?.addEventListener("scroll", this._handleScroll); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | // See https://lit.dev/docs/components/lifecycle/ |
| 173 | disconnectedCallback() { |
| 174 | super.disconnectedCallback(); |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 175 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 176 | // Remove event listeners |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 177 | document.removeEventListener( |
| 178 | "showCommitDiff", |
| Philip Zeyliger | 72682df | 2025-04-23 13:09:46 -0700 | [diff] [blame] | 179 | this._handleShowCommitDiff as EventListener, |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 180 | ); |
| Sean McCullough | 2c5bba4 | 2025-04-20 19:33:17 -0700 | [diff] [blame] | 181 | |
| Pokey Rule | 4097e53 | 2025-04-24 18:55:28 +0100 | [diff] [blame] | 182 | this.scrollContainer.value?.removeEventListener( |
| 183 | "scroll", |
| 184 | this._handleScroll, |
| 185 | ); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| Sean McCullough | d9f1337 | 2025-04-21 15:08:49 -0700 | [diff] [blame] | 188 | // 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] | 189 | // 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] | 190 | messageKey(message: AgentMessage): string { |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 191 | // If the message has tool calls, and any of the tool_calls get a response, we need to |
| 192 | // re-render that message. |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 193 | const toolCallResponses = message.tool_calls |
| 194 | ?.filter((tc) => tc.result_message) |
| 195 | .map((tc) => tc.tool_call_id) |
| 196 | .join("-"); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 197 | return `message-${message.idx}-${toolCallResponses}`; |
| 198 | } |
| 199 | |
| 200 | render() { |
| 201 | return html` |
| Sean McCullough | 2c5bba4 | 2025-04-20 19:33:17 -0700 | [diff] [blame] | 202 | <div id="scroll-container"> |
| 203 | <div class="timeline-container"> |
| 204 | ${repeat(this.messages, this.messageKey, (message, index) => { |
| Sean McCullough | d9f1337 | 2025-04-21 15:08:49 -0700 | [diff] [blame] | 205 | let previousMessage: AgentMessage; |
| Sean McCullough | 2c5bba4 | 2025-04-20 19:33:17 -0700 | [diff] [blame] | 206 | if (index > 0) { |
| 207 | previousMessage = this.messages[index - 1]; |
| 208 | } |
| 209 | return html`<sketch-timeline-message |
| 210 | .message=${message} |
| 211 | .previousMessage=${previousMessage} |
| Sean McCullough | 2deac84 | 2025-04-21 18:17:57 -0700 | [diff] [blame] | 212 | .open=${index == this.messages.length - 1} |
| Sean McCullough | 2c5bba4 | 2025-04-20 19:33:17 -0700 | [diff] [blame] | 213 | ></sketch-timeline-message>`; |
| 214 | })} |
| 215 | </div> |
| 216 | </div> |
| 217 | <div |
| 218 | id="jump-to-latest" |
| 219 | class="${this.scrollingState}" |
| 220 | @click=${this.scrollToBottom} |
| 221 | > |
| 222 | ⇩ |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 223 | </div> |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 224 | `; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | declare global { |
| 229 | interface HTMLElementTagNameMap { |
| 230 | "sketch-timeline": SketchTimeline; |
| 231 | } |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 232 | } |