| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 1 | import { css, html, LitElement } from "lit"; |
| 2 | import { customElement, property, state } from "lit/decorators.js"; |
| Pokey Rule | 4097e53 | 2025-04-24 18:55:28 +0100 | [diff] [blame] | 3 | import { ConnectionStatus, DataManager } from "../data"; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 4 | import { AgentMessage, GitLogEntry, State } from "../types"; |
| Pokey Rule | e2a8c2f | 2025-04-23 15:09:25 +0100 | [diff] [blame] | 5 | import { aggregateAgentMessages } from "./aggregateAgentMessages"; |
| Philip Zeyliger | 2d4c48f | 2025-05-02 23:35:03 +0000 | [diff] [blame] | 6 | |
| Pokey Rule | 4097e53 | 2025-04-24 18:55:28 +0100 | [diff] [blame] | 7 | import "./sketch-chat-input"; |
| 8 | import "./sketch-container-status"; |
| Philip Zeyliger | 00bcaef | 2025-05-30 04:21:15 +0000 | [diff] [blame] | 9 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 10 | import "./sketch-diff2-view"; |
| 11 | import { SketchDiff2View } from "./sketch-diff2-view"; |
| 12 | import { DefaultGitDataService } from "./git-data-service"; |
| 13 | import "./sketch-monaco-view"; |
| Pokey Rule | 4097e53 | 2025-04-24 18:55:28 +0100 | [diff] [blame] | 14 | import "./sketch-network-status"; |
| Philip Zeyliger | 99a9a02 | 2025-04-27 15:15:25 +0000 | [diff] [blame] | 15 | import "./sketch-call-status"; |
| Pokey Rule | 4097e53 | 2025-04-24 18:55:28 +0100 | [diff] [blame] | 16 | import "./sketch-terminal"; |
| 17 | import "./sketch-timeline"; |
| 18 | import "./sketch-view-mode-select"; |
| Josh Bleecher Snyder | 112b923 | 2025-05-23 11:26:33 -0700 | [diff] [blame] | 19 | import "./sketch-todo-panel"; |
| Pokey Rule | 4097e53 | 2025-04-24 18:55:28 +0100 | [diff] [blame] | 20 | |
| 21 | import { createRef, ref } from "lit/directives/ref.js"; |
| Sean McCullough | 485afc6 | 2025-04-28 14:28:39 -0700 | [diff] [blame] | 22 | import { SketchChatInput } from "./sketch-chat-input"; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 23 | |
| Philip Zeyliger | 00bcaef | 2025-05-30 04:21:15 +0000 | [diff] [blame] | 24 | type ViewMode = "chat" | "diff2" | "terminal"; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 25 | |
| 26 | @customElement("sketch-app-shell") |
| 27 | export class SketchAppShell extends LitElement { |
| Philip Zeyliger | 2d4c48f | 2025-05-02 23:35:03 +0000 | [diff] [blame] | 28 | // Current view mode (chat, diff, terminal) |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 29 | @state() |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 30 | viewMode: ViewMode = "chat"; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 31 | |
| 32 | // Current commit hash for diff view |
| 33 | @state() |
| 34 | currentCommitHash: string = ""; |
| 35 | |
| Philip Zeyliger | 47b71c9 | 2025-04-30 15:43:39 +0000 | [diff] [blame] | 36 | // Last commit information |
| 37 | @state() |
| Philip Zeyliger | 16fa8b4 | 2025-05-02 04:28:16 +0000 | [diff] [blame] | 38 | |
| 39 | // Reference to the container status element |
| 40 | containerStatusElement: any = null; |
| Philip Zeyliger | 47b71c9 | 2025-04-30 15:43:39 +0000 | [diff] [blame] | 41 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 42 | // See https://lit.dev/docs/components/styles/ for how lit-element handles CSS. |
| 43 | // Note that these styles only apply to the scope of this web component's |
| 44 | // shadow DOM node, so they won't leak out or collide with CSS declared in |
| 45 | // other components or the containing web page (...unless you want it to do that). |
| 46 | static styles = css` |
| Philip Zeyliger | 47b71c9 | 2025-04-30 15:43:39 +0000 | [diff] [blame] | 47 | .copied-indicator { |
| 48 | position: absolute; |
| 49 | top: -20px; |
| 50 | left: 50%; |
| 51 | transform: translateX(-50%); |
| 52 | background: rgba(40, 167, 69, 0.9); |
| 53 | color: white; |
| 54 | padding: 2px 6px; |
| 55 | border-radius: 3px; |
| 56 | font-size: 10px; |
| 57 | font-family: system-ui, sans-serif; |
| 58 | animation: fadeInOut 2s ease; |
| 59 | pointer-events: none; |
| 60 | } |
| Autoformatter | cf57096 | 2025-04-30 17:27:39 +0000 | [diff] [blame] | 61 | |
| Philip Zeyliger | 47b71c9 | 2025-04-30 15:43:39 +0000 | [diff] [blame] | 62 | @keyframes fadeInOut { |
| Autoformatter | cf57096 | 2025-04-30 17:27:39 +0000 | [diff] [blame] | 63 | 0% { |
| 64 | opacity: 0; |
| 65 | } |
| 66 | 20% { |
| 67 | opacity: 1; |
| 68 | } |
| 69 | 80% { |
| 70 | opacity: 1; |
| 71 | } |
| 72 | 100% { |
| 73 | opacity: 0; |
| 74 | } |
| Philip Zeyliger | 47b71c9 | 2025-04-30 15:43:39 +0000 | [diff] [blame] | 75 | } |
| Autoformatter | cf57096 | 2025-04-30 17:27:39 +0000 | [diff] [blame] | 76 | |
| Philip Zeyliger | 47b71c9 | 2025-04-30 15:43:39 +0000 | [diff] [blame] | 77 | .commit-branch-indicator { |
| 78 | color: #28a745; |
| 79 | } |
| Autoformatter | cf57096 | 2025-04-30 17:27:39 +0000 | [diff] [blame] | 80 | |
| Philip Zeyliger | 47b71c9 | 2025-04-30 15:43:39 +0000 | [diff] [blame] | 81 | .commit-hash-indicator { |
| 82 | color: #0366d6; |
| 83 | } |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 84 | :host { |
| 85 | display: block; |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 86 | font-family: |
| 87 | system-ui, |
| 88 | -apple-system, |
| 89 | BlinkMacSystemFont, |
| 90 | "Segoe UI", |
| 91 | Roboto, |
| 92 | sans-serif; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 93 | color: #333; |
| 94 | line-height: 1.4; |
| Pokey Rule | 4097e53 | 2025-04-24 18:55:28 +0100 | [diff] [blame] | 95 | height: 100vh; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 96 | width: 100%; |
| 97 | position: relative; |
| 98 | overflow-x: hidden; |
| Pokey Rule | 4097e53 | 2025-04-24 18:55:28 +0100 | [diff] [blame] | 99 | display: flex; |
| 100 | flex-direction: column; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /* Top banner with combined elements */ |
| Pokey Rule | 4097e53 | 2025-04-24 18:55:28 +0100 | [diff] [blame] | 104 | #top-banner { |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 105 | display: flex; |
| Philip Zeyliger | e66db3e | 2025-04-27 15:40:39 +0000 | [diff] [blame] | 106 | align-self: stretch; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 107 | justify-content: space-between; |
| 108 | align-items: center; |
| Philip Zeyliger | e66db3e | 2025-04-27 15:40:39 +0000 | [diff] [blame] | 109 | padding: 0 20px; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 110 | margin-bottom: 0; |
| 111 | border-bottom: 1px solid #eee; |
| Philip Zeyliger | e66db3e | 2025-04-27 15:40:39 +0000 | [diff] [blame] | 112 | gap: 20px; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 113 | background: white; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 114 | box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); |
| Philip Zeyliger | e66db3e | 2025-04-27 15:40:39 +0000 | [diff] [blame] | 115 | width: 100%; |
| 116 | height: 48px; |
| 117 | padding-right: 30px; /* Extra padding on the right to prevent elements from hitting the edge */ |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| Pokey Rule | 4097e53 | 2025-04-24 18:55:28 +0100 | [diff] [blame] | 120 | /* View mode container styles - mirroring timeline.css structure */ |
| 121 | #view-container { |
| 122 | align-self: stretch; |
| 123 | overflow-y: auto; |
| 124 | flex: 1; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 125 | display: flex; |
| 126 | flex-direction: column; |
| 127 | min-height: 0; /* Critical for proper flex child behavior */ |
| Pokey Rule | 4097e53 | 2025-04-24 18:55:28 +0100 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | #view-container-inner { |
| 131 | max-width: 1200px; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 132 | width: calc(100% - 40px); |
| Pokey Rule | 4097e53 | 2025-04-24 18:55:28 +0100 | [diff] [blame] | 133 | margin: 0 auto; |
| 134 | position: relative; |
| 135 | padding-bottom: 10px; |
| 136 | padding-top: 10px; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 137 | display: flex; |
| 138 | flex-direction: column; |
| 139 | height: 100%; /* Ensure it takes full height of parent */ |
| Pokey Rule | 4097e53 | 2025-04-24 18:55:28 +0100 | [diff] [blame] | 140 | } |
| 141 | |
| Josh Bleecher Snyder | 112b923 | 2025-05-23 11:26:33 -0700 | [diff] [blame] | 142 | /* Adjust view container when todo panel is visible in chat mode */ |
| 143 | #view-container-inner.with-todo-panel { |
| 144 | max-width: none; |
| 145 | width: 100%; |
| 146 | margin: 0; |
| 147 | padding-left: 20px; |
| 148 | padding-right: 20px; |
| 149 | } |
| 150 | |
| Pokey Rule | 4097e53 | 2025-04-24 18:55:28 +0100 | [diff] [blame] | 151 | #chat-input { |
| 152 | align-self: flex-end; |
| 153 | width: 100%; |
| 154 | box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.1); |
| 155 | } |
| 156 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 157 | .banner-title { |
| 158 | font-size: 18px; |
| 159 | font-weight: 600; |
| 160 | margin: 0; |
| 161 | min-width: 6em; |
| 162 | white-space: nowrap; |
| 163 | overflow: hidden; |
| 164 | text-overflow: ellipsis; |
| 165 | } |
| 166 | |
| 167 | .chat-title { |
| 168 | margin: 0; |
| 169 | padding: 0; |
| 170 | color: rgba(82, 82, 82, 0.85); |
| Josh Bleecher Snyder | eb5166a | 2025-04-30 17:04:20 +0000 | [diff] [blame] | 171 | font-size: 14px; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 172 | font-weight: normal; |
| 173 | font-style: italic; |
| 174 | white-space: nowrap; |
| 175 | overflow: hidden; |
| 176 | text-overflow: ellipsis; |
| 177 | } |
| 178 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 179 | /* Allow the container to expand to full width and height in diff mode */ |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 180 | #view-container-inner.diff2-active { |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 181 | max-width: 100%; |
| Pokey Rule | 46fff97 | 2025-04-25 14:57:44 +0100 | [diff] [blame] | 182 | width: 100%; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 183 | height: 100%; |
| 184 | padding: 0; /* Remove padding for more space */ |
| 185 | display: flex; |
| 186 | flex-direction: column; |
| 187 | flex: 1; |
| 188 | min-height: 0; /* Critical for flex behavior */ |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | /* Individual view styles */ |
| 192 | .chat-view, |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 193 | .diff2-view, |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 194 | .terminal-view { |
| 195 | display: none; /* Hidden by default */ |
| 196 | width: 100%; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 197 | height: 100%; |
| 198 | } |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 199 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 200 | /* Make chat view take full width available */ |
| 201 | .chat-view.view-active { |
| 202 | display: flex; |
| 203 | flex-direction: column; |
| 204 | width: 100%; |
| Josh Bleecher Snyder | 112b923 | 2025-05-23 11:26:33 -0700 | [diff] [blame] | 205 | height: 100%; |
| 206 | } |
| 207 | |
| 208 | /* Chat timeline container - takes full width, memory panel will be positioned separately */ |
| 209 | .chat-timeline-container { |
| 210 | flex: 1; |
| 211 | display: flex; |
| 212 | flex-direction: column; |
| 213 | width: 100%; |
| 214 | height: 100%; |
| 215 | margin-right: 0; /* Default - no memory panel */ |
| 216 | transition: margin-right 0.2s ease; /* Smooth transition */ |
| 217 | } |
| 218 | |
| 219 | /* Adjust chat timeline container when todo panel is visible */ |
| 220 | .chat-timeline-container.with-todo-panel { |
| 221 | margin-right: 400px; /* Make space for fixed todo panel */ |
| 222 | width: calc(100% - 400px); /* Explicitly set width to prevent overlap */ |
| 223 | } |
| 224 | |
| 225 | /* Todo panel container - fixed to right side */ |
| 226 | .todo-panel-container { |
| 227 | position: fixed; |
| 228 | top: 48px; /* Below top banner */ |
| 229 | right: 15px; /* Leave space for scroll bar */ |
| 230 | width: 400px; |
| 231 | bottom: var( |
| 232 | --chat-input-height, |
| 233 | 90px |
| 234 | ); /* Dynamic height based on chat input size */ |
| 235 | background-color: #fafafa; |
| 236 | border-left: 1px solid #e0e0e0; |
| 237 | z-index: 100; |
| 238 | display: none; /* Hidden by default */ |
| 239 | transition: bottom 0.2s ease; /* Smooth transition when height changes */ |
| 240 | /* Add fuzzy gradient at bottom to blend with text entry */ |
| 241 | background: linear-gradient( |
| 242 | to bottom, |
| 243 | #fafafa 0%, |
| 244 | #fafafa 90%, |
| 245 | rgba(250, 250, 250, 0.5) 95%, |
| 246 | rgba(250, 250, 250, 0.2) 100% |
| 247 | ); |
| 248 | } |
| 249 | |
| 250 | .todo-panel-container.visible { |
| 251 | display: block; |
| 252 | } |
| 253 | |
| 254 | /* Responsive adjustments for todo panel */ |
| 255 | @media (max-width: 1200px) { |
| 256 | .todo-panel-container { |
| 257 | width: 350px; |
| 258 | /* bottom is still controlled by --chat-input-height CSS variable */ |
| 259 | } |
| 260 | .chat-timeline-container.with-todo-panel { |
| 261 | margin-right: 350px; |
| 262 | width: calc(100% - 350px); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | @media (max-width: 900px) { |
| 267 | .todo-panel-container { |
| 268 | width: 300px; |
| 269 | /* bottom is still controlled by --chat-input-height CSS variable */ |
| 270 | } |
| 271 | .chat-timeline-container.with-todo-panel { |
| 272 | margin-right: 300px; |
| 273 | width: calc(100% - 300px); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | /* On very small screens, hide todo panel or make it overlay */ |
| 278 | @media (max-width: 768px) { |
| 279 | .todo-panel-container.visible { |
| 280 | display: none; /* Hide on mobile */ |
| 281 | } |
| 282 | .chat-timeline-container.with-todo-panel { |
| 283 | margin-right: 0; |
| 284 | width: 100%; |
| 285 | } |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 286 | } |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 287 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 288 | /* Monaco diff2 view needs to take all available space */ |
| 289 | .diff2-view.view-active { |
| 290 | flex: 1; |
| 291 | overflow: hidden; |
| 292 | min-height: 0; /* Required for proper flex child behavior */ |
| 293 | display: flex; |
| 294 | flex-direction: column; |
| 295 | height: 100%; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | /* Active view styles - these will be applied via JavaScript */ |
| 299 | .view-active { |
| 300 | display: flex; |
| 301 | flex-direction: column; |
| 302 | } |
| 303 | |
| 304 | .title-container { |
| 305 | display: flex; |
| 306 | flex-direction: column; |
| 307 | white-space: nowrap; |
| 308 | overflow: hidden; |
| 309 | text-overflow: ellipsis; |
| Josh Bleecher Snyder | eb5166a | 2025-04-30 17:04:20 +0000 | [diff] [blame] | 310 | max-width: 30%; |
| Philip Zeyliger | e66db3e | 2025-04-27 15:40:39 +0000 | [diff] [blame] | 311 | padding: 6px 0; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | .refresh-control { |
| 315 | display: flex; |
| 316 | align-items: center; |
| 317 | margin-bottom: 0; |
| 318 | flex-wrap: nowrap; |
| 319 | white-space: nowrap; |
| 320 | flex-shrink: 0; |
| Philip Zeyliger | e66db3e | 2025-04-27 15:40:39 +0000 | [diff] [blame] | 321 | gap: 15px; |
| 322 | padding-left: 15px; |
| 323 | margin-right: 50px; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 324 | } |
| 325 | |
| Pokey Rule | 397871d | 2025-05-19 15:02:45 +0100 | [diff] [blame] | 326 | .stop-button, |
| 327 | .end-button { |
| Philip Zeyliger | 2c4db09 | 2025-04-28 16:57:50 -0700 | [diff] [blame] | 328 | background: #2196f3; |
| 329 | color: white; |
| 330 | border: none; |
| 331 | padding: 4px 10px; |
| 332 | border-radius: 4px; |
| 333 | cursor: pointer; |
| 334 | font-size: 12px; |
| 335 | margin-right: 5px; |
| Philip Zeyliger | bce3a13 | 2025-04-30 22:03:39 +0000 | [diff] [blame] | 336 | display: flex; |
| 337 | align-items: center; |
| 338 | gap: 6px; |
| Philip Zeyliger | 2c4db09 | 2025-04-28 16:57:50 -0700 | [diff] [blame] | 339 | } |
| 340 | |
| Philip Zeyliger | bce3a13 | 2025-04-30 22:03:39 +0000 | [diff] [blame] | 341 | .stop-button { |
| 342 | background: #dc3545; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 343 | color: white; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 344 | } |
| 345 | |
| Philip Zeyliger | bce3a13 | 2025-04-30 22:03:39 +0000 | [diff] [blame] | 346 | .stop-button:hover:not(:disabled) { |
| 347 | background-color: #c82333; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 348 | } |
| 349 | |
| Philip Zeyliger | bce3a13 | 2025-04-30 22:03:39 +0000 | [diff] [blame] | 350 | .stop-button:disabled { |
| 351 | background-color: #e9a8ad; |
| 352 | cursor: not-allowed; |
| 353 | opacity: 0.7; |
| Philip Zeyliger | db5e9b4 | 2025-04-30 19:58:13 +0000 | [diff] [blame] | 354 | } |
| 355 | |
| Philip Zeyliger | bce3a13 | 2025-04-30 22:03:39 +0000 | [diff] [blame] | 356 | .stop-button:disabled:hover { |
| 357 | background-color: #e9a8ad; |
| 358 | } |
| 359 | |
| Pokey Rule | 397871d | 2025-05-19 15:02:45 +0100 | [diff] [blame] | 360 | .end-button { |
| 361 | background: #6c757d; |
| 362 | color: white; |
| 363 | } |
| 364 | |
| 365 | .end-button:hover:not(:disabled) { |
| 366 | background-color: #5a6268; |
| 367 | } |
| 368 | |
| 369 | .end-button:disabled { |
| 370 | background-color: #a9acaf; |
| 371 | cursor: not-allowed; |
| 372 | opacity: 0.7; |
| 373 | } |
| 374 | |
| Philip Zeyliger | bce3a13 | 2025-04-30 22:03:39 +0000 | [diff] [blame] | 375 | .button-icon { |
| 376 | width: 16px; |
| 377 | height: 16px; |
| 378 | } |
| 379 | |
| 380 | @media (max-width: 1400px) { |
| 381 | .button-text { |
| 382 | display: none; |
| 383 | } |
| 384 | |
| Philip Zeyliger | bce3a13 | 2025-04-30 22:03:39 +0000 | [diff] [blame] | 385 | .stop-button { |
| 386 | padding: 6px; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | /* Removed poll-updates class */ |
| 391 | |
| Philip Zeyliger | bc6b629 | 2025-04-30 18:00:15 +0000 | [diff] [blame] | 392 | .notifications-toggle { |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 393 | display: flex; |
| 394 | align-items: center; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 395 | font-size: 12px; |
| Philip Zeyliger | bc6b629 | 2025-04-30 18:00:15 +0000 | [diff] [blame] | 396 | margin-right: 10px; |
| Philip Zeyliger | db5e9b4 | 2025-04-30 19:58:13 +0000 | [diff] [blame] | 397 | cursor: pointer; |
| 398 | } |
| 399 | |
| 400 | .bell-icon { |
| 401 | width: 20px; |
| 402 | height: 20px; |
| 403 | position: relative; |
| 404 | display: inline-flex; |
| 405 | align-items: center; |
| 406 | justify-content: center; |
| 407 | } |
| 408 | |
| 409 | .bell-disabled::before { |
| 410 | content: ""; |
| 411 | position: absolute; |
| 412 | width: 2px; |
| 413 | height: 24px; |
| 414 | background-color: #dc3545; |
| 415 | transform: rotate(45deg); |
| 416 | transform-origin: center center; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 417 | } |
| 418 | `; |
| 419 | |
| 420 | // Header bar: Network connection status details |
| 421 | @property() |
| 422 | connectionStatus: ConnectionStatus = "disconnected"; |
| Autoformatter | cf57096 | 2025-04-30 17:27:39 +0000 | [diff] [blame] | 423 | |
| Philip Zeyliger | 47b71c9 | 2025-04-30 15:43:39 +0000 | [diff] [blame] | 424 | // Track if the last commit info has been copied |
| 425 | @state() |
| Philip Zeyliger | 16fa8b4 | 2025-05-02 04:28:16 +0000 | [diff] [blame] | 426 | // lastCommitCopied moved to sketch-container-status |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 427 | |
| Philip Zeyliger | bc6b629 | 2025-04-30 18:00:15 +0000 | [diff] [blame] | 428 | // Track notification preferences |
| 429 | @state() |
| Philip Zeyliger | db5e9b4 | 2025-04-30 19:58:13 +0000 | [diff] [blame] | 430 | notificationsEnabled: boolean = false; |
| 431 | |
| 432 | // Track if the window is focused to control notifications |
| 433 | @state() |
| 434 | private _windowFocused: boolean = document.hasFocus(); |
| Philip Zeyliger | bc6b629 | 2025-04-30 18:00:15 +0000 | [diff] [blame] | 435 | |
| Josh Bleecher Snyder | 112b923 | 2025-05-23 11:26:33 -0700 | [diff] [blame] | 436 | // Track if the todo panel should be visible |
| 437 | @state() |
| 438 | private _todoPanelVisible: boolean = false; |
| 439 | |
| 440 | // ResizeObserver for tracking chat input height changes |
| 441 | private chatInputResizeObserver: ResizeObserver | null = null; |
| 442 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 443 | @property() |
| 444 | connectionErrorMessage: string = ""; |
| 445 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 446 | // Chat messages |
| Pokey Rule | e2a8c2f | 2025-04-23 15:09:25 +0100 | [diff] [blame] | 447 | @property({ attribute: false }) |
| Sean McCullough | d9f1337 | 2025-04-21 15:08:49 -0700 | [diff] [blame] | 448 | messages: AgentMessage[] = []; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 449 | |
| 450 | @property() |
| Philip Zeyliger | 9b999b0 | 2025-04-25 16:31:50 +0000 | [diff] [blame] | 451 | set title(value: string) { |
| 452 | const oldValue = this._title; |
| 453 | this._title = value; |
| 454 | this.requestUpdate("title", oldValue); |
| 455 | // Update document title when title property changes |
| 456 | this.updateDocumentTitle(); |
| 457 | } |
| 458 | |
| 459 | get title(): string { |
| 460 | return this._title; |
| 461 | } |
| 462 | |
| 463 | private _title: string = ""; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 464 | |
| 465 | private dataManager = new DataManager(); |
| 466 | |
| Pokey Rule | e2a8c2f | 2025-04-23 15:09:25 +0100 | [diff] [blame] | 467 | @property({ attribute: false }) |
| Sean McCullough | d9f1337 | 2025-04-21 15:08:49 -0700 | [diff] [blame] | 468 | containerState: State = { |
| Philip Zeyliger | d03318d | 2025-05-08 13:09:12 -0700 | [diff] [blame] | 469 | state_version: 2, |
| Sean McCullough | d9f1337 | 2025-04-21 15:08:49 -0700 | [diff] [blame] | 470 | title: "", |
| 471 | os: "", |
| 472 | message_count: 0, |
| 473 | hostname: "", |
| 474 | working_dir: "", |
| 475 | initial_commit: "", |
| Philip Zeyliger | 99a9a02 | 2025-04-27 15:15:25 +0000 | [diff] [blame] | 476 | outstanding_llm_calls: 0, |
| 477 | outstanding_tool_calls: [], |
| Philip Zeyliger | c72fff5 | 2025-04-29 20:17:54 +0000 | [diff] [blame] | 478 | session_id: "", |
| 479 | ssh_available: false, |
| Philip Zeyliger | 2c4db09 | 2025-04-28 16:57:50 -0700 | [diff] [blame] | 480 | ssh_error: "", |
| 481 | in_container: false, |
| 482 | first_message_index: 0, |
| Sean McCullough | d9f1337 | 2025-04-21 15:08:49 -0700 | [diff] [blame] | 483 | }; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 484 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 485 | // Mutation observer to detect when new messages are added |
| 486 | private mutationObserver: MutationObserver | null = null; |
| 487 | |
| 488 | constructor() { |
| 489 | super(); |
| 490 | |
| Philip Zeyliger | 16fa8b4 | 2025-05-02 04:28:16 +0000 | [diff] [blame] | 491 | // Reference to the container status element |
| 492 | this.containerStatusElement = null; |
| 493 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 494 | // Binding methods to this |
| 495 | this._handleViewModeSelect = this._handleViewModeSelect.bind(this); |
| Sean McCullough | 34bb09a | 2025-05-13 15:39:54 -0700 | [diff] [blame] | 496 | this._handlePopState = this._handlePopState.bind(this); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 497 | this._handleShowCommitDiff = this._handleShowCommitDiff.bind(this); |
| Sean McCullough | 485afc6 | 2025-04-28 14:28:39 -0700 | [diff] [blame] | 498 | this._handleMutlipleChoiceSelected = |
| 499 | this._handleMutlipleChoiceSelected.bind(this); |
| Sean McCullough | d3906e2 | 2025-04-29 17:32:14 +0000 | [diff] [blame] | 500 | this._handleStopClick = this._handleStopClick.bind(this); |
| Pokey Rule | 397871d | 2025-05-19 15:02:45 +0100 | [diff] [blame] | 501 | this._handleEndClick = this._handleEndClick.bind(this); |
| Philip Zeyliger | bc6b629 | 2025-04-30 18:00:15 +0000 | [diff] [blame] | 502 | this._handleNotificationsToggle = |
| 503 | this._handleNotificationsToggle.bind(this); |
| Philip Zeyliger | db5e9b4 | 2025-04-30 19:58:13 +0000 | [diff] [blame] | 504 | this._handleWindowFocus = this._handleWindowFocus.bind(this); |
| 505 | this._handleWindowBlur = this._handleWindowBlur.bind(this); |
| Philip Zeyliger | bc6b629 | 2025-04-30 18:00:15 +0000 | [diff] [blame] | 506 | |
| 507 | // Load notification preference from localStorage |
| 508 | try { |
| 509 | const savedPref = localStorage.getItem("sketch-notifications-enabled"); |
| 510 | if (savedPref !== null) { |
| 511 | this.notificationsEnabled = savedPref === "true"; |
| 512 | } |
| 513 | } catch (error) { |
| 514 | console.error("Error loading notification preference:", error); |
| 515 | } |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | // See https://lit.dev/docs/components/lifecycle/ |
| 519 | connectedCallback() { |
| 520 | super.connectedCallback(); |
| 521 | |
| Philip Zeyliger | 16fa8b4 | 2025-05-02 04:28:16 +0000 | [diff] [blame] | 522 | // Get reference to the container status element |
| 523 | setTimeout(() => { |
| 524 | this.containerStatusElement = |
| 525 | this.shadowRoot?.getElementById("container-status"); |
| 526 | }, 0); |
| 527 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 528 | // Initialize client-side nav history. |
| 529 | const url = new URL(window.location.href); |
| 530 | const mode = url.searchParams.get("view") || "chat"; |
| 531 | window.history.replaceState({ mode }, "", url.toString()); |
| 532 | |
| 533 | this.toggleViewMode(mode as ViewMode, false); |
| 534 | // Add popstate event listener to handle browser back/forward navigation |
| Pokey Rule | e2a8c2f | 2025-04-23 15:09:25 +0100 | [diff] [blame] | 535 | window.addEventListener("popstate", this._handlePopState); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 536 | |
| 537 | // Add event listeners |
| Pokey Rule | e2a8c2f | 2025-04-23 15:09:25 +0100 | [diff] [blame] | 538 | window.addEventListener("view-mode-select", this._handleViewModeSelect); |
| Pokey Rule | e2a8c2f | 2025-04-23 15:09:25 +0100 | [diff] [blame] | 539 | window.addEventListener("show-commit-diff", this._handleShowCommitDiff); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 540 | |
| Philip Zeyliger | db5e9b4 | 2025-04-30 19:58:13 +0000 | [diff] [blame] | 541 | // Add window focus/blur listeners for controlling notifications |
| 542 | window.addEventListener("focus", this._handleWindowFocus); |
| 543 | window.addEventListener("blur", this._handleWindowBlur); |
| Sean McCullough | 485afc6 | 2025-04-28 14:28:39 -0700 | [diff] [blame] | 544 | window.addEventListener( |
| 545 | "multiple-choice-selected", |
| 546 | this._handleMutlipleChoiceSelected, |
| 547 | ); |
| Philip Zeyliger | db5e9b4 | 2025-04-30 19:58:13 +0000 | [diff] [blame] | 548 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 549 | // register event listeners |
| 550 | this.dataManager.addEventListener( |
| 551 | "dataChanged", |
| Philip Zeyliger | 72682df | 2025-04-23 13:09:46 -0700 | [diff] [blame] | 552 | this.handleDataChanged.bind(this), |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 553 | ); |
| 554 | this.dataManager.addEventListener( |
| 555 | "connectionStatusChanged", |
| Philip Zeyliger | 72682df | 2025-04-23 13:09:46 -0700 | [diff] [blame] | 556 | this.handleConnectionStatusChanged.bind(this), |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 557 | ); |
| 558 | |
| Philip Zeyliger | 9b999b0 | 2025-04-25 16:31:50 +0000 | [diff] [blame] | 559 | // Set initial document title |
| 560 | this.updateDocumentTitle(); |
| 561 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 562 | // Initialize the data manager |
| 563 | this.dataManager.initialize(); |
| Autoformatter | cf57096 | 2025-04-30 17:27:39 +0000 | [diff] [blame] | 564 | |
| Philip Zeyliger | 47b71c9 | 2025-04-30 15:43:39 +0000 | [diff] [blame] | 565 | // Process existing messages for commit info |
| 566 | if (this.messages && this.messages.length > 0) { |
| Philip Zeyliger | 16fa8b4 | 2025-05-02 04:28:16 +0000 | [diff] [blame] | 567 | // Update last commit info via container status component |
| 568 | setTimeout(() => { |
| 569 | if (this.containerStatusElement) { |
| 570 | this.containerStatusElement.updateLastCommitInfo(this.messages); |
| 571 | } |
| 572 | }, 100); |
| Philip Zeyliger | 47b71c9 | 2025-04-30 15:43:39 +0000 | [diff] [blame] | 573 | } |
| Josh Bleecher Snyder | 112b923 | 2025-05-23 11:26:33 -0700 | [diff] [blame] | 574 | |
| 575 | // Check if todo panel should be visible on initial load |
| 576 | this.checkTodoPanelVisibility(); |
| 577 | |
| 578 | // Set up ResizeObserver for chat input to update todo panel height |
| 579 | this.setupChatInputObserver(); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | // See https://lit.dev/docs/components/lifecycle/ |
| 583 | disconnectedCallback() { |
| 584 | super.disconnectedCallback(); |
| Pokey Rule | e2a8c2f | 2025-04-23 15:09:25 +0100 | [diff] [blame] | 585 | window.removeEventListener("popstate", this._handlePopState); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 586 | |
| 587 | // Remove event listeners |
| Pokey Rule | e2a8c2f | 2025-04-23 15:09:25 +0100 | [diff] [blame] | 588 | window.removeEventListener("view-mode-select", this._handleViewModeSelect); |
| Pokey Rule | e2a8c2f | 2025-04-23 15:09:25 +0100 | [diff] [blame] | 589 | window.removeEventListener("show-commit-diff", this._handleShowCommitDiff); |
| Philip Zeyliger | db5e9b4 | 2025-04-30 19:58:13 +0000 | [diff] [blame] | 590 | window.removeEventListener("focus", this._handleWindowFocus); |
| 591 | window.removeEventListener("blur", this._handleWindowBlur); |
| Sean McCullough | 485afc6 | 2025-04-28 14:28:39 -0700 | [diff] [blame] | 592 | window.removeEventListener( |
| 593 | "multiple-choice-selected", |
| 594 | this._handleMutlipleChoiceSelected, |
| 595 | ); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 596 | |
| 597 | // unregister data manager event listeners |
| 598 | this.dataManager.removeEventListener( |
| 599 | "dataChanged", |
| Philip Zeyliger | 72682df | 2025-04-23 13:09:46 -0700 | [diff] [blame] | 600 | this.handleDataChanged.bind(this), |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 601 | ); |
| 602 | this.dataManager.removeEventListener( |
| 603 | "connectionStatusChanged", |
| Philip Zeyliger | 72682df | 2025-04-23 13:09:46 -0700 | [diff] [blame] | 604 | this.handleConnectionStatusChanged.bind(this), |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 605 | ); |
| 606 | |
| 607 | // Disconnect mutation observer if it exists |
| 608 | if (this.mutationObserver) { |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 609 | this.mutationObserver.disconnect(); |
| 610 | this.mutationObserver = null; |
| 611 | } |
| Josh Bleecher Snyder | 112b923 | 2025-05-23 11:26:33 -0700 | [diff] [blame] | 612 | |
| 613 | // Disconnect chat input resize observer if it exists |
| 614 | if (this.chatInputResizeObserver) { |
| 615 | this.chatInputResizeObserver.disconnect(); |
| 616 | this.chatInputResizeObserver = null; |
| 617 | } |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 618 | } |
| 619 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 620 | updateUrlForViewMode(mode: ViewMode): void { |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 621 | // Get the current URL without search parameters |
| 622 | const url = new URL(window.location.href); |
| 623 | |
| 624 | // Clear existing parameters |
| 625 | url.search = ""; |
| 626 | |
| 627 | // Only add view parameter if not in default chat view |
| 628 | if (mode !== "chat") { |
| 629 | url.searchParams.set("view", mode); |
| Philip Zeyliger | 00bcaef | 2025-05-30 04:21:15 +0000 | [diff] [blame] | 630 | const diff2View = this.shadowRoot?.querySelector( |
| 631 | "sketch-diff2-view", |
| 632 | ) as SketchDiff2View; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 633 | |
| Philip Zeyliger | 00bcaef | 2025-05-30 04:21:15 +0000 | [diff] [blame] | 634 | // If in diff2 view and there's a commit hash, include that too |
| 635 | if (mode === "diff2" && diff2View?.commit) { |
| 636 | url.searchParams.set("commit", diff2View.commit); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 637 | } |
| 638 | } |
| 639 | |
| 640 | // Update the browser history without reloading the page |
| 641 | window.history.pushState({ mode }, "", url.toString()); |
| 642 | } |
| 643 | |
| Pokey Rule | e2a8c2f | 2025-04-23 15:09:25 +0100 | [diff] [blame] | 644 | private _handlePopState(event: PopStateEvent) { |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 645 | if (event.state && event.state.mode) { |
| 646 | this.toggleViewMode(event.state.mode, false); |
| 647 | } else { |
| 648 | this.toggleViewMode("chat", false); |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | /** |
| 653 | * Handle view mode selection event |
| 654 | */ |
| 655 | private _handleViewModeSelect(event: CustomEvent) { |
| Philip Zeyliger | 00bcaef | 2025-05-30 04:21:15 +0000 | [diff] [blame] | 656 | const mode = event.detail.mode as "chat" | "diff2" | "terminal"; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 657 | this.toggleViewMode(mode, true); |
| 658 | } |
| 659 | |
| 660 | /** |
| 661 | * Handle show commit diff event |
| 662 | */ |
| 663 | private _handleShowCommitDiff(event: CustomEvent) { |
| 664 | const { commitHash } = event.detail; |
| 665 | if (commitHash) { |
| 666 | this.showCommitDiff(commitHash); |
| 667 | } |
| 668 | } |
| 669 | |
| Sean McCullough | 485afc6 | 2025-04-28 14:28:39 -0700 | [diff] [blame] | 670 | private _handleMultipleChoice(event: CustomEvent) { |
| 671 | window.console.log("_handleMultipleChoice", event); |
| 672 | this._sendChat; |
| 673 | } |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 674 | |
| 675 | private _handleDiffComment(event: CustomEvent) { |
| 676 | // Empty stub required by the event binding in the template |
| 677 | // Actual handling occurs at global level in sketch-chat-input component |
| 678 | } |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 679 | /** |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 680 | * Listen for commit diff event |
| 681 | * @param commitHash The commit hash to show diff for |
| 682 | */ |
| Pokey Rule | e2a8c2f | 2025-04-23 15:09:25 +0100 | [diff] [blame] | 683 | private showCommitDiff(commitHash: string): void { |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 684 | // Store the commit hash |
| 685 | this.currentCommitHash = commitHash; |
| 686 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 687 | this.toggleViewMode("diff2", true); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 688 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 689 | this.updateComplete.then(() => { |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 690 | const diff2View = this.shadowRoot?.querySelector("sketch-diff2-view"); |
| 691 | if (diff2View) { |
| 692 | (diff2View as SketchDiff2View).refreshDiffView(); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 693 | } |
| 694 | }); |
| 695 | } |
| 696 | |
| 697 | /** |
| Philip Zeyliger | 00bcaef | 2025-05-30 04:21:15 +0000 | [diff] [blame] | 698 | * Toggle between different view modes: chat, diff2, terminal |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 699 | */ |
| Pokey Rule | e2a8c2f | 2025-04-23 15:09:25 +0100 | [diff] [blame] | 700 | private toggleViewMode(mode: ViewMode, updateHistory: boolean): void { |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 701 | // Don't do anything if the mode is already active |
| 702 | if (this.viewMode === mode) return; |
| 703 | |
| 704 | // Update the view mode |
| 705 | this.viewMode = mode; |
| 706 | |
| 707 | if (updateHistory) { |
| 708 | // Update URL with the current view mode |
| 709 | this.updateUrlForViewMode(mode); |
| 710 | } |
| 711 | |
| 712 | // Wait for DOM update to complete |
| 713 | this.updateComplete.then(() => { |
| 714 | // Update active view |
| Pokey Rule | 46fff97 | 2025-04-25 14:57:44 +0100 | [diff] [blame] | 715 | const viewContainerInner = this.shadowRoot?.querySelector( |
| 716 | "#view-container-inner", |
| 717 | ); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 718 | const chatView = this.shadowRoot?.querySelector(".chat-view"); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 719 | const diff2View = this.shadowRoot?.querySelector(".diff2-view"); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 720 | const terminalView = this.shadowRoot?.querySelector(".terminal-view"); |
| 721 | |
| 722 | // Remove active class from all views |
| 723 | chatView?.classList.remove("view-active"); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 724 | diff2View?.classList.remove("view-active"); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 725 | terminalView?.classList.remove("view-active"); |
| 726 | |
| Philip Zeyliger | 00bcaef | 2025-05-30 04:21:15 +0000 | [diff] [blame] | 727 | // Add/remove diff2-active class on view container |
| 728 | if (mode === "diff2") { |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 729 | viewContainerInner?.classList.add("diff2-active"); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 730 | } else { |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 731 | viewContainerInner?.classList.remove("diff2-active"); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | // Add active class to the selected view |
| 735 | switch (mode) { |
| 736 | case "chat": |
| 737 | chatView?.classList.add("view-active"); |
| 738 | break; |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 739 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 740 | case "diff2": |
| 741 | diff2View?.classList.add("view-active"); |
| 742 | // Refresh git/recentlog when Monaco diff view is opened |
| 743 | // This ensures branch information is always up-to-date, as branches can change frequently |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 744 | const diff2ViewComp = |
| 745 | this.shadowRoot?.querySelector("sketch-diff2-view"); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 746 | if (diff2ViewComp) { |
| 747 | (diff2ViewComp as SketchDiff2View).refreshDiffView(); |
| 748 | } |
| 749 | break; |
| Philip Zeyliger | 2d4c48f | 2025-05-02 23:35:03 +0000 | [diff] [blame] | 750 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 751 | case "terminal": |
| 752 | terminalView?.classList.add("view-active"); |
| 753 | break; |
| 754 | } |
| 755 | |
| 756 | // Update view mode buttons |
| 757 | const viewModeSelect = this.shadowRoot?.querySelector( |
| Philip Zeyliger | 72682df | 2025-04-23 13:09:46 -0700 | [diff] [blame] | 758 | "sketch-view-mode-select", |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 759 | ); |
| 760 | if (viewModeSelect) { |
| 761 | const event = new CustomEvent("update-active-mode", { |
| 762 | detail: { mode }, |
| 763 | bubbles: true, |
| 764 | composed: true, |
| 765 | }); |
| 766 | viewModeSelect.dispatchEvent(event); |
| 767 | } |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 768 | }); |
| 769 | } |
| 770 | |
| Philip Zeyliger | 9b999b0 | 2025-04-25 16:31:50 +0000 | [diff] [blame] | 771 | /** |
| 772 | * Updates the document title based on current title and connection status |
| 773 | */ |
| 774 | private updateDocumentTitle(): void { |
| 775 | let docTitle = `sk: ${this.title || "untitled"}`; |
| 776 | |
| 777 | // Add red circle emoji if disconnected |
| 778 | if (this.connectionStatus === "disconnected") { |
| 779 | docTitle += " 🔴"; |
| 780 | } |
| 781 | |
| 782 | document.title = docTitle; |
| 783 | } |
| 784 | |
| Philip Zeyliger | bc6b629 | 2025-04-30 18:00:15 +0000 | [diff] [blame] | 785 | // Check and request notification permission if needed |
| 786 | private async checkNotificationPermission(): Promise<boolean> { |
| 787 | // Check if the Notification API is supported |
| 788 | if (!("Notification" in window)) { |
| 789 | console.log("This browser does not support notifications"); |
| 790 | return false; |
| 791 | } |
| 792 | |
| 793 | // Check if permission is already granted |
| 794 | if (Notification.permission === "granted") { |
| 795 | return true; |
| 796 | } |
| 797 | |
| 798 | // If permission is not denied, request it |
| 799 | if (Notification.permission !== "denied") { |
| 800 | const permission = await Notification.requestPermission(); |
| 801 | return permission === "granted"; |
| 802 | } |
| 803 | |
| 804 | return false; |
| 805 | } |
| 806 | |
| Philip Zeyliger | db5e9b4 | 2025-04-30 19:58:13 +0000 | [diff] [blame] | 807 | // Handle notifications toggle click |
| 808 | private _handleNotificationsToggle(): void { |
| 809 | this.notificationsEnabled = !this.notificationsEnabled; |
| Philip Zeyliger | bc6b629 | 2025-04-30 18:00:15 +0000 | [diff] [blame] | 810 | |
| 811 | // If enabling notifications, check permissions |
| 812 | if (this.notificationsEnabled) { |
| 813 | this.checkNotificationPermission(); |
| 814 | } |
| 815 | |
| 816 | // Save preference to localStorage |
| 817 | try { |
| 818 | localStorage.setItem( |
| 819 | "sketch-notifications-enabled", |
| 820 | String(this.notificationsEnabled), |
| 821 | ); |
| 822 | } catch (error) { |
| 823 | console.error("Error saving notification preference:", error); |
| 824 | } |
| 825 | } |
| 826 | |
| Philip Zeyliger | db5e9b4 | 2025-04-30 19:58:13 +0000 | [diff] [blame] | 827 | // Handle window focus event |
| 828 | private _handleWindowFocus(): void { |
| 829 | this._windowFocused = true; |
| 830 | } |
| 831 | |
| 832 | // Handle window blur event |
| 833 | private _handleWindowBlur(): void { |
| 834 | this._windowFocused = false; |
| 835 | } |
| 836 | |
| Philip Zeyliger | bc6b629 | 2025-04-30 18:00:15 +0000 | [diff] [blame] | 837 | // Show notification for message with EndOfTurn=true |
| 838 | private async showEndOfTurnNotification( |
| 839 | message: AgentMessage, |
| 840 | ): Promise<void> { |
| 841 | // Don't show notifications if they're disabled |
| 842 | if (!this.notificationsEnabled) return; |
| 843 | |
| Philip Zeyliger | db5e9b4 | 2025-04-30 19:58:13 +0000 | [diff] [blame] | 844 | // Don't show notifications if the window is focused |
| 845 | if (this._windowFocused) return; |
| 846 | |
| Philip Zeyliger | bc6b629 | 2025-04-30 18:00:15 +0000 | [diff] [blame] | 847 | // Check if we have permission to show notifications |
| 848 | const hasPermission = await this.checkNotificationPermission(); |
| 849 | if (!hasPermission) return; |
| 850 | |
| Philip Zeyliger | 3201133 | 2025-04-30 20:59:40 +0000 | [diff] [blame] | 851 | // Only show notifications for agent messages with end_of_turn=true and no parent_conversation_id |
| 852 | if ( |
| 853 | message.type !== "agent" || |
| 854 | !message.end_of_turn || |
| 855 | message.parent_conversation_id |
| 856 | ) |
| 857 | return; |
| Philip Zeyliger | bc6b629 | 2025-04-30 18:00:15 +0000 | [diff] [blame] | 858 | |
| 859 | // Create a title that includes the sketch title |
| 860 | const notificationTitle = `Sketch: ${this.title || "untitled"}`; |
| 861 | |
| 862 | // Extract the beginning of the message content (first 100 chars) |
| 863 | const messagePreview = message.content |
| 864 | ? message.content.substring(0, 100) + |
| 865 | (message.content.length > 100 ? "..." : "") |
| 866 | : "Agent has completed its turn"; |
| 867 | |
| 868 | // Create and show the notification |
| 869 | try { |
| 870 | new Notification(notificationTitle, { |
| 871 | body: messagePreview, |
| Philip Zeyliger | db5e9b4 | 2025-04-30 19:58:13 +0000 | [diff] [blame] | 872 | icon: "https://sketch.dev/favicon.ico", // Use sketch.dev favicon for notification |
| Philip Zeyliger | bc6b629 | 2025-04-30 18:00:15 +0000 | [diff] [blame] | 873 | }); |
| 874 | } catch (error) { |
| 875 | console.error("Error showing notification:", error); |
| 876 | } |
| 877 | } |
| 878 | |
| Josh Bleecher Snyder | 112b923 | 2025-05-23 11:26:33 -0700 | [diff] [blame] | 879 | // Check if todo panel should be visible based on latest todo content from messages or state |
| 880 | private checkTodoPanelVisibility(): void { |
| 881 | // Find the latest todo content from messages first |
| 882 | let latestTodoContent = ""; |
| 883 | for (let i = this.messages.length - 1; i >= 0; i--) { |
| 884 | const message = this.messages[i]; |
| 885 | if (message.todo_content !== undefined) { |
| 886 | latestTodoContent = message.todo_content || ""; |
| 887 | break; |
| 888 | } |
| 889 | } |
| 890 | |
| 891 | // If no todo content found in messages, check the current state |
| 892 | if (latestTodoContent === "" && this.containerState?.todo_content) { |
| 893 | latestTodoContent = this.containerState.todo_content; |
| 894 | } |
| 895 | |
| 896 | // Parse the todo data to check if there are any actual todos |
| 897 | let hasTodos = false; |
| 898 | if (latestTodoContent.trim()) { |
| 899 | try { |
| 900 | const todoData = JSON.parse(latestTodoContent); |
| 901 | hasTodos = todoData.items && todoData.items.length > 0; |
| 902 | } catch (error) { |
| 903 | // Invalid JSON, treat as no todos |
| 904 | hasTodos = false; |
| 905 | } |
| 906 | } |
| 907 | |
| 908 | this._todoPanelVisible = hasTodos; |
| 909 | |
| 910 | // Update todo panel content if visible |
| 911 | if (hasTodos) { |
| 912 | const todoPanel = this.shadowRoot?.querySelector( |
| 913 | "sketch-todo-panel", |
| 914 | ) as any; |
| 915 | if (todoPanel && todoPanel.updateTodoContent) { |
| 916 | todoPanel.updateTodoContent(latestTodoContent); |
| 917 | } |
| 918 | } |
| 919 | } |
| 920 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 921 | private handleDataChanged(eventData: { |
| 922 | state: State; |
| Sean McCullough | d9f1337 | 2025-04-21 15:08:49 -0700 | [diff] [blame] | 923 | newMessages: AgentMessage[]; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 924 | }): void { |
| Philip Zeyliger | bce3a13 | 2025-04-30 22:03:39 +0000 | [diff] [blame] | 925 | const { state, newMessages } = eventData; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 926 | |
| 927 | // Update state if we received it |
| 928 | if (state) { |
| Josh Bleecher Snyder | e81233f | 2025-04-30 04:05:41 +0000 | [diff] [blame] | 929 | // Ensure we're using the latest call status to prevent indicators from being stuck |
| Autoformatter | f830c9d | 2025-04-30 18:16:01 +0000 | [diff] [blame] | 930 | if ( |
| 931 | state.outstanding_llm_calls === 0 && |
| 932 | state.outstanding_tool_calls.length === 0 |
| 933 | ) { |
| Josh Bleecher Snyder | e81233f | 2025-04-30 04:05:41 +0000 | [diff] [blame] | 934 | // Force reset containerState calls when nothing is reported as in progress |
| 935 | state.outstanding_llm_calls = 0; |
| 936 | state.outstanding_tool_calls = []; |
| 937 | } |
| Autoformatter | f830c9d | 2025-04-30 18:16:01 +0000 | [diff] [blame] | 938 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 939 | this.containerState = state; |
| 940 | this.title = state.title; |
| Philip Zeyliger | 9b999b0 | 2025-04-25 16:31:50 +0000 | [diff] [blame] | 941 | |
| 942 | // Update document title when sketch title changes |
| 943 | this.updateDocumentTitle(); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 944 | } |
| 945 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 946 | // Update messages |
| Pokey Rule | e2a8c2f | 2025-04-23 15:09:25 +0100 | [diff] [blame] | 947 | this.messages = aggregateAgentMessages(this.messages, newMessages); |
| Autoformatter | cf57096 | 2025-04-30 17:27:39 +0000 | [diff] [blame] | 948 | |
| Philip Zeyliger | 47b71c9 | 2025-04-30 15:43:39 +0000 | [diff] [blame] | 949 | // Process new messages to find commit messages |
| Philip Zeyliger | 16fa8b4 | 2025-05-02 04:28:16 +0000 | [diff] [blame] | 950 | // Update last commit info via container status component |
| 951 | if (this.containerStatusElement) { |
| 952 | this.containerStatusElement.updateLastCommitInfo(newMessages); |
| 953 | } |
| Philip Zeyliger | bc6b629 | 2025-04-30 18:00:15 +0000 | [diff] [blame] | 954 | |
| 955 | // Check for agent messages with end_of_turn=true and show notifications |
| Philip Zeyliger | bce3a13 | 2025-04-30 22:03:39 +0000 | [diff] [blame] | 956 | if (newMessages && newMessages.length > 0) { |
| Philip Zeyliger | bc6b629 | 2025-04-30 18:00:15 +0000 | [diff] [blame] | 957 | for (const message of newMessages) { |
| Philip Zeyliger | 3201133 | 2025-04-30 20:59:40 +0000 | [diff] [blame] | 958 | if ( |
| 959 | message.type === "agent" && |
| 960 | message.end_of_turn && |
| 961 | !message.parent_conversation_id |
| 962 | ) { |
| Philip Zeyliger | bc6b629 | 2025-04-30 18:00:15 +0000 | [diff] [blame] | 963 | this.showEndOfTurnNotification(message); |
| 964 | break; // Only show one notification per batch of messages |
| 965 | } |
| 966 | } |
| 967 | } |
| Josh Bleecher Snyder | 112b923 | 2025-05-23 11:26:33 -0700 | [diff] [blame] | 968 | |
| 969 | // Check if todo panel should be visible after agent loop iteration |
| 970 | this.checkTodoPanelVisibility(); |
| 971 | |
| 972 | // Ensure chat input observer is set up when new data comes in |
| 973 | if (!this.chatInputResizeObserver) { |
| 974 | this.setupChatInputObserver(); |
| 975 | } |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 976 | } |
| 977 | |
| 978 | private handleConnectionStatusChanged( |
| 979 | status: ConnectionStatus, |
| Philip Zeyliger | 72682df | 2025-04-23 13:09:46 -0700 | [diff] [blame] | 980 | errorMessage?: string, |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 981 | ): void { |
| 982 | this.connectionStatus = status; |
| 983 | this.connectionErrorMessage = errorMessage || ""; |
| Philip Zeyliger | 9b999b0 | 2025-04-25 16:31:50 +0000 | [diff] [blame] | 984 | |
| 985 | // Update document title when connection status changes |
| 986 | this.updateDocumentTitle(); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 987 | } |
| 988 | |
| Sean McCullough | d3906e2 | 2025-04-29 17:32:14 +0000 | [diff] [blame] | 989 | private async _handleStopClick(): Promise<void> { |
| 990 | try { |
| 991 | const response = await fetch("cancel", { |
| 992 | method: "POST", |
| 993 | headers: { |
| 994 | "Content-Type": "application/json", |
| 995 | }, |
| 996 | body: JSON.stringify({ reason: "user requested cancellation" }), |
| 997 | }); |
| 998 | |
| 999 | if (!response.ok) { |
| 1000 | const errorData = await response.text(); |
| 1001 | throw new Error( |
| 1002 | `Failed to stop operation: ${response.status} - ${errorData}`, |
| 1003 | ); |
| 1004 | } |
| 1005 | |
| Philip Zeyliger | bce3a13 | 2025-04-30 22:03:39 +0000 | [diff] [blame] | 1006 | // Stop request sent |
| Sean McCullough | d3906e2 | 2025-04-29 17:32:14 +0000 | [diff] [blame] | 1007 | } catch (error) { |
| 1008 | console.error("Error stopping operation:", error); |
| Sean McCullough | d3906e2 | 2025-04-29 17:32:14 +0000 | [diff] [blame] | 1009 | } |
| 1010 | } |
| 1011 | |
| Pokey Rule | 397871d | 2025-05-19 15:02:45 +0100 | [diff] [blame] | 1012 | private async _handleEndClick(event?: Event): Promise<void> { |
| 1013 | if (event) { |
| 1014 | event.preventDefault(); |
| 1015 | event.stopPropagation(); |
| 1016 | } |
| Philip Zeyliger | b573940 | 2025-06-02 07:04:34 -0700 | [diff] [blame] | 1017 | |
| Philip Zeyliger | 1609893 | 2025-06-04 11:02:55 -0700 | [diff] [blame] | 1018 | // Show confirmation dialog |
| 1019 | const confirmed = window.confirm( |
| 1020 | "Ending the session will shut down the underlying container. Are you sure?", |
| 1021 | ); |
| 1022 | if (!confirmed) return; |
| Pokey Rule | 397871d | 2025-05-19 15:02:45 +0100 | [diff] [blame] | 1023 | |
| 1024 | try { |
| 1025 | const response = await fetch("end", { |
| 1026 | method: "POST", |
| 1027 | headers: { |
| 1028 | "Content-Type": "application/json", |
| 1029 | }, |
| Philip Zeyliger | 1609893 | 2025-06-04 11:02:55 -0700 | [diff] [blame] | 1030 | body: JSON.stringify({ reason: "user requested end of session" }), |
| Pokey Rule | 397871d | 2025-05-19 15:02:45 +0100 | [diff] [blame] | 1031 | }); |
| 1032 | |
| 1033 | if (!response.ok) { |
| 1034 | const errorData = await response.text(); |
| 1035 | throw new Error( |
| 1036 | `Failed to end session: ${response.status} - ${errorData}`, |
| 1037 | ); |
| 1038 | } |
| 1039 | |
| 1040 | // After successful response, redirect to messages view |
| 1041 | // Extract the session ID from the URL |
| 1042 | const currentUrl = window.location.href; |
| 1043 | // The URL pattern should be like https://sketch.dev/s/cs71-8qa6-1124-aw79/ |
| 1044 | const urlParts = currentUrl.split("/"); |
| 1045 | let sessionId = ""; |
| 1046 | |
| 1047 | // Find the session ID in the URL (should be after /s/) |
| 1048 | for (let i = 0; i < urlParts.length; i++) { |
| 1049 | if (urlParts[i] === "s" && i + 1 < urlParts.length) { |
| 1050 | sessionId = urlParts[i + 1]; |
| 1051 | break; |
| 1052 | } |
| 1053 | } |
| 1054 | |
| 1055 | if (sessionId) { |
| 1056 | // Create the messages URL |
| 1057 | const messagesUrl = `/messages/${sessionId}`; |
| 1058 | // Redirect to messages view |
| 1059 | window.location.href = messagesUrl; |
| 1060 | } |
| 1061 | |
| 1062 | // End request sent - connection will be closed by server |
| 1063 | } catch (error) { |
| 1064 | console.error("Error ending session:", error); |
| 1065 | } |
| 1066 | } |
| 1067 | |
| Sean McCullough | 485afc6 | 2025-04-28 14:28:39 -0700 | [diff] [blame] | 1068 | async _handleMutlipleChoiceSelected(e: CustomEvent) { |
| 1069 | const chatInput = this.shadowRoot?.querySelector( |
| 1070 | "sketch-chat-input", |
| 1071 | ) as SketchChatInput; |
| 1072 | if (chatInput) { |
| Josh Bleecher Snyder | 6cad861 | 2025-05-30 19:25:39 +0000 | [diff] [blame] | 1073 | if (chatInput.content && chatInput.content.trim() !== "") { |
| 1074 | chatInput.content += "\n\n"; |
| 1075 | } |
| 1076 | chatInput.content += e.detail.responseText; |
| Sean McCullough | 485afc6 | 2025-04-28 14:28:39 -0700 | [diff] [blame] | 1077 | chatInput.focus(); |
| Josh Bleecher Snyder | 6cad861 | 2025-05-30 19:25:39 +0000 | [diff] [blame] | 1078 | // Adjust textarea height to accommodate new content |
| 1079 | requestAnimationFrame(() => { |
| 1080 | if (chatInput.adjustChatSpacing) { |
| 1081 | chatInput.adjustChatSpacing(); |
| 1082 | } |
| 1083 | }); |
| Sean McCullough | 485afc6 | 2025-04-28 14:28:39 -0700 | [diff] [blame] | 1084 | } |
| 1085 | } |
| 1086 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 1087 | async _sendChat(e: CustomEvent) { |
| 1088 | console.log("app shell: _sendChat", e); |
| Sean McCullough | 485afc6 | 2025-04-28 14:28:39 -0700 | [diff] [blame] | 1089 | e.preventDefault(); |
| 1090 | e.stopPropagation(); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 1091 | const message = e.detail.message?.trim(); |
| 1092 | if (message == "") { |
| 1093 | return; |
| 1094 | } |
| 1095 | try { |
| Josh Bleecher Snyder | 98b64d1 | 2025-05-12 19:42:43 +0000 | [diff] [blame] | 1096 | // Always switch to chat view when sending a message so user can see processing |
| 1097 | if (this.viewMode !== "chat") { |
| 1098 | this.toggleViewMode("chat", true); |
| 1099 | } |
| Autoformatter | 5c7f957 | 2025-05-13 01:17:31 +0000 | [diff] [blame] | 1100 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 1101 | // Send the message to the server |
| 1102 | const response = await fetch("chat", { |
| 1103 | method: "POST", |
| 1104 | headers: { |
| 1105 | "Content-Type": "application/json", |
| 1106 | }, |
| 1107 | body: JSON.stringify({ message }), |
| 1108 | }); |
| 1109 | |
| 1110 | if (!response.ok) { |
| 1111 | const errorData = await response.text(); |
| 1112 | throw new Error(`Server error: ${response.status} - ${errorData}`); |
| 1113 | } |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 1114 | } catch (error) { |
| 1115 | console.error("Error sending chat message:", error); |
| 1116 | const statusText = document.getElementById("statusText"); |
| 1117 | if (statusText) { |
| 1118 | statusText.textContent = "Error sending message"; |
| 1119 | } |
| 1120 | } |
| 1121 | } |
| 1122 | |
| Pokey Rule | 4097e53 | 2025-04-24 18:55:28 +0100 | [diff] [blame] | 1123 | private scrollContainerRef = createRef<HTMLElement>(); |
| 1124 | |
| Josh Bleecher Snyder | 112b923 | 2025-05-23 11:26:33 -0700 | [diff] [blame] | 1125 | /** |
| 1126 | * Set up ResizeObserver to monitor chat input height changes |
| 1127 | */ |
| 1128 | private setupChatInputObserver(): void { |
| 1129 | // Wait for DOM to be ready |
| 1130 | this.updateComplete.then(() => { |
| 1131 | const chatInputElement = this.shadowRoot?.querySelector("#chat-input"); |
| 1132 | if (chatInputElement && !this.chatInputResizeObserver) { |
| 1133 | this.chatInputResizeObserver = new ResizeObserver((entries) => { |
| 1134 | for (const entry of entries) { |
| 1135 | this.updateTodoPanelHeight(entry.contentRect.height); |
| 1136 | } |
| 1137 | }); |
| 1138 | |
| 1139 | this.chatInputResizeObserver.observe(chatInputElement); |
| 1140 | |
| 1141 | // Initial height calculation |
| 1142 | const rect = chatInputElement.getBoundingClientRect(); |
| 1143 | this.updateTodoPanelHeight(rect.height); |
| 1144 | } |
| 1145 | }); |
| 1146 | } |
| 1147 | |
| 1148 | /** |
| 1149 | * Update the CSS custom property that controls todo panel bottom position |
| 1150 | */ |
| 1151 | private updateTodoPanelHeight(chatInputHeight: number): void { |
| 1152 | // Add some padding (20px) between todo panel and chat input |
| 1153 | const bottomOffset = chatInputHeight; |
| 1154 | |
| 1155 | // Update the CSS custom property on the host element |
| 1156 | this.style.setProperty("--chat-input-height", `${bottomOffset}px`); |
| 1157 | } |
| 1158 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 1159 | render() { |
| 1160 | return html` |
| Pokey Rule | 4097e53 | 2025-04-24 18:55:28 +0100 | [diff] [blame] | 1161 | <div id="top-banner"> |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 1162 | <div class="title-container"> |
| 1163 | <h1 class="banner-title">sketch</h1> |
| 1164 | <h2 id="chatTitle" class="chat-title">${this.title}</h2> |
| 1165 | </div> |
| 1166 | |
| Philip Zeyliger | bce3a13 | 2025-04-30 22:03:39 +0000 | [diff] [blame] | 1167 | <!-- Container status info moved above tabs --> |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 1168 | <sketch-container-status |
| 1169 | .state=${this.containerState} |
| Philip Zeyliger | 16fa8b4 | 2025-05-02 04:28:16 +0000 | [diff] [blame] | 1170 | id="container-status" |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 1171 | ></sketch-container-status> |
| Autoformatter | cf57096 | 2025-04-30 17:27:39 +0000 | [diff] [blame] | 1172 | |
| Philip Zeyliger | 16fa8b4 | 2025-05-02 04:28:16 +0000 | [diff] [blame] | 1173 | <!-- Last Commit section moved to sketch-container-status --> |
| Philip Zeyliger | bce3a13 | 2025-04-30 22:03:39 +0000 | [diff] [blame] | 1174 | |
| Philip Zeyliger | 16fa8b4 | 2025-05-02 04:28:16 +0000 | [diff] [blame] | 1175 | <!-- Views section with tabs --> |
| 1176 | <sketch-view-mode-select></sketch-view-mode-select> |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 1177 | |
| 1178 | <div class="refresh-control"> |
| Sean McCullough | d3906e2 | 2025-04-29 17:32:14 +0000 | [diff] [blame] | 1179 | <button |
| Philip Zeyliger | bce3a13 | 2025-04-30 22:03:39 +0000 | [diff] [blame] | 1180 | id="stopButton" |
| 1181 | class="stop-button" |
| 1182 | ?disabled=${(this.containerState?.outstanding_llm_calls || 0) === |
| 1183 | 0 && |
| 1184 | (this.containerState?.outstanding_tool_calls || []).length === 0} |
| 1185 | > |
| 1186 | <svg |
| 1187 | class="button-icon" |
| 1188 | xmlns="http://www.w3.org/2000/svg" |
| 1189 | viewBox="0 0 24 24" |
| 1190 | fill="none" |
| 1191 | stroke="currentColor" |
| 1192 | stroke-width="2" |
| 1193 | stroke-linecap="round" |
| 1194 | stroke-linejoin="round" |
| 1195 | > |
| 1196 | <rect x="6" y="6" width="12" height="12" /> |
| 1197 | </svg> |
| 1198 | <span class="button-text">Stop</span> |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 1199 | </button> |
| Pokey Rule | 397871d | 2025-05-19 15:02:45 +0100 | [diff] [blame] | 1200 | <button |
| 1201 | id="endButton" |
| 1202 | class="end-button" |
| 1203 | @click=${this._handleEndClick} |
| 1204 | > |
| 1205 | <svg |
| 1206 | class="button-icon" |
| 1207 | xmlns="http://www.w3.org/2000/svg" |
| 1208 | viewBox="0 0 24 24" |
| 1209 | fill="none" |
| 1210 | stroke="currentColor" |
| 1211 | stroke-width="2" |
| 1212 | stroke-linecap="round" |
| 1213 | stroke-linejoin="round" |
| 1214 | > |
| 1215 | <path d="M18 6L6 18" /> |
| 1216 | <path d="M6 6l12 12" /> |
| 1217 | </svg> |
| 1218 | <span class="button-text">End</span> |
| 1219 | </button> |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 1220 | |
| Philip Zeyliger | db5e9b4 | 2025-04-30 19:58:13 +0000 | [diff] [blame] | 1221 | <div |
| 1222 | class="notifications-toggle" |
| 1223 | @click=${this._handleNotificationsToggle} |
| 1224 | title="${this.notificationsEnabled |
| 1225 | ? "Disable" |
| Philip Zeyliger | bce3a13 | 2025-04-30 22:03:39 +0000 | [diff] [blame] | 1226 | : "Enable"} notifications when the agent completes its turn" |
| Philip Zeyliger | db5e9b4 | 2025-04-30 19:58:13 +0000 | [diff] [blame] | 1227 | > |
| 1228 | <div |
| 1229 | class="bell-icon ${!this.notificationsEnabled |
| 1230 | ? "bell-disabled" |
| 1231 | : ""}" |
| 1232 | > |
| 1233 | <!-- Bell SVG icon --> |
| 1234 | <svg |
| 1235 | xmlns="http://www.w3.org/2000/svg" |
| 1236 | width="16" |
| 1237 | height="16" |
| 1238 | fill="currentColor" |
| 1239 | viewBox="0 0 16 16" |
| 1240 | > |
| 1241 | <path |
| 1242 | d="M8 16a2 2 0 0 0 2-2H6a2 2 0 0 0 2 2zM8 1.918l-.797.161A4.002 4.002 0 0 0 4 6c0 .628-.134 2.197-.459 3.742-.16.767-.376 1.566-.663 2.258h10.244c-.287-.692-.502-1.49-.663-2.258C12.134 8.197 12 6.628 12 6a4.002 4.002 0 0 0-3.203-3.92L8 1.917zM14.22 12c.223.447.481.801.78 1H1c.299-.199.557-.553.78-1C2.68 10.2 3 6.88 3 6c0-2.42 1.72-4.44 4.005-4.901a1 1 0 1 1 1.99 0A5.002 5.002 0 0 1 13 6c0 .88.32 4.2 1.22 6z" |
| 1243 | /> |
| 1244 | </svg> |
| 1245 | </div> |
| Philip Zeyliger | bc6b629 | 2025-04-30 18:00:15 +0000 | [diff] [blame] | 1246 | </div> |
| 1247 | |
| Philip Zeyliger | 99a9a02 | 2025-04-27 15:15:25 +0000 | [diff] [blame] | 1248 | <sketch-call-status |
| Sean McCullough | d9d4581 | 2025-04-30 16:53:41 -0700 | [diff] [blame] | 1249 | .agentState=${this.containerState?.agent_state} |
| Philip Zeyliger | 99a9a02 | 2025-04-27 15:15:25 +0000 | [diff] [blame] | 1250 | .llmCalls=${this.containerState?.outstanding_llm_calls || 0} |
| 1251 | .toolCalls=${this.containerState?.outstanding_tool_calls || []} |
| Philip Zeyliger | 7231839 | 2025-05-14 02:56:07 +0000 | [diff] [blame] | 1252 | .isIdle=${this.messages.length > 0 |
| 1253 | ? this.messages[this.messages.length - 1]?.end_of_turn && |
| 1254 | !this.messages[this.messages.length - 1]?.parent_conversation_id |
| 1255 | : true} |
| Philip Zeyliger | 5e35702 | 2025-05-16 04:50:34 +0000 | [diff] [blame] | 1256 | .isDisconnected=${this.connectionStatus === "disconnected"} |
| Philip Zeyliger | 99a9a02 | 2025-04-27 15:15:25 +0000 | [diff] [blame] | 1257 | ></sketch-call-status> |
| Philip Zeyliger | 25f6ff1 | 2025-05-02 04:24:10 +0000 | [diff] [blame] | 1258 | |
| 1259 | <sketch-network-status |
| 1260 | connection=${this.connectionStatus} |
| 1261 | error=${this.connectionErrorMessage} |
| 1262 | ></sketch-network-status> |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 1263 | </div> |
| 1264 | </div> |
| 1265 | |
| Pokey Rule | 4097e53 | 2025-04-24 18:55:28 +0100 | [diff] [blame] | 1266 | <div id="view-container" ${ref(this.scrollContainerRef)}> |
| Josh Bleecher Snyder | 112b923 | 2025-05-23 11:26:33 -0700 | [diff] [blame] | 1267 | <div |
| 1268 | id="view-container-inner" |
| 1269 | class="${this._todoPanelVisible && this.viewMode === "chat" |
| 1270 | ? "with-todo-panel" |
| 1271 | : ""}" |
| 1272 | > |
| Pokey Rule | 4097e53 | 2025-04-24 18:55:28 +0100 | [diff] [blame] | 1273 | <div |
| 1274 | class="chat-view ${this.viewMode === "chat" ? "view-active" : ""}" |
| 1275 | > |
| Josh Bleecher Snyder | 112b923 | 2025-05-23 11:26:33 -0700 | [diff] [blame] | 1276 | <div |
| 1277 | class="chat-timeline-container ${this._todoPanelVisible && |
| 1278 | this.viewMode === "chat" |
| 1279 | ? "with-todo-panel" |
| 1280 | : ""}" |
| 1281 | > |
| 1282 | <sketch-timeline |
| 1283 | .messages=${this.messages} |
| 1284 | .scrollContainer=${this.scrollContainerRef} |
| 1285 | .agentState=${this.containerState?.agent_state} |
| 1286 | .llmCalls=${this.containerState?.outstanding_llm_calls || 0} |
| 1287 | .toolCalls=${this.containerState?.outstanding_tool_calls || []} |
| Philip Zeyliger | b8a8f35 | 2025-06-02 07:39:37 -0700 | [diff] [blame] | 1288 | .firstMessageIndex=${this.containerState?.first_message_index || |
| 1289 | 0} |
| Josh Bleecher Snyder | 112b923 | 2025-05-23 11:26:33 -0700 | [diff] [blame] | 1290 | ></sketch-timeline> |
| 1291 | </div> |
| 1292 | </div> |
| 1293 | |
| 1294 | <!-- Todo panel positioned outside the main flow - only visible in chat view --> |
| 1295 | <div |
| 1296 | class="todo-panel-container ${this._todoPanelVisible && |
| 1297 | this.viewMode === "chat" |
| 1298 | ? "visible" |
| 1299 | : ""}" |
| 1300 | > |
| 1301 | <sketch-todo-panel |
| 1302 | .visible=${this._todoPanelVisible && this.viewMode === "chat"} |
| 1303 | ></sketch-todo-panel> |
| Pokey Rule | 4097e53 | 2025-04-24 18:55:28 +0100 | [diff] [blame] | 1304 | </div> |
| 1305 | <div |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 1306 | class="diff2-view ${this.viewMode === "diff2" ? "view-active" : ""}" |
| 1307 | > |
| 1308 | <sketch-diff2-view |
| 1309 | .commit=${this.currentCommitHash} |
| 1310 | .gitService=${new DefaultGitDataService()} |
| 1311 | @diff-comment="${this._handleDiffComment}" |
| 1312 | ></sketch-diff2-view> |
| 1313 | </div> |
| Philip Zeyliger | 2d4c48f | 2025-05-02 23:35:03 +0000 | [diff] [blame] | 1314 | |
| Pokey Rule | 4097e53 | 2025-04-24 18:55:28 +0100 | [diff] [blame] | 1315 | <div |
| 1316 | class="terminal-view ${this.viewMode === "terminal" |
| 1317 | ? "view-active" |
| 1318 | : ""}" |
| 1319 | > |
| 1320 | <sketch-terminal></sketch-terminal> |
| 1321 | </div> |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 1322 | </div> |
| 1323 | </div> |
| 1324 | |
| Pokey Rule | 4097e53 | 2025-04-24 18:55:28 +0100 | [diff] [blame] | 1325 | <div id="chat-input"> |
| 1326 | <sketch-chat-input @send-chat="${this._sendChat}"></sketch-chat-input> |
| 1327 | </div> |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 1328 | `; |
| 1329 | } |
| 1330 | |
| 1331 | /** |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 1332 | * Lifecycle callback when component is first connected to DOM |
| 1333 | */ |
| 1334 | firstUpdated(): void { |
| 1335 | if (this.viewMode !== "chat") { |
| 1336 | return; |
| 1337 | } |
| 1338 | |
| 1339 | // Initial scroll to bottom when component is first rendered |
| 1340 | setTimeout( |
| 1341 | () => this.scrollTo({ top: this.scrollHeight, behavior: "smooth" }), |
| Philip Zeyliger | 72682df | 2025-04-23 13:09:46 -0700 | [diff] [blame] | 1342 | 50, |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 1343 | ); |
| 1344 | |
| Philip Zeyliger | 2c4db09 | 2025-04-28 16:57:50 -0700 | [diff] [blame] | 1345 | // Setup stop button |
| 1346 | const stopButton = this.renderRoot?.querySelector( |
| 1347 | "#stopButton", |
| 1348 | ) as HTMLButtonElement; |
| 1349 | stopButton?.addEventListener("click", async () => { |
| 1350 | try { |
| Sean McCullough | 495cb96 | 2025-05-01 16:25:53 -0700 | [diff] [blame] | 1351 | const response = await fetch("cancel", { |
| Philip Zeyliger | 2c4db09 | 2025-04-28 16:57:50 -0700 | [diff] [blame] | 1352 | method: "POST", |
| 1353 | headers: { |
| 1354 | "Content-Type": "application/json", |
| 1355 | }, |
| 1356 | body: JSON.stringify({ reason: "User clicked stop button" }), |
| 1357 | }); |
| 1358 | if (!response.ok) { |
| 1359 | console.error("Failed to cancel:", await response.text()); |
| 1360 | } |
| 1361 | } catch (error) { |
| 1362 | console.error("Error cancelling operation:", error); |
| 1363 | } |
| 1364 | }); |
| 1365 | |
| Pokey Rule | 397871d | 2025-05-19 15:02:45 +0100 | [diff] [blame] | 1366 | // Setup end button |
| 1367 | const endButton = this.renderRoot?.querySelector( |
| 1368 | "#endButton", |
| 1369 | ) as HTMLButtonElement; |
| 1370 | // We're already using the @click binding in the HTML, so manual event listener not needed here |
| 1371 | |
| Philip Zeyliger | 47b71c9 | 2025-04-30 15:43:39 +0000 | [diff] [blame] | 1372 | // Process any existing messages to find commit information |
| 1373 | if (this.messages && this.messages.length > 0) { |
| Philip Zeyliger | 16fa8b4 | 2025-05-02 04:28:16 +0000 | [diff] [blame] | 1374 | // Update last commit info via container status component |
| 1375 | if (this.containerStatusElement) { |
| 1376 | this.containerStatusElement.updateLastCommitInfo(this.messages); |
| 1377 | } |
| Philip Zeyliger | 47b71c9 | 2025-04-30 15:43:39 +0000 | [diff] [blame] | 1378 | } |
| Josh Bleecher Snyder | 112b923 | 2025-05-23 11:26:33 -0700 | [diff] [blame] | 1379 | |
| 1380 | // Set up chat input height observer for todo panel |
| 1381 | this.setupChatInputObserver(); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 1382 | } |
| 1383 | } |
| 1384 | |
| 1385 | declare global { |
| 1386 | interface HTMLElementTagNameMap { |
| 1387 | "sketch-app-shell": SketchAppShell; |
| 1388 | } |
| 1389 | } |