| 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"; |
| 9 | import "./sketch-diff-view"; |
| 10 | import { SketchDiffView } from "./sketch-diff-view"; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 11 | import "./sketch-diff2-view"; |
| 12 | import { SketchDiff2View } from "./sketch-diff2-view"; |
| 13 | import { DefaultGitDataService } from "./git-data-service"; |
| 14 | import "./sketch-monaco-view"; |
| Pokey Rule | 4097e53 | 2025-04-24 18:55:28 +0100 | [diff] [blame] | 15 | import "./sketch-network-status"; |
| Philip Zeyliger | 99a9a02 | 2025-04-27 15:15:25 +0000 | [diff] [blame] | 16 | import "./sketch-call-status"; |
| Pokey Rule | 4097e53 | 2025-04-24 18:55:28 +0100 | [diff] [blame] | 17 | import "./sketch-terminal"; |
| 18 | import "./sketch-timeline"; |
| 19 | import "./sketch-view-mode-select"; |
| 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 | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 24 | type ViewMode = "chat" | "diff" | "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 | |
| 142 | #chat-input { |
| 143 | align-self: flex-end; |
| 144 | width: 100%; |
| 145 | box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.1); |
| 146 | } |
| 147 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 148 | .banner-title { |
| 149 | font-size: 18px; |
| 150 | font-weight: 600; |
| 151 | margin: 0; |
| 152 | min-width: 6em; |
| 153 | white-space: nowrap; |
| 154 | overflow: hidden; |
| 155 | text-overflow: ellipsis; |
| 156 | } |
| 157 | |
| 158 | .chat-title { |
| 159 | margin: 0; |
| 160 | padding: 0; |
| 161 | color: rgba(82, 82, 82, 0.85); |
| Josh Bleecher Snyder | eb5166a | 2025-04-30 17:04:20 +0000 | [diff] [blame] | 162 | font-size: 14px; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 163 | font-weight: normal; |
| 164 | font-style: italic; |
| 165 | white-space: nowrap; |
| 166 | overflow: hidden; |
| 167 | text-overflow: ellipsis; |
| 168 | } |
| 169 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 170 | /* Allow the container to expand to full width and height in diff mode */ |
| 171 | #view-container-inner.diff-active, |
| 172 | #view-container-inner.diff2-active { |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 173 | max-width: 100%; |
| Pokey Rule | 46fff97 | 2025-04-25 14:57:44 +0100 | [diff] [blame] | 174 | width: 100%; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 175 | height: 100%; |
| 176 | padding: 0; /* Remove padding for more space */ |
| 177 | display: flex; |
| 178 | flex-direction: column; |
| 179 | flex: 1; |
| 180 | min-height: 0; /* Critical for flex behavior */ |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | /* Individual view styles */ |
| 184 | .chat-view, |
| 185 | .diff-view, |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 186 | .diff2-view, |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 187 | .terminal-view { |
| 188 | display: none; /* Hidden by default */ |
| 189 | width: 100%; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 190 | height: 100%; |
| 191 | } |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 192 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 193 | /* Make chat view take full width available */ |
| 194 | .chat-view.view-active { |
| 195 | display: flex; |
| 196 | flex-direction: column; |
| 197 | width: 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 | /* Monaco diff2 view needs to take all available space */ |
| 201 | .diff2-view.view-active { |
| 202 | flex: 1; |
| 203 | overflow: hidden; |
| 204 | min-height: 0; /* Required for proper flex child behavior */ |
| 205 | display: flex; |
| 206 | flex-direction: column; |
| 207 | height: 100%; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | /* Active view styles - these will be applied via JavaScript */ |
| 211 | .view-active { |
| 212 | display: flex; |
| 213 | flex-direction: column; |
| 214 | } |
| 215 | |
| 216 | .title-container { |
| 217 | display: flex; |
| 218 | flex-direction: column; |
| 219 | white-space: nowrap; |
| 220 | overflow: hidden; |
| 221 | text-overflow: ellipsis; |
| Josh Bleecher Snyder | eb5166a | 2025-04-30 17:04:20 +0000 | [diff] [blame] | 222 | max-width: 30%; |
| Philip Zeyliger | e66db3e | 2025-04-27 15:40:39 +0000 | [diff] [blame] | 223 | padding: 6px 0; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | .refresh-control { |
| 227 | display: flex; |
| 228 | align-items: center; |
| 229 | margin-bottom: 0; |
| 230 | flex-wrap: nowrap; |
| 231 | white-space: nowrap; |
| 232 | flex-shrink: 0; |
| Philip Zeyliger | e66db3e | 2025-04-27 15:40:39 +0000 | [diff] [blame] | 233 | gap: 15px; |
| 234 | padding-left: 15px; |
| 235 | margin-right: 50px; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| Pokey Rule | 397871d | 2025-05-19 15:02:45 +0100 | [diff] [blame] | 238 | .stop-button, |
| 239 | .end-button { |
| Philip Zeyliger | 2c4db09 | 2025-04-28 16:57:50 -0700 | [diff] [blame] | 240 | background: #2196f3; |
| 241 | color: white; |
| 242 | border: none; |
| 243 | padding: 4px 10px; |
| 244 | border-radius: 4px; |
| 245 | cursor: pointer; |
| 246 | font-size: 12px; |
| 247 | margin-right: 5px; |
| Philip Zeyliger | bce3a13 | 2025-04-30 22:03:39 +0000 | [diff] [blame] | 248 | display: flex; |
| 249 | align-items: center; |
| 250 | gap: 6px; |
| Philip Zeyliger | 2c4db09 | 2025-04-28 16:57:50 -0700 | [diff] [blame] | 251 | } |
| 252 | |
| Philip Zeyliger | bce3a13 | 2025-04-30 22:03:39 +0000 | [diff] [blame] | 253 | .stop-button { |
| 254 | background: #dc3545; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 255 | color: white; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| Philip Zeyliger | bce3a13 | 2025-04-30 22:03:39 +0000 | [diff] [blame] | 258 | .stop-button:hover:not(:disabled) { |
| 259 | background-color: #c82333; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 260 | } |
| 261 | |
| Philip Zeyliger | bce3a13 | 2025-04-30 22:03:39 +0000 | [diff] [blame] | 262 | .stop-button:disabled { |
| 263 | background-color: #e9a8ad; |
| 264 | cursor: not-allowed; |
| 265 | opacity: 0.7; |
| Philip Zeyliger | db5e9b4 | 2025-04-30 19:58:13 +0000 | [diff] [blame] | 266 | } |
| 267 | |
| Philip Zeyliger | bce3a13 | 2025-04-30 22:03:39 +0000 | [diff] [blame] | 268 | .stop-button:disabled:hover { |
| 269 | background-color: #e9a8ad; |
| 270 | } |
| 271 | |
| Pokey Rule | 397871d | 2025-05-19 15:02:45 +0100 | [diff] [blame] | 272 | .end-button { |
| 273 | background: #6c757d; |
| 274 | color: white; |
| 275 | } |
| 276 | |
| 277 | .end-button:hover:not(:disabled) { |
| 278 | background-color: #5a6268; |
| 279 | } |
| 280 | |
| 281 | .end-button:disabled { |
| 282 | background-color: #a9acaf; |
| 283 | cursor: not-allowed; |
| 284 | opacity: 0.7; |
| 285 | } |
| 286 | |
| Philip Zeyliger | bce3a13 | 2025-04-30 22:03:39 +0000 | [diff] [blame] | 287 | .button-icon { |
| 288 | width: 16px; |
| 289 | height: 16px; |
| 290 | } |
| 291 | |
| 292 | @media (max-width: 1400px) { |
| 293 | .button-text { |
| 294 | display: none; |
| 295 | } |
| 296 | |
| Philip Zeyliger | bce3a13 | 2025-04-30 22:03:39 +0000 | [diff] [blame] | 297 | .stop-button { |
| 298 | padding: 6px; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | /* Removed poll-updates class */ |
| 303 | |
| Philip Zeyliger | bc6b629 | 2025-04-30 18:00:15 +0000 | [diff] [blame] | 304 | .notifications-toggle { |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 305 | display: flex; |
| 306 | align-items: center; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 307 | font-size: 12px; |
| Philip Zeyliger | bc6b629 | 2025-04-30 18:00:15 +0000 | [diff] [blame] | 308 | margin-right: 10px; |
| Philip Zeyliger | db5e9b4 | 2025-04-30 19:58:13 +0000 | [diff] [blame] | 309 | cursor: pointer; |
| 310 | } |
| 311 | |
| 312 | .bell-icon { |
| 313 | width: 20px; |
| 314 | height: 20px; |
| 315 | position: relative; |
| 316 | display: inline-flex; |
| 317 | align-items: center; |
| 318 | justify-content: center; |
| 319 | } |
| 320 | |
| 321 | .bell-disabled::before { |
| 322 | content: ""; |
| 323 | position: absolute; |
| 324 | width: 2px; |
| 325 | height: 24px; |
| 326 | background-color: #dc3545; |
| 327 | transform: rotate(45deg); |
| 328 | transform-origin: center center; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 329 | } |
| 330 | `; |
| 331 | |
| 332 | // Header bar: Network connection status details |
| 333 | @property() |
| 334 | connectionStatus: ConnectionStatus = "disconnected"; |
| Autoformatter | cf57096 | 2025-04-30 17:27:39 +0000 | [diff] [blame] | 335 | |
| Philip Zeyliger | 47b71c9 | 2025-04-30 15:43:39 +0000 | [diff] [blame] | 336 | // Track if the last commit info has been copied |
| 337 | @state() |
| Philip Zeyliger | 16fa8b4 | 2025-05-02 04:28:16 +0000 | [diff] [blame] | 338 | // lastCommitCopied moved to sketch-container-status |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 339 | |
| Philip Zeyliger | bc6b629 | 2025-04-30 18:00:15 +0000 | [diff] [blame] | 340 | // Track notification preferences |
| 341 | @state() |
| Philip Zeyliger | db5e9b4 | 2025-04-30 19:58:13 +0000 | [diff] [blame] | 342 | notificationsEnabled: boolean = false; |
| 343 | |
| 344 | // Track if the window is focused to control notifications |
| 345 | @state() |
| 346 | private _windowFocused: boolean = document.hasFocus(); |
| Philip Zeyliger | bc6b629 | 2025-04-30 18:00:15 +0000 | [diff] [blame] | 347 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 348 | @property() |
| 349 | connectionErrorMessage: string = ""; |
| 350 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 351 | // Chat messages |
| Pokey Rule | e2a8c2f | 2025-04-23 15:09:25 +0100 | [diff] [blame] | 352 | @property({ attribute: false }) |
| Sean McCullough | d9f1337 | 2025-04-21 15:08:49 -0700 | [diff] [blame] | 353 | messages: AgentMessage[] = []; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 354 | |
| 355 | @property() |
| Philip Zeyliger | 9b999b0 | 2025-04-25 16:31:50 +0000 | [diff] [blame] | 356 | set title(value: string) { |
| 357 | const oldValue = this._title; |
| 358 | this._title = value; |
| 359 | this.requestUpdate("title", oldValue); |
| 360 | // Update document title when title property changes |
| 361 | this.updateDocumentTitle(); |
| 362 | } |
| 363 | |
| 364 | get title(): string { |
| 365 | return this._title; |
| 366 | } |
| 367 | |
| 368 | private _title: string = ""; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 369 | |
| 370 | private dataManager = new DataManager(); |
| 371 | |
| Pokey Rule | e2a8c2f | 2025-04-23 15:09:25 +0100 | [diff] [blame] | 372 | @property({ attribute: false }) |
| Sean McCullough | d9f1337 | 2025-04-21 15:08:49 -0700 | [diff] [blame] | 373 | containerState: State = { |
| Philip Zeyliger | d03318d | 2025-05-08 13:09:12 -0700 | [diff] [blame] | 374 | state_version: 2, |
| Sean McCullough | d9f1337 | 2025-04-21 15:08:49 -0700 | [diff] [blame] | 375 | title: "", |
| 376 | os: "", |
| 377 | message_count: 0, |
| 378 | hostname: "", |
| 379 | working_dir: "", |
| 380 | initial_commit: "", |
| Philip Zeyliger | 99a9a02 | 2025-04-27 15:15:25 +0000 | [diff] [blame] | 381 | outstanding_llm_calls: 0, |
| 382 | outstanding_tool_calls: [], |
| Philip Zeyliger | c72fff5 | 2025-04-29 20:17:54 +0000 | [diff] [blame] | 383 | session_id: "", |
| 384 | ssh_available: false, |
| Philip Zeyliger | 2c4db09 | 2025-04-28 16:57:50 -0700 | [diff] [blame] | 385 | ssh_error: "", |
| 386 | in_container: false, |
| 387 | first_message_index: 0, |
| Sean McCullough | d9f1337 | 2025-04-21 15:08:49 -0700 | [diff] [blame] | 388 | }; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 389 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 390 | // Mutation observer to detect when new messages are added |
| 391 | private mutationObserver: MutationObserver | null = null; |
| 392 | |
| 393 | constructor() { |
| 394 | super(); |
| 395 | |
| Philip Zeyliger | 16fa8b4 | 2025-05-02 04:28:16 +0000 | [diff] [blame] | 396 | // Reference to the container status element |
| 397 | this.containerStatusElement = null; |
| 398 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 399 | // Binding methods to this |
| 400 | this._handleViewModeSelect = this._handleViewModeSelect.bind(this); |
| Sean McCullough | 34bb09a | 2025-05-13 15:39:54 -0700 | [diff] [blame] | 401 | this._handlePopState = this._handlePopState.bind(this); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 402 | this._handleShowCommitDiff = this._handleShowCommitDiff.bind(this); |
| Sean McCullough | 485afc6 | 2025-04-28 14:28:39 -0700 | [diff] [blame] | 403 | this._handleMutlipleChoiceSelected = |
| 404 | this._handleMutlipleChoiceSelected.bind(this); |
| Sean McCullough | d3906e2 | 2025-04-29 17:32:14 +0000 | [diff] [blame] | 405 | this._handleStopClick = this._handleStopClick.bind(this); |
| Pokey Rule | 397871d | 2025-05-19 15:02:45 +0100 | [diff] [blame] | 406 | this._handleEndClick = this._handleEndClick.bind(this); |
| Philip Zeyliger | bc6b629 | 2025-04-30 18:00:15 +0000 | [diff] [blame] | 407 | this._handleNotificationsToggle = |
| 408 | this._handleNotificationsToggle.bind(this); |
| Philip Zeyliger | db5e9b4 | 2025-04-30 19:58:13 +0000 | [diff] [blame] | 409 | this._handleWindowFocus = this._handleWindowFocus.bind(this); |
| 410 | this._handleWindowBlur = this._handleWindowBlur.bind(this); |
| Philip Zeyliger | bc6b629 | 2025-04-30 18:00:15 +0000 | [diff] [blame] | 411 | |
| 412 | // Load notification preference from localStorage |
| 413 | try { |
| 414 | const savedPref = localStorage.getItem("sketch-notifications-enabled"); |
| 415 | if (savedPref !== null) { |
| 416 | this.notificationsEnabled = savedPref === "true"; |
| 417 | } |
| 418 | } catch (error) { |
| 419 | console.error("Error loading notification preference:", error); |
| 420 | } |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | // See https://lit.dev/docs/components/lifecycle/ |
| 424 | connectedCallback() { |
| 425 | super.connectedCallback(); |
| 426 | |
| Philip Zeyliger | 16fa8b4 | 2025-05-02 04:28:16 +0000 | [diff] [blame] | 427 | // Get reference to the container status element |
| 428 | setTimeout(() => { |
| 429 | this.containerStatusElement = |
| 430 | this.shadowRoot?.getElementById("container-status"); |
| 431 | }, 0); |
| 432 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 433 | // Initialize client-side nav history. |
| 434 | const url = new URL(window.location.href); |
| 435 | const mode = url.searchParams.get("view") || "chat"; |
| 436 | window.history.replaceState({ mode }, "", url.toString()); |
| 437 | |
| 438 | this.toggleViewMode(mode as ViewMode, false); |
| 439 | // Add popstate event listener to handle browser back/forward navigation |
| Pokey Rule | e2a8c2f | 2025-04-23 15:09:25 +0100 | [diff] [blame] | 440 | window.addEventListener("popstate", this._handlePopState); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 441 | |
| 442 | // Add event listeners |
| Pokey Rule | e2a8c2f | 2025-04-23 15:09:25 +0100 | [diff] [blame] | 443 | window.addEventListener("view-mode-select", this._handleViewModeSelect); |
| Pokey Rule | e2a8c2f | 2025-04-23 15:09:25 +0100 | [diff] [blame] | 444 | window.addEventListener("show-commit-diff", this._handleShowCommitDiff); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 445 | |
| Philip Zeyliger | db5e9b4 | 2025-04-30 19:58:13 +0000 | [diff] [blame] | 446 | // Add window focus/blur listeners for controlling notifications |
| 447 | window.addEventListener("focus", this._handleWindowFocus); |
| 448 | window.addEventListener("blur", this._handleWindowBlur); |
| Sean McCullough | 485afc6 | 2025-04-28 14:28:39 -0700 | [diff] [blame] | 449 | window.addEventListener( |
| 450 | "multiple-choice-selected", |
| 451 | this._handleMutlipleChoiceSelected, |
| 452 | ); |
| Philip Zeyliger | db5e9b4 | 2025-04-30 19:58:13 +0000 | [diff] [blame] | 453 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 454 | // register event listeners |
| 455 | this.dataManager.addEventListener( |
| 456 | "dataChanged", |
| Philip Zeyliger | 72682df | 2025-04-23 13:09:46 -0700 | [diff] [blame] | 457 | this.handleDataChanged.bind(this), |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 458 | ); |
| 459 | this.dataManager.addEventListener( |
| 460 | "connectionStatusChanged", |
| Philip Zeyliger | 72682df | 2025-04-23 13:09:46 -0700 | [diff] [blame] | 461 | this.handleConnectionStatusChanged.bind(this), |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 462 | ); |
| 463 | |
| Philip Zeyliger | 9b999b0 | 2025-04-25 16:31:50 +0000 | [diff] [blame] | 464 | // Set initial document title |
| 465 | this.updateDocumentTitle(); |
| 466 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 467 | // Initialize the data manager |
| 468 | this.dataManager.initialize(); |
| Autoformatter | cf57096 | 2025-04-30 17:27:39 +0000 | [diff] [blame] | 469 | |
| Philip Zeyliger | 47b71c9 | 2025-04-30 15:43:39 +0000 | [diff] [blame] | 470 | // Process existing messages for commit info |
| 471 | if (this.messages && this.messages.length > 0) { |
| Philip Zeyliger | 16fa8b4 | 2025-05-02 04:28:16 +0000 | [diff] [blame] | 472 | // Update last commit info via container status component |
| 473 | setTimeout(() => { |
| 474 | if (this.containerStatusElement) { |
| 475 | this.containerStatusElement.updateLastCommitInfo(this.messages); |
| 476 | } |
| 477 | }, 100); |
| Philip Zeyliger | 47b71c9 | 2025-04-30 15:43:39 +0000 | [diff] [blame] | 478 | } |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | // See https://lit.dev/docs/components/lifecycle/ |
| 482 | disconnectedCallback() { |
| 483 | super.disconnectedCallback(); |
| Pokey Rule | e2a8c2f | 2025-04-23 15:09:25 +0100 | [diff] [blame] | 484 | window.removeEventListener("popstate", this._handlePopState); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 485 | |
| 486 | // Remove event listeners |
| Pokey Rule | e2a8c2f | 2025-04-23 15:09:25 +0100 | [diff] [blame] | 487 | window.removeEventListener("view-mode-select", this._handleViewModeSelect); |
| Pokey Rule | e2a8c2f | 2025-04-23 15:09:25 +0100 | [diff] [blame] | 488 | window.removeEventListener("show-commit-diff", this._handleShowCommitDiff); |
| Philip Zeyliger | db5e9b4 | 2025-04-30 19:58:13 +0000 | [diff] [blame] | 489 | window.removeEventListener("focus", this._handleWindowFocus); |
| 490 | window.removeEventListener("blur", this._handleWindowBlur); |
| Sean McCullough | 485afc6 | 2025-04-28 14:28:39 -0700 | [diff] [blame] | 491 | window.removeEventListener( |
| 492 | "multiple-choice-selected", |
| 493 | this._handleMutlipleChoiceSelected, |
| 494 | ); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 495 | |
| 496 | // unregister data manager event listeners |
| 497 | this.dataManager.removeEventListener( |
| 498 | "dataChanged", |
| Philip Zeyliger | 72682df | 2025-04-23 13:09:46 -0700 | [diff] [blame] | 499 | this.handleDataChanged.bind(this), |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 500 | ); |
| 501 | this.dataManager.removeEventListener( |
| 502 | "connectionStatusChanged", |
| Philip Zeyliger | 72682df | 2025-04-23 13:09:46 -0700 | [diff] [blame] | 503 | this.handleConnectionStatusChanged.bind(this), |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 504 | ); |
| 505 | |
| 506 | // Disconnect mutation observer if it exists |
| 507 | if (this.mutationObserver) { |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 508 | this.mutationObserver.disconnect(); |
| 509 | this.mutationObserver = null; |
| 510 | } |
| 511 | } |
| 512 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 513 | updateUrlForViewMode(mode: ViewMode): void { |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 514 | // Get the current URL without search parameters |
| 515 | const url = new URL(window.location.href); |
| 516 | |
| 517 | // Clear existing parameters |
| 518 | url.search = ""; |
| 519 | |
| 520 | // Only add view parameter if not in default chat view |
| 521 | if (mode !== "chat") { |
| 522 | url.searchParams.set("view", mode); |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 523 | const diffView = this.shadowRoot?.querySelector( |
| Philip Zeyliger | 72682df | 2025-04-23 13:09:46 -0700 | [diff] [blame] | 524 | ".diff-view", |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 525 | ) as SketchDiffView; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 526 | |
| 527 | // If in diff view and there's a commit hash, include that too |
| 528 | if (mode === "diff" && diffView.commitHash) { |
| 529 | url.searchParams.set("commit", diffView.commitHash); |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | // Update the browser history without reloading the page |
| 534 | window.history.pushState({ mode }, "", url.toString()); |
| 535 | } |
| 536 | |
| Pokey Rule | e2a8c2f | 2025-04-23 15:09:25 +0100 | [diff] [blame] | 537 | private _handlePopState(event: PopStateEvent) { |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 538 | if (event.state && event.state.mode) { |
| 539 | this.toggleViewMode(event.state.mode, false); |
| 540 | } else { |
| 541 | this.toggleViewMode("chat", false); |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | /** |
| 546 | * Handle view mode selection event |
| 547 | */ |
| 548 | private _handleViewModeSelect(event: CustomEvent) { |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 549 | const mode = event.detail.mode as "chat" | "diff" | "diff2" | "terminal"; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 550 | this.toggleViewMode(mode, true); |
| 551 | } |
| 552 | |
| 553 | /** |
| 554 | * Handle show commit diff event |
| 555 | */ |
| 556 | private _handleShowCommitDiff(event: CustomEvent) { |
| 557 | const { commitHash } = event.detail; |
| 558 | if (commitHash) { |
| 559 | this.showCommitDiff(commitHash); |
| 560 | } |
| 561 | } |
| 562 | |
| Sean McCullough | 485afc6 | 2025-04-28 14:28:39 -0700 | [diff] [blame] | 563 | private _handleMultipleChoice(event: CustomEvent) { |
| 564 | window.console.log("_handleMultipleChoice", event); |
| 565 | this._sendChat; |
| 566 | } |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 567 | |
| 568 | private _handleDiffComment(event: CustomEvent) { |
| 569 | // Empty stub required by the event binding in the template |
| 570 | // Actual handling occurs at global level in sketch-chat-input component |
| 571 | } |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 572 | /** |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 573 | * Listen for commit diff event |
| 574 | * @param commitHash The commit hash to show diff for |
| 575 | */ |
| Pokey Rule | e2a8c2f | 2025-04-23 15:09:25 +0100 | [diff] [blame] | 576 | private showCommitDiff(commitHash: string): void { |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 577 | // Store the commit hash |
| 578 | this.currentCommitHash = commitHash; |
| 579 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 580 | this.toggleViewMode("diff2", true); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 581 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 582 | this.updateComplete.then(() => { |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 583 | const diff2View = this.shadowRoot?.querySelector("sketch-diff2-view"); |
| 584 | if (diff2View) { |
| 585 | (diff2View as SketchDiff2View).refreshDiffView(); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 586 | } |
| 587 | }); |
| 588 | } |
| 589 | |
| 590 | /** |
| Philip Zeyliger | 2d4c48f | 2025-05-02 23:35:03 +0000 | [diff] [blame] | 591 | * Toggle between different view modes: chat, diff, terminal |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 592 | */ |
| Pokey Rule | e2a8c2f | 2025-04-23 15:09:25 +0100 | [diff] [blame] | 593 | private toggleViewMode(mode: ViewMode, updateHistory: boolean): void { |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 594 | // Don't do anything if the mode is already active |
| 595 | if (this.viewMode === mode) return; |
| 596 | |
| 597 | // Update the view mode |
| 598 | this.viewMode = mode; |
| 599 | |
| 600 | if (updateHistory) { |
| 601 | // Update URL with the current view mode |
| 602 | this.updateUrlForViewMode(mode); |
| 603 | } |
| 604 | |
| 605 | // Wait for DOM update to complete |
| 606 | this.updateComplete.then(() => { |
| 607 | // Update active view |
| Pokey Rule | 46fff97 | 2025-04-25 14:57:44 +0100 | [diff] [blame] | 608 | const viewContainerInner = this.shadowRoot?.querySelector( |
| 609 | "#view-container-inner", |
| 610 | ); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 611 | const chatView = this.shadowRoot?.querySelector(".chat-view"); |
| 612 | const diffView = this.shadowRoot?.querySelector(".diff-view"); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 613 | const diff2View = this.shadowRoot?.querySelector(".diff2-view"); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 614 | const terminalView = this.shadowRoot?.querySelector(".terminal-view"); |
| 615 | |
| 616 | // Remove active class from all views |
| 617 | chatView?.classList.remove("view-active"); |
| 618 | diffView?.classList.remove("view-active"); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 619 | diff2View?.classList.remove("view-active"); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 620 | terminalView?.classList.remove("view-active"); |
| 621 | |
| 622 | // Add/remove diff-active class on view container |
| 623 | if (mode === "diff") { |
| Pokey Rule | 46fff97 | 2025-04-25 14:57:44 +0100 | [diff] [blame] | 624 | viewContainerInner?.classList.add("diff-active"); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 625 | viewContainerInner?.classList.remove("diff2-active"); |
| 626 | } else if (mode === "diff2") { |
| 627 | viewContainerInner?.classList.add("diff2-active"); |
| 628 | viewContainerInner?.classList.remove("diff-active"); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 629 | } else { |
| Pokey Rule | 46fff97 | 2025-04-25 14:57:44 +0100 | [diff] [blame] | 630 | viewContainerInner?.classList.remove("diff-active"); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 631 | viewContainerInner?.classList.remove("diff2-active"); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | // Add active class to the selected view |
| 635 | switch (mode) { |
| 636 | case "chat": |
| 637 | chatView?.classList.add("view-active"); |
| 638 | break; |
| 639 | case "diff": |
| 640 | diffView?.classList.add("view-active"); |
| 641 | // Load diff content if we have a diff view |
| 642 | const diffViewComp = |
| 643 | this.shadowRoot?.querySelector("sketch-diff-view"); |
| 644 | if (diffViewComp && this.currentCommitHash) { |
| 645 | (diffViewComp as any).showCommitDiff(this.currentCommitHash); |
| 646 | } else if (diffViewComp) { |
| 647 | (diffViewComp as any).loadDiffContent(); |
| 648 | } |
| 649 | break; |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 650 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 651 | case "diff2": |
| 652 | diff2View?.classList.add("view-active"); |
| 653 | // Refresh git/recentlog when Monaco diff view is opened |
| 654 | // 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] | 655 | const diff2ViewComp = |
| 656 | this.shadowRoot?.querySelector("sketch-diff2-view"); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 657 | if (diff2ViewComp) { |
| 658 | (diff2ViewComp as SketchDiff2View).refreshDiffView(); |
| 659 | } |
| 660 | break; |
| Philip Zeyliger | 2d4c48f | 2025-05-02 23:35:03 +0000 | [diff] [blame] | 661 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 662 | case "terminal": |
| 663 | terminalView?.classList.add("view-active"); |
| 664 | break; |
| 665 | } |
| 666 | |
| 667 | // Update view mode buttons |
| 668 | const viewModeSelect = this.shadowRoot?.querySelector( |
| Philip Zeyliger | 72682df | 2025-04-23 13:09:46 -0700 | [diff] [blame] | 669 | "sketch-view-mode-select", |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 670 | ); |
| 671 | if (viewModeSelect) { |
| 672 | const event = new CustomEvent("update-active-mode", { |
| 673 | detail: { mode }, |
| 674 | bubbles: true, |
| 675 | composed: true, |
| 676 | }); |
| 677 | viewModeSelect.dispatchEvent(event); |
| 678 | } |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 679 | }); |
| 680 | } |
| 681 | |
| Philip Zeyliger | 9b999b0 | 2025-04-25 16:31:50 +0000 | [diff] [blame] | 682 | /** |
| 683 | * Updates the document title based on current title and connection status |
| 684 | */ |
| 685 | private updateDocumentTitle(): void { |
| 686 | let docTitle = `sk: ${this.title || "untitled"}`; |
| 687 | |
| 688 | // Add red circle emoji if disconnected |
| 689 | if (this.connectionStatus === "disconnected") { |
| 690 | docTitle += " 🔴"; |
| 691 | } |
| 692 | |
| 693 | document.title = docTitle; |
| 694 | } |
| 695 | |
| Philip Zeyliger | bc6b629 | 2025-04-30 18:00:15 +0000 | [diff] [blame] | 696 | // Check and request notification permission if needed |
| 697 | private async checkNotificationPermission(): Promise<boolean> { |
| 698 | // Check if the Notification API is supported |
| 699 | if (!("Notification" in window)) { |
| 700 | console.log("This browser does not support notifications"); |
| 701 | return false; |
| 702 | } |
| 703 | |
| 704 | // Check if permission is already granted |
| 705 | if (Notification.permission === "granted") { |
| 706 | return true; |
| 707 | } |
| 708 | |
| 709 | // If permission is not denied, request it |
| 710 | if (Notification.permission !== "denied") { |
| 711 | const permission = await Notification.requestPermission(); |
| 712 | return permission === "granted"; |
| 713 | } |
| 714 | |
| 715 | return false; |
| 716 | } |
| 717 | |
| Philip Zeyliger | db5e9b4 | 2025-04-30 19:58:13 +0000 | [diff] [blame] | 718 | // Handle notifications toggle click |
| 719 | private _handleNotificationsToggle(): void { |
| 720 | this.notificationsEnabled = !this.notificationsEnabled; |
| Philip Zeyliger | bc6b629 | 2025-04-30 18:00:15 +0000 | [diff] [blame] | 721 | |
| 722 | // If enabling notifications, check permissions |
| 723 | if (this.notificationsEnabled) { |
| 724 | this.checkNotificationPermission(); |
| 725 | } |
| 726 | |
| 727 | // Save preference to localStorage |
| 728 | try { |
| 729 | localStorage.setItem( |
| 730 | "sketch-notifications-enabled", |
| 731 | String(this.notificationsEnabled), |
| 732 | ); |
| 733 | } catch (error) { |
| 734 | console.error("Error saving notification preference:", error); |
| 735 | } |
| 736 | } |
| 737 | |
| Philip Zeyliger | db5e9b4 | 2025-04-30 19:58:13 +0000 | [diff] [blame] | 738 | // Handle window focus event |
| 739 | private _handleWindowFocus(): void { |
| 740 | this._windowFocused = true; |
| 741 | } |
| 742 | |
| 743 | // Handle window blur event |
| 744 | private _handleWindowBlur(): void { |
| 745 | this._windowFocused = false; |
| 746 | } |
| 747 | |
| Philip Zeyliger | bc6b629 | 2025-04-30 18:00:15 +0000 | [diff] [blame] | 748 | // Show notification for message with EndOfTurn=true |
| 749 | private async showEndOfTurnNotification( |
| 750 | message: AgentMessage, |
| 751 | ): Promise<void> { |
| 752 | // Don't show notifications if they're disabled |
| 753 | if (!this.notificationsEnabled) return; |
| 754 | |
| Philip Zeyliger | db5e9b4 | 2025-04-30 19:58:13 +0000 | [diff] [blame] | 755 | // Don't show notifications if the window is focused |
| 756 | if (this._windowFocused) return; |
| 757 | |
| Philip Zeyliger | bc6b629 | 2025-04-30 18:00:15 +0000 | [diff] [blame] | 758 | // Check if we have permission to show notifications |
| 759 | const hasPermission = await this.checkNotificationPermission(); |
| 760 | if (!hasPermission) return; |
| 761 | |
| Philip Zeyliger | 3201133 | 2025-04-30 20:59:40 +0000 | [diff] [blame] | 762 | // Only show notifications for agent messages with end_of_turn=true and no parent_conversation_id |
| 763 | if ( |
| 764 | message.type !== "agent" || |
| 765 | !message.end_of_turn || |
| 766 | message.parent_conversation_id |
| 767 | ) |
| 768 | return; |
| Philip Zeyliger | bc6b629 | 2025-04-30 18:00:15 +0000 | [diff] [blame] | 769 | |
| 770 | // Create a title that includes the sketch title |
| 771 | const notificationTitle = `Sketch: ${this.title || "untitled"}`; |
| 772 | |
| 773 | // Extract the beginning of the message content (first 100 chars) |
| 774 | const messagePreview = message.content |
| 775 | ? message.content.substring(0, 100) + |
| 776 | (message.content.length > 100 ? "..." : "") |
| 777 | : "Agent has completed its turn"; |
| 778 | |
| 779 | // Create and show the notification |
| 780 | try { |
| 781 | new Notification(notificationTitle, { |
| 782 | body: messagePreview, |
| Philip Zeyliger | db5e9b4 | 2025-04-30 19:58:13 +0000 | [diff] [blame] | 783 | icon: "https://sketch.dev/favicon.ico", // Use sketch.dev favicon for notification |
| Philip Zeyliger | bc6b629 | 2025-04-30 18:00:15 +0000 | [diff] [blame] | 784 | }); |
| 785 | } catch (error) { |
| 786 | console.error("Error showing notification:", error); |
| 787 | } |
| 788 | } |
| 789 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 790 | private handleDataChanged(eventData: { |
| 791 | state: State; |
| Sean McCullough | d9f1337 | 2025-04-21 15:08:49 -0700 | [diff] [blame] | 792 | newMessages: AgentMessage[]; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 793 | }): void { |
| Philip Zeyliger | bce3a13 | 2025-04-30 22:03:39 +0000 | [diff] [blame] | 794 | const { state, newMessages } = eventData; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 795 | |
| 796 | // Update state if we received it |
| 797 | if (state) { |
| Josh Bleecher Snyder | e81233f | 2025-04-30 04:05:41 +0000 | [diff] [blame] | 798 | // 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] | 799 | if ( |
| 800 | state.outstanding_llm_calls === 0 && |
| 801 | state.outstanding_tool_calls.length === 0 |
| 802 | ) { |
| Josh Bleecher Snyder | e81233f | 2025-04-30 04:05:41 +0000 | [diff] [blame] | 803 | // Force reset containerState calls when nothing is reported as in progress |
| 804 | state.outstanding_llm_calls = 0; |
| 805 | state.outstanding_tool_calls = []; |
| 806 | } |
| Autoformatter | f830c9d | 2025-04-30 18:16:01 +0000 | [diff] [blame] | 807 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 808 | this.containerState = state; |
| 809 | this.title = state.title; |
| Philip Zeyliger | 9b999b0 | 2025-04-25 16:31:50 +0000 | [diff] [blame] | 810 | |
| 811 | // Update document title when sketch title changes |
| 812 | this.updateDocumentTitle(); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 813 | } |
| 814 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 815 | // Update messages |
| Pokey Rule | e2a8c2f | 2025-04-23 15:09:25 +0100 | [diff] [blame] | 816 | this.messages = aggregateAgentMessages(this.messages, newMessages); |
| Autoformatter | cf57096 | 2025-04-30 17:27:39 +0000 | [diff] [blame] | 817 | |
| Philip Zeyliger | 47b71c9 | 2025-04-30 15:43:39 +0000 | [diff] [blame] | 818 | // Process new messages to find commit messages |
| Philip Zeyliger | 16fa8b4 | 2025-05-02 04:28:16 +0000 | [diff] [blame] | 819 | // Update last commit info via container status component |
| 820 | if (this.containerStatusElement) { |
| 821 | this.containerStatusElement.updateLastCommitInfo(newMessages); |
| 822 | } |
| Philip Zeyliger | bc6b629 | 2025-04-30 18:00:15 +0000 | [diff] [blame] | 823 | |
| 824 | // Check for agent messages with end_of_turn=true and show notifications |
| Philip Zeyliger | bce3a13 | 2025-04-30 22:03:39 +0000 | [diff] [blame] | 825 | if (newMessages && newMessages.length > 0) { |
| Philip Zeyliger | bc6b629 | 2025-04-30 18:00:15 +0000 | [diff] [blame] | 826 | for (const message of newMessages) { |
| Philip Zeyliger | 3201133 | 2025-04-30 20:59:40 +0000 | [diff] [blame] | 827 | if ( |
| 828 | message.type === "agent" && |
| 829 | message.end_of_turn && |
| 830 | !message.parent_conversation_id |
| 831 | ) { |
| Philip Zeyliger | bc6b629 | 2025-04-30 18:00:15 +0000 | [diff] [blame] | 832 | this.showEndOfTurnNotification(message); |
| 833 | break; // Only show one notification per batch of messages |
| 834 | } |
| 835 | } |
| 836 | } |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | private handleConnectionStatusChanged( |
| 840 | status: ConnectionStatus, |
| Philip Zeyliger | 72682df | 2025-04-23 13:09:46 -0700 | [diff] [blame] | 841 | errorMessage?: string, |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 842 | ): void { |
| 843 | this.connectionStatus = status; |
| 844 | this.connectionErrorMessage = errorMessage || ""; |
| Philip Zeyliger | 9b999b0 | 2025-04-25 16:31:50 +0000 | [diff] [blame] | 845 | |
| 846 | // Update document title when connection status changes |
| 847 | this.updateDocumentTitle(); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 848 | } |
| 849 | |
| Sean McCullough | d3906e2 | 2025-04-29 17:32:14 +0000 | [diff] [blame] | 850 | private async _handleStopClick(): Promise<void> { |
| 851 | try { |
| 852 | const response = await fetch("cancel", { |
| 853 | method: "POST", |
| 854 | headers: { |
| 855 | "Content-Type": "application/json", |
| 856 | }, |
| 857 | body: JSON.stringify({ reason: "user requested cancellation" }), |
| 858 | }); |
| 859 | |
| 860 | if (!response.ok) { |
| 861 | const errorData = await response.text(); |
| 862 | throw new Error( |
| 863 | `Failed to stop operation: ${response.status} - ${errorData}`, |
| 864 | ); |
| 865 | } |
| 866 | |
| Philip Zeyliger | bce3a13 | 2025-04-30 22:03:39 +0000 | [diff] [blame] | 867 | // Stop request sent |
| Sean McCullough | d3906e2 | 2025-04-29 17:32:14 +0000 | [diff] [blame] | 868 | } catch (error) { |
| 869 | console.error("Error stopping operation:", error); |
| Sean McCullough | d3906e2 | 2025-04-29 17:32:14 +0000 | [diff] [blame] | 870 | } |
| 871 | } |
| 872 | |
| Pokey Rule | 397871d | 2025-05-19 15:02:45 +0100 | [diff] [blame] | 873 | private async _handleEndClick(event?: Event): Promise<void> { |
| 874 | if (event) { |
| 875 | event.preventDefault(); |
| 876 | event.stopPropagation(); |
| 877 | } |
| 878 | // Show confirmation dialog |
| 879 | const confirmed = window.confirm( |
| 880 | "Ending the session will shut down the underlying container. Are you sure?", |
| 881 | ); |
| 882 | if (!confirmed) return; |
| 883 | |
| 884 | try { |
| 885 | const response = await fetch("end", { |
| 886 | method: "POST", |
| 887 | headers: { |
| 888 | "Content-Type": "application/json", |
| 889 | }, |
| 890 | body: JSON.stringify({ reason: "user requested end of session" }), |
| 891 | }); |
| 892 | |
| 893 | if (!response.ok) { |
| 894 | const errorData = await response.text(); |
| 895 | throw new Error( |
| 896 | `Failed to end session: ${response.status} - ${errorData}`, |
| 897 | ); |
| 898 | } |
| 899 | |
| 900 | // After successful response, redirect to messages view |
| 901 | // Extract the session ID from the URL |
| 902 | const currentUrl = window.location.href; |
| 903 | // The URL pattern should be like https://sketch.dev/s/cs71-8qa6-1124-aw79/ |
| 904 | const urlParts = currentUrl.split("/"); |
| 905 | let sessionId = ""; |
| 906 | |
| 907 | // Find the session ID in the URL (should be after /s/) |
| 908 | for (let i = 0; i < urlParts.length; i++) { |
| 909 | if (urlParts[i] === "s" && i + 1 < urlParts.length) { |
| 910 | sessionId = urlParts[i + 1]; |
| 911 | break; |
| 912 | } |
| 913 | } |
| 914 | |
| 915 | if (sessionId) { |
| 916 | // Create the messages URL |
| 917 | const messagesUrl = `/messages/${sessionId}`; |
| 918 | // Redirect to messages view |
| 919 | window.location.href = messagesUrl; |
| 920 | } |
| 921 | |
| 922 | // End request sent - connection will be closed by server |
| 923 | } catch (error) { |
| 924 | console.error("Error ending session:", error); |
| 925 | } |
| 926 | } |
| 927 | |
| Sean McCullough | 485afc6 | 2025-04-28 14:28:39 -0700 | [diff] [blame] | 928 | async _handleMutlipleChoiceSelected(e: CustomEvent) { |
| 929 | const chatInput = this.shadowRoot?.querySelector( |
| 930 | "sketch-chat-input", |
| 931 | ) as SketchChatInput; |
| 932 | if (chatInput) { |
| 933 | chatInput.content = e.detail.responseText; |
| 934 | chatInput.focus(); |
| 935 | } |
| 936 | } |
| 937 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 938 | async _sendChat(e: CustomEvent) { |
| 939 | console.log("app shell: _sendChat", e); |
| Sean McCullough | 485afc6 | 2025-04-28 14:28:39 -0700 | [diff] [blame] | 940 | e.preventDefault(); |
| 941 | e.stopPropagation(); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 942 | const message = e.detail.message?.trim(); |
| 943 | if (message == "") { |
| 944 | return; |
| 945 | } |
| 946 | try { |
| Josh Bleecher Snyder | 98b64d1 | 2025-05-12 19:42:43 +0000 | [diff] [blame] | 947 | // Always switch to chat view when sending a message so user can see processing |
| 948 | if (this.viewMode !== "chat") { |
| 949 | this.toggleViewMode("chat", true); |
| 950 | } |
| Autoformatter | 5c7f957 | 2025-05-13 01:17:31 +0000 | [diff] [blame] | 951 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 952 | // Send the message to the server |
| 953 | const response = await fetch("chat", { |
| 954 | method: "POST", |
| 955 | headers: { |
| 956 | "Content-Type": "application/json", |
| 957 | }, |
| 958 | body: JSON.stringify({ message }), |
| 959 | }); |
| 960 | |
| 961 | if (!response.ok) { |
| 962 | const errorData = await response.text(); |
| 963 | throw new Error(`Server error: ${response.status} - ${errorData}`); |
| 964 | } |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 965 | } catch (error) { |
| 966 | console.error("Error sending chat message:", error); |
| 967 | const statusText = document.getElementById("statusText"); |
| 968 | if (statusText) { |
| 969 | statusText.textContent = "Error sending message"; |
| 970 | } |
| 971 | } |
| 972 | } |
| 973 | |
| Pokey Rule | 4097e53 | 2025-04-24 18:55:28 +0100 | [diff] [blame] | 974 | private scrollContainerRef = createRef<HTMLElement>(); |
| 975 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 976 | render() { |
| 977 | return html` |
| Pokey Rule | 4097e53 | 2025-04-24 18:55:28 +0100 | [diff] [blame] | 978 | <div id="top-banner"> |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 979 | <div class="title-container"> |
| 980 | <h1 class="banner-title">sketch</h1> |
| 981 | <h2 id="chatTitle" class="chat-title">${this.title}</h2> |
| 982 | </div> |
| 983 | |
| Philip Zeyliger | bce3a13 | 2025-04-30 22:03:39 +0000 | [diff] [blame] | 984 | <!-- Container status info moved above tabs --> |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 985 | <sketch-container-status |
| 986 | .state=${this.containerState} |
| Philip Zeyliger | 16fa8b4 | 2025-05-02 04:28:16 +0000 | [diff] [blame] | 987 | id="container-status" |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 988 | ></sketch-container-status> |
| Autoformatter | cf57096 | 2025-04-30 17:27:39 +0000 | [diff] [blame] | 989 | |
| Philip Zeyliger | 16fa8b4 | 2025-05-02 04:28:16 +0000 | [diff] [blame] | 990 | <!-- Last Commit section moved to sketch-container-status --> |
| Philip Zeyliger | bce3a13 | 2025-04-30 22:03:39 +0000 | [diff] [blame] | 991 | |
| Philip Zeyliger | 16fa8b4 | 2025-05-02 04:28:16 +0000 | [diff] [blame] | 992 | <!-- Views section with tabs --> |
| 993 | <sketch-view-mode-select></sketch-view-mode-select> |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 994 | |
| 995 | <div class="refresh-control"> |
| Sean McCullough | d3906e2 | 2025-04-29 17:32:14 +0000 | [diff] [blame] | 996 | <button |
| Philip Zeyliger | bce3a13 | 2025-04-30 22:03:39 +0000 | [diff] [blame] | 997 | id="stopButton" |
| 998 | class="stop-button" |
| 999 | ?disabled=${(this.containerState?.outstanding_llm_calls || 0) === |
| 1000 | 0 && |
| 1001 | (this.containerState?.outstanding_tool_calls || []).length === 0} |
| 1002 | > |
| 1003 | <svg |
| 1004 | class="button-icon" |
| 1005 | xmlns="http://www.w3.org/2000/svg" |
| 1006 | viewBox="0 0 24 24" |
| 1007 | fill="none" |
| 1008 | stroke="currentColor" |
| 1009 | stroke-width="2" |
| 1010 | stroke-linecap="round" |
| 1011 | stroke-linejoin="round" |
| 1012 | > |
| 1013 | <rect x="6" y="6" width="12" height="12" /> |
| 1014 | </svg> |
| 1015 | <span class="button-text">Stop</span> |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 1016 | </button> |
| Pokey Rule | 397871d | 2025-05-19 15:02:45 +0100 | [diff] [blame] | 1017 | <button |
| 1018 | id="endButton" |
| 1019 | class="end-button" |
| 1020 | @click=${this._handleEndClick} |
| 1021 | > |
| 1022 | <svg |
| 1023 | class="button-icon" |
| 1024 | xmlns="http://www.w3.org/2000/svg" |
| 1025 | viewBox="0 0 24 24" |
| 1026 | fill="none" |
| 1027 | stroke="currentColor" |
| 1028 | stroke-width="2" |
| 1029 | stroke-linecap="round" |
| 1030 | stroke-linejoin="round" |
| 1031 | > |
| 1032 | <path d="M18 6L6 18" /> |
| 1033 | <path d="M6 6l12 12" /> |
| 1034 | </svg> |
| 1035 | <span class="button-text">End</span> |
| 1036 | </button> |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 1037 | |
| Philip Zeyliger | db5e9b4 | 2025-04-30 19:58:13 +0000 | [diff] [blame] | 1038 | <div |
| 1039 | class="notifications-toggle" |
| 1040 | @click=${this._handleNotificationsToggle} |
| 1041 | title="${this.notificationsEnabled |
| 1042 | ? "Disable" |
| Philip Zeyliger | bce3a13 | 2025-04-30 22:03:39 +0000 | [diff] [blame] | 1043 | : "Enable"} notifications when the agent completes its turn" |
| Philip Zeyliger | db5e9b4 | 2025-04-30 19:58:13 +0000 | [diff] [blame] | 1044 | > |
| 1045 | <div |
| 1046 | class="bell-icon ${!this.notificationsEnabled |
| 1047 | ? "bell-disabled" |
| 1048 | : ""}" |
| 1049 | > |
| 1050 | <!-- Bell SVG icon --> |
| 1051 | <svg |
| 1052 | xmlns="http://www.w3.org/2000/svg" |
| 1053 | width="16" |
| 1054 | height="16" |
| 1055 | fill="currentColor" |
| 1056 | viewBox="0 0 16 16" |
| 1057 | > |
| 1058 | <path |
| 1059 | 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" |
| 1060 | /> |
| 1061 | </svg> |
| 1062 | </div> |
| Philip Zeyliger | bc6b629 | 2025-04-30 18:00:15 +0000 | [diff] [blame] | 1063 | </div> |
| 1064 | |
| Philip Zeyliger | 99a9a02 | 2025-04-27 15:15:25 +0000 | [diff] [blame] | 1065 | <sketch-call-status |
| Sean McCullough | d9d4581 | 2025-04-30 16:53:41 -0700 | [diff] [blame] | 1066 | .agentState=${this.containerState?.agent_state} |
| Philip Zeyliger | 99a9a02 | 2025-04-27 15:15:25 +0000 | [diff] [blame] | 1067 | .llmCalls=${this.containerState?.outstanding_llm_calls || 0} |
| 1068 | .toolCalls=${this.containerState?.outstanding_tool_calls || []} |
| Philip Zeyliger | 7231839 | 2025-05-14 02:56:07 +0000 | [diff] [blame] | 1069 | .isIdle=${this.messages.length > 0 |
| 1070 | ? this.messages[this.messages.length - 1]?.end_of_turn && |
| 1071 | !this.messages[this.messages.length - 1]?.parent_conversation_id |
| 1072 | : true} |
| Philip Zeyliger | 5e35702 | 2025-05-16 04:50:34 +0000 | [diff] [blame] | 1073 | .isDisconnected=${this.connectionStatus === "disconnected"} |
| Philip Zeyliger | 99a9a02 | 2025-04-27 15:15:25 +0000 | [diff] [blame] | 1074 | ></sketch-call-status> |
| Philip Zeyliger | 25f6ff1 | 2025-05-02 04:24:10 +0000 | [diff] [blame] | 1075 | |
| 1076 | <sketch-network-status |
| 1077 | connection=${this.connectionStatus} |
| 1078 | error=${this.connectionErrorMessage} |
| 1079 | ></sketch-network-status> |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 1080 | </div> |
| 1081 | </div> |
| 1082 | |
| Pokey Rule | 4097e53 | 2025-04-24 18:55:28 +0100 | [diff] [blame] | 1083 | <div id="view-container" ${ref(this.scrollContainerRef)}> |
| 1084 | <div id="view-container-inner"> |
| 1085 | <div |
| 1086 | class="chat-view ${this.viewMode === "chat" ? "view-active" : ""}" |
| 1087 | > |
| 1088 | <sketch-timeline |
| 1089 | .messages=${this.messages} |
| 1090 | .scrollContainer=${this.scrollContainerRef} |
| Philip Zeyliger | 16fa8b4 | 2025-05-02 04:28:16 +0000 | [diff] [blame] | 1091 | .agentState=${this.containerState?.agent_state} |
| 1092 | .llmCalls=${this.containerState?.outstanding_llm_calls || 0} |
| 1093 | .toolCalls=${this.containerState?.outstanding_tool_calls || []} |
| Pokey Rule | 4097e53 | 2025-04-24 18:55:28 +0100 | [diff] [blame] | 1094 | ></sketch-timeline> |
| 1095 | </div> |
| 1096 | <div |
| 1097 | class="diff-view ${this.viewMode === "diff" ? "view-active" : ""}" |
| 1098 | > |
| 1099 | <sketch-diff-view |
| 1100 | .commitHash=${this.currentCommitHash} |
| 1101 | ></sketch-diff-view> |
| 1102 | </div> |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 1103 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 1104 | <div |
| 1105 | class="diff2-view ${this.viewMode === "diff2" ? "view-active" : ""}" |
| 1106 | > |
| 1107 | <sketch-diff2-view |
| 1108 | .commit=${this.currentCommitHash} |
| 1109 | .gitService=${new DefaultGitDataService()} |
| 1110 | @diff-comment="${this._handleDiffComment}" |
| 1111 | ></sketch-diff2-view> |
| 1112 | </div> |
| Philip Zeyliger | 2d4c48f | 2025-05-02 23:35:03 +0000 | [diff] [blame] | 1113 | |
| Pokey Rule | 4097e53 | 2025-04-24 18:55:28 +0100 | [diff] [blame] | 1114 | <div |
| 1115 | class="terminal-view ${this.viewMode === "terminal" |
| 1116 | ? "view-active" |
| 1117 | : ""}" |
| 1118 | > |
| 1119 | <sketch-terminal></sketch-terminal> |
| 1120 | </div> |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 1121 | </div> |
| 1122 | </div> |
| 1123 | |
| Pokey Rule | 4097e53 | 2025-04-24 18:55:28 +0100 | [diff] [blame] | 1124 | <div id="chat-input"> |
| 1125 | <sketch-chat-input @send-chat="${this._sendChat}"></sketch-chat-input> |
| 1126 | </div> |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 1127 | `; |
| 1128 | } |
| 1129 | |
| 1130 | /** |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 1131 | * Lifecycle callback when component is first connected to DOM |
| 1132 | */ |
| 1133 | firstUpdated(): void { |
| 1134 | if (this.viewMode !== "chat") { |
| 1135 | return; |
| 1136 | } |
| 1137 | |
| 1138 | // Initial scroll to bottom when component is first rendered |
| 1139 | setTimeout( |
| 1140 | () => this.scrollTo({ top: this.scrollHeight, behavior: "smooth" }), |
| Philip Zeyliger | 72682df | 2025-04-23 13:09:46 -0700 | [diff] [blame] | 1141 | 50, |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 1142 | ); |
| 1143 | |
| Philip Zeyliger | 2c4db09 | 2025-04-28 16:57:50 -0700 | [diff] [blame] | 1144 | // Setup stop button |
| 1145 | const stopButton = this.renderRoot?.querySelector( |
| 1146 | "#stopButton", |
| 1147 | ) as HTMLButtonElement; |
| 1148 | stopButton?.addEventListener("click", async () => { |
| 1149 | try { |
| Sean McCullough | 495cb96 | 2025-05-01 16:25:53 -0700 | [diff] [blame] | 1150 | const response = await fetch("cancel", { |
| Philip Zeyliger | 2c4db09 | 2025-04-28 16:57:50 -0700 | [diff] [blame] | 1151 | method: "POST", |
| 1152 | headers: { |
| 1153 | "Content-Type": "application/json", |
| 1154 | }, |
| 1155 | body: JSON.stringify({ reason: "User clicked stop button" }), |
| 1156 | }); |
| 1157 | if (!response.ok) { |
| 1158 | console.error("Failed to cancel:", await response.text()); |
| 1159 | } |
| 1160 | } catch (error) { |
| 1161 | console.error("Error cancelling operation:", error); |
| 1162 | } |
| 1163 | }); |
| 1164 | |
| Pokey Rule | 397871d | 2025-05-19 15:02:45 +0100 | [diff] [blame] | 1165 | // Setup end button |
| 1166 | const endButton = this.renderRoot?.querySelector( |
| 1167 | "#endButton", |
| 1168 | ) as HTMLButtonElement; |
| 1169 | // We're already using the @click binding in the HTML, so manual event listener not needed here |
| 1170 | |
| Philip Zeyliger | 47b71c9 | 2025-04-30 15:43:39 +0000 | [diff] [blame] | 1171 | // Process any existing messages to find commit information |
| 1172 | if (this.messages && this.messages.length > 0) { |
| Philip Zeyliger | 16fa8b4 | 2025-05-02 04:28:16 +0000 | [diff] [blame] | 1173 | // Update last commit info via container status component |
| 1174 | if (this.containerStatusElement) { |
| 1175 | this.containerStatusElement.updateLastCommitInfo(this.messages); |
| 1176 | } |
| Philip Zeyliger | 47b71c9 | 2025-04-30 15:43:39 +0000 | [diff] [blame] | 1177 | } |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 1178 | } |
| 1179 | } |
| 1180 | |
| 1181 | declare global { |
| 1182 | interface HTMLElementTagNameMap { |
| 1183 | "sketch-app-shell": SketchAppShell; |
| 1184 | } |
| 1185 | } |