The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(tui): the picker's delete lifecycle lives in a useSessionDelete hook
yoniebans98b951bf9ff1
diff --git a/ui-tui/src/components/activeSessionSwitcher.tsx b/ui-tui/src/components/activeSessionSwitcher.tsxindex 8ecbc5d5099..ef24326660e 100644--- a/ui-tui/src/components/activeSessionSwitcher.tsx+++ b/ui-tui/src/components/activeSessionSwitcher.tsx@@ -2,15 +2,10 @@ import { Box, Text, useInput, useStdout } from '@hermes/ink'import type { SessionListResult, SessionListRow } from '@hermes/shared/gateway-events'import { useCallback, useEffect, useRef, useState } from 'react'-import { mutateCanonical } from '../app/slash/canonicalSessionControls.js'import { sessionScopedModelArg } from '../domain/slash.js'import type { GatewayClient } from '../gatewayClient.js'-import type {- SessionActiveItem,- SessionActiveListResponse,- SessionCloseResponse,- SessionDeleteResponse-} from '../gatewayTypes.js'+import type { SessionActiveItem, SessionActiveListResponse, SessionCloseResponse } from '../gatewayTypes.js'+import { useSessionDelete } from '../hooks/useSessionDelete.js'import { messages } from '../i18n/runtime.js'import { useT } from '../i18n/useT.js'import { asRpcResult, rpcErrorMessage } from '../lib/rpc.js'@@ -331,11 +326,6 @@ export function ActiveSessionSwitcher({// the 1.5s live-status poll re-indexing rows can't redirect the delete to a// different session. Any other key cancels the prompt.const [confirmDelete, setConfirmDelete] = useState<null | string>(null)- const [deleting, setDeleting] = useState(false)- // Set synchronously (one stdin chunk can carry the confirming key and a click before a re-render);- // the state guard covers a settle until the updated list renders.- const deletingRef = useRef(false)- const deletePending = () => deleting || deletingRef.currentconst T = useT()const S = T.pickers.sessionconst C = T.pickers.common@@ -551,74 +541,23 @@ export function ActiveSessionSwitcher({}}, [closingId, currentSessionId, gw.isCanonical, history.length, items, load, onClose, onNew, onSelect, rowKind, sel])- const performDelete = useCallback(+ const removeDeleted = useCallback((id: string) => {- const target = history.find(h => h.id === id)-- if (!target || deletingRef.current) {- return- }-- deletingRef.current = true- setDeleting(true)- setActionErr('')- // The shared gateway has no `session.delete`: its delete is the revision-fenced- // `session.mutate operation=delete` (the whole lineage), as the classic CLI sends it.- let readSubscription: string | undefined-- const deleted = gw.isCanonical- ? mutateCanonical(gw, target.id, 'delete', {}, undefined, snapshot => {- readSubscription = snapshot.subscription_id- }).then(mutation => Boolean(mutation?.result))- : gw- .request<SessionDeleteResponse>('session.delete', { session_id: target.id })- .then(raw => asRpcResult<SessionDeleteResponse>(raw)?.deleted === target.id)-- deleted- .then(ok => {- if (!ok) {- setActionErr(- messages().pickers.common.invalidResponse(gw.isCanonical ? 'session.mutate' : 'session.delete')- )- deletingRef.current = false- setDeleting(false)-- return- }-- rawHistoryRef.current = rawHistoryRef.current.filter(h => h.id !== target.id)- setHistory(prev => prev.filter(h => h.id !== target.id))- setSel(s => Math.max(0, Math.min(s, items.length + history.length - 1)))- deletingRef.current = false- setDeleting(false)- })- .catch((e: unknown) => {- // The revision read attached this view to a session it was not showing; a refused- // delete hands it back closed so it does not reappear in the live section.- if (readSubscription) {- void gw- .request('session.detach', {- session_id: target.id,- subscription_id: readSubscription,- end_reason: 'tui_close'- })- .catch(() => undefined)- }-- setActionErr(rpcErrorMessage(e))- deletingRef.current = false- setDeleting(false)- })+ rawHistoryRef.current = rawHistoryRef.current.filter(h => h.id !== id)+ setHistory(prev => prev.filter(h => h.id !== id))+ setSel(s => Math.max(0, Math.min(s, items.length + history.length - 1)))},- [gw, history, items.length]+ [history.length, items.length])+ const { deletePending, deleting, performDelete } = useSessionDelete(gw, removeDeleted, setActionErr)+const handleRowClick = useCallback((index: number) => (event: { stopImmediatePropagation?: () => void }) => {event.stopImmediatePropagation?.()// Keys are held while a delete is pending; a click must not open the row mid-delete either.- if (deleting || deletingRef.current) {+ if (deletePending()) {return}@@ -641,7 +580,7 @@ export function ActiveSessionSwitcher({setSel(0)},- [deleting, history, items, onResume, onSelect, rowKind, total]+ [deletePending, history, items, onResume, onSelect, rowKind, total])const selectedKind = rowKind(sel)@@ -657,9 +596,12 @@ export function ActiveSessionSwitcher({// other key cancels the prompt (mirrors the standalone resume picker).if (confirmDelete !== null) {if (ch?.toLowerCase() === 'd') {- const id = confirmDelete+ const target = history.find(h => h.id === confirmDelete)setConfirmDelete(null)- performDelete(id)++ if (target) {+ performDelete(target)+ }} else {setConfirmDelete(null)}diff --git a/ui-tui/src/hooks/useSessionDelete.ts b/ui-tui/src/hooks/useSessionDelete.tsnew file mode 100644index 00000000000..94fff15b27d--- /dev/null+++ b/ui-tui/src/hooks/useSessionDelete.ts@@ -0,0 +1,81 @@+import type { SessionListRow } from '@hermes/shared/gateway-events'+import { useCallback, useRef, useState } from 'react'++import { mutateCanonical } from '../app/slash/canonicalSessionControls.js'+import type { GatewayClient } from '../gatewayClient.js'+import type { SessionDeleteResponse } from '../gatewayTypes.js'+import { messages } from '../i18n/runtime.js'+import { asRpcResult, rpcErrorMessage } from '../lib/rpc.js'++/** The picker's history delete: one request at a time, and a refused delete hands back the viewer+ * attachment its revision read made. Settles through `onDeleted` or `onError`. */+export function useSessionDelete(+ gw: GatewayClient,+ onDeleted: (id: string) => void,+ onError: (message: string) => void+) {+ const [deleting, setDeleting] = useState(false)+ // Set synchronously (one stdin chunk can carry the confirming key and a click before a re-render);+ // the state guard covers a settle until the updated list renders.+ const deletingRef = useRef(false)++ const settle = () => {+ deletingRef.current = false+ setDeleting(false)+ }++ const performDelete = useCallback(+ (target: SessionListRow) => {+ if (deletingRef.current) {+ return+ }++ deletingRef.current = true+ setDeleting(true)+ onError('')+ // The shared gateway has no `session.delete`: its delete is the revision-fenced+ // `session.mutate operation=delete` (the whole lineage), as the classic CLI sends it.+ let readSubscription: string | undefined++ const deleted = gw.isCanonical+ ? mutateCanonical(gw, target.id, 'delete', {}, undefined, snapshot => {+ readSubscription = snapshot.subscription_id+ }).then(mutation => Boolean(mutation?.result))+ : gw+ .request<SessionDeleteResponse>('session.delete', { session_id: target.id })+ .then(raw => asRpcResult<SessionDeleteResponse>(raw)?.deleted === target.id)++ deleted+ .then(ok => {+ if (ok) {+ onDeleted(target.id)+ } else {+ onError(messages().pickers.common.invalidResponse(gw.isCanonical ? 'session.mutate' : 'session.delete'))+ }++ settle()+ })+ .catch((e: unknown) => {+ // The revision read attached this view to a session it was not showing; a refused+ // delete hands it back closed so it does not reappear in the live section.+ if (readSubscription) {+ void gw+ .request('session.detach', {+ session_id: target.id,+ subscription_id: readSubscription,+ end_reason: 'tui_close'+ })+ .catch(() => undefined)+ }++ onError(rpcErrorMessage(e))+ settle()+ })+ },+ [gw, onDeleted, onError]+ )++ const pending = useCallback(() => deleting || deletingRef.current, [deleting])++ return { deletePending: pending, deleting, performDelete }+}