blob: 3c39a55c1040dc6c32ecc06339b82d3f9926ad5c [file] [log] [blame]
Sean McCullough86b56862025-04-18 13:04:03 -07001import { css, html, LitElement } from "lit";
2import { customElement, property, state } from "lit/decorators.js";
Pokey Rule4097e532025-04-24 18:55:28 +01003import { ConnectionStatus, DataManager } from "../data";
Philip Zeyliger272a90e2025-05-16 14:49:51 -07004import { AgentMessage, GitLogEntry, State } from "../types";
Pokey Rulee2a8c2f2025-04-23 15:09:25 +01005import { aggregateAgentMessages } from "./aggregateAgentMessages";
Philip Zeyliger2d4c48f2025-05-02 23:35:03 +00006
Pokey Rule4097e532025-04-24 18:55:28 +01007import "./sketch-chat-input";
8import "./sketch-container-status";
9import "./sketch-diff-view";
10import { SketchDiffView } from "./sketch-diff-view";
Philip Zeyliger272a90e2025-05-16 14:49:51 -070011import "./sketch-diff2-view";
12import { SketchDiff2View } from "./sketch-diff2-view";
13import { DefaultGitDataService } from "./git-data-service";
14import "./sketch-monaco-view";
Pokey Rule4097e532025-04-24 18:55:28 +010015import "./sketch-network-status";
Philip Zeyliger99a9a022025-04-27 15:15:25 +000016import "./sketch-call-status";
Pokey Rule4097e532025-04-24 18:55:28 +010017import "./sketch-terminal";
18import "./sketch-timeline";
19import "./sketch-view-mode-select";
Philip Zeyliger2c4db092025-04-28 16:57:50 -070020import "./sketch-restart-modal";
Pokey Rule4097e532025-04-24 18:55:28 +010021
22import { createRef, ref } from "lit/directives/ref.js";
Sean McCullough485afc62025-04-28 14:28:39 -070023import { SketchChatInput } from "./sketch-chat-input";
Sean McCullough86b56862025-04-18 13:04:03 -070024
Philip Zeyliger272a90e2025-05-16 14:49:51 -070025type ViewMode = "chat" | "diff" | "diff2" | "terminal";
Sean McCullough86b56862025-04-18 13:04:03 -070026
27@customElement("sketch-app-shell")
28export class SketchAppShell extends LitElement {
Philip Zeyliger2d4c48f2025-05-02 23:35:03 +000029 // Current view mode (chat, diff, terminal)
Sean McCullough86b56862025-04-18 13:04:03 -070030 @state()
Philip Zeyliger272a90e2025-05-16 14:49:51 -070031 viewMode: ViewMode = "chat";
Sean McCullough86b56862025-04-18 13:04:03 -070032
33 // Current commit hash for diff view
34 @state()
35 currentCommitHash: string = "";
36
Philip Zeyliger47b71c92025-04-30 15:43:39 +000037 // Last commit information
38 @state()
Philip Zeyliger16fa8b42025-05-02 04:28:16 +000039
40 // Reference to the container status element
41 containerStatusElement: any = null;
Philip Zeyliger47b71c92025-04-30 15:43:39 +000042
Sean McCullough86b56862025-04-18 13:04:03 -070043 // See https://lit.dev/docs/components/styles/ for how lit-element handles CSS.
44 // Note that these styles only apply to the scope of this web component's
45 // shadow DOM node, so they won't leak out or collide with CSS declared in
46 // other components or the containing web page (...unless you want it to do that).
47 static styles = css`
Philip Zeyliger47b71c92025-04-30 15:43:39 +000048 .copied-indicator {
49 position: absolute;
50 top: -20px;
51 left: 50%;
52 transform: translateX(-50%);
53 background: rgba(40, 167, 69, 0.9);
54 color: white;
55 padding: 2px 6px;
56 border-radius: 3px;
57 font-size: 10px;
58 font-family: system-ui, sans-serif;
59 animation: fadeInOut 2s ease;
60 pointer-events: none;
61 }
Autoformattercf570962025-04-30 17:27:39 +000062
Philip Zeyliger47b71c92025-04-30 15:43:39 +000063 @keyframes fadeInOut {
Autoformattercf570962025-04-30 17:27:39 +000064 0% {
65 opacity: 0;
66 }
67 20% {
68 opacity: 1;
69 }
70 80% {
71 opacity: 1;
72 }
73 100% {
74 opacity: 0;
75 }
Philip Zeyliger47b71c92025-04-30 15:43:39 +000076 }
Autoformattercf570962025-04-30 17:27:39 +000077
Philip Zeyliger47b71c92025-04-30 15:43:39 +000078 .commit-branch-indicator {
79 color: #28a745;
80 }
Autoformattercf570962025-04-30 17:27:39 +000081
Philip Zeyliger47b71c92025-04-30 15:43:39 +000082 .commit-hash-indicator {
83 color: #0366d6;
84 }
Sean McCullough86b56862025-04-18 13:04:03 -070085 :host {
86 display: block;
Sean McCullough71941bd2025-04-18 13:31:48 -070087 font-family:
88 system-ui,
89 -apple-system,
90 BlinkMacSystemFont,
91 "Segoe UI",
92 Roboto,
93 sans-serif;
Sean McCullough86b56862025-04-18 13:04:03 -070094 color: #333;
95 line-height: 1.4;
Pokey Rule4097e532025-04-24 18:55:28 +010096 height: 100vh;
Sean McCullough86b56862025-04-18 13:04:03 -070097 width: 100%;
98 position: relative;
99 overflow-x: hidden;
Pokey Rule4097e532025-04-24 18:55:28 +0100100 display: flex;
101 flex-direction: column;
Sean McCullough86b56862025-04-18 13:04:03 -0700102 }
103
104 /* Top banner with combined elements */
Pokey Rule4097e532025-04-24 18:55:28 +0100105 #top-banner {
Sean McCullough86b56862025-04-18 13:04:03 -0700106 display: flex;
Philip Zeyligere66db3e2025-04-27 15:40:39 +0000107 align-self: stretch;
Sean McCullough86b56862025-04-18 13:04:03 -0700108 justify-content: space-between;
109 align-items: center;
Philip Zeyligere66db3e2025-04-27 15:40:39 +0000110 padding: 0 20px;
Sean McCullough86b56862025-04-18 13:04:03 -0700111 margin-bottom: 0;
112 border-bottom: 1px solid #eee;
Philip Zeyligere66db3e2025-04-27 15:40:39 +0000113 gap: 20px;
Sean McCullough86b56862025-04-18 13:04:03 -0700114 background: white;
Sean McCullough86b56862025-04-18 13:04:03 -0700115 box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
Philip Zeyligere66db3e2025-04-27 15:40:39 +0000116 width: 100%;
117 height: 48px;
118 padding-right: 30px; /* Extra padding on the right to prevent elements from hitting the edge */
Sean McCullough86b56862025-04-18 13:04:03 -0700119 }
120
Pokey Rule4097e532025-04-24 18:55:28 +0100121 /* View mode container styles - mirroring timeline.css structure */
122 #view-container {
123 align-self: stretch;
124 overflow-y: auto;
125 flex: 1;
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700126 display: flex;
127 flex-direction: column;
128 min-height: 0; /* Critical for proper flex child behavior */
Pokey Rule4097e532025-04-24 18:55:28 +0100129 }
130
131 #view-container-inner {
132 max-width: 1200px;
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700133 width: calc(100% - 40px);
Pokey Rule4097e532025-04-24 18:55:28 +0100134 margin: 0 auto;
135 position: relative;
136 padding-bottom: 10px;
137 padding-top: 10px;
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700138 display: flex;
139 flex-direction: column;
140 height: 100%; /* Ensure it takes full height of parent */
Pokey Rule4097e532025-04-24 18:55:28 +0100141 }
142
143 #chat-input {
144 align-self: flex-end;
145 width: 100%;
146 box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.1);
147 }
148
Sean McCullough86b56862025-04-18 13:04:03 -0700149 .banner-title {
150 font-size: 18px;
151 font-weight: 600;
152 margin: 0;
153 min-width: 6em;
154 white-space: nowrap;
155 overflow: hidden;
156 text-overflow: ellipsis;
157 }
158
159 .chat-title {
160 margin: 0;
161 padding: 0;
162 color: rgba(82, 82, 82, 0.85);
Josh Bleecher Snydereb5166a2025-04-30 17:04:20 +0000163 font-size: 14px;
Sean McCullough86b56862025-04-18 13:04:03 -0700164 font-weight: normal;
165 font-style: italic;
166 white-space: nowrap;
167 overflow: hidden;
168 text-overflow: ellipsis;
169 }
170
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700171 /* Allow the container to expand to full width and height in diff mode */
172 #view-container-inner.diff-active,
173 #view-container-inner.diff2-active {
Sean McCullough86b56862025-04-18 13:04:03 -0700174 max-width: 100%;
Pokey Rule46fff972025-04-25 14:57:44 +0100175 width: 100%;
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700176 height: 100%;
177 padding: 0; /* Remove padding for more space */
178 display: flex;
179 flex-direction: column;
180 flex: 1;
181 min-height: 0; /* Critical for flex behavior */
Sean McCullough86b56862025-04-18 13:04:03 -0700182 }
183
184 /* Individual view styles */
185 .chat-view,
186 .diff-view,
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700187 .diff2-view,
Sean McCullough86b56862025-04-18 13:04:03 -0700188 .terminal-view {
189 display: none; /* Hidden by default */
190 width: 100%;
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700191 height: 100%;
192 }
193
194 /* Make chat view take full width available */
195 .chat-view.view-active {
196 display: flex;
197 flex-direction: column;
198 width: 100%;
199 }
200
201 /* Monaco diff2 view needs to take all available space */
202 .diff2-view.view-active {
203 flex: 1;
204 overflow: hidden;
205 min-height: 0; /* Required for proper flex child behavior */
206 display: flex;
207 flex-direction: column;
208 height: 100%;
Sean McCullough86b56862025-04-18 13:04:03 -0700209 }
210
211 /* Active view styles - these will be applied via JavaScript */
212 .view-active {
213 display: flex;
214 flex-direction: column;
215 }
216
217 .title-container {
218 display: flex;
219 flex-direction: column;
220 white-space: nowrap;
221 overflow: hidden;
222 text-overflow: ellipsis;
Josh Bleecher Snydereb5166a2025-04-30 17:04:20 +0000223 max-width: 30%;
Philip Zeyligere66db3e2025-04-27 15:40:39 +0000224 padding: 6px 0;
Sean McCullough86b56862025-04-18 13:04:03 -0700225 }
226
227 .refresh-control {
228 display: flex;
229 align-items: center;
230 margin-bottom: 0;
231 flex-wrap: nowrap;
232 white-space: nowrap;
233 flex-shrink: 0;
Philip Zeyligere66db3e2025-04-27 15:40:39 +0000234 gap: 15px;
235 padding-left: 15px;
236 margin-right: 50px;
Sean McCullough86b56862025-04-18 13:04:03 -0700237 }
238
Philip Zeyligerbce3a132025-04-30 22:03:39 +0000239 .restart-button,
240 .stop-button {
Philip Zeyliger2c4db092025-04-28 16:57:50 -0700241 background: #2196f3;
242 color: white;
243 border: none;
244 padding: 4px 10px;
245 border-radius: 4px;
246 cursor: pointer;
247 font-size: 12px;
248 margin-right: 5px;
Philip Zeyligerbce3a132025-04-30 22:03:39 +0000249 display: flex;
250 align-items: center;
251 gap: 6px;
Philip Zeyliger2c4db092025-04-28 16:57:50 -0700252 }
253
254 .restart-button:hover {
255 background-color: #0b7dda;
256 }
257
258 .restart-button:disabled {
259 background-color: #ccc;
260 cursor: not-allowed;
261 opacity: 0.6;
262 }
263
Philip Zeyligerbce3a132025-04-30 22:03:39 +0000264 .stop-button {
265 background: #dc3545;
Sean McCullough86b56862025-04-18 13:04:03 -0700266 color: white;
Sean McCullough86b56862025-04-18 13:04:03 -0700267 }
268
Philip Zeyligerbce3a132025-04-30 22:03:39 +0000269 .stop-button:hover:not(:disabled) {
270 background-color: #c82333;
Sean McCullough86b56862025-04-18 13:04:03 -0700271 }
272
Philip Zeyligerbce3a132025-04-30 22:03:39 +0000273 .stop-button:disabled {
274 background-color: #e9a8ad;
275 cursor: not-allowed;
276 opacity: 0.7;
Philip Zeyligerdb5e9b42025-04-30 19:58:13 +0000277 }
278
Philip Zeyligerbce3a132025-04-30 22:03:39 +0000279 .stop-button:disabled:hover {
280 background-color: #e9a8ad;
281 }
282
283 .button-icon {
284 width: 16px;
285 height: 16px;
286 }
287
288 @media (max-width: 1400px) {
289 .button-text {
290 display: none;
291 }
292
293 .restart-button,
294 .stop-button {
295 padding: 6px;
296 }
297 }
298
299 /* Removed poll-updates class */
300
Philip Zeyligerbc6b6292025-04-30 18:00:15 +0000301 .notifications-toggle {
Sean McCullough86b56862025-04-18 13:04:03 -0700302 display: flex;
303 align-items: center;
Sean McCullough86b56862025-04-18 13:04:03 -0700304 font-size: 12px;
Philip Zeyligerbc6b6292025-04-30 18:00:15 +0000305 margin-right: 10px;
Philip Zeyligerdb5e9b42025-04-30 19:58:13 +0000306 cursor: pointer;
307 }
308
309 .bell-icon {
310 width: 20px;
311 height: 20px;
312 position: relative;
313 display: inline-flex;
314 align-items: center;
315 justify-content: center;
316 }
317
318 .bell-disabled::before {
319 content: "";
320 position: absolute;
321 width: 2px;
322 height: 24px;
323 background-color: #dc3545;
324 transform: rotate(45deg);
325 transform-origin: center center;
Sean McCullough86b56862025-04-18 13:04:03 -0700326 }
327 `;
328
329 // Header bar: Network connection status details
330 @property()
331 connectionStatus: ConnectionStatus = "disconnected";
Autoformattercf570962025-04-30 17:27:39 +0000332
Philip Zeyliger47b71c92025-04-30 15:43:39 +0000333 // Track if the last commit info has been copied
334 @state()
Philip Zeyliger16fa8b42025-05-02 04:28:16 +0000335 // lastCommitCopied moved to sketch-container-status
Sean McCullough86b56862025-04-18 13:04:03 -0700336
Philip Zeyligerbc6b6292025-04-30 18:00:15 +0000337 // Track notification preferences
338 @state()
Philip Zeyligerdb5e9b42025-04-30 19:58:13 +0000339 notificationsEnabled: boolean = false;
340
341 // Track if the window is focused to control notifications
342 @state()
343 private _windowFocused: boolean = document.hasFocus();
Philip Zeyligerbc6b6292025-04-30 18:00:15 +0000344
Sean McCullough86b56862025-04-18 13:04:03 -0700345 @property()
346 connectionErrorMessage: string = "";
347
Sean McCullough86b56862025-04-18 13:04:03 -0700348 // Chat messages
Pokey Rulee2a8c2f2025-04-23 15:09:25 +0100349 @property({ attribute: false })
Sean McCulloughd9f13372025-04-21 15:08:49 -0700350 messages: AgentMessage[] = [];
Sean McCullough86b56862025-04-18 13:04:03 -0700351
352 @property()
Philip Zeyliger9b999b02025-04-25 16:31:50 +0000353 set title(value: string) {
354 const oldValue = this._title;
355 this._title = value;
356 this.requestUpdate("title", oldValue);
357 // Update document title when title property changes
358 this.updateDocumentTitle();
359 }
360
361 get title(): string {
362 return this._title;
363 }
364
365 private _title: string = "";
Sean McCullough86b56862025-04-18 13:04:03 -0700366
367 private dataManager = new DataManager();
368
Pokey Rulee2a8c2f2025-04-23 15:09:25 +0100369 @property({ attribute: false })
Sean McCulloughd9f13372025-04-21 15:08:49 -0700370 containerState: State = {
Philip Zeyligerd03318d2025-05-08 13:09:12 -0700371 state_version: 2,
Sean McCulloughd9f13372025-04-21 15:08:49 -0700372 title: "",
373 os: "",
374 message_count: 0,
375 hostname: "",
376 working_dir: "",
377 initial_commit: "",
Philip Zeyliger99a9a022025-04-27 15:15:25 +0000378 outstanding_llm_calls: 0,
379 outstanding_tool_calls: [],
Philip Zeyligerc72fff52025-04-29 20:17:54 +0000380 session_id: "",
381 ssh_available: false,
Philip Zeyliger2c4db092025-04-28 16:57:50 -0700382 ssh_error: "",
383 in_container: false,
384 first_message_index: 0,
Sean McCulloughd9f13372025-04-21 15:08:49 -0700385 };
Sean McCullough86b56862025-04-18 13:04:03 -0700386
Philip Zeyliger2c4db092025-04-28 16:57:50 -0700387 @state()
388 private restartModalOpen = false;
389
Sean McCullough86b56862025-04-18 13:04:03 -0700390 // Mutation observer to detect when new messages are added
391 private mutationObserver: MutationObserver | null = null;
392
393 constructor() {
394 super();
395
Philip Zeyliger16fa8b42025-05-02 04:28:16 +0000396 // Reference to the container status element
397 this.containerStatusElement = null;
398
Sean McCullough86b56862025-04-18 13:04:03 -0700399 // Binding methods to this
400 this._handleViewModeSelect = this._handleViewModeSelect.bind(this);
Sean McCullough34bb09a2025-05-13 15:39:54 -0700401 this._handlePopState = this._handlePopState.bind(this);
Sean McCullough86b56862025-04-18 13:04:03 -0700402 this._handleShowCommitDiff = this._handleShowCommitDiff.bind(this);
Sean McCullough485afc62025-04-28 14:28:39 -0700403 this._handleMutlipleChoiceSelected =
404 this._handleMutlipleChoiceSelected.bind(this);
Sean McCulloughd3906e22025-04-29 17:32:14 +0000405 this._handleStopClick = this._handleStopClick.bind(this);
Philip Zeyligerbc6b6292025-04-30 18:00:15 +0000406 this._handleNotificationsToggle =
407 this._handleNotificationsToggle.bind(this);
Philip Zeyligerdb5e9b42025-04-30 19:58:13 +0000408 this._handleWindowFocus = this._handleWindowFocus.bind(this);
409 this._handleWindowBlur = this._handleWindowBlur.bind(this);
Philip Zeyligerbc6b6292025-04-30 18:00:15 +0000410
411 // Load notification preference from localStorage
412 try {
413 const savedPref = localStorage.getItem("sketch-notifications-enabled");
414 if (savedPref !== null) {
415 this.notificationsEnabled = savedPref === "true";
416 }
417 } catch (error) {
418 console.error("Error loading notification preference:", error);
419 }
Sean McCullough86b56862025-04-18 13:04:03 -0700420 }
421
422 // See https://lit.dev/docs/components/lifecycle/
423 connectedCallback() {
424 super.connectedCallback();
425
Philip Zeyliger16fa8b42025-05-02 04:28:16 +0000426 // Get reference to the container status element
427 setTimeout(() => {
428 this.containerStatusElement =
429 this.shadowRoot?.getElementById("container-status");
430 }, 0);
431
Sean McCullough86b56862025-04-18 13:04:03 -0700432 // Initialize client-side nav history.
433 const url = new URL(window.location.href);
434 const mode = url.searchParams.get("view") || "chat";
435 window.history.replaceState({ mode }, "", url.toString());
436
437 this.toggleViewMode(mode as ViewMode, false);
438 // Add popstate event listener to handle browser back/forward navigation
Pokey Rulee2a8c2f2025-04-23 15:09:25 +0100439 window.addEventListener("popstate", this._handlePopState);
Sean McCullough86b56862025-04-18 13:04:03 -0700440
441 // Add event listeners
Pokey Rulee2a8c2f2025-04-23 15:09:25 +0100442 window.addEventListener("view-mode-select", this._handleViewModeSelect);
Pokey Rulee2a8c2f2025-04-23 15:09:25 +0100443 window.addEventListener("show-commit-diff", this._handleShowCommitDiff);
Sean McCullough86b56862025-04-18 13:04:03 -0700444
Philip Zeyligerdb5e9b42025-04-30 19:58:13 +0000445 // Add window focus/blur listeners for controlling notifications
446 window.addEventListener("focus", this._handleWindowFocus);
447 window.addEventListener("blur", this._handleWindowBlur);
Sean McCullough485afc62025-04-28 14:28:39 -0700448 window.addEventListener(
449 "multiple-choice-selected",
450 this._handleMutlipleChoiceSelected,
451 );
Philip Zeyligerdb5e9b42025-04-30 19:58:13 +0000452
Sean McCullough86b56862025-04-18 13:04:03 -0700453 // register event listeners
454 this.dataManager.addEventListener(
455 "dataChanged",
Philip Zeyliger72682df2025-04-23 13:09:46 -0700456 this.handleDataChanged.bind(this),
Sean McCullough86b56862025-04-18 13:04:03 -0700457 );
458 this.dataManager.addEventListener(
459 "connectionStatusChanged",
Philip Zeyliger72682df2025-04-23 13:09:46 -0700460 this.handleConnectionStatusChanged.bind(this),
Sean McCullough86b56862025-04-18 13:04:03 -0700461 );
462
Philip Zeyliger9b999b02025-04-25 16:31:50 +0000463 // Set initial document title
464 this.updateDocumentTitle();
465
Sean McCullough86b56862025-04-18 13:04:03 -0700466 // Initialize the data manager
467 this.dataManager.initialize();
Autoformattercf570962025-04-30 17:27:39 +0000468
Philip Zeyliger47b71c92025-04-30 15:43:39 +0000469 // Process existing messages for commit info
470 if (this.messages && this.messages.length > 0) {
Philip Zeyliger16fa8b42025-05-02 04:28:16 +0000471 // Update last commit info via container status component
472 setTimeout(() => {
473 if (this.containerStatusElement) {
474 this.containerStatusElement.updateLastCommitInfo(this.messages);
475 }
476 }, 100);
Philip Zeyliger47b71c92025-04-30 15:43:39 +0000477 }
Sean McCullough86b56862025-04-18 13:04:03 -0700478 }
479
480 // See https://lit.dev/docs/components/lifecycle/
481 disconnectedCallback() {
482 super.disconnectedCallback();
Pokey Rulee2a8c2f2025-04-23 15:09:25 +0100483 window.removeEventListener("popstate", this._handlePopState);
Sean McCullough86b56862025-04-18 13:04:03 -0700484
485 // Remove event listeners
Pokey Rulee2a8c2f2025-04-23 15:09:25 +0100486 window.removeEventListener("view-mode-select", this._handleViewModeSelect);
Pokey Rulee2a8c2f2025-04-23 15:09:25 +0100487 window.removeEventListener("show-commit-diff", this._handleShowCommitDiff);
Philip Zeyligerdb5e9b42025-04-30 19:58:13 +0000488 window.removeEventListener("focus", this._handleWindowFocus);
489 window.removeEventListener("blur", this._handleWindowBlur);
Sean McCullough485afc62025-04-28 14:28:39 -0700490 window.removeEventListener(
491 "multiple-choice-selected",
492 this._handleMutlipleChoiceSelected,
493 );
Sean McCullough86b56862025-04-18 13:04:03 -0700494
495 // unregister data manager event listeners
496 this.dataManager.removeEventListener(
497 "dataChanged",
Philip Zeyliger72682df2025-04-23 13:09:46 -0700498 this.handleDataChanged.bind(this),
Sean McCullough86b56862025-04-18 13:04:03 -0700499 );
500 this.dataManager.removeEventListener(
501 "connectionStatusChanged",
Philip Zeyliger72682df2025-04-23 13:09:46 -0700502 this.handleConnectionStatusChanged.bind(this),
Sean McCullough86b56862025-04-18 13:04:03 -0700503 );
504
505 // Disconnect mutation observer if it exists
506 if (this.mutationObserver) {
Sean McCullough86b56862025-04-18 13:04:03 -0700507 this.mutationObserver.disconnect();
508 this.mutationObserver = null;
509 }
510 }
511
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700512 updateUrlForViewMode(mode: ViewMode): void {
Sean McCullough86b56862025-04-18 13:04:03 -0700513 // Get the current URL without search parameters
514 const url = new URL(window.location.href);
515
516 // Clear existing parameters
517 url.search = "";
518
519 // Only add view parameter if not in default chat view
520 if (mode !== "chat") {
521 url.searchParams.set("view", mode);
Sean McCullough71941bd2025-04-18 13:31:48 -0700522 const diffView = this.shadowRoot?.querySelector(
Philip Zeyliger72682df2025-04-23 13:09:46 -0700523 ".diff-view",
Sean McCullough71941bd2025-04-18 13:31:48 -0700524 ) as SketchDiffView;
Sean McCullough86b56862025-04-18 13:04:03 -0700525
526 // If in diff view and there's a commit hash, include that too
527 if (mode === "diff" && diffView.commitHash) {
528 url.searchParams.set("commit", diffView.commitHash);
529 }
530 }
531
532 // Update the browser history without reloading the page
533 window.history.pushState({ mode }, "", url.toString());
534 }
535
Pokey Rulee2a8c2f2025-04-23 15:09:25 +0100536 private _handlePopState(event: PopStateEvent) {
Sean McCullough86b56862025-04-18 13:04:03 -0700537 if (event.state && event.state.mode) {
538 this.toggleViewMode(event.state.mode, false);
539 } else {
540 this.toggleViewMode("chat", false);
541 }
542 }
543
544 /**
545 * Handle view mode selection event
546 */
547 private _handleViewModeSelect(event: CustomEvent) {
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700548 const mode = event.detail.mode as "chat" | "diff" | "diff2" | "terminal";
Sean McCullough86b56862025-04-18 13:04:03 -0700549 this.toggleViewMode(mode, true);
550 }
551
552 /**
553 * Handle show commit diff event
554 */
555 private _handleShowCommitDiff(event: CustomEvent) {
556 const { commitHash } = event.detail;
557 if (commitHash) {
558 this.showCommitDiff(commitHash);
559 }
560 }
561
Sean McCullough485afc62025-04-28 14:28:39 -0700562 private _handleMultipleChoice(event: CustomEvent) {
563 window.console.log("_handleMultipleChoice", event);
564 this._sendChat;
565 }
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700566
567 private _handleDiffComment(event: CustomEvent) {
568 // Empty stub required by the event binding in the template
569 // Actual handling occurs at global level in sketch-chat-input component
570 }
Sean McCullough86b56862025-04-18 13:04:03 -0700571 /**
Sean McCullough86b56862025-04-18 13:04:03 -0700572 * Listen for commit diff event
573 * @param commitHash The commit hash to show diff for
574 */
Pokey Rulee2a8c2f2025-04-23 15:09:25 +0100575 private showCommitDiff(commitHash: string): void {
Sean McCullough86b56862025-04-18 13:04:03 -0700576 // Store the commit hash
577 this.currentCommitHash = commitHash;
578
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700579 this.toggleViewMode("diff2", true);
Sean McCullough86b56862025-04-18 13:04:03 -0700580
Sean McCullough86b56862025-04-18 13:04:03 -0700581 this.updateComplete.then(() => {
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700582 const diff2View = this.shadowRoot?.querySelector("sketch-diff2-view");
583 if (diff2View) {
584 (diff2View as SketchDiff2View).refreshDiffView();
Sean McCullough86b56862025-04-18 13:04:03 -0700585 }
586 });
587 }
588
589 /**
Philip Zeyliger2d4c48f2025-05-02 23:35:03 +0000590 * Toggle between different view modes: chat, diff, terminal
Sean McCullough86b56862025-04-18 13:04:03 -0700591 */
Pokey Rulee2a8c2f2025-04-23 15:09:25 +0100592 private toggleViewMode(mode: ViewMode, updateHistory: boolean): void {
Sean McCullough86b56862025-04-18 13:04:03 -0700593 // Don't do anything if the mode is already active
594 if (this.viewMode === mode) return;
595
596 // Update the view mode
597 this.viewMode = mode;
598
599 if (updateHistory) {
600 // Update URL with the current view mode
601 this.updateUrlForViewMode(mode);
602 }
603
604 // Wait for DOM update to complete
605 this.updateComplete.then(() => {
606 // Update active view
Pokey Rule46fff972025-04-25 14:57:44 +0100607 const viewContainerInner = this.shadowRoot?.querySelector(
608 "#view-container-inner",
609 );
Sean McCullough86b56862025-04-18 13:04:03 -0700610 const chatView = this.shadowRoot?.querySelector(".chat-view");
611 const diffView = this.shadowRoot?.querySelector(".diff-view");
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700612 const diff2View = this.shadowRoot?.querySelector(".diff2-view");
Sean McCullough86b56862025-04-18 13:04:03 -0700613 const terminalView = this.shadowRoot?.querySelector(".terminal-view");
614
615 // Remove active class from all views
616 chatView?.classList.remove("view-active");
617 diffView?.classList.remove("view-active");
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700618 diff2View?.classList.remove("view-active");
Sean McCullough86b56862025-04-18 13:04:03 -0700619 terminalView?.classList.remove("view-active");
620
621 // Add/remove diff-active class on view container
622 if (mode === "diff") {
Pokey Rule46fff972025-04-25 14:57:44 +0100623 viewContainerInner?.classList.add("diff-active");
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700624 viewContainerInner?.classList.remove("diff2-active");
625 } else if (mode === "diff2") {
626 viewContainerInner?.classList.add("diff2-active");
627 viewContainerInner?.classList.remove("diff-active");
Sean McCullough86b56862025-04-18 13:04:03 -0700628 } else {
Pokey Rule46fff972025-04-25 14:57:44 +0100629 viewContainerInner?.classList.remove("diff-active");
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700630 viewContainerInner?.classList.remove("diff2-active");
Sean McCullough86b56862025-04-18 13:04:03 -0700631 }
632
633 // Add active class to the selected view
634 switch (mode) {
635 case "chat":
636 chatView?.classList.add("view-active");
637 break;
638 case "diff":
639 diffView?.classList.add("view-active");
640 // Load diff content if we have a diff view
641 const diffViewComp =
642 this.shadowRoot?.querySelector("sketch-diff-view");
643 if (diffViewComp && this.currentCommitHash) {
644 (diffViewComp as any).showCommitDiff(this.currentCommitHash);
645 } else if (diffViewComp) {
646 (diffViewComp as any).loadDiffContent();
647 }
648 break;
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700649
650 case "diff2":
651 diff2View?.classList.add("view-active");
652 // Refresh git/recentlog when Monaco diff view is opened
653 // This ensures branch information is always up-to-date, as branches can change frequently
654 const diff2ViewComp = this.shadowRoot?.querySelector("sketch-diff2-view");
655 if (diff2ViewComp) {
656 (diff2ViewComp as SketchDiff2View).refreshDiffView();
657 }
658 break;
Philip Zeyliger2d4c48f2025-05-02 23:35:03 +0000659
Sean McCullough86b56862025-04-18 13:04:03 -0700660 case "terminal":
661 terminalView?.classList.add("view-active");
662 break;
663 }
664
665 // Update view mode buttons
666 const viewModeSelect = this.shadowRoot?.querySelector(
Philip Zeyliger72682df2025-04-23 13:09:46 -0700667 "sketch-view-mode-select",
Sean McCullough86b56862025-04-18 13:04:03 -0700668 );
669 if (viewModeSelect) {
670 const event = new CustomEvent("update-active-mode", {
671 detail: { mode },
672 bubbles: true,
673 composed: true,
674 });
675 viewModeSelect.dispatchEvent(event);
676 }
Sean McCullough86b56862025-04-18 13:04:03 -0700677 });
678 }
679
Philip Zeyliger9b999b02025-04-25 16:31:50 +0000680 /**
681 * Updates the document title based on current title and connection status
682 */
683 private updateDocumentTitle(): void {
684 let docTitle = `sk: ${this.title || "untitled"}`;
685
686 // Add red circle emoji if disconnected
687 if (this.connectionStatus === "disconnected") {
688 docTitle += " 🔴";
689 }
690
691 document.title = docTitle;
692 }
693
Philip Zeyligerbc6b6292025-04-30 18:00:15 +0000694 // Check and request notification permission if needed
695 private async checkNotificationPermission(): Promise<boolean> {
696 // Check if the Notification API is supported
697 if (!("Notification" in window)) {
698 console.log("This browser does not support notifications");
699 return false;
700 }
701
702 // Check if permission is already granted
703 if (Notification.permission === "granted") {
704 return true;
705 }
706
707 // If permission is not denied, request it
708 if (Notification.permission !== "denied") {
709 const permission = await Notification.requestPermission();
710 return permission === "granted";
711 }
712
713 return false;
714 }
715
Philip Zeyligerdb5e9b42025-04-30 19:58:13 +0000716 // Handle notifications toggle click
717 private _handleNotificationsToggle(): void {
718 this.notificationsEnabled = !this.notificationsEnabled;
Philip Zeyligerbc6b6292025-04-30 18:00:15 +0000719
720 // If enabling notifications, check permissions
721 if (this.notificationsEnabled) {
722 this.checkNotificationPermission();
723 }
724
725 // Save preference to localStorage
726 try {
727 localStorage.setItem(
728 "sketch-notifications-enabled",
729 String(this.notificationsEnabled),
730 );
731 } catch (error) {
732 console.error("Error saving notification preference:", error);
733 }
734 }
735
Philip Zeyligerdb5e9b42025-04-30 19:58:13 +0000736 // Handle window focus event
737 private _handleWindowFocus(): void {
738 this._windowFocused = true;
739 }
740
741 // Handle window blur event
742 private _handleWindowBlur(): void {
743 this._windowFocused = false;
744 }
745
Philip Zeyligerbc6b6292025-04-30 18:00:15 +0000746 // Show notification for message with EndOfTurn=true
747 private async showEndOfTurnNotification(
748 message: AgentMessage,
749 ): Promise<void> {
750 // Don't show notifications if they're disabled
751 if (!this.notificationsEnabled) return;
752
Philip Zeyligerdb5e9b42025-04-30 19:58:13 +0000753 // Don't show notifications if the window is focused
754 if (this._windowFocused) return;
755
Philip Zeyligerbc6b6292025-04-30 18:00:15 +0000756 // Check if we have permission to show notifications
757 const hasPermission = await this.checkNotificationPermission();
758 if (!hasPermission) return;
759
Philip Zeyliger32011332025-04-30 20:59:40 +0000760 // Only show notifications for agent messages with end_of_turn=true and no parent_conversation_id
761 if (
762 message.type !== "agent" ||
763 !message.end_of_turn ||
764 message.parent_conversation_id
765 )
766 return;
Philip Zeyligerbc6b6292025-04-30 18:00:15 +0000767
768 // Create a title that includes the sketch title
769 const notificationTitle = `Sketch: ${this.title || "untitled"}`;
770
771 // Extract the beginning of the message content (first 100 chars)
772 const messagePreview = message.content
773 ? message.content.substring(0, 100) +
774 (message.content.length > 100 ? "..." : "")
775 : "Agent has completed its turn";
776
777 // Create and show the notification
778 try {
779 new Notification(notificationTitle, {
780 body: messagePreview,
Philip Zeyligerdb5e9b42025-04-30 19:58:13 +0000781 icon: "https://sketch.dev/favicon.ico", // Use sketch.dev favicon for notification
Philip Zeyligerbc6b6292025-04-30 18:00:15 +0000782 });
783 } catch (error) {
784 console.error("Error showing notification:", error);
785 }
786 }
787
Sean McCullough86b56862025-04-18 13:04:03 -0700788 private handleDataChanged(eventData: {
789 state: State;
Sean McCulloughd9f13372025-04-21 15:08:49 -0700790 newMessages: AgentMessage[];
Sean McCullough86b56862025-04-18 13:04:03 -0700791 }): void {
Philip Zeyligerbce3a132025-04-30 22:03:39 +0000792 const { state, newMessages } = eventData;
Sean McCullough86b56862025-04-18 13:04:03 -0700793
794 // Update state if we received it
795 if (state) {
Josh Bleecher Snydere81233f2025-04-30 04:05:41 +0000796 // Ensure we're using the latest call status to prevent indicators from being stuck
Autoformatterf830c9d2025-04-30 18:16:01 +0000797 if (
798 state.outstanding_llm_calls === 0 &&
799 state.outstanding_tool_calls.length === 0
800 ) {
Josh Bleecher Snydere81233f2025-04-30 04:05:41 +0000801 // Force reset containerState calls when nothing is reported as in progress
802 state.outstanding_llm_calls = 0;
803 state.outstanding_tool_calls = [];
804 }
Autoformatterf830c9d2025-04-30 18:16:01 +0000805
Sean McCullough86b56862025-04-18 13:04:03 -0700806 this.containerState = state;
807 this.title = state.title;
Philip Zeyliger9b999b02025-04-25 16:31:50 +0000808
809 // Update document title when sketch title changes
810 this.updateDocumentTitle();
Sean McCullough86b56862025-04-18 13:04:03 -0700811 }
812
Sean McCullough86b56862025-04-18 13:04:03 -0700813 // Update messages
Pokey Rulee2a8c2f2025-04-23 15:09:25 +0100814 this.messages = aggregateAgentMessages(this.messages, newMessages);
Autoformattercf570962025-04-30 17:27:39 +0000815
Philip Zeyliger47b71c92025-04-30 15:43:39 +0000816 // Process new messages to find commit messages
Philip Zeyliger16fa8b42025-05-02 04:28:16 +0000817 // Update last commit info via container status component
818 if (this.containerStatusElement) {
819 this.containerStatusElement.updateLastCommitInfo(newMessages);
820 }
Philip Zeyligerbc6b6292025-04-30 18:00:15 +0000821
822 // Check for agent messages with end_of_turn=true and show notifications
Philip Zeyligerbce3a132025-04-30 22:03:39 +0000823 if (newMessages && newMessages.length > 0) {
Philip Zeyligerbc6b6292025-04-30 18:00:15 +0000824 for (const message of newMessages) {
Philip Zeyliger32011332025-04-30 20:59:40 +0000825 if (
826 message.type === "agent" &&
827 message.end_of_turn &&
828 !message.parent_conversation_id
829 ) {
Philip Zeyligerbc6b6292025-04-30 18:00:15 +0000830 this.showEndOfTurnNotification(message);
831 break; // Only show one notification per batch of messages
832 }
833 }
834 }
Sean McCullough86b56862025-04-18 13:04:03 -0700835 }
836
837 private handleConnectionStatusChanged(
838 status: ConnectionStatus,
Philip Zeyliger72682df2025-04-23 13:09:46 -0700839 errorMessage?: string,
Sean McCullough86b56862025-04-18 13:04:03 -0700840 ): void {
841 this.connectionStatus = status;
842 this.connectionErrorMessage = errorMessage || "";
Philip Zeyliger9b999b02025-04-25 16:31:50 +0000843
844 // Update document title when connection status changes
845 this.updateDocumentTitle();
Sean McCullough86b56862025-04-18 13:04:03 -0700846 }
847
Sean McCulloughd3906e22025-04-29 17:32:14 +0000848 private async _handleStopClick(): Promise<void> {
849 try {
850 const response = await fetch("cancel", {
851 method: "POST",
852 headers: {
853 "Content-Type": "application/json",
854 },
855 body: JSON.stringify({ reason: "user requested cancellation" }),
856 });
857
858 if (!response.ok) {
859 const errorData = await response.text();
860 throw new Error(
861 `Failed to stop operation: ${response.status} - ${errorData}`,
862 );
863 }
864
Philip Zeyligerbce3a132025-04-30 22:03:39 +0000865 // Stop request sent
Sean McCulloughd3906e22025-04-29 17:32:14 +0000866 } catch (error) {
867 console.error("Error stopping operation:", error);
Sean McCulloughd3906e22025-04-29 17:32:14 +0000868 }
869 }
870
Philip Zeyliger2c4db092025-04-28 16:57:50 -0700871 openRestartModal() {
872 this.restartModalOpen = true;
873 }
874
875 handleRestartModalClose() {
876 this.restartModalOpen = false;
877 }
878
Sean McCullough485afc62025-04-28 14:28:39 -0700879 async _handleMutlipleChoiceSelected(e: CustomEvent) {
880 const chatInput = this.shadowRoot?.querySelector(
881 "sketch-chat-input",
882 ) as SketchChatInput;
883 if (chatInput) {
884 chatInput.content = e.detail.responseText;
885 chatInput.focus();
886 }
887 }
888
Sean McCullough86b56862025-04-18 13:04:03 -0700889 async _sendChat(e: CustomEvent) {
890 console.log("app shell: _sendChat", e);
Sean McCullough485afc62025-04-28 14:28:39 -0700891 e.preventDefault();
892 e.stopPropagation();
Sean McCullough86b56862025-04-18 13:04:03 -0700893 const message = e.detail.message?.trim();
894 if (message == "") {
895 return;
896 }
897 try {
Josh Bleecher Snyder98b64d12025-05-12 19:42:43 +0000898 // Always switch to chat view when sending a message so user can see processing
899 if (this.viewMode !== "chat") {
900 this.toggleViewMode("chat", true);
901 }
Autoformatter5c7f9572025-05-13 01:17:31 +0000902
Sean McCullough86b56862025-04-18 13:04:03 -0700903 // Send the message to the server
904 const response = await fetch("chat", {
905 method: "POST",
906 headers: {
907 "Content-Type": "application/json",
908 },
909 body: JSON.stringify({ message }),
910 });
911
912 if (!response.ok) {
913 const errorData = await response.text();
914 throw new Error(`Server error: ${response.status} - ${errorData}`);
915 }
Sean McCullough86b56862025-04-18 13:04:03 -0700916 } catch (error) {
917 console.error("Error sending chat message:", error);
918 const statusText = document.getElementById("statusText");
919 if (statusText) {
920 statusText.textContent = "Error sending message";
921 }
922 }
923 }
924
Pokey Rule4097e532025-04-24 18:55:28 +0100925 private scrollContainerRef = createRef<HTMLElement>();
926
Sean McCullough86b56862025-04-18 13:04:03 -0700927 render() {
928 return html`
Pokey Rule4097e532025-04-24 18:55:28 +0100929 <div id="top-banner">
Sean McCullough86b56862025-04-18 13:04:03 -0700930 <div class="title-container">
931 <h1 class="banner-title">sketch</h1>
932 <h2 id="chatTitle" class="chat-title">${this.title}</h2>
933 </div>
934
Philip Zeyligerbce3a132025-04-30 22:03:39 +0000935 <!-- Container status info moved above tabs -->
Sean McCullough86b56862025-04-18 13:04:03 -0700936 <sketch-container-status
937 .state=${this.containerState}
Philip Zeyliger16fa8b42025-05-02 04:28:16 +0000938 id="container-status"
Sean McCullough86b56862025-04-18 13:04:03 -0700939 ></sketch-container-status>
Autoformattercf570962025-04-30 17:27:39 +0000940
Philip Zeyliger16fa8b42025-05-02 04:28:16 +0000941 <!-- Last Commit section moved to sketch-container-status -->
Philip Zeyligerbce3a132025-04-30 22:03:39 +0000942
Philip Zeyliger16fa8b42025-05-02 04:28:16 +0000943 <!-- Views section with tabs -->
944 <sketch-view-mode-select></sketch-view-mode-select>
Sean McCullough86b56862025-04-18 13:04:03 -0700945
946 <div class="refresh-control">
Sean McCulloughd3906e22025-04-29 17:32:14 +0000947 <button
Philip Zeyliger2c4db092025-04-28 16:57:50 -0700948 id="restartButton"
949 class="restart-button"
950 ?disabled=${this.containerState.message_count === 0}
951 @click=${this.openRestartModal}
Sean McCulloughd3906e22025-04-29 17:32:14 +0000952 >
Philip Zeyligerbce3a132025-04-30 22:03:39 +0000953 <svg
954 class="button-icon"
955 xmlns="http://www.w3.org/2000/svg"
956 viewBox="0 0 24 24"
957 fill="none"
958 stroke="currentColor"
959 stroke-width="2"
960 stroke-linecap="round"
961 stroke-linejoin="round"
962 >
963 <path d="M3 12a9 9 0 1 0 9-9 9.75 9.75 0 0 0-6.74 2.74L3 8" />
964 <path d="M3 3v5h5" />
965 </svg>
966 <span class="button-text">Restart</span>
Philip Zeyliger2c4db092025-04-28 16:57:50 -0700967 </button>
Philip Zeyligerbce3a132025-04-30 22:03:39 +0000968 <button
969 id="stopButton"
970 class="stop-button"
971 ?disabled=${(this.containerState?.outstanding_llm_calls || 0) ===
972 0 &&
973 (this.containerState?.outstanding_tool_calls || []).length === 0}
974 >
975 <svg
976 class="button-icon"
977 xmlns="http://www.w3.org/2000/svg"
978 viewBox="0 0 24 24"
979 fill="none"
980 stroke="currentColor"
981 stroke-width="2"
982 stroke-linecap="round"
983 stroke-linejoin="round"
984 >
985 <rect x="6" y="6" width="12" height="12" />
986 </svg>
987 <span class="button-text">Stop</span>
Sean McCullough86b56862025-04-18 13:04:03 -0700988 </button>
989
Philip Zeyligerdb5e9b42025-04-30 19:58:13 +0000990 <div
991 class="notifications-toggle"
992 @click=${this._handleNotificationsToggle}
993 title="${this.notificationsEnabled
994 ? "Disable"
Philip Zeyligerbce3a132025-04-30 22:03:39 +0000995 : "Enable"} notifications when the agent completes its turn"
Philip Zeyligerdb5e9b42025-04-30 19:58:13 +0000996 >
997 <div
998 class="bell-icon ${!this.notificationsEnabled
999 ? "bell-disabled"
1000 : ""}"
1001 >
1002 <!-- Bell SVG icon -->
1003 <svg
1004 xmlns="http://www.w3.org/2000/svg"
1005 width="16"
1006 height="16"
1007 fill="currentColor"
1008 viewBox="0 0 16 16"
1009 >
1010 <path
1011 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"
1012 />
1013 </svg>
1014 </div>
Philip Zeyligerbc6b6292025-04-30 18:00:15 +00001015 </div>
1016
Philip Zeyliger99a9a022025-04-27 15:15:25 +00001017 <sketch-call-status
Sean McCulloughd9d45812025-04-30 16:53:41 -07001018 .agentState=${this.containerState?.agent_state}
Philip Zeyliger99a9a022025-04-27 15:15:25 +00001019 .llmCalls=${this.containerState?.outstanding_llm_calls || 0}
1020 .toolCalls=${this.containerState?.outstanding_tool_calls || []}
Philip Zeyliger72318392025-05-14 02:56:07 +00001021 .isIdle=${this.messages.length > 0
1022 ? this.messages[this.messages.length - 1]?.end_of_turn &&
1023 !this.messages[this.messages.length - 1]?.parent_conversation_id
1024 : true}
Philip Zeyliger5e357022025-05-16 04:50:34 +00001025 .isDisconnected=${this.connectionStatus === "disconnected"}
Philip Zeyliger99a9a022025-04-27 15:15:25 +00001026 ></sketch-call-status>
Philip Zeyliger25f6ff12025-05-02 04:24:10 +00001027
1028 <sketch-network-status
1029 connection=${this.connectionStatus}
1030 error=${this.connectionErrorMessage}
1031 ></sketch-network-status>
Sean McCullough86b56862025-04-18 13:04:03 -07001032 </div>
1033 </div>
1034
Pokey Rule4097e532025-04-24 18:55:28 +01001035 <div id="view-container" ${ref(this.scrollContainerRef)}>
1036 <div id="view-container-inner">
1037 <div
1038 class="chat-view ${this.viewMode === "chat" ? "view-active" : ""}"
1039 >
1040 <sketch-timeline
1041 .messages=${this.messages}
1042 .scrollContainer=${this.scrollContainerRef}
Philip Zeyliger16fa8b42025-05-02 04:28:16 +00001043 .agentState=${this.containerState?.agent_state}
1044 .llmCalls=${this.containerState?.outstanding_llm_calls || 0}
1045 .toolCalls=${this.containerState?.outstanding_tool_calls || []}
Pokey Rule4097e532025-04-24 18:55:28 +01001046 ></sketch-timeline>
1047 </div>
1048 <div
1049 class="diff-view ${this.viewMode === "diff" ? "view-active" : ""}"
1050 >
1051 <sketch-diff-view
1052 .commitHash=${this.currentCommitHash}
1053 ></sketch-diff-view>
1054 </div>
Philip Zeyliger272a90e2025-05-16 14:49:51 -07001055
1056 <div
1057 class="diff2-view ${this.viewMode === "diff2" ? "view-active" : ""}"
1058 >
1059 <sketch-diff2-view
1060 .commit=${this.currentCommitHash}
1061 .gitService=${new DefaultGitDataService()}
1062 @diff-comment="${this._handleDiffComment}"
1063 ></sketch-diff2-view>
1064 </div>
Philip Zeyliger2d4c48f2025-05-02 23:35:03 +00001065
Pokey Rule4097e532025-04-24 18:55:28 +01001066 <div
1067 class="terminal-view ${this.viewMode === "terminal"
1068 ? "view-active"
1069 : ""}"
1070 >
1071 <sketch-terminal></sketch-terminal>
1072 </div>
Philip Zeyliger272a90e2025-05-16 14:49:51 -07001073
Sean McCullough86b56862025-04-18 13:04:03 -07001074 </div>
1075 </div>
1076
Pokey Rule4097e532025-04-24 18:55:28 +01001077 <div id="chat-input">
1078 <sketch-chat-input @send-chat="${this._sendChat}"></sketch-chat-input>
1079 </div>
Philip Zeyliger2c4db092025-04-28 16:57:50 -07001080
1081 <sketch-restart-modal
1082 ?open=${this.restartModalOpen}
1083 @close=${this.handleRestartModalClose}
1084 .containerState=${this.containerState}
1085 .messages=${this.messages}
1086 ></sketch-restart-modal>
Sean McCullough86b56862025-04-18 13:04:03 -07001087 `;
1088 }
1089
1090 /**
Sean McCullough86b56862025-04-18 13:04:03 -07001091 * Lifecycle callback when component is first connected to DOM
1092 */
1093 firstUpdated(): void {
1094 if (this.viewMode !== "chat") {
1095 return;
1096 }
1097
1098 // Initial scroll to bottom when component is first rendered
1099 setTimeout(
1100 () => this.scrollTo({ top: this.scrollHeight, behavior: "smooth" }),
Philip Zeyliger72682df2025-04-23 13:09:46 -07001101 50,
Sean McCullough86b56862025-04-18 13:04:03 -07001102 );
1103
Philip Zeyliger2c4db092025-04-28 16:57:50 -07001104 // Setup stop button
1105 const stopButton = this.renderRoot?.querySelector(
1106 "#stopButton",
1107 ) as HTMLButtonElement;
1108 stopButton?.addEventListener("click", async () => {
1109 try {
Sean McCullough495cb962025-05-01 16:25:53 -07001110 const response = await fetch("cancel", {
Philip Zeyliger2c4db092025-04-28 16:57:50 -07001111 method: "POST",
1112 headers: {
1113 "Content-Type": "application/json",
1114 },
1115 body: JSON.stringify({ reason: "User clicked stop button" }),
1116 });
1117 if (!response.ok) {
1118 console.error("Failed to cancel:", await response.text());
1119 }
1120 } catch (error) {
1121 console.error("Error cancelling operation:", error);
1122 }
1123 });
1124
Philip Zeyliger47b71c92025-04-30 15:43:39 +00001125 // Process any existing messages to find commit information
1126 if (this.messages && this.messages.length > 0) {
Philip Zeyliger16fa8b42025-05-02 04:28:16 +00001127 // Update last commit info via container status component
1128 if (this.containerStatusElement) {
1129 this.containerStatusElement.updateLastCommitInfo(this.messages);
1130 }
Philip Zeyliger47b71c92025-04-30 15:43:39 +00001131 }
Sean McCullough86b56862025-04-18 13:04:03 -07001132 }
1133}
1134
1135declare global {
1136 interface HTMLElementTagNameMap {
1137 "sketch-app-shell": SketchAppShell;
1138 }
1139}