Skip to content

The agent that grows with you

fix(desktop): a card answer that fails to send gives the typed words back to the composer

alt-glitch6a5bc5759854
diff --git a/apps/desktop/src/app/chat/composer/hooks/use-composer-submit.ts b/apps/desktop/src/app/chat/composer/hooks/use-composer-submit.ts
index 8291ddc5201..203f34903cb 100644
--- a/apps/desktop/src/app/chat/composer/hooks/use-composer-submit.ts
+++ b/apps/desktop/src/app/chat/composer/hooks/use-composer-submit.ts
@@ -291,11 +291,22 @@ export function useComposerSubmit({
const isSideQuestion = !attachments.length && isSideTaskSlashCommand(text)
const routable = payloadPresent && !queueEdit && !isSideQuestion
+ // Like dispatchSubmit's restore: an answer that never reached the backend gives the words back.
+ const answeredScope = draftScopeRef.current
+
+ const restore = () => {
+ stashAt(answeredScope, text, [])
+
+ if (draftScopeRef.current === answeredScope) {
+ loadIntoComposer(text, [])
+ }
+ }
+
if (
routable &&
!attachments.length &&
!SLASH_COMMAND_RE.test(text.trim()) &&
- answerParkedCard(sessionId ?? '', text.trim())
+ answerParkedCard(sessionId ?? '', text.trim(), restore)
) {
triggerHaptic('submit')
resetBrowseState(sessionId)
diff --git a/apps/desktop/src/store/clarify.ts b/apps/desktop/src/store/clarify.ts
index c0154830bfb..c3b10e4348e 100644
--- a/apps/desktop/src/store/clarify.ts
+++ b/apps/desktop/src/store/clarify.ts
@@ -261,11 +261,11 @@ export async function skipClarifyRequest(sessionId: string | null | undefined):
}
/** Answer the clarify parked on `sessionId` with composer text: the answer to its first question not yet locked. */
-function answerClarifyText(sessionId: string, text: string): boolean {
+function answerClarifyText(sessionId: string, text: string): null | Promise<boolean> {
const request = $clarifyRequests.get()[keyFor(sessionId)]
if (!request) {
- return false
+ return null
}
return lockTypedAnswer(request, text)
@@ -287,12 +287,12 @@ function typedAnswer(question: ClarifyQuestion, text: string): string {
*/
const lockChains = new Map<string, Promise<boolean>>()
-function lockTypedAnswer(request: ClarifyRequest, text: string): boolean {
+function lockTypedAnswer(request: ClarifyRequest, text: string): null | Promise<boolean> {
const gateway = $gateway.get()
const question = request.questions.find(entry => request.lockedAnswers?.[entry.qid] === undefined)
if (!gateway || !question) {
- return false
+ return null
}
const answer = typedAnswer(question, text)
@@ -356,7 +356,7 @@ function lockTypedAnswer(request: ClarifyRequest, text: string): boolean {
}
})
- return true
+ return sent
}
/** Undo one failed lock without touching answers locked after it. */
diff --git a/apps/desktop/src/store/connection-request.ts b/apps/desktop/src/store/connection-request.ts
index 98a489c986d..025ce21fbf3 100644
--- a/apps/desktop/src/store/connection-request.ts
+++ b/apps/desktop/src/store/connection-request.ts
@@ -515,16 +515,14 @@ export const continueConnectionRequest = (request: ConnectionRequest): Promise<b
respondToConnectionRequest(request, { settled_by: 'continue' })
/** The composer's words answer an open connector directory card; the tool returns them as `said`. */
-export function answerConnectorDirectory(sessionId: string, text: string): boolean {
+export function answerConnectorDirectory(sessionId: string, text: string): null | Promise<boolean> {
const request = $connectionRequests.get()[keyFor(sessionId)]
if (!request?.display || request.settled || !text.trim()) {
- return false
+ return null
}
- void respondToConnectionRequest(request, { said: text.trim(), settled_by: 'continue' }).catch(() => undefined)
-
- return true
+ return respondToConnectionRequest(request, { said: text.trim(), settled_by: 'continue' })
}
registerParkedCardAnswer('connector-directory', {
diff --git a/apps/desktop/src/store/parked-card-answers.test.ts b/apps/desktop/src/store/parked-card-answers.test.ts
index 84502c1cc9e..e46d0d8fb47 100644
--- a/apps/desktop/src/store/parked-card-answers.test.ts
+++ b/apps/desktop/src/store/parked-card-answers.test.ts
@@ -13,9 +13,9 @@ describe('parked card answers', () => {
})
it('stops at the first card that takes the text, and forgets a card once it unregisters', () => {
- const declines = vi.fn(() => false)
- const takes = vi.fn((sessionId: string) => sessionId === 'session-a')
- const later = vi.fn(() => true)
+ const declines = vi.fn<ParkedCardAnswer>(() => null)
+ const takes = vi.fn<ParkedCardAnswer>(sessionId => (sessionId === 'session-a' ? Promise.resolve(true) : null))
+ const later = vi.fn<ParkedCardAnswer>(() => Promise.resolve(true))
unregister.push(registerParkedCardAnswer('test-declines', kind(declines)))
const offTakes = registerParkedCardAnswer('test-takes', kind(takes))
@@ -26,9 +26,17 @@ describe('parked card answers', () => {
expect(later).not.toHaveBeenCalled()
offTakes()
- later.mockReturnValue(false)
+ later.mockReturnValue(null)
expect(answerParkedCard('session-a', 'hello')).toBe(false)
expect(takes).toHaveBeenCalledTimes(1)
})
+
+ it('reports an answer that failed to send, so the composer can restore the draft', async () => {
+ const onFailed = vi.fn()
+ unregister.push(registerParkedCardAnswer('test-fails', kind(() => Promise.resolve(false))))
+
+ expect(answerParkedCard('session-a', 'hello', onFailed)).toBe(true)
+ await vi.waitFor(() => expect(onFailed).toHaveBeenCalledTimes(1))
+ })
})
diff --git a/apps/desktop/src/store/parked-card-answers.ts b/apps/desktop/src/store/parked-card-answers.ts
index cbd9e0b4970..96b509501cd 100644
--- a/apps/desktop/src/store/parked-card-answers.ts
+++ b/apps/desktop/src/store/parked-card-answers.ts
@@ -1,7 +1,8 @@
import { atom, computed, type ReadableAtom } from 'nanostores'
-/** Answers a card parked on `sessionId` with composer text; true when the text answered it. */
-export type ParkedCardAnswer = (sessionId: string, text: string) => boolean
+/** Answers a card parked on `sessionId` with composer text: null when no card of this kind takes it,
+ * else a promise of whether the answer reached the backend. */
+export type ParkedCardAnswer = (sessionId: string, text: string) => null | Promise<boolean>
/** One card kind: its typed-answer handler, whether a card of it waits on a session, and the store that
* predicate reads (watched so the composer redraws when a card parks or settles). */
@@ -35,11 +36,22 @@ export function registerParkedCardAnswer(id: string, kind: ParkedCardKind): () =
/**
* Offer composer text to every card waiting on `sessionId`, in registration order. True when one
- * took it as its answer: the composer then neither sends nor steers the text.
+ * took it as its answer: the composer then neither sends nor steers the text, and `onFailed` runs
+ * if that answer never reaches the backend, so the composer can give the words back.
*/
-export function answerParkedCard(sessionId: string, text: string): boolean {
+export function answerParkedCard(sessionId: string, text: string, onFailed: () => void = () => {}): boolean {
for (const kind of kinds.values()) {
- if (kind.answer(sessionId, text)) {
+ const sent = kind.answer(sessionId, text)
+
+ if (sent) {
+ void sent
+ .then(delivered => {
+ if (!delivered) {
+ onFailed()
+ }
+ })
+ .catch(onFailed)
+
return true
}
}
diff --git a/apps/desktop/src/store/setup-beat.ts b/apps/desktop/src/store/setup-beat.ts
index 44e48788654..8da81db4d58 100644
--- a/apps/desktop/src/store/setup-beat.ts
+++ b/apps/desktop/src/store/setup-beat.ts
@@ -104,8 +104,10 @@ export function answerSetupBeat(sessionId: string, answer: SetupBeatAnswer): boo
}
/** Composer text typed while a card waits answers it as the user's words. */
-export function sayToSetupBeat(sessionId: string, text: string): boolean {
- return Boolean(text.trim()) && respond(sessionId, { said: text.trim() })
+export function sayToSetupBeat(sessionId: string, text: string): null | Promise<boolean> {
+ return text.trim() && $setupBeatRequests.get()[sessionId]
+ ? Promise.resolve(respond(sessionId, { said: text.trim() }))
+ : null
}
registerParkedCardAnswer('setup_beat', {
diff --git a/apps/desktop/src/store/theme-pick.ts b/apps/desktop/src/store/theme-pick.ts
index a9b55b3442a..2570fb9391f 100644
--- a/apps/desktop/src/store/theme-pick.ts
+++ b/apps/desktop/src/store/theme-pick.ts
@@ -63,8 +63,8 @@ export function cancelThemePick(sessionId: string): boolean {
}
/** Composer text typed while a picker waits answers it as the user's words; the model applies them. */
-export function sayToThemePick(sessionId: string, text: string): boolean {
- return Boolean(text.trim()) && answer(sessionId, { said: text.trim() })
+export function sayToThemePick(sessionId: string, text: string): null | Promise<boolean> {
+ return text.trim() && $themePicks.get()[sessionId] ? Promise.resolve(answer(sessionId, { said: text.trim() })) : null
}
registerParkedCardAnswer('theme-pick', {