blob: 7ea43c79b88ffee2d8d28a3cb306030c6c788e58 [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";
20
21import { createRef, ref } from "lit/directives/ref.js";
Sean McCullough485afc62025-04-28 14:28:39 -070022import { SketchChatInput } from "./sketch-chat-input";
Sean McCullough86b56862025-04-18 13:04:03 -070023
Philip Zeyliger272a90e2025-05-16 14:49:51 -070024type ViewMode = "chat" | "diff" | "diff2" | "terminal";
Sean McCullough86b56862025-04-18 13:04:03 -070025
26@customElement("sketch-app-shell")
27export class SketchAppShell extends LitElement {
Philip Zeyliger2d4c48f2025-05-02 23:35:03 +000028 // Current view mode (chat, diff, terminal)
Sean McCullough86b56862025-04-18 13:04:03 -070029 @state()
Philip Zeyliger272a90e2025-05-16 14:49:51 -070030 viewMode: ViewMode = "chat";
Sean McCullough86b56862025-04-18 13:04:03 -070031
32 // Current commit hash for diff view
33 @state()
34 currentCommitHash: string = "";
35
Philip Zeyliger47b71c92025-04-30 15:43:39 +000036 // Last commit information
37 @state()
Philip Zeyliger16fa8b42025-05-02 04:28:16 +000038
39 // Reference to the container status element
40 containerStatusElement: any = null;
Philip Zeyliger47b71c92025-04-30 15:43:39 +000041
Sean McCullough86b56862025-04-18 13:04:03 -070042 // 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 Zeyliger47b71c92025-04-30 15:43:39 +000047 .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 }
Autoformattercf570962025-04-30 17:27:39 +000061
Philip Zeyliger47b71c92025-04-30 15:43:39 +000062 @keyframes fadeInOut {
Autoformattercf570962025-04-30 17:27:39 +000063 0% {
64 opacity: 0;
65 }
66 20% {
67 opacity: 1;
68 }
69 80% {
70 opacity: 1;
71 }
72 100% {
73 opacity: 0;
74 }
Philip Zeyliger47b71c92025-04-30 15:43:39 +000075 }
Autoformattercf570962025-04-30 17:27:39 +000076
Philip Zeyliger47b71c92025-04-30 15:43:39 +000077 .commit-branch-indicator {
78 color: #28a745;
79 }
Autoformattercf570962025-04-30 17:27:39 +000080
Philip Zeyliger47b71c92025-04-30 15:43:39 +000081 .commit-hash-indicator {
82 color: #0366d6;
83 }
Sean McCullough86b56862025-04-18 13:04:03 -070084 :host {
85 display: block;
Sean McCullough71941bd2025-04-18 13:31:48 -070086 font-family:
87 system-ui,
88 -apple-system,
89 BlinkMacSystemFont,
90 "Segoe UI",
91 Roboto,
92 sans-serif;
Sean McCullough86b56862025-04-18 13:04:03 -070093 color: #333;
94 line-height: 1.4;
Pokey Rule4097e532025-04-24 18:55:28 +010095 height: 100vh;
Sean McCullough86b56862025-04-18 13:04:03 -070096 width: 100%;
97 position: relative;
98 overflow-x: hidden;
Pokey Rule4097e532025-04-24 18:55:28 +010099 display: flex;
100 flex-direction: column;
Sean McCullough86b56862025-04-18 13:04:03 -0700101 }
102
103 /* Top banner with combined elements */
Pokey Rule4097e532025-04-24 18:55:28 +0100104 #top-banner {
Sean McCullough86b56862025-04-18 13:04:03 -0700105 display: flex;
Philip Zeyligere66db3e2025-04-27 15:40:39 +0000106 align-self: stretch;
Sean McCullough86b56862025-04-18 13:04:03 -0700107 justify-content: space-between;
108 align-items: center;
Philip Zeyligere66db3e2025-04-27 15:40:39 +0000109 padding: 0 20px;
Sean McCullough86b56862025-04-18 13:04:03 -0700110 margin-bottom: 0;
111 border-bottom: 1px solid #eee;
Philip Zeyligere66db3e2025-04-27 15:40:39 +0000112 gap: 20px;
Sean McCullough86b56862025-04-18 13:04:03 -0700113 background: white;
Sean McCullough86b56862025-04-18 13:04:03 -0700114 box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
Philip Zeyligere66db3e2025-04-27 15:40:39 +0000115 width: 100%;
116 height: 48px;
117 padding-right: 30px; /* Extra padding on the right to prevent elements from hitting the edge */
Sean McCullough86b56862025-04-18 13:04:03 -0700118 }
119
Pokey Rule4097e532025-04-24 18:55:28 +0100120 /* View mode container styles - mirroring timeline.css structure */
121 #view-container {
122 align-self: stretch;
123 overflow-y: auto;
124 flex: 1;
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700125 display: flex;
126 flex-direction: column;
127 min-height: 0; /* Critical for proper flex child behavior */
Pokey Rule4097e532025-04-24 18:55:28 +0100128 }
129
130 #view-container-inner {
131 max-width: 1200px;
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700132 width: calc(100% - 40px);
Pokey Rule4097e532025-04-24 18:55:28 +0100133 margin: 0 auto;
134 position: relative;
135 padding-bottom: 10px;
136 padding-top: 10px;
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700137 display: flex;
138 flex-direction: column;
139 height: 100%; /* Ensure it takes full height of parent */
Pokey Rule4097e532025-04-24 18:55:28 +0100140 }
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 McCullough86b56862025-04-18 13:04:03 -0700148 .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 Snydereb5166a2025-04-30 17:04:20 +0000162 font-size: 14px;
Sean McCullough86b56862025-04-18 13:04:03 -0700163 font-weight: normal;
164 font-style: italic;
165 white-space: nowrap;
166 overflow: hidden;
167 text-overflow: ellipsis;
168 }
169
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700170 /* 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 McCullough86b56862025-04-18 13:04:03 -0700173 max-width: 100%;
Pokey Rule46fff972025-04-25 14:57:44 +0100174 width: 100%;
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700175 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 McCullough86b56862025-04-18 13:04:03 -0700181 }
182
183 /* Individual view styles */
184 .chat-view,
185 .diff-view,
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700186 .diff2-view,
Sean McCullough86b56862025-04-18 13:04:03 -0700187 .terminal-view {
188 display: none; /* Hidden by default */
189 width: 100%;
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700190 height: 100%;
191 }
Autoformatter8c463622025-05-16 21:54:17 +0000192
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700193 /* Make chat view take full width available */
194 .chat-view.view-active {
195 display: flex;
196 flex-direction: column;
197 width: 100%;
198 }
Autoformatter8c463622025-05-16 21:54:17 +0000199
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700200 /* 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 McCullough86b56862025-04-18 13:04:03 -0700208 }
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 Snydereb5166a2025-04-30 17:04:20 +0000222 max-width: 30%;
Philip Zeyligere66db3e2025-04-27 15:40:39 +0000223 padding: 6px 0;
Sean McCullough86b56862025-04-18 13:04:03 -0700224 }
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 Zeyligere66db3e2025-04-27 15:40:39 +0000233 gap: 15px;
234 padding-left: 15px;
235 margin-right: 50px;
Sean McCullough86b56862025-04-18 13:04:03 -0700236 }
237
Pokey Rule397871d2025-05-19 15:02:45 +0100238 .stop-button,
239 .end-button {
Philip Zeyliger2c4db092025-04-28 16:57:50 -0700240 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 Zeyligerbce3a132025-04-30 22:03:39 +0000248 display: flex;
249 align-items: center;
250 gap: 6px;
Philip Zeyliger2c4db092025-04-28 16:57:50 -0700251 }
252
Philip Zeyligerbce3a132025-04-30 22:03:39 +0000253 .stop-button {
254 background: #dc3545;
Sean McCullough86b56862025-04-18 13:04:03 -0700255 color: white;
Sean McCullough86b56862025-04-18 13:04:03 -0700256 }
257
Philip Zeyligerbce3a132025-04-30 22:03:39 +0000258 .stop-button:hover:not(:disabled) {
259 background-color: #c82333;
Sean McCullough86b56862025-04-18 13:04:03 -0700260 }
261
Philip Zeyligerbce3a132025-04-30 22:03:39 +0000262 .stop-button:disabled {
263 background-color: #e9a8ad;
264 cursor: not-allowed;
265 opacity: 0.7;
Philip Zeyligerdb5e9b42025-04-30 19:58:13 +0000266 }
267
Philip Zeyligerbce3a132025-04-30 22:03:39 +0000268 .stop-button:disabled:hover {
269 background-color: #e9a8ad;
270 }
271
Pokey Rule397871d2025-05-19 15:02:45 +0100272 .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 Zeyligerbce3a132025-04-30 22:03:39 +0000287 .button-icon {
288 width: 16px;
289 height: 16px;
290 }
291
292 @media (max-width: 1400px) {
293 .button-text {
294 display: none;
295 }
296
Philip Zeyligerbce3a132025-04-30 22:03:39 +0000297 .stop-button {
298 padding: 6px;
299 }
300 }
301
302 /* Removed poll-updates class */
303
Philip Zeyligerbc6b6292025-04-30 18:00:15 +0000304 .notifications-toggle {
Sean McCullough86b56862025-04-18 13:04:03 -0700305 display: flex;
306 align-items: center;
Sean McCullough86b56862025-04-18 13:04:03 -0700307 font-size: 12px;
Philip Zeyligerbc6b6292025-04-30 18:00:15 +0000308 margin-right: 10px;
Philip Zeyligerdb5e9b42025-04-30 19:58:13 +0000309 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 McCullough86b56862025-04-18 13:04:03 -0700329 }
330 `;
331
332 // Header bar: Network connection status details
333 @property()
334 connectionStatus: ConnectionStatus = "disconnected";
Autoformattercf570962025-04-30 17:27:39 +0000335
Philip Zeyliger47b71c92025-04-30 15:43:39 +0000336 // Track if the last commit info has been copied
337 @state()
Philip Zeyliger16fa8b42025-05-02 04:28:16 +0000338 // lastCommitCopied moved to sketch-container-status
Sean McCullough86b56862025-04-18 13:04:03 -0700339
Philip Zeyligerbc6b6292025-04-30 18:00:15 +0000340 // Track notification preferences
341 @state()
Philip Zeyligerdb5e9b42025-04-30 19:58:13 +0000342 notificationsEnabled: boolean = false;
343
344 // Track if the window is focused to control notifications
345 @state()
346 private _windowFocused: boolean = document.hasFocus();
Philip Zeyligerbc6b6292025-04-30 18:00:15 +0000347
Sean McCullough86b56862025-04-18 13:04:03 -0700348 @property()
349 connectionErrorMessage: string = "";
350
Sean McCullough86b56862025-04-18 13:04:03 -0700351 // Chat messages
Pokey Rulee2a8c2f2025-04-23 15:09:25 +0100352 @property({ attribute: false })
Sean McCulloughd9f13372025-04-21 15:08:49 -0700353 messages: AgentMessage[] = [];
Sean McCullough86b56862025-04-18 13:04:03 -0700354
355 @property()
Philip Zeyliger9b999b02025-04-25 16:31:50 +0000356 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 McCullough86b56862025-04-18 13:04:03 -0700369
370 private dataManager = new DataManager();
371
Pokey Rulee2a8c2f2025-04-23 15:09:25 +0100372 @property({ attribute: false })
Sean McCulloughd9f13372025-04-21 15:08:49 -0700373 containerState: State = {
Philip Zeyligerd03318d2025-05-08 13:09:12 -0700374 state_version: 2,
Sean McCulloughd9f13372025-04-21 15:08:49 -0700375 title: "",
376 os: "",
377 message_count: 0,
378 hostname: "",
379 working_dir: "",
380 initial_commit: "",
Philip Zeyliger99a9a022025-04-27 15:15:25 +0000381 outstanding_llm_calls: 0,
382 outstanding_tool_calls: [],
Philip Zeyligerc72fff52025-04-29 20:17:54 +0000383 session_id: "",
384 ssh_available: false,
Philip Zeyliger2c4db092025-04-28 16:57:50 -0700385 ssh_error: "",
386 in_container: false,
387 first_message_index: 0,
Sean McCulloughd9f13372025-04-21 15:08:49 -0700388 };
Sean McCullough86b56862025-04-18 13:04:03 -0700389
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);
Pokey Rule397871d2025-05-19 15:02:45 +0100406 this._handleEndClick = this._handleEndClick.bind(this);
Philip Zeyligerbc6b6292025-04-30 18:00:15 +0000407 this._handleNotificationsToggle =
408 this._handleNotificationsToggle.bind(this);
Philip Zeyligerdb5e9b42025-04-30 19:58:13 +0000409 this._handleWindowFocus = this._handleWindowFocus.bind(this);
410 this._handleWindowBlur = this._handleWindowBlur.bind(this);
Philip Zeyligerbc6b6292025-04-30 18:00:15 +0000411
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 McCullough86b56862025-04-18 13:04:03 -0700421 }
422
423 // See https://lit.dev/docs/components/lifecycle/
424 connectedCallback() {
425 super.connectedCallback();
426
Philip Zeyliger16fa8b42025-05-02 04:28:16 +0000427 // Get reference to the container status element
428 setTimeout(() => {
429 this.containerStatusElement =
430 this.shadowRoot?.getElementById("container-status");
431 }, 0);
432
Sean McCullough86b56862025-04-18 13:04:03 -0700433 // 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 Rulee2a8c2f2025-04-23 15:09:25 +0100440 window.addEventListener("popstate", this._handlePopState);
Sean McCullough86b56862025-04-18 13:04:03 -0700441
442 // Add event listeners
Pokey Rulee2a8c2f2025-04-23 15:09:25 +0100443 window.addEventListener("view-mode-select", this._handleViewModeSelect);
Pokey Rulee2a8c2f2025-04-23 15:09:25 +0100444 window.addEventListener("show-commit-diff", this._handleShowCommitDiff);
Sean McCullough86b56862025-04-18 13:04:03 -0700445
Philip Zeyligerdb5e9b42025-04-30 19:58:13 +0000446 // Add window focus/blur listeners for controlling notifications
447 window.addEventListener("focus", this._handleWindowFocus);
448 window.addEventListener("blur", this._handleWindowBlur);
Sean McCullough485afc62025-04-28 14:28:39 -0700449 window.addEventListener(
450 "multiple-choice-selected",
451 this._handleMutlipleChoiceSelected,
452 );
Philip Zeyligerdb5e9b42025-04-30 19:58:13 +0000453
Sean McCullough86b56862025-04-18 13:04:03 -0700454 // register event listeners
455 this.dataManager.addEventListener(
456 "dataChanged",
Philip Zeyliger72682df2025-04-23 13:09:46 -0700457 this.handleDataChanged.bind(this),
Sean McCullough86b56862025-04-18 13:04:03 -0700458 );
459 this.dataManager.addEventListener(
460 "connectionStatusChanged",
Philip Zeyliger72682df2025-04-23 13:09:46 -0700461 this.handleConnectionStatusChanged.bind(this),
Sean McCullough86b56862025-04-18 13:04:03 -0700462 );
463
Philip Zeyliger9b999b02025-04-25 16:31:50 +0000464 // Set initial document title
465 this.updateDocumentTitle();
466
Sean McCullough86b56862025-04-18 13:04:03 -0700467 // Initialize the data manager
468 this.dataManager.initialize();
Autoformattercf570962025-04-30 17:27:39 +0000469
Philip Zeyliger47b71c92025-04-30 15:43:39 +0000470 // Process existing messages for commit info
471 if (this.messages && this.messages.length > 0) {
Philip Zeyliger16fa8b42025-05-02 04:28:16 +0000472 // Update last commit info via container status component
473 setTimeout(() => {
474 if (this.containerStatusElement) {
475 this.containerStatusElement.updateLastCommitInfo(this.messages);
476 }
477 }, 100);
Philip Zeyliger47b71c92025-04-30 15:43:39 +0000478 }
Sean McCullough86b56862025-04-18 13:04:03 -0700479 }
480
481 // See https://lit.dev/docs/components/lifecycle/
482 disconnectedCallback() {
483 super.disconnectedCallback();
Pokey Rulee2a8c2f2025-04-23 15:09:25 +0100484 window.removeEventListener("popstate", this._handlePopState);
Sean McCullough86b56862025-04-18 13:04:03 -0700485
486 // Remove event listeners
Pokey Rulee2a8c2f2025-04-23 15:09:25 +0100487 window.removeEventListener("view-mode-select", this._handleViewModeSelect);
Pokey Rulee2a8c2f2025-04-23 15:09:25 +0100488 window.removeEventListener("show-commit-diff", this._handleShowCommitDiff);
Philip Zeyligerdb5e9b42025-04-30 19:58:13 +0000489 window.removeEventListener("focus", this._handleWindowFocus);
490 window.removeEventListener("blur", this._handleWindowBlur);
Sean McCullough485afc62025-04-28 14:28:39 -0700491 window.removeEventListener(
492 "multiple-choice-selected",
493 this._handleMutlipleChoiceSelected,
494 );
Sean McCullough86b56862025-04-18 13:04:03 -0700495
496 // unregister data manager event listeners
497 this.dataManager.removeEventListener(
498 "dataChanged",
Philip Zeyliger72682df2025-04-23 13:09:46 -0700499 this.handleDataChanged.bind(this),
Sean McCullough86b56862025-04-18 13:04:03 -0700500 );
501 this.dataManager.removeEventListener(
502 "connectionStatusChanged",
Philip Zeyliger72682df2025-04-23 13:09:46 -0700503 this.handleConnectionStatusChanged.bind(this),
Sean McCullough86b56862025-04-18 13:04:03 -0700504 );
505
506 // Disconnect mutation observer if it exists
507 if (this.mutationObserver) {
Sean McCullough86b56862025-04-18 13:04:03 -0700508 this.mutationObserver.disconnect();
509 this.mutationObserver = null;
510 }
511 }
512
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700513 updateUrlForViewMode(mode: ViewMode): void {
Sean McCullough86b56862025-04-18 13:04:03 -0700514 // 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 McCullough71941bd2025-04-18 13:31:48 -0700523 const diffView = this.shadowRoot?.querySelector(
Philip Zeyliger72682df2025-04-23 13:09:46 -0700524 ".diff-view",
Sean McCullough71941bd2025-04-18 13:31:48 -0700525 ) as SketchDiffView;
Sean McCullough86b56862025-04-18 13:04:03 -0700526
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 Rulee2a8c2f2025-04-23 15:09:25 +0100537 private _handlePopState(event: PopStateEvent) {
Sean McCullough86b56862025-04-18 13:04:03 -0700538 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 Zeyliger272a90e2025-05-16 14:49:51 -0700549 const mode = event.detail.mode as "chat" | "diff" | "diff2" | "terminal";
Sean McCullough86b56862025-04-18 13:04:03 -0700550 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 McCullough485afc62025-04-28 14:28:39 -0700563 private _handleMultipleChoice(event: CustomEvent) {
564 window.console.log("_handleMultipleChoice", event);
565 this._sendChat;
566 }
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700567
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 McCullough86b56862025-04-18 13:04:03 -0700572 /**
Sean McCullough86b56862025-04-18 13:04:03 -0700573 * Listen for commit diff event
574 * @param commitHash The commit hash to show diff for
575 */
Pokey Rulee2a8c2f2025-04-23 15:09:25 +0100576 private showCommitDiff(commitHash: string): void {
Sean McCullough86b56862025-04-18 13:04:03 -0700577 // Store the commit hash
578 this.currentCommitHash = commitHash;
579
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700580 this.toggleViewMode("diff2", true);
Sean McCullough86b56862025-04-18 13:04:03 -0700581
Sean McCullough86b56862025-04-18 13:04:03 -0700582 this.updateComplete.then(() => {
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700583 const diff2View = this.shadowRoot?.querySelector("sketch-diff2-view");
584 if (diff2View) {
585 (diff2View as SketchDiff2View).refreshDiffView();
Sean McCullough86b56862025-04-18 13:04:03 -0700586 }
587 });
588 }
589
590 /**
Philip Zeyliger2d4c48f2025-05-02 23:35:03 +0000591 * Toggle between different view modes: chat, diff, terminal
Sean McCullough86b56862025-04-18 13:04:03 -0700592 */
Pokey Rulee2a8c2f2025-04-23 15:09:25 +0100593 private toggleViewMode(mode: ViewMode, updateHistory: boolean): void {
Sean McCullough86b56862025-04-18 13:04:03 -0700594 // 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 Rule46fff972025-04-25 14:57:44 +0100608 const viewContainerInner = this.shadowRoot?.querySelector(
609 "#view-container-inner",
610 );
Sean McCullough86b56862025-04-18 13:04:03 -0700611 const chatView = this.shadowRoot?.querySelector(".chat-view");
612 const diffView = this.shadowRoot?.querySelector(".diff-view");
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700613 const diff2View = this.shadowRoot?.querySelector(".diff2-view");
Sean McCullough86b56862025-04-18 13:04:03 -0700614 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 Zeyliger272a90e2025-05-16 14:49:51 -0700619 diff2View?.classList.remove("view-active");
Sean McCullough86b56862025-04-18 13:04:03 -0700620 terminalView?.classList.remove("view-active");
621
622 // Add/remove diff-active class on view container
623 if (mode === "diff") {
Pokey Rule46fff972025-04-25 14:57:44 +0100624 viewContainerInner?.classList.add("diff-active");
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700625 viewContainerInner?.classList.remove("diff2-active");
626 } else if (mode === "diff2") {
627 viewContainerInner?.classList.add("diff2-active");
628 viewContainerInner?.classList.remove("diff-active");
Sean McCullough86b56862025-04-18 13:04:03 -0700629 } else {
Pokey Rule46fff972025-04-25 14:57:44 +0100630 viewContainerInner?.classList.remove("diff-active");
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700631 viewContainerInner?.classList.remove("diff2-active");
Sean McCullough86b56862025-04-18 13:04:03 -0700632 }
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;
Autoformatter8c463622025-05-16 21:54:17 +0000650
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700651 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
Autoformatter8c463622025-05-16 21:54:17 +0000655 const diff2ViewComp =
656 this.shadowRoot?.querySelector("sketch-diff2-view");
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700657 if (diff2ViewComp) {
658 (diff2ViewComp as SketchDiff2View).refreshDiffView();
659 }
660 break;
Philip Zeyliger2d4c48f2025-05-02 23:35:03 +0000661
Sean McCullough86b56862025-04-18 13:04:03 -0700662 case "terminal":
663 terminalView?.classList.add("view-active");
664 break;
665 }
666
667 // Update view mode buttons
668 const viewModeSelect = this.shadowRoot?.querySelector(
Philip Zeyliger72682df2025-04-23 13:09:46 -0700669 "sketch-view-mode-select",
Sean McCullough86b56862025-04-18 13:04:03 -0700670 );
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 McCullough86b56862025-04-18 13:04:03 -0700679 });
680 }
681
Philip Zeyliger9b999b02025-04-25 16:31:50 +0000682 /**
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 Zeyligerbc6b6292025-04-30 18:00:15 +0000696 // 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 Zeyligerdb5e9b42025-04-30 19:58:13 +0000718 // Handle notifications toggle click
719 private _handleNotificationsToggle(): void {
720 this.notificationsEnabled = !this.notificationsEnabled;
Philip Zeyligerbc6b6292025-04-30 18:00:15 +0000721
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 Zeyligerdb5e9b42025-04-30 19:58:13 +0000738 // 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 Zeyligerbc6b6292025-04-30 18:00:15 +0000748 // 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 Zeyligerdb5e9b42025-04-30 19:58:13 +0000755 // Don't show notifications if the window is focused
756 if (this._windowFocused) return;
757
Philip Zeyligerbc6b6292025-04-30 18:00:15 +0000758 // Check if we have permission to show notifications
759 const hasPermission = await this.checkNotificationPermission();
760 if (!hasPermission) return;
761
Philip Zeyliger32011332025-04-30 20:59:40 +0000762 // 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 Zeyligerbc6b6292025-04-30 18:00:15 +0000769
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 Zeyligerdb5e9b42025-04-30 19:58:13 +0000783 icon: "https://sketch.dev/favicon.ico", // Use sketch.dev favicon for notification
Philip Zeyligerbc6b6292025-04-30 18:00:15 +0000784 });
785 } catch (error) {
786 console.error("Error showing notification:", error);
787 }
788 }
789
Sean McCullough86b56862025-04-18 13:04:03 -0700790 private handleDataChanged(eventData: {
791 state: State;
Sean McCulloughd9f13372025-04-21 15:08:49 -0700792 newMessages: AgentMessage[];
Sean McCullough86b56862025-04-18 13:04:03 -0700793 }): void {
Philip Zeyligerbce3a132025-04-30 22:03:39 +0000794 const { state, newMessages } = eventData;
Sean McCullough86b56862025-04-18 13:04:03 -0700795
796 // Update state if we received it
797 if (state) {
Josh Bleecher Snydere81233f2025-04-30 04:05:41 +0000798 // Ensure we're using the latest call status to prevent indicators from being stuck
Autoformatterf830c9d2025-04-30 18:16:01 +0000799 if (
800 state.outstanding_llm_calls === 0 &&
801 state.outstanding_tool_calls.length === 0
802 ) {
Josh Bleecher Snydere81233f2025-04-30 04:05:41 +0000803 // Force reset containerState calls when nothing is reported as in progress
804 state.outstanding_llm_calls = 0;
805 state.outstanding_tool_calls = [];
806 }
Autoformatterf830c9d2025-04-30 18:16:01 +0000807
Sean McCullough86b56862025-04-18 13:04:03 -0700808 this.containerState = state;
809 this.title = state.title;
Philip Zeyliger9b999b02025-04-25 16:31:50 +0000810
811 // Update document title when sketch title changes
812 this.updateDocumentTitle();
Sean McCullough86b56862025-04-18 13:04:03 -0700813 }
814
Sean McCullough86b56862025-04-18 13:04:03 -0700815 // Update messages
Pokey Rulee2a8c2f2025-04-23 15:09:25 +0100816 this.messages = aggregateAgentMessages(this.messages, newMessages);
Autoformattercf570962025-04-30 17:27:39 +0000817
Philip Zeyliger47b71c92025-04-30 15:43:39 +0000818 // Process new messages to find commit messages
Philip Zeyliger16fa8b42025-05-02 04:28:16 +0000819 // Update last commit info via container status component
820 if (this.containerStatusElement) {
821 this.containerStatusElement.updateLastCommitInfo(newMessages);
822 }
Philip Zeyligerbc6b6292025-04-30 18:00:15 +0000823
824 // Check for agent messages with end_of_turn=true and show notifications
Philip Zeyligerbce3a132025-04-30 22:03:39 +0000825 if (newMessages && newMessages.length > 0) {
Philip Zeyligerbc6b6292025-04-30 18:00:15 +0000826 for (const message of newMessages) {
Philip Zeyliger32011332025-04-30 20:59:40 +0000827 if (
828 message.type === "agent" &&
829 message.end_of_turn &&
830 !message.parent_conversation_id
831 ) {
Philip Zeyligerbc6b6292025-04-30 18:00:15 +0000832 this.showEndOfTurnNotification(message);
833 break; // Only show one notification per batch of messages
834 }
835 }
836 }
Sean McCullough86b56862025-04-18 13:04:03 -0700837 }
838
839 private handleConnectionStatusChanged(
840 status: ConnectionStatus,
Philip Zeyliger72682df2025-04-23 13:09:46 -0700841 errorMessage?: string,
Sean McCullough86b56862025-04-18 13:04:03 -0700842 ): void {
843 this.connectionStatus = status;
844 this.connectionErrorMessage = errorMessage || "";
Philip Zeyliger9b999b02025-04-25 16:31:50 +0000845
846 // Update document title when connection status changes
847 this.updateDocumentTitle();
Sean McCullough86b56862025-04-18 13:04:03 -0700848 }
849
Sean McCulloughd3906e22025-04-29 17:32:14 +0000850 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 Zeyligerbce3a132025-04-30 22:03:39 +0000867 // Stop request sent
Sean McCulloughd3906e22025-04-29 17:32:14 +0000868 } catch (error) {
869 console.error("Error stopping operation:", error);
Sean McCulloughd3906e22025-04-29 17:32:14 +0000870 }
871 }
872
Pokey Rule397871d2025-05-19 15:02:45 +0100873 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 McCullough485afc62025-04-28 14:28:39 -0700928 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 McCullough86b56862025-04-18 13:04:03 -0700938 async _sendChat(e: CustomEvent) {
939 console.log("app shell: _sendChat", e);
Sean McCullough485afc62025-04-28 14:28:39 -0700940 e.preventDefault();
941 e.stopPropagation();
Sean McCullough86b56862025-04-18 13:04:03 -0700942 const message = e.detail.message?.trim();
943 if (message == "") {
944 return;
945 }
946 try {
Josh Bleecher Snyder98b64d12025-05-12 19:42:43 +0000947 // 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 }
Autoformatter5c7f9572025-05-13 01:17:31 +0000951
Sean McCullough86b56862025-04-18 13:04:03 -0700952 // 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 McCullough86b56862025-04-18 13:04:03 -0700965 } 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 Rule4097e532025-04-24 18:55:28 +0100974 private scrollContainerRef = createRef<HTMLElement>();
975
Sean McCullough86b56862025-04-18 13:04:03 -0700976 render() {
977 return html`
Pokey Rule4097e532025-04-24 18:55:28 +0100978 <div id="top-banner">
Sean McCullough86b56862025-04-18 13:04:03 -0700979 <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 Zeyligerbce3a132025-04-30 22:03:39 +0000984 <!-- Container status info moved above tabs -->
Sean McCullough86b56862025-04-18 13:04:03 -0700985 <sketch-container-status
986 .state=${this.containerState}
Philip Zeyliger16fa8b42025-05-02 04:28:16 +0000987 id="container-status"
Sean McCullough86b56862025-04-18 13:04:03 -0700988 ></sketch-container-status>
Autoformattercf570962025-04-30 17:27:39 +0000989
Philip Zeyliger16fa8b42025-05-02 04:28:16 +0000990 <!-- Last Commit section moved to sketch-container-status -->
Philip Zeyligerbce3a132025-04-30 22:03:39 +0000991
Philip Zeyliger16fa8b42025-05-02 04:28:16 +0000992 <!-- Views section with tabs -->
993 <sketch-view-mode-select></sketch-view-mode-select>
Sean McCullough86b56862025-04-18 13:04:03 -0700994
995 <div class="refresh-control">
Sean McCulloughd3906e22025-04-29 17:32:14 +0000996 <button
Philip Zeyligerbce3a132025-04-30 22:03:39 +0000997 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 McCullough86b56862025-04-18 13:04:03 -07001016 </button>
Pokey Rule397871d2025-05-19 15:02:45 +01001017 <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 McCullough86b56862025-04-18 13:04:03 -07001037
Philip Zeyligerdb5e9b42025-04-30 19:58:13 +00001038 <div
1039 class="notifications-toggle"
1040 @click=${this._handleNotificationsToggle}
1041 title="${this.notificationsEnabled
1042 ? "Disable"
Philip Zeyligerbce3a132025-04-30 22:03:39 +00001043 : "Enable"} notifications when the agent completes its turn"
Philip Zeyligerdb5e9b42025-04-30 19:58:13 +00001044 >
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 Zeyligerbc6b6292025-04-30 18:00:15 +00001063 </div>
1064
Philip Zeyliger99a9a022025-04-27 15:15:25 +00001065 <sketch-call-status
Sean McCulloughd9d45812025-04-30 16:53:41 -07001066 .agentState=${this.containerState?.agent_state}
Philip Zeyliger99a9a022025-04-27 15:15:25 +00001067 .llmCalls=${this.containerState?.outstanding_llm_calls || 0}
1068 .toolCalls=${this.containerState?.outstanding_tool_calls || []}
Philip Zeyliger72318392025-05-14 02:56:07 +00001069 .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 Zeyliger5e357022025-05-16 04:50:34 +00001073 .isDisconnected=${this.connectionStatus === "disconnected"}
Philip Zeyliger99a9a022025-04-27 15:15:25 +00001074 ></sketch-call-status>
Philip Zeyliger25f6ff12025-05-02 04:24:10 +00001075
1076 <sketch-network-status
1077 connection=${this.connectionStatus}
1078 error=${this.connectionErrorMessage}
1079 ></sketch-network-status>
Sean McCullough86b56862025-04-18 13:04:03 -07001080 </div>
1081 </div>
1082
Pokey Rule4097e532025-04-24 18:55:28 +01001083 <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 Zeyliger16fa8b42025-05-02 04:28:16 +00001091 .agentState=${this.containerState?.agent_state}
1092 .llmCalls=${this.containerState?.outstanding_llm_calls || 0}
1093 .toolCalls=${this.containerState?.outstanding_tool_calls || []}
Pokey Rule4097e532025-04-24 18:55:28 +01001094 ></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>
Autoformatter8c463622025-05-16 21:54:17 +00001103
Philip Zeyliger272a90e2025-05-16 14:49:51 -07001104 <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 Zeyliger2d4c48f2025-05-02 23:35:03 +00001113
Pokey Rule4097e532025-04-24 18:55:28 +01001114 <div
1115 class="terminal-view ${this.viewMode === "terminal"
1116 ? "view-active"
1117 : ""}"
1118 >
1119 <sketch-terminal></sketch-terminal>
1120 </div>
Sean McCullough86b56862025-04-18 13:04:03 -07001121 </div>
1122 </div>
1123
Pokey Rule4097e532025-04-24 18:55:28 +01001124 <div id="chat-input">
1125 <sketch-chat-input @send-chat="${this._sendChat}"></sketch-chat-input>
1126 </div>
Sean McCullough86b56862025-04-18 13:04:03 -07001127 `;
1128 }
1129
1130 /**
Sean McCullough86b56862025-04-18 13:04:03 -07001131 * 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 Zeyliger72682df2025-04-23 13:09:46 -07001141 50,
Sean McCullough86b56862025-04-18 13:04:03 -07001142 );
1143
Philip Zeyliger2c4db092025-04-28 16:57:50 -07001144 // Setup stop button
1145 const stopButton = this.renderRoot?.querySelector(
1146 "#stopButton",
1147 ) as HTMLButtonElement;
1148 stopButton?.addEventListener("click", async () => {
1149 try {
Sean McCullough495cb962025-05-01 16:25:53 -07001150 const response = await fetch("cancel", {
Philip Zeyliger2c4db092025-04-28 16:57:50 -07001151 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 Rule397871d2025-05-19 15:02:45 +01001165 // 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 Zeyliger47b71c92025-04-30 15:43:39 +00001171 // Process any existing messages to find commit information
1172 if (this.messages && this.messages.length > 0) {
Philip Zeyliger16fa8b42025-05-02 04:28:16 +00001173 // Update last commit info via container status component
1174 if (this.containerStatusElement) {
1175 this.containerStatusElement.updateLastCommitInfo(this.messages);
1176 }
Philip Zeyliger47b71c92025-04-30 15:43:39 +00001177 }
Sean McCullough86b56862025-04-18 13:04:03 -07001178 }
1179}
1180
1181declare global {
1182 interface HTMLElementTagNameMap {
1183 "sketch-app-shell": SketchAppShell;
1184 }
1185}