Skip to content

Commit 390dacf

Browse files
refactor(mobile): thread composer, route and queue control read command results directly
The inspector toggle reads its inputs at call time instead of mirroring them into a ref from a layout effect. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent a3d40e0 commit 390dacf

4 files changed

Lines changed: 138 additions & 147 deletions

File tree

‎apps/mobile/src/features/threads/ThreadComposer.tsx‎

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import * as Dialog from "../../components/ui/Dialog";
22
import type { ComposerTextPaste } from "../../native/T3ComposerEditor.types";
33
import { useAppearancePreferences } from "../settings/appearance/AppearancePreferencesProvider";
44
import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell";
5+
import { settlePromise, squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
6+
import { AsyncResult } from "effect/reactivity";
57
import { useAtomValue } from "@effect/atom-react";
68
import { clampFileAttachmentUploadBytes } from "@t3tools/client-runtime/state/attachments";
79
import { pastedTextDisposition, replaceTextSelection } from "@t3tools/client-runtime/text-paste";
@@ -547,20 +549,20 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerProps
547549
const threadKey = scopedThreadKey(props.environmentId, props.selectedThread.id);
548550
if (inFlightThreadIdsRef.current.has(threadKey)) return;
549551
inFlightThreadIdsRef.current.add(threadKey);
550-
return await (async () => {
551-
const messageId = await onSendMessage(followUp);
552-
if (messageId === null) {
553-
return;
554-
}
555-
// Armed after the send so its preference read and native Activity start don't contend
556-
// with the queued-message feedback on the tap frame.
557-
armAgentAwarenessLiveActivityForLocalWork({
558-
environmentId: props.environmentId,
559-
threadTitle: props.selectedThread.title,
560-
projectTitle: props.environmentLabel ?? "T3 Code",
561-
});
562-
})().finally(() => {
563-
inFlightThreadIdsRef.current.delete(threadKey);
552+
const sent = await settlePromise(() => onSendMessage(followUp));
553+
inFlightThreadIdsRef.current.delete(threadKey);
554+
if (AsyncResult.isFailure(sent)) {
555+
// The composer state already surfaces send errors; only the lock needed releasing.
556+
console.error("[composer] send failed", squashAtomCommandFailure(sent));
557+
return;
558+
}
559+
if (sent.value === null) return;
560+
// Armed after the send so its preference read and native Activity start don't contend
561+
// with the queued-message feedback on the tap frame.
562+
armAgentAwarenessLiveActivityForLocalWork({
563+
environmentId: props.environmentId,
564+
threadTitle: props.selectedThread.title,
565+
projectTitle: props.environmentLabel ?? "T3 Code",
564566
});
565567
};
566568

‎apps/mobile/src/features/threads/ThreadQueueControl.tsx‎

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,9 @@ export function ThreadQueueSheet({ route }: StaticScreenProps<QueueTarget>) {
113113
busyRef.current = true;
114114
setBusyRunId(runId);
115115
void Haptics.selectionAsync();
116-
await (async () => {
117-
await reorder({ ...target, input: { threadId: target.threadId, runId, beforeRunId } });
118-
})().finally(() => {
119-
busyRef.current = false;
120-
setBusyRunId(null);
121-
});
116+
await reorder({ ...target, input: { threadId: target.threadId, runId, beforeRunId } });
117+
busyRef.current = false;
118+
setBusyRunId(null);
122119
};
123120

124121
const act = async (runId: RunId, action: QueueAction) => {
@@ -150,23 +147,20 @@ export function ThreadQueueSheet({ route }: StaticScreenProps<QueueTarget>) {
150147
busyRef.current = true;
151148
setBusyRunId(runId);
152149
void Haptics.selectionAsync();
153-
await (async () => {
154-
if (action === "remove") {
155-
await cancel(buildCancelQueuedRunCommand({ ...target, runId }));
156-
} else if (workflow?.activeRun && workflow.canPromoteToSteer) {
157-
await promote({
158-
...target,
159-
input: {
160-
threadId: target.threadId,
161-
queuedRunId: runId,
162-
targetRunId: workflow.activeRun.id,
163-
},
164-
});
165-
}
166-
})().finally(() => {
167-
busyRef.current = false;
168-
setBusyRunId(null);
169-
});
150+
if (action === "remove") {
151+
await cancel(buildCancelQueuedRunCommand({ ...target, runId }));
152+
} else if (workflow?.activeRun && workflow.canPromoteToSteer) {
153+
await promote({
154+
...target,
155+
input: {
156+
threadId: target.threadId,
157+
queuedRunId: runId,
158+
targetRunId: workflow.activeRun.id,
159+
},
160+
});
161+
}
162+
busyRef.current = false;
163+
setBusyRunId(null);
170164
};
171165

172166
const canReorder = workflow?.canReorder === true && queuedRuns.length > 1;
@@ -203,12 +197,9 @@ export function ThreadQueueSheet({ route }: StaticScreenProps<QueueTarget>) {
203197
if (busyRef.current) return;
204198
busyRef.current = true;
205199
setResuming(true);
206-
await (async () => {
207-
await resume({ ...target, input: { threadId: target.threadId } });
208-
})().finally(() => {
209-
busyRef.current = false;
210-
setResuming(false);
211-
});
200+
await resume({ ...target, input: { threadId: target.threadId } });
201+
busyRef.current = false;
202+
setResuming(false);
212203
}}
213204
/>
214205
</View>

‎apps/mobile/src/features/threads/ThreadRouteScreen.tsx‎

Lines changed: 54 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
useNavigation,
1212
type StaticScreenProps,
1313
} from "@react-navigation/native";
14-
import { useEffect, useLayoutEffect, useRef, useState, type ReactNode } from "react";
14+
import { useEffect, useRef, useState, type ReactNode } from "react";
1515
import * as Option from "effect/Option";
1616
import {
1717
AuthOrchestrationOperateScope,
@@ -331,23 +331,20 @@ function ThreadRouteContent(
331331
return;
332332
}
333333
mergeBackBusyRef.current = true;
334-
return await (async () => {
335-
const result = await mergeBack({
336-
environmentId: selectedThread.environmentId,
337-
input: {
338-
sourceThreadId: selectedThread.id,
339-
targetThreadId: mergeBackTargetThreadId,
340-
runId: mergeBackRun.id,
341-
creationSource: "mobile",
342-
},
343-
});
344-
if (result._tag !== "Success") return;
345-
navigation.navigate("Thread", {
346-
environmentId: selectedThread.environmentId,
347-
threadId: mergeBackTargetThreadId,
348-
});
349-
})().finally(() => {
350-
mergeBackBusyRef.current = false;
334+
const result = await mergeBack({
335+
environmentId: selectedThread.environmentId,
336+
input: {
337+
sourceThreadId: selectedThread.id,
338+
targetThreadId: mergeBackTargetThreadId,
339+
runId: mergeBackRun.id,
340+
creationSource: "mobile",
341+
},
342+
});
343+
mergeBackBusyRef.current = false;
344+
if (result._tag !== "Success") return;
345+
navigation.navigate("Thread", {
346+
environmentId: selectedThread.environmentId,
347+
threadId: mergeBackTargetThreadId,
351348
});
352349
};
353350
const params = props.route.params;
@@ -490,25 +487,12 @@ function ThreadRouteContent(
490487
});
491488
showAuxiliaryPane("inspector");
492489
};
493-
const inspectorToggleActionRef = useRef({
494-
inspectorMode,
495-
openFilesInspector: handleOpenFilesInspector,
496-
toggleAuxiliaryPane,
497-
});
498-
useLayoutEffect(() => {
499-
inspectorToggleActionRef.current = {
500-
inspectorMode,
501-
openFilesInspector: handleOpenFilesInspector,
502-
toggleAuxiliaryPane,
503-
};
504-
});
505490
const handleToggleInspector = () => {
506-
const action = inspectorToggleActionRef.current;
507-
if (action.inspectorMode === null) {
508-
action.openFilesInspector();
491+
if (inspectorMode === null) {
492+
handleOpenFilesInspector();
509493
return;
510494
}
511-
action.toggleAuxiliaryPane();
495+
toggleAuxiliaryPane();
512496
};
513497
const handleSelectInspectorFile = (path: string) => {
514498
if (selectedThread === null) {
@@ -804,44 +788,44 @@ function ThreadRouteContent(
804788
if (!selectedThread || !selectedThreadProject || !setupMessage || localResendBusy.current)
805789
return;
806790
localResendBusy.current = true;
807-
return await (async () => {
808-
const result = await cancelWorktreeSetup({
809-
environmentId: selectedThread.environmentId,
810-
input: { threadId: selectedThread.id },
811-
});
812-
if (result._tag !== "Success" || !result.value.cancelled) return;
813-
// V2 accepts the launch before setup runs, so cancellation never rejects
814-
// the original outbox delivery. Reuse the server-owned prompt and uploads.
815-
const metadata = makeTurnCommandMetadata();
816-
const launched = await startLocalThread({
817-
environmentId: selectedThread.environmentId,
818-
input: buildProjectThreadStartTurnInput({
819-
...metadata,
820-
projectId: selectedThread.projectId,
821-
projectCwd: selectedThreadProject.workspaceRoot,
822-
text: setupMessage.text,
823-
...(setupMessage.context ? { context: setupMessage.context } : {}),
824-
uploadedAttachments: setupMessage.attachments,
825-
modelSelection: selectedThread.modelSelection,
826-
runtimeMode: selectedThread.runtimeMode,
827-
interactionMode: selectedThread.interactionMode,
828-
workspaceMode: "local",
829-
branch: null,
830-
worktreePath: null,
831-
startFromOrigin: false,
832-
worktreeBranchName: "",
833-
}),
834-
});
835-
if (launched._tag !== "Success") return;
836-
navigation.dispatch(
837-
StackActions.replace("Thread", {
838-
environmentId: String(selectedThread.environmentId),
839-
threadId: metadata.threadId,
840-
}),
841-
);
842-
})().finally(() => {
791+
const result = await cancelWorktreeSetup({
792+
environmentId: selectedThread.environmentId,
793+
input: { threadId: selectedThread.id },
794+
});
795+
if (result._tag !== "Success" || !result.value.cancelled) {
843796
localResendBusy.current = false;
797+
return;
798+
}
799+
// V2 accepts the launch before setup runs, so cancellation never rejects
800+
// the original outbox delivery. Reuse the server-owned prompt and uploads.
801+
const metadata = makeTurnCommandMetadata();
802+
const launched = await startLocalThread({
803+
environmentId: selectedThread.environmentId,
804+
input: buildProjectThreadStartTurnInput({
805+
...metadata,
806+
projectId: selectedThread.projectId,
807+
projectCwd: selectedThreadProject.workspaceRoot,
808+
text: setupMessage.text,
809+
...(setupMessage.context ? { context: setupMessage.context } : {}),
810+
uploadedAttachments: setupMessage.attachments,
811+
modelSelection: selectedThread.modelSelection,
812+
runtimeMode: selectedThread.runtimeMode,
813+
interactionMode: selectedThread.interactionMode,
814+
workspaceMode: "local",
815+
branch: null,
816+
worktreePath: null,
817+
startFromOrigin: false,
818+
worktreeBranchName: "",
819+
}),
844820
});
821+
localResendBusy.current = false;
822+
if (launched._tag !== "Success") return;
823+
navigation.dispatch(
824+
StackActions.replace("Thread", {
825+
environmentId: String(selectedThread.environmentId),
826+
threadId: metadata.threadId,
827+
}),
828+
);
845829
};
846830
const creationState = ((): ThreadDetailScreenProps["creationState"] => {
847831
if (selectedThreadCreation === null) {

‎apps/mobile/src/state/use-thread-composer-state.ts‎

Lines changed: 48 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ import {
8989
} from "@t3tools/client-runtime/state/composer-dispatch";
9090
import { Atom } from "effect/reactivity";
9191
import { AsyncResult } from "effect/reactivity";
92+
import { settlePromise, squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
9293
import { prepareTurnAttachments } from "../lib/attachmentUpload";
9394
import { DEFAULT_FOLLOW_UP_BEHAVIOR } from "../lib/followUpBehavior";
9495
import { mobilePreferencesAtom } from "./preferences";
@@ -449,46 +450,59 @@ export function useThreadComposerState() {
449450
}
450451
savingQueuedEditRef.current = true;
451452
setIsSavingQueuedEdit(true);
452-
return await (async () => {
453-
const capabilities = selectedEnvironmentRuntime?.serverConfig?.environment.capabilities;
454-
const prepared = await prepareTurnAttachments({
453+
const capabilities = selectedEnvironmentRuntime?.serverConfig?.environment.capabilities;
454+
const prepared = await settlePromise(() =>
455+
prepareTurnAttachments({
455456
environmentId: thread.environmentId,
456457
attachments: draft.attachments,
457458
supportsImageUploads: capabilities?.attachmentUploads === true,
458-
});
459-
if (prepared.status !== "ready") return;
460-
const payload = resolveQueuedEditPayload({
461-
edit,
462-
draftContext: draft.context,
463-
draftAttachments: draft.attachments,
464-
uploaded: prepared.attachments,
465-
});
466-
const result = await editQueuedRun({
467-
environmentId: thread.environmentId,
468-
input: {
469-
threadId: thread.id,
470-
runId: edit.runId,
471-
text,
472-
edit: {
473-
messageId: edit.messageId,
474-
attachments: payload.attachments,
475-
...(payload.context ? { context: payload.context } : {}),
476-
},
477-
},
478-
});
479-
if (result._tag !== "Success") {
480-
Dialog.alert(
481-
"Could not save the queued message",
482-
"It may have already started. Your edit is still in the composer.",
483-
);
484-
return;
485-
}
486-
endQueuedRunEdit(threadKey, { deferAttachmentCleanup: true });
487-
scheduleUnusedComposerAttachmentCleanup(draft.attachments);
488-
})().finally(() => {
459+
}),
460+
);
461+
if (AsyncResult.isFailure(prepared)) {
462+
savingQueuedEditRef.current = false;
463+
setIsSavingQueuedEdit(false);
464+
const error = squashAtomCommandFailure(prepared);
465+
Dialog.alert(
466+
"Could not save the queued message",
467+
error instanceof Error ? error.message : "Your edit is still in the composer.",
468+
);
469+
return;
470+
}
471+
if (prepared.value.status !== "ready") {
489472
savingQueuedEditRef.current = false;
490473
setIsSavingQueuedEdit(false);
474+
return;
475+
}
476+
const payload = resolveQueuedEditPayload({
477+
edit,
478+
draftContext: draft.context,
479+
draftAttachments: draft.attachments,
480+
uploaded: prepared.value.attachments,
491481
});
482+
const result = await editQueuedRun({
483+
environmentId: thread.environmentId,
484+
input: {
485+
threadId: thread.id,
486+
runId: edit.runId,
487+
text,
488+
edit: {
489+
messageId: edit.messageId,
490+
attachments: payload.attachments,
491+
...(payload.context ? { context: payload.context } : {}),
492+
},
493+
},
494+
});
495+
savingQueuedEditRef.current = false;
496+
setIsSavingQueuedEdit(false);
497+
if (result._tag !== "Success") {
498+
Dialog.alert(
499+
"Could not save the queued message",
500+
"It may have already started. Your edit is still in the composer.",
501+
);
502+
return;
503+
}
504+
endQueuedRunEdit(threadKey, { deferAttachmentCleanup: true });
505+
scheduleUnusedComposerAttachmentCleanup(draft.attachments);
492506
};
493507

494508
const onSendMessage = async (followUpOverride?: ActiveTurnComposerAction) => {

0 commit comments

Comments
 (0)