Skip to content

Commit ce8e6cb

Browse files
committed
Improve chat recovery
1 parent 9ecd345 commit ce8e6cb

File tree

5 files changed

+40
-9
lines changed

5 files changed

+40
-9
lines changed

src/bridge/transport.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,17 @@ export class WebBridge {
116116
*/
117117
private subagentChatIds = new Set<string>();
118118

119-
constructor(host: string, password: string, protocol?: Protocol) {
119+
/**
120+
* The preferred chat ID to restore on connect. When set (e.g. from
121+
* persisted storage), `restoreChats()` will select this chat instead
122+
* of the most recent one — giving the user continuity across sessions.
123+
*/
124+
private preferredChatId: string | null = null;
125+
126+
constructor(host: string, password: string, protocol?: Protocol, lastChatId?: string) {
120127
this.host = host;
121128
this.api = new EcaRemoteApi(host, password, protocol);
129+
this.preferredChatId = lastChatId ?? null;
122130
}
123131

124132
// ---------------------------------------------------------------------------
@@ -755,11 +763,15 @@ export class WebBridge {
755763
});
756764
}
757765

758-
// Auto-select and load the most recent chat
759-
const lastChat = summaries[summaries.length - 1];
760-
if (lastChat?.id) {
761-
this.currentChatId = lastChat.id;
762-
await this.loadChatMessages(lastChat.id);
766+
// Auto-select the preferred chat (from previous session) or fall back
767+
// to the most recent chat in the list.
768+
const preferred = this.preferredChatId
769+
? summaries.find((s) => s.id === this.preferredChatId)
770+
: null;
771+
const chatToSelect = preferred ?? summaries[summaries.length - 1];
772+
if (chatToSelect?.id) {
773+
this.currentChatId = chatToSelect.id;
774+
await this.loadChatMessages(chatToSelect.id);
763775
}
764776

765777
this.notifyChatListChange();

src/pages/ConnectionBar.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ export interface ConnectionEntry {
2222
error?: string;
2323
/** Workspace folders reported by the server once the session is connected. */
2424
workspaceFolders?: (WorkspaceFolder | string)[];
25+
/** ID of the last viewed chat — restored when reconnecting to this server. */
26+
lastChatId?: string;
2527
}
2628

2729
interface ConnectionBarProps {

src/pages/RemoteProduct.tsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export function RemoteProduct() {
5252
// --- Persistence ---
5353

5454
useEffect(() => {
55-
saveConnections(entries.map(({ id, host, password, protocol, workspaceFolders }) => ({ id, host, password, protocol, workspaceFolders })));
55+
saveConnections(entries.map(({ id, host, password, protocol, workspaceFolders, lastChatId }) => ({ id, host, password, protocol, workspaceFolders, lastChatId })));
5656
}, [entries]);
5757

5858
useEffect(() => {
@@ -270,6 +270,16 @@ export function RemoteProduct() {
270270
bridge.onChatListChanged((entries, selected) => {
271271
setChatEntries(entries);
272272
setSelectedChatId(selected);
273+
// Persist the last viewed chat ID so it can be restored on reconnect.
274+
if (connId && selected) {
275+
setEntries((prev) => {
276+
const entry = prev.find((e) => e.id === connId);
277+
if (entry?.lastChatId === selected) return prev; // no change — keep same reference
278+
return prev.map((e) =>
279+
e.id === connId ? { ...e, lastChatId: selected } : e,
280+
);
281+
});
282+
}
273283
// Workspace folders become available after session:connected,
274284
// which fires before chats are restored — so pick them up here.
275285
syncFolders();
@@ -334,6 +344,7 @@ export function RemoteProduct() {
334344
host={activeEntry.host}
335345
password={activeEntry.password}
336346
protocol={activeEntry.protocol}
347+
lastChatId={activeEntry.lastChatId}
337348
onStatusChange={(status, error) =>
338349
handleStatusChange(activeEntry.id, status, error)
339350
}

src/pages/RemoteSession.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,14 @@ interface RemoteSessionProps {
5555
host: string;
5656
password: string;
5757
protocol?: Protocol;
58+
/** ID of the last viewed chat — used to restore the previous chat on reconnect. */
59+
lastChatId?: string;
5860
onStatusChange: (status: SessionStatus, error?: string) => void;
5961
/** Called when the bridge instance changes (connected or disconnected). */
6062
onBridgeChange?: (bridge: WebBridge | null) => void;
6163
}
6264

63-
export function RemoteSession({ host, password, protocol, onStatusChange, onBridgeChange }: RemoteSessionProps) {
65+
export function RemoteSession({ host, password, protocol, lastChatId, onStatusChange, onBridgeChange }: RemoteSessionProps) {
6466
const [state, setState] = useState<
6567
| { status: 'connecting' }
6668
| { status: 'connected' }
@@ -72,6 +74,8 @@ export function RemoteSession({ host, password, protocol, onStatusChange, onBrid
7274

7375
const bridgeRef = useRef<WebBridge | null>(null);
7476
const mountedRef = useRef(true);
77+
const lastChatIdRef = useRef(lastChatId);
78+
lastChatIdRef.current = lastChatId;
7579
const onStatusChangeRef = useRef(onStatusChange);
7680
const onBridgeChangeRef = useRef(onBridgeChange);
7781
onStatusChangeRef.current = onStatusChange;
@@ -85,7 +89,7 @@ export function RemoteSession({ host, password, protocol, onStatusChange, onBrid
8589
// Disconnect any existing bridge
8690
bridgeRef.current?.disconnect();
8791

88-
const bridge = new WebBridge(host, password, protocol);
92+
const bridge = new WebBridge(host, password, protocol, lastChatIdRef.current);
8993
bridgeRef.current = bridge;
9094

9195
// Subscribe to reconnection events

src/storage/connections.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ export interface Connection {
1919
protocol?: Protocol;
2020
/** Cached workspace folders from the last successful session. */
2121
workspaceFolders?: (WorkspaceFolder | string)[];
22+
/** ID of the last viewed chat — restored when reconnecting to this server. */
23+
lastChatId?: string;
2224
}
2325

2426
// ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)