Repository navigation
fix(vscode): persist Agent Manager focus per session #12801
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
66427c4
b0e60e9
ab296d4
5372f6f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "kilo-code": patch | ||
| --- | ||
|
|
||
| Preserve prompt or Agent Manager terminal focus independently for each session when switching sessions. |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -378,6 +378,74 @@ const AgentManagerContent: Component = () => { | |||||
| return sel === null ? null : nsKey(sel) | ||||||
| }) | ||||||
|
|
||||||
| type FocusOwner = "prompt" | { terminal: string } | ||||||
| const focusMemory = new Map<string, FocusOwner>() | ||||||
| const focusKey = () => { | ||||||
| const context = terms.sideKey() | ||||||
| const sessionID = session.currentSessionID() ?? activePendingId() ?? "new" | ||||||
| return `${context}:${sessionID}` | ||||||
| } | ||||||
| const forgetSessionFocus = (sessionID: string) => { | ||||||
| for (const key of focusMemory.keys()) if (key.endsWith(`:${sessionID}`)) focusMemory.delete(key) | ||||||
| } | ||||||
| const forgetContextFocus = (context: string) => { | ||||||
| for (const key of focusMemory.keys()) if (key.startsWith(`${context}:`)) focusMemory.delete(key) | ||||||
| } | ||||||
| const forgetTerminalFocus = (terminalID: string) => { | ||||||
| for (const [key, owner] of focusMemory) { | ||||||
| if (owner !== "prompt" && owner.terminal === terminalID) focusMemory.delete(key) | ||||||
| } | ||||||
| } | ||||||
| const rememberPromptFocus = (focused: boolean) => { | ||||||
| if (focused) focusMemory.set(focusKey(), "prompt") | ||||||
| } | ||||||
| const terminalVisible = () => sidePanel() === "terminal" && !history() && !reviewActive() | ||||||
| const focusOnDraftChange = () => { | ||||||
| const key = focusKey() | ||||||
| const owner = focusMemory.get(key) | ||||||
| if (!owner || owner === "prompt") return true | ||||||
| if (!terms.sidesForContext(terms.sideKey()).some((term) => term.id === owner.terminal)) { | ||||||
| focusMemory.delete(key) | ||||||
| return true | ||||||
| } | ||||||
| return terminalVisible() ? false : true | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. SUGGESTION: Minor readability nit on the new guard. While here, the freshly extracted
Suggested change
Reply with |
||||||
| } | ||||||
| const restoreFocus = () => { | ||||||
| const key = focusKey() | ||||||
| const owner = focusMemory.get(key) | ||||||
| if (owner && owner !== "prompt") { | ||||||
| const context = terms.sideKey() | ||||||
| const terminal = terms.sidesForContext(context).find((term) => term.id === owner.terminal) | ||||||
| if (terminal && terminalVisible()) { | ||||||
| terms.setSideActive(context, terminal.id) | ||||||
| terms.requestFocus(terminal.id) | ||||||
| return | ||||||
| } | ||||||
| if (!terminal) focusMemory.delete(key) | ||||||
| } | ||||||
| window.dispatchEvent(new Event("focusPrompt")) | ||||||
| } | ||||||
| createEffect( | ||||||
| on( | ||||||
| () => terms.focusedId(), | ||||||
| (id) => { | ||||||
| if (!id) return | ||||||
| const key = terms.contextFor(id) | ||||||
| if (!key || !terms.sidesForContext(key).some((term) => term.id === id)) return | ||||||
| focusMemory.set(focusKey(), { terminal: id }) | ||||||
| }, | ||||||
| { defer: true }, | ||||||
| ), | ||||||
| ) | ||||||
| createEffect( | ||||||
| on( | ||||||
| focusKey, | ||||||
| (_key, previous) => { | ||||||
| if (previous !== undefined) queueMicrotask(restoreFocus) | ||||||
| }, | ||||||
| { defer: true }, | ||||||
| ), | ||||||
| ) | ||||||
| // Ambient setup reveal restores the panel after success unless the user engaged. | ||||||
| const ambientSetup = createAmbientSetup({ | ||||||
| terms, | ||||||
|
|
@@ -1188,7 +1256,7 @@ const AgentManagerContent: Component = () => { | |||||
| const onWindowFocus = () => { | ||||||
| document.body.style.pointerEvents = "" | ||||||
| document.body.style.overflow = "" | ||||||
| window.dispatchEvent(new Event("focusPrompt")) | ||||||
| restoreFocus() | ||||||
| } | ||||||
| window.addEventListener("focus", onWindowFocus) | ||||||
|
|
||||||
|
|
@@ -1234,7 +1302,9 @@ const AgentManagerContent: Component = () => { | |||||
| // Mark sessions loaded as soon as the session context receives data (even if empty) | ||||||
| const unsubSessions = vscode.onMessage((msg) => { | ||||||
| if (msg.type === "sessionsLoaded" && !sessionsLoaded()) setSessionsLoaded(true) | ||||||
| if (msg.type === "agentManager.sessionClosed") handleCloseTab(msg.sessionId, false) | ||||||
| if (msg.type === "agentManager.sessionClosed") { | ||||||
| handleCloseTab(msg.sessionId, false) | ||||||
| } | ||||||
| }) | ||||||
| const unsubRun = vscode.onMessage((msg) => | ||||||
| applyRunStatus(msg, { ensure: (id) => registry.ensure(id), active: () => registry.active() }), | ||||||
|
|
@@ -1251,13 +1321,14 @@ const AgentManagerContent: Component = () => { | |||||
| showToast({ variant: "error", title: t("agentManager.terminal.errorTitle"), description: message }), | ||||||
| postMessage: (message) => vscode.postMessage(message as never), | ||||||
| onCreated: (contextKey, terminalId) => appendToTabOrder(contextKey, terminalId), | ||||||
| onSideCreated: (contextKey, terminalId) => { | ||||||
| onSideCreated: (contextKey, terminalId, focus) => { | ||||||
| // Focus only when the user is still looking at this panel — | ||||||
| // a slow create landing after a mode switch must not steal it. | ||||||
| if (sidePanel() === "terminal" && !history() && !reviewActive() && terms.sideKey() === contextKey) { | ||||||
| if (focus && sidePanel() === "terminal" && !history() && !reviewActive() && terms.sideKey() === contextKey) { | ||||||
| terms.requestFocus(terminalId) | ||||||
| } | ||||||
| }, | ||||||
| onSideClosed: (_contextKey, terminalId) => forgetTerminalFocus(terminalId), | ||||||
|
marius-kilocode marked this conversation as resolved.
Resolved
|
||||||
| onScriptRunning: (contextKey, terminalId) => { | ||||||
| if (terms.sideKey() !== contextKey) return | ||||||
| // Setup output is informational: reveal without stealing focus, and | ||||||
|
|
@@ -1800,6 +1871,7 @@ const AgentManagerContent: Component = () => { | |||||
| // Second press/click: execute the delete | ||||||
| if (pendingDelete() === worktreeId) { | ||||||
| cancelPendingDelete() | ||||||
| forgetContextFocus(nsKey(worktreeId)) | ||||||
| setBusyWorktrees((prev) => new Map([...prev, [wt.id, { reason: "deleting" as const }]])) | ||||||
| vscode.postMessage({ type: "agentManager.deleteWorktree", worktreeId: wt.id }) | ||||||
| if (selection() === wt.id) { | ||||||
|
|
@@ -1939,6 +2011,7 @@ const AgentManagerContent: Component = () => { | |||||
| session.clearCurrentSession() | ||||||
| } | ||||||
| } | ||||||
| forgetSessionFocus(sessionId) | ||||||
| if (pending || localSet().has(sessionId)) { | ||||||
| setLocalSessionIDs((prev) => prev.filter((id) => id !== sessionId)) | ||||||
| } | ||||||
|
|
@@ -2500,6 +2573,8 @@ const AgentManagerContent: Component = () => { | |||||
| continueInWorktree={selection() === LOCAL} | ||||||
| promptBoxId={`agent-manager:${selection() ?? "unassigned"}`} | ||||||
| pendingSessionID={selection() === LOCAL ? activePendingId() : undefined} | ||||||
| focusOnDraftChange={focusOnDraftChange} | ||||||
| onFocusChange={rememberPromptFocus} | ||||||
| /> | ||||||
| <Show when={readOnly()}> | ||||||
| <div class="am-readonly-banner"> | ||||||
|
|
||||||
Uh oh!
There was an error while loading. Please reload this page.