Skip to content

Commit b7eb664

Browse files
authored
Merge pull request #7838 from Agenta-AI/fix/first-send-switch-away
fix(frontend): keep a first message when the user opens another chat before it is sent
2 parents 30dff47 + 0c4b755 commit b7eb664

6 files changed

Lines changed: 400 additions & 6 deletions

File tree

‎web/mobile/src/features/chat/ChatScreen.tsx‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {ScreenScaffold} from "@/components/ScreenScaffold"
1515
import {StatusTag} from "@/components/StatusTag"
1616

1717
import {useBindProjectContext} from "../context/useBindProjectContext"
18+
import {pendingTasksAtom} from "../home/pendingTask"
1819
import {AppShell} from "../nav/AppShell"
1920
import {useLivenessPoll} from "../sessions/useLivenessPoll"
2021

@@ -34,6 +35,7 @@ import {useAgentEntity} from "./useAgentEntity"
3435
import {useAgentVisibilityRecheck} from "./useAgentVisibilityRecheck"
3536
import {useApprovalActions} from "./useApprovalActions"
3637
import {useChatAccess} from "./useChatAccess"
38+
import {useLeaveHeldTask} from "./useLeaveHeldTask"
3739
import {useReferenceToolDisplays} from "./useReferenceToolDisplays"
3840
import {useSessionTranscript} from "./useSessionTranscript"
3941
import {useSessionWatch} from "./useSessionWatch"
@@ -74,6 +76,11 @@ export const ChatScreen = ({
7476
if (isSessionFresh(sessionId)) return
7577
void prefetchRecords(sessionId).catch(() => undefined)
7678
}, [boundProjectId, prefetchRecords, projectId, sessionId])
79+
useLeaveHeldTask(sessionId, projectId)
80+
// A session the server does not list yet names its agent only in the route, and the rail's
81+
// link does not carry it. A first message waiting here names it too.
82+
const taskAgentId = useAtomValue(pendingTasksAtom)[sessionId]?.agentId
83+
const routeAgentId = agentId ?? taskAgentId
7784
const {
7885
entityId: latestEntityId,
7986
agentId: resolvedAgentId,
@@ -83,11 +90,11 @@ export const ChatScreen = ({
8390
agentHidden: entityAgentHidden,
8491
retry,
8592
retrying,
86-
} = useAgentEntity(sessionId, projectId, agentId)
93+
} = useAgentEntity(sessionId, projectId, routeAgentId)
8794
const access = useChatAccess({
8895
sessionId,
8996
projectId,
90-
routeAgentId: agentId,
97+
routeAgentId,
9198
agentHidden: entityAgentHidden,
9299
})
93100
// An agent made private while this screen is open: the session row read says so (one

‎web/mobile/src/features/chat/LiveConversation.tsx‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1018,14 +1018,17 @@ export const LiveConversation = ({
10181018
filenames={pendingTask?.parts?.map(
10191019
(part) => part.filename || "Attachment",
10201020
)}
1021+
// A template task left while its connect step held it comes
1022+
// back failed; the step still goes first.
10211023
retryDisabled={
10221024
locked ||
1025+
setup.blocking ||
10231026
isHydrating ||
10241027
modelBlocked ||
10251028
(modelKeyLoading && modelKeyWaitedMs < MODEL_KEY_WAIT_LIMIT_MS)
10261029
}
10271030
onRetry={() => {
1028-
if (locked) return
1031+
if (locked || setup.blocking) return
10291032
void sendPendingTask({
10301033
sessionId,
10311034
retry: true,
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import {useEffect} from "react"
2+
3+
import {useStore} from "jotai"
4+
5+
import {leaveHeldTaskAtom} from "../home/pendingTask"
6+
7+
/** Chat screens open per session. A count, so a screen that remounts in place has not left. */
8+
const openScreens = new Map<string, number>()
9+
10+
/**
11+
* Keep a held first message when the user leaves its session before the chat screen sent it.
12+
*
13+
* Home and an agent's page hand the first message to the chat screen, which holds it until the
14+
* transcript, the vault and the setup step answer. The chat route stays mounted across a session
15+
* switch, so the session id changing is a leave too, not only an unmount. Without this the task
16+
* sat unsent with no engine left to send it, and the session had no rail row and no route agent,
17+
* so coming back showed a chat that does not exist.
18+
*
19+
* The check runs a task later, so a screen that unmounts and mounts again in one pass (React's
20+
* strict mode, a parent remount) is still open when it runs.
21+
*/
22+
export const useLeaveHeldTask = (sessionId: string, projectId: string) => {
23+
const store = useStore()
24+
useEffect(() => {
25+
openScreens.set(sessionId, (openScreens.get(sessionId) ?? 0) + 1)
26+
return () => {
27+
const left = (openScreens.get(sessionId) ?? 1) - 1
28+
if (left > 0) openScreens.set(sessionId, left)
29+
else openScreens.delete(sessionId)
30+
setTimeout(() => {
31+
if (openScreens.has(sessionId)) return
32+
store.set(leaveHeldTaskAtom, {sessionId, projectId})
33+
}, 0)
34+
}
35+
}, [projectId, sessionId, store])
36+
}

‎web/mobile/src/features/home/pendingTask.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {refusedSendReason} from "@agenta/chat/model"
22
import {markSessionFresh} from "@agenta/chat/state"
3+
import {registerLocalSessionAtom} from "@agenta/entities/session"
34
import type {FileUIPart} from "ai"
45
import {atom} from "jotai"
56

@@ -73,6 +74,31 @@ export const keepFailedSendAtom = atom(
7374
},
7475
)
7576

77+
/**
78+
* The chat screen of a session closed while its task was not on its way. A held task (the
79+
* transcript, the vault or the setup step had not answered) has nothing left to send it, so it
80+
* waits as failed, which the chat screen offers again with a retry. A held or failed task keeps
81+
* the session listed in the rail, the user's only way back to a session the server does not know.
82+
* A task already sending is left to its own send.
83+
*/
84+
export const leaveHeldTaskAtom = atom(
85+
null,
86+
(get, set, {sessionId, projectId}: {sessionId: string; projectId: string}) => {
87+
const tasks = get(pendingTasksAtom)
88+
const task = tasks[sessionId]
89+
if (!task || task.delivery === "sending") return
90+
if (!task.delivery) {
91+
set(pendingTasksAtom, {...tasks, [sessionId]: {...task, delivery: "failed"}})
92+
}
93+
set(registerLocalSessionAtom, {
94+
sessionId,
95+
projectId,
96+
agentId: task.agentId,
97+
name: task.text,
98+
})
99+
},
100+
)
101+
76102
export const sendPendingTaskAtom = atom(
77103
null,
78104
async (

0 commit comments

Comments
 (0)